diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8592795b205902c3717e44dc7776da67ec690148 Binary files /dev/null and b/.DS_Store differ diff --git a/spoj_cpp_clean/ABA12A/ABA12A-13.cpp b/spoj_cpp_clean/ABA12A/ABA12A-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96c12c362ded27a632dfd9c515ccb3dc90e8c744 --- /dev/null +++ b/spoj_cpp_clean/ABA12A/ABA12A-13.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; + +int main(){ + int T; scanf("%d", &T); + for(int t=0; t < T; ++t){ + i64 a, b; scanf("%lld %lld", &a, &b); + printf("%lld\n",b); + } + return 0; +} diff --git a/spoj_cpp_clean/ABA12C/ABA12C-13.cpp b/spoj_cpp_clean/ABA12C/ABA12C-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a055d378fb130c19256203f53add7b089275a381 --- /dev/null +++ b/spoj_cpp_clean/ABA12C/ABA12C-13.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; +const int INF=0x3f3f3f3f; + +int memo[105][105][105]; +bool mark[105][105][105]; + +int f(vector &v, int n, int i, int j, int w){ + if(i >= (int)v.size()){ + if(j <= n && w == 0)return 0; + else return INF; + } + + int &best = memo[i][j][w]; + if(mark[i][j][w])return best; + mark[i][j][w] = true; + + best = f(v,n,i+1,j,w); + if(v[i] != -1){ + for(int next=1; next + j <= n; ++next){ + int nextW = w - (next * (i+1)); + if(nextW >= 0) + best = min(best, f(v,n,i+1,next+j,nextW) + (next*v[i])); + } + } + + return best; +} + +int main(){ + //freopen("data.in", "r", stdin); + int T; scanf("%d", &T); + for(int t=0; t < T; ++t){ + int n, k; scanf("%d %d", &n, &k); + vector v(k); + for(int i=0; i= INF)res = -1; + printf("%d\n",res); + } + return 0; +} diff --git a/spoj_cpp_clean/ABA12C/ABA12C-8.cpp b/spoj_cpp_clean/ABA12C/ABA12C-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e02011840e80a97f4a41d5e3a5b30741e1a3859 --- /dev/null +++ b/spoj_cpp_clean/ABA12C/ABA12C-8.cpp @@ -0,0 +1,157 @@ +/*************************************************** + * Problem Name : ABA12C - Buying Apples!.cpp + * Problem Link : https://www.spoj.com/problems/ABA12C/ + * OJ : Spoj + * Verdict : AC + * Date : 2020-02-19 + * Problem Type : DP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +const int MAX_N = 105; + +int n, k; +ll ar[MAX_N]; +ll dp[MAX_N][MAX_N][MAX_N]; + +ll fun (int pos, int kinche, int koto) { + if (pos > k) { + if (koto == k) { + return 0LL; + + } else { + return INF; + } + } + + ll &res = dp[pos][kinche][koto]; + + if (res != -1) return res; + + ll res1 = INF, res2 = INF; + + if (ar[pos] != -1) { + if (kinche < n && koto + pos <= k) { + res1 = ar[pos] + fun (pos, kinche + 1, koto + pos); + } + } + + res2 = fun (pos + 1, kinche, koto); + return res = min (res1, res2); +} + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + int tc; + cin >> tc; + + while (tc--) { + cin >> n >> k; + + for (int i = 1; i <= k; i++) { + cin >> ar[i]; + } + + SET (dp, -1); + ll res = fun (1, 0, 0); + + if (res == INF) { + cout << -1 << "\n"; + + } else { + cout << res << "\n"; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/ABA12D/ABA12D-13.cpp b/spoj_cpp_clean/ABA12D/ABA12D-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c4d7d1bf5dd3b9c34bb289c6ce560c6e9b83b25 --- /dev/null +++ b/spoj_cpp_clean/ABA12D/ABA12D-13.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; + +int v[] = {2, 4, 9, 16, 25, 64, 289, 729, 1681, 2401, 3481, 4096, 5041, 7921, 10201, 15625, 17161, 27889, 28561, 29929, 65536, 83521, 85849, 146689, 262144, 279841, 458329, 491401, 531441, 552049, 579121, 597529, 683929, 703921, 707281, 734449, 829921}; + +int main(){ + //freopen("data.in", "r", stdin); + int T; scanf("%d", &T); + for(int t=0; t= a && v[i] <= b)++res; + printf("%d\n",res); + } + return 0; +} diff --git a/spoj_cpp_clean/ABA12E/ABA12E-13.cpp b/spoj_cpp_clean/ABA12E/ABA12E-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34ef2e44c8e3a8e1f5d8118d78cde8f5be4851ec --- /dev/null +++ b/spoj_cpp_clean/ABA12E/ABA12E-13.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; + + + +//calculate how many sums are below sum +int g(vector &v, int n, i64 sum){ + int res = 0; + for(int i=1; i &v, int n, int k){ + i64 low = 1LL, high = v[n-1], mid; + //printf("DEBUG: %lld\n",high); + while(low < high){ + mid = (low + high)/2LL; + //printf("DEBUG: low=%lld mid=%lld high=%lld\n", low,mid,high); + int w = g(v,n, mid); + //printf("DEBUG: w=%d\n",w); + if(w < k)low = mid+1; + else high = mid; + } + return low; +} + +int main(){ + //freopen("data_large.in", "r", stdin); + int n, k; scanf("%d %d", &n, &k); + vector v(n+1); + i64 high = 0LL; + for(int i=1; i<=n; ++i){ + scanf("%lld", &v[i]); + v[i] += v[i-1]; + } + i64 res = f(v, n+1, k); + printf("%lld\n",res); + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ABCDEF/ABCDEF-9.cpp b/spoj_cpp_clean/ABCDEF/ABCDEF-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58bba029294efb2cd18da364621446d909aa2aa7 --- /dev/null +++ b/spoj_cpp_clean/ABCDEF/ABCDEF-9.cpp @@ -0,0 +1,51 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + int a[n]; + vector v1, v2; + for(int i = 0; i < n; i++) + cin >> a[i]; + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + for(int k = 0; k < n; k++) + v1.push_back(a[i]*a[j]+a[k]); + + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + for(int k = 0; k < n; k++) { + if(a[i]) + v2.push_back(a[i]*(a[j]+a[k])); + } + + sort(v1.begin(), v1.end()); + sort(v2.begin(), v2.end()); + + long long count = 0; + pair::iterator, vector::iterator> bounds; + for(vector::iterator it = v1.begin(); it != v1.end(); it++) { + bounds = equal_range(v2.begin(), v2.end(), *it); + count += bounds.second-bounds.first; + } + cout << count << endl; + + return 0; +} diff --git a/spoj_cpp_clean/ABCPATH/ABCPATH-13.cpp b/spoj_cpp_clean/ABCPATH/ABCPATH-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..adc242b49c95c9b63abf65c9d2d115934824b0f1 --- /dev/null +++ b/spoj_cpp_clean/ABCPATH/ABCPATH-13.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; + +char grid[60][60]; +bool mark[60][60]; + +struct state_t{ + int i, j, k; + char x; + state_t(int _i, int _j, int _k, char _x): + i(_i), j(_j),k(_k), x(_x){} +}; + +int movi[8] = {-1,-1,-1,0,0,1,1,1}; +int movj[8] = {-1,0,1,-1,1,-1,0,1}; + +int f(int i, int j, int w, int h){ + memset(mark, false, sizeof(mark)); + queue q; + q.push(state_t(i,j,1,'A')); + mark[i][j] = true; + int best = 1; + while(!q.empty()){ + state_t st = q.front(); q.pop(); + best = max(best, st.k); + if(st.x < 'Z'){ + for(int s=0; s<8; ++s){ + int ni = st.i + movi[s], nj = st.j+movj[s]; + if(ni >= 0 && ni < h && nj >= 0 && nj < w && !mark[ni][nj] && grid[ni][nj] == st.x + 1){ + q.push(state_t(ni,nj,st.k+1,st.x+1)); + mark[ni][nj] = true; + } + } + } + } + return best; +} + +int main(){ + //freopen("data.in", "r", stdin); + int w, h, t=1; + while(scanf("%d %d", &h, &w)==2 && (w || h)){ + //printf("%d %d\n",h,w); + for(int i=0; i + +using namespace std; + +char a[51][51]; +int dp[51][51]={}; + +int h,w; +int dr[8] = {-1,-1,-1,0,0,1,1,1,}; +int dc[8] = {-1,1,0,-1,1,-1,1,0}; + +int dfs(int r, int c){ + if(dp[r][c]!=0) return dp[r][c]; + for(int i=0;i<8;i++){ + if(r+dr[i]<0||c+dc[i]<0||r+dr[i]>=h||c+dc[i]>=w) continue; + if((int)(a[r+dr[i]][c+dc[i]]-a[r][c])==1) dp[r][c]=max(dp[r][c], 1+dfs(r+dr[i],c+dc[i])); + } + return dp[r][c]; +} + +int main(){ + int t=1,ans=0,base=0; + cin>>h>>w; + while(h!=0&&w!=0){ + base=0; + ans=0; + memset(dp,0,sizeof(dp)); + for(int i=0;i>a[i][j]; + } + } + for(int i=0;i>h>>w; + } +} diff --git a/spoj_cpp_clean/ABSP1/ABSP1-3.cpp b/spoj_cpp_clean/ABSP1/ABSP1-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39b043ccff169673c9f344a90a9a7c8ddc4b0b9b --- /dev/null +++ b/spoj_cpp_clean/ABSP1/ABSP1-3.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, n, fact; + long long nums[10000], sum[10000], total; + sd(t); + while(t--){ + sd(n); + if(n == 0 or n == 1){ + printf("0\n"); + continue; + } + for(int i = 0; i < n; i++){ + scanf("%lld",&nums[i]); + } + sum[n-1] = nums[n-1]; + for(int i = n-2; i >= 0; i--){ + sum[i] = sum[i+1] + nums[i]; + } + total = 0; + for(int i = 0; i < n-1; i++){ + total += abs((n-i-1)*nums[i] - sum[i+1]); + } + printf("%lld\n",total); + } + return 0; +} diff --git a/spoj_cpp_clean/ABSYS/ABSYS-11.cpp b/spoj_cpp_clean/ABSYS/ABSYS-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59c895101c4505318b52c848d81d3067c477b0c1 --- /dev/null +++ b/spoj_cpp_clean/ABSYS/ABSYS-11.cpp @@ -0,0 +1,71 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include +#include + +using namespace std; + +bool isNumber(char *str) +{ + bool flag = true; + + for(int i = 0; str[i] != '\0'; i++) + { + if(str[i] < '0' || str[i] > '9') + { + flag = false; + break; + } + } + + return flag; +} + +int toNumber(char *str) +{ + int number; + + number = 0; + + for(int i = 0; str[i] != '\0'; i++) + number = (number * 10) + (str[i] - '0'); + + return number; +} + +int main(void) +{ + int t; + char number1[30], number2[30], sum[30]; + int n1, n2, summ; + char plus, equal; + + cin >> t; + + while(t--) + { + cin >> number1 >> plus >> number2 >> equal >> sum; + + n1 = n2 = summ = -1; + + if(isNumber(number1)) + n1 = toNumber(number1); + + if(isNumber(number2)) + n2 = toNumber(number2); + + if(isNumber(sum)) + summ = toNumber(sum); + + n1 = (n1 == -1) ? (summ - n2) : n1; + n2 = (n2 == -1) ? (summ - n1) : n2; + summ = (summ == -1) ? (n1 + n2) : summ; + + cout << n1 << " + " << n2 << " = " << summ << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ABSYS/ABSYS-16.cpp b/spoj_cpp_clean/ABSYS/ABSYS-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b21207f733bbc6aaa4094ab088318f0016cb5edc --- /dev/null +++ b/spoj_cpp_clean/ABSYS/ABSYS-16.cpp @@ -0,0 +1,40 @@ +#include +#define ll long long + +using namespace std; + +void solve(){ + vector s(5); + for(ll i=0;i<5;i++) cin>>s[i]; + if(s[0].find("machula")!=string::npos){ + ll x = stoi(s[2]); + ll y = stoi(s[4]); + cout<>t; + while(t--) + solve(); + return 0; +} diff --git a/spoj_cpp_clean/ABSYS/ABSYS-5.cpp b/spoj_cpp_clean/ABSYS/ABSYS-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ed5382c7b913ec1f30232319e112570e59d962d --- /dev/null +++ b/spoj_cpp_clean/ABSYS/ABSYS-5.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int test,num1,num2,num3,i; + char str1[100],str2[100],str3[100],op1[2],op2[2]; + cin>>test; + while(test--) + { + cin>>str1>>op1>>str2>>op2>>str3; + if(strchr(str1,'m')!=NULL) + { + num2=atoi(str2); + num3=atoi(str3); + num1=num3-num2; + } + else if(strchr(str2,'m')!=NULL) + { + num1=atoi(str1); + num3=atoi(str3); + num2=num3-num1; + } + else + { + num1=atoi(str1); + num2=atoi(str2); + num3=num1+num2; + } + cout<=0;i--)//reverse for + +using namespace std; + +int main() +{ + int t=1; + scanf("%d",&t); + while(t--) + { + char c; + int f1=0,f2=0,f3=0; + int n1=0,n2=0,n3=0; + int p=0; + scanf("\n%c",&c); + while(c!='\n') + { + if(p==0) + { + if(c>=48 && c<=57) + { + n1=n1*10 + (c-48); + } + else + { + p++; + if(c=='m') + f1=1; + while(c!='+') + scanf("%c",&c); + scanf("%c",&c); + } + } + else if(p==1) + { + if(c>=48 && c<=57) + { + n2=n2*10 + (c-48); + } + else + { + p++; + if(c=='m') + f2=1; + while(c!='=') + scanf("%c",&c); + scanf("%c",&c); + } + } + else if(p==2) + { + if(c>=48 && c<=57) + { + n3=n3*10 + (c-48); + } + else + { + p++; + if(c=='m') + f3=1; + } + } + scanf("%c",&c); + } + if(f1==1) + { + printf("%d + %d = %d\n",(n3-n2),n2,n3); + } + else if(f2==1) + { + printf("%d + %d = %d\n",n1,(n3-n1),n3); + } + else if(f3==1) + { + printf("%d + %d = %d\n",n1,n2,(n1+n2)); + } + } + return 0; +} diff --git a/spoj_cpp_clean/ABSYS/ABSYS-9.cpp b/spoj_cpp_clean/ABSYS/ABSYS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..542f22517fe1b75d59489ce3c65adf718a104aae --- /dev/null +++ b/spoj_cpp_clean/ABSYS/ABSYS-9.cpp @@ -0,0 +1,118 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar_unlocked() + +using namespace std; + +inline int first() +{ + string A; + char c; + + c = gc(); + while(isspace(c)) + c = gc(); + + while(c!=' ') + { + if(isalpha(c)) + { + while(c!=' ') + c = gc(); + return -1; + } + A.push_back(c); + c = gc(); + } + + return stoi(A); +} + +inline int second() +{ + string A; + char c; + + gc(); + gc(); + c = gc(); + + while(c!=' ') + { + if(isalpha(c)) + { + while(c!=' ') + c = gc(); + return -1; + } + A.push_back(c); + c = gc(); + } + + return stoi(A); +} + +inline int third() +{ + string A; + char c; + + gc(); + gc(); + c = gc(); + + while(c!='\n' && c!=EOF) + { + if(isalpha(c)) + { + while(c!='\n' && c!=EOF) + c = gc(); + return -1; + } + A.push_back(c); + c = gc(); + } + + return stoi(A); +} + +int main() +{ + int T; + int f, s, t; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + { + f = first(); + s = second(); + t = third(); + + if(f == -1) + f = t - s; + if(s == -1) + s = t - f; + if(t == -1) + t = f + s; + + printf("%d + %d = %d\n", f, s, t); + } + + return 0; +} diff --git a/spoj_cpp_clean/ACMAKER/ACMAKER-3.cpp b/spoj_cpp_clean/ACMAKER/ACMAKER-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8aa086b3b1793b3f676ad92cea82965bc3f3690d --- /dev/null +++ b/spoj_cpp_clean/ACMAKER/ACMAKER-3.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +// Uglyyyyyyyyyyyy + +int main(){ + int n, i, j, k, dp[151][151], count[151][151], w; + count[0][0] = 1; + for(i = 1; i <= 150; i++) + count[0][i] = 0; + for(i = 1; i <= 150; i++) + count[i][0] = 1; + string input, acronym, up_acronym, word; + set insig; + vector words; + sd(n); + while(n != 0){ + insig.clear(); + for(i = 0; i < n; i++){ + cin>>word; + insig.insert(word); + } + getline(cin,input); + while(getline(cin,input) and input != "LAST CASE"){ + words.clear(); + stringstream ss; + + ss<>up_acronym; + acronym = ""; + for(i = 0; i < up_acronym.length(); i++) acronym += tolower(up_acronym[i]); + + + while(ss>>word){ + if(insig.find(word) == insig.end()){ + words.push_back(word); + } + } + + int l1 = words.size(), l2 = acronym.length() ; + + for(i = 0; i <= l1; i++) + for(j = 0; j <= l2; j++) + dp[i][j] = 0; + + dp[0][0] = 1; + + for(i = 0; i < l1; i++){ + for(j = 1; j <= l2; j++){ + for(k = 1; k <= l2 - j + 1; k++){ + + for(int p = 1; p <= words[i].length(); p++){ + for(int q = 1; q <= k; q++){ + count[p][q] = count[p-1][q]; + if(words[i][p-1] == acronym[j+q-2]) count[p][q] += count[p-1][q-1]; + } + } + w = count[words[i].length()][k]; + dp[i+1][j+k-1] += w * dp[i][j-1]; + } + } + } + if(dp[l1][l2]){ + cout< +#include +#include +#include +#define MAX_SIZE 5010 + +char *input; +int size; + +unsigned long long cache[MAX_SIZE]; + +unsigned long long rec(int i) { + // std::cout << "i = " << i << std::endl; + if (i >= size - 1) { + if (i == size - 1 && input[i] == '0') return -1; + return 0; + } + if (cache[i] != -1) return cache[i]; + + unsigned long long ans; + switch (input[i]) { + case '0': + ans = - 1; + break; + case '2': + if (input[i+1] > '6') { + ans = rec(i + 1); + break; + } + case '1': + ans = 1 + rec(i + 1) + rec(i + 2); + break; + default: + ans = rec(i + 1); + break; + } + + return cache[i] = ans; +} + +int main() { + size_t max_size = MAX_SIZE; + input = (char*) malloc(sizeof(char) * (MAX_SIZE + 1)); + + while (true) { + size = getline(&input, &max_size, stdin); + --size; + input[size] = '\0'; + // std::cout << "'" << input << "' " << size << " size" << std::endl; + + memset(cache, -1, sizeof(unsigned long long) * MAX_SIZE); + + if (input[0] == '0') break; + + std::cout << 1 + rec(0) << std::endl; + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/ACODE/ACODE-11.cpp b/spoj_cpp_clean/ACODE/ACODE-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ef49a353659ef06c6c3297a79a906c174c5d83c --- /dev/null +++ b/spoj_cpp_clean/ACODE/ACODE-11.cpp @@ -0,0 +1,58 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include +#include +#include + +using namespace std; + +string input; +map memory; +map::iterator it; +int len; + +unsigned long long findNumDecoding(int index) +{ + if(index == len || index == len-1) + return 1ull; + + it = memory.find(index); + + if(it != memory.end()) + return (it->second); + + unsigned long long result; + + if(input[index] > '2' || (input[index] == '2' && input[index+1] > '6') || (index+2 < len && input[index+2] == '0')) + result = findNumDecoding(index+1); + else if(input[index+1] == '0') + result = findNumDecoding(index+2); + else + result = (findNumDecoding(index+1) + findNumDecoding(index+2)); + + memory.insert(make_pair(index, result)); + + return result; +} + +int main(void) +{ + cin >> input; + + while(input != "0") + { + len = input.length(); + + cout << findNumDecoding(0) << endl; + + input.clear(); + memory.clear(); + + cin >> input; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ACODE/ACODE-13.cpp b/spoj_cpp_clean/ACODE/ACODE-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9bd9b94db9fe6de4e93fed43712920cb5b883fc4 --- /dev/null +++ b/spoj_cpp_clean/ACODE/ACODE-13.cpp @@ -0,0 +1,61 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +i64 memo[5001]; +bool mark[5001]; +char buffer[5001]; +int n; + +i64 f(int k){ + if(k >= n)return 1; + if(buffer[k] == '0')return 0; + i64 &count = memo[k]; + if(mark[k])return count; + mark[k] = true; + count = f(k+1); + if((buffer[k] == '1' || buffer[k] == '2') && k+2 <= n) + count += f(k+2); + return count; +} + +int main(){ + freopen("data.in","r",stdin); + while(scanf("%s", buffer) == 1 && buffer[0] != '0'){ + n = strlen(buffer); + memset(mark,false,sizeof(mark)); + i64 res = f(0); + printf("%lld\n",res); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ACODE/ACODE-16.cpp b/spoj_cpp_clean/ACODE/ACODE-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d0646f2186224ae7de4b51b96e199a0f056c0cd --- /dev/null +++ b/spoj_cpp_clean/ACODE/ACODE-16.cpp @@ -0,0 +1,38 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define ll long long + +using namespace std; + +void solve(string a){ + ll n = a.length(); + vector dp(n,0); + dp[0]=1; + + ll num = (a[0]-'0')*10 + (a[1]-'0'); + if(num<=26) dp[1]=2; + else dp[1]=1; + + for(int i=2;i>x; + if(x=="0") break; + solve(x); + } + return 0; +} diff --git a/spoj_cpp_clean/ACODE/ACODE-19.cpp b/spoj_cpp_clean/ACODE/ACODE-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edfbc6832130c431e1f522ebb1916931b9bc51a7 --- /dev/null +++ b/spoj_cpp_clean/ACODE/ACODE-19.cpp @@ -0,0 +1,63 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 1000001 +#define PI 3.14159265 + +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + + //ll t;cin>>t; + ll dp[5001]; + char num[5001]; + while (true) { + cin >> num; + if (num[0] == '0') { + return 0; + } + int l = strlen(num); + dp[0] = 1; + if (l == 1) { + cout << "1" << endl; + continue; + } + dp[1] = 1; + int c1, c2; + c2 = num[0] - '0'; + c1 = num[1] - '0'; + if (c1 == 0) { + if (c2 == 1 || c2 == 2) { + dp[1] = 1; + } + else { + dp[1] = 0; + } + } + else { + if (c2 == 1 || (c2 == 2 && c1 <= 6)) { + dp[1] = 2; + } + else { + dp[1] = 1; + } + } + for (int i = 2; i < l; i++) { + dp[i] = 0; + c1 = num[i] - '0'; + c2 = num[i - 1] - '0'; + if (c1 != 0) { + dp[i] += dp[i - 1]; + } + if (c2 == 1 || (c2 == 2 && c1 < 7)) { + dp[i] += dp[i - 2]; + } + } + cout << dp[l - 1] << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ACODE/ACODE-5.cpp b/spoj_cpp_clean/ACODE/ACODE-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f77c64f6632f4816d07d02c2a270caf80d8ba8d --- /dev/null +++ b/spoj_cpp_clean/ACODE/ACODE-5.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +// Evaluate for "226210", other than that the code is self explanatory +using namespace std; + +mapdp; + +int main() +{ + string input; + int i,strLength; + cin>>input; + + while(input[0]!='0') + { + strLength=input.length(); + dp[0]=1; + dp[1]=1; + for(i=1;i>input; + } + return 0; +} diff --git a/spoj_cpp_clean/ACODE/ACODE-8.cpp b/spoj_cpp_clean/ACODE/ACODE-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..185078dd5371f5928506391c75fd2eb08e473837 --- /dev/null +++ b/spoj_cpp_clean/ACODE/ACODE-8.cpp @@ -0,0 +1,152 @@ +/*************************************************** + * Problem Name : ACODE - Alphacode.cpp + * Problem Link : https://www.spoj.com/problems/ACODE/ + * OJ : Spoj + * Verdict : AC + * Date : 2020-02-19 + * Problem Type : DP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +const int MAX_N = 5005; + +string str; +ll dp[MAX_N][105]; + +ll fun (int pos, int x) { + if (pos >= (int) str.size() ) { + if (x <= 26 && x > 0) return 1LL; + else return 0LL; + } + + ll &res = dp[pos][x]; + + if (res != -1) return res; + + ll res1 = 0, res2 = 0; + + if (str[pos] != '0') { + if (pos + 1 < (int) str.size() ) { + if (str[pos + 1] != '0') { + res1 = fun (pos + 1, str[pos] - '0'); + int val = (str[pos] - '0') * 10 + (str[pos + 1] - '0'); + + if (val <= 26) res2 = fun (pos + 2, val); + + } else { + int val = (str[pos] - '0') * 10 + (str[pos + 1] - '0'); + + if (val <= 26) res2 = fun (pos + 2, val); + } + + } else { + res1 = fun (pos + 1, str[pos] - '0'); + } + } + + return res = res1 + res2; +} + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + while (cin >> str) { + if (str == "0") break; + + SET (dp, -1); + ll res = fun (0, 0); + cout << res << "\n"; + } + + return 0; +} +//1013 diff --git a/spoj_cpp_clean/ACODE/ACODE-9.cpp b/spoj_cpp_clean/ACODE/ACODE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38885d14cabe7421355a5ccdbbe843ab03a0b5c2 --- /dev/null +++ b/spoj_cpp_clean/ACODE/ACODE-9.cpp @@ -0,0 +1,77 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +char str[50000]; +unsigned long long DP[50000]; + +bool init() +{ + scanf("%s", str); + + if(strcmp(str, "0")==0) + return 0; + + memset(DP, 0, sizeof(DP)); + + return 1; +} + + +inline bool valid(int i, int j) +{ + if(i==j) + { + if(str[i]=='0') + return 0; + return 1; + } + + if(str[i]=='0') + return 0; + + if((str[i]-'0')*10 + (str[j]-'0') <=26) + return 1; + + return 0; +} + +int main() +{ + int i; + + //freopen("input.txt","r",stdin); + + while(init()) + { + DP[0]=1; + + if(valid(1, 1)) + DP[1]+=DP[0]; + if(valid(0, 1)) + DP[1]++; + + for(i=2; i +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; +const int INF = 0x3f3f3f3f; + +int costs[100002][3]; +int memo[100002][3]; +bool mark[100002][3]; + +int f(int n, int i, int j){ + if(i == n-1 && j == 1)return costs[i][j]; + int &best = memo[i][j]; + if(mark[i][j])return best; + mark[i][j] = true; + best = INF; + + //trans + if(j == 0){ + best = min (best, f(n,i,j+1) + costs[i][j]); + if(i+1 < n){ + best = min(best, f(n, i+1, j) + costs[i][j]); + best = min(best, f(n, i+1, j+1) + costs[i][j]); + } + }else if(j == 1){ + best = min(best, f(n,i,j+1) + costs[i][j]); + if(i+1 < n){ + best = min(best, f(n, i+1, j-1) + costs[i][j]); + best = min(best, f(n, i+1, j) + costs[i][j]); + best = min(best, f(n, i+1, j+1) + costs[i][j]); + } + }else{ + if(i+1 < n){ + best = min(best, f(n, i+1, j-1)+ costs[i][j]); + best = min(best, f(n, i+1, j) + costs[i][j]); + } + } + return best; +} + +int main(){ + //freopen("data.in", "r", stdin); + int n; + for(int k=1; scanf("%d", &n)==1 && n; ++k){ + for(int i=0; i + +using namespace std; + +int main(void) +{ + int a1, a2, a3; + + cin >> a1 >> a2 >> a3; + + while(a1 != 0 || a2 != 0 || a3 != 0) + { + if((a3 - a2) == (a2 - a1)) + cout << "AP " << (a3 + (a2-a1)) << endl; + else + cout << "GP " << (a3 * (a2/a1)) << endl; + + cin >> a1 >> a2 >> a3; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ACPC10A/ACPC10A-13.cpp b/spoj_cpp_clean/ACPC10A/ACPC10A-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c29d26407480614f6ae805aad903621bb314bedb --- /dev/null +++ b/spoj_cpp_clean/ACPC10A/ACPC10A-13.cpp @@ -0,0 +1,48 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +int main(){ + freopen("data.in","r",stdin); + int a, b, c; + while(scanf("%d %d %d", &a, &b, &c)==3 &&(a||b||c)){ + int rate = b - a; + if(b + rate == c){ + printf("AP %d\n", c+rate); + }else{ + rate = b/a; + printf("GP %d\n",c*rate); + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ACPC10A/ACPC10A-16.cpp b/spoj_cpp_clean/ACPC10A/ACPC10A-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f635925e696270dec2c580e361e01a86d60726f --- /dev/null +++ b/spoj_cpp_clean/ACPC10A/ACPC10A-16.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#define ld long double +#define m 100000007 +using namespace std; +void check(ld a1,ld a2,ld a3){ + if(2*a2==(a1+a3)&&(a1!=0)&&(a2!=0)&&(a3!=0)) + cout<<"AP"<<" "<>a1>>a2>>a3; + while(a1!=0 || a2!=0 || a3!=0){ + check(a1,a2,a3); + cin>>a1>>a2>>a3; + } + + return 0; diff --git a/spoj_cpp_clean/ACPC10A/ACPC10A-19.cpp b/spoj_cpp_clean/ACPC10A/ACPC10A-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..449fee3bc7e16d8114108ad59c9505e98905cbad --- /dev/null +++ b/spoj_cpp_clean/ACPC10A/ACPC10A-19.cpp @@ -0,0 +1,83 @@ +#include +#define ll long long +#define endl "\n" +#define MAX 100001 +#define mod 1000000007 +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + while(true){ + int x,y,z; + cin>>x>>y>>z; + if(x==0 && y==0 && z==0){ + break; + } + else{ + cout<0 && y>0){ + if(y + +using namespace std; + +int main() +{ + int a,b,c,d; + cin>>a>>b>>c; + while(a!=0||b!=0||c!=0) + { + d=b-a; + if(d==(c-b)) + { + cout<<"AP "<<(c+d)<>a>>b>>c; + } + return 0; +} diff --git a/spoj_cpp_clean/ACPC10A/ACPC10A-9.cpp b/spoj_cpp_clean/ACPC10A/ACPC10A-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..874341ad94b5d49eefb8144a32e734c80096d1c3 --- /dev/null +++ b/spoj_cpp_clean/ACPC10A/ACPC10A-9.cpp @@ -0,0 +1,38 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +int main() +{ + int l,m,n; + + scanf("%d",&l); + scanf("%d",&m); + scanf("%d",&n); + + while(1) + { + if(m==0&&n==0&&l==0) + break; + + else if((2*m)==(n+l)) + printf("AP %d\n",(n+(m-l))); + + else + printf("GP %d\n",(n*(m/l))); + + scanf("%d",&l); + scanf("%d",&m); + scanf("%d",&n); + } + return 0; +} diff --git a/spoj_cpp_clean/ACPC10D/ACPC10D-9.cpp b/spoj_cpp_clean/ACPC10D/ACPC10D-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a823f1501a7f72908417596d5535760f605627e --- /dev/null +++ b/spoj_cpp_clean/ACPC10D/ACPC10D-9.cpp @@ -0,0 +1,89 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#define MAX 1000005 +typedef long long int LL; +LL dp[MAX][4]; +LL arr[MAX][4]; +LL N,M; +FILE* out; +bool inbounds(int i,int j) +{ + if(i>=0 && i<=N && j>=0 && j<=M) + return true; + return false; +} +LL min(LL a,LL b) +{ + if(a>b) + return b; + return a; +} +void solve(int index) +{ + for(int i=0;i +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define all(v) v.begin(), v.end() +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; ++i) +#define for0(i, stop) for(ll i = 0; i < stop; ++i) +#define rep1(i, start) for(ll i = start; i >= 1; --i) +#define rep0(i, start) for(ll i = (start-1); i >= 0; --i) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define EL '\n' +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +const ll sz = 5e4 + 10; + +pii ara[sz], lst[sz]; +ll pnt[sz], dp[sz]; + +struct Line { + ll m, c; +} tree[4*sz]; + +bool exist[4*sz]; + +inline ll f(Line &line, ll x) { + return line.m*x + line.c; +} + +void add(ll lo, ll hi, Line line, ll node) +{ + exist[node] = 1; + + if(lo == hi) { + if(f(line, pnt[lo]) < f(tree[node], pnt[lo])) + tree[node] = line; + return; + } + + ll mid = lo+hi >> 1; + + bool l = f(line, pnt[lo]) < f(tree[node], pnt[lo]); + bool m = f(line, pnt[mid]) < f(tree[node], pnt[mid]); + + if(m) swap(tree[node], line); + + if(l != m) add(lo, mid, line, node<<1); + else add(mid +1, hi, line, node<<1|1); +} + +ll query(ll lo, ll hi, ll idx, ll node) +{ + if(lo == hi) + return f(tree[node], pnt[idx]); + + ll mid = lo+hi >> 1, ret = f(tree[node], pnt[idx]); + + if(idx <= mid && exist[node<<1]) ret = min(ret, query(lo, mid, idx, node<<1)); + else if(idx > mid && exist[node<<1|1]) ret = min(ret, query(mid+1, hi, idx, node<<1|1)); + + return ret; +} + +int main() +{ + ll n; + cin >> n; + + for1(i, n) { + si(ara[i].first), si(ara[i].second); + } + sort(ara+1, ara+n+1); + + ll mxw = 0, idx = 0; + rep1(i, n) { + if(ara[i].second > mxw) + lst[++idx] = ara[i]; + + mxw = max(mxw, (ll)ara[i].second); + } + reverse(lst+1, lst+idx+1); + + for1(i, idx) pnt[i] = lst[i].first; + for0(i, 4*sz) tree[i] = {0, inf}; + + for1(i, idx) { + + add(1, idx, {lst[i].second, dp[i-1]}, 1); + + dp[i] = query(1, idx, i, 1); + } + + cout << dp[idx] << EL; + + return 0; diff --git a/spoj_cpp_clean/ACTIV/ACTIV-14.cpp b/spoj_cpp_clean/ACTIV/ACTIV-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1eabda51866ea70b58aafd4a9184c62a46468ccc --- /dev/null +++ b/spoj_cpp_clean/ACTIV/ACTIV-14.cpp @@ -0,0 +1,83 @@ +#include +using namespace std; + +#define ll long long +#define mod 100000000 + +ll dp[100009]; + +struct node { + ll l, r; +} ara[100009]; + +bool operator <(node a, node b) + +{ + if(a.l != b.l) + return a.l < b.l; + return a.r < b.r; +} + +ll n; + +ll solve(ll i) + +{ + if(i > n) + return 0; + + if(dp[i] != -1) + return dp[i]; + + ll ret = solve(i + 1) % mod; + node tmp = ara[i]; + ll pos = n + 1, lo = i + 1, hi = n; + + while(lo <= hi) { + ll mid = (lo + hi) / 2; + + if(ara[mid].l < ara[i].r) + lo = mid + 1; + else { + pos = mid; + hi = mid - 1; + } + } + + //cout << ara[i].r << " " << ara[pos].r << endl; + ret = (ret + 1 + solve(pos)) % mod; + + return dp[i] = ret; +} + +int main() + +{ + while(scanf("%lld", &n) && n != -1) { + + memset(dp, -1, sizeof(dp)); + + for(ll i = 1; i <= n; i++) + scanf("%lld %lld", &ara[i].l, &ara[i].r); + + sort(ara + 1, ara + n + 1); + + ll ans = solve(1); + + ll num = ans, cnt = 0; + if(num == 0) + cnt++; + while(num != 0) { + num /= 10; + cnt++; + } + + ll rem = 8 - cnt; + for(ll i = 1; i <= rem; i++) + printf("0"); + + printf("%lld\n", ans); + } + + return 0; +} diff --git a/spoj_cpp_clean/ADABRANC/ADABRANC-14.cpp b/spoj_cpp_clean/ADABRANC/ADABRANC-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db9eaf4cff0ba2608f249d30da91b04ad867286a --- /dev/null +++ b/spoj_cpp_clean/ADABRANC/ADABRANC-14.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; + +#define ll long long +#define pb push_back + +struct node { + ll a, b, c; + node(ll _a, ll _b, ll _c) { + a = _a; + b = _b; + c = _c; + } +}; + +bool operator <(node a, node b) { + return a.c > b.c; +} +vector edge; + +struct quer { + ll a, w, i; + quer(ll _a, ll _w, ll _i) { + a = _a; + w = _w; + i = _i; + } +}; + +bool operator <(quer a, quer b) { + return a.w > b.w; +} + +vector qry; + +ll ans[300009], par[200009], sz[200009]; + +ll findr(ll u) + +{ + if(par[u] == u) + return u; + return par[u] = findr(par[u]); +} + +int main() + +{ + ll n, m, q; + cin >> n >> m >> q; + + for(ll i = 0; i < n; i++) { + par[i] = i; + sz[i] = 1; + } + + for(ll i = 1; i <= m; i++) { + ll a, b, c; + scanf("%lld %lld %lld", &a, &b, &c); + edge.push_back( node(a, b, c) ); + } + + sort(edge.begin(), edge.end()); + + for(ll i = 1; i <= q; i++) { + ll a, c; + scanf("%lld %lld", &a, &c, &i); + qry.pb( quer(a, c, i) ); + } + sort(qry.begin(), qry.end()); + + ll j = 0; + for(ll i = 0; i < q; i++) { + + for(; j < edge.size(); j++) { + if(edge[j].c < qry[i].w) + break; + + ll u = findr(edge[j].a); + ll v = findr(edge[j].b); + + if(u == v) + continue; + + par[v] = u; + sz[u] += sz[v]; + } + + ans[ qry[i].i ] = sz[ findr(qry[i].a) ]; + } + + for(ll i = 1; i <= q; i++) + printf("%lld\n", ans[i]); + + return 0; +} diff --git a/spoj_cpp_clean/ADACOINS/ADACOINS-14.cpp b/spoj_cpp_clean/ADACOINS/ADACOINS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88cdf3771e1a0fa281542203b3cb9919ab8ff02c --- /dev/null +++ b/spoj_cpp_clean/ADACOINS/ADACOINS-14.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +#define ll long long + +bitset <100009> posSum; +ll cumsum[100009]; + +int main() + +{ + ll n, q; + cin >> n >> q; + + posSum[0] = 1; + for(ll i = 1; i <= n; i++) { + ll in; + scanf("%lld", &in); + posSum |= (posSum << in); + } + + for(ll i = 1; i <= 100000; i++) + cumsum[i] = posSum[i] + cumsum[i - 1]; + + for(ll i = 1; i <= q; i++) { + ll l, r; + scanf("%lld %lld", &l, &r); + printf("%lld\n", cumsum[r] - cumsum[l - 1]); + } + + return 0; +} diff --git a/spoj_cpp_clean/ADACYCLE/ADACYCLE-14.cpp b/spoj_cpp_clean/ADACYCLE/ADACYCLE-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cfb06c7a9ecc33093698260643e24ea88303c96 --- /dev/null +++ b/spoj_cpp_clean/ADACYCLE/ADACYCLE-14.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + +#define ll int +#define pii pair +const int inf = 1e9; +ll mat[2009][2009]; +vector g[2009]; +vector newinp[2009]; +ll dist[2009], ans[2009]; + +struct node { + ll u, c; +}; + +bool operator <(node a, node b) + +{ + return a.c > b.c; +} + +int main() + +{ + ll n; + scanf("%d", &n); + for(ll i = 1; i <= n; i++) { + for(ll j = 1; j <= n; j++) { + scanf("%d", &mat[i][j]); + if(mat[i][j]) { + newinp[i].push_back(pii(i, j)); + newinp[j].push_back(pii(i, j)); + g[i].push_back(j); + } + } + } + + for(ll i = 1; i <= n; i++) { + if(mat[i][i]) { + ans[i] = 1; + continue; + } + + for(ll j = 0; j <= 2005; j++) + dist[j] = inf; + + for(ll j = 0; j < newinp[i].size(); j++) { + ll u = newinp[i][j].first, v = newinp[i][j].second; + if(u == i) + g[2001].push_back(v); + else + g[u].push_back(2001); + } + + priority_queue pq; + pq.push({i, 0}); + dist[i] = 0; + while(!pq.empty()) { + node u = pq.top(); + pq.pop(); + + if(dist[u.u] != u.c) + continue; + for(ll v: g[u.u]) { + if(1 + u.c < dist[v]) { + dist[v] = 1 + u.c; + pq.push({v, dist[v]}); + } + } + } + if(dist[2001] != inf) + ans[i] = dist[2001]; + + g[2001].clear(); + for(ll j = 0; j < newinp[i].size(); j++) { + ll u = newinp[i][j].first, v = newinp[i][j].second; + if(u != i) { + ll last = g[u].size() - 1; + while(g[u][last] == 2001 && last >= 0) { + g[u].pop_back(); + last--; + } + } + } + } + + for(ll i = 1; i <= n; i++) { + if(ans[i] > 0) + printf("%d\n", ans[i]); + else + printf("NO WAY\n"); + } + + return 0; diff --git a/spoj_cpp_clean/ADADUNG/ADADUNG-9.cpp b/spoj_cpp_clean/ADADUNG/ADADUNG-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..273d9aa66ac86e7f2e20567137ff6e9df4b89ef5 --- /dev/null +++ b/spoj_cpp_clean/ADADUNG/ADADUNG-9.cpp @@ -0,0 +1,59 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define ll long long +#define MAXN 10000007 +using namespace std; + +const ll MOD = 1000000007; +ll memo[MAXN]; + +ll derangement(ll num) +{ + if (num <= 1) + return 0; + + if (num == 2) + return 1LL; + + if (memo[num]) + return memo[num]; + + ll temp = derangement(num - 1) + derangement(num - 2); + + temp %= MOD; + + memo[num] = (num - 1) * temp; + memo[num] %= MOD; + + return memo[num]; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + int T, N; + + cin >> T; + + while (T--) + { + cin >> N; + cout << derangement(N) << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/ADAFIELD/ADAFIELD-14.cpp b/spoj_cpp_clean/ADAFIELD/ADAFIELD-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2e94c4bb18a4b6a959f374a9e021659ad846b54 --- /dev/null +++ b/spoj_cpp_clean/ADAFIELD/ADAFIELD-14.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +#define ll long long + +int main() + +{ + ll t; + cin >> t; + while(t--) { + ll n, m, q, line, state, px, py, nx, ny, mx, mxd, mxx; + scanf("%lld %lld %lld", &n, &m, &q); + set x, y; + multiset dif, difx; + + set :: iterator it1, it2; + multiset :: iterator it3; + + x.insert(0), x.insert(n); + y.insert(0), y.insert(m); + dif.insert(m); + difx.insert(n); + + for(ll i = 1; i <= q; i++) { + scanf("%lld %lld", &state, &line); + if(state == 0) { + if( x.find(line) == x.end()) { + + it3 = x.lower_bound(line); + px = *(--it3); + + it3 = x.upper_bound(line); + nx = *it3; + + difx.erase( difx.find(nx - px) ); + difx.insert(line - px); + difx.insert(nx - line); + x.insert(line); + } + } + else if(y.find(line) == y.end()) { + + it3 = y.lower_bound(line); + py = *(--it3); + + it3 = y.upper_bound(line); + ny = *it3; + + //cout << ny << " " << py << " " << line << endl; + dif.erase( dif.find(ny - py) ); + dif.insert(line - py); + dif.insert(ny - line); + y.insert(line); + } + + mxd = *(--dif.end()); + mxx = *(--difx.end()); + mx = mxd * mxx; + + printf("%lld\n", mx); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/ADAGAME/ADAGAME-14.cpp b/spoj_cpp_clean/ADAGAME/ADAGAME-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b2b5157d2445a2969f336a2853deee52410d469 --- /dev/null +++ b/spoj_cpp_clean/ADAGAME/ADAGAME-14.cpp @@ -0,0 +1,117 @@ +#include +using namespace std; + +#define ll long long + +ll dp[10009][105][2], n, mv; + +ll solve(ll num, ll mov, ll state) + +{ + if(mov > mv) { + if(num > n) { + if(state == 0) + return 1; + else + return 0; + } + else { + if(state == 0) + return 0; + else + return 1; + } + } + + ll &ret = dp[num][mov][state]; + if(ret != -1) + return ret; + + ll rt = 0; + if(num > 999) { + if(num % 10 != 9) + rt |= !solve(num + 1, mov + 1, state ^ 1); + else + rt |= !solve(num - 9, mov + 1, state ^ 1); + + if(num % 100 < 90) + rt |= !solve(num + 10, mov + 1, state ^ 1); + else + rt |= !solve(num - 90, mov + 1, state ^ 1); + + if(num % 1000 < 900) + rt |= !solve(num + 100, mov + 1, state ^ 1); + else + rt |= !solve(num - 900, mov + 1, state ^ 1); + + if(num % 10000 < 9000) + rt |= !solve(num + 1000, mov + 1, state ^ 1); + else + rt |= !solve(num - 9000, mov + 1, state ^ 1); + } + else if(num > 99) { + if(num % 10 != 9) + rt |= !solve(num + 1, mov + 1, state ^ 1); + else + rt |= !solve(num - 9, mov + 1, state ^ 1); + + if(num % 100 < 90) + rt |= !solve(num + 10, mov + 1, state ^ 1); + else + rt |= !solve(num - 90, mov + 1, state ^ 1); + + if(num % 1000 < 900) + rt |= !solve(num + 100, mov + 1, state ^ 1); + else + rt |= !solve(num - 900, mov + 1, state ^ 1); + + rt |= !solve(num + 1000, mov + 1, state ^ 1); + } + + else if(num > 9) { + if(num % 10 != 9) + rt |= !solve(num + 1, mov + 1, state ^ 1); + else + rt |= !solve(num - 9, mov + 1, state ^ 1); + + if(num % 100 < 90) + rt |= !solve(num + 10, mov + 1, state ^ 1); + else + rt |= !solve(num - 90, mov + 1, state ^ 1); + + rt |= !solve(num + 100, mov + 1, state ^ 1); + rt |= !solve(num + 1000, mov + 1, state ^ 1); + } + + else { + if(num % 10 != 9) + rt |= !solve(num + 1, mov + 1, state ^ 1); + else + rt |= !solve(num - 9, mov + 1, state ^ 1); + + rt |= !solve(num + 10, mov + 1, state ^ 1); + rt |= !solve(num + 100, mov + 1, state ^ 1); + rt |= !solve(num + 1000, mov + 1, state ^ 1); + } + + //cout << num << " " << rt << " " << state << endl; + return ret = rt; +} + +int main() + +{ + ll t; + cin >> t; + while(t--) { + memset(dp, -1, sizeof(dp)); + scanf("%lld %lld", &n, &mv); + + if( solve(n, 1, 0) == 1) + printf("Ada\n"); + else + printf("Vinit\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/ADAGAME/ADAGAME-9.cpp b/spoj_cpp_clean/ADAGAME/ADAGAME-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc9206ddf133bbfc2c3fc4831de63bc359a16968 --- /dev/null +++ b/spoj_cpp_clean/ADAGAME/ADAGAME-9.cpp @@ -0,0 +1,111 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define gc() getchar() +using namespace std; + +short a, b, c, d; +int memo[10][10][10][10][107]; //0 = ada wins +int N; + +inline void init() +{ + a = gc() - '0'; + b = gc() - '0'; + c = gc() - '0'; + d = gc() - '0'; + + scanf("%d\n", &N); + + memset(memo, -1, sizeof(memo)); +} + +inline int inc(int num) +{ + if (num != 9) + return num + 1; + + return 0; +} + +bool solve(int aa, int bb, int cc, int dd, int n, bool player) //0 = ada, 1 = vinit +{ + if (memo[aa][bb][cc][dd][n] != -1) + return memo[aa][bb][cc][dd][n]; + + if (!n) + { + if (aa * 1000 + bb * 100 + cc * 10 + d > a * 1000 + b * 100 + c * 10 + d) + { + memo[aa][bb][cc][dd][0] = 0; + return 0; + } + + else + { + memo[aa][bb][cc][dd][0] = 1; + return 1; + } + } + + if (!player) // an pezei o ada + { + if (!solve(inc(aa), bb, cc, dd, n - 1, !player) || !solve(aa, inc(bb), cc, dd, n - 1, !player) || !solve(aa, bb, inc(cc), dd, n - 1, !player) || !solve(aa, bb, cc, inc(dd), n - 1, !player)) + { + memo[aa][bb][cc][dd][n] = 0; + return 0; + } + + else + { + memo[aa][bb][cc][dd][n] = 1; + return 1; + } + } + + else //an pezei o vinit + { + if (solve(inc(aa), bb, cc, dd, n - 1, !player) || solve(aa, inc(bb), cc, dd, n - 1, !player) || solve(aa, bb, inc(cc), dd, n - 1, !player) || solve(aa, bb, cc, inc(dd), n - 1, !player)) + { + memo[aa][bb][cc][dd][n] = 1; + return 1; + } + + else + { + memo[aa][bb][cc][dd][n] = 0; + return 0; + } + } +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + scanf("%d\n", &T); + + while (T--) + { + init(); + if (!solve(a, b, c, d, N, 0)) + printf("Ada\n"); + else + printf("Vinit\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/ADAGAME2/ADAGAME2-9.cpp b/spoj_cpp_clean/ADAGAME2/ADAGAME2-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa2cf71adb7b2b0671cf0788efaea708e31daf6f --- /dev/null +++ b/spoj_cpp_clean/ADAGAME2/ADAGAME2-9.cpp @@ -0,0 +1,107 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define gc() getchar_unlocked() +#define MAXN 100000 +#define MAXM 30 +using namespace std; + +int N, M; +int DP[MAXN + 7]; +int steps[MAXM + 7]; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +inline void init() +{ + int i; + + N = FAST_IO(); + M = FAST_IO(); + + for (i = 0; i < M; i++) + steps[i] = FAST_IO(); +} + +inline int grundy(int pos) +{ + int i; + bool a[50] = { 0 }; + + for (i = 0; i < M; i++) + if (steps[i] <= pos) + a[DP[pos - steps[i]]] = 1; + + for (i = 0; i < 50; i++) + if (!a[i]) + return i; + + return 0; +} + +inline void calc_dp() +{ + int i; + + for (i = 1; i <= MAXN; i++) + DP[i] = grundy(i); +} + +inline bool solve() +{ + int num, rval = 0; + + while (N--) + { + num = FAST_IO(); + rval ^= DP[num]; + } + + return rval; +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + T = FAST_IO(); + + while (T--) + { + init(); + calc_dp(); + if (!solve()) + printf("Vinit\n"); + else + printf("Ada\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/ADAINDEX/ADAINDEX-9.cpp b/spoj_cpp_clean/ADAINDEX/ADAINDEX-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47f2115d4aa68a6a2f6dcc4f7ad804346db65850 --- /dev/null +++ b/spoj_cpp_clean/ADAINDEX/ADAINDEX-9.cpp @@ -0,0 +1,136 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar() +using namespace std; + +struct ver +{ + int words; + int prefixes; + struct ver **edges; +}; + +void initialize(ver **vertex) +{ + int i; + + (*vertex) = (struct ver*)malloc(sizeof(struct ver)); + (*vertex)->words = 0; + (*vertex)->prefixes = 0; + (*vertex)->edges = (struct ver**)malloc(sizeof(ver) * 26); + + for (i = 0; i<26; i++) + (*vertex)->edges[i] = NULL; +} + +void addword(ver **vertex, string &word, int pos = 0) +{ + if (pos == word.size()) + { + (*vertex)->words++; + (*vertex)->prefixes++; + return; + } + + char k = word[pos]; + + (*vertex)->prefixes++; + if ((*vertex)->edges[k - 'a'] == NULL) + initialize(&(*vertex)->edges[k - 'a']); + + addword(&(*vertex)->edges[k - 'a'], word, pos + 1); +} + +int countwords(ver *vertex, string &word, int pos = 0) +{ + if (pos == word.size()) + return vertex->words; + + char k = word[pos]; + + if (vertex->edges[k - 'a'] == NULL) + return 0; + + return countwords(vertex->edges[k - 'a'], word, pos + 1); +} + +int countprefixes(ver *vertex, string &word, int pos = 0) +{ + if (pos == word.size()) + return vertex->prefixes; + + char k = word[pos]; + + if (vertex->edges[k - 'a'] == NULL) + return 0; + + return countprefixes(vertex->edges[k - 'a'], word, pos + 1); +} + +inline void next_string(string &A) +{ + A.clear(); + + char c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +struct ver *trie; +int N, Q; + +inline void init() +{ + string S; + + scanf("%d %d", &N, &Q); + + initialize(&trie); + + while (N--) + { + next_string(S); + addword(&trie, S); + } +} + +inline int query() +{ + string S; + + next_string(S); + return countprefixes(trie, S); +} + +int main() +{ +// freopen("input.txt", "r", stdin); + + init(); + + while (Q--) + printf("%d\n", query()); + + return 0; +} diff --git a/spoj_cpp_clean/ADAMATCH/ADAMATCH-14.cpp b/spoj_cpp_clean/ADAMATCH/ADAMATCH-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db1b8922ccdbfab686a6f6914b1368ca93114b04 --- /dev/null +++ b/spoj_cpp_clean/ADAMATCH/ADAMATCH-14.cpp @@ -0,0 +1,165 @@ +// #pragma GCC optimize("Ofast,unroll-loops") +// #pragma GCC target("avx,avx2,fma") + +#include +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define all(v) v.begin(), v.end() +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; ++i) +#define for0(i, stop) for(ll i = 0; i < stop; ++i) +#define rep1(i, start) for(ll i = start; i >= 1; --i) +#define rep0(i, start) for(ll i = (start-1); i >= 0; --i) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define EL '\n' +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +/*** + * Multiply (7x^2 + 8x^1 + 9x^0) with (6x^1 + 5x^0) + * ans = 42x^3 + 83x^2 + 94x^1 + 45x^0 + * A = {9, 8, 7} + * B = {5, 6} + * V = multiply(A,B) + * V = {45, 94, 83, 42} +***/ + +/*** Tricks + * Use vector < bool > if you need to check only the status of the sum + * Use bigmod if the power is over same polynomial && power is big + * Use long double if you need more precision + * Use long long for overflow +***/ +typedef vector vi; +const double PI = 2.0 * acos(0.0); +using cd = complex; + +void fft(vector & a, bool invert = 0) + +{ + int n = a.size(); + + for (int i = 1, j = 0; i < n; i++) { + int bit = n >> 1; + for (; j & bit; bit >>= 1) + j ^= bit; + j ^= bit; + + if (i < j) + swap(a[i], a[j]); + } + + for (int len = 2; len <= n; len <<= 1) { + double ang = 2 * PI / len * (invert ? -1 : 1); + cd wlen(cos(ang), sin(ang)); + for (int i = 0; i < n; i += len) { + cd w(1); + for (int j = 0; j < len / 2; j++) { + cd u = a[i+j], v = a[i+j+len/2] * w; + a[i+j] = u + v; + a[i+j+len/2] = u - v; + w *= wlen; + } + } + } + + if (invert) { + for (cd & x : a) + x /= n; + } +} + +void ifft(vector & p) + +{ + fft(p, 1); +} + +vi multiply(vi const& a, vi const& b) +{ + + vector fa(a.begin(), a.end()), fb(b.begin(), b.end()); + int n = 1; + while (n < a.size() + b.size()) + n <<= 1; + fa.resize(n); + fb.resize(n); + + fft(fa); + fft(fb); + for (int i = 0; i < n; i++) + fa[i] *= fb[i]; + ifft(fa); + + vi result(n); + for (int i = 0; i < n; i++) + result[i] = round(fa[i].real()); + + return result; +} + +const ll sz = 5e5 + 10, offset = 5e5; +char s[sz], r[sz]; + +vi num1[4], num2[4], res[4]; + +int main() +{ + scanf("%s %s", s, r); + + ll len1 = strlen(s), len2 = strlen(r); + + for0(i, 4) + num1[i].resize(len1+1,0), num2[i].resize(len2+1+offset,0); + + for(ll i = 0; s[i] != '\0'; i++) { + if(s[i] == 'A') + num1[0][i]++; + else if(s[i] == 'C') + num1[1][i]++; + else if(s[i] == 'T') + num1[2][i]++; + else + num1[3][i]++; + } + + for(ll i = 0; r[i] != '\0'; i++) { + + if(r[i] == 'A') + num2[0][-i + offset]++; + else if(r[i] == 'C') + num2[1][-i + offset]++; + else if(r[i] == 'T') + num2[2][-i + offset]++; + else + num2[3][-i + offset]++; + } + + for0(i, 4) res[i] = multiply(num1[i], num2[i]); + + ll ans = inf; + for(ll i = 0; i <= len1 - len2; i++) { + ll match = 0; + for0(j, 4) match += res[j][i+offset]; + + ans = min(ans, len2-match); + } + + cout << ans << EL; + + return 0; +} diff --git a/spoj_cpp_clean/ADAMOLD/ADAMOLD-14.cpp b/spoj_cpp_clean/ADAMOLD/ADAMOLD-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d155d89b6f397bad555770762d80a63118f1ced --- /dev/null +++ b/spoj_cpp_clean/ADAMOLD/ADAMOLD-14.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +#define ll long long +const ll inf = 1e18; + +ll n, k, ara[5009]; + +ll xorcs[5009][5009], xorsum[5009][5009], dp[5009][5009]; + +void solve(ll lo, ll hi, ll left, ll right, ll part) + +{ + if(lo > hi) return; + ll mid = (lo + hi) / 2; + + dp[mid][part] = inf; + ll indx; + for(ll i = left; i <= right && i <= mid; i++) { + + ll c = dp[i - 1][part - 1] + xorsum[i][mid]; + if(c < dp[mid][part]) { + dp[mid][part] = c; + indx = i; + } + } + + solve(lo, mid - 1, left, indx, part); + solve(mid + 1, hi, indx, right, part); +} + +int main() + +{ + scanf("%lld %lld", &n, &k); + k++; + for(ll i = 1; i <= n; i++) scanf("%lld", &ara[i]); + + for(ll i = 1; i <= n; i++) { + for(ll j = i + 1; j <= n; j++) + xorcs[i][j] = (ara[i] ^ ara[j]) + xorcs[i][j - 1]; + } + + for(ll i = 1; i <= n; i++) { + ll sum = 0; + for(ll j = i - 1; j >= 1; j--) { + sum += xorcs[j][i]; + xorsum[j][i] = sum; + } + } + + for(ll i = 0; i <= 5002; i++) { + dp[i][0] = inf; + } + dp[0][0] = 0; + + for(ll i = 1; i <= k; i++) + solve(1, n, 1, n, i); + + cout << dp[n][k] << endl; + + return 0; diff --git a/spoj_cpp_clean/ADAORANG/ADAORANG-14.cpp b/spoj_cpp_clean/ADAORANG/ADAORANG-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43b3a478fcf334b6398f1e0a914e2121220f13ac --- /dev/null +++ b/spoj_cpp_clean/ADAORANG/ADAORANG-14.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + +#define ll int +#define pb push_back +ll col[450009], fst[450009], start[450009], nodeOf[450009], tme[1000009], lca[3000009]; +vector graph[450009]; +ll TM = 1, indx = 0; + +bitset <255> cl[450009]; + +void dfs(ll u, ll p) + +{ + start[u] = TM; + nodeOf[TM] = u; + TM++; + tme[indx] = start[u]; + fst[u] = indx; + indx++; + + cl[u][ col[u] ] = 1; + for(ll i = 0; i < graph[u].size(); i++) { + + if(graph[u][i] != p) { + dfs( graph[u][i] , u); + tme[indx++] = start[u]; + cl[u] |= cl[ graph[u][i] ]; + } + } +} + +void build(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + lca[node] = tme[lo]; + return; + } + + ll mid = (lo + hi) / 2; + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + lca[node] = min(lca[2 * node], lca[2 * node + 1]); +} + +ll query(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lo > right || hi < left) + return 1e18; + if(lo >= left && hi <= right) + return lca[node]; + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, 2 * node); + ll p2 = query(mid + 1, hi, left, right, 2 * node + 1); + + return min(p1, p2); +} + +int main() + +{ + ll t, n, q, root, u, v, l, r; + cin >> t; + while(t--) { + scanf("%d %d %d", &n, &q, &root); + for(ll i = 0; i < n; i++) + scanf("%d", &col[i]); + + for(ll i = 1; i < n; i++) { + scanf("%d %d", &u, &v); + graph[u].pb(v); + graph[v].pb(u); + } + + dfs(root, -1); + build(0, indx - 1, 1); + //cout << 5; + for(ll i = 1; i <= q; i++) { + scanf("%d %d", &l, &r); + //cout << fst[l] << " " < fst[r]) + swap(l, r); + ll lc = query(0, indx - 1, fst[l], fst[r], 1); + //cout << cl[4].count() << endl; + printf("%d\n", cl[ nodeOf[lc] ].count()); + } + + for(ll i = 0; i < n; i++) { + graph[i].clear(); + cl[i].reset(); + } + TM = 1; + indx = 0; + } + + return 0; +} diff --git a/spoj_cpp_clean/ADAORANG/ADAORANG-8.cpp b/spoj_cpp_clean/ADAORANG/ADAORANG-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..337bbfd962e1e5d1c2291f8871a04564b9c7c82b --- /dev/null +++ b/spoj_cpp_clean/ADAORANG/ADAORANG-8.cpp @@ -0,0 +1,196 @@ +/*************************************************** + * Problem Name : ADAORANG - Ada and Orange Tree.cpp + * Problem Link : https://www.spoj.com/problems/ADAORANG/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-09-19 + * Problem Type : LCA, Bitset + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 450010 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +/************************************ Code Start Here ******************************************************/ +vector >adj; +bitset<251> ans[MAX]; +int par[MAX], ar[MAX]; +int table[MAX][20]; +int level[MAX]; +void dfs (int u, int p) { + for (int v : adj[u]) { + if (v == p) continue; + + par[v] = u; + level[v] = level[u] + 1; + dfs (v, u); + ans[u] |= ans[v]; + } +} +void build (int n) { + for (int i = 1; i <= n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 1; i <= n; i++) { + if (table[i][j - 1] != -1) { + table[i][j] = table[table[i][j - 1]][j - 1]; + } + } + } +} +int query (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int k = log2 (level[u]); + + for (int i = k; i >= 0; i--) { + if ( (level[u] - (1 << i) ) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = k; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + int tc, n, q, r; + cin >> tc; + + while (tc--) { + cin >> n >> q >> r; + r++; + adj.resize (n + 1); + + for (int i = 0; i <= n; i++) { + ans[i].reset(); + } + + for (int i = 1; i <= n; i++) { + cin >> ar[i]; + ans[i][ar[i]] = 1; + } + + for (int i = 1; i < n; i++) { + int u, v; + cin >> u >> v; + u++, v++; + adj[u].pb (v); + adj[v].pb (u); + } + + par[r] = -1; + level[r] = 0; + SET (table, -1); + dfs (r, -1); + build (n); + + while (q--) { + int u, v; + cin >> u >> v; + u++, v++; + int nod = query (u, v); + cout << ans[nod].count() << "\n"; + } + + for (int i = 0; i <= n; i++) { + adj[i].clear(); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/ADAPLANT/ADAPLANT-20.cpp b/spoj_cpp_clean/ADAPLANT/ADAPLANT-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b4a0f6cb25107bc18dd78fe3ef3946f313c13a5 --- /dev/null +++ b/spoj_cpp_clean/ADAPLANT/ADAPLANT-20.cpp @@ -0,0 +1,101 @@ +/** + * Author : Mr_Sohel + * Task : + * Algo : Multiset + Sliding Window +**/ +#include + +#define endl '\n' +#define sqr(x) (x) * (x) +#define gcd(x,y) __gcd(x,y) +#define lcm(x,y) ((x/gcd(x,y)) * y) +#define pb push_back +#define pf push_front +#define mk make_pair +#define fi first +#define se second +#define sz(x) (int)x.size() +#define all(x) (x).begin(),(x).end() +#define rall(x) (x).rbegin(),(x).rend() +#define prec(x) fixed<; +using pll = pair; + +const ld PI = acos((ld) - 1); +const int MOD = 1e9 + 7; +const ll INF = 2e18 + 1; +const ld EPS = 1e-9; +const int MX = 1e5 + 2; + +#ifdef LOCAL +#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) +template < typename Arg1 > +void __f(const char* name, Arg1&& arg1) { + cerr << name << " = " << arg1 << endl; +} +template < typename Arg1, typename... Args> +void __f(const char* names, Arg1&& arg1, Args&&... args) { + const char* comma = strchr(names + 1, ','); + cerr.write(names, comma - names) << " = " << arg1 << " | "; + __f(comma + 1, args...); +} +#else +#define debug(...) +#endif + +int a[MX]; + +int main() { + +#ifdef LOCAL + clock_t tStart = clock(); + freopen("in.txt", "r", stdin); + freopen("out.txt", "w", stdout); +#endif + unsyncIO; + int t; cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + for (int i = 0; i < n; i++) cin >> a[i]; + int i = 0, j = k + 1, ans = 0; + multiset mp; // i wil try with multimap later + for (i = 0; i <= j and i < n; i++) { + mp.insert(a[i]); + } + i = 0; + auto f = mp.begin(), l = --mp.end(); + // l--; + ans = max(ans, *l - *f); + while (j < n) { + f = mp.begin(), l = --mp.end(); + ans = max(ans, (*l - *f)); + /*for (auto in : mp) { + cout << in << " "; + } + cout << "ans = " << ans << endl;*/ + auto x = mp.find(a[i]); + mp.erase(x); + j++; + i++; + mp.insert(a[j]); + } + cout << ans << endl; + } +#ifdef LOCAL + cerr << "\nRuntime: " << (ld) (clock() - tStart) / CLOCKS_PER_SEC << " Seconds" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ADAPLUS/ADAPLUS-9.cpp b/spoj_cpp_clean/ADAPLUS/ADAPLUS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04c19e1d7bb9de42772922296db2ffcbef365bf2 --- /dev/null +++ b/spoj_cpp_clean/ADAPLUS/ADAPLUS-9.cpp @@ -0,0 +1,119 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar() +#define MAXN 2048 +using namespace std; + +inline char next_char() +{ + char c = gc(); + + while (isspace(c)) + c = gc(); + + return c; +} + +char board[MAXN][MAXN]; +int maxleft[MAXN][MAXN]; +int maxright[MAXN][MAXN]; +int maxup[MAXN][MAXN]; +int maxdown[MAXN][MAXN]; + +int N; + +inline void init() +{ + int i, j; + + scanf("%d", &N); + + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + board[i][j] = next_char(); + + memset(maxleft, 0, sizeof(maxleft)); + memset(maxright, 0, sizeof(maxright)); + memset(maxdown, 0, sizeof(maxdown)); + memset(maxup, 0, sizeof(maxup)); + + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + { + if (board[i][j] == '#') + maxleft[i][j] = maxleft[i][j - 1] + 1; + else + maxleft[i][j] = 0; + } + + for (i = 1; i <= N; i++) + for (j = N; j >= 1; j--) + { + if (board[i][j] == '#') + maxright[i][j] = maxright[i][j + 1] + 1; + else + maxright[i][j] = 0; + } + + for (j = 1; j <= N; j++) + for (i = 1; i <= N; i++) + { + if (board[i][j] == '#') + maxup[i][j] = maxup[i - 1][j] + 1; + else + maxup[i][j] = 0; + } + + for (j = 1; j <= N; j++) + for (i = N; i >= 1; i--) + { + if (board[i][j] == '#') + maxdown[i][j] = maxdown[i + 1][j] + 1; + else + maxdown[i][j] = 0; + } +} + +inline int solve() +{ + int ans = 0; + int i, j; + + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + if(board[i][j] == '#') + ans = max(ans, 1 + min(min(maxleft[i][j - 1], maxright[i][j + 1]), min(maxup[i - 1][j], maxdown[i + 1][j]))); + + return ans; +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/ADAQUEUE/ADAQUEUE-20.cpp b/spoj_cpp_clean/ADAQUEUE/ADAQUEUE-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b31566427b6c0949b0c9a3df6c5cad3fbafb280e --- /dev/null +++ b/spoj_cpp_clean/ADAQUEUE/ADAQUEUE-20.cpp @@ -0,0 +1,91 @@ +/** + * Author: Sohel Rana + * Date: 2022-05-25 21:07:34 + * Task: G_-_Ada_and_Queue +**/ +#include +#define endl '\n' +#define sqr(x) (x) * (x) +#define gcd(x,y) __gcd(x, y) +#define lcm(x,y) ((x/gcd(x,y)) * y) +#define pf(x) push_front(x) +#define pb(x) push_back(x) +#define eb(x) emplace_back(x) +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define sz(x) (int)x.size() +#define prec(x) fixed< q; + int flag = 0; + int t; + cin >> t; + while (t--) { + string s; + int item; + cin >> s; + if (s == "toFront"){ + cin >> item; + if (flag % 2 == 1) { + q.push_back(item); + } else + q.push_front(item); + } else if (s == "push_back") { + cin >> item; + if (flag % 2 == 1) { + q.push_front(item); + } else + q.push_back(item); + } else if (s == "front") { + if (q.empty()){ + cout << "No job for Ada?" << endl; + } else { + if (flag % 2 == 1) { + cout << q.back() << endl; + q.pop_back(); + } else { + cout << q.front()< +#include +using namespace std; + +int main() { + deque dq; + bool reversed = false; + + int Q; cin >> Q; + while (Q--) { + string cmd; cin >> cmd; + + if ((cmd == "back" && !reversed) || (cmd == "front" && reversed)) { + if (dq.empty()) cout << "No job for Ada?" << endl; + else { cout << dq.back() << endl; dq.pop_back(); } + } + else if ((cmd == "front" && !reversed) || (cmd == "back" && reversed)) { + if (dq.empty()) cout << "No job for Ada?" << endl; + else { cout << dq.front() << endl; dq.pop_front(); } + } + + else if (cmd == "reverse") { + reversed = !reversed; + } + + else if ((cmd == "push_back" && !reversed) || (cmd == "toFront" && reversed)) { + int n; cin >> n; dq.push_back(n); + } + else if ((cmd == "toFront" && !reversed) || (cmd == "push_back" && reversed)) { + int n; cin >> n; dq.push_front(n); + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ADAQUEUE/ADAQUEUE-9.cpp b/spoj_cpp_clean/ADAQUEUE/ADAQUEUE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0533a880f52140c7eb7baee889ad0ed2065b896b --- /dev/null +++ b/spoj_cpp_clean/ADAQUEUE/ADAQUEUE-9.cpp @@ -0,0 +1,136 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar() +using namespace std; + +inline void getstring(string &A) +{ + char c = gc(); + + A.clear(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +deque Q; +bool rev; + +inline void tofront(int num) +{ + if (rev) + Q.push_back(num); + else + Q.push_front(num); +} + +inline void toback(int num) +{ + if (rev) + Q.push_front(num); + else + Q.push_back(num); +} + +inline void front() +{ + if (Q.empty()) + printf("No job for Ada?\n"); + else + { + if (rev) + { + printf("%d\n", Q.back()); + Q.pop_back(); + } + + else + { + printf("%d\n", Q.front()); + Q.pop_front(); + } + } +} + +inline void back() +{ + if (Q.empty()) + printf("No job for Ada?\n"); + else + { + if (rev) + { + printf("%d\n", Q.front()); + Q.pop_front(); + } + + else + { + printf("%d\n", Q.back()); + Q.pop_back(); + } + } +} + +inline void command() +{ + string S; + int num; + + getstring(S); + + if (S == "toFront") + { + scanf("%d", &num); + tofront(num); + } + + else if (S == "front") + front(); + + else if (S == "back") + back(); + + else if (S == "push_back") + { + scanf("%d", &num); + toback(num); + } + + else + rev = !rev; +} + +int main() +{ + int N; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &N); + + while (N--) + command(); + + return 0; +} diff --git a/spoj_cpp_clean/ADARAIN/ADARAIN-9.cpp b/spoj_cpp_clean/ADARAIN/ADARAIN-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3a94f98dcab6c09ee76af8e7e6f93eb2b213904 --- /dev/null +++ b/spoj_cpp_clean/ADARAIN/ADARAIN-9.cpp @@ -0,0 +1,82 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +class BITPQ +{ +private: + int tree[1000007]; + int N; + +public: + BITPQ(int n) + { + N = n; + memset(tree, 0, sizeof(tree)); + } + + inline void point_update(int pos, int val) + { + for (; pos <= N; pos += pos&(-pos)) + tree[pos] += val; + } + + inline void range_update(int i, int j, int val) + { + point_update(i, val); + point_update(j + 1, -val); + } + + inline int point_query(int pos) + { + int sum = 0; + + for (; pos > 0; pos -= pos&(-pos)) + sum += tree[pos]; + + return sum; + } +}; + +BITPQ tree(1000006); +int N, M, L; + +inline void init() +{ + scanf("%d %d %d", &N, &M, &L); +} + +int main() +{ + int f, s; + +// freopen("input.txt", "r", stdin); + + init(); + + while (N--) + { + scanf("%d %d", &f, &s); + tree.range_update(f + 1, s + 1, 1); + } + + while (M--) + { + scanf("%d", &f); + printf("%d\n", tree.point_query(f + 1)); + } + + return 0; +} diff --git a/spoj_cpp_clean/ADASEQEN/ADASEQEN-21.cpp b/spoj_cpp_clean/ADASEQEN/ADASEQEN-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1d03fd0841e80be8f4dd07268e4930283d5df16 --- /dev/null +++ b/spoj_cpp_clean/ADASEQEN/ADASEQEN-21.cpp @@ -0,0 +1,37 @@ +/* + dynamic programming > longest common subsequence (LCS) + difficulty: easy + date: 29/Apr/2020 + by: @brpapa +*/ +#include +using namespace std; + +vector p; +string a; int N; +string b; int M; + +int memo[2020][2020]; +int dp(int i, int j) { + // atual a[i] e b[j] + + if (i == N || j == M) return 0; + + int &ans = memo[i][j]; + if (ans != -1) return ans; + + if (a[i] == b[j]) + return ans = p[a[i]-'a'] + dp(i+1, j+1); + + return ans = max(dp(i+1, j), dp(i, j+1)); +} + +int main() { + cin >> N >> M; + p.resize(26); for (int &price: p) cin >> price; + cin >> a >> b; + + memset(memo, -1, sizeof memo); + cout << dp(0, 0) << endl; + return 0; +} diff --git a/spoj_cpp_clean/ADATEAMS/ADATEAMS-19.cpp b/spoj_cpp_clean/ADATEAMS/ADATEAMS-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55209224a0d989ba4e66e091d99607071414c48a --- /dev/null +++ b/spoj_cpp_clean/ADATEAMS/ADATEAMS-19.cpp @@ -0,0 +1,120 @@ +#pragma GCC optimize("Ofast") +#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") +#pragma GCC optimize("unroll-loops") +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair p32; +typedef pair p64; +typedef pair pdd; +typedef vector v64; +typedef vector v32; +typedef vector > vv32; +typedef vector > vv64; +typedef vector > vvp64; +typedef vector vp64; +typedef vector vp32; +ll MOD = 998244353; +double eps = 1e-12; +#define forn(i,s,e) for(ll i = s; i < e; i++) +#define rforn(i,s,e) for(ll i = s; i >= e; i--) +#define endl "\n" +#define dbg(x) cout<<#x<<" = "< 0) { + if( x & 1) { + res = (res * a) % m; + } + a = (a * a) % m; + x = x >> 1; // x=x/2; + } + return res; +} + + +unsigned long long modInverse(unsigned long long a,long long m){ + return modPow(a,m-2,m); +} + + +unsigned long long chooseMod(unsigned long long n, long long r, long long m){ + //base Case + if(r==0){ + return 1; + } + if(n>a>>b>>d; + ll res = chooseMod(n, a, mod); + ll t = chooseMod(b, d, mod); + res = (res * modPow(t, a, mod))%mod; + cout<>n){ + //cout << "Case #" << it+1 << ": "; + solve(n); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ADATEAMS/ADATEAMS-9.cpp b/spoj_cpp_clean/ADATEAMS/ADATEAMS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6845947fdd56db5f4362159662c64eb9c5b8f86 --- /dev/null +++ b/spoj_cpp_clean/ADATEAMS/ADATEAMS-9.cpp @@ -0,0 +1,119 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar() +#define MAXN 1000000 +using namespace std; + +const long long MOD = 1000000007; + +long long fact[1000007]; +long long N, A, B, D; + +inline long long modpow(long long base, long long exp, long long modulus) +{ + long long result = 1; + + base %= modulus; + + while (exp > 0) + { + if (exp & 1) + result = (result * base) % modulus; + + base = (base * base) % modulus; + exp >>= 1; + } + + return result; +} + +inline bool finish() +{ + char c = gc(); + + while (isspace(c)) + c = gc(); + + if (c == EOF) + { + ungetc(c, stdin); + return 1; + } + + ungetc(c, stdin); + return 0; +} + +inline void pre() +{ + int i; + + fact[0] = 1; + + for (i = 1; i <= MAXN; i++) + { + fact[i] = fact[i - 1] * i; + fact[i] %= MOD; + } +} + +inline long long rev(long long num) +{ + return modpow(num, MOD - 2, MOD); +} + +inline long long ncr(long long n, long long k) +{ + long long res = fact[n]; + + res *= rev(fact[k]); + res %= MOD; + + res *= rev(fact[n - k]); + res %= MOD; + + return res; +} + +inline void init() +{ + scanf("%lld %lld %lld %lld", &N, &A, &B, &D); +} + +inline long long solve() +{ + long long res = ncr(N, A); + + res *= modpow(ncr(B, D), A, MOD); + res %= MOD; + + return res; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + + pre(); + + while (!finish()) + { + init(); + printf("%lld\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/ADATRIP/ADATRIP-14.cpp b/spoj_cpp_clean/ADATRIP/ADATRIP-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a17b3ffb0f0ad49124ae9299375bcd9e692270bb --- /dev/null +++ b/spoj_cpp_clean/ADATRIP/ADATRIP-14.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +#define ll int +#define inf 1000000000 +struct node { + ll u, c; +}; + +bool operator <(node a, node b) { + return a.c > b.c; +} + +vector g[500009]; +priority_queue pq; +ll dist[500009]; + +int main() + +{ + ll n, m, q, u, v, c, mx, cnt; + node tmp, nd; + scanf("%d %d %d", &n, &m, &q); + + for(ll i = 1; i <= m; i++) { + scanf("%d %d %d", &u, &v, &c); + g[u].push_back({v, c}); + g[v].push_back({u, c}); + } + + while(q--) { + scanf("%d", &u); + + for(ll i = 0; i < n; i++) + dist[i] = inf; + + pq.push({u, 0}); + dist[u] = 0; + while(!pq.empty()) { + tmp = pq.top(); + pq.pop(); + + if(dist[tmp.u] != tmp.c) + continue; + + for(ll i = 0; i < g[tmp.u].size(); i++) { + nd = g[tmp.u][i]; + if(tmp.c + nd.c < dist[nd.u]) { + dist[nd.u] = tmp.c + nd.c; + pq.push({nd.u, dist[nd.u]}); + } + } + } + + mx = 0, cnt = 0; + for(ll i = 0; i < n; i++) { + if(dist[i] == inf) + continue; + if(dist[i] > mx) { + mx = dist[i]; + cnt = 1; + } + else if(dist[i] == mx) + cnt++; + } + + printf("%d %d\n", mx, cnt); + } + + return 0; diff --git a/spoj_cpp_clean/ADDAP/ADDAP-9.cpp b/spoj_cpp_clean/ADDAP/ADDAP-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa2b5021d4f90458ed9a08ffd009429200a45aa5 --- /dev/null +++ b/spoj_cpp_clean/ADDAP/ADDAP-9.cpp @@ -0,0 +1,133 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define MOD 1000000007 +#define gc() getchar_unlocked() + +using namespace std; + +vector< vector< int > > TREE; +int BASE[100007], STEP[100007]; +bool V[100007]; +int N, Q; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +inline void init() +{ + int i, f, s, u, base, step; + + //scanf("%d", &N); + N = FAST_IO(); + + TREE.resize(N + 1); + + for (i = 0; i < N - 1; i++) + { + //scanf("%d %d", &f, &s); + f = FAST_IO(); + s = FAST_IO(); + + TREE[f].push_back(s); + TREE[s].push_back(f); + } + + //scanf("%d", &Q); + Q = FAST_IO(); + + for (i = 0; i < Q; i++) + { + //scanf("%d %d %d", &u, &base, &step); + u = FAST_IO(); + base = FAST_IO(); + step = FAST_IO(); + + BASE[u] += base; + if (BASE[u] >= MOD) + BASE[u] -= MOD; + STEP[u] += step; + if (STEP[u] >= MOD) + STEP[u] -= MOD; + } +} + +inline void BFS() +{ + deque< pair > Q; //node, parent + pair curr; + int i; + + V[1] = 1; + Q.push_back(make_pair(1, 0)); + + while (!Q.empty()) + { + curr = Q.front(); + Q.pop_front(); + + STEP[curr.first] += STEP[curr.second]; + if (STEP[curr.first] >= MOD) + STEP[curr.first] -= MOD; + + BASE[curr.first] += BASE[curr.second]; + if (BASE[curr.first] >= MOD) + BASE[curr.first] -= MOD; + + BASE[curr.first] += STEP[curr.second]; + if (BASE[curr.first] >= MOD) + BASE[curr.first] -= MOD; + + for (i = 0; i < TREE[curr.first].size(); i++) + if (!V[TREE[curr.first][i]]) + { + V[TREE[curr.first][i]] = 1; + Q.push_back(make_pair(TREE[curr.first][i], curr.first)); + } + } +} + +inline void print() +{ + int i; + + for (i = 1; i <= N; i++) + printf("%d\n", BASE[i]); +} + +int main() +{ + //freopen("input.txt", "r", stdin); + + init(); + BFS(); + print(); + + return 0; +} diff --git a/spoj_cpp_clean/ADDREV/ADDREV-11.cpp b/spoj_cpp_clean/ADDREV/ADDREV-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..964719adc1709b33ac62e36fb4416de87f5cb799 --- /dev/null +++ b/spoj_cpp_clean/ADDREV/ADDREV-11.cpp @@ -0,0 +1,93 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +//General case - For big numbers +//Even if the number crosses the range of datatype, this solution works. + +#include +#include + +using namespace std; + +char findSum(char n1, char n2, int &carry) +{ + int result; + + result = (n1-'0') + (n2-'0') + carry; + + carry = result / 10; + + result = result % 10; + + return (char)(result+'0'); +} + +int processNumber(string &num, int len) +{ + while(len > 0) + { + if(num[len-1] == '0') + len--; + else + break; + } + + return len; +} + +int main(void) +{ + int t; + string num1, num2, result; + int len1, len2, len, carry; + + cin >> t; + + while(t--) + { + cin >> num1 >> num2; + + result = ""; + carry = 0; + len1 = num1.length(); + len2 = num2.length(); + + len1 = processNumber(num1, len1); + len2 = processNumber(num2, len2); + + int i; + + for(i = 0; i < len1 && i < len2; i++) + result += findSum(num1[i], num2[i], carry); + + while(i < len1) + { + result += findSum(num1[i], '0', carry); + i++; + } + + while(i < len2) + { + result += findSum('0', num2[i], carry); + i++; + } + + if(carry) + result += '1'; + + len = result.length(); + + for(i = 0; i < len; i++) + if(result[i] != '0') + break; + + while(i < len) + cout << result[i++]; + + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ADDREV/ADDREV-13.cpp b/spoj_cpp_clean/ADDREV/ADDREV-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a6aab610371e6445de8f74bcae80d1a9d2dcdcc --- /dev/null +++ b/spoj_cpp_clean/ADDREV/ADDREV-13.cpp @@ -0,0 +1,53 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +int rev(int x){ + int res = 0; + do{ + res *= 10; + res += x % 10; + x /= 10; + }while(x > 0); + return res; +} + +int main(){ + int T; scanf("%d", &T); + for(int t=0; t +#include +#include +#define ll long long +using namespace std; + +int revit(ll a){ + ll dig=0,rev=0; + do{ + dig = a % 10; + a = a / 10; + rev = rev * 10 + dig; +}while(a>0); +return(rev); +} +int main() { + ll t,a,b; + cin>>t; + while(t--){ + cin>>a>>b; + a=revit(a); + b=revit(b); + cout< +#define ll long long + +using namespace std; + +ll rev(ll n); + +int main() { + // your code goes here + ios_base::sync_with_stdio(false); + cin.tie(NULL); + int t; + cin>>t; + while(t--){ + ll numb_first, numb_second; + cin>>numb_first; + cin>>numb_second; + numb_first=rev(numb_first); + numb_second=rev(numb_second); + ll sum; + sum=numb_first+numb_second; + sum=rev(sum); + cout<0){ + temp=(temp*10)+(n%10); + n/=10; + } + return temp; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ADDREV/ADDREV-5.cpp b/spoj_cpp_clean/ADDREV/ADDREV-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26cf29d2fa9a2eef648949716ce11322def3836d --- /dev/null +++ b/spoj_cpp_clean/ADDREV/ADDREV-5.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +long int rev(long int num) +{ + long int reverseNum; + reverseNum=0; + while(num!=0) + { + reverseNum*=10; + reverseNum+=num%10; + num=num/10; + } + return reverseNum; + +} +int main() +{ + long int num1,num2,num,t; + cin>>t; + while(t--) + { + scanf("%ld %ld",&num1,&num2); + num=rev(num1)+rev(num2); + printf("%ld\n",rev(num)); + } + return 0; + +} diff --git a/spoj_cpp_clean/ADDREV/ADDREV-7.cpp b/spoj_cpp_clean/ADDREV/ADDREV-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04b606bd875403d936754d25d31545ee38e4358d --- /dev/null +++ b/spoj_cpp_clean/ADDREV/ADDREV-7.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + + +int reverse(int x) +{ + int result=0; + while(x>0) + { + result= result * 10; + result= result + (x%10); + x= x/10; + } + return result; +} + + +int main() +{ +int n=0,x=0,y=0; +scanf("%d",&n); + +for(int i=0;i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +int main() +{ + string A, B; + string sum; + int T; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + { + cin>>A>>B; + + reverse(A.begin(), A.end()); + reverse(B.begin(), B.end()); + + sum=to_string(stoi(A, nullptr, 10) + stoi(B, nullptr, 10)); + reverse(sum.begin(), sum.end()); + + printf("%d\n", stoi(sum, nullptr, 10)); + } + + return 0; +} diff --git a/spoj_cpp_clean/AE00/AE00-11.cpp b/spoj_cpp_clean/AE00/AE00-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ddafb106f5bb953f9bb76262c96bbe05f1069ff --- /dev/null +++ b/spoj_cpp_clean/AE00/AE00-11.cpp @@ -0,0 +1,35 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include + +using namespace std; + +int main(void) +{ + int n; + long result; + + cin >> n; + + result = 0l; + + result += n; + + for(int i = 2; i <= n/2; i++) + { + if(i*i <= n) + result += (i-1); + else + { + for(int j = 2; i*j <= n; j++) + result++; + } + } + + cout << result << endl; + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/AE00/AE00-13.cpp b/spoj_cpp_clean/AE00/AE00-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27c6fa40c2db6d451be88ec3175769b6d7ad3e25 --- /dev/null +++ b/spoj_cpp_clean/AE00/AE00-13.cpp @@ -0,0 +1,51 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +int f(int n){ + int count = 0; + for(int i=1, m=(int)(ceil(sqrt((double)(n)))); i<=m; ++i){ + for(int j=i; i*j<=n; ++j)++count; + } + return count; +} + +int main(){ + freopen("data.in","r",stdin); + int n; + while(scanf("%d", &n) == 1){ + int res = f(n); + printf("%d\n",res); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/AE00/AE00-16.cpp b/spoj_cpp_clean/AE00/AE00-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19a4cb3c2156e4e4a37271c9b138603751564ddc --- /dev/null +++ b/spoj_cpp_clean/AE00/AE00-16.cpp @@ -0,0 +1,48 @@ +#include + +#define ll long long +#define ull unsigned long long +#define pb push_back +#define mp make_pair +#define tr(c,it) for(auto it = (c).begin(); it != (c).end(); it++) + +using namespace std; + +ll f(ll n){ + ll fac=0; + for(ll i=1;i<=n;i++){ + if(n%i==0){ + fac+=1; + /*if(n==6){ + cout<<"**"<>n; + ll ans=0; + for(ll i=1;i<=n;i++){ + //cout<>t; + while(t--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/AE00/AE00-19.cpp b/spoj_cpp_clean/AE00/AE00-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..989925c2c493fb944fcca1e5166d4af5f70204e2 --- /dev/null +++ b/spoj_cpp_clean/AE00/AE00-19.cpp @@ -0,0 +1,18 @@ +#include +#define ll long long +#define endl "\n" +#define MAX 100001 +#define mod 1000000007 +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + ll n; cin>>n; + ll t, ans=0; + for(t=1; (n/t)>=t ; t++){ + ans = ans + (n/t) - (t-1); + } + cout< number theory + difficulty: none + date: none + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int main() { + int n, qte = 0; + cin >> n; + + for (int k = 1; k <= sqrt(n); k++) + for (int i = k; i * k <= n; i++) + qte++; + + cout << qte << endl; + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/AE00/AE00-5.cpp b/spoj_cpp_clean/AE00/AE00-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a4973c706be9cff033cd306fc922ee461e6df37 --- /dev/null +++ b/spoj_cpp_clean/AE00/AE00-5.cpp @@ -0,0 +1,15 @@ +#include +#include + +using namespace std; + +int main() +{ + int a,b; + cin>>a; + b=0; + for(int i=1;i<=((int)sqrt(a));i++) + b=b+((a/i)-i+1); + cout< + +using namespace std; + +int main(){ + + +int n; + +cin >>n; +int cont=0; +for (int i=1; i*i<=n;i++){ + for (int j=i; j*i<=n; j++) + cont++; + + + +} + +cout < +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("/home/saikat/Desktop/logN/input.txt", "r", stdin); +#define __FileWrite freopen ("/home/saikat/Desktop/logN/output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int check (ll n) { + ll sq = sqrt (n); + + if (sq * sq == n) return 1; + else return 0; +} +int main () { + ll N; + + while (scanf ("%lld", &N) == 1) { + ll sq = sqrt (N), ans = 0; + + for (int i = 1; i <= sq; i++) { + int cnt = 0; + + for (int j = i; j <= N; j++) { + if (i * j <= N) cnt++; + } + + ans += cnt; + } + + printf ("%lld\n", ans); + } + + return 0; +} diff --git a/spoj_cpp_clean/AE1B/AE1B-13.cpp b/spoj_cpp_clean/AE1B/AE1B-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4a1d29ad1720a84a3b8be43d3f1aec8141c7ff9 --- /dev/null +++ b/spoj_cpp_clean/AE1B/AE1B-13.cpp @@ -0,0 +1,48 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +int main(){ + freopen("data.in","r",stdin); + int n, k, s; scanf("%d %d %d", &n, &k, &s); + vector v(n); + for(int i=0; i()); + int i, m, screws; + for(i=0, m=k*s, screws =0; i +#include +using namespace std; + +int MOD = 11380; +int memo[12][12][12][32]; + +int solve(int c1, int c2, int c3, int d) { + if (c1<0 || c2<0 || c3<0 || d<0) return 0; + if (c1+c2+c3==0) return 1; + int &res = memo[c1][c2][c3][d]; + if (res >= 0) return res; + res = 0; + // parenthesis first + for (int c3in=0; c3in> c1 >> c2 >> c3 >> d) cout << (solve(c1,c2,c3,d)-solve(c1,c2,c3,d-1)+MOD)%MOD << endl; +} diff --git a/spoj_cpp_clean/AGGRCOW/AGGRCOW-13.cpp b/spoj_cpp_clean/AGGRCOW/AGGRCOW-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09350ab0b16a628b7def8b9afb44d21195e60f0b --- /dev/null +++ b/spoj_cpp_clean/AGGRCOW/AGGRCOW-13.cpp @@ -0,0 +1,70 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +bool ok(vector &v, int c, int gap){ + int last = 0; c--; + for(int i=1, n=v.size(); i gap){ + c--, last = i; + } + if(c <= 0)return true; + } + if(c <= 0)return true; + else return false; +} + +int f(vector &v, int c){ + int low = 0, high = 1000000000; + while(low < high){ + int mid = (low + high)/2; + if(ok(v,c,mid))low = mid+1; + else high = mid; + } + return low; +} + +int main(){ + //freopen("data.in","r",stdin); + int T; scanf("%d", &T); + for(int t=0; t v(n); + for(int i=0; i + +using namespace std; + +int n,c; +vector a; + +int f(int x){ + int cows=1, lastpos=a[0]; + for(int i=1;i= x ){ + cows++; + lastpos=a[i]; + } + if(cows>=c) return 1; + } + return 0; +} + +void solve(){ + cin>>n>>c; + a.resize(n); + for(int i=0;i>a[i]; + } + sort(a.begin(),a.end()); + int l=0, r=a[n-1]-a[0]+1,mid; + while(r-l>=1){ + mid = (l+r)/2; + if(f(mid)==0) r=mid; + else l = mid; + } + cout<>t; + while(t--){ + solve(); + } + return 0; diff --git a/spoj_cpp_clean/AGGRCOW/AGGRCOW-19.cpp b/spoj_cpp_clean/AGGRCOW/AGGRCOW-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f11fabad437cebb6cf952db6bb90f1a1e3fb1a80 --- /dev/null +++ b/spoj_cpp_clean/AGGRCOW/AGGRCOW-19.cpp @@ -0,0 +1,53 @@ +#include +#pragma GCC optimize("Ofast") +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 1000000 +#define PI 3.14159265 + +using namespace std; + +ll n, c; +ll arr[MAX]; + +bool canbe(ll m) { + int cow_placed = 1, last_pos = arr[0]; + for (ll i = 0; i < n; i++) { + if ((arr[i] - last_pos) >= m) { + if (++cow_placed == c) { + return true; + } + last_pos = arr[i]; + } + } + return false; +} + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + + ll times; cin >> times; while (times--) { + cin >> n >> c; + for (ll i = 0; i < n; i++) { + cin >> arr[i]; + } + sort(arr, arr + n); + + ll high = arr[n - 1], low = 0, mid, pos = 0; + while (high >= low) { + mid = (high + low) / 2; + if (canbe(mid)) { + low = mid + 1; + pos = mid; + } + else { + high = mid - 1; + } + } + cout << pos << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/AGGRCOW/AGGRCOW-20.cpp b/spoj_cpp_clean/AGGRCOW/AGGRCOW-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a44ff7397d81c40d4e6559b0ae03d1531b53ed7 --- /dev/null +++ b/spoj_cpp_clean/AGGRCOW/AGGRCOW-20.cpp @@ -0,0 +1,104 @@ +/** + * Author : Mr_Sohel + * Task : + * Algo : +**/ +#include + +#define endl '\n' +#define sqr(x) (x) * (x) +#define gcd(x,y) __gcd(x,y) +#define lcm(x,y) ((x/gcd(x,y)) * y) +#define pb push_back +#define pf push_front +#define mk make_pair +#define fi first +#define se second +#define sz(x) (int)x.size() +#define all(x) (x).begin(),(x).end() +#define rall(x) (x).rbegin(),(x).rend() +#define prec(x) fixed<; +using pll = pair; + +const ld PI = acos((ld) - 1); +const int MOD = 1e9 + 7; +const ll INF = 2e18 + 1; +const ld EPS = 1e-9; +const int MX = 1e5 + 5; + +#ifdef LOCAL +#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) +template < typename Arg1 > +void __f(const char* name, Arg1&& arg1) { + cerr << name << " = " << arg1 << endl; +} +template < typename Arg1, typename... Args> +void __f(const char* names, Arg1&& arg1, Args&&... args) { + const char* comma = strchr(names + 1, ','); + cerr.write(names, comma - names) << " = " << arg1 << " | "; + __f(comma + 1, args...); +} +#else +#define debug(...) +#endif + +ll n, c; +int a[MX]; + +bool check(ll mid) { + int cnt = 1, st = 0; + for (int i = 0; i < n; i++) { + if ((a[i + 1] - a[st]) >= mid) { + cnt++; + st = i + 1; + } + } + // debug(cnt); + return cnt >= c; +} + +int main() { + +#ifdef LOCAL + clock_t tStart = clock(); + freopen("in.txt", "r", stdin); + freopen("out.txt", "w", stdout); +#endif + unsyncIO; + int t; cin >> t; + while (t--) { + cin >> n >> c; + for (int i = 0; i < n; i++) cin >> a[i]; + sort(a, a + n); + ll lo = 1, hi = 1e18, mid, ans; + while (lo <= hi) { + mid = lo + ((hi - lo) / 2); + // debug(mid); + if (check(mid)) { + lo = mid + 1; + ans = mid; + } else { + hi = mid - 1; + } + } + cout << ans << endl; + } + +#ifdef LOCAL + cerr << "\nRuntime: " << (ld) (clock() - tStart) / CLOCKS_PER_SEC << " Seconds" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/AGGRCOW/AGGRCOW-21.cpp b/spoj_cpp_clean/AGGRCOW/AGGRCOW-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d75acb482b6e29891bca1015273c90e2c4973143 --- /dev/null +++ b/spoj_cpp_clean/AGGRCOW/AGGRCOW-21.cpp @@ -0,0 +1,49 @@ +/* + miscellaneous > binary search > on answer + difficulty: medium + date: 03/Apr/2020 + problem: given an array A of size N (2 <= N <= 10^5), print the largest minimum distance between any two of C (C <= N) elements choosen any + by: @brpapa +*/ +#include +#include +#include +#define ll long long +using namespace std; + +int N, C; +vector A; + +// consigo escolher C elementos de A mantendo uma distância mínima entre eles de ans? +bool can(ll ans) { + int k = 0; // A[k] é o último elemento escolhido + int c = 1; + + for (int i = 1; i < N && c < C; i++) + if (A[i]-A[k] >= ans) { + c++; + k = i; + } + return c >= C; +} + +int main() { + int T; cin >> T; + while (T--) { + cin >> N >> C; + A.resize(N); for (ll &a: A) cin >> a; + sort(A.begin(), A.end()); + + ll low = 0, high = A[N-1]-A[0]; + ll ans; + + while (low <= high) { + ll mid = (low+high)/2; + + if (can(mid)) low = mid+1, ans = mid; + else high = mid-1; + } + cout << ans << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/AGGRCOW/AGGRCOW-4.cpp b/spoj_cpp_clean/AGGRCOW/AGGRCOW-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7745933e7d66babf806b83a349a1972fa37c8ab0 --- /dev/null +++ b/spoj_cpp_clean/AGGRCOW/AGGRCOW-4.cpp @@ -0,0 +1,131 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m , goal; +int a[100010]; +int fun(int gap) +{ + int now=a[0], cnt=1; + FOR(i,0,n) + { + if(a[i]-now >=gap) + { + cnt++; + now=a[i]; + if(cnt==goal) + return 1; + } + } + return 0; +} +int binary_srch() +{ + int st=0, en=a[n-1], mid; + while(st +#include + +using namespace std; + +int arr[100001]; +int n,c; + +bool fun(int); +int binarySearch(); + +int main() +{ + int test; + cin>>test; + while(test--) + { + cin>>n>>c; + for(int i=0;i>arr[i]; + } + sort(arr,arr+n); + cout<=x) + { + if(++placed==c) + return true; + last=arr[i]; + } + return false; + } +} diff --git a/spoj_cpp_clean/AGGRCOW/AGGRCOW-7.cpp b/spoj_cpp_clean/AGGRCOW/AGGRCOW-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc414e0e97f295d07a717db5cd442240f56ee8cb --- /dev/null +++ b/spoj_cpp_clean/AGGRCOW/AGGRCOW-7.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +int stalls[100100]; +int t,n,c,aux; + +bool colocar(int key, int dist) +{ + bool ans= false; + int vacas=1; + int pos= stalls[0]; + for (int i = 1; i < n; ++i) + { + if(stalls[i]-pos>=dist) + { + pos= stalls[i]; + vacas++; + if (vacas==key) + { + ans= true; + break; + } + } + } + + return ans; +} + + + +int BinarySearch(int hi,int lo, int key) +{ + int mid,res=0; + while (hi>lo) + { + mid = lo + (hi-lo)/2; + if (colocar(key,mid)) + { + lo= mid+1; + if (mid>res) + { + res=mid; + } + } + else + { + + hi = mid; + } + + } + return res; // lo is close to the border between no and yes +} + + +int main() +{ + + int max=0; + + scanf("%d",&t); + + while(t--) + { + scanf("%d %d",&n,&c); + memset(stalls,0,n); + for (int i = 0; i < n; ++i) + { + scanf("%d",&aux); + stalls[i]= aux; + if (aux>max) + { + max= aux; + } + } + sort(stalls,stalls+n); + printf("%d\n",BinarySearch(max,0,c) ); + } + + return 0; +} diff --git a/spoj_cpp_clean/AGGRCOW/AGGRCOW-9.cpp b/spoj_cpp_clean/AGGRCOW/AGGRCOW-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e8a5266e7dbbd4f2b4cbf84bb7f31289832a5ab --- /dev/null +++ b/spoj_cpp_clean/AGGRCOW/AGGRCOW-9.cpp @@ -0,0 +1,89 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +vector A; +int N, M; + +inline bool check(int x, int m) +{ + int i=1; + int curr=A[0]; + m--; + while(m>0) + { + if(A[i]-curr>=x) + { + m--; + curr=A[i]; + } + i++; + if(m>0 && i>=N) + return 0; + } + + return 1; +} + +void init() +{ + int i, num; + + A.clear(); + + scanf("%d %d", &N, &M); + + for(i=0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ +#include +#include +#include +#include +#include + +using namespace std; + +string A, B; +int N, M; + +inline void init() +{ + cin >> N >> M; + cin >> A; + cin >> B; +} + +inline int solve() +{ + int memoA[26], memoB[26]; + int i, rval = INT_MAX; + + memset(memoA, 0, sizeof(memoA)); + memset(memoB, 0, sizeof(memoB)); + + + for (i = 0; i < N; i++) + memoA[A[i] - 'a']++; + + for (i = 0; i < M; i++) + memoB[B[i] - 'a']++; + + for (i = 0; i < 26; i++) + if (memoB[i]) + rval = min(rval, memoA[i] / memoB[i]); + + return rval; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + int T; + + cin >> T; + + while (T--) + { + init(); + cout << solve() << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/AGS/AGS-3.cpp b/spoj_cpp_clean/AGS/AGS-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..197edc89c2028b786934db825402a318ac50d8c5 --- /dev/null +++ b/spoj_cpp_clean/AGS/AGS-3.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +long long exp(long long a, long long b, long long p){ + long long res = 1; + while(b){ + if(b&1) res = (res*a)%p; + a = (a*a)%p; + b >>= 1; + } + return res; +} + +/*Divide and conquer approach for finding the sum of a GP in O(log^2 n) time*/ +long long gp(long long r, long long n, long long p){ + if(n == 0) + return (1%p); + else if(n == 1) + return (1+r)%p; + else{ + if(n&1){ + return ( ( (1 + exp(r,(n+1)/2,p) )%p) * gp(r,(n-1)/2,p) )%p; + } + else{ + return (((( (1 + exp(r,n/2,p)) %p) * gp(r,(n-1)/2,p) )%p) + exp(r,n,p))%p; + } + } +} + +int main(){ + int t; + int a,d,r,n,MOD; + long long x,sum; + sd(t); + while(t--){ + scanf("%d %d %d",&a,&d,&r); + scanf("%d %d",&n,&MOD); + + x = (a * exp(r,(n-1)/2,MOD))%MOD; + sum = (d * gp(r,(n-1)/2,MOD) )%MOD; + + + if(n&1){ + printf("%lld\n",(((x+sum)%MOD) - (d%MOD) + MOD)%MOD); + } + else{ + printf("%lld\n",(x+sum)%MOD); + } + } + return 0; +} diff --git a/spoj_cpp_clean/AIBOHP/AIBOHP-14.cpp b/spoj_cpp_clean/AIBOHP/AIBOHP-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f61a1ebb9ed17a8b6d68a8208c689cf04abca92 --- /dev/null +++ b/spoj_cpp_clean/AIBOHP/AIBOHP-14.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +#define ll int + +ll dp[6105][6105]; +string s; + +ll solve(ll l, ll r) + +{ + if(l >= r) return 0; + ll &ret = dp[l][r]; + if(ret != -1) return ret; + + ret = 1e8; + if(s[l] != s[r]) ret = min(1 + solve(l, r - 1), 1 + solve(l + 1, r)); + else ret = solve(l + 1, r - 1); + + return ret; +} + +int main() + +{ + std::ios_base::sync_with_stdio(false); + ll t; + cin >> t; + while(t--) { + memset(dp, -1, sizeof(dp)); + cin >> s; + cout << solve(0, s.length() - 1) << endl; + } + + return 0; diff --git a/spoj_cpp_clean/AIBOHP/AIBOHP-16.cpp b/spoj_cpp_clean/AIBOHP/AIBOHP-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a3edb5667d048ea175ca9910b8d3e7751b0e10b --- /dev/null +++ b/spoj_cpp_clean/AIBOHP/AIBOHP-16.cpp @@ -0,0 +1,44 @@ +#include +#define ll long long +#define pb push_back +using namespace std; + +const int N = 6109; +ll dp[N][N] = {}; + +void LCS(string s1, string s2){ + ll n=s1.length(); + for(ll i=0;i>s1; + ll n = s1.length(); + s2=s1; + reverse(s2.begin(), s2.end()); + LCS(s1,s2); + return; +} + +int main(){ + ll t; + cin>>t; + while(t--){ + memset(dp, 0, sizeof(dp[0][0]) * N * N); + solve(); + } + return 0; diff --git a/spoj_cpp_clean/AIBOHP/AIBOHP-3.cpp b/spoj_cpp_clean/AIBOHP/AIBOHP-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8acfb11fa373e56d2da6e0e57bc9afde71d7eee3 --- /dev/null +++ b/spoj_cpp_clean/AIBOHP/AIBOHP-3.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; +int main(){ + string s1; + int t, dp[2][6101], l, indx; + sd(t); + while(t--){ + cin>>s1; + l = s1.length(); + for(int i = 0; i <= l; i++){ + dp[0][i] = 0; + } + indx = 0; + for(int i = 1; i <= l; i++){ + indx = 1 - indx; + for(int j = 1; j <= l; j++){ + if(s1[i-1] == s1[l-j]){ + dp[indx][j] = dp[1-indx][j-1] + 1; + } + else{ + dp[indx][j] = max(dp[1-indx][j], dp[indx][j-1]); + } + } + } + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +//string s, s1, s2; +char s1[6200], s2[6200]; +int dp[2][6200];//[6200]; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + s(T); + while(T--) + { + ss(s1); + n=strlen(s1); + memset(dp,0,sizeof(dp)); + FOR(i,0,n) + s2[i]=s1[n-i-1]; + if(s1[0]==s2[0]) + dp[0][0]=1; + + FOR(i,1,n) + { + if(s1[0]==s2[i]) + dp[0][i]=1; + else + dp[0][i]=dp[0][i-1]; + } + FOR(i,1,n) + { + if(s1[i]==s2[0]) + dp[i%2][0]=1; + FOR(j,1,n) + { + if(s1[i]==s2[j]) + { + dp[i%2][j]=1+dp[(i-1)%2][(j-1)]; + } + else + { + dp[i%2][j]=maX(dp[(i-1)%2][j], maX(dp[i%2][(j-1)], dp[(i-1)%2][(j-1)])); + } + } + } + p(n-maX(dp[0][n-1], dp[1][n-1]));printf("\n");//< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; +int DP[6107][6107]; +char str[6107]; +int N; + +void init() +{ + int i = 0; + char c; + + c=gc(); + while(isspace(c)) + c=gc(); + + while(!isspace(c) && c!=EOF) + { + str[i++] = c; + c = gc(); + } + + N = i; +} + +int main() +{ + int T, i, j, L; + + //freopen("input.txt","r",stdin); + scanf("%d", &T); + + while(T--) + { + init(); + + for(i=0; i +#define hell 1000000009 +#define bug1(x) cout<<"$ "< +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +bool dfs(ll soldier,ll node, ll dis, vector< vector >& adj, vector& vis, vector& p){ + //if(vis[node]==soldier) return true; + /* + if same soldier had visited that node than it is alright. + */ + if(vis[node]!=-1&&vis[node]!=soldier){ // protected by more than 1 soldier + return false; + } + vis[node]=soldier; + if(dis==0){ + return true; + } + for(auto u : adj[node]){ + //if(u==p[node]) continue; // do not check for parent of node + p[u]=node; + bool ans = dfs(soldier, u, dis-1, adj, vis, p); + if(ans==false){ + return false; + } + } + return true; +} + +void solve(){ + ll n,r,m; + cin>>n>>r>>m; + vector p(n,-1); // parent array + vector vis(n,-1); // initialise it as -1 it will contain id of soldier who visited it. + vector< vector > adj(n); + vector < pair > defence; // {city, strength of soldier} + loop(i,0,r){ + ll a,b; + cin>>a>>b; + a--; + b--; + adj[a].pb(b); + adj[b].pb(a); + } + loop(i,0,m){ + ll k,s; + cin>>k>>s; + k--; + defence.pb({k,s}); + } + + bool valid = true; + for(ll i=0;i>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/AKBAR/AKBAR-19.cpp b/spoj_cpp_clean/AKBAR/AKBAR-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c36143fd8371908c51fff257e249105ac9b9bb18 --- /dev/null +++ b/spoj_cpp_clean/AKBAR/AKBAR-19.cpp @@ -0,0 +1,132 @@ +////////////////////////// Author : Adarsh Gaur //////////////////////////// + +#include +//#include +//#include +//#include +//#include + +using namespace std; +//using namespace __gnu_pbds; + +//typedef tree, rb_tree_tag, tree_order_statistics_node_update> ordered_set; +#define int long long +typedef pair pii; +typedef vector vi; +typedef vector > vvi; +typedef vector > vvpii; +typedef vector vpii; + + +#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) +#define forn(i,s,e) for(int i=s; i used(MAXN); +vi d(MAXN); + +bool bfs_with_height(int node, int power){ + if(used[node]!=-1){ + return false; + } + queue q; + q.push(node); + used[node]=node; + d[node]=0; + while(!q.empty()){ + int v = q.front(); + if(d[v]==power){ + break; + } + q.pop(); + for(int u: adj[v]){ + if(used[u]!=node && used[u]!=-1){ + return false; + }else if(used[u]==node){ + continue; + }else{ + used[u]=node; + q.push(u); + d[u]=d[v]+1; + } + } + } + return true; +} + + +void solve(){ + int n, r, m; + cin>>n>>r>>m; + adj.clear(); + adj.resize(n+1); + used.clear(); + used.resize(n+1); + d.clear(); + d.resize(n+1); + for(auto &i: used){ + i=-1; + } + + for(int i=0;i>x>>y; + adj[x].pb(y); + adj[y].pb(x); + } + bool ans=true; + int power[m], nodes[m]; + for(int i=0;i>nodes[i]>>power[i]; + } + if(m>n){ + ans=false; + } + + + + for(int i=0;i> it; + for(int i=1;i<=it; ++i){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ALCATRAZ1/ALCATRAZ1-8.cpp b/spoj_cpp_clean/ALCATRAZ1/ALCATRAZ1-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fdfd55c68a9fc9fac15885e0904f1fa6af0b122 --- /dev/null +++ b/spoj_cpp_clean/ALCATRAZ1/ALCATRAZ1-8.cpp @@ -0,0 +1,73 @@ +/*************************************************** + * Problem name : ALCATRAZ1 - SUM OF DIGITS .cpp + * Problem Link : http://www.spoj.com/problems/ALCATRAZ1/ + * OJ : Spj + * Verdict : AC + * Date : 2017-10-07 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc; + ll sum; + scanf("%d", &tc); + getchar(); + for (int t = 1; t <= tc; t++) { + string str; + cin >> str; + sum = 0; + int sz = str.size(); + for (int i = 0; i < sz; i++) { + sum += (str[i] - '0'); + } + printf("%lld\n", sum); + } + return 0; +} + diff --git a/spoj_cpp_clean/ALCATRAZ2/ALCATRAZ2-9.cpp b/spoj_cpp_clean/ALCATRAZ2/ALCATRAZ2-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa00af7bb7556fa5ce94361e195f6a1d1d9642ce --- /dev/null +++ b/spoj_cpp_clean/ALCATRAZ2/ALCATRAZ2-9.cpp @@ -0,0 +1,91 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +const int N = 8; +const unsigned int MAXN = 1 << 8; +vector< pair > E; +int A[N]; +int M; + +inline void init() +{ + int i; + pair t; + + for (i = 0; i < N; i++) + scanf("%d", &A[i]); + + scanf("%d", &M); + + for (i = 0; i < M; i++) + { + scanf("%d %d", &t.first, &t.second); + t.first--; + t.second--; + E.push_back(t); + } +} + +inline bool test(unsigned int &bset, int pos) +{ + return bset & (1 << pos); +} + +inline bool check(unsigned int &bset) +{ + int i; + + for (i = 0; i < M; i++) + if (test(bset, E[i].first) && test(bset, E[i].second)) + return 0; + + return 1; +} + +inline int sum(unsigned int &bset) +{ + int i; + int rval = 0; + + for (i = 0; i < N; i++) + if (test(bset, i)) + rval += A[i]; + + return rval; +} + +inline int solve() +{ + int rval = 0; + unsigned int bset = 0; + + for (bset = 0; bset < MAXN; bset++) + if (check(bset)) + rval = max(rval, sum(bset)); + + return rval; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + + init(); + printf("%d\n", solve()); + + return 0; +} diff --git a/spoj_cpp_clean/ALCHE/ALCHE-13.cpp b/spoj_cpp_clean/ALCHE/ALCHE-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2059e973d42bd7a1808c2770f20118c890110e5 --- /dev/null +++ b/spoj_cpp_clean/ALCHE/ALCHE-13.cpp @@ -0,0 +1,48 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +const double eps = 1e-9; +const double ratio = 1000.0/37.0; +int main(){ + //freopen("data.in","r",stdin); + int ni, nw; + while(scanf("%d %d",&ni, &nw)==2 && ni!=-1 && nw!=-1){ + double sc2 = (double)ni / (double)nw; + + if((fabs(ratio - sc2)*2.0)/(ratio + sc2) <= eps) + printf("Y\n"); + else printf("N\n"); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ALICESIE/ALICESIE-7.cpp b/spoj_cpp_clean/ALICESIE/ALICESIE-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b43e71265f9c5c0e2ab220a4163295bf67f4d01c --- /dev/null +++ b/spoj_cpp_clean/ALICESIE/ALICESIE-7.cpp @@ -0,0 +1,21 @@ +#include +#include + +using namespace std; + +int main() { + int t; + long long int n; + scanf( "%d", &t ); + while ( t > 0 ) { + scanf( "%lld", &n ); + if ( n % 2 == 0 ) { + printf( "%lld\n", n / 2 ); + } + else { + printf( "%lld\n", n / 2 + 1 ); + } + --t; + } + return 0; +} diff --git a/spoj_cpp_clean/ALIEN/ALIEN-7.cpp b/spoj_cpp_clean/ALIEN/ALIEN-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edfb5258a33cea85102c3d842ae382e1e606b538 --- /dev/null +++ b/spoj_cpp_clean/ALIEN/ALIEN-7.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int max(int x,int y) +{ + return (x>y)?x:y; +} + +int min(int x,int y) +{ + return (x v; + vector::iterator i; + + scanf("%d",&n); + for (int i = 0; i < n; ++i) + { + scanf("%d %d",&m,&pers); + v.clear(); + for (int i = 0; i < m; ++i) + { + scanf("%d",&aux); + v.push_back(aux); + } + tam=0;suma=0;maxi=0;ini=0;min_sum=2000000000; + for (int i = 0; i < m; ++i) + { + suma+= v[i]; + tam++; + while(suma>pers) + { + suma-= v[ini]; + tam--; + ini++; + } + if (tam>=maxi) + { + if (tam==maxi) + { + min_sum= min(suma,min_sum); + }else + { + min_sum= suma; + } + } + maxi= max(maxi,tam); + } + printf("%d %d\n",min_sum,maxi ); + } + return 0; +} diff --git a/spoj_cpp_clean/ALLIZWEL/ALLIZWEL-14.cpp b/spoj_cpp_clean/ALLIZWEL/ALLIZWEL-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..896221972d0f677d9c6e16192a347460ca0d5285 --- /dev/null +++ b/spoj_cpp_clean/ALLIZWEL/ALLIZWEL-14.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +int r, c; +char graph[105][105]; +string sample = "ALLIZZWELL"; +bool checker = 0, visit[109][109]; +char test[20]; + +void dfs(int row, int col, int indx) + +{ + if(indx > 9) return; + if(row < 0 || col < 0 || row >= r || col >= c) { + return; + } + if(visit[row][col]) { + //cout << 5 << endl; + return; + } + + test[indx] = graph[row][col]; + if(test[indx] != sample[indx]) { + return; + } + + visit[row][col] = 1; + //cout << test[indx] << endl + if(indx == 9 && test[9] == sample[9]) { + checker = 1; + } + if(checker) + return; + + dfs(row - 1, col - 1, indx + 1); dfs(row - 1, col, indx + 1); dfs(row - 1, col + 1, indx + 1); + dfs(row, col -1, indx + 1); dfs(row, col + 1, indx + 1); + dfs(row + 1, col - 1, indx + 1); dfs(row + 1, col, indx + 1); dfs(row + 1, col + 1, indx + 1); + visit[row][col] = 0; +} + +int main() + +{ + int t; + scanf("%d", &t); + while(t--) { + scanf("%d %d", &r, &c); + + for(int i = 0; i < r; i++) { + for(int j = 0; j < c; j++) { + scanf(" %c", &graph[i][j]); + } + } + + checker = 0; + memset(visit, 0, sizeof(visit)); + + for(int i = 0; i < r; i++) { + for(int j = 0; j < c; j++) { + if(graph[i][j] == 'A') { + test[0] = graph[i][j]; + dfs(i, j, 0); + + for(int k = 0; k < r; k++) { + for(int l = 0; l < c; l++) { + visit[k][l] = 0; + } + } + + } + if(checker) + break; + } + + if(checker) + break; + } + + if(checker == 1) { + printf("YES\n"); + } + else + printf("NO\n"); + + /*if(t != 0) + printf("\n");*/ + } + + return 0; +} diff --git a/spoj_cpp_clean/ALLIZWEL/ALLIZWEL-9.cpp b/spoj_cpp_clean/ALLIZWEL/ALLIZWEL-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..882f7265e3132a8a41ba0397ffcfd2d1d03e3953 --- /dev/null +++ b/spoj_cpp_clean/ALLIZWEL/ALLIZWEL-9.cpp @@ -0,0 +1,109 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +int dx[]={-1, -1, -1, 0, 0, 1, 1, 1}; +int dy[]={-1, 0, 1, -1, 1, -1, 0, 1}; +char board[128][128]; +bool V[128][128]; +int R, C; +string S = "ALLIZZWELL"; + +inline void init() +{ + int i, j; + + scanf("%d %d\n", &R, &C); + memset(board, 0, sizeof(board)); + + for(i=0; i=R || y>=C) + return 0; + + if(board[x][y] != S[pos] || V[x][y]) + return 0; + + return -1; +} + +int solve(int x, int y, int pos) +{ + int i; + + int temp = check(pos, x, y); + + if(temp == 0) + return 0; + + if(temp == 1) + return 1; + + V[x][y] = 1; + + for(i=0; i<8; i++) + if(solve(x+dx[i], y+dy[i], pos+1)) + return 1; + + V[x][y] = 0; + return 0; +} + +inline bool solvee() +{ + int i, j; + + for(i=0; i +using namespace std; + +#define ll long long +ll ara[100009], n, dp[5009][5009]; + +ll solve(ll pos, ll last) + +{ + if(pos > n) + return 0; + ll &ret = dp[pos][last]; + if(ret != -1) + return ret; + + ret = 0; + if(last == 0 || ( abs(ara[last]) < abs(ara[pos]) && ( (ara[last] < 0 && ara[pos] > 0) || (ara[last] > 0 && ara[pos] < 0) ) ) ) + ret = 1 + solve(pos + 1, pos); + + ret = max(ret, solve(pos + 1, last)); + + return ret; +} + +int main() + +{ + memset(dp, -1, sizeof(dp)); + cin >> n; + for(ll i = 1; i <= n; i++) + scanf("%lld", &ara[i]); + + cout << solve(1, 0) << endl; + + return 0; diff --git a/spoj_cpp_clean/AMR10G/AMR10G-4.cpp b/spoj_cpp_clean/AMR10G/AMR10G-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f375fee56a3ff544d603e957918e5d3102e3d66 --- /dev/null +++ b/spoj_cpp_clean/AMR10G/AMR10G-4.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[1000000]; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T, mn; + s(T); + while(T--) + { + s(n);s(m); + FOR(i,0,n) + a[i]=haint(); + sort(a, a+n); + mn=INF; + FOR(i,0,n-m+1) + { + //error(i); + if(mn>a[i+m-1]-a[i]) + mn=a[i+m-1]-a[i]; + } + printf("%d\n",mn); + } + return 0; +} diff --git a/spoj_cpp_clean/AMR10G/AMR10G-9.cpp b/spoj_cpp_clean/AMR10G/AMR10G-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b4231d7e933f1306e82960adba4eacf8a0b5c37 --- /dev/null +++ b/spoj_cpp_clean/AMR10G/AMR10G-9.cpp @@ -0,0 +1,85 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +int A[20007]; +int N, K; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +inline void init() +{ + int i; + + //scanf("%d %d", &N, &K); + N = FAST_IO(); + K = FAST_IO(); + + for(i=0; i number theory > prime numbers > sieve of eratosthenes + difficulty: easy + date: 14/Nov/2019 + problem: print the n-th number that has at least 3 distinct prime factors + hint: use modified sieve + by: @brpapa +*/ +#include +#include +#include +#define MAX 2700 // max lucky number +using namespace std; + +int numDiffPFs[MAX]; + +void sieve() { + memset(numDiffPFs, 0, sizeof(numDiffPFs)); + + for (int i = 2; i < MAX; i++) { + if (numDiffPFs[i] == 0) + for (int j = i; j < MAX; j += i) + numDiffPFs[j]++; // cada j (múltiplo de i) tem i como um de seus fatores + } +} + +int main() { + int t, n; + sieve(); + + vector luckyNumbers; + for (int i = 0; luckyNumbers.size() < 1010; i++) + if (numDiffPFs[i] >= 3) + luckyNumbers.push_back(i); + + scanf("%d", &t); + while (t--) { + scanf("%d", &n); + cout << luckyNumbers[n-1] << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/AMR11E/AMR11E-7.cpp b/spoj_cpp_clean/AMR11E/AMR11E-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08f75020d9ed5068ab52cd8bcae9b01bf7f1e53f --- /dev/null +++ b/spoj_cpp_clean/AMR11E/AMR11E-7.cpp @@ -0,0 +1,79 @@ +#include +#define SIZE 1100 +using namespace std; + + +int primes[100000]; +vector p; + +void sieve(int top) +{ + memset(primes, false, sizeof(primes)); + for (int i = 2; i*i <= top ; ++i) + { + if (primes[i]==0) + { + for (int j = i*2; j <= top; j+=i) + { + primes[j]= primes[j]+1; + } + } + + + } + + for (int i = 2; i <= top; ++i) + { + if (primes[i]==0) + { + p.push_back(i); + } + } +} + +vector ans; +void LuckyNumber() +{ + for (int i = 0; i <= 3000; ++i) + { + int cont=0; + for (int j = 0; j =p[j] ; ++j) + { + if (i%p[j]==0) + { + cont++; + } + } + + if (cont>=3) + { + ans.push_back(i); + } + } +} + + + +int main() +{ + int t,n; + //printf("1\n"); + sieve(10000); + //printf("numero de numeros suertudos %d\n",(int) p.size() ); + /* + for (vector::iterator i = LuckyN.begin(); i != LuckyN.end(); ++i) + { + printf("%d, ",*i ); + } + */ + LuckyNumber(); + + + + scanf("%d",&t); + while(t--){ + scanf("%d",&n); + printf("%d\n",ans[n-1] ); + } + return 0; +} diff --git a/spoj_cpp_clean/AMR12D/AMR12D-21.cpp b/spoj_cpp_clean/AMR12D/AMR12D-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07c4a065443cd5079481682a7ba36c17fd1b61de --- /dev/null +++ b/spoj_cpp_clean/AMR12D/AMR12D-21.cpp @@ -0,0 +1,27 @@ +/* + string processing > palindrome + difficulty: none + date: none + by: @brpapa +*/ +#include +#include +using namespace std; + +bool ehPalindrome(string str) { + for (int i = 0; i < str.size() / 2; i++) + if (str[i] != str[str.size() - i - 1]) + return false; + return true; +} + +int main() { + int t; + string str; + cin >> t >> ws; + for (int i = 0; i < t; i++) { + getline(cin, str); + printf("%s\n", ehPalindrome(str) ? "YES" : "NO"); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ANARC05I/ANARC05I-3.cpp b/spoj_cpp_clean/ANARC05I/ANARC05I-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79473107bfc861eb99bac21d28001e4a20880639 --- /dev/null +++ b/spoj_cpp_clean/ANARC05I/ANARC05I-3.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + string input; + int points[1000][1000], l, x, y, ans; + cin>>input; + while(input[0] != 'Q'){ + memset(points, 0, sizeof(points)); + l = input.length(); + x = 0, y = 0, ans = 0; + points[x][y] = 1; + for(int i = 0; i < l-1 ; i++){ + if(input[i] == 'U') y ++; + else if(input[i] == 'D') y--; + else if(input[i] == 'L') x--; + else if(input[i] == 'R') x++; + //cout<>input; + } + return 0; +} diff --git a/spoj_cpp_clean/ANARC09A/ANARC09A-16.cpp b/spoj_cpp_clean/ANARC09A/ANARC09A-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc8e100b1b651c4497068f9d7f397ec852e473ca --- /dev/null +++ b/spoj_cpp_clean/ANARC09A/ANARC09A-16.cpp @@ -0,0 +1,58 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define ll long long +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +int solve(string a){ + int ans=0; + int n = a.length(); + stack b; + for(int i=0;i>test_cases; + string s; + int ind=1; + while(cin>>s){ + if(s[0]=='-'){ + return 0; + } + int ans = solve(s); + cout< +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) +#define ll long long int +using namespace std; + +/* +by:Ahmed Sakr (sakr_) with AZA ;l + +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + + __builtin_popcount(x); + +*/ + + +int main() +{Sakr_; + + string s; + int i,l,ans=0,c=1; + + while (cin>>s){ + if(s[0]=='-') break; + l=s.length(); + stackst; + + for(i=0 ;i +using namespace std; + +#define endl "\n" +#define ll long long +#define mod 1000000007 //10^9 +7 +#define MAX 100001 //10^5 +1 +#define PI (3.141592653589) +#define ps(x,y) fixed<> exp; + while (exp[0] != '-') { + cnt++; + int bracket = 0, ans = 0; + int n = exp.length(); + for (int i = 0; i < n; i++) { + if (exp[i] == '{') { + bracket++; + } else { + bracket--; + if (bracket == -1) { + ans++; + bracket = 1; + } + } + } + ans += (bracket / 2); + cout << cnt << ". " << ans << endl; + cin >> exp; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ANARC09A/ANARC09A-3.cpp b/spoj_cpp_clean/ANARC09A/ANARC09A-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb1faf1f60e4b1f27840f27a7f768d372b09e3a1 --- /dev/null +++ b/spoj_cpp_clean/ANARC09A/ANARC09A-3.cpp @@ -0,0 +1,65 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<> s; + int ca = 0; + while(s[0] != '-'){ + int cnt = 0, ans = 0; + + for(int i = 0; i < s.length(); i++){ + if(s[i] == '{') cnt++; + else{ + if(cnt == 0){ + ans++; + cnt++; + } + else cnt--; + } + } + + ans += cnt / 2; + + cout << ++ca << ". " << ans << endl; + + cin >> s; + } + return 0; +} diff --git a/spoj_cpp_clean/ANARC09A/ANARC09A-4.cpp b/spoj_cpp_clean/ANARC09A/ANARC09A-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbb1ad8c9a049c80e65a278ac5bc557b72484d26 --- /dev/null +++ b/spoj_cpp_clean/ANARC09A/ANARC09A-4.cpp @@ -0,0 +1,138 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + int cnt=1;string s; + while(cin>>s) + { + if(s[0]=='-') + break; + stack st; + st.push(s[0]); + FOR(i,1,s.length()) + { + if(!st.empty()) + { + char ch=st.top(); + if(ch=='{' && s[i]=='}') + st.pop(); + else if(s[i]=='}' || s[i]=='{') + st.push(s[i]); + } + else + st.push(s[i]); + } + string tmp; + int ans=0; + //error(st.size()); + while(!st.empty()) + { + tmp.clear(); + tmp+=st.top(); + st.pop(); + tmp+=st.top(); + st.pop(); + //error(tmp); + if(tmp=="{}") + ans+=2; + else + ans++; + } + cout< + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it) +#define meta __FUNCTION__,__LINE__ + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +using namespace std; + +const long double PI = 3.1415926535897932384626433832795; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void tr(S x, const Strings&... rest){cout< pii; + +const int N = 1000001; +int sieve[N], primes[N], pcnt; + +void getSieve(){ + for(int i = 2; i < N; i++){ + if(sieve[i] == 0){ + sieve[i] = i; primes[pcnt++] = i; + } + for(int j = 0; j < pcnt and primes[j] <= sieve[i] and i*primes[j] < N; j++) sieve[i*primes[j]] = primes[j]; + } + return; +} + +int main(){ + getSieve(); + int x, y, T = 1; + while(sd2(x,y) == 2){ + if(x == 0 and y == 0) break; + + map cnt; + while(x > 1){ + cnt[sieve[x]]++; + x /= sieve[x]; + } + + while(y > 1){ + cnt[sieve[y]]--; + y /= sieve[y]; + } + + int x = cnt.size(); + int d = 0; foreach(it, cnt) d += abs(it->se); + printf("%d. %d:%d\n", T++, x, d); + } + return 0; +} diff --git a/spoj_cpp_clean/ANARCO5B/ANARCO5B-4.cpp b/spoj_cpp_clean/ANARCO5B/ANARCO5B-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1519c2e957b3f8bfd894c51eb60c5e6facccfd75 --- /dev/null +++ b/spoj_cpp_clean/ANARCO5B/ANARCO5B-4.cpp @@ -0,0 +1,159 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m, n1, n2 ; +long long a1[10009], a2[10009]; +bool ck[10009], ckneg[10009]; +vector v1, v2, vec; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + while(s(n1)==1) + { + + if(n1==0) + break; + vec.clear(); v1.clear(); v2.clear(); + memset(ck, false, sizeof(ck)); + memset(ckneg, false, sizeof(ckneg)); + FOR(i,0,n1) + { + + sl(a1[i]); + if(a1[i]>=0) + ck[a1[i]]=true; + else + ckneg[abs(a1[i])]=true; + } + s(n2); + FOR(i,0,n2) + { + sl(a2[i]); + if(ck[a2[i]]==true || ckneg[abs(a2[i])]==true) + vec.push_back(a2[i]); + } +// error(vec.size()); + int t1=0, t2=0; + long long sum; + FOR(i,0,vec.size()) + { +// error(vec[i]); + sum=0; + while(a1[t1]<=vec[i] && t1 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + long long t, n, num[30000], k, result; + cin>>t; + while(t--){ + scanf("%lld %lld",&n, &k); + //sd2(n,k); + for(int i = 0; i < n; i++){ + scanf("%lld",&num[i]); + } + result = num[0]; + if(k > n/2){ + for(int i = 0; i < n; i++){ + result &= num[i]; + } + for(int i = 0; i < n; i++){ + printf("%lld ",result); + } + printf("\n"); + } + else{ + for(int i = 0; i < 31; i++){ + for(int j = 0; j < n; j++){ + if((num[j]&(1< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define max(a,b) ((a>b)?a:b) + +using namespace std; + +class ufs +{ + private: + struct set + { + int name; + int rank; + set *parent; + }; + int ssize; + vector S; + + set *fs(int name) + { + if(S[name].parent->name==name) + return &S[name]; + + S[name].parent=fs(S[name].parent->name); + return S[name].parent; + } + + + public: + ufs(int n) + { + int i; + ssize=n; + S.resize(ssize); + + for(i=0; iname; + } + + bool is_same_set(int a, int b) + { + return fs(a)->name==fs(b)->name; + } + + void union_sets(int a, int b) + { + set *f, *s; + + f=fs(a); + s=fs(b); + + if(f->name==s->name) + return; + + ssize--; + if(f->rank==s->rank) + { + f->rank++; + s->parent=f; + } + + else if(f->rank > s->rank) + s->parent=f; + + else + f->parent=s; + } + + int size() + { + return ssize; + } +}; + + +struct point +{ + int x; + int y; +}; + +struct segment +{ + struct point begin; + struct point end; +}; + +int ABbegin, ABend, BAbegin, BAend; +int N, M; +vector ALL; + +inline int CCW(struct point p1, struct point p2, struct point p3) +{ + float res=(p2.x - p1.x)*(p3.y - p1.y) - (p2.y - p1.y)*(p3.x - p1.x); + + if(res>0) + return 1; + else if(res<0) + return -1; + else + return 2; +} + +inline bool same_point(struct segment A, struct segment B) +{ + if(A.begin.x == B.begin.x && A.begin.y == B.begin.y) + return 1; + if(A.end.x == B.begin.x && A.end.y == B.begin.y) + return 1; + if(A.begin.x == B.end.x && A.begin.y == B.end.y) + return 1; + if(A.end.x == B.end.x && A.end.y == B.end.y) + return 1; + return 0; +} + +inline bool crosswise() +{ + if(ABbegin + ABend==0) + if(BAbegin +BAend==0) + return 1; + return 0; +} + +inline bool intersect_at_ends() +{ + if(ABbegin + ABend==0) + if(BAbegin == 2 || BAend == 2) + return 1; + + if(BAbegin + BAend==0) + if(ABbegin == 2 || ABend == 2) + return 1; + + return 0; +} + +inline double distancee(struct point A, struct point B) +{ + return sqrt( pow((float)A.x-B.x, 2) + pow((float)A.y-B.y, 2)); +} + +int extended(struct segment A, struct segment B) +{ + double dist=0, distA=0, distB=0; + + if(ABbegin == 2 && ABend == 2 ) + { + dist=distancee(A.begin, B.begin); + dist=max(dist, distancee(A.begin, B.end)); + dist=max(dist, distancee(A.end, B.begin)); + dist=max(dist, distancee(A.end, B.end)); + + distA=distancee(A.begin, A.end); + distB=distancee(B.begin, B.end); + + if(dist<=distA + distB) + return 1; + } + + return 0; +} + +inline bool intersect(struct segment A, struct segment B) +{ + ABbegin=CCW(A.begin, A.end, B.begin); + ABend=CCW(A.begin, A.end, B.end); + BAbegin=CCW(B.begin, B.end, A.begin); + BAend=CCW(B.begin, B.end, A.end); + + if(same_point(A, B)) + return 1; + + if(crosswise()) + return 1; + + if(intersect_at_ends()) + return 1; + + if(extended(A, B)) + return 1; + + return 0; +} + +void init() +{ + int i, j; + int A, B; + struct segment temp; + + scanf("%d %d", &N, &M); + + ALL.clear(); + ALL.push_back(temp); + + for(i=0; i +#include + +using namespace std; + +int main() +{ + int test,n,i,j,k,l,pay; + cin>>test; + while(test--) + { + cin>>n; + int *arr=new int[n]; + for(i=0;i>arr[i]; + sort(arr,arr+n); + j=n/4; + l=n%4; + k=n-1; + pay=0; + for(i=0;i +#include + +using namespace std; + +int main() +{ + int test,n,i,j,k,l,pay; + cin>>test; + while(test--) + { + cin>>n; + int *arr=new int[n]; + for(i=0;i>arr[i]; + sort(arr,arr+n); + reverse(arr,arr+n); + pay=0; + for(i=0;i + +using namespace std; + +int main(void) +{ + int t; + long long int term3, lastTerm3, sum; + long long int n, d, a; + + cin >> t; + + while(t--) + { + cin >> term3 >> lastTerm3 >> sum; + + n = (2ll * sum) / (term3 + lastTerm3); + + d = (lastTerm3 - term3) / (n - 5ll); + + a = term3 - (2ll * d); + + cout << n << endl; + + for(long long int i = 0ull; i < n; i++) + { + cout << a << " "; + a = a + d; + } + + cout << endl; + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/AP2/AP2-16.cpp b/spoj_cpp_clean/AP2/AP2-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d5dc17e57ab07de83c24788360a06397cc8979c --- /dev/null +++ b/spoj_cpp_clean/AP2/AP2-16.cpp @@ -0,0 +1,43 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define ll long long +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(){ + ll x, y, s; + cin>>x>>y>>s; + ll n = (2*s)/(x+y); + ll d=1; + if(n==5){ + d=0; + } + else{ + d = (y-x)/(n-5); + } + ll a = x-2*d; + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/AP2/AP2-19.cpp b/spoj_cpp_clean/AP2/AP2-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fd55e5eba59e5fad2d6c98dec62dd4949affd25 --- /dev/null +++ b/spoj_cpp_clean/AP2/AP2-19.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int t;cin>>t;while(t--){ + long long n, a, sum, b, l, d; + cin>>b>>l>>sum; + n=(sum*2)/(b+l); + d=((l-b)/(n-5)); + a=b-(d*2); + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t; + long long a, b, sum, diff, n; + sd(t); + while(t--){ + scanf("%lld %lld %lld", &a, &b, &sum); + n = (sum*2)/(a+b); + diff = (b-a)/(n-5); + a = a - 2*diff; + printf("%lld\n%lld ",n,a); + for(long long i = 1; i < n; i++){ + a += diff; + printf("%lld ",a); + } + printf("\n"); + } + return 0; +} diff --git a/spoj_cpp_clean/AP2/AP2-5.cpp b/spoj_cpp_clean/AP2/AP2-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08017ae3644caf046f06e6638b458eab07eaacfe --- /dev/null +++ b/spoj_cpp_clean/AP2/AP2-5.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +int main() +{ + long long int test,i,a,x,y,sum,n,d,term; + cin>>test; + while(test--) + { + cin>>x>>y>>sum; + n=(2*sum)/(x+y); + d=(y-x)/(n-5); + a=x-(2*d); + term=a; + cout< +#include +using namespace std; + +int main() +{ + int t, d, i; + size_t l; + string s; + char w; + while(cin >> t >> d && !(t == 0 && d == 0)) + { + cin >> s; + while(d-- > 0) + { + l = s.length() - 1; + w = '9'; + for(i = s.length() - 1; i>=0; i--) + { + if(s[i] <= w) + { + l = i; + w = s[i]; + } + } + s.erase(l, 1); + } + cout << s << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/APIO10A/APIO10A-14.cpp b/spoj_cpp_clean/APIO10A/APIO10A-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2b1d6fe099de3167f10bc63714d77e5fe5d487a --- /dev/null +++ b/spoj_cpp_clean/APIO10A/APIO10A-14.cpp @@ -0,0 +1,112 @@ +// #pragma GCC optimize("Ofast,unroll-loops") +// #pragma GCC target("avx,avx2,fma") + +#include +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define all(v) v.begin(), v.end() +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; ++i) +#define for0(i, stop) for(ll i = 0; i < stop; ++i) +#define rep1(i, start) for(ll i = start; i >= 1; --i) +#define rep0(i, start) for(ll i = (start-1); i >= 0; --i) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define EL '\n' +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +const ll sz = 1e6 + 10; + +int pnt[sz], ara[sz], pre[sz]; +ll dp[sz]; + +struct Line { + int m; ll c; +} tree[4*sz]; + +int exist[4*sz], state; + +inline ll f(Line &line, ll x) { + return (ll)line.m*x + line.c; +} + +void add(ll lo, ll hi, Line line, ll node) +{ + if(exist[node] != state) tree[node] = {0, -inf}; + exist[node] = state; + + if(lo == hi) { + if(f(line, pnt[lo]) > f(tree[node], pnt[lo])) + tree[node] = line; + return; + } + + ll mid = lo+hi >> 1; + + bool l = f(line, pnt[lo]) > f(tree[node], pnt[lo]); + bool m = f(line, pnt[mid]) > f(tree[node], pnt[mid]); + + if(m) swap(tree[node], line); + + if(l != m) add(lo, mid, line, node<<1); + else add(mid +1, hi, line, node<<1|1); +} + +ll query(ll lo, ll hi, ll idx, ll node) +{ + if(lo == hi) + return f(tree[node], pnt[idx]); + + ll mid = lo+hi >> 1, ret = f(tree[node], pnt[idx]); + + if(idx <= mid && exist[node<<1] == state) ret = max(ret, query(lo, mid, idx, node<<1)); + else if(idx > mid && exist[node<<1|1] == state) ret = max(ret, query(mid+1, hi, idx, node<<1|1)); + + return ret; +} + +inline ll sq(ll num) {return num*num;} + +int main() +{ + ll t; + cin >> t; + while(t--) { + state++; + + ll n, a, b, c; + scanf("%lld %lld %lld %lld", &n, &a, &b, &c); + + for1(i, n) { + si(ara[i]); + pre[i] = pre[i-1] + ara[i]; + + pnt[i] = pre[i]; + } + + add(1, n, {0, 0}, 1); + + for1(i, n) { + + dp[i] = query(1, n, i, 1) + a*sq(pre[i]) + b*pre[i] + c; + + add(1, n, {-2*pre[i]*a, a*sq(pre[i]) - b*pre[i] + dp[i]}, 1); + } + + pf("%lld\n", dp[n]); + } + return 0; diff --git a/spoj_cpp_clean/APS/APS-14.cpp b/spoj_cpp_clean/APS/APS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..980d8ee546aa054f39cfc726303371bb85ad4485 --- /dev/null +++ b/spoj_cpp_clean/APS/APS-14.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +#define ll int + +ll dv[10000009]; +long long ans[10000009]; +bool mark[3170]; + +int main() + +{ + for(ll i = 2; i <= 3163; i++) { + if(mark[i] == 0) { + for(ll j = i + i; j <= 10000000; j += i) { + if(j < 3170) + mark[j] = 1; + + if(dv[j] == 0) + dv[j] = i; + else + dv[j] = min(dv[j], i); + } + } + } + + for(ll i = 2; i <= 10000000; i++) { + if(dv[i] == 0) + ans[i] = ans[i - 1] + (long long)i; + else + ans[i] = ans[i - 1] + (long long)dv[i]; + } + + ll t; + cin >> t; + while(t--) { + ll n; + scanf("%d", &n); + printf("%lld\n", ans[n]); + } + + return 0; +} diff --git a/spoj_cpp_clean/ARITH2/ARITH2-5.cpp b/spoj_cpp_clean/ARITH2/ARITH2-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2de7674f3a05ed2a2cf8d4fbe9c38cc5bdb4c642 --- /dev/null +++ b/spoj_cpp_clean/ARITH2/ARITH2-5.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int main() +{ + long long int num1,num2,num3,result,i,j,k,test; + char opr[2]; + cin>>test; + while(test--) + { + cin>>num1>>opr>>num2; + if(opr[0]=='+') + result=num1+num2; + else if(opr[0]=='-') + result=num1-num2; + else if(opr[0]=='*') + result=num1*num2; + else if(opr[0]=='/') + result=num1/num2; + cin>>opr; + while(opr[0]!='=') + { + cin>>num2; + if(opr[0]=='+') + result=result+num2; + else if(opr[0]=='-') + result=result-num2; + else if(opr[0]=='*') + result=result*num2; + else if(opr[0]=='/') + result=result/num2; + cin>>opr; + } + cout< + +using namespace std; + +int main() +{ + int t; + string operador; + long long int ans, aux; + + scanf("%d",&t); + for (int i = 0; i < t; ++i) + { + scanf("%lld",&ans); + while(cin>>operador && operador[0]!='=') + { + scanf("%lld",&aux); + if (operador[0]=='+') + { + ans+= aux; + }else if (operador[0]=='-') + { + ans-= aux; + }else if (operador[0]=='*') + { + ans*= aux; + }else if (operador[0]=='/') + { + ans/= aux; + } + + } + + printf("%lld\n",ans ); + } + return 0; +} diff --git a/spoj_cpp_clean/ARMY/ARMY-11.cpp b/spoj_cpp_clean/ARMY/ARMY-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..127854e2d0e8a095956fb1634190a8d60aad99e9 --- /dev/null +++ b/spoj_cpp_clean/ARMY/ARMY-11.cpp @@ -0,0 +1,60 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include + +using namespace std; + +void findWinner(int *godzilla, int *mechagodzilla, int ng, int nm) +{ + int maxGodzilla, maxMechaGodzilla; + + maxGodzilla = -1; + + for(int i = 0; i < ng; i++) + if(godzilla[i] > maxGodzilla) + maxGodzilla = godzilla[i]; + + maxMechaGodzilla = -1; + + for(int i = 0; i < nm; i++) + if(mechagodzilla[i] > maxMechaGodzilla) + maxMechaGodzilla = mechagodzilla[i]; + + if(maxGodzilla < maxMechaGodzilla) + cout << "MechaGodzilla" << endl; + else + cout << "Godzilla" << endl; +} + +int main(void) +{ + int t; + int ng, nm; + int *godzilla, *mechagodzilla; + + cin >> t; + + while(t--) + { + cin >> ng >> nm; + + godzilla = new int[ng]; + mechagodzilla = new int[nm]; + + for(int i = 0; i < ng; i++) + cin >> godzilla[i]; + + for(int i = 0; i < nm; i++) + cin >> mechagodzilla[i]; + + findWinner(godzilla, mechagodzilla, ng, nm); + + delete[] godzilla; + delete[] mechagodzilla; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ARMY/ARMY-13.cpp b/spoj_cpp_clean/ARMY/ARMY-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d82cb1e68dae4b3e49bc0b9794000db60cd23876 --- /dev/null +++ b/spoj_cpp_clean/ARMY/ARMY-13.cpp @@ -0,0 +1,59 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +int main(){ + //freopen("data.in","r",stdin); + int T; scanf("%d", &T); + for(int t=0; t a){ + printf("MechaGodzilla\n"); + }else{ + printf("Godzilla\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ARMY/ARMY-16.cpp b/spoj_cpp_clean/ARMY/ARMY-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..085c58513d400871242ec6e1b91dfb10f1ca3bb2 --- /dev/null +++ b/spoj_cpp_clean/ARMY/ARMY-16.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +void solve(){ + int n,m; + cin>>n>>m; + vector a(n); + vector b(m); + for(int i=0;i>a[i]; + } + for(int i=0;i>b[i]; + } + sort(a.begin(),a.end()); + sort(b.begin(),b.end()); + + if(a[n-1]>test; + while(test--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/ARMY/ARMY-19.cpp b/spoj_cpp_clean/ARMY/ARMY-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be7385409d2dd45c80dadcd94bcd27e0a0b42d22 --- /dev/null +++ b/spoj_cpp_clean/ARMY/ARMY-19.cpp @@ -0,0 +1,39 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 + +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + string blank; + int t;cin>>t;while(t--){ + getline(cin, blank); + int NG, NM; + cin>>NG>>NM; + int godzilla[NG]; + int MechaG[NM]; + for(ll i=0; i>godzilla[i]; + } + for(ll i=0; i>MechaG[i]; + } + sort(godzilla, godzilla+NG); + sort(MechaG, MechaG+NM); + + if(godzilla[NG-1]>=MechaG[NM-1]){ + cout<<"Godzilla"; + } + else{ + cout<<"MechaGodzilla"; + } + + if(t!=0){cout< +#include +#include + +using namespace std; + +int main() +{ + int test,NG,NM,i,j; + cin>>test; + while(test--) + { + cin>>NG>>NM; + int *arrNG=new int[NG]; + int *arrNM=new int[NM]; + for(i=0;i>arrNG[i]; + for(j=0;j>arrNM[j]; + sort(arrNG,arrNG+NG); + sort(arrNM,arrNM+NM); + i=0;j=0; + while(1) + { + if(arrNM[j]<=arrNG[i]) + { + if(j==NM-1) + { + cout<<"Godzilla"< + +using namespace std; + +int main() +{ + int t,n,m,maxa=0,maxb=0,aux; + + scanf("%d",&t); + while(t--) + { + printf("\n"); + scanf("%d %d",&n,&m); + maxa=0; + maxb=0; + for (int i = 0; i < n; ++i) + { + scanf("%d",&aux); + if (aux>maxa) + { + maxa=aux; + } + } + + for (int i = 0; i < m; ++i) + { + scanf("%d",&aux); + if (aux>maxb) + { + maxb=aux; + } + } + + (maxa>=maxb)?printf("Godzilla\n"):printf("MechaGodzilla\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/ARMY/ARMY-9.cpp b/spoj_cpp_clean/ARMY/ARMY-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a026c18f8987ef1d2919c4653061694cc0b163c --- /dev/null +++ b/spoj_cpp_clean/ARMY/ARMY-9.cpp @@ -0,0 +1,62 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +priority_queue Godzilla, MechaGodzilla; +int NG, NM; + +int main() +{ + int T; + int i; + int num; + + scanf("%d", &T); + + while(T--) + { + scanf("%d %d", &NG, &NM); + + Godzilla = priority_queue (); + MechaGodzilla = priority_queue (); + + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, n; + long long nums[100000]; + sd(t); + while(t--){ + sd(n); + for(int i = 0; i < n; i++){ + sd(num[i]); + } + if(n == 1){ + cout< +#define hell 1000000009 +#define bug1(x) cout<<"$ "<=b;i--) +#define loop(i,a,b) for(long long i=a; i +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(){ + ll n, k; + cin>>n; + vll a(n); + loop(i,0,n) cin>>a[i]; + cin>>k; + deque< pll > dq; + vll dp(n,0); + dq.push_front({a[0],0}); + loop(i,0,n){ + ll rearValue=dq.back().first; + ll frontIndex=dq.front().second; + while(!dq.empty() and frontIndex<=i-k){ + dq.pop_front(); + frontIndex=dq.front().second; + } + while(!dq.empty() and a[i]>rearValue){ + dq.pop_back(); + rearValue=dq.back().first; + } + dq.push_back({a[i],i}); + dp[i]=dq.front().first; + } + loop(i,k-1,n){ + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/ARRAYSUB/ARRAYSUB-20.cpp b/spoj_cpp_clean/ARRAYSUB/ARRAYSUB-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ce9789a82ffd079494a228c94b1c7e9f13bcc6e --- /dev/null +++ b/spoj_cpp_clean/ARRAYSUB/ARRAYSUB-20.cpp @@ -0,0 +1,90 @@ +#include + +#define endl '\n' +#define sqr(x) (x) * (x) +#define gcd(x,y) __gcd(x,y) +#define lcm(x,y) ((x/gcd(x,y)) * y) +#define sz(x) (int)x.size() +#define all(x) (x).begin(),(x).end() +#define rall(x) (x).rbegin(),(x).rend() +#define prec(x) fixed<; +using pll = pair; + +const ld PI = acos((ld) - 1); +const ll MOD = 1e9 + 7; +const ll INF = 2e18 + 1; +const ld EPS = 1e-9; +const ll MX = 1e6 + 5; + +#ifdef LOCAL +#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) +template < typename Arg1 > +void __f(const char* name, Arg1&& arg1) { + cerr << name << " = " << arg1 << endl; +} +template < typename Arg1, typename... Args> +void __f(const char* names, Arg1&& arg1, Args&&... args) { + const char* comma = strchr(names + 1, ','); + cerr.write(names, comma - names) << " = " << arg1 << " | "; + __f(comma + 1, args...); +} +#else +#define debug(...) +#endif + +int a[MX], n, k; + + +void solve() { + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + cin >> k; + multiset ms; + for (int i = 1; i <= k; i++) { + ms.insert(a[i]); + } + auto it = --ms.end(); + cout << *it << " "; + for (int i = 1; i <= n - k; i++) { + auto x = ms.find(a[i]); // to find and remove the left most element + ms.erase(x); + ms.insert(a[i + k]); + //debug(a[i], a[i + k]); + it = --ms.end(); + cout << *it << " "; + } +} + +int main() { + +#ifdef LOCAL + clock_t tStart = clock(); + freopen("in.txt", "r", stdin); + freopen("out.txt", "w", stdout); +#endif + unsyncIO; + int t = 1; + //cin >> t; + while (t--) { + solve(); + } + +#ifdef LOCAL + cerr << "\nRuntime: " << (ld) (clock() - tStart) / CLOCKS_PER_SEC << " Seconds" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ARRAYSUB/ARRAYSUB-4.cpp b/spoj_cpp_clean/ARRAYSUB/ARRAYSUB-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1526f8cd49a8556035cca580112af3e22c435926 --- /dev/null +++ b/spoj_cpp_clean/ARRAYSUB/ARRAYSUB-4.cpp @@ -0,0 +1,117 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[1000009];//, lft[1000009], rig[1000009]; +multiset mst; +multiset ::iterator it; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T, cnt=0; + s(n); + FOR(i,0,n) + cin>>a[i]; + s(m); + FOR(i,0,m) + mst.insert(a[i]); + + it=mst.end(); + it--; + cout<<*it;//<<" "; + mst.erase(mst.find(a[0])); + + FOR(i,m,n) + {/* + //error(i);error(a[i]);cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#define max(a,b) (a>b)?a:b + +int tree[1000007*4]; +int A[1000007]; +int N, K; + +void init() +{ + int i; + + scanf("%d", &N); + + for(i = 1; i<=N; i++) + scanf("%d", &A[i]); + + scanf("%d", &K); +} + +void init_tree(int index, int a, int b) +{ + if(a==b) + { + tree[index] = A[a]; + return; + } + + init_tree(index*2, a, (a+b)/2); + init_tree(index*2+1, (a+b)/2+1, b); + + tree[index] = max(tree[index*2], tree[index*2+1]); +} + +int query(int index, int a, int b, int i, int j) +{ + int mid; + + if(a==i && b==j) + return tree[index]; + + mid = (a+b)/2; + + if(j <= mid) + return query(index*2, a, mid, i, j); + else if(i > mid) + return query(index*2+1, mid+1, b, i, j); + + return max(query(index*2, a, mid, i, mid), query(index*2+1, mid + 1, b, mid + 1, j)); +} + +int main() +{ + int i; + + //freopen("input.txt","r",stdin); + + init(); + init_tree(1, 1, N); + for(i=K; i<=N; i++) + printf("%d ", query(1, 1, N, i - K + 1, i)); + + return 0; +} diff --git a/spoj_cpp_clean/ARRPRM/ARRPRM-9.cpp b/spoj_cpp_clean/ARRPRM/ARRPRM-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..693550d3af764bc2cb5c1be6ae22dd88b99800de --- /dev/null +++ b/spoj_cpp_clean/ARRPRM/ARRPRM-9.cpp @@ -0,0 +1,108 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define MAXN 2007 +using namespace std; + +vector primes; +bool is_prime[MAXN]; +int memo[MAXN]; +int A[MAXN]; +int SUM[MAXN]; +int N; + +inline void sieve() +{ + int i, j; + + primes.push_back(2); + + memset(is_prime, 1, sizeof(is_prime)); + + for (i = 4; i < MAXN; i += 2) + is_prime[i] = 0; + + for (i = 3; i < MAXN; i += 2) + if (is_prime[i]) + { + primes.push_back(i); + + for (j = i + i; j < MAXN; j += i) + is_prime[j] = 0; + } +} + +inline void init() +{ + int i; + + cin >> N; + + for (i = 1; i <= N; i++) + { + cin >> A[i]; + SUM[i] = SUM[i - 1] + A[i]; + } + + memset(memo, -1, sizeof(memo)); +} + +inline int get_sum(int b, int e) +{ + return SUM[e] - SUM[b - 1]; +} + +int f(int left) +{ + int b, i, rval = 0; + + if (left <= 1) + return 0; + + if (memo[left] != -1) + return memo[left]; + + b = N - left + 1; + + for (i = 0; primes[i] <= left && i < primes.size(); i++) + rval = max(rval, get_sum(b, b + primes[i] - 1) + f(left - primes[i] - 1)); + + rval = max(rval, f(left - 1)); + + memo[left] = rval; + + return rval; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + int T; + + sieve(); + + cin >> T; + + while (T--) + { + init(); + cout << f(N) << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/ASCDFIB/ASCDFIB-14.cpp b/spoj_cpp_clean/ASCDFIB/ASCDFIB-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb865498a3f4b0892598e570f57d58aac68bb82f --- /dev/null +++ b/spoj_cpp_clean/ASCDFIB/ASCDFIB-14.cpp @@ -0,0 +1,120 @@ +#include +using namespace std; + +#define ll long long +const ll MOD = 100000; +const ll MOD2 = MOD * MOD; /// Only when (MOD * MOD) fits into long long +#define row 2 +#define col 2 + +struct MatExpo + +{ + ll exponents[26][row][col]; + ll ident[row][col] = { {1, 0}, {0, 1} }; /// Identity Matrix + ll result[row][col], mat[row][col]; + + MatExpo() { + + /// Creating Base Matrix + ll base[row][col] = { {1, 1}, {1, 0} }; + memcpy(exponents[0], base, sizeof(base)); + + /// Calculating all exponents + for(ll p = 1; p < 26; p++) { + + for(ll i = 0; i < row; i++) { + for(ll j = 0; j < col; j++) { + ll tmp = 0; + for(ll k = 0; k < col; k++) { + tmp += exponents[p - 1][i][k] * exponents[p - 1][k][j]; + while(tmp >= MOD2) /// Taking modulo MOD2 is easy, because we can do it by subtraction + tmp -= MOD2; + } + exponents[p][i][j] = tmp % MOD; + } + } + } + + } + + ll ans(ll m) { + + /// Return from base case + if(m < 0) + return 0; + if(m == 0 || m == 1) + return 1; + + memcpy(mat, ident, sizeof(ident)); + ll n = m - 1; /// Here, (n - 1)th power of base matrix represents the nth term + for(ll p = 25; p >= 0; p--) { + if((n >> p) & 1) { + + for(ll i = 0; i < row; i++) { + for(ll j = 0; j < col; j++) { + ll tmp = 0; + for(ll k = 0; k < col; k++) { + tmp += mat[i][k] * exponents[p][k][j]; + while(tmp >= MOD2) /// Taking modulo MOD2 is easy, because we can do it by subtraction + tmp -= MOD2; + } + result[i][j] = tmp % MOD; + } + } + + memcpy(mat, result, sizeof(result)); + } + + } + + return (result[0][0] + result[0][1]) % MOD; + } + +}; + +// MatExpo ex = MatExpo(); +// ans = ex.ans(n); nth term, term count starts from 0 + +ll check[MOD + 9]; +ll ans[1200009]; + +int main() + +{ + MatExpo ex = MatExpo(); + for(ll i = 1; i <= 1200000; i++) + ans[i] = ex.ans(i - 2); + + ll t, cs = 0; + cin >> t; + while(t--) { + memset(check, 0, sizeof(check)); + ll a, b; + scanf("%lld %lld", &a, &b); + ll r = a + b; + + printf("Case %lld:", ++cs); + for(ll i = a; i <= r; i++) + check[ ans[i] ]++; + + ll cnt = 0; + for(ll i = 0; i < MOD; i++) { + if(check[i] == 0) + continue; + for(ll j = 1; j <= check[i]; j++) { + printf(" %lld", i); + cnt++; + if(cnt == 100) + break; + } + + if(cnt == 100) + break; + } + + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/ASCDFIB/ASCDFIB-3.cpp b/spoj_cpp_clean/ASCDFIB/ASCDFIB-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a07f5d4d494363ba7ff5a4db3ad933a9b2100e60 --- /dev/null +++ b/spoj_cpp_clean/ASCDFIB/ASCDFIB-3.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int t, a, b, MOD = 100000, fib[1100001], s[100000], i, j; + +int main(){ + int count; + fib[1] = 0; + fib[2] = 1; + for(i = 3; i <= 1100000; i++){ + fib[i] = (fib[i-1] + fib[i-2])%MOD; + } + sd(t); + for(j = 1; j <= t; j++){ + for(i = 0; i < MOD; i++) + s[i] = 0; + sd2(a,b); + for(i = a; i <= a+b; i++) + s[fib[i]]++; + printf("Case %d:",j); + b += 1; + if (b > 100) b = 100; + i = 0; + while(i < 100000 and b){ + while(s[i] > 0 and b){ + printf(" %d",i); + s[i]--; + b--; + } + i++; + } + printf("\n"); + } +} diff --git a/spoj_cpp_clean/ASCDFIB/ASCDFIB-8.cpp b/spoj_cpp_clean/ASCDFIB/ASCDFIB-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e20eacdc0badcf2dfbb4bef07f828ffca3c7872f --- /dev/null +++ b/spoj_cpp_clean/ASCDFIB/ASCDFIB-8.cpp @@ -0,0 +1,191 @@ +/*************************************************** + * Problem Name : ASCDFIB - Ascending Fibonacci Numbers.cpp + * Problem Link : https://www.spoj.com/problems/ASCDFIB/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-09-21 + * Problem Type : Number Theory + Sorting(Counting Sort) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 100000 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +pii fib (int n) { + int a = 0, b = 1; + + for (int i = 3 ; i <= n; i++) { + int c = (a + b) % MOD; + a = b; + b = c; + } + + return {a, b}; +} + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + int tc, a, b; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> a >> b; + vectorvec; + + if (a == 1) { + int n1 = 0; + int n2 = 1; + vec.pb (n1); + + if (b > 0) vec.pb (n2); + + for (int i = 2; i <= b; i++) { + int n3 = (n1 + n2) % MOD; + vec.pb (n3); + n1 = n2; + n2 = n3; + } + + } else if (a == 2) { + int n1 = 1; + int n2 = 1; + vec.pb (n1); + + if (b > 0) vec.pb (n2); + + for (int i = 2; i <= b; i++) { + int n3 = (n1 + n2) % MOD; + vec.pb (n3); + n1 = n2; + n2 = n3; + } + + } else { + pii pi = fib (a); + int n1 = pi.second; + int n2 = (pi.first + pi.second) % MOD; + vec.pb (n1); + + if (b > 0) vec.pb (n2); + + for (int i = 2; i <= b; i++) { + int n3 = (n1 + n2) % MOD; + vec.pb (n3); + n1 = n2; + n2 = n3; + } + } + + int ar[MAX]; + SET (ar, 0); + + for (int i = 0; i < (int) vec.size(); i++) { + ar[vec[i]]++; + } + + cout << "Case " << t << ": "; + int cnt = 0; + + for (int i = 0; i < MAX; i++) { + for (int j = 1; j <= ar[i]; j++) { + cout << i << " "; + cnt++; + + if (cnt >= 100) break; + } + + if (cnt >= 100) break; + } + + nl; + } + + return 0; +} diff --git a/spoj_cpp_clean/ASSIST/ASSIST-7.cpp b/spoj_cpp_clean/ASSIST/ASSIST-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aac5536cf4eaf427c4d72b9004263875b390887b --- /dev/null +++ b/spoj_cpp_clean/ASSIST/ASSIST-7.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; +#define lim 50000 +vectordev; +vectorcheck(lim,false); +void pre() +{ + int i,j; + for(i=2;i<=34000;i++) + { + if(check[i]==false) + { + int x=0; + for(j=i+1;j<=34000;j++) + { + if(check[j]==false) + x++; + if(x==i) + { + check[j]=true; + x=0; + } + } + } + } + for(i=2;i<=34000;i++) + { + if(check[i]==false) + { + dev.push_back(i); + } + } + //printf("%d",dev.size()); +} +int main() +{ + pre(); + while(1) + { + int n; + scanf("%d",&n); + if(n==0) + break; + printf("%d\n",dev[n-1]); + } +} diff --git a/spoj_cpp_clean/BABTWR/BABTWR-3.cpp b/spoj_cpp_clean/BABTWR/BABTWR-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f1e7f2afe9de9238aecf341e06d71b13b849361 --- /dev/null +++ b/spoj_cpp_clean/BABTWR/BABTWR-3.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int h[91],w[91],d[91], INF = 100000000, n; +int dp[90][90]; +int partition(int left, int right){ + int p = left, pivot = h[left] * w[left]; + + int t1 = w[left], t2 = d[left], t3 = h[left]; + for(int i = left + 1; i <= right; i++){ + if(pivot < h[i]*w[i]){ + h[p] = h[i]; + h[i] = h[p+1]; + h[p+1] = t3; + + w[p] = w[i]; + w[i] = w[p+1]; + w[p+1] = t1; + + d[p] = d[i]; + d[i] = d[p+1]; + d[p+1] = t2; + + p++; + } + } + return p; +} + +void quicksort(int i, int k){ + if( i < k){ + int p = partition(i, k); + quicksort(i, p-1); + quicksort(p+1, k); + } + return; +} +int recur(int prev, int i){ + if( i > 3*n) + return 0; + else if(dp[prev][i] > 0) + return dp[prev][i]; + else if( prev == 0) + dp[prev][i] = max(recur(i, i+1) + d[i], recur(prev, i+1)); + else if((h[i] < h[prev] and w[i] < w[prev]) or(h[i] < w[prev] and w[i] < h[prev])) + dp[prev][i] = max(recur(i, i+1) + d[i], recur(prev, i+1)); + else + dp[prev][i] = recur(prev, i+1); + } +int main(){ + int i, j, a, b, c; + h[0] = INF; + w[0] = INF; + d[0] = 0; + sd(n); + while(n>0){ + memset(dp, 0, sizeof(dp)); + j = 1; + //for(i = 1; i<=90; i++){ + // h[i] = 0; + // w[i] = 0; + // d[i] = 0; + //} + for(i = 0; i < n; i++){ + sd3(a,b,c); + h[j] = a; + w[j] = b; + d[j] = c; + j++; + h[j] = b; + w[j] = c; + d[j] = a; + j++; + h[j] = c; + w[j] = a; + d[j] = b; + j++; + } + quicksort(0, 3*n); + //for(i = 0; i <= 3*n; i++) + // cout< +#include +#include +#include +#include +#include + +using namespace std; +typedef long long i64; + +struct item{ + int cost, value, u; + item(){} + item(int _cost, int _value, int _u): + cost(_cost), value(_value), u(_u){} +}; + +vector v[100]; +int memo[100][5][33000]; +bool mark[100][5][33000]; + +int f(const int n, int i, int j, int w){ + if(i >= n || w <= 0)return 0; + int &best = memo[i][j][w]; + if(mark[i][j][w])return best; + mark[i][j][w] = true; + + best = f(n, i+1, 0, w); + + if(v[i].size() > 0){ + if(j == 0){ + if(v[i][j].cost <= w) + best = max(best, f(n,i,j+1,w-v[i][j].cost) + v[i][j].value); + }else{ + if(j < (int)v[i].size() && v[i][j].cost <= w) + best = max(best, f(n,i,j+1,w-v[i][j].cost) + v[i][j].value); + best = max(best, f(n, i+1, 0, w)); + } + } + return best; +} + +int main(){ + //freopen("data.in","r", stdin); + int T; scanf("%d", &T); + for(int t=1; t<=T; ++t){ + int w, n; scanf("%d %d", &w, &n); + for(int i=0; i<100; ++i)v[i].clear(); + + for(int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, vmax, n, v, c, u, l, vol[61][4], val[61][4], count[61], maxi, dp[2][3201], indx; + sd(t); + while(t--){ + sd2(vmax, n); vmax /= 10; + for(int i = 1; i <= n; i++){ + vol[i][0] = 0; + vol[i][1] = 0; + vol[i][2] = 0; + vol[i][3] = 0; + + val[i][0] = 0; + val[i][1] = 0; + val[i][2] = 0; + val[i][3] = 0; + + count[i] = 0; + }//Initialized all arrays to 0 + + indx = 0; + dp[1][0] = 0; + for(int i= 0; i <= vmax; i++) dp[indx][i] = 0; //Base cases initialized to zero + + for(int i = 1; i <= n; i++){ + sd3(v, c, u); + v /= 10; + if(!u){ + vol[i][0] = v; + val[i][0] = c*v; + } + else{ + count[u]++; + vol[u][count[u]] = v; + val[u][count[u]] = c * v; + } + }//Input taken + + for (int i = 0; i <= n; i++) { + vol[i][3] = vol[i][0] + vol[i][1] + vol[i][2]; + val[i][3] = val[i][0] + val[i][1] + val[i][2]; + vol[i][1] = vol[i][0] + vol[i][1]; + val[i][1] = val[i][0] + val[i][1]; + vol[i][2] = vol[i][0] + vol[i][2]; + val[i][2] = val[i][0] + val[i][2]; + }//Input formatted + + for(int i = 1; i <= n; i++){ + indx = 1 - indx; + for(int j = 1; j <= vmax; j++){ + maxi = max(dp[1-indx][j], dp[indx][j-1]); + if(val[i][0] > 0){ + for(int k = 0; k < 4; k++){ + if(vol[i][k] <= j) maxi = max(maxi, dp[1-indx][j-vol[i][k]] + val[i][k]); + } + } + dp[indx][j] = maxi; + } + } + printf("%d\n",dp[indx][vmax] * 10); + } + return 0; +} diff --git a/spoj_cpp_clean/BADXOR/BADXOR-9.cpp b/spoj_cpp_clean/BADXOR/BADXOR-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba37b1b130d9e7b56c64dd271f5ec0c96d69d2db --- /dev/null +++ b/spoj_cpp_clean/BADXOR/BADXOR-9.cpp @@ -0,0 +1,77 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#define MOD 100000007 + +int N, M; +int A[1025], B[1025]; +int DP[1025][1025]; + +inline void init() +{ + int i, num; + + memset(B, 0, sizeof(B)); + + for(i=1; i<1025; i++) + DP[0][i] = 0; + + DP[0][0] = 1; + + scanf("%d %d", &N, &M); + + for(i=1; i<=N; i++) + scanf("%d", &A[i]); + + for(i=0; i=MOD) + DP[i][j] -= MOD; + } + + for(i=0; i<1024; i++) + if(!B[i]) + { + sum += DP[N][i]; + if(sum >= MOD) + sum -= MOD; + } + + printf("Case %d: %d\n", t, sum); + } + + return 0; +} diff --git a/spoj_cpp_clean/BAFO/BAFO-10.cpp b/spoj_cpp_clean/BAFO/BAFO-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..62bfcd8367f2afe365f7e48880179ea8c1a5180f --- /dev/null +++ b/spoj_cpp_clean/BAFO/BAFO-10.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +int main() +{ + int n, test=0; + while(cin >> n && n != 0) + { + int a = 0, b = 0; + for(int i=0; i> ja >> jb; + a += ja; + b += jb; + } + cout << "Teste " << ++test << endl << + (a > b ? "Aldo" : "Beto") << endl << endl; + + } + return 0; +} diff --git a/spoj_cpp_clean/BAISED/BAISED-3.cpp b/spoj_cpp_clean/BAISED/BAISED-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3633987c694bb458831a5b26c185b4465203daa0 --- /dev/null +++ b/spoj_cpp_clean/BAISED/BAISED-3.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, n, r; + long long sum; + char name[30]; + pair ranks[100000]; + sd(t); + while(t--){ + sd(n); + sum = 0; + for(int i = 0; i < n ; i++){ + scanf("%s %d",name,&r); + ranks[i] = make_pair(r,i+1); + } + sort(ranks, ranks+n); + for(int i = 0; i < n; i++){ + //cout< +using namespace std; + +#define ll unsigned long long +#define pb push_back +ll dp[2][2][22][1 << 10][1 << 5]; +vector num; + +ll solve(ll isStart, ll isSmall, ll pos, ll bt, ll used) + +{ + if(pos == 0) { + bool chk = 1; + for(ll i = 0; i <= 9; i++) { + if(!(i & 1) && !( (used >> (i / 2) ) & 1) ) + continue; + if( (i & 1) && ( (bt >> i) & 1) ) { + chk = 0; + break; + } + if( !(i & 1) && !( (bt >> i) & 1) ) { + chk = 0; + break; + } + } + + return (ll)chk; + } + ll &ret = dp[isStart][isSmall][pos][bt][used]; + if(ret != -1 && isSmall) + return ret; + + ll lim, pos2 = num.size() - pos; + if(isSmall) + lim = 9; + else + lim = num[pos2]; + + ll rt = 0; + if(!isStart) { + for(ll i = 0; i <= lim; i++) + rt += solve(0, isSmall | i < num[pos2], pos - 1, bt ^ (1 << i), !(i & 1) ? used | (1 << (i / 2)): used ); + } + else { + for(ll i = 1; i <= lim; i++) + rt += solve(0, isSmall | i < num[pos2], pos - 1, bt ^ (1 << i), !(i & 1) ? used | (1 << (i / 2)): used ); + + rt += solve(1, 1, pos - 1, 0, 0); + } + + return ret = rt; +} + +ll calc(ll n) + +{ + if(n <= 0) + return 0; + + ll tmp = n; + num.clear(); + while(tmp) { + num.pb(tmp % 10); + tmp /= 10; + } + reverse(num.begin(), num.end()); + + return solve(1, 0, num.size(), 0, 0) - 1; +} + +int main() + +{ + ll t, caseno = 0; + memset(dp, -1, sizeof(dp)); + cin >> t; + while(t--) { + ll l, r; + scanf("%llu %llu", &l, &r); + + ll ans = calc(r); + + ans -= calc(l - 1); + + printf("%llu\n", ans); + } + + return 0; diff --git a/spoj_cpp_clean/BAT1/BAT1-14.cpp b/spoj_cpp_clean/BAT1/BAT1-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..107b560b66888ec79f36dfe6b7346259def9abbe --- /dev/null +++ b/spoj_cpp_clean/BAT1/BAT1-14.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +#define ll long long + +ll dp[25][25][1009], n, m; +ll open[25], cost[25][25], rat[25][25]; + +ll solve(ll i, ll j, ll k) + +{ + if(i > n) + return 0; + if(k == 0) + return 0; + if(j > m) + return solve(i + 1, 0, k); + + ll &rt = dp[i][j][k]; + if(rt != -1) + return rt; + + ll ret = 0; + if(j == 0) { + ret = solve(i + 1, 0, k); + if(k >= open[i]) + ret = max(ret, solve(i, j + 1, k - open[i])); + + return rt = ret; + } + + for(ll cnt = 0; k - cnt * cost[i][j] >= 0; cnt++) + ret = max(ret, solve(i, j + 1, k - cnt * cost[i][j] ) + cnt * rat[i][j] ); + + return rt = ret; +} + +int main() + +{ + ll t, k; + cin >> t; + while(t--) { + memset(dp, -1, sizeof(dp)); + + scanf("%lld %lld %lld", &n, &m, &k); + for(ll i = 1; i <= n; i++) + scanf("%lld", &open[i]); + + for(ll i = 1; i <= n; i++) { + for(ll j = 1; j <= m; j++) + scanf("%lld", &cost[i][j]); + } + + for(ll i = 1; i <= n; i++) { + for(ll j = 1; j <= m; j++) + scanf("%lld", &rat[i][j] ); + } + + printf("%lld\n", solve(1, 0, k)); + } + + return 0; +} diff --git a/spoj_cpp_clean/BC/BC-4.cpp b/spoj_cpp_clean/BC/BC-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3fe462969b28e754d2967b53dce68b786948388 --- /dev/null +++ b/spoj_cpp_clean/BC/BC-4.cpp @@ -0,0 +1,118 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +//Other Includes +#include +#include +#include +#include +#include +#include +#include +#include +//some common functionn +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define all(c) (c).begin(),(c).end() +// Input macros +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) + +// Output macros +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +using namespace std; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define MAXN 100000000 +using namespace std; + +vector > factors; +int N, K; + + +inline void factorize(int num) +{ + if (num % 2 == 0) + { + factors.push_back(make_pair(2, 0)); + + while (num % 2 == 0) + { + num /= 2; + factors.back().second++; + } + } + + for (int i = 3; i <= num; i += 2) + { + if (num % i == 0) + { + factors.push_back(make_pair(i, 0)); + + while (num % i == 0) + { + num /= i; + factors.back().second++; + } + } + } +} + +inline void init() +{ + factors.clear(); + + scanf("%d %d", &N, &K); + factorize(K); +} + +inline int get_max_pow(int factor) +{ + int i; + int rval = 0; + + for (i = factor; i <= N; i *= factor) + rval += N / i; + + return rval; +} + +inline int solve() +{ + int rval = MAXN; + int i; + + for (i = 0; i < factors.size(); i++) + rval = min(rval, get_max_pow(factors[i].first) / factors[i].second); + + return rval; +} + +int main() +{ + int T, t; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + for (t = 1; t <= T; t++) + { + init(); + printf("Case %d: %d\n", t, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/BDOI16D/BDOI16D-9.cpp b/spoj_cpp_clean/BDOI16D/BDOI16D-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..137194ae853935c862ada3739c8403f3c6279843 --- /dev/null +++ b/spoj_cpp_clean/BDOI16D/BDOI16D-9.cpp @@ -0,0 +1,113 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +#define MAXN 100007 +#define MAXK 57 +#define gc() getchar_unlocked() +using namespace std; + +int N, R, K; +pair A[MAXN]; +int DP[MAXN][MAXK]; +int max_left[MAXN]; +int SUM[MAXN]; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +inline void init() +{ + int i, j; + + N = FAST_IO(); + R = FAST_IO(); + K = FAST_IO(); + + + for (i = 1; i <= N; i++) + { + A[i].first = FAST_IO(); + A[i].second = FAST_IO(); + } + + sort(A, A + N + 1); + + for (i = 1; i <= N; i++) + SUM[i] = SUM[i - 1] + A[i].second; + + max_left[1] = 1; + i = 1; + + for (j = 2; j <= N; j++) + { + while (A[j].first - A[i].first > 2 * R) + i++; + + max_left[j] = i; + } +} + +inline int solve() +{ + int i, j; + int pos, val; + + for (i = 1; i <= K; i++) + DP[1][i] = A[1].second; + + for (i = 2; i <= N; i++) + { + pos = max_left[i]; + val = SUM[i] - SUM[pos - 1]; + + for (j = 1; j <= K; j++) + DP[i][j] = max(DP[i - 1][j], val + DP[pos - 1][j - 1]); + + } + + return DP[N][K]; +} + +int main() +{ + int T, t; + +// freopen("input.txt", "r", stdin); + + T = FAST_IO(); + + for (t = 1; t <= T; t++) + { + init(); + printf("Case %d: %d\n", t, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/BEADS/BEADS-13.cpp b/spoj_cpp_clean/BEADS/BEADS-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb936ed545771953345aac61018925608d045534 --- /dev/null +++ b/spoj_cpp_clean/BEADS/BEADS-13.cpp @@ -0,0 +1,54 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +char buffer[10005]; + +int main(){ + //freopen("data.in","r",stdin); + int T; scanf("%d", &T); + for(int t=0; t +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0; i33) + { + h=h*10+c-'0'; + c=getchar();// getchar_unlocked(); + } + return h; +} +using namespace std; + +#define For(i, a, b) for (int i=(a); i<(b); ++i) +#define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x) +#define D(x) cout << #x " = " << (x) << endl + +const int N = 500005; + +int str[N]; // input +int rank[N], pos[N]; // output +int cnt[N], next[N]; // internal +bool bh[N], b2h[N]; // buckets +string s; + +bool smaller_first_char(int a, int b) +{ + return str[a] < str[b]; +} + +void SuffixSort(int n) +{ + // sort suffixes according to their first character + for (int i=0; i rank[k] = i + } // Actually j or k is the suffix postions, ^^^^see + } + + cnt[rank[n - h]]++; //h chars are considered for now + b2h[rank[n - h]] = true; + for (int i = 0; i < n; i = next[i]) // for each bucket + { + for (int j = i; j < next[i]; ++j) // elements in each bucket + { + int s = pos[j] - h; + if (s >= 0) + { + int head = rank[s]; // rank of suffix str[s..n-1] + rank[s] = head + cnt[head]++; // rank of suffix str[s..n-1] is incrementd to new postion + b2h[rank[s]] = true; // rank was updated in + } + } + for (int j = i; j < next[i]; ++j) + { + int s = pos[j] - h; + if (s >= 0 && b2h[rank[s]])// if still there is diff ranks of 2 suffix + { + for (int k = rank[s]+1; !bh[k] && b2h[k]; k++) // for all next with same + b2h[k] = false; // rank in one bucket + } + } + } + // For each h chars once only + for (int i=0; i buckets of h length, b2h -> buckets of 2*h length + pos[rank[i]] = i; // update rank + bh[i] |= b2h[i]; // update buckets + } + + } + //Finally pos[] is the Suffix array ! and rank[] is inverse ! + for (int i=0; i> t; + // while (t--) + //{ + cin >> s; + //s+=s; + // error(s); + solve(); + //} + + return 0; +} diff --git a/spoj_cpp_clean/BEENUMS/BEENUMS-5.cpp b/spoj_cpp_clean/BEENUMS/BEENUMS-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47543edbdf853e447bb740d916483abfb659e6c4 --- /dev/null +++ b/spoj_cpp_clean/BEENUMS/BEENUMS-5.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; + +int main() +{ + int num,i,j,k; + cin>>num; + while(num>0) + { + if(num==1) + cout<<"Y"<1;i+=6) + { + num=num-i; + } + if(num==1) + cout<<"Y"<>num; + } + return 0; +} diff --git a/spoj_cpp_clean/BEENUMS/BEENUMS-7.cpp b/spoj_cpp_clean/BEENUMS/BEENUMS-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b05a977bd95124b3ed71a03dedc16e69a0d8ed4e --- /dev/null +++ b/spoj_cpp_clean/BEENUMS/BEENUMS-7.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; +int main() +{ + while(1) + { + float a,b; + double k; + cin>>k; + if(k==-1) return 0; + else + a=(sqrt(1+4*((k-1)/3))-1)/2; + if(a-int(a)) cout<<"N"< +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; + +bool mark[30][120]; +int memo[30][120]; + +int f(vector < pair > &v, int i, int k){ + if(i >= (int)v.size()){ + if(k == 0)return 1; + else return 0; + } + if(k == 0)return 0; + int &count = memo[i][k]; + if(mark[i][k])return count; + mark[i][k] = true; + count = 0; + for(int j=v[i].first, n=min(v[i].second, k); j<=n; ++j){ + count += f(v,i+1,k-j); + } + return count; +} + +int main(){ + //freopen("data.in", "r", stdin); + int m, n; + while(scanf("%d %d", &m, &n)==2 && (n || m)){ + vector < pair < int, int> >v(m); + for(int i=0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +using namespace std; +#define fst first +#define snd second +#define all(c) ((c).begin()), ((c).end()) + +typedef long long ll; +struct rolling_hash { + static const unsigned long long p = 1000000007ull; + const char *s; + int n; + vector pow, phash; + rolling_hash(const char *s) : s(s), n(strlen(s)), pow(n+1), phash(n+1) { + pow[0] = 1; phash[0] = 0; + for (int i = 0; i < n; ++i) { + phash[i+1] = s[i] + phash[i] * p; + pow[i+1] = pow[i] * p; + } + } + ll hash(int i, int j) { + return phash[j] - phash[i] * pow[j-i]; + } +}; +void solve(char s[], int k) { + int n = strlen(s); + rolling_hash RH(s); + + int lo = 0, hi = n+1, l; + while (lo + 1 < hi) { + int mid = (lo + hi) / 2; + unordered_map bucket; + l = -1; + for (int i = 0; i+mid <= n; ++i) + if (++bucket[RH.hash(i, i+mid)] >= k) l = i; + if (l == -1) hi = mid; + else lo = mid; + } + unordered_map bucket; + l = -1; + for (int i = 0; i+lo <= n; ++i) + if (++bucket[RH.hash(i, i+lo)] >= k) l = i; + + if (lo == 0) { + printf("HATE\n"); + } else { + printf("%d %d\n", lo, l+1); + } +} +int main() { + int ncase; scanf("%d", &ncase); + for (int icase = 0; icase < ncase; ++icase) { + char s[50010]; + scanf("%s", s); + int k; + scanf("%d", &k); + solve(s, k); + } +} diff --git a/spoj_cpp_clean/BINGO/BINGO-13.cpp b/spoj_cpp_clean/BINGO/BINGO-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ee59090a31257fcc84fc39d8d846c8e6c2f686f --- /dev/null +++ b/spoj_cpp_clean/BINGO/BINGO-13.cpp @@ -0,0 +1,70 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +void f(int res, int n){ + for(int i=n; i>1; --i){ + while(res % i == 0 && n % i == 0){ + res /= i, n/= i; + } + } + printf("%d / %d\n",res,n); +} + +int main(){ + freopen("data.in","r", stdin); + char buffer[150]; + int res = 0, n=0; + set mark; + + while(gets(buffer) != NULL){ + string s = buffer; + for(int i=0, m=s.size(); i= 'A' && s[i] <='Z') || (s[i] >='a' && s[i] <= 'z'))){ + s[i] = toupper(s[i]); + }else s[i] = ' '; + } + istringstream iss(s); + + while(iss >> s){ + if(s == "BULLSHIT"){ + //printf("count %d\n",count); + res += mark.size(); + n++; + mark.clear(); + }else mark.insert(s); + } + } + f(res,n); + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/BINPRNUM/BINPRNUM-8.cpp b/spoj_cpp_clean/BINPRNUM/BINPRNUM-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdaab6b1e5ca77b6579adfdef617c3cb1e07b130 --- /dev/null +++ b/spoj_cpp_clean/BINPRNUM/BINPRNUM-8.cpp @@ -0,0 +1,119 @@ +/*************************************************** + * Problem Name : BINPRNUM - Binary Protean Number.cpp + * Problem Link : https://www.spoj.com/problems/BINPRNUM/en/ + * OJ : SPOj + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : DigitDP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +string num; +int n; +ll dp[93][10][2][2]; +ll digitDP (int pos, int pre, int isChoto, int isSuru) { + if (pos == n) return 1LL; + + ll &ans = dp[pos][pre][isChoto][isSuru]; + + if (ans != -1) return ans; + + int low = 0, high = 1; + + if (isChoto == 0) { + high = num[pos] - '0'; + } + + ans = 0; + + for (int i = low; i <= high; i++) { + int newSuru = isSuru; + + if (i > 0) { + newSuru = 1; + } + + if (pre == 1 && i == 1) { + } else { + ans += digitDP (pos + 1, i, isChoto | (i < high), newSuru); + } + } + + return ans; +} + +ll solve() { + n = num.size(); + SET (dp, -1); + ll ans = digitDP (0, 0, 0, 0); + return ans; +} +int main () { + __FastIO; + int tc; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> num; + ll ans = solve(); + ans--; + cout << "Case " << t << ": " << ans << "\n"; + num.clear(); + } + + return 0; +} diff --git a/spoj_cpp_clean/BINPRNUM/BINPRNUM-9.cpp b/spoj_cpp_clean/BINPRNUM/BINPRNUM-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fa63c76ebe1edec718b816136d674e7c37388a0 --- /dev/null +++ b/spoj_cpp_clean/BINPRNUM/BINPRNUM-9.cpp @@ -0,0 +1,79 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar_unlocked() +using namespace std; + +unsigned long long fib[91], sum[91]; + +inline void getstring(deque &A) +{ + char c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_front(c); + c = gc(); + } +} + +inline void pre() +{ + int i; + + fib[1] = fib[2] = 1; + sum[1] = 1; + sum[2] = 2; + + for (i = 3; i <= 90; i++) + { + fib[i] = fib[i - 1] + fib[i - 2]; + sum[i] = fib[i] + sum[i - 1]; + } +} + +inline unsigned long long solve() +{ + unsigned long long ans = 0; + deque S; + int i; + + getstring(S); + + for (i = 0; i < S.size(); i++) + if (S[i] == '1') + ans += sum[i] + 1; + + return ans; +} + +int main() +{ + int T, i; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + pre(); + + for (i = 1; i <= T; i++) + printf("Case %d: %llu\n", i, solve()); + + return 0; +} diff --git a/spoj_cpp_clean/BISHOPS/BISHOPS-5.cpp b/spoj_cpp_clean/BISHOPS/BISHOPS-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dceb5597a704b4e9993ca72845ce50dde68dc07a --- /dev/null +++ b/spoj_cpp_clean/BISHOPS/BISHOPS-5.cpp @@ -0,0 +1,73 @@ +#include +#include + +using namespace std; + +int main() +{ + string input,output; + int len,i,j,carry,temp,mult=2; + while(cin>>input) + { + len=input.length(); + if(len==1) + { + if(input[0]=='1') + { + cout<<1<=0;i--) + { + temp=(carry+((input[i]-48)*mult)); + input[i]=(temp%10)+48; + carry=temp/10; + } + while(carry!=0) + { + input=(char)((carry%10)+48)+input; + carry=carry/10; + } + //cout<=0;i--) + { + if(input[i]<(sub+'0')) + { + temp=((input[i])+10)-(sub+48); + input[i]=(char)(temp+48); + //carry=1; + sub=1; + } + else + { + temp=(input[i])-(sub+48); + input[i]=(char)(temp+48); + //carry=0; + sub=0; + } + } + for(i=0;i +#define MAXSIZE 200 +#define MAXINT 20000000 +using namespace std; + +char grafo[200][200]; +int visit[MAXSIZE][MAXSIZE]; +int movx[]= {0,1,-1,0}; +int movy[]= {1,0,0,-1}; +int t,n,m,aux; + +bool valida(int a, int b){ + return ((a=0) && (b=0))?true:false; +} + + + +void bfs(int x, int y) +{ + queue< pair >q; + visit[x][y]=0; + q.push(make_pair(x,y)); + + while(!q.empty()) + { + pair u= q.front(); q.pop(); + for (int i = 0; i < 4; ++i) + { + int nuevox= u.first+movx[i]; + int nuevoy= u.second+movy[i]; + if ( valida(nuevox,nuevoy) && visit[u.first][u.second] + 1 < visit[nuevox][nuevoy]) + { + visit[nuevox][nuevoy]= visit[u.first][u.second] + 1; + q.push(make_pair(nuevox, nuevoy) ); + } + } + + } +} + + + + + +void inicializar(int x, int visit[][MAXSIZE]) +{ + for (int i = 0; i < MAXSIZE; ++i) + { + for (int j = 0; j < MAXSIZE; ++j) + { + visit[i][j]=x; + } + } +} + + +int main() +{ + string cadena; + + scanf("%d",&t); + while(t--) + { + scanf("%d %d",&n,&m); + inicializar(MAXINT,visit); + + for (int i = 0; i < n; ++i) + { + scanf("%s",grafo[i]); + } + + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + if (grafo[i][j]=='1') + { + bfs(i,j); + } + } + } + + + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + + printf("%d",visit[i][j] ); + if (j!=m-1) + { + printf(" "); + } + } + printf("\n"); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/BITMAP/BITMAP-9.cpp b/spoj_cpp_clean/BITMAP/BITMAP-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d05f028743dce62fafa66ce5649a2ca08a9842a --- /dev/null +++ b/spoj_cpp_clean/BITMAP/BITMAP-9.cpp @@ -0,0 +1,117 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +int dx[] = {-1, 0, 0, 1}; +int dy[] = {0, 1, -1, 0}; +int grid[183][183]; +int dist[183][183]; +int N, M; +deque< pair > Q; +unordered_map< int, unordered_map > V; + +inline void init() +{ + int i, j; + char c; + + Q.clear(); + V.clear(); + + scanf("%d %d\n", &N, &M); + + for(i=1; i<=N; i++) + { + for(j=1; j<=M; j++) + { + c = gc(); + grid[i][j] = c -'0'; + + if(grid[i][j]) + { + dist[i][j] = 0; + Q.push_back(make_pair(i, j)); + V[i][j] = 1; + } + else + dist[i][j] = INT_MAX; + } + gc(); + } +} + +inline bool in_range(int x, int y) +{ + if(x < 1 || x > N || y < 1 || y > M) + return 0; + return 1; +} + +void bfs() +{ + int x, y, i; + + while(!Q.empty()) + { + x = Q.front().first; + y = Q.front().second; + Q.pop_front(); + + for(i=0; i<4; i++) + if(in_range(x + dx[i], y + dy[i])) + { + if(V.find(x + dx[i])==V.end()) + { + dist[x + dx[i]][y + dy[i]] = dist[x][y] + 1; + V[x + dx[i]][y + dy[i]] = 1; + Q.push_back(make_pair(x + dx[i], y + dy[i])); + } + else if(V[x + dx[i]].find(y + dy[i]) == V[x + dx[i]].end()) + { + dist[x + dx[i]][y + dy[i]] = dist[x][y] + 1; + V[x + dx[i]][y + dy[i]] = 1; + Q.push_back(make_pair(x + dx[i], y + dy[i])); + } + } + + } +} + +int main() +{ + int T, i, j; + + //freopen("input.txt","r",stdin); + + scanf("%d\n", &T); + + while(T--) + { + init(); + bfs(); + + for(i = 1; i<=N; i++) + { + for(j = 1; j<=M; j++) + printf("%d ", dist[i][j]); + putchar('\n'); + } + } + return 0; +} diff --git a/spoj_cpp_clean/BLCONE/BLCONE-9.cpp b/spoj_cpp_clean/BLCONE/BLCONE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f040b711d586edcd5202b479581e9296848ee76b --- /dev/null +++ b/spoj_cpp_clean/BLCONE/BLCONE-9.cpp @@ -0,0 +1,69 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +inline double volume(double r, double h) +{ + return (r * r * h) / 3; +} + +inline double area(double r, double h) +{ + return r * sqrt(r * r + h * h); +} + +double R, H; + +inline void init() +{ + scanf("%lf %lf", &R, &H); +} + +inline double solve() +{ + if (fabs(volume(R, H) - area(R, H)) <= 1e-8) + return H; + + double lo = 0, hi = H; + double r; + + while (fabs(hi - lo) >= 1e-8) + { + double h = (hi + lo) / 2.0; + double r = h * R / H; + + if (volume(r, h) <= 2 * area(R, H) - area(r, h)) + lo = h; + else + hi = h; + } + + return lo; +} + +int main() +{ + int T; + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%.6lf\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/BLINNET/BLINNET-13.cpp b/spoj_cpp_clean/BLINNET/BLINNET-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb3f5a61de11ee7edabd0e6d8c3d32821c0431f3 --- /dev/null +++ b/spoj_cpp_clean/BLINNET/BLINNET-13.cpp @@ -0,0 +1,95 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; +const int MAX_N = 10005; +struct edge{ + int a, b, cost; + edge(int _a, int _b, int _cost): + a(_a), b(_b), cost(_cost){} + bool operator <(const edge &x)const{ + if(x.cost < cost)return true; + return false; + } +}; + +struct graph{ + vector g[MAX_N]; + void addEdge(int a, int b, int cost){ + g[a].push_back(edge(a,b,cost)); + } +}; + +int f(const graph &gr, const int n){ + vector mark(n, false); + priority_queue pq; + for(int i=0; i0){ + for(int k=0, m=gr.g[i].size(); k Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar_unlocked() +using namespace std; + +int K[2007], KE[2007]; +string S; + +inline void getstring(string &A) +{ + A.clear(); + A.push_back('0'); + + char c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +inline int solve() +{ + int i; + int ans = 0; + + for (i = 1; i < S.size(); i++) + { + switch (S[i]) + { + case 'K': + K[i] = K[i - 1] + 1; + KE[i] = KE[i - 1]; + ans += KE[i]; + break; + + case 'E': + K[i] = K[i - 1]; + KE[i] = KE[i - 1] + K[i]; + break; + default: + K[i] = K[i - 1]; + KE[i] = KE[i - 1]; + } + } + + return ans; +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + getstring(S); + printf("%d\n", solve()); + } + + return 0; +} + + diff --git a/spoj_cpp_clean/BLUNIQ/BLUNIQ-9.cpp b/spoj_cpp_clean/BLUNIQ/BLUNIQ-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4bb7e34ffde7b087c434760c574212ff446221cf --- /dev/null +++ b/spoj_cpp_clean/BLUNIQ/BLUNIQ-9.cpp @@ -0,0 +1,135 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +set< pair > S; //1 = begin, 3 = end, 2 = insert + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +inline void init() +{ + S.insert(make_pair(0, 3)); + S.insert(make_pair(2000000000, 1)); +} + +inline int add(int num) +{ + set< pair >::iterator ii; + int ans; + + ii = S.insert(make_pair(num, 2)).first; + + if (prev(ii)->second == 1 && next(ii)->second == 3) + { + ans = next(ii)->first; + if (next(next(ii))->first == next(ii)->first + 1) + { + S.erase(next(next(ii))); + S.erase(next(ii)); + } + + else + { + S.insert(next(next(ii)), make_pair(next(ii)->first + 1, 3)); + S.erase(next(ii)); + } + S.erase(ii); + return ans; + } + + if (prev(ii)->second == 3 && next(ii)->second == 1) + { + ans = num; + S.insert(ii, make_pair(num, 1)); + if (next(ii)->first == num + 1) + S.erase(next(ii)); + else + S.insert(next(ii), make_pair(num + 1, 3)); + S.erase(ii); + return ans; + } +} + +inline void rem(int num) +{ + set< pair >::iterator ii; + + ii = S.lower_bound(make_pair(num, 2)); + ii = prev(ii); + if (ii->first == num) + { + if (next(ii)->first == num + 1) + { + S.erase(next(ii)); + S.erase(ii); + + return; + } + + else + { + S.insert(next(ii), make_pair(num + 1, 1)); + S.erase(ii); + + return; + } + } + + else + { + S.insert(next(ii), make_pair(num + 1, 1)); + S.insert(next(ii), make_pair(num, 3)); + } +} + +int main() +{ + int num, type, T; + + init(); + + //scanf("%d", &T); + T = FAST_IO(); + while (T--) + { + + //scanf("%d %d", &type, &num); + type = FAST_IO(); + num = FAST_IO(); + + if (type == 1) + printf("%d\n", add(num)); + else + rem(num); + } + + return 0; +} diff --git a/spoj_cpp_clean/BOMARBLE/BOMARBLE-9.cpp b/spoj_cpp_clean/BOMARBLE/BOMARBLE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..111c325b36563258f2e8e900757cd9b8e6466d77 --- /dev/null +++ b/spoj_cpp_clean/BOMARBLE/BOMARBLE-9.cpp @@ -0,0 +1,42 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +int A[10001]; + +int main() +{ + int i, N, diff; + + //freopen("input.txt","r",stdin); + + A[1] = 5; + diff = 7; + + for(i=2; i<=10000; i++) + { + A[i] = A[i-1] + diff; + diff += 3; + } + + scanf("%d", &N); + + while(N) + { + printf("%d\n", A[N]); + scanf("%d", &N); + } + + return 0; +} diff --git a/spoj_cpp_clean/BOTAS/BOTAS-10.cpp b/spoj_cpp_clean/BOTAS/BOTAS-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a6a988df97674c19776aa3569ecd7b2ea55c3271 --- /dev/null +++ b/spoj_cpp_clean/BOTAS/BOTAS-10.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +int main() +{ + int n; + while(cin >> n) + { + vector ld(31, 0), le(31, 0); + int t, pares=0; + char p; + while(n-- > 0) + { + cin >> t >> p; + t -= 30; + if(p == 'D') + { + ld[t]++; + } + else + { + le[t]++; + } + if(ld[t] >= 1 && le[t] >= 1) + { + ld[t]--; + le[t]--; + pares++; + } + } + cout << pares << endl; + } +} diff --git a/spoj_cpp_clean/BOWLING1/BOWLING1-13.cpp b/spoj_cpp_clean/BOWLING1/BOWLING1-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cdc0ac45a08b0bface4804a2056a0c0ab1f2fd3 --- /dev/null +++ b/spoj_cpp_clean/BOWLING1/BOWLING1-13.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; + +struct frame{ + int n, v1, v2, v3; + frame(){n = 0, v1 = 0, v2 = 0, v3 = 0;} +}; + +void readInput(vector &v){ + for(int i=0; i<9; ++i){ + v[i].n = 1; + scanf("%d", &v[i].v1); + if(v[i].v1 < 10){ + v[i].n++; + scanf("%d", &v[i].v2); + } + } + v[9].n = 2; + scanf("%d", &v[9].v1); + scanf("%d", &v[9].v2); + if(v[9].v1 == 10 || (v[9].v1 + v[9].v2 == 10)){ + v[9].n++; + scanf("%d", &v[9].v3); + } +} + +int f(vector &v){ + int res = 0; + for(int i=0; i<9; ++i){ + res += v[i].v1; + if(v[i].n == 1){ + res += v[i+1].v1; + if(v[i+1].n == 1){ + if(i+1 < 9)res+=v[i+2].v1; + else res += v[i+2].v2; + }else{ + res += v[i+1].v2; + } + }else{ + res += v[i].v2; + if(v[i].v1 + v[i].v2 == 10){ + res += v[i+1].v1; + } + } + } + res+=v[9].v1 + v[9].v2; + if(v[9].n == 3)res += v[9].v3; + return res; +} + +int main(){ + //freopen("data.in","r",stdin); + int T; scanf("%d", &T); + for(int t=0; t v(10); + readInput(v); + int res = f(v); + printf("%d\n",res); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/BRCKTS/BRCKTS-3.cpp b/spoj_cpp_clean/BRCKTS/BRCKTS-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..506fc947e8a53fbbec6c2a0d100751594af4ae7e --- /dev/null +++ b/spoj_cpp_clean/BRCKTS/BRCKTS-3.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + int uOpen, uClosed; + + void assignLeaf(int open, int closed ) { + uOpen = open; + uClosed = closed; + } + + void merge(node& left, node& right) { + if(right.uClosed > left.uOpen){ + uOpen = right.uOpen; + uClosed = left.uClosed + (right.uClosed - left.uOpen); + } + else{ + uOpen = (left.uOpen - right.uClosed) + right.uOpen; + uClosed = left.uClosed; + } + } +}; + +int n, m, x; +string array; +node tree[100001]; + +// l and r represent the range node i is responsible for. A single call init(1, 0, n-1) builds the segment tree in O(N) time +void init(int i, int l, int r){ + if(l == r){ + if(array[l] == '('){ + tree[i].assignLeaf(1, 0); + } + else{ + tree[i].assignLeaf(0, 1); + } + return; + } + else{ + init(2*i, l, (l+r)/2); + init((2*i) + 1, ((l+r)/2) + 1, r); + node left = tree[2*i], right = tree[(2*i)+1]; + + tree[i].merge(left, right); //Merge the children into the parent + } + return; +} + +// O(log N) +node query(int i, int l, int r, int lo, int hi){ + if(l == lo and r == hi) //If the range we want to query is the exact range + return tree[i]; + int mid = (l+r)/2; + if(lo > mid) //If only the subtree rooted at right child is repsonsible for this range + return query((2*i)+1, mid+1, r, lo, hi); + if(hi <= mid) //If only the subtree rooted at the left child is responsible for this range + return query(2*i, l, mid, lo, hi); + //If both, the left and right children are partially responsible for the range + node left = query(2*i, l, mid, lo, mid); + node right = query((2*i)+1, mid+1, r, mid+1, hi); + node result; + + result.merge(left, right); //Merge the results of the children into the parent + return result; +} + +//Call using update(1, 0, N-1, index, value) for a point update in O(log N) +void update(int i, int lo, int hi, int index){ + if (lo == hi){ + if(array[index] == '('){ + array[index] = ')'; + tree[i].assignLeaf(0, 1); + } + else{ + array[index] = '('; + tree[i].assignLeaf(1, 0); + } + return; + } + int left = 2*i, right = left+1, mid = (lo+hi)/2; + if(index <= mid) + update(left, lo, mid, index); + else + update(right, mid+1, hi, index); + tree[i].merge(tree[left], tree[right]); + return; +} + +int main(){ + int t = 1; + while(t <= 10){ + printf("Test %d:\n",t); + sd(n); + cin>>array; + + init(1, 0, n-1); + + sd(m); // Number of operations + while(m--){ + //cin>>debug; + sd(x); + //cout< 0){ + update(1, 0, n-1, x-1); + } + else{ + if(tree[1].uOpen == 0 and tree[1].uClosed == 0) + printf("YES\n"); + else{ + printf("NO\n"); + } + } + + } + t++; + } + return 0; +} diff --git a/spoj_cpp_clean/BRCKTS/BRCKTS-4.cpp b/spoj_cpp_clean/BRCKTS/BRCKTS-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..519de2ea4a1fbee1669b4954a30bfd8665aad440 --- /dev/null +++ b/spoj_cpp_clean/BRCKTS/BRCKTS-4.cpp @@ -0,0 +1,163 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +char s[40000]; +int a[40000]; +struct node +{ + int sum, tmin; +} t[90000]; +node init(int val) +{ + node nd; + nd.sum=nd.tmin=val; + return nd; +} +node combine(node left, node right) +{ + node res; + res.sum=left.sum+right.sum; + res.tmin=miN(left.tmin, left.sum+right.tmin); + return res; +} +void build(int index, int st, int en) +{ + if(st==en) + t[index]=init(a[st]); + else + { + int m=(st+en)/2; + build(2*index, st, m); + build(2*index+1, m+1, en); + t[index]=combine(t[2*index], t[2*index+1]); + } +} +void update(int index, int st, int en, int pos) +{ + if(st==pos && pos==en) + t[index]=init(a[pos]); + else + { + int mid=(st+en)/2; + if(pos<=mid) + update(index*2, st, mid, pos); + else if(pos>mid) + update(index*2+1, mid+1, en, pos); + + t[index]=combine(t[2*index], t[2*index+1]); + } +} + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T, x; + //s(T); + FOR(r,0,10) + { + s(n); + ss(s); + FOR(i,0,n) + if(s[i]=='(') a[i+1]=1; else a[i+1]=-1; + a[n+1]=0; //gave WA + build(1,1,n+1); + s(m); + cout<<"Test "< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define min(a,b) (a +using namespace std; + +#define ll long long + +struct node { + ll start; + ll stop; +} ara[100009], indx[100009]; + +bool cmp(node a, node b) + +{ + if(a.start != b.start) + return a.start < b.start; + return a.stop < b.stop; +} + +int main() + +{ + ll t, lo, hi, mx, ansi; + cin >> t; + while(t--) { + mx = 0; + for(ll i = 0; i <= 100005; i++) { + indx[i].start = 1e18; + indx[i].stop = 1e18; + } + + ll n; + scanf("%lld", &n); + for(ll i = 1; i <= n; i++) + scanf("%lld", &ara[i].start); + for(ll i = 1; i <= n; i++) + scanf("%lld", &ara[i].stop); + + sort(ara + 1, ara + n + 1, cmp); + + for(ll i = 1; i <= n; i++) { + lo = 1, hi = n, ansi = -1; + while(lo <= hi) { + ll mid = (lo + hi) / 2; + if(indx[mid].start > ara[i].start) { + hi = mid - 1; + } + else if(indx[mid].stop > ara[i].stop) { + hi = mid - 1; + } + else { + ansi = mid; + lo = mid + 1; + } + } + + if(ansi == -1) + ansi = 1; + else + ansi++; + + indx[ ansi ].start = ara[i].start; + indx[ ansi ].stop = ara[i].stop; + + mx = max(mx, ansi); + } + + printf("%lld\n", mx); + } + + return 0; +} diff --git a/spoj_cpp_clean/BROKEN/BROKEN-1.cpp b/spoj_cpp_clean/BROKEN/BROKEN-1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d58c800e722b6a880e5b53ebf39e4a3977c8bbc --- /dev/null +++ b/spoj_cpp_clean/BROKEN/BROKEN-1.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#define MAX_LENGTH 1000000 + +//unsigned long max_length = MAX_LENGTH; // OS X +unsigned int max_length = MAX_LENGTH; // SPOJ +int letters, size, best; +char *sentence; +int used[128]; + +int main(int argc, char** argv) { + + sentence = (char*) malloc(sizeof(char) * (MAX_LENGTH + 1)); + + scanf("%d", &letters); + getchar(); + while(letters > 0) { + size = getline(&sentence, &max_length, stdin); + --size; + sentence[size] = '\0'; + + best = 1; + int i = 0, j; + int last = 0; + int count = 1; + + memset(used, 0, sizeof(int) * 128); + + // used.reset(); + used[sentence[0]] = 1; + for (j = 1; j < size; ++j) { + int current = sentence[j]; + + ++used[current]; + if (used[current] == 1) { + // new letter + ++count; + while (count > letters) { + int first = sentence[i]; + --used[first]; + ++i; + if (used[first] == 0) --count; + } + } else { + // already seen this letter + best = std::max(best, j-i + 1); + } + } + + std::cout << best << std::endl; + + scanf("%d", &letters); + getchar(); + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/BROKEN/BROKEN-8.cpp b/spoj_cpp_clean/BROKEN/BROKEN-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a08c0e04ddce73f7cf05fa6009904f5a26174c2 --- /dev/null +++ b/spoj_cpp_clean/BROKEN/BROKEN-8.cpp @@ -0,0 +1,92 @@ +/*************************************************** + * Problem Name : BROKEN - Broken Keyboard.cpp + * Problem Link : https://www.spoj.com/problems/BROKEN/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-02-07 + * Problem Type : two pointer + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd (ll a, ll b) { + if (a % b == 0) return b; + + return gcd (b, a % b); +} +ll lcm (ll a, ll b) { + return a * b / gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +mapmp; +int main () { + int m; + + while (scanf ("%d", &m) == 1 && m != 0) { + string ar; + getchar(); + getline (cin, ar); + int N = ar.size(); + int mx = 0, cnt = 0; + + for (int L = 0, R = 0; R < N; ) { + mp[ar[R]]++; + + if (mp[ar[R]] == 1) cnt++; + + R++; + + while (cnt > m) { + mp[ar[L]]--; + + if (mp[ar[L]] == 0) cnt--; + + L++; + } + + int ans = R - L; + mx = max (mx, ans); + } + + printf ("%d\n", mx); + mp.clear(); + } + + return 0; +} diff --git a/spoj_cpp_clean/BRPAR/BRPAR-13.cpp b/spoj_cpp_clean/BRPAR/BRPAR-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff20d5f2d9597acdaddee78eff848664a8bac3e8 --- /dev/null +++ b/spoj_cpp_clean/BRPAR/BRPAR-13.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + +#include +#include + +using namespace std; +typedef long long i64; +const i64 mod = 1000000007; + +i64 c[1005]; +i64 f[1005]; + +void catalan(){ + c[0] = 1; + for(int n=1; n<=1002; ++n){ + for(int k=0; k &v){ + i64 num = f[m] % mod; + i64 den = 1; + for(int i=0, n=(int)v.size(); i v(n); + + for(int i=0; i recursive backtracking > pruned permutations + difficulty: easy + date: 14/Apr/2020 + by: @brpapa +*/ +#include +using namespace std; + +bitset<10> used; // used[v] = v já foi usado na permutação atual +int p[10]; // permutação atual + +int A[10], K; +bool hasSolution; + +// p[0:i-1] é válido? +bool check(int i, int v) { + int sum = 0; + for (int k = 0; k < i; k++) { + sum += p[k]*A[k]; + if (sum > K) return false; + } + return sum + (v*A[i]) <= K; +} + +// constroi p[i] +void bt(int i) { + if (hasSolution) return; + + if (i == 10) { + hasSolution = true; + for (int i = 0; i < 9; i++) + cout << p[i] << " "; + cout << p[9] << endl; + return; + } + + for (int v = 0; v < 10; v++) { + if (!used[v] && check(i, v)) { + p[i] = v; + + used[v] = 1; + bt(i+1); + used[v] = 0; + } + } +} + +int main() { + int T; cin >> T; + while (T--) { + for (int i = 0; i < 10; i++) cin >> A[i]; + cin >> K; + + hasSolution = false; + used.reset(); + + bt(0); + if (!hasSolution) cout << -1 << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/BTCODE_D/BTCODE_D-3.cpp b/spoj_cpp_clean/BTCODE_D/BTCODE_D-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0e55988c63881fc8052595d2fd51001775acdf8 --- /dev/null +++ b/spoj_cpp_clean/BTCODE_D/BTCODE_D-3.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, m, n, A[100][100], B[100][100]; + long long C[100][100]; + long long sum, max; + sd(t); + while(t--){ + sd2(m,n); + for(int i = 0; i< m; i++){ + for(int j = 0; j < n; j++){ + sd(A[i][j]); + } + } + for(int i = 0; i< m; i++){ + for(int j = 0; j < n; j++){ + sd(B[i][j]); + } + } + for(int i = 0; i< m; i++){ + for(int j = 0; j < n; j++){ + scanf("%lld",&C[i][j]); + } + } + sum = 0; + for(int i = 0; i< m; i++){ + max = 0; + for(int j = 0; j < n; j++){ + if(A[i][j] >= B[i][j]){ + if(B[i][j] * C[i][j] > max) + max = B[i][j] * C[i][j]; + } + else{ + if(A[i][j] * C[i][j] > max) + max = A[i][j] * C[i][j]; + } + } + sum += max; + } + printf("%lld\n",sum); + } + return 0; +} diff --git a/spoj_cpp_clean/BTCODE_G/BTCODE_G-14.cpp b/spoj_cpp_clean/BTCODE_G/BTCODE_G-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba78467d7282334ef1e88a30b919fa51b5d78905 --- /dev/null +++ b/spoj_cpp_clean/BTCODE_G/BTCODE_G-14.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +#define ll long long + +vector graph[100009]; +ll col[100009], par[100009]; + +ll findr(ll u) + +{ + if(par[u] == u) + return u; + return par[u] = findr(par[u]); +} + +void merg(ll u) + +{ + ll pu = findr(u); + for(ll i = 0; i < graph[u].size(); i++) { + ll nd = graph[u][i]; + if(col[nd] != col[u]) + continue; + + ll pv = findr(nd); + if(pu == pv) + continue; + + par[pv] = pu; + } +} + +int main() + +{ + ll n; + cin >> n; + for(ll i = 0; i < n; i++) + par[i] = i; + for(ll i = 1; i < n; i++) { + ll u, v; + scanf("%lld %lld", &u, &v); + graph[u].push_back(v); + graph[v].push_back(u); + } + + ll q; + cin >> q; + while(q--) { + ll state, u, v; + scanf("%lld %lld %lld", &state, &u, &v); + if(state == 1) { + col[u] = v; + merg(u); + } + else { + if(col[u] == 0 || col[v] == 0) { + printf("NO\n"); + continue; + } + ll pu = findr(u); + ll pv = findr(v); + if(pu == pv) + printf("YES\n"); + else + printf("NO\n"); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/BUGLIFE/BUGLIFE-12.cpp b/spoj_cpp_clean/BUGLIFE/BUGLIFE-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73c7c059c3c5dfed2dc2b0a316accc4937ff718d --- /dev/null +++ b/spoj_cpp_clean/BUGLIFE/BUGLIFE-12.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#define MAXV 2010 + +using namespace std; + +int V, E; + +bool graph[MAXV][MAXV]; +int color[MAXV]; + +void inicializa() +{ + for (int i = 0; i < V; i++) { + for (int j = i; j < V; j++) + graph[i][j] = graph[j][i] = false; + graph[i][i] = false; + } +} + +bool solve(int vertex, int c) +{ + int i; + color[vertex] = 1 - c; + + for (int i = 0; i < V; i++) { + if (graph[i][vertex]) { + if (color[i] == -1) { + if (!solve(i, 1 - c)) + return false; + } else if (color[i] == color[vertex]) + return false; + } + } + return true; +} + +bool graph_is_bicolorable() +{ + for (int i = 0; i < V; i++) + color[i] = -1; + for (int i = 0; i < V; i++) + if (color[i] == -1) + if (!solve(i, 0)) + return false; + return true; +} + +int main() +{ + ios::sync_with_stdio(false); + int t, p = 1; + cin >> t; + while (t--) { + cout << "Scenario #" << p++ << ":\n"; + cin >> V >> E; + inicializa(); + int a, b; + for (int i = 0; i < E; ++i) { + cin >> a >> b; + graph[a - 1][b - 1] = true; + graph[b - 1][a - 1] = true; + } + if (graph_is_bicolorable()) + cout << "No suspicious bugs found!\n"; + else + cout << "Suspicious bugs found!\n"; + } + return 0; +} diff --git a/spoj_cpp_clean/BUGLIFE/BUGLIFE-16.cpp b/spoj_cpp_clean/BUGLIFE/BUGLIFE-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d99a5656dae2f0ab7e7ed85cd594c124d43210b0 --- /dev/null +++ b/spoj_cpp_clean/BUGLIFE/BUGLIFE-16.cpp @@ -0,0 +1,88 @@ +// [amitdu6ey] + // g++ -std=c++11 -o2 -Wall filename.cpp -o filename + #include + #define hell 1000000009 + #define bug1(x) cout<<"$ "<=b;i--) + #define loop(i,a,b) for(long long i=a; i + #define vvbool vector< vector > + #define vchar vector + #define vi vector + #define vvi vector< vector > + #define pll pair + #define vll vector + #define vvl vector< vector > + #define ninf numeric_limits::min() + #define pinf numeric_limits::max() + #define comp(a,b) (abs(a-b)<1e-9) // to compare doubles + using namespace std; + +bool isbipartite(ll s, vector* adj, vector& vis, vector& col){ + queue q; + q.push(s); + vis[s]=true; + while(!q.empty()){ + ll v = q.front(); + q.pop(); + vis[v]=true; + for(auto u : adj[v]){ + if(vis[u]){ + if(col[u]==col[v]) return false; + else continue; + } + q.push(u); + vis[u]=true; + col[u]=!col[v]; + } + } + return true; +} + + bool solve(){ + ll n,m; + cin>>n>>m; + vector vis(n+9,false); + vector col(n+9,-1); + vector adj[n+9]; + for(ll i=0;i>a>>b; + adj[a].pb(b); + adj[b].pb(a); + } + for(ll x=1;x<=n;x++){ + if(vis[x]) continue; + if(!isbipartite(x,adj,vis,col)){ + return false; + } + } + return true; + } + + int main(){ + //ios_base::sync_with_stdio(0); + //cout.tie(0); + //cin.tie(0); + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int test_cases=1; + cin>>test_cases; + for(int i=1;i<=test_cases;i++){ + cout<<"Scenario #"< +using namespace std; +typedef long long ll; +void fast() { + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); +} + +vector adj[(int)3e3]; +int vis[(int)3e3]; +bool can; +int a,b,t,n,m; +void dfs(int node, int gen) { + + vis[node]=gen; + + for(auto ch:adj[node]) { + + if(!vis[ch]) { + if (vis[node] == 1) + dfs(ch, 2); + else + dfs(ch, 1); + + } else if(vis[node] == vis[ch]) { + can = false; + return; + } + } + +} + +int main() { + fast(); + + cin>>t; + int c=1; + while (t--) { + cin>>n>>m; + + memset(vis,0,sizeof vis); + memset(adj,0,sizeof adj); + can=1; + + for(int i=0;i> a >> b; + adj[a].push_back(b); + adj[b].push_back(a); + } + + bool ans = 1; + + for(int i=0;i specials > bipartite > bipartite checking + difficulty: easy + date: 19/Aug/2020 + hint: check all connect components + by: @brpapa +*/ +#include +using namespace std; + +vector> adj_list; int V; + +enum { WHITE, BLACK, WITHOUT_COLOR }; +vector state; + +bool bfs(int s) { + state[s] = WHITE; + queue q; q.push(s); + bool isBipartite = true; + + while (!q.empty() && isBipartite) { + int u = q.front(); q.pop(); + + for (int v : adj_list[u]) { + if (state[v] == WITHOUT_COLOR) { + state[v] = (state[u]+1) % 2; + q.push(v); + } + else if (state[v] == state[u]) { // conflito de cor + isBipartite = false; + } + } + } + return isBipartite; +} + +int main() { + int T, t = 1; cin >> T; + while (T--) { + int E; cin >> V >> E; + adj_list.assign(V, vector()); + while (E--) { + int u, v; cin >> u >> v; u--; v--; + adj_list[u].push_back(v); + adj_list[v].push_back(u); + } + + bool isBipartite = true; + state.assign(V, WITHOUT_COLOR); + for (int root = 0; root < V; root++) + if (state[root] == WITHOUT_COLOR) + if (!bfs(root)) { + isBipartite = false; + } + + printf("Scenario #%d:\n", t++); + if (!isBipartite) cout << "Suspicious bugs found!\n"; + else cout << "No suspicious bugs found!\n"; + } + return 0; +} diff --git a/spoj_cpp_clean/BUGLIFE/BUGLIFE-7.cpp b/spoj_cpp_clean/BUGLIFE/BUGLIFE-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..694cc2a91dfd4f52e9cf6c06936ebe09a93ff259 --- /dev/null +++ b/spoj_cpp_clean/BUGLIFE/BUGLIFE-7.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +vector grafo[2100]; +int color[2100]; +int cont=0; + +bool EsBipartito(int s) +{ + queue q; + color[s]= 0; + q.push(s); + while(!q.empty()) + { + int u= q.front();q.pop(); + for (vector::iterator i = grafo[u].begin(); i != grafo[u].end(); ++i) + { + if (color[*i]==-1) + { + color[*i]= (color[u])?0:1; + q.push(*i); + }else + { + if (color[u]==color[*i]) + { + return false; + } + } + } + cont++; + } + + return true; +} + + +void ini(int x) +{ + for (int i = 0; i <= x; ++i) + { + grafo[i].clear(); + } + +} + + + +int main() +{ + int t,n,m,u,v,aux; + + scanf("%d",&t); + aux=1; + while(t--) + { + scanf("%d %d" ,&n,&m); + + memset(color,-1,sizeof(color)); + ini(n+1); + for (int i = 1; i <= m; ++i) + { + scanf("%d %d",&u,&v); + grafo[u].push_back(v); + grafo[v].push_back(u); + } + bool ans= true; + + for (int i = 1; i <=n ; ++i) + { + if (color[i]==-1) + { + ans&= EsBipartito(i); + } + } + + printf("Scenario #%d:\n", aux); + (ans)? printf("No suspicious bugs found!\n"):printf("Suspicious bugs found!\n"); + aux++; + } + + return 0; +} diff --git a/spoj_cpp_clean/BUGLIFE/BUGLIFE-9.cpp b/spoj_cpp_clean/BUGLIFE/BUGLIFE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63351e07ebf5d99b9fd85d5692f6938baf0581ef --- /dev/null +++ b/spoj_cpp_clean/BUGLIFE/BUGLIFE-9.cpp @@ -0,0 +1,91 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int N, E; + +vector< vector > G; +int V[2010]; + +inline void init() +{ + int i, f, s; + + G.clear(); + memset(V, -1, sizeof(V)); + + scanf("%d %d", &N, &E); + + G.resize(N+1); + + for(i=0; i +#include +#include +#include + +#define ll long long + +using namespace std; + +void solve(){ + vector> arr; + ll N,a,b; + cin>>N; + for(ll i=0;i>a>>b; + arr.push_back(make_pair(b,a)); + } + sort(arr.begin(),arr.end()); + ll count=1,prev=arr[0].first; + for(ll i=1;i= prev){ + count+=1; + prev=arr[i].first; + } + } + cout<>t; + while(t--){ + solve(); + } +return 0; diff --git a/spoj_cpp_clean/BUSYMAN/BUSYMAN-21.cpp b/spoj_cpp_clean/BUSYMAN/BUSYMAN-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b72938bbf2ba475d1d8d9b94a97085a16c24044 --- /dev/null +++ b/spoj_cpp_clean/BUSYMAN/BUSYMAN-21.cpp @@ -0,0 +1,35 @@ +/* + greedy + difficulty: easy + date: 23/Apr/2020 + problem: compute the maximum number of activities (each with start and end times) that you can do without overlapping them (one at a time) + hint: sort the activites by increasing end time + by: @brpapa +*/ +#include +using namespace std; + +typedef pair ii; // start, end + +// p1 < p2 +bool cmp(const ii p1, const ii p2) { + return p1.second < p2.second; +} + +int main() { + int T; cin >> T; + while (T--) { + int N; cin >> N; + vector time(N); for (ii &t : time) cin >> t.first >> t.second; + + sort(time.begin(), time.end(), cmp); + + int ans = 1; ii curr = time[0]; + for (int i = 1; i < N; i++) + if (time[i].first >= curr.second) + ans++, curr = time[i]; + + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/BUSYMAN/BUSYMAN-7.cpp b/spoj_cpp_clean/BUSYMAN/BUSYMAN-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51ce61e9ec4ef573b82ee8257f3bbd03bf271982 --- /dev/null +++ b/spoj_cpp_clean/BUSYMAN/BUSYMAN-7.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int n=0,m=0,ini=0,f=0,cont=0,aux=0; + vector > v; + + scanf("%d",&n); + for (int i = 0; i < n; ++i) + { + scanf("%d",&m); + v.clear(); + for (int i = 0; i < m; ++i) + { + scanf("%d %d",&ini,&f); + v.push_back(make_pair(f,ini)); //el par esta hecho (fin,inicio) + } + sort(v.begin(), v.end()); + cont=0; + aux=0; + for ( vector >::iterator i = v.begin(); i != v.end(); ++i) + { + if (i->second >= aux) + { + aux=i->first; + cont++; + } + } + printf("%d\n",cont ); + } + return 0; +} diff --git a/spoj_cpp_clean/BUSYMAN/BUSYMAN-9.cpp b/spoj_cpp_clean/BUSYMAN/BUSYMAN-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e730f5a5f99be275d35481c13d099faef88c727c --- /dev/null +++ b/spoj_cpp_clean/BUSYMAN/BUSYMAN-9.cpp @@ -0,0 +1,65 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int N; +vector< pair > A; + +void init() +{ + int i; + pair temp; + + scanf("%d", &N); + A.clear(); + + for(i=0; i curr; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + { + counter=1; + init(); + + curr=*A.begin(); + + for(i=1; i=curr.first) + { + curr=A[i]; + counter++; + } + + printf("%d\n", counter); + } + return 0; +} diff --git a/spoj_cpp_clean/BUZZW/BUZZW-9.cpp b/spoj_cpp_clean/BUZZW/BUZZW-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31f0acbad855d2937a8682016c5ccc677100f15f --- /dev/null +++ b/spoj_cpp_clean/BUZZW/BUZZW-9.cpp @@ -0,0 +1,103 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar_unlocked() + +using namespace std; + +string S; + +struct rolling_hash +{ + static const unsigned long long p = 1000000007ull; + string s; + int n; + vector pow, phash; + rolling_hash(string s) : s(s), n(s.length()), pow(n+1), phash(n+1) + { + pow[0] = 1; + phash[0] = 0; + + for (int i = 0; i < n; ++i) + { + phash[i+1] = s[i] + phash[i] * p; + pow[i+1] = pow[i] * p; + } + } + + long long hash(int i, int j) + { + return phash[j] - phash[i] * pow[j-i]; + } + +}; + +inline bool init() +{ + char c; + + S.clear(); + + c = gc(); + + if(c == '\n') + return 0; + + while(c!='\n') + { + if(c != ' ') + S.push_back(c); + c = gc(); + } + + return 1; +} + +inline void solve() +{ + int i, j; + rolling_hash RL(S); + unordered_map memo; + int MAX = 0; + + for(i = 1; i <= S.length(); i++) + { + MAX = 0; + for(j = 0; j + i <= S.length(); j++) + { + memo[RL.hash(j, j + i)]++; + MAX = max(MAX, memo[RL.hash(j, j + i)]); + } + + if(MAX == 1) + return; + + printf("%d\n", MAX); + } +} + +int main() +{ + //freopen("input.txt","r", stdin); + + while(init()) + { + solve(); + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/BYTESE1/BYTESE1-7.cpp b/spoj_cpp_clean/BYTESE1/BYTESE1-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5255ee8631a2dcb53f7fd4f5b6c11106d4998759 --- /dev/null +++ b/spoj_cpp_clean/BYTESE1/BYTESE1-7.cpp @@ -0,0 +1,99 @@ +#include +#define X first +#define Y second +#define MAXINT 20000000 + +using namespace std; + + +int movx[]={0,0,-1,1}; +int movy[]={1,-1,0,0}; + + +int grafo[150][150]; +int dist[150][150]; +int t,n,m,auxn,auxm,tiempo; + +bool valido(int a, int b) +{ + if ( (a>0 && a<=n) && b>0 && b<=m) + { + return true; + }else + { + return false; + } +} + +void bfs(int sx,int sy) +{ + queue > q; + pair u; + dist[sx][sy]= grafo[sx][sy]; + q.push( make_pair(sx,sy) ); + + while(!q.empty()) + { + u= q.front(); q.pop(); + for (int i = 0; i < 4; ++i) + { + int nx= u.X + movx[i]; + int ny= u.Y + movy[i]; + if (valido(nx,ny) && dist[u.X][u.Y]+ grafo[nx][ny] < dist[nx][ny]) + { + dist[nx][ny]= dist[u.X][u.Y]+ grafo[nx][ny]; + q.push( make_pair(nx,ny) ); + } + } + } +} + + +void inicializar(int array[][150],int n,int m,int x) +{ + for (int i = 0; i <=n ; ++i) + { + for (int j = 0; j <= m; ++j) + { + array[i][j]=x; + } + } +} + + +int main() +{ + pair D; + int aux; + scanf("%d",&t); + while(t--) + { + scanf("%d %d",&n,&m); + memset(grafo,0,sizeof(grafo)); + for (int i = 1; i <= n; ++i) + { + for (int j = 1; j <= m; ++j) + { + scanf("%d",&grafo[i][j]); + } + } + + scanf("%d %d %d",&auxn,&auxm,&tiempo); + D= make_pair(auxn,auxm); + inicializar(dist,n,m,MAXINT); + bfs(1,1); + + aux= tiempo - dist[D.X][D.Y]; + + if (aux<0) + { + printf("NO\n"); + }else + { + printf("YES\n"); + printf("%d\n",aux ); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/BYTESE2/BYTESE2-12.cpp b/spoj_cpp_clean/BYTESE2/BYTESE2-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3450ada98c1786aab5198e533f874c8f335fc52a --- /dev/null +++ b/spoj_cpp_clean/BYTESE2/BYTESE2-12.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +typedef unsigned long long uint64; +typedef unsigned long long int64; + +struct tri { + int ini, opa, idx; + int tipo; + tri(int tipo = 0, int ini = 0, int opa = 0, int idx = 0) + : tipo(tipo) + , ini(ini) + , opa(opa) + , idx(idx) + { + } + bool operator<(const tri& at) const + { + if (ini != at.ini) + return ini < at.ini; + if (tipo != at.tipo) + return tipo < at.tipo; + return idx < at.idx; + } +}; + +int main() +{ + ios::sync_with_stdio(false); + int t, n, a, b; + cin >> t; + while (t--) { + cin >> n; + vector eventos; + set livre; + int ans = 0; + for (int i = 0; i < n; i++) { + cin >> a >> b; + eventos.push_back(tri(0, a, a, i)); + eventos.push_back(tri(1, b, a, i)); + } + sort(eventos.begin(), eventos.end()); + set at; + for (int i = 0; i < 2 * n; i++) { + if (eventos[i].tipo) { + at.erase(eventos[i].opa); + } else { + at.insert(eventos[i].ini); + ans = max(ans, (int)at.size()); + } + } + cout << ans << '\n'; + } + return 0; +} diff --git a/spoj_cpp_clean/BYTESM1/BYTESM1-9.cpp b/spoj_cpp_clean/BYTESM1/BYTESM1-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db0a466546cf0b5f25a6ec2b2b1bf3daf3234e0d --- /dev/null +++ b/spoj_cpp_clean/BYTESM1/BYTESM1-9.cpp @@ -0,0 +1,92 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#define MAXN 99999999 + +using namespace std; + +bool one[100000000]; +bool three[100000000]; +bool nine[100000000]; + +inline int digit_sum(int num) +{ + int sum = 0; + + while(num) + { + sum += num % 10; + num /= 10; + } + + return sum; +} + +inline pair solve(int num) +{ + while(1) + { + if(one[num]) + return make_pair(1, num); + if(three[num]) + return make_pair(3, num); + if(nine[num]) + return make_pair(9, num); + + num += digit_sum(num); + } +} + +int main() +{ + int num = 0, add = 1, T, n; + pair ans; + + while(num + add <= MAXN) + { + num += add; + one[num] = 1; + add = digit_sum(num); + } + + num = 0; + add = 3; + + while(num + add <= MAXN) + { + num += add; + three[num] = 1; + add = digit_sum(num); + } + + num = 0; + add = 9; + + while(num + add <= MAXN) + { + num += add; + nine[num] = 1; + add = digit_sum(num); + } + + scanf("%d", &T); + + while(T--) + { + scanf("%d", &n); + ans = solve(n); + printf("%d %d\n", ans.first, ans.second); + } + + return 0; +} diff --git a/spoj_cpp_clean/BYTESM2/BYTESM2-16.cpp b/spoj_cpp_clean/BYTESM2/BYTESM2-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0040656c30f694cb15c50eb3cd408fb7809b20e --- /dev/null +++ b/spoj_cpp_clean/BYTESM2/BYTESM2-16.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int a[109][109]; +int dp[109][109]; + +void solve(){ + int h,w; + cin>>h>>w; + for(int i=0;i>a[i][j]; + } + } + for(int i=0;i=0;i--){ + for(int j=0;j>test; + while(test--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/BYTESM2/BYTESM2-17.cpp b/spoj_cpp_clean/BYTESM2/BYTESM2-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6505d0d0f9df5f996f94e0bc56d37936e31e419d --- /dev/null +++ b/spoj_cpp_clean/BYTESM2/BYTESM2-17.cpp @@ -0,0 +1,65 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + __builtin_popcount(); +*/ +bool isPrime(int x) { + if (x <= 1) return false; + if (x <= 3) return true; + if (x % 2 == 0 || x % 3 == 0) return false; + for (int i = 5; i * i <= x; i = i + 6) + if (x % i == 0 || x % (i + 2) == 0) return false; + return true; +} + +int h,w; +int arr[102][102]; +int dp[102][102]; +int solve (int i,int j) { + if(i==h) { + return 0; + } + + int &a=dp[i][j]; + + if(a!=-1) return a; + + a = -100; + a= max(a, arr[i][j]+solve(i+1,j)); + if(j-1 >=0) a= max(a,arr[i][j] + solve(i+1,j-1)); + if(j+1>t; + while (t--) { + cin>>h>>w; + for(i=0;i>arr[i][j]; + } + } + memset(dp,-1,sizeof dp); + int ans = -100; + for(j=0;j +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[102][111]; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + s(T); + while(T--) + { + s(n); + s(m); + memset(a, 0, sizeof(a)); + FOR(i,0,n) + { + FOR(j,0,m) + { + s(a[i][j]); + } + } + FOR(i,1,n) + { + FOR(j,0, m) + { + if(j==0) + { + a[i][0]=a[i][0]+maX(a[i-1][0], a[i-1][1]); + } + else if(j==m-1) + { + a[i][m-1]=a[i][m-1]+maX(a[i-1][m-1], a[i-1][m-2]); + } + else + a[i][j]=a[i][j]+maX(a[i-1][j-1], maX(a[i-1][j], a[i-1][j+1])); + } + } +/* + cout<mx) + mx=a[n-1][i]; + } + cout< +#include +#include + +using namespace std; + +int main() +{ + int test,row,i,j,coloumn; + cin>>test; + while(test--) + { + cin>>row>>coloumn; + int **mat=new int*[row]; + int **dp=new int*[row]; + for(i=0;i>mat[i][j]; + } + } + for(i=0;i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int board[105][105]; +int N, M; + +inline int max3(int a, int b, int c) +{ + return max(a, max(b, c)); +} + +int solve() +{ + int i, j; + int sol; + + for(i=N-1; i>=2; i--) + { + board[i][1]+=max(board[i+1][1], board[i+1][2]); + board[i][M]+=max(board[i+1][M], board[i+1][M-1]); + + for(j=2; j<=M-1; j++) + board[i][j]+=max3(board[i+1][j-1], board[i+1][j], board[i+1][j+1]); + } + + board[1][1]+=max(board[2][1], board[2][2]); + board[1][M]+=max(board[2][M], board[2][M-1]); + sol=max(board[1][1], board[1][M]); + + for(i=2; i<=M-1; i++) + { + board[1][i]+=max3(board[2][i-1], board[2][i], board[2][i+1]); + sol=max(sol, board[1][i]); + } + + return sol; +} + +inline void init() +{ + int i, j; + + scanf("%d %d", &N, &M); + + for(i=1; i<=N; i++) + for(j=1; j<=M; j++) + scanf("%d", &board[i][j]); +} + +int main() +{ + int T; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + { + init(); + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/CADYDIST/CADYDIST-21.cpp b/spoj_cpp_clean/CADYDIST/CADYDIST-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ea6b3e48eca0714f1be1662a0c6a036f01be75e --- /dev/null +++ b/spoj_cpp_clean/CADYDIST/CADYDIST-21.cpp @@ -0,0 +1,28 @@ +/* + greedy + difficulty: easy + date: 21/Feb/2020 + by: @brpapa +*/ +#include +#include +#include +#define ll long long +using namespace std; + +int main() { + while (1) { + int N; cin >> N; if (!N) break; + vector c(N), p(N); + for (ll &v : c) cin >> v; + for (ll &v : p) cin >> v; + + sort(c.begin(), c.end()); + sort(p.begin(), p.end(), greater()); + + ll ans = 0; + for (int n = 0; n < N; n++) ans += c[n]*p[n]; + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CADYDIST/CADYDIST-9.cpp b/spoj_cpp_clean/CADYDIST/CADYDIST-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c78c7e1622682853e8f0be28687904d07c28c283 --- /dev/null +++ b/spoj_cpp_clean/CADYDIST/CADYDIST-9.cpp @@ -0,0 +1,58 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int N; +long long A[100007], B[100007]; + +inline bool init() +{ + int i; + + scanf("%d", &N); + + if(!N) + return 0; + + for(i = 0; i()); + + return 1; +} + +inline long long solve() +{ + long long ans = 0; + + for(int i = 0; i +using namespace std; + +int main() +{ + int n; + double res = 1; + cin >> n; + while(n-- > 0) + { + int va; + char op; + cin >> va >> op; + if(op == '*') + res *= va; + else if(op == '/') + res /= va; + } + cout << (int)res; + +} diff --git a/spoj_cpp_clean/CALCULA/CALCULA-10.cpp b/spoj_cpp_clean/CALCULA/CALCULA-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c96b15524b470a5b847cb20c841d2ebce01f7ddd --- /dev/null +++ b/spoj_cpp_clean/CALCULA/CALCULA-10.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main() +{ + int n, test=0; + while(cin >> n && n != 0) + { + int res; + int v; + char o; + cin >> res; + n--; + while(n-- > 0) + { + cin >> o >> v; + if(o == '+') res += v; + else res -= v; + } + cout << "Teste " << ++test << endl << res << endl << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/CAM5/CAM5-7.cpp b/spoj_cpp_clean/CAM5/CAM5-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8342093df9bbf5afee905b5e9fc2b939f75c3a8 --- /dev/null +++ b/spoj_cpp_clean/CAM5/CAM5-7.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +vector grafo[100010]; +bool visit[100010]; + +void dfs(int u) +{ + visit[u]= true; + for (int i = 0; i < grafo[u].size(); ++i) + { + int nuevo= grafo[u][i]; + if (!visit[nuevo]) + { + dfs(nuevo); + } + } +} + + +int main() +{ + int t,n,e,a,b; + int ans; + + scanf("%d",&t); + + while(t--) + { + scanf("%d",&n); + scanf("%d",&e); + + for (int i = 0; i < n; ++i) + { + visit[i]=false; + grafo[i].clear(); + } + + for (int i = 0; i < e; ++i) + { + scanf("%d %d",&a,&b); + grafo[a].push_back(b); + grafo[b].push_back(a); + } + ans=0; + for (int i = 0; i < n; ++i) + { + if (!visit[i]) + { + ans++; + dfs(i); + } + } + + printf("%d\n",ans ); + } + + return 0; +} diff --git a/spoj_cpp_clean/CANDY/CANDY-11.cpp b/spoj_cpp_clean/CANDY/CANDY-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b75294deeb69163753db5cd2c4007a4c302365e6 --- /dev/null +++ b/spoj_cpp_clean/CANDY/CANDY-11.cpp @@ -0,0 +1,47 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include + +using namespace std; + +int main(void) +{ + int n; + int input[10001]; + long long sum, moves; + + cin >> n; + + while(n != -1) + { + sum = 0ll; + + for(int i = 0; i < n; i++) + { + cin >> input[i]; + sum += input[i]; + } + + if(sum % n == 0) + { + moves = 0ll; + + sum = sum / n; + + for(int i = 0; i < n; i++) + if(input[i] < sum) + moves += (sum - input[i]); + + cout << moves << endl; + } + else + cout << -1 << endl; + + cin >> n; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CANDY/CANDY-16.cpp b/spoj_cpp_clean/CANDY/CANDY-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2387d84fb47af5bd03e110c4bb98e90666c5647a --- /dev/null +++ b/spoj_cpp_clean/CANDY/CANDY-16.cpp @@ -0,0 +1,34 @@ +#include +int fun(int arr[],int sum,int t){ +int temp,i,ans=0; +temp=sum/t; + if(temp*t==sum) + { + for(i=0;iarr[i]) + ans+=temp-arr[i]; + } + printf("%d\n",ans); + } + else + printf("-1\n"); +} + +int main() +{ + int t; + scanf("%d",&t); + while(t!=-1) + { + int ans=0,temp,sum=0,arr[t],i; + for(i=0;i +#define ll long long +#define endl "\n" +#define MAX 100001 +#define mod 1000000007 +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + int packets=0; + while(true){ + cin>>packets; + if(packets == -1){ + break; + } + int candy[packets]; + ll sum=0, temp, ans=0; + for(int i=0; i>candy[i]; + sum += candy[i]; + } + if( sum%packets != 0){ + cout<<"-1"<candy[i]){ + ans += temp - candy[i]; + } + } + cout< + +using namespace std; + +int main() +{ + int n,i,sum,count,equal; + cin>>n; + while(n>0) + { + sum=0;count=0; + int candy[n]; + for(i=0;i>candy[i]; + sum=sum+candy[i]; + } + if(sum%n==0) + { + equal=sum/n; + for(i=0;iequal) + { + count=count+(candy[i]-equal); + } + } + cout<>n; + } + return 0; +} diff --git a/spoj_cpp_clean/CANDY/CANDY-7.cpp b/spoj_cpp_clean/CANDY/CANDY-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b0a0959b27c4473ec5ba2fe1c21053cd86cce72 --- /dev/null +++ b/spoj_cpp_clean/CANDY/CANDY-7.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int candies[11000]; + + +int main() +{ + int n, aux, numero,promedio; + scanf("%d",&n); + while( n!=-1) + { + memset(candies,0,sizeof(candies)); + numero=0; + for (int i = 0; i < n; ++i) + { + scanf("%d",&aux); + candies[i]= aux; + numero+= aux; + } + + if (numero%n!=0) + { + printf("%d\n",-1); + }else + { + promedio= numero/n; + aux=0; + for (int i = 0; i < n; ++i) + { + if (candies[i]>promedio) + { + aux+= candies[i]-promedio; + } + } + + printf("%d\n",aux ); + } + + scanf("%d",&n); + } + + + return 0; +} diff --git a/spoj_cpp_clean/CANDY/CANDY-9.cpp b/spoj_cpp_clean/CANDY/CANDY-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b9c83cef348565d0182b5822ece32700260627e --- /dev/null +++ b/spoj_cpp_clean/CANDY/CANDY-9.cpp @@ -0,0 +1,69 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int N; +vector A; +long long sum; + +inline bool init() +{ + int i, num; + + sum = 0; + A.clear(); + + scanf("%d", &N); + + if(N == -1) + return 0; + + for(i=0; i + +using namespace std; + +int main(void) +{ + long long int t, n, sum; + long long int input; + + cin >> t; + + while(t--) + { + cin >> n; + + sum = 0ll; + + for(long long i = 0ll; i < n; i++) + { + cin >> input; + sum = (sum + input) % n; + } + + if(sum) + cout << "NO" << endl; + else + cout << "YES" << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CANDY3/CANDY3-16.cpp b/spoj_cpp_clean/CANDY3/CANDY3-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17fc8e709a41c91c42bf758403b5ad657c86e554 --- /dev/null +++ b/spoj_cpp_clean/CANDY3/CANDY3-16.cpp @@ -0,0 +1,32 @@ +#include + +#define ll unsigned long long + +using namespace std; + +void solve(ll n){ + ll a=0, sum=0; + for(ll i=0;i>a; + sum=(sum+a)%n; + } + if(sum == 0){ + cout<<"YES\n"; + } + else{ + cout<<"NO\n"; + } + return; +} + + +int main(){ + ll t=1; + cin>>t; + while(t--){ + ll n; + cin>>n; + solve(n); + } + return 0; +} diff --git a/spoj_cpp_clean/CANDY3/CANDY3-5.cpp b/spoj_cpp_clean/CANDY3/CANDY3-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..adbcde33a6f77985c1a7781e164694f2ce07cfdc --- /dev/null +++ b/spoj_cpp_clean/CANDY3/CANDY3-5.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +int main() +{ + long long int test,n,i,j,sum; + cin>>test; + while(test--) + { + getchar(); + cin>>n; + sum=0; + for(i=0;i>j; + sum=(sum+j)%n; + } + if(sum==0) + cout<<"YES"< + +using namespace std; + +int main() +{ + long long int t,n,aux,sum; + + scanf("%lld",&t); + while(t--) + { + scanf("%lld",&n); + sum=0; + for (int i = 0; i < n; ++i) + { + scanf("%lld",&aux); + sum= (sum%n + aux%n)%n; + + } + + (sum==0)?printf("YES\n"):printf("NO\n");; + } + + return 0; +} diff --git a/spoj_cpp_clean/CANTON/CANTON-16.cpp b/spoj_cpp_clean/CANTON/CANTON-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2b53b86dc9d0ce19debd27b334e424f2e7917c8 --- /dev/null +++ b/spoj_cpp_clean/CANTON/CANTON-16.cpp @@ -0,0 +1,70 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "<=b;i--) +#define loop(i,a,b) for(long long i=a; i +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +vector > ans(1e7+9); +void solve(){ + int t; + cin>>t; + while(t--){ + int N,a=1,n=1; + cin>>N; + + bool itr=true; + while(a<=N){ + if(!itr){ + for(int x=1, y=n;x<=n and y>=1;x++, y--){ + ans[a]={x,y}; + a++; + } + } + else{ + for(int x=1, y=n;x<=n and y>=1;x++, y--){ + ans[a]={y,x}; + a++; + } + } + itr=!itr; + n++; + } + + cout<<"TERM "<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/CANTON/CANTON-19.cpp b/spoj_cpp_clean/CANTON/CANTON-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e31d61544ff265655a7f2d6896943b907c5fa59f --- /dev/null +++ b/spoj_cpp_clean/CANTON/CANTON-19.cpp @@ -0,0 +1,38 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 1000001 +#define PI 3.14159265 + +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + + int times; cin >> times; while (times--) { + unsigned ll n, sum = 0; + cin >> n; + unsigned i, upper, lower; + for (i = 1; i < n; i++) { + sum += i; + if (sum >= n) { + sum -= i; + sum = n - sum; + break; + } + } + if (i % 2 == 0) { + upper = sum; + lower = i - sum + 1; + } + else { + lower = sum; + upper = i - sum + 1; + } + cout << "TERM " << n << " IS " << upper << "/" << lower << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CANTON/CANTON-5.cpp b/spoj_cpp_clean/CANTON/CANTON-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..349034b0a2916ed5d2f6a5769f24cbba41f497ee --- /dev/null +++ b/spoj_cpp_clean/CANTON/CANTON-5.cpp @@ -0,0 +1,43 @@ +// For reference look up algorithmist and codegeeksblog.wordpress.com or wiki diagonal argument article + +#include +#include + +using namespace std; + +int main() +{ + int test,num,temp,sum,i; + cin>>test; + while(test--) + { + cin>>num; + sum=0; + // Triangular progression series + + for(i=1;;i++) + { + sum=sum+i; + if(sum>=num) + break; + } + int aux=i+1; // i represents the number of elements in that diagonal + // and numerator and denominator in each elements sums up to 1 more than i, so aux; + + temp=num-(sum-i); // temp would be numerator or denominator, depending upon the condition + // and so other element would be aux-temp + + + if(i%2==0) + { + cout<<"TERM "< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +int V[8][8]; +deque< pair > > Q; +pair start, finish; +int mmin=INT_MAX; +int dx[] = {-1, 1, 2, 2, 1, -1, -2, -2}; +int dy[] = {-2, -2, -1, 1, 2, 2, 1, -1}; + +inline void init_V() +{ + int i, j; + + for(i=0; i<8; i++) + for(j=0; j<8; j++) + V[i][j]=INT_MAX; +} + +inline int calc_cost(pair from, pair to) +{ + return from.first * to.first + from.second * to.second; +} + +inline bool outofrange(pair pos) +{ + if(pos.first < 0 || pos.first > 7 || pos.second < 0 || pos.second > 7) + return 1; + return 0; +} + +int solve() +{ + int i; + pair > curr; + + Q.clear(); + mmin=INT_MAX; + Q.push_back(make_pair(0, start)); + + while(!Q.empty()) + { + curr=Q.front(); + Q.pop_front(); + + if(curr.second == finish) + { + mmin=min(mmin, curr.first); + continue; + } + + if(outofrange(curr.second)) + continue; + + if(V[curr.second.first][curr.second.second] <= curr.first) + continue; + + V[curr.second.first][curr.second.second]=curr.first; + + for(i=0; i<8; i++) + Q.push_back(make_pair(curr.first + calc_cost(curr.second, make_pair(curr.second.first + dx[i], curr.second.second + dy[i])), make_pair(curr.second.first + dx[i], curr.second.second + dy[i]))); + } + + if(mmin==INT_MAX) + return -1; + return mmin; +} + +int main() +{ + //freopen("input.txt","r",stdin); + + while(!feof(stdin)) + { + if(!scanf("%d %d %d %d\n", &start.first, &start.second, &finish.first, &finish.second)) + return 0; + init_V(); + printf("%d\n", solve()); + } + + return 0; +} + diff --git a/spoj_cpp_clean/CDC12_B/CDC12_B-7.cpp b/spoj_cpp_clean/CDC12_B/CDC12_B-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21e116f40091124b7d1c22244260793348c89ff3 --- /dev/null +++ b/spoj_cpp_clean/CDC12_B/CDC12_B-7.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +class Rutina +{ +public: + string nombre; + int energia; + + Rutina(){}; + Rutina(string cadena, int valor) + { + nombre= cadena; + energia= valor; + } + + bool operator< (const Rutina &otro)const + { + if (energia==otro.energia) + { + return nombre< otro.nombre; + }else + { + return energia rutinas; + +int main() +{ + int t, n,k; + string nombre; + int valor; + vector ans; + + scanf("%d",&t); + + + for (int tt = 1; tt <= t; ++tt) + { + ans.clear(); + rutinas.clear(); + scanf("%d %d",&n,&k); + for (int i = 0; i < n; ++i) + { + cin>>nombre>>valor; + rutinas.push_back( Rutina(nombre,valor) ); + } + + sort(rutinas.begin(),rutinas.end()); + + int aux=0; + for (int i = 0; i < n; ++i) + { + k-= rutinas[i].energia; + if (k<0) + { + break; + } + aux++; + ans.push_back(rutinas[i].nombre); + k+=aux; + } + + sort(ans.begin(),ans.end()); + printf("Scenario #%d: %d\n",tt,(int)ans.size()); + + + for (int i = 0; i < ans.size()-1; ++i) + { + printf("%s ",ans[i].c_str()); + } + + printf("%s\n",ans.back().c_str()); + } + + return 0; +} diff --git a/spoj_cpp_clean/CDC12_E/CDC12_E-7.cpp b/spoj_cpp_clean/CDC12_E/CDC12_E-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b677535312ce2d770b77000a37e117ab20ce4a6a --- /dev/null +++ b/spoj_cpp_clean/CDC12_E/CDC12_E-7.cpp @@ -0,0 +1,117 @@ +#include +#define MAXSIZE 1010 +#define INFINITO 0x3f3f3f3f +using namespace std; + + +class Edge +{ +public: + int X; + int Y; + int peso; + Edge(); + Edge(int a,int b,int w) + { + X=a; + Y=b; + peso=w; + } + + bool operator<(const Edge &otro)const{ + return (peso>otro.peso)?true:false; + } + +}; + + +int t,n,m,k; + +char grafo[MAXSIZE][MAXSIZE]; +int dist[MAXSIZE][MAXSIZE]; + +int movx[]= {0,1,-1,0}; +int movy[]= {1,0,0,-1}; + +bool valido(int a, int b){ + return ((a=0) && (b=0))?true:false; +} + +void bfs(int sx,int sy) +{ + priority_queueq; + int nx,ny; + dist[sx][sy]=0; + q.push( Edge(sx,sy,0) ); + + while(!q.empty()) + { + Edge u= q.top();q.pop(); + + for (int i = 0; i < 4; ++i) + { + nx= u.X + movx[i]; + ny= u.Y + movy[i]; + + if (valido(nx,ny) && dist[u.X][u.Y] + 1 + (grafo[u.X][u.Y]-'0') < dist[nx][ny] ) + { + dist[nx][ny]= dist[u.X][u.Y] + 1 + (grafo[u.X][u.Y]-'0'); + q.push( Edge(nx,ny,dist[nx][ny]) ); + } + } + } +} + + +int main() +{ + pair D,I; + scanf("%d",&t); + for (int tt = 1; tt <=t ; ++tt) + { + scanf("%d %d %d",&n,&m,&k); + for (int i = 0; i < n; ++i) + { + scanf("%s",grafo[i]); + } + + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + if (grafo[i][j]=='R') + { + I= make_pair(i,j); + grafo[i][j]='0'; + }else if (grafo[i][j]=='D') + { + D= make_pair(i,j); + grafo[i][j]='0'; + }else + { + if (grafo[i][j]=='.') + { + grafo[i][j]='0'; + } + } + dist[i][j]= INFINITO; + } + } +/* + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + printf("%c",grafo[i][j] ); + } + printf("\n"); + } + */ + + bfs(I.first,I.second); + int ans= dist[D.first][D.second]; + (ans Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int sum; +int memo[100007]; + +inline void init() +{ + memset(memo, -1, sizeof(memo)); + + memo[0] = 0; + memo[1] = 1; + memo[2] = 2; + memo[3] = 0; +} + +int F(int a) +{ + + if (memo[a] != -1) + return memo[a]; + + int root = sqrt(a); + + if (root * root == a) + { + memo[a] = 2 * F(root); + return memo[a]; + } + + for (int i = root; i > 1; i--) + if (a % i == 0) + { + memo[a] = F(i) + F(a / i); + return memo[a]; + } + + memo[a] = 0; + return 0; +} + +int main() +{ + int num; + + init(); + scanf("%d", &num); + printf("%d\n", F(num)); + + return 0; +} diff --git a/spoj_cpp_clean/CEQU/CEQU-3.cpp b/spoj_cpp_clean/CEQU/CEQU-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9eec8c712117473cd7a40c8705c5d7288c65087 --- /dev/null +++ b/spoj_cpp_clean/CEQU/CEQU-3.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, a, b, c; + sd(t); + for(int i = 1; i <= t; i++){ + sd3(a,b,c); + if(c % __gcd(a,b) == 0) printf("Case %d: Yes\n",i); + else printf("Case %d: No\n",i); + } + return 0; +} diff --git a/spoj_cpp_clean/CERC07B/CERC07B-12.cpp b/spoj_cpp_clean/CERC07B/CERC07B-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a3afb10d527e67d86959e2f14c33a56a2d1e696 --- /dev/null +++ b/spoj_cpp_clean/CERC07B/CERC07B-12.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int INF = 0x3f3f3f3f; + +int main() +{ + ios::sync_with_stdio(false); + int r, c; + char str[100]; + while (cin >> r >> c && r && c) { + int mat[100][100], aux[100][100]; + ; + for (int i = 0; i < r; i++) { + cin >> str; + for (int j = 0; j < c; j++) { + if (str[j] == 'X') + mat[i][j] = aux[i][j] = 1; + else + mat[i][j] = aux[i][j] = 0; + } + } + int resp = INF; + for (int i = 0; i < (1 << c); i++) { + int click = 0; + for (int j = 0; j < c; j++) { + if (i & (1 << j)) { + if (j - 1 >= 0) + mat[0][j - 1] = !mat[0][j - 1]; + if (j + 1 < c) + mat[0][j + 1] = !mat[0][j + 1]; + if (r >= 2) + mat[1][j] = !mat[1][j]; + mat[0][j] = !mat[0][j]; + click++; + } + } + bool flag = false; + if (r >= 2) { + for (int j = 1; j < r; j++) { + for (int k = 0; k < c; k++) { + if (mat[j - 1][k]) { + if (k - 1 >= 0) + mat[j][k - 1] = !mat[j][k - 1]; + if (k + 1 < c) + mat[j][k + 1] = !mat[j][k + 1]; + if (j + 1 < r) + mat[j + 1][k] = !mat[j + 1][k]; + mat[j - 1][k] = !mat[j - 1][k]; + mat[j][k] = !mat[j][k]; + click++; + } + mat[j - 1][k] = aux[j - 1][k]; + } + } + } + for (int j = 0; j < c; j++) { + if (mat[r - 1][j]) + flag = true; + mat[r - 1][j] = aux[r - 1][j]; + } + if (!flag) + resp = min(click, resp); + } + if (resp != INF) + cout << "You have to tap " << resp << " tiles.\n"; + else + cout << "Damaged billboard.\n"; + } + return 0; +} diff --git a/spoj_cpp_clean/CERC07R/CERC07R-12.cpp b/spoj_cpp_clean/CERC07R/CERC07R-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..865649fa8e42ff24805e6330ccf9b9e7124faa4a --- /dev/null +++ b/spoj_cpp_clean/CERC07R/CERC07R-12.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define MAX 1000010 +#define color 2 +#define mp make_pair +#define pb push_back + +using namespace std; + +typedef pair ii; +typedef pair ib; +typedef vector vi; +typedef long long int64; + +char num[MAX]; + +const int INF = 0x3f3f3f3f; + +int main() +{ + ios::sync_with_stdio(false); + string str, player1, str2, player2; + map type; + type.insert(mp("Kamen", 3)); + type.insert(mp("Rock", 3)); + type.insert(mp("Pierre", 3)); + type.insert(mp("Stein", 3)); + type.insert(mp("Ko", 3)); + type.insert(mp("Koe", 3)); + type.insert(mp("Sasso", 3)); + type.insert(mp("Roccia", 3)); + type.insert(mp("Guu", 3)); + type.insert(mp("Kamien", 3)); + type.insert(mp("Piedra", 3)); + + type.insert(mp("Nuzky", 2)); + type.insert(mp("Scissors", 2)); + type.insert(mp("Ciseaux", 2)); + type.insert(mp("Schere", 2)); + type.insert(mp("Ollo", 2)); + type.insert(mp("Olloo", 2)); + type.insert(mp("Forbice", 2)); + type.insert(mp("Choki", 2)); + type.insert(mp("Nozyce", 2)); + type.insert(mp("Tijera", 2)); + + type.insert(mp("Papir", 1)); + type.insert(mp("Paper", 1)); + type.insert(mp("Feuille", 1)); + type.insert(mp("Papier", 1)); + type.insert(mp("Carta", 1)); + type.insert(mp("Rete", 1)); + type.insert(mp("Paa", 1)); + type.insert(mp("Papel", 1)); + int t = 1; + for (cin >> str; str != "."; cin >> str) { + cout << "Game #" << t++ << ":\n"; + cin >> player1 >> str2 >> player2; + pair p1 = mp(0, player1), p2 = mp(0, player2); + for (cin >> str; str != "-" && str != "."; cin >> str) { + cin >> str2; + if (type[str] == 3 && type[str2] == 2) + p1.first++; + else if (type[str] == 2 && type[str2] == 3) + p2.first++; + else if (type[str] == 3 && type[str2] == 1) + p2.first++; + else if (type[str] == 1 && type[str2] == 3) + p1.first++; + else if (type[str] == 2 && type[str2] == 1) + p1.first++; + else if (type[str] == 1 && type[str2] == 2) + p2.first++; + } + cout << p1.second << ": "; + if (p1.first == 0 || p1.first > 1) + cout << p1.first << " points\n"; + else + cout << p1.first << " point\n"; + cout << p2.second << ": "; + if (p2.first == 0 || p2.first > 1) + cout << p2.first << " points\n"; + else + cout << p2.first << " point\n"; + if (p1.first == p2.first) + cout << "TIED GAME\n\n"; + else if (p1.first > p2.first) + cout << "WINNER: " << p1.second << "\n\n"; + else if (p1.first < p2.first) + cout << "WINNER: " << p2.second << "\n\n"; + if (str == ".") + break; + } + return 0; +} diff --git a/spoj_cpp_clean/CFPARTY/CFPARTY-4.cpp b/spoj_cpp_clean/CFPARTY/CFPARTY-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77e4df3bee0fdea761a0af0412240f6b9f5ca4a9 --- /dev/null +++ b/spoj_cpp_clean/CFPARTY/CFPARTY-4.cpp @@ -0,0 +1,106 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +//Other Includes +#include +#include +#include +#include +#include +#include +#include +#include +//some common functionn +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define all(c) (c).begin(),(c).end() +// Input macros +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) + +// Output macros +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +using namespace std; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i>n; + return true; +} + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + + int T=1; + s(T); + for(int testnum=1;testnum<=T;testnum++) + { + if(!input()) break; + + solve(); + //printf("\n"); + + } +// system("pause"); + return 0; +} + + + + diff --git a/spoj_cpp_clean/CHEFCH/CHEFCH-5.cpp b/spoj_cpp_clean/CHEFCH/CHEFCH-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a015e50baa6b0c1b14d19879facb62379d847da7 --- /dev/null +++ b/spoj_cpp_clean/CHEFCH/CHEFCH-5.cpp @@ -0,0 +1,85 @@ +#include +#include +#include + +using namespace std; + +//int cnt,cnt2; +void change(char &x,long long &cntt) +{ + if(x=='+'){ + x='-'; + cntt++; + } + else{ + x='+'; + cntt++; + } +} + +int main() +{ + long long test,len,i,j,k,k2,cnt,cnt2; + //string input; + string input,inp2,inp3; + cin>>test; + while(test--) + { + cin>>input; + inp2=input; + inp3=input; + len=input.length(); + //len=strlen(input); + cnt=0; + cnt2=0; + if(len==1) + { + cout<<"0"<0;i--) + { + if(inp3[i-1]!=inp3[i] && inp3[i+1]!=inp3[i]) + continue; + else if(inp3[i-1]==inp3[i]&&inp3[i+1]!=inp3[i]) + change(inp3[i-1],cnt2); + else if(inp3[i+1]==inp3[i]&&inp3[i-1]!=inp3[i]) + change(inp3[i+1],cnt2); + else if(inp3[i+1]==inp3[i]&&inp3[i]==inp3[i-1]) + change(inp3[i],cnt2); + } + if(cnt<=cnt2){ + cout< +#include + +using namespace std; + +int main() +{ + long long test,len,i,j,k,k2,cnt,cnt2; + string input; + cin>>test; + while(test--) + { + cin>>input; + cnt=0; + cnt2=0; + len=input.length(); + if(len==1) + { + cout<<"0"< +#include + +using namespace std; + +int main() +{ + int test,n,i,j,k,freq; + cin>>test; + while(test--) + { + cin>>n; + //int *arr=new int[n]; + int *temp=new int[n]; + int *freqArr=new int[n]; + for(i=0;i>temp[i]; + //temp[i]=arr[i]; + } + sort(temp,temp+n); + freq=1; + for(i=0,j=0;i +#include +#include + +using namespace std; + +const double INF = 0x3f3f3f3f; + +int main() +{ + ios::sync_with_stdio(false); + int m, n, t; + for (cin >> n; n != 0; cin >> n) { + cin >> m; + double ruas[n + 1][n + 1], p; + int a, b; + for (int i = 1; i < n + 1; ++i) { + for (int j = i; j < n + 1; ++j) + ruas[i][j] = ruas[j][i] = 0.0; + ruas[i][i] = 1.0; + } + for (int i = 0; i < m; ++i) { + cin >> a >> b >> p; + ruas[a][b] = ruas[b][a] = p / 100.00; + } + for (int k = 1; k < n + 1; ++k) { + for (int i = 1; i < n + 1; ++i) { + for (int j = 1; j < n + 1; ++j) { + if ((ruas[i][k] * ruas[k][j]) != 0) + ruas[i][j] = max(ruas[i][j], (ruas[i][k] * ruas[k][j])); + } + } + } + cout << fixed << setprecision(6) << ruas[1][n] * 100.0 << " percent\n"; + } + return 0; +} diff --git a/spoj_cpp_clean/CHOCOLA/CHOCOLA-13.cpp b/spoj_cpp_clean/CHOCOLA/CHOCOLA-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37401d64e68258d549b399b562920bd4c95928fe --- /dev/null +++ b/spoj_cpp_clean/CHOCOLA/CHOCOLA-13.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +#include +#include +#include + +using namespace std; + +typedef long long i64; + +int main(){ + int T; scanf("%d\n\n", &T); + for(int t=1; t<=T; ++t){ + int n, m; scanf("%d %d", &m, &n); + vector vy(n), vx(m); + vy[0] = 0, vx[0] = 0; + + int sx, sy, res; + sx = sy = res = 0; + + //printf("DEBUG: %d %d\n", m, n); + + for(int i=1; i vy[i]){ + res += vx[j] + sy; + sx-=vx[j]; + --j; + }else{ + res += vy[i] + sx; + sy -= vy[i]; + --i; + } + } + + printf("%d\n", res); + + } + return 0; +} + diff --git a/spoj_cpp_clean/CHPUZZLE/CHPUZZLE-5.cpp b/spoj_cpp_clean/CHPUZZLE/CHPUZZLE-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbeb8c248d572dd439abf6153386ee60effed730 --- /dev/null +++ b/spoj_cpp_clean/CHPUZZLE/CHPUZZLE-5.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +vector< vector >mat; + +int main() +{ + int h,w,k,i,j,cell,pixX,pixY; + cin>>h>>w>>k; + mat.resize(h, vector(w)); + for(i=1;i<=k;i++) + { + cin>>cell; + for(j=1;j<=cell;j++) + { + cin>>pixX>>pixY; + if(i==1){if(mat[pixX-1][pixY-1]==0) + mat[pixX-1][pixY-1]=i;} + } + } + for(pixX=0;pixX Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +int N, CAP; +int A[10007]; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +inline void init() +{ + int i; + + N = FAST_IO(); + CAP = FAST_IO(); + for (i = 0; i < N; i++) + A[i] = FAST_IO(); +} + +inline int solve() +{ + deque Q; + int i = 0; + long long sum = 0; + int MIN = INT_MAX; + + if (CAP == 0) + return 0; + + while (i < N) + { + while (sum < CAP && i < N) + { + Q.push_back(A[i++]); + sum += Q.back(); + } + + while (sum >= CAP && !Q.empty()) + { + MIN = min(MIN, (int)Q.size()); + sum -= Q.front(); + Q.pop_front(); + } + } + + while (sum >= CAP && !Q.empty()) + { + MIN = min(MIN, (int)Q.size()); + sum -= Q.front(); + Q.pop_front(); + } + + if (MIN == INT_MAX) + return -1; + + return MIN; +} + +int main() +{ + int T, i; + + //freopen("input.txt", "r", stdin); + + T = FAST_IO(); + + for (i = 1; i <= T; i++) + { + init(); + printf("Case %d: %d\n", i, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/CISTFILL/CISTFILL-14.cpp b/spoj_cpp_clean/CISTFILL/CISTFILL-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eba27a384bf360a66fff342a9ca77eb237dffc2c --- /dev/null +++ b/spoj_cpp_clean/CISTFILL/CISTFILL-14.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +#define ll long long +#define eps 1e-6 + +struct node { + ll b, h, w, d; +} ara[50009]; + +int main() + +{ + ll t; + cin >> t; + while(t--) { + ll n; + scanf("%lld", &n); + + ll sm = 0, mn = 1e18, mx = -1e18; + for(ll i = 1; i <= n; i++) { + scanf("%lld %lld %lld %lld", &ara[i].b, &ara[i].h, &ara[i].w, &ara[i].d); + sm += (ara[i].h * ara[i].w * ara[i].d); + mx = max(mx, ara[i].h + ara[i].b); + mn = min(mn, ara[i].b); + } + + ll v; + scanf("%lld", &v); + + if(v > sm) { + printf("OVERFLOW\n"); + continue; + } + + ll cnt = 100; + double lo = mn, hi = mx + 1, ans = -1; + while(cnt--) { + double mid = (lo + hi) / 2; + + double sum = 0; + for(ll i = 1; i <= n; i++) { + + if( (double)ara[i].b - mid > eps) + continue; + + double h = min(mid, (double)(ara[i].b + ara[i].h) ); + h = h - ara[i].b; + sum += (h * ara[i].w * ara[i].d); + } + + if((double)v - sum > eps) + lo = mid + 1; + else { + ans = mid; + hi = mid - 1; + } + } + + printf("%0.2f\n", ans); + } + + return 0; +} diff --git a/spoj_cpp_clean/CLEANRBT/CLEANRBT-13.cpp b/spoj_cpp_clean/CLEANRBT/CLEANRBT-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..966623c36eb44e498174b0636cd9debe0baf7ebf --- /dev/null +++ b/spoj_cpp_clean/CLEANRBT/CLEANRBT-13.cpp @@ -0,0 +1,105 @@ +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" + +using namespace std; + +char table[30][30]; +int mincost[30][30][1 << 11]; +const int INF = 0x3f3f3f3f; + +int movi[4] = {-1, 0, 0, 1}; +int movj[4] = {0, -1, 1, 0}; + +struct state_t{ + int i, j, cost, mask; + state_t(int _i, int _j, int _cost, int _mask): + i(_i), j(_j), cost(_cost), mask(_mask){} +}; + +void addState(queue < state_t > &q, vector < pair >&v, int ncost, int mask, int ni, int nj){ + int nmask; + if(table[ni][nj] == '.' || table[ni][nj] == 'o'){ + nmask = mask; + }else{ + int k = 0; + for(int n=(int)v.size(); k >&v, pair &robot, int w, int h){ + memset(mincost, INF, sizeof(mincost)); + queue < state_t > q; + q.push(state_t(robot.first, robot.second, 0, 0)); + mincost[robot.first][robot.second][0] = 0; + int res = INF; + + while(!q.empty()){ + state_t st = q.front(); q.pop(); + if(st.cost > mincost[st.i][st.j][st.mask])continue; + if(st.mask == (1 << (int)(v.size()))-1)res = min(res, st.cost); + + for(int k=0; k<4; ++k){ + int ni = movi[k] + st.i; + int nj = movj[k] + st.j; + if(ni >= 0 && ni < h && nj >= 0 && nj < w && table[ni][nj] != 'x'){ + addState(q, v, st.cost + 1, st.mask, ni, nj); + } + } + } + + return res; +} + +int main(){ + int w, h; + while(scanf("%d %d", &w, &h) == 2 && (w || h)){ + vector < pair > v; + pair robot; + + for(int i=0; i= INF){ + printf("-1\n"); + }else{ + printf("%d\n", res); + } + } + return 0; +} + diff --git a/spoj_cpp_clean/CLONE/CLONE-7.cpp b/spoj_cpp_clean/CLONE/CLONE-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a80a51f021412ad3d40d76bc2eded892ad5ad42 --- /dev/null +++ b/spoj_cpp_clean/CLONE/CLONE-7.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int n,m,cont[20000]; + string a[20000]; + + while(true){ + cin>>n>>m; + if(n==0) break; + + for(int i = 0;i>a[i]; + sort(a,a+n); + + int pos = 0; + fill(cont,cont+n,0); + + while(pos Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +unordered_map hashh; +int countt[20001]; +int N, L; + +bool init() +{ + int i, j; + char c; + string A; + + scanf("%d %d", &N, &L); + if(N==0 && L==0) + return 0; + + A.resize(L+1); + hashh.clear(); + memset(countt, 0, sizeof(countt)); + + c=fgetc(stdin); + while(!isalpha(c)) + c=fgetc(stdin); + + ungetc(c,stdin); + + for(i=0; i::iterator ii=hashh.begin(); ii!=hashh.end(); ii++) + countt[ii->second]++; + + for(int i=1; i<=N; i++) + printf("%d\n", countt[i]); + } + + return 0; +} diff --git a/spoj_cpp_clean/CMG/CMG-14.cpp b/spoj_cpp_clean/CMG/CMG-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a36ba92fa89ae12575512c2f28a51bcd1ff0cbb --- /dev/null +++ b/spoj_cpp_clean/CMG/CMG-14.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +#define ll int + +int main() + +{ + ll t, caseno = 0; + cin >> t; + while(t--) { + ll q; + scanf("%d", &q); + printf("Case %d:\n", ++caseno); + + stack mango; + multiset big; + + char ch; + ll x; + for(ll i = 1; i <= q; i++) { + scanf(" %c", &ch); + if(ch == 'A') { + scanf("%d", &x); + mango.push(x); + big.insert(-x); + } + else if(ch == 'Q') { + if(big.empty()) + printf("Empty\n"); + else + printf("%d\n", -*(big.begin() )); + } + else { + if(!mango.empty()) { + big.erase( big.find(-mango.top() ) ); + mango.pop(); + } + } + } + } + + return 0; +} diff --git a/spoj_cpp_clean/CMG/CMG-9.cpp b/spoj_cpp_clean/CMG/CMG-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6824e770d4237afedcb36fee63feb9aac7843581 --- /dev/null +++ b/spoj_cpp_clean/CMG/CMG-9.cpp @@ -0,0 +1,92 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar_unlocked() + +using namespace std; + +int A[100007]; +int N; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + + +int main() +{ + int T, t, i, val, back; + char c; + +// freopen("input.txt", "r", stdin); + + T = FAST_IO(); + + for (t = 1; t <= T; t++) + { + back = 0; + + N = FAST_IO(); + + printf("Case %d:\n", t); + + for (i = 0; i < N; i++) + { + c = gc(); + + switch (c) + { + case 'A': + val = FAST_IO(); + if (back == 0) + A[back++] = val; + else + { + A[back] = max(val, A[back - 1]); + back++; + } + break; + case 'Q': + if (back == 0) + printf("Empty\n"); + else + printf("%d\n", A[back - 1]); + gc(); + break; + case 'R': + if (back) + back--; + gc(); + break; + } + } + + } + + return 0; +} diff --git a/spoj_cpp_clean/CMPLS/CMPLS-11.cpp b/spoj_cpp_clean/CMPLS/CMPLS-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3aded18ff7a258049018743f8368435cbebb5c8 --- /dev/null +++ b/spoj_cpp_clean/CMPLS/CMPLS-11.cpp @@ -0,0 +1,77 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include +#include + +using namespace std; + +void findSequence(int input[], int s, int c) +{ + if(s == 1) + { + for(int i = s; i < s+c; i++) + input[i] = input[0]; + + return; + } + + int diff[110]; + + for(int i = 0; i < s-1; i++) + diff[i] = input[i+1] - input[i]; + + int number = diff[0]; + bool flag = true; + + for(int i = 0; i < s-1; i++) + { + if(diff[i] != number) + { + flag = false; + break; + } + } + + if(flag) + { + for(int i = s-1; i < s+c-1; i++) + diff[i] = diff[0]; + } + else + { + findSequence(diff, s-1, c); + } + + for(int i = s; i < s+c; i++) + input[i] = input[i-1] + diff[i-1]; + + return; +} + +int main(void) +{ + int t, s, c; + int input[110]; + + cin >> t; + + while(t--) + { + cin >> s >> c; + + for(int i = 0; i < s; i++) + cin >> input[i]; + + findSequence(input, s, c); + + for(int i = s; i < s+c; i++) + cout << input[i] << " "; + + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CNOTE/CNOTE-5.cpp b/spoj_cpp_clean/CNOTE/CNOTE-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51eac6ad64e4b0afa6c9751971b024980b09952d --- /dev/null +++ b/spoj_cpp_clean/CNOTE/CNOTE-5.cpp @@ -0,0 +1,40 @@ +#include +#include + +using namespace std; + +int main() +{ + int x,y,k,n,test,*pages,*cost,i,j; + scanf("%d",&test); + while(test--) + { + scanf("%d %d %d %d",&x,&y,&k,&n); + pages=NULL; + cost=NULL; + pages=new int[n]; + cost=new int[n]; + for(i=0;i=y) + { + printf("LuckyChef\n"); + continue; + } + else + { + int cmp; + cmp=x-y; + for(i=0;i=cmp && k>=cost[i]) + { + printf("LuckyChef\n"); + continue; + } + } + printf("UnluckyChef\n"); + } + } + return 0; +} diff --git a/spoj_cpp_clean/CNTPRIME/CNTPRIME-14.cpp b/spoj_cpp_clean/CNTPRIME/CNTPRIME-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a89457c8962d9af06b759e37979df22e10d6758 --- /dev/null +++ b/spoj_cpp_clean/CNTPRIME/CNTPRIME-14.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + +#define ll long long + +ll tree[40009], ara[10009], lazy[40009]; +bool mark[1000009]; + +void prop(ll lo, ll hi, ll node) + +{ + if(lo == hi) return; + + ll mid = (lo + hi) / 2; + lazy[2 * node] = lazy[2 * node + 1] = lazy[node]; + tree[2 * node] = (mid - lo + 1) * lazy[node]; + tree[2 * node + 1] = (hi - mid) * lazy[node]; + + lazy[node] = -1; +} + +void build(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + tree[node] = !mark[ ara[lo] ]; + return; + } + + ll mid = (lo + hi) / 2; + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + + tree[node] = tree[2 * node] + tree[2 * node + 1]; +} + +void update(ll lo, ll hi, ll left, ll right, ll val, ll node) + +{ + if(lazy[node] != -1) prop(lo, hi, node); + + if(lo > right || hi < left) + return; + if(lo >= left && hi <= right) { + tree[node] = (hi - lo + 1) * val; + lazy[node] = val; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, left, right, val, 2 * node); + update(mid + 1, hi, left, right, val, 2 * node + 1); + + tree[node] = tree[2 * node] + tree[2 * node + 1]; +} + +ll query(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lazy[node] != -1) prop(lo, hi, node); + + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) + return tree[node]; + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, 2 * node); + ll p2 = query(mid + 1, hi, left, right, 2 * node + 1); + + return p1 + p2; +} + +int main() + +{ + mark[1] = 1; + for(ll i = 2; i <= 1000000; i++) { + if(mark[i] == 0) { + for(ll j = i * i; j <= 1000000; j += i) + mark[j] = 1; + } + } + + ll t, cs = 0; + cin >> t; + while(t--) { + memset(lazy, -1, sizeof(lazy)); + memset(tree, 0, sizeof(tree)); + + ll n, q; + scanf("%lld %lld", &n, &q); + + for(ll i = 1; i <= n; i++) + scanf("%lld", &ara[i]); + + build(1, n, 1); + + printf("Case %lld:\n", ++cs); + for(ll i = 1; i <= q; i++) { + ll type; + scanf("%lld", &type); + if(type == 0) { + ll u, v, x; + scanf("%lld %lld %lld", &u, &v, &x); + update(1, n, u, v, !mark[x], 1); + } + else { + ll u, v; + scanf("%lld %lld", &u, &v); + printf("%lld\n", query(1, n, u, v, 1)); + } + } + } + + return 0; diff --git a/spoj_cpp_clean/CNT_LUCK/CNT_LUCK-0.cpp b/spoj_cpp_clean/CNT_LUCK/CNT_LUCK-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..757d3f26d14a653c723acb2fa7f4ac329d921d64 --- /dev/null +++ b/spoj_cpp_clean/CNT_LUCK/CNT_LUCK-0.cpp @@ -0,0 +1,63 @@ +#include + +#define BOOP (sizeof(long) * 8) + +/* [last_bit][k] */ +long dp[2][BOOP+1]; + +static long go(const int& k, const bool& eq, const std::string& bound, const int& sum, const int& last_bit) { + + // std::printf("k = %d. sum = %d\n", k, sum); + + if (k >= bound.length()) { + return sum == 4 || sum == 7 || sum == 44 || sum == 47; + } + + // if (dp[eq][k] != -1) + // return dp[eq][k]; + + int top = eq ? bound.at(k) - '0' : 1; + // std::printf("top = %d\n", top); + + long cnt = 0; + for (int i = 0; i <= top; ++i) { + + cnt += go(k+1, eq && i == top, bound, sum + i, i); + } + + if (!eq) return dp[eq][k] = cnt; + else return cnt; +} + +static long step(const std::string& n) { + for (int i = 0; i <= 1; ++i) + for (int j = 0; j <= BOOP; ++j) + dp[i][j] = -1; + return go(0,true,n,0,0); +} + +static void one_case(const std::string& l, const std::string& r) { + std::printf("%ld\n", step(r) - step(l)); +} + +int main(void) { + + + + int t; + std::scanf("%d", &t); + + long l,r; + while(t-->0) { + std::scanf("%ld %ld", &l, &r); + + if (!l && !r) { + std::printf("0\n"); + continue; + } + + one_case(std::bitset(std::max(0L, l-1)).to_string(), std::bitset(r).to_string()); + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CNT_LUCK/CNT_LUCK-3.cpp b/spoj_cpp_clean/CNT_LUCK/CNT_LUCK-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2eaf4e25bb85e79ff08d2d115a150a6921464996 --- /dev/null +++ b/spoj_cpp_clean/CNT_LUCK/CNT_LUCK-3.cpp @@ -0,0 +1,114 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef unsigned long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< lucky = {4,7,44,47}; + +vector itob(ll x){ + vector ret; + while(x){ + ret.pb(x&1); + x >>= 1; + } + reverse(ret.begin(), ret.end()); + return ret; +} + +ll get(vector v, bool flag){ + int n = v.size(); + + int lim = 0; + for(int i = 0; i < 4 and lucky[i] <= n; i++){ + lim = lucky[i]; + } + + for(int i = 0; i <= n; i++){ + for(int j = 0; j <= lim; j++){ + dp[i][j][0] = dp[i][j][1] = 0; + } + } + + dp[0][0][1] = 1; + for(int i = 0; i < n; i++){ + for(int s = 0; s <= lim; s++){ + for(int eq = 0; eq < 2; eq++){ + ll val = dp[i][s][eq]; + if(!val) continue; + + int mnd = (eq)? v[i] : 1; + for(int nd = 0; nd <= mnd; nd++){ + int ns = s + nd; + if(ns > lim) break; + int neq = (eq and nd == v[i]); + + dp[i+1][ns][neq] += val; + } + } + } + } + + ll res = 0; + + for(int num : lucky){ + if(num > n) break; + res += dp[n][num][0]; + if(flag) res += dp[n][num][1]; + } + + return res; +} + +void solve(){ + ll a, b; + cin >> a >> b; + vector A = itob(a); + vector B = itob(b); + + cout << get(B,1) - get(A,0) << '\n'; +} + +int main(){ _ + int t; + cin >> t; + while(t--) solve(); + return 0; +} diff --git a/spoj_cpp_clean/CODEM4/CODEM4-9.cpp b/spoj_cpp_clean/CODEM4/CODEM4-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7762cba5592825569d88b6ab713a3e922e0e9e34 --- /dev/null +++ b/spoj_cpp_clean/CODEM4/CODEM4-9.cpp @@ -0,0 +1,63 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int A[31]; +int dp[31][31], dp2[31][31]; +int N; +int sum; + +int main() +{ + int i, j, L, T; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + { + sum=0; + scanf("%d", &N); + + scanf("%d %d", &A[0], &A[1]); + sum+=A[0]; + sum+=A[1]; + dp[0][0]=dp2[0][0]=A[0]; + dp[1][1]=dp2[1][1]=A[1]; + dp[0][1]=max(A[0], A[1]); + dp2[0][1]=min(A[0], A[1]); + + for(i=2; i + +using namespace std; + +int max(int x,int y) +{ + return (x>y)?x:y; +} + +int min(int x,int y) +{ + return (x v; + vector::iterator i; + + scanf("%d",&n); + for (int i = 0; i < n; ++i) + { + scanf("%d %d",&m,&pers); + v.clear(); + for (int i = 0; i < m; ++i) + { + scanf("%d",&aux); + v.push_back(aux); + } + tam=0;suma=0;maxi=0;ini=0;min_sum=2000000000; + for (int i = 0; i < m; ++i) + { + suma+= v[i]; + tam++; + while(suma>pers) + { + suma-= v[ini]; + tam--; + ini++; + } + if (tam>=maxi) + { + if (tam==maxi) + { + min_sum= min(suma,min_sum); + }else + { + min_sum= suma; + } + } + maxi= max(maxi,tam); + } + printf("%d %d\n",min_sum,maxi ); + } + return 0; +} diff --git a/spoj_cpp_clean/COINS/COINS-11.cpp b/spoj_cpp_clean/COINS/COINS-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c120555707ca8fd02d72388188c1eefb81f6781 --- /dev/null +++ b/spoj_cpp_clean/COINS/COINS-11.cpp @@ -0,0 +1,51 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include +#include +#include + +using namespace std; + +map memory; +map::iterator it; +unsigned long long tempStorage; + +unsigned long long findMax(unsigned long long coinValue) +{ + if(coinValue == 0ull) + return 0ull; + + if(coinValue == 1ull) + return 1ull; + + it = memory.find(coinValue); + + if(it != memory.end()) + return it->second; + + tempStorage = max(coinValue, (findMax(coinValue/2ull) + findMax(coinValue/3ull) + findMax(coinValue/4ull))); + + memory.insert(pair(coinValue, tempStorage)); + + return tempStorage; +} + +int main(void) +{ + int t = 10; + unsigned long long coinValue; + + while(t--) + { + cin >> coinValue; + + cout << findMax(coinValue) << endl; + } + + memory.clear(); + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/COINS/COINS-13.cpp b/spoj_cpp_clean/COINS/COINS-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..579f3b1c8ba296ada6733470f3524e4e012d0189 --- /dev/null +++ b/spoj_cpp_clean/COINS/COINS-13.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; +map memo; + +i64 f(i64 n){ + if(n <= 2LL)return n; + + pair < map ::iterator , bool > best = memo.insert(make_pair(n,n)); + if(!best.second)return best.first->second; + + i64 next = f(n/2) + f(n/3) + f(n/4); + if(next > best.first->second)best.first->second = next; + + return best.first->second; +} + +int main(){ + i64 n; + while(scanf("%lld", &n) == 1){ + memo.clear(); + i64 res = f(n); + printf("%lld\n", res); + } +} diff --git a/spoj_cpp_clean/COINS/COINS-16.cpp b/spoj_cpp_clean/COINS/COINS-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29b5f6043426455ccc24f63c950d42d42e43a0a3 --- /dev/null +++ b/spoj_cpp_clean/COINS/COINS-16.cpp @@ -0,0 +1,41 @@ +#include + +#define ll long long +#define ull unsigned long long +#define pb push_back +#define mp make_pair +#define tr(c,it) for(auto it = (c).begin(); it != (c).end(); it++) + +using namespace std; + +map dp; + +ll f(ll n){ + if(n==0||n==1){ + return n; + } + else if(dp[n]!=0){ + return dp[n]; + } + else{ + dp[n]=max(n,f(n/2)+f(n/3)+f(n/4)); + return dp[n]; + } +} + +void solve(ll n){ + ll ans = f(n); + cout<>t; + ll n; + while(cin>>n){ + solve(n); + } + return 0; +} diff --git a/spoj_cpp_clean/COINS/COINS-19.cpp b/spoj_cpp_clean/COINS/COINS-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d8e8679a3cf3378697c814292f3327884e2f0ab --- /dev/null +++ b/spoj_cpp_clean/COINS/COINS-19.cpp @@ -0,0 +1,35 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 + +using namespace std; + +map dp; + +ll solve(ll n) +{ + if (n == 0) {return 0;} + + if (dp[n] != 0) {return dp[n];} + + ll aux = solve(n / 2) + solve(n / 3) + solve(n / 4); + + if (aux > n) {dp[n] = aux;} + else {dp[n] = n;} + + return dp[n]; +} + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + ll n; + while (cin >> n) { + cout << solve(n) << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/COINS/COINS-4.cpp b/spoj_cpp_clean/COINS/COINS-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdfe1fc433ce3a223cce01da834e327ce558774b --- /dev/null +++ b/spoj_cpp_clean/COINS/COINS-4.cpp @@ -0,0 +1,118 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +//Other Includes +#include +#include +#include +#include +#include +#include +#include +#include +//some common functionn +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define all(c) (c).begin(),(c).end() +// Input macros +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) + +// Output macros +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +using namespace std; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;imp; +long long call(int n) +{ + if(n==0) + { + return 0; + } + if(mp[n]!=0) + { + return mp[n]; + } + int a=n/2, b=n/3, c=n/4; + return mp[n]=maX(n, (call(a)+call(b)+call(c))); +} +int solve() +{ + mp[0]=0; + mp[1]=1; + mp[2]=2;mp[3]=3;mp[4]=4;mp[5]=5;mp[6]=6;mp[7]=7;mp[8]=8;mp[9]=9;mp[10]=10; + //mp[]=; + cout<>n) + { + solve(); + } + +// system("pause"); + return 0; +} + + + + diff --git a/spoj_cpp_clean/COINS/COINS-5.cpp b/spoj_cpp_clean/COINS/COINS-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c8d7a0b21a561bb09981d46d4adddcc12eb2ceb --- /dev/null +++ b/spoj_cpp_clean/COINS/COINS-5.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +using namespace std; + +map dp; + +long long int coins(long long int n) +{ + if(n==0) + return 0; + if(dp[n]!=0) + return dp[n]; + long long auxiValue=max(n,coins(n/2)+coins(n/3)+coins(n/4)); + dp[n]=auxiValue; + return dp[n]; +} +int main() +{ + long long int a; + while((cin>>a)) + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define max(a, b) (a>b)?a:b; + +using namespace std; + +map DP; +vector A; +int N; + +unsigned long long solve(int curr) +{ + if(DP.find(curr)!=DP.end()) + return DP[curr]; + + DP[curr]=max(curr, solve(curr/2)+solve(curr/3)+solve(curr/4)); + + return DP[curr]; +} + +int main() +{ + int i; + int num; + + //freopen("input.txt","r",stdin); + + + while(scanf("%d", &num)!=EOF) + { + A.push_back(num); + N=max(N, num); + } + + DP[0]=0; + DP[1]=1; + DP[2]=2; + DP[3]=3; + + for(i=0; i +using namespace std; + +int main() +{ + int n, p = 7; + cin >> n; + if(n > 100) + { + p += (n - 100) * 5; + n -= (n - 100); + } + if(n > 30) + { + p += (n - 30) * 2; + n -= (n - 30); + } + if(n > 10) + p += (n - 10); + cout << p; + return 0; +} diff --git a/spoj_cpp_clean/COT/COT-3.cpp b/spoj_cpp_clean/COT/COT-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5cbfe45bf6273f4b6647fe706cec4ae340b7e1f --- /dev/null +++ b/spoj_cpp_clean/COT/COT-3.cpp @@ -0,0 +1,178 @@ +/* +By Anudeep Nekkanti +Editorial at www.blog.anudeep.com/persistent-segment-trees-explained-with-spoj-problems +Question at http://www.spoj.com/problems/COT/ +O( (N+M) * log N ) +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define sz size() +#define pb push_back +#define rep(i,n) for(int i=0;i=b; i--) + +#define N 111111 +#define LN 19 + +int v[N], pa[N][LN], RM[N], depth[N], maxi = 0; +vector adj[N]; +map M; + +struct node +{ + int count; + node *left, *right; + + node(int count, node *left, node *right): + count(count), left(left), right(right) {} + + node* insert(int l, int r, int w); +}; + +node *null = new node(0, NULL, NULL); //see line 135 + +node * node::insert(int l, int r, int w) +{ + if(l <= w && w < r) + { + // If the node we are adding belongs to some range already covered by the segment tree of its parent, we need to create a new node corresponding to it for the segment tree rooted at the current node. This is one of the log(n) nodes that will be created. + if(l+1 == r) + { + //cout<<"index "< :: iterator it = M.begin(); it != M.end(); it++ ) + { + M[it->first] = maxi; + RM[maxi] = it->first; + maxi++; + } + + // We compressed the given weights into the range [0..n) + + rep(i, n-1) + { + int u, v; + scanf("%d%d", &u, &v); + u--; v--; + adj[u].pb(v); + adj[v].pb(u); + } + + // Root the tree at some node. + memset(pa, -1, sizeof pa); + null->left = null->right = null; + dfs(0, -1); + + // Build jump table for LCA in O( N log N ). pa[i][j] denotes the 2^ j th ancestor of i + rep(i, LN-1) + rep(j, n) + if(pa[j][i] != -1) + pa[j][i+1] = pa[pa[j][i]][i]; + + while(m--) + { + int u, v, k; + scanf("%d%d%d", &u, &v, &k); + u--; v--; + + int lca = LCA(u, v); + // Four nodes we spoke about are u, v, lca, parent(lca) + int ans = query(root[u], root[v], root[lca], (pa[lca][0] == -1 ? null : root[ pa[lca][0] ]), 0, maxi, k); + + // Reverse Map the values, that is, uncompress + printf("%d\n", RM[ans]); + } +} diff --git a/spoj_cpp_clean/COT2/COT2-14.cpp b/spoj_cpp_clean/COT2/COT2-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa6094b2a717161baadc4673d106117868e546aa --- /dev/null +++ b/spoj_cpp_clean/COT2/COT2-14.cpp @@ -0,0 +1,178 @@ +// Mo's on tree + +//find distinct "weights in nodes" of the path (from node u to node v) + +#include +using namespace std; + +const int MAXN = 40005; +const int MAXM = 100005; +const int LN = 19; + +int N, M, K, cur, A[MAXN], LVL[MAXN], DP[LN][MAXN]; +int BL[MAXN << 1], ID[MAXN << 1], VAL[MAXN], ANS[MAXM]; +int d[MAXN], l[MAXN], r[MAXN]; +bool VIS[MAXN]; +vector < int > graph[MAXN]; + +struct query +{ + int id, l, r, lc; + bool operator < (const query& rhs) + { + if(BL[l] != BL[rhs.l]) + return BL[l] < BL[rhs.l]; + + if(BL[l] & 1) + return r < rhs.r; + + return r > rhs.r; + } +} Q[MAXM]; + +// Set up Stuff +void dfs(int u, int par) +{ + l[u] = ++cur; + ID[cur] = u; + for (int i = 1; i < LN; i++) + DP[i][u] = DP[i - 1][DP[i - 1][u]]; + for (int i = 0; i < graph[u].size(); i++) + { + int v = graph[u][i]; + if (v == par) + continue; + LVL[v] = LVL[u] + 1; + DP[0][v] = u; + dfs(v, u); + } + r[u] = ++cur; + ID[cur] = u; +} + +// Function returns lca of (u) and (v) +inline int lca(int u, int v) +{ + if (LVL[u] > LVL[v]) + swap(u, v); + for (int i = LN - 1; i >= 0; i--) + if (LVL[v] - (1 << i) >= LVL[u]) + v = DP[i][v]; + if (u == v) + return u; + for (int i = LN - 1; i >= 0; i--) + { + if (DP[i][u] != DP[i][v]) + { + u = DP[i][u]; + v = DP[i][v]; + } + } + return DP[0][u]; +} + +inline void check(int x, int& res) +{ + // If (x) occurs twice, then don't consider it's value + if(VIS[x]) { + if(--VAL[A[x]] == 0) + res--; + } + else if(!VIS[x]) { + if(VAL[A[x]]++ == 0) + res++; + } + VIS[x] ^= 1; +} + +void compute() +{ + + // Perform standard Mo's Algorithm + int curL = Q[0].l, curR = Q[0].l - 1, res = 0; + + for (int i = 0; i < M; i++) + { + + while (curL < Q[i].l) + check(ID[curL++], res); + while (curL > Q[i].l) + check(ID[--curL], res); + while (curR < Q[i].r) + check(ID[++curR], res); + while (curR > Q[i].r) + check(ID[curR--], res); + + int u = ID[curL], v = ID[curR]; + + // Case 2 + if (Q[i].lc != u and Q[i].lc != v) + check(Q[i].lc, res); + + ANS[Q[i].id] = res; + + if (Q[i].lc != u and Q[i].lc != v) + check(Q[i].lc, res); + } + + for (int i = 0; i < M; i++) + printf("%d\n", ANS[i]); +} + +int main() +{ + + int u, v, x; + + scanf("%d %d", &N, &M); + + cur = 0; + memset(VIS, 0, sizeof(VIS)); + memset(VAL, 0, sizeof(VAL)); + for (int i = 1; i <= N; i++) + graph[i].clear(); + + // Inputting Values + for (int i = 1; i <= N; i++) + scanf("%d", &A[i]); + memcpy(d + 1, A + 1, sizeof(int) * N); + + // Compressing Coordinates + sort(d + 1, d + N + 1); + K = unique(d + 1, d + N + 1) - d - 1; + for (int i = 1; i <= N; i++) + A[i] = lower_bound(d + 1, d + K + 1, A[i]) - d; + + // Inputting Tree + for (int i = 1; i < N; i++) + { + scanf("%d %d", &u, &v); + graph[u].push_back(v); + graph[v].push_back(u); + } + + // Preprocess + DP[0][1] = 1; + dfs(1, -1); + int size = sqrt(cur); + + for (int i = 1; i <= cur; i++) + BL[i] = (i - 1) / size + 1; + + for (int i = 0; i < M; i++) + { + scanf("%d %d", &u, &v); + Q[i].lc = lca(u, v); + if (l[u] > l[v]) + swap(u, v); + if (Q[i].lc == u) + Q[i].l = l[u], Q[i].r = l[v]; + else + Q[i].l = r[u], Q[i].r = l[v]; + Q[i].id = i; + } + + sort(Q, Q + M); + compute(); +} + diff --git a/spoj_cpp_clean/COUNT1IT/COUNT1IT-0.cpp b/spoj_cpp_clean/COUNT1IT/COUNT1IT-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..828ead0f81730757429d954a1a91a3062b4830d4 --- /dev/null +++ b/spoj_cpp_clean/COUNT1IT/COUNT1IT-0.cpp @@ -0,0 +1,58 @@ +#include +#include + +static int smaller_or_eq(const std::multiset& s , const int y) { + return std::distance(s.begin(), s.upper_bound(y)); +} + +// static void pset(const std::multiset& myset) { +// for (auto& e : myset) { +// std::cout << e << " "; +// } +// std::cout << std::endl; +// } + +int main() { + + int n,q; + std::cin >> n >> q; + + std::multiset myset; + int tmp; + while(n-->0) { + std::cin >> tmp; + myset.insert(tmp); + } + + int _q; + while(q-->0) { + std::cin >> _q >> tmp; + + // pset(myset); + + switch (_q) { + case 1: + myset.insert( smaller_or_eq(myset, tmp) + tmp ); + // std::cout << _q << " " << tmp << " ->"; + // pset(myset); + break; + case 2: + std::cout /*<< _q << " " << tmp << " ->"*/ << smaller_or_eq(myset, tmp) << std::endl; + break; + case 3: + if (size_t(tmp) >= myset.size()) { + std::cout << "invalid\n"; + } else { + auto itr = myset.begin(); + std::advance(itr, tmp-1); + std::cout /*<< _q << " " << tmp << " ->" */<< *(itr) << std::endl; + } + } + } + + + + + return 0; + +} \ No newline at end of file diff --git a/spoj_cpp_clean/COUNT1IT/COUNT1IT-9.cpp b/spoj_cpp_clean/COUNT1IT/COUNT1IT-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fd76b258275639dfa764bc3adc10e67ccffc65d --- /dev/null +++ b/spoj_cpp_clean/COUNT1IT/COUNT1IT-9.cpp @@ -0,0 +1,182 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define gc() getchar_unlocked() + +#define MAXN 100007 +using namespace std; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +struct node +{ + int num; + int sum; + struct node *left; + struct node *right; + + node(int n) + { + num = n; + sum = 1; + left = right = nullptr; + } +}; + +int C; +int N, M; +struct node *root; +int MAXNUM; + +void add(struct node **curr, int num) +{ + if (*curr == nullptr) + { + *curr = new struct node(num); + C++; + return; + } + + if (num < (*curr)->num) + { + add(&(*curr)->left, num); + (*curr)->sum++; + } + + else if (num >(*curr)->num) + add(&(*curr)->right, num); + + else + { + (*curr)->sum++; + C++; + } +} + +int count(struct node *curr, int num) +{ + if (curr == nullptr) + return 0; + + if (curr->num <= num) + return curr->sum + count(curr->right, num); + + return count(curr->left, num); +} + +inline void init() +{ + int num, i; + + N = FAST_IO(); + M = FAST_IO(); + + num = FAST_IO(); + MAXNUM = num; + + root = new struct node(num); + + for (i = 1; i < N; i++) + { + num = FAST_IO(); + MAXNUM = max(MAXNUM, num); + add(&root, num); + } + C = N; +} + +inline void one(int num) +{ + int a = count(root, num); + MAXNUM = max(MAXNUM, num + a); + add(&root, num + a); +} + +inline void two(int num) +{ + printf("%d\n", count(root, num)); +} + +inline void three(int num) +{ + if (num > C) + { + printf("invalid\n"); + return; + } + + int lo, hi, mid, t; + + lo = 0; + hi = MAXNUM; + mid = (lo + hi) / 2; + + while (lo != hi - 1) + { + t = count(root, mid); + + if (t < num) + lo = mid; + else + hi = mid; + + mid = (lo + hi) / 2; + } + + printf("%d\n", hi); +} + +int main() +{ +// freopen("input.txt", "r", stdin); + + int t, x; + + init(); + + while (M--) + { + t = FAST_IO(); + x = FAST_IO(); + + switch (t) + { + case 1: + one(x); + break; + case 2: + two(x); + break; + case 3: + three(x); + break; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/COURIER/COURIER-3.cpp b/spoj_cpp_clean/COURIER/COURIER-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81db2197b91ee0a90aa19d0f28c83db6a19bd338 --- /dev/null +++ b/spoj_cpp_clean/COURIER/COURIER-3.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int t, n, m, b, graph[101][101],u, v, num, coun, d, z, MASK, dp[1<<12][101], orders[12][2], INF = 10001; + +int tsp(int prev, int mask){ + if(dp[mask][prev] < INF) return dp[mask][prev]; + + int dist = INF, flag = 0; + for(int i = 0; i < coun; i++){ + if(!(mask & (1< graph[i][k] + graph[k][j]){ + graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]); + //} + } + } + } + + sd(z); + coun = 0; + + for(int i = 0; i < z; i++){ + sd3(u,v,num); + while(num--){ + orders[coun][0] = u; + orders[coun][1] = v; + coun++; + } + } + /*for(int i = 0; i < 1< digit + difficulty: easy + date: 01/May/2020 + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +vector digs; +ll memo[10][9*10+1][2]; +ll dp(int i, int sum, bool limited) { + // dígito atual digs[i], soma acumulada sum + // se limited, dígito atual pode variar de 0 à digs[i], se não, de 0 à 9 + + if (i == digs.size()) return sum; + + ll &ans = memo[i][sum][limited]; + if (ans != -1) return ans; + + ans = 0; + for (int d = 0; d <= (limited? digs[i] : 9); d++) + ans += dp(i+1, sum+d, d == digs[i]? limited: 0 ); + + return ans; +} + +// retorna a soma dos dígitos dos números em [1 .. n] +ll solve(int n) { + // se n = 250, digs = {2, 5, 0} + digs.clear(); + while (n > 0) digs.push_back(n % 10), n /= 10; + reverse(digs.begin(), digs.end()); + + memset(memo, -1, sizeof memo); + return dp(0, 0, 1); +} + +int main() { + int a, b; + while (cin >> a >> b && !(a == -1 && b == -1)) + cout << solve(b) - solve(a-1) << endl; + + return 0; +} diff --git a/spoj_cpp_clean/CPCRC1C/CPCRC1C-3.cpp b/spoj_cpp_clean/CPCRC1C/CPCRC1C-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9810feda05baabb0a759b44d9ce015df051aa7da --- /dev/null +++ b/spoj_cpp_clean/CPCRC1C/CPCRC1C-3.cpp @@ -0,0 +1,103 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< itov(ll x){ + if(!x) return {0}; + vector v; + + while(x){ + v.pb(x%10); + x /= 10; + } + + reverse(v.begin(), v.end()); + + return v; +} + +const int N = 20; + +ll sum[N][2]; +ll cnt[N][2]; + +ll get(vector v, bool flag){ + int n = v.size(); + + memset(sum, 0, sizeof sum); + memset(cnt, 0, sizeof cnt); + + cnt[0][1] = 1; + + for(int i = 0; i < n; i++){ + for(int eq = 0; eq < 2; eq++){ + ll c = cnt[i][eq]; + ll s = sum[i][eq]; + if(!c) continue; + + int mnd = (eq)? v[i] : 9; + + for(int nd = 0; nd <= mnd; nd++){ + int neq = (eq and nd == v[i]); + sum[i+1][neq] += s + c * nd; + cnt[i+1][neq] += c; + } + } + } + + ll ret = sum[n][0] + ((flag)? sum[n][1] : 0); + + return ret; +} + +void solve(){ + ll a, b; + cin >> a >> b; + + if(a == -1) exit(0); + + vector A = itov(a); + vector B = itov(b); + + cout << get(B,1) - get(A,0) << endl; +} + +int main(){ _ + while(true) solve(); + return 0; +} diff --git a/spoj_cpp_clean/CPCRC1C/CPCRC1C-8.cpp b/spoj_cpp_clean/CPCRC1C/CPCRC1C-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83a5d2a86ded6d3468b960e35b31be2bf08c3a55 --- /dev/null +++ b/spoj_cpp_clean/CPCRC1C/CPCRC1C-8.cpp @@ -0,0 +1,154 @@ +/*************************************************** + * Problem Name : CPCRC1C - Sum of Digits.cpp + * Problem Link : https://www.spoj.com/problems/CPCRC1C/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : DigitDP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int n; +ll dp[20][100][2][2]; +vectorA, B; +ll digitDP (int pos, int cnt, int isChoto, int isBoro) { + if (pos == n) { + return cnt; + } + + if (dp[pos][cnt][isChoto][isBoro] != -1) { + return dp[pos][cnt][isChoto][isBoro]; + } + + ll ans = 0; + int high, low; + + if (isChoto == 0) { + high = B[pos]; + + } else { + high = 9; + } + + if (isBoro == 0) { + low = A[pos]; + + } else { + low = 0; + } + + for (int i = low; i <= high; i++) { + int new_cnt = cnt; + int new_isChoto = isChoto; + int new_isBoro = isBoro; + new_cnt += i; + + if (i < high) { + new_isChoto = 1; + } + + if (i > low) { + new_isBoro = 1; + } + + ans += (ll) digitDP (pos + 1, new_cnt, new_isChoto, new_isBoro); + } + + return dp[pos][cnt][isChoto][isBoro] = ans; +} +ll solve (ll a, ll b) { + A.clear(), B.clear(); + + while (b > 0) { + int rem = b % 10; + B.pb (rem); + b /= 10; + } + + reverse (all (B) ); + + while (a > 0) { + int rem = a % 10; + A.pb (rem); + a /= 10; + } + + n = (int) B.size(); + + while ( (int) A.size() < n) { + A.pb (0); + } + + reverse (all (A) ); + SET (dp, -1); + ll ans = digitDP (0, 0, 0, 0); + return ans; +} + +int main () { + __FastIO; + ll a, b; + + while (cin >> a >> b) { + if (a == -1 && b == -1) break; + + ll ans = solve (a, b); + cout << ans << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/CPRMT/CPRMT-4.cpp b/spoj_cpp_clean/CPRMT/CPRMT-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9fc9aef0d003eb795708a9da9bcf283e45b3671 --- /dev/null +++ b/spoj_cpp_clean/CPRMT/CPRMT-4.cpp @@ -0,0 +1,106 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[30], b[30]; +string s, t, tmp; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + while(cin>>s>>t) + { + tmp.clear(); + memset(a, 0, sizeof(a)); + memset(b, 0, sizeof(b)); + FOR(i,0,s.length()) + a[s[i]-'a']++; + FOR(i,0,t.length()) + b[t[i]-'a']++; + FOR(i,0,26) + { + FOR(j,0,miN(a[i],b[i])) + tmp+=(char) 'a'+i; + } + cout< +using namespace std; +int main() +{ + int t(0), l(0), c(0); + string s(""); + vector v_out {}; + + cin >> t; + while(t >= 1 && t < 100) { + cin >> l >> c; + if (l >= 100 || c >= 100) + break; + for (int i = 0; i < l; ++i) + { + s = ""; + for (int j = 0; j < c; ++j) + { + if + ((i + j) % 2 == 0) s.push_back('*'); + else + s.push_back('.'); + } + v_out.push_back(s); + } + v_out.push_back(""); + cin.clear(); + t--; + } + + for (string output: v_out) + cout << output << '\n'; + + return 0; +} diff --git a/spoj_cpp_clean/CPTTRN2/CPTTRN2-19.cpp b/spoj_cpp_clean/CPTTRN2/CPTTRN2-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..784baa2d96ac6f65b27003ea75ee7af20e7ddf0b --- /dev/null +++ b/spoj_cpp_clean/CPTTRN2/CPTTRN2-19.cpp @@ -0,0 +1,45 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 1000001 +#define PI 3.14159265 + +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + ll t; cin >> t; + while (t--) { + ll r, c; + cin >> r >> c; + if (r == 0 || c == 0) { + cout << endl; + continue; + } + char arr[r][c]; + for (ll i = 0; i < c; i++) { + arr[0][i] = '*'; + arr[r - 1][i] = '*'; + } + for (ll i = 0; i < r; i++) { + arr[i][0] = '*'; + arr[i][c - 1] = '*'; + } + for (ll i = 1; i < r - 1; i++) { + for (ll j = 1; j < c - 1; j++) { + arr[i][j] = '.'; + } + } + for (ll i = 0; i < r; i++) { + for (ll j = 0; j < c; j++) { + cout << arr[i][j]; + } + cout << endl; + } + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CPTTRN2/CPTTRN2-2.cpp b/spoj_cpp_clean/CPTTRN2/CPTTRN2-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..54e690dd993b7399612d50c2f669bccae32eb7c1 --- /dev/null +++ b/spoj_cpp_clean/CPTTRN2/CPTTRN2-2.cpp @@ -0,0 +1,39 @@ +// https://www.spoj.com/problems/CPTTRN2/ +// CPTTRN2 - Character Patterns (Act 2) +#include +#include + +int main() +{ + int t(0), l(0), c(0); + std::string s(""); + std::vector v_out {}; + + std::cin >> t; + while(t >= 1) { + std::cin >> l >> c; + for (int i = 0; i < l; ++i) + { + s = ""; + for (int j = 0; j < c; ++j) + { + if (i == 0 || i == l - 1) { + s.push_back('*'); + continue; + } + + if (j == 0 || j == c - 1) s.push_back('*'); + else s.push_back('.'); + } + v_out.push_back(s); + } + v_out.push_back(""); + std::cin.clear(); + t--; + } + + for (std::string output: v_out) + std::cout << output << '\n'; + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CPTTRN3/CPTTRN3-2.cpp b/spoj_cpp_clean/CPTTRN3/CPTTRN3-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..653df2bb4edbde641ff4d3883d56e7288bd16c0f --- /dev/null +++ b/spoj_cpp_clean/CPTTRN3/CPTTRN3-2.cpp @@ -0,0 +1,81 @@ +// https://www.spoj.com/problems/CPTTRN3/ +// CPTTRN3 - Character Patterns (Act 3) +#include +#include + +int main() +{ + int t(0), l(0), c(0); + std::string s(""); + std::vector v_out {}; + + std::cin >> t; + while(t > 0) { + std::cin >> l >> c; + for (int i = 0; i <= 3*l; ++i) + { + s = ""; + for (int j = 0; j < 3*c + 1; ++j) + { + if (i % 3 == 0) { + s.push_back('*'); + continue; + } + if (j % 3 == 0) s.push_back('*'); + else s.push_back('.'); + } + v_out.push_back(s); + } + v_out.push_back(""); + std::cin.clear(); + t--; + } + + for (std::string output: v_out) + std::cout << output << '\n'; + + return 0; +} + +/* +Example +Input: +3 +3 1 +4 4 +2 5 + +Output: +**** +*..* +*..* +**** +*..* +*..* +**** +*..* +*..* +**** + +************* +*..*..*..*..* +*..*..*..*..* +************* +*..*..*..*..* +*..*..*..*..* +************* +*..*..*..*..* +*..*..*..*..* +************* +*..*..*..*..* +*..*..*..*..* +************* + +**************** +*..*..*..*..*..* +*..*..*..*..*..* +**************** +*..*..*..*..*..* +*..*..*..*..*..* +**************** +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/CPTTRN4/CPTTRN4-2.cpp b/spoj_cpp_clean/CPTTRN4/CPTTRN4-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..552444d70e30c1e33e3d397091b32441b2a7679b --- /dev/null +++ b/spoj_cpp_clean/CPTTRN4/CPTTRN4-2.cpp @@ -0,0 +1,80 @@ +// https://www.spoj.com/problems/CPTTRN4/ +// CPTTRN4 - Character Patterns (Act 4) +#include +#include + +int main() +{ + int t(0), l(0), c(0), h(0), w(0); + std::vector v_out {}; + + std::cin >> t; + while(t > 0) { + std::cin >> l >> c >> h >> w; + std::string str_asterisk(""), str_mix(""); + str_asterisk = std::string((w+1)*c+1, '*'); + + for (int k = 0; k < c; ++k) + str_mix += "*" + std::string(w, '.'); + str_mix += "*"; + + v_out.push_back(str_asterisk); + for (int i = 0; i < l; ++i) + { + for (int j = 0; j < h; ++j) + { + v_out.push_back(str_mix); + } + v_out.push_back(str_asterisk); + } + v_out.push_back(""); + std::cin.clear(); + t--; + } + + for (std::string output: v_out) + std::cout << output << '\n'; + + return 0; +} + +/* +Example + +Input: +3 +3 1 2 1 +4 4 1 2 +2 5 2 2 + +Output: +*** +*.* +*.* +*** +*.* +*.* +*** +*.* +*.* +*** + +************* +*..*..*..*..* +************* +*..*..*..*..* +************* +*..*..*..*..* +************* +*..*..*..*..* +************* + +**************** +*..*..*..*..*..* +*..*..*..*..*..* +**************** +*..*..*..*..*..* +*..*..*..*..*..* +**************** + +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/CPTTRN5/CPTTRN5-2.cpp b/spoj_cpp_clean/CPTTRN5/CPTTRN5-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3df9eab49067d0bc45504af62461049b37b37c6d --- /dev/null +++ b/spoj_cpp_clean/CPTTRN5/CPTTRN5-2.cpp @@ -0,0 +1,89 @@ +// https://www.spoj.com/problems/CPTTRN5/ +// CPTTRN5 - Character Patterns (Act 5) +#include +#include + +using namespace std; + +int main() +{ + int t(0), l(0), c(0), s(0); + vector v_out {}; + + cin >> t; + while(t > 0) { + cin >> l >> c >> s; + string asterisk_line = string((s+1)*c+1, char(42)); + string base = string(s+1, char(46)); + base[0] = char(42); + + for (int i = 0; i < l; ++i) { + v_out.push_back(asterisk_line); + for (int j = 0; j < s; ++j) { + string line(""); + for (int k = 0; k < c; ++k) { + string str(base); + if (i % 2 == 0) { + if (k % 2 == 0) str[j + 1] = char(92); + else str[s - j] = char(47); + } + else { + if (k % 2 != 0) str[j + 1] = char(92); + else str[s - j] = char(47); + } + line.append(str); + } + line.push_back(char(42)); + v_out.push_back(line); + } + } + v_out.push_back(asterisk_line); + v_out.push_back(""); + std::cin.clear(); + t--; + } + + for (std::string output: v_out) + std::cout << output << '\n'; + + return 0; +} + + +// Example + +// Input: +// 3 +// 3 1 2 +// 4 4 1 +// 2 5 2 + +// Output: +// **** +// *\.* +// *.\* +// **** +// *./* +// .* +// **** +// *\.* +// *.\* +// **** + +// ********* +// *\*/*\*/* +// ********* +// */*\*/*\* +// ********* +// *\*/*\*/* +// ********* +// */*\*/*\* +// ********* + +// **************** +// *\.*./*\.*./*\.* +// *.\*/.*.\*/.*.\* +// **************** +// *./*\.*./*\.*./* +// */.*.\*/.*.\*/.* +// **************** diff --git a/spoj_cpp_clean/CPTTRN6/CPTTRN6-2.cpp b/spoj_cpp_clean/CPTTRN6/CPTTRN6-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..21189234b3941a2e68ff3a79eb2fc0d8af719af9 --- /dev/null +++ b/spoj_cpp_clean/CPTTRN6/CPTTRN6-2.cpp @@ -0,0 +1,90 @@ +// https://www.spoj.com/problems/CPTTRN6/ +// CPTTRN6 - Character Patterns (Act 6) +#include +#include + +using namespace std; + +int main() +{ + int t(0), l(0), c(0), h(0), w(0); + vector v_out {}; + + cin >> t; + while(t > 0) { + cin >> l >> c >> h >> w; + string minus_line = string((c+1)*w+c, char(45)); + string dots_line = string((c+1)*w+c, char(46)); + + char center; + string current_line; + for (int i = 1; i < (h+1)*(l+1); ++i) { + if (i % (h+1) == 0) { + center = 43; + current_line = minus_line; + } else { + center = 124; + current_line = dots_line; + } + for (int j = 1; j < (c+1)*w+c; ++j) { + if (j % (w+1) == 0) current_line[j-1] = char(center); + } + v_out.push_back(current_line); + } + v_out.push_back(""); + std::cin.clear(); + t--; + } + + for (std::string output: v_out) + std::cout << output << '\n'; + + return 0; +} + + +/* Example + +Input: +3 +3 1 2 1 +4 4 1 2 +2 5 3 2 + +Output: +.|. +.|. +-+- +.|. +.|. +-+- +.|. +.|. +-+- +.|. +.|. + +..|..|..|..|.. +--+--+--+--+-- +..|..|..|..|.. +--+--+--+--+-- +..|..|..|..|.. +--+--+--+--+-- +..|..|..|..|.. +--+--+--+--+-- +..|..|..|..|.. + + +..|..|..|..|..|.. +..|..|..|..|..|.. +..|..|..|..|..|.. +--+--+--+--+--+-- +..|..|..|..|..|.. +..|..|..|..|..|.. +..|..|..|..|..|.. +--+--+--+--+--+-- +..|..|..|..|..|.. +..|..|..|..|..|.. +..|..|..|..|..|.. + +*/ diff --git a/spoj_cpp_clean/CPTTRN7/CPTTRN7-2.cpp b/spoj_cpp_clean/CPTTRN7/CPTTRN7-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fc31ca4aca09f3383ac3835db7f8e463e82c700f --- /dev/null +++ b/spoj_cpp_clean/CPTTRN7/CPTTRN7-2.cpp @@ -0,0 +1,83 @@ +// https://www.spoj.com/problems/CPTTRN7/ +// CPTTRN7 - Character Patterns (Act 7) +#include +#include + +using namespace std; + +int main() +{ + int t(0), l(0), c(0), s(0); + + cin >> t; + while(t--) { + cin >> l >> c >> s; + + vector v_out(2 * s); + for (int i = 0; i < s; ++i) { + string str_up(""), str_down(""); + string str_border = string(s - i - 1, char(46)); + string str_center = string(2 * i, char(46)); + for (int j = 0; j < c; ++j) { + str_up += str_border + char(47) + str_center + + char(92) + str_border; + str_down += str_border + char(92) + str_center + + char(47) + str_border; + } + v_out[i] = str_up; + v_out[2 * s - i - 1] = str_down; + } + + while(l--) { + for (string s: v_out) + cout << s << "\n"; + } + + cin.clear(); + } + + return 0; +} + +/* Example + +Input: +3 +3 1 2 +4 4 1 +2 5 2 + +Output: +./\. +/..\ +\../ +.\/. +./\. +/..\ +\../ +.\/. +./\. +/..\ +\../ +.\/. + +/\/\/\/\ +\/\/\/\/ +/\/\/\/\ +\/\/\/\/ +/\/\/\/\ +\/\/\/\/ +/\/\/\/\ +\/\/\/\/ + + +./\../\../\../\../\. +/..\/..\/..\/..\/..\ +\../\../\../\../\../ +.\/..\/..\/..\/..\/. +./\../\../\../\../\. +/..\/..\/..\/..\/..\ +\../\../\../\../\../ +.\/..\/..\/..\/..\/. + +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/CPTTRN8/CPTTRN8-2.cpp b/spoj_cpp_clean/CPTTRN8/CPTTRN8-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..727fbaf9484b90d2eab940555b45968d2022ee14 --- /dev/null +++ b/spoj_cpp_clean/CPTTRN8/CPTTRN8-2.cpp @@ -0,0 +1,80 @@ +// https://www.spoj.com/problems/CPTTRN8/ +// CPTTRN8 - Character Patterns (Act 8) +#include +#include + +using namespace std; + +int main() +{ + int t(0), l(0), c(0), s(0); + + cin >> t; + while(t--) { + cin >> l >> c >> s; + + vector v_out(2 * s); + for (int i = 0; i < s; ++i) { + string str_up(""), str_down(""); + string str_border = string(s - i - 1, char(46)); + string str_center = string(2 * i, char(42)); + for (int j = 0; j < c; ++j) { + str_up += str_border + char(47) + str_center + + char(92) + str_border; + str_down += str_border + char(92) + str_center + + char(47) + str_border; + } + v_out[i] = str_up; + v_out[2 * s - i - 1] = str_down; + } + + while(l--) { + for (string s: v_out) + cout << s << "\n"; + } + + cin.clear(); + } + + return 0; +} + +// Example + +// Input: +// 3 +// 3 1 2 +// 4 4 1 +// 2 5 2 + +// Output: +// ./\. +// /**\ +// \**/ +// .\/. +// ./\. +// /**\ +// \**/ +// .\/. +// ./\. +// /**\ +// \**/ +// .\/. + +// /\/\/\/\ +// \/\/\/\/ +// /\/\/\/\ +// \/\/\/\/ +// /\/\/\/\ +// \/\/\/\/ +// /\/\/\/\ +// \/\/\/\/ + +// ./\../\../\../\../\. +// /**\/**\/**\/**\/**\ +// \**/\**/\**/\**/\**/ +// .\/..\/..\/..\/..\/. +// ./\../\../\../\../\. +// /**\/**\/**\/**\/**\ +// \**/\**/\**/\**/\**/ +// .\/..\/..\/..\/..\/. \ No newline at end of file diff --git a/spoj_cpp_clean/CRAN02/CRAN02-3.cpp b/spoj_cpp_clean/CRAN02/CRAN02-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dae207264e1c9060a22ccf7af24d2eeff4d40700 --- /dev/null +++ b/spoj_cpp_clean/CRAN02/CRAN02-3.cpp @@ -0,0 +1,60 @@ +#include +#include + +using namespace std; + +int main ( int argc ,char * argv[] ) { + + int t; + cin >> t; + + map< long long int , long long int > sum_p; + map< long long int , long long int>::iterator itr; + + while ( t-- ) { + + + long long int count = 0; + long long int n; + + cin >> n; + + long long int array[n]; + + cin >> array[0]; + + sum_p[ array[0] ] ++; + + for ( int i = 1 ; i < n ; i++ ) { + cin >> array[i]; + array[i] += array[i-1]; + sum_p[ array[i] ] ++; + } + + + for ( itr = sum_p.begin() ; itr != sum_p.end() ; itr++ ) { + long long int a,b; + + a = itr->first; + b = itr->second; + + if ( a != 0 ) { + count += b * (b-1) / 2; + } + else { + count += b + (b * (b-1)/2 ); + } + } + + cout << count << endl; + sum_p.clear(); + } + + return 0; + +} + + + + + diff --git a/spoj_cpp_clean/CRAN04/CRAN04-9.cpp b/spoj_cpp_clean/CRAN04/CRAN04-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d075dfb14aed1965e0a42c8e40a545d0a0407ac5 --- /dev/null +++ b/spoj_cpp_clean/CRAN04/CRAN04-9.cpp @@ -0,0 +1,58 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +long long count[1000010]; +int N, K; + +inline long long solve() +{ + int i; + long long sum = 0, curr_sum = 0; + char c; + + memset(count, 0, sizeof(count)); + + scanf("%d %d\n", &N, &K); + + for(i=0; i= K) + sum += count[curr_sum - K]; + count[curr_sum]++; + } + + return sum + count[K]; +} + +int main() +{ + int T; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + printf("%lld\n", solve()); + + return 0; +} diff --git a/spoj_cpp_clean/CRDS/CRDS-16.cpp b/spoj_cpp_clean/CRDS/CRDS-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02e66b1d41d648a4c3b2ea71920a8ac1a6538d47 --- /dev/null +++ b/spoj_cpp_clean/CRDS/CRDS-16.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +void solve(){ + long long n; + cin>>n; + long long c = (3*(n)*(n+1)/2 - n )% 1000007; + cout<>t; + while(t--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/CRDS/CRDS-19.cpp b/spoj_cpp_clean/CRDS/CRDS-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d576f61913eccf18379dee82e5c8a0d869f94b16 --- /dev/null +++ b/spoj_cpp_clean/CRDS/CRDS-19.cpp @@ -0,0 +1,23 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000007 +#define MAX 100001 + +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int t; cin >> t; while (t--) { + unsigned ll n; + cin >> n; + unsigned ll sum; + sum = 3 * (n * n) + n; + sum /= 2; + sum = sum % mod; + cout << sum << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CRDS/CRDS-5.cpp b/spoj_cpp_clean/CRDS/CRDS-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..628722400356d1d721f416eb1edc6fa032f943c3 --- /dev/null +++ b/spoj_cpp_clean/CRDS/CRDS-5.cpp @@ -0,0 +1,24 @@ +#include +#include +#define mod 1000007 +using namespace std; + +int main() +{ + long long test,num,i,j,k; + //cin>>test; + scanf("%lld",&test); + while(test--) + { + //cin>>num; + scanf("%lld",&num); + i=num*(num+1); + i=i%mod; + j=((num-1)*(num))/2; + j=j%mod; + //cout<<(i+j)%mod< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define MOD 1000000007 +#define gc() getchar() + +using namespace std; + +long long powers[100007]; +string A, B; +int N; + +inline void preinit() +{ + int i; + + powers[0] = 1; + + for (i = 1; i <= 100000; i++) + { + powers[i] = powers[i - 1] * 2; + powers[i] %= MOD; + } +} + +inline void getstring(string &A) +{ + char c; + + A.clear(); + + c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +inline void init() +{ + getstring(A); + getstring(B); + N = A.length(); +} + +inline long long solve(string &A, string &B) +{ + int i; + long long sum = 0; + + for (i = 0; i < N && A[i] >= B[i]; i++) + { + if (A[i] == '1') + { + sum += powers[N - i - 1]; + sum %= MOD; + } + } + + if (i == N) + { + if (A[0] == B[0]) + return sum; + + if (A[i - 1] == '1' && B[i - 1] == '0') + { + sum -= 1; + if (sum < 0) + sum += MOD; + return sum; + } + } + + for (; i < N; i++) + if (B[i] == '1') + { + sum += powers[N - i - 1]; + sum %= MOD; + } + + return sum; +} + +int main() +{ + int T, i; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + preinit(); + + for (i = 1; i <= T; i++) + { + init(); + + if (A > B) + printf("Case %d: %lld\n", i, solve(A, B)); + else + printf("Case %d: %lld\n", i, solve(B, A)); + } + + return 0; +} diff --git a/spoj_cpp_clean/CRSCNTRY/CRSCNTRY-13.cpp b/spoj_cpp_clean/CRSCNTRY/CRSCNTRY-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1b21cbbf7816395409810616a6ca58b37c7b544 --- /dev/null +++ b/spoj_cpp_clean/CRSCNTRY/CRSCNTRY-13.cpp @@ -0,0 +1,65 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +int memo[2001][2001]; +bool mark[2001][2001]; + +int f(const vector &ag, const vector &tom, int i, int j){ + const int n = ag.size(), m = tom.size(); + if(i >= n || j >= m)return 0; + int &best = memo[i][j]; + if(mark[i][j])return best; + mark[i][j] = true; + if(ag[i] == tom[j])best = 1 + f(ag,tom,i+1,j+1); + else best = max(f(ag,tom,i+1,j),f(ag, tom, i, j+1)); + return best; +} + +int main(){ + int T; scanf("%d", &T); + for(int t=0; t ag; + for(int next;scanf("%d", &next) == 1 && next != 0;)ag.push_back(next); + int best = 0; + for(int next; scanf("%d", &next) == 1 && next !=0;){ + memset(mark, false, sizeof(mark)); + vector tom; tom.push_back(next); + for(;scanf("%d", &next)==1 && next !=0;)tom.push_back(next); + const int res = f(ag,tom,0,0); + best = max(best, res); + } + printf("%d\n", best); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/CRSCNTRY/CRSCNTRY-7.cpp b/spoj_cpp_clean/CRSCNTRY/CRSCNTRY-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..861da151372b33ff868a39b84e0800bdcbf4e30c --- /dev/null +++ b/spoj_cpp_clean/CRSCNTRY/CRSCNTRY-7.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +int a[2010]; +int b[2010]; +int dp[2010][2010]={-1}; + + + +int lcs(int x, int y) +{ + if (x==-1 || y==-1) + { + //printf("esta\n"); + return 0; + }else if(dp[x][y]!=-1) + { + return dp[x][y]; + }else + { + if ( a[x]== b[y]) + { + //printf("a[%d]== b[%d] (%d==%d)?\n",x,y,a[x],b[y] ); + return dp[x][y]= lcs(x-1,y-1) + 1; + }else + { + return dp[x][y]= max(lcs(x-1,y), lcs(x,y-1)); + } + } +} + + + + + +int main() +{ + + int tc=0,i=0,j=0,aux,cont=0,ans=-1; + + scanf("%d",&tc); + while(tc--) + { + i=0; + j=0; + ans=-1; + //dp.clear(); + + //printf("tc numero %d\n",tc ); + while( true ) + { + scanf("%d",&aux); + if(!aux) { break;} + a[i]= aux; + i++; + + } + + while(true) + { + //printf("entro\n"); + j=0; + scanf("%d",&aux); + if (!aux) + { + break; + } + else + { + j=0; + b[j]= aux; + j++; + while(true) + { + scanf("%d",&aux); + if(!aux) { break;} + b[j]= aux; + j++; + } + + for (int k = 0; k <= i; ++k) + { + for (int l = 0; l <= j; ++l) + { + dp[k][l]=-1; + } + } + + //printf("max= %d\n",ans= max(ans, lcs(i,j)-1 )); + ans= max(ans, lcs(i-1,j-1) ); + //printf("tamaño de a= %d, tamaño de b= %d\n",i,j ); + + } + + + } + + printf("%d\n", ans); + } + + + + return 0; +} diff --git a/spoj_cpp_clean/CRYPTON/CRYPTON-3.cpp b/spoj_cpp_clean/CRYPTON/CRYPTON-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30faa6505bf7e869977089b9c945ef34d710a505 --- /dev/null +++ b/spoj_cpp_clean/CRYPTON/CRYPTON-3.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int primes[200], l = 1, flag; + primes[0] = 2; + for(int i = 3; i < 1000; i+=2){ + flag = 1; + for(int j = 0; j < l; j++){ + if(i%primes[j] == 0){ + flag = 0; + j = l; + } + } + if(flag){ + primes[l++] = i; + } + } + return 0; +} diff --git a/spoj_cpp_clean/CSLIKAR/CSLIKAR-7.cpp b/spoj_cpp_clean/CSLIKAR/CSLIKAR-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5fbd4005312c39246846751b2be573163a3ae56 --- /dev/null +++ b/spoj_cpp_clean/CSLIKAR/CSLIKAR-7.cpp @@ -0,0 +1,156 @@ +#include +#define X first +#define Y second +#define MAXINT 2000000 + + +using namespace std; + +int movx[]= {1,0,-1,0}; +int movy[]= {0,1,0,-1}; + +char grafo[60][60]; +int dist[60][60]; +int dist2[60][60]; +int n=0, m=0; + +bool valida(int a, int b){ + return ((a=0) && (b=0))?true:false; +} + + +void FloodFill(int sx,int sy) +{ + dist[sx][sy]=0; + queue >q; + pair u; + q.push( make_pair(sx,sy) ); + + while(!q.empty()) + { + u= q.front(); q.pop(); + for (int i = 0; i < 4; ++i) + { + int nx= u.X + movx[i]; + int ny= u.Y + movy[i]; + if (valida(nx,ny) && (grafo[nx][ny]!='X' && grafo[nx][ny]!='S' && grafo[nx][ny]!='D') && dist[u.X][u.Y] +1 < dist[nx][ny]) + { + dist[nx][ny]= dist[u.X][u.Y] +1; + q.push( make_pair(nx,ny) ); + } + } + } +} + +void bfs(int sx, int sy) +{ + dist2[sx][sy]=0; + queue >q; + pair u; + q.push( make_pair(sx,sy) ); + + while(!q.empty()) + { + u= q.front(); q.pop(); + for (int i = 0; i < 4; ++i) + { + int nx= u.X + movx[i]; + int ny= u.Y + movy[i]; + if (valida(nx,ny) && grafo[nx][ny]!='X' && dist2[u.X][u.Y] +1 < dist2[nx][ny]) + { + if (dist2[u.X][u.Y] +1 < dist[nx][ny]) + { + dist2[nx][ny]= dist2[u.X][u.Y] +1; + q.push( make_pair(nx,ny) ); + } + } + } + } +} + +void inicializar(int x) +{ + for (int i = 0; i < 60; ++i) + { + for (int j = 0; j < 60; ++j) + { + dist[i][j]=x; + } + } +} + +int main() +{ + vector< pair > v; + scanf("%d %d",&n,&m); + + for (int i = 0; i < n; ++i) + { + scanf("%s",grafo[i]); + } + + pair ini, fin; + + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + if (grafo[i][j]=='S') + { + ini= make_pair(i,j); + } + + if (grafo[i][j]=='D') + { + fin= make_pair(i,j); + } + + if (grafo[i][j]=='*') + { + v.push_back( make_pair(i,j) ); + } + } + } + inicializar(MAXINT); + //cout<<"n.Lagunas= "< +#include +#include + +class DisjointSets { +public: + DisjointSets() {} + DisjointSets(int size) { + size_ = size; + sets_ = std::vector(size, 0); + ranks_ = std::vector(size, 0); + for (int i = 0; i < size; ++i) { + sets_[i] = i; + } + } + ~DisjointSets() {} + + bool unify(int a, int b) { + int ak, bk; + + ak = find(a); + bk = find(b); + + if (ak == bk) return false; + + if (ranks_[ak] > ranks_[bk]) { + sets_[bk] = ak; + } else if (ranks_[ak] < ranks_[bk]) { + sets_[ak] = bk; + } else { + sets_[bk] = ak; + ++ranks_[ak]; + } + + return true; + } + + int find(int e) { + if (sets_[e] == e) return e; + return sets_[e] = find(sets_[e]); + } + + int size() { return size_; } +private: + int size_; + std::vector sets_; + std::vector ranks_; +}; + +class Edge { +public: + Edge() {} + Edge(int from, int to, int cost) { + from_ = from; + to_ = to; + cost_ = cost; + } + ~Edge() {} + int cost() { + return cost_; + } + int from() { + return from_; + } + int to() { + return to_; + } +private: + int from_, to_; + int cost_; +}; + +inline bool compareEdge(Edge* l, Edge* r) { + return l->cost() > r->cost(); +} + +int main(int argc, char **argv) { + int cases, price, buildings, streets; + int from, to, furlongs; + + std::cin >> cases; + while (cases-- > 0) { + std::cin >> price >> buildings >> streets; + std::vector edges; + edges.reserve(streets); + // Graph g(buildings); + + for (int i = 0; i < streets; ++i) { + std::cin >> from >> to >> furlongs; + edges.push_back(new Edge(from, to, furlongs * price)); + // g.addEdge(from, to, furlongs * price); + } + + + int components = buildings; + int total = 0; + + std::sort(edges.begin(), edges.end(), compareEdge); + + DisjointSets dj(buildings); + while (components > 1 && ! edges.empty()) { + Edge *e = edges.back(); + edges.pop_back(); + if ( dj.unify(e->from(), e->to()) ) { + --components; + total += e->cost(); + } + } + + std::cout << total << std::endl; + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/CSTREET/CSTREET-13.cpp b/spoj_cpp_clean/CSTREET/CSTREET-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06a7cc064e0f7ec9be744011969e976206d05f2c --- /dev/null +++ b/spoj_cpp_clean/CSTREET/CSTREET-13.cpp @@ -0,0 +1,79 @@ +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; + +struct road{ + int a, b, cost; + road(){} + road(int _a, int _b, int _cost): + a(_a), b(_b), cost(_cost){} + + bool operator <(const road &other)const{ + return cost < other.cost; + } +}; + +i64 f(int n, int m, i64 p, vector &v){ + vector mark(n); + for(int i=0; i v(m); + + for(int i=0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +long long mw; +int N, E; +int P; + +class ufs +{ + private: + struct set + { + int name; + int rank; + set *parent; + }; + int ssize; + vector S; + + set *fs(int name) + { + if(S[name].parent->name==name) + return &S[name]; + + S[name].parent=fs(S[name].parent->name); + return S[name].parent; + } + + + public: + ufs(int n) + { + int i; + ssize=n; + S.resize(ssize); + + for(i=0; iname; + } + + bool is_same_set(int a, int b) + { + return fs(a)->name==fs(b)->name; + } + + void union_sets(int a, int b) + { + set *f, *s; + + f=fs(a); + s=fs(b); + + if(f->name==s->name) + return; + + ssize--; + if(f->rank==s->rank) + { + f->rank++; + s->parent=f; + } + + else if(f->rank > s->rank) + s->parent=f; + + else + f->parent=s; + } + + int size() + { + return ssize; + } +}; + + +void init() +{ + int i; + int f, s; + int w; + + scanf("%d", &P); + scanf("%d", &N); + scanf("%d", &E); + + list< pair > > G; + + mw=0; + + for(i=0; i > >::iterator ii; + int v=0; + ufs g(N+1); + + G.sort(); + + ii=G.begin(); + + while(vsecond.first, ii->second.second)) + { + v++; + mw+=ii->first; + + g.union_sets(ii->second.first, ii->second.second); + } + ii++; + } +} + +int main() +{ + int T; + + //freopen("input.txt","r",stdin); + scanf("%d", &T); + while(T--) + { + init(); + printf("%d\n", mw*P); + } + return 0; +} diff --git a/spoj_cpp_clean/CUBEFR/CUBEFR-3.cpp b/spoj_cpp_clean/CUBEFR/CUBEFR-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0549279a563c0b138ae9f79111cc29774303b466 --- /dev/null +++ b/spoj_cpp_clean/CUBEFR/CUBEFR-3.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int num[1000001]; +vector v; + +int main(){ + int a, b, t, n; + for(int i = 2; i <= 100; i++){ + a = i*i*i; + b = a; + while(b <= 1000000){ + num[b] = 1; + b += a; + } + } + for(int i = 1; i <= 1000000; i++){ + if(!num[i]) + v.push_back(i); + } + //cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + cout << setprecision (4) << fixed; + int tc; + cin >> tc; + + while (tc--) { + double n; + cin >> n; + double res = cbrt (n); + int val = res * 10000.0; + double ans = (double) val / 10000.0; + cout << ans << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/CURDPROD/CURDPROD-3.cpp b/spoj_cpp_clean/CURDPROD/CURDPROD-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d36e58f2471012ab4a193819a49c018a4c03f80 --- /dev/null +++ b/spoj_cpp_clean/CURDPROD/CURDPROD-3.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%llu",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; +int t, n, T, q[10000]; +long long p,sum,m, total; + +void check(long long &val){ + total = 0; + for(int i = 0; i < n; i++) + total += val/q[i]; +} + +int main(){ + sd(t); + while(t--){ + scanf("%d%d",&n,&T); + m = 0; + for(int i = 0; i < n; i++){ + sd(q[i]); + m = (m > q[i])? m:q[i]; + } + long long lo = 1, hi = T*m, mid; + while(lo < hi){ + mid = (lo+hi)/2; + check(mid); + if(total >= T) + hi = mid; + else + lo = mid + 1; + } + printf("%lld\n",hi); + } + return 0; +} diff --git a/spoj_cpp_clean/CUTSQRS/CUTSQRS-3.cpp b/spoj_cpp_clean/CUTSQRS/CUTSQRS-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65d2d8ec824c4ecc63ecad71745967fb832a137b --- /dev/null +++ b/spoj_cpp_clean/CUTSQRS/CUTSQRS-3.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +bool isgood(int a,int b) +{ +int n; +if(b%a) +{ +n=b/a; +if(n>1) return 1; +return (1-isgood(b-a,a)); +} +return 1; +} + + +long long total_moves(int a, int b){ + long long count = 0, temp; + while(a != 0 ){ + count += (int)b/a-1; + temp = b%a; + b = a; + a = temp; + } + return count; + } + +int main(){ + int t, a, b, j, k; + long double p=log10((double)2),m; + sd(t); + while(t--){ + j = 0; + sd2(a,b); + k=total_moves(a,b); + m=k*p; + j+=(int)m+1; + if(k) m=(k-1)*p; + else m=0; + j+=(int)m+1; + j+=isgood(a,b)?5:6; + cout< +using namespace std; + +#define ll long long + +ll ltree[400009], rtree[400009], trace[100009]; + +void update(ll lo, ll hi, ll indx, ll state, ll node) + +{ + if(lo > indx || hi < indx) + return; + if(lo == hi) { + if(state) + rtree[node]++; + else + ltree[node]++; + + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, indx, state, 2 * node); + update(mid + 1, hi, indx, state, 2 * node + 1); + + if(state) + rtree[node] = rtree[2 * node] + rtree[2 * node + 1]; + else + ltree[node] = ltree[2 * node] + ltree[2 * node + 1]; +} + +ll query(ll lo, ll hi, ll left, ll right, ll state, ll node) + +{ + if(left > right) + return 0; + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) + if(state) + return rtree[node]; + else + return ltree[node]; + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, state, 2 * node); + ll p2 = query(mid + 1, hi, left, right, state, 2 * node + 1); + + return p1 + p2; +} + +int main() + +{ + ll n; + cin >> n; + while(n--) { + ll x1, x2, ans = 0; + scanf("%lld %lld", &x1, &x2); + ll cnt1 = query(1, 100000, 1, x1 - 1, 0, 1); + ll cnt2 = query(1, 100000, 1, x1, 1, 1); + ll cnt = cnt1 - cnt2 - trace[x1]; + trace[x1] += cnt; + ans += cnt; + + cnt1 = query(1, 100000, 1, x2 - 1, 0, 1); + cnt2 = query(1, 100000, 1, x2, 1, 1); + cnt = cnt1 - cnt2 - trace[x2]; + trace[x2] += cnt; + ans += cnt; + + printf("%lld\n", ans); + + update(1, 100000, x1, 0, 1); + update(1, 100000, x2, 1, 1); + } + + return 0; +} diff --git a/spoj_cpp_clean/DANGER/DANGER-19.cpp b/spoj_cpp_clean/DANGER/DANGER-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4acfd48d31a29f488061710d8ffcd8780d4ffb57 --- /dev/null +++ b/spoj_cpp_clean/DANGER/DANGER-19.cpp @@ -0,0 +1,83 @@ +#pragma GCC optimize("Ofast") +#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") +#pragma GCC optimize("unroll-loops") +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair p32; +typedef pair p64; +typedef pair pdd; +typedef vector v64; +typedef vector v32; +typedef vector > vv32; +typedef vector > vv64; +typedef vector > vvp64; +typedef vector vp64; +typedef vector vp32; +ll MOD = 998244353; +double eps = 1e-12; +#define forn(i,s,e) for(ll i = s; i < e; i++) +#define rforn(i,s,e) for(ll i = s; i >= e; i--) +#define endl "\n" +#define dbg(x) cout<<#x<<" = "<>t; + if(t=="00e0"){ + break; + } + int n = stoi(t.substr(0,2)); + int z = stoi(t.substr(3, 1)); + solve(n,z); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/DCEPC11B/DCEPC11B-3.cpp b/spoj_cpp_clean/DCEPC11B/DCEPC11B-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24c8ecf7f531db0134604754079ec61993ffa43a --- /dev/null +++ b/spoj_cpp_clean/DCEPC11B/DCEPC11B-3.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, i, n, p, c; + long long result, ans; + sd(t); + while(t--){ + scanf("%d %d",&n, &p); + if(n >= p){ + printf("0\n"); + continue; + } + result = 1; + for(i = n + 1; i < p; i++){ + result = (result*i) %p; + } + c = p-2, ans = p-1; + while(c){ + if(c&1) ans = (result*ans)%p; + result = (result*result)%p; + c >>= 1; + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/spoj_cpp_clean/DCEPC206/DCEPC206-9.cpp b/spoj_cpp_clean/DCEPC206/DCEPC206-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61e776e30ddd1c2992268273340de40fa3ef91ba --- /dev/null +++ b/spoj_cpp_clean/DCEPC206/DCEPC206-9.cpp @@ -0,0 +1,79 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define gc() getchar_unlocked() + +long long tree[10][1000007]; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +inline void update(int i, int pos, int val) +{ + for( ; pos <= 1000006; pos += pos&(-pos)) + tree[i][pos] += val; +} + +inline long long query(int i, int pos) +{ + long long sum = 0; + + for( ; pos>0; pos -= pos&(-pos)) + sum += tree[i][pos]; + + return sum; +} + +int main() +{ + int T, t, i, N, num; + long long sum; + + //freopen("input.txt","r",stdin); + + T=FAST_IO(); + + for(t=0; t Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int N, M, K; + +inline long long solve() +{ + vector X, Y; + int maxX = 0, maxY = 0; + int i, x, y; + + scanf("%d %d %d", &N, &M, &K); + + if(K == 0) + return N * M; + + X.push_back(0); + Y.push_back(0); + X.push_back(N + 1); + Y.push_back(M + 1); + + for(i = 0; i < K; i++) + { + scanf("%d %d", &x, &y); + X.push_back(x); + Y.push_back(y); + } + + + sort(X.begin(), X.end()); + sort(Y.begin(), Y.end()); + + for(i = 1; i < X.size(); i++) + { + if(X[i] - X[i - 1] - 1 > maxX) + maxX = X[i] - X[i - 1] - 1; + if(Y[i] - Y[i - 1] - 1 > maxY) + maxY = Y[i] - Y[i - 1] - 1; + } + + return maxX * maxY; +} + +int main() +{ + int T; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + printf("%lld\n", solve()); + + return 0; +} diff --git a/spoj_cpp_clean/DEFKIN2/DEFKIN2-9.cpp b/spoj_cpp_clean/DEFKIN2/DEFKIN2-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..396d420801e37a99605d824c4e81d305b55cd65f --- /dev/null +++ b/spoj_cpp_clean/DEFKIN2/DEFKIN2-9.cpp @@ -0,0 +1,41 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +float N, M, K; + +inline int solve() +{ + float minX, minY; + + scanf("%f %f %f", &N, &M, &K); + + minX = ceil((N - K) / (K + 1)); + minY = ceil((M - K) / (K + 1)); + + return minX * minY; +} + +int main() +{ + int T; + + scanf("%d", &T); + + while(T--) + printf("%d\n", solve()); + + return 0; +} diff --git a/spoj_cpp_clean/DFLOOR/DFLOOR-13.cpp b/spoj_cpp_clean/DFLOOR/DFLOOR-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06b0e8164b2a27787dbc330cbce6bd982a70e811 --- /dev/null +++ b/spoj_cpp_clean/DFLOOR/DFLOOR-13.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +char buffer[20][20]; + +int main(){ + int w, h; + while(scanf("%d %d", &w, &h) == 2 && (w || h)){ + for(int i=0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar_unlocked() +using namespace std; + +inline void printstring(string &A) +{ + int i; + + for (i = 0; i < A.length(); i++) + putchar(A[i]); +} + +inline void getstring(string &A) +{ + char c; + + A.clear(); + + c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +class trie +{ + struct node + { + bool word; + struct node **childs; + + node() + { + int i; + + word = 0; + childs = new struct node*[26]; + + for (i = 0; i < 26; i++) + childs[i] = nullptr; + } + + ~node() + { + delete[] childs; + } + + void add(string &A, int pos) + { + if (pos == A.length()) + { + word = 1; + return; + } + + if (childs[A[pos] - 'a'] == nullptr) + childs[A[pos] - 'a'] = new struct node(); + + childs[A[pos] - 'a']->add(A, pos + 1); + } + + struct node *getnode(string &prefix, int pos) + { + if (pos == prefix.length()) + return this; + + if (childs[prefix[pos] - 'a'] == nullptr) + return nullptr; + + return childs[prefix[pos] - 'a']->getnode(prefix, pos + 1); + } + + void print(string &prefix) + { + int i; + + if (word) + { + printstring(prefix); + putchar('\n'); + } + + for (i = 0; i < 26; i++) + if (childs[i] != nullptr) + { + prefix.push_back(i + 'a'); + childs[i]->print(prefix); + prefix.pop_back(); + } + } + + }; + + struct node *root; + + public: + trie() + { + root = new struct node; + } + + void addword(string &A) + { + root->add(A, 0); + } + + bool retrieve(string &prefix) + { + struct node *p = root->getnode(prefix, 0); + bool flag = 0; + + if (p == nullptr) + return 0; + + int i; + + for (i = 0; i < 26; i++) + if (p->childs[i] != nullptr) + { + prefix.push_back(i + 'a'); + p->childs[i]->print(prefix); + prefix.pop_back(); + flag = 1; + } + + return flag; + } +}; + +trie *T; + +inline void init() +{ + int i, N; + string A; + + T = new trie(); + + scanf("%d", &N); + + while (N--) + { + getstring(A); + T->addword(A); + } +} + +int main() +{ + int N, i; + string A; + +// freopen("input.txt", "r", stdin); + + init(); + + scanf("%d", &N); + + for (i = 1; i <= N; i++) + { + printf("Case #%d:\n", i); + getstring(A); + + if (!T->retrieve(A)) + printf("No match.\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/DIGCNT/DIGCNT-3.cpp b/spoj_cpp_clean/DIGCNT/DIGCNT-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf86f99883cb76cc4f6cfa6bbb8bf2d6dbc79ede --- /dev/null +++ b/spoj_cpp_clean/DIGCNT/DIGCNT-3.cpp @@ -0,0 +1,16 @@ +#include +typedef int64_t q; +q a, b, d, B, f=10; +q s(q x, q l, q p){ + if(p > x) return 0; + B = (x / p) % 10; + return (p / f * l * B) + (p * (d < B)) + ((x % p + 1)*(d == B)) + s(x, l+1, p*f); +} + +int main(){ + while(std::cin >> a >> b) if(a){ + if(a > b) std::swap(a,b); + for(d = 1; d < f; d++) std :: cout << s(b, 0, 1) - s(a-1, 0, 1) << " \n"[d == 9]; + } + return 0; +} diff --git a/spoj_cpp_clean/DIRVS/DIRVS-3.cpp b/spoj_cpp_clean/DIRVS/DIRVS-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8f98fb136b9cf65d32e5e0e4e7d0b46d30a3974 --- /dev/null +++ b/spoj_cpp_clean/DIRVS/DIRVS-3.cpp @@ -0,0 +1,144 @@ +/* ACM International Collegiate Programming Contest + Central European Regional Contest 2000 + + Problem D - Direct Visibility + Jan Kybic +*/ + +#include + +#define MAXQ 512 + +static int h[MAXQ][MAXQ] ; /* heights */ +static int fx[MAXQ][MAXQ],fy[MAXQ][MAXQ] ; /* link to the next field i.e. sequentially gives the order in which a new valid field was discovered*/ +static int f[MAXQ][MAXQ] ; /* flag==1, I was here already */ + +int bx,by,lx,ly ; /* beginning and the end of the list */ + +int p,q ; + +int solve_subtaskx(int &x1,int &y1,int &x2,int &y2) +{ int z,edge ; + int dx,z1,z2,dz,ry,t,y,dy,ry2 ; + + dx=x2-x1 ; z1=h[x1-1][y1-1] ; z2=h[x2-1][y2-1] ; + dz=z2-z1 ; dy=y2-y1 ; + z=(2*z1+1)*dx+dz ; /* the height at the first intersection times 2dx */ + y=2*dx*y1+dy ; /* the y coordinate at the first intersection times 2dx */ + for (t=x1;t0))/(2*dx) ; /* rounded y */ + ry2=(y+dx-(1-(dy>0)))/(2*dx) ; + + if (z0))/(2*dy) ; /* rounded y */ + rx2=(x+dy-(1-(dx>0)))/(2*dy) ; /* rounded y */ + if (zh[x1-1][y1-1]+1) return 0 ; /* too high */ + if (h[x2-1][y2-1]p || j<1 || j>q) continue ; /* outside */ + if (f[i-1][j-1]>-1) continue ; /* we have been here already */ + if (!can_go(bx,by,i,j,x1,y1,x2,y2)) continue ; /* illegal */ + /* possible move */ + f[i-1][j-1]=f[bx-1][by-1]+1 ; /* assign distance */ + if (i==x2 && j==y2) return f[i-1][j-1] ; /* here we are at the end */ + /* add to the list */ + fx[lx-1][ly-1]=i ; fy[lx-1][ly-1]=j ; lx=i ; ly=j ; + } + /* take out the point from the list */ + i=fx[bx-1][by-1] ; j=fy[bx-1][by-1] ; bx=i ; by=j ; + if (bx==-1) return -1 ; /* no more points, no way */ + } +} + + +void solve_one() +{ + int s,i,j,x1,x2,y1,y2 ; + int l ; + + scanf("%d %d",&p,&q) ; + /* we read the heights */ + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX], cost[MAX]; +int level[MAX], par[MAX]; +int table[MAX][20], mx_rd[MAX][20], mn_rd[MAX][20]; + +void dfs (int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + level[v] = level[u] + 1; + par[v] = u; + mx_rd[v][0] = cost[u][i]; + mn_rd[v][0] = cost[u][i]; + dfs (v, u); + } +} + +void LCA (int n) { + for (int i = 1; i <= n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) <= n; j++) { + for (int i = 1; i <= n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + mx_rd[i][j] = max (mx_rd[i][j - 1], mx_rd[x][j - 1]); + mn_rd[i][j] = min (mn_rd[i][j - 1], mn_rd[x][j - 1]); + + } else { + table[i][j] = -1; + mx_rd[i][j] = mx_rd[i][j - 1]; + mn_rd[i][j] = mn_rd[i][j - 1]; + } + } + } +} + +int LCA_Quary (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} + +int main () { + //~ __FastIO; + int n; + scanf ("%d", &n); + + for (int i = 0; i < n - 1; i++) { + int u, v, w; + scanf ("%d %d %d", &u, &v, &w); + adj[u].pb (v); + adj[v].pb (u); + cost[u].pb (w); + cost[v].pb (w); + } + + SET (par, 0); + SET (level, 0); + + mx_rd[1][0] = -1; + mn_rd[1][0] = 1000005; + par[1] = -1; + + dfs (1, -1); + LCA (n); + int Q; + scanf ("%d", &Q); + + for (int q = 1; q <= Q ; q++) { + int u, v; + scanf ("%d %d", &u, &v); + int x = LCA_Quary (u, v); + int uu = level[u] - level[x]; + int vv = level[v] - level[x]; + int mx = 0, mn = 1000005; + + for (int i = log2 (uu); i >= 0; i--) { + if ( (1 << i) <= uu) { + mx = max (mx, mx_rd[u][i]); + mn = min (mn, mn_rd[u][i]); + u = table[u][i]; + uu -= (1 << i); + } + + if (uu == 0) break; + } + + for (int i = log2 (vv); i >= 0; i--) { + if ( (1 << i) <= vv) { + mx = max (mx, mx_rd[v][i]); + mn = min (mn, mn_rd[v][i]); + v = table[v][i]; + vv -= (1 << i); + } + + if (vv == 0) break; + } + + printf ("%d %d\n", mn, mx); + } + + return 0; +} diff --git a/spoj_cpp_clean/DISTO/DISTO-7.cpp b/spoj_cpp_clean/DISTO/DISTO-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2dd1ae01cc37048af2f721ead38f063f87497b2 --- /dev/null +++ b/spoj_cpp_clean/DISTO/DISTO-7.cpp @@ -0,0 +1,93 @@ +#include +#define X first +#define Y second + +using namespace std; + +int n,k,x,y; + +char grafo[550][550]; +int dist[550][550]; + +vector< pair > v; +int movx[]= {0,1,-1,0,1,1,-1,-1}; +int movy[]= {1,0,0,-1,1,-1,1,-1}; + +bool valido(int a, int b){ + return ((n>=a && a>0) && (n>=b && b>0))?true:false; +} + +void bfs(int sx,int sy) +{ + queue< pair > q; + dist[sx][sy]= 5; + + q.push( make_pair(sx,sy) ); + + while(!q.empty()) + { + pair u= q.front(); q.pop(); + for (int i = 0; i < 8; ++i) + { + int nx= u.X + movx[i]; + int ny= u.Y + movy[i]; + if (valido(nx,ny) && dist[u.X][u.Y] -1 > dist[nx][ny]) + { + dist[nx][ny]= dist[u.X][u.Y] -1; + q.push( make_pair(nx,ny) ); + } + } + } +} + + +int main() +{ + scanf("%d %d",&n,&k); + + for (int i = 0; i < n; ++i) + { + for (int j = 0; j >grafo[i][j]; + } + } + + for (int i = 1; i <= n+1; ++i) + { + for (int j = 1; j <= n+1; ++j) + { + dist[i][j]=1; + } + } + + for (int i = 0; i < k; ++i) + { + scanf("%d %d",&x,&y); + bfs(x,y); + } + + /*for (int i = 1; i <= n; ++i) + { + for (int j = 1; j <= n; ++j) + { + printf("%d ",dist[i][j]); + } + printf("\n"); + }*/ + + for (int i = 1; i <= n; ++i) + { + for (int j = 1; j <= n; ++j) + { + printf("%c", ((char)((((int)grafo[i-1][j-1])*dist[i][j]) %93) + 33) ); + if (j!=n) + { + printf(" "); + } + } + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/DISUBSTR/DISUBSTR-14.cpp b/spoj_cpp_clean/DISUBSTR/DISUBSTR-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..71d46394e96a7d3279aa97f91580691c5f113749 --- /dev/null +++ b/spoj_cpp_clean/DISUBSTR/DISUBSTR-14.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; + +char ara[1009]; +#define ll unsigned long long +unordered_map ase; + +int main() + +{ + int n; + scanf("%lld", &n); + while(n--) { + scanf("%s", ara); + ll len = strlen(ara); + ll cnt = 0, hsh, gun; + + for(ll i = 0; i < len; i++) { + hsh = 0, gun = 1; + for(ll j = i; j < len; j++) { + hsh += (ara[j] - 32) * gun; + gun *= 101; + + if(ase.find(hsh) == ase.end()) { + cnt++; + ase[hsh] = 1; + } + } + } + + printf("%lld\n", cnt); + ase.clear(); + } + + return 0; +} diff --git a/spoj_cpp_clean/DISUBSTR/DISUBSTR-4.cpp b/spoj_cpp_clean/DISUBSTR/DISUBSTR-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da91f7c1460c4ffd13f6e3c1ab359aa05eab7160 --- /dev/null +++ b/spoj_cpp_clean/DISUBSTR/DISUBSTR-4.cpp @@ -0,0 +1,303 @@ +// Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0; i33) + { + h=h*10+c-'0'; + c=getchar();// getchar_unlocked(); + } + return h; +} +using namespace std; + +#define For(i, a, b) for (int i=(a); i<(b); ++i) +#define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x) +#define D(x) cout << #x " = " << (x) << endl + +const int N = 50005; +/* +// Begins Suffix Arrays implementation +// O(n log n) - Manber and Myers algorithm + +// Usage: +// Fill str with the characters of the string. +// Call SuffixSort(n), where n is the length of the string stored in str. +// That's it! + +// Output: +// pos = The suffix array. Contains the n suffixes of str sorted in lexicographical order. +// Each suffix is represented as a single integer (the position of str where it starts). +// rank = The inverse of the suffix array. rank[i] = the index of the suffix str[i..n) +// in the pos array. (In other words, pos[i] = k <==> rank[k] = i) +// With this array, you can compare two suffixes in O(1): Suffix str[i..n) is smaller +// than str[j..n) if and only if rank[i] < rank[j] +*/ +int str[N]; // input +int rank[N], pos[N]; // output +int cnt[N], next[N]; // internal +bool bh[N], b2h[N]; // buckets +string s; + +bool smaller_first_char(int a, int b) +{ + return str[a] < str[b]; +} + +void SuffixSort(int n) +{ + // sort suffixes according to their first character + for (int i=0; i rank[k] = i + } // Actually j or k is the suffix postions, ^^^^see + } + + cnt[rank[n - h]]++; //h chars are considered for now + b2h[rank[n - h]] = true; + for (int i = 0; i < n; i = next[i]) // for each bucket + { + for (int j = i; j < next[i]; ++j) // elements in each bucket + { + int s = pos[j] - h; + if (s >= 0) + { + int head = rank[s]; // rank of suffix str[s..n-1] + rank[s] = head + cnt[head]++; // rank of suffix str[s..n-1] is incrementd to new postion + b2h[rank[s]] = true; // rank was updated in + } + } + for (int j = i; j < next[i]; ++j) + { + int s = pos[j] - h; + if (s >= 0 && b2h[rank[s]])// if still there is diff ranks of 2 suffix + { + for (int k = rank[s]+1; !bh[k] && b2h[k]; k++) // for all next with same + b2h[k] = false; // rank in one bucket + } + } + } + // For each h chars once only + for (int i=0; i buckets of h length, b2h -> buckets of 2*h length + pos[rank[i]] = i; // update rank + bh[i] |= b2h[i]; // update buckets + } + + } + //Finally pos[] is the Suffix array ! and rank[] is inverse ! + for (int i=0; i 1 then +// 7 k := Pos[Rank[i]-1] +// 8 while A[i+h] = A[j+h] do +// 9 h := h+1 +// 10 od +// 11 Height[Rank[i]] := h +// 12 if h > 0 then h := h-1 fi +// 13 fi +// 14 od +*/ +int height[N]; // LCP(i, i-1); +// height[i] = length of the longest common prefix of suffix pos[i] and suffix pos[i-1] +// height[0] = 0 + + +// Construction LCP [longest common prefix] array +void getHeight(int n) +{ + for (int i=0; i 0) // not the the 0th rank suffix + { + int j = pos[rank[i]-1]; + while (i+h 0) h--; + } + } +} + +void solve() +{ + int n = s.size(); + for (int i=0; i> t; + while (t--) + { + cin >> s; + + solve(); +/* + error("pos[]"); + FOR(i,0, 8) + { + cout< + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< pd[N]; + +int good[N]; + +int main(){ + getSieve(); + + for(int i = 2; i < N; i++){ + int p = sieve[i]; + int x = i / sieve[i]; + int cnt = 1; + + + while(x > 1){ + if(sieve[x] == p){ + x /= p; + cnt++; + } + else{ + pd[i].pb(cnt+1); + + p = sieve[x]; + x /= p; + cnt = 1; + } + } + + pd[i].pb(cnt+1); + + if(pd[i].size() == 2 and pd[i][0] == 2 and pd[i][1] == 2){ + good[i] = 1; + } + + if(pd[i].size() > 2) pd[i].clear(); + } + + int cnt = 0; + for(int i = 1; i < N; i++){ + bool flag = false; + if(pd[i].size() == 2 and sieve[pd[i][0]] == pd[i][0] and sieve[pd[i][1]] == pd[i][1] and pd[i][0] != pd[i][1]) flag = true; + else if(pd[i].size() == 1 and good[pd[i][0]]) flag = true; + + if(flag){ + if(++cnt == 9){ + cnt = 0; + printf("%d\n", i); + } + } + + } + + return 0; +} diff --git a/spoj_cpp_clean/DIV15/DIV15-14.cpp b/spoj_cpp_clean/DIV15/DIV15-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ee9c43f0d2f8c7993f6810236299c73b47a2f78 --- /dev/null +++ b/spoj_cpp_clean/DIV15/DIV15-14.cpp @@ -0,0 +1,130 @@ +#include +using namespace std; + +#define ll long long + +bool cmp(char a, char b) + +{ + return a > b; +} + +int main() + +{ + std::ios_base::sync_with_stdio(false); + string s; + ll t; + cin >> t; + while(t--) { + cin >> s; + ll sum = 0, flag = 0, cnt = 0, idx = -1; + for(ll i = 0; i < s.length(); i++) { + sum += (s[i] - '0'); + + if(s[i] == '5' || s[i] == '0') { + flag = 1; + if(idx == -1) + idx = i; + else if(s[i] < s[idx]) + idx = i; + } + + if(s[i] > '0') + cnt++; + } + + if(flag == 0) { + cout << "impossible" << endl; + continue; + } + if(cnt == 0) { + cout << 0 << endl; + continue; + } + + string s2 = ""; + for(ll i = 0; i < s.length(); i++) { + if(idx == i) + continue; + s2 += s[i]; + } + + sort(s2.begin(), s2.end(), cmp); + + if(sum % 3 == 1) { + bool check = 0; + for(ll i = s2.length(); i >= 0; i--) { + ll nm = s2[i] - '0'; + if(nm % 3 == 1) { + check = 1; + s2.erase(s2.begin() + i); + break; + } + } + + ll cnt2 = 0; + if(!check) { + for(ll i = s2.length(); i >= 0; i--) { + ll nm = s2[i] - '0'; + if(nm % 3 == 2) { + s2.erase(s2.begin() + i); + cnt2++; + } + + if(cnt2 == 2) + break; + } + } + + if(check == 0 && cnt2 != 2) { + cout << "impossible" << endl; + continue; + } + + } + else if(sum % 3 == 2) { + bool check = 0; + for(ll i = s2.length(); i >= 0; i--) { + ll nm = s2[i] - '0'; + if(nm % 3 == 2) { + check = 1; + s2.erase(s2.begin() + i); + break; + } + } + + ll cnt1 = 0; + if(!check) { + for(ll i = s2.length(); i >= 0; i--) { + ll nm = s2[i] - '0'; + if(nm % 3 == 1) { + s2.erase(s2.begin() + i); + cnt1++; + } + + if(cnt1 == 2) + break; + } + } + + if(check == 0 && cnt1 != 2) { + cout << "impossible" << endl; + continue; + } + } + + if(s2[0] == '0' && s[idx] == '5') { + cout << "impossible" << endl; + continue; + } + + if(s2[0] == '0' && s[idx] == '0') { + cout << 0 << endl; + continue; + } + + cout << s2 << s[idx] << endl; + } + + return 0; diff --git a/spoj_cpp_clean/DIV2/DIV2-3.cpp b/spoj_cpp_clean/DIV2/DIV2-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f5bc5f179484e1546900f1c8e0dd3c169d459d5 --- /dev/null +++ b/spoj_cpp_clean/DIV2/DIV2-3.cpp @@ -0,0 +1,68 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< 3; + } + + for(int i = 1; i < N; i++){ + for(int j = i; j < N; j += i){ + if(d[j] % d[i] != 0) ok[j] = 0; + } + } + + int cnt = 0; + for(int i = 2; i < N; i++){ + if(ok[i] and ++cnt == 108){ + printf("%d\n", i); + cnt = 0; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/DIVFACT/DIVFACT-3.cpp b/spoj_cpp_clean/DIVFACT/DIVFACT-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a3902798428af14b0d670b11bd0f6e4bda905b0 --- /dev/null +++ b/spoj_cpp_clean/DIVFACT/DIVFACT-3.cpp @@ -0,0 +1,128 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<>= 1; + } + return ret; +} + +int isprime[N]; +vector primes; + +int ans[T]; +int f[N]; +ll inv[100]; + +int main(){ _ + for(int i = 1; i < 100; i++){ + inv[i] = fast(i, MOD-2); + } + + for(int i = 2; i < N; i++){ + if(isprime[i]) continue; + for(int j = 2*i; j < N; j += i){ + isprime[j] = 1; + } + + primes.pb(i); + } + + int t; + cin >> t; + + vector q; + + for(int i = 0; i < t; i++){ + int x; + cin >> x; + q.pb(mp(x,i)); + } + + sort(q.begin(), q.end()); + + ll cur = 1; + + int i = 1; + for(int j = 0; j < q.size(); j++){ + int x = q[j].fi; + + if(x == 0){ + ans[q[j].se] = 1; + continue; + } + + while(i <= x){ + int tmp = i; + for(int &p : primes){ + if(p > tmp) break; + + int cnt = 0; + + while(tmp % p == 0){ + tmp /= p; + cnt++; + } + + if(!cnt) continue; + + cur = (cur * fast(f[p]+1, MOD-2)) % MOD; + f[p] += cnt; + cur = (cur * (f[p]+1)) % MOD; + } + + i++; + } + + int id = q[j].se; + ans[id] = cur; + } + + for(int i = 0; i < t; i++){ + printf("%d\n", ans[i]); + } + + return 0; +} diff --git a/spoj_cpp_clean/DIVSUM/DIVSUM-4.cpp b/spoj_cpp_clean/DIVSUM/DIVSUM-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e42af46fe0ca07231676a276d05d10d677edcca --- /dev/null +++ b/spoj_cpp_clean/DIVSUM/DIVSUM-4.cpp @@ -0,0 +1,26 @@ +#include +# include +int t; +int main() +{ +scanf("%d",&t); + int i,j,a,sum=0; +while(t--) +{ + sum=0; + scanf("%d",&a); + j=(int)sqrt(a); + for(i=1;i*i +#define MAX 100000107 +#define MOD 10000000000000000 + +using namespace std; + + +std::vector prime; +bool p[MAX+1]; + +typedef unsigned long long llu; + +llu mulmod(llu a, llu b, llu mod) +{ + llu res = 0; // Initialize result + a = a % mod; + while (b > 0) + { + // If b is odd, add 'a' to result + if (b % 2 == 1) + res = (res + a) % mod; + + // Multiply 'a' with 2 + a = (a * 2) % mod; + + // Divide b by 2 + b /= 2; + } + + // Return result + return res % mod; +} + +void sieve(int max) +{ + memset(p,true,sizeof(p)); + for (int i = 2; i*i <= max ; ++i) + { + if (p[i]) + { + for (int j = i*2; j <=max ; j+=i) + { + p[j]= false; + } + } + } + + for (int i = 2; i <=max ; ++i) + { + if (p[i]) + { + prime.push_back(i); + } + } + } + + vector< pair > factores; + void factorizacion(llu num) + { + int exp=0; + int primo=0; + factores.clear(); + for (int i = 0; prime[i]*prime[i]<= num; ++i) + { + exp=0; + primo= prime[i]; + while (num%primo==0) + { + exp++; + num/= primo; + } + + if (exp>0) + { + factores.push_back( make_pair(primo,exp)); + } + +} + +if (num>=2) +{ + factores.push_back( make_pair(num,1) ); +} +} + + +vector divisors; + +void setDivisors(llu cur,int i){ + if(i==factores.size()){ + //cur aqui es un divisor + divisors.push_back(cur); + return; + } + for(int j=0;j<=factores[i].second;j++){ + setDivisors(cur,i+1); + + + cur= mulmod(factores[i].first,cur,MOD); + } +} + + + +int main() +{ +int t=0; +llu sum=0, n; +sieve(MAX); + +scanf("%d",&t); + + + +while(t--) +{ + scanf("%lld", &n); + sum=0; + factorizacion(n); + divisors.clear(); + setDivisors(1,0); +/* + cout<<"primos: "; + for (int i=0; prime[i]::iterator i = divisors.begin(); i != divisors.end(); ++i) + { + sum+=(*i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int A[1000007]; +int N; + +inline void init() +{ + int i; + + scanf("%d", &N); + + for (i = 1; i <= N; i++) + scanf("%d", &A[i]); +} + +inline int solve() +{ + multiset< int > V; + int i; + multiset< int >::iterator ii; + + V.insert(A[1] - 1); + + for (i = 2; i <= N; i++) + if (A[i] >= i) + { + A[i] -= i; + ii = V.insert(A[i]); + + if (next(ii) != V.end()) + V.erase(next(ii)); + } + + return V.size(); +} + +int main() +{ +// freopen("input.txt", "r", stdin); + + init(); + printf("%d\n", N - solve()); + + return 0; +} diff --git a/spoj_cpp_clean/DOTAA/DOTAA-7.cpp b/spoj_cpp_clean/DOTAA/DOTAA-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fe5dbfdbd070aee9e1594b2991ad39e1266f720 --- /dev/null +++ b/spoj_cpp_clean/DOTAA/DOTAA-7.cpp @@ -0,0 +1,23 @@ +#include +int main() +{ + int T,n,i,m,d,h[500],cnt=0; + scanf("%d",&T); + while(T--) + { + scanf("%d %d %d",&n,&m,&d); + cnt=0; + + for(i=0;i0) { + cnt++; + } + } + + if(cnt>=m) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} diff --git a/spoj_cpp_clean/DOTTA/DOTTA-4.cpp b/spoj_cpp_clean/DOTTA/DOTTA-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c87cc597fdc46a37bced7d36e3d39cd1921cd6c1 --- /dev/null +++ b/spoj_cpp_clean/DOTTA/DOTTA-4.cpp @@ -0,0 +1,122 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +//Other Includes +#include +#include +#include +#include +#include +#include +#include +#include +//some common functionn +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define all(c) (c).begin(),(c).end() +// Input macros +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) + +// Output macros +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +using namespace std; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i=m) + { + cout<<"YES"; + } + else + cout<<"NO"; + cout<0) + res+=t; + } + return true; +} + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + + int T=1; + s(T); + for(int testnum=1;testnum<=T;testnum++) + { + if(!input()) break; + + solve(); + //printf("\n"); + + } +// system("pause"); + return 0; +} + + + + diff --git a/spoj_cpp_clean/DP/DP-3.cpp b/spoj_cpp_clean/DP/DP-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbee0984c7e0185a1db7f84e0e7544c7fca20d00 --- /dev/null +++ b/spoj_cpp_clean/DP/DP-3.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int x[] = {0,1,0,-1}; +int y[] = {1,0,-1,0}; +int c, m, n, dist[3000], start, ND, node, node2, a, b, N; +vector > graph[3000]; //adjacency list representation +vector times; +vector delivery; +char grid[55][55]; + +void dijkstra(int s){ + priority_queue, vector >, greater > > pq; + pq.push(make_pair(0, s)); + memset(dist, -1, sizeof(dist)); + while(!pq.empty()){ + pair w = pq.top(); + pq.pop(); + if (dist[w.second]!=-1) + continue; + dist[w.second] = w.first; + for (int i = 0; i < graph[w.second].size(); i++){ + pair w1 = graph[w.second][i]; + pq.push(make_pair(w.first + w1.first, w1.second)); + } + } +} + +int minimize(int n2, int bitmask){ + if(n2 == ND){ + int boy1 = 0, boy2 = 0, m1 = -1, m2 = -1; + for(int i = 0; i < ND; i++){ + if(bitmask&(1<>grid[i]; + } + node = 0, start = -1, N = n*m; + for(int i = 0; i < m; i++){ + for(int j = 0; j < n; j++){ + if(grid[i][j] == '$') + delivery.push_back(node); + else if(grid[i][j] == 'X') + start = node; + for(int k = 0; k < 4; k++){ + a = i + x[k], b = j + y[k], node2 = node + y[k] + x[k]*n; + if(a < 0 || b < 0 or a >= m || b >= n) + continue; + if (grid[i][j]=='$' or grid[i][j]=='X')//moving from a building + graph[node].push_back(make_pair(2, node2)); + else if(grid[a][b] == '$' or grid[a][b] == 'X')//moving to a building + graph[node].push_back(make_pair(2, node2)); + else if(abs((grid[i][j] - '0') - (grid[a][b] - '0')) == 0)//moving to same height + graph[node].push_back(make_pair(1,node2)); + else if(abs((grid[i][j] - '0') - (grid[a][b] - '0')) == 1)//moving to height +-1 + graph[node].push_back(make_pair(3,node2)); + } + node++; + } + } + + ND = delivery.size(); + if(ND == 0 or start == -1){ + delivery.clear(); + times.clear(); + for(int i = 0; i < N; i++){ + graph[i].clear(); + } + cout<<0< +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + int count; + void assignLeaf(int value) { + count += value; + } + + void merge(node& left, node& right) { + count = left.count + right.count; + } +}; + +int n, array[30000], m , X, Y, indx = 0, result[200000], i; +map last_pos; +pair, int> q[200000]; +node tree[70001]; + + +// l and r represent the range node i is responsible for. A single call init(1, 0, n-1) builds the segment tree in O(N) time +void init(int i, int l, int r){ + if(l == r){ + tree[i].assignLeaf(array[l]); + } + else{ + init(2*i, l, (l+r)/2); + init((2*i) + 1, ((l+r)/2) + 1, r); + node left = tree[2*i], right = tree[(2*i)+1]; + + tree[i].merge(left, right); //Merge the children into the parent + } + return; +} + +// O(log N) +node query(int i, int l, int r, int lo, int hi){ + if(l == lo and r == hi) //If the range we want to query is the exact range + return tree[i]; + int mid = (l+r)/2; + if(lo > mid) //If only the subtree rooted at right child is repsonsible for this range + return query((2*i)+1, mid+1, r, lo, hi); + if(hi <= mid) //If only the subtree rooted at the left child is responsible for this range + return query(2*i, l, mid, lo, hi); + //If both, the left and right children are partially responsible for the range + node left = query(2*i, l, mid, lo, mid); + node right = query((2*i)+1, mid+1, r, mid+1, hi); + node result; + + result.merge(left, right); //Merge the results of the children into the parent + return result; +} + +//Call using update(1, 0, N-1, index, value) for a point update in O(log N) +void update(int i, int lo, int hi, int index, int value){ + if (lo == hi){ + tree[i].assignLeaf(value); + return; + } + int left = 2*i, right = left+1, mid = (lo+hi)/2; + if(index <= mid) + update(left, lo, mid, index, value); + else + update(right, mid+1, hi, index, value); + tree[i].merge(tree[left], tree[right]); + return; +} + +int main(){ + sd(n); + for(i = 0; i < n; i++) sd(array[i]); + + sd(m); + for(i = 0; i < m; i++){ + sd2(X,Y); + q[i] = make_pair(make_pair(Y-1,X-1),i); + } + + sort(q, q+m); + + for(i = 0; i < n; i++){ + update(1, 0, n-1, i, 1); + + if(last_pos.find(array[i]) != last_pos.end()) update(1, 0, n-1, last_pos[array[i]], -1); + + last_pos[array[i]] = i; + + while(indx < m and q[indx].first.first == i){ + result[q[indx].second] = query(1, 0, n-1, q[indx].first.second, q[indx].first.first).count; + indx++; + } + + } + + for(i = 0; i < m; i++) + printf("%d\n",result[i]); + + return 0; +} diff --git a/spoj_cpp_clean/DQUERY/DQUERY-9.cpp b/spoj_cpp_clean/DQUERY/DQUERY-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be1fb8787803945fcd3aadff36fc114b838a1c2c --- /dev/null +++ b/spoj_cpp_clean/DQUERY/DQUERY-9.cpp @@ -0,0 +1,140 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#define gc() getchar_unlocked() +#define LIM 30007 + +using namespace std; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +typedef struct +{ + int primary; + bool type; + int secondary; +}my_pair; + +bool comp(const my_pair &a, const my_pair &b) +{ + if(a.primary == b.primary) + return a.type; + return a.primary < b.primary; +} + +int N, M; +vector A, Q; +int BIT[LIM]; +unordered_map< int, unordered_map > memo; +int prevv[1000007]; + +inline void point_update(int pos, int val) +{ + for(; pos <= N; pos += pos & (-pos)) + BIT[pos] += val; +} + +inline int point_query(int pos) +{ + int sum = 0; + + for(; pos > 0; pos -= pos & (-pos)) + sum += BIT[pos]; + + return sum; +} + +inline int range_query(int left, int right) +{ + return point_query(right) - point_query(left - 1); +} + +inline void init() +{ + my_pair temp; + int i; + + N = FAST_IO(); + + //scanf("%d", &N); + + temp.type = 1; + for(i=1; i <= N; i++) + { + //scanf("%d", &temp.secondary); + temp.secondary = FAST_IO(); + temp.primary = i; + A.push_back(temp); + } + + M = FAST_IO(); + //scanf("%d", &M); + + temp.type = 0; + for(i=0; i < M; i++) + { + temp.secondary = FAST_IO(); + temp.primary = FAST_IO(); + //scanf("%d %d", &temp.secondary, &temp.primary); + A.push_back(temp); + Q.push_back(temp); + } + + sort(A.begin(), A.end(), comp); + + for(i=0; i < A.size(); i++) + { + if(A[i].type) + { + if(prevv[A[i].secondary] != 0) + point_update(prevv[A[i].secondary], -1); + + point_update(A[i].primary, 1); + prevv[A[i].secondary] = A[i].primary; + } + + else + memo[A[i].secondary][A[i].primary] = range_query(A[i].secondary, A[i].primary); + } +} + +int main() +{ + int i; + + //freopen("input.txt","r",stdin); + + init(); + + for(i=0; i < Q.size(); i++) + printf("%d\n", memo[Q[i].secondary][Q[i].primary]); + + return 0; +} diff --git a/spoj_cpp_clean/DRAWIT/DRAWIT-9.cpp b/spoj_cpp_clean/DRAWIT/DRAWIT-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9493a25a201433d1f492bec47cdcb30abfff7572 --- /dev/null +++ b/spoj_cpp_clean/DRAWIT/DRAWIT-9.cpp @@ -0,0 +1,96 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define MAXN 128 +using namespace std; + +int deg[MAXN]; +int N, E; + +inline void init() +{ + int i, f, s, w; + + memset(deg, 0, sizeof(deg)); + + cin >> N >> E; + + for (i = 0; i < E; i++) + { + cin >> f >> s >> w; + deg[f] += w; + deg[s] += w; + } +} + +inline int solve() //0 if not, 1 if all even, 2 if 2 odds +{ + int i, c = 0; + + for (i = 1; i <= N; i++) + if (deg[i] % 2) + c++; + + if (!c) + return 1; + if (c == 2) + return 2; + + return 0; +} + +inline void print(int c) +{ + if (c == 1) + { + cout << "1\n"; + return; + } + + int i; + + for (i = 1; i <= N; i++) + if (deg[i] % 2) + { + cout << i << "\n"; + return; + } +} + +int main() +{ + ios::sync_with_stdio(0); + + int T, c; + +// freopen("input.txt", "r", stdin); + + cin >> T; + + while (T--) + { + init(); + c = solve(); + + if (!c) + cout << "NO\n"; + else + { + cout << "YES "; + print(c); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/DRAWM/DRAWM-9.cpp b/spoj_cpp_clean/DRAWM/DRAWM-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b0bc4be6e94f6c99a6c22e7704aca71bb310068 --- /dev/null +++ b/spoj_cpp_clean/DRAWM/DRAWM-9.cpp @@ -0,0 +1,126 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define pc(x) putchar(x) +using namespace std; + +int N, MAX; +char board[37][77]; +int A[77]; + +inline void print(int curr) +{ + int i; + + for (i = 0; i < N; i++) + { + if (board[curr][i]) + pc(board[curr][i]); + else + pc(' '); + } + + pc('\n'); +} + +inline bool init() +{ + int i; + + scanf("%d", &N); + + if (N == -1) + return 0; + + memset(board, 0, sizeof(board)); + + for (i = 0; i <= N; i++) + scanf("%d", &A[i]); + + + return 1; +} + +inline void solve() +{ + int i, curr; + bool pos; //0 down, 1 up + + curr = A[0]; + pos = 0; + MAX = 0; + + for (i = 1; i <= N; i++) + { + if (A[i] > A[i - 1]) + { + if (!pos) + { + board[curr][i - 1] = '/'; + pos = 1; + } + + else + { + curr++; + board[curr][i - 1] = '/'; + } + } + + else if (A[i] < A[i - 1]) + { + if (!pos) + { + curr--; + board[curr][i - 1] = '\\'; + } + + else + { + board[curr][i - 1] = '\\'; + pos = 0; + } + } + + else + { + if (!pos) + board[curr][i - 1] = '_'; + else + { + curr++; + board[curr][i - 1] = '_'; + pos = 0; + } + } + + MAX = max(MAX, curr); + } + + for (i = MAX; i >= 0; i--) + print(i); + printf("***\n"); +} + +int main() +{ + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + + while (init()) + solve(); + + return 0; +} diff --git a/spoj_cpp_clean/DSH/DSH-9.cpp b/spoj_cpp_clean/DSH/DSH-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..773564ada0a079168cd72a90ed365079eb419016 --- /dev/null +++ b/spoj_cpp_clean/DSH/DSH-9.cpp @@ -0,0 +1,46 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int val[] = { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 20, 21, 22, 23, 24, 25, 26, 18, 19, 20, 21, 22, 23 }; + +int main() +{ + string S; + int i; + +// freopen("input.txt", "r", stdin); + + while (cin >> S) + { + int c = 0; + + for (i = 0; i < S.length(); i++) + { + printf("%d ", val[S[i] - 'a']); + if (val[S[i] - 'a'] > 23) + c++; + } + putchar('\n'); + + if (c) + printf("%d\n", c); + else + printf("no one.\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/DSUBSEQ/DSUBSEQ-3.cpp b/spoj_cpp_clean/DSUBSEQ/DSUBSEQ-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..650230e59cf1cad109a4ef0a920aa6acd60272e7 --- /dev/null +++ b/spoj_cpp_clean/DSUBSEQ/DSUBSEQ-3.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, last[26], MOD = 1000000007, l; + long long dp[100001]; + dp[0] = 1; + string s; + sd(t); + while(t--){ + cin>>s; + l = s.length(); + for(int i = 0; i < 26; i++) + last[i] = -1; + for(int i = 1; i <= l; i++){ + dp[i] = (dp[i-1]*2)%MOD; + if(last[s[i-1] - 'A'] >= 0) + dp[i] = (dp[i] - dp[last[s[i-1] - 'A'] - 1] + MOD)%MOD; + last[s[i-1] - 'A'] = i; + } + printf("%lld\n",dp[l]); + } + return 0; +} + + diff --git a/spoj_cpp_clean/DWARFLOG/DWARFLOG-9.cpp b/spoj_cpp_clean/DWARFLOG/DWARFLOG-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb98a377e1dba648e82bc6a07a3dcab0c42e2422 --- /dev/null +++ b/spoj_cpp_clean/DWARFLOG/DWARFLOG-9.cpp @@ -0,0 +1,134 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar_unlocked(); + +using namespace std; + +pair MINMAX[4*200007]; +int N, M; +int B[200007]; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +void init(int index, int a, int b) +{ + if(a == b) + { + MINMAX[index].first = MINMAX[index].second = a; + return; + } + + init(index*2, a, (a+b)/2); + init(index*2+1, (a+b)/2+1, b); + + MINMAX[index].first = min(MINMAX[index*2].first, MINMAX[index*2+1].first); + MINMAX[index].second = max(MINMAX[index*2].second, MINMAX[index*2+1].second); +} + +void update(int index, int a, int b, int pos, int val) +{ + if(a == b && a == pos) + { + MINMAX[index].first = MINMAX[index].second = val; + return; + } + + if(pos <= (a+b)/2) + update(index*2, a, (a+b)/2, pos, val); + else + update(index*2+1, (a+b)/2+1, b, pos, val); + + MINMAX[index].first = min(MINMAX[index*2].first, MINMAX[index*2+1].first); + MINMAX[index].second = max(MINMAX[index*2].second, MINMAX[index*2+1].second); +} + +pair query(int index, int a, int b, int i, int j) +{ + if(a == i && b == j) + return MINMAX[index]; + + if(j <= (a+b)/2) + return query(index*2, a, (a+b)/2, i, j); + else if(i > (a+b)/2) + return query(index*2+1, (a+b)/2+1, b, i, j); + else + { + pair left, right; + left = query(index*2, a, (a+b)/2, i, (a+b)/2); + right = query(index*2+1, (a+b)/2+1, b, (a+b)/2+1, j); + + return make_pair(min(left.first, right.first), max(left.second, right.second)); + } +} + +int main() +{ + int i, f, s, t, j; + pair temp; + + //freopen("input.txt","r",stdin); + N = FAST_IO(); + M = FAST_IO(); + + //scanf("%d %d", &N, &M); + + for(i=1; i<=N; i++) //B THESI A NANOI + B[i] = i; + init(1, 1, N); + + for(i=0; i +#define mp make_pair +#define pb push_back + +using namespace std; + +typedef pair ii; + +typedef long long int64; + +int64 __lcm(int64 a, int64 b) { return (a * (b / __gcd(a, b))); } + +int pos[6], memo[6]; +int64 n, m; + +int64 solve(int at, int mask) +{ + if (mask == 32) + return memo[at]; + int b = __builtin_popcount(mask); + if (b == at) { + int64 l = 1; + for (int i = 0; i < 5; i++) { + if (mask & (1 << i)) + l = __lcm(l, pos[i]); + } + memo[at] += (m / l - n / l); + } + return solve(at, mask + 1); +} + +int main() +{ + ios::sync_with_stdio(false); + int t; + cin >> t; + int64 a, d; + while (t--) { + for (int i = 0; i < 6; i++) + memo[i] = 0; + cin >> n >> m >> a >> d; + n--; + pos[0] = a; + pos[1] = a + d; + pos[2] = a + 2 * d; + pos[3] = a + 3 * d; + pos[4] = a + 4 * d; + + int64 ans = 0; + + for (int i = 0; i < 5; i++) + ans += (m / pos[i] - n / pos[i]); + + for (int i = 1; i <= 4; i++) { + if (i & 1) + ans -= solve(i + 1, 3); + else + ans += solve(i + 1, 3); + } + + cout << (m - n) - ans << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/EASYPIE/EASYPIE-13.cpp b/spoj_cpp_clean/EASYPIE/EASYPIE-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee1ea5b8e77f0fe0873d7c184d2550e0bec41b39 --- /dev/null +++ b/spoj_cpp_clean/EASYPIE/EASYPIE-13.cpp @@ -0,0 +1,81 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +struct problem{ + char id; + int ac, time, sub; + problem(): + ac(0), time(0), sub(0){} +}; + +int submissions[62][10]; +bool mark[62][10]; +char buffer[100]; + +int main(){ + int T; scanf("%d", &T); + for(int t = 0; t v(9); for(int i=0; i<9; ++i)v[i].id = 'A'+i; + memset(submissions, 0 ,sizeof(submissions)); + memset(mark, false, sizeof(mark)); + map teams; + for(int i=0; i::iterator it = teams.find(name); + if(it!=teams.end())team = it->second; + else team = teams.size(), teams[name] = team; + submissions[team][k]++; + if(ans == 'A' && !mark[team][k]){ + mark[team][k] = true; + v[k].ac++; + v[k].time += time; + v[k].sub += submissions[team][k]; + } + } + for(int i=0; i<9; ++i){ + printf("%c %d",v[i].id, v[i].ac); + if(v[i].ac > 0){ + double avgSub = v[i].sub / (double)v[i].ac; + double avgTime = v[i].time / (double)v[i].ac; + printf(" %.2lf %.2lf\n",avgSub, avgTime); + } + else printf("\n"); + } + } + return 0; +} diff --git a/spoj_cpp_clean/EASYPROB/EASYPROB-5.cpp b/spoj_cpp_clean/EASYPROB/EASYPROB-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13a096c17ca6c2592d102ef2a1946f06e3aa82c5 --- /dev/null +++ b/spoj_cpp_clean/EASYPROB/EASYPROB-5.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main() +{ + cout<<"137=2(2(2)+2+2(0))+2(2+2(0))+2(0)"< +#include +using namespace std; + +int main() { + int T; cin >> T; + while (T--) { + int a; cin >> a; + if (a%2) { + cout << a << endl; + continue; + } + + vector bits; + bool mostRight1Found = false; + for (int b = 31; b >= 0; b--) { + if (mostRight1Found) + bits.push_back(a & (1 << b)); + else if (a & (1 << b)) { + mostRight1Found = true; + bits.push_back(1); + } + } + + int ans = 0; + for (int b = 0; b < bits.size(); b++) + if (bits[b]) + ans |= (1 << b); + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/EC_P/EC_P-8.cpp b/spoj_cpp_clean/EC_P/EC_P-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef82a3061230607b5b713ec1025b2c91dab528ca --- /dev/null +++ b/spoj_cpp_clean/EC_P/EC_P-8.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#define pii pair +#define MAX 705 +using namespace std; +int n, m,timer; +vectoradj[MAX]; +vectorans; +mapmp; +bool vis[MAX]; +int dis[MAX], low[MAX]; +bool cmp(pii a, pii b){ + if(a.first == b.first) return a.second < b.second; + else return a.first < b.first; +} +void dfs(int u, int p){ + vis[u] = true; + dis[u] = low[u] = timer++; + for(int i = 0; i<(int)adj[u].size(); i++){ + int v = adj[u][i]; + if(v == p) continue; + if(vis[v]){ + low[u] = min(low[u], dis[v]); + }else{ + dfs(v, u); + low[u] = min(low[u], low[v]); + if(dis[u] < low[v]){ + if(u > v && !mp[pii(v, u)]){ + ans.push_back(pii(v, u)); + mp[pii(v,u)] = true; + }else if(!mp[pii(u, v)]){ + ans.push_back(pii(u, v)); + mp[pii(u,v)] = true; + } + } + } + } +} +void find_AP(){ + timer = 0; + memset(vis, false, sizeof(vis)); + memset(dis, -1, sizeof(dis)); + memset(low, -1, sizeof(low)); + ans.clear(); + mp.clear(); + for(int i = 1; i<=n; i++) if(!vis[i]) dfs(i, -1); + for(int i = 0; i> tc; + for(int t =1; t<=tc; t++){ + cin >> n >> m; + for(int i = 0; i> u >> v; + adj[u].push_back(v); + adj[v].push_back(u); + } + find_AP(); + cout << "Caso #"< + +using namespace std; + +int dp[2009][2009]; + +void solve(){ + string a,b; + cin>>a>>b; + int n = a.length(); + int m = b.length(); + memset(dp,0,sizeof(dp[0][0]*(2009)*(2009))); + for(int i=0;i>test_cases; + while(test_cases--){ + solve(); + } +} diff --git a/spoj_cpp_clean/EDIST/EDIST-21.cpp b/spoj_cpp_clean/EDIST/EDIST-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f354378900f7ab061fcc655dcf5c87a0550977cb --- /dev/null +++ b/spoj_cpp_clean/EDIST/EDIST-21.cpp @@ -0,0 +1,47 @@ +/* + dynamic programming > edit distance + difficulty: none + date: none + by: @brpapa +*/ +#include +#include +using namespace std; +#define min3(a, b, c) ((a)<(b) ? min(a, c):min(b, c)) + +int memo[2020][2020]; +string a, b; + +int edTD(int i, int j) { + //estado: letras a[i] e b[j] + + if (i == -1) + return j+1; //insere todas as letras restantes de b em a + if (j == -1) + return i+1; //insere todas as letras restantes de a em b + + int &m = memo[i][j]; + if (m != -1) return m; + + if (a[i] == b[j]) + return m = edTD(i-1, j-1); + + return m = 1 + min3( + edTD(i-1, j), //deleta a[i] + edTD(i, j-1), //insere b[j] em a[i] + edTD(i-1, j-1) //substitui a[i] por b[j] + ); +} + +int main() { + int T; + cin >> T; + + while (T--) { + cin >> a >> b; + + memset(memo, -1, sizeof(memo)); + cout << edTD(a.size()-1, b.size()-1) << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/EDIST/EDIST-3.cpp b/spoj_cpp_clean/EDIST/EDIST-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22bcbbc7b94b0cf3e51184b4bfd6d0fad0ee8528 --- /dev/null +++ b/spoj_cpp_clean/EDIST/EDIST-3.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t,temp, dp[2][2001], l1, l2, indx; + + string a, b; + sd(t); + while(t--){ + cin>>a; + cin>>b; + l1 = a.length(); + l2 = b.length(); + + indx = 0; + + temp = min(l1,l2); + l1 = max(l1,l2); + l2 = temp; + for(int i = 0; i <= l2; i++){ + dp[0][i] = i; + } + for(int i = 1; i <= l1; i++){ + indx = 1-indx; + for(int j = 0; j <= l2; j++){ + if(j == 0){ + dp[indx][j] = i; + } + else if(a[i-1] == b[j-1]){ + dp[indx][j] = dp[1-indx][j-1]; + } + else{ + dp[indx][j] = min(min(dp[1-indx][j],dp[indx][j-1]),dp[1-indx][j-1]) + 1; + } + } + } + printf("%d\n",dp[indx][l2]); + + } + return 0; +} diff --git a/spoj_cpp_clean/EDIST/EDIST-5.cpp b/spoj_cpp_clean/EDIST/EDIST-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c3774439fc0c95665b167c16455b823745d13bb --- /dev/null +++ b/spoj_cpp_clean/EDIST/EDIST-5.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +using namespace std; + +vector< vector > dp; + +void fillTable(vector< vector >&dp,string a, string b, int aLen, int bLen) +{ + int i,j,k,diff; + for(i=0;i<=aLen;i++) + dp[i][0]=i; + for(j=0;j<=bLen;j++) + dp[0][j]=j; + for(i=1;i<=aLen;i++) + { + for(j=1;j<=bLen;j++) + { + if(a[i-1]==b[j-1]) + diff=0; + else + diff=1; + dp[i][j]=min(min(dp[i-1][j]+1,dp[i][j-1]+1),dp[i-1][j-1]+diff); + } + } +} + +int main() +{ + string a,b; + int test,i,j,k,aLen,bLen; + cin>>test; + while(test--) + { + cin>>a>>b; + aLen=a.length(); + bLen=b.length(); + dp.resize(aLen+1, vector(bLen+1)); + fillTable(dp,a,b,aLen,bLen); + cout< +#include +using namespace std; +const int N=2005; +int a[N][N]; +void makezero(int x, int y) +{ + for(int i=0;i<=x;i++) + for(int j=0;j<=y;j++) + a[i][j]=0; +} +int min(int x,int y, int z) +{ + if(x<=y && x<=z) + return x; + if(y<=x && y<=z) + return y; + return z; +} + +int main() +{ + string s1,s2; + int t,k,n,m,i,j,b,c,d; + cin>>t; + for(k=1;k<=t;k++) + { + cin>>s1>>s2; + n=s1.size(); + m=s2.size(); + makezero(n,m); + for(i=0;i<=n;i++) + a[i][0]=i; + for(i=0;i<=m;i++) + a[0][i]=i; + s1='_'+s1; + s2='_'+s2; + for(i=1;i<=n;i++) + for(j=1;j<=m;j++) + { + if(s1[i]==s2[j]) + a[i][j]=min(a[i-1][j]+1,a[i][j-1]+1,a[i-1][j-1]); + else + a[i][j]=min(a[i-1][j]+1,a[i][j-1]+1,a[i-1][j-1]+1); + + } + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +int DP[3005][3005]; +string A, B; + +void init() +{ + char c; + + A.clear(); + B.clear(); + + c=fgetc(stdin); + while(!isalpha(c)) + c=fgetc(stdin); + + A.push_back(c); + + c=fgetc(stdin); + while(isalpha(c)) + { + A.push_back(c); + c=fgetc(stdin); + } + + c=fgetc(stdin); + while(!isalpha(c)) + c=fgetc(stdin); + + B.push_back(c); + + c=fgetc(stdin); + while(isalpha(c)) + { + B.push_back(c); + c=fgetc(stdin); + } + +} + +void clear() +{ + int i, j; + + for(i=0; i<=A.length(); i++) + for(j=0; j<=B.length(); j++) + DP[i][j]=0; +} + +int main() +{ + int T, i, j; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + { + init(); + clear(); + + for(i=1; i<=A.length(); i++) + DP[i][0]=i; + + for(j=1; j<=B.length(); j++) + DP[0][j]=j; + + for(j=1; j<=B.length(); j++) + for(i=1; i<=A.length(); i++) + { + if(A[i-1]==B[j-1]) + DP[i][j]=DP[i-1][j-1]; + else + DP[i][j]= min(DP[i-1][j] + 1, min(DP[i][j-1] + 1, DP[i-1][j-1] +1)); + } + + /* for(j=0; j<=B.length(); j++) + { + for(i=0; i<=A.length(); i++) + printf("%d ", DP[i][j]); + printf("\n"); + }*/ + printf("%d\n", DP[A.length()][B.length()]); + } + + return 0; +} diff --git a/spoj_cpp_clean/EGYPIZZA/EGYPIZZA-4.cpp b/spoj_cpp_clean/EGYPIZZA/EGYPIZZA-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c56408e83a469f1b1e1e36dc50ff58d2b4eb7d64 --- /dev/null +++ b/spoj_cpp_clean/EGYPIZZA/EGYPIZZA-4.cpp @@ -0,0 +1,164 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + int half=0, three=0, quat=0; + s(n); + string s; + FOR(i,0,n) + { + cin>>s; + if(s[2]=='2') + half++; + else if(s[0]=='1') + quat++; + else + three++; + } + int ans=0; + if(quat==0) + { + ans=three+half/2+half%2; + } + else if(quat<=three) + { + ans=three+half/2+half%2; + } + else if(quat>three) + { + ans=three; + quat-=three; + int q2=quat/2; + quat%=2; + if(q2==half) + { + ans+=half; + ans+=quat; + } + else if(q2>half) + { + ans+=half; + q2-=half; + ans+=q2/2; + if(q2%2!=0 || quat==1) + ans++; + } + else // q2 + +using namespace std; + +int main(void) +{ + int t; + unsigned long long k; + + cin >> t; + + while(t--) + { + cin >> k; + + if(k % 4ull) + { + if(k / 4ull) + cout << (k / 4ull); + + k = k % 4ull; + + if(k == 1ull) + cout << "192" << endl; + if(k == 2ull) + cout << "442" << endl; + if(k == 3ull) + cout << "692" << endl; + } + else + { + k = k / 4ull; + k--; + + if(k) + cout << k; + + cout << "942" << endl; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/EIGHTS/EIGHTS-14.cpp b/spoj_cpp_clean/EIGHTS/EIGHTS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f165df3b4d57236eb53170dab6229a5429c8ffdf --- /dev/null +++ b/spoj_cpp_clean/EIGHTS/EIGHTS-14.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +#define ll long long +ll pat[] = {192, 442, 692, 942}; + +int main() + +{ + ll t; + cin >> t; + while(t--) { + ll k; + scanf("%lld", &k); + ll num = (k - 1) % 4; + ll mult = (k - 1) / 4; + printf("%lld\n", mult * 1000 + pat[num]); + } + + return 0; diff --git a/spoj_cpp_clean/EIGHTS/EIGHTS-16.cpp b/spoj_cpp_clean/EIGHTS/EIGHTS-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd3353e76f1c77987a19f2f14e2e67cffb5529ce --- /dev/null +++ b/spoj_cpp_clean/EIGHTS/EIGHTS-16.cpp @@ -0,0 +1,46 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "< +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(){ + ll k; + cin>>k; + ll ans = 192 + (k-1)*250; + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/EIGHTS/EIGHTS-19.cpp b/spoj_cpp_clean/EIGHTS/EIGHTS-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c20395cd56bc9bf68754a43b9681cc1c55e5f03 --- /dev/null +++ b/spoj_cpp_clean/EIGHTS/EIGHTS-19.cpp @@ -0,0 +1,43 @@ +#include +#pragma GCC optimize("Ofast") +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 1000001 +#define PI 3.14159265 + +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + + ll times; cin >> times; while (times--) { + unsigned ll k; + cin >> k; + ll b, a; + a = k / 4; + b = k % 4; + if (b == 1) { + b = 192; + } + else if (b == 2) { + b = 442; + } + else if (b == 3) { + b = 692; + } + else { + b = 942; + a--; + } + if (a == 0) { + cout << b << endl; + } + else { + cout << a << b << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/EIGHTS/EIGHTS-21.cpp b/spoj_cpp_clean/EIGHTS/EIGHTS-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10bd457d39d3c070ade2f5bf16e803c332dcf0b3 --- /dev/null +++ b/spoj_cpp_clean/EIGHTS/EIGHTS-21.cpp @@ -0,0 +1,18 @@ +/* + math > ad-hoc > finding pattern + difficulty: easy + date: 21/Feb/2020 + by: @brpapa +*/ +#include +#define ll long long +using namespace std; + +int main() { + int T; cin >> T; + while (T--) { + ll k; cin >> k; k--; + ll ans = (192+250*(k%4)) + (k/4)*1000; + cout << ans << endl; + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/EIGHTS/EIGHTS-5.cpp b/spoj_cpp_clean/EIGHTS/EIGHTS-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..996479a60ba15ec1d3f4d097988194bc9040fdf8 --- /dev/null +++ b/spoj_cpp_clean/EIGHTS/EIGHTS-5.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main() +{ + long long int test,num,ans; + cin>>test; + while(test--) + { + cin>>num; + ans=192+(num-1)*250; + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +int main() +{ + int T, i; + unsigned long long res, num; + + scanf("%d", &T); + + while(T--) + { + scanf("%lld", &num); + printf("%lld\n", 192+(num-1)*250); + } + + + return 0; +} diff --git a/spoj_cpp_clean/EIUASSEMBLY/EIUASSEMBLY-14.cpp b/spoj_cpp_clean/EIUASSEMBLY/EIUASSEMBLY-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be322e310d189b6c84c12ace05d83bebfab55964 --- /dev/null +++ b/spoj_cpp_clean/EIUASSEMBLY/EIUASSEMBLY-14.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +#define ll long long +#define pi first +#define mi second +#define pll pair +pll r[100009]; + +int main() + +{ + ll t; + cin >> t; + while(t--) + { + ll n, m; + cin >> n >> m; + for(ll i = 1; i <= n; i++) + { + scanf("%lld %lld", &r[i].pi, &r[i].mi); + } + + ll lo = 1, hi = 1e18, ans; + while(lo <= hi) + { + ll mid = lo / 2 + hi / 2; + if(lo&1 && hi&1) mid++; + + ll b = 0, chk = 0; + for(ll i = 1; i <= n; i++) + { + if(r[i].pi >= mid) + continue; + + ll maxPossible = m / r[i].mi; + if((mid - r[i].pi) > maxPossible) { + b = 1e18; + break; + } + + b += (mid - r[i].pi) * r[i].mi; + } + + if(b > m) + hi = mid - 1; + else + { + ans = mid; + lo = mid + 1; + } + } + + cout << ans << endl; + } + + return 0; diff --git a/spoj_cpp_clean/EKO/EKO-3.cpp b/spoj_cpp_clean/EKO/EKO-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..923a90ec4e8da421990a9db7807e03ff64a872c5 --- /dev/null +++ b/spoj_cpp_clean/EKO/EKO-3.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int trees[1000000]; +long long n, m; +long long check(long long h){ + long long total = 0; + for(long long i = 0; i < n; i++){ + if(trees[i] > h) + total += (trees[i] - h); + } + return total; +} + +int main(){ + long long res; + scanf("%lld %lld",&n,&m); + long long max = 0; + for(long long i = 0; i < n; i++){ + sd(trees[i]); + if(trees[i] > max) + max = trees[i]; + } + long long low = 0, high = max, mid, height = 0; + while(low <= high){ + mid = (low+high)/2; + res = check(mid); + if(res > m){ + low = mid + 1; + if(mid > height) + height = mid; + } + else if(res < m) + high = mid - 1; + else{ + height = mid; + break; + } + } + cout< + +using namespace std; + +long long int n,x,aux,maxi; +long long int v[1000000]; + +long long int chop(long long int tam, long long int size) +{ + long long int result=0; + for (int i = 0; i < tam; ++i) + { + if (v[i]>size) + { + result= result + (v[i]-size); + } + } + return result; +} + +long long int max(long long int x, long long int y) +{ + if (x>y) + { + return x; + }else + return y; +} + +long long int Bs(long long int low,long long int high,long long int key) +{ + long long int mid=0,ans=0; + while(low>1); + if (chop(n,mid)>=key) + { + low= mid+1; + ans= max(mid,ans); + }else + { + high= mid; + } + } + return ans; +} + + + +int main() +{ + scanf("%lld %lld",&n,&x); + maxi=0; + memset(v,0,sizeof(v)); + for (int i = 0; i < n; ++i) + { + scanf("%lld",&aux); + v[i]=aux; + if (maxi +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); +#define MAX 20003 + +using namespace std; + +long long bit[101][MAX]; +int num[100]; + +long long get(int tnum, int indx){ + long long sum = 0; + while(indx > 0){ + sum += bit[tnum][indx]; + indx -= (indx & -indx); + } + return sum; +} + +void update(int tnum, int indx, long long val){ + while(indx < MAX){ + bit[tnum][indx] = (bit[tnum][indx] + val); + indx += (indx & -indx); + } + return; +} + +int main(){ + int n, k; + sd2(n,k); + while(n != 0 or k != 0){ + for(int i = 0; i < n; i++) sd(num[i]); + + memset(bit, 0, sizeof(bit)); + + for(int i = 0; i < n; i++){ + update(0, num[i]+10001, 1); + for(int j = 1; j < k; j++){ + update(j, num[i]+10001, get(j-1, num[i]+10000)); + } + } + printf("%lld\n",get(k-1,MAX-1)); + sd2(n,k); + } +} diff --git a/spoj_cpp_clean/ELEICOES/ELEICOES-10.cpp b/spoj_cpp_clean/ELEICOES/ELEICOES-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0672725f1457d9e03dac35c452c1c8075f69781f --- /dev/null +++ b/spoj_cpp_clean/ELEICOES/ELEICOES-10.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +int main() +{ + map votos; + int n, c, bc = 0; + cin >> n; + while(n-- > 0) + { + cin >> c; + votos[c]++; + if(bc < votos[c]) + bc = c; + } + cout << bc; + return 0; +} diff --git a/spoj_cpp_clean/ELEVTRBL/ELEVTRBL-9.cpp b/spoj_cpp_clean/ELEVTRBL/ELEVTRBL-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f47d9e647540d8fc269402da511c52fab2bb8b17 --- /dev/null +++ b/spoj_cpp_clean/ELEVTRBL/ELEVTRBL-9.cpp @@ -0,0 +1,69 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +deque< pair > WORK; +bool V[1000001]; + +int main() +{ + int f, s, g, u, d; + pair curr; + + //freopen("input.txt","r",stdin); + scanf("%d %d %d %d %d", &f, &s, &g, &u, &d); + + WORK.push_back(make_pair(s, 0)); + + if(s==g) + { + printf("0\n"); + return 0; + } + + while(!WORK.empty()) + { + curr=WORK.front(); + WORK.pop_front(); + + if(V[curr.first]) + continue; + + V[curr.first]=1; + + if(curr.first + u == g) + { + printf("%d\n", curr.second+1); + return 0; + } + + if(curr.first - d == g) + { + printf("%d\n", curr.second+1); + return 0; + } + + + if(curr.first + u <= f && !V[curr.first+u]) + WORK.push_back(make_pair(curr.first+u, curr.second+1)); + + if(curr.first - d >= 1 && !V[curr.first-d]) + WORK.push_back(make_pair(curr.first-d, curr.second+1)); + } + + printf("use the stairs\n"); + + return 0; +} diff --git a/spoj_cpp_clean/ELIS/ELIS-17.cpp b/spoj_cpp_clean/ELIS/ELIS-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..346354b99d6460e6e5a40a40a4747604a0d108ef --- /dev/null +++ b/spoj_cpp_clean/ELIS/ELIS-17.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +////typedef long long ll; +#define int long long +///---------------- by:Ahmed Sakr (sakr_) with AZA ------------------/// +////const int M=(int) 1e9+7; + +int n; +vector arr; +int dp[15][15]; +int solve(int i,int prev) { + /// base case + if(i>n) + return 0; + + int &ans = dp[i][prev]; + if(ans!=-1) return ans; + + ans = -10; + + if(arr[prev] < arr[i]){ + ans = max(ans, 1+solve(i+1,i)); + } + + ans = max(ans,solve(i+1,prev)); + + return ans; + +} + +void doIt() { + + + cin>>n; + arr = vector(n+5); + for(int i=1 ;i<=n;i++) { + cin>>arr[i]; + } + arr[0] =-10; + + memset(dp,-1,sizeof dp); + + cout<> t; + while (t--) { + doIt(); + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/EMOTICON/EMOTICON-13.cpp b/spoj_cpp_clean/EMOTICON/EMOTICON-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4755ce0d5fedcef282f41ef4aefa4f33abdad982 --- /dev/null +++ b/spoj_cpp_clean/EMOTICON/EMOTICON-13.cpp @@ -0,0 +1,99 @@ +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; + +char buffer[100]; +bool mark[90][90][2]; +int memo[90][90][2]; + +int f(vector &v, vector < vector > &table, string &s, int i, int start, bool ok){ + if(i >= (int)s.size())return 0; + + int &best = memo[i][start][ok?1:0]; + if(mark[i][start][ok?1:0])return best; + mark[i][start][ok?1:0] = true; + + //check if at position i an emoticon begins + bool begins = false; + for(int k=0, m=(int)table.size(); k getInfo(string &pat, string &s){ + vector res; + int last = 0, x; + while((x = s.substr(last, s.size() - last).find(pat)) != -1){ + res.push_back(x+last); + last = x+1+last; + } + return res; +} + +int processLine(vector &v, string s){ + vector < vector > table(v.size()); + bool ok = false; + for(int i=0, n=(int)v.size(); i v(n, ""); + for(int i=0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +int main() +{ + char str[10001], ch; + int i, j, k, len; + while(gets(str)) + { + len = strlen(str); + for(i=0; i +#include + +char CharToN(int c) +{ + switch(c) + { + case 'A': + case 'B': + case 'C': + return '2'; + case 'D': + case 'E': + case 'F': + return '3'; + case 'G': + case 'H': + case 'I': + return '4'; + case 'J': + case 'K': + case 'L': + return '5'; + case 'M': + case 'N': + case 'O': + return '6'; + case 'P': + case 'Q': + case 'R': + case 'S': + return '7'; + case 'T': + case 'U': + case 'V': + return '8'; + case 'W': + case 'X': + case 'Y': + case 'Z': + return '9'; + default: + return c; + } +} + +int main() +{ + using namespace std; + string n; + while(cin >> n) + { + for(int i=0; i +using namespace std; + +#ifdef cicada +#include "debug.h" +#else +#define debug(...) "Yume o akiramete shinde kure." +#endif + +#define int int64_t +#define endl "\n" +typedef pair pii; + +int const INF = 2e18; +int const mod = 1e9 +7; + + +signed main() +{ + string s; + while(getline(cin, s)){ + if(s.empty()) break; + + int n = s.size(); + vector d1(n, 0), d2(n, 0); + vector suffix(n+1, false); + + for(int i=0, l=0, r=-1; i r) ? 1 : min(d1[l + (r-i)], r-i); + while(i+k=0 and s[i+k] == s[i-k]) + k++; + d1[i] = k--; + if(i + k > r){ + l = i - k; + r = i + k; + } + + if(i + d1[i] == n) + suffix[ i - d1[i] +1] = true; + } + for(int i=0, l=0, r=-1; ir ? 0 : min(d2[l + (r-i+1)], r-i+1)) +1; + while(i+k-1 < n and i-k>=0 and s[i-k] == s[i+k-1]) + k++; + d2[i] = --k; + if(i + k -1 > r){ + l = i - k; + r = i + k -1; + } + + if(i + d2[i] == n) + suffix[i - d2[i]] = true; + } + + suffix[n] = true; + string t; + + for(int i=0; i<=n; i++){ + if(suffix[i]){ + t = s.substr(0, i); + break; + } + } + + reverse(t.begin(), t.end()); + cout << s + t << endl; + } + + return 0; +} diff --git a/spoj_cpp_clean/EQBOX/EQBOX-3.cpp b/spoj_cpp_clean/EQBOX/EQBOX-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..946cbefe1c1bee745534c069750807ad07d2ebe8 --- /dev/null +++ b/spoj_cpp_clean/EQBOX/EQBOX-3.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + + +bool check(long long a, long long b, long long x, long long y) +{ + if (x < a && y < b) return true; + if (x >= a && y >= b) return false; + double beta = atan2(y, x); + double alpha = asin(b / sqrt(x * x + y * y)) - beta; + if (alpha < 0) return false; + double p = y * sin(alpha), q = sqrt(x * x + y * y - b * b); + if (2 * p + q - a >= 0) return false; + else return true; +} +int main() +{ + int T, a, b, x, y; + scanf("%d", &T); + for (int ti = 0; ti < T; ++ti) { + scanf("%d%d%d%d", &a, &b, &x, &y); + if (check(max(a, b), min(a, b), max(x, y), min(x, y))) + puts("Escape is possible."); + else puts("Box cannot be dropped."); +} +} diff --git a/spoj_cpp_clean/ESYRCRTN/ESYRCRTN-9.cpp b/spoj_cpp_clean/ESYRCRTN/ESYRCRTN-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c09f9c75b31c29a3fb3d98b5eeabad25b3005a6f --- /dev/null +++ b/spoj_cpp_clean/ESYRCRTN/ESYRCRTN-9.cpp @@ -0,0 +1,32 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +int A[6] = { 0, 1, 4, 6, 5, 2}; + +int main() +{ + int T; + long long num; + + scanf("%d", &T); + + while (T--) + { + scanf("%lld", &num); + printf("%d\n", A[num % 6]); + } + + return 0; +} diff --git a/spoj_cpp_clean/ETF/ETF-16.cpp b/spoj_cpp_clean/ETF/ETF-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..395cf84ea14e0d36e10abcc861bc0b8d69548d36 --- /dev/null +++ b/spoj_cpp_clean/ETF/ETF-16.cpp @@ -0,0 +1,60 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "< +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(){ + int n; + cin>>n; + if(n==1 or n==2){ + cout<<1<<"\n"; + return; + } + int ans=n,i; + for(i=2;i*i<=n;i++){ + if(n%i==0){ + while(n%i==0) + n/=i; + ans-=ans/i; + } + } + if(n>1){ + ans-=ans/n; + } + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/ETF/ETF-19.cpp b/spoj_cpp_clean/ETF/ETF-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73059e2fd572c1ba95913fcdc88d77829c939838 --- /dev/null +++ b/spoj_cpp_clean/ETF/ETF-19.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +#define endl "\n" +#define int long long +#define mod 1000000007 //10^9 +7 +#define MAX 100001 //10^5 +1 +#define PI (3.141592653589) + +int32_t main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + int times; cin >> times; + while (times--) + { + int n; cin >> n; + int result = n; + for (int i = 2; i * i <= n; i++) { + if (n % i == 0) { + while (n % i == 0) { + n /= i; + } + result -= result / i; + } + } + if (n > 1) { + result -= result / n; + } + cout << result << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ETF/ETF-3.cpp b/spoj_cpp_clean/ETF/ETF-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62f7b7a49d8df5cd104ecc77026a8d4f2667ff7d --- /dev/null +++ b/spoj_cpp_clean/ETF/ETF-3.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int fi(int n){ + int result = n; + for(int i = 2; i*i <= n; i++){ + if(n%i == 0){ + result -= result/i; + while(n%i == 0) n /= i; + } + } + if(n > 1) result -= result/n; + return result; +} + + +int main(){ + int t, n; + sd(t); + while(t--){ + sd(n); + printf("%d\n",fi(n)); + } +} diff --git a/spoj_cpp_clean/ETF/ETF-4.cpp b/spoj_cpp_clean/ETF/ETF-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aec5bcdedb5e31cea920853e70dcf3cedf4f6116 --- /dev/null +++ b/spoj_cpp_clean/ETF/ETF-4.cpp @@ -0,0 +1,174 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m , pos; +int a[1000009], pm[1000000]; +void prime() +{ + int n=1000001; + memset(a,0,sizeof(a)); + a[0]=a[1]=1; + FOR(i,2,sqrt(n)+1) + if(a[i]==0) + for(int j=2; j*i>=1; + } + return ans; +} +int calc(int n) +{ + int cnt, ans=1; + for(int i=0; i + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< + +int main() +{ + int i = 0; + while(i != 42) { + std::cin >> i; + std::cout << i << '\n'; + std::cin.clear(); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/EXPRES11/EXPRES11-10.cpp b/spoj_cpp_clean/EXPRES11/EXPRES11-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a660034e0dfb8e80bd2a37daf8eb7a36097bcfef --- /dev/null +++ b/spoj_cpp_clean/EXPRES11/EXPRES11-10.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + +int main() +{ + const int maxe = 100000; + char buf[maxe]; + int n; + cin >> n; + while(n-- >= 0) + { + char c; + stack exp; + cin.get(); + while(cin.get(c) && c != '\n') + { + + switch(c) + { + case '(': + case '[': + case '{': + exp.push(c); + break; + case ')': + case ']': + case '}': + if(exp.top() == c) + exp.pop(); + break; + } + } + cout << (exp.empty() ? 'S' : 'N'); + } + return 0; +} diff --git a/spoj_cpp_clean/F91/F91-10.cpp b/spoj_cpp_clean/F91/F91-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7e08b9f5dc850da81a63f828317b953f38f28f92 --- /dev/null +++ b/spoj_cpp_clean/F91/F91-10.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int f91(int n) +{ + if(n <= 100) + return f91(f91(n + 11)); + else + return n - 10; +} + +int main() +{ + int n; + while(cin >> n && n != 0) + cout << "f91(" << n << ") = " << f91(n) << endl; + return 0; +} diff --git a/spoj_cpp_clean/FACT0/FACT0-8.cpp b/spoj_cpp_clean/FACT0/FACT0-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19afafaaec03f90bc99e2b3276f0b66a02f6305f --- /dev/null +++ b/spoj_cpp_clean/FACT0/FACT0-8.cpp @@ -0,0 +1,152 @@ +/*************************************************** + * Problem Name : FACT0 - Integer Factorization (15 digits).cpp + * Problem Link : https://www.spoj.com/problems/FACT0/ + * OJ : Spoj + * Verdict : AC + * Date : 2020-02-27 + * Problem Type : Number Theory + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100000005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorprime; +vector >fact; +int lp[MAX]; +vectorpr; +void linear_sieve (int N) { + for (int i = 2; i <= N; ++i) { + if (lp[i] == 0) { + lp[i] = i; + pr.pb (i); + } + for (int j = 0; j < (int) pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; ++j){ + lp[i * pr[j]] = pr[j]; + } + } +} +void prime_factorization(ll n){ + int sq = (int)sqrt(n)+1; + for(int i = 0; i<(int)pr.size() && pr[i] <= sq; i++){ + //~ if(mark[n] == 0)break; + ll cnt = 0; + while(n%pr[i] == 0){ + cnt++; + n/=pr[i]; + } + if(cnt)fact.pb({pr[i], cnt}); + } + if(n!=1){ + fact.pb({n,1LL}); + } +} +int main () { + __FastIO; + linear_sieve(MAX); + //~ cout << setprecision (10); + //~ cout << fixed; + ll n; + + while (cin >> n) { + if (n == 0) { + break; + } + prime_factorization(n); + bool x = 0; + for(auto p : fact){ + if(x!= 0)cout << " "; + cout << p.first << "^" << p.second; + x = 1; + } + nl; + fact.clear(); + } + + return 0; +} diff --git a/spoj_cpp_clean/FAKETSP/FAKETSP-13.cpp b/spoj_cpp_clean/FAKETSP/FAKETSP-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7f741f9dd79a1c103355430d12eefd891178937 --- /dev/null +++ b/spoj_cpp_clean/FAKETSP/FAKETSP-13.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +char buffer[5000]; +char buffer1[5000]; +double x,y,sx, sy; + +void f(){ + int n = strlen(buffer), i=0, j; + for(;i0){ + f(); + trip += sqrt(((x-sx)*(x-sx))+((y-sy)*(y-sy))); + sx = x; + sy = y; + printf("The salesman has traveled a total of %.3lf kilometers.\n",trip); + memset(buffer,'\0',sizeof(buffer)); + gets(buffer); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/FANCY/FANCY-3.cpp b/spoj_cpp_clean/FANCY/FANCY-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..910aba85e166271dd354afc15126f2bf9b54c1c3 --- /dev/null +++ b/spoj_cpp_clean/FANCY/FANCY-3.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, dp[31], count; + char c, p; + long long ans; + string inp; + memset(dp, 0, sizeof(dp)); + dp[0] = 0; + dp[1] = 1; + dp[2] = 2; + for(int i = 3; i <= 30; i++){ + dp[i] = 1; + for(int j = 1; j < i; j++){ + dp[i] += dp[i-j]; + } + } + sd(t); + while(t--){ + cin>>inp; + ans = 1; + p = inp[0]; + count = 1; + for(int i = 1; i < inp.length(); i++){ + c = inp[i]; + if(c == p){ + count++; + } + else{ + ans *= dp[count]; + count = 1; + p = c; + } + } + ans *= dp[count]; + printf("%lld\n",ans); + + } + return 0; +} diff --git a/spoj_cpp_clean/FARIDA/FARIDA-16.cpp b/spoj_cpp_clean/FARIDA/FARIDA-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..645073df12bda182b9a5bc86d5aa2e797872ac9c --- /dev/null +++ b/spoj_cpp_clean/FARIDA/FARIDA-16.cpp @@ -0,0 +1,34 @@ +#include +#define ll long long +using namespace std; + +ll f(vector& a, ll n, vector& dp){ + if(n==0) return 0; + if(dp[n]!=-1) return dp[n]; + if(n==1) dp[1] = a[1]; + else if(n==2) dp[2] = max(a[1],a[2]); + else if(n==3) dp[3] = max(a[1]+a[3],a[2]); + else dp[n] = max(f(a,n-3,dp) + a[n-1],f(a,n-2,dp) + a[n]); + return dp[n]; +} + +ll solve(){ + ll n; + cin>>n; + vector a(n+1); + vector dp(n+1,-1); + for(ll i=1;i<=n;i++){ + cin>>a[i]; + } + ll ans = f(a,n,dp); + return ans; +} + +int main(){ + ll t; + cin>>t; + for(ll i=1;i<=t;i++){ + ll ans = solve(); + cout<<"Case "< +using namespace std; + +#define ll long long +#define endl "\n" +#define mod 1000000007 //10^9 +7 +#define MAX 100001 //10^5 +1 +#define PI (3.141592653589) + + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + ll times; cin >> times; + for (int j = 0; j < times; j++) + { + ll n; + cin >> n; + if (n == 0) { + cout << "Case " << j + 1 << ": 0" << endl; + continue; + } + ll arr[n]; + for (ll i = 0; i < n; i++) { + cin >> arr[i]; + } + ll dp[n] = {0}; + dp[0] = arr[n - 1]; + if (n == 1) { + cout << "Case " << j + 1 << ": " << dp[0] << endl; + continue; + } + if (n > 1) { + dp[1] = arr[n - 2]; + } + if (n > 2) { + dp[2] = arr[n - 3] + arr[n - 1]; + } + for (ll i = 3; i < n; i++) { + if (dp[i - 2] < dp[i - 3]) { + dp[i] = arr[n - i - 1] + dp[i - 3]; + } else { + dp[i] = arr[n - i - 1] + dp[i - 2]; + } + } + if (dp[n - 1] > dp[n - 2]) { + cout << "Case " << j + 1 << ": " << dp[n - 1] << endl; + } else { + cout << "Case " << j + 1 << ": " << dp[n - 2] << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/FARIDA/FARIDA-4.cpp b/spoj_cpp_clean/FARIDA/FARIDA-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d673d433ccbcc69c8403e540a489af1fd5c3dd87 --- /dev/null +++ b/spoj_cpp_clean/FARIDA/FARIDA-4.cpp @@ -0,0 +1,123 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +//Other Includes +#include +#include +#include +#include +#include +#include +#include +#include +//some common functionn +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define all(c) (c).begin(),(c).end() +// Input macros +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) + +// Output macros +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +using namespace std; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i=n) + return 0; + if(res[x]!=0) + return res[x]; + + return res[x]=maX(a[x]+call(x+2), call(x+1)); +} + + +int solve() +{ + cout< + +using namespace std; +long long int t,n,aux,arr[1001],memo[1001]; + +long long int max(long long int x,long long int y) +{ + return (x>y)?x:y; +} + +long long int DP(long long int i) +{ + if (i>=n) return 0; + if (i==n-1) return arr[i]; + if (memo[i]!=-1) return memo[i]; + + memo[i]= max(DP(i+2)+arr[i], DP(i+1) ); + + return memo[i]; +} + +int main() +{ + + + scanf("%lld",&t); + for (int tc = 1; tc <=t ; ++tc) + { + memset(memo, -1, sizeof(memo)); + scanf("%lld",&n); + for (int i = 0; i < n; ++i) + { + scanf("%lld",&arr[i]); + } + + printf("Case %d: %lld\n",tc,DP(0)); + } + + return 0; +} diff --git a/spoj_cpp_clean/FARIDA/FARIDA-9.cpp b/spoj_cpp_clean/FARIDA/FARIDA-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a04937a14511c7cae5b85de0fd7110acc870648 --- /dev/null +++ b/spoj_cpp_clean/FARIDA/FARIDA-9.cpp @@ -0,0 +1,48 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +long long int max(long long int a, long long int b) + { + if(a > b) + return a; + else + return b; + } + +int main() + { + int t, j; + + scanf("%d", &t); + + for(j = 0; j < t; j++) + { + int n, i; + long long int a[1001], d[1001]; + + scanf("%d", &n); + + for(i = 0; i < n; i++) + scanf("%lld", &a[i]); + + d[0] = a[0]; + d[1] = max(d[0], a[1]); + + for(i = 2; i < n; i++) + d[i] = max(d[i-1], a[i] + d[i-2]); + + printf("Case %d: %lld\n", j+1, d[n-1]); + } + + return 0; + } diff --git a/spoj_cpp_clean/FASHION/FASHION-11.cpp b/spoj_cpp_clean/FASHION/FASHION-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..131f26f1f6b839006eec81a7ee0144b94d5fbfd4 --- /dev/null +++ b/spoj_cpp_clean/FASHION/FASHION-11.cpp @@ -0,0 +1,42 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include +#include + +using namespace std; + +int main(void) +{ + int t; + + int n, male[1001], female[1001]; + long sum; + + cin >> t; + + while(t--) + { + cin >> n; + + for(int i = 0; i < n; i++) + cin >> male[i]; + + for(int i = 0; i < n; i++) + cin >> female[i]; + + sort(male, male+n); + sort(female, female+n); + + sum = 0l; + + for(int i = 0; i < n; i++) + sum += (male[i] * female[i]); + + cout << sum << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/FASHION/FASHION-16.cpp b/spoj_cpp_clean/FASHION/FASHION-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32b21a03ff640a1056625ad9059d6ba9a7487435 --- /dev/null +++ b/spoj_cpp_clean/FASHION/FASHION-16.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +#define ll long long +using namespace std; + +int solve(){ + int n,m[1009],f[1009],sum=0,i; + cin>>n; + for(i=0;i>m[i]; + for(i=0;i>f[i]; + sort(m, m+n); + sort(f, f+n); + for(i=0;i>t; + while(t--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/FASHION/FASHION-19.cpp b/spoj_cpp_clean/FASHION/FASHION-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e4f37919515a29850b13ed7812018e6a8d1879b --- /dev/null +++ b/spoj_cpp_clean/FASHION/FASHION-19.cpp @@ -0,0 +1,39 @@ +#include +#define ll long long +#define endl "\n" +#define MAX 100001 +#define mod 1000000007 +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int t;cin>>t;while(t--){ + int N; + cin>>N; + int men_hotness[N]; + int women_hotness[N]; + for(int i=0; i>men_hotness[i]; + } + for(int i=0; i>women_hotness[i]; + } + sort(men_hotness, men_hotness + N); + sort(women_hotness, women_hotness + N); + + ll ans=0; + + for(int i=0; i +#include +#include + +using namespace std; + +int main() +{ + int test,n,i,j,hotness; + cin>>test; + while(test--) + { + cin>>n; + hotness=0; + int *male=NULL; + male=new int[n]; + int *female=NULL; + female=new int[n]; + for(i=0;i>male[i]; + } + for(i=0;i>female[i]; + } + sort(male,male+n); + sort(female,female+n); + for(i=0;i + +using namespace std; + +int main() +{ + int tc=0,n=0,aux=0,sum=0; + vector m; + vector f; + + + scanf("%d",&tc); + for (int i = 0; i < tc; ++i) + { + scanf("%d",&n); + m.clear(); + f.clear(); + sum=0; + for (int j = 0; j < n; ++j) + { + scanf("%d",&aux); + m.push_back(aux); + } + for (int j = 0; j < n; ++j) + { + scanf("%d",&aux); + f.push_back(aux); + } + sort(m.begin(), m.end()); + sort(f.begin(), f.end()); + for (int j = 0; j < n; ++j) + { + sum= sum + m[j]*f[j]; + } + printf("%d\n",sum ); + } + + return 0; +} diff --git a/spoj_cpp_clean/FASTFLOW/FASTFLOW-0.cpp b/spoj_cpp_clean/FASTFLOW/FASTFLOW-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01c16bb18a1910b57b866c7c96214394c0c8e8fd --- /dev/null +++ b/spoj_cpp_clean/FASTFLOW/FASTFLOW-0.cpp @@ -0,0 +1,148 @@ +#include + +#define FI first +#define SE second + +typedef long long ll; + +const static ll INF = std::numeric_limits::max(); +struct DinicFlow { + + struct edge { + ll a, b; + ll flow,cap; + + edge* rev; + + edge(const ll& _a, const ll& _b, const ll& _flow, const ll& _cap, edge* _rev = nullptr) + : a(_a), b(_b), flow(_flow), cap(_cap), rev(_rev) {} + }; + + typedef std::vector> adj_list; + + /* for the DFS part */ + std::vector blocked; + + /* for the BFS part */ + std::vector distances; + + adj_list graph; + + ll n; /* number of nodes */ + ll source; + ll sink; + + /* intakes the number of nodes in the graph */ + DinicFlow(const ll& _n) : n(_n) { + source = n++; /* idk why */ + sink = n++; + + blocked.assign(n, false); + distances.assign(n, INF); + + graph.assign(n, std::vector()); + } + + ~DinicFlow(void) { + /* gotta free that memory!*/ + for (auto& row : graph) { + for (auto& e : row) { + delete e; + } + } + } + + /* add the directed edge, and it adds the backedge for you */ + void add_edge(const ll& a, const ll& b, const ll& cap, const ll& flow = 0) { + edge* normal = new edge(a,b,flow,cap); + edge* reverse = normal->rev = new edge(b,a,0,0, normal); + + graph[a].push_back(normal); + graph[b].push_back(reverse); + + if (!normal || !reverse) { + std::fprintf(stderr,"fail\n"); + } + } + + bool bfs(void) { + distances.assign(n, INF); + distances[sink] = 0; + + std::queue order; + order.push(sink); + + while (!order.empty()) { + ll best = order.front(); + order.pop(); + + if (best == source) return true; + + for (edge* neigh : graph[best]) { + if (neigh->rev->cap > neigh->rev->flow && distances[neigh->b] > 1 + distances[best]) { + distances[neigh->b] = 1 + distances[best]; + order.push(neigh->b); + } + } + } + + return false; + } + + ll dfs(const ll& pos, const ll& min) { + if (pos == sink) { + return min; + } + + ll flow = 0; + for (edge* e : graph[pos]) { + ll curr = 0; + if (!blocked[e->b] && distances[e->b] == distances[pos] - 1 && e->cap > e->flow) { + curr = dfs(e->b, std::min(min - flow, e->cap - e->flow)); + e->flow += curr; + e->rev->flow = -(e->flow); + flow += curr; + } + if (flow == min) return flow; + } + blocked[pos] = flow != min; + return flow; + } + + ll flow(void) { + for (auto& row : graph) { + for (edge* e : row) + e->flow = 0; + } + + ll ret = 0; + while(bfs()) { + blocked.assign(blocked.size(), false); + ret += dfs(source, INF); + } + return ret; + } +}; + +int main(void) { + std::ios::sync_with_stdio(false); + std::cin.tie(nullptr); + + ll n, m; + std::cin >> n >> m; + + DinicFlow df(n); + + for (ll a,b,c; m--;) { + std::cin >> a >> b >> c; + df.add_edge(a-1,b-1, c); + df.add_edge(b-1,a-1, c); + } + + df.source = 0; + df.sink = n-1; + + std::cout << df.flow() << "\n"; + + return 0; +} diff --git a/spoj_cpp_clean/FASTFLOW/FASTFLOW-14.cpp b/spoj_cpp_clean/FASTFLOW/FASTFLOW-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e9543174619829100e25d26b009a15600df575e --- /dev/null +++ b/spoj_cpp_clean/FASTFLOW/FASTFLOW-14.cpp @@ -0,0 +1,111 @@ +#include +using namespace std; + +#define ll long long + +const ll maxnodes = 5005; + +ll nodes = maxnodes, src, dest; +ll dist[maxnodes], q[maxnodes], work[maxnodes]; + +struct Edge +{ + ll to, rev; + ll f, cap; +}; + +vector g[maxnodes]; + +void addEdge(ll s, ll t, ll cap) +{ + Edge a = {t, g[t].size(), 0, cap}; + Edge b = {s, g[s].size(), 0, 0}; + g[s].push_back(a); + g[t].push_back(b); +} + +bool dinic_bfs() +{ + fill(dist, dist + nodes, -1); + + dist[src] = 0; + ll index = 0; + q[index++] = src; + + for (ll i = 0; i < index; i++) + { + ll u = q[i]; + for (ll j = 0; j < (ll) g[u].size(); j++) + { + Edge &e = g[u][j]; + if (dist[e.to] < 0 && e.f < e.cap) + { + dist[e.to] = dist[u] + 1; + q[index++] = e.to; + } + } + } + return dist[dest] >= 0; +} + +ll dinic_dfs(ll u, ll f) +{ + if (u == dest) + return f; + + for (ll &i = work[u]; i < (ll) g[u].size(); i++) + { + Edge &e = g[u][i]; + + if (e.cap <= e.f) continue; + + if (dist[e.to] == dist[u] + 1) + { + ll flow = dinic_dfs(e.to, min(f, e.cap - e.f)); + if (flow > 0) + { + e.f += flow; + g[e.to][e.rev].f -= flow; + return flow; + } + } + } + return 0; +} + +ll maxFlow(ll _src, ll _dest) +{ + src = _src; + dest = _dest; + ll result = 0; + while (dinic_bfs()) + { + fill(work, work + nodes, 0); + while (ll delta = dinic_dfs(src, INT_MAX)) + result += delta; + } + return result; +} + + +// addEdge(u, v, C); edge from u to v. Capacity is C +// maxFlow(s, t); max flow from s to t + + +int main() + +{ + ll n, m; + cin >> n >> m; + + for(ll i = 1; i <= m; i++) { + ll u, v, c; + scanf("%lld %lld %lld", &u, &v, &c); + addEdge(u, v, c); + addEdge(v, u, c); + } + + cout << maxFlow(1, n) << endl; + + return 0; +} diff --git a/spoj_cpp_clean/FASTFLOW/FASTFLOW-3.cpp b/spoj_cpp_clean/FASTFLOW/FASTFLOW-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a22727263db1bb7d8e6454c1a5d3b3ad7a4675c --- /dev/null +++ b/spoj_cpp_clean/FASTFLOW/FASTFLOW-3.cpp @@ -0,0 +1,122 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it) +#define meta __FUNCTION__,__LINE__ + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +using namespace std; + +const long double PI = 3.1415926535897932384626433832795; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void tr(S x, const Strings&... rest){cout< pii; + +typedef ll F; + +#define FINF (1ll << 60) +#define MAXN 5010 +#define MAXE 30010 // Careful! + +struct maxFlow{ + struct Edge{ int x, y; F cap, flow;}; + + int N, visited[MAXN], prev[MAXN]; + vector E; + vector g[MAXN]; + + maxFlow(int _N) : N(_N){ } + + void addEdge(int x, int y, F cap){ + Edge e1 = {x, y, cap, 0}, e2 = {y, x, 0, 0}; + g[x].pb(E.size()); E.pb(e1); g[y].pb(E.size()); E.pb(e2); + } + + bool bfs(int s, int t){ + int x, y; + for(int i = 1; i <= N; i++) visited[i] = prev[i] = -1; + queue q; q.push(s); + while(!q.empty()){ + x = q.front(); q.pop(); + visited[x] = 1; + + foreach(it, g[x]){ + Edge &e = E[*it]; y = e.y; + + if(e.cap == 0 or visited[y] == 1) continue; + + visited[y] = 1, prev[y] = *it; + if(y == t) break; + + q.push(y); + } + } + + return (visited[t] == 1); + } + + F maxflow(int s, int t){ + F ans = 0; + while(bfs(s,t)){ + F cap = FINF; + + for(int i = t; i != s; i = E[prev[i]].x) cap = min(cap, E[prev[i]].cap); + for(int i = t; i != s; i = E[prev[i]].x){ + E[prev[i]].cap -= cap, E[prev[i]^1].cap += cap; + E[prev[i]].flow += cap; if(E[prev[i]^1].flow > 0) E[prev[i]^1].flow -= cap; + } + + ans += cap; + } + return ans; + } +}; + +int n, m; + + +map cap; + +int main(){ + sd2(n,m); + + maxFlow *mf = new maxFlow(n); + + for(int i = 0; i < m; i++){ + int a, b, c; + sd3(a,b,c); + if(a == b) continue; + cap[mp(a,b)] += c; + cap[mp(b,a)] += c; + } + + foreach(it, cap){ + int x = it->fi.fi, y = it->fi.se; + ll c = it->se; + mf->addEdge(x,y,c); + } + + printf("%lld\n", mf->maxflow(1,n)); + + return 0; +} diff --git a/spoj_cpp_clean/FASTPOW/FASTPOW-6.cpp b/spoj_cpp_clean/FASTPOW/FASTPOW-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f408b64e4ba9d5345f7b44a4121c578a761beb5 --- /dev/null +++ b/spoj_cpp_clean/FASTPOW/FASTPOW-6.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +typedef long long ll; + +ll bigMod( ll a, ll x, ll mod ) { + if( x == 0 ) return 1; + if( ( x & 1 ) == 1 ) return ( ( a * bigMod( a, ( x - 1 ), mod ) ) % mod ); + else { + ll s = bigMod( a, ( x >> 1 ), mod ); + return ( ( s * s ) % mod ); + } +} + +int main( int argc, char ** argv ) { +// freopen( "in.txt", "r", stdin ); +// freopen( "out.txt", "w", stdout ); + + ios_base::sync_with_stdio( false ); + cin.tie( NULL ); + + ll A, B, C; + while( cin >> A >> B >> C ) { + cout << bigMod( A, B, C ) << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/FATAWY/FATAWY-9.cpp b/spoj_cpp_clean/FATAWY/FATAWY-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd694ccb1673bdf57f50d78952483adbe54bb81e --- /dev/null +++ b/spoj_cpp_clean/FATAWY/FATAWY-9.cpp @@ -0,0 +1,196 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#include + +#define gc() getchar() + +using namespace std; + +vector< string > STRS; +int N, TH; + +class ufs +{ +private: + struct set + { + int name; + int rank; + set *parent; + }; + int ssize; + vector S; + + set *fs(int name) + { + if (S[name].parent->name == name) + return &S[name]; + + S[name].parent = fs(S[name].parent->name); + return S[name].parent; + } + + +public: + ufs(int n) + { + int i; + ssize = n; + S.resize(ssize); + + for (i = 0; iname; + } + + bool is_same_set(int a, int b) + { + return fs(a)->name == fs(b)->name; + } + + void union_sets(int a, int b) + { + set *f, *s; + + f = fs(a); + s = fs(b); + + if (f->name == s->name) + return; + + ssize--; + if (f->rank == s->rank) + { + f->rank++; + s->parent = f; + } + + else if (f->rank > s->rank) + s->parent = f; + + else + f->parent = s; + } + + int size() + { + return ssize; + } +}; + +inline void getstring(string &A) +{ + char c; + + A.clear(); + + A.push_back('0'); + + c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +inline int LCS(string &A, string &B) +{ + int DP[15][15]; + int i, j; + + memset(DP, 0, sizeof(DP)); + + for (i = 1; i < A.length(); i++) + for (j = 1; j < B.length(); j++) + { + if (A[i] == B[j]) + DP[i][j] = DP[i - 1][j - 1] + 1; + else + DP[i][j] = max(DP[i - 1][j], DP[i][j - 1]); + } + + return DP[A.length() - 1][B.length() - 1]; +} + +inline void init() +{ + int i; + string S; + + STRS.clear(); + + scanf("%d %d", &N, &TH); + + for (i = 0; i < N; i++) + { + getstring(S); + STRS.push_back(S); + } +} + +bool check(string &A, string &B) +{ + int lcs = LCS(A, B); + + if ((lcs * 1.0) / (A.length() - 1) >= TH / 100.0 && (lcs * 1.0) / (B.length() - 1) >= TH / 100.0) + return 1; + + return 0; +} + +inline int solve() +{ + int i, j; + ufs g(N); + + for (i = 0; i < N - 1; i++) + for (j = i + 1; j < N; j++) + if (!g.is_same_set(i, j) && check(STRS[i], STRS[j])) + g.union_sets(i, j); + + return g.size(); +} + +int main() +{ + int T, i; + + //freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + for (i = 1; i <= T; i++) + { + init(); + printf("Case #%d:\n%d\n", i, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/FCDC/FCDC-3.cpp b/spoj_cpp_clean/FCDC/FCDC-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b01fc9299fbe62943eab651dc02c950bc58e31a6 --- /dev/null +++ b/spoj_cpp_clean/FCDC/FCDC-3.cpp @@ -0,0 +1,116 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< pr; + +int find(int x){ + map f; + + int tmp = x; + for(int &y : pr){ + if(tmp % y != 0) continue; + + int cnt = 0; + + while(tmp % y == 0){ + cnt++; + tmp /= y; + } + + f[y] = cnt; + } + if(tmp > 1) f[tmp] = 1; + + int lo = 0, hi = 1000*1000*10, mid; + + while(lo + 1 < hi){ + mid = (lo + hi) / 2; + + bool flag = true; + + foreach(it, f){ + int y = it->fi, req = it->se; + + ll cur = y; + int cnt = 0; + + while(cur <= mid){ + cnt += mid / cur; + cur *= y; + } + + if(cnt < req){ + flag = false; + break; + } + } + + if(flag) hi = mid; + else lo = mid; + } + + return hi; + +} + +int main(){ _ + for(int i = 2; i < N; i++){ + if(p[i]) continue; + for(int j = 2*i; j < N; j += i){ + p[j] = 1; + } + + pr.pb(i); + } + + int a, b; + cin >> a >> b; + + if(a == b){ + cout << 0 << endl; + return 0; + } + + cout << max(find(b) - find(a), 0) << endl; + + return 0; +} diff --git a/spoj_cpp_clean/FCTRL/FCTRL-11.cpp b/spoj_cpp_clean/FCTRL/FCTRL-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c7439689c6531abdb1984a591e0da89197c9f7d --- /dev/null +++ b/spoj_cpp_clean/FCTRL/FCTRL-11.cpp @@ -0,0 +1,32 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include + +using namespace std; + +int main(void) +{ + long t; + unsigned long long number, count; + + cin >> t; + + while(t--) + { + cin >> number; + count = 0ull; + + while(number) + { + count += (number / 5ull); + number /= 5ull; + } + + cout << count << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/FCTRL/FCTRL-12.cpp b/spoj_cpp_clean/FCTRL/FCTRL-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c82282c0b095277cd86f1fdca2bfc2f8f0c6d5dc --- /dev/null +++ b/spoj_cpp_clean/FCTRL/FCTRL-12.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int pow(int n, int e) +{ + long r = n; + for (int i = 1; i < e; i++) + r *= n; + return r; +} + +int main() +{ + ios::sync_with_stdio(false); + int p; + cin >> p; + for (int i = 0; i < p; ++i) { + int con, nZeros = 0, loop = 1, count = -1; + cin >> con; + while (count != 0) { + count = (con / pow(5, loop)); + nZeros += count; + loop++; + } + cout << nZeros << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/FCTRL/FCTRL-15.cpp b/spoj_cpp_clean/FCTRL/FCTRL-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11eae63c2205c55b21a2f1c0d9a04063ba14fa93 --- /dev/null +++ b/spoj_cpp_clean/FCTRL/FCTRL-15.cpp @@ -0,0 +1,48 @@ +/* +* author: @j-tesla +*/ + +// C++14 + +#pragma GCC optimize ("O3") +#pragma GCC target ("sse4") + +#include + +using namespace std; + +#define uns unsigned +typedef long long ll; +typedef vector vb; +typedef vector vc; +typedef vector vi; +typedef pair ii; +typedef vector vu; +typedef vector vl; + +#define din(x) int x; cin >> x; +#define in(x) cin >> x; +#define full(x) x.begin(), x.end() + +#define eb emplace_back +#define ins insert +#define f first +#define s second + + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + + din(t) + while (t--) { + din(n) + int s = 0; + while (n > 0) { + n /= 5; + s += n; + } + cout << s << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/FCTRL/FCTRL-16.cpp b/spoj_cpp_clean/FCTRL/FCTRL-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..729220f0f92a593020554699e9cb14f548d17773 --- /dev/null +++ b/spoj_cpp_clean/FCTRL/FCTRL-16.cpp @@ -0,0 +1,24 @@ +#include +#include +#define ll long long +#define m 1000000007 +using namespace std; + +void trail(ll k){ + ll counter = 0; + while(k>0) + { k/=5; + counter+=k; + } + cout<>t; + while(t--){ + cin>>a; + trail(a); + } + return 0; +} diff --git a/spoj_cpp_clean/FCTRL/FCTRL-19.cpp b/spoj_cpp_clean/FCTRL/FCTRL-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f180a6567fa333377f1120c432fad6a930acca2 --- /dev/null +++ b/spoj_cpp_clean/FCTRL/FCTRL-19.cpp @@ -0,0 +1,27 @@ +#include +#define ll long long + +using namespace std; + +int main() { + // your code goes here + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; cin>>t; + + while(t--){ + int ans=0; + ll N; + cin>>N; + ll temp=1; + for(int i=1;temp>0;i++){ + temp=N/pow(5, i); + ans+=temp ; + + } + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +ll factorialPrimePower (ll n, ll p ) { + ll freq = 0; + ll x = n; + + while ( x ) { + freq += x / p; + x = x / p; + } + + return freq; +} +int main () { + __FastIO; + int tc; + ll n; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> n; + ll x = factorialPrimePower (n, 2); + ll y = factorialPrimePower (n, 5); + cout << min (x, y) << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/FCTRL2/FCTRL2-11.cpp b/spoj_cpp_clean/FCTRL2/FCTRL2-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3aaf073f69241b737063b6a587b5f4d6bfd2f978 --- /dev/null +++ b/spoj_cpp_clean/FCTRL2/FCTRL2-11.cpp @@ -0,0 +1,54 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +//Linked list version FCTRL2 +//It can even calculate the factorial of more than 100 + +#include +#include + +using namespace std; + +int main(void) +{ + int t, n, carry, temp; + list result; + + cin >> t; + + while(t--) + { + cin >> n; + + result.clear(); + + result.push_back(1); + + for(int i = 2; i <= n; i++) + { + carry = 0; + + for(list::iterator it = result.begin(); it != result.end(); it++) + { + temp = (i * (*it)) + carry; + *it = temp % 10; + carry = temp / 10; + } + + while(carry) + { + result.push_back(carry % 10); + carry /= 10; + } + } + + for(list::reverse_iterator rit = result.rbegin(); rit != result.rend(); rit++) + cout << *rit; + + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/FCTRL2/FCTRL2-19.cpp b/spoj_cpp_clean/FCTRL2/FCTRL2-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07aae9cba297f06dc6eb3c729ad0b0626d8726ee --- /dev/null +++ b/spoj_cpp_clean/FCTRL2/FCTRL2-19.cpp @@ -0,0 +1,38 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 + +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int t;cin>>t;while(t--){ + int N; + cin>>N; + int m=1; + ll x, temp=0; + int arr[200]={0}; + arr[0]=1; + for(int i=2; i<=N; i++){ + for(int j=0; j=0; i--){ + cout< + +using namespace std; + +int main() +{ + int t, x, temp, num, i, m, fact[200000], countDigit, index; + cin>>t; + while(t--) + { + cin>>num; + fact[0]=1; + i=1; + m=1; + temp=0; + while(i<=num) + { + index=0; + while(index0) + { + fact[m]=temp%10; + temp/=10; + m++; + } + i++; + } + cout<=0;i--) + { + cout< +#include +#include + +using namespace std; + +int main(void) +{ + int l; + + cin >> l; + + while(l) + { + printf("%.2f\n", ((float)l * l / M_PI / 2.0f)); + + cin >> l; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/FENCE1/FENCE1-16.cpp b/spoj_cpp_clean/FENCE1/FENCE1-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4d5d574a10b74a533ef3e3cbd15def16a3fc36d --- /dev/null +++ b/spoj_cpp_clean/FENCE1/FENCE1-16.cpp @@ -0,0 +1,48 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "< +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(double x){ + float pi = 3.14159265359; + float s = x*x/(2*pi); + printf("%.2f\n",s); + //cout<>test_cases; + while(cin>>x){ + if(comp(x,0.0)) break; + solve(x); + } + return 0; +} diff --git a/spoj_cpp_clean/FENCE1/FENCE1-19.cpp b/spoj_cpp_clean/FENCE1/FENCE1-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6385775ce5a21ef4d6c3f31a37d739e65767c18c --- /dev/null +++ b/spoj_cpp_clean/FENCE1/FENCE1-19.cpp @@ -0,0 +1,25 @@ +#include +#define ll long long +#define endl "\n" +#define mod 1000000007 +#define MAX 10001 +#define PI 3.1415926 +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + while(true) + { + double l; + cin>>l; + if(l==0){ + return 0; + } + double area=l*l; + area = area/(2*PI); + printf("%.2lf\n", area); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/FENCE1/FENCE1-5.cpp b/spoj_cpp_clean/FENCE1/FENCE1-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e337e7dcbfbeabbc6d3d75ae6c473ad916fbbdc7 --- /dev/null +++ b/spoj_cpp_clean/FENCE1/FENCE1-5.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +using namespace std; + +int main() +{ + float n,r,ans; + cin>>n; + while(n) + { + //r=(n/3.14); + ans=(n*n/(2*M_PI)); + cout<>n; + } + return 0; +} diff --git a/spoj_cpp_clean/FFARM/FFARM-3.cpp b/spoj_cpp_clean/FFARM/FFARM-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9fc2f7ec16355ddc6e7593ddd37b43c153dc5bf --- /dev/null +++ b/spoj_cpp_clean/FFARM/FFARM-3.cpp @@ -0,0 +1,93 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb(x) push_back(x) +#define mp(x,y) make_pair(x,y) +#define LET(x, a) __typeof(a) x(a) +#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define meta __FUNCTION__<<' '<<__LINE__<<' ' +#define tr(x) cout< +ostream& operator<<(ostream& out, pair const& p){out<<'('< +ostream& operator<<(ostream& out, vector const & v){ +int l = v.size(); for(int i = 0; i < l-1; i++) out<0) out< 0){ + rev = rev*10 + x%10; + x /= 10; + } + return (rev == o); +} + +int t, a, b, l, inf = 1e9; + +int main(){ + for(int i = 1; i < N; i++) palin[i] = ispalin(i); + next[N-1] = inf; + for(int i = N-2; i > 0; i--) next[i] = (palin[i+1] == 1)? i+1 : next[i+1]; + + sd(t); + while(t--){ + sd3(a,b,l); + int cnt = 0, mx = 0; + int x = (palin[a] == 1)? a : next[a], y = x; + int len = 0; + + if(x > b){ + puts("Barren Land."); + continue; + } + + int bx = x, by = y; + + while(y <= b){ + + while(y-x+1 > l){ + x = next[x]; + cnt--; + } + + cnt++; + if(cnt > mx){ + mx = cnt; + bx = x, by = y; + len = by-bx+1; + } + else if(cnt == mx and y-x+1 < len){ + bx = x, by = y; + len = by-bx+1; + } + + y = next[y]; + } + + printf("%d %d\n", bx, by); + + } + return 0; +} diff --git a/spoj_cpp_clean/FIBOSUM/FIBOSUM-14.cpp b/spoj_cpp_clean/FIBOSUM/FIBOSUM-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63a027f19d594df8c6b4df8dad707e10fa9abe21 --- /dev/null +++ b/spoj_cpp_clean/FIBOSUM/FIBOSUM-14.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +#define ll long long +const ll MOD = 1000000007; +const ll MOD2 = MOD * MOD; /// Only when (MOD * MOD) fits into long long +#define row 2 +#define col 2 + +struct MatExpo + +{ + ll exponents[64][row][col]; + ll ident[row][col] = { {1, 0}, {0, 1} }; /// Identity Matrix + ll result[row][col], mat[row][col]; + + MatExpo() { + + /// Creating Base Matrix + ll base[row][col] = { {1, 1}, {1, 0} }; + memcpy(exponents[0], base, sizeof(base)); + + /// Calculating all exponents + for(ll p = 1; p < 62; p++) { + + for(ll i = 0; i < row; i++) { + for(ll j = 0; j < col; j++) { + ll tmp = 0; + for(ll k = 0; k < col; k++) { + tmp += exponents[p - 1][i][k] * exponents[p - 1][k][j]; + while(tmp >= MOD2) /// Taking modulo MOD2 is easy, because we can do it by subtraction + tmp -= MOD2; + } + exponents[p][i][j] = tmp % MOD; + } + } + } + + } + + ll ans(ll m) { + + /// Return from base case + if(m == 0 || m == 1) + return 1; + + memcpy(mat, ident, sizeof(ident)); + ll n = m - 1; /// Here, (n - 1)th power of base matrix represents the nth term + for(ll p = 60; p >= 0; p--) { + if((n >> p) & 1) { + + for(ll i = 0; i < row; i++) { + for(ll j = 0; j < col; j++) { + ll tmp = 0; + for(ll k = 0; k < col; k++) { + tmp += mat[i][k] * exponents[p][k][j]; + while(tmp >= MOD2) /// Taking modulo MOD2 is easy, because we can do it by subtraction + tmp -= MOD2; + } + result[i][j] = tmp % MOD; + } + } + + memcpy(mat, result, sizeof(result)); + } + + } + + return (result[0][0] + result[0][1]) % MOD; + } + +}; + +// MatExpo ex = MatExpo(); +// ans = ex.ans(n); nth term, term count starts from 0 + +int main() + +{ + ll t; + cin >> t; + MatExpo ex = MatExpo(); + while(t--) { + ll n, m; + scanf("%lld %lld", &n, &m); + printf("%lld\n", (ex.ans(m + 2 - 1) - ex.ans(n + 1 - 1) + MOD) % MOD); + } + + return 0; +} diff --git a/spoj_cpp_clean/FIBPOL/FIBPOL-3.cpp b/spoj_cpp_clean/FIBPOL/FIBPOL-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02f692f687169c0946c3845959122bf5dbdc0e27 --- /dev/null +++ b/spoj_cpp_clean/FIBPOL/FIBPOL-3.cpp @@ -0,0 +1,83 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<>= 1; + } + return ret; +} + +int p[N] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73}; + +ll a; +int x; + +inline int solve(){ + scanf("%lld", &a); + + if(!a) return 1; + + + for(int i = 0; i < N; i++){ + x = a % p[i]; + x = (x * (5 * x + 2) + 1) % p[i]; + + ll res = fast(x, (p[i]-1)/2, p[i]); + + if(res > 1) return 0; + } + + return 1; + +} + +int main(){ + int t; + sd(t); + + while(t--) printf("%d\n", solve()); + + return 0; +} diff --git a/spoj_cpp_clean/FIBPOL/FIBPOL-9.cpp b/spoj_cpp_clean/FIBPOL/FIBPOL-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eddec97cc57ef78006ad5f201b48372c0acbda46 --- /dev/null +++ b/spoj_cpp_clean/FIBPOL/FIBPOL-9.cpp @@ -0,0 +1,45 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +inline bool solve(long long num) +{ + long long temp; + double res; + + res = num; + + res = sqrt((res + 1) * (res + 1) + 4 *res * res); + temp = res; + + if(temp == res) + return 1; + return 0; +} + +int main() +{ + int T; + long long num; + + scanf("%d", &T); + + while(T--) + { + scanf("%lld", &num); + printf("%d\n", solve(num)); + } + return 0; +} diff --git a/spoj_cpp_clean/FISHER/FISHER-13.cpp b/spoj_cpp_clean/FISHER/FISHER-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d7af494e4a25f3e709e8d867918079504f5515e --- /dev/null +++ b/spoj_cpp_clean/FISHER/FISHER-13.cpp @@ -0,0 +1,96 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +const int INF = 0x3f3f3f3f; +using namespace std; + +struct state_t{ + int i, cost, time; + + state_t(int _i, int _cost, int _time): + i(_i), cost(_cost), time(_time){} + + bool operator <(const state_t &x)const{ + if(x.cost < cost)return true; + else if(cost < x.cost)return false; + else return x.time < time; + } +}; + +int times[100][100]; +int tolls[100][100]; +int mincost[100][1002]; + +void addState(priority_queue &pq, int i, int nextCost, int nextTime){ + if(nextCost < mincost[i][nextTime]){ + mincost[i][nextTime] = nextCost; + pq.push(state_t(i,nextCost, nextTime)); + } +} + +pair f(int n, int t){ + memset(mincost, INF, sizeof(mincost)); + priority_queue pq; + pq.push(state_t(0,0,0)); + mincost[0][0] = 0; + while(!pq.empty()){ + state_t st = pq.top(); pq.pop(); + if(st.i == n-1 && st.time <= t)return make_pair(st.cost, st.time); + if(st.time > t)continue; + for(int k=0; k res = f(n,t); + printf("%d %d\n", res.first, res.second); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/FISHER/FISHER-21.cpp b/spoj_cpp_clean/FISHER/FISHER-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42e748fad37a23c38af32cea4e2a975fbab59ffa --- /dev/null +++ b/spoj_cpp_clean/FISHER/FISHER-21.cpp @@ -0,0 +1,55 @@ +/* + dynamic programming > 0-1 knapsack + difficulty: medium + date: 29/May/2020 + problem: get the (min) total value and total weight of the optimal knapsack + by: @brpapa +*/ +#include +using namespace std; +#define INF (1 << 30) +#define MAX_N 55 +#define MAX_T 1001 +typedef pair ii; + +int travelTime[MAX_N][MAX_N]; // tempo gasto para viajar i -> j +int tollPrice[MAX_N][MAX_N]; // preço do pedágio i -> j +int N, T; + +// retorna o pair (tollPaid, timeNeeded) para ir da cidade n até N-1, com o menor tollPaid possível, e timeNeeded <= T +ii memo[MAX_N][MAX_T]; +ii dp(int n, int t) { + // cidade atual n, tempo restante t + + if (t < 0) return ii(INF, INF); // inválido + if (n == N-1) return ii(0, 0); + + ii &ans = memo[n][t]; + if (ans != ii(-1, -1)) return ans; + + ans = ii(INF, INF); + // para cada cidade adjacente nn que não é a atual + for (int nn = 0; nn < N; nn++) if (nn != n) { + ii next = dp(nn, t-travelTime[n][nn]); + + if (next.first + tollPrice[n][nn] < ans.first) { + ans = ii( + next.first + tollPrice[n][nn], // valor do pedágio + next.second + travelTime[n][nn] // tempo gasto + ); + } + } + return ans; +} + +int main() { + while (cin >> N >> T && (N || T)) { + for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) cin >> travelTime[i][j]; + for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) cin >> tollPrice[i][j]; + + for (int i = 0; i < MAX_N; i++) for (int j = 0; j < MAX_T; j++) memo[i][j] = ii(-1, -1); + ii ans = dp(0, T); + cout << ans.first << " " << ans.second << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/FLIB/FLIB-3.cpp b/spoj_cpp_clean/FLIB/FLIB-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9da3d70f9dd2c76cdb3887d6c5e71c0fc2a6f913 --- /dev/null +++ b/spoj_cpp_clean/FLIB/FLIB-3.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +long long MOD = 1000000007; + +int main(){ + int t; + long long n; + long long M[3][3], R[3][3]; + long long t1,t2,t3,t4,t5,t6,t7,t8,t9; + + sd(t); + while(t--){ + scanf("%lld",&n); + if(n == 0){ + cout<<0< 0){ + if(n%2 == 0){ + t1 = (M[0][0] * M[0][0] + M[0][1]*M[1][0] + M[0][2]*M[2][0])%MOD; + t2 = (M[0][0] * M[0][1] + M[0][1]*M[1][1] + M[0][2]*M[2][1])%MOD; + t3 = (M[0][0] * M[0][2] + M[0][1]*M[1][2] + M[0][2]*M[2][2])%MOD; + + t4 = (M[1][0] * M[0][0] + M[1][1]*M[1][0] + M[1][2]*M[2][0])%MOD; + t5 = (M[1][0] * M[0][1] + M[1][1]*M[1][1] + M[1][2]*M[2][1])%MOD; + t6 = (M[1][0] * M[0][2] + M[1][1]*M[1][2] + M[1][2]*M[2][2])%MOD; + + t7 = (M[2][0] * M[0][0] + M[2][1]*M[1][0] + M[2][2]*M[2][0])%MOD; + t8 = (M[2][0] * M[0][1] + M[2][1]*M[1][1] + M[2][2]*M[2][1])%MOD; + t9 = (M[2][0] * M[0][2] + M[2][1]*M[1][2] + M[2][2]*M[2][2])%MOD; + + M[0][0]=t1, M[0][1]=t2, M[0][2]=t3; + M[1][0]=t4, M[1][1]=t5, M[1][2]=t6; + M[2][0]=t7, M[2][1]=t8, M[2][2]=t9; + n = n>>1; + } + else{ + t1 = (R[0][0] * M[0][0] + R[0][1]*M[1][0] + R[0][2]*M[2][0])%MOD; + t2 = (R[0][0] * M[0][1] + R[0][1]*M[1][1] + R[0][2]*M[2][1])%MOD; + t3 = (R[0][0] * M[0][2] + R[0][1]*M[1][2] + R[0][2]*M[2][2])%MOD; + + t4 = (R[1][0] * M[0][0] + R[1][1]*M[1][0] + R[1][2]*M[2][0])%MOD; + t5 = (R[1][0] * M[0][1] + R[1][1]*M[1][1] + R[1][2]*M[2][1])%MOD; + t6 = (R[1][0] * M[0][2] + R[1][1]*M[1][2] + R[1][2]*M[2][2])%MOD; + + t7 = (R[2][0] * M[0][0] + R[2][1]*M[1][0] + R[2][2]*M[2][0])%MOD; + t8 = (R[2][0] * M[0][1] + R[2][1]*M[1][1] + R[2][2]*M[2][1])%MOD; + t9 = (R[2][0] * M[0][2] + R[2][1]*M[1][2] + R[2][2]*M[2][2])%MOD; + + R[0][0]=t1, R[0][1]=t2, R[0][2]=t3; + R[1][0]=t4, R[1][1]=t5, R[1][2]=t6; + R[2][0]=t7, R[2][1]=t8, R[2][2]=t9; + + + t1 = (M[0][0] * M[0][0] + M[0][1]*M[1][0] + M[0][2]*M[2][0])%MOD; + t2 = (M[0][0] * M[0][1] + M[0][1]*M[1][1] + M[0][2]*M[2][1])%MOD; + t3 = (M[0][0] * M[0][2] + M[0][1]*M[1][2] + M[0][2]*M[2][2])%MOD; + + t4 = (M[1][0] * M[0][0] + M[1][1]*M[1][0] + M[1][2]*M[2][0])%MOD; + t5 = (M[1][0] * M[0][1] + M[1][1]*M[1][1] + M[1][2]*M[2][1])%MOD; + t6 = (M[1][0] * M[0][2] + M[1][1]*M[1][2] + M[1][2]*M[2][2])%MOD; + + t7 = (M[2][0] * M[0][0] + M[2][1]*M[1][0] + M[2][2]*M[2][0])%MOD; + t8 = (M[2][0] * M[0][1] + M[2][1]*M[1][1] + M[2][2]*M[2][1])%MOD; + t9 = (M[2][0] * M[0][2] + M[2][1]*M[1][2] + M[2][2]*M[2][2])%MOD; + + M[0][0]=t1, M[0][1]=t2, M[0][2]=t3; + M[1][0]=t4, M[1][1]=t5, M[1][2]=t6; + M[2][0]=t7, M[2][1]=t8, M[2][2]=t9; + n = (n-1)>>1; + } + //cout< +#include +#include +using namespace std; + +int main() +{ + int n, m, k; + cin >> n >> m; + vector v; + for(int i=0; i> k; + v.push_back(k); + } + sort(v.rbegin(), v.rend()); + for(int i=0; i +#include +#include +#include + +using namespace std; +vectordp(1000000); + +long long calc(long long num,long long m) +{ + long long i,j,k; + if(dp[num]!=0) + return dp[num]%m; + else + { + j=num; + i=1; + dp[num]=1; + while(j>=1 && i<=num) + { + dp[num]=dp[num]*(long long)pow((double)i,j)%m; + //dp[num]=dp[num]; + i++; + j--; + } + cout<>test; + while(test--) + { + cin>>num>>m>>query; + for(i=0;i>q; + k=(calc(num,m)/(calc(q,m)*calc(num-q,m))); + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 200005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int par[MAX]; +int find (int u) { + if (u == par[u]) return u; + + return par[u] = find (par[u]); +} +void Set (int a, int b) { + int aa = find (a); + int bb = find (b); + + if (aa != bb) { + par[bb] = aa; + } +} +int main () { + __FastIO; + int n, m; + cin >> n >> m; + mapmp; + + for (int i = 1; i < MAX; i++) { + par[i] = i; + } + + int k = 1; + + for (int i = 1; i <= m; i++) { + int a, b; + cin >> a >> b; + + if (mp[a] == 0) { + mp[a] = k++; + } + + if (mp[b] == 0) { + mp[b] = k++; + } + + Set (mp[a], mp[b]); + } + + int cnt = 0; + int chk[MAX]; + SET (chk, 0); + k--; + + for (int i = 1; i <= k; i++) { + int x = find (i); + + if (chk[x] == 0) { + cnt++; + chk[x] = 1; + } + } + + int ans = cnt + (n - k); + cout << ans << "\n"; + return 0; +} diff --git a/spoj_cpp_clean/FREQUENT/FREQUENT-3.cpp b/spoj_cpp_clean/FREQUENT/FREQUENT-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b2cd4c2dfc720af53b853315ab10a6e8bd9cc44 --- /dev/null +++ b/spoj_cpp_clean/FREQUENT/FREQUENT-3.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + int bl, br, b; + + void assignLeaf(int value) { + bl = br= b = value; + } +}; + +int n, array[1<<17], m , X, Y, temp; +node tree[1<<18][3]; + + +// l and r represent the range node i is responsible for. A single call init(1, 0, n-1) builds the segment tree in O(N) time +void init(int i, int l, int r){ + if(l == r){ + tree[i].assignLeaf(1); + } + else{ + int m = (l+r)/2; + init(2*i, l, m); + init((2*i) + 1, m + 1, r); + node left = tree[2*i], right = tree[(2*i)+1]; + + if(array[m] == array[m+1]){ + tree[i].bl = tree[2*i].bl + tree[(2*i)+1].bl*(array[l] == array[m]); + tree[i].br = tree[(2*i)+1].br + tree[2*i].br*(array[r] == array[m+1]); + tree[i].b = max(tree[2*i].br + tree[(2*i)+1].bl, max(tree[2*i].b, tree[(2*i)+1].b)); + } + else{ + tree[i].bl = tree[2*i].bl; + tree[i].br = tree[(2*i)+1].br; + tree[i].b = max(tree[2*i].b, tree[(2*i)+1].b); + } + } + return; +} + +// O(log N) +int query(int i, int l, int r, int lo, int hi){ + if(l == lo and r == hi) //If the range we want to query is the exact range + return tree[i].b; + int mid = (l+r)/2; + if(lo > mid) //If only the subtree rooted at right child is repsonsible for this range + return query((2*i)+1, mid+1, r, lo, hi); + if(hi <= mid) //If only the subtree rooted at the left child is responsible for this range + return query(2*i, l, mid, lo, hi); + //If both, the left and right children are partially responsible for the range + int left = query(2*i, l, mid, lo, mid); + int right = query((2*i)+1, mid+1, r, mid+1, hi); + + if(array[mid] == array[mid+1]){ + temp = min(tree[2*i].br, mid-lo+1) + min(tree[(2*i)+1].bl, hi-mid); + return max(temp,max(left,right)); + } + else{ + return max(left, right); + } +} + +int main(){ + while(scanf("%d",&n) == 1 and n){ + sd(m); + for(int i = 0; i < n; i++) sd(array[i]); + + init(1, 0, n-1); + + while(m--){ + sd2(X,Y); + printf("%d\n",query(1,0,n-1,X-1,Y-1)); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/FREQUENT/FREQUENT-9.cpp b/spoj_cpp_clean/FREQUENT/FREQUENT-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d8290795e6e1a5e5ad420458f62a6b27399c299 --- /dev/null +++ b/spoj_cpp_clean/FREQUENT/FREQUENT-9.cpp @@ -0,0 +1,172 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +typedef struct +{ + int max_occ_num; + int max_occ; + int max_num; + int max_num_occ; + int min_num; + int min_num_occ; +}node; + +node tree[100007*4]; +int A[100007]; +int N, Q; + +void inittree(int index, int a, int b) +{ + if(a==b) + { + tree[index].max_occ_num=A[a]; + tree[index].max_occ=1; + tree[index].max_num=A[a]; + tree[index].max_num_occ=1; + tree[index].min_num=A[a]; + tree[index].min_num_occ=1; + + return; + } + + inittree(index*2, a, (a+b)/2); + inittree(index*2+1, (a+b)/2+1, b); + + tree[index].max_occ_num=0; + tree[index].max_occ=0; + tree[index].max_num=0; + tree[index].max_num_occ=0; + tree[index].min_num=0; + tree[index].min_num_occ=0; + + tree[index].min_num = tree[index*2].min_num; + tree[index].min_num_occ = tree[index*2].min_num_occ; + if(tree[index].min_num == tree[index*2+1].min_num) + tree[index].min_num_occ+=tree[index*2+1].min_num_occ; + + tree[index].max_num = tree[index*2+1].max_num; + tree[index].max_num_occ = tree[index*2+1].max_num_occ; + if(tree[index].max_num == tree[index*2].max_num) + tree[index].max_num_occ+=tree[index*2].max_num_occ; + + if(tree[index*2].max_num == tree[index*2+1].min_num) + { + tree[index].max_occ_num = tree[index*2].max_num; + tree[index].max_occ = tree[index*2].max_num_occ + tree[index*2+1].min_num_occ; + } + + if(tree[index].max_occ < tree[index*2].max_occ) + { + tree[index].max_occ = tree[index*2].max_occ; + tree[index].max_occ_num = tree[index*2].max_occ_num; + } + + if(tree[index].max_occ < tree[index*2+1].max_occ) + { + tree[index].max_occ = tree[index*2+1].max_occ; + tree[index].max_occ_num = tree[index*2+1].max_occ_num; + } +} + +node query(int index, int a, int b, int i, int j) +{ + node left, right, treeindex; + + if(a==i && b==j) + return tree[index]; + + if(j<=(a+b)/2) + return query(index*2, a, (a+b)/2, i, j); + if(i>(a+b)/2) + return query(index*2+1, (a+b)/2+1, b, i, j); + + left=query(index*2, a, (a+b)/2, i, (a+b)/2); + right=query(index*2+1, (a+b)/2+1, b, (a+b)/2+1, j); + + treeindex.max_occ_num=0; + treeindex.max_occ=0; + treeindex.max_num=0; + treeindex.max_num_occ=0; + treeindex.min_num=0; + treeindex.min_num_occ=0; + + treeindex.min_num = left.min_num; + treeindex.min_num_occ = left.min_num_occ; + if(treeindex.min_num == right.min_num) + treeindex.min_num_occ+=right.min_num_occ; + + treeindex.max_num = right.max_num; + treeindex.max_num_occ = right.max_num_occ; + if(treeindex.max_num == left.max_num) + treeindex.max_num_occ+=left.max_num_occ; + + if(left.max_num == right.min_num) + { + treeindex.max_occ_num = left.max_num; + treeindex.max_occ = left.max_num_occ + right.min_num_occ; + } + + if(treeindex.max_occ < left.max_occ) + { + treeindex.max_occ = left.max_occ; + treeindex.max_occ_num = left.max_occ_num; + } + + if(treeindex.max_occ < right.max_occ) + { + treeindex.max_occ = right.max_occ; + treeindex.max_occ_num = right.max_occ_num; + } + + return treeindex; +} + +bool init() +{ + int i; + + scanf("%d", &N); + + if(N==0) + return 0; + + scanf("%d", &Q); + + for(i=1; i<=N; i++) + scanf("%d", &A[i]); + + inittree(1, 1, N); + + return 1; +} + + +int main() +{ + int i; + int s, f; + + //freopen("input.txt","r",stdin); + + while(init()) + for(i=0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +using namespace std; + +void fastint( register int *n ){ + register char c; + *n = 0; + while(!isdigit(c = getc( stdin ))); + do { + switch( c ){ + case ' ': case '\n': return; + default: (*n) *= 10; (*n) += ( c - '0' ); break; + } + }while( ( c = getc( stdin ) )); +} + +class UFDS { +private: + vector p, rank, setSizes; + int numSets; +public: + UFDS(int N) { + numSets = N; + rank.assign(N, 0); + p.assign(N, 0); + for (int i = 0; i < N; i++) + p[i] = i; + setSizes.assign(N, 1); + } + int findSet(int i) { + return (p[i] == i) ? i : p[i] = findSet(p[i]); + } + bool isSameSet(int i, int j) { + return findSet(i) == findSet(j); + } + void unionSet(int i, int j) { + if (!isSameSet(i, j)) { + int x = findSet(i), y = findSet(j); + if (rank[x] > rank[y]) { + setSizes[findSet(x)] += setSizes[findSet(y)]; + p[y] = x; + } else { + setSizes[findSet(y)] += setSizes[findSet(x)]; + p[x] = y; + if (rank[x] == rank[y]) + rank[y]++; + } + numSets--; + } + } + int setSize(int i) { + return setSizes[findSet(i)]; + } + int numDisjointSets() { + return numSets; + } +}; + +map nameMap; + +int main() { + int tc, f, count; + char s1[25], s2[25]; + string s1s, s2s; + fastint(&tc); + while (tc--) { + fastint(&f); + UFDS uf(f * 2); + nameMap.clear(); + count = 0; + + for (int i = 0; i < f; i++) { + scanf("%s %s", s1, s2); + s1s = string(s1); + s2s = string(s2); + + if (!nameMap.count(s1s)) + nameMap[s1s] = count++; + if (!nameMap.count(s2s)) + nameMap[s2s] = count++; + + uf.unionSet(nameMap[s1s], nameMap[s2s]); + printf("%d\n", uf.setSize(nameMap[s1])); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/FROTATAX/FROTATAX-10.cpp b/spoj_cpp_clean/FROTATAX/FROTATAX-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6a2edba3b0f287cae93ce63a28ff73bd3d1ae5cd --- /dev/null +++ b/spoj_cpp_clean/FROTATAX/FROTATAX-10.cpp @@ -0,0 +1,10 @@ +#include + +using namespace std; + +int main() +{ + float a, g, ra, rg; + cin >> a >> g >> ra >> rg; + cout << (((ra / a) > (rg / g)) ? 'A' : 'G'); +} diff --git a/spoj_cpp_clean/FUGIT09/FUGIT09-10.cpp b/spoj_cpp_clean/FUGIT09/FUGIT09-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d5efae9dfe6ce7d694542c209f9de67fd08e1214 --- /dev/null +++ b/spoj_cpp_clean/FUGIT09/FUGIT09-10.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +int main() +{ + int n, step; + float maxdistance, currx = 0, curry = 0; + char dir; + cin >> n >> maxdistance; + maxdistance *= maxdistance; + while(n-- > 0) + { + cin >> dir >> step; + switch(dir) + { + case 'N': curry += step; break; + case 'S': curry -= step; break; + case 'O': currx += step; break; + case 'L': currx -= step; break; + } + if(abs(currx * currx) + abs(curry * curry) > maxdistance) + { + cout << 1; + return 0; + } + } + cout << 0; + return 0; +} diff --git a/spoj_cpp_clean/GCD/GCD-5.cpp b/spoj_cpp_clean/GCD/GCD-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bcbaf2d5f6bd25c181c250180dc033d706b41a8 --- /dev/null +++ b/spoj_cpp_clean/GCD/GCD-5.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main() +{ + int a,b,c,rem; + cin>>a>>b; + while(b) + { + rem=a%b; + a=b; + b=rem; + } + cout< +#include +#include +#include +#define FOR(i,a,b) for(int i=a;i>B; + if(a==0) + { + cout< +#include +#include + +using namespace std; + +char b[251]; + +int func(int a) +{ + int len=strlen(b); + int rem=0; + for(int i=0;i>test; + while(test--){ + scanf("%d %s",&a,&b); + if(a==0) + printf("%s\n",b); + + else if(b[0]=='0') + printf("%d\n",a); + else{ + int rem=func(a); + printf("%d\n",gcd(a,rem)); + } + } + return 0; +} diff --git a/spoj_cpp_clean/GCDEX/GCDEX-15.cpp b/spoj_cpp_clean/GCDEX/GCDEX-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aa321d0398fa157188a9bdd734df5b6c45240d6 --- /dev/null +++ b/spoj_cpp_clean/GCDEX/GCDEX-15.cpp @@ -0,0 +1,38 @@ +/* +* author: @j-tesla +*/ + +// C++14 + +#pragma GCC optimize ("O3") +#pragma GCC target ("sse4") + +#include + +using namespace std; + +#define uns unsigned +typedef long long ll; +typedef vector vb; +typedef vector vc; +typedef vector vi; +typedef pair ii; +typedef vector vu; +typedef vector vl; + +#define din(x) int x; cin >> x; +#define in(x) cin >> x; +#define full(x) x.begin(), x.end() + +#define eb emplace_back +#define ins insert +#define f first +#define s second + + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/GCDEX/GCDEX-3.cpp b/spoj_cpp_clean/GCDEX/GCDEX-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..187e36735988dbc813f3a1615e168e5e6cd7dfe9 --- /dev/null +++ b/spoj_cpp_clean/GCDEX/GCDEX-3.cpp @@ -0,0 +1,81 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< + +using namespace std; + +int main() +{ + int test, n, factorIndex, inputArray[1000], i, j, k, gcd, factors[100]; + cin>>test; + + while(test--) + { + cin>>n; + for(i=0;i>inputArray[i]; + } + k=inputArray[0]; + j=0; + while(k) + { + if(inputArray[0]%k==0) + { + factors[j]=k; + j++; + } + k--; + } + j=0; + + while(1) + { + for(i=0;i +#include +using namespace std; + +long int gcd(long int a,long int b ) +{ + long int rem; + while(b!=0){ + rem = a%b; + a = b; + b = rem; + } + return a; +} +int main() +{ + long int i,t,n,q,l,r,result; + scanf("%ld",&t); + while(t--) + { + scanf("%ld %ld",&n,&q); + + long int* arr=new long int[n]; + long int* pre=new long int[n]; + long int* suf=new long int[n]; + + for(i=0;i=0;i--) + { + suf[i]=gcd(suf[i+1],arr[i]); + } + while(q--) + { + scanf("%ld %ld",&l,&r); + l--; + r--; + if(l==0) + result=suf[r+1]; + else if(r==n-1) + result=pre[l-1]; + else + result=gcd(pre[l-1],suf[r+1]); + printf("%ld\n",result); + //cout< + +#define FI first +#define SE second + +typedef long long ll; +typedef std::pair pairll; + +const static ll INF = 98765432100LL; + +static inline void setio(void); + +const ll MAX_SIEVE_NUM = 10000002; +const ll NUM_PRIMES = 664579; +ll *primes; + +std::unordered_map rank; + +static void one_case(void) { + for (ll i = 0; i < NUM_PRIMES; ++i) + primes[i] = std::abs(primes[i]); + + ll n; std::cin >> n; + for (ll num; n--; ) { + std::cin >> num; + for (ll i = 2, lim = std::ceil(std::sqrt(num)); i <= lim && num > 1; ++i) { + if (num % i) continue; + primes[rank[i]] = -std::abs(primes[rank[i]]); + while (num % i == 0) num /= i; + } + if (num > 1) primes[rank[num]] = -std::abs(primes[rank[num]]); + } + ll ans = 0; + for (; ans < NUM_PRIMES && primes[ans] <= 0; ++ans); + std::cout << primes[ans] << "\n"; +} + +int main(void) { + setio(); + + primes = new ll[NUM_PRIMES]; + + char *sieve = new char[MAX_SIEVE_NUM]; + std::fill(sieve, sieve+MAX_SIEVE_NUM, true); + sieve[0] = sieve[1] = false; + + for (ll i = 2, cnt = 0; i < MAX_SIEVE_NUM; ++i) { + if (!sieve[i]) continue; + + rank[i] = cnt; + primes[cnt++] = i; + + for (ll j = i+i; j < MAX_SIEVE_NUM; j += i) + sieve[j] = false; + } + + delete [] sieve; + + ll n; std::cin >> n; + while (n--) one_case(); + + delete [] primes; + + return 0; +} + +static inline void setio(void) { + std::ios::sync_with_stdio(false);std::cin.tie(nullptr); + std::cout.precision(10); std::cout << std::fixed; +} diff --git a/spoj_cpp_clean/GCJ102A/GCJ102A-9.cpp b/spoj_cpp_clean/GCJ102A/GCJ102A-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb934e0f0391542ad2524a86e166c36e99bedcb5 --- /dev/null +++ b/spoj_cpp_clean/GCJ102A/GCJ102A-9.cpp @@ -0,0 +1,86 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +using namespace std; + +const int MAXN = 155, INF = 1000000005; + +int TC = 1, T, N, A [MAXN][MAXN]; + +inline bool valid (int r, int c) +{ + return r >= 0 && r < N && c >= 0 && c < N; +} + +inline bool sym1 (int num) +{ + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + if (i + j < num) + { + int sum = 2 * num - (i + j); + int diff = i - j; + + int a = (sum + diff) / 2, b = (sum - diff) / 2; + + if (valid (a, b) && A [i][j] != A [a][b]) + return false; + } + + return true; +} + +inline bool sym2 (int num) +{ + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + if (i - j < num) + { + int sum = i + j; + int diff = 2 * num - (i - j); + + int a = (sum + diff) / 2, b = (sum - diff) / 2; + + if (valid (a, b) && A [i][j] != A [a][b]) + return false; + } + + return true; +} + +int main () +{ + for (scanf ("%d", &T); TC <= T; TC++) + { + scanf ("%d", &N); + + for (int sum = 0; sum <= 2 * (N - 1); sum++) + for (int i = max (0, sum - (N - 1)); i <= min (sum, N - 1); i++) + scanf ("%d", &A [i][sum - i]); + + int bestsum = INF, bestdiff = INF; + + for (int sum = 0; sum <= 2 * (N - 1); sum++) + if (sym1 (sum)) + bestsum = min (bestsum, abs (sum - (N - 1))); + + for (int diff = -(N - 1); diff <= N - 1; diff++) + if (sym2 (diff)) + bestdiff = min (bestdiff, abs (diff)); + + printf ("Case #%d: %d\n", TC, (N + bestsum + bestdiff) * (N + bestsum + bestdiff) - N * N); + } + + return 0; +} + diff --git a/spoj_cpp_clean/GCJ1C09A/GCJ1C09A-9.cpp b/spoj_cpp_clean/GCJ1C09A/GCJ1C09A-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd2fc87eeab33da89d1c10ebc045d4460c42f667 --- /dev/null +++ b/spoj_cpp_clean/GCJ1C09A/GCJ1C09A-9.cpp @@ -0,0 +1,51 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +char input[100]; +int value[256], vused; + +int main() { + int T; + scanf("%d ",&T); + for (int test=1;test<=T;test++) { + scanf("%s ",input); + for (char c = 'a'; c <= 'z'; c++) {value[c] = 0;} + for (char c = '0'; c <= '9'; c++) {value[c] = 0;} + vused = 0; + + for (int i = 0; i < strlen(input); i++) { + if (value[input[i]] == 0) { + value[input[i]] = ++vused; + } + } + if (vused <= 1) {vused = 2;} + long long ans = 0; + for (int i = 0; i < strlen(input); i++) { + long long tmp = value[input[i]]; + if (tmp == 1) { + } else if (tmp == 2) { + tmp = 0; + } else { + tmp--; + } + ans = ans*vused + tmp; + } + printf("Case #%d: %lld\n",test,ans); + } +} diff --git a/spoj_cpp_clean/GERGOVIA/GERGOVIA-21.cpp b/spoj_cpp_clean/GERGOVIA/GERGOVIA-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2958ffd90c1632970201d37d779d30f26ce39154 --- /dev/null +++ b/spoj_cpp_clean/GERGOVIA/GERGOVIA-21.cpp @@ -0,0 +1,23 @@ +/* + greedy + difficulty: easy + date: 23/Apr/2020 + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +int main() { + int N; + while (cin >> N && N) { + vector A(N); for (int &a : A) cin >> a; + + ll cost = 0; + for (int i = 0; i+1 < N; i++) + A[i+1] += A[i], cost += abs(A[i]); + + cout << cost << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/GERGOVIA/GERGOVIA-7.cpp b/spoj_cpp_clean/GERGOVIA/GERGOVIA-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82dd95d6bc2e8008298607e7ade97cc2018d3df2 --- /dev/null +++ b/spoj_cpp_clean/GERGOVIA/GERGOVIA-7.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main() { + + int t,n; + long long trabajo=0; + + while(scanf("%d",&n)!=EOF && n!=0) + { + trabajo=0; + int arr[n]; + for(int i=0 ; i +#include +#include + +using namespace std; + +int main() +{ + ios::sync_with_stdio(false); + int g, b; + for (cin >> g >> b; g != -1 && b != -1; cin >> g >> b) { + float MAX = max(g, b), MIN = min(g, b) + 1; + float resp = (MAX / MIN); + cout << ceil(resp) << "\n"; + } + return 0; +} diff --git a/spoj_cpp_clean/GIRLSNBS/GIRLSNBS-16.cpp b/spoj_cpp_clean/GIRLSNBS/GIRLSNBS-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1ede6543a2bf2370136e24a9aa92a616ab67a61 --- /dev/null +++ b/spoj_cpp_clean/GIRLSNBS/GIRLSNBS-16.cpp @@ -0,0 +1,42 @@ +#include + +#define ll long long + +using namespace std; + +void solve(ll a,ll b){ + ll ans; + if(a==0 && b==0){ + ans=0; + } + else if(a==b){ + ans=1; + } + else if(a>b){ + ans = a/(b+1); + if(a%(b+1)!=0){ + ans+=1; + } + } + else{ + ans = b/(a+1); + if(b%(a+1)!=0){ + ans+=1; + } + } + cout<>a>>b; + if(a==-1 || b==-1){ + break; + } + else{ + solve(a,b); + } + } + return 0; +} diff --git a/spoj_cpp_clean/GIRLSNBS/GIRLSNBS-19.cpp b/spoj_cpp_clean/GIRLSNBS/GIRLSNBS-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68a7f1c6eda3fb8a80f9c15c2d58d57b7246b3a8 --- /dev/null +++ b/spoj_cpp_clean/GIRLSNBS/GIRLSNBS-19.cpp @@ -0,0 +1,57 @@ +#include +#define ll long long +#define endl "\n" +#define MAX 100001 +#define mod 1000000007 +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + while(true){ + int G, B; + cin>>G>>B; + int ans; + if(G == -1 && B == -1){ + break; + } + else{ + cout<G){ + if(B%(G+1)==0){ + ans = B/(G+1); + } + else{ + ans = (B/(G+1)) + 1; + } + } + else{ + if(G%(B+1)==0){ + ans = G/(B+1); + } + else{ + ans = (G/(B+1)) + 1; + } + } + cout< +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + + +using namespace std; + +int main(){ + int g, b; + sd2(g,b); + while(g!= -1 and b != -1){ + if( g == 0 or b == 0) + cout< +#include + +using namespace std; + +int main() +{ + int x,y,i,j,k; + cin>>x>>y; + while(x>=0) + { + if(x==0&&y==0) + cout<<0<>x>>y; + } + return 0; +} diff --git a/spoj_cpp_clean/GIRLSNBS/GIRLSNBS-9.cpp b/spoj_cpp_clean/GIRLSNBS/GIRLSNBS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52f8d6e9af83c7defef098b5226931914c322ab7 --- /dev/null +++ b/spoj_cpp_clean/GIRLSNBS/GIRLSNBS-9.cpp @@ -0,0 +1,31 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int main() +{ + float G, B; + + scanf("%f %f", &G, &B); + + while(G!=-1 && B!=-1) + { + printf("%d\n", (int)ceil(max(G,B)/(min(G,B)+1))); + scanf("%f %f", &G, &B); + } + + return 0; +} diff --git a/spoj_cpp_clean/GIVEAWAY/GIVEAWAY-14.cpp b/spoj_cpp_clean/GIVEAWAY/GIVEAWAY-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..233ba7dd6b9796604ab1f10cf4f836aded26990f --- /dev/null +++ b/spoj_cpp_clean/GIVEAWAY/GIVEAWAY-14.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +#define ll long long +#define block 720 + +vector v[block]; +ll ara[100009]; + +ll query(ll l, ll r, ll c) + +{ + ll lb = l / block, rb = r / block; + + ll cnt = 0; + if(lb == rb) { + for(ll i = l; i <= r; i++) + if(ara[i] >= c) + cnt++; + + return cnt; + } + + for(ll i = l; i < (lb + 1) * block; i++) + if(ara[i] >= c) + cnt++; + for(ll i = rb * block; i <= r; i++) + if(ara[i] >= c) + cnt++; + + for(ll i = lb + 1; i < rb; i++) { + ll pos = lower_bound(v[i].begin(), v[i].end(), c) - v[i].begin(); + cnt += (v[i].size() - pos); + } + + return cnt; +} + +void update(ll id, ll val) + +{ + ll tmp = ara[id]; + ara[id] = val; + + ll b = id / block; + ll pos = lower_bound(v[b].begin(), v[b].end(), tmp) - v[b].begin(); + v[b][pos] = ara[id]; + sort(v[b].begin(), v[b].end()); +} + +int main() + +{ + ll n; + cin >> n; + for(ll i = 0; i < n; i++) { + ll in; + scanf("%lld", &in); + v[i / block].push_back(in); + ara[i] = in; + } + for(ll i = 0; i < block; i++) + sort(v[i].begin(), v[i].end()); + + ll q; + cin >> q; + while(q--) { + ll t; + scanf("%lld", &t); + if(t == 0) { + ll a, b, c; + scanf("%lld %lld %lld", &a, &b, &c); + printf("%lld\n", query(a - 1, b - 1, c)); + } + else { + ll a, b; + scanf("%lld %lld", &a, &b); + update(a - 1, b); + } + } + + return 0; diff --git a/spoj_cpp_clean/GIVEAWAY/GIVEAWAY-8.cpp b/spoj_cpp_clean/GIVEAWAY/GIVEAWAY-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..933add0c38821ef0d7d6137633bc174a53408118 --- /dev/null +++ b/spoj_cpp_clean/GIVEAWAY/GIVEAWAY-8.cpp @@ -0,0 +1,168 @@ +/*************************************************** + * Problem Name : GIVEAWAY - Give Away.cpp + * Problem Link : http://www.spoj.com/problems/GIVEAWAY/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-05-25 + * Problem Type : Data Structure (sqrt root decomposition) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 500005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorblock[750]; +int ar[MAX]; +int blockSz, N; + +//~ int Lower(int id, int value) { + //~ int high = (int)block[id].size()-1, low = 0, ans = -1; + //~ while (high >= low) { + //~ int mid = (high + low) / 2; + //~ if (block[id][mid] == value) { + //~ ans = mid; + //~ high = mid - 1; + //~ } else if (block[id][mid] > value) { + //~ high = mid - 1; + //~ } else { + //~ low = mid + 1; + //~ } + //~ } + //~ if (ans == -1) { + //~ ans = high+1; + //~ } + //~ return ans; +//~ } + +void update(int i, int pre, int value) { + int id = i/blockSz; + ar[i] = value; + //~ int l = id * blockSz; + //~ int r = l + block[id].size() -1; + //~ block[id].clear(); + //~ for(int i = l; i<=r; i++){ + //~ block[id].pb(ar[i]); + //~ } + + int x = (int)(lower_bound(all(block[id]), pre) - block[id].begin()); + //~ int x = Lower(id, value); + block[id][x] = value; + sort(all(block[id])); +} + +int query(int L, int R, int value) { + int l = L/blockSz; + int r = R/blockSz; + if (l == r) { + int sum = 0; + for (int i = L; i <= R; i++) { + if(ar[i] >= value) sum++; + } + return sum; + } + int sum = 0; + for (int i = L; i < (l + 1)*blockSz; i++) { + if(ar[i] >=value) sum++; + } + for (int i = l + 1; i < r; i++) { + //~ cout << block[i][0];nl; + //~ cout << block[i][1];nl; + //~ cout << block[i].size();nl; + int x = (int)(lower_bound(all(block[i]), value) - block[i].begin()); + //~ int x = Lower(i, value); + //~ cout << " i : " << x <<"\n"; + int sz = (int)block[i].size(); + //~ cout <=value) sum++; + } + return sum; +} +int main () { + __FastIO; + int Q; + cin >> N; + //~ ar.resize(N); + blockSz = (int)sqrt(N); + //~ cout << blockSz <<"\n"; + //~ return 0; + for(int i = 0; i> ar[i]; + int id = i/blockSz; + block[id].pb(ar[i]); + } + //~ int x ; + //~ cin >>x ; + //~ int X = (int)(lower_bound(all(ar), x) - ar.begin()); + //~ int X = lower(x); + //~ cout << X<<"\n"; + + for(int i = 0; i<=blockSz; i++) { + sort(all(block[i])); + } + cin >> Q; + for(int q = 1; q<=Q; q++) { + int c, l, r; + int value; + cin >> c; + if(c == 0) { + cin >> l >> r >> value; + int ans = query(l-1, r-1, value); + cout << ans <<"\n"; + } + else { + cin >> l >> value; + int id = l-1; + int pre = ar[id]; + update(id, pre, value); + } + } + return 0; +} + diff --git a/spoj_cpp_clean/GLJIVE/GLJIVE-4.cpp b/spoj_cpp_clean/GLJIVE/GLJIVE-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f51a34bda232e0a9f21b93dc28e72da6a1042a0f --- /dev/null +++ b/spoj_cpp_clean/GLJIVE/GLJIVE-4.cpp @@ -0,0 +1,119 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[10], mx=0; +/* +void calc(int i, int sum) +{ + if(abS(sum-100)9) + return ; + calc(i+1,sum); + calc(i+1,sum+a[i]); + +} +*/ + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + FOR(i,0,10) + a[i]=haint(); + //calc(0,0); + int sum=0; + FOR(i,0,10) + { + sum+=a[i]; + if(abS(sum-100) implementation + difficulty: easy + date: 21/Feb/2020 + by: @brpapa +*/ +#include +#include +using namespace std; +#define VISITED -1 + +int grid[22][22]; +int R, C; + +vector binDecoded; +int d; +int dr[4] = {0, 1, 0,-1}; +int dc[4] = {1, 0,-1, 0}; + +void walk(int r, int c) { + binDecoded.push_back(grid[r][c]); + grid[r][c] = VISITED; + + bool has = false; + for (int i = 0; !has && i < 4; i++) { + int nr = r+dr[d], nc = c+dc[d]; + if (nr < 0 || nr == R || nc < 0 || nc == C || grid[nr][nc] == VISITED) + d = (d+1)%4; + else has = true; + } + + if (has) walk(r+dr[d], c+dc[d]); +} + +int main() { + int T; cin >> T; + for (int t = 1; t <= T; t++) { + cin >> R >> C; + string bin; cin >> bin; + for (int b = 0; b < R*C; b++) + grid[b/C][b%C] = bin[b] == '1'? 1:0; + + d = 0; + binDecoded.clear(); + walk(0, 0); + + vector ans; + for (int i = 0; i < R*C - (R*C)%5; i += 5) { + int dec = 0; + for (int j = 4; j >= 0; j--) + if (binDecoded[i+4-j]) + dec |= (1 << j); + ans.push_back(dec == 0? ' ' : 'A'+dec-1); + } + + cout << t << " "; + for (char c : ans) cout << c; + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/GNY07H/GNY07H-3.cpp b/spoj_cpp_clean/GNY07H/GNY07H-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f76333264ecce1cc523b77278eecdedc22b2f525 --- /dev/null +++ b/spoj_cpp_clean/GNY07H/GNY07H-3.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int n, w; + int a[101], b[101], c[101]; + sd(n); + a[0] = 1; + a[1] = 1; + b[0] = 0; + b[1] = 1; + c[0] = 0; + c[1] = 1; + + for(int i = 2; i <= 100; i++){ + a[i] = a[i-1] + a[i-2] + b[i-1] + 2*c[i-1]; + b[i] = a[i-1] + b[i-2]; + c[i] = a[i-1] + c[i-1]; + } + for(int i = 1; i <= n; i++){ + sd(w); + printf("%d %d\n",i,a[w]); + } + return 0; +} diff --git a/spoj_cpp_clean/GNYR09F/GNYR09F-9.cpp b/spoj_cpp_clean/GNYR09F/GNYR09F-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4eb9b0c82b68f8dd23552c350028210b5a6b368d --- /dev/null +++ b/spoj_cpp_clean/GNYR09F/GNYR09F-9.cpp @@ -0,0 +1,47 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +unsigned long long DP[110][110][2]; +int N, K; + +int main() +{ + int i, j, k, T, N, K; + //freopen("input.txt","r",stdin); + + DP[1][0][0] = 1; + DP[1][0][1] = 1; + + for(i=2; i<=109; i++) + for(j=0; j<=109; j++) + { + DP[i][j][0] = DP[i-1][j][1] + DP[i-1][j][0]; + if(j > 0) + DP[i][j][1] = DP[i-1][j-1][1]; + DP[i][j][1]+=DP[i-1][j][0]; + } + + scanf("%d", &T); + + for(i=1; i<=T; i++) + { + scanf("%d %d %d", &i, &N, &K); + cout< +using namespace std; +typedef long long ll; + +struct Point { + double x, y; + Point() {} + Point(double _x, double _y) : x(_x), y(_y) {} + Point(const Point &P) : Point(P.x, P.y) {} + + bool operator==(const Point &O) { return x == O.x && y == O.x; } + bool operator!=(const Point &O) { return !((*this) == O); } + bool operator<(const Point &O) { return x != O.x ? (x < O.x) : (y < O.x); } + + Point operator-() { return Point(-x, -y); } + Point operator+(const Point &O) { return Point(x + O.x, y + O.y); } + Point operator-(const Point &O) { return Point(x - O.x, y - O.y); } +}; + +struct Vector : Point { + Vector() {} + Vector(double _x, double _y) : Point(_x, _y) {} + Vector(Point P) : Point(P.x, P.y) {} + Vector(Point P, Point Q) : Vector(Q-P) {} + + double norm() { return sqrt(x*x + y*y); } + double norm2() { return x*x + y*y; } +}; + +Point translate(Point P, Vector V) { + return Point(P.x + V.x, P.y + V.y); +} + +Vector scale(Vector V, double k) { + return Vector(V.x * k, V.y * k); +} + +double dot(Vector V, Vector U) { + return V.x*U.x + V.y*U.y; +} + +double dist(Point P, Point Q) { + return Vector(P, Q).norm(); +} + +double cross(Vector V, Vector U) { + return V.x*U.y - V.y*U.x; +} + +int orientation(Point P, Point Q, Point R) { + double x = cross(Vector(P, Q), Vector(Q, R)); + if (x == 0) return 0; + return (x > 0)? 1 : -1; +} + +double dist_point_to_line(Point P, Point A, Point B) { + if (A == B) throw invalid_argument("A and B should not be equals"); + + Vector AP(A, P), AB(A, B); + double u = dot(AP, AB)/AB.norm2(); + Point C = translate(A, scale(AB, u)); + return dist(P, C); +} + +int main() { + int T; cin >> T; + while (T--) { + double x, y; cin >> x >> y; + double cx, cy, r; cin >> cx >> cy >> r; + + if (cx+r < x) { cout << "Goal!" << endl; continue; } + + double d1 = dist_point_to_line(Point(cx,cy), Point(x,y), Point(52.5, -3.66)); + double d2 = dist_point_to_line(Point(cx,cy), Point(x,y), Point(52.5, +3.66)); + + if ((d1 > r && orientation(Point(x,y), Point(52.5, -3.66), Point(cx,cy)) == +1) + || (d2 > r && orientation(Point(x,y), Point(52.5, +3.66), Point(cx,cy)) == -1)) + cout << "Goal!" << endl; + else cout << "No goal..." << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/GOC11A/GOC11A-9.cpp b/spoj_cpp_clean/GOC11A/GOC11A-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cf6ce985dcd0f09f3d04277c46d5a135076becc --- /dev/null +++ b/spoj_cpp_clean/GOC11A/GOC11A-9.cpp @@ -0,0 +1,84 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar() +using namespace std; + +string S, T; + +inline void getstring(string &A) +{ + char c; + + A.clear(); + + c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c)) + { + A.push_back(c); + c = gc(); + } +} + +inline void solve() +{ + int i, j; + + for (i = 0; i < S.length(); i++) + { + if (isalpha(S[i])) + T.push_back(S[i]); + else + { + string temp = T; + + for (j = 0; j < S[i] - '0' - 1; j++) + T += temp; + } + } +} + +int main() +{ + int t, Q, num; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &t); + + while (t--) + { + T.clear(); + getstring(S); + solve(); + + scanf("%d", &Q); + + while (Q--) + { + scanf("%d", &num); + if (num > T.length()) + printf("-1\n"); + else + printf("%c\n", T[num - 1]); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/GOC11B/GOC11B-9.cpp b/spoj_cpp_clean/GOC11B/GOC11B-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7517dfd4ac6919a73560e7697aa150b18d692980 --- /dev/null +++ b/spoj_cpp_clean/GOC11B/GOC11B-9.cpp @@ -0,0 +1,67 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +const long long MOD = 1000000007; +long long N, M; + +template +T modpow(T base, T exp, T modulus) { + base %= modulus; + T result = 1; + while (exp > 0) { + if (exp & 1) result = (result * base) % modulus; + base = (base * base) % modulus; + exp >>= 1; + } + return result; +} + +inline void init() +{ + scanf("%lld %lld", &N, &M); +} + +inline long long solve() +{ + long long f, s; + + f = modpow(M, N, MOD); + + if (N % 2) + s = modpow(M, N / 2 + 1, MOD); + else + s = modpow(M, N / 2, MOD); + + f = f - s; + if (f < 0) + f += MOD; + + return f; +} + +int main() +{ + int T, i; + + scanf("%d", &T); + + for (i = 1; i <= T; i++) + { + init(); + printf("Case %d: %lld\n", i, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/GONE/GONE-14.cpp b/spoj_cpp_clean/GONE/GONE-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87b6d393001537476d135a7e73cf2a5fb7653846 --- /dev/null +++ b/spoj_cpp_clean/GONE/GONE-14.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; + +#define ll long long +#define pb push_back +ll dp[2][2][12][90]; +vector num; +bool isPrime[100]; + +ll solve(ll isStart, ll isSmall, ll pos, ll sum) + +{ + if(pos == 0) { + if(!isPrime[sum]) + return 1; + else + return 0; + } + ll &ret = dp[isStart][isSmall][pos][sum]; + if(ret != -1 && isSmall) + return ret; + + ll lim, pos2 = num.size() - pos; + if(isSmall) + lim = 9; + else + lim = num[pos2]; + + ll rt = 0; + if(!isStart) { + for(ll i = 0; i <= lim; i++) + rt += solve(0, isSmall | i < num[pos2], pos - 1, i + sum); + } + else { + for(ll i = 1; i <= lim; i++) + rt += solve(0, isSmall | i < num[pos2], pos - 1, i + sum); + + rt += solve(1, 1, pos - 1, 0); + } + + return ret = rt; +} + +ll calc(ll n) + +{ + if(n <= 1) + return 0; + + ll tmp = n; + num.clear(); + while(tmp) { + num.pb(tmp % 10); + tmp /= 10; + } + reverse(num.begin(), num.end()); + + return solve(1, 0, num.size(), 0); +} + +int main() + +{ + isPrime[0] = 1; + isPrime[1] = 1; + for(ll i = 2; i <= 90; i++) { + if(isPrime[i] == 0) { + for(ll j = i * i; j <= 90; j += i) + isPrime[j] = 1; + } + } + + memset(dp, -1, sizeof(dp)); + ll t; + cin >> t; + while(t--) { + ll l, r; + scanf("%lld %lld", &l, &r); + + printf("%lld\n", calc(r) - calc(l - 1)); + + } + + return 0; diff --git a/spoj_cpp_clean/GONE/GONE-3.cpp b/spoj_cpp_clean/GONE/GONE-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b55d63bcc4ce70c82f97f779ec02d9029f1d733 --- /dev/null +++ b/spoj_cpp_clean/GONE/GONE-3.cpp @@ -0,0 +1,112 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< pr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89}; + +vector digits(int x){ + vector res; + while(x){ + res.pb(x % 10); + x /= 10; + } + + reverse(res.begin(), res.end()); + + return res; +} + +int get(int x){ + if(x <= 1) return 0; + + vector d = digits(x); + + int n = d.size(); + + memset(dp, 0, sizeof dp); + + dp[0][0][1] = 1; + for(int i = 0; i < n; i++){ + for(int sum = 0; sum <= 9 * i; sum++){ + for(int eq = 0; eq < 2; eq++){ + int val = dp[i][sum][eq]; + if(!val) continue; + + for(int nd = 0; nd < 10; nd++){ + if(eq and nd > d[i]) break; + + int neq = (eq and nd == d[i]); + int nsum = sum + nd; + + dp[i+1][nsum][neq] += val; + } + } + } + } + + int res = 0; + for(int p : pr){ + for(int eq = 0; eq < 2; eq++){ + res += dp[n][p][eq]; + } + } + + return res; +} + +void solve(){ + int f, t; + cin >> f >> t; + + if(f > t){ + cout << 0 << endl; + return; + } + + cout << get(t) - get(f-1) << endl; +} + +int main(){ _ + int c; + cin >> c; + while(c--) solve(); + return 0; +} diff --git a/spoj_cpp_clean/GONE/GONE-8.cpp b/spoj_cpp_clean/GONE/GONE-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bde992a519aefd39f6e84c92f6299829858fd124 --- /dev/null +++ b/spoj_cpp_clean/GONE/GONE-8.cpp @@ -0,0 +1,142 @@ +/*************************************************** + * Problem Name : GONE - G-One Numbers.cpp + * Problem Link : https://www.spoj.com/problems/GONE/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : DigitDP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorA, B; +int n; +ll dp[10][2][2][75]; +bool isPrime (int n) { + if (n == 0 || n == 1) return false; + + for (int i = 2; i < n; i++) { + if (n % i == 0) return false; + } + + return true; +} + +ll digitDP (int pos, int isChoto, int isBoro, int sum) { + if (n == pos) { + if (isPrime (sum) ) return 1LL; + else return 0LL; + } + + ll &ans = dp[pos][isChoto][isBoro][sum]; + + if (ans != -1) return ans; + + int low = 0, high = 9; + + if (isChoto == 0) high = B[pos]; + + if (isBoro == 0) low = A[pos]; + + ans = 0; + + for (int i = low; i <= high; i++) { + ans += digitDP (pos + 1, isChoto | (i < high), isBoro | (i > low), sum + i); + } + + return ans; +} + +ll solve (ll a, ll b) { + A.clear(), B.clear(); + + while (b > 0) { + B.pb (b % 10); + b /= 10; + } + + while (a > 0) { + A.pb (a % 10); + a /= 10; + } + + n = (int) B.size(); + + while ( (int) A.size() < n) { + A.pb (0); + } + + reverse (all (A) ); + reverse (all (B) ); + SET (dp, -1); + ll ans = digitDP (0, 0, 0, 0); + return ans; +} +int main () { + __FastIO; + int tc; + cin >> tc; + + while (tc--) { + ll a, b; + cin >> a >> b; + ll ans = solve (a, b); + cout << ans << "\n"; + } + + return 0; +} + diff --git a/spoj_cpp_clean/GOO/GOO-9.cpp b/spoj_cpp_clean/GOO/GOO-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7d2f4253b6fb09e15a2dc03ed8b8ff3bed607d7 --- /dev/null +++ b/spoj_cpp_clean/GOO/GOO-9.cpp @@ -0,0 +1,37 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int N; +unsigned long long sum, aces; + +int main() +{ + int T; + + scanf("%d", &T); + + while (T--) + { + scanf("%d", &N); + + sum = pow(2, N - 1); + aces = sum + (sum / 2) * (N - 1); + + printf("%llu %llu\n", sum, aces); + } + + return 0; +} diff --git a/spoj_cpp_clean/GOODG/GOODG-9.cpp b/spoj_cpp_clean/GOODG/GOODG-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0dbfd53a8d55401dd624053797d2c09008ee80eb --- /dev/null +++ b/spoj_cpp_clean/GOODG/GOODG-9.cpp @@ -0,0 +1,175 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +class ConvexHullDynamic +{ + typedef long long coef_t; + typedef long long coord_t; + typedef long long val_t; + + /* + * Line 'y=a*x+b' represented by 2 coefficients 'a' and 'b' + * and 'xLeft' which is intersection with previous line in hull(first line has -INF) + */ +private: + struct Line + { + coef_t a, b; + double xLeft; + + enum Type { line, maxQuery, minQuery } type; + coord_t val; + + explicit Line(coef_t aa = 0, coef_t bb = 0) : a(aa), b(bb), xLeft(-INFINITY), type(Type::line), val(0) {} + val_t valueAt(coord_t x) const { return a*x + b; } + friend bool areParallel(const Line& l1, const Line& l2) { return l1.a == l2.a; } + friend double intersectX(const Line& l1, const Line& l2) { return areParallel(l1, l2) ? INFINITY : 1.0*(l2.b - l1.b) / (l1.a - l2.a); } + bool operator<(const Line& l2) const + { + if (l2.type == line) + return this->a < l2.a; + else if (l2.type == maxQuery) + return this->xLeft < l2.val; + else //(l2.type == minQuery) + return this->xLeft > l2.val; + } + }; + +private: + bool isMax; //whether or not saved envelope is top(search of max value) + std::set hull; //envelope itself + +private: + /* + * INFO: Check position in hull by iterator + * COMPLEXITY: O(1) + */ + bool hasPrev(std::set::iterator it) { return it != hull.begin(); } + bool hasNext(std::set::iterator it) { return it != hull.end() && std::next(it) != hull.end(); } + + /* + * INFO: Check whether line l2 is irrelevant + * NOTE: Following positioning in hull must be true + * l1 is next left to l2 + * l2 is right between l1 and l3 + * l3 is next right to l2 + * COMPLEXITY: O(1) + */ + bool irrelevant(const Line& l1, const Line& l2, const Line& l3) { return intersectX(l1, l3) <= intersectX(l1, l2); } + bool irrelevant(std::set::iterator it) + { + return hasPrev(it) && hasNext(it) + && (isMax && irrelevant(*std::prev(it), *it, *std::next(it)) + || !isMax && irrelevant(*std::next(it), *it, *std::prev(it))); + } + + /* + * INFO: Updates 'xValue' of line pointed by iterator 'it' + * COMPLEXITY: O(1) + */ + std::set::iterator updateLeftBorder(std::set::iterator it) + { + if (isMax && !hasPrev(it) || !isMax && !hasNext(it)) + return it; + + double val = intersectX(*it, isMax ? *std::prev(it) : *std::next(it)); + Line buf(*it); + it = hull.erase(it); + buf.xLeft = val; + it = hull.insert(it, buf); + return it; + } + +public: + explicit ConvexHullDynamic(bool isMax) : isMax(isMax) {} + + /* + * INFO: Adding line to the envelope + * Line is of type 'y=a*x+b' represented by 2 coefficients 'a' and 'b' + * COMPLEXITY: Adding N lines(N calls of function) takes O(N*log N) time + */ + void addLine(coef_t a, coef_t b) + { + //find the place where line will be inserted in set + Line l3 = Line(a, b); + auto it = hull.lower_bound(l3); + + //if parallel line is already in set, one of them becomes irrelevant + if (it != hull.end() && areParallel(*it, l3)) + { + if (isMax && it->b < b || !isMax && it->b > b) + it = hull.erase(it); + else + return; + } + + //try to insert + it = hull.insert(it, l3); + if (irrelevant(it)) { hull.erase(it); return; } + + //remove lines which became irrelevant after inserting line + while (hasPrev(it) && irrelevant(std::prev(it))) hull.erase(std::prev(it)); + while (hasNext(it) && irrelevant(std::next(it))) hull.erase(std::next(it)); + + //refresh 'xLine' + it = updateLeftBorder(it); + if (hasPrev(it)) + updateLeftBorder(std::prev(it)); + if (hasNext(it)) + updateLeftBorder(std::next(it)); + } + /* + * INFO: Query, which returns max/min(depends on hull type - see more info above) value in point with abscissa 'x' + * COMPLEXITY: O(log N), N-amount of lines in hull + */ + val_t getBest(coord_t x) const + { + Line q; + q.val = x; + q.type = isMax ? Line::Type::maxQuery : Line::Type::minQuery; + + auto bestLine = hull.lower_bound(q); + if (isMax) --bestLine; + return bestLine->valueAt(x); + } +}; + +int N; +ConvexHullDynamic HULL(1); + +int main() +{ + int i; + long long a, d; + + //freopen("input.txt", "r", stdin); + + scanf("%d", &N); + + HULL.addLine(0, 0); + + for (i = 1; i <= N; i++) + { + scanf("%lld %lld", &a, &d); + d = -d; + HULL.addLine(d, HULL.getBest(i) + a - d * i); + } + + printf("%lld\n", HULL.getBest(N + 1)); + + return 0; +} diff --git a/spoj_cpp_clean/GR/GR-9.cpp b/spoj_cpp_clean/GR/GR-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fccffd78bdd267a9a8e79c948c312ccea1fbb564 --- /dev/null +++ b/spoj_cpp_clean/GR/GR-9.cpp @@ -0,0 +1,91 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +int A[30][30]; +int N, T; + +inline void init() +{ + int i, j, c; + + scanf("%d %d", &N, &T); + + T %= 4; + c = 1; + + switch (T) + { + case 0: + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++, c++) + A[i][j] = c; + break; + case 1: + for (j = N; j >= 1; j--) + for (i = 1; i <= N; i++, c++) + A[i][j] = c; + break; + case 2: + for (i = N; i >= 1; i--) + for (j = N; j >= 1; j--, c++) + A[i][j] = c; + break; + case 3: + for (j = 1; j <= N; j++) + for (i = N; i >= 1; i--, c++) + A[i][j] = c; + break; + } +} + +inline void solve() +{ + int i, j, Ameen = 0, Shafeek = 0, Atef = 0, Sameh = 0; + + for (i = 1; i <= N; i++) + { + Ameen += A[1][i]; + Shafeek += A[i][N]; + Atef += A[N][i]; + Sameh += A[i][1]; + } + + if (Ameen > Shafeek && Ameen > Atef && Ameen > Sameh) + printf("Ameen\n"); + + else if (Shafeek > Atef && Shafeek > Sameh) + printf("Shafeek\n"); + + else if (Atef > Sameh) + printf("Atef\n"); + + else + printf("Sameh\n"); + + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + printf("%d ", A[i][j]); + printf("\n"); + } +} + +int main() +{ + init(); + solve(); + + return 0; +} diff --git a/spoj_cpp_clean/GRASSPLA/GRASSPLA-18.cpp b/spoj_cpp_clean/GRASSPLA/GRASSPLA-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a0b6068b60595e62b8131934c4c2067d630042f --- /dev/null +++ b/spoj_cpp_clean/GRASSPLA/GRASSPLA-18.cpp @@ -0,0 +1,192 @@ +// Problem name: Grass Planting +// Problem link: https://www.spoj.com/problems/GRASSPLA/ +// Submission link: https://www.spoj.com/submit/GRASSPLA/id=21038866 + +#include +#include +#include + +#define DAD(x) ((x) >> 1) +#define LEFT(x) ((x) << 1) +#define RIGHT(x) ((x) << 1 | 1) +#define pb(x) push_back(x) +#define mp(i, j) make_pair(i, j) +#define F first +#define S second +#define LOG2(x) (31 - __builtin_clz(x)) +#define endl '\n' + +using namespace std; + +typedef pair pii; +typedef vector vii; + +struct SegmentTree{ +private: + int height; + vector tree, lazy; + + void apply(int i, int v){ + tree[ i ] += v; + if(i < n) + lazy[ i ] += v; + } + + void build(int i){ + for( ; i = DAD(i) ; ) + tree[ i ] = tree[ LEFT(i) ] + tree[ RIGHT(i) ] + lazy[ i ]; + } + + void refresh(int i){ + for(int h = height, k ; h ; h--){ + k = (i >> h); + if(lazy[ k ] != 0){ + apply(LEFT(k), lazy[ k ]); + apply(RIGHT(k), lazy[ k ]); + lazy[ k ] = 0; + } + } + } + +public: + int n; + + SegmentTree(int n) : n(n), height(LOG2(n)), tree(n << 1, 0), lazy(n, 0){ } + + void Update(int l, int r, int v){ + l += n; + r += n; + for( ; l < r ; l = DAD(l), r = DAD(r)){ + if(l & 1) + apply(l++, v); + if(r & 1) + apply(--r, v); + } + build(l); + build(r - 1); + } + + int Query(int l, int r){ + l += n; + r += n; + refresh(l); + refresh(r - 1); + int ans = 0; + for( ; l < r ; l = DAD(l), r = DAD(r)){ + if(l & 1) + ans += tree[ l++ ]; + if(r & 1) + ans += tree[ --r ]; + } + return ans; + } +}; + +struct HeavyLight{ +private: + int n; + vector size, dad, heavy, depth, position, head; + vector adj; + SegmentTree tree; + + int Map(int v){ + return --v; + } + + int f(int a, int b){ + return a + b; + } + +public: + HeavyLight(int n) : n(n), adj(n), tree(n) { } + + void AddEdge(int u, int v, int w){ + adj[ u = Map(u) ].pb(mp(v = Map(v), w)); + adj[ v ].pb(mp(u, w)); + } + + void DFS(int u){ + size[ u ] = 1; + int v; + for(int i = adj[ u ].size() - 1 ; i >= 0 ; --i){ + v = adj[ u ][ i ].F; + if(v != dad[ u ]){ + dad[ v ] = u; + depth[ v ] = depth[ u ] + 1; + DFS(v); + size[ u ] += size[ v ]; + if(heavy[ u ] == -1 || size[ heavy[ u ] ] < size[ v ]) + heavy[ u ] = v; + } + } + } + + void Build(){ + size.assign(n, 0); + dad.assign(n, -1); + heavy.assign(n, -1); + depth.assign(n, 0); + + DFS(0); + + position.assign(n, 0); + head.assign(n, 0); + + for(int u = 0, pos = 0 ; u < n ; ++u) + if(dad[ u ] == -1 || heavy[ dad[ u ] ] != u) + for(int v = u, parent = u ; v != -1 ; parent = v, v = heavy[ v ]) + head[ v ] = u, position[ v ] = pos++; + } + + void Update(int u, int v, int w){ + u = Map(u); + v = Map(v); + while(head[ u ] != head[ v ]){ + if(depth[ head[ u ] ] < depth[ head[ v ] ]) + swap(u, v); + tree.Update(position[ head[ u ] ], position[ u ] + 1, w); + u = dad[ head[ u ] ]; + } + if(depth[ u ] > depth[ v ]) + swap(u, v); + tree.Update(position[ u ] + 1, position[ v ] + 1, w); + } + + int Query(int u, int v){ + u = Map(u); + v = Map(v); + int ans = 0; + while(head[ u ] != head[ v ]){ + if(depth[ head[ u ] ] < depth[ head[ v ] ]) + swap(u, v); + ans = f(ans, tree.Query(position[ head[ u ] ], position[ u ] + 1)); + u = dad[ head[ u ] ]; + } + if(depth[ u ] > depth[ v ]) + swap(u, v); + ans = f(ans, tree.Query(position[ u ] + 1, position[ v ] + 1)); + return ans; + } +}; + +int main(){ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + int n, m, u, v; + char ope; + cin >> n >> m; + HeavyLight hld(n); + for(int i = 1 ; i < n ; i++) + cin >> u >> v, hld.AddEdge(u, v, 0); + hld.Build(); + while(m--){ + cin >> ope >> u >> v; + if(ope == 'P') + hld.Update(u, v, 1); + else + cout << hld.Query(u, v) << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/GRAVITY/GRAVITY-8.cpp b/spoj_cpp_clean/GRAVITY/GRAVITY-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b95add823636defc7dd0d057219c1ebc05f15a8f --- /dev/null +++ b/spoj_cpp_clean/GRAVITY/GRAVITY-8.cpp @@ -0,0 +1,184 @@ +/*************************************************** + * Problem Name : GRAVITY - GRAVITY.cpp + * Problem Link : https://www.spoj.com/problems/GRAVITY/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2020-05-10 + * Problem Type : + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 105 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int fx[] = {1, -1, 0, 0, 1, -1, -1, 1}; +int fy[] = {0, 0, 1, -1, 1, -1, 1, -1}; + +static string mat[MAX]; +int row, col; +int d[MAX][MAX]; +bool vis[MAX][MAX]; + + + +void bfs (int sx, int sy) { + SET (vis, 0); + vis[sx][sy] = 1; + queue q; + q.push (pii (sx, sy) ); + d[sx][sy] = 0; + + while (!q.empty() ) { + pii top = q.front(); + q.pop(); + + for (int k = 0; k < 8; k++) { + int tx = top.first + fx[k]; + int ty = top.second + fy[k]; + + if (tx >= 0 && ty >= 0 && tx < row && ty < col && vis[tx][ty] == 0 + && mat[tx][ty] != '#') { + vis[tx][ty] = 1; + d[tx][ty] = d[top.first][top.second] + 1; + q.push (pii (tx, ty) ); + } + } + } +} +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + + while (tc--) { + int o; + cin >> o >> row >> col; + cin.ignore(); + int sx, sy, dx, dy; + + for (int i = 0; i < row; i++) { + getline (cin, mat[i]); + } + + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + if (mat[i][j] == 'S') { + sx = i; + sy = j; + } + + if (mat[i][j] == 'T') { + dx = i; + dy = j; + } + } + } + + SET (d, 0); + bfs (sx, sy); + int val = d[dx][dy]; + + if (val == 0) { + cout << "Impossible\n"; + + } else { + //~ cout << val << "\n"; + if (val > o) { + cout << "Impossible\n"; + + } else { + cout << "Possible\n"; + } + } + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS1/GSS1-12.cpp b/spoj_cpp_clean/GSS1/GSS1-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49c5f4be26d8f7129329ce6ece82d11b7d1a05fb --- /dev/null +++ b/spoj_cpp_clean/GSS1/GSS1-12.cpp @@ -0,0 +1,125 @@ +#include +#define mp make_pair +#define pb push_back +#define MAXN 51000 + +using namespace std; + +typedef long long int64; + +int m, n; + +struct data { + int64 sum, pref, suff, ans; + data(int64 sum = 0, int64 pref = 0, int64 suff = 0, int64 ans = 0) + : sum(sum) + , pref(pref) + , suff(suff) + , ans(ans) + { + } +}; + +data t[4 * MAXN]; +int a[MAXN]; + +int readInt() +{ + bool minus = false; + int result = 0; + char ch = getchar_unlocked(); + while (true) { + if (ch == '-') + break; + if (ch >= '0' && ch <= '9') + break; + ch = getchar_unlocked(); + } + if (ch == '-') + minus = true; + else + result = ch - '0'; + while (true) { + ch = getchar_unlocked(); + if (ch < '0' || ch > '9') + break; + result = result * 10 + (ch - '0'); + } + if (minus) + return -result; + else + return result; +} + +data combine(data l, data r) +{ + data res; + res.sum = l.sum + r.sum; + res.pref = max(l.pref, l.sum + r.pref); + res.suff = max(r.suff, r.sum + l.suff); + res.ans = max(max(l.ans, r.ans), l.suff + r.pref); + return res; +} + +data make_data(int64 val) +{ + return data(val, val, val, val); +} + +void build(int node, int tl, int tr) +{ + if (tl == tr) + t[node] = make_data(a[tl]); + else { + int tm = (tl + tr) >> 1; + build(2 * node, tl, tm); + build(2 * node + 1, tm + 1, tr); + t[node] = combine(t[2 * node], t[2 * node + 1]); + } +} + +void update(int node, int tl, int tr, int pos, int64 new_val) +{ + if (tl == tr) + t[node] = make_data(new_val); + else { + int tm = (tl + tr) >> 1; + if (pos <= tm) + update(2 * node, tl, tm, pos, new_val); + else + update(2 * node + 1, tm + 1, tr, pos, new_val); + t[node] = combine(t[2 * node], t[2 * node + 1]); + } +} + +data query(int node, int tl, int tr, int l, int r) +{ + if (l > r || l < 0 || r < 0 || r > tr || l < tl) + return make_data(0); + if (l == tl && tr == r) + return t[node]; + int tm = (tl + tr) >> 1; + if (r <= tm) + return query(2 * node, tl, tm, l, r); + if (l > tm) + return query(2 * node + 1, tm + 1, tr, l, r); + return combine(query(2 * node, tl, tm, l, tm), query(2 * node + 1, tm + 1, tr, tm + 1, r)); +} + +int main() +{ + n = readInt(); + int x, y; + for (int i = 0; i < n; i++) { + a[i] = readInt(); + } + build(1, 0, n - 1); + m = readInt(); + while (m--) { + x = readInt(); + y = readInt(); + data resp = query(1, 0, n - 1, x - 1, y - 1); + printf("%lld\n", resp.ans); + } + return 0; +} diff --git a/spoj_cpp_clean/GSS1/GSS1-16.cpp b/spoj_cpp_clean/GSS1/GSS1-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60f2da3202cccc9a6a2fb77412abccb7b609c884 --- /dev/null +++ b/spoj_cpp_clean/GSS1/GSS1-16.cpp @@ -0,0 +1,145 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define ll long long +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; +struct SegmentTreeNode{ + // data + ll sum, preMaxSum, postMaxSum, maxSum; + //functions + void assignLeaf(ll value){ + sum=preMaxSum=postMaxSum=maxSum=value; + } + void merge(SegmentTreeNode& left, SegmentTreeNode& right){ + sum = left.sum + right.sum; + preMaxSum=max({left.preMaxSum, left.sum + right.preMaxSum}); + postMaxSum=max({right.postMaxSum, right.sum + left.postMaxSum}); + maxSum=max({preMaxSum, postMaxSum, left.maxSum, right.maxSum, left.postMaxSum + right.preMaxSum}); + } + ll getValue(){ + return maxSum; + } +}; + + + +// T is the type of input array elements +// V is the type of required aggregate statistic +template +class SegmentTree { + SegmentTreeNode* nodes; + int N; + +public: + SegmentTree(T arr[], int N) { + this->N = N; + nodes = new SegmentTreeNode[getSegmentTreeSize(N)]; + buildTree(arr, 1, 0, N-1); + } + + ~SegmentTree() { + delete[] nodes; + } + + V getValue(int lo, int hi) { + SegmentTreeNode result = getValue(1, 0, N-1, lo, hi); + return result.getValue(); + } + + void update(int index, T value) { + update(1, 0, N-1, index, value); + } + + private: + void buildTree(T arr[], int stIndex, int lo, int hi) { + if (lo == hi) { + nodes[stIndex].assignLeaf(arr[lo]); + return; + } + + int left = 2 * stIndex, right = left + 1, mid = (lo + hi) / 2; + buildTree(arr, left, lo, mid); + buildTree(arr, right, mid + 1, hi); + nodes[stIndex].merge(nodes[left], nodes[right]); + } + + SegmentTreeNode getValue(int stIndex, int left, int right, int lo, int hi) { + if (left == lo && right == hi) + return nodes[stIndex]; + + int mid = (left + right) / 2; + if (lo > mid) + return getValue(2*stIndex+1, mid+1, right, lo, hi); + if (hi <= mid) + return getValue(2*stIndex, left, mid, lo, hi); + + SegmentTreeNode leftResult = getValue(2*stIndex, left, mid, lo, mid); + SegmentTreeNode rightResult = getValue(2*stIndex+1, mid+1, right, mid+1, hi); + SegmentTreeNode result; + result.merge(leftResult, rightResult); + return result; + } + + int getSegmentTreeSize(int N) { + int size = 1; + for (; size < N; size <<= 1); + return size << 1; + } + + void update(int stIndex, int lo, int hi, int index, T value) { + if (lo == hi) { + nodes[stIndex].assignLeaf(value); + return; + } + + int left = 2 * stIndex, right = left + 1, mid = (lo + hi) / 2; + if (index <= mid) + update(left, lo, mid, index, value); + else + update(right, mid+1, hi, index, value); + + nodes[stIndex].merge(nodes[left], nodes[right]); + } +}; + + + + + + + + +void solve(){ + ll n; + cin>>n; + ll a[n]; + for(ll i=0;i>a[i]; + SegmentTree st(a,n); + ll q; + cin>>q; + for(ll i=0;i>l>>r; + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/GSS1/GSS1-3.cpp b/spoj_cpp_clean/GSS1/GSS1-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7408d8e8a1510b1fd77265ad607fd482b1eb526b --- /dev/null +++ b/spoj_cpp_clean/GSS1/GSS1-3.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int n, array[50000], m , X, Y; +node tree[150001]; + +struct node{ + int prefixMaxSum, suffixMaxSum, maxsum, sum; + + void assignLeaf(int value) { + prefixMaxSum = suffixMaxSum = maxsum = sum = value; + } + + void merge(node& left, node& right) { + sum = left.sum + right.sum; + prefixMaxSum = max(left.prefixMaxSum, left.sum + right.prefixMaxSum); + suffixMaxSum = max(right.suffixMaxSum, right.sum + left.suffixMaxSum); + maxsum = max(prefixMaxSum, max(suffixMaxSum, max(left.maxsum, max(right.maxsum, left.suffixMaxSum + right.prefixMaxSum)))); + } +}; + +// l and r represent the range node i is responsible for. A single call init(1, 0, n-1) builds the segment tree in O(N) time +void init(int i, int l, int r){ + if(l == r){ + tree[i].assignLeaf(array[l]); + } + else{ + init(2*i, l, (l+r)/2); + init((2*i) + 1, ((l+r)/2) + 1, r); + node left = tree[2*i], right = tree[(2*i)+1]; + + tree[i].merge(left, right); //Merge the children into the parent + } + return; +} + +// O(log N) +node query(int i, int l, int r, int lo, int hi){ + if(l == lo and r == hi) //If the range we want to query is the exact range + return tree[i]; + int mid = (l+r)/2; + if(lo > mid) //If only the subtree rooted at right child is repsonsible for this range + return query((2*i)+1, mid+1, r, lo, hi); + if(hi <= mid) //If only the subtree rooted at the left child is responsible for this range + return query(2*i, l, mid, lo, hi); + //If both, the left and right children are partially responsible for the range + node left = query(2*i, l, mid, lo, mid); + node right = query((2*i)+1, mid+1, r, mid+1, hi); + node result; + + result.merge(left, right); //Merge the results of the children into the parent + return result; +} + +int main(){ + sd(n); + for(int i = 0; i < n; i++) sd(array[i]); + + init(1, 0, n-1); + + sd(m); + while(m--){ + sd2(X,Y); + printf("%d\n",query(1,0,n-1,X-1,Y-1).maxsum); + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS1/GSS1-4.cpp b/spoj_cpp_clean/GSS1/GSS1-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3226ee0496d174d098d912421a4a4f6e400c9442 --- /dev/null +++ b/spoj_cpp_clean/GSS1/GSS1-4.cpp @@ -0,0 +1,191 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h;//*flag; +} +/* +6 +-1 -2 3 0 -1 9 + 11 + 3 9 + -1 3 0 9 + -1 - 2 0 -1 + +*/ +int n, m, x, y ; +int a[50009]; +struct data +{ + int pref, suff, sum, ans; +}; +data T[500000]; + +data init(int val) +{ + data res; + res.sum=val; + res.pref=res.suff=res.ans=maX(-1000000, val); + return res; +} +data combine (data left, data right) { + data res; + res.sum = left.sum + right.sum; + res.pref = maX (left.pref, left.sum + right.pref); + res.suff = maX (right.suff, right.sum + left.suff); + res.ans = maX (maX (left.ans, right.ans), left.suff + right.pref); + return res; +} + +void build(int index, int st, int en) +{ + if(st==en) + T[index]=init(a[st]); + else + { + int mid=st+(en-st)/2; + build(2*index, st, mid); + build(2*index+1, mid+1, en); + T[index]=combine(T[2*index], T[2*index+1]); + } + +} + +data query(int index, int st, int en, int x, int y) +{ //1 "8 16" 20 + if (x == st && y == en) + return T[index]; + int mid = (st + en) / 2; + if (y <= mid) + return query (index*2, st, mid, x, y); + if (x > mid) + return query (index*2+1, mid+1, en, x, y); + return combine ( + query (index*2, st, mid, x, mid), + query (index*2+1, mid+1, en, mid+1, y) + ); +} +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + s(n); + FOR(i,0,n) + s(a[i+1]); + + build(1,1,n+1); +/* + error("Prefix"); + FOR(i,0,17) + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 50005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +struct Tree { + ll left_sum, right_sum, total_sum, max_sum; + Tree() { + left_sum = -INF; + right_sum = -INF; + total_sum = -INF; + max_sum = -INF; + } +} tr[MAX * 4]; + +int ar[MAX]; + + +Tree combine (Tree a, Tree b) { + Tree tmp; + tmp.left_sum = max (a.left_sum, a.total_sum + b.left_sum); + tmp.right_sum = max (b.right_sum, b.total_sum + a.right_sum); + tmp.total_sum = a.total_sum + b.total_sum; + tmp.max_sum = max3 (a.max_sum, b.max_sum, a.right_sum + b.left_sum); + return tmp; +} + +void build (int nod, int low, int high) { + if (low > high) return; + + if (low == high) { + tr[nod].left_sum = ar[low]; + tr[nod].right_sum = ar[low]; + tr[nod].total_sum = ar[low]; + tr[nod].max_sum = ar[low]; + return; + } + + int mid = (high + low) / 2; + build (nod * 2, low, mid); + build (nod * 2 + 1, mid + 1, high); + tr[nod] = combine (tr[nod * 2], tr[nod * 2 + 1]); +} + +Tree query (int nod, int low, int high, int qlow, int qhigh) { + if (qlow > high || qhigh < low) { + Tree tmp; + return tmp; + } + + if (low >= qlow && high <= qhigh) { + return tr[nod]; + } + + int mid = (high + low) / 2; + Tree a = query (nod * 2, low, mid, qlow, qhigh); + Tree b = query (nod * 2 + 1, mid + 1, high, qlow, qhigh); + return combine (a, b); +} + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + int n; + cin >> n; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + build (1, 0, n - 1); + int q; + cin >> q; + + while (q--) { + int l, r; + cin >> l >> r; + Tree ans = query (1, 0, n - 1, l - 1, r - 1); + cout << ans.max_sum << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS1/GSS1-9.cpp b/spoj_cpp_clean/GSS1/GSS1-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..56c7c00a3789622515904088e059f850d161be60 --- /dev/null +++ b/spoj_cpp_clean/GSS1/GSS1-9.cpp @@ -0,0 +1,108 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define MAX 50007 +using namespace std; + +struct n +{ + long long left; + long long right; + long long all; + long long best; +}; + +typedef struct n node; + +node tree[MAX*4]; +int arr[MAX+1]; +int N, E; + +void init(int index, int a, int b) +{ + if(a==b) + { + tree[index].all=tree[index].best=tree[index].left=tree[index].right=arr[a]; + return; + } + + init(index*2, a, (a+b)/2); + init(index*2+1, (a+b)/2+1, b); + + tree[index].left=max(tree[index*2].left, tree[index*2].all+tree[index*2+1].left); + tree[index].right=max(tree[index*2+1].right, tree[index*2+1].all + tree[index*2].right); + tree[index].all=tree[index*2].all+tree[index*2+1].all; + tree[index].best=max(tree[index*2].right+tree[index*2+1].left, max(tree[index*2].best, tree[index*2+1].best)); +} + +node query(int index, int a, int b, int i, int j) +{ + node temp; + node *left, *right; + + if(a==i && b==j) + { + temp.all=tree[index].all; + temp.best=tree[index].best; + temp.left=tree[index].left; + temp.right=tree[index].right; + + return temp; + } + + if(j<=(a+b)/2) + return query(index*2, a, (a+b)/2, i, j); + + else if(i>=(a+b)/2+1) + return query(index*2+1, (a+b)/2+1, b, i, j); + + else + { + left=&query(index*2, a, (a+b)/2, i, (a+b)/2); + right=&query(index*2+1, (a+b)/2+1, b, (a+b)/2+1, j); + + temp.all=left->all+right->all; + temp.left=max(left->left, left->all + right->left); + temp.right=max(right->right, right->all + left->right); + temp.best=max(left->right+right->left, max(left->best, right->best)); + + return temp; + } +} + +int main() +{ + + int i; + int f, s; + node *temp; + + scanf("%d", &N); + + for(i=1; i<=N; i++) + scanf("%d", &arr[i]); + + init(1, 1, N); + + scanf("%d", &E); + + for(i=0; ibest, temp->all), max(temp->left, temp->right))); + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS2/GSS2-14.cpp b/spoj_cpp_clean/GSS2/GSS2-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af1ea154fc078ef78266b2ef1fbdc23b614fded6 --- /dev/null +++ b/spoj_cpp_clean/GSS2/GSS2-14.cpp @@ -0,0 +1,122 @@ +#include +using namespace std; + +#define ll int +#define c 100009 +ll ara[100009], last[c + 100009], ans[100009]; + +struct node { + ll ans; + ll cur; +} tree[300009]; + +struct laz { + ll mx; + ll cur; +} lazy[300009]; + +struct query { + ll l, r, id; +} qry[100009]; + +bool cmp(query a, query b) + +{ + return a.r < b.r; +} + +void prop(ll lo, ll hi, ll node) + +{ + if(lo == hi) + return; + + lazy[2 * node].mx = max(lazy[2 * node].mx, lazy[2 * node].cur + lazy[node].mx); + lazy[2 * node].cur += lazy[node].cur; + tree[2 * node].ans = max(tree[2 * node].ans, tree[2 * node].cur + lazy[node].mx); + tree[2 * node].cur += lazy[node].cur; + + lazy[2 * node + 1].mx = max(lazy[2 * node + 1].mx, lazy[2 * node + 1].cur + lazy[node].mx); + lazy[2 * node + 1].cur += lazy[node].cur; + tree[2 * node + 1].ans = max(tree[2 * node + 1].ans, tree[2 * node + 1].cur + lazy[node].mx); + tree[2 * node + 1].cur += lazy[node].cur; + + lazy[node].mx = 0; + lazy[node].cur = 0; +} + +void update(ll lo, ll hi, ll left, ll right, ll val, ll node) + +{ + if(lazy[node].cur || lazy[node].mx) prop(lo, hi, node); + + if(lo > right || hi < left) + return; + if(lo >= left && hi <= right) { + tree[node].cur += val; + tree[node].ans = max(tree[node].ans, tree[node].cur); + lazy[node].cur += val; + lazy[node].mx = max(lazy[node].mx, lazy[node].cur); + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, left, right, val, 2 * node); + update(mid + 1, hi, left, right, val, 2 * node + 1); + + tree[node].cur = max(tree[2 * node].cur, tree[2 * node + 1].cur); + tree[node].ans = max(tree[2 * node].ans, tree[2 * node + 1].ans); +} + +ll query(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lazy[node].cur || lazy[node].mx) prop(lo, hi, node); + + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) + return tree[node].ans; + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, 2 * node); + ll p2 = query(mid + 1, hi, left, right, 2 * node + 1); + + return max(p1, p2); +} + +int main() + +{ + ll n; + cin >> n; + for(ll i = 1; i <= n; i++) + scanf("%d", &ara[i]); + + ll q; + cin >> q; + + for(ll i = 1; i <= q; i++) { + scanf("%d %d", &qry[i].l, &qry[i].r); + qry[i].id = i; + } + + sort(qry + 1, qry + q + 1, cmp); + + ll indx = 1; + for(ll i = 1; i <= n; i++) { + update(1, n, last[ c + ara[i] ] + 1, i, ara[i], 1); + last[ c + ara[i] ] = i; + + while(indx <= q && qry[indx].r == i) { + ans[ qry[indx].id ] = query(1, n, qry[indx].l, qry[indx].r, 1); + indx++; + } + } + + for(ll i = 1; i <= q; i++) { + printf("%d\n", ans[i]); + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS2/GSS2-3.cpp b/spoj_cpp_clean/GSS2/GSS2-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6763fa3639a009004efa877be4c2100bbbf2fc27 --- /dev/null +++ b/spoj_cpp_clean/GSS2/GSS2-3.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ +} diff --git a/spoj_cpp_clean/GSS3/GSS3-12.cpp b/spoj_cpp_clean/GSS3/GSS3-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb483e7525c531025da6877aa9493c519d26554 --- /dev/null +++ b/spoj_cpp_clean/GSS3/GSS3-12.cpp @@ -0,0 +1,129 @@ +#include +#define mp make_pair +#define pb push_back +#define MAXN 51000 + +using namespace std; + +typedef long long int64; + +int m, n; + +struct data { + int64 sum, pref, suff, ans; + data(int64 sum = 0, int64 pref = 0, int64 suff = 0, int64 ans = 0) + : sum(sum) + , pref(pref) + , suff(suff) + , ans(ans) + { + } +}; + +data t[4 * MAXN]; +int a[MAXN]; + +int readInt() +{ + bool minus = false; + int result = 0; + char ch = getchar_unlocked(); + while (true) { + if (ch == '-') + break; + if (ch >= '0' && ch <= '9') + break; + ch = getchar_unlocked(); + } + if (ch == '-') + minus = true; + else + result = ch - '0'; + while (true) { + ch = getchar_unlocked(); + if (ch < '0' || ch > '9') + break; + result = result * 10 + (ch - '0'); + } + if (minus) + return -result; + else + return result; +} + +data combine(data l, data r) +{ + data res; + res.sum = l.sum + r.sum; + res.pref = max(l.pref, l.sum + r.pref); + res.suff = max(r.suff, r.sum + l.suff); + res.ans = max(max(l.ans, r.ans), l.suff + r.pref); + return res; +} + +data make_data(int64 val) +{ + return data(val, val, val, val); +} + +void build(int node, int tl, int tr) +{ + if (tl == tr) + t[node] = make_data(a[tl]); + else { + int tm = (tl + tr) >> 1; + build(2 * node, tl, tm); + build(2 * node + 1, tm + 1, tr); + t[node] = combine(t[2 * node], t[2 * node + 1]); + } +} + +void update(int node, int tl, int tr, int pos, int64 new_val) +{ + if (tl == tr) + t[node] = make_data(new_val); + else { + int tm = (tl + tr) >> 1; + if (pos <= tm) + update(2 * node, tl, tm, pos, new_val); + else + update(2 * node + 1, tm + 1, tr, pos, new_val); + t[node] = combine(t[2 * node], t[2 * node + 1]); + } +} + +data query(int node, int tl, int tr, int l, int r) +{ + if (l > r || l < 0 || r < 0 || r > tr || l < tl) + return make_data(0); + if (l == tl && tr == r) + return t[node]; + int tm = (tl + tr) >> 1; + if (r <= tm) + return query(2 * node, tl, tm, l, r); + if (l > tm) + return query(2 * node + 1, tm + 1, tr, l, r); + return combine(query(2 * node, tl, tm, l, tm), query(2 * node + 1, tm + 1, tr, tm + 1, r)); +} + +int main() +{ + n = readInt(); + int opc, x, y; + for (int i = 0; i < n; i++) { + a[i] = readInt(); + } + build(1, 0, n - 1); + m = readInt(); + while (m--) { + opc = readInt(); + x = readInt(); + y = readInt(); + if (opc) { + data resp = query(1, 0, n - 1, x - 1, y - 1); + printf("%lld\n", resp.ans); + } else + update(1, 0, n - 1, x - 1, y); + } + return 0; +} diff --git a/spoj_cpp_clean/GSS3/GSS3-3.cpp b/spoj_cpp_clean/GSS3/GSS3-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22e041edb45b18b694c640a36ec1d220d3ae4026 --- /dev/null +++ b/spoj_cpp_clean/GSS3/GSS3-3.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + int prefixMaxSum, suffixMaxSum, maxsum, sum; + + void assignLeaf(int value) { + prefixMaxSum = suffixMaxSum = maxsum = sum = value; + } + + void merge(node& left, node& right){ + sum = left.sum + right.sum; + prefixMaxSum = max(left.prefixMaxSum, left.sum + right.prefixMaxSum); + suffixMaxSum = max(right.suffixMaxSum, left.suffixMaxSum + right.sum); + maxsum = max(prefixMaxSum, max(suffixMaxSum, max(left.maxsum, max(right.maxsum, left.suffixMaxSum + right.prefixMaxSum)))); + } +}; + +int n, array[50000], m , X, Y, type; +node tree[150001]; + +void init(int i, int l, int r){ + if(l == r){ + tree[i].prefixMaxSum = array[l]; + tree[i].suffixMaxSum = array[l]; + tree[i].maxsum = array[l]; + tree[i].sum = array[l]; + } + else{ + init(2*i, l, (l+r)/2); + init((2*i) + 1, ((l+r)/2) + 1, r); + + node left = tree[2*i], right = tree[(2*i)+1]; + //Merge the children to form the parent node i + tree[i].merge(left, right); + } + return; +} + +node query(int i, int l, int r, int lo, int hi){ + if(l == lo and r == hi) + return tree[i]; + int mid = (l+r)/2; + if(lo > mid) + return query((2*i)+1, mid+1, r, lo, hi); + if(hi <= mid) + return query(2*i, l, mid, lo, hi); + + node left = query(2*i, l, mid, lo, mid); + node right = query((2*i)+1, mid+1, r, mid+1, hi); + node result; + + result.merge(left,right); + + return result; +} + + +void update(int stIndex, int lo, int hi, int index, int value) { + if (lo == hi) { + tree[stIndex].prefixMaxSum = value; + tree[stIndex].suffixMaxSum = value; + tree[stIndex].maxsum = value; + tree[stIndex].sum = value; + return; + } + int left = 2 * stIndex, right = left + 1, mid = (lo + hi) / 2; + if (index <= mid) + update(left, lo, mid, index, value); + else + update(right, mid+1, hi, index, value); + tree[stIndex].merge(tree[left], tree[right]); +} + +int main(){ + sd(n); + for(int i = 0; i < n; i++) sd(array[i]); + + init(1, 0, n-1); + + sd(m); + while(m--){ + sd3(type,X,Y); + if(type == 1) + printf("%d\n",query(1,0,n-1,X-1,Y-1).maxsum); + else if(type == 0){ + update(1, 0, n-1, X-1, Y); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS3/GSS3-4.cpp b/spoj_cpp_clean/GSS3/GSS3-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2019ef2523d9b321f6a4f825ef52bab3d34fa1c4 --- /dev/null +++ b/spoj_cpp_clean/GSS3/GSS3-4.cpp @@ -0,0 +1,250 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[50009]; +struct data +{ + int pref, suff, sum, ans; +};data T[500000]; + +data init(int val) +{ + data res; + res.sum=val; + res.pref=res.suff=res.ans=maX(-1000000, val); + return res; +} +data combine (data left, data right) { + data res; + res.sum = left.sum + right.sum; + res.pref = maX (left.pref, left.sum + right.pref); + res.suff = maX (right.suff, right.sum + left.suff); + res.ans = maX (maX (left.ans, right.ans), left.suff + right.pref); + return res; +} + +void build(int index, int st, int en) +{ + if(st==en) + T[index]=init(a[st]); + else + { + int mid=st+(en-st)/2; + build(2*index, st, mid); + build(2*index+1, mid+1, en); + T[index]=combine(T[2*index], T[2*index+1]); + } + +} +void update(int index, int st, int en, int x, int val) +{ + if(st==x && en==x) + T[index]=init(val); + else + { + int mid = (st+en)/2; + if(x<=mid) + update(index*2, st, mid, x, val); + else if(x>mid) + update(index*2+1, mid+1, en, x, val); + + T[index]=combine(T[2*index], T[2*index+1]); + } +} + + +data query(int index, int st, int en, int x, int y) +{ //1 "8 16" 20 + if (x == st && y == en) + return T[index]; + int mid = (st + en) / 2; + if (y <= mid) + return query (index*2, st, mid, x, y); + if (x > mid) + return query (index*2+1, mid+1, en, x, y); + return combine ( + query (index*2, st, mid, x, mid), // Take care here its x to mid + query (index*2+1, mid+1, en, mid+1, y) + ); +} + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int p, q, r; + //s(T); + s(n); + FOR(i,1,n+1) + s(a[i]); + build(1,1,n+1); + + s(m); + FOR(i,0,m) + { + s(p); s(q); s(r); + if(p==0) + a[q]=r, update(1,1,n+1,q,r); + else + printf("%d\n",query(1,1,n+1,q,r).ans); +/* + error("Prefix"); + FOR(i,0,17) + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +typedef long long ll; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 50005 +#define INF 1000000009 +#define MOD 1000000007 +/************************************ Code Start Here ******************************************************/ +struct Tree { + ll left_sum, right_sum, total_sum, max_sum; + Tree() { + left_sum = -INF; + right_sum = -INF; + total_sum = -INF; + max_sum = -INF; + } +} tr[MAX * 4]; + +int ar[MAX]; + + +Tree combine (Tree a, Tree b) { + Tree tmp; + tmp.left_sum = max (a.left_sum, a.total_sum + b.left_sum); + tmp.right_sum = max (b.right_sum, b.total_sum + a.right_sum); + tmp.total_sum = a.total_sum + b.total_sum; + tmp.max_sum = max3 (a.max_sum, b.max_sum, a.right_sum + b.left_sum); + return tmp; +} + +void build (int nod, int low, int high) { + if (low > high) return; + + if (low == high) { + tr[nod].left_sum = ar[low]; + tr[nod].right_sum = ar[low]; + tr[nod].total_sum = ar[low]; + tr[nod].max_sum = ar[low]; + return; + } + + int mid = (high + low) / 2; + build (nod * 2, low, mid); + build (nod * 2 + 1, mid + 1, high); + tr[nod] = combine (tr[nod * 2], tr[nod * 2 + 1]); +} + +void update (int nod, int low, int high, int pos, int val) { + if (pos > high || pos < low) { + return; + } + + if (low >= pos && high <= pos) { + tr[nod].left_sum = val; + tr[nod].right_sum = val; + tr[nod].total_sum = val; + tr[nod].max_sum = val; + return; + } + + int mid = (high + low) / 2; + update (nod * 2, low, mid, pos, val); + update (nod * 2 + 1, mid + 1, high, pos, val); + tr[nod] = combine (tr[nod * 2], tr[nod * 2 + 1]); +} + +Tree query (int nod, int low, int high, int qlow, int qhigh) { + if (qlow > high || qhigh < low) { + Tree tmp; + return tmp; + } + + if (low >= qlow && high <= qhigh) { + return tr[nod]; + } + + int mid = (high + low) / 2; + Tree a = query (nod * 2, low, mid, qlow, qhigh); + Tree b = query (nod * 2 + 1, mid + 1, high, qlow, qhigh); + return combine (a, b); +} + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + int n; + cin >> n; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + build (1, 0, n - 1); + int q; + cin >> q; + + while (q--) { + int l, r; + int c; + cin >> c; + cin >> l >> r; + + if (c == 0) { + update (1, 0, n - 1, l - 1, r); + + } else { + Tree ans = query (1, 0, n - 1, l - 1, r - 1); + cout << ans.max_sum << "\n"; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS3/GSS3-9.cpp b/spoj_cpp_clean/GSS3/GSS3-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..679aeb7dbfe30084d10aa92ff87d410020bb5424 --- /dev/null +++ b/spoj_cpp_clean/GSS3/GSS3-9.cpp @@ -0,0 +1,134 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define MAX 50007 +using namespace std; + +struct n +{ + long long left; + long long right; + long long all; + long long best; +}; + +typedef struct n node; + +node tree[MAX*4]; +int arr[MAX+1]; +int N, E; + +void init(int index, int a, int b) +{ + if(a==b) + { + tree[index].all=tree[index].best=tree[index].left=tree[index].right=arr[a]; + return; + } + + init(index*2, a, (a+b)/2); + init(index*2+1, (a+b)/2+1, b); + + tree[index].left=max(tree[index*2].left, tree[index*2].all+tree[index*2+1].left); + tree[index].right=max(tree[index*2+1].right, tree[index*2+1].all + tree[index*2].right); + tree[index].all=tree[index*2].all+tree[index*2+1].all; + tree[index].best=max(tree[index*2].right+tree[index*2+1].left, max(tree[index*2].best, tree[index*2+1].best)); +} + +void update(int index, int a, int b, int pos, int val) +{ + if(a==b) + { + tree[index].all=tree[index].best=tree[index].left=tree[index].right=val; + return; + } + + if(pos<=(a+b)/2) + update(index*2, a, (a+b)/2, pos, val); + else + update(index*2+1, (a+b)/2+1, b, pos, val); + + tree[index].left=max(tree[index*2].left, tree[index*2].all+tree[index*2+1].left); + tree[index].right=max(tree[index*2+1].right, tree[index*2+1].all + tree[index*2].right); + tree[index].all=tree[index*2].all+tree[index*2+1].all; + tree[index].best=max(tree[index*2].right+tree[index*2+1].left, max(tree[index*2].best, tree[index*2+1].best)); +} + +node query(int index, int a, int b, int i, int j) +{ + node temp; + node *left, *right; + + if(a==i && b==j) + { + temp.all=tree[index].all; + temp.best=tree[index].best; + temp.left=tree[index].left; + temp.right=tree[index].right; + + return temp; + } + + if(j<=(a+b)/2) + return query(index*2, a, (a+b)/2, i, j); + + else if(i>=(a+b)/2+1) + return query(index*2+1, (a+b)/2+1, b, i, j); + + else + { + left=&query(index*2, a, (a+b)/2, i, (a+b)/2); + right=&query(index*2+1, (a+b)/2+1, b, (a+b)/2+1, j); + + temp.all=left->all+right->all; + temp.left=max(left->left, left->all + right->left); + temp.right=max(right->right, right->all + left->right); + temp.best=max(left->right+right->left, max(left->best, right->best)); + + return temp; + } +} + +int main() +{ + + int i; + int type, f, s; + node *temp; + + scanf("%d", &N); + + for(i=1; i<=N; i++) + scanf("%d", &arr[i]); + + init(1, 1, N); + + scanf("%d", &E); + + for(i=0; ibest, temp->all), max(temp->left, temp->right))); + } + + else + update(1, 1, N, f, s); + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS5/GSS5-12.cpp b/spoj_cpp_clean/GSS5/GSS5-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43b8680912a9d8b5acaa2173ba35157e04c34b78 --- /dev/null +++ b/spoj_cpp_clean/GSS5/GSS5-12.cpp @@ -0,0 +1,168 @@ +//#include +#include +#include +#include +#include +#define mp make_pair +#define pb push_back +#define MAXN 51000 + +using namespace std; + +typedef long long int64; + +int m, n; + +struct data { + int64 sum, pref, suff, ans; + data(int64 sum = 0, int64 pref = 0, int64 suff = 0, int64 ans = 0) + : sum(sum) + , pref(pref) + , suff(suff) + , ans(ans) + { + } +}; + +data t[5 * MAXN]; +int a[MAXN]; + +int readInt() +{ + bool minus = false; + int result = 0; + char ch = getchar(); + while (true) { + if (ch == '-') + break; + if (ch >= '0' && ch <= '9') + break; + ch = getchar_unlocked(); + } + if (ch == '-') + minus = true; + else + result = ch - '0'; + while (true) { + ch = getchar_unlocked(); + if (ch < '0' || ch > '9') + break; + result = result * 10 + (ch - '0'); + } + if (minus) + return -result; + else + return result; +} + +data combine(data l, data r) +{ + data res; + res.sum = l.sum + r.sum; + res.pref = max(l.pref, l.sum + r.pref); + res.suff = max(r.suff, r.sum + l.suff); + res.ans = max(max(l.ans, r.ans), l.suff + r.pref); + return res; +} + +data make_data(int64 val) +{ + return data(val, val, val, val); +} + +void build(int node, int tl, int tr) +{ + if (tl == tr) + t[node] = make_data(a[tl]); + else { + int tm = (tl + tr) >> 1; + build(2 * node, tl, tm); + build(2 * node + 1, tm + 1, tr); + t[node] = combine(t[2 * node], t[2 * node + 1]); + } +} + +void update(int node, int tl, int tr, int pos, int64 new_val) +{ + if (tl == tr) + t[node] = make_data(new_val); + else { + int tm = (tl + tr) >> 1; + if (pos <= tm) + update(2 * node, tl, tm, pos, new_val); + else + update(2 * node + 1, tm + 1, tr, pos, new_val); + t[node] = combine(t[2 * node], t[2 * node + 1]); + } +} + +data query(int node, int tl, int tr, int l, int r) +{ + if (l > r || l < 0 || r < 0 || r > tr || l < tl) + return make_data(0); + if (l == tl && tr == r) + return t[node]; + int tm = (tl + tr) >> 1; + if (r <= tm) + return query(2 * node, tl, tm, l, r); + if (l > tm) + return query(2 * node + 1, tm + 1, tr, l, r); + return combine(query(2 * node, tl, tm, l, tm), query(2 * node + 1, tm + 1, tr, tm + 1, r)); +} + +int main() +{ + int t; + t = readInt(); + while (t--) { + n = readInt(); + int opc, x1, y1, x2, y2; + for (int i = 0; i < n; i++) { + a[i] = readInt(); + } + build(1, 0, n - 1); + + m = readInt(); + data resp; + while (m--) { + x1 = readInt(); + y1 = readInt(); + + x2 = readInt(); + y2 = readInt(); + + data suf, sum, pref; + int64 ans = 0; + + if (x1 == y1 && y1 == x2) { + sum = query(1, 0, n - 1, x1 - 1, y1 - 1); //miolo + ans = sum.sum; + if (x2 != y2) { + pref = query(1, 0, n - 1, x2, y2 - 1); //pref + ans += pref.pref; + } + } else if (y1 >= x2) { + //compartilham um subconjunto + suf = query(1, 0, n - 1, x1 - 1, x2 - 2); //suf + sum = query(1, 0, n - 1, x2 - 1, y1 - 1); //miolo + pref = query(1, 0, n - 1, y1, y2 - 1); //pref + ans = max(suf.suff + sum.sum + pref.pref, suf.suff + sum.sum); + ans = max(ans, max(sum.sum + pref.pref, suf.sum + sum.sum)); + ans = max(ans, max(suf.sum + sum.sum + pref.pref, suf.sum + sum.sum + pref.sum)); + ans = max(ans, max(suf.suff + sum.sum + pref.sum, sum.sum + pref.sum)); + } else { + //disjuntos + suf = query(1, 0, n - 1, x1 - 1, y1 - 1); //suf + if (x2 - y1 > 1) + sum = query(1, 0, n - 1, y1, x2 - 2); //miolo + else + sum = make_data(0); + pref = query(1, 0, n - 1, x2 - 1, y2 - 1); //pref + ans = suf.suff + sum.sum + pref.pref; + } + + printf("%lld\n", ans); + } + } + return 0; +} diff --git a/spoj_cpp_clean/GSS5/GSS5-3.cpp b/spoj_cpp_clean/GSS5/GSS5-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b451e01829d81d18d8af4b3f2331bfeb5562fe0 --- /dev/null +++ b/spoj_cpp_clean/GSS5/GSS5-3.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + int prefixMaxSum, suffixMaxSum, maxsum, sum; + + void assignLeaf(int value) { + prefixMaxSum = suffixMaxSum = maxsum = sum = value; + } + + void merge(node& left, node& right) { + sum = left.sum + right.sum; + prefixMaxSum = max(left.prefixMaxSum, left.sum + right.prefixMaxSum); + suffixMaxSum = max(right.suffixMaxSum, right.sum + left.suffixMaxSum); + maxsum = max(prefixMaxSum, max(suffixMaxSum, max(left.maxsum, max(right.maxsum, left.suffixMaxSum + right.prefixMaxSum)))); + } +}; + +int t, n, array[50000], m , X1, Y1, X2, Y2, res; +node tree[150001]; + +// l and r represent the range node i is responsible for. A single call init(1, 0, n-1) builds the segment tree in O(N) time +void init(int i, int l, int r){ + if(l == r){ + tree[i].assignLeaf(array[l]); + } + else{ + init(2*i, l, (l+r)/2); + init((2*i) + 1, ((l+r)/2) + 1, r); + node left = tree[2*i], right = tree[(2*i)+1]; + + tree[i].merge(left, right); //Merge the children into the parent + } + return; +} + +// O(log N) +node query(int i, int l, int r, int lo, int hi){ + if(lo > hi) + return tree[0]; + if(l == lo and r == hi) //If the range we want to query is the exact range + return tree[i]; + int mid = (l+r)/2; + if(lo > mid) //If only the subtree rooted at right child is repsonsible for this range + return query((2*i)+1, mid+1, r, lo, hi); + if(hi <= mid) //If only the subtree rooted at the left child is responsible for this range + return query(2*i, l, mid, lo, hi); + //If both, the left and right children are partially responsible for the range + node left = query(2*i, l, mid, lo, mid); + node right = query((2*i)+1, mid+1, r, mid+1, hi); + node result; + + result.merge(left, right); //Merge the results of the children into the parent + return result; +} + +//Call using update(1, 0, N-1, index, value) for a point update in O(log N) +void update(int i, int lo, int hi, int index, int value){ + if (lo == hi){ + tree[i].assignLeaf(value); + return; + } + int left = 2*i, right = left+1, mid = (lo+hi)/2; + if(index <= mid) + update(left, lo, mid, index, value); + else + update(right, mid+1, hi, index, value); + tree[i].merge(tree[left], tree[right]); + return; +} + +int main(){ + sd(t); + while(t--){ + sd(n); + for(int i = 0; i < n; i++) sd(array[i]); + + init(1, 0, n-1); + + sd(m); + while(m--){ + scanf("%d %d %d %d",&X1, &Y1, &X2, &Y2); + if(Y1 < X2){ + // no overlap + res = query(1,0,n-1,X1-1,Y1-1).suffixMaxSum + query(1,0,n-1,X2-1,Y2-1).prefixMaxSum + query(1,0,n-1,Y1, X2-2).sum; + printf("%d\n",res); + } + else{ + // overlap + res = max(query(1,0,n-1,X1-1,X2-2).suffixMaxSum + query(1,0,n-1,X2-1,Y2-1).prefixMaxSum, + max(query(1,0,n-1,X1-1,Y1-1).suffixMaxSum + query(1,0,n-1,Y1,Y2-1).prefixMaxSum, query(1,0,n-1,X2-1,Y1-1).maxsum)); + printf("%d\n",res); + } + } + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS5/GSS5-8.cpp b/spoj_cpp_clean/GSS5/GSS5-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7111282e80433da0c13971095a75195eb6ac724 --- /dev/null +++ b/spoj_cpp_clean/GSS5/GSS5-8.cpp @@ -0,0 +1,200 @@ +/*************************************************** + * Problem Name : GSS5 - Can you answer these queries V.cpp + * Problem Link : https://www.spoj.com/problems/GSS5/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2020-04-18 + * Problem Type : Segemen Tree + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +struct Node { + ll left_sum, right_sum, total_sum, max_sum; + Node() { + left_sum = -INF; + right_sum = -INF; + total_sum = 0; + max_sum = -INF; + } +} tr[MAX * 4]; + +ll ar[MAX]; + + +Node combine (Node a, Node b) { + Node tmp; + tmp.left_sum = max (a.left_sum, a.total_sum + b.left_sum); + tmp.right_sum = max (b.right_sum, b.total_sum + a.right_sum); + tmp.total_sum = a.total_sum + b.total_sum; + tmp.max_sum = max3 (a.max_sum, b.max_sum, (a.right_sum + b.left_sum) ); + return tmp; +} + +void build (int nod, int low, int high) { + if (low > high) return; + + if (low == high) { + tr[nod].left_sum = ar[low]; + tr[nod].right_sum = ar[low]; + tr[nod].total_sum = ar[low]; + tr[nod].max_sum = ar[low]; + return; + } + + int mid = (high + low) / 2; + build (nod * 2, low, mid); + build (nod * 2 + 1, mid + 1, high); + tr[nod] = combine (tr[nod * 2], tr[nod * 2 + 1]); +} +Node query (int nod, int low, int high, int qlow, int qhigh) { + if (qlow > high || qhigh < low) { + Node tmp; + return tmp; + } + + if (low >= qlow && high <= qhigh) { + return tr[nod]; + } + + int mid = (high + low) / 2; + Node a = query (nod * 2, low, mid, qlow, qhigh); + Node b = query (nod * 2 + 1, mid + 1, high, qlow, qhigh); + Node res = combine (a, b); + return res; +} + +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + int n; + cin >> tc; + + while (tc--) { + cin >> n; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + build (1, 0, n - 1); + int q; + cin >> q; + + while (q--) { + int x1, y1, x2, y2; + cin >> x1 >> y1 >> x2 >> y2; + x1--, y1--, x2--, y2--; + ll ans = 0; + + if (y1 >= x2) { + Node res1 = query (1, 0, n - 1, x1, x2 - 1); + Node res2 = query (1, 0, n - 1, x2, y2); + ans = res1.right_sum + res2.left_sum; + res1 = query (1, 0, n - 1, x2, y1); + res2 = query (1, 0, n - 1, y1 + 1, y2); + ans = max (ans, res1.right_sum + res2.left_sum); + res1 = query (1, 0, n - 1, x2, y1); + ans = max (ans, res1.max_sum); + + } else { + Node res1 = query (1, 0, n - 1, x1, y1); + Node res2 = query (1, 0, n - 1, y1 + 1, x2 - 1); + Node res3 = query (1, 0, n - 1, x2, y2); + ans = res1.right_sum + res2.total_sum + res3.left_sum; + } + + cout << ans << "\n"; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/GSS6/GSS6-3.cpp b/spoj_cpp_clean/GSS6/GSS6-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2dce2f6398d99405638e4cb1fcab79abde3e6377 --- /dev/null +++ b/spoj_cpp_clean/GSS6/GSS6-3.cpp @@ -0,0 +1,241 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<size : 0; +} + +void updSize(node *cur){ + if(!cur) return; + cur->size = getSize(cur->l) + 1 + getSize(cur->r); +} + +void updNode(node *cur){ + if(!cur) return; + cur->reset(); + + node *l = cur->l; + node *r = cur->r; + if(l and r){ + cur->lsum = max(l->lsum, max(l->sum + cur->sum, l->sum + cur->sum + r->lsum)); + cur->rsum = max(r->rsum, max(r->sum + cur->sum, r->sum + cur->sum + l->rsum)); + cur->sum = l->sum + cur->val + r->sum; + cur->mx = max(cur->mx, max(l->mx, r->mx)); + cur->mx = max(cur->mx, max(cur->lsum, cur->rsum)); + + cur->mx = max(cur->mx, max(l->rsum + cur->val, r->lsum + cur->val)); + cur->mx = max(cur->mx, l->rsum + cur->val + r->lsum); + } + else if(l){ + cur->lsum = max(l->lsum, l->sum + cur->lsum); + cur->rsum = max(cur->rsum, cur->sum + l->rsum); + cur->sum = cur->sum + l->sum; + + cur->mx = max(cur->mx, max(cur->lsum, cur->rsum)); + cur->mx = max(cur->mx, l->mx); + } + else if(r){ + cur->lsum = max(cur->lsum, cur->sum + r->lsum); + cur->rsum = max(r->rsum, r->sum + cur->rsum); + cur->sum = cur->sum + r->sum; + + cur->mx = max(cur->mx, max(cur->lsum, cur->rsum)); + cur->mx = max(cur->mx, r->mx); + } +} + +typedef pair LR; + +LR split(node *root, int pos){ + if(!root) return LR(NULL, NULL); + + LR ret; + + int cur_size = getSize(root->l) + 1; + if(cur_size <= pos){ + LR tmp = split(root->r, pos - cur_size); + root->r = tmp.fi; + ret = mp(root, tmp.se); + } + else{ + LR tmp = split(root->l, pos); + root->l = tmp.se; + ret = mp(tmp.fi, root); + } + + updSize(root); // update the parameters of root since its children have possibly been modified + updNode(root); + return ret; +} + +node *merge(node *l, node *r){ + if(!l or !r) return l ? l : r; + + if(l->pri > r->pri){ + l->r = merge(l->r, r); + updSize(l); // update the parameters of l since its children have possibly been modified + updNode(l); + return l; + } + else{ + r->l = merge(l, r->l); + updSize(r); // update the parameters of r since its children have possibly been modified + updNode(r); + return r; + } +} + +ll range_query(node * &treap, int x, int y){ + node *l, *mid, *r; + + LR tmp; + + tmp = split(treap, y); + mid = tmp.fi, r = tmp.se; + + tmp = split(mid, x-1); + l = tmp.fi, mid = tmp.se; + + ll ans = mid->mx; + + treap = merge(l, mid); + treap = merge(treap, r); + + return ans; +} + +void insert(node * &treap, int x, int y){ + node *nn = new node(y); + + LR tmp = split(treap, x-1); + + treap = merge(tmp.fi, nn); + treap = merge(treap, tmp.se); +} + +void del(node * &treap, int x){ + LR tmp = split(treap, x-1); + node *l = tmp.fi, *r = tmp.se; + + tmp = split(r, 1); + r = tmp.se; + + treap = merge(l, r); +} + +void replace(node * &treap, int x, int y){ + LR tmp = split(treap, x-1); + node *l = tmp.fi, *r = tmp.se; + + tmp = split(r, 1); + r = tmp.se; + + tmp.fi = new node(y); + treap = merge(l, tmp.fi); + treap = merge(treap, r); +} + +} // end of namespace + +using namespace Treap; + +int main(){ + srand(time(NULL)); + + int n; + sd(n); + + int xx; + sd(xx); + node *treap = new node(xx); + for(int i = 2; i <= n; i++){ + sd(xx); + node *nn = new node(xx); + treap = merge(treap, nn); + } + + + char t; + int x, y; + + int q; + sd(q); + while(q--){ + scanf(" %c%d", &t, &x); + if(t == 'I'){ + sd(y); + insert(treap, x, y); + } + else if(t == 'D'){ + del(treap, x); + } + else if(t == 'R'){ + sd(y); + replace(treap, x, y); + } + else{ + sd(y); + printf("%d\n", range_query(treap, x, y)); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/GUESSTHE/GUESSTHE-13.cpp b/spoj_cpp_clean/GUESSTHE/GUESSTHE-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fd414e8b104f987a596422c4daf83e14b7dfb27 --- /dev/null +++ b/spoj_cpp_clean/GUESSTHE/GUESSTHE-13.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +typedef long long i64; +char buffer[100]; + +int gcd(int a, int b){ + if(b == 0)return a; + else return gcd(b, a % b); +} + +int main(){ + //freopen("data.in", "r", stdin); + while(scanf("%s", buffer) == 1 && buffer[0] != '*'){ + string s(buffer); + i64 n = 1LL; + for(int i=0, k=1, m=s.size(); i 0); + } + if(flag)printf("%d\n",n); + else printf("%d\n",-1); + } + return 0; +} diff --git a/spoj_cpp_clean/HACKRNDM/HACKRNDM-3.cpp b/spoj_cpp_clean/HACKRNDM/HACKRNDM-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b50c58e914322069ef86088b061132343a29cd6 --- /dev/null +++ b/spoj_cpp_clean/HACKRNDM/HACKRNDM-3.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + map a; + int n, k, i, temp, count = 0; + sd2(n,k); + int array[n]; + for(i = 0; i < n; i++){ + sd(temp); + a[temp] = 1; + array[i] = temp; + } + for(i = 0; i < n; i++){ + if(a.find(array[i] - k ) != a.end()) + count++; + } + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +unordered_map A; +vector B; +int N, K; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +int main() +{ + int i, num, c=0; + + //freopen("input.txt","r",stdin); + + N=FAST_IO(); + K=FAST_IO(); + //scanf("%d %d", &N, &K); + + for(i=0; i +#define mp make_pair +#define pb push_back + +using namespace std; + +int neigh[100010]; + +typedef pair ii; +typedef long long int64; + +int main() +{ + ios::sync_with_stdio(false); + int c, n; + while (cin >> c >> n && c + n) { + for (int i = 0; i < n; i++) { + cin >> neigh[i]; + } + map vizinhos; + vizinhos.insert(mp(0, 0)); + int ans = 0; + bool flag = false; + for (int i = 0; i < n; i++) { + ans = (ans + neigh[i]) % c; + if (!vizinhos.count(ans)) + vizinhos.insert(mp(ans, i + 1)); + else { + flag = true; + for (int k = vizinhos[ans] + 1; k <= i + 1; k++) { + cout << ((k == vizinhos[ans] + 1) ? "" : " "); + cout << k; + } + cout << endl; + break; + } + } + if (!flag) + cout << "no sweets\n"; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/HANGOVER/HANGOVER-11.cpp b/spoj_cpp_clean/HANGOVER/HANGOVER-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66ff25ca8663490e505fa29b2c166c6e406ada4b --- /dev/null +++ b/spoj_cpp_clean/HANGOVER/HANGOVER-11.cpp @@ -0,0 +1,35 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include + +using namespace std; + +int main(void) +{ + float input, memory[300]; + + memory[0] = memory[1] = 0.0f; + + for(int i = 2; i < 300; i++) + memory[i] = memory[i-1] + (1.0f/i); + + cin >> input; + + while(input != 0) + { + int i; + + for(i = 2; i < 300; i++) + if(memory[i] > input) + break; + + cout << i-1 << " card(s)" << endl; + + cin >> input; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/HANGOVER/HANGOVER-16.cpp b/spoj_cpp_clean/HANGOVER/HANGOVER-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddf8f3e1bb5f3efacb053525b12a71feaed10b07 --- /dev/null +++ b/spoj_cpp_clean/HANGOVER/HANGOVER-16.cpp @@ -0,0 +1,53 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "< +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(double n){ + double ans=0.0, x=2.0; + ll cnt=0; + while(ans>test_cases; + double n; + while(cin>>n){ + if(n==0.00) + break; + solve(n); + } + return 0; +} diff --git a/spoj_cpp_clean/HANGOVER/HANGOVER-19.cpp b/spoj_cpp_clean/HANGOVER/HANGOVER-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93830394eebf507f306047024076587cbd6842f8 --- /dev/null +++ b/spoj_cpp_clean/HANGOVER/HANGOVER-19.cpp @@ -0,0 +1,32 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 + +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + while(true){ + float sum; + cin>>sum; + if(sum==0.00){ + return 0; + } + else{ + cout<var){ + var= var + (1.00/i); + i++; + n++; + } + cout< + +using namespace std; + +float hang_arr[300]; + +int main() +{ + float a,b,c; + for(float i=1;i<300;i++) + { + hang_arr[(int)i]=(1/(i+1)); + } + /*for(int i=1;i<10;i++) + cout<>a; + float sum=0.00; + while(a) + { + for(int i=1;;i++) + { + sum=sum+hang_arr[i]; + if(sum>=a) + { + cout<>a; + } + return 0; +} diff --git a/spoj_cpp_clean/HASH/HASH-3.cpp b/spoj_cpp_clean/HASH/HASH-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c3a50f2cf2e052ea3ebadaa21bc870200f05be5 --- /dev/null +++ b/spoj_cpp_clean/HASH/HASH-3.cpp @@ -0,0 +1,88 @@ +/* + * Hash problem sample solution by Jelani Nelson + * + */ + +#include +using namespace std; +typedef long long LL; + +// if high, calculate size of intersection of u%m,(u+d)%m,...,(u+n*d)%m with +// [-L+1, 0]. else, calculate size of intersection with [0, L-1]. +LL calc(LL u, LL d, LL n, LL L, LL m, bool high) { + if (!L) + return 0; + if (m == 1 || L >= m) + return n + 1; + if (!d || !n) + return (high ? (!u || (u > m - L)) : (u < L)) * (n + 1); + + LL k; + if (high) + k = (u + n*d + m - 1)/m + !u; + else + k = (u + n*d)/m + 1; + + LL f0 = 0, f1 = 0; + if (high) { + if (!u) + f0 = 1; + else { + LL c1 = max((m - L + 1 - u + d - 1)/d, 0LL); + LL c2 = min((m - u)/d, n); + f0 = max(c2 - c1 + 1, 0LL); + } + LL w = (u + n*d + m - 1)/m * m; + LL c = (w - L + 1 - u + d - 1)/d; + if (k > 1) + f1 = max(n - c + 1, 0LL); + } else { + if (u <= L - 1) + f0 = min((L - 1 - u)/d, n) + 1; + LL w = (u + n*d)/m * m; + LL c = (w - u + d - 1)/d; + if (k > 1) + f1 = max(min((w + L - 1 - u)/d, n) - c + 1, 0LL); + } + + if (k <= 2) + return f0 + f1; + if (d == 1) + return f0 + f1 + L*(k - 2); + + LL r = L % d, s = m % d, t; + if (high) { + LL e = (u + ((1+!!u)*m - u)/d * d) % m; + if (!e) + e = m; + e = m - e; + if (s <= d/2) + t = calc(e % d, s, k - 3, r, d, 0); + else + t = calc((d - (e % d)) % d, d - s, k - 3, r, d, 1); + } else { + LL e = (u + (m - u + d - 1)/d * d) % m; + if (d - s <= d/2) + t = calc(e % d, d - s, k - 3, r, d, 0); + else + t = calc((d - (e % d)) % d, s, k - 3, r, d, 1); + } + + return f0 + f1 + (k - 2)*(L/d) + t; +} + +// calculate number of values a*x+b,a*(x+1)+b,...,a*(x+n)+b +// that land in [c, d] when taken modulo m. running time is +// O(min(log(a), log(n))) +int main() { + LL a, b, x, n, c, d, m, no_cases; + cin >> no_cases; + while (no_cases--) { + cin >> a >> b >> x >> n >> c >> d >> m; + LL u = (a*x + b) % m; + LL ret = n + 1 - calc(u, a, n, c, m, 0) - calc(u, a, n, m - d, m, 1); + ret += calc(u, a, n, 1, m, 0); + cout << ret << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/HASHIT/HASHIT-13.cpp b/spoj_cpp_clean/HASHIT/HASHIT-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..631ae0641cf1dc82aae9bdd9a35715aff25414b3 --- /dev/null +++ b/spoj_cpp_clean/HASHIT/HASHIT-13.cpp @@ -0,0 +1,97 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +int getPos(string &key){ + int k = 0; + for(int i=0, n = key.size(); i &inside,vector &mark, vector &v, int &counter, string &key){ + if(inside.find(key)==inside.end()){ + int k = getPos(key); + for(int i=0; i<20; ++i){ + if(!mark[((i*i)+(23*i)+k)%101]){ + inside.insert(key); + v[((i*i)+(23*i)+k)%101] = key; + mark[((i*i)+(23*i)+k)%101] = true; + counter++; + break; + } + } + } +} +void del(set &inside,vector &mark, vector &v, int &counter, string &key){ + set ::iterator it = inside.find(key); + if(it != inside.end()){ + inside.erase(it); + int k = getPos(key); + for(int i=0; i<20; ++i){ + if(mark[((i*i)+(23*i)+k)%101] && v[((i*i)+(23*i)+k)%101] == key){ + mark[((i*i)+(23*i)+k)%101]= false; + counter--; + break; + } + } + } +} +int main(){ + int T; scanf("%d", &T); + char buffer[1000]; + for(int t=0; t mark(101, false); + vector v(101, ""); + set inside; + int counter = 0; + int n; scanf("%d", &n); + for(int k=0; k> ins >> key; + //printf("%s %s\n",ins.c_str(), key.c_str()); + if(ins == "ADD")add(inside,mark,v,counter,key); + else del(inside,mark,v,counter,key); + } + printf("%d\n",counter); + for(int i=0; i<101; ++i){ + if(mark[i]){ + printf("%d:%s\n",i,v[i].c_str()); + } + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/HAYBALE/HAYBALE-19.cpp b/spoj_cpp_clean/HAYBALE/HAYBALE-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5a3055f9432c59c56ab49555a6e98a7d0482745 --- /dev/null +++ b/spoj_cpp_clean/HAYBALE/HAYBALE-19.cpp @@ -0,0 +1,55 @@ +// Author : @AdarshGaur + +#include +using namespace std; + +#ifdef cicada +#include "debug.h" +#else +#define debug(...) "Yume o akiramete shinde kure." +#endif + +#define int int64_t +#define endl "\n" +typedef pair pii; + +int const INF = 2e18; +int const mod = 1e9 +7; + + +void solve(){ + int n, k; + cin >> n >> k; + + int a[n]; + memset(a, 0, sizeof(a)); + for(int i=0; i> l >> r; + l--, r--; + a[l]++; + if(r+1> test; + for(int t=1; t<=test; t++){ + // cout<<"Case #"< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar() + +using namespace std; + +int N; + +inline bool get_bit() +{ + char c; + + c = gc(); + gc(); + gc(); + gc(); + + if (c == 'l') + return 1; + + return 0; +} + +inline void solve() +{ + bool v = 0; + bool b; + + scanf("%d\n", &N); + + while (N--) + { + b = get_bit(); + v ^= b; + } + + if (v) + printf("lxh\n"); + else + printf("hhb\n"); +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + scanf("%d", &T); + + while (T--) + { + solve(); + } + + return 0; +} diff --git a/spoj_cpp_clean/HC12/HC12-19.cpp b/spoj_cpp_clean/HC12/HC12-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20eace3a402af83881ebff0ec12fb7ab43b57fef --- /dev/null +++ b/spoj_cpp_clean/HC12/HC12-19.cpp @@ -0,0 +1,135 @@ +#pragma GCC optimize("Ofast") +#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") +#pragma GCC optimize("unroll-loops") +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair p32; +typedef pair p64; +typedef pair pdd; +typedef vector v64; +typedef vector v32; +typedef vector > vv32; +typedef vector > vv64; +typedef vector > vvp64; +typedef vector vp64; +typedef vector vp32; +ll MOD = 998244353; +double eps = 1e-12; +#define forn(i,s,e) for(ll i = s; i < e; i++) +#define rforn(i,s,e) for(ll i = s; i >= e; i--) +#define endl "\n" +#define dbg(x) cout<<#x<<" = "< 0) { +// if( x & 1) { +// res = (res * a) % m; +// } +// a = (a * a) % m; +// x = x >> 1; // x=x/2; +// } +// return res; +// } + + +// unsigned long long modInverse(unsigned long long a,long long m){ +// return modPow(a,m-2,m); +// } + + +unsigned long long chooseMod(unsigned long long n, long long r, long long m){ + //base Case + if(r==0){ + return 1; + } + if(n>n>>k; + ll arr[n]; + forn(i,0,n){ + cin>>arr[i]; + } + ll ans=0; + sort(arr, arr+n); + rforn(i,n,k+1){ + ll temp = chooseMod(i-1, k-1, mod); + ans = (ans + ((temp * arr[i-1]%mod)%mod))%mod; + } + ans = (ans + arr[k-1]%mod)%mod; + cout<> t; + forn(it, 1, t+1){ + cout << "Case #" << it << ": "; + solve(); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/HELPR2D2/HELPR2D2-3.cpp b/spoj_cpp_clean/HELPR2D2/HELPR2D2-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cc920d3e57fb68cc382a34c3efaec6ffa82cc36 --- /dev/null +++ b/spoj_cpp_clean/HELPR2D2/HELPR2D2-3.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + int best, l, r, indx, isLeaf; + + void assignLeaf(int value, int i) { + best = l = r = value; + indx = i; + isLeaf = 1; + } + + void merge(node& left, node& right) { + best = max(left.best, right.best); + l = left.best; + r = right.best; + } +}; + +int T, k, n, array[1<<17], c, s, w, a, cnt; +char q[6]; +node tree[1<<18]; + + +// l and r represent the range node i is responsible for. A single call init(1, 0, n-1) builds the segment tree in O(N) time +void init(int i, int l, int r){ + if(l == r){ + tree[i].assignLeaf(array[l], l); + } + else{ + init(2*i, l, (l+r)/2); + init((2*i) + 1, ((l+r)/2) + 1, r); + node left = tree[2*i], right = tree[(2*i)+1]; + + tree[i].merge(left, right); //Merge the children into the parent + } + return; +} + +// O(log N) +void query(int i, int val){ + if(tree[i].isLeaf == 1){ + array[tree[i].indx] -= val; + tree[i].best -= val; + tree[i].l = tree[i].r = tree[i].best; + return; + } + else if(tree[i].best < val) + return; + else{ + if(tree[i].l >= val) + query(2*i, val); + else + query((2*i)+1,val); + tree[i].merge(tree[2*i], tree[(2*i)+1]); + return; + } +} + +int main(){ + sd(T); + while(T--){ + sd(k); + for(int i = 0; i < 100000; i++) + array[i] = k; + + init(1, 0, 99999); + + s = 0, w = 0; + sd(n); + while(n){ + scanf("%s",q); + if(q[0] == 'b'){ + scanf("%s",q); + a = atoi(q); + scanf("%s",q); + c = atoi(q); + while(a--){ + query(1,c); + n--; + } + } + else{ + c = atoi(q); + query(1,c); + n--; + } + } + for(int i = 0; i < 100000; i++){ + if(array[i] != k){ + s++; + w += array[i]; + } + } + printf("%d %d\n",s,w); + } + return 0; +} diff --git a/spoj_cpp_clean/HIGHWAYS/HIGHWAYS-7.cpp b/spoj_cpp_clean/HIGHWAYS/HIGHWAYS-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e53052d128efa2077739c3f1e2f47c87ee8abe5c --- /dev/null +++ b/spoj_cpp_clean/HIGHWAYS/HIGHWAYS-7.cpp @@ -0,0 +1,81 @@ +#include +#define nodo second +#define peso first +#define MAXINT 2000000 + +using namespace std; + +vector< pair > grafo[100100]; +int dist[100100]; + +int t,n,m,a,b,p,ini,fin; + +void inicializarD(int x,int n) +{ + for (int i = 0; i <= n; ++i) + { + dist[i]= x; + } +} + + +void Dijkstra(int source, int destiny) +{ + inicializarD(MAXINT,n+10); + dist[source]=0; + priority_queue< pair,vector > ,greater > > q; + int v; + int peso_u_v; + + q.push( make_pair(0,source) ); + + while(!q.empty()) + { + pair u= q.top(); q.pop(); + + for (int i = 0; i < grafo[u.nodo].size(); ++i) + { + peso_u_v= grafo[u.nodo][i].peso; + v= grafo[u.nodo][i].nodo; + + if (dist[u.nodo] + peso_u_v < dist[v]) + { + dist[v]= dist[u.nodo] + peso_u_v; + q.push( make_pair(peso_u_v,v) ); + } + } + } + + (dist[fin]==MAXINT)?printf("NONE\n"):printf("%d\n",dist[fin] ); + +} + +void inicializarG(int n) +{ + for (int i = 0; i <= n; ++i) + { + grafo[i].clear(); + } +} + +int main() +{ + + scanf("%d",&t); + while(t>0) + { + scanf("%d %d %d %d",&n,&m,&ini,&fin); + inicializarG(n); + for (int i = 0; i < m; ++i) + { + scanf("%d %d %d",&a,&b,&p); + grafo[a].push_back( make_pair(p,b) ); + grafo[b].push_back( make_pair(p,a) ); + } + + Dijkstra(ini,fin); + + t--; + } + return 0; +} diff --git a/spoj_cpp_clean/HISTOGRA/HISTOGRA-8.cpp b/spoj_cpp_clean/HISTOGRA/HISTOGRA-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fccad006d20bd5cfba6a1b66b5c96d8f0ab23ae0 --- /dev/null +++ b/spoj_cpp_clean/HISTOGRA/HISTOGRA-8.cpp @@ -0,0 +1,127 @@ +/*************************************************** + * Problem Name : HISTOGRA - Largest Rectangle in a Histogram.cpp + * Problem Link : https://www.spoj.com/problems/HISTOGRA/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-09-23 + * Problem Type : Data Structure (Segment tree) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[MAX], tree[MAX * 3], n; +ll mx; +void build (int nod, int low, int high) { + if (low == high) { + tree[nod] = low; + return ; + + } else { + int mid = (high + low) / 2; + int left = nod * 2; + int right = nod * 2 + 1; + build (left, low, mid); + build (right, mid + 1, high); + + if (ar[tree[left]] <= ar[tree[right]]) + tree[nod] = tree[left]; + else tree[nod] = tree[right]; + } +} +int query (int nod, int low, int high, int l, int r) { + if (l > high || r < low) { + return 0; + } + + if (l <= low && r >= high) { + return tree[nod]; + } + + int mid = (high + low) / 2; + int left = nod * 2; + int right = nod * 2 + 1; + int a = query (left, low, mid, l, r); + int b = query (right, mid + 1, high, l, r); + + if ( ar[a] <= ar[b]) return a; + else return b; +} +void solve (int l, int r) { + if (l > r) return ; + + int idx = query (1, 1, n, l, r); + ll xx = (ll) ar[idx] * (ll) (r - l + 1LL); + mx = max (mx, xx); + solve (l, idx - 1); + solve (idx + 1, r); +} + +int main () { + __FastIO; + + while (cin >> n) { + if (n == 0) break; + + for (int i = 1; i <= n; i++) { + cin >> ar[i]; + } + + ar[0] = 1000000005; + build (1, 1, n); + mx = 0; + solve (1, n); + cout << mx << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/HOLES/HOLES-5.cpp b/spoj_cpp_clean/HOLES/HOLES-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22337e410fd3740091a809040a0d109858e7cb6b --- /dev/null +++ b/spoj_cpp_clean/HOLES/HOLES-5.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; + +int main() +{ + int cases,count,i; + char input[100]; + + cin>>cases; + + while(cases--) + { + cin>>input; + count=0; + + for(i=0; i +using namespace std; + +#define ll long long + +bitset <1000009> check; +ll dv[10000009]; +unordered_map cnt; + +#define ll long long + +ll mulmod(ll a, ll b, ll c) +{ + ll x = 0, y = a % c; + while (b) + { + if (b & 1) x = (x + y) % c; + y = (y << 1) % c; + b >>= 1; + } + return x % c; +} + +ll fastPow(ll x, ll n, ll MOD) +{ + ll ret = 1; + while (n) + { + if (n & 1) ret = mulmod(ret, x, MOD); + x = mulmod(x, x, MOD); + n >>= 1; + } + return ret; +} + +bool isPrime(ll n) +{ + if(n == 2 || n == 3) return true; + if(n == 1 || !(n & 1)) return false; + ll d = n - 1; + int s = 0; + while (d % 2 == 0) + { + s++; + d /= 2; + } + + int a[9] = { 2, 3, 5, 7, 11, 13, 17, 19, 23 }; + for(int i = 0; i < 9; i++) + { + bool comp = fastPow(a[i], d, n) != 1; + if(comp) for(int j = 0; j < s; j++) + { + ll fp = fastPow(a[i], (1LL << (ll)j)*d, n); + if (fp == n - 1) + { + comp = false; + break; + } + } + if(comp) return false; + } + return true; +} + + +int main() + +{ + for(ll i = 2; i <= 1000000; i++) { + if(check[i] == 0) { + for(ll j = i * i; j <= 1000000; j += i) + check[j] = 1; + } + } + ll n, m; + while(scanf("%lld", &n) && n != -1) { + + ll ans1 = 1, ans2; + m = n; + + + if(n <= 1000000 && check[n] == 0) { + ans1 = 1, ans2 = n; + } + else if(n > 1000000 && isPrime(n)) { + ans1 = 1, ans2 = n; + } + else { + ll indx = 0; + for(ll i = 2; i * i * i <= n; i++) { + if(n % i == 0) { + dv[indx++] = i; + while(n % i == 0) { + n /= i; + cnt[i]++; + } + } + } + + if(n > 1) { + ll sqt = sqrt(n); + if(sqt * sqt == n) { + dv[indx++] = sqt; + cnt[sqt] += 2; + } + dv[indx++] = n; + cnt[n]++; + } + + ll lim; + for(ll i = 0; i < indx; i++) { + lim = cnt[ dv[i] ] / 2; + for(ll j = 1; j <= lim; j++) + ans1 *= dv[i]; + } + + ans2 = m / (ans1 * ans1); + } + + printf("%lld %lld\n", ans1, ans2); + cnt.clear(); + } + + return 0; diff --git a/spoj_cpp_clean/HOMO/HOMO-7.cpp b/spoj_cpp_clean/HOMO/HOMO-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0831e535e21fe5c822c3a53eb660a426dc103f48 --- /dev/null +++ b/spoj_cpp_clean/HOMO/HOMO-7.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + +int main() +{ + int t, aux; + char accion[20]; + map m; + map::iterator it; + int homo=0,hetero=0; + + scanf("%d",&t); + while(t--) + { + scanf("%s %d",accion,&aux); + if (!strcmp(accion,"insert")) + { + m[aux]++; + + if (m[aux]==2) + { + homo++; + }else if (m[aux]==1) + { + hetero++; + } + }else + { + if(m[aux]>0) + { + m[aux]--; + if (m[aux]==1) + { + homo--; + }else if (m[aux]==0) + { + hetero--; + } + } + } + + if (homo>= 1 && hetero>= 2) + { + printf("both\n"); + }else if (homo>=1) + { + printf("homo\n"); + }else if (hetero>= 2) + { + printf("hetero\n"); + }else + { + printf("neither\n"); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/HORRIBLE/HORRIBLE-14.cpp b/spoj_cpp_clean/HORRIBLE/HORRIBLE-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f795a543eb5a6fffec02dafbb927b4eed7c84ae --- /dev/null +++ b/spoj_cpp_clean/HORRIBLE/HORRIBLE-14.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define ll long long + +ll tree[400009]; +ll prop[400009]; + +void init(ll left, ll right, ll node) + +{ + if(left == right) { + tree[node] = 0; + prop[node] = 0; + return; + } + + ll mid = (left + right) / 2; + init(left, mid, 2 * node); + init(mid + 1, right, 2 * node + 1); + tree[node] = tree[2 * node] + tree[2 * node + 1]; + prop[node] = prop[2 * node] + prop[2 * node + 1]; +} + +void update(ll lo, ll hi, ll left, ll right, ll value, ll node) + +{ + if(lo > right || hi < left) + return; + if(lo >= left && hi <= right) { + tree[node] += (hi - lo + 1) * value; + prop[node] += value; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, left, right, value, 2 * node); + update(mid + 1, hi, left, right, value, 2 * node + 1); + tree[node] = tree[2 * node] + tree[2 * node + 1] + (hi - lo + 1) * prop[node]; +} + +ll query(ll lo, ll hi, ll left, ll right, ll carry, ll node) + +{ + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) + return tree[node] + (hi - lo + 1) * carry; + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, carry + prop[node], 2 * node); + ll p2 = query(mid + 1, hi, left, right, carry + prop[node], 2 * node + 1); + + return p1 + p2; +} + +int main() + +{ + ll t; + cin >> t; + while(t--) { + ll n, c; + scanf("%lld %lld", &n, &c); + init(0, n - 1, 1); + for(ll i = 1; i <= c; i++) { + ll state, p, q, v; + scanf("%lld", &state); + if(state == 0) { + scanf("%lld %lld %lld", &p, &q, &v); + update(0, n - 1, p - 1, q - 1, v, 1); + } + else { + scanf("%lld %lld", &p, &q); + printf("%lld\n", query(0, n - 1, p - 1, q - 1, 0, 1)); + } + } + + } + + return 0; +} diff --git a/spoj_cpp_clean/HORRIBLE/HORRIBLE-3.cpp b/spoj_cpp_clean/HORRIBLE/HORRIBLE-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf64026e92e547b89b2593de021a08f4578e3421 --- /dev/null +++ b/spoj_cpp_clean/HORRIBLE/HORRIBLE-3.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%lld",&x); +#define sd2(x,y) scanf("%lld %lld",&x,&y); +#define sd3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define sull2(x,y) scanf("%ull %ull",&x,&y); +#define sull3(x,y,z) scanf("%ull %ull %ull",&x,&y,&z); + +using namespace std; + +long long t1[100001], t2[100001]; +long long n; +long long get(long long t[100001],long long i){ + long long sum = 0; + while(i > 0){ + sum += t[i]; + i = i - (i&(-i)); + } + return sum; +} + +void update(long long t[100001],long long i, long long val){ + while( i <= n){ + t[i] += val; + i = i + (i&(-i)); + } + return; +} + +void range_update(long long left, long long right, long long val){ + update(t1, left, val); + update(t1, right+1, -val); + update(t2, left, val*(left-1)); + update(t2, right+1, -val*right); + return; +} + +long long query(long long b){ + return get(t1,b)* b - get(t2,b); +} + +int main(){ + long long T, c, type, p, q, val; + sd(T); + while(T--){ + sd2(n,c); + for(long long i = 0; i <= n; i++){ + t1[i] = 0; + t2[i] = 0; + } + for(long long i = 0; i < c; i++){ + sd(type); + if(type){ + sd2(p,q); + printf("%lld\n",query(q)-query(p-1)); + } + else{ + sd3(p,q,val); + range_update(p,q,val); + } + } + + } + return 0; +} diff --git a/spoj_cpp_clean/HORRIBLE/HORRIBLE-4.cpp b/spoj_cpp_clean/HORRIBLE/HORRIBLE-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..618c90b3636fcbbb30ebb60ed6df9ad4f82721f1 --- /dev/null +++ b/spoj_cpp_clean/HORRIBLE/HORRIBLE-4.cpp @@ -0,0 +1,162 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +struct node +{ + long long sum, nodes_value; +}t[500000]; + +void clear() +{ + FOR(i,0,4*n) + t[i].sum=t[i].nodes_value=0; +} + +void update(int index, int st, int en, int x, int y, long long val) +{ + if(st>y || eny || en=x && en<=y) + { + long long d=en-st+1; + return d*cumulative_values+t[index].sum; + } + else + { + int mid=(st+en)/2; + cumulative_values+=t[index].nodes_value; + long long a=query(2*index, st, mid, x, y, cumulative_values); + long long b=query(2*index+1, mid+1, en, x, y, cumulative_values); + return a+b; + } +} +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T, a, b,x; + long long c; + s(T); + long long offset=0; + while(T--) + { + + s(n); + s(m); + clear(); + FOR(i,0,m) + { + s(x); + if(x==0) + { + s(a); s(b); sl(c); + update(1, 0, n-1, a-1, b-1, c); + } + else + { + s(a); s(b); + cout< + +using namespace std; + +typedef long long ll; +const int MAXN = 1e5; + +struct info { + ll prop, sum; +} tree[MAXN*3]; + +void init( int node, int left, int right ) { + if( left > right ) return; + if( left == right ) { + tree[node].sum = 0; + return; + } + int child1 = node << 1, child2 = (node << 1) + 1, mid = (left + right) >> 1; + init( child1, left, mid ); + init( child2, (mid + 1), right ); + tree[node].sum = tree[child1].sum + tree[child2].sum; +} + +void update( int node, int left, int right, int I, int J, ll value ) { + if( left > right || I > right || J < left ) return; + if( left >= I && right <= J ) { + tree[node].sum += ((right - left + 1) * value); + tree[node].prop += value; + return; + } + int child1 = node << 1, child2 = (node << 1) + 1, mid = (left + right) >> 1; + update( child1, left, mid, I, J, value ); + update( child2, (mid + 1), right, I, J, value ); + tree[node].sum = tree[child1].sum + tree[child2].sum + (right - left + 1) * tree[node].prop; +} + +ll query( int node, int left, int right, int I, int J, ll carry = 0 ) { + if( left > right || I > right || J < left ) return 0; + if( left >= I && right <= J ) return tree[node].sum + carry * (right - left + 1); + int child1 = node << 1, child2 = (node << 1) + 1, mid = (left + right) >> 1; + ll p1 = query( child1, left, mid, I, J, (carry + tree[node].prop) ); + ll p2 = query( child2, (mid + 1), right, I, J, (carry + tree[node].prop) ); + return (p1 + p2); +} + +int main( int argc, char ** argv ) { +// freopen( "in.txt", "r", stdin ); +// freopen( "out.txt", "w", stdout ); + + int T; + scanf( "%d", &T ); + for( int nCase = 1 ; nCase <= T ; ++nCase ) { + int N, C; + scanf( "%d %d", &N, &C ); + + memset( tree, 0, sizeof( tree ) ); + init( 1, 1, N ); + + while( C-- ) { + int cmd, p, q; + scanf( "%d %d %d", &cmd, &p, &q ); + switch( cmd ) { + case 0: + ll v; + scanf( "%lld", &v ); + update( 1, 1, N, p, q, v ); + break; + case 1: + ll ans = query( 1, 1, N, p, q ); + printf( "%lld\n", ans ); + break; + } + } + } + return 0; +} diff --git a/spoj_cpp_clean/HORRIBLE/HORRIBLE-8.cpp b/spoj_cpp_clean/HORRIBLE/HORRIBLE-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95542958df3ec1d9446cba4e4115c90ae1747418 --- /dev/null +++ b/spoj_cpp_clean/HORRIBLE/HORRIBLE-8.cpp @@ -0,0 +1,155 @@ +/*************************************************** + * Problem Name : HORRIBLE - Horrible Queries.cpp + * Problem Link : https://www.spoj.com/problems/HORRIBLE/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-09-23 + * Problem Type : Data Structure (Lagy Propagate) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +ll arr[MAX], tree[MAX * 3], lazy[MAX * 3]; +void build (ll node, ll a, ll b) { + if (a > b) return; + + if (a == b) { + tree[node] = arr[a]; + return; + } + + build (node * 2, a, (a + b) / 2); + build (node * 2 + 1, (a + b) / 2 + 1, b); + tree[node] = tree[node * 2] + tree[node * 2 + 1]; +} + +ll query (ll node, ll a, ll b, ll i, ll j) { + if (a > b || a > j || b < i) return 0; + + if (lazy[node] != 0 ) { + tree[node] += lazy[node] * (ll) (b - a + 1); + + if (a != b) { + lazy[node * 2] += lazy[node]; + lazy[node * 2 + 1] += lazy[node]; + } + + lazy[node] = 0; + } + + if (a >= i && b <= j) return tree[node]; + + ll q1 = query (node * 2, a, (a + b) / 2, i, j); + ll q2 = query (node * 2 + 1, (a + b) / 2 + 1, b, i, j); + return q1 + q2; +} + +void update (ll node, ll a, ll b, ll i, ll j, + ll inc) { + if (a > b) return; + + if (lazy[node] != 0) { + tree[node] += lazy[node] * (ll) (b - a + 1); + + if (a != b) { + lazy[node * 2] += lazy[node]; + lazy[node * 2 + 1] += lazy[node]; + } + + lazy[node] = 0; + } + + if (a > b || a > j || b < i) return; + + if (a >= i && b <= j) { + tree[node] += inc * (b - a + 1); + + if (a != b) { + lazy[node * 2] += inc; + lazy[node * 2 + 1] += inc; + } + + return; + } + + update (node * 2, a, (a + b) / 2, i, j, inc); + update (node * 2 + 1, (a + b) / 2 + 1, b, i, j, inc); + tree[node] = tree[node * 2] + tree[node * 2 + 1]; +} +int main () { + __FastIO; + int tc, n, q; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> n >> q; + build (1, 0, n - 1); + SET (lazy, 0); + + while (q--) { + int c, u, v, val; + cin >> c; + + if (c == 0) { + cin >> u >> v >> val; + update (1, 0, n - 1, u - 1, v - 1, val); + + } else { + cin >> u >> v; + cout << query (1, 0, n - 1, u - 1, v - 1) << "\n"; + } + } + } + + return 0; +} diff --git a/spoj_cpp_clean/HORRIBLE/HORRIBLE-9.cpp b/spoj_cpp_clean/HORRIBLE/HORRIBLE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7e581706853baa4d0bbce576dd782f7a28f4248 --- /dev/null +++ b/spoj_cpp_clean/HORRIBLE/HORRIBLE-9.cpp @@ -0,0 +1,123 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define gc() getchar_unlocked() + +int N; +long long tree1[100007]; +long long tree2[100007]; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +inline void point_update_1(int pos, long long val) +{ + for( ; pos <= N; pos += pos & (-pos)) + tree1[pos] += val; +} + +inline void point_update_2(int pos, long long val) +{ + for( ; pos <= N; pos += pos & (-pos)) + tree2[pos] += val; +} + +inline void range_update(int i, int j, long long val) +{ + point_update_1(i, val); + point_update_1(j + 1, -val); + + point_update_2(i, val * (i-1)); + point_update_2(j + 1, - val * j); +} + +inline long long point_query_1(int pos) +{ + long long sum = 0; + + for(; pos > 0; pos -= pos & (-pos)) + sum += tree1[pos]; + + return sum; +} + +inline long long point_query_2(int pos) +{ + long long sum = 0; + + for(; pos > 0; pos -= pos & (-pos)) + sum += tree2[pos]; + + return sum; +} + +inline long long start_query(int pos) +{ + return point_query_1(pos) * pos - point_query_2(pos); +} + +inline long long range_query(int i, int j) +{ + return start_query(j) - start_query(i-1); +} + +int main() +{ + int T, Q; + int type, f, s, val; + +// freopen("input.txt","r",stdin); + + T=FAST_IO(); + + while(T--) + { + memset(tree1, 0, sizeof(tree1)); + memset(tree2, 0, sizeof(tree2)); + + N=FAST_IO(); + Q=FAST_IO(); + + while(Q--) + { + type=FAST_IO(); + f=FAST_IO(); + s=FAST_IO(); + + if(type==0) + { + val=FAST_IO(); + range_update(f, s, val); + } + + else + printf("%lld\n", range_query(f, s)); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/HOTELS/HOTELS-13.cpp b/spoj_cpp_clean/HOTELS/HOTELS-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa188e0212aab7a57bdab4fc09f72de9f2a9ed71 --- /dev/null +++ b/spoj_cpp_clean/HOTELS/HOTELS-13.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +#include +#include +#include + +using namespace std; +typedef long long i64; + +i64 f(vector &v, int n, i64 m){ + int low = 1, high = 1; + i64 best = 0LL; + while (low < n){ + //open + for(;high < n && v[high] - v[low-1] <= m; high++); + + //check + i64 next = v[high-1] - v[low-1]; + best = max(best, next); + + //close + low++; + } + return best; +} + +int main(){ + //freopen("data.in","r",stdin); + int n; i64 m; + scanf("%d %lld", &n, &m); + vector v(n+1); + v[0] = 0LL; + for(int i=1; i<=n; ++i){ + scanf("%lld", &v[i]); + v[i] += v[i-1]; + } + i64 res = f(v,n+1,m); + printf("%lld\n", res); + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/HOTELS/HOTELS-16.cpp b/spoj_cpp_clean/HOTELS/HOTELS-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0427d6109dda7d3d05b7da6c30d7f8b43449564 --- /dev/null +++ b/spoj_cpp_clean/HOTELS/HOTELS-16.cpp @@ -0,0 +1,60 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "<=b;i--) +#define loop(i,a,b) for(long long i=a; i +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(){ + ll n,m; + cin>>n>>m; + vll a(n); + loop(i,0,n) cin>>a[i]; + ll j=0, ans=0; + vll psum(n,0); + psum[0]=a[0]; + loop(i,1,n) psum[i]=psum[i-1]+a[i]; + loop(i,1,n){ + if(psum[i]>m){ + while(psum[i]-psum[j]>m){ + j++; + } + } + ans = max(ans, psum[i]-psum[j]); + } + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/HOTELS/HOTELS-7.cpp b/spoj_cpp_clean/HOTELS/HOTELS-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e6fb002215e8576f41aa51ecdd84d7055632524 --- /dev/null +++ b/spoj_cpp_clean/HOTELS/HOTELS-7.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int max(int x,int y) +{ + if(x>y) + { + return x; + }else + { + return y; + } +} + + +int main() +{ + + int tam=0,suma=0,k=0,n=0,aux=0,ini,fin; + int maximo= -1; + + scanf("%d %d",&n,&k); + int v[n+1]; + for (int i = 0; i < n; ++i) + { + scanf("%d",&aux); + v[i]=aux; + } + ini=0;fin=n-1; + aux=0; + for (int i = 0; i < n; ++i) + { + suma= max(suma+v[i],0); + while(suma>k) + { + suma-=v[aux]; + aux++; + } + if (suma<=k) + { + maximo=max(suma,maximo); + }if (suma<=0) + { + suma=0; + aux=i+1; + } + } + + + printf("%d\n",maximo ); + return 0; +} diff --git a/spoj_cpp_clean/HOTELS/HOTELS-8.cpp b/spoj_cpp_clean/HOTELS/HOTELS-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f314b6aee326f1a498fea471482c3e76a0f39b2c --- /dev/null +++ b/spoj_cpp_clean/HOTELS/HOTELS-8.cpp @@ -0,0 +1,94 @@ +/*************************************************** + * Problem Name : HOTELS - Hotels Along the Croatian Coast.cpp + * Problem Link : https://www.spoj.com/problems/HOTELS/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-09-30 + * Problem Type : Two Pointer Technique + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 300005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[MAX]; +int twoPointer (int n, int val) { + int i = 0, j = 0; + int sum = 0, ans = 0; + + while (1) { + if (sum + ar[j] <= val) { + sum += ar[j]; + j++; + ans = max (ans, sum); + if(j == n) break; + + } else { + ans = max (ans, sum); + sum -= ar[i]; + i++; + } + } + + return ans; +} +int main () { + __FastIO; + int n, val; + cin >> n >> val; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + cout << twoPointer (n, val) << "\n"; + return 0; +} diff --git a/spoj_cpp_clean/HOTELS/HOTELS-9.cpp b/spoj_cpp_clean/HOTELS/HOTELS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc3e42dc488b59d47660a3644249f73289f4d56f --- /dev/null +++ b/spoj_cpp_clean/HOTELS/HOTELS-9.cpp @@ -0,0 +1,68 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +deque A; +int N, M; + +int main() +{ + int i, num, MAX = 0, sum = 0; + + //freopen("input.txt","r",stdin); + + scanf("%d %d", &N, &M); + + for(i=0; i M) + { + A.clear(); + sum = 0; + continue; + } + + if(sum + num <= M) + { + A.push_back(num); + sum += num; + MAX=max(MAX, sum); + continue; + } + + while(sum + num > M) + { + sum -= A.front(); + A.pop_front(); + } + + sum += num; + A.push_back(num); + MAX = max(MAX, sum); + } + + printf("%d\n", MAX); + + return 0; +} diff --git a/spoj_cpp_clean/HPYNOS/HPYNOS-16.cpp b/spoj_cpp_clean/HPYNOS/HPYNOS-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ecec9934ff70fed217c6101e97712e31049403b2 --- /dev/null +++ b/spoj_cpp_clean/HPYNOS/HPYNOS-16.cpp @@ -0,0 +1,61 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "<=b;i--) +#define loop(i,a,b) for(long long i=a; i +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(){ + ll n,cnt=0; + cin>>n; + map vis; + while(n!=1){ + if(vis[n]){ + cout<<-1; + return; + } + vis[n]=true; + ll x = n; + n=0; + while(x>0){ + n+=(x%10)*(x%10); + x/=10; + } + cnt++; + } + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/HPYNOS/HPYNOS-19.cpp b/spoj_cpp_clean/HPYNOS/HPYNOS-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0fe381ae3ad89d3f0b40f0cb4b68454d3728e87 --- /dev/null +++ b/spoj_cpp_clean/HPYNOS/HPYNOS-19.cpp @@ -0,0 +1,33 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 + +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + unsigned ll num, sum=0; + unsigned int digit, t=0; + cin>>num; + while(num!=1){ + sum=0; + while(num!=0) + { + digit = num%10; + num/=10; + sum+=(digit*digit); + } + num = sum; + t++; + if(num==37){ + cout<<"-1"; + return 0; + } + } + cout< +#include + +using namespace std; + +int main() +{ + int num,temp,i,j,k,count; + + scanf("%d",&num); + + count=0; + if(num==1) + { + cout<<"0"< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +char str[15]; +bool V[300]; + +int break_num(int num) +{ + int i; + int sum=0; + + strcpy(str,to_string(num).c_str()); + for(i=0; i + +using namespace std; + +int main() +{ + int num,temp,count,i,j,k; + bool arr[1000]; + for(i=0;i<1000;i++) + arr[i]=false; + cin>>num; + count=0; + if(num==1) + { + cout<<"0"< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 105 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[MAX], n; +int fun (int i) { + if (i == n) return 0; + + return ar[i] + fun (i + 1); +} +int main () { + __FastIO; + int tc; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> n; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + cout << "Case " << t << ": " << fun (0) << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/HS08PAUL/HS08PAUL-15.cpp b/spoj_cpp_clean/HS08PAUL/HS08PAUL-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28a9a210667f54bcf16ac311afb356fe1a25d00e --- /dev/null +++ b/spoj_cpp_clean/HS08PAUL/HS08PAUL-15.cpp @@ -0,0 +1,88 @@ +/* +* author: @j-tesla +*/ + +// C++14 + +#pragma GCC optimize ("O3") +#pragma GCC target ("sse4") + +#include + +using namespace std; + +#define uns unsigned +typedef long long ll; +typedef vector vb; +typedef vector vc; +typedef vector vi; +typedef pair ii; +typedef vector vu; +typedef vector vl; + +#define din(x) int x; cin >> x; +#define in(x) cin >> x; +#define full(x) x.begin(), x.end() + +#define eb emplace_back +#define ins insert +#define f first +#define s second + +const int N = 10000000; + +inline ll form(const int i, const int j) { + return (i * i) + (j * j * j * j); +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + + int sqrtN = sqrt(N); + + vb is_prime(N + 1, true); + is_prime[0] = is_prime[1] = false; // 0 and 1 aren't primes + + for (int i = 2; i <= sqrtN; ++i) { + if (is_prime[i]) { + for (int j = i * i; j <= N; j += i) { + is_prime[j] = false; + } + } + } + + set Pauls; + int x = 1; + for (int x = 1; x <= sqrtN; ++x) { + for (int y = 1; form(x, y) <= N; ++y) { + + ll formula = form(x, y); + if (is_prime[formula]) { + Pauls.insert(formula); + } + + // almost forgot to increment (while loops are dangerous) + } + } + + + din(t) + while (t--) { + din(n) + int counter = 0; + bool flag = true; + for (int Paul : Pauls) { + if (Paul > n) { + cout << counter << endl; + flag = false; + break; + } + ++counter; + } + if (flag) + cout << Pauls.size() << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/HS08PAUL/HS08PAUL-20.cpp b/spoj_cpp_clean/HS08PAUL/HS08PAUL-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31bcfbe56b4268d5c879132ba05772c83aed425c --- /dev/null +++ b/spoj_cpp_clean/HS08PAUL/HS08PAUL-20.cpp @@ -0,0 +1,116 @@ +/** + * Author : Mr_Sohel + * Task : + * Algo : +**/ +#include + +#define endl '\n' +#define sqr(x) (x) * (x) +#define gcd(x,y) __gcd(x,y) +#define lcm(x,y) ((x/gcd(x,y)) * y) +#define pb push_back +#define pf push_front +#define mk make_pair +#define fi first +#define se second +#define sz(x) (int)x.size() +#define all(x) (x).begin(),(x).end() +#define rall(x) (x).rbegin(),(x).rend() +#define prec(x) fixed<; +using pll = pair; + +const ld PI = acos((ld) - 1); +const int MOD = 1e9 + 7; +const ll INF = 2e18 + 1; +const ld EPS = 1e-9; +const int MX = 1e7; + +#ifdef LOCAL +#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) +template < typename Arg1 > +void __f(const char* name, Arg1&& arg1) { + cerr << name << " = " << arg1 << endl; +} +template < typename Arg1, typename... Args> +void __f(const char* names, Arg1&& arg1, Args&&... args) { + const char* comma = strchr(names + 1, ','); + cerr.write(names, comma - names) << " = " << arg1 << " | "; + __f(comma + 1, args...); +} +#else +#define debug(...) +#endif + +bitset < MX + 5 > mark; + +void sieve() { + for (int i = 4; i <= MX; i += 2) mark[i] = true; + int sq = sqrt(MX) + 1; + for (int i = 3; i <= sq; i += 2) { + if (!mark[i]) { + for (int j = i * i; j <= MX; j += (i + i)) { + mark[j] = true; + } + } + } +} + +vector ans (MX, 13175); +set combi; + +void calc() { + for (int i = 1; i * i <= MX; i++) { + for (int j = 1; (j * j * j * j) <= MX; j++) { + int a = (i * i) + (j * j * j * j); + if (a <= MX and !mark[a]) { + combi.insert(a); + } + } + } + int i = 0, cnt = 0; + for (auto it = combi.begin(); it != combi.end(); ) { + if (*it <= i) { + cnt++; + it++; + } else { + ans[i] = cnt; + i++; + } + } +} + +int main() { + +#ifdef LOCAL + clock_t tStart = clock(); + freopen("in.txt", "r", stdin); + freopen("out.txt", "w", stdout); +#endif + unsyncIO; + sieve(); + calc(); + int t; cin >> t; + while (t--) { + int n; cin >> n; + cout << ans[n] << endl; + } + +#ifdef LOCAL + cerr << "\nRuntime: " << (ld) (clock() - tStart) / CLOCKS_PER_SEC << " Seconds" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/HS12HDPW/HS12HDPW-2.cpp b/spoj_cpp_clean/HS12HDPW/HS12HDPW-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f5a48e9699909af23981bcb1d3e572228ea1054d --- /dev/null +++ b/spoj_cpp_clean/HS12HDPW/HS12HDPW-2.cpp @@ -0,0 +1,74 @@ +// https://www.spoj.com/problems/HS12HDPW/ +// HS12HDPW - Hidden Password +#include +#include +#include +using namespace std; + +int main() +{ + int t; + cin >> t; + while(t--) { + int n; + cin >> n; + vector keys; + while(n--) { + string ascii; + cin >> ascii; + int bitA(1); // 00000001 + int bitB(8); // 00001000 + int a(0), b(0), i(0); + for (char c: ascii) { + a += (c & bitA); + bitA <<= 1; + if (bitA > 32) bitA = 1; + b += int((c & bitB) && bitB) * pow(2, i++); + bitB <<= 1; + if (bitB > 32) bitB = 1; + } + keys.push_back(a); + keys.push_back(b); + } + string code; + cin >> code; + for(int i: keys) + cout << code[i]; + cout << "\n"; + } + + return 0; +} + +/* +Example + +Input: +2 +2 +qwe345 rf3Arg +XSBSRasdew9873465hkldsfsalndfvnfq489uqovkLKJHaeDaae555Sk5asdpASD + +3 +2S4J5K 111111 lrtb2A +isimgsow45ipfgisd56wfgngdfcdkgc7kKKKkuuJJgfstdygQdWORQADFSLKF2K8 + +Output: +keep +coding + +Explanation +Let us have a look at the first 6-tuple: qwe345 + +char. ASCII code + q 113 = 01110001B + w 119 = 01110111B + e 101 = 01100101B + 3 51 = 00110011B + 4 52 = 00110100B + 5 53 = 00110101B + +a(blue bits) = 110111B = 55 +b(green bits) = 101110B = 46 + +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/HS12MBR/HS12MBR-2.cpp b/spoj_cpp_clean/HS12MBR/HS12MBR-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d8eb4c13c6072c9fd24dccef19fb0a9a11506530 --- /dev/null +++ b/spoj_cpp_clean/HS12MBR/HS12MBR-2.cpp @@ -0,0 +1,70 @@ +// https://www.spoj.com/problems/HS12MBR/ +// HS12MBR - Minimum Bounding Rectangle +#include +using namespace std; + +int main() +{ + int t, n; + cin >> t; // number of test cases + while(t--) { + cin >> n; // number of objects + char obj; + int x1, y1, x2, y2, r; + int xmin(1000), ymin(1000); + int xmax(-1000), ymax(-1000); + + while (n--) { + cin >> obj; + switch (obj) { + case 'p': // point + cin >> x1 >> y1; + if (x1 <= xmin) xmin = x1; + if (x1 >= xmax) xmax = x1; + if (y1 <= ymin) ymin = y1; + if (y1 >= ymax) ymax = y1; + break; + case 'c': // circle + cin >> x1 >> y1 >> r; + if (x1 - r <= xmin) xmin = x1 - r; + if (x1 + r >= xmax) xmax = x1 + r; + if (y1 - r <= ymin) ymin = y1 - r; + if (y1 + r >= ymax) ymax = y1 + r; + break; + case 'l': // line + cin >> x1 >> y1 >> x2 >> y2; + if (x1 > x2) swap(x1, x2); + if (y1 > y2) swap(y1, y2); + if (x1 <= xmin) xmin = x1; + if (x2 >= xmax) xmax = x2; + if (y1 <= ymin) ymin = y1; + if (y2 >= ymax) ymax = y2; + break; + } + } + cout << xmin << char(32) << ymin << char(32); + cout << xmax << char(32) << ymax << endl; + } + + return 0; +} + +/* +Example +Input: +3 +1 +p 3 3 + +2 +c 10 10 20 +c 20 20 10 + +1 +l 0 0 100 20 + +Output: +3 3 3 3 +-10 -10 30 30 +0 0 100 20 +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/HUBULLU/HUBULLU-14.cpp b/spoj_cpp_clean/HUBULLU/HUBULLU-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dab4a9504ec9856198c0f90f9f61433e321abb0 --- /dev/null +++ b/spoj_cpp_clean/HUBULLU/HUBULLU-14.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +#define ll long long + +int main() + +{ + ll t; + cin >> t; + while(t--) { + ll n, state; + scanf("%lld %lld", &n, &state); + if(state == 0) + printf("Airborne wins.\n"); + else + printf("Pagfloyd wins.\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/HUBULLU/HUBULLU-7.cpp b/spoj_cpp_clean/HUBULLU/HUBULLU-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2e1f57ee1d1548bfe8a29c64d8f1a2a998a4e63 --- /dev/null +++ b/spoj_cpp_clean/HUBULLU/HUBULLU-7.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main() +{ + int t,n,m; + scanf("%d",&t); + while(t--) + { + scanf("%d %d",&n,&m); + (m)?printf("Pagfloyd wins.\n"):printf("Airborne wins.\n");; + } + return 0; +} diff --git a/spoj_cpp_clean/HUBULLU/HUBULLU-9.cpp b/spoj_cpp_clean/HUBULLU/HUBULLU-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f3ec2c09ac72bcefb05e4a07c52ff7f37a56837 --- /dev/null +++ b/spoj_cpp_clean/HUBULLU/HUBULLU-9.cpp @@ -0,0 +1,33 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +int main() +{ + int T, n, p; + + scanf("%d", &T); + + while(T--) + { + scanf("%d %d", &n, &p); + + if(p) + printf("Pagfloyd wins.\n"); + else + printf("Airborne wins.\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/HUBULULLU/HUBULULLU-5.cpp b/spoj_cpp_clean/HUBULULLU/HUBULULLU-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e6780848b621e0e885009801668057f6ad0e5da --- /dev/null +++ b/spoj_cpp_clean/HUBULULLU/HUBULULLU-5.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + int test,n,p; + cin>>test; + while(test--) + { + cin>>n>>p; + if(p==0) + cout<<"Airborne wins."< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int fuk[1000]={0}; +int givnew(long long n) +{ + int x=0; + while(n) + { + x+=((n%10)*(n%10)); + n/=10; + } + return x; +} +map mp; +int main() +{ + fuk[4]=1;fuk[16]=1;fuk[20]=1;fuk[37]=1;fuk[42]=1; + fuk[58]=1;fuk[89]=1;fuk[145]=1; + // 4, 16, 20, 37, 42, 58, 89, 145, + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + long long n; + cin>>n; + int cnt=0; + while(1) + { + n=givnew(n); + cnt++; + if(fuk[n]==1) + { + cout<<"-1"; + return 0; + } + if(n==1) + break; + + } + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int N, M; +int marked[100007]; + +inline void init() +{ + scanf("%d %d", &N, &M); + + memset(marked, 0, sizeof(marked)); + marked[0] = 1; + marked[N + 1] = 1; +} + +inline int solve() +{ + int sum = 0, num, i; + + for (i = 0; i < M; i++) + { + scanf("%d", &num); + if (!marked[num]) + sum++; + if (!marked[num - 1]) + sum++; + if (!marked[num + 1]) + sum++; + + marked[num] = marked[num - 1] = marked[num + 1] = 1; + } + + return sum; +} + +int main() +{ + int T, i; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + for (i = 1; i <= T; i++) + { + init(); + printf("Case %d: %d\n", i, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/IAPCR2F/IAPCR2F-9.cpp b/spoj_cpp_clean/IAPCR2F/IAPCR2F-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0812bf806f4ddec98bc9fdf230b012ffa4714e7d --- /dev/null +++ b/spoj_cpp_clean/IAPCR2F/IAPCR2F-9.cpp @@ -0,0 +1,174 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +class ufs +{ + private: + struct set + { + int name; + int rank; + int money; + set *parent; + }; + int ssize; + vector S; + + set *fs(int name) + { + if (S[name].parent->name == name) + return &S[name]; + + S[name].parent = fs(S[name].parent->name); + return S[name].parent; + } + + + public: + ufs(int n) + { + int i; + ssize = n; + S.resize(ssize + 1); + + for (i = 1; iname; + } + + int get_money(int name) + { + return fs(name)->money; + } + + bool is_same_set(int a, int b) + { + return fs(a)->name == fs(b)->name; + } + + void union_sets(int a, int b) + { + set *f, *s; + + f = fs(a); + s = fs(b); + + if (f->name == s->name) + return; + + ssize--; + if (f->rank == s->rank) + { + f->rank++; + s->parent = f; + f->money += s->money; + } + + else if (f->rank > s->rank) + { + s->parent = f; + f->money += s->money; + } + + else + { + f->parent = s; + s->money += f->money; + } + } + + int size() + { + return ssize; + } +}; + +ufs *DSU; +int N, M; +bool V[1024]; + +inline void init() +{ + int i, f, s; + + delete DSU; + + memset(V, 0, sizeof(V)); + + scanf("%d %d", &N, &M); + + DSU = new ufs(N); + + for (i = 0; i < M; i++) + { + scanf("%d %d", &f, &s); + DSU->union_sets(f, s); + } +} + +inline void solve(int c) +{ + int i, t; + vector val; + + for (i = 1; i <= N; i++) + { + t = DSU->find_set(i); + if (!V[t]) + { + V[t] = 1; + val.push_back(DSU->get_money(t)); + } + } + + printf("Case %d: %d\n", c, val.size()); + + sort(val.begin(), val.end()); + + for (i = 0; i < val.size(); i++) + printf("%d ", val[i]); + + putchar('\n'); +} + +int main() +{ + int T, i; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + for (i = 1; i <= T; i++) + { + init(); + solve(i); + } + + return 0; +} diff --git a/spoj_cpp_clean/ICCSECRET/ICCSECRET-9.cpp b/spoj_cpp_clean/ICCSECRET/ICCSECRET-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..896e225508bea46df3554540cc448b44874b60f8 --- /dev/null +++ b/spoj_cpp_clean/ICCSECRET/ICCSECRET-9.cpp @@ -0,0 +1,81 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +int A[] = { 1, 2, 3 ,4, 5, 6, 7, 8, 9 }; +string S; +int N; + +inline void init() +{ + sort(A, A + 9); + + cin >> S; + N = S.length(); +} + +inline bool check() +{ + int i; + + for (i = 1; i <= N; i++) + { + switch (S[i - 1]) + { + case 'A': + if (A[i - 1] > A[i]) + return 0; + break; + case 'B': + if (A[i - 1] < A[i]) + return 0; + break; + } + } + + return 1; +} + +inline void solve() +{ + do + { + if (check()) + return; + } while (next_permutation(A, A + N + 1)); +} + +int main() +{ + int T, i; + + //freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + init(); + solve(); + + for (i = 0; i <= N; i++) + printf("%d", A[i]); + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/IIITD1/IIITD1-9.cpp b/spoj_cpp_clean/IIITD1/IIITD1-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..225f2dddfb14cdf5480b6480a17c627466e04015 --- /dev/null +++ b/spoj_cpp_clean/IIITD1/IIITD1-9.cpp @@ -0,0 +1,40 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define ll long long +using namespace std; + +int main() +{ + int T; + ll n1, n2; + +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + cin >> T; + + while (T--) + { + cin >> n1; + n2 = abs(n1); + n2 += pow(10, int(log10(n2))); + if (!n1) + cout << "1\n"; + else + cout << n2 - n1 << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/IITKWCPA/IITKWCPA-3.cpp b/spoj_cpp_clean/IITKWCPA/IITKWCPA-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..123f21dd0daaabcc6da54dd986dccca20451e946 --- /dev/null +++ b/spoj_cpp_clean/IITKWCPA/IITKWCPA-3.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + string input, s; + set d; + int t, flag = 0; + sd(t); + while(t--){ + s = ""; + d.clear(); + getline(cin, input); + for(int i = 0; i < input.length(); i++){ + if(input[i] != ' ') + s += input[i]; + else if(s != ""){ + d.insert(s); + s = ""; + } + } + if(s != "") + d.insert(s); + if(flag == 1) + cout< +#include + +using namespace std; + +int main() +{ + int test,n,i,j,count; + long int arr[100]; + bool used[100]; + scanf("%d",&test); + while(test--) + { + scanf("%d",n); + //long int* arr=new long int[n]; + for(i=0;i +#include +#include + +using namespace std; + +int main() +{ + int test,n,i,j,ans; + long int arr[200]; + bool used[200]; + scanf("%d",&test); + //cin>>test; + while(test--) + { + scanf("%d",&n); + //cin>>n; + //long int* arr=new long int[n]; + for(i=0;i>arr[i]; + used[i]=false; + } + sort(arr,arr+n); + ans=0; + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; +int t, k, l, freq[90], pos, cap, number=1, dp[90][90][90], INF = 100000000, mini; +char keys[100], letters[100], first_char; +string result[90]; +int main(){ + sd(t); + while(number <= t){ + sd2(k,l); + scanf("%s",keys); + scanf("%s",letters); + first_char = letters[0]; + for(int i = 0; i < l; i++){ + sd(freq[i]); + } + for(int i = 0; i < k; i++){ + result[i] = ""; + for(int j = 0; j < l; j++){ + for(int p = 0; p < l; p++){ + dp[i][j][p] = INF; + } + } + } + dp[0][0][0] = freq[0]; + for(int i = 1; i < l; i++){ + dp[0][i][i] = (dp[0][i-1][i-1] + (i+1)*freq[i]); + } + + for(int i = 1; i < k; i++){//which key + for(int j = i; j < l; j++){//which letter + for(int p = 0; p <= j - i; p++){//width of that button + if(p == 0){ + mini = INF; + for(int q = 0; q < j; q++){ + mini = min(mini,dp[i-1][j-1][q]); + + } + dp[i][j][p] = mini + (p+1)*freq[j]; + } + else{ + dp[i][j][p] = dp[i][j-1][p-1] + (p+1)*freq[j]; + } + } + } + } + cap = l-1; + for(int i = k-1; i >= 0; i--){ + for(int j = cap; j >= 0; j--){ + mini = INF, pos = -1; + for(int p = 0; p < j; p++){ + if(dp[i][j][p] <= mini){ + mini = dp[i][j][p]; + pos = p; + } + } + cap = cap - pos - 1; + while(pos >= 0){ + result[i] = letters[j--] + result[i]; + pos--; + } + j = -1; + } + } + if(result[0][0] != first_char){ + result[0] = first_char + result[0]; + } + printf("Keypad #%d:\n",number); + for(int i = 0; i < k; i++){ + printf("%c: ",keys[i]); + printf("%s\n",result[i].c_str()); + } + printf("\n"); + number++; + } + return 0; +} diff --git a/spoj_cpp_clean/ILKQUERY/ILKQUERY-9.cpp b/spoj_cpp_clean/ILKQUERY/ILKQUERY-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b353681b5385ef0a5743913e701e31ea3ea04ba --- /dev/null +++ b/spoj_cpp_clean/ILKQUERY/ILKQUERY-9.cpp @@ -0,0 +1,200 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +int N, Q, MAX; +int A[100007], B[100007]; +unordered_map map; +vector< int > appear[100007]; +int ans[100007]; +typedef pair< pair, pair > query; //i, k, l, pos +vector QUERIES; + +inline int FAST_IO() +{ + char ch; + int val = 0; + bool neg = 0; + + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + + if (ch == '-') + { + neg = 1; + ch = gc(); + } + + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + + if(neg) + return -val; + + return val; +} + +class BITPQ +{ + private: + int tree[100007]; + int N; + + public: + BITPQ(int n) + { + N = n; + memset(tree, 0, sizeof(tree)); + } + + inline void point_update(int pos, int val) + { + for (; pos <= N; pos += pos&(-pos)) + tree[pos] += val; + } + + inline void range_update(int i, int j, int val) + { + point_update(i, val); + point_update(j + 1, -val); + } + + inline int point_query(int pos) + { + int sum = 0; + + for (; pos > 0; pos -= pos&(-pos)) + sum += tree[pos]; + + return sum; + } +}; + +BITPQ *BIT; +inline void init() +{ + int i, c; + query temp; + + N = FAST_IO(); + Q = FAST_IO(); + //scanf("%d %d", &N, &Q); + + BIT = new BITPQ(N); + + for (i = 0; i < N; i++) + { + A[i] = FAST_IO(); + // scanf("%d", &A[i]); + B[i] = A[i]; + } + + sort(B, B + N); + + map[B[0]] = 1; + + c = 2; + for (i = 1; i < N; i++) + if (B[i] != B[i - 1]) + map[B[i]] = c++; + + MAX = map[B[N - 1]]; + + for (i = 0; i < N; i++) + appear[map[A[i]]].push_back(i); + + for (i = 0; i < Q; i++) + { + temp.second.second = i; + + temp.first.second = FAST_IO(); + temp.first.first = FAST_IO(); + temp.second.first = FAST_IO(); + //scanf("%d %d %d", &temp.first.second, &temp.first.first, &temp.second.first); + QUERIES.push_back(temp); + } + + sort(QUERIES.begin(), QUERIES.end()); +} + +inline int solve_query(int k, int l) +{ + if (k == 0) + return -1; + + int lo = 1, hi = MAX + 1, mid; + + while (lo + 1 != hi) + { + mid = (lo + hi) / 2; + if (BIT->point_query(mid) <= k - 1) + lo = mid; + else + hi = mid; + } + + //if (BIT->point_query(lo) != k - 1) + // return -1; + + if (appear[lo].size() < l) + return -1; + + return appear[lo][l - 1]; +} + +inline void solve() +{ + int i = 0, j; + + for (j = 0; j < QUERIES.size(); j++) + { + for (; i <= min(QUERIES[j].first.first, N - 1); i++) + BIT->range_update(map[A[i]] + 1, MAX + 1, 1); + + ans[QUERIES[j].second.second] = solve_query(QUERIES[j].first.second, QUERIES[j].second.first); + } +} + +inline void print() +{ + int i; + + for (i = 0; i < Q; i++) + printf("%d\n", ans[i]); +} + + +int main() +{ + //freopen("input.txt", "r", stdin); + + init(); + solve(); + print(); + + delete BIT; + + return 0; +} diff --git a/spoj_cpp_clean/ILKQUERY2/ILKQUERY2-9.cpp b/spoj_cpp_clean/ILKQUERY2/ILKQUERY2-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb409ccacb6b13fc2d265e9cebfad3700b62c498 --- /dev/null +++ b/spoj_cpp_clean/ILKQUERY2/ILKQUERY2-9.cpp @@ -0,0 +1,272 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#define gc() getchar() + +using namespace std; + +int A[100007]; +int N, Q; +unordered_map MAP; + +inline int FAST_IO() +{ + char ch; + int val = 0; + bool neg = 0; + + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + + if (ch == '-') + { + neg = 1; + ch = gc(); + } + + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + if (!neg) + return val; + return -val; +} + +class PersistentSegmentTree +{ + struct node + { + long long data; + struct node *left; + struct node *right; + + node(int d = 0) + { + data = d; + left = right = nullptr; + } + + struct node *insert(int i, int j, int pos) + { + struct node *temp = new struct node(); + + if (i == j) + { + *temp = *this; + + if (temp->data) + temp->data = 0; + else + temp->data = 1; + + return temp; + } + + int mid = (i + j) >> 1; + + if (pos <= mid) + { + temp->right = this->right; + temp->left = this->left->insert(i, mid, pos); + temp->data = temp->left->data + temp->right->data; + return temp; + } + + else + { + temp->left = this->left; + temp->right = this->right->insert(mid + 1, j, pos); + temp->data = temp->left->data + temp->right->data; + return temp; + } + } + + int query(int i, int j, int a, int b) + { + if (a == i && b == j) + return this->data; + + int mid = (i + j) >> 1; + + if (a > mid) + return this->right->query(mid + 1, j, a, b); + else if (b <= mid) + return this->left->query(i, mid, a, b); + else + return this->left->query(i, mid, a, mid) + this->right->query(mid + 1, j, mid + 1, b); + } + + ~node() + { + if (this->left != nullptr) + delete this->left; + + if (this->right != nullptr) + delete this->right; + + delete this; + } + }; + + vector trees; + int range; + + void init(struct node **curr, int i, int j) + { + *curr = new struct node(); + + if (i == j) + return; + + int mid = (i + j) >> 1; + + init(&(*curr)->left, i, mid); + init(&(*curr)->right, mid + 1, j); + } + + void only_update(struct node **curr, int i, int j, int pos) + { + if (i == j) + { + if ((*curr)->data) + { + (*curr)->data = 0; + return; + } + + (*curr)->data = 1; + return; + } + + int mid = (i + j) >> 1; + + if (pos <= mid) + only_update(&(*curr)->left, i, mid, pos); + else + only_update(&(*curr)->right, mid + 1, j, pos); + + (*curr)->data = (*curr)->left->data + (*curr)->right->data; + } + +public: + PersistentSegmentTree(int r) + { + struct node *temp; + range = r; + trees.push_back(temp); + init(&trees.back(), 1, range); + } + + inline void init_new(int pos) + { + trees.push_back(trees[0]->insert(1, range, pos)); + } + + inline void update(int num, int pos) + { + trees[num] = trees[num]->insert(1, range, pos); + } + + inline int query(int i, int j, int num) + { + return trees[num]->query(1, range, i, j); + } + + ~PersistentSegmentTree() + { + int i; + + for (i = 0; i < trees.size(); i++) + delete trees[i]; + } +}; + +PersistentSegmentTree *T; + +inline void init() +{ + int i, c; + vector< pair > B; + + N = FAST_IO(); + Q = FAST_IO(); + + T = new PersistentSegmentTree(N); + B.push_back(make_pair(0, 0)); + + for (i = 1; i <= N; i++) + { + A[i] = FAST_IO(); + B.push_back(make_pair(A[i], i)); + } + + sort(B.begin(), B.end()); + + c = 2; + + MAP[B[1].first] = 1; + + T->init_new(B[1].second); + + for (i = 2; i <= N; i++) + { + if (B[i].first != B[i - 1].first) + { + MAP[B[i].first] = c++; + T->init_new(B[i].second); + } + else + T->update(MAP[B[i].first], B[i].second); + } +} + +int main() +{ + int k, type, i, j, num; + + //freopen("input.txt", "r", stdin); + + init(); + + for (k = 0; k < Q; k++) + { + type = FAST_IO(); + + switch (type) + { + case 0: + i = FAST_IO(); + j = FAST_IO(); + num = FAST_IO(); + + if (MAP.find(num) == MAP.end()) + printf("0\n"); + else + printf("%d\n", T->query(i + 1, j + 1, MAP[num])); + break; + case 1: + num = FAST_IO(); + num++; + T->update(MAP[A[num]], num); + break; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/IMPEDIDO/IMPEDIDO-10.cpp b/spoj_cpp_clean/IMPEDIDO/IMPEDIDO-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..01930e747a4055723c58dc86bdfa83679ca86d20 --- /dev/null +++ b/spoj_cpp_clean/IMPEDIDO/IMPEDIDO-10.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +using namespace std; + +int main() +{ + int atacante, natacantes, ndefensores; + while(cin >> natacantes >> ndefensores && !(natacantes == 0 && ndefensores == 0)) + { + cin >> atacante; + for(int c, i = 1; i> c; + if(c < atacante) + atacante = c; + } + vector defensores(ndefensores); + for(int i = 0; i> defensores[i]; + } + if(ndefensores > 1) + { + sort(defensores.begin(), defensores.end()); + cout << ((atacante < defensores[1]) ? 'Y' : 'N') << endl; + } else + cout << 'Y'; + + } + return 0; +} diff --git a/spoj_cpp_clean/INCARDS/INCARDS-14.cpp b/spoj_cpp_clean/INCARDS/INCARDS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..317bbd56ea93e35c841050d1b7d91bdb4c8ff138 --- /dev/null +++ b/spoj_cpp_clean/INCARDS/INCARDS-14.cpp @@ -0,0 +1,85 @@ +#include +using namespace std; + +#define ll long long +#define pb push_back + +ll d1[1000009], d2[1000009]; +struct node { + ll u, c; +}; + +bool operator <(node a, node b) + +{ + return a.c > b.c; +} + +vector g[1000009], rev[1000009]; + +int main() + +{ + ll t; + cin >> t; + while(t--) { + for(ll i = 1; i <= 1000005; i++) { + d1[i] = 1e18; + d2[i] = 1e18; + g[i].clear(); + rev[i].clear(); + } + + ll n, q; + scanf("%lld %lld", &n, &q); + + for(ll i = 1; i <= q; i++) { + ll u, v, c; + scanf("%lld %lld %lld", &u, &v, &c); + g[u].pb({v, c}); + rev[v].pb({u, c}); + } + + priority_queue pq; + pq.push({1, 0}); + d1[1] = 0; + while(!pq.empty()) { + node u = pq.top(); + pq.pop(); + + if(d1[u.u] != u.c) + continue; + + for(node v: g[u.u]) { + if(u.c + v.c < d1[v.u]) { + d1[v.u] = u.c + v.c; + pq.push({v.u, d1[v.u]}); + } + } + } + + pq.push({1, 0}); + d2[1] = 0; + while(!pq.empty()) { + node u = pq.top(); + pq.pop(); + + if(d2[u.u] != u.c) + continue; + + for(node v: rev[u.u]) { + if(u.c + v.c < d2[v.u]) { + d2[v.u] = u.c + v.c; + pq.push({v.u, d2[v.u]}); + } + } + } + + ll ans = 0; + for(ll i = 1; i <= n; i++) + ans += d1[i] + d2[i]; + + printf("%lld\n", ans); + } + + return 0; diff --git a/spoj_cpp_clean/INCDSEQ/INCDSEQ-3.cpp b/spoj_cpp_clean/INCDSEQ/INCDSEQ-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94efc13794bc7b2043cd0a963c332b22262b3749 --- /dev/null +++ b/spoj_cpp_clean/INCDSEQ/INCDSEQ-3.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int K = 52, MOD = 5000000, N = 10001, f[52][10001], r[52][10001], INDEX = 0, seq[10001], v[10001], k, n, t, z, temp; +map last; + +int get(int *b, int indx){ + if(b == f[0]) return 1; + int sum = 0; + while(indx){ + sum = (sum + b[indx] + MOD)%MOD; + indx -= (indx & -indx); + } + return sum; +} +void update(int *b, int indx, int v){ + while(indx < N){ + b[indx] = (b[indx] + v + MOD)%MOD; + indx += (indx & -indx); + } + return; +} +int main(){ + sd2(n,k); + + for(int i = 0; i < n; ++i) scanf("%d", seq + i), v[i] = seq[i]; + sort(seq, seq + n); + + for(int i = 0; i < n; ++i) if(!last.count(seq[i])) last[seq[i]] = ++INDEX; + + for(int i = 0; i < n; ++i){ + z = last[v[i]]; + for(int L = 1; L <= k; ++L){ + int &x = r[L - 1][z - 1]; // The number of subsequences obtained on the previous occurence of the ith number + t = get(f[L - 1], z - 1); // The number obtained now + if(t != x){ // If this is not equal, then we must modify t before updating the bit + temp = t; + if((t -= x) < 0) t += MOD; + x = temp; + update(f[L], z, t); + } + } + } + printf("%d\n", get(f[k], N)); + return 0; +} + diff --git a/spoj_cpp_clean/INCSEQ/INCSEQ-14.cpp b/spoj_cpp_clean/INCSEQ/INCSEQ-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d6d21bfcb3f07397a76de5076a9d1b29420836c --- /dev/null +++ b/spoj_cpp_clean/INCSEQ/INCSEQ-14.cpp @@ -0,0 +1,73 @@ +#include +using namespace std; + +#define ll long long +#define mod 5000000 + +ll tree[52][400009]; + +void update(ll lo, ll hi, ll indx, ll node, ll val, ll len) + +{ + if(lo > indx || hi < indx) + return; + if(lo == hi) { + tree[len][node] += val; + if(tree[len][node] > mod) + tree[len][node] %= mod; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, indx, 2 * node, val, len); + update(mid + 1, hi, indx, 2 * node + 1, val, len); + + tree[len][node] = tree[len][2 * node] + tree[len][2 * node + 1]; + if(tree[len][node] > mod) + tree[len][node] %= mod; +} + +ll query(ll lo, ll hi, ll left, ll right, ll node, ll len) + +{ + + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) + return tree[len][node]; + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, 2 * node, len); + ll p2 = query(mid + 1, hi, left, right, 2 * node + 1, len); + + return (p1 + p2) % mod; +} + +int main() + +{ + ll n, k, ans = 0, in, ase; + cin >> n >> k; + for(ll i = 1; i <= n; i++) { + scanf("%lld", &in); + in++; + + for(ll j = 1; j <= k; j++) { + if(j == 1) + ase = 1; + else if(in == 1) + ase = 0; + else + ase = query(1, 100001, 1, in - 1, 1, j - 1); + + update(1, 100001, in, 1, ase, j); + + if(j == k) + ans = (ans + ase) % mod; + } + } + + printf("%lld\n", ans); + + return 0; +} diff --git a/spoj_cpp_clean/INCSEQ/INCSEQ-3.cpp b/spoj_cpp_clean/INCSEQ/INCSEQ-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a323ed434b84c60d43aaef9a1ab774cb3c25ec6f --- /dev/null +++ b/spoj_cpp_clean/INCSEQ/INCSEQ-3.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); +#define MAX 100002 + +using namespace std; + +/* +get(tnum, i) represents the number of increasing sequences of length tnum and smaller than i + +Proof of correctness - An update operation will traverse upwards in the tree. Only ranges which contain numbers greater than the +index i are affected by the update. This is what is required since the number of increasin sequnces of length k and highed element i have to be counted in the number of seqeunces with length k and highest element i+1 as well. Moreover when we obtain get(k, i), we will perform an update as update(k+1, i, get(k,i)). This is because the number of increasing sequences of length k+1 and maximum value i is the sum of get(k, i-1) and get(k-1, i). The BIT autmoatically accounts for get(k, i-1) for all j > i-1. +*/ +int bit[51][MAX], num[10000], MOD = 5000000, dp[51]; + +int get(int tnum, int indx){ + int sum = 0; + while(indx > 0){ + sum = (sum + bit[tnum][indx])%MOD; + indx -= (indx & -indx); + } + return sum; +} + +void update(int tnum, int indx, int val){ + while(indx < MAX){ + bit[tnum][indx] = (bit[tnum][indx] + val)%MOD; + indx += (indx & -indx); + } + return; +} + +int main(){ + int n, k; + sd2(n,k); + for(int i = 0; i < n; i++) sd(num[i]); + + memset(bit, 0, sizeof(bit)); + + for(int i = 0; i < n; i++){ + for(int j = 0; j < k; j++) + dp[j] = get(j, num[i]); + + update(0, num[i]+1, 1); + for(int j = 1; j < k; j++){ + update(j, num[i]+1, dp[j-1]); + } + } + + printf("%d\n",get(k-1,MAX-1)); +} diff --git a/spoj_cpp_clean/INVCNT/INVCNT-12.cpp b/spoj_cpp_clean/INVCNT/INVCNT-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa5229d7096a51b0b76930899c44c8d45da20c61 --- /dev/null +++ b/spoj_cpp_clean/INVCNT/INVCNT-12.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define mp make_pair +#define pb push_back +#define MAXV 200010 + +using namespace std; + +typedef vector vi; +typedef pair ii; +typedef long long int64; + +int64 posicao[MAXV]; + +void create(vector& v, int n) +{ + v.assign(n + 1, 0); +} + +int rsq(vector& v, int a, int b) +{ + if (a == 0) { + int sum = 0; + for (; b >= 0; b = (b & (b + 1)) - 1) { + sum += v[b]; + } + return sum; + } else { + return rsq(v, 0, b) - rsq(v, 0, a - 1); + } +} + +void adjust(vector& t, int k, int value) +{ + for (; k <= t.size(); k |= k + 1) { + t[k] += value; + } +} + +int main() +{ + ios::sync_with_stdio(false); + int m, n; + cin >> m; + while (m--) { + int64 a, sum = 0; + cin >> n; + vector ft; + create(ft, n + 1); + map numeros; + for (int i = 0; i < n; i++) { + cin >> posicao[i]; + numeros.insert(mp(posicao[i], 0)); + } + + map::iterator it; + int k = 0; + for (it = numeros.begin(); it != numeros.end(); it++) + it->second = k++; + + for (int i = 0; i < n; ++i) { + int resp = numeros[posicao[i]]; + adjust(ft, resp, 1); + //count how many guys were added in that range + sum += abs(rsq(ft, 0, resp) - rsq(ft, 0, n)); + } + cout << sum << "\n"; + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/INVCNT/INVCNT-14.cpp b/spoj_cpp_clean/INVCNT/INVCNT-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..708b710e836c3bb1c6e5e3beee6ea46657c84d0a --- /dev/null +++ b/spoj_cpp_clean/INVCNT/INVCNT-14.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + +#define siz 200009 +#define ll int + +vector tree[3 * siz]; +ll ara[siz]; +long long cnt = 0; + +void build(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + tree[node].push_back( ara[lo] ); + return; + } + + ll mid = (lo + hi) / 2; + + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + + merge(tree[2 * node].begin(), tree[2 * node].end(), + tree[2 * node + 1].begin(), tree[2 * node + 1].end(), + back_inserter(tree[node]) ); + + for(ll i = 0; i < tree[2 * node + 1].size(); i++) { + ll indx = upper_bound(tree[2 * node].begin(), tree[2 * node].end(), tree[2 * node + 1][i] ) - tree[2 * node].begin(); + //cout << indx << " " << tree[2 * node][i] << endl; + cnt += (tree[2 * node].size() - indx); + } +} + +int main() + +{ + ll t; + cin >> t; + while(t--) { + cnt = 0; + ll n, in; + scanf("%d", &n); + + for(ll i = 1; i <= n; i++) { + scanf("%d", &ara[i]); + } + + build(1, n, 1); + + printf("%lld\n", cnt); + for(ll i = 0; i < 3 * siz; i++) + tree[i].clear(); + } + return 0; +} diff --git a/spoj_cpp_clean/INVCNT/INVCNT-16.cpp b/spoj_cpp_clean/INVCNT/INVCNT-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d9a5af1b4c2e92078dbed694dd67074e34b9d35 --- /dev/null +++ b/spoj_cpp_clean/INVCNT/INVCNT-16.cpp @@ -0,0 +1,59 @@ +#include +#define ll long long +using namespace std; + +ll ans = 0; + +void merge(vector& a, int l, int mid, int r){ + int i=l, j=mid+1, k=0; + vector t(r-l+1); + while(i<=mid&&j<=r){ + if(a[i]<=a[j]){ + t[k++]=a[i++]; + } + else{ + ans+=(mid-i+1); + t[k++]=a[j++]; + } + } + while(i<=mid){ + t[k++]=a[i++]; + } + while(j<=r){ + t[k++]=a[j++]; + } + k=0; + for(int v=l;v<=r;v++){ + a[v]=t[k++]; + } +} + +void merge_sort(vector& a, int l, int r){ + if(r-l>=1){ + int mid = (l+r)/2; + merge_sort(a,l,mid); + merge_sort(a,mid+1,r); + merge(a,l,mid,r); + } +} + +void solve(){ + int n; + cin>>n; + vector a(n); + for(int i=0;i>a[i]; + merge_sort(a,0,n-1); + cout<>t; + while(t--){ + ans=0; + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/INVCNT/INVCNT-3.cpp b/spoj_cpp_clean/INVCNT/INVCNT-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3009e1bc79e31b3d84981cc6c67a34831fa33e2 --- /dev/null +++ b/spoj_cpp_clean/INVCNT/INVCNT-3.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + vector num; + long long count; + int s; + + void assignLeaf(int value){ + num.clear(); + num.push_back(value); + count = 0, s = 1; + } + void merge(node& left, node& right){ + int i = 0, j = 0; + num.clear(); + count = left.count + right.count; + while(i < left.s and j < right.s){ + if(left.num[i] > right.num[j]){ + num.push_back(right.num[j]); + count += (left.s - i); + j++; + } + else{ + num.push_back(left.num[i]); + i++; + } + } + while(i < left.s){ + num.push_back(left.num[i]); + i++; + } + + while(j < right.s){ + num.push_back(right.num[j]); + j++; + } + + s = num.size(); + } +}; + +int t, n, array[1<<18]; +node tree[1<<19]; +long long *res; + +void init(int i, int l, int r){ + if(l == r){ + tree[i].assignLeaf(array[l]); + } + else{ + init(i<<1, l, (l+r)>>1); + init((i<<1) + 1, ((l+r)>>1) + 1, r); + node left = tree[i<<1], right = tree[(i<<1)+1]; + + tree[i].merge(left, right); + } + return; +} + +int main(){ + sd(t); + res = &tree[1].count; + while(t--){ + sd(n); + for(int i = 0; i < n; i++) scanf("%d",&array[i]); + + init(1,0,n-1); + + printf("%lld\n",*res); + } + return 0; +} diff --git a/spoj_cpp_clean/INVCNT/INVCNT-4.cpp b/spoj_cpp_clean/INVCNT/INVCNT-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e73f57e8161d209adc87718f4c08b9b7b68be4c --- /dev/null +++ b/spoj_cpp_clean/INVCNT/INVCNT-4.cpp @@ -0,0 +1,145 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +long long ans=0; +int a[200009]; +void merge(int x, int y, int mid) +{ + int tmp[y-x+1]; + long long st=0, low=x, high=mid+1; + while(low<=mid && high<=y) + { + if(a[low]<=a[high]) + { + tmp[st++]=a[low]; + low++; + } + else + { + ans+=(mid-low+1); + tmp[st++]=a[high]; + high++; + } + } + if(low!=mid+1) + { + while(low<=mid) + tmp[st++]=a[low++]; + } + else if(high!=y+1) + { + while(high<=y) + tmp[st++]=a[high++]; + } + FOR(i,0,st) + { + a[x+i]=tmp[i]; + } +} + +void mergesort(int x, int y) +{ + if(x +#include +#include + +#define LL long long int +using namespace std; + +LL count_inv(LL *arr, LL *temp, LL p, LL q); +LL merge(LL *arr, LL *temp, LL p, LL mid, LL q); + +LL count_inv(LL *arr, LL *temp, LL p, LL q) +{ + LL inv=0; + if(p>num; + LL *arr=new LL[num]; + LL *temp=new LL[num]; + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 200005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +ll tree[MAX]; +int getNext (int id) { + return id + (id & -id); +} +int getPar (int id) { + return id - (id & -id); +} +void update (int id, int n, int val) { + while (id <= n) { + tree[id] += val; + id = getNext (id); + } +} +ll query (int id) { + ll sum = 0; + + while (id > 0) { + sum += tree[id]; + id = getPar (id); + } + + return sum; +} +int main () { + __FastIO; + int n, tc; + ll ar[MAX]; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> n; + setst; + mapmp; + + for (int i = 1; i <= n; i++) { + cin >> ar[i]; + st.insert (ar[i]); + } + + int k = 1; + + for (auto it = st.begin(); it != st.end(); it++) { + if (mp[*it] == 0) mp[*it] = k++; + } + + k--; + SET (tree, 0); + ll sum = 0; + + for (int i = 1; i <= n; i++) { + ll xx = query (k); + ll x = query (mp[ar[i]]); + sum += (xx - x); + update (mp[ar[i]], k, 1); + } + + cout << sum << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/INVCNT/INVCNT-9.cpp b/spoj_cpp_clean/INVCNT/INVCNT-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d412e0ede7e7107d28560ce688a7b8050699c67 --- /dev/null +++ b/spoj_cpp_clean/INVCNT/INVCNT-9.cpp @@ -0,0 +1,93 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +unsigned long long tree[200007]; +int A[400007]; +unordered_map map, reverse_map; +int N; + +inline void update(int pos, int val) +{ + for( ; pos <= 200006; pos += pos & (-pos)) + tree[pos] += val; +} + +inline unsigned long long query(int pos) +{ + unsigned long long sum = 0; + + for( ; pos > 0; pos -= pos & (-pos)) + sum += tree[pos]; + + return sum; +} + +void init() +{ + int i, counter; + int temp[200007]; + + memset(tree, 0, sizeof(tree)); + + scanf("%d", &N); + + for(i=0; i= 0; i--) + { + update(map[A[i]] + 1, 1); + sum += query(map[A[i]]); + } + + printf("%llu\n", sum); + } + + return 0; +} diff --git a/spoj_cpp_clean/IOIPALIN/IOIPALIN-21.cpp b/spoj_cpp_clean/IOIPALIN/IOIPALIN-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3a1c30d621a035cd5b1ebb663c6ce3eb4f3421b --- /dev/null +++ b/spoj_cpp_clean/IOIPALIN/IOIPALIN-21.cpp @@ -0,0 +1,39 @@ +/* + dynamic programming > longest common subsequence (LCS) + difficulty: easy + date: 26/Mar/2020 + problem: given a string, determine the minimal quantity of characters to be inserted into it in order to obtain a palindrome + hint: compute length of string minus lcs between string and reversed string + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +string A, B; +int N; + +int memo[5050][5050]; +int lcs(int i, int j) { + // i-ésimo posição em A, j-ésima posição em B + + if (i == N || j == N) return 0; + + int &ans = memo[i][j]; + if (ans != -1) return ans; + + if (A[i] == B[j]) + return ans = 1+lcs(i+1, j+1); + + return ans = max(lcs(i+1, j), lcs(i, j+1)); +} + +int main() { + cin >> N >> A; + B = A; reverse(B.begin(), B.end()); + + memset(memo, -1, sizeof memo); + cout << N-lcs(0, 0) << endl; + return 0; +} diff --git a/spoj_cpp_clean/IOIPALIN/IOIPALIN-3.cpp b/spoj_cpp_clean/IOIPALIN/IOIPALIN-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2cde7b76e328960b96b3e7ad71421e1d226c771 --- /dev/null +++ b/spoj_cpp_clean/IOIPALIN/IOIPALIN-3.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; +int main(){ + string s1; + int dp[2][5001], l, indx; + sd(l); + cin>>s1; + for(int i = 0; i <= l; i++){ + dp[0][i] = 0; + } + indx = 0; + for(int i = 1; i <= l; i++){ + indx = 1 - indx; + for(int j = 1; j <= l; j++){ + if(s1[i-1] == s1[l-j]){ + dp[indx][j] = dp[1-indx][j-1] + 1; + } + else{ + dp[indx][j] = max(dp[1-indx][j], dp[indx][j-1]); + } + } + } + + + + cout< +//#include +//#include +//#include +//#include + +using namespace std; +//using namespace __gnu_pbds; + +//typedef tree, rb_tree_tag, tree_order_statistics_node_update> ordered_set; +#define int long long +typedef pair pii; +typedef vector vi; +typedef vector > vvi; +typedef vector > vvpi; +typedef vector vpii; +#define forn(i,s,e) for(int i = s; i < e; i++) +#define trace(it, cont) for(auto it=cont.begin(); it!=cont.end(); ++it) +#define endl "\n" +#define dbg(x) cout<<#x<<" = "<>n>>m; + adj.resize(n+1); + int root=1; + for(int i=0; i>x>>y; + adj[x].push_back(y); + adj[y].pb(x); + root = x; + } + //for checking if the given graph is connected or not + // and if there is any self loops present or not + bool ans = dfs(root,-1); + if(ans){ + cout<<"YES"< +#include + +using namespace std; + +int main() +{ + string input,temp1,temp2,temp3; + int len,i,j,k; + while(cin>>input) + { + len=input.length(); + i=0; + while(i96 && input[i]<123) + { + + } + else + { + cout<<"Error!"; + break; + } + } + } + return 0; +} diff --git a/spoj_cpp_clean/JEDNAKOS/JEDNAKOS-9.cpp b/spoj_cpp_clean/JEDNAKOS/JEDNAKOS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fb91dc2e965e2c80bf199f5711449a3684cd2ef --- /dev/null +++ b/spoj_cpp_clean/JEDNAKOS/JEDNAKOS-9.cpp @@ -0,0 +1,115 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#include +#define INF 100000 + +using namespace std; + + +int memo[6001][5000]; +string A, S; +int sum; + +void init() +{ + char c; + int counter=0; + + //c=fgetc(stdin); + c=getchar_unlocked(); + + while(c=='0') + c=getchar_unlocked(); + + while(c!='=') + { + if(c=='0') + counter++; + A.push_back(c); + //c=fgetc(stdin); + if(counter==3) + { + while(c=='0') + c=getchar_unlocked(); + ungetc(c, stdin); + counter=0; + } + c=getchar_unlocked(); + } + + //c=fgetc(stdin); + c=getchar_unlocked(); + + while(isdigit(c)) + { + S.push_back(c); + //c=fgetc(stdin); + c=getchar_unlocked(); + } + + sum=atoi(S.c_str()); + +} + +int solve(int sum, int pos) +{ + int i; + int mmin=INF; + char c; + int curr=-1; + + if(memo[sum][pos]!=0) + return memo[sum][pos]; + + if(pos>A.length()) + return INF; + + if(atoi(A.c_str()+pos)==sum) + { + memo[sum][pos]=1; + return 1; + } + + for(i=pos+1; isum) + break; + + + mmin=min(mmin, solve(sum-curr, i)); + } + + memo[sum][pos]=1+mmin; + return 1+mmin; +} + +int main() +{ + //freopen("input.txt","r",stdin); + init(); + + printf("%d\n", solve(atoi(S.c_str()), 0)-1); + + + return 0; +} diff --git a/spoj_cpp_clean/JOKER1/JOKER1-3.cpp b/spoj_cpp_clean/JOKER1/JOKER1-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45e9d5a1dbd38b40892734c79c1b2e3efcc63f53 --- /dev/null +++ b/spoj_cpp_clean/JOKER1/JOKER1-3.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main() +{ + int t, a[100],n,flag; + long long result, MOD = 1000000007; + sd(t); + while(t--){ + sd(n); + for(int i=0;i=0;i--){ + if(a[i]-i<=0){ + flag=0; + break; + } + result=(result*(a[i]-i))%MOD; + } + if(!flag) + printf("0\n"); + else + printf("%lld\n",result); + } + printf("KILL BATMAN\n"); + return 0; +} diff --git a/spoj_cpp_clean/JOKER1/JOKER1-9.cpp b/spoj_cpp_clean/JOKER1/JOKER1-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c95cee99fd57531ef95dffbae34beefa2ee22913 --- /dev/null +++ b/spoj_cpp_clean/JOKER1/JOKER1-9.cpp @@ -0,0 +1,67 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define MOD 1000000007 +using namespace std; + +int A[100]; +int N; + +inline void init() +{ + int i; + + scanf("%d", &N); + + for (i = 0; i < N; i++) + scanf("%d", &A[i]); + + sort(A, A + N); +} + +inline long long solve() +{ + long long sum = 0; + int i; + + sum = A[0]; + + for (i = 1; i < N; i++) + { + A[i]-= i; + sum *= A[i]; + sum %= MOD; + } + + return sum; +} + +int main() +{ + int T; + + //freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%lld\n", solve()); + } + + printf("KILL BATMAN\n"); + + return 0; +} diff --git a/spoj_cpp_clean/JOSWAP/JOSWAP-9.cpp b/spoj_cpp_clean/JOSWAP/JOSWAP-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb402df1e7fd98dd14331126c872260145086e62 --- /dev/null +++ b/spoj_cpp_clean/JOSWAP/JOSWAP-9.cpp @@ -0,0 +1,74 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define ll long long +#define MAXN 100007 +using namespace std; + +const ll MOD = 10e9 + 7; + +int N; +ll A[MAXN]; +bool flag; + +inline void init() +{ + int i, num; + + cin >> N; + + memset(A, 0, sizeof(A)); + flag = 0; + + for (i = 0; i < N; i++) + { + cin >> num; + A[num]++; + + if (A[num] > 1) + flag = 1; + } +} + +inline ll solve() +{ + int i; + ll rval = 0; + + for (i = 1; i < MAXN; i++) + if (A[i]) + rval += A[i] * (N - A[i]); + + rval /= 2; + + return rval + flag; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + int T; + + cin >> T; + + while (T--) + { + init(); + cout << solve() << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/JRIDE/JRIDE-3.cpp b/spoj_cpp_clean/JRIDE/JRIDE-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79112a102112ae32f5f5857c8df2258f70c81201 --- /dev/null +++ b/spoj_cpp_clean/JRIDE/JRIDE-3.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int b, i = 1, s, routes[100000], start, end, max, niceness, t1, t2, length, length_temp; + sd(b); + while(i <= b){ + sd(s); + s--; + for(int j = 0; j < s; j++){ + sd(routes[j]); + } + niceness = 0, max = 0, start = 0, end = 0, t1 = 1, t2 = 2, length = 0, length_temp = 0; + for(int j = 0; j < s; j ++){ + niceness += routes[j]; + if(niceness > max){ + max = niceness; + t2 = j + 2; + start = t1; + end = t2; + length_temp++; + length = length_temp; + } + else if(niceness == max and t2-t1 > length and niceness > 0){ + t2 = j + 2; + end = t2; + start = t1; + length_temp++; + length = length_temp; + } + else if(niceness >= 0){ + t2++; + length_temp++; + } + else{ + niceness = 0; + t1 = j + 2; + t2 = j + 3; + length_temp = 0; + } + } + if(length == 0){ + printf("Route %d has no nice parts\n",i); + } + else{ + printf("The nicest part of route %d is between stops %d and %d\n",i,start,end); + } + i++; + } + return 0; +} diff --git a/spoj_cpp_clean/JULKA/JULKA-19.cpp b/spoj_cpp_clean/JULKA/JULKA-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f2ec73951cc670d84f48e47d48d05d0a0c51808 --- /dev/null +++ b/spoj_cpp_clean/JULKA/JULKA-19.cpp @@ -0,0 +1,148 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 +#define PI 3.1415926 + +using namespace std; + +vector sub(vector x, vector y) { + // Assuming x >= y + reverse(x.begin(), x.end()); + reverse(y.begin(), y.end()); + + vector ans; + int len = y.size(); + for(int i = 0; i < len; i++) { + if (x[i] < y[i]) { // If we have 1 - 2 ==> 10 + 1 - 2 + x[i+1]--; // Borrow from next 10's place + ans.push_back(10+x[i]-y[i]); + } else + ans.push_back(x[i]-y[i]); // Simply subtract and push the result + } + + if (x.size() > len) { // If there are any digits left in x, just add them to ans + for(int i = len; i < x.size(); i++) { + // x[i] can be zero if we had borrowed 1 from x[i] to x[i-1] + if(x[i] < 0) { + x[i+1]--; + ans.push_back(10+x[i]); + } + else{ + ans.push_back(x[i]); + } + } + } + reverse(ans.begin(), ans.end()); + vector arr; + int i = 0; + while (ans[i] == 0){ i++;} // Omit the leading zeros and return result + for(; i < ans.size(); i++){ + arr.push_back(ans[i]); + } + return arr; +} + +vector add(vector x, vector y) { + // Reverse the numbers so that we can add them from units place + reverse(x.begin(), x.end()); + reverse(y.begin(), y.end()); + + vector ans; // Our final answer + int len = min(x.size(), y.size()), carry = 0; + for(int i = 0; i < len; i++) { + // Add the digits at the i_th pos from x and y and add the prev carry + int val = x[i] + y[i] + carry; + carry = val/10; + ans.push_back(val%10); // Push the sum of digits to ans + } + + if (x.size() > len) { // If any digits remain in x, add them to ans + for(int i = len; i < x.size(); i++) { + int val = x[i] + carry; + carry = val/10; + ans.push_back(val%10); + } + } + + if (y.size() > len) { // If any digits remain in y, add them to ans + for(int i = len; i < y.size(); i++) { + int val = y[i] + carry; + carry = val/10; + ans.push_back(val%10); + } + } + + // While there is still some carry left-over, add new digits to ans + while (carry) { + ans.push_back(carry%10); + carry /= 10; + } + + // Reverse ans to get back actual answer and return. + reverse(ans.begin(), ans.end()); + return ans; +} + +vector divBy2(vector x){ + vector ans; + int val = 0; // val stores any previous remainders from division + for(int i = 0; i < x.size(); i++){ + if(val){ + // If there are remainders, then add them in current division + // eg : 38/2 ==> 3/2 = 1, rem = 1 ==> val = 1*10 + 8 = 18 ==> 18/2 + val = val*10 + x[i]; + ans.push_back(val/2); + val = val % 2; + } + else{ // No remainders from prev division + if(x[i]<2){ // If digit < 2, add 0 to result and put the digit as rem + val += x[i]; + ans.push_back(0); + } else{ // Just divide! + val = x[i]%2; + ans.push_back(x[i]/2); + } + } + } + vector arr; + int i = 0; + while (ans[i] == 0) i++; // Omit leading zeros and return ans + for (; i < ans.size(); i++){ + arr.push_back(ans[i]); + } + return arr; +} + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + string s1, s2; + for(int z=0; z<10; z++) + { + cin>>s1>>s2; + vector S, D, sum, div, diff; + for(int i = 0; i < s1.length(); i++){ + S.push_back(s1[i]-'0'); + } + for(int i = 0; i < s2.length(); i++){ + D.push_back(s2[i]-'0'); + } + sum = add(S, D); + div = divBy2(sum); + for(int i = 0; i < div.size(); i++){ + cout << div[i]; + } + cout< + +using namespace std; + +int main() +{ + int a,b,x,y; + int i=10; + while(i--) + { + cin>>a>>b; + x=(a+b)/2; + y=a-x; + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +#define gc() getchar() +using namespace std; + +int N, M; +char board[1024][1024]; +int dist[1024][1024]; +bool V[1024][1024]; +int dx[] = { 0, 0, 1, -1 }; +int dy[] = { 1, -1, 0, 0 }; + +inline void init() +{ + int i, j; + + memset(V, 1, sizeof(V)); + + scanf("%d %d\n", &N, &M); + + for (i = 1; i <= N; i++) + { + for (j = 1; j <= M; j++) + { + board[i][j] = gc(); + V[i][j] = 0; + dist[i][j] = INT_MAX; + } + gc(); + } +} + + +inline int solve() +{ + int i, x, y; + deque< pair > > Q; + pair > curr; + + Q.push_back(make_pair(0, make_pair(1, 1))); + dist[1][1] = 0; + + while (!Q.empty()) + { + curr = Q.front(); + Q.pop_front(); + x = curr.second.first; + y = curr.second.second; + V[x][y] = 1; + if (x == N && y == M) + return dist[N][M]; + + for (i = 0; i < 4; i++) + if (!V[x + dx[i]][y + dy[i]]) + { + if (board[x][y] == board[x + dx[i]][y + dy[i]] && dist[x + dx[i]][y + dy[i]] > curr.first) + { + dist[x + dx[i]][y + dy[i]] = curr.first; + Q.push_front(make_pair(curr.first, make_pair(x + dx[i], y + dy[i]))); + } + else if (board[x][y] != board[x + dx[i]][y + dy[i]] && dist[x + dx[i]][y + dy[i]] > curr.first + 1) + { + dist[x + dx[i]][y + dy[i]] = curr.first + 1; + Q.push_back(make_pair(curr.first + 1, make_pair(x + dx[i], y + dy[i]))); + } + } + } + + return -1; +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/KDOMINO/KDOMINO-14.cpp b/spoj_cpp_clean/KDOMINO/KDOMINO-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2f8d0ccc0b72014cce0d9ae03d70233d10261fe --- /dev/null +++ b/spoj_cpp_clean/KDOMINO/KDOMINO-14.cpp @@ -0,0 +1,114 @@ +#include +using namespace std; + +#define block 1000 +#define ll long long +#define ad 200002 + +ll ara[200009], answer = 0, answer2; +bool ans[200009]; +unordered_map cnt; +ll cntcnt[ad + 200009]; + +struct query { + ll l, r, k, i; +} qry[200009]; + +bool compare(query a, query b) + +{ + if(a.l / block != b.l / block) { + return a.l / block < b.l / block; + } + + return a.r < b.r; +} + +void del(ll indx) + +{ + ll tmp = cnt[ ara[indx] ]--; + cntcnt[ad + tmp]--; + cntcnt[ad + tmp - 1]++; + + if(cntcnt[ad + answer] == 0) + answer--; +} + +void add(ll indx) + +{ + ll tmp = cnt[ ara[indx] ]++; + cntcnt[ad + tmp ]--; + cntcnt[ad + tmp + 1 ]++; + answer = max(answer, tmp + 1); +} + +int main() + +{ + + ll n, q; + scanf("%lld %lld", &n, &q); + answer = 0; + + for(ll i = 1; i <= n; i++) + scanf("%lld", &ara[i]); + + for(ll i = 0; i < q; i++) + { + scanf("%lld %lld %lld", &qry[i].l, &qry[i].r, &qry[i].k); + qry[i].i = i; + } + + sort(qry, qry + q, compare); + + ll curl = 1, curr = 1; + for(ll i = 0; i < q; i++) + { + + while(curl < qry[i].l) + { + del(curl); + curl++; + } + + while(curl > qry[i].l) + { + add(curl - 1); + curl--; + } + + while(curr <= qry[i].r) + { + add(curr); + curr++; + } + + while(curr > qry[i].r + 1) + { + del(curr - 1); + curr--; + } + + //cout << i << " " << endl; + + answer2 = answer * qry[i].k; + ll len = qry[i].r - qry[i].l + 1; + if(answer2 >= len) + ans[ qry[i].i ] = 1; + else + ans[ qry[i].i ] = 0; + } + + for(ll i = 0; i < q; i++) { + if(ans[i] != 1) + printf("NO\n"); + else + printf("YES\n"); + } + + return 0; +} + + diff --git a/spoj_cpp_clean/KGSS/KGSS-12.cpp b/spoj_cpp_clean/KGSS/KGSS-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dad54be2ae6829602a3937ad64fb371541595e89 --- /dev/null +++ b/spoj_cpp_clean/KGSS/KGSS-12.cpp @@ -0,0 +1,89 @@ +#include +#define MAXN 100010 +#define mp make_pair + +using namespace std; + +typedef pair segment; + +int n; +segment tree[4 * MAXN]; +int elem[MAXN]; + +/* get highest and second highest values */ +segment merge(segment l, segment h) +{ + int vet[] = { l.first, l.second, h.first, h.second }; + sort(vet, vet + 4); + return mp(vet[3], vet[2]); +} + +void build(int node, int tl, int tr) +{ + if (tl == tr) + tree[node] = mp(elem[tl], 0); + else { + int tm = (tl + tr) / 2; + build(2 * node, tl, tm); + build(2 * node + 1, tm + 1, tr); + tree[node] = merge(tree[2 * node], tree[2 * node + 1]); + } +} + +/* get the sum of the interval +int sum (int node, int tl, int tr, int left, int right) +{ + if (left > right) return 0; + if (left == tl && right == tr) return tree[node]; + int tm = (tl + tr) / 2; + return sum (2*node, tl, tm, left, min(right, tm)) + + sum (2*node+1, tm+1, tr, max(left, tm+1), right); +} +*/ + +segment query(int node, int tl, int tr, int left, int right) +{ + if (left > right) + return mp(0, 0); + if (left == tl && right == tr) + return tree[node]; + int tm = (tl + tr) / 2; + + return merge(query(2 * node, tl, tm, left, min(right, tm)), + query(2 * node + 1, tm + 1, tr, max(left, tm + 1), right)); +} + +void update(int node, int tl, int tr, int pos, int new_val) +{ + if (tl == tr) + tree[node].first = new_val; + else { + int tm = (tl + tr) / 2; + if (pos <= tm) + update(2 * node, tl, tm, pos, new_val); + else + update(2 * node + 1, tm + 1, tr, pos, new_val); + tree[node] = merge(tree[2 * node], tree[2 * node + 1]); + } +} + +int main() +{ + int n, m, a, b; + char opc; + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d", &elem[i]); + build(1, 0, n - 1); + scanf("%d", &m); + for (int i = 0; i < m; i++) { + scanf(" %c %d %d", &opc, &a, &b); + if (opc == 'Q') { + segment resp = query(1, 0, n - 1, a - 1, b - 1); + printf("%d\n", resp.first + resp.second); + } else { + update(1, 0, n - 1, a - 1, b); + } + } + return 0; +} diff --git a/spoj_cpp_clean/KGSS/KGSS-14.cpp b/spoj_cpp_clean/KGSS/KGSS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16ea4498be811e217a54b05bf46b8ad5987546c7 --- /dev/null +++ b/spoj_cpp_clean/KGSS/KGSS-14.cpp @@ -0,0 +1,111 @@ +#include +using namespace std; + +#define ll long long +#define pii pair +#define mx first +#define mx2 second + +ll ara[100009]; +pii tree[300009]; + +void build(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + tree[node].mx = ara[lo]; + tree[node].mx2 = -1; + return; + } + + ll mid = (lo + hi) / 2; + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + + tree[node].mx = max(tree[2 * node].mx, tree[2 * node + 1].mx); + ll tmp = max(tree[2 * node].mx2, tree[2 * node + 1].mx2); + if(tree[2 * node].mx > tree[2 * node + 1].mx) + tmp = max(tree[2 * node + 1].mx, tmp); + else + tmp = max(tree[2 * node].mx, tmp); + + tree[node].mx2 = tmp; +} + +void update(ll lo, ll hi, ll i, ll val, ll node) + +{ + if(lo > i || hi < i) + return; + if(lo == hi) { + tree[node].mx = val; + tree[node].mx2 = -1; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, i, val, 2 * node); + update(mid + 1, hi, i, val, 2 * node + 1); + + tree[node].mx = max(tree[2 * node].mx, tree[2 * node + 1].mx); + ll tmp = max(tree[2 * node].mx2, tree[2 * node + 1].mx2); + if(tree[2 * node].mx > tree[2 * node + 1].mx) + tmp = max(tree[2 * node + 1].mx, tmp); + else + tmp = max(tree[2 * node].mx, tmp); + + tree[node].mx2 = tmp; +} + +pii query(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lo > right || hi < left) + return make_pair(-1e18, -1e18); + if(lo >= left && hi <= right) + return tree[node]; + + ll mid = (lo + hi) / 2; + pii p1 = query(lo, mid, left, right, 2 * node); + pii p2 = query(mid + 1, hi, left, right, 2 * node + 1); + + pii ret; + ret.mx = max(p1.mx, p2.mx); + ll tmp = max(p1.mx2, p2.mx2); + if(p1.mx > p2.mx) + tmp = max(p2.mx, tmp); + else + tmp = max(p1.mx, tmp); + + ret.mx2 = tmp; + return ret; +} + +int main() + +{ + ll n; + cin >> n; + for(ll i = 1; i <= n; i++) + scanf("%lld", &ara[i]); + + build(1, n, 1); + ll q; + cin >> q; + for(ll i = 1; i <= q; i++) { + char ch; + ll x, y; + scanf(" %c %lld %lld", &ch, &x, &y); + + if(ch == 'U') { + update(1, n, x, y, 1); + } + else { + pii ans = query(1, n, x, y, 1); + printf("%lld\n", ans.mx + ans.mx2); + } + + } + + return 0; +} diff --git a/spoj_cpp_clean/KGSS/KGSS-3.cpp b/spoj_cpp_clean/KGSS/KGSS-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..365d035107ee401c90d18de05d81e635eebd1432 --- /dev/null +++ b/spoj_cpp_clean/KGSS/KGSS-3.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + int maxNum, secondMaxNum; + void assignLeaf(int num){ + maxNum = num; + secondMaxNum = -1; + } + + void merge(node& left, node& right) { + maxNum = max(left.maxNum, right.maxNum); + secondMaxNum = min(max(left.maxNum, right.secondMaxNum), max(right.maxNum, left.secondMaxNum)); + } + + int getval(){ + return maxNum + secondMaxNum; + } +}; + +int n, q, x, y, array[100005]; +char c, d; +node tree[300001]; + +// l and r represent the range node i is responsible for. A single call init(1, 0, n-1) builds the segment tree in O(N) time +void init(int i, int l, int r){ + if(l == r){ + tree[i].assignLeaf(array[l]); + return; + } + else{ + init(2*i, l, (l+r)/2); + init((2*i) + 1, ((l+r)/2) + 1, r); + node left = tree[2*i], right = tree[(2*i)+1]; + + tree[i].merge(left, right); //Merge the children into the parent + } + return; +} + +// O(log N) +node query(int i, int l, int r, int lo, int hi){ + if(l == lo and r == hi) //If the range we want to query is the exact range + return tree[i]; + int mid = (l+r)/2; + if(lo > mid) //If only the subtree rooted at right child is repsonsible for this range + return query((2*i)+1, mid+1, r, lo, hi); + if(hi <= mid) //If only the subtree rooted at the left child is responsible for this range + return query(2*i, l, mid, lo, hi); + //If both, the left and right children are partially responsible for the range + node left = query(2*i, l, mid, lo, mid); + node right = query((2*i)+1, mid+1, r, mid+1, hi); + node result; + + result.merge(left, right); //Merge the results of the children into the parent + return result; +} + +//Call using update(1, 0, N-1, index, value) for a point update in O(log N) +void update(int i, int lo, int hi, int index, int value){ + if (lo == hi){ + tree[i].assignLeaf(value); + return; + } + int left = 2*i, right = left+1, mid = (lo+hi)/2; + if(index <= mid) + update(left, lo, mid, index, value); + else + update(right, mid+1, hi, index, value); + tree[i].merge(tree[left], tree[right]); + return; +} + +int main(){ + sd(n); + for(int i = 0; i < n; i++) + sd(array[i]); + init(1, 0, n-1); + sd(q); + while(q--){ + scanf(" %c",&c); + scanf("%d %d",&x,&y); + //cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +pair tr[4 * MAX]; +int ar[MAX]; +pii combine (pii a, pii b) { + pii tmp; + + if (a.first > b.first) { + tmp.first = a.first; + tmp.second = max3 (a.second, b.first, b.second); + + } else { + tmp.first = b.first; + tmp.second = max3 (a.first, a.second, b.second); + } + + return tmp; +} + +void build (int nod, int low, int high) { + if (low == high) { + tr[nod] = {ar[low], -1}; + return; + + } else { + int mid = (low + high) / 2; + build (nod * 2, low, mid); + build (nod * 2 + 1, mid + 1, high); + tr[nod] = combine (tr[nod * 2], tr[nod * 2 + 1]); + } +} + +pair query (int nod, int low, int high, int qlow, int qhigh) { + if (low > high || high < qlow || low > qhigh) { + return make_pair (-1, -1); + } + + if (low >= qlow && qhigh >= high) { + return tr[nod]; + } + + int mid = (low + high) / 2; + return combine (query (nod * 2, low, mid, qlow, qhigh ), + query (nod * 2 + 1, mid + 1, high, qlow, qhigh) ); +} + +void update (int nod, int low, int high, int pos, int new_val) { + if (low == high) { + tr[nod] = {new_val, -1}; + + } else { + int mid = (low + high) / 2; + + if (pos <= mid) { + update (nod * 2, low, mid, pos, new_val); + + } else { + update (nod * 2 + 1, mid + 1, high, pos, new_val); + } + + tr[nod] = combine (tr[nod * 2], tr[nod * 2 + 1]); + } +} + +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int n, q; + cin >> n; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + build (1, 0, n - 1); + cin >> q; + + while (q--) { + char ch; + int l, r; + cin >> ch >> l >> r; + + if (ch == 'U') { + update (1, 0, n - 1, l - 1, r); + + } else { + pii tmp = query (1, 0, n - 1, l - 1, r - 1); + cout << tmp.first + tmp.second << "\n"; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/KGSS/KGSS-9.cpp b/spoj_cpp_clean/KGSS/KGSS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3942c903aa98cb64488576aaf81f76dba36133bf --- /dev/null +++ b/spoj_cpp_clean/KGSS/KGSS-9.cpp @@ -0,0 +1,145 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#define max(a,b) (a>b?a:b) +#define gc() getchar_unlocked() + +int tree_max[100007*4]; +int tree_best[100007*4]; +int A[100007]; +int N; + +typedef struct +{ + int best; + int max; +}node; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +inline char FAST_CHAR() +{ + char ch; + + ch=gc(); + + while(isspace(ch)) + ch=gc(); + + return ch; +} + +void init_tree(int index, int a, int b) +{ + if(a == b) + { + tree_max[index] = tree_best[index] = A[a]; + return; + } + + init_tree(index*2, a, (a+b)/2); + init_tree(index*2+1, (a+b)/2+1, b); + + tree_max[index] = max(tree_max[index*2], tree_max[index*2+1]); + tree_best[index] = max(tree_max[index*2] + tree_max[index*2+1], max(tree_best[index*2], tree_best[index*2+1])); +} + +void update(int index, int a, int b, int pos, int val) +{ + if(a == b && a == pos) + { + tree_max[index] = tree_best[index] = val; + return; + } + + if(pos <= (a+b)/2) + update(index*2, a, (a+b)/2, pos, val); + else + update(index*2+1, (a+b)/2+1, b, pos, val); + + tree_max[index] = max(tree_max[index*2], tree_max[index*2+1]); + tree_best[index] = max(tree_max[index*2] + tree_max[index*2+1], max(tree_best[index*2], tree_best[index*2+1])); +} + +node query(int index, int a, int b, int i, int j) +{ + node left, right, curr; + + if(a == i && b == j) + { + curr.best=tree_best[index]; + curr.max=tree_max[index]; + return curr; + } + + if(j <= (a+b)/2) + return query(index*2, a, (a+b)/2, i, j); + else if(i > (a+b)/2) + return query(index*2+1, (a+b)/2+1, b, i, j); + + left = query(index*2, a, (a+b)/2, i, (a+b)/2); + right = query(index*2+1, (a+b)/2+1, b, (a+b)/2+1, j); + + curr.max = max(left.max, right.max); + curr.best = max(left.max + right.max, max(left.best, right.best)); + + return curr; +} + + +int main() +{ + int M, i; + int f, s; + char type; + + //freopen("input.txt","r",stdin); + + N=FAST_IO(); + + for(i=1; i<=N; i++) + A[i]=FAST_IO(); + + init_tree(1, 1, N); + + M=FAST_IO(); + for(i=0; i 0-1 knapsack + difficulty: easy + date: 22/Oct/2019 + by: @brpapa +*/ +#include +#include +#define INF (1 << 30) +using namespace std; + +int S, N; // capacidade, qte de itens +int w[2020], v[2020]; // peso do item i, valor do item i + +int memo[2020][2020]; +int dp(int i, int s) { + // item i, mochila com capacidade s ainda disponível (carregando S-s) + + if (s < 0) return -INF; + if (i == N) return 0; + + int &ans = memo[i][s]; + if (ans != -1) return ans; + + return ans = max( + dp(i+1, s), + v[i] + dp(i+1, s-w[i]) + ); +} + +int main() { + cin >> S >> N; + for (int i = 0; i < N; i++) cin >> w[i] >> v[i]; + + memset(memo, -1, sizeof(memo)); + cout << dp(0, S); + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/KNAPSACK/KNAPSACK-3.cpp b/spoj_cpp_clean/KNAPSACK/KNAPSACK-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e17e45302018b3c85a3b7fd49d88cbf0f63cf0d2 --- /dev/null +++ b/spoj_cpp_clean/KNAPSACK/KNAPSACK-3.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int s, n, size[2001], value[2001]; + int dp[2][2001], maxi, indx; + sd2(s,n); + for(int i = 1; i <= n; i++){ + sd2(size[i],value[i]); + } + memset(dp, 0, sizeof(dp)); + maxi = 0, indx = 0; + for(int i = 1; i <= n; i++){ + indx = 1 - indx; + for(int j = 1; j <= s; j++){ + if(size[i] <= j) + dp[indx][j] = max(dp[1-indx][j-size[i]] + value[i], dp[1-indx][j]); + else + dp[indx][j] = dp[1-indx][j]; + if(dp[indx][j] > maxi) + maxi = dp[indx][j]; + } + } + printf("%d\n",maxi); + return 0; +} diff --git a/spoj_cpp_clean/KNMOVE/KNMOVE-9.cpp b/spoj_cpp_clean/KNMOVE/KNMOVE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1ebbdf4810c653913f29b630ac0fc307b5cbe7e --- /dev/null +++ b/spoj_cpp_clean/KNMOVE/KNMOVE-9.cpp @@ -0,0 +1,71 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int moves[1007][1007]; +int dx[] = { -2, -2, 2, 2, 1, 1, -1, -1 }; +int dy[] = { -1, 1, -1, 1, -2, 2, -2, 2 }; + +inline bool check(int x, int y) +{ + if (x < 1 || y < 1 || x > 1000 || y > 1000 || moves[x][y] != -1) + return 0; + + return 1; +} + +inline void init() +{ + int i; + deque < pair > > Q; + pair > curr; + + memset(moves, -1, sizeof(moves)); + moves[1][1] = 0; + + + Q.push_back(make_pair(0, make_pair(1, 1))); + + while (!Q.empty()) + { + curr = Q.front(); + Q.pop_front(); + + for (i = 0; i < 8; i++) + if (check(curr.second.first + dx[i], curr.second.second + dy[i])) + { + moves[curr.second.first + dx[i]][curr.second.second + dy[i]] = curr.first + 1; + Q.push_back(make_pair(curr.first + 1, make_pair(curr.second.first + dx[i], curr.second.second + dy[i]))); + } + } +} + +int main() +{ + int T, x, y; + + init(); + + scanf("%d", &T); + + while (T--) + { + scanf("%d %d", &x, &y); + printf("%d\n", moves[x][y]); + } + + return 0; +} diff --git a/spoj_cpp_clean/KOICOST/KOICOST-14.cpp b/spoj_cpp_clean/KOICOST/KOICOST-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d8b38b69232b4b8a768a189a08349e76a9f4657 --- /dev/null +++ b/spoj_cpp_clean/KOICOST/KOICOST-14.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +#define ll long long + +struct edge { + ll u, v, c; + edge(ll _u = 0, ll _v = 0, ll _c = 0) { + u = _u; + v = _v; + c = _c; + } +}; + +vector e; + +bool cmp(edge a, edge b) + +{ + return a.c < b.c; +} + +ll par[100009], sz[100009]; + +ll findr(ll u) + +{ + if(par[u] == u) + return u; + return par[u] = findr(par[u]); +} + +int main() + +{ + ll n, m; + cin >> n >> m; + + for(ll i = 1; i <= n; i++) { + par[i] = i; + sz[i] = 1; + } + + for(ll i = 1; i <= m; i++) { + ll u, v, c; + scanf("%lld %lld %lld", &u, &v, &c); + e.push_back( edge(u, v, c) ); + } + + sort(e.begin(), e.end(), cmp); + + for(ll i = 1; i < e.size(); i++) + e[i].c += e[i - 1].c; + + ll ans = 0; + for(ll i = e.size() - 1; i >= 0; i--) { + ll u = findr(e[i].u); + ll v = findr(e[i].v); + + if(u == v) + continue; + + ans += ( (e[i].c * sz[u] * sz[v]) % 1000000000 ); + ans %= 1000000000; + sz[u] += sz[v]; + par[v] = par[u]; + } + + cout << ans << endl; + + return 0; +} diff --git a/spoj_cpp_clean/KOPC12B/KOPC12B-3.cpp b/spoj_cpp_clean/KOPC12B/KOPC12B-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c2438e5aa36bc727aa6736234accbaba6f119b2 --- /dev/null +++ b/spoj_cpp_clean/KOPC12B/KOPC12B-3.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; +int t, n; +long long dp[2000002], MOD = 1000000007, result, a; +long long inv(long long x){ + long long ans = 1, p = x, n = 1000000005; + while(n){ + if(n&1) ans = (ans*p)%MOD; + p = (p*p)%MOD; + n >>= 1; + } + return ans; +} + +int main(){ + dp[0] = 1; + for(long long i = 1; i <= 2000001; i++) + dp[i] = (dp[i-1]*i) % MOD; + sd(t); + a = inv(2); + while(t--){ + scanf("%d", &n); + result = ((((inv(dp[n])*inv(dp[n]))%MOD))*a)%MOD; + result = (result*dp[n+n])%MOD; + printf("%lld\n", ((result*n)%MOD)); + } + return 0; +} + + diff --git a/spoj_cpp_clean/KOZE/KOZE-7.cpp b/spoj_cpp_clean/KOZE/KOZE-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..133c49dd83bae4ef7ab401cfb5998119a30dba2b --- /dev/null +++ b/spoj_cpp_clean/KOZE/KOZE-7.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int movx[]={0,0,1,-1}; +int movy[]={-1,1,0,0}; +int n,m; +char grafo[300][300]; +int visit[300][300]; +int ovejas,lobos; + +bool valido(int a, int b) +{ + return ( (a>=0 && a=0 && blobos) + { + lobos=0; + }else + { + ovejas=0; + } + anso+= ovejas; + ansl+= lobos; + } + } + } + + printf("%d %d\n",anso,ansl); + + return 0; +} diff --git a/spoj_cpp_clean/KQUERY/KQUERY-3.cpp b/spoj_cpp_clean/KQUERY/KQUERY-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e63b656d9adc21d4421fd1f0c1ca8018c1b958a --- /dev/null +++ b/spoj_cpp_clean/KQUERY/KQUERY-3.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + int val, l, r, idx; + bool t; +}; + +bool operator<(const node& a, const node& b ){ + if ( a.val == b.val ){ + return a.t < b.t; + } + return a.val < b.val; +} +int N, Q, bit[30001], i, ans[230000], r; +node req[230000]; + +int get(int indx){ + int sum = 0; + while (indx > 0){ + sum += bit[indx]; + indx -= ( indx & -indx ); + } + return sum; +} +void update(int indx){ + while (indx <= 30000){ + bit[indx] += 1; + indx += (indx & -indx); + } + return; +} +int main(){ + sd(N); + for ( i = 0; i < N; ++i ){ + sd(req[ i ].val); + req[i].idx = i + 1; + req[i].t = 0; + } + sd(Q); + for ( i = N; i < N + Q; ++i ){ + sd3(req[i].l,req[i].r,req[i].val); + req[i].idx = i - N; + req[i].t = 1; + } + + sort( req, req + N + Q ); + + for ( i = 0; i < N + Q; ++i ){ + if (req[i].t){ + r = get(req[i].r) - get(req[i].l - 1); + ans[req[i].idx] = (req[i].r - req[i].l + 1 ) - r; + } + else{ + update(req[i].idx); + } + } + for ( i = 0; i < Q; ++i ) { + printf( "%d\n", ans[i]); + } + return 0; +} diff --git a/spoj_cpp_clean/KQUERY2/KQUERY2-14.cpp b/spoj_cpp_clean/KQUERY2/KQUERY2-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13e01fcb4ecb0982d7dcbbbf9157317af95f8a23 --- /dev/null +++ b/spoj_cpp_clean/KQUERY2/KQUERY2-14.cpp @@ -0,0 +1,124 @@ +// #pragma GCC optimize("Ofast,unroll-loops") +// #pragma GCC target("avx,avx2,fma") + +#include +using namespace std; + +#define ll int +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define all(v) v.begin(), v.end() +#define inf (1LL << 61) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; ++i) +#define for0(i, stop) for(ll i = 0; i < stop; ++i) +#define rep1(i, start) for(ll i = start; i >= 1; --i) +#define rep0(i, start) for(ll i = (start-1); i >= 0; --i) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define EL '\n' +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +const ll sz = 3e4+10, nsz = 1e4+5, block = sqrtl(sz*14); +ll ara[sz], tree[4*nsz][sz/block + 5]; + +void update(ll lo, ll hi, ll idx, ll v, ll b, ll node) +{ + if(lo > idx || hi < idx) + return; + if(lo == hi) { + tree[node][b] += v; + return; + } + + ll mid = lo+hi>>1, lft = node<<1, rgt = lft|1; + update(lo, mid, idx, v, b, lft); + update(mid+1, hi, idx, v, b, rgt); + + tree[node][b] = tree[lft][b] + tree[rgt][b]; +} + +ll query(ll lo, ll hi, ll l, ll r, ll b, ll node) +{ + if(lo > r || hi < l) + return 0; + if(lo >= l && hi <= r) + return tree[node][b]; + + ll mid = lo+hi >> 1; + return query(lo, mid, l, r, b, node<<1) + + query(mid+1, hi, l, r, b, node<<1|1); +} + +ll query_block(ll l, ll r, ll k) +{ + ll lb = l / block, rb = r / block, ret = 0; + + if(lb == rb) { + for(ll i = l; i <= r; i++) + ret += (ara[i] > k); + + return ret; + } + + for(ll i = l; i < (lb+1)*block; i++) + ret += (ara[i] > k); + + for(ll i = lb+1; i < rb; i++) + ret += query(1, nsz, k+1, nsz, i, 1); + + for(ll i = rb*block; i <= r; i++) + ret += (ara[i] > k); + + return ret; +} + +int main() +{ + ll n; + cin >> n; + for0(i, n) { + si(ara[i]); + + ll b = i / block; + update(1, nsz, ara[i], 1, b, 1); + } + + ll q; + cin >> q; + while(q--) { + + ll op, l, r, k; + si(op); + + if(op == 1) { + scanf("%d %d %d", &l, &r, &k); + l--, r--; + + pf("%d\n", query_block(l, r, k)); + } + else { + scanf("%d %d", &l, &k); + l--; + + ll prv = ara[l]; + ara[l] = k; + + update(1, nsz, prv, -1, l/block, 1); + update(1, nsz, ara[l], 1, l/block, 1); + } + } + + return 0; +} + diff --git a/spoj_cpp_clean/KQUERYO/KQUERYO-14.cpp b/spoj_cpp_clean/KQUERYO/KQUERYO-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f559d416354087e95c6692f3ab58b02bfa7d4d7 --- /dev/null +++ b/spoj_cpp_clean/KQUERYO/KQUERYO-14.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +#define ll long long +#define block 720 + +vector v[block]; +ll ara[100009]; + +ll query(ll l, ll r, ll c) + +{ + ll lb = l / block, rb = r / block; + + ll cnt = 0; + if(lb == rb) { + for(ll i = l; i <= r; i++) + if(ara[i] > c) + cnt++; + + return cnt; + } + + for(ll i = l; i < (lb + 1) * block; i++) + if(ara[i] > c) + cnt++; + for(ll i = rb * block; i <= r; i++) + if(ara[i] > c) + cnt++; + + for(ll i = lb + 1; i < rb; i++) { + ll pos = upper_bound(v[i].begin(), v[i].end(), c) - v[i].begin(); + cnt += (v[i].size() - pos); + } + + return cnt; +} + +int main() + +{ + ll n; + cin >> n; + for(ll i = 0; i < n; i++) { + ll in; + scanf("%lld", &in); + v[i / block].push_back(in); + ara[i] = in; + } + for(ll i = 0; i < block; i++) + sort(v[i].begin(), v[i].end()); + + ll q, lastans = 0; + cin >> q; + while(q--) { + ll a, b, c; + scanf("%lld %lld %lld", &a, &b, &c); + a ^= lastans, b ^= lastans, c^= lastans; + if(a < 1) a = 1; if(b > n) b = n; + printf("%lld\n", lastans = a > b? 0: query(a - 1, b - 1, c)); + } + + return 0; +} diff --git a/spoj_cpp_clean/LABYR1/LABYR1-12.cpp b/spoj_cpp_clean/LABYR1/LABYR1-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b732a9809d3b5837162c4bfc6f5f38d7d808f269 --- /dev/null +++ b/spoj_cpp_clean/LABYR1/LABYR1-12.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#define mp make_pair + +using namespace std; + +typedef pair ii; + +const int INF = 0x3f3f3f3f; + +int main() +{ + ios::sync_with_stdio(false); + int t, m, n; + cin >> t; + while (t--) { + cin >> m >> n; + char c; + int k = 0, l = 0; + int adj[n][m]; + bool visitado[n][m]; + queue> bfs, bfss; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + cin >> c; + if (c == '.') { + adj[i][j] = 0; + if (bfs.empty()) + bfs.push(mp(0, mp(i, j))); + } else + adj[i][j] = INF; + visitado[i][j] = false; + } + } + int MAX = 0; + while (!bfs.empty()) { + int a = bfs.front().first; + int q = bfs.front().second.first; + int w = bfs.front().second.second; + if (MAX < a) { + MAX = a; + k = q; + l = w; + } + visitado[q][w] = true; + bfs.pop(); + if (q + 1 < n && adj[q + 1][w] == 0 && !visitado[q + 1][w]) { + bfs.push(mp(a + 1, mp(q + 1, w))); + visitado[q + 1][w] = true; + } + if (q - 1 >= 0 && adj[q - 1][w] == 0 && !visitado[q - 1][w]) { + bfs.push(mp(a + 1, mp(q - 1, w))); + visitado[q - 1][w] = true; + } + if (w + 1 < m && adj[q][w + 1] == 0 && !visitado[q][w + 1]) { + bfs.push(mp(a + 1, mp(q, w + 1))); + visitado[q][w + 1] = true; + } + if (w - 1 >= 0 && adj[q][w - 1] == 0 && !visitado[q][w - 1]) { + bfs.push(mp(a + 1, mp(q, w - 1))); + visitado[q][w - 1] = true; + } + } + + memset(visitado, false, sizeof(visitado)); + bfs.push(mp(0, mp(k, l))); + MAX = 0; + + while (!bfs.empty()) { + int a = bfs.front().first; + int q = bfs.front().second.first; + int w = bfs.front().second.second; + if (MAX < a) + MAX = a; + visitado[q][w] = true; + bfs.pop(); + if (q + 1 < n && adj[q + 1][w] == 0 && !visitado[q + 1][w]) { + bfs.push(mp(a + 1, mp(q + 1, w))); + visitado[q + 1][w] = true; + } + if (q - 1 >= 0 && adj[q - 1][w] == 0 && !visitado[q - 1][w]) { + bfs.push(mp(a + 1, mp(q - 1, w))); + visitado[q - 1][w] = true; + } + if (w + 1 < m && adj[q][w + 1] == 0 && !visitado[q][w + 1]) { + bfs.push(mp(a + 1, mp(q, w + 1))); + visitado[q][w + 1] = true; + } + if (w - 1 >= 0 && adj[q][w - 1] == 0 && !visitado[q][w - 1]) { + bfs.push(mp(a + 1, mp(q, w - 1))); + visitado[q][w - 1] = true; + } + } + + cout << "Maximum rope length is " << MAX << ".\n"; + } +} diff --git a/spoj_cpp_clean/LABYR1/LABYR1-21.cpp b/spoj_cpp_clean/LABYR1/LABYR1-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b30522055e76634c0901b290b9f998b319ce0d2c --- /dev/null +++ b/spoj_cpp_clean/LABYR1/LABYR1-21.cpp @@ -0,0 +1,59 @@ +/* + graphs > specials > tree + difficulty: easy + date: 27/May/2020 + problem: compute the diameter of a given implicit tree + by: @brpapa +*/ +#include +#define UNVISITED 0 +#define VISITING 1 +using namespace std; + +char grid[1001][1001]; int R, C; +int state[1001][1001]; + +bool isValid(int r, int c) { + if (r < 0 || r >= R || c < 0 || c >= C) return false; + if (grid[r][c] == '#') return false; + return true; +} + +int dr[4] = {1, 0,-1, 0}; +int dc[4] = {0, 1, 0,-1}; +int lr, lc; + +int maxLvl; +void dfs(int r, int c, int lvl) { + if (lvl > maxLvl) { maxLvl = lvl; lr = r, lc = c; } + state[r][c] = VISITING; + + for (int d = 0; d < 4; d++) { + int nr = r+dr[d]; + int nc = c+dc[d]; + if (isValid(nr, nc) && state[nr][nc] == UNVISITED) + dfs(nr, nc, lvl+1); + } +} + +int main() { + int T; cin >> T; + while (T--) { + cin >> C >> R; + int ur = -1, uc = -1; + for (int r = 0; r < R; r++) for (int c = 0; c < C; c++) { + cin >> grid[r][c]; + if (grid[r][c] == '.' && ur == -1 && uc == -1) ur = r, uc = c; + } + + maxLvl = 0; + memset(state, UNVISITED, sizeof state); + dfs(ur, uc, 0); // set (lr,lc) como o nó mais distante de (ur, uc) + + maxLvl = 0; + memset(state, UNVISITED, sizeof state); + dfs(lr, lc, 0); + printf("Maximum rope length is %d.\n", maxLvl); + } + return 0; +} diff --git a/spoj_cpp_clean/LABYR1/LABYR1-4.cpp b/spoj_cpp_clean/LABYR1/LABYR1-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db12d2b4f05353e86662ca2179c2f65212c71353 --- /dev/null +++ b/spoj_cpp_clean/LABYR1/LABYR1-4.cpp @@ -0,0 +1,163 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int visit[1111][1111]; +string s[1111]; +int tmpx, tmpy; +int ans=0; +int xr[4]={1, -1, 0, 0}; +int yr[4]={0, 0, 1, -1}; + +void DFS(int x, int y, int cnt) +{ + visit[x][y]=1; + if(cnt>ans) + ans=cnt, tmpx=x, tmpy=y; + FOR(i,0,4) + { + int xx=x+xr[i], yy=y+yr[i]; + + if(xx>=0 && yy>=0 && xx>s[i]; + ans=0; + int res=0; + FOR(i,0,n) + { + FOR(j,0,m) + { + if(s[i][j]!='#' && visit[i][j]==0) + { + + DFS(i,j,0); + memset(visit, 0, sizeof(visit)); + DFS(tmpx, tmpy,0); + if(res +#define MAXINT 20000000 +#define MAXSIZE 1100 + +using namespace std; +int t,n,m; +char grafo[1100][1100]; +int dist[1100][1100]; + +int movx[]= {0,1,-1,0}; +int movy[]= {1,0,0,-1}; + +bool valida(int a, int b){ + return ((a=0) && (b=0))?true:false; +} + +pair bfs(int sx,int sy) +{ + queue< pair >q; + + pair maxi; + dist[sx][sy]=0; + q.push(make_pair(sx,sy)); + + while(!q.empty()) + { + pair u= q.front(); q.pop(); + for (int i = 0; i < 4; ++i) + { + int nuevoX= u.first+ movx[i]; + int nuevoy= u.second+ movy[i]; + if (valida(nuevoX,nuevoy) && grafo[nuevoX][nuevoy]=='.' && dist[u.first][u.second] + 1 < dist[nuevoX][nuevoy]) + { + dist[nuevoX][nuevoy]= dist[u.first][u.second] + 1; + maxi= make_pair(nuevoX,nuevoy); + q.push(maxi); + } + } + + } + + return maxi; +} + +void inicializar(int x, int visit[][MAXSIZE]) +{ + for (int i = 0; i < MAXSIZE; ++i) + { + for (int j = 0; j < MAXSIZE; ++j) + { + visit[i][j]=x; + } + } +} + +int main() +{ + scanf("%d",&t); + while(t--) + { + scanf("%d %d",&n,&m); + for (int i = 0; i < m; ++i) + { + scanf("%s",grafo[i]); + } + + bool algo= false; + pair a; + for (int i = 0; i < m; ++i) + { + for (int j = 0; j < n; ++j) + { + if (grafo[i][j]=='.') + { + a= make_pair(i,j); + algo= true; + break; + } + } + if (algo) + { + break; + } + } + inicializar(MAXINT,dist); + pair aux= bfs(a.first,a.second); + inicializar(MAXINT,dist); + pair aux2= bfs(aux.first,aux.second); + + int res= 0; + res= dist[aux2.first][aux2.second]; + + printf("Maximum rope length is %d.\n",res ); + } + + + return 0; +} diff --git a/spoj_cpp_clean/LARSUBP/LARSUBP-3.cpp b/spoj_cpp_clean/LARSUBP/LARSUBP-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d2b027c799891c0cbc71848976c1ac22ee1725f --- /dev/null +++ b/spoj_cpp_clean/LARSUBP/LARSUBP-3.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, temp, cas = 1; + char s[10001]; + long long MOD = 1000000007, ends_with[10], sum; + sd(t); + while(cas <= t){ + scanf("%s",s); + sum = 0; + + for(int i = 0; i < 10; i++) + ends_with[i] = 0; + + int i = 0; + while(s[i] != '\0'){ + temp = s[i] - '0'; + ends_with[temp] = (ends_with[temp] + 1) % MOD; + for(int j = 0; j < temp; j++){ + ends_with[temp] += ends_with[j] % MOD; + } + i++; + } + + sum += (ends_with[0] + ends_with[1] + ends_with[2] + ends_with[3] + ends_with[4] + ends_with[5] + ends_with[6] + ends_with[7] + ends_with[8] + ends_with[9])%MOD; + + printf("Case %d: %lld\n",cas,sum); + cas++; + } + return 0; +} diff --git a/spoj_cpp_clean/LASTDIG/LASTDIG-11.cpp b/spoj_cpp_clean/LASTDIG/LASTDIG-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e45c3dc6ecc33c5da63dd6b9b3cd4184e762d91 --- /dev/null +++ b/spoj_cpp_clean/LASTDIG/LASTDIG-11.cpp @@ -0,0 +1,38 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include +#include + +using namespace std; + +int main(void) +{ + int t; + int a, res; + unsigned long long b; + + cin >> t; + + while(t--) + { + cin >> a >> b; + + res = 1; + + while(b) + { + if(b & 1) + res = (res * a) % 10; + + b >>= 1; + a = (a * a) % 10; + } + + cout << res << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/LASTDIG/LASTDIG-16.cpp b/spoj_cpp_clean/LASTDIG/LASTDIG-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02de34270796cab7fe4cef9ba89235782bec0e4e --- /dev/null +++ b/spoj_cpp_clean/LASTDIG/LASTDIG-16.cpp @@ -0,0 +1,34 @@ +#include +#define ll long long +using namespace std; +void solve(){ + ll a,b; + cin>>a>>b; + a%=10; + if(b==0){ + cout<<1<<"\n"; + return; + } + b%=4; + if(b==0){ + cout<<(ll)pow(a,4)%10<<"\n"; + } + else if(b==1){ + cout<<(ll)pow(a,1)%10<<"\n"; + } + else if(b==2){ + cout<<(ll)pow(a,2)%10<<"\n"; + } + else{ + cout<<(ll)pow(a,3)%10<<"\n"; + } + return; +} +int main(){ + int test_cases=1; + cin>>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/LASTDIG/LASTDIG-19.cpp b/spoj_cpp_clean/LASTDIG/LASTDIG-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f624b5a993554701ac406c805e253c6c802eb868 --- /dev/null +++ b/spoj_cpp_clean/LASTDIG/LASTDIG-19.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +int main() +{ + int t;cin>>t;while(t--){ + long long b; + int a,temp,curr,ans=1; + cin>>curr>>b; + a=curr%10; + if(b==0){ + ans=1; + goto ne; + } + else if(a==2 || a==3 || a==8 || a==7){ + temp=b%4; + if(temp==0){temp=4;} + } + else if(a==4 || a==9){ + temp=b%2; + if(temp==0) + temp=2; + } + else{ans=a;} + for(int i=1;i<=temp;i++){ans*=curr;} + ans=ans%10; + ne: + cout< +#include + +using namespace std; + +int main() +{ + long int test,a,b,i,j,unit,rem,cycle[4]; + cin>>test; + while(test--) + { + cin>>a>>b; + a=a%10; + if(a==0) + { + cout<<"0"< +using namespace std; +typedef long long ll; +/************************************ Code Start Here ******************************************************/ + +ll bigMod (ll a, ll b, ll MOD) { + ll res = 1; + + while (b) { + if (b & 1) { + res = (res * a) % MOD; + } + + a = (a * a) % MOD; + b >>= 1; + } + + return res; +} + +int main () { + ios_base::sync_with_stdio (false); cin.tie (0); cout.tie (0); + int tc; + cin >> tc; + + while (tc--) { + ll a, b; + cin >> a >> b; + cout << bigMod (a, b, 10) << '\n'; + } + + return 0; +} diff --git a/spoj_cpp_clean/LASTDIG/LASTDIG-9.cpp b/spoj_cpp_clean/LASTDIG/LASTDIG-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec01323fc94e997311b38a74aa67ef477001f4df --- /dev/null +++ b/spoj_cpp_clean/LASTDIG/LASTDIG-9.cpp @@ -0,0 +1,43 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +int modular_pow(int base, int exponent, int modulus) +{ + int res=1; + base = base%modulus; + while(exponent > 0) + { + if(exponent % 2) + res = (res*base) % modulus; + exponent >>= 1; + base = (base*base) % modulus; + } + + return res; +} + +int main() +{ + int N, i, b, e; + + //freopen("input.txt","r",stdin); + scanf("%d", &N); + + for(i=0; i +#include +#include + +using namespace std; + +int main() +{ +long long test,b,i,j,unit,rem,cycle[4]; +char a[2000]; +cin>>test; +while(test--) +{ + cin>>a>>b; + int temp=(a[(strlen(a))-1]-48); + if(temp==0) + { + cout<<"0"< + +using namespace std; + +int main() +{ + int test,a,b,i,j,ans; + cin>>test; + while(test--) + { + cin>>a>>b; + ans=1; + while(b>0) + { + if(b%2==1) + { + ans=(ans*a)%10; + b--; + } + b=b/2; + a=(a*a)%10; + } + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define MAXN 10007 +using namespace std; + +vector < vector > G(MAXN); +bool V[MAXN]; +int N, M; + +inline void init() +{ + int i, f, s; + + cin >> N >> M; + + for (i = 0; i < M; i++) + { + cin >> f >> s; + G[f].push_back(s); + } + +} + +int dfs(int curr) +{ + int i; + int rval = 1; + + for (i = 0; i < G[curr].size(); i++) + if (!V[G[curr][i]]) + { + V[G[curr][i]] = 1; + rval += dfs(G[curr][i]); + } + + return rval; +} + +inline int solve() +{ + int rval = 0; + int i; + + for (i = 1; i <= N; i++) + { + memset(V, 0, sizeof(V)); + V[i] = 1; + rval = max(rval, dfs(i)); + } + + return rval; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + init(); + cout << solve() << "\n"; + + return 0; +} diff --git a/spoj_cpp_clean/LATGACH3/LATGACH3-3.cpp b/spoj_cpp_clean/LATGACH3/LATGACH3-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d94fbaaace1468d09c6954ac89847fc92015920 --- /dev/null +++ b/spoj_cpp_clean/LATGACH3/LATGACH3-3.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main() +{ + int ans[31], n; + ans[0]=1; + ans[1]=0; + ans[2]=3; + ans[3]=0; + ans[4]=11; + ans[5]=0; + ans[6]=41; + ans[7]=0; + ans[8]=153; + ans[9]=0; + ans[10]=571; + ans[11]=0; + ans[12]=2131; + ans[13]=0; + ans[14]=7953; + ans[15]=0; + ans[16]=29681; + ans[17]=0; + ans[18]=110771; + ans[19]=0; + ans[20]=413403; + ans[21]=0; + ans[22]=1542841; + ans[23]=0; + ans[24]=5757961; + ans[25]=0; + ans[26]=21489003; + ans[27]=0; + ans[28]=80198051; + ans[29]=0; + ans[30]=299303201; + + for(int i = 4; i < 31; i++) + ans[i] = ans[i-2] * 4 - ans[i-4]; + sd(n); + //for(int i = 4; i < 31; i++) + // cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 1005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +int level[MAX], par[MAX]; +int table[MAX][11]; + +void dfs (int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + level[v] = level[u] + 1; + par[v] = u; + dfs (v, u); + } +} + +void LCA (int n) { + SET (table, -1); + + for (int i = 1; i <= n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 1; i <= n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + } + } + } +} + +int LCA_Quary (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} +int main () { + __FastIO; + int tc, n; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> n; + + for (int i = 1; i <= n; i++) { + int x, v; + cin >> x; + + for (int j = 0; j < x; j++) { + cin >> v; + adj[i].pb (v); + } + } + + SET (level, 0); + SET (par, 0); + par[1] = -1; + dfs (1, -1); + LCA (n); + int Q; + cin >> Q; + cout << "Case " << t << ":\n"; + + for (int q = 1; q <= Q; q++) { + int u, v; + cin >> u >> v; + cout << LCA_Quary (u, v) << "\n"; + } + + for (int i = 0; i < MAX; i++) adj[i].clear(); + } + + return 0; +} diff --git a/spoj_cpp_clean/LCA/LCA-9.cpp b/spoj_cpp_clean/LCA/LCA-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38a9b454bd3f8cd349d3e0c24cb843725a34ed03 --- /dev/null +++ b/spoj_cpp_clean/LCA/LCA-9.cpp @@ -0,0 +1,218 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +class ufs +{ + private: + struct set + { + int name; + int rank; + set *parent; + }; + int ssize; + vector S; + + set *fs(int name) + { + if(S[name].parent->name==name) + return &S[name]; + + S[name].parent=fs(S[name].parent->name); + return S[name].parent; + } + + + public: + ufs(int n) + { + int i; + ssize=n; + S.resize(ssize); + + for(i=0; iname; + } + + bool is_same_set(int a, int b) + { + return fs(a)->name==fs(b)->name; + } + + void union_sets(int a, int b) + { + set *f, *s; + + f=fs(a); + s=fs(b); + + if(f->name==s->name) + return; + + ssize--; + if(f->rank==s->rank) + { + f->rank++; + s->parent=f; + } + + else if(f->rank > s->rank) + s->parent=f; + + else + f->parent=s; + } + + int size() + { + return ssize; + } +}; + +vector< vector > T; +vector ancestor; +vector color; //0-white, 1-black +vector< vector > LCA; +vector has_parent; +int N, Q; +int root; +ufs S(1); + +void TarjanOLCA(int u) +{ + int i, v; + + ancestor[u]=u; + + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +int level[MAX], par[MAX]; +int table[MAX][20]; + +void dfs (int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + level[v] = level[u] + 1; + par[v] = u; + dfs (v, u); + } +} + +void LCA (int n) { + SET (table, -1); + + for (int i = 0; i < n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 0; i < n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + } + } + } +} + +int LCA_Quary (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} +int main () { + //~ __FastIO; + int n; + cin >> n; + + for (int i = 0; i < n; i++) { + int x, v; + cin >> x; + + for (int j = 1; j <= x; j++) { + cin >> v; + adj[i].pb (v); + } + } + + par[0] = -1; + dfs (0, -1); + LCA (n); + int Q; + cin >> Q; + + for (int q = 1; q <= Q ; q++) { + int u, v; + cin >> u >> v; + cout << LCA_Quary (u, v) << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/LCMSUM/LCMSUM-12.cpp b/spoj_cpp_clean/LCMSUM/LCMSUM-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e24fe88bec64dcda81012602c263972235f6aed --- /dev/null +++ b/spoj_cpp_clean/LCMSUM/LCMSUM-12.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#define pb push_back +#define mp make_pair +#define MAX 1000000 + +using namespace std; + +typedef pair prime_pot; +typedef unsigned long long int64; + +int64 resp[MAX], sum[MAX]; + +void cal_sum() +{ + for (int i = 1; i <= MAX; i++) + sum[i] = i; + for (int i = 2; i <= MAX; i++) + if (sum[i] == i) + for (int j = 2 * i; j <= MAX; j += i) + sum[j] -= sum[j] / i; + for (int i = 2; i <= MAX; i++) + if (sum[i] == i) + sum[i] = i - 1; + for (int i = 1; i <= MAX; i++) + for (int j = i; j <= MAX; j += i) + resp[j] += i * sum[i]; +} + +int main() +{ + cal_sum(); + int64 a, n, d; + scanf("%llu", &n); + while (n--) { + scanf("%llu", &a); + printf("%llu\n", (1 + resp[a]) * a / 2); + } + return 0; +} diff --git a/spoj_cpp_clean/LCMSUM/LCMSUM-3.cpp b/spoj_cpp_clean/LCMSUM/LCMSUM-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d51bb66bc1f7d2674190e6b119e373a29c108596 --- /dev/null +++ b/spoj_cpp_clean/LCMSUM/LCMSUM-3.cpp @@ -0,0 +1,70 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it) +#define meta __FUNCTION__,__LINE__ + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +using namespace std; + +const long double PI = 3.1415926535897932384626433832795; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void tr(S x, const Strings&... rest){cout< pii; + +const int N = 1001000; + +int p[N], phi[N]; +ll ans[N]; + +int main(){ + for(int i = 1; i < N; i++) phi[i] = i; + for(int i = 2; i < N; i++) p[i] = 1; + + for(int i = 2; i < N; i++){ + if(!p[i]) continue; + for(int j = i; j < N; j += i){ + p[j] = 0; + phi[j] -= phi[j] / i; + } + } + + for(int i = 1; i < N; i++){ + for(int j = i; j < N; j += i){ + ans[j] += 1ll * i * phi[i]; + } + } + + for(int i = 1; i < N; i++){ + ans[i] /= 2; + ans[i] *= i; + } + + int t; sd(t); + while(t--){ + int x; sd(x); + printf("%lld\n", ans[x] + x); + } + + return 0; +} diff --git a/spoj_cpp_clean/LCPC12F/LCPC12F-9.cpp b/spoj_cpp_clean/LCPC12F/LCPC12F-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..295cd8349122786441b8b174b9923b28f69b35c9 --- /dev/null +++ b/spoj_cpp_clean/LCPC12F/LCPC12F-9.cpp @@ -0,0 +1,51 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int N, X; +long long sum; +long long DP[4096]; + +inline void init() +{ + scanf("%d %d", &X, &N); + sum = 0; + memset(DP, 0, sizeof(DP)); +} + +int main() +{ + int T, i, j, num; + + scanf("%d", &T); + + for(i = 1; i <= T; i++) + { + init(); + + for(j = 0; j < N; j++) + { + scanf("%d", &num); + if(X < num) + continue; + sum += DP[X-num]; + DP[num]++; + } + + printf("%d. %lld\n", i, sum); + } + + return 0; +} diff --git a/spoj_cpp_clean/LCS/LCS-14.cpp b/spoj_cpp_clean/LCS/LCS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..136f2c5dc8165230e6d8bb643c9f88ae17aabb86 --- /dev/null +++ b/spoj_cpp_clean/LCS/LCS-14.cpp @@ -0,0 +1,225 @@ +#include +#define MAX_N 1000020 +using namespace std; +#define ll int + +int n, t; +char s[500099]; +char punc[] = {'~', '!', '@', '#', '$', '%', '^' , '&', '*', '('}; +int length[12], tree[4 * MAX_N], nxt[10][MAX_N], prv[10]; +vector id[2]; + +int SA[MAX_N], LCP[MAX_N]; + + +int RA[MAX_N], tempRA[MAX_N]; +int tempSA[MAX_N]; +int c[MAX_N]; +int Phi[MAX_N], PLCP[MAX_N]; +// second approach: O(n log n) +// the input string, up to 100K characters +// the length of input string +// rank array and temporary rank array +// suffix array and temporary suffix array +// for counting/radix sort +void countingSort(int k) { +// O(n) + int i, sum, maxi = max(300, n); +// up to 255 ASCII chars or length of n + memset(c, 0, sizeof c); +// clear frequency table + for (i = 0; i < n; i++) +// count the frequency of each integer rank + c[i + k < n ? RA[i + k] : 0]++; + for (i = sum = 0; i < maxi; i++) { + int t = c[i]; c[i] = sum; sum += t; + } + for (i = 0; i < n; i++) +// shuffle the suffix array if necessary + tempSA[c[SA[i] + k < n ? RA[SA[i] + k] : 0]++] = SA[i]; + for (i = 0; i < n; i++) +// update the suffix array SA + SA[i] = tempSA[i]; +} + + + +void buildSA() { +// this version can go up to 100000 characters + int i, k, r; + for (i = 0; i < n; i++) RA[i] = s[i]; +// initial rankings + for (i = 0; i < n; i++) SA[i] = i; +// initial SA: {0, 1, 2, ..., n-1} + for (k = 1; k < n; k <<= 1) { +// repeat sorting process log n times + countingSort(k); // actually radix sort: sort based on the second item + countingSort(0); +// then (stable) sort based on the first item + tempRA[SA[0]] = r = 0; +// re-ranking; start from rank r = 0 + for (i = 1; i < n; i++) +// compare adjacent suffixes + tempRA[SA[i]] = // if same pair => same rank r; otherwise, increase r + (RA[SA[i]] == RA[SA[i - 1]] && RA[SA[i] + k] == RA[SA[i - 1] + k]) ? r : ++r; + for (i = 0; i < n; i++) +// update the rank array RA + RA[i] = tempRA[i]; + if (RA[SA[n - 1]] == n - 1) break; +// nice optimization trick + } +} + +void buildLCP() { + int i, L; + Phi[SA[0]] = -1; +// default value + for (i = 1; i < n; i++) +// compute Phi in O(n) + Phi[SA[i]] = SA[i - 1]; +// remember which suffix is behind this suffix + for (i = L = 0; i < n; i++) { +// compute Permuted LCP in O(n) + if (Phi[i] == -1) { PLCP[i] = 0; continue; } +// special case + while (s[i + L] == s[Phi[i] + L]) L++; +// L increased max n times + PLCP[i] = L; + L = max(L - 1, 0); +// L decreased max n times + } + for (i = 0; i < n; i++) +// compute LCP in O(n) + LCP[i] = PLCP[SA[i]]; +// put the permuted LCP to the correct position +} + +// n = string length +// s = the string +// memset(LCP, 0, sizeof(LCP)); setting all index of LCP to zero +// buildSA(); for building suffix array +// buildLCP(); for building LCP array +// LCP is the longest common prefix with the next suffix here + +void build(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + tree[node] = LCP[lo]; + return; + } + + ll mid = (lo + hi) / 2; + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + + tree[node] = min(tree[2 * node], tree[2 * node + 1]); +} + +ll query(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lo > right || hi < left) + return 1e9; + if(lo >= left && hi <= right) + return tree[node]; + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, 2 * node); + ll p2 = query(mid + 1, hi, left, right, 2 * node + 1); + + return min(p1, p2); +} + +int main() + +{ + int add = 0, cnt = 0; + while(scanf("%s", s + add) != EOF) { + int len = strlen(s + add); + s[add + len] = punc[cnt]; + length[cnt] = add + len; + cnt++; + add += len + 1; + } + s[add - 1] = '\0'; + n = strlen(s); + + if(cnt == 1) { + printf("%s\n", s); + return 0; + } + + memset(LCP, 0, sizeof(LCP)); + buildSA(); + //cout << 5 << endl; + buildLCP(); + +// for (int i=0;i 1) + continue; + + ans = max(ans, query(0, n - 1, i, now, 1)); + //cout << i << " " << now << " " << LCP[22] << " " << LCP[23] << endl; + } + + cout << ans << endl; + + return 0; diff --git a/spoj_cpp_clean/LCS0/LCS0-14.cpp b/spoj_cpp_clean/LCS0/LCS0-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..938c8ecb5ab8c4661cfb504b66b49f137bb1bb0e --- /dev/null +++ b/spoj_cpp_clean/LCS0/LCS0-14.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +#define ll register unsigned short int +const int siz = 50002; + +char y[50009], x[50009]; +bitset M[30], M2[30], L[siz], num1, num2, carry; +int m; +bool chk = 0; + +bitset operator +(const bitset &a, const bitset &b) + +{ + num1 = a, num2 = b, chk = 0; + while(num2 != 0) { + carry = num1 & num2; + num1 ^= num2; + num2 = (carry << 1); + + /// extra part + if(num2[m + 1] == 1) { + chk = 1; + num2[m + 1] = 0; + } + } + return num1; +} + +int main() + +{ + std::ios_base::sync_with_stdio(false); + cin.tie(NULL); + ll i, j, ans = 0; + + cin >> (y + 1) >> (x + 1); + ll n = strlen(y + 1); + m = strlen(x + 1); + + for(i = 1; i <= m; i++) + M[x[i] - 'a'][i] = 1; + + for(i = 0; i < 27; i++) { + for(j = 1; j <= m; j++) + M2[i][j] = !M[i][j]; + } + + for(i = 1; i <= m; i++) + L[0][i] = 1; + + for(j = 1; j <= n; j++) { + + L[j] = L[j - 1] + (L[j - 1] & M[y[j] - 'a']); + L[j] |= (L[j - 1] & M2[y[j] - 'a']); + ans += chk; + } + + cout << ans << endl; + + return 0; +} diff --git a/spoj_cpp_clean/LCS2/LCS2-14.cpp b/spoj_cpp_clean/LCS2/LCS2-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1d5320ea31cd46f817bc977d935a615ddf62e4b --- /dev/null +++ b/spoj_cpp_clean/LCS2/LCS2-14.cpp @@ -0,0 +1,233 @@ +#include +#define MAXN 1000020 +using namespace std; +#define ll int + +int t; +char s[MAXN]; +char punc[] = {'~', '!', '@', '#', '$', '%', '^' , '&', '*', '('}; +int col[MAXN]; + +int AN; +int A[3 * MAXN + 100]; +int cnt[MAXN + 1]; // Should be >= 256 +int SA[MAXN + 1], REVSA[MAXN + 1], LCP[MAXN + 1]; + +/* Used by suffix_array. */ +void radix_pass(int* A, int AN, int* R, int RN, int* D) { + memset(cnt, 0, sizeof(int) * (AN + 1)); + int* C = cnt + 1; + for(int i = 0; i < RN; i++) ++C[A[R[i]]]; + for(int i = -1, v = 0; i <= AN && v < RN; v += C[i++]) swap(v, C[i]); + for(int i = 0; i < RN; i++) D[C[A[R[i]]]++] = R[i]; +} + +/* DC3 in O(N) using 20N bytes of memory. Stores the suffix array of the string + * [A,A+AN) into SA where SA[i] (0<=i<=AN) gives the starting position of the + * i-th least suffix of A (including the empty suffix). + */ +void suffix_array(int* A, int AN) { + // Base case... length 1 string. + if(!AN) { + SA[0] = 0; + } else if(AN == 1) { + SA[0] = 1; SA[1] = 0; + return; + } + + // Sort all strings of length 3 starting at non-multiples of 3 into R. + int RN = 0; + int* SUBA = A + AN + 2; + int* R = SUBA + AN + 2; + for(int i = 1; i < AN; i += 3) SUBA[RN++] = i; + for(int i = 2; i < AN; i += 3) SUBA[RN++] = i; + A[AN + 1] = A[AN] = -1; + radix_pass(A + 2, AN - 2, SUBA, RN, R); + radix_pass(A + 1, AN - 1, R, RN, SUBA); + radix_pass(A + 0, AN - 0, SUBA, RN, R); + + // Compute the relabel array if we need to recursively solve for the + // non-multiples. + int resfix, resmul, v; + if(AN % 3 == 1) { + resfix = 1; resmul = RN >> 1; + } else { + resfix = 2; resmul = RN + 1 >> 1; + } + for(int i = v = 0; i < RN; i++) { + v += i && (A[R[i - 1] + 0] != A[R[i] + 0] || + A[R[i - 1] + 1] != A[R[i] + 1] || + A[R[i - 1] + 2] != A[R[i] + 2]); + SUBA[R[i] / 3 + (R[i] % 3 == resfix) * resmul] = v; + } + + // Recursively solve if needed to compute relative ranks in the final suffix + // array of all non-multiples. + if(v + 1 != RN) { + suffix_array(SUBA, RN); + SA[0] = AN; + for(int i = 1; i <= RN; i++) { + SA[i] = SA[i] < resmul ? 3 * SA[i] + (resfix==1?2:1) : + 3 * (SA[i] - resmul) + resfix; + } + } else { + SA[0] = AN; + memcpy(SA + 1, R, sizeof(int) * RN); + } + + // Compute the relative ordering of the multiples. + int NMN = RN; + for(int i = RN = 0; i <= NMN; i++) { + if(SA[i] % 3 == 1) { + SUBA[RN++] = SA[i] - 1; + } + } + radix_pass(A, AN, SUBA, RN, R); + + // Compute the reverse SA for what we know so far. + for(int i = 0; i <= NMN; i++) { + SUBA[SA[i]] = i; + } + + // Merge the orderings. + int ii = RN - 1; + int jj = NMN; + int pos; + for(pos = AN; ii >= 0; pos--) { + int i = R[ii]; + int j = SA[jj]; + int v = A[i] - A[j]; + if(!v) { + if(j % 3 == 1) { + v = SUBA[i + 1] - SUBA[j + 1]; + } else { + v = A[i + 1] - A[j + 1]; + if(!v) v = SUBA[i + 2] - SUBA[j + 2]; + } + } + SA[pos] = v < 0 ? SA[jj--] : R[ii--]; + } +} + +/* Copies the string in s into A and reduces the characters as needed. */ +void prep_string() { + int v = AN = 0; + memset(cnt, 0, 256 * sizeof(int)); + for(char* ss = s; *ss; ++ss, ++AN) cnt[*ss]++; + for(int i = 0; i < AN; i++) cnt[s[i]]++; + for(int i = 0; i < 256; i++) cnt[i] = cnt[i] ? v++ : -1; + for(int i = 0; i < AN; i++) A[i] = cnt[s[i]]; +} + +/* Computes the reverse SA index. REVSA[i] gives the index of the suffix + * starting a i in the SA array. In other words, REVSA[i] gives the number of + * suffixes before the suffix starting at i. This can be useful in itself but + * is also used for compute_lcp(). + */ +void compute_reverse_sa() { + for(int i = 0; i <= AN; i++) { + REVSA[SA[i]] = i; + } +} + +/* Computes the longest common prefix between adjacent suffixes. LCP[i] gives + * the length of the longest common prefix between the suffix starting at + * SA[i-1] and SA[i]. Runs in O(N) time. + */ +void compute_lcp() { + int len = 0; + for(int i = 0; i < AN; i++, len = max(0, len - 1)) { + int s = REVSA[i]; + int j = SA[s - 1]; + for(; i + len < AN && j + len < AN && A[i + len] == A[j + len]; len++); + LCP[s] = len; + } +} + +/* Call these inside test case: + * prep_string(); + * suffix_array(A, len); + * compute_reverse_sa(); + * compute_lcp(); +*/ + +deque > dq; +ll counter = 0, freq[MAXN]; + +inline void ad(ll &cl) + +{ + freq[cl]++; + if(freq[cl] == 1) + counter++; +} + +inline void del(ll &cl) + +{ + freq[cl]--; + if(freq[cl] == 0) + counter--; +} + +int main() + +{ + int add = 0, cnt = 0; + while(scanf("%s", s + add) != EOF) { + int len = strlen(s + add); + s[add + len] = punc[cnt]; + //length[cnt] = add + len; + cnt++; + add += len + 1; + } + s[add - 1] = '\0'; + int n = strlen(s); + + if(cnt == 1) { + printf("%s\n", s); + return 0; + } + + prep_string(); + suffix_array(A, n); + compute_reverse_sa(); + compute_lcp(); + +// for (int i=1;i<=n;i++) printf("%d ",SA[i]); +// printf("\n"); +// for (int i=1;i<=n;i++) printf("%d ",LCP[i]); +// printf("\n"); + ll i, j, k; + for(ll i = 1; i <= n; i++) { + if(!isalpha(s[i])) + col[i] = 1; + if(i != 0) + col[i] += col[i - 1]; + } + + ll ans = 0, L = 1; + for(i = 1; i <= n; i++) { + + while(dq.size() && dq[dq.size() - 1].first > LCP[i]) + dq.pop_back(); + + dq.push_back(make_pair(LCP[i], i)); + ad(col[SA[i]]); + + while(counter == cnt) { + ans = max(ans, dq[0].first); + + if(L == dq[0].second) + dq.pop_front(); + + del(col[ SA[L++] ]); + if(counter == cnt - 1) // LCP[i] holds the matching between ith and (i-1)th suffix. + ans = max(ans, dq[0].first); // So, the suffix just deleted still has an impact. + } + } + + printf("%d\n", ans); + + return 0; +} diff --git a/spoj_cpp_clean/LGLOVE/LGLOVE-14.cpp b/spoj_cpp_clean/LGLOVE/LGLOVE-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..111855dc92e020666c573a7a8b3e92f388667d6a --- /dev/null +++ b/spoj_cpp_clean/LGLOVE/LGLOVE-14.cpp @@ -0,0 +1,177 @@ +#include +using namespace std; + +#define siz 300000 +#define mod 1000000007 +#define ll long long +ll ara[100009], mtree[300009], xtree[300009], lazy[300009], mul[siz + 9], indx = 0; +vector prime; +bool mark[siz + 9]; + +void propagate(ll lo, ll hi, ll node) + +{ + if(lo == hi) + return; + + lazy[2 * node] += lazy[node]; + lazy[2 * node + 1] += lazy[node]; + + mtree[2 * node] += lazy[node]; + mtree[2 * node + 1] += lazy[node]; + + xtree[2 * node] += lazy[node]; + xtree[2 * node + 1] += lazy[node]; + lazy[node] = 0; +} + +void build(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + mtree[node] = ara[lo]; + xtree[node] = ara[lo]; + return; + } + + ll mid = (lo + hi) / 2; + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + + mtree[node] = min(mtree[2 * node], mtree[2 * node + 1]); + xtree[node] = max(xtree[2 * node], xtree[2 * node + 1]); +} + +void update(ll lo, ll hi, ll left, ll right, ll val, ll node) + +{ + if(lazy[node]) propagate(lo, hi, node); + if(lo > right || hi < left) + return; + if(lo >= left && hi <= right) { + lazy[node] += val; + mtree[node] += val; + xtree[node] += val; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, left, right, val, 2 * node); + update(mid + 1, hi, left, right, val, 2 * node + 1); + + mtree[node] = min(mtree[2 * node], mtree[2 * node + 1]); + xtree[node] = max(xtree[2 * node], xtree[2 * node + 1]); +} + +ll query1(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lazy[node]) propagate(lo, hi, node); + + if(lo > right || hi < left) + return 1e18; + if(lo >= left && hi <= right) { + return mtree[node]; + } + + ll mid = (lo + hi) / 2; + ll p1 = query1(lo, mid, left, right, 2 * node); + ll p2 = query1(mid + 1, hi, left, right, 2 * node + 1); + + return min(p1, p2); +} + +ll query2(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lazy[node]) propagate(lo, hi, node); + + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) { + return xtree[node]; + } + + ll mid = (lo + hi) / 2; + ll p1 = query2(lo, mid, left, right, 2 * node); + ll p2 = query2(mid + 1, hi, left, right, 2 * node + 1); + + return max(p1, p2); +} + +ll lcm(ll n) + +{ + ll sqt = sqrt(n); + ll gun = 1; + for(ll i = 0; prime[i] <= sqt; i++) { + ll tmp = n / prime[i]; + ll mulp = 1; + while(tmp >= prime[i]) { + mulp = (mulp * (prime[i] % mod) ) % mod; + tmp /= prime[i]; + } + + gun = (gun * mulp) % mod; + } + + //cout << gun << " ff " << endl; + + ll pos = upper_bound(prime.begin(), prime.end(), n) - prime.begin(); + pos--; + //cout << pos << " " << mul[pos] << endl; + gun = (gun * mul[pos]) % mod; + + return gun; +} + +int main() + +{ + prime.push_back(2); + mul[indx++] = 2; + for(ll i = 3; i <= siz; i += 2) { + if(!mark[i]) { + for(ll j = i * i; j <= siz; j += i + i) { + mark[j] = 1; + } + prime.push_back(i); + mul[indx] = (i * mul[indx - 1]) % mod; + indx++; + } + } + + ll n, q, op, l, r, x; + scanf("%lld %lld", &n, &q); + for(ll i = 1; i <= n; i++) { + scanf("%lld", &ara[i]); + } + + build(1, n, 1); + for(ll i = 1; i <= q; i++) { + scanf("%lld %lld %lld", &op, &l, &r); + l++, r++; + if(op == 0) { + scanf("%lld", &x); + update(1, n, l, r, x, 1); + } + else if(op == 2) { + ll num = query1(1, n, l, r, 1); + //cout << num << endl; + if(num > 1) + printf("%lld\n", lcm(num)); + else + printf("%lld\n", num); + } + else { + ll num = query2(1, n, l, r, 1); + //cout << num << endl; + if(num > 1) + printf("%lld\n", lcm(num)); + else + printf("%lld\n", num); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/LINEUP/LINEUP-21.cpp b/spoj_cpp_clean/LINEUP/LINEUP-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e79c66f00b6cc1b2ff98c96b29882ccc46e114a --- /dev/null +++ b/spoj_cpp_clean/LINEUP/LINEUP-21.cpp @@ -0,0 +1,44 @@ +/* + dynamic programming + difficulty: easy + date: 24/Oct/2020 + hint: use bitmask + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; +const int INF = 1 << 30; +const int N = 11; +int s[N][1 << N]; // s[i][j] = strength of i-th player on j-th position + +vector> memo; +int dp(int i, int mask) { + // player i, conjunto de posições já ocupadas mask + + if (i == N) return 0; + if (mask == ((1 << N) -1)) return 0; + + int &ans = memo[i][mask]; + if (ans != -1) return ans; + + ans = -INF; + for (int j = 0; j < N; j++) + if (!(mask & (1 << j)) && s[i][j] > 0) + ans = max(ans, s[i][j] + dp(i+1, mask | (1 << j))); + + return ans; +} + +int main() { + int T; cin >> T; + while (T--) { + for (int i = 0; i < 11; i++) + for (int j = 0; j < 11; j++) + cin >> s[i][j]; + + memo.assign(N, vector(1 << N, -1)); + cout << dp(0, 0) << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/LISA/LISA-13.cpp b/spoj_cpp_clean/LISA/LISA-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4dbbb96d2324bf4e2564cce879ba43a2cbd86910 --- /dev/null +++ b/spoj_cpp_clean/LISA/LISA-13.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +using namespace std; +typedef unsigned long long i64; + +char buffer[1000]; + +bool mark[200][200]; +i64 memo[200][200]; + +i64 f(string &s, bool flag, int a, int b){ + //since it is infix it can be reduced to x op y + i64 x, y; + + //if there are no operators return x + bool ok = true; + for(int i=a; i> x; + return x; + } + + //memoization + i64 &best = memo[a][b]; + if(mark[a][b])return best; + mark[a][b] = true; + + best = flag? (1 << 64) - 1 : 0LL; + //since there are one or more operators split the sentence around them + for(int i=a; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#define gc() getchar() + +using namespace std; + +int NUMBERS[100]; +bool OPERATIONS[100]; +unsigned long long DPMAX[100][100]; +unsigned long long DPMIN[100][100]; +int N; + +void init() +{ + char c; + int temp; + int i=0, j=0; + + c=gc(); + while(c!='\n' && c!=EOF) + { + if(isdigit(c)) + { + ungetc(c, stdin); + scanf("%d", &temp); + NUMBERS[i++]=temp; + } + else + { + switch(c) + { + case '+': + OPERATIONS[j++]=0; + break; + case '*': + OPERATIONS[j++]=1; + break; + } + } + c=gc(); + } + N=i; +} + +void solve() +{ + int i, j, k, L; + + for(i=0; i +using namespace std; + +#define ll long long +ll tree[400009]; +bool prop[400009]; + +void init(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + tree[node] = 0; + prop[node] = 0; + return; + } + + ll mid = (lo + hi) / 2; + init(lo, mid, 2 * node); + init(mid + 1, hi, 2 * node + 1); + + tree[node] = tree[2 * node] + tree[2 * node + 1]; + prop[node] = prop[2 * node] + prop[2 * node + 1]; +} + +void update(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lo > right || hi < left) + return; + if(lo >= left && hi <= right) { + tree[node] = hi - lo + 1 - tree[node]; + prop[node] = prop[node] ^ 1; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, left, right, 2 * node); + update(mid + 1, hi , left, right, 2 * node + 1); + + tree[node] = tree[2 * node] + tree[2 * node + 1]; + if(prop[node] == 1) + tree[node] = hi - lo + 1 - tree[node]; +} + +ll query(ll lo, ll hi, ll left, ll right, bool carry, ll node) + +{ + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) { + if(carry == 0) + return tree[node]; + else + return hi - lo + 1 - tree[node]; + } + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, carry ^ prop[node], 2 * node); + ll p2 = query(mid + 1, hi, left, right, carry ^ prop[node], 2 * node + 1); + + return p1 + p2; +} + +int main() + +{ + ll n, m; + scanf("%lld %lld", &n, &m); + init(0, n - 1, 1); + + for(ll i = 1; i <= m; i++) { + ll state, p, q; + scanf("%lld %lld %lld", &state, &p, &q); + if(state == 0) + update(0, n - 1, p - 1, q - 1, 1); + else + printf("%lld\n", query(0, n - 1, p - 1, q - 1, 0, 1)); + } + return 0; +} diff --git a/spoj_cpp_clean/LITE/LITE-9.cpp b/spoj_cpp_clean/LITE/LITE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6af026d7732baf8acb10f33b92d04daa4faddb0 --- /dev/null +++ b/spoj_cpp_clean/LITE/LITE-9.cpp @@ -0,0 +1,118 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int tree[100007*4]; +bool lazy[100007*4]; +int N, E; + +void update(int node, int a, int b, int i, int j) +{ + if(lazy[node]) + { + tree[node]=b-a+1-tree[node]; + if(a!=b) + { + lazy[node*2]=!lazy[node*2]; + lazy[node*2+1]=!lazy[node*2+1]; + } + lazy[node]=0; + } + + + if(a==i && b==j) + { + tree[node]=b-a+1-tree[node]; + if(a!=b) + { + lazy[node*2]=!lazy[node*2]; + lazy[node*2+1]=!lazy[node*2+1]; + } + return; + } + + if(j<=(a+b)/2) + update(2*node, a, (a+b)/2, i, j); + + else if(i>=(a+b)/2+1) + update(2*node+1, (a+b)/2+1, b, i, j); + + else + { + update(2*node, a, (a+b)/2, i, (a+b)/2); + update(2*node+1, (a+b)/2+1, b, (a+b)/2+1, j); + } + + tree[node]=0; + int mid = (a+b)/2; + + if(lazy[node*2]) + tree[node]+=mid-a+1-tree[node*2]; + else + tree[node]+=tree[node*2]; + + if(lazy[node*2+1]) + tree[node]+=b-(mid+1)+1-tree[node*2+1]; + else + tree[node]+=tree[node*2+1]; + return; +} + +int query(int node, int a, int b, int i, int j) +{ + if(lazy[node]) + { + tree[node]=b-a+1-tree[node]; + if(a!=b) + { + lazy[node*2]=!lazy[node*2]; + lazy[node*2+1]=!lazy[node*2+1]; + } + lazy[node]=0; + } + + + if(a==i && b==j) + return tree[node]; + + if(j<=(a+b)/2) + return query(node*2, a, (a+b)/2, i, j); + if(i>=(a+b)/2+1) + return query(node*2+1, (a+b)/2+1, b, i, j); + + return query(2*node, a, (a+b)/2, i, (a+b)/2) + query(2*node+1, (a+b)/2+1, b, (a+b)/2+1, j); +} + + + + + +int main() +{ + int i, f, s, type; + scanf("%d %d", &N, &E); + + for(i=0; i + +#define sd2(x,y) scanf("%d%d",&x,&y) + +#define fi first +#define se second +#define mp make_pair + +using namespace std; + +typedef pair pii; + +const int M = 500, INF = 1e9; + +int mx, n, k, sumv[M], sumw[M]; + +bool cmp(const pii &p1, const pii &p2){ + return (ll) p1.se * p2.fi > (ll) p1.fi * p2.se; +} + +vector t; +inline int getBound(int x, int lim){ + int cur = 0, ben = 0; + for(int i = x; i < n; i++) + if(cur + t[i].fi <= lim) cur += t[i].fi, ben += t[i].se; + else return ben + (1ll * t[i].se * (lim-cur)) / t[i].fi; + return ben; +} + +void branchAndBound(int x, int curb, int curw){ + if(x == n){ + mx = max(mx, curb); + return; + } + + if(sumw[x] + curw <= k){ + mx = max(mx, sumv[x] + curb); + return; + } + + int ub1 = (k - curw - t[x].fi >= 0)? curb + t[x].se + getBound(x+1, k - curw - t[x].fi) : -INF; + int ub2 = curb + getBound(x+1, k - curw); + + if(ub1 <= mx and ub2 <= mx) return; + + if(ub1 > ub2){ + branchAndBound(x+1, curb + t[x].se, curw + t[x].fi); + if(ub2 <= mx) return; + branchAndBound(x+1, curb, curw); + } + else{ + branchAndBound(x+1, curb, curw); + if(ub1 <= mx) return; + branchAndBound(x+1, curb + t[x].se, curw + t[x].fi); + } + return; +} + +int main(){ + sd2(k,n); + + t.resize(n); + for(int i = 0; i < n; i++) sd2(t[i].se,t[i].fi); + + sort(t.begin(), t.end(), cmp); + + for(int i = 0; i < n; i++) sumv[i] = t[i].se, sumw[i] = t[i].fi; + t.clear(); + + for(int i = n-2; i >= 0; i--) sumv[i] += sumv[i+1], sumw[i] += sumw[i+1]; + + branchAndBound(0, 0, 0); + + printf("%d", mx); + return 0; +} diff --git a/spoj_cpp_clean/LKS/LKS-4.cpp b/spoj_cpp_clean/LKS/LKS-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe9cc411ce4a66a64b3a1119973cc3bc14302e1b --- /dev/null +++ b/spoj_cpp_clean/LKS/LKS-4.cpp @@ -0,0 +1,117 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define p(n) printf("%d",n) + +using namespace std; +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define s(n) scanf("%d",&n) +#define p(n) printf("%d",n) + +int n, m , k; +double best=0.0; +struct STORE +{ + int v, w; +} data[504]; +bool comp(STORE const &a, STORE const &b) +{ + double d1=a.v, d2=a.w, d3=b.v, d4=b.w; + d1=a.v/a.w; d2=b.v/b.w; + return d1 < d2; +} +double Upperbound(int m, int k) +{ + double mx=0; + FORs(i,m,0) + { + if(k > data[i].w) + { + mx+=data[i].v; + k-=data[i].w; + } + else + { + mx+=((k*data[i].v/data[i].w)); + break; + } + } + return mx; +} +int cnt=0; +void knap(int i, double val, int size) +{ + //cnt++; + if(val > best) + best=val; + if(i<0 || size==0 ) + return ; + //error(cnt);error(val+Upperbound(i,size)); + if(size-data[i].w>=0) + if(val+Upperbound(i, size) > (double)best) + knap(i-1, val+(double)data[i].v, size-data[i].w); + if(val+Upperbound(i-1, size) > (double)best) + knap(i-1, val, size); +} +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + s(k); s(n); + int a, b; + FOR(i,0,n) + { + s(a); s(b); + //if(b<=k && a!=0) + data[i].v=a, data[i].w=b;//, pos++; + } + sort(data, data+n, comp); +/* + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#define max(a,b) ((a>b)?a:b) + +int dp[2][2000001]; +int V[501], W[501]; +int N, K; + +int main() +{ + int i, j; + + + scanf("%d %d", &K, &N); + + for(i=1; i<=N; i++) + scanf("%d %d", &V[i], &W[i]); + + for(j=1; j<=N; j++) + for(i=1; i<=K; i++) + { + if(i-W[j]>=0) + dp[j%2][i]=max(V[j]+dp[(j-1)%2][i-W[j]], dp[(j-1)%2][i]); + else + dp[j%2][i]=dp[(j-1)%2][i]; + } + + printf("%d\n", dp[N%2][K]); + + return 0; +} diff --git a/spoj_cpp_clean/LOTGAME/LOTGAME-3.cpp b/spoj_cpp_clean/LOTGAME/LOTGAME-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae8f32bb81ebd8238886e17391bd17a0c3d4d0a2 --- /dev/null +++ b/spoj_cpp_clean/LOTGAME/LOTGAME-3.cpp @@ -0,0 +1,114 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< bin(int x){ + vector ret; + while(x){ + ret.pb(x%2); + x /= 2; + } + return ret; +} + +void pad(vector &v, int l){ + while(v.size() < l) v.pb(0); + reverse(v.begin(), v.end()); +} + +void solve(){ + int a, b, k; + cin >> a >> b >> k; + + vector A = bin(a); + vector B = bin(b); + vector K = bin(k); + + int mx = max(A.size(), max(B.size(), K.size())); + pad(A, mx); + pad(B, mx); + pad(K, mx); + + int n = mx; + + memset(dp, 0, sizeof dp); + + dp[0][1][1][1] = 1; + for(int i = 0; i < n; i++){ + for(int ek = 0; ek < 2; ek++){ + for(int ea = 0; ea < 2; ea++){ + for(int eb = 0; eb < 2; eb++){ + ll val = dp[i][ek][ea][eb]; + if(!val) continue; + + int mdK = (ek)? K[i] : 1; + int mdA = (ea)? A[i] : 1; + int mdB = (eb)? B[i] : 1; + + for(int x = 0; x <= mdK; x++){ + for(int y = 0; y <= mdA; y++){ + for(int z = 0; z <= mdB; z++){ + if((y&z) != x) continue; + int neqk = (ek and x == K[i]); + int neqa = (ea and y == A[i]); + int neqb = (eb and z == B[i]); + + dp[i+1][neqk][neqa][neqb] += val; + } + } + } + } + } + } + } + + ll res = dp[n][0][0][0]; + cout << res << endl; +} + +int main(){ _ + int t; + cin >> t; + for(int i = 1; i <= t; i++){ + cout << "Case #" << i << ": "; + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/LPIS/LPIS-14.cpp b/spoj_cpp_clean/LPIS/LPIS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbfe9dc4b1a100f7fc7a02d7dd2b07fe6d6d26a3 --- /dev/null +++ b/spoj_cpp_clean/LPIS/LPIS-14.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +#define ll long long + +ll cnt[1000009]; + +int main() + +{ + ll n, ans = 0; + cin >> n; + for(ll i = 1; i <= n; i++) { + ll in; + scanf("%lld", &in); + cnt[in] = max(1LL, cnt[in - 1] + 1); + ans = max(ans, cnt[in]); + } + + cout << ans << endl; + + return 0; diff --git a/spoj_cpp_clean/LPPP/LPPP-3.cpp b/spoj_cpp_clean/LPPP/LPPP-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afa2d3d003d616f29c5b250cfaa74763e3942637 --- /dev/null +++ b/spoj_cpp_clean/LPPP/LPPP-3.cpp @@ -0,0 +1,105 @@ + #include + #include + + using namespace std; + + bool isSubsetSum (long long int arr[], long long int n, long long int sum) + { + // Base Cases + if (sum == 0) + return true; + if ((n == 0 || arr[n-1]==0) && sum!=0) + return false; + // If last element is greater than sum, then ignore it + if (arr[n-1] > sum) + return isSubsetSum (arr, n-1, sum); + /* else, check if sum can be obtained by any of the following + (a) including the last element + (b) excluding the last element + */ + else + { + if(isSubsetSum (arr, n-1, sum-arr[n-1])) + { + arr[n-1]=0; + return true; + } + else return isSubsetSum (arr, n-1, sum); + } + } + + + int main() { + long long int t,e = 0,n,k;cin>>t; + while(++e <= t) + { + cin>>n; + k = 3; + long long int a[n],sum=0; + for(long long int i=0;i>a[i]; + sum+=a[i]; + } + if(k==1) + { + cout<<"Case "<m) + { + flag=1; + break; + } + else if(a[i]==m) + { + a[i]=0; + count++; + } + } + if(flag==1) + { + cout<<"Case "< +using namespace std; + +#ifdef cicada +#include "debug.h" +#else +#define debug(...) "Yume o akiramete shinde kure." +#endif + +#define int int64_t +#define endl "\n" +typedef pair pii; + +int const INF = 2e18; +int const mod = 1e9 +7; + + +signed main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL);cout.precision(20); + + string s; + int n; + cin >> n >> s; + + int d1[n], d2[n]; + memset(d1, 0, sizeof(d1)); + memset(d2, 0, sizeof(d2)); + + // odd ones + int l = 0, r = -1, mx = 0; + for(int i=0; i r) ? 1 : min(d1[l + (r-i)], r-i); + while( i+k =0 and s[i+k] == s[i-k]) + k++; + d1[i] = k--; + if(i + k > r) + l = i-k, r = i+k; + + if(mx < d1[i]*2 -1) + mx = d1[i]*2 -1; + } + + l = 0, r = -1; + for(int i=0; i r) ? 1 : min(d2[l + (r-i) +1] , r-i +1) +1; + while( i+k-1 =0 and s[i+k-1] == s[i-k]) + k++; + d2[i] = --k; + if(i + k -1 > r) + l = i -k, r = i+k -1; + + if(mx < d2[i]*2) + mx = d2[i] *2; + } + + cout << mx << endl; + + return 0; +} diff --git a/spoj_cpp_clean/LUCIFER/LUCIFER-14.cpp b/spoj_cpp_clean/LUCIFER/LUCIFER-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04370d078c05395e64289a2be98df874b893f04e --- /dev/null +++ b/spoj_cpp_clean/LUCIFER/LUCIFER-14.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; + +#define ll long long +#define pb push_back +ll dp[2][2][12][85][85]; +vector num; +bool check[105]; + +ll solve(ll isStart, ll isSmall, ll pos, ll odd, ll even) + +{ + if(pos == 0) { + if(even - odd < 0) + return 0; + else if(check[even - odd] == 0) + return 1; + else + return 0; + } + ll &ret = dp[isStart][isSmall][pos][odd][even]; + if(ret != -1 && isSmall) + return ret; + + ll lim, pos2 = num.size() - pos; + if(isSmall) + lim = 9; + else + lim = num[pos2]; + + ll rt = 0; + if(!isStart) { + for(ll i = 0; i <= lim; i++) + rt += solve(0, isSmall | i < num[pos2], pos - 1, (pos & 1) ? odd + i: odd, !(pos & 1) ? even + i: even); + } + else { + for(ll i = 1; i <= lim; i++) + rt += solve(0, isSmall | i < num[pos2], pos - 1, (pos & 1) ? odd + i: odd, !(pos & 1) ? even + i: even); + + if(pos > 1) + rt += solve(1, 1, pos - 1, 0, 0); + } + + return ret = rt; +} + +ll calc(ll n) + +{ + if(n < 10) + return 0; + + ll tmp = n; + num.clear(); + while(tmp) { + num.pb(tmp % 10); + tmp /= 10; + } + reverse(num.begin(), num.end()); + + return solve(1, 0, num.size(), 0, 0); +} + +bool isPrime(ll n) + +{ + ll sqt = sqrt(n); + for(ll i = 2; i <= sqt; i++) + if(n % i == 0) + return 0; + + return 1; +} + +int main() + +{ + check[0] = 1; + check[1] = 1; + for(ll i = 2; i <= 100; i++) + if(!isPrime(i)) + check[i] = 1; + + ll t, caseno = 0; + memset(dp, -1, sizeof(dp)); + cin >> t; + while(t--) { + ll l, r; + scanf("%lld %lld", &l, &r); + + ll ans = calc(r); + + ans -= calc(l - 1); + + printf("%lld\n", ans); + } + + return 0; +} diff --git a/spoj_cpp_clean/LUCIFER/LUCIFER-3.cpp b/spoj_cpp_clean/LUCIFER/LUCIFER-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2144cd3c072e6581b4aebabf4f8f638d9d98531d --- /dev/null +++ b/spoj_cpp_clean/LUCIFER/LUCIFER-3.cpp @@ -0,0 +1,107 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< itov(ll x){ + if(!x) return {0}; + vector v; + while(x){ + v.pb(x%10); + x /= 10; + } + reverse(v.begin(), v.end()); + return v; +} + +vector primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43}; + +const int N = 12; + +ll dp[N][100][2]; + +ll get(vector v, bool flag){ + int n = v.size(); + + memset(dp, 0, sizeof dp); + + int off = 50; + + dp[0][off][1] = 1; + for(int i = 0; i < n; i++){ + for(int s = -45; s <= 45; s++){ + for(int eq = 0; eq < 2; eq++){ + ll val = dp[i][off+s][eq]; + if(!val) continue; + + int mnd = (eq)? v[i] : 9; + + for(int nd = 0; nd <= mnd; nd++){ + int neq = (eq and nd == v[i]); + int nsum = s + (((n-i) % 2 == 1)? -nd : nd); + + dp[i+1][off+nsum][neq] += val; + } + } + } + } + + ll ret = 0; + for(int p : primes){ + ret += dp[n][off+p][0]; + if(flag) ret += dp[n][off+p][1]; + } + + return ret; + +} + +void solve(){ + ll a, b; + cin >> a >> b; + vector A = itov(a); + vector B = itov(b); + + cout << get(B,1) - get(A,0) << endl; +} + +int main(){ _ + int t; + cin >> t; + while(t--) solve(); + return 0; +} diff --git a/spoj_cpp_clean/LUCIFER/LUCIFER-8.cpp b/spoj_cpp_clean/LUCIFER/LUCIFER-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1729fe61d027d7978491c5cdcef7193a4b153504 --- /dev/null +++ b/spoj_cpp_clean/LUCIFER/LUCIFER-8.cpp @@ -0,0 +1,180 @@ +/*************************************************** + * Problem Name : LUCIFER - LUCIFER Number.cpp + * Problem Link : https://www.spoj.com/problems/LUCIFER/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : DigitDP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorA, B; +int num; +char sieve[105]; +void primeSieve ( int n ) { + sieve[0] = sieve[1] = 1; + + for ( int i = 4; i <= n; i += 2 ) { + sieve[i] = 1; + } + + int sqrtn = sqrt ( n ); + + for ( int i = 3; i <= sqrtn; i += 2 ) { + if ( sieve[i] == 0 ) { + for ( int j = i * i; j <= n; j += 2 * i ) sieve[j] = 1; + } + } +} +ll dp[10][50][50][2][2]; +ll digitDP (int pos, int odd, int even, int isChoto, int isBoro) { + if (pos == num) { + int x; + + if (pos % 2 == 1) { + x = even - odd; + + } else { + x = odd - even; + } + + if (x <= 0) { + return 0LL; + } + + if (sieve[x] == 0) { + return 1LL; + + } else { + return 0LL; + } + } + + ll &ans = dp[pos][odd][even][isChoto][isBoro]; + + if (ans != -1) return ans; + + int low = 0, high = 9; + + if (isChoto == 0) { + high = B[pos]; + } + + if (isBoro == 0) { + low = A[pos]; + } + + ans = 0; + + for (int i = low; i <= high; i++) { + if (pos % 2 == 0) { + ans += digitDP (pos + 1, odd + i, even, isChoto | (i < high), + isBoro | (i > low) ); + + } else { + ans += digitDP (pos + 1, odd, even + i, isChoto | (i < high), + isBoro | (i > low) ); + } + } + + return ans; +} + +ll solve (ll a, ll b) { + A.clear(), B.clear(); + + while (b > 0) { + int rem = b % 10; + B.pb (rem); + b /= 10; + } + + reverse (all (B) ); + + while (a > 0) { + int rem = a % 10; + A.pb (rem); + a /= 10; + } + + num = (int) B.size(); + + while ( (int) A.size() < num) { + A.pb (0); + } + + reverse (all (A) ); + SET (dp, -1); + ll ans = digitDP (0, 0, 0, 0, 0); + return ans; +} +int main () { + __FastIO; + primeSieve (100); + int tc; + cin >> tc; + + while (tc--) { + int a, b; + cin >> a >> b; + ll ans = solve (a, b); + cout << ans << "\n"; + } + + return 0; +} + diff --git a/spoj_cpp_clean/MAIN111/MAIN111-14.cpp b/spoj_cpp_clean/MAIN111/MAIN111-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc02817dde292429fd6b7dc10480a8c8094cd652 --- /dev/null +++ b/spoj_cpp_clean/MAIN111/MAIN111-14.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; + +#define ll long long +ll dgt[8], cs[1000009]; +bool mark[100009]; + +int main() + +{ + mark[1] = 1; + mark[0] = 1; + for(ll i = 3; i * i <= 100000; i += 2) { + if(mark[i] == 0) { + for(ll j = i * i; j <= 100000; j += i + i) { + mark[j] = 1; + } + } + } + + for(ll i = 4; i <= 100000; i += 2) + mark[i] = 1; + + for(ll i = 1; i <= 100000; i++) { + + ll nm = i, indx = 0; + while(nm != 0) { + ll rem = nm % 10; + nm /= 10; + dgt[indx++] = rem; + } + + reverse(dgt, dgt + indx); + ll rng = (1 << indx) - 1; + + bool Sprime = 1; + for(ll j = 1; j <= rng; j++) { + ll num = 0; + for(ll k = indx - 1; k >= 0; k--) { + if( (j >> k) & 1) + num = num * 10 + dgt[k]; + } + if(mark[num] == 0) { + Sprime = 0; + break; + } + } + + if(Sprime) + cs[i] = 1; + } + + for(ll i = 1; i <= 100000; i++) + cs[i] += cs[i - 1]; + + ll t; + cin >> t; + + while(t--) { + ll a, b; + scanf("%lld %lld", &a, &b); + if(a > b) + swap(a, b); + + printf("%lld\n", cs[b] - cs[a - 1]); + } + + return 0; +} diff --git a/spoj_cpp_clean/MAIN112/MAIN112-14.cpp b/spoj_cpp_clean/MAIN112/MAIN112-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..467c0d9d6502b5386309b751a3679d66c61812e6 --- /dev/null +++ b/spoj_cpp_clean/MAIN112/MAIN112-14.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +#define ll long long + +ll ara[20], c[20], n; +ll dp[1 << 16][17]; + +ll solve(ll mask, ll prv, ll indx) + +{ + if(indx > n) + return 0; + + ll &ret = dp[mask][prv]; + if(ret != -1) + return ret; + + ll add = 0, mx = 1e18; + for(ll i = 1; i <= n; i++) { + bool check = (mask >> i) & 1; + if(check) + continue; + + if(prv != 0) + add = abs(ara[i] - ara[prv]) * c[indx]; + + mx = min(mx, add + solve(mask | (1 << i), i, indx + 1) ); + } + + return ret = mx; +} + +int main() + +{ + ll t; + cin >> t; + while(t--) { + memset(dp, -1, sizeof(dp)); + scanf("%lld", &n); + for(ll i = 1; i <= n; i++) + scanf("%lld", &ara[i]); + for(ll i = 1; i <= n; i++) + scanf("%lld", &c[i]); + + printf("%lld\n", solve(0, 0, 1) ); + } + + return 0; +} diff --git a/spoj_cpp_clean/MAIN72/MAIN72-3.cpp b/spoj_cpp_clean/MAIN72/MAIN72-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c950e7f145c7f927ca076fccdabb31eefd31860a --- /dev/null +++ b/spoj_cpp_clean/MAIN72/MAIN72-3.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, n, num[101], sum[100001], integers[100001], l, temp; + long long ans; + sd(t); + while(t--){ + sd(n); + for(int i = 0; i <= 100000; i++) + sum[i] = 0; + for(int i = 0; i < n; i++){ + sd(num[i]); + } + integers[0] = 0; //Those integers which are sums of subsets + integers[1] = num[0]; + l = 2, ans = num[0]; + sum[integers[0]] = 1; //Those integers which are sums of subsets and have already been cosidered. + sum[integers[1]] = 1; + for(int i = 1; i < n; i++){ + temp = l; + for(int j = 0; j < l; j++){ // For all those sums that alrady exist, add the ith element, if it is not set then it is a new integer. + if(sum[integers[j] + num[i]] == 0){ + integers[temp] = integers[j] + num[i]; + sum[integers[j] + num[i]] = 1; + ans += integers[temp]; + temp++; + } + } + l = temp; + } + printf("%lld\n",ans); + } + return 0; +} + diff --git a/spoj_cpp_clean/MAJOR/MAJOR-3.cpp b/spoj_cpp_clean/MAJOR/MAJOR-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b046f03ed35b5914ee74482b635a4acaf4462dd --- /dev/null +++ b/spoj_cpp_clean/MAJOR/MAJOR-3.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, temp, ans; + map count; + long long n, max; + sd(t); + while(t--){ + count.clear(); + max = 0; + scanf("%lld",&n); + for(long long i = 0; i < n; i++){ + sd(temp); + if(count.find(temp) == count.end()) + count[temp] = 1; + else{ + count[temp]++; + if(count[temp] > max){ + max = count[temp]; + ans = temp; + } + } + } + if(max > n/2){ + printf("YES %d\n",ans); + } + else + printf("NO\n"); + + } + return 0; +} diff --git a/spoj_cpp_clean/MARTIAN/MARTIAN-3.cpp b/spoj_cpp_clean/MARTIAN/MARTIAN-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a9c8827157a962f0d9a85828adb04d58f597fc5 --- /dev/null +++ b/spoj_cpp_clean/MARTIAN/MARTIAN-3.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int n, m, A[500][500], B[500][500], dp[500][500], temp; //A and B are the cummulative sums of Y and B + sd2(n,m); + while(n > 0 and m > 0){ + for(int i = 0; i < n; i++){ + for(int j = 0; j < m; j++){ + sd(temp); + A[i][j] = (j)? temp + A[i][j-1]: temp; + } + } + + for(int i = 0; i < n; i++){ + for(int j = 0; j < m; j++){ + sd(temp); + B[i][j] = (i)? temp + B[i-1][j]: temp; + } + } + + dp[0][0] = max(A[0][0], B[0][0]); + + for(int i = 1; i < m; i++) + dp[0][i] = max(A[0][i], B[0][i] + dp[0][i-1]); + + for(int i = 1; i < n; i++) + dp[i][0] = max(A[i][0] + dp[i-1][0], B[i][0]); + + for(int i = 1; i < n; i++) + for(int j = 1; j < m; j++) + dp[i][j] = max(A[i][j] + dp[i-1][j], B[i][j] + dp[i][j-1]); + + printf("%d\n", dp[n-1][m-1]); + sd2(n,m); + } + return 0; +} diff --git a/spoj_cpp_clean/MARYBMW/MARYBMW-8.cpp b/spoj_cpp_clean/MARYBMW/MARYBMW-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..308a4760b07a05121b3ee6b0eed2d5e9514cf24b --- /dev/null +++ b/spoj_cpp_clean/MARYBMW/MARYBMW-8.cpp @@ -0,0 +1,178 @@ +/*************************************************** + * Problem Name : MARYBMW - BMW.cpp + * Problem Link : https://www.spoj.com/problems/MARYBMW/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-09-13 + * Problem Type : Graph (MST) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 50005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +vectorcost[MAX]; +int par[MAX]; +ll mn[MAX]; +struct Edge { + int u, v; + ll w; +}; +bool cmp (Edge a, Edge b) { + return a.w > b.w; +} +vectorE; + +int find (int r) { + if (par[r] == r) return r; + else return par[r] = find (par[r]); +} +void MST (int n) { + sort (all (E), cmp); + + for (int i = 1; i <= n; i++) { + par[i] = i; + } + + int cnt = 0; + + for (int i = 0; i < (int) E.size(); i++) { + int u = find (E[i].u); + int v = find (E[i].v); + + if (u != v) { + par[u] = v; + cnt++; + adj[E[i].u].pb (E[i].v); + adj[E[i].v].pb (E[i].u); + cost[E[i].u].pb (E[i].w); + cost[E[i].v].pb (E[i].w); + + if (cnt == n) break; + } + } +} +//~ bool vis[MAX]; +//~ ll bfs (int s, int d) { +//~ SET (vis, false); +//~ queueQ; +//~ vis[s] = true; +//~ mn[s] = 1000000000000000005; +//~ Q.push (s); + +//~ while (!Q.empty() ) { +//~ int u = Q.front(); +//~ Q.pop(); + +//~ for (int i = 0; i < (int) adj[u].size(); i++) { +//~ int v = adj[u][i]; + +//~ if (!vis[v]) { +//~ vis[v] = true; +//~ mn[v] = min (mn[u], cost[u][i]); + +//~ if (v == d) return mn[d]; + +//~ Q.push (v); +//~ } +//~ } +//~ } + +//~ return -1; +//~ } +bool flag; +void dfs (int u, int p, int d) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + mn[v] = min (mn[u], cost[u][i]); + + if (v == d) flag = 1; + + dfs (v, u, d); + } +} +int main () { + __FastIO; + int tc, N, M; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> N >> M; + + for (int i = 1; i <= M; i++) { + Edge get; + cin >> get.u >> get.v >> get.w; + E.pb (get); + } + + MST (N); + //~ ll ans = bfs (1, N); + flag = 0; + mn[1] = 1000000000000000005; + dfs (1, -1, N); + + if (!flag) cout << "-1\n"; + else cout << mn[N] << "\n"; + + E.clear(); + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + cost[i].clear(); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/MATCHING/MATCHING-0.cpp b/spoj_cpp_clean/MATCHING/MATCHING-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..552cacbfe64d0f27931cd57ace54bd82b5282f7b --- /dev/null +++ b/spoj_cpp_clean/MATCHING/MATCHING-0.cpp @@ -0,0 +1,112 @@ +#include + +#define FI first +#define SE second + +typedef long long ll; + +struct HopcroftKarp { + typedef long long ll; + + struct Node { + ll id; + ll level; + + Node* pair; + std::vector adj; + + bool used; + + Node(const ll& ii) + : id(ii), level(-1), pair(nullptr), used(false) {} + }; + + ll n; + std::vector graph; + + HopcroftKarp(const ll& nn) { + n = nn; + graph.resize(n); + for (ll i = 0; i < n; ++i) + graph[i] = new Node(i); + } + + ~HopcroftKarp(void) { + for (Node* e : graph) + delete e; + } + + void add_edge(const ll& a, const ll& b) { + if (!graph[a] || !graph[b]) return; + graph[a]->adj.push_back(graph[b]); + graph[b]->adj.push_back(graph[a]); + } + + /* matches the graph. */ + ll match(void) { + for (ll match = 0; ;) { + std::queue order; + for (auto& node : graph) { + node->level = -1; + if (node->pair == nullptr) { + node->level = 0; + order.push(node); + } + } + + while (!order.empty()) { + Node* curr = order.front(); order.pop(); + for (auto& neigh : curr->adj) { + Node* w = neigh->pair; + if (w != nullptr && w->level < 0) { + w->level = curr->level + 1; + order.push(w); + } + } + } + + for (auto& node : graph) + node->used = false; + + ll d = 0; + for (auto& node : graph) + d += node->pair == nullptr && dfs(node); + + match += d; + + /* the end */ + if (d == 0) return match; + } + } + + bool dfs(Node* curr) { + curr->used = true; + for (auto& neigh : curr->adj) { + Node* w = graph[neigh->id]->pair; + if (w == nullptr || !w->used && curr->level < w->level && dfs(w)) { + curr->pair = neigh; + neigh->pair = curr; + return true; + } + } + return false; + } +}; + +int main(void) { + std::ios::sync_with_stdio(false); + std::cin.tie(nullptr); + + ll n, m, p; + std::cin >> n >> m >> p; + + HopcroftKarp HK(n + m); + for (ll a,b; p--;) { + std::cin >> a >> b; + HK.add_edge(a-1, b-1 + n); + } + + std::cout << HK.match() << "\n"; + + return 0; +} diff --git a/spoj_cpp_clean/MATSUM/MATSUM-3.cpp b/spoj_cpp_clean/MATSUM/MATSUM-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ca18f1b376c66b51e2c96ecc8c2a28a31085599 --- /dev/null +++ b/spoj_cpp_clean/MATSUM/MATSUM-3.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int tree[1024][1024], mat[1024][1024], N; + +void add(int x, int y, int value){ + for (int i = x; i < N; i |= i + 1) + for (int j = y; j < N; j |= j + 1) + tree[i][j] += value; +} + +int sum(int x, int y){ + int res = 0; + for (int i = x; i >= 0; i = (i & (i + 1)) - 1) + for (int j = y; j >= 0; j = (j & (j + 1)) - 1) + res += tree[i][j]; + return res; +} + +int main(){ + int T, x1, y1, x2, y2; + + sd(T); + + while(T--){ + scanf("%d", &N); + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + tree[i][j] = mat[i][j] = 0; + + char cmd[10]; + + while (scanf(" %s", cmd) == 1 && cmd[0] != 'E') { + + if (cmd[1] == 'E'){ + int x, y, num; + scanf("%d %d %d", &x, &y, &num); + if (mat[x][y] != num){ + add(x, y, num-mat[x][y]); + mat[x][y] = num; + } + } + else{ + + scanf("%d %d %d %d", &x1, &y1, &x2, &y2); + + printf("%d\n", sum(x2, y2) - sum(x1-1, y2) - sum(x2, y1-1) + sum(x1-1, y1-1)); + } + } + } +} diff --git a/spoj_cpp_clean/MATSUM/MATSUM-4.cpp b/spoj_cpp_clean/MATSUM/MATSUM-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a74dae6e9e89006b6815676a9a1021d84d0c071c --- /dev/null +++ b/spoj_cpp_clean/MATSUM/MATSUM-4.cpp @@ -0,0 +1,192 @@ +// Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0; i33) + { + h=h*10+c-'0'; + c=getchar();// getchar_unlocked(); + } + return h; +} +using namespace std; + +int n, m, d; +int matrix[1025][1025], mat[1026][1026]; // LL is macro for long long +void update (int x , int y , int val) +{ +// val=val-matrix[x][y]; // This is the value that we must add while updating + int origy=y; + while(x<=n) + { + y=origy; + while(y<=n) + { + matrix[x][y]+=val; + y+=y & -y; + } + x+= x & -x; + } +} + +int querry(int x, int y) +{ + int sum=0; + int origy=y; + while(x>0) + { + y=origy; + while(y>0) + { + sum+=matrix[x][y]; + y-=y& -y; + } + x-= x & -x; + } + return sum; +} + +int main() +{ + int t; + s(t); // macro for reading integer + while(t--) + { + s(n); + fill(matrix,0); + char s[5]; int x1, y1,x2,y2; + int val; + fill(mat, 0); + while(true) + { + scanf("%s",s); + if(!strcmp(s,"SET")) + { + scanf("%d %d %d",&x1,&y1,&val); + x1++; y1++; + if (mat[x1][y1] != val) + { + update(x1,y1,val-mat[x1][y1]); + mat[x1][y1] = val; + } + } + else if(!strcmp(s,"SUM")) + { + scanf("%d %d %d %d",&x1,&y1,&x2,&y2); + x1++; x2++; y1++; y2++; + val=(querry(x2,y2)-querry(x1-1,y2)-querry(x2,y1-1)+querry(x1-1,y1-1)); + printf("%d\n",val); + } + else + { + printf("\n"); + break; + } + } + } +} +/* +int main() +{ + int T; + scanf("%d", &T); + + for (int cs = 1; cs <= T; cs++) + { + scanf("%d", &N); + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + tree[i][j] = mat[i][j] = 0; + + char cmd[10]; + while (scanf(" %s", cmd) == 1 && cmd[0] != 'E') + { + if (cmd[1] == 'E') + { + int x, y, num; + scanf("%d %d %d", &x, &y, &num); + if (mat[x][y] != num) + { + add(x, y, num-mat[x][y]); + mat[x][y] = num; + } + } + else + { + int x1, y1, x2, y2; + scanf("%d %d %d %d", &x1, &y1, &x2, &y2); + int res = sum(x2, y2) - sum(x1-1, y2) - sum(x2, y1-1) + sum(x1-1, y1-1); + printf("%d\n", res); + } + } + } +} +*/ diff --git a/spoj_cpp_clean/MAXISET/MAXISET-3.cpp b/spoj_cpp_clean/MAXISET/MAXISET-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a57ff77f4bd098baf256a755addf31ad7b64957d --- /dev/null +++ b/spoj_cpp_clean/MAXISET/MAXISET-3.cpp @@ -0,0 +1,172 @@ +/* + Solution for SPOJ problem - http://www.spoj.com/problems/MAXISET/ + Solution using meet-in-the-middle, bitmasking and dynamic programming +*/ + +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<> 1, k2 = n - k1; + + int other = (1 << k2) - 1; + + + int up1 = 1 << k1; + for(int i = 1; i < up1; i++){ + int sub = i&(i-1); + + int lo = 0; + while(!(i&(1 << lo))) lo++; + + if(!sub){ + ok[i] = (g[lo] >> k1) ^ other; + sum[i] = w[lo]; + continue; + } + + if(g[lo]&sub){ + ok[i] = 0; + sum[i] = 0; + continue; + } + + ok[i] = ok[sub]&((g[lo] >> k1) ^ other); + sum[i] = sum[sub] + w[lo]; + } + + int up2 = 1 << k2; + for(int i = 1; i < up2; i++){ + ll sub = i&(i-1); + + int lo = 0; + while(!(i&(1< +#define hell 1000000009 +#define bug1(x) cout<<"$ "<=b;i--) +#define loop(i,a,b) for(long long i=a; i +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(int i){ + double r; + cin>>r; + cout<<"Case "<>test_cases; + loop(i,1,test_cases+1){ + solve(i); + } + return 0; +} diff --git a/spoj_cpp_clean/MAXLN/MAXLN-5.cpp b/spoj_cpp_clean/MAXLN/MAXLN-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cabc2a07e24b1eb5748fc45d607abdbf93d5a0c --- /dev/null +++ b/spoj_cpp_clean/MAXLN/MAXLN-5.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + double r; + long long i,test; + double ans; + cin>>test; + i=1; + while(test--) + { + cin>>r; + ans=(4*r*r)+(0.25); + cout<<"Case "< +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define all(v) v.begin(), v.end() +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; ++i) +#define for0(i, stop) for(ll i = 0; i < stop; ++i) +#define rep1(i, start) for(ll i = start; i >= 1; --i) +#define rep0(i, start) for(ll i = (start-1); i >= 0; --i) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define EL '\n' +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +/*** + * Multiply (7x^2 + 8x^1 + 9x^0) with (6x^1 + 5x^0) + * ans = 42x^3 + 83x^2 + 94x^1 + 45x^0 + * A = {9, 8, 7} + * B = {5, 6} + * V = multiply(A,B) + * V = {45, 94, 83, 42} +***/ + +/*** Tricks + * Use vector < bool > if you need to check only the status of the sum + * Use bigmod if the power is over same polynomial && power is big + * Use long double if you need more precision + * Use long long for overflow +***/ +typedef vector vi; +const double PI = 2.0 * acos(0.0); +using cd = complex; + +void fft(vector & a, bool invert = 0) + +{ + int n = a.size(); + + for (int i = 1, j = 0; i < n; i++) { + int bit = n >> 1; + for (; j & bit; bit >>= 1) + j ^= bit; + j ^= bit; + + if (i < j) + swap(a[i], a[j]); + } + + for (int len = 2; len <= n; len <<= 1) { + double ang = 2 * PI / len * (invert ? -1 : 1); + cd wlen(cos(ang), sin(ang)); + for (int i = 0; i < n; i += len) { + cd w(1); + for (int j = 0; j < len / 2; j++) { + cd u = a[i+j], v = a[i+j+len/2] * w; + a[i+j] = u + v; + a[i+j+len/2] = u - v; + w *= wlen; + } + } + } + + if (invert) { + for (cd & x : a) + x /= n; + } +} + +void ifft(vector & p) + +{ + fft(p, 1); +} + +vi multiply(vi const& a, vi const& b) +{ + + vector fa(a.begin(), a.end()), fb(b.begin(), b.end()); + int n = 1; + while (n < a.size() + b.size()) + n <<= 1; + fa.resize(n); + fb.resize(n); + + fft(fa); + fft(fb); + for (int i = 0; i < n; i++) + fa[i] *= fb[i]; + ifft(fa); + + vi result(n); + for (int i = 0; i < n; i++) + result[i] = round(fa[i].real()); + + return result; +} + +const ll sz = 1e5 + 10, offset = 1e5; +char s[sz]; + +vi numa1, numa2, numb1, numb2, numc1, numc2; + +int main() +{ + scanf("%s", s); + + ll len = strlen(s); + numa1.resize(len+1,0), numa2.resize(len+1+offset,0); + numb1.resize(len+1,0), numb2.resize(len+1+offset,0); + numc1.resize(len+1,0), numc2.resize(len+1+offset,0); + + for(ll i = 0; s[i] != '\0'; i++) { + if(s[i] == 'a') + numa1[i]++, numa2[-i+offset]++; + else if(s[i] == 'b') + numb1[i]++, numb2[-i+offset]++; + else + numc1[i]++, numc2[-i+offset]++; + } + + vi res1 = multiply(numa1, numa2); + vi res2 = multiply(numb1, numb2); + vi res3 = multiply(numc1, numc2); + + ll mx = 0; + for1(i, len) mx = max(mx, res1[i+offset] + res2[i+offset] + res3[i+offset]); + + bool f = 0; + cout << mx << EL; + for1(i, len) { + if(res1[i+offset] + res2[i+offset] + res3[i+offset] != mx) + continue; + + if(f) pf(" "); + pf("%lld", i); + + f = 1; + } + pn; + + return 0; diff --git a/spoj_cpp_clean/MAXSUMSU/MAXSUMSU-21.cpp b/spoj_cpp_clean/MAXSUMSU/MAXSUMSU-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b2103b3fa7ac708c07a6ecc6c13a7f7484a12a0 --- /dev/null +++ b/spoj_cpp_clean/MAXSUMSU/MAXSUMSU-21.cpp @@ -0,0 +1,37 @@ +/* + dynamic programming > max 1D range sum > kadane + difficulty: easy + date: 10/Jan/2020 + by: @brpapa +*/ +#include +#include +#define INF 0x3f3f3f3f +using namespace std; + +int N; +vector a; + +int kadaneBU(int l, int r) { + int maxTotal = -INF, maxCurr = 0; + + for (int i = l; i < r; i++) { + maxCurr = max(maxCurr + a[i], a[i]); + maxTotal = max(maxTotal, maxCurr); + } + return maxTotal; +} + +int main() { + int T; + cin >> T; + while (T--) { + cin >> N; + a.resize(N); + for (int &aa : a) + cin >> aa; + + cout << kadaneBU(0, N) << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/MAZE/MAZE-4.cpp b/spoj_cpp_clean/MAZE/MAZE-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d25e6d24966f4b4959f1508c1545ee7f85ec6c7 --- /dev/null +++ b/spoj_cpp_clean/MAZE/MAZE-4.cpp @@ -0,0 +1,222 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +//Other Includes +#include +#include +#include +#include +#include +#include +#include +#include +//some common functionn +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define all(c) (c).begin(),(c).end() +// Input macros +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) + +// Output macros +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +using namespace std; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i=0 && yn>=0 && xn<3 && yn +using namespace std; + +#define ll long long + +vector prime, palprime; +bool mark[1000009]; + +bool isPal(ll n) + +{ + ll num = 0, tmp = n; + while(tmp != 0) { + ll rem = tmp % 10; + num = num * 10 + rem; + tmp /= 10; + } + + if(num == n) + return 1; + return 0; +} + +ll mul(ll n) + +{ + ll tmp = n, ret = 1; + while(tmp != 0) { + ll rem = tmp % 10; + if(rem != 0) + ret *= rem; + tmp /= 10; + } + + return ret; +} + +int main() + +{ + prime.push_back(2); + palprime.push_back(2); + for(ll i = 3; i <= 1000000; i += 2) { + if(mark[i] == 0) { + for(ll j = i * i; j <= 1000000; j += i + i) { + mark[j] = 1; + } + if(isPal(i)) { + palprime.push_back(i); + //cout << i << endl; + } + + prime.push_back(i); + } + } + //cout << prime.size() << endl; + + sort(prime.begin(), prime.end()); + sort(palprime.begin(), palprime.end()); + + ll t; + cin >> t; + while(t--) { + ll n; + scanf("%lld", &n); + ll m = mul( palprime[n - 1] ); + printf("%lld %lld\n", palprime[n - 1], prime[m - 1]); + } + + return 0; +} diff --git a/spoj_cpp_clean/MBEEWALK/MBEEWALK-5.cpp b/spoj_cpp_clean/MBEEWALK/MBEEWALK-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..492baf8911a9581267063b9e1ba013429b855ab2 --- /dev/null +++ b/spoj_cpp_clean/MBEEWALK/MBEEWALK-5.cpp @@ -0,0 +1,39 @@ +#include +#include + +using namespace std; + +vectordp(20); +int calc(int x) +{ + if(x==2||x==4) + return dp[x]; + else if(x%2==1) + { + if(dp[x-1]==0) + return dp[x-1]=calc(x-1); + else + return dp[x-1]; + } + else + { + + if(dp[x]==0) + return dp[x]=15*calc(x-1); + else + return dp[x]; + } +} +int main() +{ + int test,num,i,j,k; + dp[2]=6; + dp[4]=90; + cin>>test; + while(test--) + { + cin>>num; + cout< +#define mp make_pair +#define pb push_back +#define MAX 220 + +using namespace std; + +typedef pair ii; +typedef vector vi; +typedef unsigned long long int64; + +int n, b, x; + +const int INF = 0x3f3f3f3f; + +int main() +{ + ios::sync_with_stdio(false); + while (cin >> n >> b && n + b) { + int maior = -1; + vi globo; + unordered_set ball; + for (int i = 0; i < b; ++i) { + cin >> x; + globo.pb(x); + maior = max(maior, x); + } + for (int i = 0; i < globo.size(); i++) + for (int j = i; j < globo.size(); j++) + ball.insert(abs(globo[i] - globo[j])); + if (ball.size() == n + 1) + cout << "Y\n"; + else + cout << "N\n"; + } + return 0; +} diff --git a/spoj_cpp_clean/MBINGO/MBINGO-21.cpp b/spoj_cpp_clean/MBINGO/MBINGO-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18aafc4efcd52af32c7110019c379c641f606877 --- /dev/null +++ b/spoj_cpp_clean/MBINGO/MBINGO-21.cpp @@ -0,0 +1,36 @@ +/* + ad-hoc + difficulty: none + date: none + by: @brpapa +*/ +#include +#include +using namespace std; + +bool chamouTodas(vector vet, int tam) { + for (int i = 0; i < tam; i++) + if (!vet[i]) + return false; + return true; +} +int main() { + int n, b, dif; + while (true) { + cin >> n >> b; + if (n == 0 && b == 0) + return 0; + + vector bolas(b); + vector ok(n+1, false); + for (int i = 0; i < b; i++) + cin >> bolas[i]; + + ok[0] = true; //pode retirar as mesmas bolas, logo sempre terá uma dif abs nula + for (int i = 0; i < b - 1; i++) + for (int j = i + 1; j < b; j++) + ok[abs(bolas[i] - bolas[j])] = true; + + printf("%s\n", chamouTodas(ok, n+1) ? "Y" : "N"); + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/MC/MC-3.cpp b/spoj_cpp_clean/MC/MC-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da353bc74fefc362d2280c17f989c70869e08be9 --- /dev/null +++ b/spoj_cpp_clean/MC/MC-3.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); + +using namespace std; +string S, T; +int l1, l2; + + +int main(){ + cin>>S; + int matrix[1001][1001], temp; + while(S[0] != '#'){ + cin>>T; + l1 = S.length(), l2 = T.length(); + + for(int i = 0; i <= l1; i++){ + matrix[0][i] = 0; + } + for(int i = 0; i <= l2; i++){ + matrix[i][0] = 0; + } + + //cout<>S; + } + return 0; +} diff --git a/spoj_cpp_clean/MCHAOS/MCHAOS-14.cpp b/spoj_cpp_clean/MCHAOS/MCHAOS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab356946939c1b87f7ba3c7361dc0a6f114150ce --- /dev/null +++ b/spoj_cpp_clean/MCHAOS/MCHAOS-14.cpp @@ -0,0 +1,83 @@ +#include +using namespace std; + +#define ll long long +struct node { + string s; + ll id; +} ara[100009], ara2[100009]; + +ll BITree[100009]; + +bool cmp(node a, node b) + +{ + return a.s < b.s; +} + +ll getSum(ll index) +{ + ll sum = 0; // Iniialize result + + // Traverse ancestors of BITree[index] + while (index>0) + { + // Add current element of BITree to sum + sum += BITree[index]; + + // Move index to parent node in getSum View + index -= index & (-index); + } + return sum; +} + +void updateBIT(ll n, ll index, ll val) + +{ + + // Traverse all ancestors and add 'val' + while (index <= n) + { + // Add 'val' to current node of BI Tree + BITree[index] += val; + + // Update index to that of parent in update View + index += index & (-index); + } +} + +int main() + +{ + std::ios_base::sync_with_stdio(false); + + ll n; + cin >> n; + for(ll i = 1; i <= n; i++) + cin >> ara[i].s; + + sort(ara + 1, ara + n + 1, cmp); + + for(ll i = 1; i <= n; i++) { + ara[i].id = i; + + string tmp = ara[i].s; + reverse(tmp.begin(), tmp.end()); + + ara2[i].s = tmp; + ara2[i].id = i; + } + + sort(ara2 + 1, ara2 + n + 1, cmp); + + ll ans = 0; + for(ll i = 1; i <= n; i++) { + updateBIT(n, ara2[i].id, 1); + + ans += (getSum(n) - getSum(ara2[i].id) ); + } + + cout << ans << endl; + + return 0; +} diff --git a/spoj_cpp_clean/MCLONUM/MCLONUM-9.cpp b/spoj_cpp_clean/MCLONUM/MCLONUM-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b89a42a4faffe0d48abdfa917af3d93ecfea1fa0 --- /dev/null +++ b/spoj_cpp_clean/MCLONUM/MCLONUM-9.cpp @@ -0,0 +1,175 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +string ans; +string A; +string B; +bool mark[64]; + +inline string read_string() +{ + char c; + string S; + + c = gc(); + + while(isspace(c)) + c = gc(); + + S.push_back(c); + + c = gc(); + + while(!isspace(c) && c != EOF) + { + S.push_back(c); + c = gc(); + } + + return S; +} + +inline void init() +{ + A = read_string(); + B = read_string(); + sort(B.begin(), B.end()); +} + +inline bool bigger(int pos) +{ + int i, j; + + if(pos == B.size() - 1) + { + for(i=0; i= A[pos]) + { + ans.push_back(B[i]); + return 1; + } + + return 0; + } + + for(i=0; i A[pos]) + { + mark[i] = 1; + ans.push_back(B[i]); + for(j=0; j +using namespace std; + +int dp[1000001], n, k, l, m, i; + +int solve(int c) + +{ + if(c < 0) + return 0; + + if(dp[c] != -1) { + //cout << c << " " << dp[c] << endl; + return !dp[c]; + } + + return dp[c] = solve(c - 1) | solve(c - k) | solve(c - l); +} + +int main() + +{ + memset(dp, -1, sizeof(dp)); + dp[0] = 0; + dp[1] = 1; + scanf("%d %d %d", &k, &l, &m); + for(i = 2; i <= 1000000; i++) { + /*if(i - 1 >= 0) + dp[i] |= !dp[i - 1]; + if(i - k >= 0) + dp[i] |= !dp[i - k]; + if(i - l >= 0) + dp[i] |= !dp[i - l];*/ + solve(i); + } + + for(i = 1; i <= m; i++) { + scanf("%d", &n); + if(dp[n]) + printf("A"); + else + printf("B"); + } + return 0; +} diff --git a/spoj_cpp_clean/MCOINS/MCOINS-3.cpp b/spoj_cpp_clean/MCOINS/MCOINS-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4e588d7e95f353b5abe088797b2fcaacbb604e4 --- /dev/null +++ b/spoj_cpp_clean/MCOINS/MCOINS-3.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int k,l,m,j; + sd3(k,l,m); + int dp[1000001]; + dp[1] = 1; + dp[k] = 1; + dp[l] = 1; + for(int i = 2; i < k; i++){ + dp[i] = (i%2)? 1:0; + } + for(int i = k+1 ; i < l; i++){ + dp[i] = abs(min(dp[i-1], dp[i-k]) - 1); + } + for(int i = l+1; i <= 1000000; i++){ + dp[i] = abs(min(min(dp[i-1], dp[i-k]), dp[i-l]) - 1); + } + for(int i = 0; i < m; i++){ + sd(j); + if(dp[j]) printf("A"); + else printf("B"); + } + return 0; +} diff --git a/spoj_cpp_clean/MCOINS/MCOINS-9.cpp b/spoj_cpp_clean/MCOINS/MCOINS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3b3f0f142bce359baba659169c0ff9bcce6fbd5 --- /dev/null +++ b/spoj_cpp_clean/MCOINS/MCOINS-9.cpp @@ -0,0 +1,70 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +vector Result; +int N, K, L, maxi; +vector NUMS; + +inline bool check(int num) +{ + if(num-1>=0 && Result[num-1]==0) + return 1; + else if(num-K>=0 && Result[num-K]==0) + return 1; + else if(num-L>=0 && Result[num-L]==0) + return 1; + + return 0; +} + +void solve() +{ + int i; + Result.resize(maxi+1); + + Result[0]=0; + + for(i=1; i<=maxi; i++) + Result[i]=check(i); +} + +int main() +{ + int i; + +// freopen("coins.in","r",stdin); +// freopen("coins.out","w",stdout); + + scanf("%d %d %d", &K, &L, &N); + NUMS.resize(N); + for(i=0; i +typedef int64_t q;q a,b,d,B,f=10;q s(q x,q l,q p){if(p>x)return 0;B=x/p%10;return(p/f*l*B)+(p*(d>a>>b)if(a){if(a>b)std::swap(a,b);for(d=0;d +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int n, d; +ll dp[10][2][2][2]; +ll dpNum[10][2][2]; +vectorA, B; +ll makeNumber (int pos, int isChoto, int isBoro) { + if (pos == n) { + return dpNum[pos][isChoto][isBoro] = 1; + } + + if (dpNum[pos][isChoto][isBoro] != -1) { + return dpNum[pos][isChoto][isBoro]; + } + + int low = 0, high = 9; + + if (isChoto == 0) { + high = B[pos]; + } + + if (isBoro == 0) { + low = A[pos]; + } + + ll ans = 0; + + for (int i = low; i <= high; i++) { + int new_isChoto = isChoto; + int new_isBoro = isBoro; + + if (i < high) { + new_isChoto = 1; + } + + if (i > low) { + new_isBoro = 1; + } + + ans += makeNumber (pos + 1, new_isChoto, new_isBoro); + } + + return dpNum[pos][isChoto][isBoro] = ans; +} +ll digitDP (int pos, int isChoto, int isBoro, int isSuru) { + if (pos == n) return 0; + + if (dp[pos][isChoto][isBoro][isSuru] != -1) { + return dp[pos][isChoto][isBoro][isSuru]; + } + + ll ans = 0; + int low = 0, high = 9; + + if (isChoto == 0) { + high = B[pos]; + } + + if (isBoro == 0) { + low = A[pos]; + } + + for (int i = low; i <= high; i++) { + int new_isChoto = isChoto; + int new_isBoro = isBoro; + int new_isSuru = isSuru; + ll cnt = 0; + + if (i < high) { + new_isChoto = 1; + } + + if (i > low) { + new_isBoro = 1; + } + + if (i > 0) { + new_isSuru = 1; + } + + if (i == d && isSuru == 1) { + cnt = dpNum[pos + 1][new_isChoto][new_isBoro]; + } + + ans += cnt + digitDP (pos + 1, new_isChoto, new_isBoro, new_isSuru); + } + + return dp[pos][isChoto][isBoro][isSuru] = ans; +} + + +void solve (ll a, ll b) { + A.clear(), B.clear(); + + while (b > 0) { + int rem = b % 10; + B.pb (rem); + b /= 10; + } + + reverse (all (B) ); + + while (a > 0) { + int rem = a % 10; + A.pb (rem); + a /= 10; + } + + n = (int) B.size(); + + while ( (int) A.size() < n) { + A.pb (0); + } + + reverse (all (A) ); + SET (dpNum, -1); + makeNumber (0, 0, 0); +} +int main () { + __FastIO; + ll a, b; + + while (cin >> a >> b) { + if (a == 0 && b == 0) break; + + ll aa = min (a, b); + ll bb = max (a, b); + solve (aa, bb); + + for (int i = 0; i <= 9; i++) { + d = i; + SET (dp, -1); + int x = 0; + + if (i != 0) x = 1; + + ll ans = digitDP (0, 0, 0, x); + cout << ans << " "; + } + + nl; + } + + return 0; +} diff --git a/spoj_cpp_clean/MENU/MENU-14.cpp b/spoj_cpp_clean/MENU/MENU-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41deebb5c1ff2f01409802d20f80d2101eb03d62 --- /dev/null +++ b/spoj_cpp_clean/MENU/MENU-14.cpp @@ -0,0 +1,120 @@ +#include +using namespace std; + +#define ll long long +ll n, m, k, cst[55], v[55]; + +struct node { + ll id; + ll prof; + ll cst; + + node(ll _id = 0, ll _p = 0, ll _c = 0) { + id = _id; + prof = _p; + cst = _c; + } +}; + +node dp[25][55][2][105]; + +node mx(node a, node b) + +{ + if(a.prof > b.prof) + return a; + else if(a.prof < b.prof) + return b; + else { + if(a.cst < b.cst) + return a; + else + return b; + } +} + +node solve(ll day, ll prv, ll prvv, ll rem) + +{ + if(day > k) + return node(0, 0, 0); + + node &ret = dp[day][prv][prvv][rem]; + if(ret.id != -1) + return ret; + + node rt; + for(ll i = 1; i <= n; i++) { + if(rem < cst[i]) + continue; + + node tmp; + if(i == prv) + tmp = solve(day + 1, i, 1, rem - cst[i]); + else + tmp = solve(day + 1, i, 0, rem - cst[i]); + + if(tmp.id == 0 && day != k) + continue; + + if(i == prv && prvv == 1) + tmp.cst += cst[i]; + else if(i == prv) { + tmp.cst += cst[i]; + tmp.prof += (v[i] / 2); + } + else { + tmp.cst += cst[i]; + tmp.prof += v[i]; + } + tmp.id = i; + + rt = mx(rt, tmp); + } + + //cout << day << " " << prv << " " << prvv << " " << rem << endl; + return ret = rt; +} + +int main() + +{ + while(scanf("%lld %lld %lld", &k, &n, &m) && !(n == 0 && m == 0 && k == 0)) { + + for(ll i = 1; i <= n; i++) { + scanf("%lld %lld", &cst[i], &v[i]); + v[i] *= 2; + } + + for(ll i = 0; i <= 22; i++) { + for(ll j = 0; j <= 51; j++) + for(ll k = 0; k <= 1; k++) + for(ll l = 0; l <= 101; l++) + dp[i][j][k][l].id = -1; + } + + node ans = solve(1, 0, 0, m); + printf("%0.1f\n", ans.prof / 2.0); + + if(ans.prof != 0) { + + printf("%lld", ans.id); + ll rem2 = m - cst[ans.id], prv = ans.id, prvv = 0; + for(ll i = 2; i <= k; i++) { + + ll id = dp[i][prv][prvv][rem2].id; + printf(" %lld", id); + //cout << i << " " << prv << " " << prvv << " " << rem2 << endl; + rem2 -= cst[id]; + if(id == prv) + prvv = 1; + else + prvv = 0; + prv = id; + } + } + + printf("\n"); + } + + return 0; diff --git a/spoj_cpp_clean/MENU/MENU-3.cpp b/spoj_cpp_clean/MENU/MENU-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae30d02eda440a72fe2713d8ae2a1b870ee0ce55 --- /dev/null +++ b/spoj_cpp_clean/MENU/MENU-3.cpp @@ -0,0 +1,164 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<> k >> n >> m; + + if(k == 0 and n == 0 and m == 0) break; + + for(int i = 0; i <= k; i++){ + for(int j = 0; j <= m; j++){ + for(int x = 0; x <= n; x++){ + dp[i][j][x][0] = dp[i][j][x][1] = dp[i][j][x][2] = -INF; + back[i][j][x][0] = back[i][j][x][1] = back[i][j][x][2] = mp(-1,-1); + } + } + } + + dp[0][0][0][0] = 0; + + for(int i = 1; i <= n; i++){ + cin >> cost[i] >> ben[i]; + } + + for(int d = 1; d <= k; d++){ + for(int b = 1; b <= m; b++){ + for(int i = 1; i <= n; i++){ + if(cost[i] <= b){ + double mx = -INF; + pii id = mp(-1,-1); + + for(int j = 0; j <= n; j++){ + if(i == j) continue; + + if(dp[d-1][b-cost[i]][j][0] > mx){ + mx = dp[d-1][b-cost[i]][j][0]; + id = mp(j,0); + } + + if(dp[d-1][b-cost[i]][j][1] > mx){ + mx = dp[d-1][b-cost[i]][j][1]; + id = mp(j,1); + } + } + + dp[d][b][i][0] = mx + ben[i]; + back[d][b][i][0] = id; + } + + if(2 * cost[i] <= b){ + dp[d][b][i][1] = dp[d-1][b-cost[i]][i][0] + (ben[i] * 0.5); + back[d][b][i][1] = mp(i,0); + } + + if(3 * cost[i] <= b){ + double v1 = dp[d-1][b-cost[i]][i][1]; + double v2 = dp[d-1][b-cost[i]][i][2]; + + if(v1 > v2){ + dp[d][b][i][2] = v1; + back[d][b][i][2] = mp(i,1); + } + else{ + dp[d][b][i][2] = v2; + back[d][b][i][2] = mp(i,2); + } + } + } + } + } + + double best = 0; + pii id = mp(-1,-1); + int budg = 0; + + for(int b = 0; b <= m; b++){ + for(int i = 1; i <= n; i++){ + for(int j = 0; j < 3; j++){ + if(dp[k][b][i][j] > best){ + best = dp[k][b][i][j]; + id = mp(i,j); + budg = b; + + } + } + } + } + + printf("%.1lf\n", best); + if(best == 0){ + cout << endl; + continue; + } + + vector ans; + + while(k > 0){ + int x = id.fi; + + assert(1 <= x and x <= n); + + ans.pb(x); + id = back[k][budg][x][id.se]; + budg -= cost[x]; + k--; + } + + reverse(ans.begin(), ans.end()); + + foreach(it, ans) cout << *it << ' '; + cout << endl << endl; + } + + return 0; +} diff --git a/spoj_cpp_clean/METEORO/METEORO-10.cpp b/spoj_cpp_clean/METEORO/METEORO-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9b4d9793be959800846d5aaaccd466abaeba8f6f --- /dev/null +++ b/spoj_cpp_clean/METEORO/METEORO-10.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +bool inRange(int v, int a, int b) +{ + if(a > b) + return inRange(v, b, a); + return v >= a && v <= b; +} + +int main() +{ + int fx1, fy1, fx2, fy2, test=0; + while((cin >> fx1 >> fy1 >> fx2 >> fy2) && !(fx1 == 0 && fx2 == 0 && fy1 == 0 && fy2 == 0)) + { + int n, x, y, c=0; + cin >> n; + while(n-- > 0 && cin >> x >> y) + { + if(inRange(x, fx1, fx2) && inRange(y, fy1, fy2)) + c++; + } + cout << "Teste " << ++test << endl << c << endl << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/MFLAR10/MFLAR10-12.cpp b/spoj_cpp_clean/MFLAR10/MFLAR10-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80bcbbf5dc2b11da4d23f8b48da5a2703b031ffc --- /dev/null +++ b/spoj_cpp_clean/MFLAR10/MFLAR10-12.cpp @@ -0,0 +1,39 @@ +/* + Resolucao: + Verificar se a primeira letra de cada palavra + e a mesma para a frase inteira utilizando + processamento de strings e uso de stringstream +*/ + +#include +#include +#include + +using namespace std; + +int main() +{ + ios::sync_with_stdio(false); + string str, saida; + for (getline(cin, str); str != "*"; getline(cin, str)) { + istringstream test(str); + bool flag = true; + int count = 0; + char p; + while (test >> saida) { + if (count == 0) + p = saida[0]; + if (p != saida[0]) + if (p + 32 != saida[0]) + if (p - 32 != saida[0]) { + cout << "N\n"; + flag = false; + break; + } + count++; + } + if (flag) + cout << "Y\n"; + } + return 0; +} diff --git a/spoj_cpp_clean/MICEMAZE/MICEMAZE-9.cpp b/spoj_cpp_clean/MICEMAZE/MICEMAZE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80a92b551ff6c25a9e50a9b2b359e1288442399e --- /dev/null +++ b/spoj_cpp_clean/MICEMAZE/MICEMAZE-9.cpp @@ -0,0 +1,77 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#define INF 1000000000 +using namespace std; + +int N, EXIT, T, E; +vector< vector< pair > > G; +int dist[101][101]; + +void init() +{ + int i; + int f, s, w; + + scanf("%d %d %d %d", &N, &EXIT, &T, &E); + G.resize(N+1); + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + string s; + cin>>s; + int N=s.length(), ans=0, p=1, low=0; + //error(ans);error(low); error(p);cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; +int t, n, m, a[100], dp[100][20000], prev[100][20000], cur; +int main(){ + vector op; + sd(t); + while(t--){ + sd2(n,m); + for(int i = 0; i < n; i++){ + sd(a[i]); + } + memset(dp, 0, sizeof(dp)); + op.clear(); + dp[0][10000+a[0]] = 1; + prev[0][10000+a[0]] = -1; + for(int i = 1; i < n; i++){ + for(int j = 0; j < 20000; j++){ + if(a[i] + j < 20000 and dp[i-1][j+a[i]]){ + dp[i][j] = 1; + prev[i][j] = 1; + } + if(i > 1 and j >= a[i] and dp[i-1][j-a[i]]){ + dp[i][j] = 1; + prev[i][j] = 2; + } + } + } + //cout<<"yo"< 0; i--){ + op.push_back(prev[i][j]); + j = (prev[i][j] == 1)? j + a[i] : j - a[i]; + } + //reverse(op.begin(), op.end()); + + cur = 0; + for(int i = op.size()-1; i >= 0; i--){ + if(op[i] == 1) cur++; + else if(op[i] == 2)printf("%d\n",cur+1); + } + while(cur--)printf("1\n"); + cout< +#include +#include +#include +using namespace std; +int main(){ + int t = 1, h, w, temp; + while(t--){ + scanf("%d %d", &h, &w); + int dp[h][w], maxi = 1000000; + for(int i = 0; i < w; i++) + scanf("%d",&dp[0][i]); + if(h > 1){ + if(w>=2){ + for(int i = 1; i < h; i++){ + scanf("%d",&temp); + dp[i][0] = min(dp[i-1][0] + temp, dp[i-1][1] + temp); + for(int j = 1; j < w-1; j++){ + scanf("%d",&temp); + dp[i][j] = min(min(dp[i-1][j-1] + temp, dp[i-1][j] + temp), dp[i-1][j+1] + temp); + } + scanf("%d",&temp); + dp[i][w-1] = min(dp[i-1][w-1] + temp, dp[i-1][w-2] + temp); + }// End of taking inputs. dp[h-1] contains the answer + for(int i = 0; i < w; i++) + if(dp[h-1][i] < maxi) + maxi = dp[h-1][i]; + + } + else{ + maxi = dp[0][0]; + for( int i = 1; i< h; i++){ + scanf("%d",&temp); + maxi += temp; + } + } + } + else{ + for(int i = 0; i < w; i++) + if(dp[h-1][i] < maxi) + maxi = dp[h-1][i]; + } + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int board[105][105]; +int N, M; + +inline int min3(int a, int b, int c) +{ + return min(a, min(b, c)); +} + +int solve() +{ + int i, j; + int sol; + + for(i=N-1; i>=2; i--) + { + board[i][1]+=min(board[i+1][1], board[i+1][2]); + board[i][M]+=min(board[i+1][M], board[i+1][M-1]); + + for(j=2; j<=M-1; j++) + board[i][j]+=min3(board[i+1][j-1], board[i+1][j], board[i+1][j+1]); + } + + board[1][1]+=min(board[2][1], board[2][2]); + board[1][M]+=min(board[2][M], board[2][M-1]); + sol=min(board[1][1], board[1][M]); + + for(i=2; i<=M-1; i++) + { + board[1][i]+=min3(board[2][i-1], board[2][i], board[2][i+1]); + sol=min(sol, board[1][i]); + } + + return sol; +} + +inline void init() +{ + int i, j; + + scanf("%d %d", &N, &M); + + for(i=1; i<=N; i++) + for(j=1; j<=M; j++) + scanf("%d", &board[i][j]); +} + +int main() +{ + int T; + + //freopen("input.txt","r",stdin); + + init(); + printf("%d\n", solve()); + + return 0; +} diff --git a/spoj_cpp_clean/MISPELLING/MISPELLING-4.cpp b/spoj_cpp_clean/MISPELLING/MISPELLING-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d731a6c79f318a154cbeca29a32bfd295deb963 --- /dev/null +++ b/spoj_cpp_clean/MISPELLING/MISPELLING-4.cpp @@ -0,0 +1,116 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +//Other Includes +#include +#include +#include +#include +#include +#include +#include +#include +//some common functionn +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define all(c) (c).begin(),(c).end() +// Input macros +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) + +// Output macros +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +using namespace std; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;in) + cout<>n; + cin>>s; + return true; +} + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + + int T=1; + s(T); + for(int testnum=1;testnum<=T;testnum++) + { + if(!input()) break; + cout< +using namespace std; +int map[501][501] = {0}; +int main() +{ + int n, x, y; + cin >> n; + while(n-- > 0) + { + cin >> x >> y; + if(map[x][y] == 1) + { + cout << 1; + return 0; + } + map[x][y] = 1; + } + cout << 0; + return 0; +} diff --git a/spoj_cpp_clean/MIXTURES/MIXTURES-14.cpp b/spoj_cpp_clean/MIXTURES/MIXTURES-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba6e2a7a6c190f11f6f7678ecc9545385ff9a5fc --- /dev/null +++ b/spoj_cpp_clean/MIXTURES/MIXTURES-14.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +#define ll long long +#define pll pair +ll col[105]; +pll dp[105][105]; + +pll solve(ll i, ll j) + +{ + if(i == j) + return pll(col[i], 0); + if(dp[i][j].first != -1) + return dp[i][j]; + + ll ret = 1e18, col; + for(ll k = i; k < j; k++) { + pll p1 = solve(i, k); + pll p2 = solve(k + 1, j); + + if(p1.second + p2.second + (p1.first * p2.first) < ret) { + ret = p1.second + p2.second + (p1.first * p2.first); + col = (p1.first + p2.first) % 100; + } + else if(p1.second + p2.second + (p1.first * p2.first) == ret) { + if( (p1.first + p2.first) % 100 < col) + col = (p1.first + p2.first) % 100; + } + } + + return dp[i][j] = pll(col, ret); +} + +int main() + +{ + ll n, in; + while(scanf("%lld", &n) != EOF) { + + for(ll i = 0; i <= 102; i++) + for(ll j = 0; j <= 102; j++) + dp[i][j].first = -1, dp[i][j].second = -1; + + for(ll i = 1; i <= n; i++) + scanf("%lld", &col[i]); + + pll p = solve(1, n); + printf("%lld\n", p.second); + } + return 0; +} diff --git a/spoj_cpp_clean/MIXTURES/MIXTURES-3.cpp b/spoj_cpp_clean/MIXTURES/MIXTURES-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ba752c5c20852d8d476195371b3f5b3fcb0da2e --- /dev/null +++ b/spoj_cpp_clean/MIXTURES/MIXTURES-3.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int n, mix[101], dp[101][101][2], INF = 1<<30, MOD = 100, j, temp; + while(scanf("%d",&n)!=-1){ + for(int i = 1; i <= n; i++){ + sd(mix[i]); + } + + for(int i = 1; i <= n; i++){ + dp[i][i][0] = mix[i]; //(a+b)%100 + dp[i][i][1] = 0; //Smoke + } + for(int l = 2; l <= n; l++){ + for(int i = 1; i <= n-l+1; i++){ + j = i + l -1; + dp[i][j][1] = INF; + for(int k = i; k <= j-1; k++){ + temp = dp[i][k][1] + dp[k+1][j][1] + dp[i][k][0]*dp[k+1][j][0]; + if(temp < dp[i][j][1]){ + dp[i][j][0] = (dp[i][k][0] + dp[k+1][j][0])%MOD; + dp[i][j][1] = temp; + } + } + } + } + /*for(int i = 1; i <= n; i++){ + for(int j = 1; j <= n; j++){ + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define min(a, b) ((a0) + dp(); + + return 0; +} diff --git a/spoj_cpp_clean/MKJUMPS/MKJUMPS-21.cpp b/spoj_cpp_clean/MKJUMPS/MKJUMPS-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7ef6e0c06f9eb7a204250a5b1a6e2858740b243 --- /dev/null +++ b/spoj_cpp_clean/MKJUMPS/MKJUMPS-21.cpp @@ -0,0 +1,57 @@ +/* + brute force > recursive backtracking + difficulty: easy + date: 14/Apr/2020 + problem: given an unweighted implicit graph, count the longest possible path + hint: DFS + backtracking + by: @brpapa +*/ +#include +using namespace std; + +const int MAX_N = 15; + +const int BLOCKED = -1; +const int UNVISITED = 0; +const int VISITED = 1; + +int board[MAX_N][MAX_N]; +int maxQtyVisited; + +void dfs(int r, int c, int qtyVisited) { + maxQtyVisited = max(maxQtyVisited, qtyVisited); + board[r][c] = VISITED; + + int dr[8] = {-2, -2, -1, -1, 1, 1, 2, 2}; + int dc[8] = {-1, 1, -2, 2, -2, 2, -1, 1}; + + for (int d = 0; d < 8; d++) { + int nr = r + dr[d]; + int nc = c + dc[d]; + + if (nr >= 0 && nc >= 0 && board[nr][nc] == UNVISITED) + dfs(nr, nc, qtyVisited+1); + } + board[r][c] = UNVISITED; // backtracking +} + +int main() { + int t = 1, N; + while (cin >> N && N) { + memset(board, BLOCKED, sizeof board); + maxQtyVisited = 0; + + int ans = 0; + for (int n = 0; n < N; n++) { + int s, e; cin >> s >> e; + for (int c = s; c < s+e; c++) + board[n][c] = UNVISITED, ans++; + } + + dfs(0, 0, 1); + ans -= maxQtyVisited; + printf("Case %d, %d %s can not be reached.\n", t++, ans, ans != 1? "squares":"square"); + } + + return 0; +} diff --git a/spoj_cpp_clean/MKTHNUM/MKTHNUM-14.cpp b/spoj_cpp_clean/MKTHNUM/MKTHNUM-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..056ec8c17a08c5ba2385b1e42ebfb6a656778223 --- /dev/null +++ b/spoj_cpp_clean/MKTHNUM/MKTHNUM-14.cpp @@ -0,0 +1,83 @@ +#include +using namespace std; + +#define ll long long + +struct node { + ll cnt; + node *left, *right; + + node(ll c, node *l, node *r) { + cnt = c; + left = l, right = r; + } + node() {} + + node *add(ll lo, ll hi, ll id); +}; + +node *null = new node(0, NULL, NULL); + +node *node::add(ll lo, ll hi, ll indx) + +{ + //cout << lo << " " << hi << " " << indx << endl; + if(lo <= indx && indx <= hi) { + if(lo == hi) + return new node(this -> cnt + 1, null, null); + + ll mid = (lo + hi) / 2; + + return new node(this -> cnt + 1, this -> left -> add(lo, mid, indx), this -> right -> add(mid + 1, hi, indx)); + } + + // Out of range, we can use previous tree node. + return this; +} + +ll query(node *a, node *b, ll lo, ll hi, ll k) + +{ + if(lo == hi) + return lo; + + ll mid = (lo + hi) / 2; + ll cnt = a -> left -> cnt - b -> left -> cnt; + if(cnt >= k) + return query(a -> left, b -> left, lo, mid, k); + else + return query(a -> right, b -> right, mid + 1, hi, k - cnt); +} + +ll a[100009], a2[100009], realVal[100009]; +unordered_map msk; +node *root[100009]; + +int main() + +{ + ll n, q; + cin >> n >> q; + for(ll i = 0; i < n; i++) { + scanf("%lld", &a[i]); + a2[i] = a[i]; + } + sort(a2, a2 + n); + + null -> left = null -> right = null; + for(ll i = 0; i < n; i++) { + msk[ a[i] ] = lower_bound(a2, a2 + n, a[i]) - a2; + realVal[msk[a[i]]] = a[i]; + root[i] = (i == 0 ? null : root[i - 1]) -> add(0, 100000, msk[a[i]]); + } + + for(ll i = 1; i<= q; i++) { + ll l, r, k; + scanf("%lld %lld %lld", &l, &r, &k); + l--, r--; + + printf("%lld\n", realVal[query(root[r], (l == 0 ? null : root[l - 1]), 0, 100000, k)]); + } + + return 0; +} diff --git a/spoj_cpp_clean/MKTHNUM/MKTHNUM-3.cpp b/spoj_cpp_clean/MKTHNUM/MKTHNUM-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e21d2e986998518991b43e039c04b07753ed109 --- /dev/null +++ b/spoj_cpp_clean/MKTHNUM/MKTHNUM-3.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct node{ + vector num; + int s; + + void assignLeaf(int value){ + num.push_back(value); + s = 1; + } + void merge(node& left, node& right){ + int i = 0, j = 0; + while(i < left.s and j < right.s){ + if(left.num[i] < right.num[j]){ + num.push_back(left.num[i]); + i++; + } + else{ + num.push_back(right.num[j]); + j++; + } + } + while(i < left.s){ + num.push_back(left.num[i]); + i++; + } + + while(j < right.s){ + num.push_back(right.num[j]); + j++; + } + s = num.size(); + } +}; + +int n, m, array[1<<17], x, y, z, found, cur, res; +node tree[1<<18]; + + +// l and r represent the range node i is responsible for. A single call init(1, 0, n-1) builds the segment tree in O(N) time +void init(int i, int l, int r){ + if(l == r){ + tree[i].assignLeaf(array[l]); + } + else{ + init(i*2, l, (l+r)/2); + init((i*2) + 1, ((l+r)/2) + 1, r); + node left = tree[i*2], right = tree[(i*2)+1]; + + tree[i].merge(left, right); //Merge the children into the parent + } + return; +} + +// Returns the no of numbers less than a given number in the range x to y. If that is equal to k-1 and if the given number lies in this range then that number is our answer. +int query(int i, int l, int r){ + if(x > r or y < l){ + return 0; + } + if(x <= l and y >= r){ + int lo = 0, hi = tree[i].s - 1, mid; + while(lo < hi){ + mid = (lo+hi)/2; + if(tree[i].num[mid] >= cur) + hi = mid; + else + lo = mid + 1; + } + if(tree[i].num[lo] == cur) + found = 1; + else if(tree[i].num[lo] < cur) + lo += 1; + return lo; + } + else{ + int mid = (l+r)/2; + return query(i*2, l, mid) + query((i*2)+1, mid+1,r); + } +} + +int main(){ + sd2(n,m); + for(int i = 0; i < n; i++) sd(array[i]); + + init(1, 0, n-1); + while(m--){ + sd3(x,y,z); + x--; + y--; + int lo = 0, hi = tree[1].s-1, mid; + while(lo <= hi){ + // We check if a given number is present in the queried range and if present + // wether the number of numbers smaller than it is equal to k-1. + found = 0; + mid = (lo+hi)/2; + cur = tree[1].num[mid]; + res = query(1, 0, n-1); + + if(res == (z-1) and found == 1){ + printf("%d\n",cur); + break; + } + else if(res > (z-1)){ + hi = mid - 1; + } + else{ + lo = mid + 1; + } + } + } + + return 0; +} diff --git a/spoj_cpp_clean/MKTHNUM/MKTHNUM-8.cpp b/spoj_cpp_clean/MKTHNUM/MKTHNUM-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0c99e1e379ffc2c6be8ebbcad0ee26596375756 --- /dev/null +++ b/spoj_cpp_clean/MKTHNUM/MKTHNUM-8.cpp @@ -0,0 +1,149 @@ +/*************************************************** + * Problem Name : MKTHNUM - K-th Number.cpp + * Problem Link : https://www.spoj.com/problems/MKTHNUM/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-03-07 + * Problem Type : Segment Tree + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("/home/saikat/Desktop/logN/input.txt", "r", stdin); +#define __FileWrite freopen ("/home/saikat/Desktop/logN/output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[MAX]; +vector >tree (MAX * 4); +vectorqr; +vector mergeTree (int left, int right) { + vectorvec ( (int) tree[left].size() + (int) tree[right].size() ); + merge (all (tree[left]), all (tree[right]), vec.begin() ); + return vec; +} + +void build (int nod, int low, int high) { + if (low == high) { + tree[nod].pb (ar[low]); + return; + } + + int mid = (high + low) / 2; + build (nod * 2, low, mid); + build (nod * 2 + 1, mid + 1, high); + tree[nod] = mergeTree (nod * 2, nod * 2 + 1); +} + +void query (int nod, int low, int high, int qlow, int qhigh) { + if (qlow > high || qhigh < low) { + return; + } + + if (qlow <= low && qhigh >= high) { + qr.pb (nod); + return; + } + + int mid = (high + low) / 2; + query (nod * 2, low, mid, qlow, qhigh); + query (nod * 2 + 1, mid + 1, high, qlow, qhigh); +} + +int binary (int l, int r, int n, int kth) { + int low = 0, high = n - 1; + int pos = 0; + + while (high >= low) { + int mid = (high + low) / 2; + int sum = 0; + + for (int i = 0; i < (int) qr.size(); i++) { + int nod = qr[i]; + int x = upper_bound (all (tree[nod]), tree[1][mid] - 1) - tree[nod].begin(); + sum += x; + } + + if (sum >= kth) { + high = mid - 1; + + } else { + pos = mid; + low = mid + 1; + } + } + + return pos; +} + + +int main () { + __FastIO; + int n, q; + cin >> n >> q; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + build (1, 0, n - 1); + + while (q--) { + int l, r, kth; + cin >> l >> r >> kth; + qr.clear(); + query (1, 0, n - 1, l - 1, r - 1); + int pos = binary (l - 1, r - 1, n, kth); + cout << tree[1][pos] << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/MKTHNUM/MKTHNUM-9.cpp b/spoj_cpp_clean/MKTHNUM/MKTHNUM-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7bd33e053b00006d38a3d22fa4c4210a4cca3bf --- /dev/null +++ b/spoj_cpp_clean/MKTHNUM/MKTHNUM-9.cpp @@ -0,0 +1,192 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +class PersistentSegmentTree +{ + struct node + { + long long data; + struct node *left; + struct node *right; + + node(int d = 0) + { + data = d; + left = right = nullptr; + } + + struct node *insert(int i, int j, int pos) + { + struct node *temp = new struct node(); + + if (i == j) + { + temp->data++; + return temp; + } + + int mid = (i + j) >> 1; + + if (pos <= mid) + { + temp->right = this->right; + temp->left = this->left->insert(i, mid, pos); + temp->data = temp->left->data + temp->right->data; + return temp; + } + + else + { + temp->left = this->left; + temp->right = this->right->insert(mid + 1, j, pos); + temp->data = temp->left->data + temp->right->data; + return temp; + } + } + + ~node() + { + if (this->left != nullptr) + delete this->left; + + if (this->right != nullptr) + delete this->right; + + delete this; + } + }; + + vector trees; + int range; + + void init(struct node **curr, int i, int j) + { + *curr = new struct node(); + + if (i == j) + return; + + int mid = (i + j) >> 1; + + init(&(*curr)->left, i, mid); + init(&(*curr)->right, mid + 1, j); + } + + int qq(struct node *i, struct node *j, int l, int r, int k) + { + if (l == r) + return l; + + int count = j->left->data - i->left->data; + int mid = (l + r) >> 1; + + if (count >= k) + return qq(i->left, j->left, l, mid, k); + + return qq(i->right, j->right, mid + 1, r, k - count); + } + + +public: + PersistentSegmentTree(int r) + { + struct node *temp; + range = r; + trees.push_back(temp); + init(&trees.back(), 1, range); + } + + inline void update(int pos) + { + trees.push_back(trees.back()->insert(1, range, pos)); + } + + inline int query(int i, int j, int k) + { + return qq(trees[i], trees[j], 1, range, k); + } + + ~PersistentSegmentTree() + { + int i; + + for (i = 0; i < trees.size(); i++) + delete trees[i]; + } +}; + +PersistentSegmentTree *T; +unordered_map MAP, RMAP; +int N, M; + +inline void init() +{ + int i, num, c = 1; + vector A, B; + + scanf("%d %d", &N, &M); + T = new PersistentSegmentTree(N); + + for (i = 0; i < N; i++) + { + scanf("%d", &num); + A.push_back(num); + B.push_back(num); + } + + sort(A.begin(), A.end()); + + MAP[A[0]] = 1; + RMAP[1] = A[0]; + c++; + + for (i = 1; i < A.size(); i++) + { + if (A[i] != A[i - 1]) + { + MAP[A[i]] = c; + RMAP[c] = A[i]; + c++; + } + } + + for (i = 0; i < B.size(); i++) + T->update(MAP[B[i]]); +} + +inline int solve() +{ + int i, j, k; + + scanf("%d %d %d", &i, &j, &k); + + return RMAP[T->query(i - 1, j, k)]; +} + +int main() +{ + int i; + + //freopen("input.txt", "r", stdin); + + init(); + + for (i = 0; i < M; i++) + printf("%d\n", solve()); + + return 0; +} diff --git a/spoj_cpp_clean/MMAXPER/MMAXPER-9.cpp b/spoj_cpp_clean/MMAXPER/MMAXPER-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..729aac0f26cd43541d0e6c7fb34e7dc4da77edb8 --- /dev/null +++ b/spoj_cpp_clean/MMAXPER/MMAXPER-9.cpp @@ -0,0 +1,51 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int memo[1010][1010]; +int a[1010], b[1010]; +int N; + +unsigned int f(int pos, int h) +{ + if(pos==N) + return 0; + + if(memo[pos][h]!=0) + return memo[pos][h]; + + if(pos==0) + memo[pos][h]=max(b[pos]+f(pos+1, a[pos]),a[pos]+f(pos+1, b[pos])); + else + memo[pos][h]=max(abs(a[pos]-h)+b[pos]+f(pos+1, a[pos]), abs(b[pos]-h)+a[pos]+f(pos+1, b[pos])); + return memo[pos][h]; +} + +int main() +{ + int i; + + //freopen("input.txt","r",stdin); + + scanf("%d", &N); + + for(i=0; i + +#define rep(i,n) for(i=0;i<(n);i++) + +int bigmod(int b, int p, int m) { + if(p == 0) return 1; + if(p == 1) return b%m; + if(p&1) return ((b%m) * bigmod(b,p-1,m)) % m; + else { + int sq = bigmod(b,p/2,m); + return (sq * sq) % m; + } +} + +int main() { + int n,res,i,x; + int primes[] = {2,3,167}; + int powers[] = {2,1,1}; + int MOD = 29; + while(scanf(" %d",&n) == 1) { + if(n == 0) break; + res = 1; + rep(i,3) { + x = (bigmod(primes[i],powers[i]*n+1,MOD) - 1 + MOD) % MOD; + res = (res * x) % MOD; + x = bigmod(primes[i]-1,MOD-2,MOD); + res = (res * x) % MOD; + } + printf("%d\n",res); + } + return 0; +} diff --git a/spoj_cpp_clean/MORSE/MORSE-9.cpp b/spoj_cpp_clean/MORSE/MORSE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e31118af7eaea45d8845ce0192391e1f8744e42 --- /dev/null +++ b/spoj_cpp_clean/MORSE/MORSE-9.cpp @@ -0,0 +1,141 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +string codex[]={"01", "1000", "1010", "100", "0", "0010", "110", "0000", "00", "0111", "101", "0100", "11", "10", "111", "0110", "1101", "010", "000", "1", "001", "0001", "011", "1001", "1011", "1100"}; + +int memo[10010]; +string code; + +int N; + +struct ver +{ + int words; + struct ver **edges; +}; + +ver *root=NULL; + +void initialize(ver **vertex) +{ + (*vertex)=new struct ver; + (*vertex)->words=0; + (*vertex)->edges=new struct ver *[2]; + (*vertex)->edges[0]=(*vertex)->edges[1]=NULL; +} + +void addword(ver **vertex, string word, int pos) +{ + if(*vertex==NULL) + initialize(vertex); + + if(pos==word.size()) + { + (*vertex)->words++; + return; + } + + char k=word[pos]; + + addword(&(*vertex)->edges[k-'0'], word, pos+1); +} + +void free_trie(ver **vertex) +{ + if((*vertex)==NULL) + return; + + free_trie(&(*vertex)->edges[0]); + free_trie(&(*vertex)->edges[1]); + delete *vertex; +} + +void query(ver *vertex, int start, int pos) +{ + if(vertex==NULL || pos>code.length()) + return; + + char k=code[pos]; + if(vertex->words!=0) + memo[pos]+=memo[start]*vertex->words; + + query(vertex->edges[k-'0'], start, pos+1); +} + +void iinit() +{ + char c; + string ctemp; + string temp; + int i, j; + + free_trie(&root); + root=NULL; + memset(memo, 0, sizeof(memo)); + code.clear(); + + memo[0]=1; + + cin>>code; + + for(i=0; i>temp; + ctemp.clear(); + + for(j=0; j Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define MOD 1000000007 +#define MAXN 300007 +#define ll long long +using namespace std; + +int dummy[MAXN]; +ll ans[MAXN]; +int N, L; +vector< pair > A; + +inline void init() +{ + int i; + + A.clear(); + memset(ans, 0, sizeof(ans)); + + scanf("%d %d", &N, &L); + + for (i = 0; i < N; i++) + scanf("%d", &dummy[i]); + + sort(dummy, dummy + N); + + for (i = 0; i < N; i++) + A.push_back(make_pair(dummy[i], i)); + + A.push_back(make_pair(MAXN * 2, N)); +} + +inline ll solve() +{ + vector< pair >::iterator ii; + int i; + + for (i = 0; i < N; i++) + { + ii = lower_bound(A.begin(), A.end(), make_pair(A[i].first + L, 0)); + ans[ii->second] += ans[A[i].second] * 2 + 1; + ans[ii->second] %= MOD; + } + + return ans[N] % MOD; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + int T; + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%lld\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/MPILOT/MPILOT-9.cpp b/spoj_cpp_clean/MPILOT/MPILOT-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65b36feb253550806116221fa17926b9733e43b2 --- /dev/null +++ b/spoj_cpp_clean/MPILOT/MPILOT-9.cpp @@ -0,0 +1,58 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +priority_queue Q; +vector< pair > A; + +int main() +{ + int N, i; + pair temp; + long long sum=0, tt; + + //freopen("input.txt","r",stdin); + + scanf("%d", &N); + + A.push_back(make_pair(0,0)); + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[500009], sz; +//bool c[1000000]; +set st; +int main() +{// 500000 + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //0, 1, 3,6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9 + // memset(c,false,sizeof(c)); + a[0]=0,a[1]=1,a[2]=3,a[3]=6,a[4]=2,a[5]=7,a[6]=13,a[7]=20; + FOR(i,0,8) + st.insert(a[i]); + FOR(i,8,500001) + { + m=a[i-1]-i; + if(m > 0 ) + { + sz=st.size(); + st.insert(m); + if(sz==st.size()) + a[i]=m+i+i, st.insert(m+i+i); + else + a[i]=m; + } + else + { + a[i]=m+i+i; + st.insert(m+i+i); + } + } + // s(T); + while(s(m)==1) + { + if(m==-1) + break; + printf("%d\n",a[m] ); + } + return 0; +} diff --git a/spoj_cpp_clean/MRECAMAN/MRECAMAN-7.cpp b/spoj_cpp_clean/MRECAMAN/MRECAMAN-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b5442e0a5e53a1fa7c98d9e6123ab88f2f05095 --- /dev/null +++ b/spoj_cpp_clean/MRECAMAN/MRECAMAN-7.cpp @@ -0,0 +1,36 @@ +#include +#define SIZE 500500 +using namespace std; + +set test; + +long long int mapa[SIZE]; + +void RecamansSequence(int max) +{ + for (long long int i = 1; i <= max; ++i) + { + if (mapa[i-1]-i>0 && (test.find(mapa[i-1]-i)==test.end()) ) + { + mapa[i]= mapa[i-1]-i; + }else + { + mapa[i]= mapa[i-1]+i; + } + test.insert(mapa[i]); + } +} + + + +int main() +{ + int n=0; + RecamansSequence(500000); + + while(scanf("%d",&n) && n!=-1) + { + printf("%lld\n",mapa[n] ); + } + return 0; +} diff --git a/spoj_cpp_clean/MRECAMAN/MRECAMAN-9.cpp b/spoj_cpp_clean/MRECAMAN/MRECAMAN-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9a74cd21a41f31de1c5f626c898228edc6a30c7 --- /dev/null +++ b/spoj_cpp_clean/MRECAMAN/MRECAMAN-9.cpp @@ -0,0 +1,52 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int memo[500007]; + +inline void pre() +{ + int i, prev = 0, curr; + unordered_map A; + + A[0] = 1; + + for(i = 1; i<= 500000; i++) + { + curr = prev - i; + if(curr < 0 || A.find(curr) != A.end()) + curr = prev + i; + + A[curr] = 1; + memo[i] = curr; + prev = curr; + } +} + +int main() +{ + int num; + + pre(); + scanf("%d", &num); + + while(num != -1) + { + printf("%d\n", memo[num]); + scanf("%d", &num); + } + + return 0; +} diff --git a/spoj_cpp_clean/MST/MST-21.cpp b/spoj_cpp_clean/MST/MST-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9bb3d42e04400472d8e12f5b58339d9d687a32c --- /dev/null +++ b/spoj_cpp_clean/MST/MST-21.cpp @@ -0,0 +1,63 @@ +/* + graphs > minimum spanning tree (MST) + difficulty: easy + date: 03/Jun/2020 + by: @brpapa +*/ +#include +using namespace std; +struct UFDS { + vector p; // p[i] = pai do item i + + UFDS(int N) { + p.resize(N); + for (int i = 0; i < N; i++) p[i] = i; + } + + int findSet(int i) { + if (p[i] == i) return i; + return p[i] = findSet(p[i]); + } + + bool isSameSet(int i, int j) { + return findSet(i) == findSet(j); + } + + void unionSet(int i, int j) { + int setI = findSet(i); + int setJ = findSet(j); + if (!isSameSet(setI, setJ)) p[setI] = setJ; + } +}; + +typedef long long ll; + +struct Tedge { + int u, v; ll w; + Tedge() {} + Tedge(int u, int v, ll w) : u(u), v(v), w(w) {} + bool operator<(const Tedge& p) const { return w < p.w; } +}; + +int main() { + int V, E; cin >> V >> E; + vector edgeList(E); + for (Tedge &e : edgeList) { + int u, v; cin >> u >> v; u--; v--; + ll w; cin >> w; + + e = Tedge(u, v, w); + } + + UFDS uf(V); + sort(edgeList.begin(), edgeList.end()); + + ll ans = 0; + for (Tedge e: edgeList) + if (!uf.isSameSet(e.u, e.v)) { + uf.unionSet(e.u, e.v); + ans += e.w; + } + cout << ans << endl; + return 0; +} diff --git a/spoj_cpp_clean/MSTICK/MSTICK-3.cpp b/spoj_cpp_clean/MSTICK/MSTICK-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a0f6c789c4195ba88706f14a332ce35294c0302 --- /dev/null +++ b/spoj_cpp_clean/MSTICK/MSTICK-3.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, n, l, w; + vector >array; + sd(t); + while(t--){ + sd(n); + array.clear(); + for(int i = 0; i < n; i++){ + sd2(l, w); + array.push_back(pair(l,w)); + } + + sort(array.begin(), array.end()); + + for(int i = 0; i < n; i++){ + printf("%d %d\n",array[i].first, array[i].second); + } + + + } + return 0; +} diff --git a/spoj_cpp_clean/MSUBSTR/MSUBSTR-19.cpp b/spoj_cpp_clean/MSUBSTR/MSUBSTR-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae9f3bf3008dda4222c98c596ba702e6a03b17b7 --- /dev/null +++ b/spoj_cpp_clean/MSUBSTR/MSUBSTR-19.cpp @@ -0,0 +1,71 @@ +// Author : @AdarshGaur + +#include +using namespace std; + +#ifdef cicada +#include "debug.h" +#else +#define debug(...) "Yume o akiramete shinde kure." +#endif + +#define int int64_t +#define endl "\n" +typedef pair pii; + +int const INF = 2e18; +int const mod = 1e9 +7; + + +void solve(){ + string s; + cin >> s; + + int n = s.size(), mx = 0, cnt = 0; + vector d1(n, 0), d2(n, 0); + for(int i=0, l=0, r=-1; i r) ? 1 : min(d1[l + (r-i)], r-i); + while(i+k=0 and s[i+k] == s[i-k]) + k++; + d1[i] = k--; + if(i +k > r){ + l = i - k; + r = i + k; + } + mx = max(mx, d1[i]*2 -1); + } + for(int i=0, l=0, r=-1; i r ? 0 : min(d2[l + (r-i+1)], r-i+1)) +1; + while(i+k-1 < n and i-k >=0 and s[i+k-1] == s[i-k]) + k++; + d2[i] = --k; + if(i + k-1 > r){ + l = i - k; + r = i + k -1; + } + mx = max(mx, d2[i]*2); + } + + for(int i=0; i> test; + for(int t=1; t<=test; t++){ + // cout<<"Case #"< +using namespace std; + +#define ll long long +const ll maxnodes = 10005; + +ll nodes = maxnodes, src, dest; +ll dist[maxnodes], q[maxnodes], work[maxnodes]; + +struct Edge +{ + ll to, rev; + ll f, cap; +}; + +vector g[maxnodes]; + +void addEdge(ll s, ll t, ll cap) +{ + Edge a = {t, g[t].size(), 0, cap}; + Edge b = {s, g[s].size(), 0, 0}; + g[s].push_back(a); + g[t].push_back(b); +} + +bool dinic_bfs() +{ + fill(dist, dist + nodes, -1); + + dist[src] = 0; + ll index = 0; + q[index++] = src; + + for (ll i = 0; i < index; i++) + { + ll u = q[i]; + for (ll j = 0; j < (ll) g[u].size(); j++) + { + Edge &e = g[u][j]; + if (dist[e.to] < 0 && e.f < e.cap) + { + dist[e.to] = dist[u] + 1; + q[index++] = e.to; + } + } + } + return dist[dest] >= 0; +} + +ll dinic_dfs(ll u, ll f) +{ + if (u == dest) + return f; + + for (ll &i = work[u]; i < (ll) g[u].size(); i++) + { + Edge &e = g[u][i]; + + if (e.cap <= e.f) continue; + + if (dist[e.to] == dist[u] + 1) + { + ll flow = dinic_dfs(e.to, min(f, e.cap - e.f)); + if (flow > 0) + { + e.f += flow; + g[e.to][e.rev].f -= flow; + return flow; + } + } + } + return 0; +} + +ll maxFlow(ll _src, ll _dest) +{ + src = _src; + dest = _dest; + ll result = 0; + while (dinic_bfs()) + { + fill(work, work + nodes, 0); + while (ll delta = dinic_dfs(src, INT_MAX)) + result += delta; + } + return result; +} + + +// addEdge(u, v, C); edge from u to v. Capacity is C +// maxFlow(s, t); max flow from s to t + +int main() + +{ + ll n; + cin >> n; + for(ll i = 1; i <= n; i++) { + char u, v; + ll c; + scanf(" %c %c %lld", &u, &v, &c); + addEdge((ll)u, (ll)v, c); + addEdge((ll)v, (ll)u, c); + } + + cout << maxFlow((ll)'A', (ll)'Z') << endl; + + return 0; +} diff --git a/spoj_cpp_clean/MTOTALF/MTOTALF-3.cpp b/spoj_cpp_clean/MTOTALF/MTOTALF-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83ef398bb6c159817724f2ea7acbaa719107f062 --- /dev/null +++ b/spoj_cpp_clean/MTOTALF/MTOTALF-3.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int n, capacity[125][125], flow, INF = 1000000; +char S = 'A', T = 'Z', s, t; +vector graph[125]; + +int find_path(){ + queue Q; + int visited[125]; memset(visited, 0, sizeof(visited)); + int from[125]; memset(from, -1, sizeof(from)); + int where, next, prev, flag = 1, path_cap = INF; + Q.push(S); visited[S] = 1; + while(!Q.empty() and flag == 1){ + where = Q.front(); + Q.pop(); + for(int i = 0; i < graph[where].size(); i++){ + next = graph[where][i]; + if(visited[next] != 1 and capacity[where][next] > 0){ + Q.push(next); + visited[next] = 1; + from[next] = where; + if(next == T){ + flag = 0; + break; + } + } + } + } + + where = T; + while(from[where] > -1){ + prev = from[where]; + path_cap = min(path_cap, capacity[prev][where]); + where = prev; + } + + where = T; + while(from[where] > -1){ + prev = from[where]; + capacity[prev][where] -= path_cap; + capacity[where][prev] -= path_cap; + where = prev; + } + + if(path_cap == INF) + return 0; + return path_cap; + +} + +int max_flow(){ + int result = 0, path_cap; + while(true){ + path_cap = find_path(); + if(path_cap == 0) + return result; + result += path_cap; + } + return result; +} + +int main(){ + sd(n); + memset(capacity, 0, sizeof(capacity)); + for(int i = 0; i < n; i++){ + scanf(" %c %c %d",&s,&t,&flow); + //cout< + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it) +#define meta __FUNCTION__,__LINE__ + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +using namespace std; + +const long double PI = 3.1415926535897932384626433832795; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void tr(S x, const Strings&... rest){cout< pii; + +int main(){ + + return 0; +} diff --git a/spoj_cpp_clean/MUL/MUL-14.cpp b/spoj_cpp_clean/MUL/MUL-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7ae41b46cce9685fe2f0ab401c33d9d6be85b57 --- /dev/null +++ b/spoj_cpp_clean/MUL/MUL-14.cpp @@ -0,0 +1,160 @@ +// #pragma GCC optimize("Ofast,unroll-loops") +// #pragma GCC target("avx,avx2,fma") + +#include +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define all(v) v.begin(), v.end() +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; ++i) +#define for0(i, stop) for(ll i = 0; i < stop; ++i) +#define rep1(i, start) for(ll i = start; i >= 1; --i) +#define rep0(i, start) for(ll i = (start-1); i >= 0; --i) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define EL '\n' +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +/*** + * Multiply (7x^2 + 8x^1 + 9x^0) with (6x^1 + 5x^0) + * ans = 42x^3 + 83x^2 + 94x^1 + 45x^0 + * A = {9, 8, 7} + * B = {5, 6} + * V = multiply(A,B) + * V = {45, 94, 83, 42} +***/ + +/*** Tricks + * Use vector < bool > if you need to check only the status of the sum + * Use bigmod if the power is over same polynomial && power is big + * Use long double if you need more precision + * Use long long for overflow +***/ +typedef vector vi; +const double PI = 2.0 * acos(0.0); +using cd = complex; + +void fft(vector & a, bool invert = 0) + +{ + int n = a.size(); + + for (int i = 1, j = 0; i < n; i++) { + int bit = n >> 1; + for (; j & bit; bit >>= 1) + j ^= bit; + j ^= bit; + + if (i < j) + swap(a[i], a[j]); + } + + for (int len = 2; len <= n; len <<= 1) { + double ang = 2 * PI / len * (invert ? -1 : 1); + cd wlen(cos(ang), sin(ang)); + for (int i = 0; i < n; i += len) { + cd w(1); + for (int j = 0; j < len / 2; j++) { + cd u = a[i+j], v = a[i+j+len/2] * w; + a[i+j] = u + v; + a[i+j+len/2] = u - v; + w *= wlen; + } + } + } + + if (invert) { + for (cd & x : a) + x /= n; + } +} + +void ifft(vector & p) + +{ + fft(p, 1); +} + +vi multiply(vi const& a, vi const& b) +{ + + vector fa(a.begin(), a.end()), fb(b.begin(), b.end()); + int n = 1; + while (n < a.size() + b.size()) + n <<= 1; + fa.resize(n); + fb.resize(n); + + fft(fa); + fft(fb); + for (int i = 0; i < n; i++) + fa[i] *= fb[i]; + ifft(fa); + + vi result(n); + for (int i = 0; i < n; i++) + result[i] = round(fa[i].real()); + + return result; +} + +string s1, s2; +vi num1, num2, res; + +int main() +{ + fastio; + ll t; + cin >> t; + while(t--) { + cin >> s1 >> s2; + reverse(all(s1)), reverse(all(s2)); + + num1.reserve(s1.length()); + for(char &ch: s1) num1.pb(ch-'0'); + + num2.reserve(s2.length()); + for(char &ch : s2) num2.pb(ch-'0'); + + res = multiply(num1, num2); + + ll carry = 0; + for(ll &val : res) { + val += carry; + + carry = val / 10; + val %= 10; + } + + bool f = 0; + if(carry) f = 1, cout << carry; + + for(ll i = (ll)res.size() - 1; i >= 0; i--) { + if(!f && res[i] == 0 && i > 0) + continue; + + cout << res[i]; + f = 1; + } + cout << EL; + + num1.clear(), num2.clear(); + res.clear(); + } + + return 0; +} diff --git a/spoj_cpp_clean/MULTQ3/MULTQ3-12.cpp b/spoj_cpp_clean/MULTQ3/MULTQ3-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9019db9d46063ffb3030d35c86cfd50c6dbdbb0 --- /dev/null +++ b/spoj_cpp_clean/MULTQ3/MULTQ3-12.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#define MAXN 110000 +#define mp make_pair + +using namespace std; + +struct segment { + int a, b, c; + + segment(int a = 0, int b = 0, int c = 0) + : a(a) + , b(b) + , c(c) + { + } + + segment operator+(segment s) + { + return segment(a + s.a, b + s.b, c + s.c); + } + + void swap() + { + a = a ^ b; + b = a ^ b; + a = a ^ b; + + a = a ^ c; + c = a ^ c; + a = a ^ c; + } +}; + +segment tree[5 * MAXN]; +int lazy[5 * MAXN]; + +int readInt() +{ + bool minus = false; + int result = 0; + char ch = getchar_unlocked(); + while (true) { + if (ch == '-') + break; + if (ch >= '0' && ch <= '9') + break; + ch = getchar_unlocked(); + } + if (ch == '-') + minus = true; + else + result = ch - '0'; + while (true) { + ch = getchar_unlocked(); + if (ch < '0' || ch > '9') + break; + result = result * 10 + (ch - '0'); + } + if (minus) + return -result; + else + return result; +} + +void build(int node, int tl, int tr) +{ + if (tl == tr) // leaf + { + tree[node] = segment(1, 0, 0); + } else { + int tm = (tl + tr) >> 1; + build(2 * node, tl, tm); + build(2 * node + 1, tm + 1, tr); + tree[node] = tree[2 * node] + tree[2 * node + 1]; + } +} + +void lazy_propagate(int node) +{ + lazy[node] %= 3; + for (int i = 0; i < lazy[node]; i++) + tree[node].swap(); + + lazy[2 * node] += lazy[node]; + lazy[2 * node + 1] += lazy[node]; + lazy[node] = 0; +} + +segment propagated_node(int node) +{ + if (lazy[node]) + lazy_propagate(node); + return tree[node]; +} + +int query(int node, int tl, int tr, int l, int r) +{ + if (l > r) + return 0; + if (tl >= l && tr <= r) { + if (lazy[node]) + lazy_propagate(node); + return tree[node].a; + } else { + //if this node is not propagated yet, propagate it + if (lazy[node]) + lazy_propagate(node); + + int tm = (tl + tr) >> 1; + return query(2 * node, tl, tm, l, min(tm, r)) + query(2 * node + 1, tm + 1, tr, max(l, tm + 1), r); + } +} + +void update(int node, int tl, int tr, int i, int j, int value) +{ + // current segment is not within range [i, j] + if (tl > tr || tl > j || tr < i) + return; + + if (tl >= i && tr <= j) { + lazy[node] += value; + return; + } else // not a leaf node + { + int tm = (tl + tr) >> 1; + update(2 * node, tl, tm, i, j, value); // updating left child + update(2 * node + 1, tm + 1, tr, i, j, value); // updating right child + //inner node will receive values from propagated children + tree[node] = propagated_node(2 * node) + propagated_node(2 * node + 1); + } +} + +int main() +{ + int n, m, a, b, opc; + n = readInt(); + build(1, 0, n - 1); + m = readInt(); + //no need to initialize lazy, since it is just one test case at once + for (int i = 0; i < m; i++) { + opc = readInt(); + a = readInt(); + b = readInt(); + if (opc) { + int resp = query(1, 0, n - 1, a, b); + printf("%d\n", resp); + } else { + update(1, 0, n - 1, a, b, 1); + } + } + return 0; +} diff --git a/spoj_cpp_clean/MULTQ3/MULTQ3-14.cpp b/spoj_cpp_clean/MULTQ3/MULTQ3-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51396c8a9b9b626969fce8f927b0f66718fabde4 --- /dev/null +++ b/spoj_cpp_clean/MULTQ3/MULTQ3-14.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + +#define ll long long + +struct node { + ll d0; + ll d1; + ll d2; +} tree[400009]; +ll prop[400009]; + +void init(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + tree[node].d0 = 1; + tree[node].d1 = 0; + tree[node].d2 = 0; + prop[node] = 0; + return; + } + + ll mid = (lo + hi) / 2; + init(lo, mid, 2 * node); + init(mid + 1, hi, 2 * node + 1); + tree[node].d0 = tree[2 * node].d0 + tree[2 * node + 1].d0; + tree[node].d1 = tree[2 * node].d1 + tree[2 * node + 1].d1; + tree[node].d2 = tree[2 * node].d2 + tree[2 * node + 1].d2; + prop[node] = prop[2 * node] + prop[2 * node + 1]; +} + +void update(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lo > right || hi < left) + return; + if(lo >= left && hi <= right) { + prop[node] = (prop[node] + 1) % 3; + int tmp0 = tree[node].d0, tmp1 = tree[node].d1, tmp2 = tree[node].d2; + tree[node].d0 = tmp2; + tree[node].d1 = tmp0; + tree[node].d2 = tmp1; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, left, right, 2 * node); + update(mid + 1, hi, left, right, 2 * node + 1); + + tree[node].d0 = tree[2 * node].d0 + tree[2 * node + 1].d0; + tree[node].d1 = tree[2 * node].d1 + tree[2 * node + 1].d1; + tree[node].d2 = tree[2 * node].d2 + tree[2 * node + 1].d2; + + switch(prop[node]) { + int tmp0, tmp1, tmp2; + case 1: + tmp0 = tree[node].d0, tmp1 = tree[node].d1, tmp2 = tree[node].d2; + tree[node].d0 = tmp2; + tree[node].d1 = tmp0; + tree[node].d2 = tmp1; + break; + case 2: + tmp0 = tree[node].d0, tmp1 = tree[node].d1, tmp2 = tree[node].d2; + tree[node].d0 = tmp1; + tree[node].d1 = tmp2; + tree[node].d2 = tmp0; + break; + } +} + +ll query(ll lo, ll hi, ll left, ll right, ll carry, ll node) + +{ + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) { + switch(carry) { + case 0: + return tree[node].d0; + case 1: + return tree[node].d2; + case 2: + return tree[node].d1; + } + } + + ll mid = (lo + hi) / 2; + carry = (carry + prop[node]) % 3; + ll p1 = query(lo, mid, left, right, carry, 2 * node); + ll p2 = query(mid + 1, hi, left, right, carry, 2 * node + 1); + + return p1 + p2; +} + +int main() + +{ + ll n, m; + scanf("%lld %lld", &n, &m); + init(0, n - 1, 1); + + while(m--) { + ll state, p, q; + scanf("%lld %lld %lld", &state, &p, &q); + if(state == 0) { + update(0, n - 1, p, q, 1); + //cout << 5; + } + else { + printf("%lld\n", query(0, n - 1, p, q, 0, 1)); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/MULTQ3/MULTQ3-8.cpp b/spoj_cpp_clean/MULTQ3/MULTQ3-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12eb5f70f5c140e7be0703c8de66b79d941c012f --- /dev/null +++ b/spoj_cpp_clean/MULTQ3/MULTQ3-8.cpp @@ -0,0 +1,194 @@ +/*************************************************** + * Problem Name : MULTQ3 - Multiples of 3.cpp + * Problem Link : https://www.spoj.com/problems/MULTQ3/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-09-27 + * Problem Type : Data Structure (Segment Tree with lazy) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[MAX], lazy[MAX * 3]; +int zero[MAX * 3], one[MAX * 3], two[MAX * 3]; +void build (int nod, int low, int high) { + if (low > high) return ; + + if (low == high) { + zero[nod] = 1; + return ; + } + + int mid = (high + low) / 2; + int left = nod * 2; + int right = nod * 2 + 1; + build (left, low, mid); + build (right, mid + 1, high); + zero[nod] = zero[left] + zero[right]; + one[nod] = one[left] + one[right]; + two[nod] = two[left] + two[right]; +} +int query (int nod, int low, int high, int qlow, int qhigh) { + if (low > high || low > qhigh || high < qlow) { + return 0; + } + + if (lazy[nod] != 0) { + if (lazy[nod] == 1) { + int zz = two[nod]; + two[nod] = zero[nod]; + zero[nod] = one[nod]; + one[nod] = zz; + + } else { + int zz = two[nod]; + two[nod] = one[nod]; + one[nod] = zero[nod]; + zero[nod] = zz; + } + + if (low != high) { + lazy[nod * 2] += lazy[nod]; + lazy[nod * 2] %= 3; + lazy[nod * 2 + 1] += lazy[nod]; + lazy[nod * 2 + 1] %= 3; + } + + lazy[nod] = 0; + } + + if (low >= qlow && high <= qhigh) { + return zero[nod]; + } + + int mid = (high + low) / 2; + int left = nod * 2; + int right = nod * 2 + 1; + int q1 = query (left, low, mid, qlow, qhigh); + int q2 = query (right, mid + 1, high, qlow, qhigh); + return q1 + q2; +} +void update (int nod, int low, int high, int qlow, int qhigh, int val) { + if (low > high) return ; + + if (lazy[nod] != 0) { + if (lazy[nod] == 1) { + int zz = two[nod]; + two[nod] = zero[nod]; + zero[nod] = one[nod]; + one[nod] = zz; + + } else { + int zz = two[nod]; + two[nod] = one[nod]; + one[nod] = zero[nod]; + zero[nod] = zz; + } + + if (low != high) { + lazy[nod * 2] += lazy[nod]; + lazy[nod * 2] %= 3; + lazy[nod * 2 + 1] += lazy[nod]; + lazy[nod * 2 + 1] %= 3; + } + + lazy[nod] = 0; + } + + if (low > qhigh || high < qlow) return ; + + if (low >= qlow && high <= qhigh) { + int zz = two[nod]; + two[nod] = zero[nod]; + zero[nod] = one[nod]; + one[nod] = zz; + + if (low != high) { + lazy[nod * 2] += val; + lazy[nod * 2] %= 3; + lazy[nod * 2 + 1] += val; + lazy[nod * 2 + 1] %= 3; + } + + return; + } + + int mid = (high + low) / 2; + int left = nod * 2; + int right = nod * 2 + 1; + update (left, low, mid, qlow, qhigh, val); + update (right, mid + 1, high, qlow, qhigh, val); + zero[nod] = zero[left] + zero[right]; + one[nod] = one[left] + one[right]; + two[nod] = two[left] + two[right]; +} +int main () { + //~ __FastIO; + int n, q; + scanf ("%d %d", &n, &q); + build (1, 0, n - 1); + + while (q--) { + int c, x, y; + scanf ("%d %d %d", &c, &x, &y); + + if (!c) { + update (1, 0, n - 1, x, y, 1); + + } else { + printf ("%d\n", query (1, 0, n - 1, x, y) ); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/NAJPF/NAJPF-0.cpp b/spoj_cpp_clean/NAJPF/NAJPF-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd0d8b492a98ae2c53d0b481625f800113e01ab9 --- /dev/null +++ b/spoj_cpp_clean/NAJPF/NAJPF-0.cpp @@ -0,0 +1,90 @@ +#include + +typedef long long ll; + +static inline void setio(void); + +template +struct KMP { + typedef long long ll; + + const ll n; + + /* kmp table. */ + std::vector p; + const Sequence needle; + + KMP(const Sequence& _needle) : needle(_needle), n(_needle.size()) { + /* compute the table */ + p.assign(n+1, 0); + p[0] = -1; p[1] = 0; + for (ll i = 2, k = 0; i <= n; ++i) { + while (k > 0 && needle[k] != needle[i-1]) + k = p[k]; + p[i] = needle[k] == needle[i-1] ? ++k : 0; + } + } + + std::vector find_occurences_in(const Sequence& haystack) const { + std::vector locs; + actual_kmp_search(haystack, true, locs); + return locs; + } + + ll count_occurences_in(const Sequence& haystack) const { + return actual_kmp_search(haystack); + } + +private: + + /* search for the needle in the haystack. */ + ll actual_kmp_search(const Sequence& haystack, const bool& fill_vec = false, std::vector& locs = std::vector()) const { + ll cnt = 0; + for (ll i = 0, k = 0; i < haystack.size(); ++i) { + while (k > 0 && needle[k] != haystack[i]) + k = p[k]; + if (needle[k] == haystack[i]) + ++k; + if (k == needle.size()) { + if (fill_vec) locs.push_back(i); + ++cnt; + k = p[k]; + } + } + return cnt; + } +}; + +static void one_case(void) { + std::string A,B; + std::cin >> A >> B; + + KMP pat(B); + + std::vector ret = pat.find_occurences_in(A); + if (ret.empty()) { + std::cout << "Not Found\n"; + } else { + std::cout << ret.size() << "\n"; + for (const auto& pos : ret) + std::cout << pos - B.length() + 2 << " "; + std::cout << "\n"; + } +} + +int main(void) { + setio(); + + ll n; std::cin >> n; + while (n--) { + one_case(); + std::cout << "\n"; + } + + return 0; +} + +static inline void setio(void) { + std::ios::sync_with_stdio(false);std::cin.tie(nullptr); + std::cout.precision(10); std::cout << std::fixed; +} \ No newline at end of file diff --git a/spoj_cpp_clean/NAKANJ/NAKANJ-14.cpp b/spoj_cpp_clean/NAKANJ/NAKANJ-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39687adc52e7ccbef7679efaab478c356d666778 --- /dev/null +++ b/spoj_cpp_clean/NAKANJ/NAKANJ-14.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; + +struct node { + int col; + int row; + int dist; + + node(int c, int r, int d) { + col = c; + row = r; + dist = d; + } +}; + +int main() + +{ + int n; + scanf("%d", &n); + char s1[3], s2[3]; + int rowset[] = {2, 2, -2, -2, 1, 1, -1, -1}; + int colset[] = {1, -1, 1, -1, 2, -2, 2, -2}; + while(n--) { + scanf("%s %s", s1, s2); + int row1 = s1[1] - '0', col1 = s1[0] - 'a' + 1; + int row2 = s2[1] - '0', col2 = s2[0] - 'a' + 1; + if(row1 == row2 && col1 == col2) { + printf("0\n"); + continue; + } + + bool visit[9][9] = {}; + queue q; + node temp(col1, row1, 0); + q.push(temp); + + while(!q.empty()) { + node u = q.front(); + q.pop(); + + for(int i = 0; i < 8; i++) { + int rowtemp = u.row + rowset[i]; + int coltemp = u.col + colset[i]; + + if(rowtemp >= 1 && rowtemp <= 8) { + if(coltemp >= 1 && coltemp <= 8) { + if(visit[rowtemp][coltemp] == 0) { + if(rowtemp == row2 && coltemp == col2) { + printf("%d\n", u.dist + 1); + goto over; + } + + else { + q.push(node(coltemp, rowtemp, u.dist + 1)); + } + } + } + } + + } + + } + + + over:; + + } + + return 0; +} diff --git a/spoj_cpp_clean/NAKANJ/NAKANJ-7.cpp b/spoj_cpp_clean/NAKANJ/NAKANJ-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9844e9059f10c6ceea02b6aac0ea323e94569247 --- /dev/null +++ b/spoj_cpp_clean/NAKANJ/NAKANJ-7.cpp @@ -0,0 +1,89 @@ +#include +#define X first +#define Y second + +using namespace std; + +int MovX[]= {2,-2,1,-1,2,-2,1,-1}; +int MovY[]= {1,1,2,2,-1,-1,-2,-2}; + +int grafo[8][8]; +int dist[8][8]; +bool visit[8][8]; + +bool esValido(int x, int y) +{ + return (x>0 && x<=8 && y>0 && y<=8)? true: false; +} + + +void bfs(int sx, int sy) +{ + queue< pair > q; + dist[sx][sy]=0; + q.push( make_pair(sx,sy) ); + visit[sx][sy]= true; + while(!q.empty()) + { + pair u= q.front(); q.pop(); + for (int i = 0; i < 8; ++i) + { + int nuevoX= u.X+ MovX[i]; + int nuevoY= u.Y + MovY[i]; + if (esValido(nuevoX,nuevoY) && !visit[nuevoX][nuevoY] && dist[u.X][u.Y] + 1 < dist[nuevoX][nuevoY]) + { + visit[u.X][u.Y]= true; + dist[nuevoX][nuevoY]= dist[u.X][u.Y]+ 1; + q.push( make_pair(nuevoX, nuevoY) ); + } + } + } +} + + +int main() +{ + int t; + char a[3], b[3]; + map m; + + m['a']=m['1']=1; + m['b']=m['2']=2; + m['c']=m['3']=3; + m['d']=m['4']=4; + m['e']=m['5']=5; + m['f']=m['6']=6; + m['g']=m['7']=7; + m['h']=m['8']=8; + + scanf("%d",&t); + while(t--) + { + for (int i = 1; i <= 8; ++i) + { + for (int j = 1; j <= 8; ++j) + { + dist[i][j]=2000000; + visit[i][j]= 0; + } + } + scanf("%s %s",a,b); + bfs(m[a[0]],m[a[1]]); + printf("%d\n", dist[ m[b[0]] ][ m[b[1]]]); + //printf("desde %d %d\n", m[a[0]],m[a[1]]); + + //printf("hasta %d %d es %d \n", m[b[0]],m[b[1]], dist[ m[b[0]] ][ m[b[1]]] ); + /* + for (int i = 1; i <= 8; ++i) + { + for (int j = 1; j <= 8; ++j) + { + printf("%d ",dist[i][j]); + } + printf("\n"); + } + */ + } + + return 0; +} diff --git a/spoj_cpp_clean/NAKANJ/NAKANJ-9.cpp b/spoj_cpp_clean/NAKANJ/NAKANJ-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e44b393409dc2f60816cc923911f6607249700de --- /dev/null +++ b/spoj_cpp_clean/NAKANJ/NAKANJ-9.cpp @@ -0,0 +1,95 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#define gc() getchar_unlocked(); +#define N 8 + +using namespace std; + +int dx[] = {-1, 1, 2, 2, 1, -1, -2, -2}; +int dy[] = {-2, -2, -1, 1, 2, 2, 1, -1}; +pair start, finish; + +inline bool in_range(pair pos) +{ + if(pos.first <1 || pos.first > 8 || pos.second < 1 || pos.second > 8) + return 0; + return 1; +} + +inline void init() +{ + char a, b, c, d; + + a=gc(); + b=gc(); + gc(); + c=gc(); + d=gc(); + gc(); + + start.first = b-'0'; + start.second = a - 'a' + 1; + + finish.first = d - '0'; + finish.second = c - 'a' + 1; + +} + +int solve() +{ + deque< pair > > Q; + pair > curr, temp; + int i; + + Q.push_back(make_pair(0, start)); + + while(!Q.empty()) + { + curr = Q.front(); + Q.pop_front(); + + if(curr.second == finish) + return curr.first; + if(!in_range(curr.second)) + continue; + + for(i=0; i<8; i++) + { + temp = curr; + temp.first++; + temp.second.first += dx[i]; + temp.second.second += dy[i]; + if(in_range(temp.second)) + Q.push_back(temp); + } + } + + return 1000; +} + +int main() +{ + int T; + + //freopen("input.txt","r",stdin); + + scanf("%d\n", &T); + while(T--) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/spoj_cpp_clean/NARYTREE/NARYTREE-8.cpp b/spoj_cpp_clean/NARYTREE/NARYTREE-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d02b24bdf46d0b5ac5ae868ea813d16da3aa522f --- /dev/null +++ b/spoj_cpp_clean/NARYTREE/NARYTREE-8.cpp @@ -0,0 +1,124 @@ +/*************************************************** + * Problem Name : NARYTREE - N-ARY TREE.cpp + * Problem Link : https://www.spoj.com/problems/NARYTREE/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2020-04-18 + * Problem Type : adhoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + ll m, n; + cin >> tc; + + while (tc--) { + cin >> m >> n; + int cnt = 1; + ll val = 1; + ll sum = 1; + + while (sum < m) { + cnt++; + val = val * n; + sum += val; + } + + if (m == 0) cnt = 0; + + cout << cnt << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/NDS/NDS-9.cpp b/spoj_cpp_clean/NDS/NDS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79c79d2b031356597609d04bd70a874b75d4ae80 --- /dev/null +++ b/spoj_cpp_clean/NDS/NDS-9.cpp @@ -0,0 +1,83 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar_unlocked() +using namespace std; + +int N, K; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +inline int solve() +{ + vector V; + vector::iterator ii; + + int i, num; + + N = FAST_IO(); + num = FAST_IO(); + V.push_back(num); + + for (i = 0; i < N - 1; i++) + { + num = FAST_IO(); + + if (num > V.back()) + V.push_back(num); + + else + { + ii = lower_bound(V.begin(), V.end(), num); + *ii = num; + } + } + + K = FAST_IO(); + + if (K > V.size()) + return -1; + + return V[K - 1]; +} + +int main() +{ + int T; + + T = FAST_IO(); + + while (T--) + { + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/NEG2/NEG2-4.cpp b/spoj_cpp_clean/NEG2/NEG2-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28990684a5fb17d2e246bcd2efb58edd79dc42b0 --- /dev/null +++ b/spoj_cpp_clean/NEG2/NEG2-4.cpp @@ -0,0 +1,111 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +long long n, m ; +string negabinary() +{ + int remainder; + string digits; + while(n!=0) + { + remainder = n% -2; + n/=-2; + if(remainder < 0) + { + n++; + remainder+=2; + } + digits.push_back(remainder+'0'); + } + reverse(digits.begin(), digits.end()); + return digits; +} +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + cin>>n; + if(n==0) + cout<<"0"; + else + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +#define gc() getchar_unlocked() + +using namespace std; + +unsigned char mask[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; +#define tget(i) ( (t[(i)/8]&mask[(i)%8]) ? 1 : 0 ) +#define tset(i, b) t[(i)/8]=(b) ? (mask[(i)%8]|t[(i)/8]) : ((~mask[(i)%8])&t[(i)/8]) +#define chr(i) (cs==sizeof(int)?((int*)s)[i]:((unsigned char *)s)[i]) +#define isLMS(i) (i>0 && tget(i) && !tget(i-1)) + +// find the start or end of each bucket +inline void getBuckets(unsigned char *s, int *bkt, int n, int K, int cs, bool end) { + int i, sum = 0; + for (i = 0; i <= K; i++) + bkt[i] = 0; // clear all buckets + for (i = 0; i < n; i++) + bkt[chr(i)]++; // compute the size of each bucket + for (i = 0; i <= K; i++) { + sum += bkt[i]; + bkt[i] = end ? sum : sum - bkt[i]; + } +} +// compute SAl +inline void induceSAl(unsigned char *t, int *SA, unsigned char *s, int *bkt, int n, int K, int cs, bool end) { + int i, j; + getBuckets(s, bkt, n, K, cs, end); // find starts of buckets + for (i = 0; i < n; i++) { + j = SA[i] - 1; + if (j >= 0 && !tget(j)) + SA[bkt[chr(j)]++] = j; + } +} +// compute SAs +inline void induceSAs(unsigned char *t, int *SA, unsigned char *s, int *bkt, int n, int K, int cs, bool end) { + int i, j; + getBuckets(s, bkt, n, K, cs, end); // find ends of buckets + for (i = n - 1; i >= 0; i--) { + j = SA[i] - 1; + if (j >= 0 && tget(j)) + SA[--bkt[chr(j)]] = j; + } +} + +// find the suffix array SA of s[0..n-1] in {1..K}^n +// require s[n-1]=0 (the sentinel!), n>=2 +// use a working space (excluding s and SA) of at most 2.25n+O(1) for a constant alphabet +inline void SA_IS(unsigned char *s, int *SA, int n, int K, int cs) { + int i, j; + unsigned char *t = (unsigned char *)malloc(n / 8 + 1); // LS-type array in bits + // Classify the type of each character + tset(n - 2, 0); + tset(n - 1, 1); // the sentinel must be in s1, important!!! + for (i = n - 3; i >= 0; i--) + tset(i, (chr(i) 0 && (isLMS(pos + d) || isLMS(prev + d))) + break; + if (diff) { + name++; + prev = pos; + } + pos = (pos % 2 == 0) ? pos / 2 : (pos - 1) / 2; + SA[n1 + pos] = name - 1; + } + for (i = n - 1, j = n - 1; i >= n1; i--) + if (SA[i] >= 0) + SA[j--] = SA[i]; + // stage 2: solve the reduced problem + // recurse if names are not yet unique + int *SA1 = SA, *s1 = SA + n - n1; + if (name < n1) + SA_IS((unsigned char*)s1, SA1, n1, name - 1, sizeof(int)); + else + // generate the suffix array of s1 directly + for (i = 0; i < n1; i++) + SA1[s1[i]] = i; + // stage 3: induce the result for the original problem + bkt = (int *)malloc(sizeof(int) * (K + 1)); // bucket array + // put all left-most S characters into their buckets + getBuckets(s, bkt, n, K, cs, true); // find ends of buckets + for (i = 1, j = 0; i < n; i++) + if (isLMS(i)) + s1[j++] = i; // get p1 + for (i = 0; i < n1; i++) + SA1[i] = s1[SA1[i]]; // get index in s + for (i = n1; i < n; i++) + SA[i] = -1; // init SA[n1..n-1] + for (i = n1 - 1; i >= 0; i--) { + j = SA[i]; + SA[i] = -1; + SA[--bkt[chr(j)]] = j; + } + induceSAl(t, SA, s, bkt, n, K, cs, false); + induceSAs(t, SA, s, bkt, n, K, cs, true); + free(bkt); + free(t); +} + +const int maxn = 200000; +int sa[maxn]; +unsigned char *s; +int n; +string str; + +inline void getstring(string &A) +{ + char c; + + A.clear(); + + c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +inline bool lessoreq(int pos, string &A) +{ + int c = 0; + + while (pos + c < str.length() && c < A.length() && str[pos + c] == A[c]) + c++; + + if (pos + c == str.length()) + return 1; + + if (c == A.length()) + return 0; + + return str[pos + c] <= A[c]; +} + +inline int myupperbound(string &A) +{ + int lo = 0, hi = n + 1, mid; + + while (lo != hi - 1) + { + mid = (lo + hi) / 2; + + if (lessoreq(sa[mid], A)) + lo = mid; + else + hi = mid; + } + + return hi; +} + +inline void init() +{ + getstring(str); + n = str.size(); + s = (unsigned char*)str.c_str(); + SA_IS(s, sa, n + 1, 256, 1); +} + +inline void solve() +{ + string temp; + int pos, i, myn; + + + getstring(temp); + if (str.length() == 1) + if (temp < str) + { + putchar(str[0]); + putchar('\n'); + return; + } + + pos = myupperbound(temp); + + if (pos == n + 1) + { + printf("-1\n"); + return; + } + + myn = min(str.length() - sa[pos], temp.length()); + + for (i = 0; i < myn; i++) + { + if (str[sa[pos] + i] != temp[i]) + { + putchar(str[sa[pos] + i]); + putchar('\n'); + return; + } + putchar(str[sa[pos] + i]); + } + + putchar(str[sa[pos] + i]); + putchar('\n'); +} + +int main() +{ + int Q, i; + + //freopen("input.txt","r", stdin); + + init(); + scanf("%d", &Q); + + for (i = 0; i < Q; i++) + solve(); + + return 0; +} diff --git a/spoj_cpp_clean/NFACTOR/NFACTOR-14.cpp b/spoj_cpp_clean/NFACTOR/NFACTOR-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af2fd6ece7b7e19f2afeb87a0c51afdbff601b16 --- /dev/null +++ b/spoj_cpp_clean/NFACTOR/NFACTOR-14.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +#define siz 1000000 + +int coun[siz + 5]; +char mark[siz + 5]; + +struct ab { + int pf[11]; +}num[siz + 5]; + +int main() + +{ + for(int i = 2; i <= siz; i++) { + if(mark[i] == '\0') { + for(int j = i + i; j <= siz; j += i) { + mark[j] = '1'; + coun[j]++; + } + coun[i]++; + } + } + + for(int i = 1; i <= siz; i++) { + for(int j = 1; j <= 7; j++) + num[i].pf[j] = num[i - 1].pf[j]; + + num[i].pf[coun[i]]++; + } + + int T, a, b, n; + scanf("%d", &T); + while(T--) { + scanf("%d %d %d", &a, &b, &n); + + if(n == 0 && a == 1) { + printf("1\n"); + } + + else { + printf("%d\n", num[b].pf[n] - num[a - 1].pf[n]); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/NFACTOR/NFACTOR-3.cpp b/spoj_cpp_clean/NFACTOR/NFACTOR-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cca2a1c84bae8e84d81b887730f4818e2cb36c58 --- /dev/null +++ b/spoj_cpp_clean/NFACTOR/NFACTOR-3.cpp @@ -0,0 +1,79 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it) +#define meta __FUNCTION__,__LINE__ + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +using namespace std; + +const long double PI = 3.1415926535897932384626433832795; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void tr(S x, const Strings&... rest){cout< pii; + +const int N = 1001000; +const int M = 11; + +int sieve[N], primes[N], pcnt; + +void getSieve(){ + for(int i = 2; i < N; i++){ + if(sieve[i] == 0){ + sieve[i] = i; primes[pcnt++] = i; + } + for(int j = 0; j < pcnt and primes[j] <= sieve[i] and i*primes[j] < N; j++) sieve[i*primes[j]] = primes[j]; + } + return; +} + +int dp[N][M]; + +int main(){ + getSieve(); + + for(int i = 1; i < N; i++){ + int d = 0, j = i; + while(j > 1){ + int cur = sieve[j]; d++; + while(cur == sieve[j] and j > 1){ + j /= sieve[j]; + } + } + if(d < M) + dp[i][d]++; + } + + for(int i = 1; i < N; i++){ + for(int j = 0; j < M; j++){ + dp[i][j] += dp[i-1][j]; + } + } + + int t; sd(t); + while(t--){ + int a, b, n; sd3(a,b,n); + printf("%d\n", dp[b][n] - dp[a-1][n]); + } + return 0; +} diff --git a/spoj_cpp_clean/NFURY/NFURY-9.cpp b/spoj_cpp_clean/NFURY/NFURY-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a08fd5b8f91c35f8b8b4f3db56f3b93d8f1959f --- /dev/null +++ b/spoj_cpp_clean/NFURY/NFURY-9.cpp @@ -0,0 +1,57 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +vector SQRS; +int DP[1007]; + +inline void precalc() +{ + int i, j; + + DP[0] = 0; + + for (i = 1; i * i <= 1000; i++) + SQRS.push_back(i*i); + + for (i = 1; i <= 1000; i++) + { + DP[i] = INT_MAX; + + for (j = 0; j < SQRS.size() && SQRS[j] <= i; j++) + DP[i] = min(DP[i], DP[i - SQRS[j]] + 1); + } +} + +int main() +{ + int T, num; + + //freopen("input.txt", "r", stdin); + + precalc(); + + scanf("%d", &T); + + while (T--) + { + scanf("%d", &num); + printf("%d\n", DP[num]); + } + + return 0; +} diff --git a/spoj_cpp_clean/NGIRL/NGIRL-9.cpp b/spoj_cpp_clean/NGIRL/NGIRL-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9bc1bdfb650983b74e4d211eab0461c4b4b560be --- /dev/null +++ b/spoj_cpp_clean/NGIRL/NGIRL-9.cpp @@ -0,0 +1,85 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define ll long long +#define SQ 100007 +using namespace std; + +const ll MAXN = 10e10; + +vector< pair > prime_squares; +bool is_prime[SQ]; +ll N, K; +int c; + +inline void sieve() +{ + int i, j; + + c = 2; + + memset(is_prime, 1, sizeof(is_prime)); + + prime_squares.push_back(make_pair(4, 1)); + + for (i = 3; i < SQ; i += 2) + if (is_prime[i]) + { + prime_squares.push_back(make_pair((ll)i * (ll)i, c++)); + for (j = i + i; j < SQ; j += i) + is_prime[j] = 0; + } +} + +inline void init() +{ + cin >> N >> K; +} + +inline void solve() +{ + ll a1, a2; + + if (N < 1) + cout << "0 0\n"; + + K = min(K, N); + + a1 = prev(upper_bound(prime_squares.begin(), prime_squares.end(), make_pair(N, SQ)))->second; + a2 = upper_bound(prime_squares.begin(), prime_squares.end(), make_pair(K, SQ))->second; + + cout << a1 << " " << a1 - a2 + 1 << "\n"; + +} +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + sieve(); + + int T; + + cin >> T; + + while (T--) + { + init(); + solve(); + } + + return 0; +} diff --git a/spoj_cpp_clean/NGM/NGM-14.cpp b/spoj_cpp_clean/NGM/NGM-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89cc78afb4b5aadc03d02d4a127d3becfdc52012 --- /dev/null +++ b/spoj_cpp_clean/NGM/NGM-14.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +#define ll long long + +int main() + +{ + ll n; + cin >> n; + if(n < 10) { + cout << 1 << endl << n << endl; + } + else if(n % 10 == 0) { + printf("2\n"); + } + else { + cout << 1 << endl << n % 10 << endl; + } + + return 0; +} diff --git a/spoj_cpp_clean/NGM/NGM-16.cpp b/spoj_cpp_clean/NGM/NGM-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1863158c0bd87affbc1f0aba63fbaeebc7b564d6 --- /dev/null +++ b/spoj_cpp_clean/NGM/NGM-16.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int main() { + // your code goes here + long long n; + cin>>n; + if(n%10 == 0){ + cout<<2<<"\n"; + } + else{ + cout<<1<<"\n"; + cout<< n%10 <<"\n"; + } + return 0; diff --git a/spoj_cpp_clean/NGM/NGM-19.cpp b/spoj_cpp_clean/NGM/NGM-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7498df6f9b7b9504ccd239673b83906c35447a33 --- /dev/null +++ b/spoj_cpp_clean/NGM/NGM-19.cpp @@ -0,0 +1,24 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 + +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + // int t;cin>>t;while(t--){ + ll num; + cin>>num; + num = num%10; + if(num==0){ + cout<<"2"; + } + else{ + cout<<"1"< + +using namespace std; + +int main() +{ + int num; + cin>>num; + if(num%10==0) + cout<<2< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +int main() +{ + int num, t; + + scanf("%d", &num); + + t = num % 10; + + if (t == 0) + printf("2\n"); + else + printf("1\n%d\n", t); + + return 0; +} diff --git a/spoj_cpp_clean/NGM2/NGM2-12.cpp b/spoj_cpp_clean/NGM2/NGM2-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6afeace872ec77638c603284fa4a698cd3199c9c --- /dev/null +++ b/spoj_cpp_clean/NGM2/NGM2-12.cpp @@ -0,0 +1,53 @@ +#include +#define mp make_pair +#define pb push_back + +using namespace std; + +typedef pair ii; + +typedef long long int64; + +int64 __lcm(int64 a, int64 b) { return (a * (b / __gcd(a, b))); } + +int64 pos[16], memo[32678]; +int64 n, k; + +int64 solve(int at, int mask) +{ + if (mask == (1 << k)) + return memo[at]; + int b = __builtin_popcount(mask); + if (b == at) { + int64 l = 1; + for (int i = 0; i < k; i++) { + if (mask & (1 << i)) + l = __lcm(l, pos[i]); + } + memo[at] += n / l; + } + return solve(at, mask + 1); +} + +int main() +{ + ios::sync_with_stdio(false); + int64 a, d; + cin >> n >> k; + + int64 ans = 0; + for (int i = 0; i < k; i++) { + cin >> pos[i]; + ans += n / pos[i]; + } + + for (int i = 1; i < k; i++) { + if (i & 1) + ans -= solve(i + 1, 3); + else + ans += solve(i + 1, 3); + } + + cout << n - ans << endl; + return 0; +} diff --git a/spoj_cpp_clean/NGM2/NGM2-8.cpp b/spoj_cpp_clean/NGM2/NGM2-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1f64f752618e4ae8251a6944169e8bd80d5d081 --- /dev/null +++ b/spoj_cpp_clean/NGM2/NGM2-8.cpp @@ -0,0 +1,79 @@ +/*************************************************** + * Problem name : NGM2 - Another Game With Numbers.cpp + * Problem Link : http://www.spoj.com/problems/NGM2/ + * OJ : Spoj + * Verdict : AC + * Date : 2017-10-30 + * Problem Type : Inclusion-Exclusion + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +ll bitMask_inclusion(ll N, ll k, ll ar[]) { + ll ans = 0; + for (ll mask = 1; mask < (1 << k); mask++) { + ll cnt = 0, lc = 1; + for (ll i = 0; i < k; i++) { + if (mask & (1 << i)) { + lc = lcm(lc, ar[i]); + cnt++; + } + } + if (cnt % 2 == 0) ans -= N / lc; + else ans += N / lc; + } + return ans; +} +int main () { + ll N, k, ar[MAX]; + scanf("%lld %lld", &N, &k); + for (int i = 0; i < k; i++) { + scanf("%lld", &ar[i]); + } + printf("%lld\n", N - bitMask_inclusion(N, k, ar)); + return 0; +} diff --git a/spoj_cpp_clean/NHAY/NHAY-13.cpp b/spoj_cpp_clean/NHAY/NHAY-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf3e3f0347acd05715c6dadacee9c2fc0e681d27 --- /dev/null +++ b/spoj_cpp_clean/NHAY/NHAY-13.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +typedef long long i64; + +vector build_failure(char *needle, int m){ + vector v(m+1, 0); + for(int i=2; i<=m; ++i){ + int j = v[i-1]; + for(;;){ + if(needle[i-1] == needle[j]){ + v[i] = j+1; + break; + } + if(j == 0){ + v[i] = 0; + break; + } + j = v[j]; + } + } + return v; +} + +void kmp(char *ptr, vector &v, int m){ + int i = 0, j = 0; + char x; scanf("%c", &x); + for(;x != '\n';){ + if(ptr[i] == x){ + i++; + scanf("%c", &x); ++j; + if(i == m)printf("%d\n",j-m); + }else if(i > 0){ + i = v[i]; + }else{ + scanf("%c",&x); ++j; + } + } +} + +int main(){ + //freopen("data.in","r",stdin); + int m; char x; + char *needle; + while(scanf("%d\n",&m) == 1 && m){ + needle = new char[m+1]; + scanf("%s\n",needle); needle[m] = '\0'; + //printf("%s\n",needle); + + vector v = build_failure(needle, m); + kmp(needle, v, m); + printf("\n"); + delete(needle); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/NHAY/NHAY-4.cpp b/spoj_cpp_clean/NHAY/NHAY-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b546550eb6ab484cc674014e6de646140d5c2c31 --- /dev/null +++ b/spoj_cpp_clean/NHAY/NHAY-4.cpp @@ -0,0 +1,139 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +string pat, txt; +int failarray[10000]; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + while(cin>>n) + { + cin>>pat>>txt; + int i=1, len=0; + failarray[0]=0; + while(i +#include +#include +using namespace std; + +void build_failure_function(char pattern[],int m,int F[]) +{ + // let m be the length of the pattern + + F[0] = F[1] = 0; // always true + + for(int i = 2; i <= m; i++) { + // j is the index of the largest next partial match + // (the largest suffix/prefix) of the string under + // index i - 1 + int j = F[i - 1]; + for( ; ; ) { + // check to see if the last character of string i - + // - pattern[i - 1] "expands" the current "candidate" + // best partial match - the prefix under index j + if(pattern[j] == pattern[i - 1]) { + F[i] = j + 1; break; + } + // if we cannot "expand" even the empty string + if(j == 0) + { + F[i] = 0; + break; + } + // else go to the next best "candidate" partial match + j = F[j]; + } + } + /*for(int i=0;i<=m;i++) + cout<<"\nF["<>a; + //cout<0) + { + i=f[i]; + } + else + break; + } + pos++; + //cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorpos; +int f[MAX]; +void failure(string pattern) { + f[0] = 0; + int j = 0, i = 1, len = pattern.size(); + while (i < len ) { + if (pattern[j] == pattern[i]) { + f[i++] = ++j; + } else { + if (j) { + j = f[j - 1]; + } else { + f[i] = 0; + i++; + } + } + } +} +void KMP_match(string txt, string patten) { + int i = 0, j = 0, ln_t = txt.size(), ln_p = patten.size(); + int ans = -1; + while (j < ln_t) { + if (txt[j] == patten[i]) { + i++, j++; + if (i == ln_p) { + ans = j - ln_p; + pos.push_back(ans); + i = f[i - 1]; + } + } else { + if (i) { + i = f[i - 1]; + } else { + j++; + } + } + } +} +int main () { + int n, t = 1; + string txt, patten; + while (scanf("%d", &n) == 1) { + getchar(); + cin >> patten >> txt; + failure(patten); + KMP_match(txt, patten); + if (pos.size() != 0 ) { + if(t!=1) nl; + t++; + for (int i = 0; i < (int)pos.size(); i++) { + printf("%d\n", pos[i]); + } + pos.clear(); + } + } + return 0; +} diff --git a/spoj_cpp_clean/NHAY/NHAY-9.cpp b/spoj_cpp_clean/NHAY/NHAY-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a38cf01454aadd381180ccfdb8a81346cc3a0939 --- /dev/null +++ b/spoj_cpp_clean/NHAY/NHAY-9.cpp @@ -0,0 +1,71 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +using namespace std; + +int T; +string needle, haystack; +int N; +int pos; + +int main() +{ + char c; + + //freopen("input.txt","r",stdin); + while(!feof(stdin)) + { + c=fgetc(stdin); + if(c==EOF) + return 0; + while(!isalpha(c)) + { + c=fgetc(stdin); + if(c==EOF) + return 0; + } + + ungetc(c, stdin); + + c=fgetc(stdin); + while(isalpha(c)) + { + needle.push_back(c); + c=fgetc(stdin); + } + + pos=0; + + c=fgetc(stdin); + while(isalpha(c)) + { + haystack.push_back(c); + c=fgetc(stdin); + } + + pos=haystack.find(needle, pos); + while(pos>=0 && pos Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +struct node +{ + char info; + struct node *left; + struct node *right; +}; + +void construct(struct node **cur) +{ + char c; + + scanf("%c", &c); + + *cur=(struct node*)malloc(sizeof(struct node)); + (*cur)->info=c; + (*cur)->left=NULL; + (*cur)->right=NULL; + + if(c=='n') + { + construct(&(*cur)->left); + construct(&(*cur)->right); + } +} + +int count(struct node *cur) +{ + if(cur->info=='l') + return 0; + + return 1+max(count(cur->left), count(cur->right)); +} + +void free_node(struct node **cur) +{ + if((*cur)->info=='l') + { + free(*cur); + return; + } + + free_node(&(*cur)->left); + free_node(&(*cur)->right); + free(*cur); +} + +int main() +{ + int T, i; + struct node *root; + scanf("%d\n", &T); + char c; + + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +struct person{ + int x, y, z; + + void assign(int a, int b, int c){ + x = a; y = b; z = c; + } + + bool operator<(const person& o) const + { + return x < o.x; + } + +}; + +int t, n, a, b, c, res; +person ranks[100000]; +set S; +vector to_erase; + +bool comp(const person& p1, const person& p2){ + return p1.z < p2.z; +} + +bool is_inside(int i){ + set::iterator j = S.lower_bound(ranks[i]); + // j is the first element that fails the comaprison test + if(j == S.begin()) return false; + j--; + return j->y < ranks[i].y; +} + + +int main(){ + sd(t); + while(t--){ + sd(n); + S.clear(); + for(int i = 0; i < n ; i++){ + sd3(a,b,c); + ranks[i].assign(a,b,c); + } + sort(ranks, ranks+n, comp); + S.insert(ranks[0]); //The first person is assumed to be excellent + res = 1; + for(int i = 1; i < n; i++){ + if (is_inside(i)) continue; //There is some person better than the ith person so we dont need to consider i in our result. + + // Nobody is better than i yet, so increase our count + res++; + to_erase.clear(); + for(set::iterator it = S.lower_bound(ranks[i]); it != S.end(); it++){ + + if(it->y > ranks[i].y) + to_erase.push_back(*it); + } + for(unsigned j = 0; j < to_erase.size(); j++){ + S.erase(to_erase[j]); + } + S.insert(ranks[i]); + } + printf("%d\n",res); + } + return 0; +} diff --git a/spoj_cpp_clean/NINJA1/NINJA1-9.cpp b/spoj_cpp_clean/NINJA1/NINJA1-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb5e8dda5afbaae9f978f15a3529ad618bee06bd --- /dev/null +++ b/spoj_cpp_clean/NINJA1/NINJA1-9.cpp @@ -0,0 +1,72 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +long long A[100007], B[100007]; +int N; + +inline void init() +{ + int i; + + scanf("%d", &N); + + for (i = 0; i < N; i++) + scanf("%lld", &A[i]); + + for (i = 0; i < N; i++) + scanf("%lld", &B[i]); + +} + +inline bool solve() +{ + int i; + long long prev; + + prev = min(A[0], B[0]); + + for (i = 1; i < N; i++) + { + if (max(A[i], B[i]) < prev) + return 0; + + if (min(A[i], B[i]) < prev) + prev = max(A[i], B[i]); + else + prev = min(A[i], B[i]); + } + + return 1; + +} + +int main() +{ + int T; + + scanf("%d", &T); + + while (T--) + { + init(); + if (solve()) + printf("YES\n"); + else + printf("NO\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/NINJA3/NINJA3-9.cpp b/spoj_cpp_clean/NINJA3/NINJA3-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19b54c7fabddbbb2708502092ab19b703735035c --- /dev/null +++ b/spoj_cpp_clean/NINJA3/NINJA3-9.cpp @@ -0,0 +1,88 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +inline unsigned long long gcd(unsigned long long u, unsigned long long v) +{ + unsigned long long shift; + + if (u == 0) return v; + if (v == 0) return u; + + for (shift = 0; ((u | v) & 1) == 0; ++shift) + { + u >>= 1; + v >>= 1; + } + + while ((u & 1) == 0) + u >>= 1; + + do + { + + while ((v & 1) == 0) + v >>= 1; + + if (u > v) + { + unsigned long long t = v; + v = u; + u = t; + } + + v = v - u; + } while (v != 0); + + + return u << shift; +} + +unsigned long long N, a, b; + +inline void init() +{ + cin >> N >> a >> b; + //scanf("%llu %llu %llu", &N, &a, &b); +} + +inline void solve() +{ + int i; + unsigned long long GCD; + + GCD = gcd(a, b); + + for (i = 0; i < GCD; i++) + // printf("%llu", N); + cout << N; + +// printf("\n"); + cout << endl; +} + +int main() +{ + int T; + + //scanf("%d", &T); + cin >> T; + while (T--) + { + init(); + solve(); + } + + return 0; +} diff --git a/spoj_cpp_clean/NINJA4/NINJA4-9.cpp b/spoj_cpp_clean/NINJA4/NINJA4-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eac9fdcae89a111897291ea0e0222ed7d2c2bf9d --- /dev/null +++ b/spoj_cpp_clean/NINJA4/NINJA4-9.cpp @@ -0,0 +1,65 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int N; + +inline int solvex() +{ + int ans; + + if (N == 0) + return 0; + + if (N == 1) + return 1; + + ans = ceil(N / 2.0); + + if (ans % 2 == 0) + ans = -ans; + + return ans; +} + +inline int solvey() +{ + int ans; + + if (N == 0 || N == 1) + return 0; + + ans = ceil((N - 1) / 4.0) * 2; + + if (N % 4 == 0 || N % 4 == 1) + ans = -ans; + + return ans; +} + +int main() +{ + int T; + + scanf("%d", &T); + + while (T--) + { + scanf("%d", &N); + printf("%d %d\n", solvex(), solvey()); + } + + return 0; +} diff --git a/spoj_cpp_clean/NINJA5/NINJA5-9.cpp b/spoj_cpp_clean/NINJA5/NINJA5-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e57196de28167eee4884e77de4672d2b1bbb606f --- /dev/null +++ b/spoj_cpp_clean/NINJA5/NINJA5-9.cpp @@ -0,0 +1,66 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int A[2000014]; +int N, K; + +inline void init() +{ + int i; + + scanf("%d %d", &N, &K); + + for (i = 1; i <= K; i++) + scanf("%d", &A[i]); + + sort(A + 1, A + K + 1); + + A[0] = -1; + + if (A[K] != N && A[K] != N - 1) + A[++K] = N; +} + +inline int solve() +{ + int i, sum, c = 0; + + for (i = 1; i <= K; i++) + { + sum = A[i] - A[i - 1]; + if (sum == 1) + return 0; + + c += sum / 2; + } + + return c; +} + +int main() +{ + int T; + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/NINJA7/NINJA7-9.cpp b/spoj_cpp_clean/NINJA7/NINJA7-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3282e077645b1f152c7ce78691dbcb75b6a74dc2 --- /dev/null +++ b/spoj_cpp_clean/NINJA7/NINJA7-9.cpp @@ -0,0 +1,75 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define MAXN 100007 + +using namespace std; + +int A[MAXN]; +int N; + +inline void init() +{ + int i, num; + + scanf("%d", &N); + + for (i = 0; i < N; i++) + scanf("%d", &A[i]); + + for (i = 0; i < N; i++) + { + scanf("%d", &num); + A[i] -= num; + } + + sort(A, A + N); +} + +inline int solve() +{ + int i, c = 0; + long long sum = 0; + + for (i = N - 1; i >= 0; i--) + { + if (sum + A[i] >= 0) + { + sum += A[i]; + c++; + } + + else + return c; + } + + return N; +} + +int main() +{ + int T; + + //freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/NINJA8/NINJA8-9.cpp b/spoj_cpp_clean/NINJA8/NINJA8-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..adf81b4a19030b68e297b9562d1ffbac6ce5ea40 --- /dev/null +++ b/spoj_cpp_clean/NINJA8/NINJA8-9.cpp @@ -0,0 +1,93 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define gc() getchar_unlocked() + +using namespace std; + +unsigned int A[1000007]; +int N; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +inline void init() +{ + int i; + + N = FAST_IO(); + +// scanf("%d", &N); + + for (i = 0; i < N; i++) + //scanf("%d", &A[i]); + A[i] = FAST_IO(); +} + +inline int solve() +{ + int i; + int xxor = 0; + int pos = 0, ans = 0; + + xxor = A[0]; + + for (i = 1; i < N; i++) + xxor ^= A[i]; + + if (xxor == 0) + return 0; + + for (; xxor; pos++) + xxor >>= 1; + + pos--; + + for (i = 0; i < N; i++) + if ((A[i] >> pos) % 2) + ans++; + + return ans; +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + //scanf("%d", &T); + T = FAST_IO(); + + while (T--) + { + init(); + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/NITTROAD/NITTROAD-8.cpp b/spoj_cpp_clean/NITTROAD/NITTROAD-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..928e5c4fcd218b501b9e36f8f74ae768fae86848 --- /dev/null +++ b/spoj_cpp_clean/NITTROAD/NITTROAD-8.cpp @@ -0,0 +1,156 @@ +/*************************************************** + * Problem Name : NITTROAD - Roads of NITT.cpp + * Problem Link : http://www.spoj.com/problems/NITTROAD/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-05-31 + * Problem Type : Data Structure (DSU) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 20005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +struct st { + int idx, val; + char ch; +}; +int size[MAX], par[MAX], ans; +void init (int n) { + for (int i = 1; i <= n; i++) { + par[i] = i; + size[i] = 1; + } +} +int find (int r) { + if (par[r] == r) return r; + else return par[r] = find (par[r]); +} +void makeSet (int a, int b) { + a = find (a); + b = find (b); + + if (a != b) { + if (size[a] < size[b]) { + swap (a, b); + } + + ans = ans - (size[a] * size[b]); + par[b] = a; + size[a] += size[b]; + } +} +int main () { + __FastIO; + int tc; + cin >> tc; + + while (tc--) { + int n, q; + cin >> n; + init (n); + vectoredge[MAX]; + + for (int i = 1; i <= n - 1; i++) { + int u, v; + cin >> u >> v; + edge[i].pb (pii (u, v) ); + } + + cin >> q; + st Q[MAX]; + mapmp; + + for (int i = 1; i <= q; i++) { + char ch; + int x; + cin >> ch; + + if (ch == 'R') { + cin >> x; + Q[i].idx = x; + Q[i].ch = ch; + mp[x] = 1; + + } else { + Q[i].ch = ch; + } + } + + int nn = n - 1; + ans = (nn * (nn + 1) ) / 2; + + for (int i = 1; i <= n - 1; i++) { + if (mp[i] == 0) { + int a = edge[i][0].first; + int b = edge[i][0].second; + makeSet (a, b); + } + } + + vectorv; + + for (int i = q; i >= 1; i--) { + char ch = Q[i].ch; + + if (ch == 'R') { + int x = Q[i].idx; + int a = edge[x][0].first; + int b = edge[x][0].second; + makeSet (a, b); + + } else { + v.pb (ans); + } + } + + for (int i = (int) v.size() - 1; i >= 0; i--) { + cout << v[i] << "\n"; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/NOCHANGE/NOCHANGE-3.cpp b/spoj_cpp_clean/NOCHANGE/NOCHANGE-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..789f8b40a5a4b0a7add2de9f02330b1a805b36d5 --- /dev/null +++ b/spoj_cpp_clean/NOCHANGE/NOCHANGE-3.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int x, k, values[5]; + sd2(x,k); + bool dp[x+1]; + memset(dp, false,sizeof(dp)); + dp[0] = true; + for(int i = 0; i < k; i ++){ + sd(values[i]); + if(i>0) + values[i] += values[i-1]; + } + for(int i = 0; i < k; i++){ + for(int j = values[i]; j <= x; j++){ + dp[j] |= dp[j-values[i]]; + } + } + if(dp[x] == true) + cout<<"YES"< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define MOD 1000000007 + +using namespace std; + +long long DP[1024][128][3]; //0 chicken, 1 goat, 2 buffalo +int N, K; + +int main() +{ + int i, currK; + int T; + long long sum; + + for(i=1; i<=1023; i++) + DP[i][0][0] = 1; + + DP[1][1][1] = DP[1][1][2] = 1; + + for(i=2; i<= 1023; i++) + for(currK = 1; currK <= min(127,i); currK++) + { + DP[i][currK][0] += DP[i-1][currK][0] + DP[i-1][currK][1] + DP[i-1][currK][2]; + DP[i][currK][1] += DP[i-1][currK-1][0] + DP[i-1][currK-1][2]; + DP[i][currK][2] += DP[i-1][currK-1][0] + DP[i-1][currK-1][1]; + + while(DP[i][currK][0] >= MOD) + DP[i][currK][0] -= MOD; + + while(DP[i][currK][1] >= MOD) + DP[i][currK][1] -= MOD; + + while(DP[i][currK][2] >= MOD) + DP[i][currK][2] -= MOD; + } + + scanf("%d", &T); + + while(T--) + { + scanf("%d %d", &N, &K); + + if(N <= 2) + { + printf("1\n"); + continue; + } + + N -= 2; + + if(K > N) + K = N; + + sum = 0; + + for(i=0; i<=K; i++) + { + sum = sum + DP[N][i][0] + DP[N][i][1] + DP[N][i][2]; + + while(sum >= MOD) + sum -= MOD; + } + + printf("%lld\n", sum); + } + + + return 0; +} diff --git a/spoj_cpp_clean/NR2/NR2-3.cpp b/spoj_cpp_clean/NR2/NR2-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fbcfe8e694e945751b19fb6b472e76e78a263a9 --- /dev/null +++ b/spoj_cpp_clean/NR2/NR2-3.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int n; + long long uncommon = ~(0LL), common = 0, num; + sd(n); + while(n--){ + scanf("%lld",&num); + common |= num; + uncommon &= num; + } + printf("%lld\n",common^uncommon); + return 0; +} diff --git a/spoj_cpp_clean/NSTEPS/NSTEPS-11.cpp b/spoj_cpp_clean/NSTEPS/NSTEPS-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1386bb7b89f1f6427d8967f1c2276f52bd80d97 --- /dev/null +++ b/spoj_cpp_clean/NSTEPS/NSTEPS-11.cpp @@ -0,0 +1,32 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + + #include + + using namespace std; + + int main(void) + { + int n, x, y; + + cin >> n; + + while(n--) + { + cin >> x >> y; + + if(x == y || x == y+2) + { + if(x % 2 == 0 && y % 2 == 0) + cout << (x+y) << endl; + else + cout << (x+y-1) << endl; + } + else + cout << "No Number" << endl; + } + + return 0; + } \ No newline at end of file diff --git a/spoj_cpp_clean/NSTEPS/NSTEPS-19.cpp b/spoj_cpp_clean/NSTEPS/NSTEPS-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b275af4346ee09f29267ed49cc56e20849c2897 --- /dev/null +++ b/spoj_cpp_clean/NSTEPS/NSTEPS-19.cpp @@ -0,0 +1,66 @@ +#include +#define ll long long +#define endl "\n" +#define MAX 100001 +#define mod 1000000007 +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int t; + cin>>t; + while(t--) + { + int x,y; + cin>>x>>y; + int num=0; + if( x==0 && y==0) + { + cout<<"0"< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T, x,y; + s(T); + while(T--) + { + s(x); s(y); + if(x!=y && (x-y)!=2) + { + cout<<"No Number"; + } + else if(x==y) + { + + //o 1, 2 3, 4 + if(x==0) + cout<<"0"; + else if(x==1) + cout<<"1"; + else if(x%2==0) + cout< +#include + +using namespace std; + +int main() +{ + long int x,y,test; + scanf("%ld",&test); + while(test--) + { + scanf("%ld %ld",&x,&y); + if(x==y) + { + if(x%2==0) + printf("%ld\n",x+x); + else + printf("%ld\n",(x+x-1)); + } + else if(x==(y+2)) + { + if(x%2==0) + { + x--; + printf("%ld\n",x+x); + } + else + { + x--; + printf("%ld\n",(x+x-1)); + } + } + else + printf("No Number\n"); + } + return 0; +} diff --git a/spoj_cpp_clean/NSTEPS/NSTEPS-9.cpp b/spoj_cpp_clean/NSTEPS/NSTEPS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf7ac9a544310eaf0477816c0072ab780a75048d --- /dev/null +++ b/spoj_cpp_clean/NSTEPS/NSTEPS-9.cpp @@ -0,0 +1,43 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +int main() +{ + int N, i; + int x, y; + + //freopen("input.txt","r",stdin); + + scanf("%d", &N); + + for(i=0; i10000 || y>10000 || x +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; +int convert(string x){ + int l = x.length(), i, result = 0, mult = 1; + char c; + for(i = 0; i < l; i++){ + c = x[i]; + if((int)c > 48 and (int)c < 58){ + mult = c - 48; + } + else{ + if(c == 'm'){ + result += 1000 * mult; + mult = 1; + } + else if( c == 'c'){ + result += 100 * mult; + mult = 1; + } + else if( c == 'x'){ + result += 10 * mult; + mult = 1; + } + else if( c == 'i'){ + result += 1 * mult; + mult = 1; + } + } + } + return result; +} + +string revert(int a){ + string result; + int d1 = a % 10; + a = a/10; + int d2 = a % 10; + a = a/10; + int d3 = a % 10; + a = a/10; + int d4 = a % 10; + //cout< 1){ + result += (char)(d4 + 48); + result += 'm'; + } + if(d3 == 1) + result += 'c'; + else if(d3 > 1){ + result += (char)(d3 + 48); + result += 'c';} + if(d2 == 1) + result += 'x'; + else if(d2 > 1){ + result += (char)(d2 + 48); + result += 'x';} + if(d1 == 1) + result += 'i'; + else if(d1 > 1){ + result += (char)(d1 + 48); + result += 'i'; } + return result; +} + + +int main(){ + string x, y; + int t, a, b; + sd(t); + while(t--){ + cin>>x>>y; + a = convert(x); + b = convert(y); + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX], cost[MAX]; +int par[MAX], level[MAX]; +int table[MAX][20], mx_rd[MAX][20]; + +void dfs (int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + par[v] = u; + level[v] = level[u] + 1; + mx_rd[v][0] = cost[u][i]; + dfs (v, u); + } +} + +void LCA (int n) { + for (int i = 1; i <= n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 1 ; i <= n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + mx_rd[i][j] = max (mx_rd[i][j - 1], mx_rd[x][j - 1]); + + } else { + table[i][j] = -1; + mx_rd[i][j] = mx_rd[i][j - 1]; + } + } + } +} + +int LCA_Quary (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} + +int getMAX (int u, int uu) { + int mx = 0; + + for (int i = log2 (uu); i >= 0; i--) { + if ( (1 << i) <= uu) { + mx = max (mx, mx_rd[u][i]); + u = table[u][i]; + uu -= (1 << i); + } + + if (uu == 0) break; + } + + return mx; +} + +int main () { + __FastIO; + int n, Q; + + while (cin >> n) { + if (n == 0) break; + + for (int i = 0; i < n - 1; i++) { + int u, v, w; + cin >> u >> v >> w; + adj[u].pb (v); + adj[v].pb (u); + cost[u].pb (w); + cost[v].pb (w); + } + + par[1] = -1; + level[1] = 0; + mx_rd[1][0] = 0; + dfs (1, -1); + LCA (n); + cin >> Q; + + for (int q = 1; q <= Q; q++) { + int u, v; + cin >> u >> v; + int x = LCA_Quary (u, v); + int uu = level[u] - level[x]; + int vv = level[v] - level[x]; + cout << max (getMAX (u, uu), getMAX (v, vv) ) << "\n"; + } + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + cost[i].clear(); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/NUMOFPAL/NUMOFPAL-19.cpp b/spoj_cpp_clean/NUMOFPAL/NUMOFPAL-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bde57519c64746882c544b8168bf7fe8d4337566 --- /dev/null +++ b/spoj_cpp_clean/NUMOFPAL/NUMOFPAL-19.cpp @@ -0,0 +1,60 @@ +// Author : @AdarshGaur + +#include +using namespace std; + +#ifdef cicada +#include "debug.h" +#else +#define debug(...) "Yume o akiramete shinde kure." +#endif + +#define int int64_t +#define endl "\n" +typedef pair pii; + +int const INF = 2e18; +int const mod = 1e9 +7; + + +signed main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL);cout.precision(20); + + + string s; + cin >> s; + + int n = s.size(), ans = 0; + int d1[n], d2[n]; + memset(d1, 0, sizeof(d1)); + memset(d2, 0, sizeof(d2)); + for(int i=0, l=0, r=-1; ir) ? 1 : min(d1[l + (r-i)], r-i); + while(i+k=0 and s[i+k] == s[i-k]) + k++; + d1[i] = k--; + if(i +k > r){ + l = i-k; + r = i+k; + } + ans += d1[i]; + } + + for(int i=0, l=0, r=-1; i r ? 0 : min(d2[l + (r-i+1)], r-i+1)) +1; + while(i+k-1 =0 and s[i-k] == s[i+k-1]) + k++; + d2[i] = --k; + if(i + k-1 > r){ + l = i-k; + r = i+k-1; + } + ans += d2[i]; + } + + cout << ans << endl; + + return 0; +} diff --git a/spoj_cpp_clean/NUMPATH/NUMPATH-9.cpp b/spoj_cpp_clean/NUMPATH/NUMPATH-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74bccb0f3193eb888504d7e2189e9fdfc81b8ca9 --- /dev/null +++ b/spoj_cpp_clean/NUMPATH/NUMPATH-9.cpp @@ -0,0 +1,53 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +int R, C; + +inline long long factorial(int num) +{ + long long ans = 1; + int i; + + if (num == 0) + return 1; + + for (i = 2; i <= num; i++) + ans *= i; + + return ans; +} + +inline long long solve() +{ + if (R == 1 && C == 1) + return 1; + + return factorial(R + C - 2) / (factorial(R - 1) * factorial(C - 1)); +} + +int main() +{ + int T; + + scanf("%d", &T); + + while (T--) + { + scanf("%d %d", &R, &C); + printf("%lld\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/NUMTSN/NUMTSN-0.cpp b/spoj_cpp_clean/NUMTSN/NUMTSN-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1026930f17e6beabbdf95cc0ffc7fe9387254bf4 --- /dev/null +++ b/spoj_cpp_clean/NUMTSN/NUMTSN-0.cpp @@ -0,0 +1,93 @@ +#include + +/* [eq][num3][num6][num9][k] */ +long long dp[51][51][51][51]; + +long long ans[101]; + +/* three count, six count, nine count, */ +static long long go(const int& tc, const int& sc, const int& nc, const std::string& bound, bool eq, const int& k) { + if (k >= bound.size()) { + return tc >= 1 && tc == sc && tc == nc; + } + + if (!eq && dp[tc][sc][nc][k] != -1) { + return dp[tc][sc][nc][k]; + } + + int top = eq ? bound.at(k) - '0' : 9; + + long long cnt = 0; + for (int i = 0; i <= top; ++i) { + cnt += go(tc + (i == 3), sc + (i == 6), nc + (i == 9), bound, eq && i == top, k+1); + if (cnt >= 1000000007) + cnt %= 1000000007; + } + + // std::printf("go(%d,%d,%d,,%d,%d) -> %lld\n", tc,sc,nc,eq,k, cnt); + + if (!eq) return dp[tc][sc][nc][k] = cnt % 1000000007; + else return cnt % 1000000007; +} + +static long long sub(const std::string& num) { + return go(0,0,0,num, true, 0); +} + +static bool is_369(const std::string& s) { + int tc,sc,nc; + tc = sc = nc = 0; + for (const auto& c : s) { + tc += c == '3'; + sc += c == '6'; + nc += c == '9'; + } + return tc >= 1 && tc == sc && tc == nc; +} + +static long long one_case(const std::string& l, const std::string& r) { + long long a = sub(r); + long long b = sub(l); + + return (a - b < 0 ? 1000000007L + a - b : a - b) + is_369(l); +} + +static void pad(std::string& n) { + std::stringstream ss; + for (int i = 0; i + n.length() < 51; ++i) { + ss << "0"; + } + ss << n; + n = ss.str(); +} + +int main(void) { + + for (int i = 0; i < 51; ++i) { + for (int j = 0; j < 51; ++j) { + for (int k = 0; k < 51; ++k) { + for (int l = 0; l < 51; ++l) { + dp[i][j][k][l] = -1; + } + } + } + } + + int t; + std::scanf("%d", &t); + + std::string l,r; + for (int i = 0; i < t; ++i) { + std::cin >> l >> r; + pad(l); + pad(r); + + ans[i] = one_case(l,r); + } + + for (int i = 0; i < t; ++i) { + std::printf("%lld\n", ans[i]); + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/NUMTSN/NUMTSN-14.cpp b/spoj_cpp_clean/NUMTSN/NUMTSN-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99e16ff5ac241e87c77955f6b04f94d0fe6fdf44 --- /dev/null +++ b/spoj_cpp_clean/NUMTSN/NUMTSN-14.cpp @@ -0,0 +1,99 @@ + +#include +using namespace std; + +#define ll long long +#define pb push_back +#define mod 1000000007 +ll dp[2][2][52][52][52][52]; +ll num[52], sz; +char l[55], r[55]; + +ll solve(ll isStart, ll isSmall, ll pos, ll cnt3, ll cnt6, ll cnt9) + +{ + if(pos == 0) { + if(cnt3 == cnt6 && cnt6 == cnt9 && cnt3 > 0) + return 1; + return 0; + } + ll &ret = dp[isStart][isSmall][pos][cnt3][cnt6][cnt9]; + if(ret != -1 && isSmall) + return ret; + + ll lim, pos2 = sz - pos; + if(isSmall) + lim = 9; + else + lim = num[pos2]; + + ll rt = 0; + if(!isStart) { + for(ll i = 0; i <= lim; i++) { + rt += solve(0, isSmall | i < num[pos2], pos - 1, (i == 3) + cnt3, (i == 6) + cnt6, (i == 9) + cnt9); + if(rt > mod) + rt %= mod; + } + } + else { + for(ll i = 1; i <= lim; i++) { + rt += solve(0, isSmall | i < num[pos2], pos - 1, (i == 3) + cnt3, (i == 6) + cnt6, (i == 9) + cnt9); + if(rt > mod) + rt %= mod; + } + + rt += solve(1, 1, pos - 1, 0, 0, 0); + if(rt > mod) + rt %= mod; + } + + return ret = rt; +} + +int main() + +{ + ll t, caseno = 0; + memset(dp, -1, sizeof(dp)); + cin >> t; + while(t--) { + scanf("%s %s", l, r); + + sz = 0; + for(ll i = 0; r[i] != '\0'; i++) + num[sz++] = r[i] - '0'; + + ll ans = solve(1, 0, sz, 0, 0, 0); + ll len = strlen(l); + + ll mn = 1, nm; + for(ll i = len - 1; i >= 0; i--) { + nm = l[i] - '0'; + if(nm == 0) { + l[i] = '9'; + mn = 1; + } + else { + l[i] = (nm - 1) + '0'; + mn = 0; + } + + if(mn == 0) + break; + } + + sz = mn = 0; + for(ll i = 0; l[i] != '\0'; i++) { + if(l[i] != '0') + mn = 1; + if(mn) + num[sz++] = l[i] - '0'; + } + + ans -= solve(1, 0, sz, 0, 0, 0); + ans = (ans + mod) % mod; + + printf("%lld\n", ans); + } + + return 0; diff --git a/spoj_cpp_clean/NUMTSN/NUMTSN-3.cpp b/spoj_cpp_clean/NUMTSN/NUMTSN-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..910b8aed0771bff4dde49714259c3ee18eccd6b3 --- /dev/null +++ b/spoj_cpp_clean/NUMTSN/NUMTSN-3.cpp @@ -0,0 +1,136 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<= MOD) x -= MOD; +} + +void sub(int &x, int y){ + x -= y; + if(x < 0) x += MOD; +} + +void pre(){ + dp[0][0][0][0] = 1; + for(int i = 0; i < 52; i++){ + for(int x = 0; x < M; x++){ + for(int y = 0; y < M; y++){ + for(int z = 0; z < M; z++){ + int val = dp[i][x][y][z]; + if(!val) continue; + + for(int d = 0; d < 10; d++){ + int nx = x + (d == 3); + int ny = y + (d == 6); + int nz = z + (d == 9); + + if(nx > 16 or ny > 16 or nz > 16) continue; + + add(dp[i+1][nx][ny][nz], val); + } + } + } + } + } +} + +vector digits(string &x){ + vector ret; + + for(char c : x) ret.pb(c - '0'); + return ret; +} + +int get(string &s){ + vector v = digits(s); + + int n = v.size(); + + int c3 = 0, c6 = 0, c9 = 0; + int ans = 0; + + for(int i = 0; i < n; i++){ + for(int d = 0; d < v[i]; d++){ + int nx = c3 + (d == 3); + int ny = c6 + (d == 6); + int nz = c9 + (d == 9); + + for(int j = max(max(1,nx), max(ny,nz)); j < M; j++){ + add(ans, dp[n-i-1][j-nx][j-ny][j-nz]); + } + } + + c3 += (v[i] == 3); + c6 += (v[i] == 6); + c9 += (v[i] == 9); + } + + if(c3 == c6 and c6 == c9 and c3 > 0) add(ans, 1); + + return ans; +} + +void solve(){ + string a, b; + cin >> a >> b; + + int x = 0, y = 0, z = 0; + for(char c : a) if(c == '3') x++; else if(c == '6') y++; else if(c == '9') z++; + + int flag = (x == y) and (y == z) and (x > 0); + + cout << (get(b) - get(a) + flag + MOD) % MOD << endl; +} + +int main(){ _ + pre(); + + int t; + cin >> t; + + while(t--) solve(); + + return 0; +} diff --git a/spoj_cpp_clean/NUMTSN/NUMTSN-8.cpp b/spoj_cpp_clean/NUMTSN/NUMTSN-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0badcd21b1f485c6233dd19dc14039ea9f8532e4 --- /dev/null +++ b/spoj_cpp_clean/NUMTSN/NUMTSN-8.cpp @@ -0,0 +1,160 @@ +/*************************************************** + * Problem Name : NUMTSN - 369 Numbers.cpp + * Problem Link : https://www.spoj.com/problems/NUMTSN/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : DigitDP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int n; +ll dp[53][53][53][53][3][3]; +vectorA, B; +ll digitDp (int pos, int cnt3, int cnt6, int cnt9, int isChoto, int isBoro) { + if (pos == n) { + if (cnt3 != 0 && cnt3 == cnt6 && cnt6 == cnt9) return 1; + else return 0; + } + + if (dp[pos][cnt3][cnt6][cnt9][isChoto][isBoro] != -1 && isChoto == 1 + && isBoro == 1) { + return dp[pos][cnt3][cnt6][cnt9][isChoto][isBoro]; + } + + ll ans = 0; + int low = 0, high = 9; + + if (isChoto == 0) { + high = B[pos]; + } + + if (isBoro == 0) { + low = A[pos]; + } + + for (int i = low; i <= high; i++) { + int new_cnt3 = cnt3; + int new_cnt6 = cnt6; + int new_cnt9 = cnt9; + int new_isChoto = isChoto; + int new_isBoro = isBoro; + + if (i == 3) new_cnt3++; + + if (i == 6) new_cnt6++; + + if (i == 9) new_cnt9++; + + if (i < high) { + new_isChoto = 1; + } + + if (i > low) { + new_isBoro = 1; + } + + ans = (ans + ( (ll) digitDp (pos + 1, new_cnt3, new_cnt6, new_cnt9, + new_isChoto, new_isBoro) % MOD) ) % MOD; + } + + return dp[pos][cnt3][cnt6][cnt9][isChoto][isBoro] = ans % MOD; +} + +ll solve (string a, string b) { + A.clear(), B.clear(); + int x = 51 - (int) a.size(); + + for (int i = 0; i < x; i++) { + A.pb (0); + } + + x = 51 - (int) b.size(); + + for (int i = 0; i < x; i++) { + B.pb (0); + } + + for (int i = 0; i < (int) b.size(); i++) { + B.pb (b[i] - '0'); + } + + for (int i = 0; i < (int) a.size(); i++) { + A.pb (a[i] - '0'); + } + + n = (int) B.size(); + ll ans = digitDp (0, 0, 0, 0, 0, 0); + return ans; +} + + +int main () { + __FastIO; + int tc; + cin >> tc; + SET (dp, -1); + + for (int t = 1; t <= tc; t++) { + string a, b; + cin >> a >> b; + ll ans = solve (a, b); + cout << ans << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/NY10A/NY10A-16.cpp b/spoj_cpp_clean/NY10A/NY10A-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c01e10ea7a27b99cc1edf07903b78d9e618a6cd6 --- /dev/null +++ b/spoj_cpp_clean/NY10A/NY10A-16.cpp @@ -0,0 +1,58 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "<=b;i--) +#define loop(i,a,b) for(long long i=a; i +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(){ + ll id; + cin>>id; + string s; + cin>>s; + ll n=s.length(); + map val; + loop(i,0,n-2){ + val[s.substr(i,3)]++; + } + cout< list = { "TTT", "TTH", "THT", "THH", "HTT", "HTH", "HHT", "HHH"}; + for(auto item : list){ + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/NY10A/NY10A-5.cpp b/spoj_cpp_clean/NY10A/NY10A-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf01d6ba5c6a02e365f979f468609c1f1d3a7c16 --- /dev/null +++ b/spoj_cpp_clean/NY10A/NY10A-5.cpp @@ -0,0 +1,49 @@ +#include +#include + +using namespace std; + +void search(string pat, string input) +{ + int m,n,i,j,k,x; + m=pat.length(); + n=input.length(); + k=0; + for(i=0;i>test; + while(test--) + { + cin>>k; + cin>>input; + //cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int n ; +vectorA; +ll dp[67][10][2]; +ll digitDP (int pos, int pre, int isChoto) { + if (pos == n) { + return 1LL; + } + + ll &ans = dp[pos][pre][isChoto]; + + if (ans != -1) return ans; + + int low = 0, high = 9; + + if (isChoto == 0) { + high = A[pos]; + } + + ans = 0; + + for (int i = low ; i <= high; i++) { + if (i >= pre) { + ans += digitDP (pos + 1, i, isChoto | (i < high) ); + } + } + + return ans; +} +ll solve (int b) { + A.clear(); + + for (int i = 0; i < b; i++) { + A.pb (9); + } + + n = (int) A.size(); + SET (dp, -1); + ll ans = digitDP (0, 0, 0); + return ans; +} +int main () { + __FastIO; + int tc; + cin >> tc; + + while (tc--) { + int a, b; + cin >> a >> b; + ll ans = solve (b); + cout << a << " " << ans << "\n"; + } + + return 0; +} + diff --git a/spoj_cpp_clean/OBIHANOI/OBIHANOI-10.cpp b/spoj_cpp_clean/OBIHANOI/OBIHANOI-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b54b5d8511c64469a8b4cc1150618ab1b97e1ecb --- /dev/null +++ b/spoj_cpp_clean/OBIHANOI/OBIHANOI-10.cpp @@ -0,0 +1,10 @@ +#include +using namespace std; + +int main() +{ + int n, test = 0; + while(cin >> n && n != 0) + cout << "Teste " << ++test << endl << (1 << n)-1 << endl << endl; + return 0; +} diff --git a/spoj_cpp_clean/ODDDIV/ODDDIV-8.cpp b/spoj_cpp_clean/ODDDIV/ODDDIV-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebd976de5199e792d6dbee42a4e2862fb2b5245b --- /dev/null +++ b/spoj_cpp_clean/ODDDIV/ODDDIV-8.cpp @@ -0,0 +1,161 @@ +/*************************************************** + * Problem name : ODDDIV - Odd Numbers of Divisors.cpp + * Problem Link : http://www.spoj.com/problems/ODDDIV/ + * OJ : Spoj + * Verdict : AC + * Date : 2017-11-12 + * Problem Type : Number Theory + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorV[MAX]; +ll Lower(ll k, ll value) { + ll high = V[k].size()-1, low = 0, ans = -1; + while (high >= low) { + int mid = (high + low) / 2; + if (V[k][mid] == value) { + return mid; + } else if (V[k][mid] > value) { + high = mid - 1; + } else { + low = mid + 1; + } + } + if (ans == -1) { + ans = high; + } + return ans; +} +ll Upper(ll k, ll value) { + ll mid, high = V[k].size()-1, low = 0, ans = -1; + while (high >= low) { + mid = (high + low) / 2; + if (V[k][mid] == value) { + return mid; + } else if (V[k][mid] > value) { + ans = mid; + high = mid - 1; + } else { + low = mid + 1; + } + } + if (ans == -1) { + ans = low; + } + return ans; +} +vector prime; +char sieve[MAX]; +void primeSieve (ll n) { + sieve[0] = sieve[1] = 1; + prime.push_back(2); + for ( ll i = 4; i < n; i += 2 ) sieve[i] = 1; + ll sqrtn = sqrt ( n ); + for ( ll i = 3; i <= sqrtn; i += 2 ) { + if ( sieve[i] == 0 ) { + for ( ll j = i * i; j < n; j += 2 * i ) sieve[j] = 1; + } + } + for ( ll i = 3; i < n; i += 2 ) if ( sieve[i] == 0 ) prime.push_back(i); +} +ll NOD(ll n) { + ll sqrtn = sqrt ( n ); + ll res = 1; + for (ll i = 0; i < (ll)prime.size() && prime[i] <= sqrtn; i++ ) { + if ( n % prime[i] == 0 ) { + ll p = 0; + while ( n % prime[i] == 0 ) { + n /= prime[i]; + p++; + } + sqrtn = sqrt ( n ); + p *= 2; + p++; + res *= p; + } + } + if ( n != 1 ) { + res *= 3; + } + return res; +} +void preCal() { + //~ ll sum = 5; + //~ for (ll i = 4; i <= MAX ;) { + //~ V.push_back(i); + //~ i = i + sum; + //~ sum = sum + 2; + //~ } + for (ll i = 1; i < MAX; i++) { + ll div = NOD(i); + V[div].push_back(i * i); + } +} +int main () { + primeSieve(sqrt(MAX)); + preCal(); + ll tc, k, lo, hi; + scanf("%lld", &tc); + for (ll t = 1; t <= tc; t++) { + scanf("%lld %lld %lld", &k, &lo, &hi); + ll x = Upper(k, lo); + ll y = Lower(k, hi); + //~ ll x = sqrt(lo), y = sqrt(hi); + //~ printf("%lld %lld\n", x, y); + ll cnt = y - x; + //~ for (ll i = x; i <= y; i++) { + //~ if (k == VV[i]) cnt++; + //~ } + printf("%lld\n", cnt+1); + } + return 0; +} diff --git a/spoj_cpp_clean/OFFSIDE/OFFSIDE-12.cpp b/spoj_cpp_clean/OFFSIDE/OFFSIDE-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cfcfc12eb73e4f29a81ce2f5afedb9c1f56bbb3 --- /dev/null +++ b/spoj_cpp_clean/OFFSIDE/OFFSIDE-12.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +#define mp make_pair +#define pb push_back + +using namespace std; + +int a, d; +int ata[12]; + +int main() +{ + ios::sync_with_stdio(false); + while (cin >> a >> d && a + d) { + priority_queue pq; + int def, def1, def2; + for (int i = 0; i < a; ++i) + cin >> ata[i]; + for (int i = 0; i < d; ++i) { + cin >> def; + pq.push(-def); + } + def1 = -pq.top(); + pq.pop(); + def2 = -pq.top(); + pq.pop(); + bool flag = false; + for (int i = 0; i < a; i++) { + if (ata[i] >= def2 || (ata[i] >= def1 && ata[i] >= def2)) + ; + else { + cout << "Y\n"; + flag = true; + break; + } + } + if (!flag) + cout << "N\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/OFFSIDE/OFFSIDE-16.cpp b/spoj_cpp_clean/OFFSIDE/OFFSIDE-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..adc19222341bc8e5f67dd69d6e8811da1942cfa4 --- /dev/null +++ b/spoj_cpp_clean/OFFSIDE/OFFSIDE-16.cpp @@ -0,0 +1,58 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "<=b;i--) +#define loop(i,a,b) for(long long i=a; i +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(ll n, ll m){ + vll a(n); + vll d(m); + loop(i,0,n) cin>>a[i]; + loop(i,0,m) cin>>d[i]; + sort(a.begin(),a.end()); + sort(d.begin(), d.end()); + if(a[0]>test_cases; + while(true){ + ll a,d; + cin>>a>>d; + if(a==0 and d==0) break; + solve(a,d); + } + return 0; +} diff --git a/spoj_cpp_clean/OFFSIDE/OFFSIDE-5.cpp b/spoj_cpp_clean/OFFSIDE/OFFSIDE-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ed3751139f4856484aeff728bcb543df3acf984 --- /dev/null +++ b/spoj_cpp_clean/OFFSIDE/OFFSIDE-5.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; + +int main() +{ + int a,d,i,j,k; + cin>>a>>d; + while(a!=0) + { + int *arrA=new int[a]; + int *arrD=new int[d]; + for(i=0;i>arrA[i]; + for(i=0;i>arrD[i]; + sort(arrA,arrA+a); + sort(arrD,arrD+d); + if(arrA[0]>=arrD[1]) + cout<<"N"<>a>>d; + } +} diff --git a/spoj_cpp_clean/OFORTUNE/OFORTUNE-3.cpp b/spoj_cpp_clean/OFORTUNE/OFORTUNE-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a92cc0d1c13d78a437df61675698d405d1fd236 --- /dev/null +++ b/spoj_cpp_clean/OFORTUNE/OFORTUNE-3.cpp @@ -0,0 +1,44 @@ +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); +#define max(x,y) ((x) > (y))? (x):(y); + +int main(){ + int m, p, y, n, type, charge, res, p_temp, interest, i, j; + double rate; + sd(m); + while(m--){ + sd(p); + sd(y); + sd(n); + res = 0; + for(i = 0; i < n; i++){ + scanf("%d %lf %d",&type, &rate, &charge); + p_temp = p, interest = 0; + if(type == 0){ + for(j = 0; j < y; j++){ + interest += (p_temp * rate); + p_temp -= charge; + } + res = max(res, p_temp + interest); + } + else{ + for(j = 0; j < y; j++){ + interest = p_temp * rate; + p_temp = p_temp + interest - charge; + } + res = max(res, p_temp); + } + } + printf("%d\n",res); + } + return 0; +} diff --git a/spoj_cpp_clean/OLOLO/OLOLO-16.cpp b/spoj_cpp_clean/OLOLO/OLOLO-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..696a17db9c5de9f3cb677857d71762918eb63931 --- /dev/null +++ b/spoj_cpp_clean/OLOLO/OLOLO-16.cpp @@ -0,0 +1,28 @@ +#include +#define ll long long +#define pb push_back +using namespace std; + +void solve(){ + ll n; + cin>>n; + ll x = 0; + for(ll i=0;i>y; + x=y^x; + } + cout<>t; + while(t--){ + solve(); + } + return 0; diff --git a/spoj_cpp_clean/OLOLO/OLOLO-19.cpp b/spoj_cpp_clean/OLOLO/OLOLO-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18d41e9cdfe65a22cfe318cb1ef2e559fbe167b8 --- /dev/null +++ b/spoj_cpp_clean/OLOLO/OLOLO-19.cpp @@ -0,0 +1,19 @@ +#include +#define endl "\n" +#define ll long long +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + ll ans = 0, t; + scanf("%lld", &t); + ll arr[t]; + for (ll i = 0; i < t; i++) { + scanf("%lld", &arr[i]); + ans ^= arr[i]; + } + cout << ans; + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/OLOLO/OLOLO-3.cpp b/spoj_cpp_clean/OLOLO/OLOLO-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0db8e7b8064e78db0700b27b3834c34539aa4dbf --- /dev/null +++ b/spoj_cpp_clean/OLOLO/OLOLO-3.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int n, t, ans; + sd(n); + while(n--){ + sd(t); + ans ^= t; + } + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[500009]; +int res; +map mp; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + s(n); + res=haint(); + //s(res); + FOR(i,0,n-1) + { + m=haint(); + //s(m); + res=res^m; + } + cout< +#include +#include + +using namespace std; + +int main() +{ + int test,num,out,j,k; + scanf("%lld",&test); + out=0; + while(test--) + { + scanf("%d",&num); + out=out^num; + } + cout< + +using namespace std; + +int main() +{ + int n=0,aux=0; + vector v; + + scanf("%d",&n); + for (int i = 0; i < n; ++i) + { + scanf("%d",&aux); + v.push_back(aux); + } + sort(v.begin(),v.end()); + aux=0; + for (int i = 0; i < v.size(); ++i) + { + aux^=v[i]; + } + printf("%d\n",aux ); + + return 0; +} diff --git a/spoj_cpp_clean/OMWG/OMWG-9.cpp b/spoj_cpp_clean/OMWG/OMWG-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60806adc920bc8127a1830151cbcbeae66d43b9a --- /dev/null +++ b/spoj_cpp_clean/OMWG/OMWG-9.cpp @@ -0,0 +1,27 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +using namespace std; + +int main() +{ + int T, N, M, i; + + scanf("%d", &T); + + while(T--) + { + scanf("%d %d", &N, &M); + printf("%d\n", (N - 1) * M + N * (M - 1)); + } + return 0; +} diff --git a/spoj_cpp_clean/ONEZERO/ONEZERO-3.cpp b/spoj_cpp_clean/ONEZERO/ONEZERO-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0a6a5bad748db9ffb27f03572d6f3a53a85a83c --- /dev/null +++ b/spoj_cpp_clean/ONEZERO/ONEZERO-3.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; +int t, num, states[10000000], n, i; +queue q, empty; +int bfs(){ + int cur; + states[1] = 1%num; + q = empty; + q.push(1); + while(!q.empty()){ + cur = q.front(); + //cout< +#include +#include +#include + +using namespace std; + +int main(void) +{ + int t; + string input; + + cin >> t; + + while(t--) + { + cin >> input; + + queue result; + stack operatr; + + for(int i = 0; i < input.length(); i++) + { + if(input[i] >= 'a' && input[i] <= 'z') + result.push(input[i]); + + switch(input[i]) + { + case '+': + case '-': + case '*': + case '/': + case '^': operatr.push(input[i]); + break; + } + + if(input[i] == ')') + { + result.push(operatr.top()); + operatr.pop(); + } + } + + while(!result.empty()) + { + cout << result.front(); + result.pop(); + } + + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ONP/ONP-12.cpp b/spoj_cpp_clean/ONP/ONP-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25c57f23e0e580448aa387d391c74c7759c540fe --- /dev/null +++ b/spoj_cpp_clean/ONP/ONP-12.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +stack s; +vector output; + +bool checkOperator(char p) +{ + if (p == '+' || p == '-' || p == '*' || p == '/' || p == '^') + return true; + return false; +} + +int hasPrecedence(char p, char q) +{ + if (p == '^' && (q == '+' || q == '-')) + return 1; + if ((p == '+' || p == '-') && q == '^') + return -1; + if (p == '^' && (q == '*' || q == '/')) + return 1; + if ((p == '*' || p == '/') && q == '^') + return -1; + if ((p == '*' || p == '/') && (q == '+' || q == '-')) + return 1; + if ((p == '+' || p == '-') && (q == '*' || q == '/')) + return -1; + return 0; +} + +void setOperator(char op) +{ + queue temp; + if (!s.empty() && s.top() != '(' && hasPrecedence(op, s.top()) <= 0) { + output.push_back(s.top()); + s.pop(); + } + + while (!s.empty() && s.top() != '(' && hasPrecedence(op, s.top()) <= 0) { + temp.push(s.top()); + s.pop(); + } + s.push(op); + + while (!temp.empty()) { + output.push_back(temp.front()); + temp.pop(); + } +} + +int main() +{ + int t; + scanf("%d", &t); + bool blank = false; + getchar(); + + while (t--) { + char ch[1010]; + string input; + output.clear(); + gets(ch); + for (int i = 0; i < strlen(ch); i++) { + if (ch[i] == '(') { + if (input.length() && isdigit(input[input.length() - 1])) + input += '#'; + } else if (isdigit(ch[i])) { + if (input.length() && input[input.length() - 1] == ')') + input += '#'; + } + input += ch[i]; + } + + for (size_t i = 0; i < input.length(); i++) { + if (isdigit(input[i]) || isalpha(input[i])) + output.push_back(input[i]); + else if (checkOperator(input[i])) { + setOperator(input[i]); + } else if (input[i] == '(') + s.push('('); + else if (input[i] == ')') { + while (!s.empty() && s.top() != '(') { + output.push_back(s.top()); + s.pop(); + } + if (!s.empty()) + s.pop(); + } + } + + while (!s.empty()) { + output.push_back(s.top()); + s.pop(); + } + + for (int i = 0; i < output.size(); i++) + if (output[i] != '#') + printf("%c", output[i]); + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/ONP/ONP-13.cpp b/spoj_cpp_clean/ONP/ONP-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3f4485424074d0084fe3e718f1d9bbd6ec0963a --- /dev/null +++ b/spoj_cpp_clean/ONP/ONP-13.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +string f(string &s, int &i){ + if(i >= (int)(s.size()))return ""; + string res = ""; + stack op; + for(int n = s.size(); i 0){ + if(s[i] =='('){ + i++; + (res += f(s,i)) += op.top(), op.pop(); + }else + (res += s[i]) += op.top(), op.pop(); + }else{ + if(s[i] == '('){ + i++; + res += f(s,i); + }else + res += s[i]; + } + } + } + return res; +} + +int main(){ + int T; scanf("%d", &T); + char buffer[1000]; + for(int t=0; t +#include + +#define ll long long +#define ull unsigned long long +#define pb push_back +#define mp make_pair +#define tr(c,it) for(auto it = (c).begin(); it != (c).end(); it++) + +using namespace std; + +void solve(){ + string exp; + vector v{'(','+','-','/','*','^'}; + stack s; + vector ans; + cin>>exp; + for(char c : exp){ + if(find(v.begin(),v.end(),c)!=v.end()){ + s.push(c); + } + else if(isalpha(c)){ + ans.push_back(c); + } + else if(c==')'){ + ans.push_back(s.top()); + s.pop(); + s.pop(); + } + } + for(char u : ans){ + cout<>t; + while(t--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/ONP/ONP-19.cpp b/spoj_cpp_clean/ONP/ONP-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a676636a54ffc476ca71339d19cd77326bb4e9bb --- /dev/null +++ b/spoj_cpp_clean/ONP/ONP-19.cpp @@ -0,0 +1,47 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 + +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int t;cin>>t;while(t--){ + char c,s; + vector signs; + vector sol; + int var=0, vsign=0; + do{ + cin>>c; + if(c=='('){ + var++; + continue; + } + else if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^' || c=='%' || c=='?' || c=='_' || c=='~' || c=='|' || c==';'){ + signs.push_back(c); + vsign++; + } + else if(c==')'){ + var--; + s = signs.at(vsign-1); + sol.push_back(s); + signs.pop_back(); + vsign--; + } + else{ + sol.push_back(c); + } + }while(var!=0); + + for(auto it:sol){ + cout< specials > tree + difficulty: medium + date: 04/Mar/2020 + problem: infix to postfix conversion + hint: see that the given expression is the in-order traversal in a binary tree, then print post-order traversal recursively without building the tree + by: @brpapa +*/ +#include +using namespace std; + +/* +(a+(b*c)) + + + + / \ + a * + / \ + b c +*/ + +bool isOperator(char c) { + return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'; +} + +string exp; +string rpn(int l, int r) { + // substring [l .. r] de exp + + if (l == r) return string(1, exp[l]); + + int brackets = 0; + for (int k = l; k <= r; k++) { + if (exp[k] == '(') brackets++; + if (exp[k] == ')') brackets--; + + if (brackets == 0 && isOperator(exp[k])) + return rpn(l, k-1) + rpn(k+1, r) + exp[k]; + } + + if (exp[l] == '(' && exp[r] == ')') + return rpn(l+1, r-1); +} + +int main() { + int T; cin >> T; + while (T--) { + cin >> exp; + cout << rpn(0, exp.size()-1) << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/ONP/ONP-9.cpp b/spoj_cpp_clean/ONP/ONP-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..050fbdefa36993bc9d8e9e76254fbb1e21724495 --- /dev/null +++ b/spoj_cpp_clean/ONP/ONP-9.cpp @@ -0,0 +1,51 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +stack S; +char str[401]; + +int main() +{ + int T, i; + + //freopen("input.txt","r",stdin); + scanf("%d", &T); + + while(T--) + { + scanf("%s\n", str); + + for(i=0; i='a' && str[i]<='z') + printf("%c", str[i]); + else + S.push(str[i]); + } + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/OPCPIZZA/OPCPIZZA-3.cpp b/spoj_cpp_clean/OPCPIZZA/OPCPIZZA-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..644b5f6570bdc20cc46c2ce93025904cf6097fb6 --- /dev/null +++ b/spoj_cpp_clean/OPCPIZZA/OPCPIZZA-3.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; +const int MAX = 100000; +int a[MAX]; +int main() +{ + int n, m, i, j, ans, test; + scanf("%d", &test); + while(test--) + { + scanf("%d %d", &n, &m); + for(i = 0; i < n; i++) + scanf("%d", &a[i]); + sort(a, a + n); + for(i = ans = 0; i < n; i++) + { + j = lower_bound(a, a + n, m - a[i]) - a; + if(j != i && j != n && a[j] == m - a[i]) + ans++; + } + printf("%d\n", ans >> 1); + } + return 0; +} diff --git a/spoj_cpp_clean/ORDERS/ORDERS-14.cpp b/spoj_cpp_clean/ORDERS/ORDERS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..744da3276ab919ecc472933788fa7e06d389edc1 --- /dev/null +++ b/spoj_cpp_clean/ORDERS/ORDERS-14.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +using namespace std; + +#define ll long long +ll val[200009]; + +int main() + +{ + ll t; + cin >> t; + while(t--) { + vector v; + ll n; + scanf("%lld", &n); + v.push_back(0); + for(ll i = 1; i <= n; i++) { + ll in; + scanf("%lld", &in); + ll pos = i - in; + v.insert(v.begin() + pos, i); + } + + for(ll i = 1; i <= n; i++) { + val[ v[i] ] = i; + } + + for(ll i = 1; i <= n; i++) { + if(i != 1) + printf(" "); + printf("%lld", val[i]); + } + printf("\n"); + + } + + return 0; +} + diff --git a/spoj_cpp_clean/ORDERSET/ORDERSET-9.cpp b/spoj_cpp_clean/ORDERSET/ORDERSET-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f1af25cae6ec6d6aafd97b3f10d8859f22da5df --- /dev/null +++ b/spoj_cpp_clean/ORDERSET/ORDERSET-9.cpp @@ -0,0 +1,173 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +int N; + +unordered_map link, reverse_link; +int RAW_INPUT[200007]; +char type[200007]; +bool exist[200007]; +int tree[200007]; + +inline int FAST_IO() +{ + char ch; + int val = 0; + bool neg = 0; + + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + if (ch == '-') + { + neg = 1; + ch = gc(); + } + + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + + if(neg) + return -val; + + return val; + +} + +inline char FAST_CHAR() +{ + char ch; + + ch = gc(); + + while (isspace(ch)) + ch = gc(); + + return ch; +} + +inline void point_update(int pos, int val) +{ + for( ; pos <= 200006; pos += pos & (-pos)) + tree[pos] += val; +} + +inline void range_update(int i, int j, int val) +{ + point_update(i, val); + point_update(j + 1, -val); +} + +inline int query(int pos) +{ + int sum = 0; + + for( ; pos > 0; pos -= pos & (-pos)) + sum += tree[pos]; + return sum; +} + +void init() +{ + int i, t=2; + int temp[200007]; + //scanf("%d\n", &N); + N = FAST_IO(); + + for(i=0; i= RAW_INPUT[i]) + up = (down+up)/2; + else if(ans < RAW_INPUT[i]) + down = (down+up)/2; + } + printf("%d\n", reverse_link[down]); + } + break; + } + return 0; +} diff --git a/spoj_cpp_clean/ORDSUM23/ORDSUM23-9.cpp b/spoj_cpp_clean/ORDSUM23/ORDSUM23-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcf90a0b3b8c43617fcee581468efa1e14dbeec6 --- /dev/null +++ b/spoj_cpp_clean/ORDSUM23/ORDSUM23-9.cpp @@ -0,0 +1,47 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +#define MOD 1000000007 +using namespace std; + +int DP[1000007]; + +inline void init() +{ + int i; + + DP[2] = DP[3] = 1; + + for (i = 4; i <= 1000000; i++) + { + DP[i] = DP[i - 3] + DP[i - 2]; + if (DP[i] >= MOD) + DP[i] -= MOD; + } +} + +int main() +{ + int num, T; + + init(); + scanf("%d", &T); + + while (T--) + { + scanf("%d", &num); + printf("%d\n", DP[num]); + } + + return 0; +} diff --git a/spoj_cpp_clean/PAIRS1/PAIRS1-21.cpp b/spoj_cpp_clean/PAIRS1/PAIRS1-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e90792322508216209265e76a1e077573e4ffed --- /dev/null +++ b/spoj_cpp_clean/PAIRS1/PAIRS1-21.cpp @@ -0,0 +1,25 @@ +/* + miscellaneous > binary search + difficulty: easy + date: 02/Mar/2020 + problem: given the A array, count the quantity of pairs i < j such that A[i]-A[j] == K + hint: search on the sorted array A by the A[n]+K, for all n in [0 .. N-1] + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int main() { + int N, K; cin >> N >> K; + vector A(N); for (int &a : A) cin >> a; + sort(A.begin(), A.end()); + + int count = 0; + for (int n = 0; n < N; n++) + if (binary_search(A.begin()+n, A.end(), A[n]+K)) + count++; + cout << count << endl; + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/PALIN/PALIN-11.cpp b/spoj_cpp_clean/PALIN/PALIN-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8cd6ca0800a17900956c5e84dd0f07314abb8879 --- /dev/null +++ b/spoj_cpp_clean/PALIN/PALIN-11.cpp @@ -0,0 +1,137 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include +#include + +using namespace std; + +long processLeadingZeros(string &input, long len) +{ + long nonZeroIndex = 0l; + + for(long i = 0; i < len; i++) + { + if(input[i] != '0') + { + nonZeroIndex = i; + break; + } + } + + if(nonZeroIndex != 0l) + { + long i = 0l; + + while(nonZeroIndex < len) + input[i++] = input[nonZeroIndex++]; + + len = i; + } + + return len; +} + +bool checkForAllNine(string &input, long len) +{ + bool flag = true; + + for(long i = 0l; i < len; i++) + { + if(input[i] != '9') + { + flag = false; + break; + } + } + + return flag; +} + +bool checkIfGreaterAfterMakingPallindrome(string &input, long len) +{ + bool flag = false; + + for(long i = 0l; i < len / 2l; i++) + { + if(input[i] > input[len-i-1l]) + flag = true; + else if(input[i] < input[len-i-1l]) + flag = false; + + input[len-i-1l] = input[i]; + } + + return flag; +} + +int main(void) +{ + int t; + long l, h, len; + string input; + bool flag; + + cin >> t; + + while(t--) + { + cin >> input; + + len = input.length(); + + len = processLeadingZeros(input, len); + + flag = checkForAllNine(input, len); + + if(flag) + { + cout << "1"; + + for(long i = 0l; i < len-1l; i++) + cout << "0"; + + cout << "1" << endl; + continue; + } + + if(len == 1l) + { + input[0]++; + cout << input[0] << endl; + continue; + } + + flag = checkIfGreaterAfterMakingPallindrome(input, len); + + if(!flag) + { + l = (len - 1l) / 2l; + h = len / 2l; + + while(l >= 0 && h < len) + { + if(input[l] == '9') + input[l] = input[h] = '0'; + else + { + input[l] += 1; + input[h] = input[l]; + break; + } + + l--; + h++; + } + } + + for(l = 0l; l < len; l++) + cout << input[l]; + + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/PALIN/PALIN-16.cpp b/spoj_cpp_clean/PALIN/PALIN-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7032ead709a5b49c9245b660fc3c48cc3774727c --- /dev/null +++ b/spoj_cpp_clean/PALIN/PALIN-16.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +bool allnine(string s){ + int n = s.length(); + for(int i=0;i>s; + int n = s.length(); + if(allnine(s)){ + cout<<1; + for(int i=0;i a(n); + for(int i=0;i=0){ + mid1--; + mid2++; + } + if(mid1<0||a[mid1] left + int mid=n/2, carry=1; + if(n%2==0) mid-=1; + while(mid>=0){ + a[mid]+=carry; + carry=a[mid]/10; + a[mid]%=10; + mid--; + } + } + for(int i=0;i>t; + while(t--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/PALIN/PALIN-19.cpp b/spoj_cpp_clean/PALIN/PALIN-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7afc14c4467cac24bcb181d5ed35b102c21063a --- /dev/null +++ b/spoj_cpp_clean/PALIN/PALIN-19.cpp @@ -0,0 +1,69 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 1000001 + +using namespace std; + +void palin(string& s) +{ + ll len = s.length(); + ll flag = 1; + for (ll i = 0; i < len; i++) { + if (s[i] != '9') { + flag = 0; + break; + } + } + if (flag == 1) { + s[0] = '1'; + for (ll i = 1; i < len; i++) { + s[i] = '0'; + } + s = s + "1"; + } + else { + ll temp, z; + for (ll i = 0; i < len / 2; i++) { + if (s[i] > s[len - i - 1]) { + flag = -1; + } + else if (s[i] < s[len - i - 1]) { + flag = 1; + } + s[len - i - 1] = s[i]; + } + if (flag == 0 || flag == 1) { + if (len % 2 == 0) { + temp = (len / 2) - 1; + } + else { + temp = len / 2; + } + z = 0; + while (s[temp - z] == '9') { + s[temp - z] = '0'; + s[len - 1 - temp + z] = '0'; + z++; + } + s[temp - z]++; + s[len - 1 - temp + z] = s[temp - z]; + } + } +} + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + string p; + ll t; cin >> t; + while (t--) + { + cin >> p; + palin(p); + cout << p << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/PALIN/PALIN-4.cpp b/spoj_cpp_clean/PALIN/PALIN-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7265a7491251c8c493cceca86f05d3af056feb0e --- /dev/null +++ b/spoj_cpp_clean/PALIN/PALIN-4.cpp @@ -0,0 +1,129 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +//Other Includes +#include +#include +#include +#include +#include +#include +#include +#include +//some common functionn +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define all(c) (c).begin(),(c).end() +// Input macros +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) + +// Output macros +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +using namespace std; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;istr3) + { + cout<>s; + + return true; +} + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + + int T=1; + s(T); + for(int testnum=1;testnum<=T;testnum++) + { + if(!input()) break; + + solve(); + //printf("\n"); + + } +// system("pause"); + return 0; +} + + + + diff --git a/spoj_cpp_clean/PALIN/PALIN-8.cpp b/spoj_cpp_clean/PALIN/PALIN-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2d6b42cf3deb966f22235e0daefbc4c844b7cfc --- /dev/null +++ b/spoj_cpp_clean/PALIN/PALIN-8.cpp @@ -0,0 +1,161 @@ +/*************************************************** + * Problem Name : PALIN - The Next Palindrome.cpp + * Problem Link : https://www.spoj.com/problems/PALIN/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-08-08 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int AreAll9s ( int *num, int n ) { + int i; + + for ( i = 0; i < n; ++i ) { + if ( num[i] != 9 ) { + return 0; + } + } + + return 1; +} +void printArray (int arr[], int n) { + int i; + + for (i = 0; i < n; i++) { + cout << arr[i]; + } + + nl; +} +void generateNextPalindromeUtil (int num[], int n ) { + int mid = n / 2; + bool leftsmaller = false; + int i = mid - 1; + int j = (n % 2) ? mid + 1 : mid; + + while (i >= 0 && num[i] == num[j]) { + i--, j++; + } + + if ( i < 0 || num[i] < num[j]) { + leftsmaller = true; + } + + while (i >= 0) { + num[j] = num[i]; + j++; + i--; + } + + if (leftsmaller == true) { + int carry = 1; + i = mid - 1; + + if (n % 2 == 1) { + num[mid] += carry; + carry = num[mid] / 10; + num[mid] %= 10; + j = mid + 1; + + } else { + j = mid; + } + + while (i >= 0) { + num[i] += carry; + carry = num[i] / 10; + num[i] %= 10; + num[j++] = num[i--]; + } + } +} +void generateNextPalindrome ( int num[], int n ) { + int i; + + if ( AreAll9s ( num, n ) ) { + cout << "1"; + + for ( i = 1; i < n; i++ ) { + cout << "0"; + } + + cout << "1\n"; + + } else { + generateNextPalindromeUtil ( num, n ); + printArray (num, n); + } +} + +int main () { + __FastIO; + int tc; + string k; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> k; + int n = (int) k.size(); + int ar[n + 5]; + + for (int i = 0; i < n; i++) { + ar[i] = k[i] - '0'; + } + + generateNextPalindrome (ar, n); + } + + return 0; +} diff --git a/spoj_cpp_clean/PALIN2/PALIN2-5.cpp b/spoj_cpp_clean/PALIN2/PALIN2-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f05a582e77dbc8679d5dca7a50d5bd57836c678a --- /dev/null +++ b/spoj_cpp_clean/PALIN2/PALIN2-5.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +using namespace std; + +int main() +{ + int t,len,centre,left,right,l,r,i; + string s; + + cin>>t; + + while(t--) + { + char ch=NULL; + string s; + cin>>s; + len=s.size(); + + if(len%2) + { + centre=len/2; + left=centre-1; + right=centre+1; + } + else + { + centre=len/2; + left=centre-1; + right=centre; + } + + while(left>=0 && s[left]==s[right]) + { + left--; + right++; + } + + if(left<0) + { + if(len%2) + { + if(s[centre]!=57) + s[centre]++; + else + { + s[centre]=48; + l=centre-1; + r=centre+1; + while(s[l]==57 && s[r]==57 && l>=0) + { + s[l]=s[r]=48; + l--; + r++; + } + if(l<0) + { + s[0]=49; + ch=49; + } + else + { + s[l]++; + s[r]=s[l]; + } + } + } + else + { + + left=centre-1; // if 0 is in the centre, include also that case... + right=centre; + while(left>=0) + { + if(s[left]!=57) + { + s[left]++; + s[right]=s[left]; + break; + } + else + { + s[left]=s[right]=48; + right++; + left--; + } + } + if(left<0) + { + s[0]=49; + ch=49; + } + + } + } + else + { + if(s[right] < s[left]) + while(left>=0) + { + s[right]=s[left]; + left--; + right++; + } + else + { + centre=len/2; + if(len%2) + { + left=centre-1; + right=centre+1; + } + else + { + left=centre-1; + right=centre; + } + if(len%2 && s[centre]!=57) + { + s[centre]++; + } + else + while(left>=0) + { + if(s[left]!=57) + { + s[right]=s[left]=s[left]+1; + if(len%2) + s[centre]=48; + break; + } + else + { + s[left]=s[right]=48; + left--; + right++; + } + } + while(left>=0) + { + s[right]=s[left]; + left--; + right++; + } + + + + + } + + } + if(ch) + cout< +//#include +#include +//#include + +using namespace std; + +int main(){ + + int test_cases; + cin >> test_cases; + + while(test_cases){ + + string palindrome; + cin >> palindrome; + int length = palindrome.length(); + + //need to make it uniform at both ends + //start copying from the start till the end + int i = 0, j = length-1; + bool number_increased = false; + while(i<=j){ + if(palindrome[i] > palindrome[j]){ + palindrome[j] = palindrome[i]; + number_increased = true; + } + else if(palindrome[i]=0 && i=0 && palindrome[i]=='9'){ + //move towards the start and replace it with 0 + palindrome[i] = '0'; + palindrome[length-1-i] = '0'; + i--; + } + + if(i>=0){ + //means reached a digit which is not 9 + palindrome[i]++; + palindrome[length-i-1] = palindrome[i]; + } + else{ + //reached the start + //need to insert a 1 before the string, 1+ 00000 +1 + palindrome = '1' + palindrome; + palindrome[palindrome.length()-1] = '1'; + } + } + } + + } + cout << palindrome << endl; + test_cases--; + } + + return 0; +} diff --git a/spoj_cpp_clean/PARADOX/PARADOX-3.cpp b/spoj_cpp_clean/PARADOX/PARADOX-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..461a0bc655e559394f3887866a30da98b0adc089 --- /dev/null +++ b/spoj_cpp_clean/PARADOX/PARADOX-3.cpp @@ -0,0 +1,67 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it) +#define meta __FUNCTION__,__LINE__ + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +using namespace std; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void tr(S x, const Strings&... rest){cout< pii; + +const int N = 100; + +int n; + +vector g[N]; +int in[N]; + +int main(){ _ + cin >> n; + while(n){ + for(int i = 1; i <= n; i++){ + g[i].clear(); + in[i] = 0; + } + + int x; string s; + + for(int i = 1; i <= n; i++){ + cin >> x >> s; + g[i].pb(mp(x,s[0] == 't')); + in[x]++; + } + + for(int i = 1; i <= n; i++){ + if(!in[i]){ + + } + } + + + cin >> n; + } + return 0; +} + diff --git a/spoj_cpp_clean/PARIDADE/PARIDADE-10.cpp b/spoj_cpp_clean/PARIDADE/PARIDADE-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..12a1516e762426e2c231ff72dd57c32e3009ef17 --- /dev/null +++ b/spoj_cpp_clean/PARIDADE/PARIDADE-10.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + unsigned int n; + while(cin >> n && n != 0) + { + bitset<32> bits(n); + string res; + if(bits.count() != 0) { + res = bits.to_string(); + size_t f = res.find('1'); + if(f != string::npos) res.erase(0, f); + } + cout << "The parity of " << res << " is " << bits.count() << " (mod 2)." << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/PARTIT/PARTIT-14.cpp b/spoj_cpp_clean/PARTIT/PARTIT-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05fdd4d41baf517af8c5ec4164cf5b13bd29d1b9 --- /dev/null +++ b/spoj_cpp_clean/PARTIT/PARTIT-14.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + +#define ll long long + +ll n, m, k; +ll dp[230][230][12]; + +ll solve(ll start, ll tot, ll len) + +{ + ll &ret = dp[start][tot][len]; + if(ret != -1) + return ret; + + if(len == 1) + return ret = (start == tot); + ll rt = 0; + for(ll i = start; i * (len - 1) + start <= tot; i++) + rt += solve(i, tot - start, len - 1); + + return ret = rt; +} + +int main() + +{ + memset(dp, -1, sizeof(dp)); + ll t; + cin >> t; + while(t--) { + scanf("%lld %lld %lld", &m, &n, &k); + + ll num = 1; + while(1) { + ll tmp; + while((tmp = solve(num, m, n)) < k) { + k -= tmp; + num++; + } + + m -= num; + n--; + printf("%lld ", num); + + if(n == 0) + break; + } + + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/PARTY/PARTY-13.cpp b/spoj_cpp_clean/PARTY/PARTY-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..546790b6cba0ce4e28629df02fdc35c6eefd0a7a --- /dev/null +++ b/spoj_cpp_clean/PARTY/PARTY-13.cpp @@ -0,0 +1,76 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; +const int INF = 0x3f3f3f3f; + +int memo[110][550]; +bool mark[110][550]; + +int f(const vector < pair > &v, int i, int k){ + if(i >= v.size() || k <= 0)return 0; + int &best = memo[i][k]; + if(mark[i][k])return best; + mark[i][k] = true; + best = f(v, i + 1, k); + if(v[i].first <= k){ + int next = f(v, i+1, k-v[i].first)+v[i].second; + best = max(best, next); + } + return best; +} + +int g(const vector < pair > &v, int i, int fun){ + if(fun == 0)return 0; + if(i >= v.size())return INF; + int &best = memo[i][fun]; + if(mark[i][fun])return best; + mark[i][fun] = true; + best = g(v, i+1, fun); + best = min(best, g(v, i+1, fun-v[i].second)+v[i].first); + return best; +} +int main(){ + int n, p; + while(scanf("%d %d", &p, &n)==2 && (p || n)){ + vector < pair > v(n); + memset(mark, false,sizeof(mark)); + for(int i=0; i +#define hell 1000000009 +#define bug1(x) cout<<"$ "<=b;i--) +#define loop(i,a,b) for(long long i=a; i +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +int W, n; + +void solve(){ + int dp[n+1][W+1]; + loop(i,0,n+1){ + dp[i][0]=0; + } + loop(i,0,W+1){ + dp[0][i]=0; + } + int w, va; + vector wt(n+1); + vector val(n+1); + for(int i=1;i<=n;i++){ + cin>>w>>va; + wt[i]=w; + val[i]=va; + } + for(int i=1;i<=n;i++){ + for(int j=1;j<=W;j++){ + if(j=dp[n][W]){ + W_ans=j; + } + } + + int maxfun=dp[n][W]; + int minmoney=0; + for(int i=0;i<=W;i++) + { + if(dp[n][i]==maxfun) + { + minmoney=i; + break; + } + } + cout<>test_cases; + while(true){ + cin>>W>>n; + if(W==0 and n==0) break; + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/PARTY/PARTY-4.cpp b/spoj_cpp_clean/PARTY/PARTY-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6014aa8af58cbba78a28c87bfbb886d39821c8a --- /dev/null +++ b/spoj_cpp_clean/PARTY/PARTY-4.cpp @@ -0,0 +1,118 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int dp[555][509]; +int cost[109], fun[109]; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + int Budget; + while(cin>>Budget>>n) + { + if(n==0 && Budget==0) + break; + + FOR(i,0,n) + s(cost[i]), s(fun[i]); + FOR(i,0,n+1) + { + FOR(j,0,Budget+1) + { + if(i==0 || j==0) + dp[i][j]=0; + else if( cost[i-1]<=j) + dp[i][j]= maX( fun[i-1]+dp[i-1][j-cost[i-1]], dp[i-1][j]); + else + dp[i][j]=dp[i-1][j]; + } + } + int a1; + FORs(i,Budget,0) + { + if(dp[n][i]>=dp[n][Budget]) + a1=i; + } + cout< +#include + +using namespace std; + +int main() +{ + int budget, parties; + cin>>budget>>parties; + while(budget>0) + { + int fun[parties],cost[parties]; + for(int i=0;i>cost[i]>>fun[i]; + } + int dp[parties+1][budget+1]; + for(int i=0;i<=parties;i++) + { + for(int w=0;w<=budget;w++) + { + if(i==0 || w==0) + dp[i][w]=0; + else if(cost[i-1]<=w) + dp[i][w]=max(fun[i-1]+dp[i-1][w-cost[i-1]],dp[i-1][w]); + else + dp[i][w]=dp[i-1][w]; + } + } + int j=budget; + do + { + if(dp[parties][j-1]==dp[parties][j]) + j--; + else + { + cout<>budget>>parties; + } + return 0; +} diff --git a/spoj_cpp_clean/PARTY/PARTY-9.cpp b/spoj_cpp_clean/PARTY/PARTY-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ec0df8495cf85ca1eb55699da62a940d405f900 --- /dev/null +++ b/spoj_cpp_clean/PARTY/PARTY-9.cpp @@ -0,0 +1,74 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#define min(a,b) ((aDP[i][j-1]) + { + DP[i][j]=FUN[j]+DP[i-COST[j]][j-1]; + MONEY[i][j]=COST[j] + MONEY[i-COST[j]][j-1]; + } + + else if(FUN[j]+DP[i-COST[j]][j-1]==DP[i][j-1]) + { + DP[i][j]=DP[i][j-1]; + MONEY[i][j]=min(COST[j] + MONEY[i-COST[j]][j-1], MONEY[i][j-1]); + } + + else + { + DP[i][j]=DP[i][j-1]; + MONEY[i][j]=MONEY[i][j-1]; + } + } + else + { + DP[i][j]=DP[i][j-1]; + MONEY[i][j]=MONEY[i][j-1]; + } + } + + printf("%d %d\n", MONEY[budget][N], DP[budget][N]); + scanf("%d %d", &budget, &N); + } + + return 0; +} diff --git a/spoj_cpp_clean/PATHGAME/PATHGAME-9.cpp b/spoj_cpp_clean/PATHGAME/PATHGAME-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c774760a4c009ad04f219f14aa0543d22fd6b825 --- /dev/null +++ b/spoj_cpp_clean/PATHGAME/PATHGAME-9.cpp @@ -0,0 +1,119 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar() + +using namespace std; + +int V[3][57]; +int board[4][57]; +int N, blacks; + +inline void init() +{ + int i; + char c; + + memset(V, 0, sizeof(V)); + memset(board, 0, sizeof(board)); + blacks = 0; + + scanf("%d\n", &N); + + for (i = 1; i <= N; i++) + { + c = gc(); + if (c == '.') + board[1][i] = 1; + else + blacks++; + } + + gc(); + + for (i = 1; i <= N; i++) + { + c = gc(); + if (c == '.') + board[2][i] = 1; + else + blacks++; + } +} + +inline int BFS() +{ + deque< pair > >Q; + pair >curr; //len, x, y + + if (board[1][1]) + { + Q.push_back(make_pair(1, make_pair(1, 1))); + V[1][1] = 1; + } + + if (board[2][1]) + { + Q.push_back(make_pair(1, make_pair(2, 1))); + V[2][1] = 1; + } + + while (!Q.empty()) + { + curr = Q.front(); + Q.pop_front(); + + if (curr.second.second == N) + return curr.first; + + if (board[curr.second.first - 1][curr.second.second] && !V[curr.second.first - 1][curr.second.second]) + { + V[curr.second.first - 1][curr.second.second] = 1; + Q.push_back(make_pair(curr.first + 1, make_pair(curr.second.first - 1, curr.second.second))); + } + + if (board[curr.second.first + 1][curr.second.second] && !V[curr.second.first + 1][curr.second.second]) + { + V[curr.second.first + 1][curr.second.second] = 1; + Q.push_back(make_pair(curr.first + 1, make_pair(curr.second.first + 1, curr.second.second))); + } + + if (board[curr.second.first][curr.second.second + 1] && !V[curr.second.first][curr.second.second + 1]) + { + V[curr.second.first][curr.second.second + 1] = 1; + Q.push_back(make_pair(curr.first + 1, make_pair(curr.second.first, curr.second.second + 1))); + } + } + + return -1; +} + +int main() +{ + int T; + + //freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%d\n", N * 2 - BFS() - blacks); + } + + return 0; +} diff --git a/spoj_cpp_clean/PCROSS1/PCROSS1-2.cpp b/spoj_cpp_clean/PCROSS1/PCROSS1-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fa8033ce8ba03ca14a61b1ea77a219a0428539e --- /dev/null +++ b/spoj_cpp_clean/PCROSS1/PCROSS1-2.cpp @@ -0,0 +1,60 @@ +// https://www.spoj.com/problems/PCROSS1/ +// PCROSS1 - Cross Pattern (Act 1) +#include + +int main() +{ + int t(0), m(0), n(0), ci(0), cj(0); + + std::cin >> t; + while(t >= 1 && t <= 100) { + + std::cin >> m >> n >> ci >> cj; + if (m < 1 || m > 100) break; + if (n < 1 || n > 100) break; + if (ci < 1 || ci > m) break; + if (cj < 1 || cj > n) break; + + std::string line = std::string(n, char(46)); + std::string center = std::string(n, char(42)); + line[cj - 1] = char(42); + + for (int i = 1; i <= m; ++i) { + if (i == ci) + std::cout << center << '\n'; + else + std::cout << line << '\n'; + } + t--; + } + + return 0; +} + +/* +Input: + +3 +1 1 1 1 +5 5 3 3 +8 13 3 5 + +Output: + +* + +..*.. +..*.. +***** +..*.. +..*.. + +....*........ +....*........ +************* +....*........ +....*........ +....*........ +....*........ +....*........ +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/PCROSS2/PCROSS2-2.cpp b/spoj_cpp_clean/PCROSS2/PCROSS2-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a491b5dc02daeedd2831e0f0e1c9f093667ad5cd --- /dev/null +++ b/spoj_cpp_clean/PCROSS2/PCROSS2-2.cpp @@ -0,0 +1,58 @@ +// https://www.spoj.com/problems/PCROSS2/ +// PCROSS2 - Cross Pattern (Act 2) +#include +using std::cin; +using std::cout; + +int main() { + + int t(0), m(0), n(0), ci(0), cj(0); + + cin >> t; + while(t >= 1 && t <= 100) { + cin >> m >> n >> ci >> cj; + + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + if ((i + j) == (ci + cj) || (i - j) == (ci - cj)) { + cout << char(42); + } else { + cout << char(46); + } + } + cout << '\n'; + } + cout << '\n'; + t--; + } + return 0; +} + +/* +Example + +Input: +3 +1 1 1 1 +5 5 3 3 +8 13 3 5 + +Output: +* + +*...* +.*.*. +..*.. +.*.*. +*...* + +..*...*...... +...*.*....... +....*........ +...*.*....... +..*...*...... +.*.....*..... +*.......*.... +.........*... + +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/PERMUT1/PERMUT1-3.cpp b/spoj_cpp_clean/PERMUT1/PERMUT1-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0243e801a72c0f212a3a90406cfb3114bf1724df --- /dev/null +++ b/spoj_cpp_clean/PERMUT1/PERMUT1-3.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int d, n, k, i, j; + sd(d); + long long dp[13][99]; + memset(dp, 0, sizeof(dp)); + for(i = 1; i < 13; i++) + dp[i][0] = 1; + for(i = 1; i <= 98; i++) + dp[1][i] = 0; + for(i = 2; i<= 12; i++){ + for(j = 0; j <= 98; j++) + if(j - + 1 < 0) + dp[i][j] = dp[i][j-1] + dp[i-1][j]; + else + for(k = j - i + 1; k <= j; k++) + dp[i][j] += dp[i-1][k]; + } + + /*for(i = 1; i <=5; i++){ + for(j = 0; j <= 10; j++) + cout< + +using namespace std; + +int main(void) +{ + int n; + int input[100002]; + + cin >> n; + + while(n) + { + for(int i = 1; i <= n; i++) + cin >> input[i]; + + int j; + for(j = 1; j <= n; j++) + if(j != input[j] && j != input[input[j]]) + break; + + if(j == n+1) + cout << "ambiguous" << endl; + else + cout << "not ambiguous" << endl; + + cin >> n; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/PERMUT2/PERMUT2-16.cpp b/spoj_cpp_clean/PERMUT2/PERMUT2-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fde0965f0ff84c2dc8f31510951beeec9564859 --- /dev/null +++ b/spoj_cpp_clean/PERMUT2/PERMUT2-16.cpp @@ -0,0 +1,62 @@ + +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "< +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +void solve(int n){ + vi a(n+1); + loop(i,1,n+1){ + cin>>a[i]; + + } + vi b(n+1); + loop(i,1,n+1){ + b[a[i]]=i; + } + for(int i=1;i<=n;i++){ + if(a[i]!=b[i]){ + cout<<"not ambiguous"<>test_cases; + while(true){ + int n; + cin>>n; + if(n==0) break; + solve(n); + } + return 0; +} diff --git a/spoj_cpp_clean/PERMUT2/PERMUT2-19.cpp b/spoj_cpp_clean/PERMUT2/PERMUT2-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd58aa763860de92beaa605f357c464ea23e3cf3 --- /dev/null +++ b/spoj_cpp_clean/PERMUT2/PERMUT2-19.cpp @@ -0,0 +1,37 @@ +#include +#define endl "\n" +#define ll long long + +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + while (true) { + ll n; + cin >> n; + if (n == 0) { + return 0; + } + ll arr[n + 1]; + arr[0] = 0; + for (ll i = 1; i < n + 1; i++) { + cin >> arr[i]; + } + bool pos = true; + for (ll i = 1; i < n + 1; i++) { + if (arr[i] != i && arr[arr[i]] != i) { + pos = false; + break; + } + } + if (pos == false) { + cout << "not ambiguous" << endl; + } + else { + cout << "ambiguous" << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/PERMUT2/PERMUT2-5.cpp b/spoj_cpp_clean/PERMUT2/PERMUT2-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9a5bc3896381b442933828ae3b6378e6e860b68 --- /dev/null +++ b/spoj_cpp_clean/PERMUT2/PERMUT2-5.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int ambiguity; + int n,i,j,k; + cin>>n; + while(n) + { + int *arr=new int[n]; + //int *arr2=new int[n]; + for(i=0;i>arr[i]; + //arr2[i]=i+1; + } + for(i=0;i>n; + } + + return 0; + +} diff --git a/spoj_cpp_clean/PFDEP/PFDEP-3.cpp b/spoj_cpp_clean/PFDEP/PFDEP-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6328c7ff2f23e4162d1a699eb3ed943473de893 --- /dev/null +++ b/spoj_cpp_clean/PFDEP/PFDEP-3.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int m, n, k, a, b, indegree[101]; +vector adj_list[101]; +priority_queue q; + +int main(){ + memset(indegree, 0, sizeof(indegree)); + sd2(n,m); + while(m--){ + sd(a); + sd(k); + indegree[a] += k; + while(k--){ + sd(b); + adj_list[b].push_back(a); + } + } + for(int i = 1; i <= n; i++){ + if(indegree[i] == 0) + q.push(-i); + } + while(!q.empty()){ + a = -q.top(); + q.pop(); + printf("%d ", a); + for(int i = 0; i < adj_list[a].size(); i++){ + indegree[adj_list[a][i]]--; + if(indegree[adj_list[a][i]] == 0) + q.push(-adj_list[a][i]); + } + } + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar_unlocked() +#define MOD 1000000007 + +using namespace std; + +int N, E; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +inline int solve() +{ + int i, j, f, s; + + N = FAST_IO(); + E = FAST_IO(); + //scanf("%d %d", &N, &E); + + vector< vector > G(N+1); + vector DP(N+1); + + for(i = 0; i < E; i++) + { + f = FAST_IO(); + s = FAST_IO(); + //scanf("%d %d", &f, &s); + G[f].push_back(s); + } + + DP[1] = 1; + + for(i = 1; i < N; i++) + for(j = 0; j < G[i].size(); j++) + { + DP[G[i][j]] += DP[i]; + while(DP[G[i][j]] >= MOD) + DP[G[i][j]] -= MOD; + } + + while(DP[N] >= MOD) + DP[N] -= MOD; + + return DP[N]; +} + +int main() +{ + int T, i; + + //freopen("input.txt","r",stdin); + + T = FAST_IO(); + //scanf("%d", &T); + + for(i = 1; i <= T; i++) + printf("Case %d: %d\n", i, solve()); + + return 0; +} diff --git a/spoj_cpp_clean/PHONELST/PHONELST-4.cpp b/spoj_cpp_clean/PHONELST/PHONELST-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b99e87bb3289d10e6208a95107e40aed2eadb39 --- /dev/null +++ b/spoj_cpp_clean/PHONELST/PHONELST-4.cpp @@ -0,0 +1,194 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int arr[20]; +struct node +{ + int count; + node *next[10]; +} *root; +node* newnode() +{ + node *nd; + nd=new node; + nd->count=0; + FOR(i,0,10)nd->next[i]=NULL; + return nd; +} +void ins(string inp) +{ + if(root==NULL) + { + root=newnode(); + } + node *rot=root; + int flag=0; + FOR(i,0,inp.length()) + { + int curr=inp[i]-'0'; + if(rot->next[curr]==NULL) + { + flag=1; + rot->next[curr]=newnode(); + rot=rot->next[curr]; + } + else + { + rot=rot->next[curr]; + } + } + rot->count++; +} +bool chek(string inp) +{ + node *rot=root; + FOR(i,0,inp.length()) + { + int curr=inp[i]-'0'; + if(rot->next[curr]->count!=0) + { + if(i!=inp.length()-1) + return false; + else if(rot->count>1) + return false; + + } + rot=rot->next[curr]; + } + return true; +} +/* +void output(int p) +{ + FOR(i,0,p+1) + cout<next[i]!=NULL) + { + //error(i); + arr[p]=i; + if(root->next[i]->tell) + { + //error(p); + output(p); + } + disp(root->next[i], p+1); + } + +}*/ +string a[10009]; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T, flag=0; + //s(T); + + cin>>T; + while(T--) + { + s(n);flag=0; + FOR(i,0,n) + { + cin>>a[i]; + ins(a[i]); + } + FOR(i,0,n) + { + if(chek(a[i])==false){ + flag=1; break; + } + } + if(flag==0) + cout<<"YES\n"; + else + cout<<"NO\n"; + root=NULL; + //delete root; + } + //disp(root, 0); + //delete root; + return 0; +} diff --git a/spoj_cpp_clean/PHONELST/PHONELST-9.cpp b/spoj_cpp_clean/PHONELST/PHONELST-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11ab644fd029942e21539380df31f3a3e9beb12b --- /dev/null +++ b/spoj_cpp_clean/PHONELST/PHONELST-9.cpp @@ -0,0 +1,61 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +bool init() +{ + int N; + int i; + + char temp[11]; + vector ALL; + string str; + + scanf("%d", &N); + + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t; + long long n, a, b; + sd(t); + for(int i = 1; i <= t; i++){ + scanf("%lld",&n); + a = sqrt(n); + b = (int)sqrt(n+1); + if(b*b == n+1 or (b+1)*(b+1) == n+1) + printf("Case %d: %lld\n",i, a); + else + printf("Case %d: %lld\n",i, a-1); + + } + return 0; +} diff --git a/spoj_cpp_clean/PIGBANK/PIGBANK-7.cpp b/spoj_cpp_clean/PIGBANK/PIGBANK-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03ffe0651fa2324e4df4fb9cb8c5499e582ab42b --- /dev/null +++ b/spoj_cpp_clean/PIGBANK/PIGBANK-7.cpp @@ -0,0 +1,71 @@ +#include +#define INFINITO 200000000 + +using namespace std; + + int p[600]; + int w[600]; + int dp[10050]; + int T,E,F,N,auxP,auxW; + +int min(int x, int y) +{ + return (x +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + printf("3.1415926535897932384626433832795"); +} diff --git a/spoj_cpp_clean/PKA/PKA-14.cpp b/spoj_cpp_clean/PKA/PKA-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b57747ffe9b6de9e55ca733e1e0e76f8420ceebe --- /dev/null +++ b/spoj_cpp_clean/PKA/PKA-14.cpp @@ -0,0 +1,95 @@ +#include +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define forn(i, stop) for(ll i = 1; i <= stop; i++) +#define rep(i, start) for(ll i = start; i >= 1; i--) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +struct node { + ll b, e; + bool operator <(const node &a) const { + if(b == a.b) + return e > a.e; + return b < a.b; + } + +} ara[305]; + +ll child[305]; +bool vis[305]; +ll n; + +ll dfs(ll u) { + + if(vis[u]) + return child[u]; + + vis[u] = 1; + child[u] = 0; + ll valEnd[305]; + + for(ll i = u+1; i <= n; i++) { + if(ara[i].e > ara[u].e) + continue; + + ll val = dfs(i); + ll now = val; + + for(ll j = u+1; j < i; j++) { + if(ara[i].b >= ara[j].e) + val = max(val, valEnd[j] + now); + } + + valEnd[i] = val; + child[u] = max(child[u], val); + } + + return ++child[u]; +} + +int main() + +{ +// freopen("in.txt", "r", stdin); +// freopen("out1.txt", "w", stdout); + ll t; + sl(t); + while(t--) { + ll idx = 0; + sl(n); + forn(i, n) + sl(ara[i].b), sl(ara[i].e); + + sort(ara + 1, ara + n + 1); + + ms(vis, 0); + ara[0] = {-1, inf}; + ll ans = dfs(0) - 1; +// forn(i, n) { +// if(!vis[i]) +// ans = max(ans, dfs(0)); +// } + + pf("%lld\n", ans); + } + + return 0; +} + diff --git a/spoj_cpp_clean/PL/PL-21.cpp b/spoj_cpp_clean/PL/PL-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e7ec38fee918eab6e828c94bf003a9d5e9128e1 --- /dev/null +++ b/spoj_cpp_clean/PL/PL-21.cpp @@ -0,0 +1,32 @@ +/* + greedy + difficulty: none + date: none + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int ehPossivel(string str) { + vector freq(26, 0); + for (int i = 0; i < str.size(); i++) + freq[(int)str[i] - 97]++; + + int cont = 0; + for (int i = 0; i < 26; i++) { + if (freq[i] % 2 == 1) + cont++; + if (cont == 2) + return -1; + } + return 1; +} + +int main() { + string str; + while (getline(cin, str)) + cout << ehPossivel(str) << endl; + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/PLAYGAME/PLAYGAME-9.cpp b/spoj_cpp_clean/PLAYGAME/PLAYGAME-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abe0442c26beb3a7810fd467859d99863fac54f9 --- /dev/null +++ b/spoj_cpp_clean/PLAYGAME/PLAYGAME-9.cpp @@ -0,0 +1,36 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + int T; + long long N; + + cin >> T; + + while (T--) + { + cin >> N; + if (N % 3) + cout << "Hemlata\n"; + else + cout << "Ragini\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/PLD/PLD-3.cpp b/spoj_cpp_clean/PLD/PLD-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f7e667c404ee44852f3135323529461c07c6c70 --- /dev/null +++ b/spoj_cpp_clean/PLD/PLD-3.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sllu(x) scanf("%llu",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int k; + string s; + sd(k); + cin>>s; + + return 0; +} diff --git a/spoj_cpp_clean/POKER/POKER-3.cpp b/spoj_cpp_clean/POKER/POKER-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4d432a8ae7d8688d59e9f63e1667e6236033c37 --- /dev/null +++ b/spoj_cpp_clean/POKER/POKER-3.cpp @@ -0,0 +1,182 @@ + +#include +#include +#include +using namespace std; +int main() +{ +int t,i,j,k,rank_2,rank_3; +int poker[13][4]; +int row[13]; +int column[4]; +string str; +getline(cin,str); +if(str[1] == 0) +t = str[0] - 48; +else +t = str[1] - 48 + 10*(str[0] - 48); +bool done,flush; +while(t--) +{ +flush = false; +rank_2 = 0; +rank_3 = 0; +done = false; +for(i=0;i<13;i++) +{ +for(j=0;j<4;j++) +poker[i][j] = 0; +} +for(i=0;i<4;i++) +column[i] = 0; +for(i=0;i<13;i++) +row[i] = 0; +getline(cin,str); +for(i=0;i<14;i += 3) +{ +switch(str[i]) +{ +case 'A': +j = 1; +break; +case 'T': +j = 10; +break; +case 'J': +j = 11; +break; +case 'Q': +j = 12; +break; +case 'K': +j = 13; +break; +default: +j = str[i] - 48; +} +switch(str[i+1]) +{ +case 'S': +k = 0; +break; +case 'H': +k = 1; +break; +case 'D': +k = 2; +break; +case 'C': +k = 3; +break; +} +poker[j-1][k] = 1; +} +for(i=0;i<4;i++) +{ +for(j=0;j<13;j++) +column[i] += poker[j][i]; +} +for(i=0;i<13;i++) +{ +for(j=0;j<4;j++) +row[i] += poker[i][j]; +} +for(i=0;i<4;i++) +{ +if(column[i] == 5) +{ +if(poker[0][i] == 1 && poker[9][i] == 1 && poker[10][i] == 1 && poker[11][i] == 1 && poker[12][i] == 1) +{ +printf("royal flush \n"); +done = true; +} +else +{ +for(j=0;j<9;j++) +{ +if(poker[j][i] == 1) +break; +} +for(k=j+1;k<=(j+4);k++) +{ +if(poker[k][i] != 1) +break; +} +if(k == (j+5)) +{ +printf("straight flush \n"); +done = true; +} +} +flush = true; +break; +} +} +if(!done) +{ +for(i=0;i<13;i++) +{ +if(row[i] == 4) +{ +printf("four of a kind \n"); +done = true; +} +else if(row[i] == 3) +rank_3 ++ ; +else if(row[i] == 2) +rank_2 ++ ; +} +} +if(!done && rank_3 == 1 && rank_2 == 1) +{ +printf("full house \n"); +done = true; +} +if(!done && flush) +{ +printf("flush \n"); +done = true; +} +if(!done) +{ +for(j=0;j<9;j++) +{ +if(row[j] == 1) +break; +} +for(k=j+1;k<=(j+4);k++) +{ +if(row[k] != 1) +break; +} +if(k == (j+5)) +{ +printf("straight \n"); +done = true; +} +else if(row[0] == 1 && row[9] == 1 && row[10] == 1 && row[11] == 1 && row[12] == 1) +{ +printf("straight \n"); +done = true; +} +} +if(!done && rank_3 == 1) +{ +printf("three of a kind \n"); +done = true; +} +if(!done && rank_2 == 2) +{ +printf("two pairs \n"); +done = true; +} +if(!done && rank_2 == 1) +{ +printf("pair \n"); +done = true; +} +if(!done) +printf("high card \n"); +} +return 0; +} diff --git a/spoj_cpp_clean/POLYMUL/POLYMUL-0.cpp b/spoj_cpp_clean/POLYMUL/POLYMUL-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ac96885088a93b41a0bb83e316fb9cb19adebf4 --- /dev/null +++ b/spoj_cpp_clean/POLYMUL/POLYMUL-0.cpp @@ -0,0 +1,107 @@ +#include + +#define FI first +#define SE second + +typedef long long ll; +typedef std::pair pairll; + +const static ll INF = 0x3f3f3f3f3f3f3f3f; + +namespace FFT { + typedef long long ll; + + using cd = std::complex; + const double PI = std::acos(-1); + + void fft(std::vector& a, const bool& invert) { + ll n = a.size(); + + for (ll i = 1, j = 0; i < n; ++i) { + ll bit = n >> 1; + for (; j & bit; bit >>= 1) + j ^= bit; + j ^= bit; + + if (i < j) + std::swap(a[i], a[j]); + } + + for (ll len = 2; len <= n; len <<= 1) { + const double ang = 2 * PI / len * (invert ? -1 : 1); + cd wlen(std::cos(ang), std::sin(ang)); + for (ll i = 0; i < n; i += len) { + cd w(1); + for (ll j = 0; j < len / 2; ++j) { + cd u = a[i+j], v = a[i+j+len/2] * w; + a[i+j] = u+v; + a[i+j+len/2] = u-v; + w *= wlen; + } + } + } + + if (invert) { + for (auto& x : a) + x /= n; + } + } + + std::vector multiply(const std::vector& a, const std::vector& b) { + std::vector fa(a.begin(),a.end()), fb(b.begin(), b.end()); + ll n = 1; + while (n < a.size() + b.size()) + n <<= 1; + fa.resize(n); + fb.resize(n); + + fft(fa, false); + fft(fb, false); + + for (ll i = 0; i < n; ++i) + fa[i] *= fb[i]; + + fft(fa, true); + + std::vector result(n); + for (ll i = 0; i < n; ++i) + result[i] = std::llround(fa[i].real()); + + return result; + } + + /* if it's numbers and not polynomials, we have to normalise */ + void normalise(std::vector& result) { + for (ll i = 0, carry = 0; i < result.size(); ++i) { + result[i] += carry; + carry = result[i] / 10; + result[i] %= 10; + } + } +}; + +static inline void setio(void); + +static void one_case(void) { + ll n; std::cin >> n; + std::vector a(n+1), b(n+1); + for (auto& e : a) std::cin >> e; + for (auto& e : b) std::cin >> e; + + std::vector res = FFT::multiply(a, b); + for (ll i = 0; i <= 2 * n; ++i) + std::cout << res[i] << " "; + std::cout << '\n'; +} + +int main(void) { + setio(); + ll n; std::cin >> n; + while (n--) one_case(); + return 0; +} + +static inline void setio(void) { + std::ios::sync_with_stdio(false);std::cin.tie(nullptr); + std::cout.precision(10); std::cout << std::fixed; +} diff --git a/spoj_cpp_clean/POLYMUL/POLYMUL-14.cpp b/spoj_cpp_clean/POLYMUL/POLYMUL-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5aa33352b7fae65aa9f6a0f86bea51046b798c97 --- /dev/null +++ b/spoj_cpp_clean/POLYMUL/POLYMUL-14.cpp @@ -0,0 +1,139 @@ +// #pragma GCC optimize("Ofast,unroll-loops") +// #pragma GCC target("avx,avx2,fma") + +#include +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define all(v) v.begin(), v.end() +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; ++i) +#define for0(i, stop) for(ll i = 0; i < stop; ++i) +#define rep1(i, start) for(ll i = start; i >= 1; --i) +#define rep0(i, start) for(ll i = (start-1); i >= 0; --i) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define EL '\n' +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +/*** + * Multiply (7x^2 + 8x^1 + 9x^0) with (6x^1 + 5x^0) + * ans = 42x^3 + 83x^2 + 94x^1 + 45x^0 + * A = {9, 8, 7} + * B = {5, 6} + * V = multiply(A,B) + * V = {45, 94, 83, 42} +***/ + +/*** Tricks + * Use vector < bool > if you need to check only the status of the sum + * Use bigmod if the power is over same polynomial && power is big + * Use long double if you need more precision + * Use long long for overflow +***/ +typedef vector vi; +const double PI = 2.0 * acos(0.0); +using cd = complex; + +void fft(vector & a, bool invert = 0) + +{ + int n = a.size(); + + for (int i = 1, j = 0; i < n; i++) { + int bit = n >> 1; + for (; j & bit; bit >>= 1) + j ^= bit; + j ^= bit; + + if (i < j) + swap(a[i], a[j]); + } + + for (int len = 2; len <= n; len <<= 1) { + double ang = 2 * PI / len * (invert ? -1 : 1); + cd wlen(cos(ang), sin(ang)); + for (int i = 0; i < n; i += len) { + cd w(1); + for (int j = 0; j < len / 2; j++) { + cd u = a[i+j], v = a[i+j+len/2] * w; + a[i+j] = u + v; + a[i+j+len/2] = u - v; + w *= wlen; + } + } + } + + if (invert) { + for (cd & x : a) + x /= n; + } +} + +void ifft(vector & p) + +{ + fft(p, 1); +} + +vi multiply(vi const& a, vi const& b) +{ + + vector fa(a.begin(), a.end()), fb(b.begin(), b.end()); + int n = 1; + while (n < a.size() + b.size()) + n <<= 1; + fa.resize(n); + fb.resize(n); + + fft(fa); + fft(fb); + for (int i = 0; i < n; i++) + fa[i] *= fb[i]; + ifft(fa); + + vi result(n); + for (int i = 0; i < n; i++) + result[i] = round(fa[i].real()); + + return result; +} + +vi num1, num2, res; + +int main() +{ + ll t; + cin >> t; + while(t--) { + ll n; + sl(n);n++; + + num1.resize(n), num2.resize(n); + rep0(i, n) sl(num1[i]); + rep0(i, n) sl(num2[i]); + + res = multiply(num1, num2); + rep0(i, (2*n-1)) { + if(i + 1 != 2*n-1) pf(" "); + pf("%lld", res[i]); + } + pn; + + res.clear(); + } + + return 0; diff --git a/spoj_cpp_clean/PON/PON-20.cpp b/spoj_cpp_clean/PON/PON-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67f3e7aa69d56ad94bb1e047a3efedb956e05752 --- /dev/null +++ b/spoj_cpp_clean/PON/PON-20.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; +#define INF 1<<30 +#define maxn 100005 +#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); +typedef long long ll; + + +using u64 = uint64_t; +using u128 = __uint128_t; + +u64 binpower(u64 base, u64 e, u64 mod) { + u64 result = 1; + base %= mod; + while (e) { + if (e & 1) + result = (u128)result * base % mod; + base = (u128)base * base % mod; + e >>= 1; + } + return result; +} + +bool check_composite(u64 n, u64 a, u64 d, int s) { + u64 x = binpower(a, d, n); + if (x == 1 || x == n - 1) + return false; + for (int r = 1; r < s; r++) { + x = (u128)x * x % n; + if (x == n - 1) + return false; + } + return true; +}; + +bool MillerRabin(u64 n, int iter=5) { // returns true if n is probably prime, else returns false. + if (n < 4) + return n == 2 || n == 3; + + int s = 0; + u64 d = n - 1; + while ((d & 1) == 0) { + d >>= 1; + s++; + } + + for (int i = 0; i < iter; i++) { + int a = 2 + rand() % (n - 3); + if (check_composite(n, a, d, s)) + return false; + } + return true; +} +int main() +{ + FASTIO; + u64 n; + int T; + cin >> T; + while(T--){ + cin >> n; + if(MillerRabin(n)) cout << "YES\n"; + else cout << "NO\n"; + } +} diff --git a/spoj_cpp_clean/POPULAR/POPULAR-10.cpp b/spoj_cpp_clean/POPULAR/POPULAR-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..669e8c29dc2472cbbf8368ad5e729a6973347ff2 --- /dev/null +++ b/spoj_cpp_clean/POPULAR/POPULAR-10.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + +int main() +{ + int n; + while(cin >> n && n != 0) + { + int bv = 0, v; + vector votos; + votos.reserve(n); + for(int i=0; i> v; + if(v == 1) + { + votos[j]++; + if(votos[j] > bv) + bv = votos[j]; + } + } + cout << bv << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/POSTERIN/POSTERIN-19.cpp b/spoj_cpp_clean/POSTERIN/POSTERIN-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63b1603c5910647fcef09578192bdee03a37f69c --- /dev/null +++ b/spoj_cpp_clean/POSTERIN/POSTERIN-19.cpp @@ -0,0 +1,77 @@ +/* + * Author : Adarsh Gaur +*/ + +#include +//#include +//#include +//#include +//#include + +using namespace std; +//using namespace __gnu_pbds; + +//typedef tree, rb_tree_tag, tree_order_statistics_node_update> ordered_set; +#define int long long +typedef pair pii; +typedef vector vi; +typedef vector > vvi; +typedef vector > vvpii; +typedef vector vpii; + + +#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) +#define forn(i,s,e) for(int i=s; i>n; + int h[n+1], width[n+1]; + for(int i=1;i<=n; ++i){ + cin>>width[i]>>h[i]; + } + + set s; + int ans=1; + s.insert(h[1]); + + for(int i=2; i<=n; i++){ + auto itr = s.find(h[i]); + + if(h[i]>h[i-1]){ + s.insert(h[i]); + ans++; + + }else if(h[i] Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +unordered_map > memo; +int a, b, c; + +long long solve() +{ + deque< pair > > Q; + pair > curr; + + memo.clear(); + Q.push_back(make_pair(0, make_pair(0, 0))); + + while(!Q.empty()) + { + curr=Q.front(); + Q.pop_front(); + + if(curr.second.first==c || curr.second.second==c) + return curr.first; + + if(memo.find(curr.second.first)!=memo.end() && memo[curr.second.first].find(curr.second.second)!=memo[curr.second.first].end()) + continue; + else + memo[curr.second.first][curr.second.second]=1; + + Q.push_back(make_pair(curr.first+1, make_pair(0, curr.second.second))); + Q.push_back(make_pair(curr.first+1, make_pair(curr.second.first, 0))); + + if(curr.second.first!=a) + Q.push_back(make_pair(curr.first+1, make_pair(a, curr.second.second))); + if(curr.second.second!=b) + Q.push_back(make_pair(curr.first+1, make_pair(curr.second.first, b))); + + if(curr.second.first!=0 && curr.second.second!=b) + Q.push_back(make_pair(curr.first+1, make_pair(curr.second.first - min(curr.second.first, b-curr.second.second), curr.second.second + min(curr.second.first, b-curr.second.second)))); + + if(curr.second.second!=0 && curr.second.first!=a) + Q.push_back(make_pair(curr.first+1, make_pair(curr.second.first + min(a-curr.second.first, curr.second.second), curr.second.second - min(a-curr.second.first, curr.second.second)))); + } + + return -1; +} + +int main() +{ + int T; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + { + scanf("%d %d %d", &a, &b, &c); + printf("%lld\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/POWERUP/POWERUP-3.cpp b/spoj_cpp_clean/POWERUP/POWERUP-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3d6a500c71634adae060fb15ff876b503f2ac46 --- /dev/null +++ b/spoj_cpp_clean/POWERUP/POWERUP-3.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +long long exp(long long a, long long b, long long p){ + long long res = 1; + //a = a%p; + while(b>0){ + if(b&1) res = (res*a)%p; + a = (a*a)%p; + b >>= 1; + } + return res; +} + + +int main(){ + long long a,b,c,res,MOD=1000000007; + scanf("%lld %lld %lld",&a,&b,&c); + while(a != -1){ + if(a%MOD != 0){ + res = exp(b,c,MOD-1); + res = exp(a,res,MOD); + } + else{ + if(b == 0 and c != 0) + res = 1; + else + res = 0; + } + + printf("%lld\n",res); + scanf("%lld %lld %lld",&a,&b,&c); + } + return 0; +} diff --git a/spoj_cpp_clean/PPATH/PPATH-17.cpp b/spoj_cpp_clean/PPATH/PPATH-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eddc91afe76beea474dc73462dbd124eddc62471 --- /dev/null +++ b/spoj_cpp_clean/PPATH/PPATH-17.cpp @@ -0,0 +1,136 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +const int N= (int)1e4+5; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +int solve() { + int a,b; + string s1,s2,s; + + cin>>s1>>s2; + stringstream xx; xx<>a; + stringstream yy; yy<>b; + + + int dis[N]; + memset(dis,-1,sizeof dis); + dis[a]=0; + + queue q; + q.push(a); + + while (!q.empty()) { + int u=q.front(); q.pop(); + + char i,n=s2[0]; + stringstream x1; + x1<>s; + for(i='1';i<='9';i++) { + s[0]=i; + stringstream nums; + int num; + nums<>num; + + if(isPrime(num ) && dis[num]==-1) { + q.push(num); + dis[num] = dis[u]+1; + } + } + + n=s2[1]; + stringstream x2; + x2<>s; + for(i='0';i<='9';i++) { + s[1]=i; + stringstream nums; + int num; + nums<>num; + + if(isPrime(num ) && dis[num]==-1) { + q.push(num); + dis[num] = dis[u]+1; + } + } + + n=s2[2]; + stringstream x3; + x3<>s; + for(i='0';i<='9';i++) { + s[2]=i; + stringstream nums; + int num; + nums<>num; + + if(isPrime(num ) && dis[num]==-1) { + q.push(num); + dis[num] = dis[u]+1; + } + } + + n=s2[3]; + stringstream x4; + x4<>s; + for(i='0';i<='9';i++) { + s[3]=i; + stringstream nums; + int num; + nums<>num; + + if(isPrime(num ) && dis[num]==-1) { + q.push(num); + dis[num] = dis[u]+1; + } + } + + } + + + return dis[b]; +} + +int main() { + FAST + + int t; cin>>t; + + while (t--) { + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +map > graph; +int primes[9000], temp; +int inf = 1000000; +int shortest(int p1, int p2){ + int dist[10000]; + int visited[10000]; + int prev[10000]; + int x; + stack q; + for(int i = 168; i <= 1228; i++){ + if(primes[i] != p1){ + dist[primes[i]] = inf; + prev[primes[i]] = inf; + } + q.push(primes[i]); + } + while(!q.empty()){ + x = q.top(); + q.pop(); + visitied[x] = 1; + for(int i = 0; i < graph[x].size(); i++){ + + } + } + +} +int main(){ + int flag; + temp = 1; + primes[0] = 2; + for(int i = 3; i < 10000; i+=2){ + flag = 0; + for(int j = 0; j < temp; j++){ + if(i%primes[j] == 0){ + j = temp; + flag = 1; + } + } + if(flag == 0){ + primes[temp] = i; + temp++; + } + } + for(int i = 168; i <= 1228; i++){ + for(int j = i + 1; j <= 1228; j++){ + if(primes[j]/1000 == primes[i]/1000 or (primes[j]/100)%10 == (primes[i]/100)%10 or (primes[j]/10)%10 == (primes[i]/10)%10 or primes[j]%10 == primes[i]%10){ + graph[primes[i]].push_pack(primes[j]); + } + } + } + int t, p1, p2; + sd(t); + while(t--){ + sd2(p1,p2); + printf("%d\n",shortest(p1,p2)); + } + return 0; +} diff --git a/spoj_cpp_clean/PPATH/PPATH-9.cpp b/spoj_cpp_clean/PPATH/PPATH-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87247eac63b9da0a576e4ac1ababb7eb65d6b31e --- /dev/null +++ b/spoj_cpp_clean/PPATH/PPATH-9.cpp @@ -0,0 +1,163 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#define N 10000 + +using namespace std; + +bool sieve[10000]; + +void calc_primes() +{ + int i, j; + + for(i = 2; i < N; i++) + if(!sieve[i]) + for(j = i + i; j < N; j+=i) + sieve[j] = 1; +} + +int solve(int start, int target) +{ + deque > Q; + pair curr; + vector V(N); + int i, tempn; + string temp; + + if(start == target) + return 0; + + curr = make_pair(to_string(start), 0); + Q.push_back(curr); + + while(!Q.empty()) + { + curr = Q.front(); + Q.pop_front(); + + for(i=1; i<=9; i++) + { + temp = curr.first; + temp[0] = i + '0'; + tempn = stoi(temp); + + if(!sieve[tempn] && !V[tempn]) + { + if(tempn == target) + return curr.second + 1; + Q.push_back(make_pair(temp, curr.second + 1)); + V[tempn] = 1; + } + + temp = curr.first; + temp[1] = i + '0'; + tempn = stoi(temp); + + if(!sieve[tempn] && !V[tempn]) + { + if(tempn == target) + return curr.second + 1; + Q.push_back(make_pair(temp, curr.second + 1)); + V[tempn] = 1; + } + + temp = curr.first; + temp[2] = i + '0'; + tempn = stoi(temp); + + if(!sieve[tempn] && !V[tempn]) + { + if(tempn == target) + return curr.second + 1; + Q.push_back(make_pair(temp, curr.second + 1)); + V[tempn] = 1; + } + + temp = curr.first; + temp[3] = i + '0'; + tempn = stoi(temp); + + if(!sieve[tempn] && !V[tempn]) + { + if(tempn == target) + return curr.second + 1; + Q.push_back(make_pair(temp, curr.second + 1)); + V[tempn] = 1; + } + } + + temp = curr.first; + temp[1] = '0'; + tempn = stoi(temp); + + if(!sieve[tempn] && !V[tempn]) + { + if(tempn == target) + return curr.second + 1; + Q.push_back(make_pair(temp, curr.second + 1)); + V[tempn] = 1; + } + + temp = curr.first; + temp[2] = '0'; + tempn = stoi(temp); + + if(!sieve[tempn] && !V[tempn]) + { + if(tempn == target) + return curr.second + 1; + Q.push_back(make_pair(temp, curr.second + 1)); + V[tempn] = 1; + } + + temp = curr.first; + temp[3] = '0'; + tempn = stoi(temp); + + if(!sieve[tempn] && !V[tempn]) + { + if(tempn == target) + return curr.second + 1; + Q.push_back(make_pair(temp, curr.second + 1)); + V[tempn] = 1; + } + } + + return -1; +} + +int main() +{ + int T, temp, f, s; + + //freopen("input.txt","r",stdin); + + calc_primes(); + scanf("%d", &T); + + while(T--) + { + scanf("%d %d", &f, &s); + temp = solve(f, s); + + if(temp!=-1) + printf("%d\n", temp); + else + printf("Impossible\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/PPBRJB/PPBRJB-3.cpp b/spoj_cpp_clean/PPBRJB/PPBRJB-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..069952f41a13d1b71a337367792e21fd604aa04f --- /dev/null +++ b/spoj_cpp_clean/PPBRJB/PPBRJB-3.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +bool is_prime (const int& n, vector& v) { + double root = sqrt (n); + vector::iterator it = v.begin(); + while ( it != v.end() && *it <= root ) { + if (!( n % *it)) return 0; + it++; + } + return 1; +} + +long long fill_dp(long long (&dp)[41], int empty){ + if(empty < 4) + return 1; + else if(dp[empty] != 0) + return dp[empty]; + else{ + dp[empty] = fill_dp(dp, empty - 4) + fill_dp(dp, empty - 1); + } + return dp[empty]; + +} +int main() { + /* Enter your code here. Read input from STDIN. Print output to STDOUT */ + int t, n; + long long dp[41]; + int ans[41]; + ans[0] = 0; + ans[1] = 0; + ans[2] = 0; + ans[3] = 0; + ans[4] = 1; + vector primes; + primes.push_back(2); + + for(int i = 0; i<41; i++) + dp[i] = 0; + dp[1] = 1; + dp[2] = 1; + dp[3] = 1; + dp[4] = 2; + + fill_dp(dp, 40); + + for (int i=3; i<=217286; i+=2) + if ( is_prime( i, primes ) ){ + primes.push_back( i ); + } + ans[40] = primes.size(); + int j = 5, count=0; + vector::iterator it = primes.begin(); + while ( it != primes.end()) { + if (*it > dp[j]){ + ans[j] = count; + j++; + } + else if(*it == dp[j]){ + count++; + ans[j] = count; + j++; + it++; + } + else{ + it++; + count++; + } + } + + cin>>t; + while(t--){ + cin>>n; + cout< + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< itov(ll x){ + if(!x) return {0}; + vector v; + + while(x){ + v.pb(x%10); + x /= 10; + } + + reverse(v.begin(), v.end()); + + return v; +} + +const int N = 20; + +ll sum[N][2]; +ll cnt[N][2]; + +ll get(vector v, bool flag){ + int n = v.size(); + + memset(sum, 0, sizeof sum); + memset(cnt, 0, sizeof cnt); + + cnt[0][1] = 1; + + for(int i = 0; i < n; i++){ + for(int eq = 0; eq < 2; eq++){ + ll c = cnt[i][eq]; + ll s = sum[i][eq]; + if(!c) continue; + + int mnd = (eq)? v[i] : 9; + + for(int nd = 0; nd <= mnd; nd++){ + int neq = (eq and nd == v[i]); + sum[i+1][neq] += s + c * nd; + cnt[i+1][neq] += c; + } + } + } + + ll ret = sum[n][0] + ((flag)? sum[n][1] : 0); + + return ret; +} + +void solve(){ + ll a, b; + cin >> a >> b; + + vector A = itov(a); + vector B = itov(b); + + cout << get(B,1) - get(A,0) << endl; +} + +int main(){ _ + int t; + cin >> t; + while(t--) solve(); + return 0; +} diff --git a/spoj_cpp_clean/PR003004/PR003004-8.cpp b/spoj_cpp_clean/PR003004/PR003004-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57eb2ee4adc3a5d1b394cc64ee62f061d3a816c6 --- /dev/null +++ b/spoj_cpp_clean/PR003004/PR003004-8.cpp @@ -0,0 +1,155 @@ +/*************************************************** + * Problem Name : PR003004 - Digit Sum.cpp + * Problem Link : https://www.spoj.com/problems/PR003004/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-02-08 + * Problem Type : adHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define FileRead freopen ("/home/saikat/Desktop/logN/input.txt", "r", stdin); +#define FileWrite freopen ("/home/saikat/Desktop/logN/output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int n; +ll dp[20][150][2][2]; +vectorA, B; +ll digitDP (int pos, int cnt, int isChoto, int isBoro) { + if (pos == n) { + return cnt; + } + + if (dp[pos][cnt][isChoto][isBoro] != -1) { + return dp[pos][cnt][isChoto][isBoro]; + } + + ll ans = 0; + int high, low; + + if (isChoto == 0) { + high = B[pos]; + + } else { + high = 9; + } + + if (isBoro == 0) { + low = A[pos]; + + } else { + low = 0; + } + + for (int i = low; i <= high; i++) { + int new_cnt = cnt; + int new_isChoto = isChoto; + int new_isBoro = isBoro; + new_cnt += i; + + if (i < high) { + new_isChoto = 1; + } + + if (i > low) { + new_isBoro = 1; + } + + ans += (ll) digitDP (pos + 1, new_cnt, new_isChoto, new_isBoro); + } + + return dp[pos][cnt][isChoto][isBoro] = ans; +} +ll solve (ll a, ll b) { + A.clear(), B.clear(); + + while (b > 0) { + int rem = b % 10; + B.pb (rem); + b /= 10; + } + + reverse (all (B) ); + + while (a > 0) { + int rem = a % 10; + A.pb (rem); + a /= 10; + } + + n = (int) B.size(); + + while ( (int) A.size() < n) { + A.pb (0); + } + + reverse (all (A) ); + SET (dp, -1); + ll ans = digitDP (0, 0, 0, 0); + return ans; +} + +int main () { + __FastIO; + int tc; + ll a, b; + cin >> tc; + + while (tc--) { + cin >> a >> b; + ll ans = solve (a, b); + cout << ans << "\n"; + } + + return 0; +} + diff --git a/spoj_cpp_clean/PRATA/PRATA-9.cpp b/spoj_cpp_clean/PRATA/PRATA-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a4f45d525eb5014d7a99e85545438907d3bf40f --- /dev/null +++ b/spoj_cpp_clean/PRATA/PRATA-9.cpp @@ -0,0 +1,81 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define MAXN 4004010 +using namespace std; + +inline int get_pratas(int R, int M) +{ + double r = R, m = M; + + return floor((sqrt(r * r + 8 * r * M) - r) / (2 * r)); +} + +int A[57]; +int N, P; + +inline void init() +{ + int i; + + scanf("%d", &P); + scanf("%d", &N); + + for (i = 0; i < N; i++) + scanf("%d", &A[i]); +} + +inline int sum_pratas(int M) +{ + int sum = 0; + + for (int i = 0; i < N; i++) + sum += get_pratas(A[i], M); + + return sum; +} + +inline int solve() +{ + int hi = MAXN, lo = 0; + + while (lo != hi - 1) + { + int med = (lo + hi) / 2; + + if (sum_pratas(med) >= P) + hi = med; + else + lo = med; + } + + return hi; +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/PRIC/PRIC-3.cpp b/spoj_cpp_clean/PRIC/PRIC-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5820eac4d6c0657a1bfa2d0ec2f5fceffc5ffc4 --- /dev/null +++ b/spoj_cpp_clean/PRIC/PRIC-3.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#define ll long long +using namespace std; +ll sieve_atkins(int n) +{ + ll sum=0; + vector is_prime(n + 1); + is_prime[2] = true; + is_prime[3] = true; + for (ll int i = 5; i <= n; i++) + { + is_prime[i] = false; + } + ll int lim = ceil(sqrt(n)); + for (ll int x = 1; x <= lim; x++) + { + for (ll int y = 1; y <= lim; y++) + { + ll int num = (4 * x * x + y * y); + if (num <= n && (num % 12 == 1 || num % 12 == 5)) + { + is_prime[num] = true; + } + num = (3 * x * x + y * y); + if (num <= n && (num % 12 == 7)) + { + is_prime[num] = true; + } + if (x > y) + { + num = (3 * x * x - y * y); + if (num <= n && (num % 12 == 11)) + { + is_prime[num] = true; + } + } + } + } + for (ll int i = 5; i <= lim; i++) + { + if (is_prime[i]) + { + for (ll int j = i * i; j <= n; j += i) + { + is_prime[j] = false; + } + } + } + for (ll int i = 2; i <= n; i++) + { + if (is_prime[i]) + { + sum+=i; + } + } + return sum; +} + +int main(){ + int n; + cin>>n; + cout< +#include +#include +#include +#include + +using namespace std; + +vector primelist; + +void flagPrime(void) +{ + primelist.push_back(2ull); + + unsigned long long sqrtofbillion = sqrt(1000000000); + unsigned long long peak; + + for(unsigned long long i = 3ull; i <= sqrtofbillion; i += 2ull) + { + bool flag = true; + peak = sqrt(i); + + vector::iterator it; + + for(it = primelist.begin(); it != primelist.end(); it++) + { + if(*it > peak) + break; + else + { + if(i % *it == 0) + { + flag = false; + break; + } + } + } + + if(flag) + primelist.push_back(i); + } +} + +int main(void) +{ + int t; + unsigned long long m, n; + + flagPrime(); + + cin >> t; + + for(int k = 0; k < t; k++) + { + if(k) + cout << endl; + + cin >> m >> n; + + if(m < 2ull) + m = 2ull; + + unsigned long long peak = sqrt(n) + 1ull; + + set noprime; + noprime.clear(); + + vector::iterator it; + + for(it = primelist.begin(); it != primelist.end(); it++) + { + if(*it >= peak) + break; + + unsigned long long j; + + if(*it >= m) + j = (*it) * 2ull; + else + j = m + ((*it - m % *it) % *it); + + for(; j <= n; j += *it) + noprime.insert(j); + } + + for(unsigned long long i = m; i <= n; i++) + if(noprime.count(i) == 0) + cout << i << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/PRIME1/PRIME1-14.cpp b/spoj_cpp_clean/PRIME1/PRIME1-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2efc3e5e23eec3a44ee38226f50f3d5390714ee8 --- /dev/null +++ b/spoj_cpp_clean/PRIME1/PRIME1-14.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define ll long long +#define siz 31700 + +bitset mark; + +int main() + +{ + ll tmp = sqrt(siz); + mark[1] = 1; + vector primes; + primes.push_back(2); + for(ll i = 3; i <= tmp; i += 2) { + + if(mark[i] == 0) { + for(ll j = i * i; j <= siz; j += i + i) { + mark[j] = 1; + //cout << j << endl; + } + } + //cout << i << endl; + } + + for(ll i = 3; i <= siz; i += 2) { + if(mark[i] == 0) + primes.push_back(i); + } + + //cout << primes.size(); + ll t; + cin >> t; + + while(t--) { + ll a, b; + scanf("%lld %lld", &a, &b); + + for(ll i = a; i <= b; i++) { + if(i % 2 == 0 && i != 2) + continue; + if(i == 1) + continue; + + bool flag = 1; + for(ll j = 0; j < primes.size(); j++) { + if(i <= primes[j]) + break; + if(i % primes[j] == 0) { + flag = 0; + break; + } + } + + if(flag == 1) { + printf("%lld\n", i); + } + + } + + if(t != 0) + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/PRIME1/PRIME1-15.cpp b/spoj_cpp_clean/PRIME1/PRIME1-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..754e1367b0203ef06c5aefa0c3e495740faa3d82 --- /dev/null +++ b/spoj_cpp_clean/PRIME1/PRIME1-15.cpp @@ -0,0 +1,77 @@ +/* +* I have just solved the "Prime Generator" challenge on SPOJ - https://www.spoj.com/PRIME1 http://www.spoj.com/problems/PRIME1 #spoj #onlinejudge #problems +*/ + +#pragma GCC optimize ("O3") +#pragma GCC target ("sse4") + +#include + +using namespace std; + +typedef long long ll; +typedef unsigned uns; +typedef vector vb; +typedef vector vi; +typedef pair ii; +typedef vector vu; +typedef vector vl; +typedef vector vs; + +#define din(x) int x; cin >> x; +#define in(x) cin >> x; +#define full(x) x.begin(), x.end() + +#define eb emplace_back +#define ins insert +#define f first +#define s second + +vector segmentedSieve(ll L, ll R) { + // generate all primes up to sqrt(R) + ll lim = sqrt(R); + vector mark(lim + 1, false); + vector primes; + for (ll i = 2; i <= lim; ++i) { + if (!mark[i]) { + primes.emplace_back(i); + for (ll j = i * i; j <= lim; j += i) + mark[j] = true; + } + } + + vector isPrime(R - L + 1, true); + for (ll i : primes) + for (ll j = max(i * i, (L + i - 1) / i * i); j <= R; j += i) + isPrime[j - L] = false; + if (L == 1) + isPrime[0] = false; + return isPrime; +} + + + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + short t; + in(t) + while(t--) { + din(m) din(n) + vb isPrime = segmentedSieve(m, n); + + for (auto i = 0; i < isPrime.size(); ++i) { + + if (isPrime[i]) { + cout << m + i << endl; + } + + } + cout << endl; + + } + + + return 0; +} diff --git a/spoj_cpp_clean/PRIME1/PRIME1-16.cpp b/spoj_cpp_clean/PRIME1/PRIME1-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30a542c82af3984f51f851581f33440fc5258de7 --- /dev/null +++ b/spoj_cpp_clean/PRIME1/PRIME1-16.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#define ll long long +using namespace std; +ll isprime(int x){ + if(x == 1) + return 0; + for(ll i=2;i<=sqrt(x);i++){ + if(x%i == 0) + return 0; + } + return 1; +} +ll gprime(ll m,ll n){ + for(ll i=m;i<=n;i++) + if(isprime(i)) + cout<>t; + while(t--){ + cin>>m>>n; + gprime(m,n); + cout<<"\n"; + } + return 0; +} diff --git a/spoj_cpp_clean/PRIME1/PRIME1-19.cpp b/spoj_cpp_clean/PRIME1/PRIME1-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a25c02ed35480ab9c0d6739cf1d4c05b3c8a1e36 --- /dev/null +++ b/spoj_cpp_clean/PRIME1/PRIME1-19.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +using namespace std; +void calculate_primes(vector& primes); +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + vector primes(32001,1); + calculate_primes(primes); + int cases; + cin >> cases; + for(int t=0; t> m >> n; + vector sieve(n-m+1,1); + for(i=2; i<=32000; i++) + { + if(primes[i]==1) + { + if(m%i == 0) + { + j = m/i; + } + else + { + j = m/i + 1; + } + if( j < i) + { + j = i; + } + long long int temp = j*i; + while(temp<=n) + { + sieve[temp-m]=0; + j++; + temp = j*i; + } + } + } + if(m == 1) + { + sieve[0] = 0; // 1 is always composite + } + for(i=0; i& primes) +{ + primes[0]=0; + primes[1]=0; + for(long long int i=2; i<=32000; i++) + { + long long int j = i; + long long int temp = j*i; + while(temp <= 32000) + { + primes[temp] = 0; + j++; + temp = j*i; + } + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/PRIME1/PRIME1-21.cpp b/spoj_cpp_clean/PRIME1/PRIME1-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5266770a5bc467ed625d3ecbff01b7580d103546 --- /dev/null +++ b/spoj_cpp_clean/PRIME1/PRIME1-21.cpp @@ -0,0 +1,55 @@ +/* + math > number theory > prime numbers > sieve of eratosthenes + difficulty: medium + date: 17/Mar/2020 + problem: print all primes p in [m .. n], 0 <= m <= n <= 10^9, n-m <= 10^5 + hint: sieve + prime checking + by: @brpapa +*/ +#include +#include +#include +#include +using namespace std; + +vector primes; +bitset<35001> is; + +void sieve(int ub) { + is.set(); is[0] = is[1] = false; + + for (int i = 2; i < ub; i++) + if (is[i]) { + for (int j = i*i; j < ub; j += i) + is[j] = false; + + primes.push_back(i); + } +} + +bool isPrime(int n) { + if (n <= primes.back()) return is[(int)n]; + + int sn = (int) sqrt(n); + + for (int p : primes) { + if (p > sn) break; + if (n % p == 0) return false; + } + return true; +} + +int main() { + sieve(35000); // maior que sqrt(10^9) + + int T; cin >> T; + while (T--) { + int m, n; cin >> m >> n; + + for (int i = m; i <= n; i++) + if (isPrime(i)) cout << i << endl; + + if (T > 0) cout << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/PRIME1/PRIME1-8.cpp b/spoj_cpp_clean/PRIME1/PRIME1-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d05f15f09cd32dce56925d6090d2f1bc79524f87 --- /dev/null +++ b/spoj_cpp_clean/PRIME1/PRIME1-8.cpp @@ -0,0 +1,78 @@ +/*************************************************** + * Problem name : PRIME1 - Prime Generator.cpp + * Problem Link : http://www.spoj.com/problems/PRIME1/ + * OJ : Spoj + * Verdict : AC + * Date : 2017-10-07 + * Problem Type : Number Theory + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +bool isPrime(ll n) { + if (n == 1) return 0; + ll sq = sqrt(n); + for (ll i = 2; i <= sq; i++) { + if (n % i == 0) return 0; + } + return 1; +} +int main () { + int tc; + ll m, n; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%lld %lld", &m, &n); + for (ll i = m; i <= n; i++) { + if (isPrime(i)) { + printf("%lld\n", i); + } + } + } + return 0; +} + diff --git a/spoj_cpp_clean/PRIME1/PRIME1-9.cpp b/spoj_cpp_clean/PRIME1/PRIME1-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40da780359edb1a17dc7b8dcd9ed6079b80006a8 --- /dev/null +++ b/spoj_cpp_clean/PRIME1/PRIME1-9.cpp @@ -0,0 +1,73 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +int main() { + int primes[4000]; + int numprimes = 0; + + primes[numprimes++] = 2; + for (int i = 3; i <= 32000; i+=2) { + bool isprime = true; + int cap = sqrt(i)+1; + for (int j = 0; j < numprimes; j++) { + if (primes[j] >= cap) break; + if (i % primes[j] == 0) { + isprime = false; + break; + } + } + if (isprime) primes[numprimes++] = i; + } + + int T,N,M; + scanf("%d",&T); + + for (int t = 0; t < T; t++) { + if (t) printf("\n"); + scanf("%d %d",&M,&N); + if (M < 2) M = 2; + + int cap = sqrt(N) + 1; + + bool isprime[100001]; + memset(isprime,true,sizeof(isprime)); + + for (int i = 0; i < numprimes; i++) { + int p = primes[i]; + + if(p >= cap) break; + + int start; + + if (p >= M) start = p*2; + else start = M + ((p - M % p) % p); + + for (int j = start; j <= N; j += p) { + isprime[j - M] = false; + } + } + + int start = (M % 2)?M:M+1; + + if (M == 2) { + printf("2\n"); + } + for (int i = start; i <= N; i+=2) { + if (isprime[i-M]) printf("%d\n",i); + } + } + return 0; +} diff --git a/spoj_cpp_clean/PRIONPRI/PRIONPRI-21.cpp b/spoj_cpp_clean/PRIONPRI/PRIONPRI-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b50aee0849496a7025c934a3855e823ba2b86049 --- /dev/null +++ b/spoj_cpp_clean/PRIONPRI/PRIONPRI-21.cpp @@ -0,0 +1,27 @@ +/* + math > number theory > prime numbers + difficulty: easy + date: 14/Nov/2019 + problem: prime checking + by: @brpapa +*/ +#include +#include +using namespace std; + +bool isPrime(int n) { + for (int i = 2; i <= sqrt(n); i++) + if (n % i == 0) + return false; + return true; +} + +int main() { + int t, tmp; + cin >> t; + for (int i = 0; i < t; i++) { + cin >> tmp; + printf("%s\n", isPrime(tmp) ? "YES" : "NO"); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/PRMQUER/PRMQUER-14.cpp b/spoj_cpp_clean/PRMQUER/PRMQUER-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98fc414c3afce3f07e3a26c98a8509bc951fd827 --- /dev/null +++ b/spoj_cpp_clean/PRMQUER/PRMQUER-14.cpp @@ -0,0 +1,187 @@ +#include +using namespace std; + +#define siz 10000000 +#define ll long long +bitset mark; +int ara[100009], tree[300009], tree2[300009], prop[300009]; + +void propagate(int lo, int hi, int node) + +{ + if(lo != hi) { + prop[2 * node] = prop[node]; + prop[2 * node + 1] = prop[node]; + } + + int x = prop[node]; + if(x <= siz) { + if(mark[x] == 0 && (x == 2 || (x % 2 != 0))) { + tree[node] = hi - lo + 1; + } + else + tree[node] = 0; + } + else + tree[node] = 0; + + if(lo == hi) + tree2[node] = x; + + prop[node] = 1e9; +} + +void build(int lo, int hi, int node) + +{ + if(lo == hi) { + tree2[node] = ara[lo]; + if(ara[lo] <= siz) { + if(mark[ ara[lo] ] == 0 && (ara[lo] == 2 || (ara[lo] % 2 != 0))) { + tree[node] = 1; + return; + } + else { + tree[node] = 0; + return; + } + } + else { + tree[node] = 0; + return; + } + } + + int mid = (lo + hi) / 2; + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + tree[node] = tree[2 * node] + tree[2 * node + 1]; +} + +void update(int lo, int hi, int indx, int val, int node) + +{ + if(prop[node] != 1e9) propagate(lo, hi, node); + + if(indx > hi || indx < lo) + return; + + if(lo == indx && hi == indx) { + tree2[node] += val; + int x = tree2[node]; + if(x <= siz) + { + if(mark[x] == 0 && (x == 2 || (x % 2 != 0))) + { + tree[node] = 1; + } + else + tree[node] = 0; + } + else + tree[node] = 0; + + return; + } + + int mid = (lo + hi) / 2; + update(lo, mid, indx, val, 2 * node); + update(mid + 1, hi, indx, val, 2 * node + 1); + + tree[node] = tree[2 * node] + tree[2 * node + 1]; +} + +void update2(int lo, int hi, int left, int right, int x, int node) + +{ + if(prop[node] != 1e9) propagate(lo, hi, node); + + if(lo > right || hi < left) + return; + + if(lo >= left && hi <= right) { + if(x <= siz) + { + if(mark[x] == 0 && (x == 2 || (x % 2 != 0)) ) + { + tree[node] = hi - lo + 1; + } + else + tree[node] = 0; + } + else + tree[node] = 0; + + prop[node] = x; + return; + } + + int mid = (lo + hi) / 2; + update2(lo, mid, left, right, x, 2 * node); + update2(mid + 1, hi, left, right, x, 2 * node + 1); + + tree[node] = tree[2 * node] + tree[2 * node + 1]; +} + +int query(int lo, int hi, int left, int right, int node) + +{ + if(prop[node] != 1e9) propagate(lo, hi, node); + + if(lo > right || hi < left) + return 0; + + if(lo >= left && hi <= right) + return tree[node]; + + int mid = (lo + hi) / 2; + int p1 = query(lo, mid, left, right, 2 * node); + int p2 = query(mid + 1, hi, left, right, 2 * node + 1); + return p1 + p2; +} + +int main() + +{ + for(int i = 0; i <= 300005; i++) + prop[i] = 1e9; + + mark[1] = 1; + for(ll i = 3; i * i <= siz; i += 2) { + if(mark[i] == 0) { + for(ll j = i * i; j <= siz; j += i + i) { + mark[j] = 1; + } + } + } + + int n, q; + cin >> n >> q; + for(int i = 1; i <= n; i++) { + scanf("%d", &ara[i]); + } + build(1, n, 1); + + for(int i = 1; i <= q; i++) { + char ch; + scanf(" %c", &ch); + if(ch == 'A') { + int val, indx; + scanf("%d %d", &val, &indx); + update(1, n, indx, val, 1); + } + else if(ch == 'R') { + int val, l, r; + scanf("%d %d %d", &val, &l, &r); + update2(1, n, l, r, val, 1); + } + else { + int l, r; + scanf("%d %d", &l, &r); + int ans = query(1, n, l, r, 1); + printf("%d\n", ans); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/PRO/PRO-7.cpp b/spoj_cpp_clean/PRO/PRO-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be7ce9595bef3117287fec36f0e800eeacc77a0d --- /dev/null +++ b/spoj_cpp_clean/PRO/PRO-7.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int main() +{ + int t,k,aux; + multiset s; + multiset::iterator prin,fin; + long long int ans; + + scanf("%d",&t); + ans=0; + while(t--) + { + scanf("%d",&k); + for (int i = 0; i < k; ++i) + { + scanf("%d",&aux); + s.insert(aux); + } + + prin= s.begin(); + fin= s.end(); + fin--; + + ans+= (*fin) - (*prin); + + s.erase(prin); + s.erase(fin); + } + + printf("%lld\n",ans ); + + return 0; +} diff --git a/spoj_cpp_clean/PSYCHON/PSYCHON-9.cpp b/spoj_cpp_clean/PSYCHON/PSYCHON-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cc394a92d361c0d63fbbacb5c2113dfe9a6f203 --- /dev/null +++ b/spoj_cpp_clean/PSYCHON/PSYCHON-9.cpp @@ -0,0 +1,104 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#define SIEVE_N 10000001 + +using namespace std; + +bool sieve[SIEVE_N+1]; +vector primes; + +inline void calc_primes() +{ + int i, j; + + primes.push_back(2); + + for(i=4; i<=SIEVE_N; i+=2) + sieve[i] = 1; + + for(i=3; i<=SIEVE_N; i+=2) + if(!sieve[i]) + { + primes.push_back(i); + for(j=i+i; j<=SIEVE_N; j+=i) + sieve[j] = 1; + } +} + +inline bool psycho(int num) +{ + vector prime_divisors; + int prime_pos = 0, even = 0, odd = 0, counter = 0, i, prev; + + while(sieve[num]) + { + if(num % primes[prime_pos] == 0) + { + prime_divisors.push_back(primes[prime_pos]); + num /= primes[prime_pos]; + } + else + prime_pos++; + } + + prime_divisors.push_back(num); + + prev = prime_divisors[0]; + counter++; + + for(i=1; iodd) + return 1; + return 0; +} + +int main() +{ + int T, num; + + calc_primes(); + + scanf("%d", &T); + + while(T--) + { + scanf("%d", &num); + + if(psycho(num)) + printf("Psycho Number\n"); + else + printf("Ordinary Number\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/PT07X/PT07X-14.cpp b/spoj_cpp_clean/PT07X/PT07X-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe63a7dd34191364d9a8a0f4be432afef4aced1b --- /dev/null +++ b/spoj_cpp_clean/PT07X/PT07X-14.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +#define ll long long +#define pb push_back + +vector graph[100009]; +ll dp[100009][2]; + +ll dfs(ll u, ll p, bool state) + +{ + ll &ret = dp[u][state]; + if(ret != -1) + return ret; + ll rt = 0; + for(ll i = 0; i < graph[u].size(); i++) { + ll nd = graph[u][i]; + if(nd == p) + continue; + + rt += min( (ll)(state ^ 1) + dfs(nd, u, state ^ 1), 1 + dfs(nd, u, state | 1) ); + } + + return ret = rt; +} + +int main() + +{ + memset(dp, -1, sizeof(dp) ); + + ll n; + cin >> n; + for(ll i = 1; i < n; i++) { + ll u, v; + scanf("%lld %lld", &u, &v); + graph[u].pb(v); + graph[v].pb(u); + } + + ll ans1 = dfs(1, -1, 0); + ll ans2 = 1 + dfs(1, -1, 1); + + //cout << ans1 << " " << ans2 << endl; + cout << min(ans1, ans2) << endl; + + return 0; diff --git a/spoj_cpp_clean/PT07Y/PT07Y-12.cpp b/spoj_cpp_clean/PT07Y/PT07Y-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bab886427802c65c1f4c4317a74c3246875af3c3 --- /dev/null +++ b/spoj_cpp_clean/PT07Y/PT07Y-12.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +vector> vli; + +bool visitado[10010]; +bool flag = false; + +void runDFS(int u) +{ + list::iterator it; + for (it = vli[u].begin(); it != vli[u].end(); it++) { + if (!visitado[*it]) { + visitado[*it] = true; + runDFS(*it); + } else if (visitado[*it]) { + flag = true; + return; + } + } +} + +void DFS(int n) +{ + for (int i = 0; i < n; ++i) + if (!visitado[i]) { + runDFS(i); + if (flag) { + cout << "NO"; + return; + } + } + cout << "YES"; +} + +int main() +{ + ios::sync_with_stdio(false); + int n, m; + cin >> n >> m; + vli.resize(n); + for (int i = 0; i < n; ++i) + visitado[i] = false; + int a, b; + for (int i = 0; i < m; ++i) { + cin >> a >> b; + vli[a - 1].push_back(b - 1); + } + DFS(n); + return 0; +} diff --git a/spoj_cpp_clean/PT07Y/PT07Y-16.cpp b/spoj_cpp_clean/PT07Y/PT07Y-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b7fbcf2bc3f7353f945ff5a1fab66940c754ccf --- /dev/null +++ b/spoj_cpp_clean/PT07Y/PT07Y-16.cpp @@ -0,0 +1,58 @@ +// Author - Amit Kr. Dubey + +#include +#include +#include +#include + +#define ll long long +#define pb push_back + +using namespace std; + +map isVisited; +vector< vector > adj; +ll counter= 0; + +ll dfs(ll s){ + if(isVisited[s]){ + return 0; + } + else{ + isVisited[s]=true; + counter++; + for(auto x : adj[s]){ + dfs(x); + } + } + return 0; +} + +void solve(){ + ll n,m; + cin>>n>>m; + adj.resize(n+1); + ll a,b; + for(ll i=0;i>a>>b; + adj[a].pb(b); + adj[b].pb(a); + } + dfs(a); + ll nodes = counter; + if(m==nodes-1){ + cout<<"YES\n"; + } + else{ + cout<<"NO\n"; + } + return; +} + +int main(){ + int t=1; + //cin>>t; + while(t--){ + solve(); + } + return 0; diff --git a/spoj_cpp_clean/PT07Y/PT07Y-17.cpp b/spoj_cpp_clean/PT07Y/PT07Y-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb627891a50232f19e867c191c250e269e30ba0d --- /dev/null +++ b/spoj_cpp_clean/PT07Y/PT07Y-17.cpp @@ -0,0 +1,46 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +using namespace std; +typedef long long ll; +void fast() { + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); +} +const int N=(int)1e4+5; + +int n,m; +vector adj[N]; +int vis[N]; +void dfs(int node) { + vis[node]=1; + + for(auto ch:adj[node]) { + if(!vis[ch]) dfs(ch); + } +} +int main() { + fast(); + + int a, b; + + cin >> n >> m; + + for (int i = 0; i < m; i++) { + cin >> a >> b; + adj[a].push_back(b); + adj[b].push_back(a); + } + + int ans = 0; + for (int i = 1; i <= n; i++) { + if (!vis[i]) { + ans++; + dfs(i); + } + } + if(ans==1 && n==(m+1)) cout<<"YES\n"; + else cout<<"NO\n"; + + +} \ No newline at end of file diff --git a/spoj_cpp_clean/PT07Y/PT07Y-5.cpp b/spoj_cpp_clean/PT07Y/PT07Y-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebeaa90ca0a304fd0467234554f4f7b439cd059e --- /dev/null +++ b/spoj_cpp_clean/PT07Y/PT07Y-5.cpp @@ -0,0 +1,79 @@ +#include +#include +//#include + +using namespace std; + +class graph +{ + int v; + list *adj; + bool isCyclicUtil(int v,bool visited[],int parent); +public: + void addEdge(int u, int v); + bool isCyclic(); + graph(int v); +}; +//bool graph::isCyclicUtil(int u,bool visited[],int parent); +graph::graph(int v) +{ + this->v=v; + adj=new list[v]; +} +void graph::addEdge(int u, int v) +{ + adj[u-1].push_back(v-1); + adj[v-1].push_back(u-1); +} + +bool graph::isCyclicUtil(int u,bool visited[],int parent) +{ + visited[u]=true; + list::iterator i; + for(i=adj[u].begin();i!=adj[u].end();i++) + { + if(!visited[*i]) + { + if(isCyclicUtil(*i,visited,u)) + return true; + } + else if(*i!=parent) + return true; + } + return false; +} + +bool graph::isCyclic() +{ + bool *visited=new bool[v]; + for(int i=0;i>v>>e; + graph obj(v); + for(int i=0;i>x>>y; + obj.addEdge(x,y); + } + if(!obj.isCyclic()&&e==(v-1)) + cout<<"YES\n"; + else + cout<<"NO\n"; + return 0; +} diff --git a/spoj_cpp_clean/PT07Y/PT07Y-7.cpp b/spoj_cpp_clean/PT07Y/PT07Y-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ee476a7d603a95536e8c4c1c7e81b2eae1f5a85 --- /dev/null +++ b/spoj_cpp_clean/PT07Y/PT07Y-7.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +class UF { + int *id, cnt, *sz; + public: +// Create an empty union find data structure with N isolated sets. +UF(int N) { + cnt = N; id = new int[N]; sz = new int[N]; + for (int i = 0; iconnected(u,v)) + { + set->merge(u,v); + }else + { + printf("NO\n"); + return 0; + } + + } + + printf("YES\n"); + + return 0; +} diff --git a/spoj_cpp_clean/PT07Y/PT07Y-8.cpp b/spoj_cpp_clean/PT07Y/PT07Y-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cf445027a68a6b77af2cd1bcfd9515760a30341 --- /dev/null +++ b/spoj_cpp_clean/PT07Y/PT07Y-8.cpp @@ -0,0 +1,107 @@ +/*************************************************** + * Problem Name : PT07Y - Is it a tree.cpp + * Problem Link : https://www.spoj.com/problems/PT07Y/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-12-16 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 10005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +bool vis[MAX]; +bool flag; +int cnt; +void dfs (int u, int p) { + vis[u] = true; + cnt++; + + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + if (vis[v]) { + flag = 1; + return; + } + + dfs (v, u); + } +} +int main () { + __FastIO; + int n, m; + cin >> n >> m; + + for (int i = 0; i < m; i++) { + int u, v; + cin >> u >> v; + adj[u].pb (v); + adj[v].pb (u); + } + + if (m != n - 1) cout << "NO\n"; + else { + SET (vis, false); + flag = 0; + cnt = 0; + dfs (1, -1); + + if (flag || cnt != n) cout << "NO\n"; + else cout << "YES\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/PT07Y/PT07Y-9.cpp b/spoj_cpp_clean/PT07Y/PT07Y-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9c72a3366205560fcbc7183d808a3158a570133 --- /dev/null +++ b/spoj_cpp_clean/PT07Y/PT07Y-9.cpp @@ -0,0 +1,48 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +set A; + +int main() +{ + int i, n, m; + int v1, v2; + + scanf("%d %d", &n, &m); + + if(n-m!=1) + { + printf("NO\n"); + return 0; + } + + for(i=0; i +using namespace std; + +int dist[100009]; +vector vis(100009); +vector adj[100009]; + +pair bfs(int s, int n){ + for(int i=0;i<=n;i++) vis[i]=false; + for(int i=0;i<=n;i++) dist[i]=0; + vis[s]=true; + queue q; + q.push(s); + while(!q.empty()){ + int a = q.front(); + q.pop(); + for(int b : adj[a]){ + if(vis[b]) continue; + q.push(b); + vis[b]=true; + dist[b]=dist[a]+1; + } + } + int dis=0, id=0; + for(int i=1;i<=n;i++){ + if(dis>nodes; + for(int i=0;i>a>>b; + adj[a].push_back(b); + adj[b].push_back(a); + } + auto _ = bfs(1,nodes); + int node1=_.first; + auto __ = bfs(node1,nodes); + int dis = __.second; + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/PT07Z/PT07Z-3.cpp b/spoj_cpp_clean/PT07Z/PT07Z-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7954b9cc3c0f51701654d50a2e217ce8f8d9d94c --- /dev/null +++ b/spoj_cpp_clean/PT07Z/PT07Z-3.cpp @@ -0,0 +1,69 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it) +#define meta __FUNCTION__,__LINE__ + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +using namespace std; + +const long double PI = 3.1415926535897932384626433832795; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void tr(S x, const Strings&... rest){cout< pii; + +const int N = 100100; + +vector g[N]; + +int x, mx; + +void dfs(int cur, int len, int prev){ + if(len > mx){ + mx = len; + x = cur; + } + foreach(it, g[cur]){ + if(*it == prev) continue; + + dfs(*it, len+1, cur); + } +} + +int main(){ + int n; sd(n); + for(int i = 1; i < n; i++){ + int u, v; + sd2(u,v); + g[u].pb(v); + g[v].pb(u); + } + + x = 1, mx = 0; + dfs(1,0,-1); + dfs(x,0,-1); + + printf("%d\n", mx); + + return 0; +} diff --git a/spoj_cpp_clean/PT07Z/PT07Z-5.cpp b/spoj_cpp_clean/PT07Z/PT07Z-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cafa27a443ca733b383593edad13fe79d86bae4 --- /dev/null +++ b/spoj_cpp_clean/PT07Z/PT07Z-5.cpp @@ -0,0 +1,65 @@ +#include +#include +#include + +using namespace std; +int dfs(list *adj,stack &s,bool *visited, int *distance) +{ + int temp,peg,dist=0; + list::iterator itr; + distance[s.top()]=0; + int node; + while(s.empty()==false) + { + temp=s.top(); + s.pop(); + if(distance[temp]>=dist)// This block checks for the farthest node from the starting point of dfs and returns that node + { + dist=distance[temp]; + node=temp; + } + visited[temp]=true; + for(itr=adj[temp].begin(); itr!=adj[temp].end(); itr++) + { + if(visited[*itr]==false) + { + s.push(*itr); + distance[*itr]=distance[temp]+1; + } + } + } + return node; +} +int main() +{ + int num,e1,e2,i,j,k,temp,u,v; + cin>>num; + list adj[num]; + stack s; + bool visited[num]; + int distance[num]; + for(i=0;i>e1>>e2; + adj[e1-1].push_back(e2-1); + adj[e2-1].push_back(e1-1); + } + s.push(0); + u=dfs(adj,s,visited,distance); + while(!s.empty()) + s.pop(); + s.push(u); + for(i=0;i + +using namespace std; + +vector< int > ady[10010]; +int dist[10010]; + + +int max(int x, int y) +{ + return (x>y)?x:y; +} + +int bfs(int s, int size){ + int maxi=0; + int v; + int aux; + queue q; + + memset(dist, -1, sizeof(dist)); + dist[s] = 0; + q.push(s); + + while (!q.empty()) { + int u = q.front(); q.pop(); + + for (int i=0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +vector< vector > G; +list< pair > Q; +vector V; +int N; + +void init() +{ + int i; + int from, to; + + scanf("%d", &N); + + G.resize(N+1); + V.resize(N+1); + for(i=0; i mmax, curr; + int i; + + Q.push_back(make_pair(0, 1)); + + while(!Q.empty()) + { + curr=Q.front(); + Q.pop_front(); + + mmax=max(mmax,curr); + V[curr.second]=1; + + for(i=0; i mmax, curr; + int i; + + Q.push_back(make_pair(0, root)); + + while(!Q.empty()) + { + curr=Q.front(); + Q.pop_front(); + + mmax=max(mmax, curr); + V[curr.second]=1; + + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100000005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +char mark[MAX]; +int precal_row[MAX]; +int precal_col[MAX]; + +int lp[MAX]; +vectorprime; +void linear_sieve (int N) { + for (int i = 2; i <= N; ++i) { + if (lp[i] == 0) { + lp[i] = i; + prime.pb(i); + } + + for (int j = 0; j < (int) prime.size() && prime[j] <= lp[i] && i * prime[j] <= N; ++j) + lp[i * prime[j]] = prime[j]; + } + + int cnt = 0, x = 1, len = 1; + + for (int v : prime) { + cnt++; + mark[v] = 1; + precal_row[v] = x; + precal_col[v] = cnt; + + if (cnt == len) { + cnt = 0, len++, x++; + } + } +} +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + linear_sieve (MAX - 3); + int tc; + cin >> tc; + + while (tc--) { + int n; + cin >> n; + mark[n] == 1 ? cout << precal_row[n] << " " << precal_col[n] << "\n" : cout << + -1 << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/PUCMM210/PUCMM210-3.cpp b/spoj_cpp_clean/PUCMM210/PUCMM210-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fba61b7150c5da9929f2dd5fff65efa1c9f9cc8a --- /dev/null +++ b/spoj_cpp_clean/PUCMM210/PUCMM210-3.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +long long sum[1000001], MOD = 1000000003; + +int main(){ + int t, n; + long long temp; + sum[1] = 1; + for(long long i = 2; i <= 1000000; i++){ + temp = ((i*(i+1))/2)%MOD; + sum[i] = (sum[i-1] + (temp*temp)%MOD)%MOD; + } + sd(t); + while(t--){ + sd(n); + printf("%lld\n",sum[n]); + } + return 0; +} diff --git a/spoj_cpp_clean/PWRARR/PWRARR-9.cpp b/spoj_cpp_clean/PWRARR/PWRARR-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8846928a68514c4cfea65c51486e596a93965b8 --- /dev/null +++ b/spoj_cpp_clean/PWRARR/PWRARR-9.cpp @@ -0,0 +1,122 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar_unlocked() + +using namespace std; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +class BITPQ +{ +private: + int tree[50007]; + int N; + +public: + BITPQ(int n = 50007) + { + N = n; + memset(tree, 0, sizeof(tree)); + } + + inline void reset(int n = 50007) + { + N = n; + memset(tree, 0, sizeof(tree)); + } + + inline void point_update(int pos, int val) + { + for (; pos <= N; pos += pos&(-pos)) + tree[pos] += val; + } + + inline void range_update(int i, int j, int val) + { + point_update(i, val); + point_update(j + 1, -val); + } + + inline int point_query(int pos) + { + int sum = 0; + + for (; pos > 0; pos -= pos&(-pos)) + sum += tree[pos]; + + return sum; + } +}; + +int A[50007]; +BITPQ tree; +int N; + +inline void init() +{ + int i; + + N = FAST_IO(); + tree.reset(); + + for (i = 1; i <= N; i++) + { + A[i] = FAST_IO(); + tree.point_update(A[i], 1); + } +} + +inline long long solve() +{ + long long ans = 0; + int i; + + for (i = 1; i <= N; i++) + ans += tree.point_query(A[i]); + + return ans; +} + +int main() +{ + int T; + + //freopen("input.txt", "r", stdin); + + T = FAST_IO(); + + while (T--) + { + init(); + printf("%lld\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/QKP/QKP-3.cpp b/spoj_cpp_clean/QKP/QKP-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ca48b3802b8bbc87c5b5d04d2b9040f3af77b78 --- /dev/null +++ b/spoj_cpp_clean/QKP/QKP-3.cpp @@ -0,0 +1,38 @@ +#include +#define FOR(i,n) for(int i = 0; i < n; ++i) +int dq[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, +{1, 1}, {1, 0}, {1, -1}, {0, -1}}; +int dk[8][2] = {{-2,-1},{-1,-2}, {1,-2}, {2, -1}, +{-2, 1},{-1, 2}, {1, 2}, {2, 1}}; +int ss; +char b[1000][1000]; +void read(char T){ +int k, x, y; scanf("%d", &k); ss -= k; +while(k-- && scanf("%d %d", &x, &y) == 2) b[--x][--y] = T; +} +int main(void){ +for(int bn = 1, c, r; scanf("%d %d", &r, &c) == 2 && (r || c); ++bn){ +ss = r * c; +FOR(i, r) FOR(j, c) b[i][j] = '.'; +read('Q'); read('K'); read('P'); +FOR(i, r) FOR(j, c) +if(b[i][j] == 'K') FOR(d, 8){ +int ni = i + dk[d][0], nj = j + dk[d][1]; +if(ni < 0 || nj < 0 || ni >= r || nj >= c) continue; +ss -= b[ni][nj] == '.'; +if(b[ni][nj] == '.') b[ni][nj] = 'A'; +} +else if(b[i][j] == 'Q') FOR(d, 8){ +int ni = i + dq[d][0], nj = j + dq[d][1]; +while(ni >= 0 && ni < r && nj >= 0 && nj < c){ +if(b[ni][nj] > 'A') break; +ss -= b[ni][nj] == '.'; +b[ni][nj] = 'A'; +ni += dq[d][0]; +nj += dq[d][1]; +} +} +printf("Board %d has %d safe squares.\n", bn, ss); +} +return 0; +} diff --git a/spoj_cpp_clean/QMAX3VN/QMAX3VN-3.cpp b/spoj_cpp_clean/QMAX3VN/QMAX3VN-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76a0abbcc5d27c6bc33179c138f81233b5d23284 --- /dev/null +++ b/spoj_cpp_clean/QMAX3VN/QMAX3VN-3.cpp @@ -0,0 +1,189 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<size : 0; +} + +void updSize(node *cur){ + if(!cur) return; + cur->size = getSize(cur->l) + 1 + getSize(cur->r); +} + +void updNode(node *cur){ + if(!cur) return; + cur->mx = cur->val; + if(cur->l){ + cur->mx = max(cur->mx, cur->l->mx); + } + if(cur->r){ + cur->mx = max(cur->mx, cur->r->mx); + } +} + +typedef pair LR; + +LR split(node *root, int pos){ + if(!root) return LR(NULL, NULL); + +// propagate(root); // lazy propagation + + LR ret; + + int cur_size = getSize(root->l) + 1; + if(cur_size <= pos){ + LR tmp = split(root->r, pos - cur_size); + root->r = tmp.fi; + ret = mp(root, tmp.se); + } + else{ + LR tmp = split(root->l, pos); + root->l = tmp.se; + ret = mp(tmp.fi, root); + } + + updSize(root); // update the parameters of root since its children have possibly been modified + updNode(root); + return ret; +} + +node *merge(node *l, node *r){ + if(!l or !r) return l ? l : r; + + if(l->pri > r->pri){ +// propagate(l); // lazy propagation + l->r = merge(l->r, r); + updSize(l); // update the parameters of l since its children have possibly been modified + updNode(l); + return l; + } + else{ +// propagate(r); // lazy propagation + r->l = merge(l, r->l); + updSize(r); // update the parameters of r since its children have possibly been modified + updNode(r); + return r; + } +} + +int range_query(node * &treap, int x, int y){ + node *l, *mid, *r; + + LR tmp; + + tmp = split(treap, y); + mid = tmp.fi, r = tmp.se; + + tmp = split(mid, x-1); + l = tmp.fi, mid = tmp.se; + + int ans = mid->mx; + + treap = merge(l, mid); + treap = merge(treap, r); + + return ans; +} + +void insert(node * &treap, int x, int y){ + node *nn = new node(y); + + LR tmp = split(treap, x-1); + + treap = merge(tmp.fi, nn); + treap = merge(treap, tmp.se); +} + +} // end of namespace + +using namespace Treap; + +void solve(int n){ + char c; + int x, y; + + cin >> c >> x >> y; + node *treap = new node(x); + + for(int i = 2; i <= n; i++){ + cin >> c >> x >> y; + + if(c == 'A'){ + insert(treap, y, x); + } + else{ + cout << range_query(treap, x, y) << '\n'; + } + } +} + +int main(){ _ + srand(time(NULL)); + int n; + while(cin >> n){ + solve(n); + } + return 0; +} diff --git a/spoj_cpp_clean/QTREE/QTREE-0.cpp b/spoj_cpp_clean/QTREE/QTREE-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e460655e2470a2d208ba196716275a236fb804a --- /dev/null +++ b/spoj_cpp_clean/QTREE/QTREE-0.cpp @@ -0,0 +1,304 @@ +#include + +typedef int ll; + +struct BinaryLiftingGraph { + typedef long long ll; + const ll INF = 98765432100LL; + + const ll root = 0; + ll n; + std::vector> graph; + std::vector> table; + std::vector depth; + bool unbuilt; + + BinaryLiftingGraph(const ll& _n) : n(_n) { + graph.resize(n); + unbuilt = true; + } + + void add_edge(const ll& a, const ll& b) { + graph[a].push_back(b); + graph[b].push_back(a); + unbuilt = true; + } + + /* run this before queries. */ + void build(void) { + if (!unbuilt) return; + unbuilt = false; + + table.assign(std::ceil(std::log2(n))+1, std::vector(n,-1)); + depth.assign(n, INF); + + std::queue order; + order.push(root); + depth[root] = 0; + + while (!order.empty()) { + ll cur = order.front(); + order.pop(); + + for (const auto& neigh : graph[cur]) { + if (depth[neigh] > 1 + depth[cur]) { + depth[neigh] = 1 + depth[cur]; + table[0][neigh] = cur; + order.push(neigh); + } + } + } + + for (ll deep = 1; deep < static_cast(table.size()); ++deep) { + for (ll node = 0; node < n; ++node){ + ll mid = table[deep-1][node]; + if (mid != -1) + table[deep][node] = table[deep-1][mid]; + } + } + } + + ll walk(ll node, const ll& k) { + build(); + for (ll i = 0; i < static_cast(table.size()) && node != -1; ++i) { + if (k & (1 << i)) + node = table[i][node]; + } + return node; + } + + ll get_lca(ll a, ll b) { + build(); + if (depth[a] > depth[b]) + a = walk(a, depth[a] - depth[b]); + else if (depth[b] > depth[a]) + b = walk(b, depth[b] - depth[a]); + + if (a == b) { + return a; + } + + for (ll i = table.size()-1; i >= 0; --i) { + if (table[i][a] != table[i][b]) { + a = table[i][a]; + b = table[i][b]; + } + } + + return walk(a,1); + } + + /* returns edge distance between two nodes in tree. */ + ll dist(const ll& a, const ll& b) { + build(); + return depth[a] + depth[b] - 2 * depth[get_lca(a,b)]; + } +}; + +template +struct st { + const ll n; + ll max[4 * MAX_N + 1]; + + st(const ll& _n) : n(_n) { + memset(max, 0, sizeof(max)); + } + + void upd(const ll& l, const ll& v) { + upd(0, 0, n-1, l, v); + } + + void upd(const ll& i, const ll& rl, const ll& rr, const ll& l, const ll& v) { + if (rl == l && rr == l) { + max[i] = v; + } else { + ll mid = (rl + rr) / 2; + if (l <= mid) + upd(2 * i + 1, rl, mid, l, v); + else + upd(2 * i + 2, mid+1, rr, l, v); + max[i] = std::max(max[2*i+1], max[2*i+2]); + } + } + + ll qry(const ll& l, const ll& r) const { + return qry(0, 0, n-1, l, r); + } + + ll qry(const ll& i, const ll& rl, const ll& rr, const ll& l, const ll& r) const { + if (rl == l && rr == r) { + return max[i]; + } + ll mid = (rl + rr) / 2; + ll ret = 0; + if (l <= mid) + ret = std::max(ret, qry(2*i+1, rl, mid, l, std::min(mid,r))); + if (mid < r) + ret = std::max(ret, qry(2*i+2, mid+1, rr, std::max(mid+1,l), r)); + return ret; + } +}; + +template +struct hld { + + struct edge { + ll a, b, id; + bool heavy; + }; + + const ll n; ll edge_count; + std::vector gr[MAX_N]; + ll edge_weights[MAX_N], edge_order[MAX_N], edge_map[MAX_N]; + + // dfs stuff + ll po, pre[MAX_N], skip[MAX_N], sub[MAX_N], dep[MAX_N]; + edge parent[MAX_N]; + + // decomp stuff + ll chain_head[MAX_N], first_edge[MAX_N], last_edge[MAX_N], in_chain[MAX_N], pos_in_chain[MAX_N], max_kid[MAX_N], chain_count; + + st segtree; + + hld(const ll& _n) : n(_n), edge_count(0), segtree(_n) { + memset(chain_head, 0xff, sizeof(chain_head)); + memset(first_edge, 0xff, sizeof(first_edge)); + memset(last_edge , 0xff, sizeof(last_edge)); + } + + void add_edge(const ll& a, const ll& b, const ll& c = 1) { + gr[a].push_back(edge{a,b,edge_count,false}); + gr[b].push_back(edge{b,a,edge_count,false}); + edge_weights[edge_count++] = c; + } + + void run(void) { + po = 0; dfs(0, -1); + + edge_count = 0; + decomp(0, -1, chain_count = 0); + for (ll i = 0; i < edge_count; ++i) { + segtree.upd(edge_order[i], edge_weights[i]); + } + } + + ll dfs(const ll& cur, const ll& par) { + pre[cur] = po++; + sub[cur] = 1; + dep[cur] = par >= 0 ? dep[par] + 1 : 0; + for (const auto& nei : gr[cur]) { + if (nei.b != par) { + parent[nei.b] = nei; + sub[cur] += dfs(nei.b, cur); + } + } + skip[cur] = po-1; + return sub[cur]; + } + + void decomp(const ll& cur, const ll& par, const ll& cc) { + if (chain_head[cc] < 0) chain_head[cc] = cur; + else edge_map[parent[cur].id] = cc; + + in_chain[cur] = cc; + pos_in_chain[cur] = dep[cur] - dep[chain_head[cc]]; + + ll max_sub = 0; max_kid[cur] = -1; + for (const auto& nei : gr[cur]) { + if (nei.b != par && max_sub < sub[nei.b]) { + max_sub = sub[max_kid[cur] = nei.b]; + } + } + + if (max_kid[cur] == -1) { + last_edge[cc] = chain_head[cc] == cur ? -1 : parent[cur].id; + return ; + } + + if (chain_head[cc] == cur) { + first_edge[cc] = parent[max_kid[cur]].id; + } + edge_order[parent[max_kid[cur]].id] = edge_count++; + decomp(max_kid[cur], cur, cc); + + for (const auto& nei : gr[cur]) { + if (nei.b == par || nei.b == max_kid[cur]) continue; + edge_order[nei.id] = edge_count++; + decomp(nei.b, cur, ++chain_count); + } + } + + ll qry(const ll& node, const ll& ancestor) { + ll ret = 0; + ll cur = node; + for (; in_chain[cur] != in_chain[ancestor]; ) { + if (first_edge[in_chain[cur]] >= 0) { + ret = std::max(ret, segtree.qry(edge_order[first_edge[in_chain[cur]]], edge_order[parent[cur].id])); + } + cur = chain_head[in_chain[cur]]; + ret = std::max(ret, edge_weights[parent[cur].id]); + cur = parent[cur].a; + } + + if (cur != ancestor) { + ret = std::max(ret, segtree.qry(edge_order[parent[max_kid[ancestor]].id], edge_order[parent[cur].id])); + } + return ret; + } + + void upd(const ll& edge_id, const ll& val) { + segtree.upd(edge_order[edge_id], edge_weights[edge_id] = val); + } +}; + +static inline void setio(void); + +int main(void) { + setio(); + + ll t; std::cin >> t; + + while (t--) { + ll n; std::cin >> n; + + hld<10001> tree(n); + BinaryLiftingGraph lifty(n); + + for (ll i = 1, a,b,c; i < n; ++i) { + std::cin >> a >> b >> c; + tree.add_edge(a-1, b-1, c); + lifty.add_edge(a-1, b-1); + } + + tree.run(); + lifty.build(); + + // for (ll i = 0; i < n; ++i) { + // std::cerr << "node " << i+1 << " is in the chain " << tree.in_chain[i] << "\n"; + // } + + for (;;) { + std::string type; std::cin >> type; + if (type == "DONE") break; + else if (type == "CHANGE") { + ll id, cost; std::cin >> id >> cost; + tree.upd(id-1, cost); + } else { + ll a, b, lca; std::cin >> a >> b; + lca = lifty.get_lca(a-1,b-1); + // std::cerr << "lca of " << a << " and " << b << " is " << lca+1 << "\n"; + std::cout << std::max(tree.qry(a-1, lca), tree.qry(b-1, lca)) << "\n"; + } + // std::cout << std::flush; + } + } + + // std::cerr << static_cast(clock())/CLOCKS_PER_SEC << "\n"; + + return 0; +} + +static inline void setio(void) { + std::ios::sync_with_stdio(false);std::cin.tie(nullptr); + std::cout.precision(10); std::cout << std::fixed; +} diff --git a/spoj_cpp_clean/QTREE/QTREE-18.cpp b/spoj_cpp_clean/QTREE/QTREE-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08b885c646e5667b58540850415c420ddf287a15 --- /dev/null +++ b/spoj_cpp_clean/QTREE/QTREE-18.cpp @@ -0,0 +1,162 @@ +// Problem name: Query on a tree +// Problem link: https://www.spoj.com/problems/QTREE/ +// Submission link: https://www.spoj.com/submit/QTREE/id=21038186 + +#include +#include + +#define MAX ((int)10000) +#define DAD(x) ((x) >> 1) +#define LEFT(x) ((x) << 1) +#define RIGHT(x) ((x) << 1 | 1) +#define mp(i, j) make_pair(i, j) +#define F first +#define S second +#define pb(x) push_back(x) +#define endl '\n' + +using namespace std; + +typedef pair pii; +typedef vector vi; +typedef vector vii; +typedef vector vvii; + +struct SegmentTree{ +private: + int n; + vi tree; + +public: + SegmentTree(int n) : n(n), tree(n << 1, 0){ } + + void Build(vi array){ + if(n != array.size()) + n = array.size(), tree.assign(n << 1, 0); + for(int i = 0 ; i < n ; i++) + tree[ i + n ] = array[ i ]; + for(int i = n - 1 ; i ; i--) + tree[ i ] = max(tree[ LEFT(i) ], tree[ RIGHT(i) ]); + } + + void Update(int pos, int v){ + for(tree[ pos += n ] = v ; pos ; pos = DAD(pos)) + tree[ DAD(pos) ] = max(tree[ pos ], tree[ pos ^ 1 ]); + } + + int Query(int l, int r){ + int ans = (~0); + for(l += n, r += n ; l < r ; l = DAD(l), r = DAD(r)){ + if(l & 1) + ans = max(ans, tree[ l++ ]); + if(r & 1) + ans = max(ans, tree[ --r ]); + } + return ans; + } +}; + +struct HeavyLightDecomposition{ +private: + int n, root; + vi size, dad, specialChild, depth, position, chainHead, parentEdge; + vvii adj; + SegmentTree tree; + + int Map(int v){ + return --v; + } + + void DFS(int u){ + size[ u ] = 1; + for(int i = adj[ u ].size() - 1, v ; i >= 0 ; i--){ + v = adj[ u ][ i ].F; + if(v != dad[ u ]){ + dad[ v ] = u; + depth[ v ] = depth[ u ] + 1; + parentEdge[ v ] = adj[ u ][ i ].S; + DFS(v); + size[ u ] += size[ v ]; + if(specialChild[ u ] == -1 || size[ specialChild[ u ] ] < size[ v ]) + specialChild[ u ] = v; + } + } + } + + int LCA(int u, int v){ + while(chainHead[ u ] != chainHead[ v ]){ + if(depth[ chainHead[ u ] ] < depth[ chainHead[ v ] ]) + swap(u, v); + u = dad[ chainHead[ u ] ]; + } + return depth[ u ] < depth[ v ] ? u : v; + } + +public: + HeavyLightDecomposition(int n) : n(n), size(n, 0), dad(n, -1), specialChild(n, -1), depth(n, 0), position(n, 0), chainHead(n, 0), parentEdge(n, 0), tree(n), adj(n){ } + + void AddEdge(int u, int v, int w){ + adj[ u = Map(u) ].pb(mp(v = Map(v), w)); + adj[ v ].pb(mp(u, w)); + } + + void Build(int from){ + root = Map(from); + vi baseArray(n); + DFS(root); + for(int u = 0, pos = 0 ; u < n ; u++) + if(dad[ u ] == -1 || specialChild[ dad[ u ] ] != u) + for(int v = u ; v != -1 ; v = specialChild[ v ]) + chainHead[ v ] = u, position[ v ] = pos, baseArray[ pos++ ] = parentEdge[ v ]; + tree.Build(baseArray); + } + + void Update(int u, int v, int w){ + u = Map(u); + v = Map(v); + if(u == dad[ v ]) + swap(u, v); + tree.Update(position[ u ], w); + } + + int Query(int u, int v){ + if(u == v) + return 0; + u = Map(u); + v = Map(v); + int ans = (~0); + while(chainHead[ u ] != chainHead[ v ]){ + if(depth[ chainHead[ u ] ] < depth[ chainHead[ v ] ]) + swap(u, v); + ans = max(ans, tree.Query(position[ chainHead[ u ] ], position[ u ] + 1)); + u = dad[ chainHead[ u ] ]; + } + if(depth[ u ] > depth[ v ]) + swap(u, v); + return max(ans, tree.Query(position[ u ] + 1, position[ v ] + 1)); + } +}; + +int main(){ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + int t, n, u, v, w; + pii edge[ MAX ]; + for(cin >> t ; t-- ; ){ + cin >> n; + HeavyLightDecomposition hld(n); + for(int i = 1 ; i < n ; i++) + cin >> u >> v >> w, hld.AddEdge(u, v, w), edge[ i - 1 ] = mp(u, v); + hld.Build(1); + for(string s ; cin >> s && s != "DONE" ; ){ + cin >> u >> v; + if(s[ 0 ] == 'Q') + cout << hld.Query(u, v) << endl; + else + u--, hld.Update(edge[ u ].F, edge[ u ].S, v); + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/QTREE/QTREE-3.cpp b/spoj_cpp_clean/QTREE/QTREE-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e633fe67058b0ea7b1da77c35d12727a8ba92288 --- /dev/null +++ b/spoj_cpp_clean/QTREE/QTREE-3.cpp @@ -0,0 +1,230 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d%d",&x,&y); +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z); + +#define root 0 +#define N 10001 +#define LOGN 14 + +using namespace std; + +vector adj_list[N], costs[N], indexx[N]; +int t, n, a, b, c; +int chain_no, chain_ind[N], chain_head[N], pos_in_base[N]; +int base_array[N], ptr; +int depth[N], pa[LOGN][N], other_end[N], subtree_size[N]; +int s_tree[N*2]; +char instr[7]; + +void init(int i, int l, int r){ + if(l+1 == r){ //Different from the segment tree template where you you l == r. + s_tree[i] = base_array[l]; //This is because we are initalizing as l,m; m,r instead of l,m; m+1,r + return; + } + int left = (i<<1), right = left + 1, mid = (l+r)>>1; + + init(left, l, mid); + init(right, mid, r); + + s_tree[i] = s_tree[left] > s_tree[right] ? s_tree[left]: s_tree[right]; + + return; +} + +void update_tree(int i, int l, int r, int x, int val){ + //cout<<"Are we there yet?"< x or r <= x) //Not in range + return; + if(l == x and l+1 == r){ //Perfectly in range + s_tree[i] = val; + return; + } + + int left = (i<<1), right = left + 1, mid = (l+r)>>1; + + update_tree(left, l, mid, x, val); //Update left half + update_tree(right, mid, r, x, val); //Update right half + + s_tree[i] = s_tree[left] > s_tree[right] ? s_tree[left]: s_tree[right]; //Merge the result + return; +} + +int query_tree(int i, int l, int r, int x, int y){ + if(l >= y or r <= x) //Completely out of range + return -1; + else if(l >= x and r <= y) + return s_tree[i]; + + int left = (i<<1), right = left + 1, mid = (l+r)>>1; + + return max(query_tree(left, l, mid, x, y), query_tree(right, mid, r, x, y)); +} + +int query_up(int u, int v){ //This assumes that v is the ancestor of u + if(u == v) return 0; + + int uchain, vchain = chain_ind[v], ans = -1; + + while(1){ + uchain = chain_ind[u]; + + if(uchain == vchain){ //We are in the same chain and hence we can directly perform the query from u to v + if(u == v) break; + //query from u to v since we are in the final chain + ans = max(ans, query_tree(1, 0, ptr, pos_in_base[v] + 1, pos_in_base[u] + 1)); + break; + } + //Query from u to its chain head + ans = max(ans, query_tree(1, 0, ptr, pos_in_base[chain_head[uchain]], pos_in_base[u] + 1)); + u = pa[0][chain_head[uchain]]; //Go to the next chain + } + + return ans; +} + +int LCA(int u, int v){ + if(depth[u] < depth[v]) + swap(u, v); + + int diff = depth[u] - depth[v]; + + for(int i = 0; i < LOGN; i++) + if((diff>>i) & 1) + u = pa[i][u]; + + if(u != v){ + for(int i = LOGN-1; i >= 0; i--){ + if(pa[i][u] != pa[i][v]) + { + u = pa[i][u]; + v = pa[i][v]; + } + } + u = pa[0][u]; + } + return u; +} + +void hld(int cur, int cost, int prev){ + if(chain_head[chain_no] == -1){ + chain_head[chain_no] = cur; + } + chain_ind[cur] = chain_no; //The chain number in which a node belongs + pos_in_base[cur] = ptr; //The position of the node in base_array + base_array[ptr++] = cost; //Cost of the edge connecting current node to its parent + + int s_child = -1, new_cost, l = adj_list[cur].size(); + + for(int i = 0; i < l; i++){ + if(adj_list[cur][i] != prev){ + if(s_child == -1 || subtree_size[adj_list[cur][i]] > subtree_size[s_child]){ + s_child = adj_list[cur][i]; //The child with maximum subtree size + new_cost = costs[cur][i]; //The cost of the edge connecting the special child to the current node + } + } + } + if(s_child != -1){ //This condition will fail for leaf nodes, so we will have nothing else left to do + hld(s_child, new_cost, cur); //Add the special child to the current chain + } + + for(int i = 0; i < l; i++){ + if(adj_list[cur][i] != s_child and adj_list[cur][i] != prev){ + chain_no++; + hld(adj_list[cur][i], costs[cur][i], cur); //Decompose the normal children into new chains + } + } + + return; +} + +void dfs(int cur, int prev, int d){ + pa[0][cur] = prev; //The immediate ancestor of the current node + depth[cur] = d; //The depth of the current node + subtree_size[cur] = 1; //The size of the subtree rooted at the current node + int l = adj_list[cur].size(); + for(int i = 0; i < l; i++){ + if(adj_list[cur][i] != prev){ + other_end[indexx[cur][i]] = adj_list[cur][i]; //The other end of the current edge + dfs(adj_list[cur][i], cur, d+1); //Performing dfs on the neighbour of each node + subtree_size[cur] += subtree_size[adj_list[cur][i]]; //Updating the size of the subtree + } + } + return; +} + +void query(int u, int v){ + int lca = LCA(u,v); + printf("%d\n",max(query_up(u,lca),query_up(v,lca))); + return; +} + +void change(int edge, int val){ + update_tree(1, 0, ptr, pos_in_base[other_end[edge]], val); + return; +} + +int main(){ + sd(t); + while(t--){ + + sd(n); + + for(int i = 0; i < n; i++){ + adj_list[i].clear(); //Reset the containers to accomodate for new test case + costs[i].clear(); + indexx[i].clear(); + chain_head[i] = -1; + for(int j = 0; j < LOGN; j++) + pa[j][i] = -1; + } + + for(int i = 1; i < n; i++){ + sd3(a,b,c); + a--, b--; + adj_list[a].push_back(b); + adj_list[b].push_back(a); + costs[a].push_back(c); + costs[b].push_back(c); + indexx[a].push_back(i-1); + indexx[b].push_back(i-1); + } + + chain_no = 0, ptr = 0; + dfs(root, -1, 0); //Will compute the subtree size of each node, their depths and immediate ancestors; + hld(root, -1, -1); //Decompose the tree into log(N) number of linear chains + init(1, 0, ptr); //Initialize the segment + + for(int i = 1; i < LOGN; i++){ //Compute the jump table for lca using dynamic programming + for(int j = 0; j < n; j++){ + if(pa[i-1][j] != -1) + pa[i][j] = pa[i-1][pa[i-1][j]]; + } + } + + while(1){ + scanf("%s",instr); + if(instr[0] == 'D') + break; + sd2(a,b); + if(instr[0] == 'Q'){ + query(a-1, b-1); + } + else{ + change(a-1, b); + } + } + + } + return 0; +} diff --git a/spoj_cpp_clean/QTREE2/QTREE2-0.cpp b/spoj_cpp_clean/QTREE2/QTREE2-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..131b6f93e4c71559195dd1b9adf0fa697e30afd6 --- /dev/null +++ b/spoj_cpp_clean/QTREE2/QTREE2-0.cpp @@ -0,0 +1,135 @@ +#include + +#define FI first +#define SE second + +typedef long long ll; +typedef std::pair pairll; + +const static ll INF = 0x3f3f3f3f3f3f3f3f; + +static inline void setio(void); + +template +struct binary_lifting_graph { + + const ll n, root; + ll lift[LOG][MAX_N]; + ll depth[MAX_N], dist[MAX_N]; + + std::vector gr[MAX_N]; + + binary_lifting_graph(const ll& _n) : n(_n), root(0) { + memset(lift, 0xff, sizeof(lift)); + memset(depth, 0x3f, sizeof(depth)); + memset(dist, 0x3f, sizeof(dist)); + } + + void add_edge(const ll& a, const ll& b, const ll& c) { + gr[a].push_back({b, c}); + gr[b].push_back({a, c}); + } + + void run(void) { + depth[root] = dist[root] = 0; + std::queue order; + order.push(root); + while (!order.empty()) { + ll cur = order.front(); order.pop(); + for (const auto& nei : gr[cur]) { + if (depth[nei.FI] > 1 + depth[cur]) { + dist[nei.FI] = nei.SE + dist[cur]; + depth[nei.FI] = 1 + depth[cur]; + lift[0][nei.FI] = cur; + order.push(nei.FI); + } + } + } + + for (ll lg = 1; lg < LOG; ++lg) { + for (ll i = 0; i < n; ++i) { + ll mid = lift[lg-1][i]; + if (mid != -1) + lift[lg][i] = lift[lg-1][mid]; + } + } + } + + ll walk(ll i, const ll& k) const { + for (ll lg = LOG-1; lg >= 0; --lg) { + if (k & (1 << lg)) + i = lift[lg][i]; + } + return i; + } + + ll get_lca(ll a, ll b) const { + if (depth[a] > depth[b]) + a = walk(a, depth[a] - depth[b]); + if (depth[b] > depth[a]) + b = walk(b, depth[b] - depth[a]); + + if (a == b) return a; + + for (ll lg = LOG-1; lg >= 0; --lg) { + if (lift[lg][a] != lift[lg][b]) { + a = lift[lg][a]; + b = lift[lg][b]; + } + } + + return walk(a, 1); + } + + ll get_weighted_dist(const ll& a, const ll& b) const { + return dist[a] + dist[b] - 2 * dist[get_lca(a,b)]; + } + + ll get_uweighted_dist(const ll& a, const ll& b) const { + return depth[a] + depth[b] - 2 * depth[get_lca(a,b)]; + } +}; + +int main(void) { + setio(); + + ll t; std::cin >> t; + while (t--) { + ll n; std::cin >> n; + + binary_lifting_graph<15, 10001> gr(n); + for (ll i = 1, a,b,c; i < n; ++i) { + std::cin >> a >> b >> c; + gr.add_edge(a-1,b-1,c); + } + + gr.run(); + + for (std::string type; ; ) { + std::cin >> type; + if (type == "DONE") break; + else if (type == "DIST") { + ll a, b; std::cin >> a >> b; + std::cout << gr.get_weighted_dist(a-1, b-1) << "\n"; + } else { + ll a,b,k; std::cin >> a >> b >> k; + const ll lca = gr.get_lca(--a, --b); + + const ll atolca = gr.get_uweighted_dist(a, lca) + 1; + if (atolca >= k) { + std::cout << gr.walk(a, k-1)+1 << "\n"; + } else { + k -= atolca; + std::cout << gr.walk(b, gr.get_uweighted_dist(b, lca) - k)+1 << "\n"; + } + } + } + } + + return 0; +} + +static inline void setio(void) { + std::ios::sync_with_stdio(false);std::cin.tie(nullptr); + std::cout.precision(10); std::cout << std::fixed; +} \ No newline at end of file diff --git a/spoj_cpp_clean/QTREE2/QTREE2-3.cpp b/spoj_cpp_clean/QTREE2/QTREE2-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4d06a5b63ece4b47e9ebaaaca543e5ec02443b5 --- /dev/null +++ b/spoj_cpp_clean/QTREE2/QTREE2-3.cpp @@ -0,0 +1,244 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d%d",&x,&y); +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z); + +#define root 0 +#define N 10100 +#define LOGN 14 + +using namespace std; + +vector adj_list[N], lengths[N], indexx[N]; +int t, n, a, b, c; +int chain_no, chain_ind[N], chain_head[N], pos_in_base[N]; +int base_array[N], ptr; +int depth[N], pa[LOGN][N], other_end[N], subtree_size[N]; +long long s_tree[N*4]; +char instr[7]; + +void init(int i, int l, int r){ + if(l+1 == r){ //Different from the segment tree template where you use l == r. + s_tree[i] = base_array[l]; //This is because we are initalizing as l,m; m,r instead of l,m; m+1,r + return; + } + int left = (i<<1), right = left + 1, mid = (l+r)>>1; + + init(left, l, mid); + init(right, mid, r); + + s_tree[i] = s_tree[left] + s_tree[right]; //Sum of the distances in the left subtree + sum of distances in the right subtree + + return; +} + +long long query_tree(int i, int l, int r, int x, int y){ + if(l >= y or r <= x) //Completely out of range + return 0; + else if(l >= x and r <= y) + return s_tree[i]; + + int left = (i<<1), right = left + 1, mid = (l+r)>>1; + + return query_tree(left, l, mid, x, y) + query_tree(right, mid, r, x, y); +} + +long long query_up(int u, int v){ //This assumes that v is the ancestor of u + if(u == v) return 0; + + int uchain, vchain = chain_ind[v]; + long long ans = 0; + + while(1){ + uchain = chain_ind[u]; + + if(uchain == vchain){ //We are in the same chain and hence we can directly perform the query from u to v + if(u == v) break; + //query from u to v since we are in the final chain + ans += query_tree(1, 0, ptr, pos_in_base[v] + 1, pos_in_base[u] + 1); + break; + } + //Query from u to its chain head + ans += query_tree(1, 0, ptr, pos_in_base[chain_head[uchain]], pos_in_base[u] + 1); + u = pa[0][chain_head[uchain]]; //Go to the next chain + } + + return ans; +} + +int LCA(int u, int v){ + if(depth[u] < depth[v]) + swap(u, v); + + int diff = depth[u] - depth[v]; + + for(int i = 0; i < LOGN; i++) + if((diff>>i) & 1) + u = pa[i][u]; + + if(u != v){ + for(int i = LOGN-1; i >= 0; i--){ + if(pa[i][u] != pa[i][v]) + { + u = pa[i][u]; + v = pa[i][v]; + } + } + u = pa[0][u]; + } + return u; +} + +void hld(int cur, int length, int prev){ + if(chain_head[chain_no] == -1){ + chain_head[chain_no] = cur; + } + chain_ind[cur] = chain_no; //The chain number in which a node belongs + pos_in_base[cur] = ptr; //The position of the node in base_array + base_array[ptr++] = length; //Cost of the edge connecting current node to its parent + + int s_child = -1, new_length, l = adj_list[cur].size(); + + for(int i = 0; i < l; i++){ + if(adj_list[cur][i] != prev){ + if(s_child == -1 || subtree_size[adj_list[cur][i]] > subtree_size[s_child]){ + s_child = adj_list[cur][i]; //The child with maximum subtree size + new_length = lengths[cur][i]; //The length of the edge connecting the special child to the current node + } + } + } + if(s_child != -1){ //This condition will fail for leaf nodes, so we will have nothing else left to do + hld(s_child, new_length, cur); //Add the special child to the current chain + } + + for(int i = 0; i < l; i++){ + if(adj_list[cur][i] != s_child and adj_list[cur][i] != prev){ + chain_no++; + hld(adj_list[cur][i], lengths[cur][i], cur); //Decompose the normal children into new chains + } + } + + return; +} + +void dfs(int cur, int prev, int d){ + pa[0][cur] = prev; //The immediate ancestor of the current node + depth[cur] = d; //The depth of the current node + subtree_size[cur] = 1; //The size of the subtree rooted at the current node + int l = adj_list[cur].size(); + for(int i = 0; i < l; i++){ + if(adj_list[cur][i] != prev){ + other_end[indexx[cur][i]] = adj_list[cur][i]; //The other end of the current edge + dfs(adj_list[cur][i], cur, d+1); //Performing dfs on the neighbour of each node + subtree_size[cur] += subtree_size[adj_list[cur][i]]; //Updating the size of the subtree + } + } + return; +} + +void query(int u, int v){ + int lca = LCA(u,v); + printf("%lld\n",query_up(u,lca) + query_up(v,lca)); + return; +} + +void kth(int u, int v, int k){ + if(k == 1){ + printf("%d\n",u+1); + return; + } + int lca = LCA(u,v), diff = depth[u] - depth[lca]; + if(diff == k-1){ + printf("%d\n",lca+1); + } + else if(diff > k-1){ + //Now we just need the k-1 th ancestor of u + k--; + for(int i = LOGN; i >= 0; i--){ + if( (1<=0; i--){ + if( (1< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +class LCA { + public: + vectorlevel, par; + vector >road_cost, table; + LCA (vector > &adj, vector> &cost, int n) { + level.resize (n + 1); + par.resize (n + 1); + road_cost.resize (n + 1); + table.resize (n + 1); + + for (int i = 0; i <= n; i++) { + road_cost[i].resize (17, 0); + table[i].resize (17, -1); + } + + par[1] = -1; + level[1] = 0; + road_cost[1][0] = 0; + dfs (adj, cost, 1, -1); + lca_build (n); + } + void dfs (vector > &adj, vector> &cost, int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + level[v] = level[u] + 1; + par[v] = u; + road_cost[v][0] = cost[u][i]; + dfs (adj, cost, v, u); + } + } + void lca_build (int n) { + for (int i = 1; i <= n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 1; i <= n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + road_cost[i][j] = road_cost[i][j - 1] + road_cost[x][j - 1]; + + } + } + } + } + int lca_query (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; + } + int get_cost (int u, int v) { + int len = level[u] - level[v]; + int sum = 0; + int lim = log2 (len); + + for (int i = lim; i >= 0; i--) { + if ( (1 << i) <= len) { + sum = sum + road_cost[u][i]; + u = table[u][i]; + len -= (1 << i); + } + + if (len == 0) break; + } + + return sum; + } + int get_kth(int u, int v,int k){ + int x = lca_query(u,v); + int len = level[u] - level[x]+1; + if(k>len){ + len = k-len; + len = (level[v] - level[x]) - len; + + int lim = log2(len); + for(int i = lim; i>=0; i--){ + if((1<=0; i--){ + if((1<> tc; + + while (tc--) { + int n; + cin >> n; + vector > adj (n + 1), cost (n + 1); + + for (int i = 0; i < n - 1; i++) { + int u, v, w; + cin >> u >> v >> w; + adj[u].pb (v); + adj[v].pb (u); + cost[u].pb (w); + cost[v].pb (w); + } + + LCA obj (adj, cost, n); + string str; + + while (cin >> str) { + if (str == "DONE") { + break; + } + + if (str == "DIST") { + int u, v; + cin >> u >> v; + int x = obj.lca_query (u, v); + int res = obj.get_cost (u, x) + obj.get_cost (v, x); + cout << res << "\n"; + + } else if (str == "KTH") { + int u, v, k; + cin >> u >> v >> k; + int res = obj.get_kth(u, v, k); + cout << res << "\n"; + } + } + } + + return 0; +} +/* +1 + +6 +1 2 1 +2 4 1 +2 5 2 +1 3 1 +3 6 2 +KTH 4 6 4 +DIST 4 6 + +*/ diff --git a/spoj_cpp_clean/QTREE3/QTREE3-0.cpp b/spoj_cpp_clean/QTREE3/QTREE3-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..387cbefefeccbc10dd55d4a159ba0a439163937f --- /dev/null +++ b/spoj_cpp_clean/QTREE3/QTREE3-0.cpp @@ -0,0 +1,173 @@ +#include + +typedef int ll; + +const static ll INF = 0x3f3f3f3f; + +static inline void setio(void); + +template +struct st { + const ll n; + ll min[4 * MAX_N + 1]; + + st(const ll& _n) : n(_n) { + memset(min, 0x3f, sizeof(min)); + } + + void upd(const ll& l, const ll& v) { + upd(0, 0, n-1, l, v); + } + + void upd(const ll& i, const ll& rl, const ll& rr, const ll& l, const ll& v) { + if (rl == l && rr == l) { + min[i] = v; + } else { + ll mid = (rl + rr) / 2; + if (l <= mid) + upd(2 * i + 1, rl, mid, l, v); + else + upd(2 * i + 2, mid+1, rr, l, v); + min[i] = std::min(min[2*i+1], min[2*i+2]); + } + } + + ll qry(const ll& l, const ll& r) const { + return qry(0, 0, n-1, l, r); + } + + ll qry(const ll& i, const ll& rl, const ll& rr, const ll& l, const ll& r) const { + if (rl == l && rr == r) { + return min[i]; + } + ll mid = (rl + rr) / 2; + ll ret = INF; + if (l <= mid) + ret = std::min(ret, qry(2*i+1, rl, mid, l, std::min(mid,r))); + if (mid < r) + ret = std::min(ret, qry(2*i+2, mid+1, rr, std::min(mid+1,l), r)); + return ret; + } +}; + +template +struct hld { + struct edge { + ll a, b; + }; + + const ll n; + std::vector gr[MAX_N]; + + ll sub[MAX_N], dep[MAX_N]; + edge parent[MAX_N]; + + ll chain_head[MAX_N], in_chain[MAX_N], + t_order[MAX_N], inv_t_order[MAX_N], + max_kid[MAX_N], po, chain_count; + + st segtree; + + char is_black[MAX_N]; + + hld(const ll& _n) : n(_n), segtree(_n) { + memset(chain_head, 0xff, sizeof(chain_head)); + memset(is_black, 0, sizeof(is_black)); + } + + void add_edge(const ll& a, const ll& b) { + gr[a].push_back({a, b}); + gr[b].push_back({b, a}); + } + + void run(void) { + dfs(0, -1); + + po = 0; + decomp(0, -1, chain_count = 0); + } + + ll dfs(const ll& cur, const ll& par) { + dep[cur] = par >= 0 ? dep[par] + 1 : 0; + sub[cur] = 1; + for (const auto& nei : gr[cur]) { + if (nei.b != par) { + parent[nei.b] = nei; + sub[cur] += dfs(nei.b, cur); + } + } + return sub[cur]; + } + + void decomp(const ll& cur, const ll& par, const ll& cc) { + if (chain_head[cc] < 0) chain_head[cc] = cur; + + in_chain[cur] = cc; + t_order[cur] = po++; + inv_t_order[t_order[cur]] = cur; + + ll max_sub = 0; max_kid[cur] = -1; + for (const auto& nei : gr[cur]) { + if (nei.b != par && max_sub < sub[nei.b]) { + max_sub = sub[max_kid[cur] = nei.b]; + } + } + + if (max_kid[cur] == -1) return; // importante + + decomp(max_kid[cur], cur, cc); + + for (const auto& nei : gr[cur]) { + if (nei.b == par || nei.b == max_kid[cur]) continue; + decomp(nei.b, cur, ++chain_count); + } + } + + ll qry(const ll& node, const ll& ancestor) { + ll ret = INF, cur = node; + for (; in_chain[cur] != in_chain[ancestor]; ) { + ret = std::min(ret, segtree.qry(t_order[chain_head[in_chain[cur]]], t_order[cur])); + cur = parent[chain_head[in_chain[cur]]].a; + } + ret = std::min(ret, segtree.qry(t_order[ancestor], t_order[cur])); + if (ret == INF) return -1; + else return inv_t_order[ret]+1; + } + + void upd(const ll& node) { + if (is_black[node]) { + segtree.upd(t_order[node], INF); + } else { + segtree.upd(t_order[node], t_order[node]); + } + is_black[node] = !is_black[node]; + } +}; + +int main(void) { + setio(); + + ll n, q; std::cin >> n >> q; + + hld<100001> gr(n); + for (ll i = 1, a, b; i < n; ++i) { + std::cin >> a >> b; + gr.add_edge(a-1, b-1); + } + + gr.run(); + + while (q--) { + ll type, v; std::cin >> type >> v; + if (type == 0) + gr.upd(v-1); + else std::cout << gr.qry(v-1, 0) << "\n"; + } + + return 0; +} + +static inline void setio(void) { + std::ios::sync_with_stdio(false);std::cin.tie(nullptr); + std::cout.precision(10); std::cout << std::fixed; +} diff --git a/spoj_cpp_clean/QTREE3/QTREE3-3.cpp b/spoj_cpp_clean/QTREE3/QTREE3-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2bae05bd93979d3d23a82a07bc9387f36f6bfae --- /dev/null +++ b/spoj_cpp_clean/QTREE3/QTREE3-3.cpp @@ -0,0 +1,234 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d%d",&x,&y); +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z); + +#define root 0 +#define N 101000 +#define LOGN 17 + +using namespace std; + +int BLACK = 1; +int WHITE = 0; + +struct point{ + int color, ind; + + void assign_leaf(int x, int y){ + color = x; + ind = y; + } + + void merge(point& left, point& right){ + if(left.color == BLACK){ + color = BLACK; + ind = left.ind; + } + else if(right.color == BLACK){ + color = BLACK; + ind = right.ind; + } + else{ + color = WHITE; + ind = -1; + } + } + +}; + +vector adj_list[N]; +int n, q, a, b, c; +int chain_no, chain_ind[N], chain_head[N], pos_in_base[N], chain_count[N], color[N]; +int base_array[N], reverse_map[N], ptr; +int depth[N], pa[N], subtree_size[N]; +point s_tree[N*6]; + +void init(int i, int l, int r){ + if(l+1 == r){ + s_tree[i].assign_leaf(base_array[l], l); + return; + } + int left = (i<<1), right = left + 1, mid = (l+r)>>1; + + init(left, l, mid); + init(right, mid, r); + + s_tree[i].merge(s_tree[left],s_tree[right]); + return; +} + +void update_tree(int i, int l, int r, int x){ + if(l > x or r <= x) //Not in range + return; + if(l == x and l+1 == r){ //Perfectly in range + s_tree[i].color = 1 - s_tree[i].color; + if(s_tree[i].color == BLACK){ + s_tree[i].ind = l; + } + else{ + s_tree[i].ind = -1; + } + return; + } + + int left = (i<<1), right = left + 1, mid = (l+r)>>1; + + update_tree(left, l, mid, x); //Update left half + update_tree(right, mid, r, x); //Update right half + + s_tree[i].merge(s_tree[left],s_tree[right]); //Merge the result + return; +} + +point query_tree(int i, int l, int r, int x, int y){ + if(l >= y or r <= x){ //Completely out of range + point res; + res.assign_leaf(WHITE, -1); + return res; + } + else if(l >= x and r <= y) + return s_tree[i]; + + int left = (i<<1), right = left + 1, mid = (l+r)>>1; + point res, le = query_tree(left, l, mid, x, y), ri = query_tree(right, mid, r, x, y); + res.merge(le, ri); + return res; +} + +int query_up(int u, int v){ + if(color[v] == BLACK) + return 1; + else if(u == v){ + return -1; + } + + + int uchain, vchain = chain_ind[v], ind = -1; + point temp; + while(1){ + uchain = chain_ind[u]; + if(uchain == vchain){ + if(u == v) break; + + //if(chain_count[uchain] > 0){ + temp = query_tree(1, 0, ptr, pos_in_base[v]+1, pos_in_base[u]+1); + if(temp.color == BLACK) + ind = temp.ind; + //} + break; + } + //if(chain_count[uchain] > 0){ + temp = query_tree(1, 0, ptr, pos_in_base[chain_head[uchain]], pos_in_base[u] + 1); + if(temp.color == BLACK) + ind = temp.ind; + //} + + u = pa[chain_head[uchain]]; + } + + if(ind == -1) + return -1; + else + return reverse_map[ind] + 1; + +} + +void hld(int cur, int prev){ + if(chain_head[chain_no] == -1){ + chain_head[chain_no] = cur; + } + chain_ind[cur] = chain_no; //The chain number in which a node belongs + pos_in_base[cur] = ptr; //The position of the node in base_array + reverse_map[ptr] = cur; + base_array[ptr++] = WHITE; //Cost of the edge connecting current node to its parent + + int s_child = -1, l = adj_list[cur].size(); + + for(int i = 0; i < l; i++){ + if(adj_list[cur][i] != prev){ + if(s_child == -1 || subtree_size[adj_list[cur][i]] > subtree_size[s_child]){ + s_child = adj_list[cur][i]; //The child with maximum subtree size + } + } + } + if(s_child != -1){ //This condition will fail for leaf nodes, so we will have nothing else left to do + hld(s_child, cur); //Add the special child to the current chain + } + + for(int i = 0; i < l; i++){ + if(adj_list[cur][i] != s_child and adj_list[cur][i] != prev){ + chain_no++; + hld(adj_list[cur][i], cur); //Decompose the normal children into new chains + } + } + + return; +} + +void dfs(int cur, int prev, int d){ + pa[cur] = prev; //The immediate ancestor of the current node + depth[cur] = d; //The depth of the current node + subtree_size[cur] = 1; //The size of the subtree rooted at the current node + int l = adj_list[cur].size(); + for(int i = 0; i < l; i++){ + if(adj_list[cur][i] != prev){ + dfs(adj_list[cur][i], cur, d+1); //Performing dfs on the neighbour of each node + subtree_size[cur] += subtree_size[adj_list[cur][i]]; //Updating the size of the subtree + } + } + return; +} + +int main(){ + + sd2(n,q); + + for(int i = 0; i < n; i++){ + chain_head[i] = -1; + color[i] = WHITE; + chain_count[i] = 0; + } + + for(int i = 1; i < n; i++){ + sd2(a,b); + a--, b--; + adj_list[a].push_back(b); + adj_list[b].push_back(a); + } + + chain_no = 0, ptr = 0; + dfs(root, -1, 0); //Will compute the subtree size of each node, their depths and immediate ancestors; + hld(root, -1); //Decompose the tree into log(N) number of linear chains + init(1, 0, ptr); //Initialize the segment + + for(int i = 0; i < q; i++){ + sd2(a,b); + if(a == 0){ + int node = b-1; + color[node] = 1 - color[node]; + if(color[node] == WHITE){ + chain_count[chain_ind[node]]--; + } + else{ + chain_count[chain_ind[node]]++; + } + update_tree(1, 0, ptr, pos_in_base[node]); + } + else{ + printf("%d\n",query_up(b-1,0)); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/QTREE5/QTREE5-18.cpp b/spoj_cpp_clean/QTREE5/QTREE5-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff49f058edcdba62f6ba5d152af2bd66852a74ea --- /dev/null +++ b/spoj_cpp_clean/QTREE5/QTREE5-18.cpp @@ -0,0 +1,143 @@ +// Problem name: Query on a tree V +// Problem link: https://www.spoj.com/problems/QTREE5/ +// Submission link: https://www.spoj.com/submit/QTREE5/id=21032863 + +#include +#include +#include + +#define pb(x) push_back(x) +#define LOG2(x) (32 - __builtin_clz(x)) +#define INF ((int)(1 << 17)) +#define endl '\n' + +using namespace std; + +typedef vector vi; +typedef vector vvi; + +struct CentroidDecomposition{ +private: + int n; + vvi adj, dist; + vi size, dad, depth, centroidDad, centroidDepth; + vector< multiset > minRed; + vector check; + + int Map(int v){ + return --v; + } + + void DFS(int u, int p = -1){ + size[ u ] = 1; + int v; + for(int i = adj[ u ].size() - 1 ; i >= 0 ; --i){ + v = adj[ u ][ i ]; + if(v != p && !check[ v ]){ + dad[ v ] = u; + depth[ v ] = depth[ u ] + 1; + DFS(v, u); + size[ u ] += size[ v ]; + } + } + } + + int GetCentroid(int u){ + DFS(u); + int n = size[ u ]; + for(int i = adj[ u ].size() - 1, v, parent = -1 ; i >= 0 ; --i){ + v = adj[ u ][ i ]; + if(v != parent && !check[ v ] && size[ v ] > (n >> 1)){ + parent = u; + u = v; + i = adj[ u ].size(); + } + } + return u; + } + + void FindDistances(int u, int p, int h, int d){ + dist[ h ][ u ] = d; + for(int i = adj[ u ].size() - 1, v ; i >= 0 ; --i){ + v = adj[ u ][ i ]; + if(v != p && !check[ v ]) + FindDistances(v, u, h, d + 1); + } + } + + void Build(int u, int p, int d){ + int centroid = GetCentroid(u); + centroidDad[ centroid ] = p; + centroidDepth[ centroid ] = d; + check[ centroid ] = 1; + FindDistances(centroid, -1, d, 0); + for(int i = adj[ centroid ].size() - 1, v ; i >= 0 ; --i){ + v = adj[ centroid ][ i ]; + if(!check[ v ]) + Build(v, centroid, d + 1); + } + } + +public: + CentroidDecomposition(int n) : n(n), adj(n), size(n, 0), dad(n, -1), depth(n, 0), check(n, 0), centroidDad(n, -1), centroidDepth(n, 0), dist(LOG2(n), vi(n, 0)), minRed(n){ } + + void AddEdge(int u, int v){ + adj[ u = Map(u) ].pb(v = Map(v)); + adj[ v ].pb(u); + } + + void Build(){ + Build(0, -1, 0); + check.assign(n, 0); + } + + int LCA(int u, int v){ + u = Map(u); + v = Map(v); + while(u != v){ + if(centroidDepth[ u ] < centroidDepth[ v ]) + swap(u, v); + u = centroidDad[ u ]; + } + return u; + } + + void Update(int u){ + u = Map(u); + bool white = check[ u ] = !check[ u ]; + for(int v = u ; v != -1 ; v = centroidDad[ v ]){ + if(white) + minRed[ v ].insert(dist[ centroidDepth[ v ] ][ u ]); + else + minRed[ v ].erase(minRed[ v ].find(dist[ centroidDepth[ v ] ][ u ])); + } + } + + int Query(int u){ + int ans = INF; + for(int v = u = Map(u) ; v != -1 ; v = centroidDad[ v ]) + ans = min(ans, (minRed[ v ].empty() ? INF : *(minRed[ v ].begin())) + dist[ centroidDepth[ v ] ][ u ]); + return ans; + } +}; + +int main(){ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + int n, m, u, v; + cin >> n; + CentroidDecomposition cd(n); + for(int i = 1 ; i < n ; i++) + cin >> u >> v, cd.AddEdge(u, v); + cd.Build(); + for(cin >> m ; m-- ; ){ + cin >> u >> v; + if(u == 0) + cd.Update(v); + else + cout << ((u = cd.Query(v)) == INF ? -1 : u) << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/QUE1/QUE1-9.cpp b/spoj_cpp_clean/QUE1/QUE1-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..112fd8e00fc1dc3f3a0fe633211905c29b62eff4 --- /dev/null +++ b/spoj_cpp_clean/QUE1/QUE1-9.cpp @@ -0,0 +1,124 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#include +#define gc() getchar_unlocked() +using namespace std; + +int N; +pair people[10007]; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +struct node +{ + int val; + int lesser; + struct node *left; + struct node *right; + + node(int v) + { + val = v; + lesser = 0; + left = right = NULL; + } +}; + +void insert(struct node **curr, int val, int lesser) +{ + if (*curr == NULL) + { + *curr = new struct node(val); + return; + } + + if (lesser <= (*curr)->lesser) + { + (*curr)->lesser++; + insert(&((*curr)->left), val, lesser); + } + + else + insert(&((*curr)->right), val, lesser - 1 - (*curr)->lesser); +} + +void print(struct node *curr) +{ + if (curr == NULL) + return; + + print(curr->left); + printf("%d ", curr->val); + print(curr->right); +} + +struct node *root; + +inline void init() +{ + int i; + + //scanf("%d", &N); + N = FAST_IO(); + + for (i = 0; i < N; i++) + //scanf("%d", &people[i].first); + people[i].first = FAST_IO(); + + for (i = 0; i < N; i++) + //scanf("%d", &people[i].second); + people[i].second = FAST_IO(); + + + sort(people, people + N, greater >()); + + root = new struct node(people[0].first); +} + +int main() +{ + int T, i; + + //freopen("input.txt", "r", stdin); + + //scanf("%d", &T); + T = FAST_IO(); + while (T--) + { + init(); + for (i = 1; i < N; i++) + insert(&root, people[i].first, people[i].second); + print(root); + putchar('\n'); + } + + return 0; +} diff --git a/spoj_cpp_clean/QUE2/QUE2-9.cpp b/spoj_cpp_clean/QUE2/QUE2-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..112fd8e00fc1dc3f3a0fe633211905c29b62eff4 --- /dev/null +++ b/spoj_cpp_clean/QUE2/QUE2-9.cpp @@ -0,0 +1,124 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#include +#define gc() getchar_unlocked() +using namespace std; + +int N; +pair people[10007]; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +struct node +{ + int val; + int lesser; + struct node *left; + struct node *right; + + node(int v) + { + val = v; + lesser = 0; + left = right = NULL; + } +}; + +void insert(struct node **curr, int val, int lesser) +{ + if (*curr == NULL) + { + *curr = new struct node(val); + return; + } + + if (lesser <= (*curr)->lesser) + { + (*curr)->lesser++; + insert(&((*curr)->left), val, lesser); + } + + else + insert(&((*curr)->right), val, lesser - 1 - (*curr)->lesser); +} + +void print(struct node *curr) +{ + if (curr == NULL) + return; + + print(curr->left); + printf("%d ", curr->val); + print(curr->right); +} + +struct node *root; + +inline void init() +{ + int i; + + //scanf("%d", &N); + N = FAST_IO(); + + for (i = 0; i < N; i++) + //scanf("%d", &people[i].first); + people[i].first = FAST_IO(); + + for (i = 0; i < N; i++) + //scanf("%d", &people[i].second); + people[i].second = FAST_IO(); + + + sort(people, people + N, greater >()); + + root = new struct node(people[0].first); +} + +int main() +{ + int T, i; + + //freopen("input.txt", "r", stdin); + + //scanf("%d", &T); + T = FAST_IO(); + while (T--) + { + init(); + for (i = 1; i < N; i++) + insert(&root, people[i].first, people[i].second); + print(root); + putchar('\n'); + } + + return 0; +} diff --git a/spoj_cpp_clean/QUERM/QUERM-10.cpp b/spoj_cpp_clean/QUERM/QUERM-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..755db173a176d429416fb509d0454c775587857d --- /dev/null +++ b/spoj_cpp_clean/QUERM/QUERM-10.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +int main() +{ + int n, test=0; + while(cin >> n && n != 0) + { + for(int j, i=0; i> j && j == (i+1)) + cout << "Teste " << ++test << endl << j << endl << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/QUEST5/QUEST5-3.cpp b/spoj_cpp_clean/QUEST5/QUEST5-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..001caafbc593397bc4ec5e6e78cf5cd10da60fb3 --- /dev/null +++ b/spoj_cpp_clean/QUEST5/QUEST5-3.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d%d",&x,&y); +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z); +#define fi first +#define se second + +using namespace std; + +int t, n, a, b, r, cnt; +vector > p; + +int main(){ + sd(t); + while(t--){ + sd(n); + cnt = 0; + p.clear(); + for(int i = 0; i < n; i++){ + sd2(a,b); + p.push_back(make_pair(b,a)); + } + + sort(p.begin(), p.end()); + + r = p[0].fi; + cnt = 1; + for(int i = 1; i < n; i++){ + if(p[i].se > r){ + cnt++; + r = p[i].fi; + } + } + + printf("%d\n",cnt); + } + return 0; +} diff --git a/spoj_cpp_clean/QUILT/QUILT-1.cpp b/spoj_cpp_clean/QUILT/QUILT-1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35f7f28c71d30cd57c83eecbcf69433166c60b0d --- /dev/null +++ b/spoj_cpp_clean/QUILT/QUILT-1.cpp @@ -0,0 +1,169 @@ +#include +#include +#include + +/* +S -> A + | B + | turn(S) + | sew(S, S) +*/ + +typedef struct { + char** info; + int rows, cols; +} thing; + +thing *newA() { + // char **a = (char**) malloc(sizeof(char*) * 2); + char **a = (char**) malloc(sizeof(char*) * 2); + a[0] = (char*) malloc(sizeof(char) * 2); + a[1] = (char*) malloc(sizeof(char) * 2); + a[0][0] = '/'; + a[0][1] = '/'; + a[1][0] = '/'; + a[1][1] = '+'; + + thing* result = (thing*) malloc(sizeof(thing)); + result->info = a; + result->rows = 2; + result->cols = 2; + return result; +} + +thing *newB() { + char **b = (char**) malloc(sizeof(char*) * 2); + b[0] = (char*) malloc(sizeof(char) * 2); + b[1] = (char*) malloc(sizeof(char) * 2); + b[0][0] = '-'; + b[0][1] = '-'; + b[1][0] = '-'; + b[1][1] = '-'; + + thing* result = (thing*) malloc(sizeof(thing)); + result->info = b; + result->rows = 2; + result->cols = 2; + return result; +} + +char transform(char c) { + switch (c) { + case '/' : return '\\'; + case '\\': return '/'; + case '-' : return '|'; + case '|' : return '-'; + } + return c; +} + +thing *turn(thing* t) { + std::cout << std::flush; + + int cols = t->cols, rows = t->rows; + char **result = (char**) malloc(sizeof(char*) * cols); + char **before = t->info; + + for (int i = 0; i < cols; ++i) { + result[i] = (char*) malloc(sizeof(char) * rows); + } + + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + result[j][rows - i - 1] = transform(before[i][j]); + } + free(before[i]); + } + free(before); + + t->info = result; + t->rows = cols; + t->cols = rows; + + return t; +} + +thing *sew(thing* l, thing *r) { + if (l->rows != r->rows) return NULL; + + int cols = l->cols + r->cols, rows = l->rows; + char** result = l->info; + + for (int i = 0; i < rows; ++i) { + result[i] = (char*) realloc(result[i], sizeof(char) * cols); + for (int j = 0; j < r->cols; ++j) { + result[i][j + l->cols] = r->info[i][j]; + } + free(r->info[i]); + } + free(r->info); + free(r); + + l->info = result; + l->rows = rows; + l->cols = cols; + return l; +} + +thing* parser() { + + char c; + c = getchar(); + + switch (c) { + case 'A': return newA(); + case 'B': return newB(); + + case 't': + if ((c = getchar()) == 'u' && + (c = getchar()) == 'r' && + (c = getchar()) == 'n') { + thing *to_turn = parser(); + return turn(to_turn); + } + + case 's': + if ((c = getchar()) == 'e' && + (c = getchar()) == 'w') { + thing *left = parser(); + thing *right = parser(); + return sew(left, right); + } + + case ',': + case '(': + case ')': + case ';': + case ' ': + case '\n': + return parser(); + } + + return NULL; +} + +int main() { + + thing *program; + int quilt = 0; + + for (program = parser(); !feof(stdin); program = parser()) { + ++quilt; + + std::cout << "Quilt " << quilt << ":" << std::endl; + + if (program == NULL) { + std::cout << "error" << std::endl; + continue; + } + + for (int i = 0; i < program->rows; ++i) { + for (int j = 0; j < program->cols; ++j) { + std::cout << program->info[i][j]; + } + std::cout << std::endl; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/R6PL/R6PL-9.cpp b/spoj_cpp_clean/R6PL/R6PL-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..344006fe6982ab12eb22a3c144154ac313aff529 --- /dev/null +++ b/spoj_cpp_clean/R6PL/R6PL-9.cpp @@ -0,0 +1,73 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define MAXN 207 +#define MAXV 257 +using namespace std; + +bool DP[MAXN][MAXN * MAXV]; +int N; + +inline void init() +{ + cin >> N; + memset(DP, 0, sizeof(DP)); + DP[0][0] = 1; +} + +inline int solve() +{ + int i, j, x, y; + + for (i = 0; i < N; i++) + { + cin >> x >> y; + int diff = max(x, y) - min(x, y); + + for (j = 0; j < MAXN * MAXV; j++) + if (DP[i][j]) + { + DP[i + 1][j + diff] = 1; + if (j - diff >= 0) + DP[i + 1][j - diff] = 1; + else + DP[i + 1][diff - j] = 1; + } + } + + for (i = 0; i < MAXN * MAXV; i++) + if (DP[N][i]) + return i; + + return -1; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + int T; + + cin >> T; + + while (T--) + { + init(); + cout << solve() << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/RAIL/RAIL-12.cpp b/spoj_cpp_clean/RAIL/RAIL-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c3f825c45e96da7869f080ed423a8380eeb38f --- /dev/null +++ b/spoj_cpp_clean/RAIL/RAIL-12.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#define mp make_pair + +using namespace std; + +int v, a; + +typedef pair ii; +typedef vector vii; +int visitado[10000]; + +int main() +{ + ios::sync_with_stdio(false); + int n, p; + cin >> n; + vector grafo; + while (n--) { + cin >> v >> a; + grafo.resize(v + 1); + vii::iterator ia, ib; + vii va, vb; + int b, c; + memset(visitado, 0, sizeof(visitado)); + for (int i = 0; i < a; ++i) { + cin >> b >> c >> p; + grafo[b].push_back(mp(c, p)); + grafo[c].push_back(mp(b, p)); + } + int num = a; + for (int i = 1; i <= v; ++i) { + if (grafo[i].size() == 2) { + int j = 0; + va = grafo[i]; + int a = va[j].first, p1 = va[j].second; + j++; + int b = va[j].first, p2 = va[j].second; + grafo[i].clear(); + ib = grafo[a].begin(); + while (ib->first != i) + ib++; + grafo[a].erase(ib + 0); + ib = grafo[b].begin(); + while (ib->first != i) + ib++; + grafo[b].erase(ib + 0); + grafo[a].push_back(mp(b, p1 + p2)); + grafo[b].push_back(mp(a, p1 + p2)); + num--; + } + } + cout << num << "\n"; + for (int i = 1; i <= v; ++i) + sort(grafo[i].begin(), grafo[i].end()); + ii ant = mp(0, 0); + for (int i = 1; i <= v; ++i) { + for (ia = grafo[i].begin(); ia != grafo[i].end(); ia++) { + if (!visitado[ia->first]) { + if (ant.first == ia->first && ant.second == ia->second) + continue; + else + cout << i << " " << ia->first << " " << ia->second << "\n"; + ant = mp(ia->first, ia->second); + } + } + visitado[i] = 1; + grafo[i].clear(); + } + if (n > 0) + cout << "\n"; + } + return 0; +} diff --git a/spoj_cpp_clean/RANKLIST/RANKLIST-5.cpp b/spoj_cpp_clean/RANKLIST/RANKLIST-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89f3833f8d67bd3b75a0951c8aec27c8ace71573 --- /dev/null +++ b/spoj_cpp_clean/RANKLIST/RANKLIST-5.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + long long test,n,sum,nth,pSUM; + cin>>test; + while(test--) + { + cin>>n>>sum; + nth=(1+(n-1)); + pSUM=(((1+nth)*n)/2); + if(sum==pSUM) + cout<<"0"< +#define sizee 100001 +using namespace std; + +int main() +{ + long long test,i,n,sum,nth,pSUM,temp,tempn; + cin>>test; + while(test--) + { + cin>>n>>sum; + i=0; + nth=1+(n-1); + pSUM=((1+nth)*n)/2; + if(pSUM==sum) + cout<=sum) + { + tempn--; + nth=1+(tempn-1); + temp=((1+nth)*tempn)/2; + } + cout< +using namespace std; + +#define ll long long +#define pb push_back +ll dp[2][2][12][65][65]; +vector num; + +ll solve(ll isStart, ll isSmall, ll pos, ll odd, ll even) + +{ + if(pos == 0) { + if(even - odd == 1) + return 1; + else + return 0; + } + ll &ret = dp[isStart][isSmall][pos][odd][even]; + if(ret != -1 && isSmall) + return ret; + + ll lim, pos2 = num.size() - pos; + if(isSmall) + lim = 9; + else + lim = num[pos2]; + + ll rt = 0; + if(!isStart) { + for(ll i = 0; i <= lim; i++) + rt += solve(0, isSmall | i < num[pos2], pos - 1, (pos & 1) ? odd + i: odd, !(pos & 1) ? even + i: even); + } + else { + for(ll i = 1; i <= lim; i++) + rt += solve(0, isSmall | i < num[pos2], pos - 1, (pos & 1) ? odd + i: odd, !(pos & 1) ? even + i: even); + + if(pos > 1) + rt += solve(1, 1, pos - 1, 0, 0); + } + + return ret = rt; +} + +ll calc(ll n) + +{ + if(n < 10) + return 0; + + ll tmp = n; + num.clear(); + while(tmp) { + num.pb(tmp % 10); + tmp /= 10; + } + reverse(num.begin(), num.end()); + + return solve(1, 0, num.size(), 0, 0); +} + +int main() + +{ + ll t, caseno = 0; + memset(dp, -1, sizeof(dp)); + cin >> t; + while(t--) { + ll l, r; + scanf("%lld %lld", &l, &r); + + ll ans = calc(r); + + ans -= calc(l - 1); + + printf("%lld\n", ans); + } + + return 0; diff --git a/spoj_cpp_clean/RAONE/RAONE-3.cpp b/spoj_cpp_clean/RAONE/RAONE-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f04718e2f37ccf6688843e5f3eee9c955b8827a5 --- /dev/null +++ b/spoj_cpp_clean/RAONE/RAONE-3.cpp @@ -0,0 +1,105 @@ +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "< digits(int x){ + vector res; + while(x){ + res.pb(x%10); + x /= 10; + } + return res; +} + +int get(int x){ + if(x <= 0) return 0; + + vector v = digits(x); + reverse(v.begin(), v.end()); + + int n = v.size(); + + memset(dp, 0, sizeof dp); + + int off = 90; + + dp[0][off][1] = 1; + for(int i = 0; i < n; i++){ + for(int sum = -90; sum <= 90; sum++){ + for(int eq = 0; eq < 2; eq++){ + int val = dp[i][off+sum][eq]; + if(!val) continue; + + int mnd = (eq)? v[i] : 9; + + for(int nd = 0; nd <= mnd; nd++){ + int pos = (n-i); + + int nsum = (pos&1)? sum - nd : sum + nd; + int neq = (eq and nd == v[i]); + + dp[i+1][off+nsum][neq] += val; + } + } + } + } + + int res = dp[n][off+1][0] + dp[n][off+1][1]; + return res; + +} + +void solve(){ + int f, t; + cin >> f >> t; + int ans; + if(f > t) ans = 0; + else ans = get(t) - get(f-1); + cout << ans << '\n'; +} + +int main(){ _ + int t; + cin >> t; + while(t--) solve(); + return 0; +} diff --git a/spoj_cpp_clean/RAONE/RAONE-8.cpp b/spoj_cpp_clean/RAONE/RAONE-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..121eb72dd230011f1072f1adb4f025d5094ce85f --- /dev/null +++ b/spoj_cpp_clean/RAONE/RAONE-8.cpp @@ -0,0 +1,163 @@ +/*************************************************** + * Problem Name : RAONE - Ra-One Numbers.cpp + * Problem Link : https://www.spoj.com/problems/RAONE/en/ + * OJ : Spoj + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : DigitDP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorA, B; +int num; +ll dp[10][50][50][2][2]; +ll digitDP (int pos, int odd, int even, int isChoto, int isBoro) { + if (pos == num) { + int x; + + if (pos % 2 == 1) { + x = even - odd; + + } else { + x = odd - even; + } + + if (x <= 0) { + return 0LL; + } + + if (x == 1) { + return 1LL; + + } else { + return 0LL; + } + } + + ll &ans = dp[pos][odd][even][isChoto][isBoro]; + + if (ans != -1) return ans; + + int low = 0, high = 9; + + if (isChoto == 0) { + high = B[pos]; + } + + if (isBoro == 0) { + low = A[pos]; + } + + ans = 0; + + for (int i = low; i <= high; i++) { + if (pos % 2 == 0) { + ans += digitDP (pos + 1, odd + i, even, isChoto | (i < high), + isBoro | (i > low) ); + + } else { + ans += digitDP (pos + 1, odd, even + i, isChoto | (i < high), + isBoro | (i > low) ); + } + } + + return ans; +} + +ll solve (ll a, ll b) { + A.clear(), B.clear(); + + while (b > 0) { + int rem = b % 10; + B.pb (rem); + b /= 10; + } + + reverse (all (B) ); + + while (a > 0) { + int rem = a % 10; + A.pb (rem); + a /= 10; + } + + num = (int) B.size(); + + while ( (int) A.size() < num) { + A.pb (0); + } + + reverse (all (A) ); + SET (dp, -1); + ll ans = digitDP (0, 0, 0, 0, 0); + return ans; +} +int main () { + __FastIO; + int tc; + cin >> tc; + + while (tc--) { + int a, b; + cin >> a >> b; + ll ans = solve (a, b); + cout << ans << "\n"; + } + + return 0; +} + diff --git a/spoj_cpp_clean/RATING/RATING-14.cpp b/spoj_cpp_clean/RATING/RATING-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9bace35f6a273a9a9d32516ab10167cf60c57415 --- /dev/null +++ b/spoj_cpp_clean/RATING/RATING-14.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; + +#define ll long long + +ll ans[300009], tree[400009]; + +struct node { + ll a, h, id; +} ara[300009]; + +bool cmp(node a, node b) + +{ + if(a.a != b.a) + return a.a < b.a; + return a.h < b.h; +} + +void update(ll lo, ll hi, ll indx, ll node) + +{ + if(lo > indx || hi < indx) + return; + if(lo == hi) { + tree[node]++; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, indx, 2 * node); + update(mid + 1, hi, indx, 2 * node + 1); + + tree[node] = tree[ 2 * node] + tree[ 2 * node + 1]; +} + +ll query(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(left > right) + return 0; + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) + return tree[node]; + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, 2 * node); + ll p2 = query(mid + 1, hi, left, right, 2 * node + 1); + + return p1 + p2; +} + +int main() + +{ + ll n; + cin >> n; + for(ll i = 1; i <= n; i++) { + scanf("%lld %lld", &ara[i].a, &ara[i].h); + ara[i].id = i; + } + + sort(ara + 1, ara + n + 1, cmp); + + for(ll i = 1; i <= n; i++) { + + ll ret = query(1, 100000, 1, ara[i].h, 1); + ll rata = ara[i].a, rath = ara[i].h; + ll j = i; + + while(ara[j].a == rata && ara[j].h == rath && j <= n) { + ans[ ara[j].id ] = ret; + update(1, 100000, ara[i].h, 1); + j++; + } + i = j - 1; + } + + for(ll i = 1; i <= n; i++) + printf("%lld\n", ans[i]); + + return 0; +} diff --git a/spoj_cpp_clean/RECUPERA/RECUPERA-10.cpp b/spoj_cpp_clean/RECUPERA/RECUPERA-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8164483de27b77af74fb93c73bac2f699aaf8c39 --- /dev/null +++ b/spoj_cpp_clean/RECUPERA/RECUPERA-10.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +int main() +{ + int inst = 0, n; + while(cin >> n) + { + int s = 0; + bool p = false; + cout << "Instancia " << ++inst << endl; + for(int i=0; i> v; + if(!p) + { + p = v == s; + if(p) + cout << v; + s += v; + } + } + if(!p) cout << "nao achei"; + cout << endl << endl; + } +} diff --git a/spoj_cpp_clean/REDSUBLIA/REDSUBLIA-9.cpp b/spoj_cpp_clean/REDSUBLIA/REDSUBLIA-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a51c8d436861e03bfdec68fc78ebeb1e0b76ba4 --- /dev/null +++ b/spoj_cpp_clean/REDSUBLIA/REDSUBLIA-9.cpp @@ -0,0 +1,188 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar_unlocked() +using namespace std; + +bool memo[26][26][26][26][26][26]; + +inline void getstring(string &A) +{ + char c = gc(); + + A.clear(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +inline char nextchar() +{ + char c = gc(); + + while (isspace(c)) + c = gc(); + + return c; +} + +inline void insert(string &A) +{ + memo[A[0] - 'a'][A[1] - 'a'][A[2] - 'a'][A[3] - 'a'][A[4] - 'a'][A[5] - 'a'] = 1; +} + +inline void query(string &A) +{ + int i; + + for (i = 'a'; i < A[0]; i++) + if (memo[i - 'a'][A[1] - 'a'][A[2] - 'a'][A[3] - 'a'][A[4] - 'a'][A[5] - 'a']) + { + A[0] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = 'a'; i < A[1]; i++) + if (memo[A[0] - 'a'][i - 'a'][A[2] - 'a'][A[3] - 'a'][A[4] - 'a'][A[5] - 'a']) + { + A[1] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = 'a'; i < A[2]; i++) + if (memo[A[0] - 'a'][A[1] - 'a'][i - 'a'][A[3] - 'a'][A[4] - 'a'][A[5] - 'a']) + { + A[2] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = 'a'; i < A[3]; i++) + if (memo[A[0] - 'a'][A[1] - 'a'][A[2] - 'a'][i - 'a'][A[4] - 'a'][A[5] - 'a']) + { + A[3] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = 'a'; i < A[4]; i++) + if (memo[A[0] - 'a'][A[1] - 'a'][A[2] - 'a'][A[3] - 'a'][i - 'a'][A[5] - 'a']) + { + A[4] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = 'a'; i < A[5]; i++) + if (memo[A[0] - 'a'][A[1] - 'a'][A[2] - 'a'][A[3] - 'a'][A[4] - 'a'][i - 'a']) + { + A[5] = i; + printf("%s\n", A.c_str()); + return; + } + + //bigger + + for (i = A[5] + 1; i <= 'z'; i++) + if (memo[A[0] - 'a'][A[1] - 'a'][A[2] - 'a'][A[3] - 'a'][A[4] - 'a'][i - 'a']) + { + A[5] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = A[4] + 1; i <= 'z'; i++) + if (memo[A[0] - 'a'][A[1] - 'a'][A[2] - 'a'][A[3] - 'a'][i - 'a'][A[5] - 'a']) + { + A[4] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = A[3] + 1; i <= 'z'; i++) + if (memo[A[0] - 'a'][A[1] - 'a'][A[2] - 'a'][i - 'a'][A[4] - 'a'][A[5] - 'a']) + { + A[3] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = A[2] + 1; i <= 'z'; i++) + if (memo[A[0] - 'a'][A[1] - 'a'][i - 'a'][A[3] - 'a'][A[4] - 'a'][A[5] - 'a']) + { + A[2] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = A[1] + 1; i <= 'z'; i++) + if (memo[A[0] - 'a'][i - 'a'][A[2] - 'a'][A[3] - 'a'][A[4] - 'a'][A[5] - 'a']) + { + A[1] = i; + printf("%s\n", A.c_str()); + return; + } + + for (i = A[0] + 1; i <= 'z'; i++) + if (memo[i - 'a'][A[1] - 'a'][A[2] - 'a'][A[3] - 'a'][A[4] - 'a'][A[5] - 'a']) + { + A[0] = i; + printf("%s\n", A.c_str()); + return; + } + + printf("0\n"); +} + +int main() +{ + int T, Q, t; + string A; + char type; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + for (t = 1; t <= T; t++) + { + scanf("%d", &Q); + + memset(memo, 0, sizeof(memo)); + printf("Test %d\n", t); + + while (Q--) + { + type = nextchar(); + getstring(A); + + if (type == 'W') + insert(A); + else + query(A); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/RENT/RENT-14.cpp b/spoj_cpp_clean/RENT/RENT-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5b381e3f5a68c39fd32393ddafe457d46a6f5ac --- /dev/null +++ b/spoj_cpp_clean/RENT/RENT-14.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +#define ll long long + +ll n; + +struct node{ + ll l, r, c; +} ara[10009]; + +ll dp[10009], b[10009]; + +bool cmp(node a, node b) + +{ + return a.l < b.l; +} + +ll solve(ll indx) + +{ + if(indx > n) + return 0; + if(dp[indx] != -1) + return dp[indx]; + + ll pos = upper_bound(b + 1, b + n + 1, ara[indx].r) - b; + if(pos > 1) { + if(b[pos - 1] == ara[indx].r) + pos--; + } + + return dp[indx] = max(solve(indx + 1), solve(pos) + ara[indx].c); +} + +int main() + +{ + ll t; + cin >> t; + while(t--) { + memset(dp, -1, sizeof(dp)); + scanf("%lld", &n); + for(ll i = 1; i <= n; i++) { + scanf("%lld %lld %lld", &ara[i].l, &ara[i].r, &ara[i].c); + ara[i].r += ara[i].l; + b[i] = ara[i].l; + } + + sort(ara + 1, ara + n + 1, cmp); + sort(b + 1, b + n + 1); + + printf("%lld\n", solve(1)); + } + + return 0; +} diff --git a/spoj_cpp_clean/RIVALS/RIVALS-3.cpp b/spoj_cpp_clean/RIVALS/RIVALS-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fcb761452226d73366cd4b4a359c06a7036bfbb --- /dev/null +++ b/spoj_cpp_clean/RIVALS/RIVALS-3.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; +int t; +long long dp[2000001], MOD = 1000000007, result; +int x, y; + +long long inv(long long p){ + long long ans = 1, n = 1000000005; + while(n){ + if(n&1) ans = (ans*p)%MOD; + p = (p*p)%MOD; + n >>= 1; + } + return ans; +} + +int main(){ + dp[0] = 1; + for(int i = 1; i <= 2000000; i++) + dp[i] = (dp[i-1]*i) % MOD; + sd(t); + while(t--){ + scanf("%lld %lld", &x, &y); + result = (inv(dp[x])*inv(dp[y]))%MOD; + result = (result*dp[x+y])%MOD; + printf("%lld\n", result); + } + return 0; +} + + diff --git a/spoj_cpp_clean/RMQSQ/RMQSQ-8.cpp b/spoj_cpp_clean/RMQSQ/RMQSQ-8.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d7dfe855adbbd19a7a098ec3a9dd5ec499124d09 --- /dev/null +++ b/spoj_cpp_clean/RMQSQ/RMQSQ-8.cpp @@ -0,0 +1,64 @@ +/*************************************************** + * Problem name : RMQSQ - Range Minimum Query.cpp + * OJ : Spoj + * Result : AC + * Date : 24-03-17 + * Problem Type : SparseTable + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 100005 +#define SET(a) memset(a,-1,sizeof(a)) +using namespace std; +typedef long long ll; +ll ar[MAX]; +ll st[MAX][16]; +void build_st(ll n) { + for (int i = 0; i < n; i++) st[i][0] = i; + for (int j = 1; j <= 1< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +int G[207][207]; +int N; + +void init() +{ + int i, j; + + scanf("%d", &N); + + for(i=1; i<=N; i++) + for(j=1; j<=N; j++) + scanf("%d", &G[i][j]); +} + +inline int check(int A, int B) +{ + int i; + + for(i=1; i<=N; i++) + if(i!=A && i!=B && G[A][i] + G[i][B] == G[A][B]) + return 0; + + return 1; +} + +int main() +{ + int T; + int i, j; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + { + init(); + + for(i=1; i<=N; i++) + for(j = i+1; j<=N; j++) + if(check(i, j)) + printf("%d %d\n", i, j); + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/ROADS/ROADS-13.cpp b/spoj_cpp_clean/ROADS/ROADS-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f403786493d44e488d35b488ec8088799dc4671 --- /dev/null +++ b/spoj_cpp_clean/ROADS/ROADS-13.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +const int MAX_N = 101; +const int INF = 0x3f3f3f3f; + +struct edge{ + int s, d, cost, fee; + edge(int _s, int _d, int _cost, int _fee): + s(_s), d(_d),cost(_cost), fee(_fee){} +}; + +struct graph{ + vector v[MAX_N]; + void addEdge(int s, int d, int cost, int fee){ + v[s].push_back(edge(s,d,cost,fee)); + } +}; + +struct state_t{ + int k, w,cost; + state_t(int _k, int _w, int _cost): + k(_k), w(_w),cost(_cost){} + bool operator <(const state_t &x)const{ + if(x.cost < cost)return true; + else if(cost < x.cost)return false; + else if(x.w > w)return true; + else return false; + } +}; +int mincost[MAX_N][10001]; +void addState(priority_queue &pq, state_t &st, edge &e){ + if(st.w >= e.fee && mincost[e.d][st.w - e.fee] >= st.cost + e.cost){ + mincost[e.d][st.w - e.fee] = st.cost+e.cost; + pq.push(state_t(e.d,st.w-e.fee,st.cost+e.cost)); + } +} + +int dijkstra(graph &g, int src, int dest, int money){ + memset(mincost,INF, sizeof(mincost)); + priority_queue pq; + pq.push(state_t(0,money,0)); + while(!pq.empty()){ + state_t st = pq.top(); pq.pop(); + if(st.k == dest)return st.cost; + if(mincost[st.k][st.w] < st.cost)continue; + for(int i=0, n=g.v[st.k].size(); i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int t, n, r, k, a, b, indegree[1000], processed[1000], cur, i, j; +vector adj_list[1000], current, next; + +int main(){ + sd(t); + for(int T = 1; T <= t; T++){ + sd2(n,r); + for(i = 0; i <= n; i++){ + adj_list[i].clear(); + processed[i] = 0; + } + while(r--){ + sd2(a,b); + adj_list[b].push_back(a); + indegree[a]++; + } + for(i = 0; i < n; i++){ + if(indegree[i] == 0){ + current.push_back(i); + } + } + cur = 1; + printf("Scenario #%d:\n", T); + while(!current.empty()){ + sort(current.begin(), current.end()); + next.clear(); + for(i = 0; i < current.size(); i++){ + printf("%d %d\n",cur, current[i]); + processed[current[i]] = 1; + for(j = 0; j < adj_list[current[i]].size(); j++){ + if(processed[adj_list[current[i]][j]] == 0){ + indegree[adj_list[current[i]][j]]--; + if(indegree[adj_list[current[i]][j]] == 0) next.push_back(adj_list[current[i]][j]); + } + } + } + cur++; + current = next; + } + } + return 0; +} diff --git a/spoj_cpp_clean/RPLB/RPLB-21.cpp b/spoj_cpp_clean/RPLB/RPLB-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a8dcf79bd49d59e70803b3e1d1fdd0808de474e --- /dev/null +++ b/spoj_cpp_clean/RPLB/RPLB-21.cpp @@ -0,0 +1,35 @@ +/* + dynamic programming + difficulty: easy + date: 17/Oct/2020 + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; +const int INF = 1 << 30; + +vector A; int N; + +vector> memo; +int dp(int i, int k) { + if (k < 0) return -INF; + if (i >= N) return 0; + + int &ans = memo[i][k]; + if (ans != -1) return ans; + + return ans = max(dp(i+1, k), A[i] + dp(i+2, k-A[i])); +} + +int main() { + int T, t = 1; cin >> T; + while (T--) { + int K; cin >> N >> K; + A.resize(N); for (int &a : A) cin >> a; + + memo.assign(N, vector(K+1, -1)); + cout << "Scenario #" << (t++) << ": " << dp(0, K) << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/RPLB/RPLB-7.cpp b/spoj_cpp_clean/RPLB/RPLB-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14404876146e6bb345cb165ca9a437ec708404ae --- /dev/null +++ b/spoj_cpp_clean/RPLB/RPLB-7.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; +int t,n,k; + +int arr[1001]; +int memo[1001][1001]; + +int max(int x, int y) +{ + return (x>y)?x:y; +} + +int mochila(int i,int w) +{ + if(w>k) return -20000; + if(i>=n) return 0; + + int &mejor= memo[i][w]; + if(mejor!=-1) return mejor; + + mejor= max( mochila(i+1,w), mochila(i+2,w+arr[i]) + arr[i]); + + return mejor; +} + + +int main() +{ + scanf("%d",&t); + for (int tc = 1; tc <= t; ++tc) + { + memset(memo,-1,sizeof(memo)); + scanf("%d %d",&n,&k); + for (int i = 0; i < n; ++i) + { + scanf("%d",&arr[i]); + } + + printf("Scenario #%d: %d\n",tc,mochila(0,0) ); + + } + + return 0; +} diff --git a/spoj_cpp_clean/RPLC/RPLC-7.cpp b/spoj_cpp_clean/RPLC/RPLC-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0288c001931d2654f06a21a6f0e9f6661930261 --- /dev/null +++ b/spoj_cpp_clean/RPLC/RPLC-7.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + long long int t, n, aux,llevo,min; + + + scanf("%lld",&t); + for (int j = 1; j <= t; ++j) + { + scanf("%lld",&n); + llevo=0; + min= 20000000; + for (int i = 0; i < n; ++i) + { + scanf("%lld",&aux); + llevo+=aux; + if (llevo0)?printf("Scenario #%d: %d\n",j,1): printf("Scenario #%d: %lld\n",j,abs(min)+1); + } + return 0; +} diff --git a/spoj_cpp_clean/RPLD/RPLD-3.cpp b/spoj_cpp_clean/RPLD/RPLD-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5294ff56dbcc212166effb5b0c6379526e340dd --- /dev/null +++ b/spoj_cpp_clean/RPLD/RPLD-3.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, n, r, id, sub; + set stud[10001]; + sd(t); + while(t--){ + for(int i = 0; i <= 10000; i++){ + stud[i].clear(); + } + sd2(n,r); + int flag = 0; + for(int i = 0; i < r; i++){ + sd2(id,sub); + if(flag == 0) + if(stud[id].find(sub) == stud[id].end()){ + stud[id].insert(sub); + } + else{ + flag = 1; + } + } + if(flag == 0) + printf("possible\n"); + else + printf("impossible\n"); + + } + return 0; +} diff --git a/spoj_cpp_clean/RPLE/RPLE-7.cpp b/spoj_cpp_clean/RPLE/RPLE-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..301dd8c8281b6239befa343b68da88b250e20aa0 --- /dev/null +++ b/spoj_cpp_clean/RPLE/RPLE-7.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int main() +{ + int t,n,m,a,b; + bool spies[1100],people[1100]; + + scanf("%d",&t); + for (int i = 1; i <= t; ++i) + { + scanf("%d %d",&n,&m); + memset(spies,0,sizeof(spies)); + memset(people,0,sizeof(people)); + for (int j = 0; j < m; ++j) + { + scanf("%d %d",&a,&b); + spies[a]=true; + people[b]=true; + } + + bool res= false; + for (int k = 0; k < n; ++k) + { + if (spies[k] && people[k]) + { + res= true; + break; + } + } + + (res)?printf("Scenario #%d: spied\n",i ):printf("Scenario #%d: spying\n",i ); + } + return 0; +} diff --git a/spoj_cpp_clean/RPLE/RPLE-9.cpp b/spoj_cpp_clean/RPLE/RPLE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc84f0e7da8ca80a3ffc28b72c8054bc0fa12813 --- /dev/null +++ b/spoj_cpp_clean/RPLE/RPLE-9.cpp @@ -0,0 +1,60 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +bool IN[1007], OUT[1007]; +int N, E; + +inline bool solve() +{ + bool ans = 1; + + memset(IN, 0, sizeof(IN)); + memset(OUT, 0, sizeof(OUT)); + + int i, f, s; + + scanf("%d %d", &N, &E); + + for (i = 0; i < E; i++) + { + scanf("%d %d", &f, &s); + OUT[f] = 1; + IN[s] = 1; + + if ((OUT[f] && IN[f]) || (OUT[s] && IN[s])) + ans = 0; + } + + return ans; +} + +int main() +{ + int T, i; + +// freopen("input.txt", "r", stdin); + scanf("%d", &T); + + for (i = 1; i <= T; i++) + { + if (solve()) + printf("Scenario #%d: spying\n", i); + else + printf("Scenario #%d: spied\n", i); + } + + return 0; +} diff --git a/spoj_cpp_clean/RPLN/RPLN-9.cpp b/spoj_cpp_clean/RPLN/RPLN-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6079e59d4f329d619c9cb7c35762793af6c487f7 --- /dev/null +++ b/spoj_cpp_clean/RPLN/RPLN-9.cpp @@ -0,0 +1,80 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int A[100007]; +int tree[100007*4]; +int N, Q; + +void build(int node, int a, int b) +{ + if(a==b) + { + tree[node]=A[a]; + return; + } + + build(node*2, a, (a+b)/2); + build(node*2+1, (a+b)/2+1, b); + tree[node]=min(tree[node*2], tree[node*2+1]); +} + +int query(int node, int a, int b, int i, int j) +{ + if(a==i && b==j) + return tree[node]; + + if(j<=(a+b)/2) + return query(node*2, a, (a+b)/2, i, j); + if(i>(a+b)/2) + return query(node*2+1, (a+b)/2+1, b, i, j); + + return min(query(node*2, a, (a+b)/2, i, (a+b)/2), query(node*2+1, (a+b)/2+1, b, (a+b)/2+1, j)); +} + +inline void init() +{ + int i; + + scanf("%d %d", &N, &Q); + + for(i=1; i<=N; i++) + scanf("%d", &A[i]); + + build(1, 1, N); +} + +int main() +{ + int T, i, j, f, s; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + for(i=1; i<=T; i++) + { + init(); + printf("Scenario #%d:\n\n", i); + + for(j=0; j Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define ll long long +using namespace std; + +ll ans; +int N; + +inline ll solve() +{ + ll curr = 0, num; + + cin >> N; + ans = LLONG_MAX; + + while (N--) + { + cin >> num; + curr += num; + ans = min(ans, curr); + } + + if (ans <= 0) + return 1LL - ans; + else + return 0; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + int T; + + cin >> T; + + while (T--) + cout << solve() << "\n"; + + return 0; +} diff --git a/spoj_cpp_clean/SAMER08F/SAMER08F-11.cpp b/spoj_cpp_clean/SAMER08F/SAMER08F-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..697c8dd732b31403ce17b13c92177b6a02d2a376 --- /dev/null +++ b/spoj_cpp_clean/SAMER08F/SAMER08F-11.cpp @@ -0,0 +1,25 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include + +using namespace std; + +int main(void) +{ + int n; + + cin >> n; + + while(n) + { + cout << ((n * (n+1) * (2*n+1)) / 6) << endl; + + cin >> n; + } + + return 0; + +} \ No newline at end of file diff --git a/spoj_cpp_clean/SAMER08F/SAMER08F-16.cpp b/spoj_cpp_clean/SAMER08F/SAMER08F-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2ebf70f12966c241c301d9c05c2acc43c64263c --- /dev/null +++ b/spoj_cpp_clean/SAMER08F/SAMER08F-16.cpp @@ -0,0 +1,22 @@ +#include +#include +#define ll long long +#define m 100000007 +using namespace std; + +int RPF(int n){ + /*int sum=0,i; + for(i=1;i<=n;i++) + sum+=i*i; + return sum;*/ + return n*(n+1)*(2*n+1)/6; +} + +int main() { + int n; + cin>>n; + do{ + cout<>n; + }while(n!=0); + return 0; diff --git a/spoj_cpp_clean/SAMER08F/SAMER08F-5.cpp b/spoj_cpp_clean/SAMER08F/SAMER08F-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26481cd11eccca5b29be18253eb63a2395c4d3ec --- /dev/null +++ b/spoj_cpp_clean/SAMER08F/SAMER08F-5.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int sqr(int n) +{ + if(n==1) + return 1; + else + return (n*n+sqr(n-1)); +} +int main() +{ + int n; + cin>>n; + while(n) + { + cout<>n; + } + return 0; +} diff --git a/spoj_cpp_clean/SAMER08F/SAMER08F-9.cpp b/spoj_cpp_clean/SAMER08F/SAMER08F-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05a0a56e534a75ff76fa234948722c9e3dd2222f --- /dev/null +++ b/spoj_cpp_clean/SAMER08F/SAMER08F-9.cpp @@ -0,0 +1,39 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +inline long long solve(int num) +{ + long long ans = 0; + int i; + + for(i=1; i<=num; i++) + ans += pow( (float)(num - i) + 1, 2); + + return ans; +} + +int main() +{ + int N; + + scanf("%d", &N); + + while(N!=0) + { + printf("%lld\n", solve(N)); + scanf("%d", &N); + } + + return 0; +} diff --git a/spoj_cpp_clean/SAMER08G/SAMER08G-12.cpp b/spoj_cpp_clean/SAMER08G/SAMER08G-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..407c4f39774ac561e0758aadf44e22fd132cce26 --- /dev/null +++ b/spoj_cpp_clean/SAMER08G/SAMER08G-12.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define mp make_pair +#define pb push_back +#define MAX 1010 + +using namespace std; + +typedef pair ii; +typedef vector vi; +typedef unsigned long long int64; +vector> race(1010); + +int n, c, pos; + +const int INF = 0x3f3f3f3f; + +int main() +{ + ios::sync_with_stdio(false); + bool flag = false; + while (cin >> n) { + if (flag) + for (int i = 0; i < n; ++i) + race[i].clear(); + bool fail = false; + for (int i = 0; i < n; ++i) { + cin >> c >> pos; + if (pos < 0) { + if (i + pos >= 0) { + race[i + pos].pb(c); + if (race[i + pos].size() > 1) + fail = true; + } else + fail = true; + } else if (pos >= 0) { + if (i + pos < n) { + race[i + pos].pb(c); + if (race[i + pos].size() > 1) + fail = true; + } else + fail = true; + } + } + if (fail) + cout << "-1\n"; + else { + for (int i = 0; i < n; ++i) + cout << *(race[i].begin()) << " "; + cout << "\n"; + } + flag = true; + } + return 0; +} diff --git a/spoj_cpp_clean/SAMER08G/SAMER08G-3.cpp b/spoj_cpp_clean/SAMER08G/SAMER08G-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d45811d71a9c0b22491bf8c299a6a4e557d89c8 --- /dev/null +++ b/spoj_cpp_clean/SAMER08G/SAMER08G-3.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int n, pos[1000], c, p, flag; + sd(n); + while(n){ + for(int i = 0; i < n; i++) + pos[i] = 0; + flag = 0; + for(int i = 0; i < n; i++){ + sd2(c,p); + if(i+ p < n and i + p >=0){ + if(pos[i+p] == 0) + pos[i+p] = c; + else + flag = 1; + } + else + flag = 1; + } + if(flag) + printf("-1\n"); + else{ + for(int i = 0; i < n; i++) + printf("%d ", pos[i]); + printf("\n"); + } + sd(n); + } + return 0; +} diff --git a/spoj_cpp_clean/SARRAY/SARRAY-21.cpp b/spoj_cpp_clean/SARRAY/SARRAY-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94132ee408fff1004bbf8d257ddb776e3cb8b752 --- /dev/null +++ b/spoj_cpp_clean/SARRAY/SARRAY-21.cpp @@ -0,0 +1,66 @@ +/* + strings > suffix array + difficulty: medium + date: 26/Aug/2020 + problem: just build a suffix array in O(N * log N) + by: @brpapa +*/ +#include +using namespace std; +typedef pair ii; + +#define MAX_N 100010 +char T[MAX_N]; +int N; +int ra[MAX_N], temp_ra[MAX_N]; +int sa[MAX_N], temp_sa[MAX_N]; +int c[MAX_N]; + +/* O(N) */ +void counting_sort(int k) { + int i, sum, maxi = max(300, N); + memset(c, 0, sizeof c); + for (i = 0; i < N; i++) + c[i + k < N ? ra[i + k] : 0]++; + for (i = sum = 0; i < maxi; i++) { + int t = c[i]; + c[i] = sum; + sum += t; + } + for (i = 0; i < N; i++) + temp_sa[c[sa[i] + k < N ? ra[sa[i] + k] : 0]++] = sa[i]; + for (i = 0; i < N; i++) + sa[i] = temp_sa[i]; +} + +/* O(N * log N) */ +void build_sa() { + int i, k, r; + for (i = 0; i < N; i++) + ra[i] = T[i]; + for (i = 0; i < N; i++) + sa[i] = i; + for (k = 1; k < N; k <<= 1) { + counting_sort(k); + counting_sort(0); + temp_ra[sa[0]] = r = 0; + for (i = 1; i < N; i++) + temp_ra[sa[i]] = + (ra[sa[i]] == ra[sa[i - 1]] && ra[sa[i] + k] == ra[sa[i - 1] + k]) ? r : ++r; + for (i = 0; i < N; i++) + ra[i] = temp_ra[i]; + if (ra[sa[N - 1]] == N - 1) + break; + } +} + +int main() { + scanf("%100000s", T); + N = (int)strlen(T); + T[N++] = '$'; + + build_sa(); + for (int i = 1; i < N; i++) + printf("%d\n", sa[i]); + return 0; +} diff --git a/spoj_cpp_clean/SAS001/SAS001-14.cpp b/spoj_cpp_clean/SAS001/SAS001-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d79bfd9d158a8d3796800d8556b629c25c84ff2 --- /dev/null +++ b/spoj_cpp_clean/SAS001/SAS001-14.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; + +#define ll long long + +ll tree[4000009], ara[1000009]; + +void build(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + tree[node] = ara[lo]; + return; + } + + ll mid = (lo + hi) / 2; + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + + tree[node] = tree[2 * node] + tree[2 * node + 1]; +} + +void update(ll lo, ll hi, ll indx, ll val, ll node) + +{ + if(lo > indx || hi < indx) + return; + if(lo == hi) { + tree[node] += val; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, indx, val, 2 * node); + update(mid + 1, hi, indx, val, 2 * node + 1); + + tree[node] = tree[2 * node] + tree[2 * node + 1]; +} + +ll query(ll lo, ll hi, ll left, ll right, ll node) + +{ + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) + return tree[node]; + + ll mid = (lo + hi) / 2; + ll p1 = query(lo, mid, left, right, 2 * node); + ll p2 = query(mid + 1, hi, left, right, 2 * node + 1); + + return p1 + p2; +} + +int main() + +{ + ll n, l, r; + char ch; + cin >> n; + for(ll i = 1; i <= n; i++) + scanf("%lld", &ara[i]); + + build(1, n, 1); + ll q; + cin >> q; + while(q--) { + scanf(" %c", &ch); + scanf("%lld %lld", &l, &r); + if(ch == 'q') { + printf("%lld\n", query(1, n, l, r, 1)); + } + else + update(1, n, l, r, 1); + } + return 0; +} diff --git a/spoj_cpp_clean/SBANK/SBANK-21.cpp b/spoj_cpp_clean/SBANK/SBANK-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39fc4fa9f07eb78d974304f0d825d6695d5dc8dd --- /dev/null +++ b/spoj_cpp_clean/SBANK/SBANK-21.cpp @@ -0,0 +1,31 @@ +/* + ad-hoc + difficulty: easy + date: 02/Mar/2020 + by: @brpapa +*/ +#include +#include +using namespace std; + +int main() { + int T; cin >> T; + while (T--) { + int N; cin >> N >> ws; + + map m; + while (N--) { + string str; getline(cin, str); + if (str.size() == 32) str.erase(str.end()-1); + + if (m[str]) m[str]++; + else m[str] = 1; + } + + for (auto i : m) { + cout << i.first << " " << i.second << endl; + } + if (T > 0) cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/SBANK/SBANK-4.cpp b/spoj_cpp_clean/SBANK/SBANK-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8cc9ff1ad3b38304dd624e13a720405bbf4e8243 --- /dev/null +++ b/spoj_cpp_clean/SBANK/SBANK-4.cpp @@ -0,0 +1,123 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +#include +unordered_map mp; +//vector vec; +set st; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T;string s; + s(T); + while(T--) + { + mp.clear(); vec.clear(); + s(n); + getchar(); + FOR(i,0,n) + { + getline(cin,s); + st.insert(s); + /* + if(mp[s]==0) + { + vec.push_back(s); + } + */ + mp[s]++; + } + //sort(vec.begin(), vec.end()); +for (it=st.begin(); it!=st.end(); ++it) + cout<< *it<<" "<0) + cout< + +using namespace std; + +int main() +{ + int tc=0, n=0; + string account,aux; + map mp; + map::iterator it; + + scanf("%d",&tc); + for (int i = 0; i < tc ; ++i) + { + mp.clear(); + scanf("%d\n",&n); + for (int i = 0; i < n; ++i) + { + getline(cin,account); + mp[account]++; + } + for ( it = mp.begin(); it != mp.end(); ++it) + { + printf("%s %d\n",(*it).first.c_str(),(*it).second ); + } + printf("\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/SBANK/SBANK-9.cpp b/spoj_cpp_clean/SBANK/SBANK-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a17435879c6ed24248af1a4a18b028a7600b916b --- /dev/null +++ b/spoj_cpp_clean/SBANK/SBANK-9.cpp @@ -0,0 +1,66 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +map all; + +int main() +{ + int t, i, N; + map::iterator ii; + char s[32]; + char c; + string str; + + //freopen("input.txt","r",stdin); + + scanf("%d", &t); + + while(t--) + { + all.clear(); + + scanf("%d", &N); + + c=fgetc(stdin); + while(!isdigit(c)) + c=fgetc(stdin); + ungetc(c, stdin); + + for(i=0; isecond++; + else + all[str]=1; + } + + for(ii=all.begin(); ii!=all.end(); ii++) + printf("%s %d\n", ii->first.c_str(), ii->second); + printf("\n"); + } + + + + return 0; +} diff --git a/spoj_cpp_clean/SBETS/SBETS-7.cpp b/spoj_cpp_clean/SBETS/SBETS-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb914e50267b93186f55cb02c6c2466b3cb5cd42 --- /dev/null +++ b/spoj_cpp_clean/SBETS/SBETS-7.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +int main() +{ + int tc=0,eqp1=0,eqp2=0; + char team1[4],team2[4]; + map mapa; + map::iterator it,resul; + + + scanf("%d",&tc); + for (int i = 0; i < tc; ++i) + { + mapa.clear(); + for (int i = 0; i < 16; ++i) + { + scanf("%s %s %d %d",team1,team2,&eqp1,&eqp2); + if (eqp1>eqp2) + { + mapa[team1]++; + }else + { + mapa[team2]++; + } + } + resul= mapa.begin(); + for ( it = mapa.begin(); it != mapa.end(); ++it) + { + if ((*it).second>(*resul).second) + { + resul=it; + } + } + + cout<<(*resul).first< + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(auto it=(v).begin(); it != (v).end(); ++it) + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__) + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void trace(const char* name, T&& arg1){cout< +void trace(const char* names, T&& arg1, Args&&... args){ +const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<= 0; i--){ + int x = n-l+i; + if(s[i] == '0'){ + dp[x][0] = dp[x+1][0] + dp[x+1][1]; + dp[x][1] = dp[x+1][1]; + } + else{ + dp[x][0] = dp[x+1][0]; + dp[x][1] = dp[x+1][0] + dp[x+1][1]; + } + + dp[x][0] %= d; + dp[x][1] %= d; + } + + for(int x = n-l-1; x >= 0; x--){ + dp[x][0] = dp[x+1][0] + dp[x+1][1]; + dp[x][1] = dp[x+1][1]; + + dp[x][0] %= d; + dp[x][1] %= d; + } + +// for(int i = 0; i < n; i++){ +// tr(i, dp[i][0], dp[i][1]); +// } + + printf("%d\n", dp[0][0]); + +} + +int main(){ + int t; + sd(t); + while(t--) solve(); + return 0; +} diff --git a/spoj_cpp_clean/SCAVHUNT/SCAVHUNT-9.cpp b/spoj_cpp_clean/SCAVHUNT/SCAVHUNT-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..160c1e13e8297041f826d4202a787c95be46eb23 --- /dev/null +++ b/spoj_cpp_clean/SCAVHUNT/SCAVHUNT-9.cpp @@ -0,0 +1,64 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +using namespace std; + +unordered_map mapn, mapr; +string start, finish; +string A, B; + +void init() +{ + int N, i; + + mapn.clear(); + mapr.clear(); + scanf("%d", &N); + + cin>>start>>finish; + mapn[start]=finish; + + for(i=0; i>A>>B; + mapn[A]=B; + mapr[B]=A; + } + + for(1; mapr.find(start)!=mapr.end(); start=mapr[start]); + for(1; mapn.find(finish)!=mapn.end(); finish=mapn[finish]); +} + +int main() +{ + int T, i; + string curr; + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + for(i=1; i<=T; i++) + { + init(); + + printf("Scenario #%d:\n", i); + + for(curr=start; curr!=finish; curr=mapn[curr]) + printf("%s\n", curr.c_str()); + printf("%s\n\n", curr.c_str()); + } + + return 0; +} diff --git a/spoj_cpp_clean/SCUBADIV/SCUBADIV-21.cpp b/spoj_cpp_clean/SCUBADIV/SCUBADIV-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddd7f13a12931bd511fdfcb877c3e55dfff6e8a7 --- /dev/null +++ b/spoj_cpp_clean/SCUBADIV/SCUBADIV-21.cpp @@ -0,0 +1,43 @@ +/* + dynamic programming > 0-1 knapsack + difficulty: easy + date: 29/Apr/2020 + by: @brpapa +*/ +#include +using namespace std; + +struct T { + int qtyOx, qtyNi, w; + T() {} +}; +vector A; int N; + +int memo[1010][22][80]; +int dp(int n, int ox, int ni) { + // cilindro atual A[n], demanda por uma qte ox e ni de oxigênio e nitrogênio + + if (ox <= 0 && ni <= 0) return 0; + if (n == N) return 1 << 30; + + int &ans = memo[n][max(ox, 0)][max(ni, 0)]; + if (ans != -1) return ans; + + return ans = min( + A[n].w + dp(n+1, ox-A[n].qtyOx, ni-A[n].qtyNi), + dp(n+1, ox, ni) + ); +} + +int main() { + int c; + cin >> c; + while (c--) { + int ox, ni; cin >> ox >> ni >> N; + A.resize(N); for (T &a : A) cin >> a.qtyOx >> a.qtyNi >> a.w; + + memset(memo, -1, sizeof memo); + cout << dp(0, ox, ni) << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/SEGSQRSS/SEGSQRSS-3.cpp b/spoj_cpp_clean/SEGSQRSS/SEGSQRSS-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11fce10f9452e9012c70ef97329ef478367941d1 --- /dev/null +++ b/spoj_cpp_clean/SEGSQRSS/SEGSQRSS-3.cpp @@ -0,0 +1,178 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) +#define fi first +#define se second +#define pb(x) push_back(x) +#define mp(x,y) make_pair(x,y) +#define LET(x, a) __typeof(a) x(a) +#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++) +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout); + +#define tr(x) cout<>1; + + build(left, l, mid); build(right, mid, r); + + tree[id].combine(tree[left], tree[right]); + return; +} + +// range update and utility functions +void upd(int id,int l,int r, long long x){ // update the current node and its index in the lazy array + lazy[id] += x; + tree[id].update(x, r-l); +} + +void shift(int id,int l,int r){ //propogate update information to the children + int mid = (l+r)/2; + + if(lazy2[id] > 0){ + tree[id*2].update2(lazy2[id], mid-l); + lazy[id*2] = 0; + tree[id*2 + 1].update2(lazy2[id], r-mid); + lazy[id*2 + 1] = 0; + } + + upd(id * 2, l, mid, lazy[id]); + upd(id * 2 + 1, mid, r, lazy[id]); + + lazy[id] = 0; // passing is done, reset the index in the lazy array + lazy2[id] = 0; +} + +// range update -> update(x, y, val); +void update(int x, int y, long long val, int id = 1, int l = 0, int r = n){ + if(x >= r or l >= y) return; + if(x <= l && r <= y){ + upd(id, l, r, val); + return; + } + + shift(id, l, r); // pass the updates to the children + + int left = id<<1, right = left+1, mid = (l+r)>>1; + + update(x, y, val, left, l, mid); + update(x, y, val, right, mid, r); + + tree[id].combine(tree[left], tree[right]); + return; +} + +void update2(int x, int y, long long val, int id = 1, int l = 0, int r = n){ + if(x >= r or l >= y) return; + if(x <= l && r <= y){ + lazy2[id] = x; + lazy[id] = 0; + tree[id].update2(val, r-l); + +// tr3(l, r, tree[id].s1); + + return; + } + + shift(id, l, r); // pass the updates to the children + + int left = id<<1, right = left+1, mid = (l+r)>>1; + + update2(x, y, val, left, l, mid); + update2(x, y, val, right, mid, r); + + tree[id].combine(tree[left], tree[right]); + return; +} + +// range query -> query(x, y); +long long query(int x, int y, int id = 1, int l = 0, int r = n){ + if(x >= r or l >= y) return 0; + if(x <= l && r <= y) return tree[id].s1; + + shift(id, l, r); //use this with lazy propogation + + int left = id<<1, right = left+1, mid = (l+r)>>1; + + return query(x, y, left, l, mid) + query(x, y, right, mid, r); +} + +int main(){ + sd(t); + for(int i = 1; i <= t; i++){ + printf("Case %d:\n",i); + sd2(n,q); + for(int i = 0; i < n; i++){ + sd(a[i]); + } + + build(); + int type, x, y, val; + while(q--){ + sd3(type, x, y); + if(type == 2){ + printf("%lld\n", query(x-1,y)); + } + else if(type == 1){ + sd(val); + update(x-1,y,val); + } + else{ + sd(val); + update2(x-1,y,val); + } + } + } + return 0; +} diff --git a/spoj_cpp_clean/SEGSQRSS/SEGSQRSS-4.cpp b/spoj_cpp_clean/SEGSQRSS/SEGSQRSS-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98d3eea58779fc6a2166157f36e22e6387fd6fc6 --- /dev/null +++ b/spoj_cpp_clean/SEGSQRSS/SEGSQRSS-4.cpp @@ -0,0 +1,106 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define all(c) (c).begin(),(c).end() +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; + +int solve() +{ + return 1; +} + +bool input() +{ + return true; +} + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + + int T=1; + //s(T); + for(int testnum=1;testnum<=T;testnum++) + { + if(!input()) break; + + solve(); + //printf("\n"); + + } +// system("pause"); + return 0; +} diff --git a/spoj_cpp_clean/SGAME/SGAME-7.cpp b/spoj_cpp_clean/SGAME/SGAME-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53f0a1d7eb8712af7606a75f583c7a090e27c1ca --- /dev/null +++ b/spoj_cpp_clean/SGAME/SGAME-7.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; +int t,n,a,b, grados[300]; + + +int max(int a, int b) +{ + return (a>b)?a:b; +} + +int main() +{ + scanf("%d",&t); + while(t--) + { + scanf("%d",&n); + memset(grados,0,sizeof(grados)); + while( scanf("%d %d",&a,&b)!=EOF && a!=-1 && b!=-1) + { + grados[a]++; + grados[b]++; + } + + bool ans= true; + + for (int i = 0; i < 300; ++i) + { + if (grados[i]%2==1) + { + ans= false; + break; + } + } + + (ans)?printf("YES\n"):printf("NO\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/SHILL/SHILL-9.cpp b/spoj_cpp_clean/SHILL/SHILL-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bea8fa8f7a520d00e74a49fe23cbdaca2a90d50a --- /dev/null +++ b/spoj_cpp_clean/SHILL/SHILL-9.cpp @@ -0,0 +1,124 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +#define INF 1000000007 +#define gc() getchar_unlocked() + +vector< vector< pair > > G; +bool marked[100007]; + +int N; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +inline void init() +{ + int i, j; + int num, M; + + //scanf("%d", &N); + N = FAST_IO(); + + G.resize(N + 1); + + for (i = 1; i <= N; i++) + { + //scanf("%d", &M); + M = FAST_IO(); + + for (j = 0; j < M; j++) + { + //scanf("%d", &num); + num = FAST_IO(); + G[i].push_back(make_pair(num, 0)); + } + + for (j = 0; j < M; j++) + { + //scanf("%d", &num); + num = FAST_IO(); + G[i][j].second = num; + } + } +} + +long long dfs(int curr) +{ + if (G[curr].size() == 0) + return INF; + + int i; + long long sum = 0, val; + + for (i = 0; i < G[curr].size(); i++) + { + val = dfs(G[curr][i].first); + + if (val <= G[curr][i].second) + sum += val; + else + { + marked[G[curr][i].first] = 1; + sum += G[curr][i].second; + } + } + + return sum; +} + +inline void print(int curr) +{ + int i; + + for (i = 0; i < G[curr].size(); i++) + { + if (!marked[G[curr][i].first]) + print(G[curr][i].first); + else + printf("%d ", G[curr][i].second); + } +} + +int main() +{ + //freopen("input.txt", "r", stdin); + + init(); + + if (N == 1) + printf("0\n"); + else + printf("%lld\n", dfs(1)); + + print(1); + + return 0; +} diff --git a/spoj_cpp_clean/SHOP/SHOP-7.cpp b/spoj_cpp_clean/SHOP/SHOP-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b82ce9ce774d64c2cf3b759b145b0a4307d3d92e --- /dev/null +++ b/spoj_cpp_clean/SHOP/SHOP-7.cpp @@ -0,0 +1,92 @@ +#include +#define MAXSIZE 30 +#define MAXINT 20000 +using namespace std; + +char grafo[30][30]; +int dist[30][30]; +int movx[]= {0,1,-1,0}; +int movy[]= {1,0,0,-1}; +int n,m; + + +bool valida(int a, int b){ + return ((a=0) && (b=0))?true:false; +} + + +void bfs(int sx, int sy) +{ + queue< pair > q; + dist[sx][sy]=0; + q.push(make_pair(sx,sy)); + + while(!q.empty()) + { + pair u= q.front(); q.pop(); + for (int i = 0; i < 4; ++i) + { + int nx= u.first + movx[i]; + int ny= u.second + movy[i]; + if (valida(nx,ny) && isdigit(grafo[nx][ny]) && dist[u.first][u.second] + (grafo[nx][ny] -'0')< dist[nx][ny] ) + { + dist[nx][ny]= dist[u.first][u.second] + (grafo[nx][ny]-'0'); + q.push( make_pair(nx,ny) ); + } + } + + } +} + + + + +void inicializar(int x, int visit[][MAXSIZE]) +{ + for (int i = 0; i < MAXSIZE; ++i) + { + for (int j = 0; j < MAXSIZE; ++j) + { + visit[i][j]=x; + } + } +} + + +int main() +{ + + while(scanf("%d %d",&n,&m)!=EOF && n!=0 && m!=0) + { + for (int i = 0; i < m; ++i) + { + scanf("%s",grafo[i]); + } + + pair S,D; + inicializar(MAXINT,dist); + + for (int i = 0; i < m; ++i) + { + for (int j = 0; j < n; ++j) + { + if (grafo[i][j]=='S') + { + S= make_pair(i,j); + grafo[i][j]='0'; + } + + if (grafo[i][j]=='D') + { + D= make_pair(i,j); + grafo[i][j]='0'; + } + } + } + + bfs(S.first,S.second); + printf("%d\n",dist[D.first][D.second]); + } + + return 0; +} diff --git a/spoj_cpp_clean/SHPATH/SHPATH-17.cpp b/spoj_cpp_clean/SHPATH/SHPATH-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e599bd72a055d8a21fea9dfab897d1aa1fa40f23 --- /dev/null +++ b/spoj_cpp_clean/SHPATH/SHPATH-17.cpp @@ -0,0 +1,106 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e4+9; + + +void solve() { + + int n,p,b,w;cin>>n; + map mp; + + vector> adj[n+5]; + + string s; + + for(int i=1;i<=n;i++) { + cin>>s; + mp[s]=i; + cin>>p; + while (p--) { + cin>>b>>w; + adj[i].push_back({b,w}); + } + } + + int r; cin>>r; + + string s1,s2; + int dis[n+5]; + bool vis[n+5]; + while (r--) { + cin>>s1>>s2; + + for(int i=1;i<=n;i++) { + dis[i]=inf; + vis[i]=0; + } + + priority_queue> pq; + dis[mp[s1]] =0; + pq.push({0,mp[s1]}); + + while (!pq.empty()) { + int a=pq.top().second; pq.pop(); + + if(vis[a]) continue; + vis[a]=1; + + for(auto u:adj[a]) { + int b= u.first, w=u.second; + + if(dis[a]+w>t; + + while (t--) { + solve(); + //cout<<"\n"; + } + + +} + diff --git a/spoj_cpp_clean/SHPATH/SHPATH-8.cpp b/spoj_cpp_clean/SHPATH/SHPATH-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f08e5062743812faf336f159d63f27a2c68fc24 --- /dev/null +++ b/spoj_cpp_clean/SHPATH/SHPATH-8.cpp @@ -0,0 +1,141 @@ +/*************************************************** + * Problem Name : SHPATH - The Shortest Path.cpp + * Problem Link : https://www.spoj.com/problems/SHPATH/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-09-16 + * Problem Type : Graph (dijkstra) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 10005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX], cost[MAX]; +int dis[MAX]; +struct node { + int x, w; + node (int a, int c) { + x = a; + w = c; + } +}; +bool operator < (node a, node b) { + return a.w > b.w; +} +int dijkstra (int n, int s, int d) { + for(int i = 1; i<=n; i++){ + dis[i] = 10000005; + } + priority_queueQ; + Q.push (node (s, 0) ); + dis[s] = 0; + + while (!Q.empty() ) { + node top = Q.top(); + int u = top.x; + Q.pop(); + int sz = adj[u].size(); + + for (int i = 0; i < sz; i++) { + int v = adj[u][i]; + int w = cost[u][i]; + + if (dis[u] + w < dis[v]) { + dis[v] = dis[u] + w; + Q.push (node (v, dis[v]) ); + } + } + } + return dis[d]; +} +int main () { + __FastIO; + int tc, n; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> n; + mapmp; + + for (int i = 1; i <= n; i++) { + string str; + cin >> str; + mp[str] = i; + int p; + cin >> p; + + for (int j = 1; j <= p; j++) { + int v, w; + cin >> v >> w; + adj[i].pb (v); + cost[i].pb (w); + } + } + + int Q; + cin >> Q; + + for (int q = 1; q <= Q; q++) { + string a, b; + cin >> a >> b; + int ans = dijkstra (n, mp[a], mp[b]); + cout << ans << "\n"; + } + + for(int i = 0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#include +#define INF 1000000000 +#define gc() getchar_unlocked() + +using namespace std; + +unordered_map map; +int N; +vector< vector< pair > > G; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +inline string gs() +{ + char c; + string temp; + + c = gc(); + while(!isalpha(c)) + c=gc(); + + temp.push_back(c); + c = gc(); + while(isalpha(c)) + { + temp.push_back(c); + c = gc(); + } + + return temp; +} + +inline void init() +{ + int i, j, E, s, w; + string temp; + char c; + + + //scanf("%d\n", &N); + N = FAST_IO(); + + map.clear(); + G.clear(); + + G.resize(N+1); + + for(i = 1; i <= N; i++) + { + /* + c = gc(); + temp.clear(); + while(c!='\n') + { + temp.push_back(c); + c = gc(); + } + */ + + temp = gs(); + //scanf("%d", &E); + E = FAST_IO(); + + for(j = 0; j < E; j++) + { + s = FAST_IO(); + w = FAST_IO(); + //scanf("%d %d\n", &s, &w); + G[i].push_back(make_pair(s, w)); + } + + map[temp] = i; + } +} + +inline int dijkstra(int root, int target) +{ + set< pair > Q; + vector D(N+1); + int i; + int alt; + pair u; + + for(i=1; i<=N; i++) + D[i] = INF; + + D[root] = 0; + + Q.insert(make_pair(0, root)); + Q.insert(make_pair(INF, N+1)); + + while(Q.begin()->first != INF) + { + u = *Q.begin(); + Q.erase(Q.begin()); + + if(u.second == target) + return u.first; + + for(i = 0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +#define gc() getchar_unlocked() +using namespace std; + +vector DP[107][107]; +int board[107][107]; +bool V[107][107][507]; +int N, M, K; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +inline void init() +{ + int i, j; + + //scanf("%d %d %d", &N, &M, &K); + N = FAST_IO(); + M = FAST_IO(); + K = FAST_IO(); + + memset(V, 0, sizeof(V)); + + for (i = 1; i <= N; i++) + for (j = 1; j <= M; j++) + //scanf("%d", &board[i][j]); + board[i][j] = FAST_IO(); +} + +inline int solve() +{ + int i, j; + vector::iterator ii; + + DP[1][1].clear(); + + if (board[1][1] <= K) + DP[1][1].push_back(board[1][1]); + else + return -1; + + for (i = 2; i <= N; i++) + { + DP[i][1].clear(); + for (ii = DP[i - 1][1].begin(); ii != DP[i - 1][1].end(); ii++) + if (*ii + board[i][1] <= K && !V[i][1][*ii]) + { + V[i][1][*ii] = 1; + DP[i][1].push_back(*ii + board[i][1]); + } + } + + for (i = 2; i <= M; i++) + { + DP[1][i].clear(); + for (ii = DP[1][i - 1].begin(); ii != DP[1][i - 1].end(); ii++) + if (*ii + board[1][i] <= K && !V[1][i][*ii]) + { + V[1][i][*ii] = 1; + DP[1][i].push_back(*ii + board[1][i]); + } + } + + for (i = 2; i <= N; i++) + for (j = 2; j <= M; j++) + { + DP[i][j].clear(); + for (ii = DP[i - 1][j - 1].begin(); ii != DP[i - 1][j - 1].end(); ii++) + if (*ii + board[i][j] <= K && !V[i][j][*ii]) + { + V[i][j][*ii] = 1; + DP[i][j].push_back(*ii + board[i][j]); + } + + for (ii = DP[i - 1][j].begin(); ii != DP[i - 1][j].end(); ii++) + if (*ii + board[i][j] <= K && !V[i][j][*ii]) + { + V[i][j][*ii] = 1; + DP[i][j].push_back(*ii + board[i][j]); + } + + for (ii = DP[i][j - 1].begin(); ii != DP[i][j - 1].end(); ii++) + if (*ii + board[i][j] <= K && !V[i][j][*ii]) + { + V[i][j][*ii] = 1; + DP[i][j].push_back(*ii + board[i][j]); + } + } + + if (DP[N][M].empty()) + return -1; + + int MMAX = -1; + + for (ii = DP[N][M].begin(); ii != DP[N][M].end(); ii++) + MMAX = max(MMAX, *ii); + + return MMAX; +} + +int main() +{ + int T; + + //freopen("input.txt", "r", stdin); + + //scanf("%d", &T); + T = FAST_IO(); + + while (T--) + { + init(); + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/SILVER/SILVER-7.cpp b/spoj_cpp_clean/SILVER/SILVER-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae3b8b6f8767e85206a0398710b646dc9c404235 --- /dev/null +++ b/spoj_cpp_clean/SILVER/SILVER-7.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main() +{ + int aux=0; + + while(scanf("%d",&aux) && aux!=0) + { + printf("%d\n",(int)log2(aux) ); + } + return 0; +} diff --git a/spoj_cpp_clean/SILVER/SILVER-9.cpp b/spoj_cpp_clean/SILVER/SILVER-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e682f0ec78d16a9db3b14f46808e3e2f3d165575 --- /dev/null +++ b/spoj_cpp_clean/SILVER/SILVER-9.cpp @@ -0,0 +1,32 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +int main() +{ + int num; + int res; + + //freopen("input.txt","r",stdin); + + scanf("%d", &num); + while(num!=0) + { + res=log((float)num)/log((float)2) -1; + printf("%d\n", res+1); + scanf("%d", &num); + } + + + return 0; +} diff --git a/spoj_cpp_clean/SMILEY1807/SMILEY1807-9.cpp b/spoj_cpp_clean/SMILEY1807/SMILEY1807-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05da3d4b6b369a1a05d1dd1cb4504b2dd51e6aaa --- /dev/null +++ b/spoj_cpp_clean/SMILEY1807/SMILEY1807-9.cpp @@ -0,0 +1,57 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +int one, eight, zero, seven; + +int main() +{ + char c; + + //freopen("input.txt","r",stdin); + + c = gc(); + while(c != EOF) + { + switch(c) + { + case '1': + one++; + break; + case '8': + if(one == 0) + break; + eight = max(eight, one) + 1; + break; + case '0': + if(eight == 0) + break; + zero = max(zero, eight) + 1; + break; + case '7': + if(zero == 0) + break; + seven = max(seven, zero) + 1; + break; + } + + c = gc(); + } + + printf("%d\n", seven); + + return 0; +} diff --git a/spoj_cpp_clean/SMPCIRC/SMPCIRC-2.cpp b/spoj_cpp_clean/SMPCIRC/SMPCIRC-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..768ffd8d523929d2a48cd0e55b824929e98ae3ba --- /dev/null +++ b/spoj_cpp_clean/SMPCIRC/SMPCIRC-2.cpp @@ -0,0 +1,59 @@ +// https://www.spoj.com/problems/SMPCIRC/ +// SMPCIRC - Two Circles +#include +#include +using std::cout; +using std::cin; + +int main() +{ + int t; + cin >> t; + while(t--) { + char ans = 'O'; + int x1, y1, r1, x2, y2, r2; + cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2; + + // Circunferências iguais + if (x1 == x2 && y1 == y2 && r1 == r2) { + cout << ans << '\n'; + continue; + } + + // Distância euclediana entre os centros das circunferências + double d = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)); + + // Circunferências internas + if (d < r1 - r2 || d < r2 - r1) ans = 'I'; + + // Tangentes internas + if (d == r1 - r2 || d == r2 - r1) ans = 'E'; + + cout << ans << '\n'; + } + + return 0; +} + +/* +Given two circles: O1 with the center o1 = (xo1, yo1) and a radius r1 +and O2 with the center o2 = (xo2, yo2) and radius r2, +please compute if O1 is inside O2 or if O2 is inside O1. + +Output description +For each test case print one character: +I, if O1 is inside O2 (or if O2 is inside O1), +E, if O1 is internally tangent to O2 (or if O2 is internally tangent to O1), +O, in other cases. + +Example + +Input: +2 +103 104 5 100 100 10 +103 104 10 100 100 10 + +Output: +E +O +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/SMPDIV/SMPDIV-2.cpp b/spoj_cpp_clean/SMPDIV/SMPDIV-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..aa8f89355adbf4ffb2467cfe332a5be1e8b7f780 --- /dev/null +++ b/spoj_cpp_clean/SMPDIV/SMPDIV-2.cpp @@ -0,0 +1,38 @@ +// https://www.spoj.com/problems/SMPDIV/ +// SMPDIV - Divisibility +#include +using std::cout; +using std::cin; + +int main() +{ + int t; + cin >> t; + + while(t--) { + int n, x, y; + cin >> n >> x >> y; + bool flag = false; + for (int i = 2; i < n; ++i) { + if (i % x == 0 && i % y != 0 ){ + cout << i << char(32); + flag = true; + } + } + if (flag) cout << '\n'; + } + return 0; +} + +/* +Example + +Input: +2 +7 2 4 +35 5 12 + +Output: +2 6 +5 10 15 20 25 30 +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/SMPSEQ3/SMPSEQ3-2.cpp b/spoj_cpp_clean/SMPSEQ3/SMPSEQ3-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3520f1e69bacea9b7b21e053156c30970456b24a --- /dev/null +++ b/spoj_cpp_clean/SMPSEQ3/SMPSEQ3-2.cpp @@ -0,0 +1,31 @@ +// https://www.spoj.com/problems/SMPSEQ3/ +// SMPSEQ3 - Fun with Sequences +#include +#include +#include + +int main() +{ + std::vector s; + int n(0), m(0), value(0); + + std::cin >> n; + while (n--) { + std::cin >> value; + s.push_back(value); + } + + std::cin >> m; + while (m--) { + std::cin >> value; + std::replace(s.begin(), s.end(), value, 999); + } + + for(int num: s) { + if (num < 999) { + std::cout << num << char(32); + } + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/SMPSEQ4/SMPSEQ4-2.cpp b/spoj_cpp_clean/SMPSEQ4/SMPSEQ4-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bf5b51f28f38739e6481bed75e0abc206c9605d6 --- /dev/null +++ b/spoj_cpp_clean/SMPSEQ4/SMPSEQ4-2.cpp @@ -0,0 +1,35 @@ +// https://www.spoj.com/problems/SMPSEQ4/ +// SMPSEQ4 - Fun with Sequences (Act 2) +#include +#include +#include + +int main() +{ + std::vector s, q; + int n(0), m(0), value(0); + + std::cin >> n; + while (n--) { + std::cin >> value; + s.push_back(value); + } + + std::cin >> m; + while (m--) { + std::cin >> value; + q.push_back(value); + } + + std::vector intersection(s.size() + q.size(), 999); + std::vector::iterator it; + it = std::set_intersection(s.begin(), s.end(), q.begin(), q.end(), intersection.begin()); + + for(int num: intersection) { + if (num < 999) { + std::cout << num << char(32); + } + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/SMPSEQ5/SMPSEQ5-2.cpp b/spoj_cpp_clean/SMPSEQ5/SMPSEQ5-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e77bd37ef53cf1b99487c9fed830765fb93b653a --- /dev/null +++ b/spoj_cpp_clean/SMPSEQ5/SMPSEQ5-2.cpp @@ -0,0 +1,30 @@ +// https://www.spoj.com/problems/SMPSEQ5/ +// SMPSEQ5 - Fun with Sequences (Act 3) +#include +#include + +int main() +{ + std::vector s, q; + int n(0), m(0), value(0); + + std::cin >> n; + while (n--) { + std::cin >> value; + s.push_back(value); + } + + std::cin >> m; + while (m--) { + std::cin >> value; + q.push_back(value); + } + + int min = s.size() < q.size() ? s.size() : q.size(); + for(int i = 0; i < min; i++) { + if (s[i] == q[i]) + std::cout << i + 1 << char(32); + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/SMPSEQ6/SMPSEQ6-2.cpp b/spoj_cpp_clean/SMPSEQ6/SMPSEQ6-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9fd6dad954a06ae672dabcf0a2d256ff42d1b702 --- /dev/null +++ b/spoj_cpp_clean/SMPSEQ6/SMPSEQ6-2.cpp @@ -0,0 +1,64 @@ +// https://www.spoj.com/problems/SMPSEQ6/ +// SMPSEQ6 - Fun with Sequences (Act 4) +#include +#include + +int main() +{ + std::vector s, q; + int n(0), m(0), x(0), value(0); + + std::cin >> n; + std::cin >> x; + m = n; + while (n--) { + std::cin >> value; + s.push_back(value); + } + + while (m--) { + std::cin >> value; + q.push_back(value); + } + + for(int i = 0; i < s.size(); i++) { + for (int y = -x; y <= x; ++y) { + if ((i + y) > 0 && s[i] == q[i + y]) + std::cout << i + 1 << char(32); + } + } + + return 0; +} + +/* +Example 1 + +Input: +5 2 +-1 2 -1 1 -1 +3 -2 -1 1 2 + +Output: +1 3 4 5 + +Example 2 + +Input: +6 4 +-1 2 2 2 2 -2 +3 -2 3 3 3 -1 + +Output: +6 + +Example 3 + +Input: +6 0 +-1 2 10 12 6 -2 +2 -2 10 21 6 -1 + +Output: +3 5 +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/SMPSEQ7/SMPSEQ7-2.cpp b/spoj_cpp_clean/SMPSEQ7/SMPSEQ7-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..884566fd46927be9eca65f42c1ec6009557e08dd --- /dev/null +++ b/spoj_cpp_clean/SMPSEQ7/SMPSEQ7-2.cpp @@ -0,0 +1,79 @@ +// https://www.spoj.com/problems/SMPSEQ7/ +// SMPSEQ7 - Fun with Sequences (Act 5) +#include + +int main() +{ + bool is_possible = true; + int n(0), value(0), last_value(0); + + std::cin >> n; + if (n < 2 || n > 100) { + is_possible = false; + n = -1; // não testar + } + + // primeira fase : decrescente + if (n > 0) { + std::cin >> last_value; + n = n - 1; + } + + while (n > 0) { + std::cin >> value; + n = n - 1; + if (value < last_value) { + last_value = value; + } else { + last_value = value; + break; // = ou > + } + } + + // segunda fase : crescente + while (n > 0) { + std::cin >> value; + n = n - 1; + if (value > last_value) { + last_value = value; + } else { + is_possible = false; // = ou > + } + } + + if (is_possible) + std::cout << "Yes"; + else + std::cout << "No"; + + return 0; +} + +/* +Example 1 + +Input: +5 +-1 2 -1 1 -1 + +Output: +No + +Example 2 + +Input: +6 +3 1 -2 -2 -1 3 + +Output: +Yes + +Example 3 + +Input: +6 +2 2 1 0 1 2 + +Output: +No +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/SMPSUM/SMPSUM-2.cpp b/spoj_cpp_clean/SMPSUM/SMPSUM-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..66e6ff015ce3e330d5ec65ebf1d8f59afb8b6b98 --- /dev/null +++ b/spoj_cpp_clean/SMPSUM/SMPSUM-2.cpp @@ -0,0 +1,52 @@ +// https://www.spoj.com/problems/SMPSUM/ +// SMPSUM - Iterated sums +#include + +using std::cin; +using std::cout; + +int smpsum(int a, int b) +{ + if (a < 1) return 0; + if (a > b) return 0; + if (b > 100) return 0; + if (a == b) return a * b; + + int s(0); + + while(a <= b) { + s += a * a; + a++; + if (a > b) break; + s += b * b; + b--; + } + + return s; +} + +int main() +{ + int a(0), b(0); + + cin >> a >> b; + cout << smpsum(a, b); + cin.clear(); + + return 0; +} + +/* +Example +Input: +1 4 + +Output: +30 +Example 2 +Input: +5 6 + +Output: +61 +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/SMPWOW/SMPWOW-2.cpp b/spoj_cpp_clean/SMPWOW/SMPWOW-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2531d9a949cd195c059959d7353a93e72a4b8804 --- /dev/null +++ b/spoj_cpp_clean/SMPWOW/SMPWOW-2.cpp @@ -0,0 +1,35 @@ +// https://www.spoj.com/problems/SMPWOW/ +// SMPWOW - Wow +#include + +int main() +{ + int x(0); + std::string out("W"); + + std::cin >> x; + for (int i = 0; i < x; i++) + out.push_back('o'); + out.push_back('w'); + + std::cout << out; + std::cin.clear(); + + return 0; +} + +/* +Example 1 +Input: +1 + +Output: +Wow + +Example 2 +Input: +7 + +Output: +Wooooooow +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/SNGINT/SNGINT-21.cpp b/spoj_cpp_clean/SNGINT/SNGINT-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8208e724a5db9fd1b653f3ce9dd79f9c8feb2e9b --- /dev/null +++ b/spoj_cpp_clean/SNGINT/SNGINT-21.cpp @@ -0,0 +1,27 @@ +/* + greedy + difficulty: easy + date: 22/Apr/2020 + problem: given an integer n, find the smallest positive integer m > 0 such that the product of digits of m equals n + by: @brpapa +*/ +#include +using namespace std; + +int main() { + int T; cin >> T; + while (T--) { + int n; cin >> n; + if (n == 0) { cout << 10 << endl; continue; } + if (n == 1) { cout << 1 << endl; continue; } + + vector revAns; + for (int d = 9; d >= 2 && n > 1; d--) + while (n%d == 0) revAns.push_back(d), n /= d; + + if (n > 1) cout << -1; + else for (int i = revAns.size()-1; i >= 0; i--) cout << revAns[i]; + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/SOCNETC/SOCNETC-14.cpp b/spoj_cpp_clean/SOCNETC/SOCNETC-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a08635e6d624ad89686db018dbe62e315fade9e6 --- /dev/null +++ b/spoj_cpp_clean/SOCNETC/SOCNETC-14.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +#define ll long long + +ll par[100009], sz[100009]; + +ll findroot(ll u) + +{ + if(par[u] == u) + return u; + return par[u] = findroot(par[u]); +} + +int main() + +{ + for(ll i = 1; i <= 100005; i++) { + par[i] = i; + sz[i] = 1; + } + + ll n, m; + cin >> n >> m; + ll q; + cin >> q; + for(ll i = 1; i <= q; i++) { + char state; + scanf(" %c", &state); + + if(state == 'S') { + ll in; + scanf("%lld", &in); + ll u = findroot(in); + printf("%lld\n", sz[u]); + } + else if(state == 'E') { + ll n1, n2; + scanf("%lld %lld", &n1, &n2); + ll u = findroot(n1); + ll v = findroot(n2); + if(u != v) + printf("No\n"); + else + printf("Yes\n"); + } + else { + ll n1, n2; + scanf("%lld %lld", &n1, &n2); + ll u = findroot(n1); + ll v = findroot(n2); + if( (sz[u] + sz[v]) <= m && u != v) { + sz[u] += sz[v]; + par[v] = u; + } + } + + } + + return 0; +} diff --git a/spoj_cpp_clean/SOMA/SOMA-10.cpp b/spoj_cpp_clean/SOMA/SOMA-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e52f4c7718ace66b3385f4242c0c108c5acb0237 --- /dev/null +++ b/spoj_cpp_clean/SOMA/SOMA-10.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main() +{ + int n, soma = 0; + cin >> n; + while(n-- > 0) + { + int j; + cin >> j; + soma += j; + } + cout << soma; + return 0; +} diff --git a/spoj_cpp_clean/SPEED/SPEED-5.cpp b/spoj_cpp_clean/SPEED/SPEED-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1a747cd1ce96ece70bafbf000e103bbc4da12f2 --- /dev/null +++ b/spoj_cpp_clean/SPEED/SPEED-5.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; +long int gcd(long int a, long int b) +{ + long int rem; + while(b) + { + rem=a%b; + a=b; + b=rem; + } + return a; +} + +int main() +{ + long int test, a, b,answer; + cin>>test; + while(test--) + { + cin>>a>>b; + if(a>b) + answer=a-b; + else + answer=b-a; + if(a<0) + a=a*(-1); + if(b<0) + b=b*(-1); + cout<<(answer/gcd(a,b))< recursive backtracking + difficulty: none + date: none + by: @brpapa +*/ +#include +using namespace std; + +char mapa[41][41]; +bool posVerificada[41][41], ok = false; +int n, m, maxEspinhos; + +bool posValida(int l, int c) { + return (l >= 0 && l < n && c >= 0 && c < m && mapa[l][c] != '#'); +} +//considerando o tesouro ('x') como posição inicial, percorre até a saída ('@') +void anda(int l, int c, int espinhos) { + if (mapa[l][c] == '@') { + ok = true; + return; + } + if (mapa[l][c] == 's') { + espinhos++; + if (espinhos * 2 > maxEspinhos) //dobro de espinhos pois tem que ir e voltar + return; + } + + //para cada possível lado, tenta andar + if (!ok && posValida(l - 1, c) && !posVerificada[l - 1][c]) { + posVerificada[l - 1][c] = true; + anda(l - 1, c, espinhos); + posVerificada[l - 1][c] = false; + } + if (!ok && posValida(l, c + 1) && !posVerificada[l][c + 1]) { + posVerificada[l][c + 1] = true; + anda(l, c + 1, espinhos); + posVerificada[l][c + 1] = false; + } + if (!ok && posValida(l + 1, c) && !posVerificada[l + 1][c]) { + posVerificada[l + 1][c] = true; + anda(l + 1, c, espinhos); + posVerificada[l + 1][c] = false; + } + if (!ok && posValida(l, c - 1) && !posVerificada[l][c - 1]) { + posVerificada[l][c - 1] = true; + anda(l, c - 1, espinhos); + posVerificada[l][c - 1] = false; + } +} + +int main() { + int l, c; + cin >> n >> m >> maxEspinhos; + for (int i = 0; i < n; i++) { + string str; + cin >> str; + for (int j = 0; j < m; j++) { + mapa[i][j] = str[j]; + if (mapa[i][j] == 'x') { + l = i; + c = j; + } + posVerificada[i][j] = false; + } + } + anda(l, c, 0); //numero de espinhos evitados inicia com 0 + printf("%s\n", ok ? "SUCCESS" : "IMPOSSIBLE"); + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/SPTTRN1/SPTTRN1-2.cpp b/spoj_cpp_clean/SPTTRN1/SPTTRN1-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9b7860c294c9ddc37cec5a9ee79731403a27803a --- /dev/null +++ b/spoj_cpp_clean/SPTTRN1/SPTTRN1-2.cpp @@ -0,0 +1,128 @@ +// https://www.spoj.com/problems/SPTTRN1/ +// SPTTRN1 - Straight Line Spiral Pattern (Act 1) +#include + +using std::cin; +using std::cout; +using std::string; + +void spiral(int s) +{ + string matrix = string(s * s, char(46)); + + int x = 0, y = 0; + int x0 = 0, y0 = 2; + int x1 = s - 1, y1 = s - 1; + int step = 0; + + while(true) { + while(true) { + matrix[y * s + x] = '*'; + + if (step % 4 == 0) { + x++; + if (x == x1) { + x1 -= 2; + break; + } + } + + if (step % 4 == 1) { + y++; + if (y == y1) { + y1 -= 2; + break; + } + } + + if (step % 4 == 2) { + x--; + if (x == x0) { + x0 += 2; + break; + } + } + + if (step % 4 == 3) { + y--; + if (y == y0) { + y0 += 2; + break; + } + } + } + step++; + if (step == s) break; + } + matrix[y * s + x] = '*'; + + for(int i = 0; i < matrix.size(); ++i) { + if (i % s == 0 && i != 0) cout << '\n'; + cout << matrix[i]; + } +} + +int main() +{ + int t(0), s(0); + + cin >> t; + while(t--) { + cin >> s; + spiral(s); + cout << '\n'; + } + + return 0; +} + +/* +Example + +Input: +5 +4 +5 +11 +13 +78 + +Output: +**** +...* +*..* +**** + +***** +....* +***.* +*...* +***** + +*********** +..........* +*********.* +*.......*.* +*.*****.*.* +*.*...*.*.* +*.*.***.*.* +*.*.....*.* +*.*******.* +*.........* +*********** + +************* +............* +***********.* +*.........*.* +*.*******.*.* +*.*.....*.*.* +*.*.***.*.*.* +*.*.*...*.*.* +*.*.*****.*.* +*.*.......*.* +*.*********.* +*...........* +************* + +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/SPTTRN2/SPTTRN2-2.cpp b/spoj_cpp_clean/SPTTRN2/SPTTRN2-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e8a8d8ee5c5e21e6d850294c3027220187ae691a --- /dev/null +++ b/spoj_cpp_clean/SPTTRN2/SPTTRN2-2.cpp @@ -0,0 +1,129 @@ +// https://www.spoj.com/problems/SPTTRN2/ +// SPTTRN2 - Straight Line Spiral Pattern (Act 2) +#include + +using std::cin; +using std::cout; +using std::string; + +void spiral(int s) +{ + string matrix = string(s * s, char(46)); + + int x = 0, y = 0; + int x0 = 2, y0 = 0; + int x1 = s - 1, y1 = s - 1; + int step = 0; + + while(true) { + while(true) { + matrix[y * s + x] = '*'; + + if (step % 4 == 0) { + y++; + if (y == y1) { + y1 -= 2; + break; + } + } + + if (step % 4 == 1) { + x++; + if (x == x1) { + x1 -= 2; + break; + } + } + + if (step % 4 == 2) { + y--; + if (y == y0) { + y0 += 2; + break; + } + } + + if (step % 4 == 3) { + x--; + if (x == x0) { + x0 += 2; + break; + } + } + } + step++; + // if (step == s) break; + if (step == s) break; + } + matrix[y * s + x] = '*'; + + for(int i = 0; i < matrix.size(); ++i) { + if (i % s == 0 && i != 0) cout << '\n'; + cout << matrix[i]; + } +} + +int main() +{ + int t(0), s(0); + + cin >> t; + while(t--) { + cin >> s; + spiral(s); + cout << '\n'; + } + + return 0; +} + +/* +Example + +Input: +5 +4 +5 +11 +13 +78 + +Output: +*.** +*..* +*..* +**** + +*.*** +*.*.* +*.*.* +*...* +***** + +*.********* +*.*.......* +*.*.*****.* +*.*.*...*.* +*.*.*.*.*.* +*.*.*.*.*.* +*.*.***.*.* +*.*.....*.* +*.*******.* +*.........* +*********** + +*.*********** +*.*.........* +*.*.*******.* +*.*.*.....*.* +*.*.*.***.*.* +*.*.*.*.*.*.* +*.*.*.*.*.*.* +*.*.*...*.*.* +*.*.*****.*.* +*.*.......*.* +*.*********.* +*...........* +************* + +*/ \ No newline at end of file diff --git a/spoj_cpp_clean/SQBR/SQBR-3.cpp b/spoj_cpp_clean/SQBR/SQBR-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39ee59d7f60802eda8caf3f69d9e2e9b110466ff --- /dev/null +++ b/spoj_cpp_clean/SQBR/SQBR-3.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int d, n, k, temp, open[39], dp[39][39]; + sd(d); + while(d--){ + sd2(n,k); + for(int i = 1; i < 39; i++) + open[i] = 0; + + for(int i = 0; i < k; i++){ + sd(temp); + open[temp] = 1; + } + + dp[1][1] = 1; + + for(int i = 1; i <= 2*n; i++){ + for(int j = 0; j <= i; j++){ + if(i == 1){ + dp[i][j] = (j == 1)? 1:0; + } + else{ + if(open[i] == 1){ + dp[i][j] = (j == 0)? 0:dp[i-1][j-1]; + } + else{ + dp[i][j] = dp[i-1][j+1] + ((j==0)?0:dp[i-1][j-1]); + } + } + } + } + + printf("%d\n",dp[2*n][0]); + + } + + return 0; +} diff --git a/spoj_cpp_clean/SQFREE/SQFREE-8.cpp b/spoj_cpp_clean/SQFREE/SQFREE-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..897b6903899ec4e11451bfcf9d424f0a136825e2 --- /dev/null +++ b/spoj_cpp_clean/SQFREE/SQFREE-8.cpp @@ -0,0 +1,70 @@ +/*************************************************** + * Problem name : SQFREE - Square-free integers.cpp + * Problem Link : http://www.spoj.com/problems/SQFREE/ + * OJ : Spoj + * Verdict : AC + * Date : 2017-11-11 + * Problem Type : Number Theory + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define MAX 10000005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +/************************************ Code Start Here ******************************************************/ +ll MF[MAX +2]; +char ar[MAX +2]; +void Mobius_Function(ll n) { + ar[0] = ar[1] = 1; + for (ll i = 1; i < n; i++) MF[i] = 1; + for (ll i = 2; i < n; i++) { + if (ar[i] == 0) { + MF[i] = -1; + for (ll j = i + i; j < n; j += i) { + if (j % (i * i) == 0)MF[j] = 0; + ar[j] = 1; + MF[j] = (-1) * MF[j]; + } + } + } +} +vectormob; +void preCal(ll n) { + //~ ll sq = sqrt(n); + Mobius_Function(n); + for (ll i = 2 ; i < n; i++) { + if (MF[i] != 0) { + mob.push_back(i); + } + } +} +int main () { + preCal(MAX); + ll tc, n; + scanf("%lld", &tc); + for (ll t = 1; t <= tc; t++) { + scanf("%lld", &n); + ll sq = sqrt(n); + ll ans = n; + for (ll i = 0; mob[i] <= sq; i++) { + ll x = n/(mob[i]*mob[i]); + ans+=(MF[mob[i]] * x); + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/spoj_cpp_clean/SQRBR/SQRBR-9.cpp b/spoj_cpp_clean/SQRBR/SQRBR-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a86f4d45b4f4b1aa4ae5072bd7b226137503d67e --- /dev/null +++ b/spoj_cpp_clean/SQRBR/SQRBR-9.cpp @@ -0,0 +1,82 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +unordered_map > memo; + + +bool A[40]; +int N; + +int f(int open, int pos) +{ + if(memo.find(open)!=memo.end() && memo[open].find(pos)!=memo[open].end()) + return memo[open][pos]; + + if(pos>N) + { + if(open==0) + return 1; + return 0; + } + if(A[pos]) + return f(open+1, pos+1); + + memo[open][pos]=0; + memo[open][pos]=f(open+1, pos+1); + + if(open>0) + memo[open][pos]+=f(open-1, pos+1); + + return memo[open][pos]; +} + +inline void init() +{ + int i, K, num; + + memo.clear(); + memset(A, 0, sizeof(A)); + + scanf("%d %d", &N, &K); + N*=2; + + for(i=0; i Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +using namespace std; + +int N, K; +int A[10007]; + +inline void init() +{ + scanf("%d %d", &N, &K); + + for (int i = 1; i <= N; i++) + A[i] = 1; +} + +inline int solve() +{ + int i, sum = 0, t; + + for (i = N; i > 1; i--) + { + t = min(A[i], K); + + A[i] -= t; + A[(int)sqrt(i)] += t; + K -= t; + + if (!K) + break; + } + + for (i = 1; i <= N; i++) + sum += A[i] * i; + + return sum; +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%d\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/SQRROOT/SQRROOT-20.cpp b/spoj_cpp_clean/SQRROOT/SQRROOT-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91208ae6393e48febe13e51d963c73b7c7ae5d8b --- /dev/null +++ b/spoj_cpp_clean/SQRROOT/SQRROOT-20.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +float squareRoot(float n) +{ + float x = n; + float y = 1; + float e = 0.000001; + while(x - y > e) { + x = (x + y)/2; + y = n/x; + } + return x; +} + +int main() +{ + int t, n; + scanf("%d", &t); + while(t--) { + scanf("%d",&n); + printf ("%.0f\n",squareRoot(n)); + } + + return 0; +} diff --git a/spoj_cpp_clean/STACKEZ/STACKEZ-21.cpp b/spoj_cpp_clean/STACKEZ/STACKEZ-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b979e3a922fed762218346427b69732cf63d25d --- /dev/null +++ b/spoj_cpp_clean/STACKEZ/STACKEZ-21.cpp @@ -0,0 +1,32 @@ +/* + ad-hoc + difficulty: none + date: none + by: @brpapa +*/ +#include +#include +using namespace std; + +int main() { + int t, op; + long long int n; + stack p; + + scanf("%d", &t); + while (t--) { + scanf("%d", &op); + if (op == 1) { + scanf("%lli", &n); + p.push(n); + } else if (op == 2 && !p.empty()) { + p.pop(); + } else if (op == 3) { + if (!p.empty()) + printf("%lli\n", p.top()); + else + printf("Empty!\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/STAMPS/STAMPS-11.cpp b/spoj_cpp_clean/STAMPS/STAMPS-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5f276c49d0af0cc81699587e81c17e3373be87a --- /dev/null +++ b/spoj_cpp_clean/STAMPS/STAMPS-11.cpp @@ -0,0 +1,48 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include +#include + +using namespace std; + +int main(void) +{ + int t; + long moreStamps; + int numFriends, result; + long numStamps[1001]; + long sum; + + cin >> t; + + for(int s = 1; s <= t; s++) + { + cin >> moreStamps >> numFriends; + + for(int i = 0; i < numFriends; i++) + cin >> numStamps[i]; + + sort(numStamps, numStamps+numFriends, greater()); + + sum = 0l; + + for(result = 0; result < numFriends; result++) + { + sum += numStamps[result]; + if(sum >= moreStamps) + break; + } + + cout << "Scenario #" << s << ":" << endl; + + if(result == numFriends) + cout << "impossible" << endl << endl; + else + cout << result+1 << endl << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/STAMPS/STAMPS-13.cpp b/spoj_cpp_clean/STAMPS/STAMPS-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd6880804e5cb95fbaf8210f758f5234953f0a7c --- /dev/null +++ b/spoj_cpp_clean/STAMPS/STAMPS-13.cpp @@ -0,0 +1,52 @@ +#pragma warning(disable:4018) // signed/unsigned mistatch +#pragma warning(disable:4244) // w64 to int cast +#pragma warning(disable:4267) // big to small -- possible loss of data +#pragma warning(disable:4786) // long identifiers +#pragma warning(disable:4800) // forcing int to bool +#pragma warning(disable:4996) // deprecations +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +int main(){ + //freopen("data.in","r",stdin); + int T; scanf("%d", &T); + for(int t=1; t<=T; ++t){ + int k, n; scanf("%d %d", &k, &n); + vector v(n); + for(int i=0; i()); + int count = 0,w=0; + for(int i=0; i +#define ll long long + +using namespace std; + +void f(){ + ll m, n; + cin>>m>>n; + vector a(n); + for(ll i=0;i>a[i]; + } + sort(a.begin(), a.end(), greater()); + ll sum=0; + for(ll i=0;i=m){ + cout<>test_cases; + for(ll j=0;j +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 + +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + ll t; cin >> t; + ll stamp, f; + for (ll i = 1; i <= t; i++) { + cin >> stamp; + cin >> f; + ll arr[f]; + for (ll j = 0; j < f; j++) { + cin >> arr[j]; + } + sort(arr, arr + f, greater()); + ll times = 0; + bool possible = false; + for (ll j = 0; j < f; j++) { + stamp = stamp - arr[j]; + times++; + if (stamp <= 0) { + possible = true; + break; + } + } + cout << "Scenario #" << i << ":" << endl; + + if (possible == true) { + cout << times << endl; + } + else {cout << "impossible" << endl;} + + if (i != t) {cout << endl;} + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/STAMPS/STAMPS-5.cpp b/spoj_cpp_clean/STAMPS/STAMPS-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..098c6510dfd784426360c3bbe34d37ef98e54e11 --- /dev/null +++ b/spoj_cpp_clean/STAMPS/STAMPS-5.cpp @@ -0,0 +1,38 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int test,more,people,i,j=1,sum; + scanf("%d",&test); + while(test--) + { + scanf("%d",&more); + scanf("%d",&people); + int *arr=new int[people]; + for(i=0;i=0;i--) + { + sum=sum+arr[i]; + if(sum>=more) + { + printf("Scenario #%d:\n%d\n",j,people-i); + break; + } + } + if(i<0) + { + printf("Scenario #%d:\nimpossible\n",j); + } + j++; + printf("\n"); + delete arr; + } + return 0; +} diff --git a/spoj_cpp_clean/STAMPS/STAMPS-7.cpp b/spoj_cpp_clean/STAMPS/STAMPS-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b8115f425ad610569f7a47e488d62d3437ac969 --- /dev/null +++ b/spoj_cpp_clean/STAMPS/STAMPS-7.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int main() +{ + int tc=0,n=0,prest=0,aux=0,cont,sum; + vector v; + + scanf("%d",&tc); + for (int i = 0; i < tc; ++i) + { + scanf("%d %d",&prest,&n); + v.clear(); + for (int j = 0; j < n; ++j) + { + scanf("%d",&aux); + v.push_back(-aux); + } + sort(v.begin(),v.end()); + cont=0; + sum=0; + for (int j = 0; j < v.size(); ++j) + { + if (sum>=prest) + { + break; + } + sum+= (-v[j]); + cont++; + } + printf("Scenario #%d:\n",i+1); + (sum>=prest)?printf("%d\n",cont):printf("impossible\n"); + printf("\n"); + + } + return 0; +} diff --git a/spoj_cpp_clean/STFM/STFM-5.cpp b/spoj_cpp_clean/STFM/STFM-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd2bba1494b3d417fc80ef4c136e6396b7e4ca6d --- /dev/null +++ b/spoj_cpp_clean/STFM/STFM-5.cpp @@ -0,0 +1,38 @@ +#include +//#include + +#define ll long long + +using namespace std; + +ll fact(ll num) +{ + if(num==1) + return 1; + else + return num*fact(num-1); +} +ll calc(ll num) +{ + ll i,j,k; + ll sum=0; + for(i=1;i<=num;i++) + { + sum=sum+(i*(fact(i)+num)); + } + return sum; +} +int main() +{ + ll test,n,m,i,j,k; + cin>>n>>m; + ll *arr=new long long[n]; + for(i=0;i>arr[i]; + ll sum=0; + for(i=0;i void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +bool f(const vector &v){ + stack side; + int k = 1; + for(int i=0, n=v.size(); i v(n); + for(int i=0; i +using namespace std; + +#define ll long long + +ll ara[1009]; + +stack st; + +int main() + +{ + ll n; + while(scanf("%lld", &n) && n != 0) { + for(ll i = 1; i <= n; i++) + scanf("%lld", &ara[i]); + + ll last = 0; + for(ll i = 1; i <= n; i++) { + if(ara[i] - last != 1) + st.push(ara[i]); + else { + last = ara[i]; + while(!st.empty() && st.top() - last == 1) { + last = st.top(); + st.pop(); + } + } + } + + if(st.empty()) { + printf("yes\n"); + } + else { + printf("no\n"); + + while(!st.empty()) + st.pop(); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/STPAR/STPAR-16.cpp b/spoj_cpp_clean/STPAR/STPAR-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da0dabc066b7885c441e11efed41b1192ff9e7b6 --- /dev/null +++ b/spoj_cpp_clean/STPAR/STPAR-16.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +bool solve(int n){ + vector a(n); + vector ans; + stack s; + vector ok(n+1,false); + for(int i=0;i>a[i]; + } + int key=1,i=0; + while(i>n; + while(n){ + if(solve(n)){ + cout<<"yes"<>n; + } + return 0; +} + + + + diff --git a/spoj_cpp_clean/STPAR/STPAR-19.cpp b/spoj_cpp_clean/STPAR/STPAR-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..026810c55306bd429be5f4fd602f49b42d6bd554 --- /dev/null +++ b/spoj_cpp_clean/STPAR/STPAR-19.cpp @@ -0,0 +1,62 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 1000001 +#define PI 3.14159265 + +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + + //ll times;cin>>times; + while (true) { + int n; cin >> n; + if (n == 0) { + return 0; + } + int arr[n]; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + vector street; + street.push_back(0); + int pos = 1, p = 0; + bool ans = true; + for (int i = 0; i < n; i++) { + if (arr[i] == pos) { + ///let go + pos++; + } + else { + if (street[p] == pos) { + street.pop_back(); + pos++; + p--; + i--; + } + else { + street.push_back(arr[i]); + p++; + } + } + } + for (int i = p; i > 0; i--) { + if (street[i] != pos) { + ans = false; + break; + } + pos++; + } + if (ans == true) { + cout << "yes" << endl; + } + else { + cout << "no" << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/STPAR/STPAR-21.cpp b/spoj_cpp_clean/STPAR/STPAR-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cc55da3be9c76a463457ce2b0f43af0d8e81347 --- /dev/null +++ b/spoj_cpp_clean/STPAR/STPAR-21.cpp @@ -0,0 +1,44 @@ +/* + greedy + difficulty: none + date: none + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int main() { + int n, aux; + while (true) { + scanf("%d", &n); + if (n == 0) + return 0; + + queue fila; //inicializa a formação inicial vazia + for (int i = 0; i < n; i++) { + scanf("%d", &aux); + fila.push(aux); + } + + stack pilha; + for (int sEsp = 1; sEsp <= n; sEsp++) { + // printf("%d: %d %d\n", sEsp, fila.empty()? 0: fila.front(), pilha.empty()? 0: pilha.top()); + if (!pilha.empty() && pilha.top() == sEsp) { + pilha.pop(); + continue; + } + if (!fila.empty()) { + if (fila.front() != sEsp) { + pilha.push(fila.front()); + fila.pop(); + sEsp--; //como não é a saída esperada, não pode ser incrementada na próxima iteração + } + else + fila.pop(); + } + } + printf("%s\n", (pilha.empty() && fila.empty()) ? "yes" : "no"); + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/STPAR/STPAR-5.cpp b/spoj_cpp_clean/STPAR/STPAR-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c32cb14d60e598b48940f66f4d04b84d6db5e6d2 --- /dev/null +++ b/spoj_cpp_clean/STPAR/STPAR-5.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; + +//stack s; + +int main() +{ + int n,i,j,k; + cin>>n; + while(n) + { + int arr[n]; + stack s; + s.push(0); + for(i=0;i>arr[i]; + for(i=0,j=1;i>n; + } + return 0; +} diff --git a/spoj_cpp_clean/STPAR/STPAR-9.cpp b/spoj_cpp_clean/STPAR/STPAR-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..632b2fe583933d9c932a06f4e7a998baba492ad8 --- /dev/null +++ b/spoj_cpp_clean/STPAR/STPAR-9.cpp @@ -0,0 +1,114 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +deque road, side; +int N; + +bool init() +{ + int i, num; + + scanf("%d", &N); + + if(N==0) + return 0; + + road.clear(); + side.clear(); + + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int gcd(int a, int b) +{ + if(b==0) + return a; + return gcd(b, a%b); +} +int a[1000009],b[1000009],c[1000009]; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T, gd; + T=0; + while(1) + { + T++; + s(n); + if(n==0)break; + FOR(i,0,n) + { + s(a[i]);s(b[i]);s(c[i]); + } + + // a[0]= + // b[0]= + c[0]+=b[0]; + + a[1]+=b[0]; + b[1]+=miN(c[0], miN(a[1], b[0])); + c[1]+=miN(b[1], miN(b[0], c[0])); + FOR(i,2,n) + { + a[i]+=miN(a[i-1], b[i-1]); + b[i]+=miN(a[i], miN(a[i-1], miN(b[i-1], c[i-1]))); + c[i]+=miN(b[i], miN(b[i-1], c[i-1])); + } + cout< +#include + +int main() +{ + int t; + std::string s; + std::vector v {}; + + std::cin >> t; + while(t >= 1 && t <= 100) { + std::cin >> s; + std::string temp(""); + temp.push_back(s[0]); + for (int i = 1; i < s.size() / 2; ++i) + if (i % 2 == 0) temp.push_back(s[i]); + v.push_back(temp); + std::cin.clear(); + t--; + } + + for (std::string output: v) + std::cout << output << '\n'; + + return 0; +} diff --git a/spoj_cpp_clean/STRINGQ/STRINGQ-9.cpp b/spoj_cpp_clean/STRINGQ/STRINGQ-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fff38ba9e7939e24073a9876491d8a58dc97182 --- /dev/null +++ b/spoj_cpp_clean/STRINGQ/STRINGQ-9.cpp @@ -0,0 +1,397 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar() +#define MOD 106109099 + +using namespace std; + +typedef struct +{ + int count[26]; + int sum; + bool lazy; + int marked; + + inline void reset() + { + memset(count, 0, sizeof(count)); + sum = 0; + marked = 0; + lazy = 0; + } + + inline void mark(int val) + { + marked = val; + lazy = 1; + } + + inline void update() + { + int temp = marked; + int s = sum; + + reset(); + + count[temp] = s; + sum = s; + } + +}node; + +node tree[4 * 50007]; +long long fact[50007]; +long long inverse[50007]; +string S; +int N, M; + +int mul_inv(int a, int b) +{ + int b0 = b, t, q; + int x0 = 0, x1 = 1; + + if (b == 1) + return 1; + + while (a > 1) + { + q = a / b; + + t = b; + b = a % b; + a = t; + + t = x0; + x0 = x1 - q * x0; + x1 = t; + } + + if (x1 < 0) + x1 += b0; + + return x1; +} + +inline void preinit() +{ + int i; + + fact[0] = 1; + + for (i = 1; i <= 50000; i++) + { + fact[i] = (fact[i - 1] * i) % MOD; + inverse[i] = mul_inv(fact[i], MOD); + } +} + +inline void getstring(string &A) +{ + char c; + + A.clear(); + + c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +inline bool check(node &curr, bool is_odd) +{ + int i; + + if (!is_odd) + { + for (i = 0; i < 26; i++) + if (curr.count[i] % 2) + return 0; + } + + else + { + int c = 0; + + for (i = 0; i < 26; i++) + if (curr.count[i] % 2) + { + if (c) + return 0; + c = 1; + } + } + + return 1; +} + +inline long long get_palindromes(node &a) +{ + int i; + long long ans = fact[a.sum / 2]; + + for (i = 0; i < 26; i++) + if (a.count[i] / 2 > 1) + ans = (ans * inverse[a.count[i] / 2]) % MOD; + + return ans; +} + +//SEGMENT TREE FUNCTIONS + +inline void merge(node &parent, node &left, node &right) +{ + int i; + + for (i = 0; i < 26; i++) + parent.count[i] = left.count[i] + right.count[i]; + + parent.sum = left.sum + right.sum; +} + +inline void merge2(node &parent, node &child) +{ + int i; + + for (i = 0; i < 26; i++) + parent.count[i] += child.count[i]; +} + +void init_tree(int index, int a, int b) +{ + if (a == b) + { + tree[index].reset(); + tree[index].count[S[a - 1] - 'a'] = 1; + tree[index].sum = 1; + return; + } + + int mid = (a + b) / 2; + + init_tree(index * 2, a, mid); + init_tree(index * 2 + 1, mid + 1, b); + + merge(tree[index], tree[index * 2], tree[index * 2 + 1]); +} + +node *query(int index, int a, int b, int i, int j) +{ + if (tree[index].lazy) + { + if (a != b) + { + tree[index * 2].lazy = 1; + tree[index * 2].mark(tree[index].marked); + + tree[index * 2 + 1].lazy = 1; + tree[index * 2 + 1].mark(tree[index].marked); + } + + tree[index].update(); + } + + if (i == a && j == b) + { + node *q = new node; + *q = tree[index]; + return q; + } + + int mid = (a + b) / 2; + + if (j <= mid) + return query(index * 2, a, mid, i, j); + + if (i > mid) + return query(index * 2 + 1, mid + 1, b, i, j); + + node *q = new node; + node *l, *r; + + l = query(index * 2, a, mid, i, mid); + r = query(index * 2 + 1, mid + 1, b, mid + 1, j); + + merge(*q, *l, *r); + delete l; + delete r; + + return q; +} + +void replace(int index, int a, int b, int i, int j, int val) +{ + if (tree[index].lazy) + { + if (a != b) + { + tree[index * 2].lazy = 1; + tree[index * 2].mark(tree[index].marked); + + tree[index * 2 + 1].lazy = 1; + tree[index * 2 + 1].mark(tree[index].marked); + } + + tree[index].update(); + } + + if (a == i && b == j) + { + tree[index].mark(val); + tree[index].update(); + + if (a != b) + { + tree[index * 2].lazy = 1; + tree[index * 2].mark(val); + + tree[index * 2 + 1].lazy = 1; + tree[index * 2 + 1].mark(val); + } + + return; + } + + int mid = (a + b) / 2; + + if (j <= mid) + replace(index * 2, a, mid, i, j, val); + + else if (i > mid) + replace(index * 2 + 1, mid + 1, b, i, j, val); + + else + { + replace(index * 2, a, mid, i, mid, val); + replace(index * 2 + 1, mid + 1, b, mid + 1, j, val); + } + + tree[index].reset(); + + if (tree[index * 2].lazy) + tree[index].count[tree[index * 2].marked] += tree[index * 2].sum; + else + merge2(tree[index], tree[index * 2]); + + if (tree[index * 2 + 1].lazy) + tree[index].count[tree[index * 2 + 1].marked] += tree[index * 2 + 1].sum; + else + merge2(tree[index], tree[index * 2 + 1]); + + tree[index].sum = tree[index * 2].sum + tree[index * 2 + 1].sum; +} + +inline void update(int i, int j) +{ + node *t = query(1, 1, N, i, j); + + int ii, temp = i; + + for (ii = 0; ii < 26; ii++) + if (t->count[ii]) + { + replace(1, 1, N, temp, temp + t->count[ii] - 1, ii); + temp = temp + t->count[ii]; + } + + delete t; +} + +inline char get_alpha(node &t) +{ + int i; + + for (i = 0; i < 26; i++) + if (t.count[i]) + return i + 'a'; + + return 0; +} + +inline void print_final() +{ + node *t; + int i; + + for (i = 0; i < S.length(); i++) + { + t = query(1, 1, N, i + 1, i + 1); + putchar(get_alpha(*t)); + + delete t; + } + + putchar('\n'); +} + +inline void init() +{ + getstring(S); + N = S.length(); + + scanf("%d\n", &M); + init_tree(1, 1, N); +} + +int main() +{ + int T, i, f, s; + char c; + node *t; +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d", &T); + + preinit(); + + while (T--) + { + init(); + + for (i = 0; i < M; i++) + { + scanf("%c %d %d\n", &c, &f, &s); + + switch (c) + { + case 'Q': + t = query(1, 1, N, f, s); + if (!check(*t, t->sum % 2)) + printf("0\n"); + else + printf("%lld\n", get_palindromes(*t)); + + delete t; + break; + + case 'U': + update(f, s); + break; + } + } + + print_final(); + } + + return 0; +} diff --git a/spoj_cpp_clean/STRMATCH/STRMATCH-14.cpp b/spoj_cpp_clean/STRMATCH/STRMATCH-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c80aa8b6b6eeb061e6df25dce8e9a51c6391420 --- /dev/null +++ b/spoj_cpp_clean/STRMATCH/STRMATCH-14.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +#define ll int + +ll n, q; +char s[500009]; +ll trie[5000009][30], cnt[5000009], id = 0; + +void build() + +{ + ll cur = 0; + for(ll i = 0; i < n; i++) { + cur = 0; + for(ll j = i; j < n; j++) { + + if(trie[cur][s[j] - 'a'] == -1) + trie[cur][s[j] - 'a'] = ++id; + + cur = trie[ cur ][ s[j] - 'a']; + cnt[ cur ]++; + } + } +} + +void query() + +{ + ll cur = 0; + ll ans, len; + while(q--) { + scanf("%s", s); + cur = 0; + bool flag = 1; + for(ll i = 0; s[i] != '\0'; i++) { + if(trie[ cur][ s[i] - 'a'] != -1) + cur = trie[cur][ s[i] - 'a']; + else { + flag = 0; + break; + } + } + + if(flag) + printf("%d\n", cnt[ cur ]); + else + printf("0\n"); + } +} + +int main() + +{ + memset(trie, -1, sizeof(trie)); + cin >> n >> q; + scanf("%s", s); + build(); + query(); + + return 0; diff --git a/spoj_cpp_clean/STRQ/STRQ-5.cpp b/spoj_cpp_clean/STRQ/STRQ-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51044000cffe7c4995c2fc87b6bbe67c4052e2fa --- /dev/null +++ b/spoj_cpp_clean/STRQ/STRQ-5.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + char input[1000001],chLeft,chRight; + int query,left,right,i,j,k; + //scanf("%s",&input); + gets(input); + scanf("%d",&query); + while(query--) + { + j=0; + getchar(); + chLeft=getc(stdin); + getchar(); + chRight=getc(stdin); + //cout< +#include +#include + +#define gc getchar_unlocked + +using namespace std; +void scanint(int &x) +{ + register int c = gc(); + x = 0; + for(;(c<48 || c>57);c = gc()); + for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;} +} +void scanchar(char &x) +{ + register char c = gc(); + //x = 0; + for(;(c<97 || c>122);c = gc()); + //for(;c>96 && c<123;c = gc()) x=c;//{x = (x<<1) + (x<<3) + c - 48;} + x=c; +} +void scanString(char x[]) +{ + register char c = gc(); + //x = 0; + int i=0; + for(;(c<97 || c>122);c = gc()); + for(;c>96 && c<123;c = gc()) + x[i++]=c;//{x = (x<<1) + (x<<3) + c - 48;} +} +int main() +{ + char input[1000001],chLeft,chRight; + int query,left,right,i,j,k; + //scanf("%s",&input); + //gets(input); + scanString(input); + //scanf("%d",&query); + scanint(query); + while(query--) + { + j=0; + /*getchar(); + chLeft=getc(stdin); + getchar(); + chRight=getc(stdin);*/ + scanchar(chLeft); + scanchar(chRight); + //cout< +#include +#include + +using namespace std; + +int main() +{ + char input[1000001],chLeft,chRight; + int query,left,right,i,j,k,len,cnt; + gets(input); + //string input="checfcheff"; + //len=input.length(); + len=strlen(input); + int *pref[6]; + int *suff[6]; + pref[0]=new int[len]; + pref[5]=new int[len]; + pref[2]=new int[len]; + pref[3]=new int[len]; + suff[0]=new int[len]; + suff[5]=new int[len]; + suff[2]=new int[len]; + suff[3]=new int[len]; + for(i=0,cnt=0;i=0;i--) + { + if(input[i]=='c') + suff[0][i]=++cnt; + else + suff[0][i]=cnt; + } + for(i=len-1,cnt=0;i>=0;i--) + { + if(input[i]=='h') + suff[5][i]=++cnt; + else + suff[5][i]=cnt; + } + for(i=len-1,cnt=0;i>=0;i--) + { + if(input[i]=='e') + suff[2][i]=++cnt; + else + suff[2][i]=cnt; + } + for(i=len-1,cnt=0;i>=0;i--) + { + if(input[i]=='f') + suff[3][i]=++cnt; + else + suff[3][i]=cnt; + } + /*for(i=0;ileft-1;i--) + { + if(input[i]==chRight) + cnt=cnt+pref[chLeft-99][i-1]; + } + } + else + { + for(i=left-1;i +#include +#include + +using namespace std; +#define n 100005 +#define ll long long + +char parens[n]; +ll inp[n],sum[n],dp[n],maxx[n]; + +char opposite(char x) +{ + if(x==')') return '('; + if(x==']') return '['; + if(x=='>') return '<'; + if(x=='}') return '{'; + return ':'; +} + +int main() +{ + ll test,num,i,j,k,result; + stack s; + scanf("%lld",&test); + while(test--) + { + scanf("%lld",&num); + sum[0]=0; + parens[0]='#'; + result=0; + scanf("%s",parens+1); + for(i=1;i<=num;i++) + { + scanf("%lld",&inp[i]); + sum[i]=sum[i-1]+inp[i]; + dp[i]=0; + } + s.push(0); + for(i=1;i<=num;i++) + { + if(parens[s.top()]==opposite(parens[i])) + { + dp[i]=s.top(); + //cout<<"dp["< + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) + +#define fi first +#define se second +#define pb push_back +#define mp make_pair +#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it) +#define meta __FUNCTION__,__LINE__ + +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); + +using namespace std; + +const long double PI = 3.1415926535897932384626433832795; + +template +ostream& operator<<(ostream& out,pair const& p){out<<'('< +ostream& operator<<(ostream& out,vector const& v){ +int l=v.size();for(int i=0;i0)out< +void tr(S x, const Strings&... rest){cout< pii; + +const int N = 100100; +const int M = 26; + +struct suffixAutomaton{ + struct state{ + int len; // length of longest string in this class + int link; // pointer to suffix link + int link_char; + int next[M]; // adjacency list + ll ways[M]; + ll cnt; // number of times the strings in this state occur in the original string + + bool terminal; // by default, empty string is a suffix + // a state is terminal if it corresponds to a suffix + + state(){ + len = 0, link = -1, cnt = 0; + terminal = false; + for(int i = 0; i < M; i++) next[i] = -1, ways[i] = 0; + } + }; + + state st[N*2]; + int sz, last, l; + char offset = 'a'; // Careful! + + ll vis[N*2]; + + suffixAutomaton(string &s){ + int l = s.length(); + + for(int i = 0; i < 2*l; i++) st[i] = state(); + + sz = 1, last = 0; + st[0].len = 0; + st[0].link = -1; + + for(int i = 0; i < l; i++) + addChar(s[i] - offset); + + for(int i = last; i != -1; i = st[i].link) st[i].terminal = true; + } + + void addChar(int c){ + int cur = sz++; assert(cur < N*2); + st[cur].len = st[last].len + 1; + st[cur].cnt = 1; + st[cur].link_char = c; + + int p = last; + while(p != -1 and st[p].next[c] == -1){ + st[p].next[c] = cur; + p = st[p].link; + } + + last = cur; + + if(p == -1){ + st[cur].link = 0; return; + } + + int q = st[p].next[c]; + + if(st[q].len == st[p].len + 1){ + st[cur].link = q; + return; + } + + int clone = sz++; + + for(int i = 0; i < M; i++) st[clone].next[i] = st[q].next[i]; + st[clone].link = st[q].link; + st[clone].len = st[p].len + 1; + st[clone].cnt = 0; // cloned states initially have cnt = 0 + + while(p != -1 and st[p].next[c] == q){ + st[p].next[c] = clone; + p = st[p].link; + } + + st[q].link = st[cur].link = clone; + + return; + } + + ll dfs(int cur){ + if(vis[cur]) return vis[cur]; + vis[cur] = 1; + for(int j = 0; j < M; j++){ + if(st[cur].next[j] == -1) continue; + st[cur].ways[j] += dfs(st[cur].next[j]); + vis[cur] += st[cur].ways[j]; + } + return vis[cur]; + } + + void numberOfOccPreprocess(){ + dfs(0); + } + + void query(int k){ + int cur = 0; + string res; + + while(k > 0){ + ll cnt = 0; + + for(int j = 0; j < M; j++){ + if(st[cur].ways[j] + cnt < k){ + cnt += st[cur].ways[j]; + } + else{ + k -= cnt; + res += (char)('a' + j); + cur = st[cur].next[j]; + break; + } + } + + k--; + } + cout << res << '\n'; + } + +}; + +int main(){ _ + string s; + cin >> s; + suffixAutomaton *suf = new suffixAutomaton(s); + suf->numberOfOccPreprocess(); + + int q, k; + cin >> q; + while(q--){ + cin >> k; + suf->query(k); + } + + return 0; +} diff --git a/spoj_cpp_clean/SUBMERGE/SUBMERGE-7.cpp b/spoj_cpp_clean/SUBMERGE/SUBMERGE-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1875799b3d28a69d2c3b81100b31a33dde43e12 --- /dev/null +++ b/spoj_cpp_clean/SUBMERGE/SUBMERGE-7.cpp @@ -0,0 +1,93 @@ +#include +#define SIZE 10100 +#define DFS_WHITE -1 +using namespace std; + +int dfs_low[SIZE], dfs_num[SIZE],dfs_parent[SIZE],articulation_vertex[SIZE], dfsNumberCounter, rootChildren, dfsRoot, V ; + +vector grafo[SIZE]; + + +int min(int a,int b) +{ + return (a= dfs_num[u]) // for articulation point + articulation_vertex[u] = true; // store this information first + //if (dfs_low[v] > dfs_num[u]) // for bridge + //printf(" Edge (%d, %d) is a bridge\n", u, v); + dfs_low[u] = min(dfs_low[u], dfs_low[v]); // update dfs_low[u] + } + else if (v != dfs_parent[u]) // a back edge and not direct cycle + dfs_low[u] = min(dfs_low[u], dfs_num[v]); // update dfs_low[u] + } +} + +int main() +{ + int E,a,b; + while(scanf("%d %d",&V,&E) && (V!=0 && E!=0) ) + { + inicializarG(V); + for (int i = 0; i < E; ++i) + { + scanf("%d %d", &a,&b); + grafo[a].push_back(b); + grafo[b].push_back(a); + } + + + dfsNumberCounter = 0; + memset(dfs_num, DFS_WHITE, sizeof dfs_num); + memset(articulation_vertex, false, sizeof articulation_vertex); + //printf("Bridges:\n"); + dfsRoot = 1; rootChildren = 0; + articulationPointAndBridge(1); + articulation_vertex[dfsRoot] = (rootChildren > 1); // special case + /*for (int i = 1; i <= V; ++i) + { + if (dfs_num[i] == DFS_WHITE) { + dfsRoot = i; rootChildren = 0; + articulationPointAndBridge(i); + articulation_vertex[dfsRoot] = (rootChildren > 1); // special case + } + }*/ + int ans=0; + //printf("Articulation Points:\n"); + for (int i = 1; i <= V; ++i) + { + if (articulation_vertex[i]) + { + ans++; + //printf(" Vertex %d\n", i); + } + } + + printf("%d\n",ans ); + + } + + return 0; +} diff --git a/spoj_cpp_clean/SUBMERGE/SUBMERGE-8.cpp b/spoj_cpp_clean/SUBMERGE/SUBMERGE-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe32462b71be3961ca517c08c440b6a388e39740 --- /dev/null +++ b/spoj_cpp_clean/SUBMERGE/SUBMERGE-8.cpp @@ -0,0 +1,131 @@ +/*************************************************** + * Problem Name : SUBMERGE - Submerging Islands.cpp + * Problem Link : https://www.spoj.com/problems/SUBMERGE/ + * OJ : Spoj + * Verdict : AC + * Date : 2018-10-25 + * Problem Type : Graph (AP) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 10005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +int n, m, t; +bool vis[MAX], chk[MAX]; +int dis[MAX], low[MAX], cnt; +void dfs (int u, int p) { + vis[u] = true; + dis[u] = low[u] = t++; + int child = 0; + + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + if (vis[v] == true) { + low[u] = min (low[u], dis[v]); + + } else { + dfs (v, u); + low[u] = min (low[u], low[v]); + + if (dis[u] <= low[v] && p != -1 && chk[u] == 0) { + cnt++; + chk[u] = 1; + } + + child++; + } + } + + if (p == -1 && child > 1 && chk[u] == 0) { + cnt++; + chk[u] = 1; + } +} +void find_articulation_point() { + t = 0, cnt = 0; + SET (vis, false); + SET (chk, false); + SET (dis, -1); + SET (low, -1); + + for (int i = 1; i <= n; i++) { + if (!vis[i]) dfs (i, -1); + } +} + +int main () { + __FastIO; + + while (cin >> n >> m) { + if (n == 0 && m == 0) break; + + for (int i = 0; i < m; i++) { + int u, v; + cin >> u >> v; + adj[u].pb (v); + adj[v].pb (u); + } + + find_articulation_point(); + cout << cnt << "\n"; + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/SUBSUMS/SUBSUMS-13.cpp b/spoj_cpp_clean/SUBSUMS/SUBSUMS-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03a9da86c28a06726c65f2cc856d7f03f47073e9 --- /dev/null +++ b/spoj_cpp_clean/SUBSUMS/SUBSUMS-13.cpp @@ -0,0 +1,71 @@ +#include "assert.h" +#include "ctype.h" +#include "float.h" +#include "math.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "stdarg.h" +#include "time.h" +#include "algorithm" +#include "numeric" +#include "functional" +#include "utility" +#include "bitset" +#include "vector" +#include "list" +#include "set" +#include "map" +#include "queue" +#include "stack" +#include "string" +#include "sstream" +#include "iostream" +#define all(v) (v).begin(), (v).end() +typedef long long i64; +template void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());} +using namespace std; + +int main(){ +#ifndef ONLINE_JUDGE + freopen("data.in","r",stdin); +#endif + int n, a, b; scanf("%d %d %d",&n, &a, &b); + vector v(n); + vector v1, v2; v1.reserve(1 << (n/2)), v2.reserve(1 << ((n+1)/2)); + for(int i=0; i()); + sort(all(v2)); + i64 res = 0; + //utilizando lower_bound upper_bound nlogn + /*for(int i=0, m=v2.size(); i::iterator low = lower_bound(all(v1),(i64)(a)-v2[i]); + vector ::iterator high = upper_bound(all(v1),(i64)(b)-v2[i]); + res += high - low; + }*/ + //utilizando ventanas deslizantes + int p1, p2, p3, m, w; + for(p1 = p2 = p3 = 0, m = v1.size(), w = v2.size(); p1 < m && p2 < w; ++p1){ + for(;p2 < w && v2[p2] < (i64)(a)-v1[p1]; ++p2); + if(p2 > p3)p3 = p2; + for(;p3 < w && v2[p3] <= (i64)(b)-v1[p1];++p3); + res += p3-p2; + } + printf("%lld\n",res); + return 0; +} diff --git a/spoj_cpp_clean/SUBXOR/SUBXOR-14.cpp b/spoj_cpp_clean/SUBXOR/SUBXOR-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ac0f076ed5331e36174d53d2c5c622d16cb868a --- /dev/null +++ b/spoj_cpp_clean/SUBXOR/SUBXOR-14.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +#define ll long long + +ll trie[2000099][2], nodes = 1, cnt[4000198]; + +void add(ll num) + +{ + ll cur = 0; + for(ll i = 20; i >= 0; i--) { + ll nxt = (num >> i) & 1; + if(trie[cur][nxt] == 0) { + trie[cur][nxt] = nodes; + nodes++; + } + cur = trie[cur][nxt]; + cnt[cur]++; + } +} + +ll query(ll num, ll k) + +{ + ll cur = 0, ans = 0; + for(ll i = 20; i >= 0; i--) { + ll nxt = (num >> i) & 1; + ll nxt2 = (k >> i) & 1; + if(nxt2 == 1) { + ans += cnt[ trie[cur][nxt] ]; + cur = trie[cur][nxt ^ 1]; + } + else { + cur = trie[cur][nxt]; + } + + if(cur == 0) + break; + } + + //ans += cnt[cur]; + return ans; +} + +int main() + +{ + ll t, k, n; + scanf("%lld", &t); + while(t--) { + memset(trie, 0, sizeof(trie)); + memset(cnt, 0, sizeof(cnt)); + scanf("%lld %lld", &n, &k); + ll cur = 0, ans = 0; + add(0); + for(ll i = 1; i <= n; i++) { + ll in; + scanf("%lld", &in); + cur ^= in; + ans += query(cur, k); + add(cur); + } + + printf("%lld\n", ans); + } + + return 0; diff --git a/spoj_cpp_clean/SUBXOR/SUBXOR-3.cpp b/spoj_cpp_clean/SUBXOR/SUBXOR-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee885183ce9610aa76929c90c83b4208cd99cceb --- /dev/null +++ b/spoj_cpp_clean/SUBXOR/SUBXOR-3.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d%d",&x,&y); +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z); + +using namespace std; + +struct node{ //right is 1 and left is 0 + node *left, *right; + int left_count, right_count; +}; + +node *ROOT; + +void insert(node *cur, int num){ + for(int i = 17; i >= 0; i--){ //if ith significant bit is 1 go to the right child + if(((num>>i)&1) > 0){ + cur->right_count++; + if(cur->right == NULL){ + node *temp = new node; + cur->right = temp; + } + cur = cur->right; + } + else{ //if ith significant bit is 0 go to the left child + cur->left_count++; + if(cur->left == NULL){ + node *temp = new node; + cur->left = temp; + } + cur = cur->left; + } + } + return; +} + +int query(node *cur, int q, int k){ + int res = 0, b1, b2; + for(int i = 17; i >= 0; i--){ + b1 = (k>>i)&1; + b2 = (q>>i)&1; + if(b1){ + if(b2){ + res += cur->right_count; + if(cur->left == NULL) return res; + cur = cur->left; + } + else{ + res += cur->left_count; + if(cur->right == NULL) return res; + cur = cur->right; + } + } + else{ + if(b2){ + if(cur->right == NULL) return res; + cur = cur->right; + } + else{ + if(cur->left == NULL) return res; + cur = cur->left; + } + } + } + return res; +} + +int main(){ + int t, n, k, num, val; + long long ans; + sd(t); + while(t--){ + sd2(n,k); + ROOT = new node; + val = ans = 0; + insert(ROOT, 0); + for(int i = 0; i < n; i++){ + sd(num); + val ^= num; + ans += (long long) query(ROOT, val, k); + insert(ROOT, val); + } + printf("%lld\n",ans); + } + return 0; +} + diff --git a/spoj_cpp_clean/SUMFOUR/SUMFOUR-14.cpp b/spoj_cpp_clean/SUMFOUR/SUMFOUR-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..417e45fea860e0ba67490e02e3a9aefa4ff655f6 --- /dev/null +++ b/spoj_cpp_clean/SUMFOUR/SUMFOUR-14.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +#define ll int + +ll ara[2500][4], lst1[2500*2500 + 5], lst2[2500*2500 + 5], id1 = 0, id2 = 0; + +int main() + +{ + ll n; + cin >> n; + for(ll i = 0; i < n; i++) { + for(ll j = 0; j < 4; j++) + scanf("%d", &ara[i][j]); + } + + for(ll i = 0; i < n; i++) { + for(ll j = 0; j < n; j++) + lst1[id1++] = ara[i][0] + ara[j][1]; + } + sort(lst1, lst1 + id1); + + for(ll i = 0; i < n; i++) { + for(ll j = 0; j < n; j++) + lst2[id2++] = ara[i][2] + ara[j][3]; + } + sort(lst2, lst2 + id2, greater ()); + +// cout << endl; +// for(ll i = 0; i < id1; i++) +// cout << lst1[i] << " "; +// cout << endl; +// for(ll i = 0; i < id2; i++) +// cout << lst2[i] << " "; +// cout << endl; + + ll ans = 0, cnt1 = 0, cnt2 = 0, k = 0; + for(ll i = 0; i < id1;) { + cnt1 = 1; + i++; + while(i < id1 && lst1[i] == lst1[i - 1]) { + i++; + cnt1++; + } + + while(k < id2 && lst2[k] > -lst1[i-1]) + k++; + if(k == id2 || lst2[k] < -lst1[i-1]) + continue; + + cnt2 = 1; + k++; + while(k < id2 && lst2[k] == lst2[k - 1]) { + k++; + cnt2++; + } + + + ans += (cnt1 * cnt2); + } + + cout << ans << endl; + + return 0; +} diff --git a/spoj_cpp_clean/SUMFOUR/SUMFOUR-4.cpp b/spoj_cpp_clean/SUMFOUR/SUMFOUR-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70fc56088ffc4237dc6fbca602c5c7e9151bab35 --- /dev/null +++ b/spoj_cpp_clean/SUMFOUR/SUMFOUR-4.cpp @@ -0,0 +1,184 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[4003],b[4003],c[4003],d[4003]; +int v1[17000000], v2[17000000]; +int pos1, pos2; + +int binarysrch(int data) +{ + int st=0, en=pos1, mid, cnt, i; + while(st<=en) + { + mid=(st+en)/2; + //error(mid); + if(data==v2[mid]) + { + //error(data); + cnt=0;i=mid; + while(data==v2[i] && i=0) + i--, cnt++; + return cnt; + } + else if (data < v2[mid]) + en=mid-1; + else + st=mid+1; + } + return 0; +} +/* +int binarysrch(int val) +{ + int size=0,temp=0,low=0,high=pos1,mid; + while(low<=high) + { + mid=(high+low)/2; + if(v2[mid]==val) + { + temp=mid-1; + while(mid=0&&v2[temp]==val) + { + size++; + temp--; + } + return size; + } + else if(v2[mid]>val) + high=mid-1; + else + low=mid+1; + } + return 0; +} +*/ +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + s(T); + FOR(i,0,T) + { + s(a[i]);s(b[i]);s(c[i]);s(d[i]); + } + pos1=0, pos2=0; + FOR(i,0,T) + { + FOR(j,0,T) + { + m=a[i]+b[j]; + v1[pos1++]=m; + m=(c[i]+d[j])*-1; + v2[pos2++]=m; + } + }// 56 -2 -10 -72 -86 + sort(v1,v1+pos1);sort(v2,v2+pos2); + int tmp,cnt1,cnt2,ans=0; + /* + FOR(i,0,pos1) + cout<0 && cnt2>0) + //error(tmp); + ans+=(cnt1*cnt2); + } + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +//This code is writtent like this due to byte limit of source code :P + +#define s(v) scanf("%d",&v); +main(t,n,i,j){s(t)while(t--){s(n)int a[100][100];for(i=0;i=0;i--){for(j=0;j<=i;j++)a[i][j]+=a[i+1][j+1]>a[i+1][j]?a[i+1][j+1]:a[i+1][j];}printf("%d\n",a[0][0]);}} diff --git a/spoj_cpp_clean/SUMSUM/SUMSUM-14.cpp b/spoj_cpp_clean/SUMSUM/SUMSUM-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a85bd7217936d4b2082568814723bfd019f573dc --- /dev/null +++ b/spoj_cpp_clean/SUMSUM/SUMSUM-14.cpp @@ -0,0 +1,138 @@ +#include +using namespace std; + +int one[300009][32], ara[100009]; +char state[5]; +#define ll long long + +void build(int lo, int hi, int node) + +{ + if(lo == hi) { + for(int i = 30; i >= 0; i--) { + int state = (ara[lo] >> i) & 1; + if(state == 0) { + one[node][i] = 0; + } + else { + one[node][i] = 1; + } + } + return; + } + + int mid = (lo + hi) / 2; + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + + for(int i = 30; i >= 0; i--) { + one[node][i] = one[2 * node][i] + one[2 * node + 1][i]; + } +} + +void update(int lo, int hi, int indx, int x, int node) + +{ + if(lo > indx || hi < indx) + return; + if(lo == indx && hi == indx) { + for(int i = 30; i >= 0; i--) { + int state = (x >> i) & 1; + if(state == 0) { + one[node][i] = 0; + } + else { + one[node][i] = 1; + } + } + return; + } + + int mid = (lo + hi) / 2; + update(lo, mid, indx, x, 2 * node); + update(mid + 1, hi, indx, x, 2 * node + 1); + + for(int i = 30; i >= 0; i--) { + one[node][i] = one[2 * node][i] + one[2 * node + 1][i]; + } +} + +int query(int lo, int hi, int left, int right, int bt, int node) + +{ + if(lo > right || hi < left) + return 0; + if(lo >= left && hi <= right) { + return one[node][bt]; + } + + int mid = (lo + hi) / 2; + int p1 = query(lo, mid, left, right, bt, 2 * node); + int p2 = query(mid + 1, hi, left, right, bt, 2 * node + 1); + + return p1 + p2; +} + +int main() + +{ + int n, q; + scanf("%d %d", &n, &q); + for(int i = 1; i <= n; i++) + scanf("%d", &ara[i]); + build(1, n, 1); + + for(int i = 1; i <= q; i++) { + int op; + scanf("%d", &op); + if(op == 1) { + int x, indx; + scanf("%d %d", &x, &indx); + update(1, n, indx, x, 1); + } + if(op == 2) { + scanf(" %s", state); + int l, r; + ll zer, on, ans = 0, total; + scanf("%d %d", &l, &r); + if(l == r) { + printf("0\n"); + continue; + } + if(l > r) + swap(l, r); + + if(state[0] == 'X') { + for(int j = 30; j >= 0; j--) { + on = query(1, n, l, r, j, 1); + zer = (r - l + 1) - on; + total = zer + on; + total = (total * (total - 1)) / 2; + total -= (on * (on - 1)) / 2; + total -= (zer * (zer - 1)) / 2; + ans += (1 << j) * total; + } + } + else if(state[0] == 'O') { + for(int j = 30; j >= 0; j--) { + on = query(1, n, l, r, j, 1); + zer = (r - l + 1) - on; + total = zer + on; + total = (total * (total - 1)) / 2; + total -= (zer * (zer - 1)) / 2; + ans += (1 << j) * total; + } + } + else { + for(int j = 30; j >= 0; j--) { + on = query(1, n, l, r, j, 1); + total = (on * (on - 1)) / 2; + ans += (1 << j) * total; + } + } + + printf("%lld\n", ans); + } + } + return 0; +} diff --git a/spoj_cpp_clean/SYNC13C/SYNC13C-14.cpp b/spoj_cpp_clean/SYNC13C/SYNC13C-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..601d8186956efaf98aadefe7972002e142032aa6 --- /dev/null +++ b/spoj_cpp_clean/SYNC13C/SYNC13C-14.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +#define ll int + +int main() + +{ + ll t; + + scanf("%d", &t); + ll c1, c2; + while(t--) { + scanf("%d %d", &c1, &c2); + + if( (c1 & 1) && (c2 & 1) ) + printf("Ramesh\n"); + else + printf("Suresh\n"); + + } + + return 0; +} diff --git a/spoj_cpp_clean/TAP2016G/TAP2016G-9.cpp b/spoj_cpp_clean/TAP2016G/TAP2016G-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36b7c1ed7b93e6384e2291c59859efcefd721349 --- /dev/null +++ b/spoj_cpp_clean/TAP2016G/TAP2016G-9.cpp @@ -0,0 +1,179 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#include + +#define MAXN 100007 +#define SHIFT 29 +#define gc() getchar() +using namespace std; + +struct node +{ + int index; + struct node *ace; + struct node *zero; + + node() + { + ace = zero = nullptr; + } +}; + +inline bool eoi() +{ + char c = gc(); + + while (isspace(c)) + c = gc(); + + if (c == EOF) + return 1; + + ungetc(c, stdin); + + return 0; +} + +int N; +vector > > G; +int val[MAXN]; +struct node *tree; + +inline void init() +{ + int i, f, s, w; + + scanf("%d", &N); + + G.clear(); + G.resize(N + 1); + + for (i = 0; i < N - 1; i++) + { + scanf("%d %d %d", &f, &s, &w); + G[f].push_back(make_pair(s, w)); + G[s].push_back(make_pair(f, w)); + } + + delete tree; + + tree = new struct node; +} + +void insert(struct node **curr, int index, bitset<30> num, int pos) +{ + if (pos == -1) + { + (*curr)->index = index; + return; + } + + if (num[pos]) + { + if ((*curr)->ace == nullptr) + (*curr)->ace = new struct node; + + insert(&(*curr)->ace, index, num, pos - 1); + } + + else + { + if ((*curr)->zero == nullptr) + (*curr)->zero = new struct node; + + insert(&(*curr)->zero, index, num, pos - 1); + } +} + +int ffind(struct node *curr, bitset<30> num, int pos) +{ + if (pos == -1) + return curr->index; + + if (num[pos]) + { + if (curr->zero != nullptr) + return ffind(curr->zero, num, pos - 1); + else + return ffind(curr->ace, num, pos - 1); + } + + else + { + if (curr->ace != nullptr) + return ffind(curr->ace, num, pos - 1); + else + return ffind(curr->zero, num, pos - 1); + } + + return 0; +} + +void destruct(struct node **curr) +{ + if ((*curr)->ace != nullptr) + { + destruct(&(*curr)->ace); + delete (*curr)->ace; + } + + if ((*curr)->zero != nullptr) + { + destruct(&(*curr)->zero); + delete (*curr)->zero; + } +} + +void dfs(int curr, int fro, int v) +{ + val[curr] = v; + + for (int i = 0; i < G[curr].size(); i++) + if(G[curr][i].first != fro) + dfs(G[curr][i].first, curr, v ^ G[curr][i].second); +} + +inline void solve() +{ + int i; + + for (i = 1; i <= N; i++) + { + bitset<30> temp(val[i]); + insert(&tree, i, temp, 29); + } + + for (i = 1; i <= N; i++) + { + bitset<30> temp(val[i]); + printf("%d\n", val[i] ^ val[ffind(tree, temp, 29)]); + } +} + +int main() +{ +// freopen("input.txt", "r", stdin); + + while (!eoi()) + { + init(); + dfs(1, 1, 0); + solve(); + destruct(&tree); + } + return 0; +} diff --git a/spoj_cpp_clean/TAUT/TAUT-13.cpp b/spoj_cpp_clean/TAUT/TAUT-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8290711c37aa2572d14a381b501f96fa84d63f00 --- /dev/null +++ b/spoj_cpp_clean/TAUT/TAUT-13.cpp @@ -0,0 +1,71 @@ +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +char buffer[150]; + +bool getV(vector &vars, int comb, char x){ + int k = x - 'a'; + return comb & (1 << vars[k]); +} + +bool g(vector &vars, int comb, string &s){ + stack v; + + for(int i=(int)s.size()-1; i>=0; --i){ + if(s[i] >= 'a' && s[i] <= 'z'){ + v.push(getV(vars, comb, s[i])); + }else{ + bool a, b; + if(s[i] == 'N'){ + a = v.top(); v.pop(); + a = !a; + }else{ + a = v.top(); v.pop(); + b = v.top(); v.pop(); + + if(s[i] == 'C')a = a && b; + else if(s[i] == 'D')a = a || b; + else if(s[i] == 'I')a = !a?true:b; + else a = (!a || b) && (a || !b); + } + v.push(a); + } + } + + return v.top(); +} + +void f(string s){ + int vars = 0; + vector mark(26, -1); + for(int i=0, n=(int)s.size(); i= 'a' && s[i] <= 'z' && mark[s[i]-'a'] == -1){ + mark[s[i] - 'a'] = vars; + ++vars; + } + } + + bool ok = true; + for(int k=0; k < (1 << vars) && ok; ++k){ + ok = ok && g(mark, k, s); + } + + if(!ok)printf("NO\n"); + else printf("YES\n"); +} + +int main(){ + int n; scanf("%d", &n); + for(int i=0; i +using namespace std; + +#define ll long long +const ll maxnodes = 10005; + +ll nodes = maxnodes, src, dest; +ll dist[maxnodes], q[maxnodes], work[maxnodes]; + +struct Edge +{ + ll to, rev; + ll f, cap; +}; + +vector g[maxnodes]; + +void addEdge(ll s, ll t, ll cap) +{ + Edge a = {t, g[t].size(), 0, cap}; + Edge b = {s, g[s].size(), 0, 0}; + g[s].push_back(a); + g[t].push_back(b); +} + +bool dinic_bfs() +{ + fill(dist, dist + nodes, -1); + + dist[src] = 0; + ll index = 0; + q[index++] = src; + + for (ll i = 0; i < index; i++) + { + ll u = q[i]; + for (ll j = 0; j < (ll) g[u].size(); j++) + { + Edge &e = g[u][j]; + if (dist[e.to] < 0 && e.f < e.cap) + { + dist[e.to] = dist[u] + 1; + q[index++] = e.to; + } + } + } + return dist[dest] >= 0; +} + +ll dinic_dfs(ll u, ll f) +{ + if (u == dest) + return f; + + for (ll &i = work[u]; i < (ll) g[u].size(); i++) + { + Edge &e = g[u][i]; + + if (e.cap <= e.f) continue; + + if (dist[e.to] == dist[u] + 1) + { + ll flow = dinic_dfs(e.to, min(f, e.cap - e.f)); + if (flow > 0) + { + e.f += flow; + g[e.to][e.rev].f -= flow; + return flow; + } + } + } + return 0; +} + +ll maxFlow(ll _src, ll _dest) +{ + src = _src; + dest = _dest; + ll result = 0; + while (dinic_bfs()) + { + fill(work, work + nodes, 0); + while (ll delta = dinic_dfs(src, INT_MAX)) + result += delta; + } + return result; +} + + +// addEdge(u, v, C); edge from u to v. Capacity is C +// maxFlow(s, t); max flow from s to t + +pair per[405], taxi[205]; + +int main() + +{ + ll T; + cin >> T; + while(T--) { + ll p, t, s, c; + scanf("%lld %lld %lld %lld", &p, &t, &s, &c); + + for(ll i = 1; i <= p; i++) { + ll x, y; + scanf("%lld %lld", &x, &y); + + per[i] = make_pair(x, y); + } + + for(ll i = 1; i <= t; i++) { + + ll x, y; + scanf("%lld %lld", &x, &y); + + taxi[i] = make_pair(x, y); + } + + for(ll i = 1; i <= t; i++) { + + for(ll j = 1; j <= p; j++) { + ll dist = abs(per[j].first - taxi[i].first) + abs(per[j].second - taxi[i].second); + + if(dist * 200 <= s * c) + addEdge(i, j + t, 1); + } + } + + for(ll i = 1; i <= t; i++) + addEdge(0, i, 1); + for(ll i = 1; i <= p; i++) + addEdge(i + t, t + p + 1, 1); + + printf("%lld\n", maxFlow(0, t + p + 1)); + + for(ll i = 0; i < maxnodes; i++) { + g[i].clear(); + dist[i] = 0; + q[i] = 0; + work[i] = 0; + } + + } + + return 0; +} diff --git a/spoj_cpp_clean/TBATTLE/TBATTLE-9.cpp b/spoj_cpp_clean/TBATTLE/TBATTLE-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..255597b6cbb06a265ea0884aeccedfad41553731 --- /dev/null +++ b/spoj_cpp_clean/TBATTLE/TBATTLE-9.cpp @@ -0,0 +1,93 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +#define gc() getchar_unlocked() + +using namespace std; + +int N; + +inline int FAST_IO() +{ + char ch; + int val=0; + ch=gc(); + while(isspace(ch) && ch!=EOF) + ch=gc(); + val=0; + while(isdigit(ch)) + { + val=(val*10)+(ch-'0'); + ch=gc(); + } + return val; +} + +int main() +{ + long long res = 1, res2 = 1; + int i, j, num; + int MIN = INT_MAX, MIN_I, MIN_J; + vector A; + + //freopen("input.txt","r",stdin); + + N = FAST_IO(); + + //scanf("%d", &N); + + for(i = 0; i < N; i++) + { + num = FAST_IO(); + + //scanf("%d", &num); + + if(num % N == 0) + { + printf("%d %d\n", i, i); + return 0; + } + + A.push_back(num); + res *= num; + res %= N; + + if(res == 0) + { + res2 = 1; + + for(j = i; (res2 * A[j]) % N != 0; j--) + res2 = (res2 * A[j]) % N; + + if(i - j + 1 < MIN) + { + MIN = i - j + 1; + MIN_I = j; + MIN_J = i; + } + + res = A.back(); + } + } + + if(MIN == INT_MAX) + printf("-1\n"); + else + printf("%d %d\n", MIN_I, MIN_J); + + return 0; +} diff --git a/spoj_cpp_clean/TDKPRIME/TDKPRIME-14.cpp b/spoj_cpp_clean/TDKPRIME/TDKPRIME-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04ca910032eca3871e8d5c5e68555d747bd85abb --- /dev/null +++ b/spoj_cpp_clean/TDKPRIME/TDKPRIME-14.cpp @@ -0,0 +1,49 @@ +#include +#include +long long int B[100000005]; +bool A[100000005]; +int main() +{ + long long int X=100000000,i,j,p=2,q,Y; + for(i=0; i<=X; i++) + { + A[i]=1; + } + for(i=2; i*i<=X; ) + { + if(A[i] != 0) + { + for(j=i; (i*j) <= X; j++) + { + if(A[i*j]!=0) + { + A[i*j]=0; + } + } + } + if(i==2) + { + i++; + } + else + { + i+=2; + } + } + B[1]=2; + for(i=3; i<=X; i+=2) + { + if(A[i]!=0) + { + B[p]=i; + p++; + } + } + scanf("%lld",&q); + for(i=0; i + +using namespace std; + +typedef long long ll; +typedef vector vb; +typedef vector vi; +typedef pair ii; +typedef vector vu; +typedef vector vl; + +#define din(x) int x; cin >> x; +#define in(x) cin >> x; +#define full(x) x.begin(), x.end() + +#define eb emplace_back +#define ins insert +#define f first +#define s second + +void count_primes(int n) { + const int S = 10000; + + vector primes; + int nsqrt = sqrt(n); + vector is_prime(nsqrt + 1, true); + for (int i = 2; i <= nsqrt; i++) { + if (is_prime[i]) { + primes.push_back(i); + for (int j = i * i; j <= nsqrt; j += i) + is_prime[j] = false; + } + } + + int result = 0; + vector block(S); + for (int k = 0; k * S <= n; k++) { + fill(block.begin(), block.end(), true); + int start = k * S; + for (int p : primes) { + int start_idx = (start + p - 1) / p; + int j = max(start_idx, p) * p - start; + for (; j < S; j += p) + block[j] = false; + } + if (k == 0) + block[0] = block[1] = false; + for (int i = 0; i < S && start + i <= n; i++) { + if (block[i]) { + result++; + if ( result%100 == 1) + cout << i + k*S << endl; + } + } + } + +} + + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + count_primes(100000000); + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/TDPRIMES/TDPRIMES-20.cpp b/spoj_cpp_clean/TDPRIMES/TDPRIMES-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bedc64735644e2fa64657294e38799ce86742072 --- /dev/null +++ b/spoj_cpp_clean/TDPRIMES/TDPRIMES-20.cpp @@ -0,0 +1,50 @@ +#include +#define endl "\n" +#define pi acos(-1) +#define faster_io ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); + +using namespace std; + +#define MAX 1000000001 // limit is 10^6-1 +vector primes; +bool marked[MAX]; //for memory efficient , + +bool isPrime(long long int n) { + if (n < 2) + return false; + if (n == 2) + return true; + if (n % 2 == 0) + return false; + return marked[n] == false; +} +void sieve(long long int n) { + for (int i = 3; i * i <= n; i += 2) { + if (marked[i] == false && i * i <= n) { // i is a prime + for (int j = i * i; j <= n; j += i) { + marked[j] = true; + } + } + } +// if you need to store the primes then -> + primes.push_back(2); + for (long long int i = 3; i <= n; i += 2) { + if (marked[i] == false) // i is a prime + primes.push_back(i); + } +} + +int main() +{ + //freopen("/home/sohel/Documents/my_codes/out.txt", "wt", stdout); + faster_io; + + sieve(1000000000); + //to check n is prime or not + + for(long long int i = 0; i < (long long int) primes.size(); i += 500) { + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +#define max 100000000 +#define lmt 10000 + +unsigned flag[max / 64]; +int prime[max], total; + +#define chkC(n) (flag[n>>6] & (1<<((n>>1) & 31))) +#define setC(n) (flag[n>>6] |= (1 <<((n>>1) & 31))) + +void sieve() { + unsigned i, j, k; + flag[0] |= 0; + + for (i = 3; i < lmt; i += 2) { + if (!chkC (i) ) { + for (j = i * i, k = i << 1; j < max; j += k) { + setC (j); + } + } + } + + prime[ (j = 0)++] = 2; + + for (i = 3; i < max; i += 2) { + if (!chkC (i) ) { + prime[j++] = i; + } + } + + total = j - 1; +} +int main () { + __FastIO; + sieve(); + int i = 0; + + while (total > i) { + cout << prime[i] << "\n"; + i += 100; + } + + return 0; +} diff --git a/spoj_cpp_clean/TEMPLEQ/TEMPLEQ-14.cpp b/spoj_cpp_clean/TEMPLEQ/TEMPLEQ-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c7d756692c2a5f0876a8592a63895613fe07046 --- /dev/null +++ b/spoj_cpp_clean/TEMPLEQ/TEMPLEQ-14.cpp @@ -0,0 +1,149 @@ +#include +using namespace std; + +#define ll long long +#define pii pair +#define val first +#define id second +ll posArray[100009], posTree[100009]; +ll tree[300009], lazy[300009]; +pii ara[100009]; + +void prop(ll lo, ll hi, ll node) + +{ + if(lo == hi) + return; + + lazy[2 * node] += lazy[node]; + lazy[2 * node + 1] += lazy[node]; + + tree[2 * node] += lazy[node]; + tree[2 * node + 1] += lazy[node]; + + lazy[node] = 0; +} + +void build(ll lo, ll hi, ll node) + +{ + if(lo == hi) { + tree[node] = ara[lo].val; + posTree[ ara[lo].id ] = lo; + posArray[lo] = ara[lo].id; + return; + } + + ll mid = (lo + hi) / 2; + build(lo, mid, 2 * node); + build(mid + 1, hi, 2 * node + 1); + + tree[node] = max(tree[2 * node], tree[2 * node + 1]); +} + +void update(ll lo, ll hi, ll left, ll right, ll v, ll node) + +{ + if(left > right) + return; + + if(lazy[node]) prop(lo, hi, node); + + if(lo > right || hi < left) + return; + if(lo >= left && hi <= right) { + tree[node] += v; + lazy[node] += v; + return; + } + + ll mid = (lo + hi) / 2; + update(lo, mid, left, right, v, 2 * node); + update(mid + 1, hi, left, right, v, 2 * node + 1); + + tree[node] = max(tree[2 * node], tree[2 * node + 1]); +} + +ll query1(ll lo, ll hi, ll pos, ll node) + +{ + if(lazy[node]) prop(lo, hi, node); + + if(lo > pos || hi < pos) + return -2e18; + if(lo == hi) + return tree[node]; + + ll mid = (lo + hi) / 2; + ll p1 = query1(lo, mid, pos, 2 * node); + ll p2 = query1(mid + 1, hi, pos, 2 * node + 1); + + return max(p1, p2); +} + +ll query2(ll lo, ll hi, ll v, ll node) + +{ + if(lo == hi) + return lo; + + if(lazy[node]) prop(lo, hi, node); + + ll mid = (lo + hi) / 2, p1 = 0, p2 = 0; + + /*if(lazy[2 * node] != 0) prop(lo, mid, 2 * node); + if(lazy[2 * node + 1] != 0) prop(mid + 1, hi, 2 * node + 1);*/ + + if(tree[2 * node] >= v) + p1 = query2(lo, mid, v, 2 * node); + else if(tree[2 * node + 1] >= v) + p2 = query2(mid + 1, hi, v, 2 * node + 1); + else + return hi + 1; + + return max(p1, p2); +} + +int main() + +{ + ll n, q; + cin >> n >> q; + for(ll i = 1; i <= n; i++) { + scanf("%lld", &ara[i].val); + ara[i].id = i; + } + + sort(ara + 1, ara + n + 1); + build(1, n, 1); + + for(ll i = 1; i <= q; i++) { + ll t, x; + scanf("%lld %lld", &t, &x); + + if(t == 1) { + ll pos = posTree[x]; + + ll v = query1(1, n, pos, 1); + ll indx = query2(1, n, v + 1, 1) - 1; + + ll posAr = posArray[indx]; + + swap(posTree[x], posTree[posAr]); + swap(posArray[pos], posArray[indx]); + + update(1, n, indx, indx, 1, 1); + } + else if(t == 2) { + ll indx = query2(1, n, x, 1); + printf("%lld\n", n - indx + 1); + } + else { + ll indx = query2(1, n, x, 1); + update(1, n, indx, n, -1, 1); + } + + } + + return 0; +} diff --git a/spoj_cpp_clean/TESSER/TESSER-21.cpp b/spoj_cpp_clean/TESSER/TESSER-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0a3f2c6cd69d3a1841c038ef57f7a87ea0d34ac --- /dev/null +++ b/spoj_cpp_clean/TESSER/TESSER-21.cpp @@ -0,0 +1,42 @@ +/* + string processing + difficulty: none + date: none + by: @brpapa +*/ +#include +#include +using namespace std; + +bool strContemSub(string sub, string str) { + for (int i = 0; i <= (str.size() - sub.size()); i++) + if (sub == str.substr(i, sub.size())) + return true; + return false; +} + +int main() { + int t, n, anterior, aux; + char str[100001]; + string sub; + cin >> t; + while (t--) { + cin >> n; + cin >> anterior; + for (int i = 1; i < n; i++) { + cin >> aux; + + if (aux == anterior) + str[i - 1] = 'E'; + else if (aux > anterior) + str[i - 1] = 'G'; + else if (aux < anterior) + str[i - 1] = 'L'; + anterior = aux; + } + str[n] = '\0'; + cin >> sub; + printf("%s\n", strContemSub(sub, str) ? "YES" : "NO"); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/TEST/TEST-11.cpp b/spoj_cpp_clean/TEST/TEST-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc391de908b2c0bad8af33161aeccd9f7f89c3f9 --- /dev/null +++ b/spoj_cpp_clean/TEST/TEST-11.cpp @@ -0,0 +1,19 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include +using namespace std; + +int main() { + int input; + + do{ + cin >> input; + if(input != 42) + cout << input << endl; + }while(input != 42); + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/TEST/TEST-12.cpp b/spoj_cpp_clean/TEST/TEST-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6dd127bcdb0a4d23d36467b417a3adb2d7207c6b --- /dev/null +++ b/spoj_cpp_clean/TEST/TEST-12.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int main() +{ + ios::sync_with_stdio(false); + int p; + bool flag = false; + while (cin >> p) { + if (p == 42) + flag = true; + if (!flag) + cout << p << endl; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/TEST/TEST-16.cpp b/spoj_cpp_clean/TEST/TEST-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6379e2e5344305f5bd36eee07435ccb251dd774 --- /dev/null +++ b/spoj_cpp_clean/TEST/TEST-16.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +using namespace std; + +int main() { + // your code goes here + long long a; + while(1){ + cin>>a; + if(a==42){ + exit(0); + } + cout< +#define ll long long +#define mod 10000001 + +using namespace std; + +int main(){ + // your code goes here + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + while(1){ + cin>>t; + if(t==42){ + break; + } + cout< + +using namespace std; + +int main( int argc, char ** argv ) { +// freopen( "in.txt", "r", stdin ); +// freopen( "out.txt", "w", stdout ); + + int n; + while( scanf( "%d", &n )==1 && n != 42 ) { + printf( "%d\n", n ); + } + return 0; +} diff --git a/spoj_cpp_clean/TEST/TEST-7.cpp b/spoj_cpp_clean/TEST/TEST-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0bf0fe014bfc6b55483e8dc588c2eb8bd404cd3 --- /dev/null +++ b/spoj_cpp_clean/TEST/TEST-7.cpp @@ -0,0 +1,12 @@ +#include + +using namespace std; +int main () { + int n; n=0; + do { + cin >>n; + if (n!=42) + cout < +#include +int main() +{ + int n; + + while(1){ + scanf("%d", &n); + if(n==42) break; + printf("%d\n",n); + } + return 0; +} diff --git a/spoj_cpp_clean/TEST/TEST-9.cpp b/spoj_cpp_clean/TEST/TEST-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a63a0529933ea4f51d47623024f7d302ad2462f0 --- /dev/null +++ b/spoj_cpp_clean/TEST/TEST-9.cpp @@ -0,0 +1,29 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +int main() +{ + int num; + + scanf("%d", &num); + + while(num!=42) + { + printf("%d\n", num); + scanf("%d", &num); + } + + return 0; +} diff --git a/spoj_cpp_clean/TEST1/TEST1-2.cpp b/spoj_cpp_clean/TEST1/TEST1-2.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a9395e752620c8d5732536b591bd3422964897c5 --- /dev/null +++ b/spoj_cpp_clean/TEST1/TEST1-2.cpp @@ -0,0 +1,17 @@ +// https://www.spoj.com/problems/TESTINT/ +// TESTINT - Test 1 +#include +using std::cout; +using std::cin; + +int main() +{ + int a(201), b(201); + while (a >= 200 || b >= 200) { + cin >> a; + cin >> b; + } + cout << a + b; + + return 0; +} diff --git a/spoj_cpp_clean/THRBL/THRBL-21.cpp b/spoj_cpp_clean/THRBL/THRBL-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62730bed0075cb6d66515e688efaf7b0224f7f72 --- /dev/null +++ b/spoj_cpp_clean/THRBL/THRBL-21.cpp @@ -0,0 +1,70 @@ +/* + data structures > sparse table + difficulty: easy + date: 03/Oct/2020 + problem: range max query + by: @brpapa +*/ +#include +using namespace std; +#define pow2(i) (1 << (i)) +typedef long long ll; + +class sparse_table { + private: + vector A; int N; + vector> table; + + ll range_combination(ll a, ll b) { + return max(a, b); + } + + void build() { + int P = (int)floor(log2(N)); + table.assign(P + 1, vector(N)); + + for (int n = 0; n < N; n++) + table[0][n] = A[n]; + + for (int p = 1; p <= P; p++) + for (int n = 0; n + pow2(p) <= N; n++) { + table[p][n] = range_combination( + table[p - 1][n], + table[p - 1][n + pow2(p - 1)]); + } + } + + public: + sparse_table() {} + sparse_table(vector const &A) { + this->A = A; + N = (int)A.size(); + build(); + } + + int range_query(int l, int r) { + int size = r - l + 1; + int p = (int)floor(log2(size)); + return range_combination(table[p][l], table[p][r - pow2(p) + 1]); + } +}; + +int main() { + int N, M; + while (cin >> N >> M) { + vector A(N); for (ll &a : A) cin >> a; + + sparse_table st(A); + ll ans = 0; + + while (M--) { + int l , r; cin >> l >> r; l--; r--; + + if (r - l <= 1 || A[l] >= st.range_query(l + 1, r - 1)) + ans++; + } + + cout << ans << endl; + } + return 0; +} diff --git a/spoj_cpp_clean/TIPTOP/TIPTOP-3.cpp b/spoj_cpp_clean/TIPTOP/TIPTOP-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b573136ba9fd2003dcefad1bb027524d402cf371 --- /dev/null +++ b/spoj_cpp_clean/TIPTOP/TIPTOP-3.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sllu(x) scanf("%llu",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, i = 1; + unsigned long long n, root; + sd(t); + while(i <= t){ + scanf("%llu",&n); + root = sqrt(n); + if(root*root == n or (root-1)*(root-1) == n or (root+1)*(root+1) == n) + printf("Case %d: Yes\n",i); + else + printf("Case %d: No\n",i); + i++; + } + return 0; +} diff --git a/spoj_cpp_clean/TOANDFRO/TOANDFRO-11.cpp b/spoj_cpp_clean/TOANDFRO/TOANDFRO-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..570f4797eacbafcc50451ef8a642b053ddf6bd4b --- /dev/null +++ b/spoj_cpp_clean/TOANDFRO/TOANDFRO-11.cpp @@ -0,0 +1,46 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include + +using namespace std; + +int main(void) +{ + int n; + string input; + int len; + + cin >> n; + + while(n) + { + cin >> input; + len = input.length(); + + for(int i = 0; i < n; i++) + { + int j = i; + + while(j < len) + { + cout << input[j]; + + j = j + ((n-i)*2) - 1; + + if(j < len) + cout << input[j]; + + j = j + (i * 2) + 1; + } + } + + cout << endl; + + cin >> n; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/TOANDFRO/TOANDFRO-16.cpp b/spoj_cpp_clean/TOANDFRO/TOANDFRO-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9330721dc4f8ebc7f5810470e6f1ce028519ee12 --- /dev/null +++ b/spoj_cpp_clean/TOANDFRO/TOANDFRO-16.cpp @@ -0,0 +1,52 @@ +#include + +#define ll long long +#define ull unsigned long long +#define pb push_back +#define mp make_pair +#define tr(c,it) for(auto it = (c).begin(); it != (c).end(); it++) + +using namespace std; + +void solve(ll c){ + ll r; + char a[1090][1090]; + string s; + cin>>s; + ll n = s.length(); + r = n/c; + ll k=0; + for(ll i=0;i=0;j--){ + a[i][j]=s[k]; + k++; + } + } + for(ll i=0;i>t; + while(true){ + cin>>c; + if(c==0){ + break; + } + solve(c); + } + return 0; +} diff --git a/spoj_cpp_clean/TOANDFRO/TOANDFRO-19.cpp b/spoj_cpp_clean/TOANDFRO/TOANDFRO-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1086e30047e987832fd623270e94897181bab4d --- /dev/null +++ b/spoj_cpp_clean/TOANDFRO/TOANDFRO-19.cpp @@ -0,0 +1,51 @@ +#include +#define endl "\n" +#define ll long LONG +#define mod 1000000007 +#define MAX 100001 + +using namespace std; + +void reverse_string(string &str){ + int n = str.length(); + + for (int i = 0; i < n / 2; i++){ + swap(str[i], str[n - i - 1]); + } +} + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int counter=0; + while(true){ + int N;cin>>N; + if(N==0){ + return 0; + } + if(counter!=0){ + cout<>str; + counter++; + int row = (str.length()/N); + char a[row][N]; + for(int i=0; i +#include +//#include + +using namespace std; + +int main() +{ + char input[200]; + int i,j,coloumn,row,length,id,index; + cin>>coloumn; + while(coloumn) + { + cin>>input; + length=strlen(input); + row=length/coloumn; + char mat[row][coloumn]; + //char* mat = new char[row][coloumn]; + id=0;index=0; + for(i=0;i=0;j--) + { + mat[i][j]=input[index]; + index++; + } + id=0; + } + } + for(j=0;j>coloumn; + } + return 0; + +} diff --git a/spoj_cpp_clean/TOANDFRO/TOANDFRO-9.cpp b/spoj_cpp_clean/TOANDFRO/TOANDFRO-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..177051aa5d52555570929022df810ff7a331bb77 --- /dev/null +++ b/spoj_cpp_clean/TOANDFRO/TOANDFRO-9.cpp @@ -0,0 +1,66 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +string S; +int col; +int rows; + +inline bool init() +{ + scanf("%d", &col); + + if(col == 0) + return 0; + + cin>>S; + + rows = S.size() / col; + + return 1; +} + +inline string solve() +{ + int i, j; + vector< string > A(rows); + string res = ""; + + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 10005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX], order; +setnextNode; +int indeg[MAX]; +int cnt; +void topSort() { + int u, v; + cnt = 0; + + while (!nextNode.empty() ) { + u = *nextNode.begin(); + nextNode.erase (nextNode.begin() ); + order.pb (u); + cnt++; + + for (int i = 0; i != (int) adj[u].size(); i++) { + v = adj[u][i]; + indeg[v]--; + + if (!indeg[v]) { + nextNode.insert (v); + } + } + } +} + +int main() { + __FastIO; + int n, m; + cin >> n >> m; + + for (int i = 0; i < m; i++) { + int u, v; + cin >> u >> v; + adj[u].pb (v); + indeg[v] += 1; + } + + for (int i = 1; i <= n; i++) { + if (!indeg[i]) { + nextNode.insert (i); + } + } + + topSort(); + + if (cnt != n) { + cout << "Sandro fails.\n"; + + } else { + for (int i = 0; i < n; i++) { + if (i != 0) cout << " "; + + cout << order[i]; + } + + nl; + } + + return 0; +} diff --git a/spoj_cpp_clean/TOUR/TOUR-3.cpp b/spoj_cpp_clean/TOUR/TOUR-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55c86b8b1a5b2d55e2813245dce78d3627848f53 --- /dev/null +++ b/spoj_cpp_clean/TOUR/TOUR-3.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t, no_of_rounds, n, i, j, temp, count; + set data[1001]; + int no_opp[1001]; + sd(t); + + while(t--){ + sd(n); + no_of_rounds = ceil(log(n)/log(2)), count = 0; + + for(i = 1; i <= n; i++){ + sd(no_opp[i]); + for(j = 1; j <= no_opp[i]; j++){ + sd(temp); + data[temp].insert(i); + } + } + + /*cout<::iterator it = data[i].begin(); + while(it != data[i].end()){ + cout<<*it<<" "; + it++; + } + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define MAXN 200007 +#define ll long long +using namespace std; + +const ll MOD = 1e9 + 7; + +int N; +int tree[MAXN]; +int A[MAXN]; + +inline void update(int pos) +{ + for (; pos <= N; pos += pos & (-pos)) + tree[pos]++; +} + +inline int query(int pos) +{ + int rval = 0; + + for (; pos > 0; pos -= pos & (-pos)) + rval += tree[pos]; + + return rval; +} + +inline void init() +{ + int i; + + cin >> N; + + memset(tree, 0, sizeof(tree)); + + for (i = N; i >= 1; i--) + cin >> A[i]; +} + +inline ll solve() +{ + ll rval = 1; + ll fact = 1; + int i; + + for (i = 1; i <= N; i++) + { + if (i != 1) + fact *= (i - 1); + + fact %= MOD; + rval += fact * query(A[i]); + rval %= MOD; + update(A[i] + 1); + } + + return rval; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + int T; + + cin >> T; + + while (T--) + { + init(); + cout << solve() << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/TRANCLS/TRANCLS-9.cpp b/spoj_cpp_clean/TRANCLS/TRANCLS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ca8892992e95047f0b6b6b69d96b6eabd746141 --- /dev/null +++ b/spoj_cpp_clean/TRANCLS/TRANCLS-9.cpp @@ -0,0 +1,73 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +using namespace std; + +vector< bitset<100> > A, TC; +int N; + +inline void init() +{ + int i, f, s; + + scanf("%d", &N); + + A.clear(); + A.resize(N); + + for (i = 0; i < N; i++) + { + scanf("%d %d", &f, &s); + A[f][s] = 1; + } +} + +inline int solve() +{ + int i, j, k, rval = 0; + + TC = A; + + for (k = 0; k < N; k++) + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + if (!TC[i][j] && TC[i][k] && TC[k][j]) + TC[i][j] = 1; + + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + if (!A[i][j] && TC[i][j]) + rval++; + + return rval; +} + +int main() +{ + int T, t; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + for (t = 1; t <= T; t++) + { + init(); + printf("Case #%d: %d\n", t, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/TRAVERSE/TRAVERSE-7.cpp b/spoj_cpp_clean/TRAVERSE/TRAVERSE-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..533fe46f9fe68bbf6ce0574d776f3691428bcedc --- /dev/null +++ b/spoj_cpp_clean/TRAVERSE/TRAVERSE-7.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int n,aux; +char matriz[11][11]; +int Memo[11][11]; + +bool valido(int x,int y) +{ + return (x>=0 && x=0 && y +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; i++) +#define for0(i, stop) for(ll i = 0; i < stop; i++) +#define rep1(i, start) for(ll i = start; i >= 1; i--) +#define rep0(i, start) for(ll i = (start-1); i >= 0; i--) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +const ll sz = 1e5 + 10; +vector g[2][sz]; +ll cnt[2][100009], lvl[sz], n; +const ll base = 10001533, mod = 2000004143; + +ll hashDfs(ll u, ll p, ll id) +{ + ll h = 1; + deque hashSub; + for(ll v: g[id][u]) { + if(v == p) + continue; + + hashSub.pb(hashDfs(v, u, id)); + } + sort(hashSub.begin(), hashSub.end()); + + for(ll val: hashSub) + h = ((h * base) ^ val) % mod; + + return h; +} + +pll findCenter(ll id) +{ + queue q; + ll mxl = 0; + for1(i, n) { + if(cnt[id][i] == 1) { + q.push(i); + lvl[i] = 0; + } + } + while(!q.empty()) { + ll u = q.front(); + q.pop(); + + for(ll v: g[id][u]) { + cnt[id][v]--; + if(cnt[id][v] == 1) { + q.push(v); + lvl[v] = lvl[u] + 1; + mxl = max(mxl, lvl[v]); + } + } + } + + pll ret = mp(-1, -1); + for1(i, n) { + if(lvl[i] == mxl) { + if(ret.first == -1) + ret.first = i; + else + ret.second = i; + } + } + return ret; +} + +ll getHash(ll id) +{ + pll cen1 = findCenter(id); + ll root = cen1.first; + if(cen1.second != -1) { + root = 0; + g[id][0].pb(cen1.first), g[id][0].pb(cen1.second); + + for(ll &v : g[id][cen1.first]) + if(v == cen1.second) v = 0; + for(ll &v : g[id][cen1.second]) + if(v == cen1.first) v = 0; + + } + return hashDfs(root, -1, id); +} + +int main() + +{ + ll t; + cin >> t; + while(t--) { + sl(n); + if(n == 1) { + pf("YES\n"); + continue; + } + + ll u, v; + for1(i, n-1) { + sl(u), sl(v); + g[0][u].pb(v), g[0][v].pb(u); + cnt[0][u]++, cnt[0][v]++; + } + for1(i, n-1) { + sl(u), sl(v); + g[1][u].pb(v), g[1][v].pb(u); + cnt[1][u]++, cnt[1][v]++; + } + + ll h1 = getHash(0), h2 = getHash(1); + if(h1 == h2) + pf("YES\n"); + else + pf("NO\n"); + + for0(i, (n+1)) { + g[0][i].clear(), g[1][i].clear(); + cnt[0][i] = cnt[1][i] = 0; + } + } + + return 0; +} diff --git a/spoj_cpp_clean/TRGRID/TRGRID-4.cpp b/spoj_cpp_clean/TRGRID/TRGRID-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76cd71493852a549ab2efb1431135f289b58b802 --- /dev/null +++ b/spoj_cpp_clean/TRGRID/TRGRID-4.cpp @@ -0,0 +1,113 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + s(T); + while(T--) + { + s(n); s(m); + if(n%2==0) + { + if(n<=m) + cout<<"L\n"; + else if(m%2==0) + cout<<"U\n"; + else + cout<<"D\n"; + + } + else + { + if(n<=m) + cout<<"R\n"; + else if(m%2==0) + cout<<"U\n"; + else + cout<<"D\n"; + } + } + return 0; +} diff --git a/spoj_cpp_clean/TRICOUNT/TRICOUNT-11.cpp b/spoj_cpp_clean/TRICOUNT/TRICOUNT-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..529875f501609f5889cc711f382f9070905965d4 --- /dev/null +++ b/spoj_cpp_clean/TRICOUNT/TRICOUNT-11.cpp @@ -0,0 +1,25 @@ +/* + * Written by Nitin Kumar Maharana + * nitin.maharana@gmail.com + */ + +#include + +using namespace std; + +int main(void) +{ + int t; + long long int n; + + cin >> t; + + while(t--) + { + cin >> n; + + cout << ((n * (n + 2ll) * (2ll * n + 1ll)) / 8ll) << endl; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/TRICOUNT/TRICOUNT-19.cpp b/spoj_cpp_clean/TRICOUNT/TRICOUNT-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0991b909177b4e25454607053bce6669a7500807 --- /dev/null +++ b/spoj_cpp_clean/TRICOUNT/TRICOUNT-19.cpp @@ -0,0 +1,14 @@ +#include +#define ll long long +using namespace std; +int main(){ + ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + ll t;cin>>t;while(t--){ + ll N;cin>>N; + ll ans; + ans = (ll)((N*(N+2)*((2*N)+1))/8); + cout< +#include + +using namespace std; + +int main() +{ + long long int test,n; + scanf("%lld",&test); + while(test--) + { + scanf("%lld",&n); + printf("%lld\n",(n*(n+2)*(2*n+1)/8)); + } + return 0; +} diff --git a/spoj_cpp_clean/TRIGALGE/TRIGALGE-20.cpp b/spoj_cpp_clean/TRIGALGE/TRIGALGE-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a8b023dcc4bd61d5d3e2cccd5d261c56c95fce2 --- /dev/null +++ b/spoj_cpp_clean/TRIGALGE/TRIGALGE-20.cpp @@ -0,0 +1,92 @@ +/** + * Author : Mr_Sohel + * Task : + * Algo : +**/ +#include + +#define endl '\n' +#define sqr(x) (x) * (x) +#define gcd(x,y) __gcd(x,y) +#define lcm(x,y) ((x/gcd(x,y)) * y) +#define pb push_back +#define pf push_front +#define mk make_pair +#define fi first +#define se second +#define sz(x) (int)x.size() +#define all(x) (x).begin(),(x).end() +#define rall(x) (x).rbegin(),(x).rend() +#define prec(x) fixed<; +using pll = pair; + +const db PI = acos((db) - 1); +const int MOD = 1e9 + 7; +const ll INF = 2e18 + 1; +const ld EPS = 1e-7; +const int MX = 2e6; + +#ifdef LOCAL +#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) +template < typename Arg1 > +void __f(const char* name, Arg1&& arg1) { + cerr << name << " = " << arg1 << endl; +} +template < typename Arg1, typename... Args> +void __f(const char* names, Arg1&& arg1, Args&&... args) { + const char* comma = strchr(names + 1, ','); + cerr.write(names, comma - names) << " = " << arg1 << " | "; + __f(comma + 1, args...); +} +#else +#define debug(...) +#endif + +ld calc(ld a, ld b, ld x, ld c) { + ld ans = (a * x) + (b * sin(x)); + ans = ans - c; + return ans; +} + +int main() { + +#ifdef LOCAL + clock_t tStart = clock(); + freopen("in.txt", "r", stdin); + freopen("out.txt", "w", stdout); +#endif + unsyncIO; + int t; cin >> t; + while (t--) { + ld a, b, c; + cin >> a >> b >> c; + ld lo = 0.0, hi = c, res, mid; + while (lo <= hi) { + mid = (lo + hi) / 2.0; + res = calc(a, b, mid, c); + if (res > EPS) { + hi = mid - EPS; + } else { + lo = mid + EPS; + } + } + cout << prec(6) << mid << endl; + } +#ifdef LOCAL + cerr << "\nRuntime: " << (ld) (clock() - tStart) / CLOCKS_PER_SEC << " Seconds" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/TRIGALGE/TRIGALGE-3.cpp b/spoj_cpp_clean/TRIGALGE/TRIGALGE-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1065431a5929284c6fcd8c6fb8ab7d9bc26581dc --- /dev/null +++ b/spoj_cpp_clean/TRIGALGE/TRIGALGE-3.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t; + double x, a, b, c; + sd(t); + while(t--){ + scanf("%lf %lf %lf",&a,&b,&c); + x = c/a; + for(int i = 0; i < 10; i++){ + x = x - (((a*x) + (b*sin(x)) - c)/(a+(b*cos(x)))); + } + printf("%.6lf\n",x); + } + return 0; +} diff --git a/spoj_cpp_clean/TRISQ/TRISQ-5.cpp b/spoj_cpp_clean/TRISQ/TRISQ-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be37cb516de71d9ada5871012179f2f1d8aa755b --- /dev/null +++ b/spoj_cpp_clean/TRISQ/TRISQ-5.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +int main() +{ + int test,num,i; + scanf("%d",&test); + while(test--) + { + scanf("%d",&num); + if(num<4) + { + printf("0\n"); + continue; + } + i=(num/2)-1; + //j=(i*(1+i))/2; + printf("%d\n",(i*(1+i))/2); + } + return 0; + +} diff --git a/spoj_cpp_clean/TRSTAGE/TRSTAGE-3.cpp b/spoj_cpp_clean/TRSTAGE/TRSTAGE-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40d7356fae58e8739f3bcd0548ba9fa6d22c8286 --- /dev/null +++ b/spoj_cpp_clean/TRSTAGE/TRSTAGE-3.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int n, m, p, a, b, x, y, z, tickets[8], graph[31][31], INF = 10000000; + +double dp[31][256], eps = 1e-9; + +void calculate(){ + double time, ans = INF; + int MASK = 1<>ticket)&1 != 0) continue; + if(graph[i][j] == INF) continue; + if(abs(dp[i][mask]-INF) <= eps ) continue; + time = dp[i][mask] + (graph[i][j]/(1.0*tickets[ticket])); + if(dp[j][mask|(1< time){ + dp[j][mask|(1< +#include +#include + +int treat[2001]; +int cache[2001][2001]; +int n; + +// WRONG +// int greedy(int left, int right, int age) { +// if (left == right) return age * treat[left]; +// if (treat[left] < treat[right]) +// return (age * treat[left]) + greedy(left + 1, right, age + 1); +// else +// return (age * treat[right]) + greedy(left, right - 1, age + 1); +// } + +// int rec(int left, int right, int age) { +// if (left == right) return age * treat[left]; + +// return std::max( +// (age * treat[left]) + rec(left + 1, right, age + 1), +// (age * treat[right]) + rec(left, right - 1, age + 1) +// ); +// } + +int memo(int left, int right) { + int age = n - (right - left + 1) + 1; + + if (left == right) return age * treat[left]; + if (cache[left][right] != -1) return cache[left][right]; + + return cache[left][right] = std::max( + (age * treat[left]) + memo(left + 1, right), + (age * treat[right]) + memo(left, right - 1) + ); +} + +int main(int argc, char** argv) { + + scanf("%d", &n); + + int i; + for (i = 0; i < n; ++i) { + scanf("%d", &treat[i]); + } + + memset(cache, -1, sizeof(int) * 2001 * 2001); + + printf("%d", memo(0, n-1)); + return 0; +} diff --git a/spoj_cpp_clean/TRT/TRT-16.cpp b/spoj_cpp_clean/TRT/TRT-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec46cb9384e27130977fae95a5deb7b215bb4868 --- /dev/null +++ b/spoj_cpp_clean/TRT/TRT-16.cpp @@ -0,0 +1,62 @@ +// [amitdu6ey] +// g++ -std=c++11 -o2 -Wall filename.cpp -o filename +#include +#define hell 1000000009 +#define bug1(x) cout<<"$ "<=b;i--) +#define loop(i,a,b) for(long long i=a; i +#define vvbool vector< vector > +#define vchar vector +#define vi vector +#define vvi vector< vector > +#define pll pair +#define vll vector +#define vvl vector< vector > +#define ninf numeric_limits::min() +#define pinf numeric_limits::max() +#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles +using namespace std; + +ll n; +ll a[2009]; +ll dp[2009][2009]; + +ll f(ll l, ll r){ + if(l==r) return n*a[l]; + if(l>r) return 0; + if(dp[l][r]!=-1) return dp[l][r]; + return dp[l][r]=max( (n-(r-l))*a[l]+f(l+1,r), ((n-(r-l))*a[r]+f(l,r-1)) ); +} + +void solve(){ + cin>>n; + loop(i,0,n) cin>>a[i]; + loop(i,0,n){ + loop(j,0,n){ + dp[i][j]=-1; + } + } + cout<>test_cases; + while(test_cases--){ + solve(); + } + return 0; +} diff --git a/spoj_cpp_clean/TRT/TRT-17.cpp b/spoj_cpp_clean/TRT/TRT-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de4bcc955fef24d4e896218d52d66bf72e7bdf79 --- /dev/null +++ b/spoj_cpp_clean/TRT/TRT-17.cpp @@ -0,0 +1,54 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define int long long +using namespace std; +/** + * by:Ahmed Sakr (sakr_) with AZA ; + * freopen("input.txt","r",stdin); + * freopen("output.txt","w",stdout); + * __builtin_popcountll(); +**/ + +int n; +vector arr; + +int dp[2005][2005]; + +int solve(int l,int r,int days) { + + if (l > r) { + return 0; + } + + int &ans = dp[l][r]; + if(ans!=-1) return ans; + + ans = max(days * arr[l] + solve(l + 1, r, days + 1), + days * arr[r] + solve(l, r - 1, days + 1)); + + return ans; +} + +void solveit() { + + cin>>n; + arr=vector(n+2); + memset(dp,-1,sizeof dp); + + for(int i=0;i>arr[i]; + } + + cout<>t; + while (t--) { + solveit(); + } +} \ No newline at end of file diff --git a/spoj_cpp_clean/TRT/TRT-21.cpp b/spoj_cpp_clean/TRT/TRT-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72085511250c6f00664fc932e4a5ee277426d574 --- /dev/null +++ b/spoj_cpp_clean/TRT/TRT-21.cpp @@ -0,0 +1,35 @@ +/* + dynamic programming + difficulty: easy + date: 29/Apr/2020 + hint: don't memoize the current day + by: @brpapa +*/ +#include +using namespace std; + +vector A; int N; + +int memo[2020][2020]; +int dp(int l, int r) { + int d = l+(N-r); // dia atual + + if (l == r) return A[l]*d; + + int &ans = memo[l][r]; + if (ans != -1) return ans; + + return ans = max( + A[l]*d + dp(l+1, r), + A[r]*d + dp(l, r-1) + ); +} + +int main() { + cin >> N; + A.resize(N); for (int &a: A) cin >> a; + + memset(memo, -1, sizeof memo); + cout << dp(0, N-1) << endl; + return 0; +} diff --git a/spoj_cpp_clean/TRT/TRT-4.cpp b/spoj_cpp_clean/TRT/TRT-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5c7c3c7938054bbd4fb5cb29c06666694525ab0 --- /dev/null +++ b/spoj_cpp_clean/TRT/TRT-4.cpp @@ -0,0 +1,111 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int a[2009]; +int mat[2009][2009]={0}; +int fun(int year, int be, int en) +{ + if(be>en ) + return 0; + if(mat[be][en]!=0) + return mat[be][en]; + + return mat[be][en]=maX( year*a[be]+fun(year+1, be+1, en), + year*a[en]+fun(year+1,be, en-1) + ); +} + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + s(n); + FOR(i,0,n) s(a[i]); + + cout< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int V[2001]; +int DP[2001][2001]; + +int main() +{ + int i, N, j, k; + //freopen("input.txt","r",stdin); + + scanf("%d", &N); + + scanf("%d", &V[1]); + DP[1][1]=V[1]*N; + + for(i=2; i<=N; i++) + { + scanf("%d", &V[i]); + DP[i][i]=V[i]*N; + DP[i-1][i]=min(V[i], V[i-1])*(N-1) + max(V[i], V[i-1])*N; + } + + for(k=3; k<=N; k++) + for(i=1; i<=N-k+1; i++) + { + j=i+k-1; + DP[i][j]=max(V[i]*(N-(j-i)) + DP[i+1][j], V[j]*(N-(j-i)) + DP[i][j-1]); + } + + printf("%d\n", DP[1][N]); + return 0; +} diff --git a/spoj_cpp_clean/TRYCOMP/TRYCOMP-0.cpp b/spoj_cpp_clean/TRYCOMP/TRYCOMP-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..71bb2d1b6320a7ccc67fd54bf19dbd10ae2d7ddc --- /dev/null +++ b/spoj_cpp_clean/TRYCOMP/TRYCOMP-0.cpp @@ -0,0 +1,170 @@ +#include + +#define FI first +#define SE second + +typedef long long ll; + +struct Trie { + const static ll ALPHA = 26; + const static char ALPHA_BEG = 'a'; + + struct TrieNode { + ll count; + char value; + + ll all_count; + + std::vector next; + TrieNode* parent; + + TrieNode* max; + ll max_len; + + TrieNode(const char& val, TrieNode* p = nullptr) { + value = val; + count = 0; + next.assign(ALPHA, nullptr); + parent = p; + + max = nullptr; + max_len = 0; + + all_count = 0; + } + + ~TrieNode(void) { + for (auto& e : next) + if (e != nullptr) + delete e; + } + + /* returns how many words have `this` as prefix*/ + ll count_all(void) { + return all_count; + } + + // TrieNode* do_max(void) { + + // } + }; + + /* the ROOT */ + TrieNode* root; + + Trie(void) { + root = new TrieNode('$', nullptr); + } + + ~Trie(void) { + delete root; + } + + /* inserts a string. makes nodes as needed. */ + void insert(const std::string& s) { + TrieNode* curr = this->root; + + for (ll i = 0; i < s.length(); ++i) { + ll tmp = std::tolower(s[i]) - ALPHA_BEG; + if (curr->next[tmp] == nullptr) + curr->next[tmp] = new TrieNode(tmp + ALPHA_BEG, curr); + curr = curr->next[tmp]; + } + + auto end = curr; + ll len = 0; + curr->count++; + + for(; curr != nullptr; curr = curr->parent, ++len) { + if (curr->max == nullptr || curr->max->count <= end->count) { + curr->max = end; + curr->max_len = len; + } + curr->all_count++; + } + } + + /* removes a string if it was there. */ + void remove(const std::string& s) { + TrieNode* curr = find_end(s); + if (curr == nullptr) return; + + curr->count--; + + for (; curr != nullptr; curr = curr->parent) + curr->all_count--; + } + + /* finds the last node of entry of `s` if it's there + returns nullptr otherwise. */ + TrieNode* find_end(const std::string& s) const { + TrieNode* curr = this->root; + + for (ll i = 0; i < s.length(); ++i) { + ll tmp = std::tolower(s[i]) - ALPHA_BEG; + if (curr->next[tmp] == nullptr) + return nullptr; + curr = curr->next[tmp]; + } + + return curr; + } + + std::string build(const TrieNode* end) const { + std::stringstream ss; + for (auto i = end; i != nullptr && i->parent != nullptr; i = i->parent) + ss << i->value; + std::string s = ss.str(); + std::reverse(s.begin(),s.end()); + return s; + } + + /* returns how many instances of `s` are in trie. */ + ll count(const std::string& s) const { + TrieNode* ret = find_end(s); + return ret == nullptr ? 0 : ret->count; + } + + /* returns how many strings + are in the entire trie + with root as prefix. */ + ll count_all(void) const { + return this->root->count_all(); + } + + /* returns true if the string is in the trie. */ + bool find(const std::string& s) const { + return count(s) > 0; + } +}; + + +int main(void) { + std::ios::sync_with_stdio(false); std::cin.tie(nullptr); + + Trie tr; + + ll n; std::cin >> n; + std::vector vec(n); + for (auto& e : vec) + std::cin >> e; + + std::sort(vec.rbegin(),vec.rend()); + for (auto& e : vec) + tr.insert(e); + + std::string s; + ll q; std::cin >> q; + while (q--) { + std::cin >> s; + auto node = tr.find_end(s); + if (node == nullptr) { + std::cout << "-1"; + } else { + std::cout << tr.build(node->max) << " " << node->max->count; + } + std::cout << "\n"; + } + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/TSORT/TSORT-3.cpp b/spoj_cpp_clean/TSORT/TSORT-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..282878f2d0fa188b7df292e0f0becd1bdffa605c --- /dev/null +++ b/spoj_cpp_clean/TSORT/TSORT-3.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int t; + int array[1000000]; + sd(t); + for(int i = 0; i < t; i++) + sd(array[i]); + sort(array, array+t); + for(int i = 0; i < t; i++) + print(array[i]); + return 0; +} diff --git a/spoj_cpp_clean/TSUM/TSUM-14.cpp b/spoj_cpp_clean/TSUM/TSUM-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d34dbdd4e457a6921a3e0d095ecf9a433d7ddf7 --- /dev/null +++ b/spoj_cpp_clean/TSUM/TSUM-14.cpp @@ -0,0 +1,172 @@ +// #pragma GCC optimize("Ofast,unroll-loops") +// #pragma GCC target("avx,avx2,fma") + +#include +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define all(v) v.begin(), v.end() +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; ++i) +#define for0(i, stop) for(ll i = 0; i < stop; ++i) +#define rep1(i, start) for(ll i = start; i >= 1; --i) +#define rep0(i, start) for(ll i = (start-1); i >= 0; --i) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define EL '\n' +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +/*** + * Multiply (7x^2 + 8x^1 + 9x^0) with (6x^1 + 5x^0) + * ans = 42x^3 + 83x^2 + 94x^1 + 45x^0 + * A = {9, 8, 7} + * B = {5, 6} + * V = multiply(A,B) + * V = {45, 94, 83, 42} +***/ + +/*** Tricks + * Use vector < bool > if you need to check only the status of the sum + * Use bigmod if the power is over same polynomial && power is big + * Use long double if you need more precision + * Use long long for overflow +***/ +typedef vector vi; +const double PI = 2.0 * acos(0.0); +using cd = complex; + +void fft(vector & a, bool invert = 0) + +{ + int n = a.size(); + + for (int i = 1, j = 0; i < n; i++) { + int bit = n >> 1; + for (; j & bit; bit >>= 1) + j ^= bit; + j ^= bit; + + if (i < j) + swap(a[i], a[j]); + } + + for (int len = 2; len <= n; len <<= 1) { + double ang = 2 * PI / len * (invert ? -1 : 1); + cd wlen(cos(ang), sin(ang)); + for (int i = 0; i < n; i += len) { + cd w(1); + for (int j = 0; j < len / 2; j++) { + cd u = a[i+j], v = a[i+j+len/2] * w; + a[i+j] = u + v; + a[i+j+len/2] = u - v; + w *= wlen; + } + } + } + + if (invert) { + for (cd & x : a) + x /= n; + } +} + +void ifft(vector & p) + +{ + fft(p, 1); +} + +vi multiply(vi const& a, vi const& b) +{ + + vector fa(a.begin(), a.end()), fb(b.begin(), b.end()); + int n = 1; + while (n < a.size() + b.size()) + n <<= 1; + fa.resize(n); + fb.resize(n); + + fft(fa); + fft(fb); + for (int i = 0; i < n; i++) + fa[i] *= fb[i]; + ifft(fa); + + vi result(n); + for (int i = 0; i < n; i++) + result[i] = round(fa[i].real()); + + return result; +} + +const ll MAX_N = 2e4 + 10, offset = 2e4, sz = 4e4 + 10; +vi num1, num2, res1, res2; + +ll ara[sz], cntDouble[2*MAX_N + 2*offset], cntTriple[3*MAX_N + 3*offset]; + +int main() +{ + num1.resize(MAX_N + offset, 0), num2.resize(2*MAX_N + 2*offset, 0); + + ll n; + cin >> n; + + for1(i, n) { + sl(ara[i]); + ara[i] += offset; + + ll doubl = 2 * ara[i], tripl = 3*ara[i]; + + num1[ara[i]]++; + num2[doubl]++; + + cntDouble[doubl]++, cntTriple[tripl]++; + } + + res1 = multiply(num1, num1); + + for0(i, res1.size()) { + + if(i < 2*MAX_N + 2*offset) + res1[i] -= cntDouble[i]; + + assert(res1[i] % 2 == 0); + res1[i] /= 2; + } + + //cout << res1[1] << " || " << res1[2] << endl; + res1 = multiply(res1, num1); + + res2 = multiply(num2, num1); + + for0(i, res2.size()) { + if(i < 3*MAX_N + 3*offset) + res2[i] -= cntTriple[i]; + } + + for0(i, res1.size()) { + if(i >= res2.size()) break; + + assert(res1[i] >= res2[i]); + res1[i] -= res2[i]; + } + + for0(i, res1.size()) { + if(res1[i]) + pf("%lld : %lld\n", i-3*offset, res1[i] / 3); + } + + return 0; diff --git a/spoj_cpp_clean/TV/TV-10.cpp b/spoj_cpp_clean/TV/TV-10.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7b8b5ab094bbd34050eb3a4d0e228a7369bc46fa --- /dev/null +++ b/spoj_cpp_clean/TV/TV-10.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + int h, w, x, y, cx, cy; + int map[1001][1001]; + while(cin >> h >> w && !(h == 0 && w == 0)) + { + for(y = 0; y < h; y++) + for(x = 0; x < w; x++) + cin >> map[x][y]; + cx = cy = 0; + while(cin >> x >> y && !(x == 0 && y == 0)) + { + cx += x; + cy += y; + } + for(y = 1; y <= h; y++) + { + for(x = 1; x <= w; x++) + { + cout << map[(x + cx) % w][(y + cy) % h] << ' '; + } + cout << '\n'; + } + } +} diff --git a/spoj_cpp_clean/TWENDS/TWENDS-4.cpp b/spoj_cpp_clean/TWENDS/TWENDS-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e6da2ffdc7370a9b8b533322fc816790afc8b42 --- /dev/null +++ b/spoj_cpp_clean/TWENDS/TWENDS-4.cpp @@ -0,0 +1,127 @@ +// Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0; i33) + { + h=h*10+c-'0'; + c=getchar();// getchar_unlocked(); + } + return h; +} + +int n, m; +int dp[1003][1003]; +int a[1003]; +int calc(int st, int en) +{ + if(st>=en) return 0; + + if(dp[st][en]!=-1) + return dp[st][en]; + + int one, two; + if(a[st+1]>=a[en]) + one=a[st]-a[st+1]+calc(st+2, en ); + else + one=a[st]-a[en]+ calc(st+1, en-1); + + if(a[en-1]>a[st]) + two=a[en]-a[en-1]+calc(st, en-2); + else + two=a[en]-a[st]+calc(st+1, en-1); + + return dp[st][en]=maX(one, two); +} + +int main() +{ + //freopen("output.txt","w",stdout); + int T, g=1; + + while(cin>>n) + { + if(n==0)break; + fill(dp, -1); + FOR(i,0,n) + s(a[i]); + cout<<"In game "< Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int N; +int DP[10000][10000]; +int A[10000]; +int sum; + +inline bool max_pos(int left, int right) +{ + if(left>=right) + return 0; + return 1; +} + + +bool init() +{ + int i; + + scanf("%d", &N); + sum=0; + if(N==0) + return 0; + + for(i=1; i<=N; i++) + { + scanf("%d", &A[i]); + sum+=A[i]; + } + + return 1; +} + +int main() +{ + int i, j, L, t; + int left, right; + + //freopen("input.txt","r",stdin); + + for(t=1; init(); t++) + { + for(i=2; i<=N; i++) + DP[i-1][i]=max(A[i], A[i-1]); + + for(L=4; L<=N; L+=2) + { + for(i=1; i<=N; i++) + { + j=i+L-1; + + left=A[i]; + + if(max_pos(A[i+1], A[j])==0) + left+=DP[i+2][j]; + else + left+=DP[i+1][j-1]; + + right=A[j]; + + if(max_pos(A[i], A[j-1])==0) + right+=DP[i+1][j-1]; + else + right+=DP[i][j-2]; + + DP[i][j]=max(left, right); + } + } + + printf("In game %d, the greedy strategy might lose by as many as %d points.\n", t, DP[1][N]-(sum-DP[1][N])); + } + + return 0; +} diff --git a/spoj_cpp_clean/TWOKINGS/TWOKINGS-14.cpp b/spoj_cpp_clean/TWOKINGS/TWOKINGS-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eb4b32af7a0d9caa9791facb1ca83eec3acf4e4 --- /dev/null +++ b/spoj_cpp_clean/TWOKINGS/TWOKINGS-14.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +#define ll long long + +char grid[12][12]; +bool vis[12][12]; +ll n, m; +ll row[] = {1, -1, 0, 0, -1, -1, 1, 1}; +ll col[] = {0, 0, 1, -1, -1, 1, -1, 1}; + +void dfs(ll r, ll c, char ch) + +{ + for(ll i = 0; i < 8; i++) { + ll tr = r + row[i]; + ll tc = c + col[i]; + + if(tr > 0 && tr <= n && tc > 0 && tc <= m && vis[tr][tc] == 0 && grid[tr][tc] == ch) { + vis[tr][tc] = 1; + dfs(tr, tc, ch); + } + } +} + +int main() + +{ + ll t, caseno = 0; + cin >> t; + while(t--) { + memset(vis, 0, sizeof(vis)); + scanf("%lld %lld", &n, &m); + for(ll i = 1; i <= n; i++) + scanf("%s", grid[i] + 1); + + ll cnt = 0, cnt2 = 0; + for(ll i = 1; i <= n; i++) + { + for(ll j = 1; j <= m; j++) + { + if(vis[i][j]) + continue; + if(grid[i][j] == 'X') + cnt++; + else + cnt2++; + dfs(i, j, grid[i][j]); + } + } + + if(cnt >= cnt2) + printf("X\n"); + else + printf("O\n"); + } + + return 0; +} + diff --git a/spoj_cpp_clean/TWOSQRS/TWOSQRS-4.cpp b/spoj_cpp_clean/TWOSQRS/TWOSQRS-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be9e02ac55420f418737b73d494a9bcc896ff68e --- /dev/null +++ b/spoj_cpp_clean/TWOSQRS/TWOSQRS-4.cpp @@ -0,0 +1,114 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T, cnt, flag=0; + long long no;//, tmp; + //prime(); + s(T); + //FOR(i,0,20) + //error(pm[i]); + while(T--) + { + sl(no); + flag=0; + if(no%4==3) flag=1; + else + for(long long i=2; i*i<=no; i++) + { + cnt=0; + while(no%i==0) + cnt++, no/=i; + if(i%4==3 && cnt%2!=0) + { + flag=1; break; + } + } + if(no%4==3) flag=1; + if(flag==1) + printf("No\n"); + else + printf("Yes\n"); + } + return 0; +} diff --git a/spoj_cpp_clean/UCBINTA/UCBINTA-3.cpp b/spoj_cpp_clean/UCBINTA/UCBINTA-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..092c968244d2445fea095d7508b834245e227fd7 --- /dev/null +++ b/spoj_cpp_clean/UCBINTA/UCBINTA-3.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int n, num[1000][1000], a; + sd(n); + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + sd(num[i][j]); + } + } + if(n == 2){ + printf("%d %d\n",num[0][1]/2, num[0][1]/2); + } + else{ + a = (num[0][1] + num[0][2] - num[1][2])/2; + printf("%d ", a); + for(int i = 1; i < n; i++) + printf("%d ", num[i][0] - a); + printf("\n"); + } + return 0; +} diff --git a/spoj_cpp_clean/UCBINTF/UCBINTF-9.cpp b/spoj_cpp_clean/UCBINTF/UCBINTF-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdcf6f1e5a77477cbb9adcb04ed5ae2bb09de9ff --- /dev/null +++ b/spoj_cpp_clean/UCBINTF/UCBINTF-9.cpp @@ -0,0 +1,69 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +#define gc() getchar() + +using namespace std; + +int main() +{ + int C = 0, A = 0, i; + string S; + + //freopen("input.txt", "r", stdin); + + + cin >> S; + + for (i = 0; i < S.length(); i++) + { + if (i == 0 || S[i - 1] == '|') + switch (S[i]) + { + case 'C': + C++; + break; + case 'G': + C++; + break; + case 'F': + C++; + break; + case 'A': + A++; + break; + case 'E': + A++; + break; + case 'D': + A++; + break; + } + } + + if (C > A) + printf("C-dur"); + else if (C < A) + printf("A-mol"); + else + { + if (S[S.length() - 1] == 'C' || S[S.length() - 1] == 'G' || S[S.length() - 1] == 'F') + printf("C-dur"); + else + printf("A-mol"); + } + + return 0; +} diff --git a/spoj_cpp_clean/UCV2013A/UCV2013A-7.cpp b/spoj_cpp_clean/UCV2013A/UCV2013A-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..914932493bc1ddac0f96ae600a471f84769a13b5 --- /dev/null +++ b/spoj_cpp_clean/UCV2013A/UCV2013A-7.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +const long long int m= 1000000007; + +long long int Mpow(int b, int p) +{ + long long int aux=0; + if (p==0) + { + return 1; + } + + if (p%2==0) + { + aux= Mpow(b,p/2); + return (aux*aux) % m; + }else + { + return (Mpow(b,p-1)*(b%m))%m; + } +} + +int main() +{ + int a,b; + long long int ans=0; + while(scanf("%d %d",&a,&b) && a!=0 && b!=0 ) + { + ans=0; + for (int i = 1; i <=b ; ++i) + { + ans= (ans+Mpow(a,i))% m; + } + + printf("%lld\n",ans ); + + } + + return 0; +} diff --git a/spoj_cpp_clean/UCV2013A/UCV2013A-9.cpp b/spoj_cpp_clean/UCV2013A/UCV2013A-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fc1e3cd05434c224b9a7e44f0e1a2f173fcca8b --- /dev/null +++ b/spoj_cpp_clean/UCV2013A/UCV2013A-9.cpp @@ -0,0 +1,39 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include + +int main() +{ + long long N, L; + int i; + long long res; + long long s; + //freopen("input.txt","r",stdin); + scanf("%lld %lld", &N, &L); + + while(N!=0 && L!=0) + { + res=s=N; + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef long double ld; +typedef pair p32; +typedef pair p64; +typedef pair pdd; +typedef vector v64; +typedef vector v32; +typedef vector > vv32; +typedef vector > vv64; +typedef vector > vvp64; +typedef vector vp64; +typedef vector vp32; +ll MOD = 998244353; +double eps = 1e-12; +#define forn(i,s,e) for(ll i = s; i < e; i++) +#define rforn(i,s,e) for(ll i = s; i >= e; i--) +#define endl "\n" +#define dbg(x) cout<<#x<<" = "<>arr[i]; + } + forn(i, 0, n){ + cin>>brr[i]; + temp+=brr[i]-arr[i]; + } + ans = f[temp]; + forn(i, 0, n){ + temp=brr[i]-arr[i]; + ans = (ans * invf[temp])%mod; + } + cout<>t){ + //cout << "Case #" << it+1 << ": "; + if(t==0){ + break; + } + solve(t); + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/UCV2013H/UCV2013H-7.cpp b/spoj_cpp_clean/UCV2013H/UCV2013H-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b844bdcc66e41c5dfabe82fe614a2e7af31e3e72 --- /dev/null +++ b/spoj_cpp_clean/UCV2013H/UCV2013H-7.cpp @@ -0,0 +1,76 @@ +#include +#define MAXSIZE +using namespace std; + +int grafo[300][300]; +bool visit[300][300]; +int n,m; + +int movx[]= {0,1,-1,0}; +int movy[]= {1,0,0,-1}; + + +bool valido(int a, int b){ + return ((a=0) && (b=0))?true:false; +} + + + +int dfs(int x,int y) +{ + int cont=0; + visit[x][y]= true; + for (int i = 0; i < 4; ++i) + { + int nx= x+ movx[i]; + int ny= y+ movy[i]; + + if (valido(nx,ny) && grafo[nx][ny]==1 && !visit[nx][ny] ) + { + cont++; + cont+= dfs(nx,ny); + } + } + + return cont; +} + +int main() +{ + map mp; + int ans; + while(scanf("%d %d",&n,&m)!=EOF && n!=0 && m!=0) + { + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + scanf("%d",&grafo[i][j]); + visit[i][j]= false; + } + } + + ans=0; + mp.clear(); + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + if (!visit[i][j] && grafo[i][j]==1) + { + ans++; + mp[dfs(i,j)+1]++; + //printf("%d\n",dfs(i,j)+1); + } + } + } + + printf("%d\n",ans ); + for (map::iterator i = mp.begin(); i != mp.end(); ++i) + { + printf("%d %d\n",(*i).first, (*i).second); + } + } + + return 0; +} diff --git a/spoj_cpp_clean/UCV2013J/UCV2013J-3.cpp b/spoj_cpp_clean/UCV2013J/UCV2013J-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad7e7cf1dad9b9a58b6f9b91c864c775e518895f --- /dev/null +++ b/spoj_cpp_clean/UCV2013J/UCV2013J-3.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +int main(){ + int n, i, temp, nodes[1000000], ans; + int l1; + sd(n); + while(n){ + for(i = 0; i < n; i++){ + sd(nodes[i]); + } + temp = n, i = 1, ans = 0; + while(i < temp){ + temp -= i; + i = i << 1; + } + if(i > 1) i = i >> 1; + + l1 = i - (temp+1)/2; + + for(i = n-temp; i < n; i++){ + ans += nodes[i]; + } + for(i = 1; i <= l1; i++){ + ans += nodes[n-temp-i]; + } + printf("%d\n",ans); + sd(n); + } + return 0; +} diff --git a/spoj_cpp_clean/UFPR14D/UFPR14D-9.cpp b/spoj_cpp_clean/UFPR14D/UFPR14D-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..470ff968c7910c9d00feac7991df02600ef2aeac --- /dev/null +++ b/spoj_cpp_clean/UFPR14D/UFPR14D-9.cpp @@ -0,0 +1,47 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +int SUM[100007]; +int N; + +inline void init() +{ + int num, i; + + scanf("%d", &N); + + for(i = 1; i<=N; i++) + { + scanf("%d", &num); + SUM[i] = SUM[i - 1] + num; + } +} + +int main() +{ + int A, B, Q; + + init(); + + scanf("%d", &Q); + + while(Q--) + { + scanf("%d %d", &A, &B); + printf("%d\n", SUM[B] - SUM[A - 1]); + } + + return 0; +} diff --git a/spoj_cpp_clean/UJ/UJ-4.cpp b/spoj_cpp_clean/UJ/UJ-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7757b2df70f60478522f17d5e69c2b3f2d4805a3 --- /dev/null +++ b/spoj_cpp_clean/UJ/UJ-4.cpp @@ -0,0 +1,90 @@ +//Data Structure includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maX(a,b) ( (a) > (b) ? (a) : (b)) +#define miN(a,b) ( (a) < (b) ? (a) : (b)) +#define FOR(i,a,b) for(int i=a;i=b;i--) +#define fill(a,v) memset(a,v,sizeof a) +#define abS(x) ((x)<0?-(x):(x)) +#define mP make_pair +#define pB push_back +#define error(x) cout << #x << " : " << (x) << endl +#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++) +#define mod 1000000007 +#define MAXN 1000010 +#define MP make_pair +#define INF mod +#define s(n) scanf("%d",&n) +#define sc(n) scanf("%c",&n) +#define sl(n) scanf("%lld",&n) +#define sf(n) scanf("%lf",&n) +#define ss(n) scanf("%s",n) +#define p(n) printf("%d",n) +#define pc(n) printf("%c",n) +#define pl(n) printf("%lld",n) +#define pf(n) printf("%lf",n) +#define ps(n) printf("%s",n) + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; + +void chekarre(int * arr,int n) +{ + cout<<"["; + for(int i=0;i33) + { + h=h*10+c-'0'; + c=getchar();//getchar_unlocked(); + } + return h; +} +int n, m ; + +int main() +{ + //freopen("input.txt","r",stdin); + //freopen("output.txt","w",stdout); + int T; + //s(T); + + return 0; +} diff --git a/spoj_cpp_clean/ULM09/ULM09-9.cpp b/spoj_cpp_clean/ULM09/ULM09-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff5c9016913949dca60c7ad270d6e13fcd72b063 --- /dev/null +++ b/spoj_cpp_clean/ULM09/ULM09-9.cpp @@ -0,0 +1,171 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#define gc() getchar_unlocked() + +using namespace std; + +inline int FAST_IO() +{ + char ch; + int val = 0; + ch = gc(); + while (isspace(ch) && ch != EOF) + ch = gc(); + val = 0; + while (isdigit(ch)) + { + val = (val * 10) + (ch - '0'); + ch = gc(); + } + return val; +} + +class ufs +{ +private: + struct set + { + int name; + int rank; + set *parent; + }; + int ssize; + vector S; + + set *fs(int name) + { + if (S[name].parent->name == name) + return &S[name]; + + S[name].parent = fs(S[name].parent->name); + return S[name].parent; + } + + +public: + ufs(int n) + { + int i; + ssize = n; + S.resize(ssize); + + for (i = 0; iname; + } + + bool is_same_set(int a, int b) + { + return fs(a)->name == fs(b)->name; + } + + void union_sets(int a, int b) + { + set *f, *s; + + f = fs(a); + s = fs(b); + + if (f->name == s->name) + return; + + ssize--; + if (f->rank == s->rank) + { + f->rank++; + s->parent = f; + } + + else if (f->rank > s->rank) + s->parent = f; + + else + f->parent = s; + } + + int size() + { + return ssize; + } +}; + +vector< pair > > EDGES; //w, u, v +int N, E, total; + +inline bool init() +{ + int i, f, s, w; + + //scanf("%d %d", &N, &E); + N = FAST_IO(); + E = FAST_IO(); + + if (N == 0 && E == 0) + return 0; + + total = 0; + EDGES.clear(); + + for (i = 0; i < E; i++) + { + f = FAST_IO(); + s = FAST_IO(); + w = FAST_IO(); + //scanf("%d %d %d", &f, &s, &w); + total += w; + EDGES.push_back(make_pair(w, make_pair(f, s))); + } + + return 1; +} + +inline int solve() +{ + int i, c, ans = 0; + + ufs g(N); + + sort(EDGES.begin(), EDGES.end()); + + for (i = 0, c = 0; i + +using namespace std; + +const int MAXN = 1e4; +int n, tree[MAXN+2]; + +int pointQuery( int idx ) { + int sum = 0; + while( idx > 0 ) { + sum += tree[idx]; + idx -= (idx & (-idx)); + } + return sum; +} + +void pointUpdate( int idx, int value ) { + while( idx <= n ) { + tree[idx] += value; + idx += (idx & (-idx)); + } +} + +void rangeUpdate( int l, int r, int value ) { + pointUpdate( l, value ); + pointUpdate( (r + 1), -value ); +} + +int main( int argc, char ** argv ) { +// freopen( "in.txt", "r", stdin ); +// freopen( "out.txt", "w", stdout ); + + int t; + scanf( "%d", &t ); + for( int nCase = 1 ; nCase <= t ; ++nCase ) { + int u; + scanf( "%d %d", &n, &u ); + + memset( tree, 0, sizeof( tree ) ); + + while( u-- ) { + int l, r, val; + scanf( "%d %d %d", &l, &r, &val ); + + rangeUpdate( ++l, ++r, val ); + } + + int q; + scanf( "%d", &q ); + while( q-- ) { + int idx; + scanf( "%d", &idx ); + + printf( "%d\n", pointQuery( ++idx ) ); + } + } + return 0; +} diff --git a/spoj_cpp_clean/UPDATEIT/UPDATEIT-9.cpp b/spoj_cpp_clean/UPDATEIT/UPDATEIT-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f68739782112adf224e9b732500eb851b3ca8517 --- /dev/null +++ b/spoj_cpp_clean/UPDATEIT/UPDATEIT-9.cpp @@ -0,0 +1,72 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +int tree[10007]; +int N, U, Q; + +inline void point_update(int pos, int val) +{ + for( ; pos<=N; pos += pos&(-pos)) + tree[pos] += val; +} + +inline void range_update(int i, int j, int val) +{ + point_update(i, val); + point_update(j+1, -val); +} + +inline int point_query(int pos) +{ + int sum = 0; + + for( ; pos > 0; pos -= pos&(-pos)) + sum += tree[pos]; + + return sum; +} + +int main() +{ + int i, T; + int f, s, val; + + //freopen("input.txt","r",stdin); + + scanf("%d", &T); + + while(T--) + { + memset(tree, 0, sizeof(tree)); + + scanf("%d %d", &N, &U); + N++; + + for(i=0; i +using namespace std; + +typedef long long ll; +#define endl "\n" +#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) + +void solve(ll t){ + ll x=t, temp=0; + if( (t&(t-1))==0 ){ + cout<>=1; + temp++; + } + temp--; + int y = t - (1<>t; + while(t){ + //cout << "Case #" << it+1 << ": "; + solve(t); + cin>>t; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/VECTAR10/VECTAR10-9.cpp b/spoj_cpp_clean/VECTAR10/VECTAR10-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ae2750e902f21edd52b04325f4b65757f4a732d --- /dev/null +++ b/spoj_cpp_clean/VECTAR10/VECTAR10-9.cpp @@ -0,0 +1,56 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int p[] = { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288 }; +int V = 524288; +int N; + +inline bool init() +{ + scanf("%d", &N); + + return N != 0; +} + +inline int solve(int N) +{ + int *ii; + + if (N == 1) + return 1; + + if (N >= V) + ii = &V; + + else + { + ii = upper_bound(p, p + 19, N); + ii--; + } + + if (*ii == N) + return N; + + return (N - *ii) * 2; +} + +int main() +{ + while (init()) + printf("%d\n", solve(N)); + + return 0; +} diff --git a/spoj_cpp_clean/VECTAR11/VECTAR11-9.cpp b/spoj_cpp_clean/VECTAR11/VECTAR11-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cf17b909f88fda37d793255fa57a35e7b0e4be5 --- /dev/null +++ b/spoj_cpp_clean/VECTAR11/VECTAR11-9.cpp @@ -0,0 +1,56 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +using namespace std; + +int N; +int DP[1000007]; + +inline bool check(int pos) +{ + int i; + + for (i = 1; i * i <= pos; i++) + if (!DP[pos - i * i]) + return 1; + + return 0; +} + +inline void pre() +{ + int i; + + for (i = 1; i <= 1000000; i++) + DP[i] = check(i); +} + +int main() +{ + int T; + + pre(); + + scanf("%d", &T); + + while (T--) + { + scanf("%d", &N); + if (DP[N]) + printf("Win\n"); + else + printf("Lose\n"); + } + + return 0; +} diff --git a/spoj_cpp_clean/VECTAR12/VECTAR12-9.cpp b/spoj_cpp_clean/VECTAR12/VECTAR12-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8991e5fd5c30e8af616f63d2b73b38336d92c92f --- /dev/null +++ b/spoj_cpp_clean/VECTAR12/VECTAR12-9.cpp @@ -0,0 +1,64 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include + +#define MOD 1000000007 +using namespace std; + +int N, K; +int DP[7007][7007]; + +inline void init() +{ + int i, j; + + DP[0][0] = 1; + + for (i = 1; i < 7000; i++) + { + DP[i][0] += DP[i - 1][0] + DP[i - 1][1]; + + if (DP[i][0] >= MOD) + DP[i][0] -= MOD; + + for (j = 1; j <= i; j++) + { + DP[i][j] += DP[i - 1][j - 1] + DP[i - 1][j]; + + if (DP[i][j] >= MOD) + DP[i][j] -= MOD; + + DP[i][j] += DP[i - 1][j + 1]; + + if (DP[i][j] >= MOD) + DP[i][j] -= MOD; + } + } +} + +int main() +{ +// freopen("input.txt", "r", stdin); + + init(); + int T; + + scanf("%d", &T); + + while (T--) + { + scanf("%d %d", &N, &K); + printf("%d\n", DP[N][K]); + } + + return 0; +} diff --git a/spoj_cpp_clean/VECTAR14/VECTAR14-9.cpp b/spoj_cpp_clean/VECTAR14/VECTAR14-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c84059db8ad04aff316314d544f1db21afeeee19 --- /dev/null +++ b/spoj_cpp_clean/VECTAR14/VECTAR14-9.cpp @@ -0,0 +1,100 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define MOD 1000000007 +#define gc() getchar() +using namespace std; + +long long memo[1024][1024]; +int N; +string S; + +inline void getstring(string &A) +{ + char c = gc(); + + A.clear(); + + A.push_back('0'); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +inline void init() +{ + getstring(S); + memset(memo, -1, sizeof(memo)); + N = S.size(); +} + +long long f(int pos, int open) +{ + int i; + long long ans = 0; + + if (memo[pos][open] != -1) + return memo[pos][open]; + + if (!open) + ans++; + + for (i = pos + 1; i < N && S[i] != '('; i++); + + if (i < N) + ans += f(i, open + 1); + + if (ans >= MOD) + ans -= MOD; + + if (open) + { + for (i = pos + 1; i < N && S[i] != ')'; i++); + + if (i < N) + ans += f(i, open - 1); + } + + if (ans >= MOD) + ans -= MOD; + + memo[pos][open] = ans; + + return ans; +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + init(); + printf("%lld\n", f(0, 0)); + } + + return 0; +} diff --git a/spoj_cpp_clean/VECTAR7/VECTAR7-9.cpp b/spoj_cpp_clean/VECTAR7/VECTAR7-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5f4defbb0675e176d5e1e098d15aeb40bef3dcf --- /dev/null +++ b/spoj_cpp_clean/VECTAR7/VECTAR7-9.cpp @@ -0,0 +1,97 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define MOD 1000000007 +using namespace std; + +long long nCr[50][50]; +long long A, B; + +long long modular_pow(long long base, long long exponent, long long modulus) +{ + long long result = 1; + + while (exponent > 0) + { + if (exponent % 2 == 1) + result = (result * base) % modulus; + exponent = exponent >> 1; + base = (base * base) % modulus; + } + return result; +} + +inline void init() +{ + int i, j; + + for (i = 0; i < 50; i++) + nCr[i][0] = nCr[i][i] = 1; + + for (i = 1; i < 50; i++) + for (j = 1; j <= i / 2; j++) + { + nCr[i][j] = nCr[i][i - j] = nCr[i - 1][j] + nCr[i - 1][j - 1]; + nCr[i][j] %= MOD; + nCr[i][i - j] %= MOD; + + } + +} + +inline long long solve() +{ + long long mmax, mmin; + long long result = 0, p; + + mmax = max(A, B); + mmin = min(A, B); + + if (mmax < 25) + return 0; + + if (mmax == 25 && mmax - mmin < 2) + return 0; + + if (mmax == 25) + return nCr[mmax + mmin - 1][mmin]; + + if (mmax - mmin != 2) + return 0; + + result = nCr[48][24]; + p = modular_pow(2, mmin - 24, MOD); + + result = (result * p) % MOD; + + return result; +} + +int main() +{ + int T; + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + init(); + + while (T--) + { + scanf("%lld %lld", &A, &B); + printf("%lld\n", solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/VECTAR8/VECTAR8-9.cpp b/spoj_cpp_clean/VECTAR8/VECTAR8-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ff43fd828280b48cedb4ff941606eea8603852c --- /dev/null +++ b/spoj_cpp_clean/VECTAR8/VECTAR8-9.cpp @@ -0,0 +1,91 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define MAXN 1000000 +using namespace std; + +bool not_prime[1000007]; +vector< pair > primes; + +bool check(int num) +{ + string temp = to_string(num); + + int i; + + for (i = 0; i < temp.size(); i++) + if (temp[i] == '0') + return 0; + + for (i = 0; i < temp.size() - 1; i++) + { + temp[i] = '0'; + if (not_prime[stoi(temp)]) + return 0; + } + + return 1; +} + +inline void sieve() +{ + int i, j; + + not_prime[1] = 1; + + primes.push_back(make_pair(2, 1)); + + for (i = 4; i <= MAXN; i += 2) + not_prime[i] = 1; + + for (i = 3; i <= MAXN; i += 2) + if (!not_prime[i]) + { + if (check(i)) + primes.push_back(make_pair(i, primes.size() + 1)); + + for (j = i + i; j <= MAXN; j += i) + not_prime[j] = 1; + } +} + +int main() +{ + int T, num; + vector > ::iterator ii; + + sieve(); + +// freopen("input.txt", "r", stdin); + + scanf("%d", &T); + + while (T--) + { + scanf("%d", &num); + ii = upper_bound(primes.begin(), primes.end(), make_pair(num, 10000000)); + if (ii == primes.end()) + { + ii = prev(ii); + printf("%d\n", ii->second); + } + + else + printf("%d\n", ii->second - 1); + } + + return 0; +} diff --git a/spoj_cpp_clean/VFMUL/VFMUL-14.cpp b/spoj_cpp_clean/VFMUL/VFMUL-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..411ead57674244467a9af45cf06d6085f78a0e2a --- /dev/null +++ b/spoj_cpp_clean/VFMUL/VFMUL-14.cpp @@ -0,0 +1,159 @@ +// #pragma GCC optimize("Ofast,unroll-loops") +// #pragma GCC target("avx,avx2,fma") + +#include +using namespace std; + +#define ll long long +#define ull unsigned long long +#define dd double +#define ld long double +#define sl(n) scanf("%lld", &n) +#define si(n) scanf("%d", &n) +#define sd(n) scanf("%lf", &n) +#define pll pair +#define pii pair +#define mp make_pair +#define pb push_back +#define all(v) v.begin(), v.end() +#define inf (1LL << 62) +#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) +#define for1(i, stop) for(ll i = 1; i <= stop; ++i) +#define for0(i, stop) for(ll i = 0; i < stop; ++i) +#define rep1(i, start) for(ll i = start; i >= 1; --i) +#define rep0(i, start) for(ll i = (start-1); i >= 0; --i) +#define ms(n, i) memset(n, i, sizeof(n)) +#define casep(n) printf("Case %lld:", ++n) +#define pn printf("\n") +#define pf printf +#define EL '\n' +#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); + +/*** + * Multiply (7x^2 + 8x^1 + 9x^0) with (6x^1 + 5x^0) + * ans = 42x^3 + 83x^2 + 94x^1 + 45x^0 + * A = {9, 8, 7} + * B = {5, 6} + * V = multiply(A,B) + * V = {45, 94, 83, 42} +***/ + +/*** Tricks + * Use vector < bool > if you need to check only the status of the sum + * Use bigmod if the power is over same polynomial && power is big + * Use long double if you need more precision + * Use long long for overflow +***/ +typedef vector vi; +const double PI = 2.0 * acos(0.0); +using cd = complex; + +void fft(vector & a, bool invert = 0) + +{ + int n = a.size(); + + for (int i = 1, j = 0; i < n; i++) { + int bit = n >> 1; + for (; j & bit; bit >>= 1) + j ^= bit; + j ^= bit; + + if (i < j) + swap(a[i], a[j]); + } + + for (int len = 2; len <= n; len <<= 1) { + double ang = 2 * PI / len * (invert ? -1 : 1); + cd wlen(cos(ang), sin(ang)); + for (int i = 0; i < n; i += len) { + cd w(1); + for (int j = 0; j < len / 2; j++) { + cd u = a[i+j], v = a[i+j+len/2] * w; + a[i+j] = u + v; + a[i+j+len/2] = u - v; + w *= wlen; + } + } + } + + if (invert) { + for (cd & x : a) + x /= n; + } +} + +void ifft(vector & p) + +{ + fft(p, 1); +} + +vi multiply(vi const& a, vi const& b) +{ + + vector fa(a.begin(), a.end()), fb(b.begin(), b.end()); + int n = 1; + while (n < a.size() + b.size()) + n <<= 1; + fa.resize(n); + fb.resize(n); + + fft(fa); + fft(fb); + for (int i = 0; i < n; i++) + fa[i] *= fb[i]; + ifft(fa); + + vi result(n); + for (int i = 0; i < n; i++) + result[i] = round(fa[i].real()); + + return result; +} + +string s1, s2; +vi num1, num2, res; + +int main() +{ + fastio; + ll t; + cin >> t; + while(t--) { + cin >> s1 >> s2; + reverse(all(s1)), reverse(all(s2)); + + num1.reserve(s1.length()); + for(char &ch: s1) num1.pb(ch-'0'); + + num2.reserve(s2.length()); + for(char &ch : s2) num2.pb(ch-'0'); + + res = multiply(num1, num2); + + ll carry = 0; + for(ll &val : res) { + val += carry; + + carry = val / 10; + val %= 10; + } + + bool f = 0; + if(carry) f = 1, cout << carry; + + for(ll i = (ll)res.size() - 1; i >= 0; i--) { + if(!f && res[i] == 0 && i > 0) + continue; + + cout << res[i]; + f = 1; + } + cout << EL; + + num1.clear(), num2.clear(); + res.clear(); + } + + return 0; diff --git a/spoj_cpp_clean/VFMUL/VFMUL-3.cpp b/spoj_cpp_clean/VFMUL/VFMUL-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c0c72f729166f51a7f9969f04ef375e63f08ea1 --- /dev/null +++ b/spoj_cpp_clean/VFMUL/VFMUL-3.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x) +#define sd2(x,y) scanf("%d%d",&x,&y) +#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z) +#define fi first +#define se second +#define pb(x) push_back(x) +#define mp(x,y) make_pair(x,y) +#define LET(x, a) __typeof(a) x(a) +#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++) +#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); +#define __ freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout); + +#define tr(x) cout< base; + +long long nn; +char a[1000000], b[1000000]; +long double PI = 2 * acos(0.0L); +long long BASE = 100000; + +long long rev (long long num, long long lg_n) { + long long res = 0; + for (long long i=0; i & a, bool invert) { + long long n = (long long) a.size(); + long long lg_n = 0; + while ((1 << lg_n) < n) ++lg_n; + + for (long long i=0; i & a, const vector & b, vector & res) { + vector fa (a.begin(), a.end()), fb (b.begin(), b.end()); + size_t n = 1; + while (n < max (a.size(), b.size())) n <<= 1; + n <<= 1; + fa.resize (n), fb.resize (n); + + fft (fa, false), fft (fb, false); + for (size_t i=0; i n1, n2, r; + +long long l1, l2; + +int main(){ + scanf("%lld",&nn); + while(nn--){ + scanf("%s %s", a, b); + n1.clear(), n2.clear(), r.clear(); + + l1 = strlen(a), l2 = strlen(b); + + for(long long i = l1-1; i >= 0; i -= 5){ + long long x = 0; + for(long long j = max(0LL,i-5+1); j <= i; j++){ + x = (x*10) + (long long)(a[j]-'0'); + } + n1.pb(x); + } + +// foreach(it, n1) cout << *it << " "; cout << endl; + + for(long long i = l2-1; i >= 0; i -= 5){ + long long x = 0; + for(long long j = max(0LL,i-5+1); j <= i; j++){ + x = (x*10) + (long long)(b[j]-'0'); + } + n2.pb(x); + } +// foreach(it, n2) cout << *it << " "; cout << endl; + + multiply(n1, n2, r); + +// foreach(it, r) cout << *it << " "; cout << endl; + + long long i = r.size()-1; + while(i > 0 and r[i] == 0) i--; + printf("%lld", r[i]); + for(i--; i >= 0; i--) printf("%.*lld", 5, r[i]); printf("\n"); + } + return 0; +} diff --git a/spoj_cpp_clean/VPL2_AC/VPL2_AC-9.cpp b/spoj_cpp_clean/VPL2_AC/VPL2_AC-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d13584b62504cd3089bd6cde572680944c6aae6 --- /dev/null +++ b/spoj_cpp_clean/VPL2_AC/VPL2_AC-9.cpp @@ -0,0 +1,116 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define gc() getchar_unlocked() + +using namespace std; + +int dist[26]; +string S; +int C; + +inline void getstring(string &A) +{ + char c; + + A.clear(); + + c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +inline void init_dist() +{ + dist['G' - 'A'] = 1; + dist['R' - 'A'] = 2; + dist['Y' - 'A'] = 3; + dist['B' - 'A'] = 4; + dist['O' - 'A'] = 5; +} + +inline int cost(char a, char b) +{ + a = dist[a - 'A']; + b = dist[b - 'A']; + + if (a > b) + return a - b; + + return b - a; +} + +inline void init() +{ + scanf("%d", &C); + getstring(S); +} + +inline int solve() +{ + int i, j, c = 0; + int MAX = 1; + + i = 0; + j = 1; + + while (j < S.length()) + { + while (j < S.length() && cost(S[j], S[j - 1]) + c <= C) + { + c += cost(S[j], S[j - 1]); + j++; + MAX = max(MAX, j - i); + } + + if (j == S.length()) + break; + + while (c + cost(S[j], S[j - 1]) > C) + { + c -= cost(S[i], S[i + 1]); + i++; + } + } + + return MAX; +} + +int main() +{ + int T, i; + + //freopen("input.txt", "r", stdin); + + init_dist(); + + scanf("%d", &T); + + for (i = 1; i <= T; i++) + { + init(); + printf("Scenario #%d: %d\n", i, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/WILITS/WILITS-5.cpp b/spoj_cpp_clean/WILITS/WILITS-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d1fef40f16169314649b5ee9398f153ce6cfa1f --- /dev/null +++ b/spoj_cpp_clean/WILITS/WILITS-5.cpp @@ -0,0 +1,20 @@ +#include +#include + +using namespace std; + +int main() +{ + unsigned long long int n,a; + cin>>n; + a=2; + while(a + +using namespace std; + +int main(void) +{ + unsigned long long input; + + cin >> input; + + if(input & (input-1)) + cout << "NIE" << endl; + else + cout << "TAK" << endl; + + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/WILLITST/WILLITST-16.cpp b/spoj_cpp_clean/WILLITST/WILLITST-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..947319975b9932f645ee364918f9b4e7c803d545 --- /dev/null +++ b/spoj_cpp_clean/WILLITST/WILLITST-16.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +using namespace std; + +int main(){ + long long k,i; + cin>>k; + for(i=1;i<100;i++){ + + if(pow(2,i)==k){ + cout<<"TAK"; + break; + } + if(pow(2,i)>k){ + cout<<"NIE"; + return 0; + } + } + if(i>=100){ + cout<<"NIE"; + } + + return 0; + } diff --git a/spoj_cpp_clean/WILLITST/WILLITST-19.cpp b/spoj_cpp_clean/WILLITST/WILLITST-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..659a987eb19c547e98b06e7329c9047cbca7d4f8 --- /dev/null +++ b/spoj_cpp_clean/WILLITST/WILLITST-19.cpp @@ -0,0 +1,38 @@ +#include +#define endl "\n" +#define ll long long +#define mod 1000000007 +#define MAX 100001 + +using namespace std; +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + unsigned ll arr[60]; + arr[0]=2; + for(int i=1; i<60; i++){ + arr[i] = arr[i-1]*2; + } + unsigned ll num; + bool yes=false; + cin>>num; + if(num==1){ + cout<<"TAK"; + return 0; + } + for(int i=0; i<60; i++){ + if(num==arr[i]){ + yes=true; + break; + } + } + if(yes==true){ + cout<<"TAK"; + } + else{ + cout<<"NIE"; + } + return 0; +} \ No newline at end of file diff --git a/spoj_cpp_clean/WILLITST/WILLITST-9.cpp b/spoj_cpp_clean/WILLITST/WILLITST-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe880441b8dd9f2e75ff528046ff2d85e2e081d0 --- /dev/null +++ b/spoj_cpp_clean/WILLITST/WILLITST-9.cpp @@ -0,0 +1,40 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +unordered_map< unsigned long long, bool > found; + +int main() +{ + unsigned long long N; + scanf("%llu", &N); + + while(N>1) + { + if(found.find(N)!=found.end()) + { + printf("NIE\n"); + return 0; + } + found[N]=1; + if(N%2==0) + N/=2; + else + N=3*N+3; + } + + printf("TAK\n"); + return 0; +} diff --git a/spoj_cpp_clean/WOWSUBSTR/WOWSUBSTR-9.cpp b/spoj_cpp_clean/WOWSUBSTR/WOWSUBSTR-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f2a8211bdff72bd88d98a38c61c58895d08f057 --- /dev/null +++ b/spoj_cpp_clean/WOWSUBSTR/WOWSUBSTR-9.cpp @@ -0,0 +1,84 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define MOD 100007 +#define gc() getchar() +using namespace std; + +inline void getstring(string &A) +{ + char c = gc(); + + A.clear(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +string S; + +inline int solve() +{ + int rval = 0; + int memo[26]; + int i, j; + + memset(memo, 0, sizeof(memo)); + + j = 0; + + for (i = 0; i < S.size(); i++) + { + if (memo[S[i] - 'a']) + while (memo[S[i] - 'a']) + { + memo[S[j] - 'a']--; + j++; + } + + memo[S[i] - 'a']++; + + int n = i - j + 1; + + rval += (n * (1 + n)) / 2; + rval %= MOD; + } + + return rval; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + + int T, i; + + scanf("%d", &T); + + for (i = 1; i <= T; i++) + { + getstring(S); + printf("Case %d: %d\n", i, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/WOWSUBSTR2/WOWSUBSTR2-9.cpp b/spoj_cpp_clean/WOWSUBSTR2/WOWSUBSTR2-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4ab0c40be3a634c7b475ec368c2b6769f07da08 --- /dev/null +++ b/spoj_cpp_clean/WOWSUBSTR2/WOWSUBSTR2-9.cpp @@ -0,0 +1,78 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +#define MOD 100007 +#define MAXN 500007 +#define MAXM 1000007 +using namespace std; + +int memo[MAXM]; +int N, M; +int S[MAXN]; + +inline void init() +{ + int i; + + scanf("%d %d", &N, &M); + + for (i = 0; i < N; i++) + scanf("%d", &S[i]); +} + +inline int solve() +{ + int rval = 0; + int i, j; + + memset(memo, 0, sizeof(memo)); + + j = 0; + + for (i = 0; i < N; i++) + { + if (memo[S[i]]) + while (memo[S[i]]) + { + memo[S[j]]--; + j++; + } + + memo[S[i]]++; + + int n = i - j + 1; + + rval += (n * (1 + n)) / 2; + rval %= MOD; + } + + return rval; +} + +int main() +{ +// freopen("input.txt", "r", stdin); + + int T, i; + + scanf("%d", &T); + + for (i = 1; i <= T; i++) + { + init(); + printf("Case %d: %d\n", i, solve()); + } + + return 0; +} diff --git a/spoj_cpp_clean/WPC4F/WPC4F-9.cpp b/spoj_cpp_clean/WPC4F/WPC4F-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8150494983a7ee9af68e697bedb8f2e097fee272 --- /dev/null +++ b/spoj_cpp_clean/WPC4F/WPC4F-9.cpp @@ -0,0 +1,54 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include + +using namespace std; + +int DP[21][3]; +int A[21][3]; + +int N; + +inline void init() +{ + int i; + + scanf("%d", &N); + + for(i=1; i<=N; i++) + scanf("%d %d %d", &A[i][0], &A[i][1], &A[i][2]); +} + +int main() +{ + int T, i; + + //freopen("input.txt","r",stdin); + scanf("%d", &T); + + while(T--) + { + init(); + + for(i=1; i<=N; i++) + { + DP[i][0]=A[i][0] + min(DP[i-1][1], DP[i-1][2]); + DP[i][1]=A[i][1] + min(DP[i-1][0], DP[i-1][2]); + DP[i][2]=A[i][2] + min(DP[i-1][0], DP[i-1][1]); + } + + printf("%d\n", min(DP[N][0], min(DP[N][1], DP[N][2]))); + } + + return 0; +} diff --git a/spoj_cpp_clean/YODANESS/YODANESS-3.cpp b/spoj_cpp_clean/YODANESS/YODANESS-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34748ce16ff692ed3c35a632ce1b4bc54e0801e4 --- /dev/null +++ b/spoj_cpp_clean/YODANESS/YODANESS-3.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +//Merge Sort Tree +struct node{ + vector num; + int s, inv; + + void assignLeaf(int value){ + num.clear(); + num.push_back(value); + s = 1; + inv = 0; + } + void merge(node& left, node& right){ + int i = 0, j = 0; + num.clear(); + inv = left.inv + right.inv; + while(i < left.s and j < right.s){ + if(left.num[i] > right.num[j]){ + num.push_back(right.num[j]); + inv = inv + (left.s - i); + j++; + } + else{ + num.push_back(left.num[i]); + i++; + } + } + while(i < left.s){ + num.push_back(left.num[i]); + i++; + } + + while(j < right.s){ + num.push_back(right.num[j]); + j++; + } + s = num.size(); + } +}; + +int t, n, array[1<<15], i; +int *res; +char word[21]; +map dic; +node tree[1<<16]; + + +// l and r represent the range node i is responsible for. A single call init(1, 0, n-1) builds the segment tree in O(N) time +void init(int i, int l, int r){ + if(l == r){ + tree[i].assignLeaf(array[l]); + } + else{ + init(i*2, l, (l+r)/2); + init((i*2) + 1, ((l+r)/2) + 1, r); + node left = tree[i*2], right = tree[(i*2)+1]; + + tree[i].merge(left, right); //Merge the children into the parent + } + return; +} + +int main(){ + res = &(tree[1].inv); + sd(t); + while(t--){ + sd(n); + dic.clear(); + for(i = 0; i < n; i++){ + scanf("%s",word); + dic[word] = i; + } + for(i = 0; i < n; i++){ + scanf("%s",word); + array[dic[word]] = i; + } + init(1,0,n-1); + + printf("%d\n",*res); + } + return 0; +} diff --git a/spoj_cpp_clean/YODANESS/YODANESS-9.cpp b/spoj_cpp_clean/YODANESS/YODANESS-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5206b4685216fdaa4fd2b796233ed1931359f82d --- /dev/null +++ b/spoj_cpp_clean/YODANESS/YODANESS-9.cpp @@ -0,0 +1,108 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include +#include +#include + +#define gc() getchar_unlocked() + +using namespace std; + +unsigned long long tree[30007]; +unordered_map MAP; +vector YODA; +int N; + +inline void getstring(string &A) +{ + char c; + + A.clear(); + + c = gc(); + + while (isspace(c)) + c = gc(); + + while (!isspace(c) && c != EOF) + { + A.push_back(c); + c = gc(); + } +} + +inline void update(int pos, int val) +{ + for (; pos <= N; pos += pos & (-pos)) + tree[pos] += val; +} + +inline unsigned long long query(int pos) +{ + unsigned long long sum = 0; + + for (; pos > 0; pos -= pos & (-pos)) + sum += tree[pos]; + + return sum; +} + +void init() +{ + int i; + string A; + + YODA.clear(); + memset(tree, 0, sizeof(tree)); + + scanf("%d", &N); + + for (i = 0; i= 0; i--) + { + update(MAP[YODA[i]] + 1, 1); + sum += query(MAP[YODA[i]]); + } + + printf("%llu\n", sum); + } + + return 0; +} diff --git a/spoj_cpp_clean/ZCR/ZCR-9.cpp b/spoj_cpp_clean/ZCR/ZCR-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1dda7fc6fe49e91193b59796847f17c7afef90e3 --- /dev/null +++ b/spoj_cpp_clean/ZCR/ZCR-9.cpp @@ -0,0 +1,65 @@ +/* +**************************************************************** +**************************************************************** +-> Coded by Stavros Chryselis +-> Visit my github for more solved problems over multiple sites +-> https://github.com/StavrosChryselis +-> Feel free to email me at stavrikios@gmail.com +**************************************************************** +**************************************************************** +*/ + +#include +#include +#include +#include + +#define MAXN 1024 +using namespace std; + +int N, K; +double A[MAXN]; +double DP[MAXN][MAXN]; + +inline void init() +{ + cin >> N >> K; + + for (int i = 1; i <= N; i++) + cin >> A[i]; +} + +inline double solve() +{ + int i, j; + + memset(DP, 0, sizeof(DP)); + DP[0][0] = 1; + + for (i = 1; i <= N; i++) + DP[0][i] = DP[0][i - 1] * (1 - A[i]); + + for (j = 1; j <= K; j++) + for (i = 1; i <= N; i++) + DP[j][i] = DP[j][i - 1] + DP[j - 1][i - 1] * A[i]; + + return min(1.00, DP[K][N]); +} + +int main() +{ +// freopen("input.txt", "r", stdin); + ios::sync_with_stdio(0); + + int T; + + cin >> T; + + while (T--) + { + init(); + cout << setiosflags(ios::fixed) << setprecision(10) << solve() << "\n"; + } + + return 0; +} diff --git a/spoj_cpp_clean/ZSUM/ZSUM-3.cpp b/spoj_cpp_clean/ZSUM/ZSUM-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1752349305d360fe7b5a2903aa7126f6d813224b --- /dev/null +++ b/spoj_cpp_clean/ZSUM/ZSUM-3.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sd(x) scanf("%d",&x); +#define sd2(x,y) scanf("%d %d",&x,&y); +#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z); +#define sull(x) scanf("%ull",&x); +#define print(x) printf("%d\n",x); +#define print2(x,y) printf("%d %d\n",x,y); +#define print3(x,y,z) printf("%d %d %d\n",x,y,z); +#define printull(x) printf("%ull\n",x); + +using namespace std; + +long long MOD = 10000007; + +long long exp(long long a, long long b){ + long long ans = 1; + while(b){ + if(b&1) ans = (ans*a)%MOD; + a = (a*a)%MOD; + b >>= 1; + } + return ans; +} + +int main(){ + long long n,k,res; + scanf("%lld %lld",&n,&k); + while(n!= 0 or k != 0){ + res = (2*exp(n-1,k))%MOD; + res = (res + exp(n,k))%MOD; + res = (res + (2*exp(n-1,n-1)%MOD))%MOD; + printf("%lld\n", (res + exp(n,n))%MOD); + scanf("%lld %lld",&n,&k); + } + return 0; +} diff --git a/uva_cpp_clean/100/100-15.cpp b/uva_cpp_clean/100/100-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9ed8fad356663d7a625b996e9a9a0e113cbae08 --- /dev/null +++ b/uva_cpp_clean/100/100-15.cpp @@ -0,0 +1,51 @@ +/* + 3n+1 問題 +*/ +#include +#include + +using namespace std; + +int dp[1000001] = {1, 1}; + +int calculate(int n) { + // 大於 10^6 的就不暫存直接暴力算 + // 因為會很快就收斂到較小的數值 + if (n > 1000000) { + if (n & 1) + return calculate(3 * n + 1) + 1; + else + return calculate(n / 2) + 1; + } + // 使用動態規劃避免重複計算 + if (dp[n]) + return dp[n]; + if (n & 1) + dp[n] = calculate(3 * n + 1) + 1; + else + dp[n] = calculate(n / 2) + 1; + + return dp[n]; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j; + while (cin >> i >> j) { + cout << i << " " << j << " "; + // 這題 i 不一定小於 j + // 這點要注意一下 + if (i > j) + swap(i, j); + + int ans = 0; + + for (int k = i; k <= j; k++) + ans = max(ans, calculate(k)); + + cout << ans << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/100/100-18.cpp b/uva_cpp_clean/100/100-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b2c366874002a433480e9259de3d525cfb628b78 --- /dev/null +++ b/uva_cpp_clean/100/100-18.cpp @@ -0,0 +1,41 @@ +/** +* 100 - The 3n + 1 problem +*/ + +#include +#include + +using namespace std; + +int main(){ + int i, j, auxI, auxJ, max, cycle; + while(cin >> i >> j){ + auxI = i; + auxJ = j; + if(i > j) swap(i, j); + max = -1; + while(i <= j){ + cycle = 1; + long long int n = i; + while(n != 1){ + if(n % 2 == 1) n = 3 * n + 1; + else n /= 2; + cycle++; + } + if(cycle > max) max = cycle; + i++; + } + printf("%d %d %d\n", auxI, auxJ, max); + } + return 0; +} + +/** + +Sample Input +1 10 +100 200 +201 210 +900 1000 + +*/ diff --git a/uva_cpp_clean/100/100-19.cpp b/uva_cpp_clean/100/100-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b58bb1cfb1daacbf511fa2299019ab11ba3eef29 --- /dev/null +++ b/uva_cpp_clean/100/100-19.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +using namespace std; + +map c; + +int collatz(long n){ + if(c.count(n) == 0){ + if(n%2==0){ + c[n] = collatz(n/2)+1; + } else{ + c[n] = collatz((3*n)+1)+1; + } + } + return c[n]; +} + +int main(){ + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + c[0]=0; c[1]=1; + for(long i = 2; i<= 1000000; i++) collatz(i); + int i, j, ia, ja; + while(scanf("%d %d",&i, &j)!=EOF){ + int max = 0; + if(jmax) max = c[m]; + printf("%d %d %d\n",i,j,max); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/100/100-21.cpp b/uva_cpp_clean/100/100-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cd9bf144a4f8b0a32303d503ef9f177c0c8a074 --- /dev/null +++ b/uva_cpp_clean/100/100-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 100 + Name: The 3n + 1 problem + Problem: https://onlinejudge.org/external/1/100.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MAX 10000000 +long long int memo[MAX]; +long long int alen(long long int n, int d) { + if (n == 1) return 1; + if (!d || n<0) return -1e7; + if (n < MAX && memo[n]) return memo[n]; + + long long int x = n; + int b = 0; + for (; !(x&1); x>>=1) + b++; + + + long long int r = 1; + if (x < MAX && memo[x]) + r = memo[x]; + + else if (x>1 && 3*x+1>0) { + r = alen(3*x + 1, d-1) + 1; + if (x < MAX && r>0) memo[x] = r; + } + + if (n < MAX && r>0) memo[n] = b + r; + return b + r; +} + +int main(){ + bool b; + long long int l, r; + while (cin>>l>>r && (l||r)) { + long long int best = 0; + if ((b = r < l)) { + r ^= l; + l ^= r; + r ^= l; + } + + for (long long int i=r; i>=l; i--) { + long long int al = alen(i, 800); + if (al >= best) best = al; + } + + printf("%lld %lld %lld\n", b?r:l, b?l:r, best); + } +} + diff --git a/uva_cpp_clean/100/100-5.cpp b/uva_cpp_clean/100/100-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..87a5cb13a1584861a7bbc56056a8450aa184c437 --- /dev/null +++ b/uva_cpp_clean/100/100-5.cpp @@ -0,0 +1,34 @@ +#include +int main() +{ +int n,m,i,k,j,c,s; +while(scanf("%d %d",&n,&m)==2) +{ +s=0; +printf("%d %d ",n,m); +if(n>m) +{ +k=m; +m=n; +n=k; +} +for(i=n;i<=m;i++) + { + c=1; + j=i; + while(j>1) + { + if(j%2==0) + j=j/2; + else + j=(3*j)+1; + c++; + } + if(c>=s) + s=c; + } +printf("%d\n",s); +} +return 0; +} + diff --git a/uva_cpp_clean/100/100-6.cc b/uva_cpp_clean/100/100-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..192a7a99cd471a3b555dd96a662f8d7c44be20dc --- /dev/null +++ b/uva_cpp_clean/100/100-6.cc @@ -0,0 +1,23 @@ +// https://uva.onlinejudge.org/external/1/100.pdf +#include + +using namespace std; + +int collatz(int n) { + int i = 1; + while (n > 1) { + if (n % 2) n = n * 3 + 1; + else n >>= 1; + i++; + } + return i; +} + +int main() { + int a, b; + while (cin >> a >> b) { + int m = 0; + for (int i = min(a, b); i <= max(a, b); i++) m = max(m, collatz(i)); + cout << a << " " << b << " " << m << endl; + } +} diff --git a/uva_cpp_clean/100/100-9.cpp b/uva_cpp_clean/100/100-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fe378eed0e2390a52ec3aa5ea627fb423f59584 --- /dev/null +++ b/uva_cpp_clean/100/100-9.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main(){ + vector dp(1000000); + int ct, x, y, ans, tmp, l, r; + long long n; + for(int i = 1; i < 1000000; i++){ + ct = 1; + n = i; + while(n != 1){ + ct++; + if(n & 1) + n = 3 * n + 1; + else + n /= 2; + if( n < i ) + ct += dp[n] - 1, n = 1; + } + dp[i] = ct; + } + while(scanf("%d %d", &x, &y) == 2){ + ans = 0; + l = min(x, y); + r = max(x, y); + for(int i = l; i <= r; i++) + if(dp[i] > ans) + ans = dp[i]; + printf("%d %d %d\n", x, y, ans); + } + return(0); +} diff --git a/uva_cpp_clean/100/main.cpp b/uva_cpp_clean/100/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e054237ef2dfe7011bd8c05b180ea781b003e2f7 --- /dev/null +++ b/uva_cpp_clean/100/main.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +unsigned int ciclo(unsigned int n){ + if(n == 1) return 1; + if(n & 1) return 1+ciclo(3*n+1); + else return 1+ciclo(n/2); +} + +int main(){ + unsigned int i, j; + while(cin >> i >> j){ + unsigned int mayor = max(i, j), menor = min(i, j); + cout << i << ' ' << j << ' '; + unsigned int res = 0; + while(menor <= mayor){ + // cout << res << ' ' << ciclo(menor) << endl; + res = max(res, ciclo(menor)); + menor++; + } + cout << res << endl; + } + +return 0; +} diff --git a/uva_cpp_clean/10000/10000-21.cpp b/uva_cpp_clean/10000/10000-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..101e55ec78ad6d9277c46d75a31b2097ddcea690 --- /dev/null +++ b/uva_cpp_clean/10000/10000-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10000 + Name: Longest Paths + Problem: https://onlinejudge.org/external/100/10000.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int cse = 1; + int n, s, ev[110*110], ew[110*110], d[100]; + while (cin>>n && n) { + cin>>s; + + int en; + for (en=0; cin>>ev[en]>>ew[en]; en++) + if (!ev[en] || !ew[en]) + break; + + for (int i=0; i<=n; i++) + d[i] = 1000000; + d[s] = 0; + + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10001 + Name: Garden of Eden + Problem: https://onlinejudge.org/external/100/10001.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define isset(n,i) (((n)>>(i))&1) + +int automaton,n; +bool nstate[40],bstate[40]; + +bool BT(int i) +{ + if(i==n) + { + int t1=4*bstate[n-2] + 2*bstate[n-1] + bstate[0]; + int t2=4*bstate[n-1] + 2*bstate[0] + bstate[1]; + if(nstate[n-1] == isset(automaton,t1) && nstate[0]==isset(automaton,t2)) + return true; + else + return false; + } + + bool ans=false; + + int t1=4*bstate[i-2] + 2*bstate[i-1] + 1; + if(isset(automaton,t1) == nstate[i-1]) + { + bstate[i]=1; + ans|= BT(i+1); + } + + + int t2=4*bstate[i-2] + 2*bstate[i-1] + 0; + if(isset(automaton,t2) == nstate[i-1]) + { + bstate[i]=0; + ans|= BT(i+1); + } + + return ans; +} + + +bool answer() +{ + int i; + for(i=0; i<8; i++) + if( isset(automaton,i) == nstate[1] ) + { + bstate[0]= isset(i,2); + bstate[1]= isset(i,1); + bstate[2]= isset(i,0); + if(BT(3)) + return true; + } + + return false; +} + + +int main() +{ + char s[40]; + int i; + + while(cin>>automaton>>n>>s) + { + for(i=0; s[i]; i++) + if(s[i]=='1') + nstate[i]=1; + else + nstate[i]=0; + + if(answer()) + puts("REACHABLE"); + else + puts("GARDEN OF EDEN"); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10003/10003-13.cpp b/uva_cpp_clean/10003/10003-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..471537f37d55d3709fef3a6e298db86d9553a4ee --- /dev/null +++ b/uva_cpp_clean/10003/10003-13.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 100 + +int n, l; +int dp[MAX][MAX]; + +int minCost(vector a, int left, int right) +{ + + if (right - left == 1) + { + return 0; + } + + if (dp[left][right] != -1) + { + return dp[left][right]; + } + + int ans = 999999999; + + for (int i = left + 1; i < right; i++) + { + ans = min(ans, minCost(a, left, i) + minCost(a, i, right) + (a[right] - a[left])); + } + + return dp[left][right] = ans; +} + +int main() +{ + cin >> l; + + while (l != 0) + { + cin >> n; + + vector points; + + memset(dp, -1, sizeof dp); + + points.push_back(0); + + for (int i = 0; i < n; i++) + { + int x; + + cin >> x; + + points.push_back(x); + } + + points.push_back(l); + + int ans = minCost(points, 0, n + 1); + + printf("The minimum cutting is %d.\n", ans); + + cin >> l; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10003/10003-15.cpp b/uva_cpp_clean/10003/10003-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5738b573beebc0b8c5d2d5a8a2d7b3c7fe858811 --- /dev/null +++ b/uva_cpp_clean/10003/10003-15.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +int length; +int cut[50], cost[51][51]; + +int dp(int x, int y) { + if (cost[x][y] > -1) { + return cost[x][y]; + } + + int ans = 2147483647, i; + + for (i = x + 1; i < y; i++) { + ans = min(ans, dp(x, i) + dp(i, y)); + } + + cost[x][y] = ans + cut[y] - cut[x]; + + return cost[x][y]; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + int i, n, j; + while (cin >> length && length) { + for (i = 0; i < 51; i++) { + for (j = i; j < 51; j++) + if (i + 1 == j) { + cost[i][j] = 0; + } else { + cost[i][j] = -1; + } + } + + cin >> n; + + cut[0] = 0; + cut[n + 1] = length; + + for (i = 1; i <= n; i++) { + cin >> cut[i]; + } + + cout << "The minimum cutting is " << dp(0, n + 1) << ".\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10003/10003-19.cpp b/uva_cpp_clean/10003/10003-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e57dde45a3256a1d29e8fe174239840f65d0e76 --- /dev/null +++ b/uva_cpp_clean/10003/10003-19.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +int grid[53][53]; +int l, n; +int cuts[53]; + +int mincost(int i, int j) { + if (grid[i][j] == -1) { + int min = 900000; + bool cut = false; + for (int m = i + 1; m < j; m++) { + cut = true; + int val = mincost(i, m) + mincost(m, j); + if (val < min) min = val; + } + grid[i][j] = cut? cuts[j] - cuts[i] + min: 0; + } + return grid[i][j]; +} + +int main() { + while (scanf("%d", &l) != EOF && l != 0) { + scanf("%d", &n); + cuts[0] = 0; + for (int i = 1; i <= n; i++) { + scanf("%d", &cuts[i]); + } + cuts[n+1] = l; + for (int i = 0; i <= n + 1; i++) { + for (int j = 0; j <= n + 1; j++) { + grid[i][j] = -1; + } + } + int min = mincost(0, n + 1); + printf("The minimum cutting is %d.\n", min); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10003/10003-20.cpp b/uva_cpp_clean/10003/10003-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdc41b1f88d6171e94de214f100bd58f3b3038d0 --- /dev/null +++ b/uva_cpp_clean/10003/10003-20.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +const int MAXN = 50; + +int l, n, c[MAXN+2]; +int dp[MAXN+2][MAXN+2]; + +int mcm( int x, int y ) { + if( x == (y - 1) ) return 0; + int &ret = dp[x][y]; + if( ret != -1 ) return ret; + ret = INT_MAX; + for( int i = x + 1 ; i <= (y - 1) ; ++i ) { + int temp = mcm( x, i ) + mcm( i, y ) + (c[y] - c[x]); + if( temp < ret ) ret = temp; + } + return ret; +} + +int main( int argc, char ** argv ) { + while( scanf( "%d", &l ) == 1 && l ) { + scanf( "%d", &n ); + c[0] = 0; + for( int i = 1 ; i <= n ; ++i ) scanf( "%d", &c[i] ); + c[n+1] = l; + + memset( dp, -1, sizeof( dp ) ); + int cost = mcm( 0, (n + 1) ); + + printf( "The minimum cutting is %d.\n", cost ); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10003/10003-21.cpp b/uva_cpp_clean/10003/10003-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f81b083bd0683e2c23de5569fd95ae5719ea031 --- /dev/null +++ b/uva_cpp_clean/10003/10003-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10003 + Name: Cutting Sticks + Problem: https://onlinejudge.org/external/100/10003.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +short int c[55],DP[55][55]; +int main(){ + int ll; + register int l,i,e,k,n,m; + + while(scanf("%d",&ll)==1 && ll){ + scanf("%d",&n); + for(i=1;i<=n;i++) + scanf("%d",c+i); + c[n+1]=ll; + + for(l=2; l<=n+1; l++) + for(i=0; i<=n+1-l; i++){ + e=i+l; + short int &m=DP[i][e]=32767; + for(k=i+1; k +#define ll long long +#define INF (ll)(1 << 30) +using namespace std; + +vector cuts; + +ll memo[1010][1010]; +ll dp(int l, int r) { + ll &ans = memo[l][r]; + if (ans != -1) return ans; + + ans = INF; + for (int cut : cuts) + if (cut > l && cut < r) + ans = min(ans, (ll) r-l + dp(cut, r) + dp(l, cut)); + + if (ans == INF) // nao tem corte no intervalo [l .. r] + ans = 0ll; // caso base + + return ans; +} + +int main() { + while (true) { + int len; cin >> len; + if (len == 0) return 0; + + int N; cin >> N; + cuts.resize(N); + for (int &cut : cuts) cin >> cut; + + memset(memo, -1ll, sizeof memo); + printf("The minimum cutting is %ld.\n", dp(0, len)); + } +} diff --git a/uva_cpp_clean/10003/10003-9.cpp b/uva_cpp_clean/10003/10003-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..745c06bf89ba1df71c50e8ed663504d2a7375480 --- /dev/null +++ b/uva_cpp_clean/10003/10003-9.cpp @@ -0,0 +1,27 @@ +#include + +#define MAX_N 60 + +using namespace std; + +int l,n,v[MAX_N],memo[MAX_N][MAX_N]; + +int cut(int _left,int _right){ + if(_left+1==_right) return 0; + if(~memo[_left][_right]) return memo[_left][_right]; + int ans = INT_MAX; + for(int i=_left+1;i<_right;i++) + ans = min(ans,cut(_left,i)+cut(i,_right)+(v[_right]-v[_left])); + return memo[_left][_right] = ans; +} + +int main(){ + while(scanf("%d",&l),l){ + memset(memo,-1,sizeof memo); + scanf("%d",&n); + for(int i=1;i<=n;i++) scanf("%d",&v[i]); + v[n+1] = l; + printf("The minimum cutting is %d.\n",cut(0,n + 1)); + } + return(0); +} diff --git a/uva_cpp_clean/10004/10004-10.cpp b/uva_cpp_clean/10004/10004-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/uva_cpp_clean/10004/10004-10.cpp differ diff --git a/uva_cpp_clean/10004/10004-12.cpp b/uva_cpp_clean/10004/10004-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5197eb1a16a075f9da8cdbe9cff168a4c0e43c6 --- /dev/null +++ b/uva_cpp_clean/10004/10004-12.cpp @@ -0,0 +1,97 @@ + #include + using namespace std; + + #define fastio \ + ios_base::sync_with_stdio(0); \ + cin.tie(NULL); \ + cout.tie(NULL) + #define PI(a, b) pair + #define MP make_pair + #define EB emplace_back + #define MOD 1000000007 + #define int long long int + #define S second + #define F first + #define FOR(var, len) for (var = 0; var < len; var++) + #define debug1 cout << "debug1" << '\n' + #define debug2 cout << "debug2" << '\n' + #define debug3 cout << "debug3" << '\n' + + // Maths Utils + int binExp(int a, int b, int m) + { + int r = 1; + while (b) + { + if (b % 2 == 1) + r = (r * a) % m; + + a = (a * a) % m; + b = b / 2; + } + return r; + } + + vector adj[205]; + int color[205]; + + bool bip(int s) + { + // vis[s] = 1; + color[s] = 0; + + queue q; + q.push(s); + + int src, des; + while (!q.empty()) + { + src = q.front(); + q.pop(); + for (auto des : adj[src]) + { + if (color[des] == color[src]) + return false; + else if (color[des] == -1){ + color[des] = 1 - color[src]; + q.push(des); + } + } + } + return true; + } + + int32_t main() + { + fastio; + while (1) + { + int n,m, a, b; + cin>>n; + if(n == 0)break; + cin >> m; + + for (int i = 0; i < m; i++) + { + cin >> a >> b; + adj[a].EB(b); + adj[b].EB(a); + } + + // for(auto x:adj){ + // for(auto y:x) + // cout< +#include +#include +#include +#include + +using namespace std; + +bool ok; +int n, m; +vector colored; +vector < vector > graph; + +void bfs(int start) +{ + queue bfsQ; + + bfsQ.push(start); + + colored[start] = 0; + + while (!bfsQ.empty() && ok) + { + int top = bfsQ.front(); + + bfsQ.pop(); + + for (int i = 0; i < graph[top].size(); i++) + { + int child = graph[top][i]; + + if (colored[child] == colored[top]) + { + ok = false; + } + + else if (colored[child] == -1) + { + colored[child] = !colored[top]; + + bfsQ.push(child); + } + } + } +} + +int main() +{ + while (cin >> n, n != 0) + { + cin >> m; + + ok = true; + + graph.clear(); + colored.clear(); + + graph.resize(n); + colored.resize(n, -1); + + for (int i = 0; i < m; i++) + { + int x, y; + + cin >> x >> y; + + graph[x].push_back(y); + graph[y].push_back(x); + } + + bfs(0); + + printf("%s\n", ok ? "BICOLORABLE." : "NOT BICOLORABLE."); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10004/10004-14.cpp b/uva_cpp_clean/10004/10004-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a72db52c969e90f2821a20010da580c1cd93f27e --- /dev/null +++ b/uva_cpp_clean/10004/10004-14.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#define MAX 205 +using namespace std; +typedef long long ll; +vectorV[MAX]; +int color[MAX]; ///white = 0, red = 1 ; blue = 2 +int bicoloring(int s) { + queueQ; + Q.push(s); + color[s] = 1; + while (!Q.empty()) { + int u = Q.front(); + Q.pop(); + int sz = V[u].size(); + for (int i = 0; i < sz; i++) { + int v = V[u][i]; + if (color[v] == 0) { + if (color[u] == 1) { + color[v] = 2; + } else { + color[v] = 1; + } + Q.push(v); + } + if (color[u] == color[v]) { + return 0; + } + } + } + return 1; + +} +int main() { + int n, e; + while (scanf("%d", &n) == 1 && n != 0) { + scanf("%d", &e); + for (int i = 1; i <= e; i++) { + int x, y; + scanf("%d %d", &x, &y); + V[x].push_back(y); + V[y].push_back(x); + } + int check = bicoloring(0); + if (check) { + printf("BICOLORABLE.\n"); + } else { + printf("NOT BICOLORABLE.\n"); + } + for (int i = 0; i < MAX; i++) { + V[i].clear(); + color[i] = 0; + } + } + return 0; +} diff --git a/uva_cpp_clean/10004/10004-18.cpp b/uva_cpp_clean/10004/10004-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5799f3b340ed750a306747e4087b51232a8a2d86 --- /dev/null +++ b/uva_cpp_clean/10004/10004-18.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +using namespace std; //Bicolorable graph == bipartite graph + +int matrizA[200][200] = { }, colores[200] = { }; + +bool DFS(int n){ + vector visitado(n, false); + bool bicoloreable = true; + stack pila; + pila.push(0); + colores[0] = 0; + while(!pila.empty() && bicoloreable){ + int vertice = pila.top(); + pila.pop(); + visitado[vertice] = true; + for(int i = 0; i < n; i++){ + if(matrizA[i][vertice] == 1 && !visitado[i]){ + visitado[i] = true; + colores[i] = 1 - colores[vertice]; + pila.push(i); + }else if(matrizA[i][vertice] == 1 && colores[i] == colores[vertice] && i != vertice){ + bicoloreable = false; + break; + } + } + } + visitado.clear(); + return bicoloreable; +} + +void reset(){ + for(int i = 0; i < 200; i++){ + colores[i] = 0; + for(int j = 0; j < 200; j++){ + matrizA[i][j] = 0; + } + } +} + +int main(){ + int n, l, vertice1, vertice2; + while(true) { + cin >> n; + if(n == 0) break; + cin >> l; + for(int i = 0; i < l; i++){ + cin >> vertice1 >> vertice2; + matrizA[vertice1][vertice2] = 1; + matrizA[vertice2][vertice1] = 1; + } + if(DFS(n)) cout << "BICOLORABLE." << endl; else cout << "NOT BICOLORABLE."< +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +vector adjList; +vi color; + +bool proc() { + bool ok = true; + queue q; + q.push(0); + color[0] = 0; + while (!q.empty() && ok) { + for (int i = 0; i < adjList[q.front()].size() && ok; i++) { + if (color[adjList[q.front()][i]] == -1) { + color[adjList[q.front()][i]] = 1-color[q.front()]; + q.push(adjList[q.front()][i]); + } else if (color[adjList[q.front()][i]] == color[q.front()]) ok = false; + } + q.pop(); + } + return ok; +} + +int main() { + int n; + while (cin >> n && n != 0) { + adjList.assign(n, vi()); + color.assign(n, -1); + int l; + cin >> l; + for (int i = 0; i < l; i++) { + int a1, a2; + cin >> a1 >> a2; + adjList[a1].push_back(a2); + adjList[a2].push_back(a1); + } + bool ok = proc(); + if (ok) printf("BICOLORABLE.\n"); + else printf("NOT BICOLORABLE.\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10004/10004-20.cpp b/uva_cpp_clean/10004/10004-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e300a5edcec8e501b19cba0e6debcb0a5feda87d --- /dev/null +++ b/uva_cpp_clean/10004/10004-20.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +const int MAXN = 200; +const int GRAY = -1; +const int WHITE = 0; +const int BLACK = 1; +vector v[MAXN+2]; +int color[MAXN+2]; + +bool dfs( int node, int c ) { + if( color[node] == (1 - c) ) { + return false; + } + if( color[node] == c ) { + return true; + } + color[node] = c; + bool ret = true; + for( int i = 0 ; (i < v[node].size()) && ret ; ++i ) { + int now = v[node][i]; + ret &= dfs( now, (1 - c) ); + } + return ret; +} + +int main( int argc, char ** argv ) { + int n, e; + while( scanf( "%d", &n ) == 1 && n ) { + scanf( "%d", &e ); + for( int i = 1 ; i <= e ; ++i ) { + int x, y; + scanf( "%d %d", &x, &y ); + v[x].push_back( y ); + v[y].push_back( x ); + } + + memset( color, GRAY, sizeof( color ) ); + bool flag = dfs( 0, WHITE ); + if( flag ) { + puts( "BICOLORABLE." ); + } + else { + puts( "NOT BICOLORABLE." ); + } + + for( int i = 0 ; i < n ; ++i ) { + v[i].clear(); + } + } + return 0; +} diff --git a/uva_cpp_clean/10004/10004-5.cpp b/uva_cpp_clean/10004/10004-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1b3e829083df1b274c51f9bd30058c560050a4f4 --- /dev/null +++ b/uva_cpp_clean/10004/10004-5.cpp @@ -0,0 +1,196 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// +int col[400]; +int vis[400]; + +vi g[400]; + +bool dfs(int u) +{ + vis[u]=1; + int n=sz(g[u]); + + for0(i,n) + { + if(!vis[g[u][i]]) + { + col[g[u][i]] = col[u]^1; + vis[g[u][i]]=1; + dfs(g[u][i]); + } + else if(col[u] ^ col[g[u][i]]==0) + return 0; + } + + return 1; +} + +int main() +{ + int n,e; + while(si(n) && n) + { + si(e); + for0(i,e) + { + int x,y; + sii(x,y); + g[x].pb(y); + g[y].pb(x); + } + + if(dfs(0)){ + puts("BICOLORABLE."); + } + else{ + puts("NOT BICOLORABLE."); + } + + for0(i,200) + { + g[i].clear(); + } + + cover(col,0); + cover(vis,0); + } + return 0; +} diff --git a/uva_cpp_clean/10004/10004-6.cc b/uva_cpp_clean/10004/10004-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..0b1c18173a86b26d340076fa5af12ec7855c77d4 --- /dev/null +++ b/uva_cpp_clean/10004/10004-6.cc @@ -0,0 +1,43 @@ +// https://uva.onlinejudge.org/external/100/10004.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +using qi=queue; +int main(){ + int n,m,u,v; + for(;;){ + cin>>n>>m; + if(!n)break; + vvi g(n); + for(int i=0;i>u>>v; + g[u].push_back(v); + g[v].push_back(u); + } + vi p(n),c(n); + qi q; + q.push(0); + p[0]=-1; + c[0]=2; + bool ok=1; + while(!q.empty()){ + u=q.front(); + int z=c[u]%2+1; + q.pop(); + for(int k:g[u]){ + if(!c[k]){ + p[k]=u; + q.push(k); + c[k]=z; + }else if(z!=c[k]){ + ok=0; + break; + } + } + if(!ok)break; + } + if(ok)cout<<"BICOLORABLE.\n"; + else cout<<"NOT BICOLORABLE.\n"; + } +} diff --git a/uva_cpp_clean/10004/10004-6.cpp b/uva_cpp_clean/10004/10004-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6065a1c48e92a468690303983790e1f171b3092 --- /dev/null +++ b/uva_cpp_clean/10004/10004-6.cpp @@ -0,0 +1,44 @@ +/* + graphs > specials > bipartite > bipartite checking + difficulty: easy + date: 31/Jan/2020 + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int main() { + int V; + while (cin >> V) { + if (V == 0) break; + vector adjList[202]; + + int E; cin >> E; + while (E--) { + int v1, v2; cin >> v1 >> v2; + adjList[v1].push_back(v2); + } + + vector color(V, -1); color[0] = 0; + queue q; q.push(0); + bool isBipartite = true; + + while (!q.empty() && isBipartite) { + int v = q.front(); q.pop(); + + for (int u : adjList[v]) { + if (color[u] == -1) { // cor ainda não definida (não visitado) + color[u] = 1 - color[v]; // 1 ou 0 + q.push(u); + } else if (color[u] == color[v]) { // conflito de cor + isBipartite = false; + break; + } + } + } + cout << (isBipartite? "BICOLORABLE." : "NOT BICOLORABLE.") << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10004/10004-9.cpp b/uva_cpp_clean/10004/10004-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74b561a517ca706c9e51850aea29f77ce2424b15 --- /dev/null +++ b/uva_cpp_clean/10004/10004-9.cpp @@ -0,0 +1,56 @@ +#include + +#define WHITE -1 +#define RED 0 +#define BLACK 1 + +using namespace std; + +const int MAX_V = 1000; + +int V, E, a, b, color[MAX_V]; +vector AdjList[MAX_V]; + +bool isBipartite(int source){ + bool isOK = true; + queue Q; + Q.push(source); + fill(color, color + V, WHITE); + color[source] = RED; + while( not Q.empty() and isOK ){ + int u = Q.front(); Q.pop(); + for(int j = 0; j < AdjList[u].size() and isOK; j++){ + int v = AdjList[u][j]; + if( color[v] == WHITE ) + color[v] = color[u] ^ 1, Q.push(v); + else if( color[v] == color[u] ) + isOK = false; + } + } + return isOK; +} + +void solve(){ + puts( isBipartite(0) ? "BICOLORABLE." : "NOT BICOLORABLE."); +} + +void read(){ + for(int i = 0; i < E; i++){ + scanf("%d %d", &a, &b); + AdjList[a].push_back(b); + AdjList[b].push_back(a); + } +} + +void clear(){ + for(int i = 0; i < V; i++) AdjList[i].clear(); +} + +int main(){ + while(scanf("%d %d", &V, &E), V){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/10005/10005-21.cpp b/uva_cpp_clean/10005/10005-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9e22f147c80ccfe7f19f7bf11ba8a6862e80a1b --- /dev/null +++ b/uva_cpp_clean/10005/10005-21.cpp @@ -0,0 +1,122 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10005 + Name: Packing polygons + Problem: https://onlinejudge.org/external/100/10005.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 1e-8 + +struct Point { + double x, y; + Point():x(0),y(0){} + Point(double x, double y):x(x),y(y){} + bool operator < (const Point &o) const { return x < o.x; } + bool operator == (const Point &o) const { return fabs(x-o.x)> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} + +double dist2(const Point p1, const Point p2) { + double xx = p1.x - p2.x, yy = p1.y - p2.y; + return xx*xx + yy*yy; +} + +struct Line { + double a, b, c; + Line():a(0),b(0),c(1){} + Line(double x, double y):a(x),b(y),c(1){} + Line(double a, double b, double c):a(a),b(b),c(c){} + Line(const Point p1, const Point p2) { + if (fabs(p1.x - p2.x) < EPS) { + b = 0; + if (fabs(p1.x) < EPS) a=1, c=0; + else a = -1/p1.x, c = 1; + } + else { + a = p2.y - p1.y; + b = p1.x - p2.x; + c = -p1.y*b - p1.x*a; + } + } + double y(double x0) { return (-a*x0 - c) / b; } + double x(double y0) { return (-b*y0 - c) / a; } +}; + +Line bisector(const Point &a, const Point &b) { + Line l(a, b); + Point m = (a+b) / 2; + swap(l.a, l.b); + l.b = -l.b; + l.c = -l.a*m.x - l.b*m.y; + return l; +} + +int intersect(const Line l1, const Line l2, Point &r) { + r.x = l1.b*l2.c - l2.b*l1.c; + r.y = l2.a*l1.c - l1.a*l2.c; + double cc = l1.a*l2.b - l2.a*l1.b; + if (fabs(cc) < EPS) + return fabs(r.y) cr+EPS) { + c = P[i]; cr = 0; + for (int j=0; j cr+EPS) { + c = (P[i] + P[j]) / 2; + cr = dist2(P[i], c); + for (int k=0; k cr+EPS) { + circle3pts(P[i], P[j], P[k], c); + cr = dist2(P[i], c); + } + } + } + return sqrt(cr); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + double r; + Point P[143]; + while (cin >> n && n) { + for (int i=0; i> P[i]; + cin >> r; + cout << (minCoverCircle(P, n) <= r + ? "The polygon can be packed in the circle.\n" + : "There is no way of packing that polygon.\n" + ); + } +} diff --git a/uva_cpp_clean/10005/10005-9.cpp b/uva_cpp_clean/10005/10005-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..277e2dde3e35f871226ce865a0337d920f5b6167 --- /dev/null +++ b/uva_cpp_clean/10005/10005-9.cpp @@ -0,0 +1,67 @@ +#include + +typedef double ptype; + +using namespace std; + +struct Point { + ptype x, y; + Point() {} + Point(ptype x_, ptype y_): x(x_), y(y_) {} +}; + +int N; +double R; +vector vert; +Point center; + +bool getCenter(Point p1, Point p2) { + double d2 = pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2); + double det = R * R / d2 - 0.25; + if (det < 0.0) return false; + double h = sqrt(det); + center.x = (p1.x + p2.x) * 0.5 + (p1.y - p2.y) * h; + center.y = (p1.y + p2.y) * 0.5 + (p2.x - p1.x) * h; + return true; +} + +bool packed () { + for (Point p_i: vert) { + double dis = pow(p_i.x - center.x, 2) + pow(p_i.y - center.y, 2); + if (dis > R * R) return false; + } + return true; +} + +bool possible () { + for (int i = 0; i < N; i++) { + for (int j = i + 1; j < N; j++) { + if (getCenter(vert[i], vert[j]) and packed()) return true; + if (getCenter(vert[j], vert[i]) and packed()) return true; + } + } + return false; +} + +bool readCase () { + scanf("%d", &N); + if (N == 0) return false; + vert.resize(N); + for (int i = 0; i < N; i++) scanf("%lf %lf", &vert[i].x, &vert[i].y); + scanf("%lf", &R); + return true; +} + +inline void clear () { + vert.clear(); +} + +int main () { + while (readCase()) { + puts(possible() + ? "The polygon can be packed in the circle." + : "There is no way of packing that polygon."); + clear(); + } + return (0); +} diff --git a/uva_cpp_clean/10006/10006-19.cpp b/uva_cpp_clean/10006/10006-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8faf08a02fefe4fe9b5cceb575873ab80dfc4cf2 --- /dev/null +++ b/uva_cpp_clean/10006/10006-19.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef unordered_set usi; + +ll sieve_size; +bitset<10000000> bs; +usi primes, carmichael; + +void sieve(ll upperbound = 65000) { + sieve_size=upperbound+1; bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.insert((int)i); + } +} + +long long pwr(long long b, long long e, long long mod) { + if (e == 0) return 1%mod; + if (e == 1) return b%mod; + long long aux = pwr(b, e/2, mod); + aux = (aux*aux)%mod; + if (e%2) aux = (aux*(b%mod))%mod; + return aux; +} + +int main() { + sieve(); + for (int i = 3; i <= 65000; i++) { + if (primes.count(i)) continue; + bool ok = true; + for (int j = 2; j < i && ok; j++) { + ok = pwr(j, i, i) == j; + } + if (ok) carmichael.insert(i); + } + int n; + while (cin >> n && n) { + if (carmichael.count(n)) printf("The number %d is a Carmichael number.\n", n); + else printf("%d is normal.\n", n); + } + return 0; +} diff --git a/uva_cpp_clean/10006/10006-20.cpp b/uva_cpp_clean/10006/10006-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df3aabc35e478ff6162cbe8a87e10be3d1ad3a18 --- /dev/null +++ b/uva_cpp_clean/10006/10006-20.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; + +int carmichael_number[15] = {561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341, 41041, 46657, 52633, 62745, 63973}; + +int main() +{ + int n; + + while( cin >> n && n ) + { + bool flag = false; + for(int i=0 ; i<15 ; i++) + if( n==carmichael_number[i] ) + flag = true; + + if( flag ) cout << "The number " << n << " is a Carmichael number." << endl; + else cout << n << " is normal." << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10006/10006-21.cpp b/uva_cpp_clean/10006/10006-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4540acd139122a5bbfbbc78ad5550cdc7e582887 --- /dev/null +++ b/uva_cpp_clean/10006/10006-21.cpp @@ -0,0 +1,88 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10006 + Name: Carmichael Numbers + Problem: https://onlinejudge.org/external/100/10006.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define N 66000 +unsigned int prime[N / 64]; +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) +void sieve() { + memset(prime, -1, sizeof(prime)); + unsigned int i, i2, sqrtN = (unsigned int)sqrt((double)N) + 1; + for(i = 3; i>6] &= ~(1<<((j>>1)&31)); + } +} + +bool isPrime(int n) { + return n==2 || ((n&1) && gP(n)); +} + +int powmod(int b, int p, int m) { + if (!p) return 1; + if (p==1) return b%m; + + long long int h = powmod(b, p>>1, m); + return ((p&1) ? h*((b*h)%m) : h*h)%m; +} + +bool fermat(int n, int a) { + return powmod(a, n, n) == a; +} + +bool isC[65001]; +bool isCarmichael(int n) { + if (isPrime(n)) return false; + for(int i=2; i>n && n) + printf(isCarmichael(n) ? "The number %d is a Carmichael number.\n" : "%d is normal.\n", n); +} + +// high probablity: sum of digits is zero +/* +#include +#include +using namespace std; + +bool isC[65001]; +int main(){ + int n; + isC[561]=isC[1105]=isC[1729]=isC[2465]=isC[2821]=isC[6601]= + isC[8911]=isC[10585]=isC[15841]=isC[29341]=isC[41041]= + isC[46657]=isC[52633]=isC[62745]=isC[63973]=true; + while(cin>>n && n) + printf(isC[n] ? "The number %d is a Carmichael number.\n" : "%d is normal.\n", n); +} +*/ diff --git a/uva_cpp_clean/10006/10006-4.cpp b/uva_cpp_clean/10006/10006-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d90b0962230046c9e66493f9e82fe89dca59c78a --- /dev/null +++ b/uva_cpp_clean/10006/10006-4.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int number; + int carmichaelNumbers[16] = {561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341, 41041, 46657, 52633, 62745, 63973}; + + do + { + cin >> number; + if (number == 0) + return 0; + + bool flag = false; + + for (int i = 0; i < 16; i++) + { + if (carmichaelNumbers[i] == number) + { + flag = true; + break; + } + } + + if (flag) + cout << "The number " << number << " is a Carmichael number." << endl; + else + cout << number << " is normal." << endl; + } while (number != 0); + + return 0; +} diff --git a/uva_cpp_clean/10007/10007-21.cpp b/uva_cpp_clean/10007/10007-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..482be78698b7a0dadb1be1a2bc5fcbf5ae41b096 --- /dev/null +++ b/uva_cpp_clean/10007/10007-21.cpp @@ -0,0 +1,337 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10007 + Name: Count the Trees + Problem: https://onlinejudge.org/external/100/10007.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + + if (cells) delete [] cells; + cells = ncells; + } + if (!cp) + memset(cells, 0, capacity*CellSize); + else memset(cells+sz, 0, (capacity-sz)*CellSize); + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + // --------------------------------------------------------------------- + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + setTo(x); + } + + BigInt(const char s[]) { + while (s[0]==' ' || s[0]=='\n' || s[0]=='\t') s++; + sign = 1; + if (s[0] == '+') s++; + if (s[0] == '-') { sign=-1; s++; } + while (s[0]=='0') s++; + if (!s[0]) { init(); return; } + + int len = strlen(s); + size = (len+3)/MAX_D; + cells = new CellType[capacity = size+16]; + for (int i=len-1; i>=0; i-=MAX_D) { + int rc = 0; + for (int k=i-MAX_D+1; k<=i; k++) { + if (k<0 || s[k]=='-' || s[k]=='+') continue; + rc = rc*10 + s[k] - '0'; + } + cells[(len-i-1) / MAX_D] = rc; + } + for (int i=size; i= 0) + delete [] cells; + } + + int getSize() { return size; } + int getCell(int i) { return cells[i]; } + bool isZero() { return !sign; } + BigInt& setOne() { sign=size=cells[0]=1; return *this; } + BigInt& setTo(int x) { + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + return *this; + } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { + return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign; + } + bool operator == (const BigInt &o) const { return sign==o.sign && numEq(o); } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + int osz = size; + setSize(max(size, o.size)+1); + for (int i=osz; i= MAX_C) { + cells[i] -= MAX_C; + ++cells[i+1]; + } + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (*this == o) { + sign = size = 0; + return *this; + } + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i 10000) { + return *this *= BigInt(x); + } + + sign *= sgn(x); + if (!sign) { + size = 0; + return *this; + } + if (x<0) x = -x; + + int cr = 0; + for (int i=0; i < size; i++) { + cells[i] = cells[i] * x + cr; + cr = cells[i] / MAX_C; + cells[i] %= MAX_C; + } + + if (cr) { + int ex = (countDigits(cr)+MAX_D-1)/MAX_D, sz=size; + setSize(size + ex); + size = sz; + for (; cr; cr /= MAX_C) + cells[size++] = cr % MAX_C; + } + + return autoSize(); + } + BigInt operator* (const BigInt &o) const { + if (sign*o.sign == 0) + return *new BigInt(0); + if (o.size == 1) + return *new BigInt(*this) *= o.cells[0]; + + BigInt r(sign*o.sign); + r.setSize(size + o.size + 1, 0); + for (int i=0; i=0; --i) { + t = t*MAX_C + cells[i]; + cells[i] = t / den; + t -= (long long) cells[i] * den; + } + return autoSize(); + } + BigInt operator/ (int den) const { return *new BigInt(*this) /= den; } + + int operator% (int den) const { + int r = 0; + for (int i = size-1; i>=0; --i) + r = (r * MAX_C + cells[i]) % den; + return r; + } + + // --------------------------------------------------------------------- + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + +// ---------------------------------------------------------------------------------- + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + BigInt F[313]; + F[0].setOne(); + F[1].setOne(); + for (int i=2; i<301; ++i) + F[i] = F[i-1] * i; + + BigInt C[313]; + C[0].setOne(); + for (int i=1; i<313; ++i) { + C[i] = C[i-1]; + C[i] *= 4*i-2; + C[i] /= i+1; + } + + int n; + while (cin >> n && n) + cout << C[n] * F[n] << endl; +} diff --git a/uva_cpp_clean/10007/10007-9.cpp b/uva_cpp_clean/10007/10007-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c1d255cef4e489275cb611e0bb838232481f3d --- /dev/null +++ b/uva_cpp_clean/10007/10007-9.cpp @@ -0,0 +1,14 @@ +from sys import stdin,stdout + +def main(): + rt = [1] + for it in range(1,310): + rt.append(rt[it-1]*(it<<1)*((it<<1)-1)//(it+1)) + while True: + idx = int(stdin.readline()) + if idx==0: + break + stdout.write(str(rt[idx])+"\n") + +if __name__ == "__main__": + main() diff --git a/uva_cpp_clean/10008/10008-14.cpp b/uva_cpp_clean/10008/10008-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4acfa8d5f063af96a06974360080231a1555049 --- /dev/null +++ b/uva_cpp_clean/10008/10008-14.cpp @@ -0,0 +1,100 @@ +/*************************************************** + * Problem name : 10008 What’s Cryptanalysis?.cpp + * Problem Link : https://uva.onlinejudge.org/external/100/10008.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-26 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,0,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 260 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +struct st { + int ch; + int num; +}; +bool compare(st x, st y) { + if (x.num == y.num) return x.ch < y.ch; + else return x.num > y.num; +} +int main () { + int n; + string str; + int mp[MAX]; + scanf("%d", &n); + getchar(); + SET(mp); + for (int l = 1; l <= n; l++) { + getline(cin, str); + int sz = str.size(); + for (int i = 0; i < sz; i++) { + if (isalpha(str[i])) { + if (str[i] >= 'a' && str[i] <= 'z') { + str[i] = toupper(str[i]); + } + mp[str[i]]++; + } + } + //~ cout << str; + } + st ar[27]; + int j = 0; + for (int i = 65; i <= 91; i++) { + if (mp[i] != 0) { + ar[j].ch = i; + ar[j].num = mp[i]; + j++; + //~ printf("%c %d\n", i, mp[i]); + } + } + sort(ar, ar + j, compare); + for (int i = 0; i < j; i++) { + printf("%c %d\n", ar[i].ch, ar[i].num); + } + return 0; +} + diff --git a/uva_cpp_clean/10008/10008-15.cpp b/uva_cpp_clean/10008/10008-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37281840f74127aacafd997e9175076a7a57af9d --- /dev/null +++ b/uva_cpp_clean/10008/10008-15.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +using namespace std; + +struct wrap { + int count; + char alphabet; + bool operator<(const wrap& other) const { + if (count == other.count) + return alphabet < other.alphabet; + return count > other.count; + } +}; + +int main() { + int n, t, cnt; + char c; + vector data(26); + + while (cin >> n) { + cin.ignore(1024, '\n'); + + for (auto&& i : data) + i.count = 0; + + for (int i = 0; i < n; i++) { + while ((c = cin.get()) != '\n') { + if (!isalpha(c)) + continue; + cnt++; + t = toupper(c) - 65; + data[t].count++; + data[t].alphabet = toupper(c); + } + } + + sort(data.begin(), data.end()); + + for (const auto& i : data) { + if (i.count) + cout << i.alphabet << " " << i.count << "\n"; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10008/10008-19.cpp b/uva_cpp_clean/10008/10008-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70c3e58ab0cc7ec6b1606b29428d63e69593aa8d --- /dev/null +++ b/uva_cpp_clean/10008/10008-19.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +using namespace std; + +int main() { + char c; + vector > v; + v.assign(30, pair(0, 0)); + for (int i = 0; i < 30; i++) v[i].second = i; + while (scanf("%c", &c) != EOF) + if (isalpha(c)) { + if (isupper(c)) v[c-'A'].first--; + else v[c-'a'].first--; + } + sort(v.begin(), v.end()); + for (int i = 0; i < 30; i++) + if (v[i].first < 0) printf("%c %d\n", v[i].second + 'A', -v[i].first); + else break; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10008/10008-20.cpp b/uva_cpp_clean/10008/10008-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce62162f1a2c06f69722b431de36b66f285a4bc0 --- /dev/null +++ b/uva_cpp_clean/10008/10008-20.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +using namespace std; + +const char character[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +int char_count[30]; + +int main() +{ + for(int i=0 ; i<30 ; i++) char_count[i] = 0; + + int tc, max_count; + + cin >> tc; + cin.get(); + while( tc-- ) + { + char line[1005]; + gets(line); + + max_count = 0; + for(int i=0 ; i='A' && line[i]<='Z' ) + { + char_count[line[i]-'A']++; + if( char_count[line[i]-'A']>max_count ) max_count = char_count[line[i]-'A']; + } + + if( line[i]>='a' && line[i]<='z' ) + { + char_count[line[i]-'a']++; + if( char_count[line[i]-'a']>max_count ) max_count = char_count[line[i]-'a']; + } + } + } + + for(int i=max_count ; i>0 ; i--) + for(int j=0 ; j<26 ; j++) + if( char_count[j]==i ) cout << character[j] << " " << char_count[j] << endl; + + return 0; +} diff --git a/uva_cpp_clean/10008/10008-5.cpp b/uva_cpp_clean/10008/10008-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cacee2285771be91813f89876de741c547c06e82 --- /dev/null +++ b/uva_cpp_clean/10008/10008-5.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; + +struct Map +{ + char c; + int count; +}; + +bool compare(Map a, Map b) +{ + if(a.count > b.count ) + { + return true; + } + else if(a.count == b.count) + { + return a.c < b.c; + } + else + { + return false; + } +// return a.c < b.c ; +} +int main() +{ + char c; + string s; + Map str[26]; + for(int i=0; i<26; i++) + { + str[i].c = i + 'A'; + str[i].count = 0; + } + int cases, len; + cin>>cases; + cin.ignore(); + while(cases--) + { + getline(cin, s); + len = s.length(); + for(int i=0; i +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, m; +map names; +map numes; +vector < vector > graph; + +void initialize() +{ + names.clear(); + + graph.clear(); + + graph.resize(n + 10); +} + +void makeGraph() +{ + int index = 1; + + for (int i = 0; i < n; i++) + { + int v, w; + string a, b; + + cin >> a >> b; + + if (names[a] == 0) + { + v = index++; + + names[a] = v; + + numes[v] = a; + } + + else + { + v = names[a]; + } + + if (names[b] == 0) + { + w = index++; + + names[b] = w; + + numes[w] = b; + } + + else + { + w = names[b]; + } + + graph[v].push_back(w); + graph[w].push_back(v); + } +} + +string bfs(int start, int end) +{ + vector parent(n + 10, -1); + + vector visited(n + 10, false); + + visited[start] = true; + + queue q; + + q.push(start); + + while (!q.empty()) + { + int top = q.front(); + + q.pop(); + + for (int i = 0; i < graph[top].size(); i++) + { + if (!visited[graph[top][i]]) + { + visited[graph[top][i]] = true; + + q.push(graph[top][i]); + + parent[graph[top][i]] = top; + } + } + } + + stack s; + + string ans = ""; + + for (int i = end; i != -1; i = parent[i]) + { + s.push(numes[i][0]); + } + + while (!s.empty()) + { + ans += s.top(); + + s.pop(); + } + + return ans; +} + +int main() +{ + bool first = true; + + int t; + + cin >> t; + + while (t--) + { + cin >> n >> m; + + initialize(); + + makeGraph(); + + for (int i = 0; i < m; i++) + { + string start, end; + + cin >> start >> end; + + cout << bfs(names[start], names[end]) << endl; + } + + if (t != 0) cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10009/10009-21.cpp b/uva_cpp_clean/10009/10009-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74e7caf9d318f390efabab6d6a263f62062f3fcb --- /dev/null +++ b/uva_cpp_clean/10009/10009-21.cpp @@ -0,0 +1,61 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10009 + Name: All Roads Lead Where? + Problem: https://onlinejudge.org/external/100/10009.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int T, n, q, par[26], asc[26][26]; +char c1[100], c2[100]; +int main(){ + cin>>T; + while (T--) { + cin >> n >> q; + + for (int i=0; i>c1>>c2; + + par[c2[0]-'A'] = c1[0]-'A'; + asc[c1[0]-'A'][c2[0]-'A']=1; + } + + for (int k=0; k<26; k++) { + asc[k][k]=1; + for (int i=0; i<26; i++) + if (asc[i][k]) + for (int j=0; j<26; j++) + if (asc[k][j]) + asc[i][j]=1; + } + + for (int i=0; i>c1>>c2; + int j, u=c1[0]-'A', v=c2[0]-'A'; + for (j=u; !asc[j][v]; j=par[j]) + putchar('A' + j); + putchar('A'+j); + list ll; + for (int k=v; k!=j; k=par[k]) + ll.push_front(k); + + for (int j: ll) putchar('A' + j); + putchar('\n'); + } + + if (T) { + memset(asc, 0, sizeof(asc)); + putchar('\n'); + } + } +} diff --git a/uva_cpp_clean/10009/10009-5.cpp b/uva_cpp_clean/10009/10009-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2d5e68f64fa851fe3cebcadc09a79bc6a0f09923 --- /dev/null +++ b/uva_cpp_clean/10009/10009-5.cpp @@ -0,0 +1,268 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +vector g[1000]; +int vis[1000],level[1000],par[1000]; + +string res=""; + +mci in; +mapmp; + +void bfs(int s,int d) +{ +// PP(s,d); + + queueq; + + cover(vis,0); + cover(level,0); + + q.push(s); + + vis[s]=1; + level[s]=0; + + while(!q.empty()) + { + int p=q.front(); + q.pop(); + + int len=g[p].size(); +// printf("%d ",p); + for0(i,len) + { + int node=g[p][i]; + if(vis[node]==0) + { + level[node]=level[p]+1; + vis[node]=1; + + q.push(node); + +// par[node]=p; + + if(node==d) + { + res+=mp[node]; + int k=level[node]; + k--; + while(k!=0) + { + int len=g[node].size(); + for0(i,len) + { + if(k==level[g[node][i]]) + { + res+=mp[g[node][i]]; + k--; + node=g[node][i]; + break; + } + } + } + res+=mp[s]; + } + } + } + } +} + +//void path(int child,int p) +//{ +// if(child==p) +// { +// res+=mp[p]; +// return; +// } +// path(par[child],p); +// +// res+=mp[child]; +//} + +int main() +{ +// input; +// output; + int t,no=0; + si(t); + while(t--) + { + int n,e; + sii(n,e); + + res=""; + + int node=1; + for0(i,n) + { + string a,b; + cin>>a>>b; + + if(in[a[0]]==0) + in[a[0]]=node++; + if(in[b[0]]==0) + in[b[0]]=node++; + + mp[in[a[0]]]=a[0]; + mp[in[b[0]]]=b[0]; + + g[in[a[0]]].pb(in[b[0]]); + g[in[b[0]]].pb(in[a[0]]); + } + + while(e--) + { + string a,b; + cin>>a>>b; + res=""; +// bfs(in[a[0]]); + bfs(in[a[0]],in[b[0]]); +// path(in[b[0]],in[a[0]]); + reverse(res.begin(),res.end()); + cout< + +using namespace std; + +int tc, m, q; +string s1, s2, u, v, _source, _target; +map > G; +set vert; +map d; +map p; + +void printPath(string u){ + if(u == _source){ + printf("%c", _source[0]); + return; + } + printPath(p[u]); + printf("%c", u[0]); +} + +void bfs(string source){ + for(auto it : vert) d[it] = -1; + queue Q; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + u = Q.front(), Q.pop(); + for(int i = 0; i < G[u].size(); i++){ + v = G[u][i]; + if(d[v] == -1) + d[v] = d[u] + 1, p[v] = u, Q.push(v); + } + if(d[_target] != -1) return; + } +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + G.clear(), vert.clear(); + scanf("%d %d\n", &m, &q); + for(int i = 0; i < m; i++){ + cin >> s1 >> s2; + G[s1].push_back(s2), G[s2].push_back(s1); + vert.insert(s1), vert.insert(s2); + } + while(q--){ + cin >> _source >> _target; + bfs(_source); + printPath(_target), putchar('\n'); + } + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10010/10010-18.cpp b/uva_cpp_clean/10010/10010-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..510bc355ee8a706c9f6adbae168d3f4d31662789 --- /dev/null +++ b/uva_cpp_clean/10010/10010-18.cpp @@ -0,0 +1,68 @@ +#include +#include + +using namespace std; + +int m, n; +const int dir[][2] = {{1,0},{-1,0},{0,-1},{0,1},{1,1},{1,-1},{-1,-1},{-1,1}}; +char wordSoup[60][60]; +bool found; + +void toLower(char word[]){ + for(int i = 0; word[i] > 0; i++){ + if (word[i] >= 'A' && word[i] <= 'Z') word[i] += 32; + } +} + +void foundWord(int i, int j, char wordArray[60]){ + for(int k = 0; k < 8; k++){ + int ni = i; + int nj = j; + int waI = 0; + while(wordSoup[ni][nj] == wordArray[waI]){ + ni += dir[k][0]; + nj += dir[k][1]; + waI++; + if(wordArray[waI] == '\0'){ + found = true; + return; + } + if (ni < 0 || ni >= m || nj < 0 || nj >= n) break; + } + } +} + +int main(){ + int cases, words; + cin >> cases; + while(cases-- > 0){ + cin >> m >> n; + memset(wordSoup, ' ', sizeof(wordSoup[0][0])*60*60); + for(int i = 0; i < m; i++){ + for(int j = 0; j < n; j++) cin >> wordSoup[i][j]; + toLower(wordSoup[i]); + } + cin >> words; + char wordArray[60]; + cin.getline(wordArray, 60); + for(int k = 0; k < words; k++){ + cin.getline(wordArray,60); + toLower(wordArray); + found = false; + for(int i = 0; i < m; i++){ + for(int j = 0; j < n; j++){ + if(wordSoup[i][j] == wordArray[0]){ + foundWord(i, j, wordArray); + if(found){ + cout << (i + 1) << " " << (j + 1) << endl; + break; + } + } + } + if(found) break; + } + } + if(cases != 0) cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10010/10010-19.cpp b/uva_cpp_clean/10010/10010-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa673f7a5aeb8129fb90a72fe59551662a371b55 --- /dev/null +++ b/uva_cpp_clean/10010/10010-19.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +using namespace std; + +string grid[51]; + +bool inside(int x, int y, int m, int n) { + return x < m && x >= 0 && y < n && y >= 0; +} + +bool search(const string & word, int x, int y, int pos, const int & incrx, const int & incry, const int & m, const int & n) { + if(tolower(word[pos]) == tolower(grid[x][y])) { + if (pos < word.length()-1) { + int found = false; + if (inside(x+incrx, y+incry, m, n)) found = search(word, x+incrx, y+incry, pos+1, incrx, incry, m, n); + return found; + } else { + return true; + } + } else return false; +} + +int main() { + int cases; + cin >> cases; + while (cases--) { + int m, n, k; + cin >> m >> n; + for (int i = 0; i < m; i++) cin >> grid[i]; + cin >> k; + string word; + for (int i = 0; i < k; i++) { + cin >> word; + bool found = false; + for (int x = 0; x < m && !found; x++) { + for (int y = 0; y < n && !found; y++) { + for (int incrx = -1; incrx <= 1 && !found; incrx++) { + for (int incry = -1; incry <= 1 && !found; incry++) { + if (incrx == 0 && incry == 0) continue; + found = search(word, x, y, 0, incrx, incry, m, n); + if (found) printf("%d %d\n", x+1, y+1); + } + } + } + } + } + if (cases) printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10010/10010-21.cpp b/uva_cpp_clean/10010/10010-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fac5616bb1137037c8f53478a19a912de9279331 --- /dev/null +++ b/uva_cpp_clean/10010/10010-21.cpp @@ -0,0 +1,58 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10010 + Name: Where's Waldorf? + Problem: https://onlinejudge.org/external/100/10010.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main() +{ + int tt;cin>>tt; + int m,n,k,i,j,u,bits,len; + char mat[1000][1000],word[1000],t; + while(tt--){ + cin>>m>>n;cin.ignore(1); + for(i=0;i>t; + if(isalpha(t))mat[i][j]=tolower(t); + } + cin.ignore(1); + } + cin>>k;cin.ignore(1); + + while(k--){ + cin>>word; + len=strlen(word); + for(i=0;i=n || mat[i][j+u]!=word[u])bits&=62; + if(i+u>=m || mat[i+u][j]!=word[u])bits&=61; + if(i+u>=m || j+u>=n || mat[i+u][j+u]!=word[u])bits&=59; + + if(j-u<0 || mat[i][j-u]!=word[u])bits&=55; + if(i-u<0 || mat[i-u][j]!=word[u])bits&=47; + if(i-u<0 || j-u<0 || mat[i-u][j-u]!=word[u])bits&=31; + } + if(u==len && bits){printf("%d %d\n",i+1,j+1);goto br;} + } + br:; + } + if(tt)cout< + +using namespace std; + +int n, m; +vector grid; +string s; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool isValid (const int r, const int c) { + return 0 <= r and r < n and 0 <= c and c < m; +} + +bool check (int r, int c, int pos, const int dir) { + if (pos == s.size() or grid[r][c] != s[pos]) return false; + if (pos == s.size() - 1) return true; + int nr = r + dr[dir]; + int nc = c + dc[dir]; + if (not isValid(nr, nc)) return false; + return check(nr, nc, pos + 1, dir); +} + +void processQueries () { + int k; + cin >> k; + while (k--) { + cin >> s; + transform(begin(s), end(s), begin(s), ::tolower); + for (int r = 0; r < n; r++) { + for (int c = 0; c < m; c++) { + bool ok = false; + for (int d = 0; d < 8 and not ok; d++) + if (check(r, c, 0, d)) ok = true; + if (ok) { + cout << r + 1 << ' ' << c + 1 << endl; + r = n, c = m; + } + } + } + } +} + +void readGrid () { + cin >> n >> m; + grid.resize(n); + for (int r = 0; r < n; r++) { + cin >> grid[r]; + transform(begin(grid[r]), end(grid[r]), begin(grid[r]), ::tolower); + } +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + readGrid(); + processQueries(); + if (tc) puts(""); + } + return (0); +} diff --git a/uva_cpp_clean/10013/10013-19.cpp b/uva_cpp_clean/10013/10013-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4215218f345a69cb198377be77b8dfa6e5142fcf --- /dev/null +++ b/uva_cpp_clean/10013/10013-19.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + +int main() { + int N; + cin >> N; + while (N--) { + int M; + cin >> M; + vector v1(M), v2(M); + for (int i = 0; i < M; i++) { + cin >> v1[i] >> v2[i]; + } + bool carry = false; + for (int i = M - 1; i >= 0; i--) { + int val = v1[i] + v2[i] - '0' - '0'; + if (carry) val++; + v1[i]= val%10 + '0'; + carry = val >= 10; + } + if (carry) printf("1"); + for (int i = 0; i < M; i++) printf("%c", v1[i]); + printf("\n"); + if (N) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10013/10013-5.cpp b/uva_cpp_clean/10013/10013-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5ff7493576280659492a7bedd4a330c22ad7a5e2 --- /dev/null +++ b/uva_cpp_clean/10013/10013-5.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + int t,k=1; + cin>>t; + while(t--) + { + int n,a,b,d=0; + vectorv; + vectorv1; + cin>>n; + for(int i=0;i>a; + cin>>b; + v.push_back(a+b); + } + for(int i=n-1,j=0;i>=0;i--) + { + if(v[i]>=10) + { + v1.push_back(v[i]%10); + d=v[i]/10; + v[i-1]=v[i-1]+d; + d=0; + } + else + v1.push_back(v[i]); + } + if(d!=0) + v1.push_back(d); + for(int i=v1.size()-1;i>=0;i--) + cout<<""< +#include +#include + +using namespace std; + +int main() +{ + int tc; + + cin >> tc; + while( tc-- ) + { + int n; + double a0, an; + cin >> n >> a0 >> an; + + double sum = 0.0; + for(int i=0 ; i> ci; + + sum += (n-i)*ci; + } + + cout << fixed << setprecision(2) << (an+(n*a0)-(2*sum))/(n+1) << endl; + + if( tc ) cout << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10014/10014-9.cpp b/uva_cpp_clean/10014/10014-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdb41b9d411195950110e18462c4b83c75672093 --- /dev/null +++ b/uva_cpp_clean/10014/10014-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int tc, n; +double a[3010], c[3010]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + scanf("%lf %lf", &a[0], &a[n+1]); + for(int i = 1; i <= n; i++) scanf("%lf", &c[i]); + double sum = 0; + for(int i = 1; i <= n; i++) sum += 2 * i * c[n + 1 - i]; + double ans = n * a[0] + a[n + 1] - sum; + ans /= n + 1; + printf("%.2lf\n", ans); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10015/10015-20.cpp b/uva_cpp_clean/10015/10015-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b464da89c2fc190f04cdeae620393f3652fa4d5a --- /dev/null +++ b/uva_cpp_clean/10015/10015-20.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +using namespace std; + +#define N 40000 +#define Check(N,pos) ((bool) ((N)&(1<<(pos)))) +#define Set(N,pos) ((N)=((N)|(1<<(pos)))) + +unsigned status[(N>>5)+1]; +vector < unsigned > primes; + +void bitSieve() +{ + unsigned i, j, sqrtN = unsigned (sqrt(N)); + for(i=3 ; i<=sqrtN ; i+=2) + if( !Check(status[i>>5], i&31) ) + for(j=i*i ; j<=N ; j+=(i<<1)) status[j>>5] = Set(status[j>>5], j&31); + + primes.push_back(2); + for(i=3 ; i<=N ; i+=2) + if( !Check(status[i>>5], i&31) ) primes.push_back(i); +} + +int main() +{ + bitSieve(); + + unsigned n; + while( scanf("%u", &n)==1 && n ) + { + unsigned pos = 0; + for(unsigned i=1 ; i<=n ; i++) pos = (pos+primes[n-i])%i; + + printf("%u\n", pos+1); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10016/10016-21.cpp b/uva_cpp_clean/10016/10016-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48a2cb0775f259cd79f8ab6da31456421297fd8f --- /dev/null +++ b/uva_cpp_clean/10016/10016-21.cpp @@ -0,0 +1,100 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10016 + Name: Flip-Flop the Squarelotron + Problem: https://onlinejudge.org/external/100/10016.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-'); + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +// ------------------------------------------------------------------------- + +int X[200][200]; +int main() { + int T = readInt(); + while (T--) { + int n = readInt(); + for (int i=0; i>1; ++i) { + int s = i, e = n-i-1, + l = n-2*i, md=l>>1, + cnt = readInt(); + + for (int j=0; j + +#define SIZE 110 + +using namespace std; + +int tc, n, t, option, v[SIZE][SIZE], aux[SIZE][SIZE], pib, px, py, dt, tmp; + +void complete(){ + for(int i = px; i <= py + dt; i++) + v[i][py] = aux[i][py], v[i][py + dt] = aux[i][py + dt]; + for(int j = py; j <= py + dt; j++) + v[px][j] = aux[px][j], v[px + dt][j] = aux[px + dt][j]; +} + +void transform1(int a, int b, int c, int d){ + for(int i = px; i <= px + dt; i++) + aux[i][py] = v[a * i + b][c * py + d], aux[i][py + dt] = v[a * i + b][c * (py + dt) + d]; + for(int j = py; j <= py + dt; j++) + aux[px][j] = v[a * px + b][c * j + d], aux[px + dt][j] = v[a * (px + dt) + b][c * j + d]; +} + +void transform2(int a,int b,int c,int d){ + for(int i = px; i <= px + dt; i++) + aux[i][py] = v[a * py + b][c * i + d], aux[i][py + dt] = v[a * (py + dt) + b][c * i + d]; + for(int j = py; j <= py + dt; j++) + aux[px][j] = v[a * j + b][c * px + d], aux[px + dt][j] = v[a * j + b][c * (px + dt) + d]; +} + +void Upside_Down(int r){ + px = py = tmp-r; + dt = (r<<1) + pib; + transform1(-1, n + 1, 1, 0); + complete(); +} + +void Left_Right(int r){ + px = py = tmp-r; + dt = (r<<1) + pib; + transform1(1, 0, -1, n + 1); + complete(); +} + +void Main_Diagonal(int r){ + px = py = tmp-r; + dt = (r<<1) + pib; + transform2(1, 0, 1, 0); + complete(); +} + +void Main_Inverse_Diagonal(int r){ + px = py = tmp-r; + dt = (r<<1) + pib; + transform2(-1, n + 1, -1, n + 1); + complete(); + +} + +void printMatrix(){ + for(int i = 1; i <= n; i++) + for(int j = 1;j <= n; j++) + printf( j != n ? "%d " : "%d\n", v[i][j]); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + pib = (n&1) ? 0 : 1; + tmp = (n+1) >> 1; + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) scanf("%d", &v[i][j]); + for(int ring = tmp - 1; ring >= 0; ring--){ + scanf("%d", &t); + while(t--){ + scanf("%d", &option); + if(option == 1) Upside_Down(ring); + else if(option == 2) Left_Right(ring); + else if(option == 3) Main_Diagonal(ring); + else Main_Inverse_Diagonal(ring); + } + } + printMatrix(); + } + return(0); +} diff --git a/uva_cpp_clean/10017/10017-20.cpp b/uva_cpp_clean/10017/10017-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c8d04cd9863dc22c5c487ccd41ff54665aed1d3 --- /dev/null +++ b/uva_cpp_clean/10017/10017-20.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +int n, m, nCase = 1; +vector A, B, C; + +void printHanoi() +{ + int aLen = A.size(), bLen = B.size(), cLen = C.size(); + + printf("A=>"); + if( aLen ) + { + printf(" "); + for(int i=0 ; i"); + if( bLen ) + { + printf(" "); + for(int i=0 ; i"); + if( cLen ) + { + printf(" "); + for(int i=0 ; i &A, vector &B, vector &C) +{ + if( !m ) return; + if( n==1 ) + { + C.push_back(A.back()); + A.pop_back(); + printHanoi(); + m--; + return; + } + towersOfHanoi(n-1, A, C, B); + if( !m ) return; + C.push_back(A.back()); + A.pop_back(); + printHanoi(); + m--; + if( !m ) return; + towersOfHanoi(n-1, B, A, C); +} + +int main() +{ + while( scanf("%d %d", &n, &m)==2 && (n || m) ) + { + A.clear(), B.clear(), C.clear(); + for(int i=n ; i>=1 ; i--) A.push_back(i); + + printf("Problem #%d\n\n", nCase); + printHanoi(); + towersOfHanoi(n, A, B, C); + nCase++; + } + return 0; +} diff --git a/uva_cpp_clean/10017/10017-9.cpp b/uva_cpp_clean/10017/10017-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..deb09688151add4327bef8b4b70254c1a5862e10 --- /dev/null +++ b/uva_cpp_clean/10017/10017-9.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int tc, n, m; +deque Tower[5]; + +void printTower(int id){ + printf("%c=>", id + 'A'); + if(Tower[id].empty()){ + putchar('\n'); + return; + } + printf(" "); + for(int it = 0, sz = Tower[id].size() - 1; it <= sz; it++) + printf( it != sz ? "%d " : "%d\n", Tower[id][it]); +} + +void printDisks(){ + putchar('\n'); + printTower(0); + printTower(1); + printTower(2); +} + +void backtrack(int disks, int source, int destination, int intermediate){ + if( m == 0 ) return; + if( disks == 1 ){ + m--; + Tower[destination].push_back(Tower[source].back()); + Tower[source].pop_back(); + printDisks(); + return; + } + backtrack(disks - 1, source, intermediate, destination); + backtrack(1, source, destination, intermediate); + backtrack(disks - 1, intermediate, destination, source); +} + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + for(int it = 0; it < 3; it++) Tower[it].clear(); + for(int disk = n; disk >= 1; disk--) + Tower[0].push_back(disk); + printf("Problem #%d\n", ++tc); + printDisks(); + backtrack(n, 0, 2, 1); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10018/10018-15.cpp b/uva_cpp_clean/10018/10018-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0145bf26c6420e430940e1995e6b6f94f4e26ce8 --- /dev/null +++ b/uva_cpp_clean/10018/10018-15.cpp @@ -0,0 +1,33 @@ +/* + 回文 +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + + int n_testcase; + cin >> n_testcase; + + while (n_testcase--) { + int step = 0; + string origin, temp; + cin >> origin; + temp = origin; + reverse(temp.begin(), temp.end()); + + do { + temp = origin = to_string(stoul(temp) + stoul(origin)); + reverse(temp.begin(), temp.end()); + step++; + } while (temp != origin); + + cout << step << " " << origin << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/10018/10018-19.cpp b/uva_cpp_clean/10018/10018-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20bc11c90e95ad367b68ee181988e022701b4e82 --- /dev/null +++ b/uva_cpp_clean/10018/10018-19.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +bool revadd(unsigned int & n){ + unsigned int inv = 0, aux = n; + while (aux != 0) { + inv *= 10; + inv += (aux%10); + aux /= 10; + } + if (inv == n) return true; + n += inv; + return false; +} + +int main() { + int n; + scanf("%d", &n); + while (n--) { + unsigned int p; + scanf("%u", &p); + int cont = 0; + while (!revadd(p)) cont++; + if (cont == 0) { + p *= 2; + while (!revadd(p)) cont++; + } + printf("%d %u\n", cont, p); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10018/10018-5.cpp b/uva_cpp_clean/10018/10018-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..34a7f0eac6e0f3210c25d1b654d30286901a3588 --- /dev/null +++ b/uva_cpp_clean/10018/10018-5.cpp @@ -0,0 +1,36 @@ +#include +int main() +{ + long long n,a,s,c; + int t,i; + while(scanf("%d",&t)==1) + { + for(i=1;i<=t;i++) + { + scanf("%lld",&n); + a=n; + c=0; + while(1) + { + s=0; + while(n!=0) + { + s=s*10+n%10; + n/=10; + } + if(s==a) + break; + else + { + n=s+a; + a=s+a; + c++; + } + + } + printf("%lld %lld\n",c,a); + } + } + + return 0; +} diff --git a/uva_cpp_clean/10018/10018-9.cpp b/uva_cpp_clean/10018/10018-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39a7b57741f9c8265eeeb4b4cbbac9b3c6f0a0fe --- /dev/null +++ b/uva_cpp_clean/10018/10018-9.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int tc, ct; +unsigned num; + +unsigned reversed_number(unsigned n){ + unsigned pib = 0; + while(n) pib = pib * 10 + n % 10, n /= 10; + return pib; +} + +void Lychrel_number(){ + ct = 0; + while( ct < 50 ){ + ct++; + num += reversed_number(num); + if( num == reversed_number(num) ) break; + } +} + +int main(){ + scanf("%d", &tc); + while( tc-- ){ + scanf("%u", &num); + Lychrel_number(); + printf("%d %u\n", ct, num); + } + return(0); +} diff --git a/uva_cpp_clean/10019/10019-15.cpp b/uva_cpp_clean/10019/10019-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9eaeb4e4cb2e5db3b01f2bb42aa3ca182ce5b15 --- /dev/null +++ b/uva_cpp_clean/10019/10019-15.cpp @@ -0,0 +1,22 @@ +/* + 進位轉換 +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + + string number; + int n_testcase; + cin >> n_testcase; + while (n_testcase--) { + cin >> number; + cout << __builtin_popcount(stoi(number, NULL, 10)) << " " << __builtin_popcount(stoi(number, NULL, 16)) << "\n"; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10019/10019-19.cpp b/uva_cpp_clean/10019/10019-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b83a4fcc0fc45f4320149cdd2174c84fa51f22d --- /dev/null +++ b/uva_cpp_clean/10019/10019-19.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +int bits(int hexDigit) { + switch(hexDigit) { + case 0: return 0; + case 1: return 1; + case 2: return 1; + case 3: return 2; + case 4: return 1; + case 5: return 2; + case 6: return 2; + case 7: return 3; + case 8: return 1; + case 9: return 2; + } + return 0; +} + +int main() { + int N; + scanf("%d", &N); + for (int i = 0; i < N; i++) { + int M1, M2, b1 = 0, b2 = 0; + scanf("%d", &M1); + M2 = M1; + while (M1 != 0) { + b1 += M1 % 2; + M1 = M1 >> 1; + } + while (M2 != 0) { + b2 += bits(M2 % 10); + M2 /= 10; + } + printf("%d %d\n", b1, b2); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10019/10019-20.cpp b/uva_cpp_clean/10019/10019-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..155aea75e81a7a88197b2a6cab0b774e5ce184ae --- /dev/null +++ b/uva_cpp_clean/10019/10019-20.cpp @@ -0,0 +1,50 @@ +#include +#include + +using namespace std; + +int dec_bin(int a) +{ + int temp, one = 0; + while( a!=0 ) + { + temp = a%2; + a /= 2; + + if( temp==1 ) ++one; + } + return one; +} + +int hex_bin(int a) +{ + int temp, one = 0; + while( a!=0 ) + { + temp = a%10; + a /= 10; + + if( temp==1 || temp==2 || temp==4 || temp==8 ) ++one; + else if( temp==3 || temp==5 || temp==6 || temp==9 ) one += 2; + else if( temp==7 ) one += 3; + } + return one; +} + +int main() +{ + int tc; + + cin >> tc; + while( tc-- ) + { + int M; + cin >> M; + + int b1 = dec_bin(M); + int b2 = hex_bin(M); + + cout << b1 << " " << b2 << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10019/10019-5.cpp b/uva_cpp_clean/10019/10019-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1602e45e12d0fe5ae7f5f5f8d7d26af086312598 --- /dev/null +++ b/uva_cpp_clean/10019/10019-5.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +int a[1000],b[1000]; +int binary(int n) +{ + int s,c=0; + while(n!=0) + { + s=n%2; + n=n/2; + if(s==1) + c++; + } + return c; +} +int hex(int n) +{ + int s,c=0; + while(n!=0) + { + s=n%10; + n=n/10; + if(s==0) + c+=0; + else if (s==1||s==2||s==4||s==8) + c+=1; + else if (s==3||s==5||s==6||s==9) + c+=2; + else if(s==7) + c+=3; + } +return c; +} +int main() +{ + int n,m; + scanf("%d",&n); + while(n--) + { + + scanf("%d",&m); + int b=binary(m); + int c=hex(m); + printf("%d %d\n",b,c); + } + return 0; +} diff --git a/uva_cpp_clean/10019/10019-9.cpp b/uva_cpp_clean/10019/10019-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0766a69740057a53fa4c4ac93bc6c402926f826e --- /dev/null +++ b/uva_cpp_clean/10019/10019-9.cpp @@ -0,0 +1,3 @@ +for N in range( int( input() ) ): + M = int( input() ) + print("%d %d" %(bin(M).count('1'), bin(sum(16 ** pos * int(digit) for pos, digit in enumerate(reversed(str(M))))).count('1'))) \ No newline at end of file diff --git a/uva_cpp_clean/10020/10020-19.cpp b/uva_cpp_clean/10020/10020-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4976d84572631d81bd6d8e7520e8265c1a6b6068 --- /dev/null +++ b/uva_cpp_clean/10020/10020-19.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; + +int main() { + int N; + cin >> N; + while (N--) { + int M; + cin >> M; + int L, R; + vector v; + while (cin >> L >> R && !(L == 0 && R == 0)) v.push_back(make_pair(L, R)); + sort(v.begin(), v.end()); + vector sol; + int last = 0; + bool covered = false; + for (int i = 0; i < v.size() && !covered; i++) { + if (v[i].first > last) break; + ii maxi = v[i]; + while (i < v.size() && v[i].first <= last) { + if (v[i].second > maxi.second) maxi = v[i]; + i++; + } + if (i < v.size()) i--; + if (maxi.second > last) { + last = maxi.second; + sol.push_back(maxi); + } + if (last >= M) covered = true; + } + if (covered) { + printf("%d\n", sol.size()); + for (int i = 0; i < sol.size(); i++) printf("%d %d\n", sol[i].first, sol[i].second); + } else printf("0\n"); + if (N != 0) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10020/10020-21.cpp b/uva_cpp_clean/10020/10020-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd979205ccad99e6177408af1c701f5e93f0c2ae --- /dev/null +++ b/uva_cpp_clean/10020/10020-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10020 + Name: Minimal coverage + Problem: https://onlinejudge.org/external/100/10020.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +struct line{ + int s,e; + bool operator<(const line &t)const{ + if(s!=t.s)return st.e; + } +}; + +line a[100001]; +bool ch[100001]; +int main(){ + int T,i,j,k,m,n,cnt,cur; + + cin>>T; + while(T--){ + cin>>m; + n=0; + while(scanf("%d%d",&a[n].s,&a[n].e)==2 && (a[n].s||a[n].e)) + ch[n++]=0; + + sort(a,a+n); + cur=cnt=0; + + for(i=0;ia[k].e) + k=j; + + ch[k]=1; + cnt++; + cur=a[k].e; + i=k; + } + + if(cur>=m){ + cout< + +using namespace std; + +typedef pair pii; + +int tc, l, r, M, pib, _max; +vector v,ans; + +int main(){ + scanf("%d", &tc); + while(tc--){ + v.clear(), ans.clear(); + scanf("%d", &M); + while(scanf("%d %d", &l, &r), l + r) v.push_back( make_pair(l, r) ); + sort(v.begin(), v.end()); + l = _max = 0; + for(int i = 0; i < v.size(); i++){ + pib = -1; + for(int j = i; j < v.size(); j++){ + if(v[j].first > l){ + i = j - 1; + break; + } + if(v[j].second > _max) _max = v[j].second, pib = j; + } + if(pib == -1) break; + ans.push_back(v[pib]), l = _max; + if(_max >= M) break; + } + if(ans.back().second < M) ans.clear(); + printf("%d\n", (int)ans.size()); + for(int i = 0; i < ans.size(); i++) printf("%d %d\n", ans[i].first, ans[i].second); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10025/10025-15.cpp b/uva_cpp_clean/10025/10025-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..def38a55a7ca3801ee937678000e24f291a8a059 --- /dev/null +++ b/uva_cpp_clean/10025/10025-15.cpp @@ -0,0 +1,37 @@ +/* + 找規律 +*/ +#include + +using namespace std; + +int main() { + int n_testcase; + cin >> n_testcase; + + while (n_testcase--) { + int n = 0, sum = 0, k; + cin >> k; + + if (k == 0) { + if (n_testcase) + cout << "3\n\n"; + else + cout << "3\n"; + } else { + k = k > 0 ? k : -k; + + while (sum < k) + sum += (++n); + + if ((sum - k) & 1) + n += n & 1 ? 2 : 1; + + if (n_testcase) + cout << n << "\n\n"; + else + cout << n << "\n"; + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10025/10025-18.cpp b/uva_cpp_clean/10025/10025-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..67e4790aa11181f1f5b31214f686a9e852e2b322 --- /dev/null +++ b/uva_cpp_clean/10025/10025-18.cpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +int main(){ + int testCase; + long long K, N; + cin >> testCase; + while(testCase-- > 0){ + cin >> K; + K = abs(K); + N = ceil((sqrt(1+8*K)-1)/2); + if(K == 0) cout << 3 << endl; + else if(N*(N+1)/2 == K) cout << N << endl; + else{ + for(long long i = N;; i++){ + if((i*(i+1)/2 - K)%2 == 0){ //Caso cuando la formula da diferente de K + cout << i << endl; + break; + } + } + } + if(testCase != 0) printf("\n"); + } + return 0; +} +/*Formulas: + (1) n(n+1)/2 = K + (2) n(n+1)/2 - K = 2b + (4) n = (sqrt(1+8*K)-1)/2 + Para hallar (2): + Se tiene que n(n+1)/2 = Z (3), donde Z <> K. Se reexpresa Z, entonces: Z = a + b. La sumatoria actual para que + de K se necesita que tenga terminos restandose, entonces, K = a - b. Reemplazando estas ecuaciones en (3) se + obtiene (2). +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10025/10025-19.cpp b/uva_cpp_clean/10025/10025-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15009520a6233e2ad39bf92f53b9f4fc6af1a095 --- /dev/null +++ b/uva_cpp_clean/10025/10025-19.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int main() { + int N; + cin >> N; + while (N--) { + long long k; + cin >> k; + if (k == 0) printf("3\n"); + else { + if (k < 0) k = -k; + long long val = (floor(sqrt(1+8*k))-1)/2; + long long S = (val+1)*val/2; + while (S < k || S%2 != k%2) { + val++; + S = (val+1)*val/2; + } + printf("%lld\n", val); + } + if (N) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10025/10025-21.cpp b/uva_cpp_clean/10025/10025-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f06ff154848f5bf9424e1dc271e5c2a10655fe0 --- /dev/null +++ b/uva_cpp_clean/10025/10025-21.cpp @@ -0,0 +1,58 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10025 + Name: The ? 1 ? 2 ? ... ? n = k problem + Problem: https://onlinejudge.org/external/100/10025.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 1073741824 +#define MAXM 20000000 + +int memo[MAXM]; +int find(int n) { + if (n < MAXM) { + if (memo[n]>0) + return memo[n]; + memo[n] = INF; + } + + int k = ceil((sqrt(8.0*n+1)-1)/2); + for (;; k++) { + int sm = (k*(k+1))>>1; + int di = sm - n; + if (!di) return k; + if (!(di&1)) { + int dh = di>>1; + if (dh<=k || find(dh)<=k) { + if (n < MAXM) + memo[n]=k; + return k; + } + } + } + return INF; +} + + +int main(){ + int T, n; + cin>>T; + while (T--) { + cin >> n; + if (n<0) n=-n; + if (!n) puts("3"); + else if (n==1) puts("1"); + else cout << find(n) << endl; + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/10026/10026-19.cpp b/uva_cpp_clean/10026/10026-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35c179e97fbaa353b2e1c5205584db2b1a12fbaa --- /dev/null +++ b/uva_cpp_clean/10026/10026-19.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iii; + +bool op(iii p1, iii p2) { + return p1.first.second*p2.first.first > p2.first.second*p1.first.first; +} + +int main() { + int cases; + vector v; + scanf("%d", &cases); + bool first2 = false; + while (cases--) { + v.clear(); + if (first2) printf("\n"); + else first2 = true; + int N; + scanf("%d", &N); + for (int i = 0; i < N; i++) { + int f, s; + scanf("%d %d", &f, &s); + v.push_back(iii(ii(f, s), i)); + } + stable_sort(v.begin(), v.end(), op); + bool first = false; + for (int i = 0; i < v.size(); i++) { + if (first) printf(" "); + else first = true; + printf("%d", v[i].second + 1); + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10029/10029-19.cpp b/uva_cpp_clean/10029/10029-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..895bbad0e8d9f5ea0c1f3a7b2de560b6da5bd30e --- /dev/null +++ b/uva_cpp_clean/10029/10029-19.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +typedef vector vi; + +vector AdjList; + +string dic[25010]; +int dp[25010]; +int N = 0; + +int proc(int i) { + if (dp[i] == 0) { + dp[i] = 1; + for (int j = 0; j < AdjList[i].size(); j++) { + dp[i] = max(dp[i], 1 + proc(AdjList[i][j])); + } + } + return dp[i]; +} + +int main() { + while (cin >> dic[N]) N++; + AdjList.assign(N, vi()); + for (int i = 0; i < N; i++) { + for (int j = i + 1; j < N; j++) { + if (dic[i].length() == dic[j].length()) { + int diff = 0; + for (int k = 0; k < dic[j].length() && diff < 2; k++) { + if (dic[i][k] != dic[j][k]) diff++; + } + if (diff < 2) AdjList[i].push_back(j); + } + else if (dic[i].length() == dic[j].length() + 1) { + int diff = 0; + for (int k = 0; k < dic[j].length() && diff < 2; k++) { + if (dic[i][k + diff] != dic[j][k]) diff++, k--; + } + if (diff < 2) AdjList[i].push_back(j); + } + else if (dic[i].length() + 1 == dic[j].length()) { + int diff = 0; + for (int k = 0; k < dic[i].length() && diff < 2; k++) { + if (dic[i][k] != dic[j][k + diff]) diff++, k--; + } + if (diff < 2) AdjList[i].push_back(j); + } + } + } + int M = 0; + for (int i = 0; i < N; i++) { + M = max(M, proc(i)); + } + printf("%d\n", M); + return 0; +} diff --git a/uva_cpp_clean/10033/10033-20.cpp b/uva_cpp_clean/10033/10033-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e44f9c3160a93b43c45ee2f3c57fa8d6b8e95e0e --- /dev/null +++ b/uva_cpp_clean/10033/10033-20.cpp @@ -0,0 +1,58 @@ +#include +#include + +#define MOD 1000 + +int RAM[1000][3], Register[10]; +char input[5], flag = '0'; + +int main() +{ + int tc; + + scanf("%d\n", &tc); + while( tc-- ) + { + memset(RAM, 0, sizeof(RAM)); + memset(Register, 0, sizeof(Register)); + + for(int i=0 ; gets(input) && input[0] ; i++) + { + RAM[i][0] = input[0]-'0'; + RAM[i][1] = input[1]-'0'; + RAM[i][2] = input[2]-'0'; + } + + int nInstruction = 1; + for(int i=0 ; ; i++) + { + if( RAM[i][0]==1 && RAM[i][1]==0 && RAM[i][2]==0 ) break; + else if( RAM[i][0]==2 ) Register[RAM[i][1]] = RAM[i][2]; + else if( RAM[i][0]==3 ) Register[RAM[i][1]] = (RAM[i][2]+Register[RAM[i][1]])%MOD; + else if( RAM[i][0]==4 ) Register[RAM[i][1]] = (Register[RAM[i][1]]*RAM[i][2])%MOD; + else if( RAM[i][0]==5 ) Register[RAM[i][1]] = Register[RAM[i][2]]; + else if( RAM[i][0]==6 ) Register[RAM[i][1]] = (Register[RAM[i][2]]+Register[RAM[i][1]])%MOD; + else if( RAM[i][0]==7 ) Register[RAM[i][1]] = (Register[RAM[i][1]]*Register[RAM[i][2]])%MOD; + else if( RAM[i][0]==8 ) + { + int a = Register[RAM[i][2]]; + Register[RAM[i][1]] = (100*RAM[a][0]) + (10*RAM[a][1]) + RAM[a][2]; + } + else if( RAM[i][0]==9 ) + { + int a = Register[RAM[i][2]]; + RAM[a][0] = Register[RAM[i][1]]/100; + RAM[a][1] = (Register[RAM[i][1]]%100)/10; + RAM[a][2] = Register[RAM[i][1]]%10; + } + else if( RAM[i][0]==0 && Register[RAM[i][2]] ) i = Register[RAM[i][1]] - 1; + + nInstruction++; + } + + if( flag=='1' ) printf("\n"); + flag = '1'; + printf("%d\n", nInstruction); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10034/10034-13.cpp b/uva_cpp_clean/10034/10034-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d93743b6bd7ecced5f3a1bb38c5480bf9fadfce9 --- /dev/null +++ b/uva_cpp_clean/10034/10034-13.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int t, n; +vector parent, sz; +vector < pair > point; + +class comparator +{ +public: + + bool operator()(pair> &x, pair> &y) + { + return x.first > y.first; + } +}; + +priority_queue >, vector>>, comparator> pq; + +void initialize() +{ + point.clear(); + parent.clear(); + sz.clear(); + + sz.resize(n, 1); + parent.resize(n); + + for (int i = 0; i < n; i++) + parent[i] = i; +} + +double dist(double a, double b, double c, double d) +{ + return sqrt(((a - c)*(a - c)) + ((b - d)*(b - d))); +} + +void makeGraph() +{ + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if(i != j) + { + pq.push({ dist(point[i].first, point[i].second, point[j].first, point[j].second), {i, j} }); + } + } + } +} + +int find(int x) +{ + if (x == parent[x]) + { + return parent[x]; + } + + else + { + return parent[x] = find(parent[x]); + } +} + +bool merge(int x, int y) +{ + int px = find(x); + int py = find(y); + + if(px == py) + { + return false; + } + + else + { + if(sz[px] < sz[py]) + { + swap(px, py); + } + + parent[py] = px; + + sz[px] += sz[py]; + + return true; + } +} + +double mst() +{ + double how = 0; + int count = 0; + + while (!pq.empty()) + { + pair< double, pair > top = pq.top(); + + pq.pop(); + + if(count < n - 1 && merge(top.second.first, top.second.second)) + { + how += top.first; + + count++; + } + } + + return how; +} + +int main() +{ + bool ok = false; + + cin >> t; + + while (t--) + { + cin >> n; + + initialize(); + + for (int i = 0; i < n; i++) + { + double x, y; + + cin >> x >> y; + + point.push_back({ x,y }); + } + + makeGraph(); + + if (!ok) ok = true; + + else cout << endl; + + printf("%.2f\n", mst()); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10034/10034-18.cpp b/uva_cpp_clean/10034/10034-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..62f5bfd63ea5805462501799dc5a7d9f18fbda70 --- /dev/null +++ b/uva_cpp_clean/10034/10034-18.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +int parent[200]; + +float getDist(pair p1, pair p2){ + float x = p1.first - p2.first, y = p1.second - p2.second; + return sqrt(x*x + y*y); +} + +int cmp(const pair >& p1, const pair >& p2){ + return p1.first < p2.first; +} + +void makeSet(int n){ + for(int i = 0; i < n; i++) parent[i] = i; +} + +int find(int i){ + if(i == parent[i]) return i; + return find(parent[i]); +} + +bool sameSet(int i, int j){ + return find(i) == find(j); +} + +void unionSet(int i, int j){ + int iRoot = find(i), jRoot = find(j); + parent[iRoot] = jRoot; +} + +int main(){ + int n, tc; + float x, y; + vector > points; + vector > > edges; + vector > > kruskal; + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + points.clear(); + edges.clear(); + kruskal.clear(); + makeSet(n); + for(int i = 0; i < n; i++) scanf("%f %f", &x, &y), points.push_back(make_pair(x, y)); + for(int i = 0; i < n; i++){ + for(int j = i + 1; j < n; j++){ + float w = getDist(points[i], points[j]); + pair nodes = make_pair(i, j); + edges.push_back(make_pair(w, nodes)); + } + } + sort(edges.begin(), edges.end(), cmp); + for(int i = 0; i < edges.size(); i++){ + int nodeI = edges[i].second.first, nodeJ = edges[i].second.second; + if(!sameSet(nodeI, nodeJ)){ + kruskal.push_back(edges[i]); + unionSet(nodeI, nodeJ); + } + } + float total = 0; + for(int i = 0; i < kruskal.size(); i++) total+=kruskal[i].first; + printf("%.2f\n", total); + if(tc) printf("\n"); + } + return 0; +} +/* + Problema sencillo, la estrategia es encontrar el MST y sumar los pesos de este para obtener la respuesta. + Para hallar el MST hay que armar el grafo, osea, cada coordenada son nodos y el grafo lo sacas uniendo cada + puntos con todos, es decir, obtener un grafo Kn. Luego, a ese grafo le sacas el MST, yo use Kruskal por + cuestiones de practica. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10034/10034-19.cpp b/uva_cpp_clean/10034/10034-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e0ff0234bc215d087a6d45b5ea782feab7b8b8c --- /dev/null +++ b/uva_cpp_clean/10034/10034-19.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef pair point; +typedef pair ii; +typedef pair line; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +priority_queue pq; +vector v; + +int main() { + int cases; + cin >> cases; + while (cases--) { + v.clear(); + int n; + cin >> n; + for (int i = 0; i < n; i++) { + double x, y; + cin >> x >> y; + v.push_back(point(x,y)); + for (int j = 0; j < v.size() - 1; j++) { + line l; l.second.first = j; l.second.second = i; + double dx = x - v[j].first, dy = y - v[j].second; + l.first = -sqrt(dx*dx + dy*dy); + pq.push(l); + } + } + ufds u(n); + double d = 0; + while (!pq.empty()) { + if (!u.isSameSet(pq.top().second.first, pq.top().second.second)) { + d -= pq.top().first; + u.unionSet(pq.top().second.first, pq.top().second.second); + } + pq.pop(); + } + printf("%.2f\n", d); + if (cases) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10034/10034-20.cpp b/uva_cpp_clean/10034/10034-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce0b87ed0e69f9624e65cd3cd8ebf534f945c0ac --- /dev/null +++ b/uva_cpp_clean/10034/10034-20.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +const int MAXN = 100; + +struct coordinate { + double x, y; +} point[MAXN+2]; + +double distance( double x1, double y1, double x2, double y2 ) { + return ( sqrt( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) ) ); +} + +struct data { + int x, y; + double cost; + bool operator < (const data &temp ) const { + return (cost < temp.cost); + } +}; + +vector edge; +int pr[MAXN+2]; + +int findParent( int node ) { + return ( (pr[node] == node) ? node : findParent( pr[node] ) ); +} + +double mst( int n ) { + sort( edge.begin(), edge.end() ); + for( int i = 0 ; i <= MAXN ; ++i ) pr[i] = i; + + double cost = 0.0; + int counter = 0; + for( int i = 0 ; i < edge.size() ; ++i ) { + int x = findParent( edge[i].x ); + int y = findParent( edge[i].y ); + if( x != y ) { + pr[x] = y; + ++counter; + cost += edge[i].cost; + if( counter == (n - 1) ) break; + } + } + return cost; +} + +int main( int argc, char ** argv ) { + int T; + scanf( "%d", &T ); + for( int nCase = 1 ; nCase <= T ; ++nCase ) { + int n; + scanf( "%d", &n ); + for( int i = 1 ; i <= n ; ++i ) scanf( "%lf %lf", &point[i].x, &point[i].y ); + + for( int i = 0 ; i <= n ; ++i ) edge.clear(); + + for( int i = 1 ; i <= n ; ++i ) + for( int j = i + 1 ; j <= n ; ++j ) { + double c = distance( point[i].x, point[i].y, point[j].x, point[j].y ); + data get; + get.x = i, get.y = j, get.cost = c; + edge.push_back( get ); + get.x = j, get.y = i, get.cost = c; + edge.push_back( get ); + } + + if( nCase != 1 ) puts( "" ); + printf( "%0.2lf\n", mst( n ) ); + } + return 0; +} diff --git a/uva_cpp_clean/10034/10034-21.cpp b/uva_cpp_clean/10034/10034-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3181bb2e6de6c7cc04db0e109b1ca1f7b8a79abb --- /dev/null +++ b/uva_cpp_clean/10034/10034-21.cpp @@ -0,0 +1,83 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10034 + Name: Freckles + Problem: https://onlinejudge.org/external/100/10034.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + + +double X[102], Y[102], M[102][102]; +double dist(int i, int j) { + double x = X[i] - X[j], + y = Y[i] - Y[j]; + return sqrt(x*x + y*y); +} + + +struct Edge { + int i, j; + double dist; + bool operator < (const Edge& o) const { + return dist < o.dist; + } +}; + +Edge edges[102*102]; +bool tr[102][102]; + +int main(){ + int N; + cin>>N; + while(N--) { + int n; + cin>>n; + for(int i=0; i>X[i]>>Y[i]; + + int l = 0; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int par[1000005]; + +struct point { + double x,y; +}; + +struct f{ + + int a,b; + double d; + + f(int x,int y,double z) + { + a=x,b=y,d=z; + } +}; + +int Find(int n) +{ + if(par[n]==n) + return n; + return par[n]=Find(par[n]); +} + +void Union(int a,int b) +{ + int u= Find(a); + int v= Find(b); + if(u!=v) + { + par[u]=v; + } +} + +double dis(point a,point b) +{ + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); +} + +bool cmp(f a,f b) +{ + return a.d v; + + for0(i,n) + { + for0(j,n) + { + if(i==j) continue; + + double p = dis(a[i],a[j]); + v.pb(f(i,j,p)); + } + } + + sort(v.begin(),v.end(),cmp); + int len = v.size(); + + double res = 0.0; + for0(i,len) + { + int x= Find(v[i].a); + int y= Find(v[i].b); + if(x!=y) + { + Union(x,y); + res+=v[i].d; + } + } + + printf("%.2lf\n",res); + + if(t) + nl; + + } + + return 0; +} + diff --git a/uva_cpp_clean/10034/10034-6.cpp b/uva_cpp_clean/10034/10034-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02aed50f6a24e50f721665d7c6d1be065df3fab5 --- /dev/null +++ b/uva_cpp_clean/10034/10034-6.cpp @@ -0,0 +1,75 @@ +/* + graphs > minimum spanning tree (MST) + difficulty: easy + date: 10/Nov/2019 + by: @brpapa +*/ +#include +#include +#include +#include +#define MAX_V 110 +using namespace std; + +struct Tedge { + int v1, v2; + double w; + Tedge(int v1, int v2, double w) : v1(v1), v2(v2), w(w) {} + bool operator<(const Tedge& e) const { + return this->w < e.w; + } +}; +vector edgeList; // arestas não direcionadas + +typedef pair Tpoint; +Tpoint points[MAX_V]; // coordenada do vértice (first: x, second: y) + +int p[MAX_V]; // ufds +int findSet(int i) { + if (p[i] == i) + return i; + return p[i] = findSet(p[i]); +} + +double distEuclidian(Tpoint c1, Tpoint c2) { + return sqrt(pow(c1.first-c2.first, 2) + pow(c1.second-c2.second, 2)); +} + +int main() { + int V, T; + cin >> T; + + while (T--) { + edgeList.clear(); + cin >> V; + + double x, y, d; + for (int v = 0; v < V; v++) { + cin >> x >> y; + points[v] = make_pair(x, y); + + p[v] = v; // init ufds + + for (int t = 0; t < v; t++) { // para cada vértice t já lido + d = distEuclidian(points[t], points[v]); + edgeList.push_back(Tedge(t, v, d)); + } + } + + double mstCost = 0; + sort(edgeList.begin(), edgeList.end()); + + for (Tedge e: edgeList) { + int set_v1 = findSet(e.v1); + int set_v2 = findSet(e.v2); + + if (set_v1 != set_v2) { + p[set_v1] = set_v2; // union sets + mstCost += e.w; + } + } + + printf("%.2lf%s", mstCost, (T == 0)?"\n":"\n\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10034/10034-9.cpp b/uva_cpp_clean/10034/10034-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b07a9d0cbf4ed297c79172b9a0e26f38d5d84dea --- /dev/null +++ b/uva_cpp_clean/10034/10034-9.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +const int SIZE = 110; +int tc, n; +float x[SIZE], y[SIZE]; +vector < pair > EdgeList; + +float Kruskal(){ + EdgeList.clear(); + for(int i = 0; i < n; i++) + for(int j = i + 1; j < n; j++) + EdgeList.push_back( make_pair( hypot(x[i] - x[j], y[i] - y[j]), ii(i , j) ) ) ; + sort(EdgeList.begin(), EdgeList.end()); + UnionFind UF(n); + float min_cost = 0, setSize = 1; + for(int it = 0; it < EdgeList.size(); it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + min_cost += front.first, setSize++; + UF.unionSet(front.second.first, front.second.second); + if( setSize == n ) break; + } + return min_cost; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + for(int it = 0; it < n; it++) scanf("%f %f", x + it, y + it); + printf("%.2f\n", Kruskal()); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10035/10035-15.cpp b/uva_cpp_clean/10035/10035-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f37e206f0dfce70f2f924e120e5b09d4fc6dfc9b --- /dev/null +++ b/uva_cpp_clean/10035/10035-15.cpp @@ -0,0 +1,29 @@ +/* + 直式加法 +*/ +#include + +using namespace std; + +int main() { + int n, m; + while (cin >> n >> m && (n != 0 || m != 0)) { + int count = 0, j = 0; + + do { + if (n % 10 + m % 10 + j > 9) + count += j = 1; + else + j = 0; + n /= 10; + m /= 10; + } while (n != 0 || m != 0); + + if (count == 0) + cout << "No carry operation.\n"; + else if (count == 1) + cout << "1 carry operation.\n"; + else + cout << count << " carry operations.\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10035/10035-19.cpp b/uva_cpp_clean/10035/10035-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2719680c785779f3c11cc620274fe52613c1141f --- /dev/null +++ b/uva_cpp_clean/10035/10035-19.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main() { + int s1, s2; + while (scanf("%d %d", &s1, &s2) && !(s1 == 0 && s2 == 0)) { + int count = 0, carrie = 0; + while (s1 != 0 || s2 != 0) { + int sum = s1%10 + s2%10 + carrie; + if (sum >= 10) carrie = 1, count++; + else carrie = 0; + s1 /= 10; + s2 /= 10; + } + if (count > 1) printf("%d carry operations.\n", count); + else if (count == 1) printf("1 carry operation.\n"); + else printf("No carry operation.\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10035/10035-3.cpp b/uva_cpp_clean/10035/10035-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74c4f01aee384974438b51a46aad3bad3dda5053 --- /dev/null +++ b/uva_cpp_clean/10035/10035-3.cpp @@ -0,0 +1,28 @@ +#include + +int main() +{ + int firstInt,secondInt,c, total; + while ( scanf("%d %d",&firstInt,&secondInt), firstInt || secondInt ) + { + total = c = 0; + while ( firstInt || secondInt ) + { + if ( firstInt % 10 + secondInt % 10 + c > 9 ) { + total++; + c = (firstInt%10+secondInt%10+c)/10; + } + else + c = 0; + firstInt /= 10; + secondInt /= 10; + } + if ( total == 1 ) + puts("1 carry operation."); + else if ( total ) + printf("%d carry operations.\n", total); + else + puts("No carry operation."); + } +} + diff --git a/uva_cpp_clean/10035/10035-5.cpp b/uva_cpp_clean/10035/10035-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0365944862d9edb865e3de66840828b4a0382330 --- /dev/null +++ b/uva_cpp_clean/10035/10035-5.cpp @@ -0,0 +1,64 @@ +#include +#include +int main() +{ + char a[20],b[20],e[20],d[20]; + int p,c,i,l,s,len,y,k,n,j; + while(scanf("%s%s",a,b)==2) + { + k=(a[0]-'0'); + l=(b[0]-'0'); + if(k==0&&l==0) + break; + l=strlen(a); + len=strlen(b); + if(l>len) + { + y=l; + n=l-len; + for(i=0;i=0;i--) + { + s=(e[i]-'0')+(d[i]-'0'); + if(s>=10) + { + c++; + e[i-1]++; + } + } + if(c==0) + printf("No carry operation.\n"); + else if(c==1) + printf("1 carry operation.\n"); + else + printf("%d carry operations.\n",c); + } + return 0; +} diff --git a/uva_cpp_clean/10035/10035-9.cpp b/uva_cpp_clean/10035/10035-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8527a9ae09f46860db9a6b6a7efdef683fb544f --- /dev/null +++ b/uva_cpp_clean/10035/10035-9.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main(){ + long a, b; + int carry; + bool foo; + while(scanf("%ld %ld", &a, &b), a | b){ + carry = 0; + foo = false; + while(a | b){ + if((a % 10 + b % 10 + foo) / 10) + carry++, foo = true; + else + foo = false; + a /= 10; + b /= 10; + } + if(carry){ + if(carry == 1) + printf("1 carry operation.\n"); + else + printf("%d carry operations.\n", carry); + } + else + printf("No carry operation.\n"); + } + return(0); +} diff --git a/uva_cpp_clean/10036/10036-10.cpp b/uva_cpp_clean/10036/10036-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20241419c04af7b4cb54a67db415343cd8760cd3 --- /dev/null +++ b/uva_cpp_clean/10036/10036-10.cpp @@ -0,0 +1,61 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + __builtin_popcount(); +*/ +bool isPrime(int x) { + if (x <= 1) return false; + if (x <= 3) return true; + if (x % 2 == 0 || x % 3 == 0) return false; + for (int i = 5; i * i <= x; i = i + 6) + if (x % i == 0 || x % (i + 2) == 0) return false; + return true; +} +int n,k; +vector arr; +vector> dp; +bool solve(int i,int sum){ + if(i==n) { + + if(sum==0) return 1; + + return 0; + } + + if(dp[i][sum] != -1) return dp[i][sum]; + + return dp[i][sum] = ((solve(i+1,abs(sum+arr[i])%k)) || (solve(i+1,abs(sum-arr[i])%k))); + + + +} + +int32_t main() { + Sakr_ + + int t,i; + cin>>t; + + while (t--) { + cin>>n>>k; + arr= vector(n+2); + dp = vector>(n+2,vector(k+2,-1)); + + for(i=0;i>arr[i]; + } + cout<<(solve(1,arr[0]%k)?"Divisible\n":"Not divisible\n"); + + } + + + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10036/10036-19.cpp b/uva_cpp_clean/10036/10036-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3776536df885047e2916c219648fec8be41e8d6c --- /dev/null +++ b/uva_cpp_clean/10036/10036-19.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; + +int num[10002]; +int N, K; + +unordered_set proc(int ini, int fin) { + if (ini < fin) { + unordered_set s1 = proc(ini, (ini + fin)/2), s2 = proc((ini + fin)/2 + 1, fin), r; + for (auto it1 = s1.begin(); it1 != s1.end(); ++it1) { + for (auto it2 = s2.begin(); it2 != s2.end(); ++it2) { + r.insert((*it1 + *it2)%K); + r.insert((*it1 - *it2 + K)%K); + } + } + return r; + } else { + unordered_set r; + r.insert(((num[ini]%K)+K)%K); + return r; + } +} + +int main() { + int M; + cin >> M; + while (M--) { + cin >> N >> K; + for (int i = 0; i < N; i++) { + cin >> num[i]; + } + unordered_set r = proc(0, N-1); + if (r.count(0)) printf("Divisible\n"); + else printf("Not divisible\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10036/10036-21.cpp b/uva_cpp_clean/10036/10036-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05523301ce32520be0e49e697e175e634bb77092 --- /dev/null +++ b/uva_cpp_clean/10036/10036-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10036 + Name: Divisibility + Problem: https://onlinejudge.org/external/100/10036.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +int d[10001],k,n; + +int saved[10001][101]; +bool smod(int ind,int m){ + if(m<0)m+=k; + + if(ind==n-1) + return d[ind]%k==m; + + if(saved[ind][m]!=-1)return saved[ind][m]; + + return saved[ind][m]=( smod(ind+1,(m+d[ind]%k)%k)|| + smod(ind+1,m-d[ind]%k)); +} + +int main(){ + int T,i,j; + cin>>T; + while(T--){ + cin>>n>>k; + for(i=0;i>d[i]; + if(d[i]<0)d[i]=-d[i]; + } + + for(i=0;i<=n;i++) + for(j=0;j<=k;j++) + saved[i][j]=-1; + + if(smod(0,0)) + puts("Divisible"); + else + puts("Not divisible"); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10036/10036-5.cpp b/uva_cpp_clean/10036/10036-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..dc41f490d03470fa1c6fc899e6a3984e5ac20e91 --- /dev/null +++ b/uva_cpp_clean/10036/10036-5.cpp @@ -0,0 +1,142 @@ +/* + Author : Shipu Ahamed (Psycho Timekiller) + Facebook : http://facebook.com/DeesheharaShipu +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout<string toString( T Number ){stringstream st;st << Number;return st.str();} +int SOD(int n) {int sum=0;for(int i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx 10010 +#define mod 1000000007 +////=====================================//// + +int a[mx],dp[mx][203],n,k; + +bool solve(int i,int sum) +{ + if(i==n) + { + if(sum%k==0) + { + return 1; + } + else + return 0; + } + + if(dp[i][sum]!=-1) + return dp[i][sum]; + + return dp[i][sum]=(solve(i+1,(sum+a[i])%k)|solve(i+1,(sum-a[i])%k)); +} + +int main() +{ + int t; + si(t); + while(t--) + { + sii(n,k); + + cover(dp,-1); + + for(int i=0;i + +#define SIZE 10010 +#define MAX_K 110 + +using namespace std; + +int tc,n,k,v[SIZE]; +bool memo[SIZE][MAX_K],visited[SIZE][MAX_K]; + +bool dp(int pos,int current){ + current = ((current%k)+k)%k; + if(pos==n) return (current==0); + if(visited[pos][current]) return memo[pos][current]; + visited[pos][current] = true; + if(memo[pos][current]==false) + memo[pos][current] |= dp(pos+1,current+v[pos]); + if(memo[pos][current]==false) + memo[pos][current] |= dp(pos+1,current-v[pos]); + return memo[pos][current]; +} + +int main(){ + scanf("%d",&tc); + while(tc--){ + memset(visited,false,sizeof visited); + memset(memo,false,sizeof memo); + scanf("%d %d",&n,&k); + for(int i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10037 + Name: Bridge + Problem: https://onlinejudge.org/external/100/10037.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int n, w[1001], DP[1001], P[1001] = {1, 2}; +int main() { + int T; cin>>T; + while (T--) { + cin>>n; + for (int i=0; i> w[i]; + sort(w, w+n); + + DP[0] = w[0]; + DP[1] = max(w[0], w[1]); + for (int i=2; i1; i -= P[i]) { + cout << w[0] << endl; + if (P[i] == 1) + printf("%d %d\n", w[0], w[i]); + + else { + printf("%d %d\n", w[i-1], w[i]); + cout << w[1] << endl; + printf("%d %d\n", w[0], w[1]); + } + } + + if (T) putchar('\n'); + } +} diff --git a/uva_cpp_clean/10038/10038-11.cpp b/uva_cpp_clean/10038/10038-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e29c399b501144231d1e9cdf685ed5dc75a827f --- /dev/null +++ b/uva_cpp_clean/10038/10038-11.cpp @@ -0,0 +1,66 @@ +#include +/* +Problem: 10038 - Jolly Jumpers +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=979 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int N; +int freq[3001], a, b; + +int main () { + fastio; + + while ( cin >> N && !cin.eof() ){ + memset(freq, 0, sizeof(freq)); + + bool isJolly = true; + + cin >> a; + for (int i = 1; i < N; ++i){ + cin >> b; + + if (abs(a-b) >= N) isJolly = false; + else freq[abs(a-b)]++; + + a = b; + } + + for (int i = 1; i < N; ++i) + isJolly &= freq[i]; + + cout << (isJolly? "Jolly" : "Not jolly") << '\n'; + + } + + return 0; +} +/* +Sample input:- +----------------- +4 1 4 2 3 +5 1 4 2 -1 6 + +Sample output:- +----------------- + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10038/10038-15.cpp b/uva_cpp_clean/10038/10038-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1b12ae7058f93be30b71f9cf2548d42d1570ae7 --- /dev/null +++ b/uva_cpp_clean/10038/10038-15.cpp @@ -0,0 +1,41 @@ +/* + 模擬 +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n; + while (cin >> n) { + int last, num; + vector seen(n, false); + seen[0] = true; + cin >> last; + + for (int i = 1; i < n; i++) { + cin >> num; + + if (seen[0]) { + int&& check = abs(num - last); + + if (check >= n || seen[check]) { + seen[0] = false; + cout << "Not jolly\n"; + } else { + seen[check] = true; + last = num; + } + } + } + + if (seen[0]) + cout << "Jolly\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10038/10038-18.cpp b/uva_cpp_clean/10038/10038-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fbcb42e3273af5a12b30795272d4e1183a80b359 --- /dev/null +++ b/uva_cpp_clean/10038/10038-18.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int len; + while(cin >> len){ + int seq[3002], delta[3002], hits = 0; + for(int i = 0; i < len; i++){ + cin >> seq[i]; + } + for(int i = 0; i < len - 1; i++){ + delta[i] = abs(seq[i + 1] - seq[i]); + } + sort(delta, delta+len-1); + for(int i = 0; i < len - 1; i++){ + if(delta[i] != delta[i + 1]) hits++; + } + if(hits == len - 1) printf("Jolly\n"); + else printf("Not jolly\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10038/10038-19.cpp b/uva_cpp_clean/10038/10038-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f66625cfea122b502549d0c4ce6447791bfc463 --- /dev/null +++ b/uva_cpp_clean/10038/10038-19.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; + +int main() { + unordered_set set; + int n; + while (cin >> n) { + bool jolly = true; + int previous; + cin >> previous; + if (n != 1) { + set.clear(); + for (int i = 1; i < n; i++) { + int item; + cin >> item; + set.insert(abs(item-previous)); + previous = item; + } + for (int i = 1; i < n; i++) { + if(!set.count(i)) { jolly = false; break; } + } + } + if(jolly) cout << "Jolly" << endl; + else cout << "Not jolly" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10038/10038-21.cpp b/uva_cpp_clean/10038/10038-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0c546a2e9c1b97ec285c517d4dd45d66c20a898 --- /dev/null +++ b/uva_cpp_clean/10038/10038-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10038 + Name: Jolly Jumpers + Problem: https://onlinejudge.org/external/100/10038.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int sub(int a, int b) { + return a>n) { + int cnt=0; + if (!n) cout << "Jolly\n"; + else { + for (int i=1; i<=n; i++) + bl[i]=false; + + int x, y; + cin >> x; + for(int i=1; i>y; + int d = sub(x, y); + if (d>0 && d //bitset +#include //printf scanf +#include //abs + +using namespace std; + +bitset<3000> boolFlag(0); +int inputArray[3000]; + +int main () +{ + int N, tempInt; + bool jolly; + + while ( scanf("%d",&N) != EOF ) + { + jolly = true; + boolFlag.reset(); + + + // Reading input: + for ( int l = 0; l < N; l++ ) + scanf ("%d", &inputArray[l] ); + + // Flaging values passed: + for ( int i = 1; i < N; i++ ) + { + tempInt = abs(inputArray[i-1]-inputArray[i]); + if ( tempInt > (N -1) || tempInt == 0 ) + { + jolly = false; + break; + } + boolFlag [tempInt] = true; + } + + // Checking if the requirements are met: + if ( jolly && boolFlag.count() == N-1 ) + printf ("Jolly\n"); + else + printf("Not jolly\n"); + } +} diff --git a/uva_cpp_clean/10038/10038-5.cpp b/uva_cpp_clean/10038/10038-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..62ea0f1ed43375efa37650a2c47c926f8a2c42eb --- /dev/null +++ b/uva_cpp_clean/10038/10038-5.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +using namespace std; +int main() +{ +int n,c,b[3010],a[3010],i; +while(scanf("%d",&n)==1) +{ +c=1; +for(i=0;i +#include + +using namespace std; + +typedef vector vb; + +int main() { + while (true) { + int n; + cin >> n; + if (cin.eof()) break; + vector v(n - 1, false); + int a; + cin >> a; + for (int i = 1; i < n; i++) { + int b; + cin >> b; + int d = abs(a - b) - 1; + a = b; + if (d < 0 || d > n - 2) continue; + v[d] = true; + } + bool ok = true; + for (int i = 0; i < n - 1; i++) + if (!v[i]) { + ok = false; + break; + } + cout << (ok ? "Jolly" : "Not jolly") << endl; + } +} diff --git a/uva_cpp_clean/10038/main.cpp b/uva_cpp_clean/10038/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..383f038347a2d6d06e8806409f011a05bce3abf6 --- /dev/null +++ b/uva_cpp_clean/10038/main.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +int main(){ + + int n, actual, next; + set myset; + while(cin >> n){ + myset.clear(); + cin >> actual; + for(int i=1; i < n; i++){ + cin >> next; + myset.insert(max(actual, next)-min(actual, next)); + // cout << max(actual, next)-min(actual, next) << endl; + actual = next; + } + bool res = true; + for(int i=1; i +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_testcase; + cin >> n_testcase; + while (n_testcase--) { + int n, temp; + cin >> n; + vector relative(n, 0); + + for (int i = 0; i < n; i++) + cin >> relative[i]; + + sort(relative.begin(), relative.end()); + + int mid = relative.size() / 2; + + if (n & 1) + mid = relative[mid]; + else + mid = (relative[mid] + relative[mid - 1]) / 2; + + int ans = 0; + + for (const int& i : relative) + ans += abs(i - mid); + + cout << ans << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10041/10041-18.cpp b/uva_cpp_clean/10041/10041-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..42da7f8fe06ed8e3be99c048d64da3dec13124be --- /dev/null +++ b/uva_cpp_clean/10041/10041-18.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int n, streets[510], optimalStreet, len, sum; + scanf("%d", &n); + while(n-- > 0){ + sum = 0; + scanf("%d", &len); + for(int i = 0; i < len; i++) scanf("%d", &streets[i]); + sort(streets, streets + len); + optimalStreet = len / 2; + for(int i = 0; i < len; i++) sum += abs(streets[optimalStreet] - streets[i]); + printf("%d\n", sum); + } + return 0; +} +/* + La estrategia es buscar la calle que este en la mitad, ya que se quiere minimizar las distancias entre las casas, + la unica manera es que Vito viva en la mitad de la calle. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10041/10041-2.cpp b/uva_cpp_clean/10041/10041-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..669f009e94ed2ca83f7b5b3d0e4209e8548bfae6 --- /dev/null +++ b/uva_cpp_clean/10041/10041-2.cpp @@ -0,0 +1,55 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; scanf("%d", &tc); + while (tc--) { + int n; scanf("%d", &n); + vector v(n); + for(int& i: v) scanf("%d",&i); + sort(all(v)); + int b = 0, sum = 0; + + if(n%2!=0) b=v[n/2]; + else b=v[n/2]; + + for(int i=n/2+1;i=0;i--) + sum=sum+abs(b-v[i]); + + printf("%d\n", sum); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10041/10041-20.cpp b/uva_cpp_clean/10041/10041-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19df33c8858a6dff076c97dd1891bca05fa1d7b1 --- /dev/null +++ b/uva_cpp_clean/10041/10041-20.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +int s[505]; + +int main() +{ + int tc; + scanf("%d", &tc); + + while( tc-- ) + { + int r; + scanf("%d", &r); + + for(int i=0 ; i=0 ) d += s[i]-s[r/2]; + } + + printf("%d\n", d); + } + return 0; +} diff --git a/uva_cpp_clean/10041/10041-21.cpp b/uva_cpp_clean/10041/10041-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c61ad6223d8d27d98a1b414d10829f4cfb5b63a7 --- /dev/null +++ b/uva_cpp_clean/10041/10041-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10041 + Name: Vito's Family + Problem: https://onlinejudge.org/external/100/10041.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int a[506]; +int main(){ + int n,T,s,i; + cin>>T; + while(T--){ + cin>>n; + for(i=0;i>a[i]; + sort(a,a+n); + s=0; + for(i=0;i +#include +using namespace std; +int main() +{ + int i,t,n,a[5000],sum; + scanf("%d",&t); + while(t--) + { + int p=50000000; + scanf("%d",&n); + + for(i=0;i + +using namespace std; + +int tc, n, _min, tmp; +vector v; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + v.resize(n); _min = INT_MAX; + for(int i = 0; i < n; i++) + scanf("%d", &v[i]); + for(int i = 0; i < n; i++){ + tmp = 0; + for(int j = 0; j < n; j++) + tmp += abs(v[i] - v[j]); + if(tmp < _min) + _min = tmp; + } + printf("%d\n", _min); + } + return(0); +} diff --git a/uva_cpp_clean/10042/10042-19.cpp b/uva_cpp_clean/10042/10042-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d85533dce892e7a508c7238cca84ec7b0d052ccd --- /dev/null +++ b/uva_cpp_clean/10042/10042-19.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<100000001> bs; +vi primes; + +inline void sieve(ll upperbound = 40000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline void primeFactors(ll n, vii & factors) { + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +vii v; + +bool check(int n) { + v.clear(); + primeFactors(n, v); + if (v.size() == 1 && v[0].second == 1) return false; + int s1 = 0, s2 = 0; + while (n) { + s1 += n%10; + n /= 10; + } + for (int i = 0; i < v.size(); i++) { + int val = v[i].first; + int acum = 0; + while (val) { + acum += val%10; + val /= 10; + } + acum *= v[i].second; + s2 += acum; + } + return (s1 == s2); +} + +int main() { + sieve(); + int T; + cin >> T; + while (T--) { + int n; + cin >> n; + n++; + while (!check(n)) n++; + printf("%d\n", n); + } + return 0; +} diff --git a/uva_cpp_clean/10042/10042-20.cpp b/uva_cpp_clean/10042/10042-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dfe2138bc5ac987b8ccd55e9c786ac06b769e60e --- /dev/null +++ b/uva_cpp_clean/10042/10042-20.cpp @@ -0,0 +1,73 @@ +#include +#include + +int tc, n; + +bool isPrime(int x) +{ + if( x==2 ) return (true); + else if( !(x&1) ) return (false); + else + { + int sqrtX = sqrt(x); + for(int i=3 ; i<=sqrtX ; i+=2) + if( !(x%i) ) return (false); + } + return (true); +} + +int sumOfDigits(int x) +{ + int sum = 0; + while( x ) + { + sum += x%10; + x /= 10; + } + return (sum); +} + +int sumOfPrimeFactors(int x) +{ + int sum = 0; + while( !(x&1) ) + { + sum += 2; + x /= 2; + } + int i = 3, sqrtX = sqrt(x); + while( i<=sqrtX ) + if( !(x%i) ) + { + sum += sumOfDigits(i); + x /= i; + } + else i+=2; + if( x>1 ) sum += sumOfDigits(x); + return (sum); +} + +bool smithNumber(int x) +{ + if( !isPrime(x) ) + { + int sum1 = sumOfDigits(x), sum2 = sumOfPrimeFactors(x); + if( sum1==sum2 ) return (true); + else return (false); + } + return (false); +} + +int main() +{ + scanf("%d", &tc); + while( tc-- ) + { + scanf("%d", &n); + + while( !smithNumber(++n) ) ; + + printf("%d\n", n); + } + return 0; +} diff --git a/uva_cpp_clean/10042/10042-21.cpp b/uva_cpp_clean/10042/10042-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fb8433237620fd072d3aced1629565d82430aa9 --- /dev/null +++ b/uva_cpp_clean/10042/10042-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10042 + Name: Smith Numbers + Problem: https://onlinejudge.org/external/100/10042.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int dsum(int x) { + if (x<10) + return x; + else if (x<100) + return (x/10) + (x%10); + + int sum = 0; + for(; x; x/=10) + sum += x%10; + return sum; +} + +bool isSmith(int x) { + int n = x; + int ds = dsum(x); + int sq = sqrt(double(x)); + for (int i=2; i<=sq; i++) + if (x%i == 0) { + int ds0 = dsum(i); + do { + ds -= ds0; + if (ds<0) return 0; + x /= i; + } while (x%i == 0); + sq = sqrt(double(x)); + } + + if (x == n) return 0; + if (x > 1) ds -= dsum(x); + return !ds; +} + + +int main(){ + int T, n, x; + cin>>T; + while (T--) { + cin >> n; + for (x=n+1; !isSmith(x); x++); + cout << x << endl; + } +} diff --git a/uva_cpp_clean/10042/10042-6.cpp b/uva_cpp_clean/10042/10042-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4864789c2bf2c7ad3b92eb6a75963d723bd29c8 --- /dev/null +++ b/uva_cpp_clean/10042/10042-6.cpp @@ -0,0 +1,45 @@ +/* + math > number theory > prime numbers > prime factorization + difficulty: easy + date: 13/Feb/2020 + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int sumDigits(int n) { + int sum = 0; + while (n > 0) { + sum += n%10; + n /= 10; + } + return sum; +} + +int main() { + int T; cin >> T; + while (T--) { + int n; cin >> n; + + for (int ans = n+1; true; ans++) { + int tmp = ans; vector ansFactors; + + for (int f = 2; f <= sqrt(tmp) && tmp > 0; f++) + while (tmp%f == 0) { + ansFactors.push_back(f); + tmp /= f; + } + if (ansFactors.empty()) continue; // ans é primo, logo não é smith + if (tmp > 1) ansFactors.push_back(tmp); + + int sumFactorsDigits = 0; + for (int f: ansFactors) sumFactorsDigits += sumDigits(f); + if (sumDigits(ans) == sumFactorsDigits) { + cout << ans << endl; break; + } + } + } + return 0; +} diff --git a/uva_cpp_clean/10044/10044-13.cpp b/uva_cpp_clean/10044/10044-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d61a76d77627c15a84fef49592d5a787050d3e9f --- /dev/null +++ b/uva_cpp_clean/10044/10044-13.cpp @@ -0,0 +1,192 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +string name; + +int parent[5005]; + +char line[10000]; + +queue childs; + +vector names; + +map match; + +vector > graph; + +int scenario = 1, P, N, edge, v, w, step, pointer, len; + +void bfs(int start) +{ + for (int i = 0; i < 5005; i++) + { + parent[i] = 6000; + } + + parent[start] = 0; + + childs.push(start); + + while (!childs.empty()) + { + int head = childs.front(); + + childs.pop(); + + for (unsigned int i = 0; i < graph[head].size(); i++) + { + if (parent[graph[head][i]] == 6000) + { + parent[graph[head][i]] = parent[head] + 1; + + childs.push(graph[head][i]); + } + } + } +} + +void makeNames() +{ + len = strlen(line); + + pointer = 0; + + while (pointer < len) + { + name = ""; + + while (pointer < len && line[pointer] == ' ') + { + pointer++; + } + + if (pointer >= len) + { + break; + } + + while (line[pointer] != ',') + { + name += line[pointer++]; + } + + name += line[pointer++]; + + while (line[pointer] != ',' && line[pointer] != ':') + { + name += line[pointer++]; + } + + names.push_back(name); + + if (pointer >= len || line[pointer] == ':') + { + break; + } + + pointer++; + } +} + +void makeGraph() +{ + for (unsigned int i = 0; i < names.size() - 1; i++) + { + for (unsigned int j = i + 1; j < names.size(); j++) + { + if (match[names[i]] == 0) + { + match[names[i]] = edge; + + v = edge; + + edge++; + } + + else + { + v = match[names[i]]; + } + + if (match[names[j]] == 0) + { + match[names[j]] = edge; + + w = edge; + + edge++; + } + + else + { + w = match[names[j]]; + } + + graph[v].push_back(w); + + graph[w].push_back(v); + } + + } +} + +int main() +{ + cin >> step; + + while (step--) + { + graph.resize(5005); + + cin >> P >> N; + + edge = 1; + + cin.get(); + + while (P--) + { + cin.getline(line,100000); + + makeNames(); + + makeGraph(); + + names.clear(); + } + + bfs(match["Erdos, P."]); + + cout << "Scenario " << scenario << endl; + + while (N--) + { + cin.getline(line,100000); + + if (parent[match[line]] != 6000) + { + cout << line << " " << parent[match[line]] << endl; + } + + else + { + cout << line << " infinity" << endl; + } + } + + scenario++; + + match.clear(); + + graph.clear(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10044/10044-21.cpp b/uva_cpp_clean/10044/10044-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58004d979b4166173e3b79772d1a7720c7bca20c --- /dev/null +++ b/uva_cpp_clean/10044/10044-21.cpp @@ -0,0 +1,164 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10044 + Name: Erdos Numbers + Problem: https://onlinejudge.org/external/100/10044.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +#define INF 536870912 +#define MAXN 5001 +#define MAXL 20000 + + +int graph[MAXN][MAXN], adj[MAXN][MAXN], deg[MAXN]; +int d[MAXN], q[MAXN], inq[MAXN], qs; + +#define CLR( x, v ) memset( x, v, sizeof( x ) ) +#define BUBL { \ + t = q[i]; q[i] = q[j]; q[j] = t; \ + t = inq[q[i]]; inq[q[i]] = inq[q[j]]; inq[q[j]] = t; } + +int dijkstra( int n, int s, int t ) { + CLR( d, 9 ); CLR( inq, -1 ); + d[s] = qs = 0; + inq[q[qs++] = s] = 0; + + while( qs ) { + // get the minimum from the q + int u = q[0]; inq[u] = -1; + + // bubble down + q[0] = q[--qs]; + if( qs ) inq[q[0]] = 0; + for( int i = 0, j = 2*i + 1, t; j < qs; i = j, j = 2*i + 1 ) { + if( j + 1 < qs && d[q[j + 1]] < d[q[j]] ) j++; + if( d[q[j]] >= d[q[i]] ) break; + BUBL; + } + + // relax neighbours + for( int k = 0, v = adj[u][k]; k < deg[u]; v = adj[u][++k] ) { + int newd = d[u] + graph[u][v]; + if( newd < d[v] ) { + d[v] = newd; + if( inq[v] < 0 ) { inq[q[qs] = v] = qs; qs++; } + + // bubble up + for( int i = inq[v], j = ( i - 1 )/2, t; + d[q[i]] < d[q[j]]; i = j, j = ( i - 1 )/2 ) + BUBL; + } + } + } + + return d[t]; +} + + + + + + + + + + + + + + +char line[MAXL]; +int ex[MAXN*MAXN], + ey[MAXN*MAXN]; + +int main(){ + int T, p, n; + cin>>T; + for(int cse=1; cse<=T; cse++) { + int cnt = 0, ec = 0; + map ids; + ids["Erdos, P."] = cnt++; + + + cin>>p>>n; + while(getchar()!='\n'); + + for (int i=0; i cur; + + string name; + bool ff = false; + for (char* s=strtok(line, " ,"); s; s=strtok(NULL, " ,")) { + if (ff) { + ff = false; + name = name + ", " + s; + + int id = 0; + if (name != "Erdos, P.") { + id = ids[name]; + if (id == 0) + id = ids[name] = cnt++; + } + + for (int j=cur.size()-1; j>=0; j--) { + int k = cur[j]; + if (k!=id) { + ex[ec] = k; + ey[ec++] = id; + } + } + + cur.push_back(id); + } + + else { + ff = true; + name = s; + } + } + } + + + for (int i=0; isecond); + cout << line << " "; + if (res >= cnt) cout << "infinity\n"; + else cout << res << endl; + } + } +} diff --git a/uva_cpp_clean/10047/10047-5.cpp b/uva_cpp_clean/10047/10047-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a9f905ff93ee19c08a87e3056e5309b5893c5831 --- /dev/null +++ b/uva_cpp_clean/10047/10047-5.cpp @@ -0,0 +1,269 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int dx[] = {-1,0,1,0}; +int dy[] = {0,1,0,-1}; + +struct node +{ + int x,y,dir=0,time=0,col=0; +}; + +string s[30]; +int vis[30][30][5][5]; +int res,r,c; + +int bfs(int i,int j) +{ +// puts("OK"); + queueq; + + for0(i,27) + { + for0(j,27) + { + for0(k,5) + { + for0(l,5) + { + vis[i][j][k][l] = INT_MAX; + } + } + } + } + +// cover(vis,INT_MAX); + res=INT_MAX; + node p; + p.x=i; + p.y=j; + q.push(p); + vis[p.x][p.y][p.dir][p.col] = 0; + while(!q.empty()) + { +// puts("OK"); + node u; + u=q.front();q.pop(); + + if(s[u.x][u.y] == 'T' && u.col==0) + { + res=min(res,u.time); + continue; + } +// puts("OK"); + node v; + v.x=u.x+dx[u.dir]; + v.y=u.y+dy[u.dir]; + v.dir=u.dir; + v.time=u.time+1; + v.col=(u.col+1)%5; +// puts("OK"); + if(v.x>=0 && v.x=0 && v.y>s[i]; + } + + int f=0; + for0(i,r) + { + for0(j,c) + { + if(s[i][j]=='S') + { + bfs(i,j); + f=1; + break; + } + } + if(f) + break; + } + + if(print) + nl; + + Case(no); + if(res==INT_MAX) + { + puts("destination not reachable"); + } + else + { + printf("minimum time = %d sec\n",res); + } + print=true; + } + + return 0; +} diff --git a/uva_cpp_clean/10048/10048-13.cpp b/uva_cpp_clean/10048/10048-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0108cfe647ae7294914d2c61db17310baf9d629 --- /dev/null +++ b/uva_cpp_clean/10048/10048-13.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 105 +#define oo INT_MAX + +int n, m, q, t = 1; +int parent[MAX], sz[MAX]; +int temp[MAX][MAX]; + +class comparator +{ + +public: + + bool operator()(pair , int> &x, pair , int> &y) + { + return x.second > y.second; + } +}; + +priority_queue , int>, vector , int> >, comparator> pq; + +int findP(int v) +{ + if (v == parent[v]) return parent[v]; + + else return parent[v] = findP(parent[v]); +} + +bool merge(pair a) +{ + int x = a.first; + + int y = a.second; + + if (findP(x) == findP(y)) return true; + + else + { + x = findP(x); + + y = findP(y); + + if (sz[x] < sz[y]) swap(x, y); + + parent[y] = x; + + sz[x] += sz[y]; + + return false; + } +} + +void mst() +{ + while (!pq.empty()) + { + pair , int> head = pq.top(); + + if (!merge(head.first)) + { + temp[head.first.first][head.first.second] = head.second; + + temp[head.first.second][head.first.first] = head.second; + } + + pq.pop(); + } +} + + +void initialize(int n) +{ + for (int i = 0; i < n; i++) + { + parent[i] = i; + + sz[i] = 1; + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + temp[i][j] = oo; + } + } +} + +int main() +{ + while (cin >> n >> m >> q, n != 0 || m != 0 || q != 0) + { + initialize(n); + + for (int i = 0; i < m; i++) + { + int x, y, w; + + cin >> x >> y >> w; + + x--; y--; + + pq.push(make_pair(make_pair(x, y), w)); + } + + mst(); + + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + temp[i][j] = min(temp[i][j], max(temp[i][k], temp[k][j])); + } + } + } + + if (t > 1) cout << endl; + + cout << "Case #" << t++ << endl; + + for (int i = 0; i < q; i++) + { + int x, y; + + cin >> x >> y; + + x--; y--; + + if (temp[x][y] != oo) cout << temp[x][y] << endl; + + else cout << "no path" << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10048/10048-19.cpp b/uva_cpp_clean/10048/10048-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7c21549d9d0cb5187ba3afea5aadb8f7717500a --- /dev/null +++ b/uva_cpp_clean/10048/10048-19.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair street; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +priority_queue pq; + +int main() { + bool first = false; + int C, S, Q, c = 1; + while (cin >> C >> S >> Q && !(C == 0 && S == 0 && Q == 0)) { + if (first) printf("\n"); + else first = true; + for (int i = 0; i < S; i++) { + int c1, c2, d; + cin >> c1 >> c2 >> d; + street s = make_pair(-d, make_pair(c1-1, c2-1)); + pq.push(s); + } + ufds u(C); + unordered_map noise; + while (!pq.empty()) { + if (!u.isSameSet(pq.top().second.first, pq.top().second.second)) { + for (int i = 0; i < C; i++) { + if (u.isSameSet(pq.top().second.first, i)) + for (int j = 0; j < C; j++) { + if (u.isSameSet(pq.top().second.second, j)) + noise[i*1000+j] = noise[i+1000*j] = max(noise[i*1000+pq.top().second.first], max(noise[j*1000+pq.top().second.second], -pq.top().first)); + } + } + u.unionSet(pq.top().second.first, pq.top().second.second); + } + pq.pop(); + } + printf("Case #%d\n", c); + c++; + while (Q--) { + int c1, c2; + cin >> c1 >> c2; + c1--, c2--; + if (u.isSameSet(c1, c2)) printf("%d\n", noise[c1*1000 + c2]); + else printf("no path\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/10048/10048-20.cpp b/uva_cpp_clean/10048/10048-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..426bb60f73d103343e377e580004ff8263922ef0 --- /dev/null +++ b/uva_cpp_clean/10048/10048-20.cpp @@ -0,0 +1,44 @@ +#include + +#define MAX 105 +#define INF ((1<<31)-(1)) +#define Min(a, b) ((ab)?(a):(b)) + +int decibel[MAX][MAX]; + +int main() +{ + int C, S, Q, nCase = 1; + bool flag = false; + while( scanf("%d %d %d", &C, &S, &Q)==3 && (C || S || Q) ) + { + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10048 + Name: Audiophobia + Problem: https://onlinejudge.org/external/100/10048.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + + +#define MAXN 1001 + +int ids[MAXN]; +int find(int i) { + if (ids[i] == i) return i; + return ids[i] = find(ids[i]); +} +void join(int x, int y) { + ids[find(x)] = find(y); +} + + +struct Edge { + int x, y, d; + bool operator < (const Edge &e) const { + return d adj[MAXN]; +int mp[MAXN][MAXN]; +int seen[MAXN]; + +int dfs(int v, int w) { + if (v==w) return 0; + if (seen[v]) return -1; + seen[v] = 1; + + int mx = -1; + for (int i: adj[v]) { + int r = dfs(i, w); + if (r>=0) + mx = max(mx, max(r, mp[v][i])); + } + + seen[v] = 0; + return mx; +} + + + + +int main(){ + int c,s,q, cse=1, x, y; + while (cin>>c>>s>>q && (c||s||q)) { + if (cse > 1) cout<> e.x >> e.y >> e.d; + } + + sort(eds, eds+s); + int cnt = 0; + for (int i=0; i>x>>y; + int r = dfs(x, y); + if (r<0) cout<<"no path\n"; + else cout << r << endl; + } + } +} diff --git a/uva_cpp_clean/10048/10048-6.cpp b/uva_cpp_clean/10048/10048-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a7a50085ae5a423c30cf7815330cbcd33ad2026 --- /dev/null +++ b/uva_cpp_clean/10048/10048-6.cpp @@ -0,0 +1,110 @@ +/* + graphs > minimum spanning tree (MST) > minimax path + difficulty: easy + date: 08/Sep/2020 + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +int V; +vector> edge_list; // {{w, u, v}, ...}, arestas u -w-> v + +class ufds { + private: + vector parent; // parent[n] = pai do elemento n + vector size; // size[n] = tamanho do conjunto identificado por n + int qty_disjoint_sets; + + public: + ufds() {} + ufds(int N) { + parent.resize(N); + size.assign(N, 1); + qty_disjoint_sets = N; + + // inicialmente, há N conjuntos disjuntos + for (int n = 0; n < N; n++) parent[n] = n; + } + + /* O(1) - retorna a raiz do conjunto de n */ + int find_set(int n) { + if (parent[n] == n) return n; + return parent[n] = find_set(parent[n]); // path compression + } + + /* O(1) - os conjuntos de n e de m são os mesmos? */ + bool is_same_set(int n, int m) { + return find_set(n) == find_set(m); + } + + /* O(1) - conecta os conjuntos de n e de m */ + void union_sets(int n, int m) { + int n_id = find_set(n); + int m_id = find_set(m); + + // union by size: conecta a menor árvore à maior árvore + if (!is_same_set(n_id, m_id)) { + if (size[n_id] > size[m_id]) + swap(n_id, m_id); + + parent[n_id] = m_id; + size[m_id] += size[n_id]; + + qty_disjoint_sets--; + } + } + + /* O(1) - tamanho do conjunto de n */ + int set_size(int n) { + return size[find_set(n)]; + } + + /* O(1) - quantidade de conjuntos disjuntos */ + int count() { + return qty_disjoint_sets; + } +}; + +/* O(E*log(V)) - returns the max_w of the minimum path from s to t, or -1 if not exists path from s to t */ +ll kruskal(int s, int t) { + ufds sets(V); + sort(edge_list.begin(), edge_list.end()); + + // para cada aresta u -w-> v, priorizando as de menor w + for (auto edge: edge_list) { + int u, v; ll w; tie(w,u,v) = edge; + + // evita ciclos + if (!sets.is_same_set(u, v)) { + sets.union_sets(u, v); + // se s e t já se conectaram, as próximas arestas da MST não farão parte do caminho s -> t + if (sets.is_same_set(s, t)) + return w; + } + } + return -1; +} + +int main() { + int E, Q, test = 1; + while (cin >> V >> E >> Q && (V || E || Q)) { + if (test > 1) cout << endl; + edge_list.clear(); + while (E--) { + int u, v; ll w; cin >> u >> v >> w; u--; v--; + edge_list.push_back({w, u, v}); + } + + cout << "Case #" << (test++) << endl; + while (Q--) { + int s, t; cin >> s >> t; s--; t--; + ll max_w = kruskal(s, t); + if (max_w == -1) cout << "no path" << endl; + else cout << max_w << endl; + } + } + + return 0; +} diff --git a/uva_cpp_clean/10048/10048-9.cpp b/uva_cpp_clean/10048/10048-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..515c0e54f0ad7d62e6f600d80c06e9a0a2e21d54 --- /dev/null +++ b/uva_cpp_clean/10048/10048-9.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int LIM = 110, SIZE = 10000; + +typedef pair ii; + +struct Edge_{ + int u, v, w; + bool operator < (const Edge_& other) const{ + return ( w < other.w ); + } +}E[SIZE]; + +int tc, N, M, Q, S, D, edge; +vector < ii > AdjList[LIM]; +bool vis[LIM]; + +void dfs(int u, int v, int w){ + vis[u] = true; + if( u == v ){ + edge = w; + return; + } + for(int it = 0; it < AdjList[u].size(); it++) + if( not vis[AdjList[u][it].first] ) + dfs(AdjList[u][it].first, v, max(w, AdjList[u][it].second) ); +} + +void Kruskal(){ + UnionFind UF(N + 1); + int setSize = 1; + sort(E, E + M); + for(int it = 0; it < M and setSize != N; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ){ + AdjList[E[it].u].push_back( ii(E[it].v, E[it].w) ); + AdjList[E[it].v].push_back( ii(E[it].u, E[it].w) ); + setSize++, UF.unionSet(E[it].u, E[it].v); + } +} + +void clear_(){ + for(int it = 1; it <= N; it++) AdjList[it].clear(); +} + +int main(){ + while(scanf("%d %d %d", &N, &M, &Q), N | M | Q){ + if(tc++) putchar('\n'); + for(int it = 0; it < M; it++) + scanf("%d %d %d", &E[it].u, &E[it].v, &E[it].w); + Kruskal(); + printf("Case #%d\n", tc); + for(int it = 0; it < Q; it++){ + scanf("%d %d", &S, &D); + fill(vis, vis + N + 1, false); + edge = -1; + dfs(S, D, 0); + if( edge == -1 ) puts("no path"); + else printf("%d\n", edge); + } + clear_(); + } + return(0); +} diff --git a/uva_cpp_clean/10049/10049-19.cpp b/uva_cpp_clean/10049/10049-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2061c512a52fe413c90ed9322936e3edb9f0fb66 --- /dev/null +++ b/uva_cpp_clean/10049/10049-19.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +using namespace std; + +int main() { + vector v; + int last = 5; + v.push_back(0); + v.push_back(1); + v.push_back(3); + v.push_back(5); + for (int i = 4; true; i++) { + int res = lower_bound(v.begin(), v.end(), i) - v.begin(); + last += res; + v.push_back(last); + if (last > 2000000000) break; + } + int n; + while (cin >> n && n != 0) { + int res = lower_bound(v.begin(), v.end(), n) - v.begin(); + printf("%d\n", res); + } + return 0; +} diff --git a/uva_cpp_clean/10050/10050-14.cpp b/uva_cpp_clean/10050/10050-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0e60255534aac5050a0ad06dcf6f977df9eba3a1 --- /dev/null +++ b/uva_cpp_clean/10050/10050-14.cpp @@ -0,0 +1,67 @@ +/*************************************************** + * Problem name : 10050 Hartals.cpp + * Problem Link : https://uva.onlinejudge.org/external/100/10050.pdf + * OJ : Uva + * Verdict : Accepted + * Date : 27.05.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define debug printf("#########\n") +#define nl printf("\n") +#define MAX 103 +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +using namespace std; +typedef long long ll; +int main () { + int tc, N, P, h[MAX]; + mapmp; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d %d", &N, &P); + for (int i = 0; i < P; i++) { + scanf("%d", &h[i]); + int f = 6, s = 7; + //~ printf("%d %d\n", f,s); + for (int j = h[i]; j <= N; j += h[i]) { + //~ printf("%d %d\n", f,s); + while(j > s) { + f += 7; + s += 7; + } + if (j != f && j != s) { + //~ printf("%d ", j); + mp[j] = 1; + } + } + //~ nl; + } + int sz = mp.size(); + printf("%d\n", sz); + mp.clear(); + } + + return 0; +} + diff --git a/uva_cpp_clean/10050/10050-15.cpp b/uva_cpp_clean/10050/10050-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e6e13444fb6853a4617c82ac78f82eb0c6fe6fb --- /dev/null +++ b/uva_cpp_clean/10050/10050-15.cpp @@ -0,0 +1,33 @@ +/* + 模擬 +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_testcase; + cin >> n_testcase; + while (n_testcase--) { + int sum = 0, day, n, m; + cin >> day >> n; + vector calendar(day, false); + + for (int i = 0; i < n; i++) { + cin >> m; + for (int j = m - 1; j < day; j += m) + calendar[j] = true; + } + + for (int i = 0; i < day; i++) + if (calendar[i] && i % 7 < 5) + sum++; + + cout << sum << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10050/10050-19.cpp b/uva_cpp_clean/10050/10050-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1dc88e517800d2625a3666a854e813764ad771a1 --- /dev/null +++ b/uva_cpp_clean/10050/10050-19.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +int main() { + unordered_set set; + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + set.clear(); + int N, P; + scanf("%d", &N); scanf("%d", &P); + for (int j = 0; j < P; j++) { + int hartal; + scanf("%d", &hartal); + for (int m = hartal; m <= N; m+=hartal) if(m % 7 != 0 && m % 7 != 6) set.insert(m); + } + printf("%d\n", set.size()); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10050/10050-20.cpp b/uva_cpp_clean/10050/10050-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c171e30cf3b9470d964d769caaefcb8c3dc2c04c --- /dev/null +++ b/uva_cpp_clean/10050/10050-20.cpp @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +int parties[105]; + +int main() +{ + int tc; + + cin >> tc; + while( tc-- ) + { + int N, P; + cin >> N >> P; + for(int i=0 ; i

> parties[i]; + + int lost_days = 0; + for(int i=1 ; i<=N ; i++) + { + if( i%7==6 || i%7==0 ) continue; + else + { + bool flag = false; + for(int j=0 ; j

>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10050 + Name: Hartals + Problem: https://onlinejudge.org/external/100/10050.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() +{ + bool days[3651]; + int t,k,n,p,i,j,count; + + cin>>t; + while(t--){ + int n; + cin>>n; + for(i=1;i<=n;i++)days[i]=0; + + int p; + cin>>p; + for(i=0;i>k; + for(j=k;j<=n;j+=k)days[j]=true; + } + count=0; + for(i=1;i<=n;i++)if(i%7==6)i++;else if(days[i])count++; + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int visit[3700]; + +int main() +{ + int t; + si(t); + while(t--) + { + int n,p; + sii(n,p); + + int cnt=0; + while(p--) + { + int x; + si(x); + for(int i=x;i<=n;i+=x) + { + if(i%7!=6 && i%7!=0 && visit[i]!=1) + { + visit[i]=1; + cnt++; + } + } + } + + P(cnt); + + cover(visit,0); + } + return 0; +} diff --git a/uva_cpp_clean/10051/10051-21.cpp b/uva_cpp_clean/10051/10051-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb3588acf4a572babced2e10597c1fb9a55ed7cf --- /dev/null +++ b/uva_cpp_clean/10051/10051-21.cpp @@ -0,0 +1,99 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10051 + Name: Tower of Cubes + Problem: https://onlinejudge.org/external/100/10051.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// -------------------------------------------------------------------- + +char fnames[6][10] = { + "front", "back", + "left", "right", + "top", "bottom" +}; + +int C[555][6], D[555][6], n; +struct State { + int i, j; + State():i(0),j(0){} + State(int i, int j):i(i),j(j){} +} P[555][6]; + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; (n=readUInt()); ++cse) { + for (int i=0; i=0; --i) + for (int c=0; c<6; ++c) { + int bc = C[i][c^1]; + + D[i][c] = 1; + for (int j=i+1; jD[i][c]) + D[i][c] = D[j][k]+1, + P[i][c] = State(j, k); + + if (D[i][c] > bestd) { + bestd = D[i][c]; + best = State(i, c); + } + } + + if (cse > 1) cout << "\n"; + cout << "Case #" << cse << "\n" << bestd << "\n"; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10054 + Name: The Necklace + Problem: https://onlinejudge.org/external/100/10054.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------- + +int E[51][51], S[51]; +void dfs(int u) { + for (int v=1; v<=50; ++v) + if (E[u][v]) { + --E[u][v]; --E[v][u]; + dfs(v); + cout << v << ' ' << u << "\n"; + } +} + +char BUF[8000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 8000000); + ios_base::sync_with_stdio(0); + + int T = readUInt(); + for (int cse=1; cse<=T; ++cse) { + int n = readUInt(); + + if (cse > 1) cout << "\n"; + cout << "Case #" << cse << "\n"; + + memset(E, 0, sizeof(E)); + memset(S, 0, sizeof(S)); + + int mn = 100; + for (int i=0; i +/* +Problem: 10055 - Hashmat the Brave Warrior +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=996 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +ll n1, n2; // n1 --> Hashmat's soldiers, n2 --> opponent's soldiers + +int main () { + + while (scanf("%lld%lld", &n1, &n2) != EOF){ + + printf("%lld\n", abs(n1-n2)); + + } + + return 0; +} +/* +Sample input:- +----------------- +10 12 +10 14 +100 200 + +Sample output:- +----------------- +2 +4 +100 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10055/10055-15.cpp b/uva_cpp_clean/10055/10055-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cb5c8ff1216cb0619de97bde77d886942e3e6f3 --- /dev/null +++ b/uva_cpp_clean/10055/10055-15.cpp @@ -0,0 +1,17 @@ +/* + 絕對值 +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + long long n, m; + while (cin >> n >> m) + cout << abs(m - n) << "\n"; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10055/10055-18.cpp b/uva_cpp_clean/10055/10055-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8613444dc8b7682313464c08ebbd2dd898d1c3ea --- /dev/null +++ b/uva_cpp_clean/10055/10055-18.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main(){ + long long n1, n2; + while(true){ + cin >> n1 >> n2; + if(cin.fail()) break; + cout << abs(n1-n2) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10055/10055-19.cpp b/uva_cpp_clean/10055/10055-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9403716e2855efa9d7ff5ff3b65720a6032dcaa3 --- /dev/null +++ b/uva_cpp_clean/10055/10055-19.cpp @@ -0,0 +1,12 @@ +#include +using namespace std; + +int main() { + long i, j; + while (scanf("%ld %ld", &i, &j) != EOF) { + long diff = j-i; + if (diff < 0) diff = i-j; + printf("%ld\n",diff); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10055/10055-21.cpp b/uva_cpp_clean/10055/10055-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4523855b90640ecabe00503169d66e25b5fd9636 --- /dev/null +++ b/uva_cpp_clean/10055/10055-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10055 + Name: Hashmat the Brave Warrior + Problem: https://onlinejudge.org/external/100/10055.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + long long int x, y; + while (cin>>x>>y) + cout << (x +#include + +using namespace std; + +int main() +{ + long long army1, army2; + while ( scanf("%lld %lld", &army1, &army2) != EOF ) + printf("%lld\n", abs(army1-army2)); +} diff --git a/uva_cpp_clean/10055/10055-5.cpp b/uva_cpp_clean/10055/10055-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..793411386940a929f1ede8b29ab3ab9c8664a1ae --- /dev/null +++ b/uva_cpp_clean/10055/10055-5.cpp @@ -0,0 +1,11 @@ +#include +#include +using namespace std; +int main() +{ + long long a,b; + while(cin>>a>>b) + cout< + +using namespace std; + +int main(){ + long long a, b; + while(scanf("%lld %lld", &a, &b) == 2) + printf("%lld\n", abs(b - a)); + return(0); +} diff --git a/uva_cpp_clean/10055/main.cpp b/uva_cpp_clean/10055/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..456d74b7b2c26106ce2f166972628151f23b966b --- /dev/null +++ b/uva_cpp_clean/10055/main.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main(){ + +long mios, enemigos; + +while (cin >> mios >> enemigos){ + + if (enemigos >= mios){ + cout << enemigos - mios << endl; + } + else{ + cout << mios - enemigos << endl; + } +} +return 0; + +} diff --git a/uva_cpp_clean/10056/10056-15.cpp b/uva_cpp_clean/10056/10056-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e51b41db613e0e123d8b9b65c7520aa1ff1c0f20 --- /dev/null +++ b/uva_cpp_clean/10056/10056-15.cpp @@ -0,0 +1,41 @@ +/* + 數學 +*/ +#include +#include +#include + +using namespace std; + +double power(double base, int exponential) { + if (exponential == 0) + return 1; + else if (exponential == 1) + return base; + + double ans = power(base, exponential / 2); + + if (exponential & 1) + return ans * ans * base; + return ans * ans; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_testcase; + cin >> n_testcase; + while (n_testcase--) { + int n_people, index; + double probability; + cin >> n_people >> probability >> index; + double q = 1 - probability; + double common_ratio = power(q, n_people); + + if (common_ratio == 1) + cout << "0.0000\n"; + else + cout << setprecision(4) << fixed << power(q, index - 1) * probability / (1 - common_ratio) << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10057/10057-15.cpp b/uva_cpp_clean/10057/10057-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..351691602210c02fd0383389a8ee9e3fa78141e0 --- /dev/null +++ b/uva_cpp_clean/10057/10057-15.cpp @@ -0,0 +1,38 @@ +/* + 中位數 +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n; + while ((cin >> n)) { + vector number(n, 0); + unordered_map counter; + + for (int i = 0; i < n; i++) { + cin >> number[i]; + counter[number[i]]++; + } + + sort(number.begin(), number.end()); + + int mid = (number.size() - 1) / 2; + + if (number.size() & 1) + cout << number[mid] << " " << counter[number[mid]] << " 1\n"; + else { + int temp = 0; + for (int i = number[mid]; i <= number[mid + 1]; i++) + temp += counter[i]; + cout << number[mid] << " " << temp << " " << number[mid + 1] - number[mid] + 1 << "\n"; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10057/10057-21.cpp b/uva_cpp_clean/10057/10057-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e98f25532a6eff2ef19529932676779adf6aec9 --- /dev/null +++ b/uva_cpp_clean/10057/10057-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10057 + Name: A mid-summer night's dream. + Problem: https://onlinejudge.org/external/100/10057.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; +#define EPS 1e-9 + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return 0; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +// --------------------------------------------------------------- + +int n, X[1000017]; +int main() { + while ((n=readUInt())) { + for (int i=0; i>1; + nth_element(X, X+mid , X+n); + int a1 = X[mid], a2 = a1; + if (n%2 == 0) { + nth_element(X, X+mid+1, X+n); + a2 = X[mid+1]; + } + + int cnt = 0; + for (int i=0; i + +#define MAX_N 1000010 +#define MAX_V 65540 + +using namespace std; + +int n, nn, v[MAX_N], ct[MAX_V]; + +int main(){ + while(scanf("%d",&n) == 1){ + memset(ct, 0, sizeof ct); + for(int i = 0; i < n; i++) scanf("%d", &v[i]), ct[v[i]]++; + nn = n>>1; + if(n&1){ + nth_element(v, v + nn,v + n); + printf("%d %d 1\n", v[nn], ct[v[nn]]); + } + else{ + nth_element(v,v + nn,v + n); + nth_element(v,v + nn - 1,v + n); + if(v[nn] == v[nn - 1]) printf("%d %d 1\n",v[nn], ct[v[nn]]); + else printf("%d %d %d\n",v[nn - 1],ct[v[nn - 1]]+ct[v[nn]],v[nn] - v[nn - 1] + 1); + } + } + return(0); +} diff --git a/uva_cpp_clean/10061/10061-21.cpp b/uva_cpp_clean/10061/10061-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c86d58ea33c3c455a2b8ca931ff159a68adb758 --- /dev/null +++ b/uva_cpp_clean/10061/10061-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10061 + Name: How many zero's and how many digits ? + Problem: https://onlinejudge.org/external/100/10061.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + + +int count(int n, int k) { + int r = 0; + for (int i=k, t; (t=n/i); i*=k) + r += t; + return r; +} + +int TZ(int n, int b) { + int r = INF, m=b; + for (int i=2; i*i<=m; ++i) + if (m%i == 0) { + int c = 0; + while (m%i == 0) + m /= i, + c++; + + r = min(r, count(n, i)/c); + } + if (m > 1) + r = min(r, count(n, m)); + return r; +} + +double LF[(1<<20)+1]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i=1; i<=(1<<20); ++i) + LF[i] = LF[i-1] + log(i); + + int n, b; + while (cin >> n >> b) { + cout << TZ(n, b) << ' ' << int(LF[n]/log(b)+1) << endl; + } +} diff --git a/uva_cpp_clean/10061/10061-9.cpp b/uva_cpp_clean/10061/10061-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a47b9d4f28a1c2bf11b3bd2af2bf8d59c031799d --- /dev/null +++ b/uva_cpp_clean/10061/10061-9.cpp @@ -0,0 +1,62 @@ +#include + +#define SIZE 30 + +using namespace std; + +bool is_prime[SIZE]; +vector prime; +vector < pair > Factor; +int N, B; +const long double EPS = 1e-15; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(int it1 = 2; it1 < SIZE; it1++) + if( is_prime[it1] ){ + for(int it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +void GetFactors(int n){ + Factor.clear(); + for(int it = 0; it < prime.size() and prime[it] <= n and n != 1; it++) + if( n % prime[it] == 0 ){ + int ct = 0; + while( n % prime[it] == 0 ) n /= prime[it], ct++; + Factor.push_back( make_pair(prime[it], ct) ); + } + if( n != 1 ) Factor.push_back( make_pair(n, 1) ); +} + +int trailingZeros(){ + int zeros = INT_MAX; + for(int it = 0; it < Factor.size(); it++){ + int p = Factor[it].first, e = 0; + while( N / p ) e += N / p, p *= Factor[it].first; + zeros = min(zeros, e / Factor[it].second); + } + return zeros; +} + +inline long double logB(long double a,long double base){ + return log10(a) / log10(base); +} + +int NumberDigits(){ + long double digits = 0; + for(int it = 2; it <= N; it++) digits += logB( it, B); + return ceil( digits + EPS ); +} + +int main(){ + sieve(); + while(scanf("%d %d", &N, &B) == 2){ + GetFactors(B); + printf("%d %d\n", trailingZeros(), NumberDigits()); + } + return(0); +} diff --git a/uva_cpp_clean/10062/10062-14.cpp b/uva_cpp_clean/10062/10062-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..097bb1feba6641c7ada0864dbb4615e0e6d7e153 --- /dev/null +++ b/uva_cpp_clean/10062/10062-14.cpp @@ -0,0 +1,95 @@ +/*************************************************** + * Problem name : 10062 Tell me the frequencies!.cpp + * Problem Link : https://uva.onlinejudge.org/external/100/10062.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-27 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,0,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 260 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +struct st { + int ch; + int num; +}; +bool compare(st a, st b) { + if (a.num == b.num) return a.ch > b.ch; + else return a.num < b.num; +} +int main () { + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + string str; + int mp[MAX], t = 0; + //~ mapmp; + while (getline(cin,str)) { + SET(mp); + int sz = str.size(); + for (int i = 0; i < sz; i++) { + mp[str[i]]++; + } + st ar[MAX]; + int j = 0; + for (int i = 32; i <= 128; i++) { + if (mp[i] != 0 ) { + ar[j].ch = i; + ar[j].num = mp[i]; + j++; + } + } + sort(ar, ar + j, compare); + if(t) nl; + t++; + for (int i = 0; i < j; i++) { + printf("%d %d\n", ar[i].ch, ar[i].num); + } + //~ mp.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/10062/10062-15.cpp b/uva_cpp_clean/10062/10062-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5259f7a3eed82c749a3b1c7ba0d6236a0a8094d6 --- /dev/null +++ b/uva_cpp_clean/10062/10062-15.cpp @@ -0,0 +1,62 @@ +/* + 排序 + 時間複雜度: O(NlogN) +*/ +#include +#include +#include +#include + +using namespace std; + +class Count { + private: + int alphabet, counter; + + public: + static int now; + Count() { + alphabet = ++now; + counter = 0; + } + void operator++() { + ++counter; + } + bool operator<(const Count& other) const { + if (this->counter == other.counter) + return this->alphabet > other.alphabet; + return this->counter < other.counter; + } + friend ostream& operator<<(ostream& out, const Count& count) { + if (count.counter) + out << count.alphabet << " " << count.counter << "\n"; + return out; + } +}; + +int Count::now = -1; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + string str; + bool flag = false; + while (getline(cin, str)) { + if (flag) + cout << "\n"; + + vector counter(129); + + for (const char& c : str) + ++counter[c]; + + sort(counter.begin(), counter.end()); + + for (Count i : counter) + cout << i; + + Count::now = -1; + flag = true; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10062/10062-19.cpp b/uva_cpp_clean/10062/10062-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f327ab330891cc70b11ea7aee325f77c38c8740 --- /dev/null +++ b/uva_cpp_clean/10062/10062-19.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + string s; + bool first = true; + while (getline(cin, s)) { + if (first) first = false; + else printf("\n"); + vector > v(96, make_pair(0, 0)); + for (int i = 0; i < 96; i++) v[i].second = -(i + 32); + for (int i = 0; i < s.length(); i++) v[s[i] - 32].first++; + sort(v.begin(), v.end()); + for (int i = 0; i < 96; i++) { + if (v[i].first == 0) continue; + printf("%d %d\n", -v[i].second, v[i].first); + } + } + return 0; +} diff --git a/uva_cpp_clean/10062/10062-20.cpp b/uva_cpp_clean/10062/10062-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9f5d853ed510c27b6ffceb632e57d6c11cc668f --- /dev/null +++ b/uva_cpp_clean/10062/10062-20.cpp @@ -0,0 +1,39 @@ +#include +#include + +char line[1005]; +int counter[130]; + +int main() +{ + bool new_line = false; + + while( gets(line) ) + { + for(int i=0 ; i<130 ; i++) counter[i] = 0; + + int len = strlen(line); + for(int i=0 ; i=0 ; j--) + { + if( counter[j]==i ) + { + printf("%d %d\n", j, i); + len--; + } + } + if( !len ) break; + } + } + return 0; +} diff --git a/uva_cpp_clean/10062/10062-5.cpp b/uva_cpp_clean/10062/10062-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4e652edd278be9cafa036cdb0fc56ab29db9ad1b --- /dev/null +++ b/uva_cpp_clean/10062/10062-5.cpp @@ -0,0 +1,110 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; + +ll gcd(ll a, ll b) +{ + if (b==0) return a; + return gcd(b, a%b); +} + +ll lcm(ll a, ll b) +{ + return a/gcd(a, b)*b; +} + +bool cmp(ll a,ll b) +{ + return a > b; +} + +int main() +{ +// input; +// output; + string s; + int f=0; + while(getline(cin,s)) + { + if(f) + pf("\n"); + int l=s.size(); + mapmp; + mapmp1; + priority_queuepq; + for(int i=0;i:: iterator it; + int a=-pq.top(); + for(it=mp.end();;it--) + { + if(a==it->se) + { + cout<ff<<" "<se<se=0; + break; + } + } + pq.pop(); + } + f=1; + } + return 0; +} diff --git a/uva_cpp_clean/10063/10063-19.cpp b/uva_cpp_clean/10063/10063-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d9acb69ced1e2c1cd9b1a4c7979b54eafcca9ea --- /dev/null +++ b/uva_cpp_clean/10063/10063-19.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +string s; + +void proc(char p[12], int l) { + char next[12]; + if (l == s.length()) { + for (int i = 0; i < l; i++) printf("%c", p[i]); + printf("\n"); + } else { + for (int i = 0; i < l; i++) next[i+1] = p[i]; + next[0] = s[l]; + for (int i = 0; i <= l; i++) { + proc(next, l + 1); + char auxi = next[i]; + next[i] = next[i+1]; + next[i+1] = auxi; + } + } +} + +int main() { + char p[12]; + bool first = false; + while (cin >> s) { + if (first) printf("\n"); + else first = true; + proc(p, 0); + } + return 0; +} diff --git a/uva_cpp_clean/10063/10063-21.cpp b/uva_cpp_clean/10063/10063-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a9235a811cec88934d48faa731248bc7fdb1dcc --- /dev/null +++ b/uva_cpp_clean/10063/10063-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10063 + Name: Knuth's Permutation + Problem: https://onlinejudge.org/external/100/10063.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char S[17], P[17], n; +void dfs(int idx) { + if (idx == n) { + puts(P); + return; + } + + for (int i=idx; i>0; --i) + P[i] = P[i-1]; + + for (int i=0; i<=idx; ++i) { + P[i] = S[idx]; + dfs(idx + 1); + P[i] = P[i+1]; + } +} + +int main() { + bool frst = 1; + while (cin.getline(S, 17)) { + if (frst) frst = 0; + else puts(""); + + P[ n=strlen(S) ] = 0; + dfs(0); + } +} diff --git a/uva_cpp_clean/10065/10065-19.cpp b/uva_cpp_clean/10065/10065-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7aaa3668125a3de5cbac3d5e2ff48ee6188efb5 --- /dev/null +++ b/uva_cpp_clean/10065/10065-19.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long double ld; +typedef complex point, vect; +typedef vector polygon; +typedef polygon::iterator polIter; +const ld inf = 1e9; const ld eps = 1e-9; const ld pi = acos(-1.0); + +ld area(polygon &p) { + ld result = 0.0; + for (int i = 0; i < p.size(); i++) + result += imag(conj(p[i])*p[(i+1)%p.size()]); + return abs(result) / 2.0; +} + +double dist(point p1, point p2) { + return hypot(p1.real() - p2.real(), p1.imag() - p2.imag()); +} + +double cross(vect a, vect b) { + return a.real() * b.imag() - a.imag() * b.real(); +} + +vect toVec(point a, point b) { + return vect(b.real() - a.real(), b.imag() - a.imag()); +} + +bool ccw(point p, point q, point r) { + return cross(toVec(p, q), toVec(p, r)) > 0; +} + +bool collinear(point p, point q, point r) { + return fabs(cross(toVec(p, q), toVec(p, r))) < eps; +} + +point pivot(0, 0); +bool angleCmp(point a, point b) { + if (collinear(pivot, a, b)) return dist(pivot, a) < dist(pivot, b); + double d1x = a.real() - pivot.real(), d1y = a.imag() - pivot.imag(); + double d2x = b.real() - pivot.real(), d2y = b.imag() - pivot.imag(); + return (atan2(d1y, d1x) - atan2(d2y, d2x)) < 0; +} + +polygon CH(polygon P) { + int i, j, n = (int)P.size(); + if (n <= 3) { + if (!(P[0] == P[n-1])) P.push_back(P[0]); + return P; + } + int P0 = 0; + for (i = 1; i < n; i++) + if (P[i].imag() < P[P0].imag() || (P[i].imag() == P[P0].imag() && P[i].real() > P[P0].real())) + P0 = i; + point temp = P[0]; P[0] = P[P0]; P[P0] = temp; + pivot = P[0]; + sort(++P.begin(), P.end(), angleCmp); + polygon S; + S.push_back(P[n-1]); S.push_back(P[0]); S.push_back(P[1]); + i = 2; + while (i < n) { + j = (int)S.size()-1; + if (ccw(S[j-1], S[j], P[i])) S.push_back(P[i++]); + else S.pop_back(); + } + return S; +} + +int main() { + int N, c = 1; + while (cin >> N && N != 0) { + polygon pol; + for (int i = 0; i < N; i++) { + double x, y; + cin >> x >> y; + pol.push_back(point(x, y)); + } + ld tArea = abs(area(pol)); + polygon chull = CH(pol); + ld hArea = abs(area(chull)); + printf("Tile #%d\n", c); + printf("Wasted Space = %.2Lf %%\n\n", ((hArea-tArea)*100)/hArea); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/10066/10066-10.cpp b/uva_cpp_clean/10066/10066-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a414b9675ea15a8b879b57b28f066cad6a993477 --- /dev/null +++ b/uva_cpp_clean/10066/10066-10.cpp @@ -0,0 +1,72 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define int long long +#define oo 1e15 +using namespace std; +/** + * by:Ahmed Sakr (sakr_) with AZA ; + * freopen("input.txt","r",stdin); + * freopen("output.txt","w",stdout); + * __builtin_popcountll(); +**/ +class utility { + +public : + static int gcd(int a, int b) { return (!b) ? a : gcd(b, a % b); } + static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } +}; + +int n1,n2; +int a[105],b[105]; + +int dp[105][105]; + +int solve(int i,int j) { + + if (i == n1 || j == n2) return 0; + + + int &ans = dp[i][j]; + if(ans!=-1) return ans; + + ans = -oo; + if (a[i] == b[j]) { + ans = max(ans, 1 + solve(i + 1, j + 1)); + } else { + ans = max(ans, solve(i + 1, j)); + ans = max(ans, solve(i, j + 1)); + } + return ans; +} + +void solveit() { + + int i; + int c=1; + + while (cin>>n1>>n2 && (n1!=0 && n2!=0)) { + + for(i=0;i>a[i]; + + for(i=0;i>b[i]; + + memset(dp,-1,sizeof dp); + + cout<<"Twin Towers #"<>timeOfRepeteLoop; + while (timeOfRepeteLoop--) { + solveit(); + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/10066/10066-14.cpp b/uva_cpp_clean/10066/10066-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..15fd7a9c2b8d9dd3b9d68f9e8be39d6332220884 --- /dev/null +++ b/uva_cpp_clean/10066/10066-14.cpp @@ -0,0 +1,52 @@ +/*************************************************** + * Problem name : 10066 - The Twin Towers.cpp + * OJ : Uva + * Result : AC + * Date : 27-04-17 + * Problem Type : DP(LCS) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define MAX 105 +using namespace std; +typedef long long ll; +int A[MAX], B[MAX]; +int dp[MAX][MAX]; +int n1, n2; +int LCS(int i, int j) { + if (i == n1 || j == n2) return 0; + if (dp[i][j] != -1) return dp[i][j]; + int ans = 0; + if (A[i] == B[j]) return ans = 1 + LCS(i + 1, j + 1); + else { + int val1 = LCS(i + 1, j); + int val2 = LCS(i, j + 1); + ans = max(val1, val2); + } + return dp[i][j] = ans; +} +int main () { + int t = 1; + while (scanf("%d %d", &n1, &n2) == 2 && n1 != 0 && n2 != 0) { + for (int i = 0; i < n1; i++) scanf("%d", &A[i]); + for (int i = 0; i < n2; i++) scanf("%d", &B[i]); + SET(dp); + printf("Twin Towers #%d\nNumber of Tiles : %d\n\n",t++, LCS(0, 0)); + } + return 0; +} + diff --git a/uva_cpp_clean/10066/10066-15.cpp b/uva_cpp_clean/10066/10066-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26933a595e30d95b5cb9515ecf85429b6d7e9a6e --- /dev/null +++ b/uva_cpp_clean/10066/10066-15.cpp @@ -0,0 +1,38 @@ +/* + 最長公共子序列 + 時間複雜度: O(N^2) +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, m, case_n = 1; + while (cin >> n >> m && (n || m)) { + ++m; + ++n; + vector tower_1(n, 0), tower_2(m, 0); + vector> dp(n, vector(m, 0)); + + for (int i = 1; i < n; i++) + cin >> tower_1[i]; + + for (int i = 1; i < m; i++) + cin >> tower_2[i]; + + for (int i = 1; i < n; i++) { + for (int j = 1; j < m; j++) { + if (tower_1[i] == tower_2[j]) + dp[i][j] = dp[i - 1][j - 1] + 1; + else + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + cout << "Twin Towers #" << case_n++ << "\nNumber of Tiles : " << dp.back().back() << "\n\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10066/10066-21.cpp b/uva_cpp_clean/10066/10066-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5872e444028fb2a1cfa0d3dc1597b521a6c2b178 --- /dev/null +++ b/uva_cpp_clean/10066/10066-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10066 + Name: The Twin Towers + Problem: https://onlinejudge.org/external/100/10066.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int a[102], b[102], DP[102][102]; +int main(){ + int n, m, cse=0; + + while (cin>>n>>m && (n||m)) { + for (int i=1; i<=n; i++) + cin >> a[i]; + + for (int j=1; j<=m; j++) { + cin >> b[j]; + for (int i=1; i<=n; i++) + DP[i][j]=0; + } + + for (int i=1; i<=n; i++) + for (int j=1; j<=m; j++) + if (a[i] == b[j]) + DP[i][j] = DP[i-1][j-1] + 1; + else if (j>0) + DP[i][j] = max(DP[i-1][j], DP[i][j-1]); + + printf("Twin Towers #%d\nNumber of Tiles : %d\n\n", ++cse, DP[n][m]); + } +} diff --git a/uva_cpp_clean/10066/10066-5.cpp b/uva_cpp_clean/10066/10066-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a75975e633181143f3a77ed734ed3bafcf1218bc --- /dev/null +++ b/uva_cpp_clean/10066/10066-5.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int i,j,n,m,lcs[2000][2000]; +int main() +{ + int a[110],b[110],no=0; + + while(sc("%d",&n)==1) + { + sc("%d",&m); + if(n==0&&m==0) + break; + for(i=0;i + +using namespace std; + +const int MAX_N = 1e2 + 10; + +int n1, n2, arr1[MAX_N], arr2[MAX_N], dp[MAX_N][MAX_N]; + +int lcs () { + for (int i = 1; i <= n1; i++) { + for (int j = 1; j <= n2; j++) { + if (arr1[i - 1] == arr2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; + else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[n1][n2]; +} + +int main () { + int tc = 0; + while (cin >> n1 >> n2, n1 or n2) { + for (int i = 0; i < n1; i++) cin >> arr1[i]; + for (int i = 0; i < n2; i++) cin >> arr2[i]; + printf("Twin Towers #%d\n", ++tc); + printf("Number of Tiles : %d\n", lcs()); + puts(""); + } + return (0); +} diff --git a/uva_cpp_clean/10067/10067-21.cpp b/uva_cpp_clean/10067/10067-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39ff6edbe9a1efdecbd9ae94f1e51d3f0a9cfa8b --- /dev/null +++ b/uva_cpp_clean/10067/10067-21.cpp @@ -0,0 +1,72 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10067 + Name: Playing with Wheels + Problem: https://onlinejudge.org/external/100/10067.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int readNum(){ + int a,b,c,d; + cin>>a>>b>>c>>d; + return a*1000+b*100+c*10+d; +} + +int rot(int num, int w) { + int sgn = w<0?-1:1; + w = w<0?-w-1:w-1; + int t=1; + for (int i=0; i>T; + while (T--) { + memset(d, 0, sizeof(d)); + int s = readNum(), + t = readNum(); + + cin>>n; + for (int i=0; i q; + q.push(s); + while (!q.empty()) { + int num = q.front(); q.pop(); + int dd = d[num]; + if (num == t) { result = dd; break; } + if (dd < 0) continue; + d[num] = -1; + + for (int i=-4; i<=4; i++) if(i) { + int nn = rot(num, i); + if (!d[nn]) { + q.push(nn); + d[nn] = dd+1; + } + } + } + + cout << result << endl; + } +} diff --git a/uva_cpp_clean/10069/10069-21.cpp b/uva_cpp_clean/10069/10069-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94b88180f3931ddbeb83865374c3818e1cac80c4 --- /dev/null +++ b/uva_cpp_clean/10069/10069-21.cpp @@ -0,0 +1,146 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10069 + Name: Distinct Subsequences + Problem: https://onlinejudge.org/external/100/10069.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 100]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + BigInt(const BigInt& o): cells(0), capacity(0) { + init(); + sign = o.sign; + setSize(o.size, 0); + for (int i=0; i= 0) + delete [] cells; + } + + public: + void setZero() { + sign = size = cells[0] = 0; + } + void setOne() { + sign = size = cells[0] = 1; + } + bool isZero() { + return sign==0; + } + + BigInt& operator=(const BigInt &o) { + sign = o.sign; + size = o.size; + for (int i=0; i= MAX_C ) { + cells[i] -= MAX_C; + cells[i+1]++; + } + return autoSize(); + } + // --------------------------------------------------------------------- + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; + + +char s1[10016], s2[117]; +BigInt DP[10017][103]; +int main(){ + int T; + scanf("%d", &T); + while (T--) { + scanf("%s%s", s1, s2); + int l1=strlen(s1), l2=strlen(s2); + + if (l1 < l2) puts("0"); + else { + for (int i=0; i +#include +using namespace std; + +void proc(string & s, bool & leap, bool & huluculu, bool & bulukulu) { + bool d3, d4, d5, d11, d100, d400; + d4 = (10*(s[s.length()-2]-'0') + (s[s.length()-1]-'0')) % 4 == 0; + d5 = s[s.length()-1] == '0' || s[s.length()-1] == '5'; + d100 = s[s.length()-1] == '0' && s[s.length()-2] == '0'; + d400 = d100 && ((10*(s[s.length()-4]-'0') + (s[s.length()-3]-'0')) % 4 == 0); + long long sum = 0, altsum = 0; + for (int i = 0; i < s.length(); i++) { + sum += s[i]-'0'; + if (i%2) altsum -= (s[i]-'0'); + else altsum += (s[i]-'0'); + } + d3 = sum%3 == 0; + d11 = altsum%11 == 0; + leap = d4 && (!d100 || d400); + huluculu = d3 && d5; + bulukulu = d5 && d11 && leap; +} + +int main() { + string s; + bool first = false; + while (getline(cin, s)) { + if (first) printf("\n"); + else first = true; + bool leap, huluculu, bulukulu; + proc(s, leap, huluculu, bulukulu); + if (!leap && !huluculu && !bulukulu) printf("This is an ordinary year.\n"); + else { + if (leap) printf("This is leap year.\n"); + if (huluculu) printf("This is huluculu festival year.\n"); + if (bulukulu) printf("This is bulukulu festival year.\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10070/10070-21.cpp b/uva_cpp_clean/10070/10070-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b92d92ee76b04fd26d40d4cc6ce7380606b701f --- /dev/null +++ b/uva_cpp_clean/10070/10070-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10070 + Name: Leap Year or Not Leap Year and ... + Problem: https://onlinejudge.org/external/100/10070.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +bool isleap(int y) { + return y%400==0 || (y%4==0 && y%100); +} + +int main(){ + char ch; + bool nfirst = false; + do { + bool fl = false; + int m4=0, m100=0, m400=0, m15=0, m55=0; + while ((ch=getchar())!='\n' && ch!=EOF) { + fl = true; + int x = ch - '0'; + m4 = (m4*10+x)%4; + m15 = (m15*10+x)%15; + m55 = (m55*10+x)%55; + m100 = (m100*10+x)%100; + m400 = (m400*10+x)%400; + } + + if (fl) { + if (nfirst) cout << endl; + else nfirst = true; + + bool il = m400==0 || (m4==0 && m100); + if (il) cout << "This is leap year.\n"; + if (m15==0) cout << "This is huluculu festival year.\n"; + if (il && m55==0) cout << "This is bulukulu festival year.\n"; + if (!il && m15) cout << "This is an ordinary year.\n"; + } + } while (ch != EOF); +} diff --git a/uva_cpp_clean/10070/10070-5.cpp b/uva_cpp_clean/10070/10070-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..12edba8d0f29e2c2690ea93e0230b52a82fa9371 --- /dev/null +++ b/uva_cpp_clean/10070/10070-5.cpp @@ -0,0 +1,49 @@ +#include +#include + +int divider(char y[],int z,int p) +{ + int c = 0,i; + for(i=0;i +/* +Problem: 10071 - Back to High School Physics +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1012 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int v, t; + +int main (){ + + while( scanf("%d%d", &v, &t) != EOF){ + printf("%d\n", 2*v*t); + } + + return 0; +} +/* +Sample input:- +----------------- +0 0 +5 12 + +Sample output:- +----------------- +0 +120 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10071/10071-15.cpp b/uva_cpp_clean/10071/10071-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d37865556ea7f67d1077aacf4af4a7eec8ab8359 --- /dev/null +++ b/uva_cpp_clean/10071/10071-15.cpp @@ -0,0 +1,17 @@ +/* + 數學? +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int v, t; + while (cin >> v >> t) + cout << 2 * v * t << "\n"; + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10071/10071-16.cpp b/uva_cpp_clean/10071/10071-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e2ec64726d655564179f15b1d1cd62f4986ab2d --- /dev/null +++ b/uva_cpp_clean/10071/10071-16.cpp @@ -0,0 +1,19 @@ +//============================================================================ +// Name : 10071 - Back To High School Physics.cpp +// Author : ztrixack +// Copyright : MIT +// Description : 10071 Back To High School Physics in C++, Ansi-style +// Run Time : 0.120 seconds +//============================================================================ + +#include +using namespace std; + +int main () { + long long int v, t; + while(cin >> v >> t) { + v *= t << 1; + cout << v << endl; + } + +} diff --git a/uva_cpp_clean/10071/10071-18.cpp b/uva_cpp_clean/10071/10071-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..96c972efdb35bd542c9399f741b3f33600c1ceb1 --- /dev/null +++ b/uva_cpp_clean/10071/10071-18.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main(){ + int v, t; + while(true){ + cin >> v >> t; + if(cin.fail()) break; + cout << v*t*2 << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10071/10071-19.cpp b/uva_cpp_clean/10071/10071-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3abf5a7fd3ecfa3fc74fd1323370cbcd4f79415c --- /dev/null +++ b/uva_cpp_clean/10071/10071-19.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; + +int main() { + int v, t; + while (scanf("%d %d", &v, &t) != EOF) printf("%d\n", 2*v*t); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10071/10071-21.cpp b/uva_cpp_clean/10071/10071-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd337f65356354eb6ad574694b1f16f82a13da80 --- /dev/null +++ b/uva_cpp_clean/10071/10071-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10071 + Name: Back to High School Physics + Problem: https://onlinejudge.org/external/100/10071.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int v, t; + while(cin>>v>>t) + cout<<2*v*t< + +int main() +{ + int v, t, x; + while ( scanf("%d %d",&v, &t) != EOF ) + { + x = 2 * t * v; + printf("%d\n", x); + } +} diff --git a/uva_cpp_clean/10071/10071-5.cpp b/uva_cpp_clean/10071/10071-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c80e6af47e227dc3980ea9cfff6e0f3bb3265416 --- /dev/null +++ b/uva_cpp_clean/10071/10071-5.cpp @@ -0,0 +1,20 @@ +#include +main() +{ +int v,t; + +while(scanf("%d %d",&v,&t)==2) + +{ +if(v>=-100 && v<=100) +{ + if(t>=0 && t<= 200) +printf("%d\n",2*v*t); +} + +else +break; +} + +return 0; +} diff --git a/uva_cpp_clean/10071/10071-9.cpp b/uva_cpp_clean/10071/10071-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8156a0c7f627ff0be15ee6bf284a87b5f7d9cba --- /dev/null +++ b/uva_cpp_clean/10071/10071-9.cpp @@ -0,0 +1,10 @@ +#include + +using namespace std; + +int main(){ + int v, t; + while(scanf("%d %d", &v, &t) == 2) + printf("%d\n", v * t * 2); + return(0); +} diff --git a/uva_cpp_clean/10071/main.cpp b/uva_cpp_clean/10071/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fb899f4446a5b5fc853b03d2e284badfbdcde61 --- /dev/null +++ b/uva_cpp_clean/10071/main.cpp @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main(){ + + int a, b; + while(cin >> a >>b) cout << a*b*2 << endl; + + +return 0; +} diff --git a/uva_cpp_clean/10074/10074-14.cpp b/uva_cpp_clean/10074/10074-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a924d0173dbb9a5105a85eb3cc75c72e2687e156 --- /dev/null +++ b/uva_cpp_clean/10074/10074-14.cpp @@ -0,0 +1,174 @@ +/*************************************************** + * Problem Name : 10074 - Take the Land.cpp + * Problem Link : https://uva.onlinejudge.org/external/100/10074.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : DP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 105 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int tree[MAX * 4][MAX * 4]; +int vec[MAX][MAX]; +int n, m, mx; +void build (int indx, int nod, int low, int high) { + if (low == high) { + tree[indx][nod] = low; + return ; + } + + int mid = (high + low) / 2; + int left = nod * 2; + int right = nod * 2 + 1; + build (indx, left, low, mid); + build (indx, right, mid + 1, high); + + if (vec[indx][tree[indx][left]] <= vec[indx][tree[indx][right]]) { + tree[indx][nod] = tree[indx][left]; + + } else { + tree[indx][nod] = tree[indx][right]; + } +} +int query (int indx, int nod, int low, int high, int qlow, int qhigh) { + if (qlow > high || qhigh < low) { + return 0; + } + + if (qlow <= low && qhigh >= high) { + return tree[indx][nod]; + } + + int mid = (high + low) / 2; + int left = nod * 2; + int right = nod * 2 + 1; + int a = query (indx, left, low, mid, qlow, qhigh); + int b = query (indx, right, mid + 1, high, qlow, qhigh); + + if (vec[indx][a] <= vec[indx][b]) { + return a; + + } else { + return b; + } +} +void solve (int indx, int l, int r) { + if (l > r) return ; + + int idx = query (indx, 1, 1, m, l, r); + int xx = vec[indx][idx] * (r - l + 1); + mx = max (mx, xx); + solve (indx, l, idx - 1); + solve (indx, idx + 1, r); +} +int main () { + __FastIO; + while (cin >> n >> m) { + if (n == 0 && m == 0) break; + + SET (tree, 0); + SET (vec, 0); + int ar[MAX][MAX]; + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + cin >> ar[i][j]; + } + } + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (i == 1) { + if (ar[i][j] == 0) { + vec[i][j] = 1; + + } else { + vec[i][j] = 0; + } + + } else { + if (ar[i][j] == 0) { + vec[i][j] = vec[i - 1][j] + 1; + + } else { + vec[i][j] = 0; + } + } + } + } + + for (int i = 0; i < MAX; i++) { + vec[i][0] = 1000000; + } + + int ans = 0; + + for (int i = 1; i <= n; i++) { + build (i, 1, 1, m); + } + + for (int i = 1; i <= n; i++) { + mx = 0; + solve (i, 1, m); + ans = max (ans, mx); + } + + cout << ans << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/10074/10074-9.cpp b/uva_cpp_clean/10074/10074-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33077f8ddcbdd3a840d24cacd5682db52a0723c4 --- /dev/null +++ b/uva_cpp_clean/10074/10074-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int n, m, ans, area, v[105][105]; + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + for(int i = 1; i <= n; i++) + for(int j = 1; j <= m; j++) + scanf("%d", &v[i][j]), v[i][j] += v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1]; + ans = 0; + for(int i = 1; i <= n; i++) + for(int j = 1; j <= m; j++) + for(int ii = i; ii <= n; ii++) + for(int jj = j; jj <= m; jj++){ + if((ii - i + 1) * (jj - j + 1) > ans){ + area = v[ii][jj] - v[ii][j - 1] - v[i - 1][jj] + v[i - 1][j - 1]; + if(area == 0) ans = (ii - i + 1) * (jj - j + 1); + } + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10077/10077-15.cpp b/uva_cpp_clean/10077/10077-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79bffd796c2892f9f157247340e42b8edcc983ad --- /dev/null +++ b/uva_cpp_clean/10077/10077-15.cpp @@ -0,0 +1,74 @@ +/* + 遞迴、二元樹 +*/ +#include +#include + +using namespace std; + +class Fraction { + int numerator; + int denominator; + + public: + Fraction(int numerator, int denominator) { + this->denominator = denominator; + this->numerator = numerator; + } + + Fraction() {} + + Fraction& operator+=(const Fraction& other) { + this->denominator += other.denominator; + this->numerator += other.numerator; + int&& factor = __gcd(denominator, numerator); + this->denominator /= factor; + this->numerator /= factor; + return *this; + } + + bool operator!=(const Fraction& other) const { + return this->denominator != other.denominator || this->numerator != other.numerator; + } + + bool operator<(const Fraction& other) const { + return value() < other.value(); + } + + bool operator>(const Fraction& other) const { + return other.value() > value(); + } + + explicit operator bool() const { + return denominator != 1 || numerator != 1; + } + + friend istream& operator>>(istream& in, Fraction& fraction) { + in >> fraction.numerator >> fraction.denominator; + return in; + } + + double value() const { + return (double)numerator / denominator; + } +}; + +int main() { + Fraction to_find; + while (cin >> to_find && to_find) { + Fraction left = {0, 1}, middle = {1, 1}, right = {1, 0}; + while (to_find != middle) { + if (to_find < middle) { + cout << "L"; + right = middle; + middle += left; + } else { + cout << "R"; + left = middle; + middle += right; + } + } + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10077/10077-20.cpp b/uva_cpp_clean/10077/10077-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ccb40b503d870c43290a5171d7183bb27eadd30b --- /dev/null +++ b/uva_cpp_clean/10077/10077-20.cpp @@ -0,0 +1,30 @@ +#include + +int main () +{ + int m, n; + + while( scanf("%d %d", &m, &n)==2 && ( m!=1 || n!=1 ) ) + { + int a = 0, b = 1, c = 1, d = 0, e = 1, f = 1; + while( true ) + { + if( e==m && f==n ) break; + + if( e*n>f*m ) + { + printf("L"); + b = e, d = f; + } + else + { + printf("R"); + a = e, c = f; + } + e = a + b; + f = c + d; + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10077/10077-9.cpp b/uva_cpp_clean/10077/10077-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb0524aa81b9f0630541e43cecab9b5fa7fed6dc --- /dev/null +++ b/uva_cpp_clean/10077/10077-9.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main(){ + int n, m, nn, mm, nl, ml, nr, mr; + while(scanf("%d%d", &n, &m), n != 1 || m != 1){ + nl = 0, nr = 1; + ml = 1, mr = 0; + while(true){ + nn = nl + nr; + mm = ml + mr; + if(nn == n && mm == m) + break; + if(m * nn < n * mm){ + putchar('R'); + nl = nn; + ml = mm; + } + else{ + putchar('L'); + nr = nn; + mr = mm; + } + } + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10078/10078-19.cpp b/uva_cpp_clean/10078/10078-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..efe9c5d8cd0b884ac402b979d88bfcc783951278 --- /dev/null +++ b/uva_cpp_clean/10078/10078-19.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + +typedef pair ii; + +int main() { + int N; + while (cin >> N && N != 0) { + ii last = ii(0, 0), first, second, p, v1 = ii(0, 0), v2; + int p1 = 0, p2 = 0; + bool ok = true; + for (int i = 0; i < N; i++) { + cin >> p.first >> p.second; + if (!ok) continue; + if (!i) first = p, last = p; + if (i == 1) second = p; + v2 = ii(p.first - last.first, p.second - last.second); + p2 = (v1.first * v2.second) - (v1.second * v2.first); + ok = ((p1 <= 0 && p2 <= 0) || (p1 >= 0 && p2 >= 0)); + p1 = p2; + v1 = v2; + last = p; + } + if (ok) { // We must repeat the process with the first and the second point. + p = first; + v2 = ii(p.first - last.first, p.second - last.second); + p2 = (v1.first * v2.second) - (v1.second * v2.first); + ok = ((p1 <= 0 && p2 <= 0) || (p1 >= 0 && p2 >= 0)); + p1 = p2; + v1 = v2; + last = p; + } + if (ok) { + p = second; + v2 = ii(p.first - last.first, p.second - last.second); + p2 = (v1.first * v2.second) - (v1.second * v2.first); + ok = ((p1 <= 0 && p2 <= 0) || (p1 >= 0 && p2 >= 0)); + } + if (ok) printf("No\n"); + else printf("Yes\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10079/10079-15.cpp b/uva_cpp_clean/10079/10079-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d50b20308deff0094ecac67d5451d7ba511920c --- /dev/null +++ b/uva_cpp_clean/10079/10079-15.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + long long n; + while (cin >> n && n >= 0) + cout << (n + 1) * n / 2 + 1 << "\n"; + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10079/10079-19.cpp b/uva_cpp_clean/10079/10079-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee466053a1d417b43b783c7e80bcc2a486cba370 --- /dev/null +++ b/uva_cpp_clean/10079/10079-19.cpp @@ -0,0 +1,10 @@ +#include +using namespace std; + +int main() { + long N; + while (scanf("%ld", &N) && N>=0) { + printf("%ld\n", 1+(((N+1)*N)/2)); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10079/10079-21.cpp b/uva_cpp_clean/10079/10079-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6149e98f241f11a9b055b7a4ea37964301c5d451 --- /dev/null +++ b/uva_cpp_clean/10079/10079-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10079 + Name: Pizza Cutting + Problem: https://onlinejudge.org/external/100/10079.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + long long int n; + while(cin>>n && n>=0) + if (n%2) + cout << n*((n+1)/2) + 1 << endl; + else + cout << (n/2)*(n+1) + 1 << endl; +} + diff --git a/uva_cpp_clean/10079/10079-5.cpp b/uva_cpp_clean/10079/10079-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a9542bc369adcb88fb5597764d1b51c654504878 --- /dev/null +++ b/uva_cpp_clean/10079/10079-5.cpp @@ -0,0 +1,17 @@ +#include + +int main() +{ + long int n,a; + while(scanf("%ld", &n)==1) + { + if( n>=0 && n<=210000000) + { + a= ((n*(n+1))/2)+1; + printf("%ld\n", a); + } + else if(n<0) + break; + } + return 0; +} diff --git a/uva_cpp_clean/10080/10080-18.cpp b/uva_cpp_clean/10080/10080-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d13634644e349ef8c3fee5702a23701193df6c4c --- /dev/null +++ b/uva_cpp_clean/10080/10080-18.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +double gx[150], gy[150], hx[150], hy[150]; +bool graph[150][150], visited[150]; +int match[150]; + +double dist(int i, int j){ + double x = gx[i] - hx[j]; + double y = gy[i] - hy[j]; + return sqrt(x*x + y*y); +} + +bool DFS(int node, int m){ + for(int i = 0; i < m; i++){ + if(graph[node][i] && !visited[i]){ + visited[i] = true; + if(match[i] < 0 || DFS(match[i], m)){ + match[i] = node; + return true; + } + } + } + return false; +} + +int fordFulkerson(int n, int m){ + int maxFlow = 0; + memset(match, -1, sizeof(match)); + for(int i = 0; i < n; i++){ + memset(visited, false, sizeof(visited)); + if(DFS(i, m)) maxFlow++; + } + return maxFlow; +} + +int main(){ + int n, m, s, v; + while(scanf("%d %d %d %d", &n, &m, &s, &v) == 4){ + for(int i = 0; i < n; i++) scanf("%lf %lf", &gx[i], &gy[i]); + for(int i = 0; i < m; i++) scanf("%lf %lf", &hx[i], &hy[i]); + for(int i = 0; i < n; i++){ + for(int j = 0; j < m; j++){ + if(dist(i, j) <= s*v) graph[i][j] = true; + else graph[i][j] = false; + } + } + printf("%d\n", n - fordFulkerson(n, m)); + } + return 0; +} +/* + Este es un problema de MBM o Maximum bipartite Matching, use el ford fulkerson para resolverlo, ya que + para los grafos bipartitos, el matching se reduce a un problema de maximo flujo. El ford fulkerson + posee varios tipos de modificaciones como: + -) los caminos de aumento no se guardan + -) en vez del grafo residual, se usa un vector que confirma el match + -) la comprobacion del camino de aumento se hace en cada nodo de uno de los grafos en vez de + siempre hacerlo desde el nodo source + -) se supone que cada nodo tiene una capacidad de 1, por eso se usa un vector que confirma el match + -) cuando se analiza si el nodo "u" puede hacer matching con el nodo "v", se tiene en cuenta si el + nodo "x" que ya hizo matching con "v" puede hacerlo con otro con tal de que "u" pueda hacer + matching con "v" + Siguiendo con el problema, arme el grafo con los Gopher que si podrian llegar a los hoyos (para ahorrar espacio), + luego, realize el ford fulkerson para obtener de los que si puderon salvarse, ya despues, solo es hacer complemento +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10080/10080-21.cpp b/uva_cpp_clean/10080/10080-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9264f6cba1d2bb4cc640f40df03b7e1c39912b68 --- /dev/null +++ b/uva_cpp_clean/10080/10080-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10080 + Name: Gopher II + Problem: https://onlinejudge.org/external/100/10080.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#define MAXN 200 +#define inf 1000000000 + +int max_flow(int n,int mat[][MAXN],int source,int sink,int flow[][MAXN]){ + int pre[MAXN],que[MAXN],d[MAXN],p,q,t,i,j; + if (source==sink) return inf; + for (i=0;i0) + flow[pre[i]-1][i]+=d[sink],i=pre[i]-1; + else + flow[i][-pre[i]-1]-=d[sink],i=-pre[i]-1; + } + for (j=i=0;i specials > bipartite > max cardinality bipartite matching + difficulty: medium + date: 17/Aug/2020 + problem: max number of preys that can hide safely from an predator attack + hint: consider a bipartite graph with edges that connect a gopher (prey) to an reachable hole + by: @brpapa +*/ +#include +using namespace std; + +vector> adj_list; int V, LV; + +vector match; // match[r] = vértice l já combinado com r +vector seen; + +/* O(E) - returns 1 if an augmenting path is found */ +int has_aug(int l) { + if (seen[l]) return 0; + seen[l] = true; + + for (int r : adj_list[l]) { + if (match[r] == -1 || has_aug(match[r])) { + match[r] = l; + return 1; + } + } + return 0; +} + +/* O(V*E) - return the max qty of matchings */ +int mcbm() { + int ans = 0; + match.assign(V, -1); + for (int l = 0; l < LV; l++) { + seen.assign(LV, false); + ans += has_aug(l); + } + return ans; +} + + +int main() { + int N, M, sec, vel; + while (cin >> N >> M >> sec >> vel) { + V = N+M; LV = N; adj_list.assign(V, vector()); + + // [0, N-1] = gophers + // [N, N+M-1] = holes + + vector> gophers(N); + for (int n = 0; n < N; n++) + cin >> gophers[n].first >> gophers[n].second; + + for (int m = 0; m < M; m++) { + pair hole; cin >> hole.first >> hole.second; + + for (int n = 0; n < N; n++) { + double dx = hole.first - gophers[n].first; + double dy = hole.second - gophers[n].second; + double dist = sqrt(dx*dx + dy*dy); + double max_dist = (double)sec*vel; + + if (dist <= max_dist) // gopher n reach hole N+m? + adj_list[n].push_back(N+m); + } + } + + cout << (N-mcbm()) << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10081/10081-21.cpp b/uva_cpp_clean/10081/10081-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63332debe8930ddf048d8c8a22aef5b8f095c542 --- /dev/null +++ b/uva_cpp_clean/10081/10081-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10081 + Name: Tight Words + Problem: https://onlinejudge.org/external/100/10081.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(5); + + double DP[11][101][11]; + for (int k=0; k<10; ++k) { + for (int j=0; j<=k; ++j) + DP[k][0][j] = 1; + + for (int i=1; i<101; ++i) + for (int j=0; j<=k; ++j) { + DP[k][i][j] = DP[k][i-1][j]; + if (j>0) DP[k][i][j] += DP[k][i-1][j-1]; + if (j> k >> n) { + double sum = 0; + for (int j=0; j<=k; ++j) + sum += DP[k][n-1][j]; + cout << 100*sum/(k+1) << '\n'; + } +} diff --git a/uva_cpp_clean/10082/10082-14.cpp b/uva_cpp_clean/10082/10082-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aac9d6f3bb3724db7e5fdf7b034046de5b7b0912 --- /dev/null +++ b/uva_cpp_clean/10082/10082-14.cpp @@ -0,0 +1,74 @@ +/*************************************************** + * Problem name : 10082 WERTYU.cpp + * Problem Link : https://uva.onlinejudge.org/external/100/10082.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-05 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +string mp = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./"; +char fun(char ch) { + for (int i = 0; i < (int)mp.size(); i++) { + if (mp[i] == ch) return mp[i - 1]; + } + return ch; +} +int main () { + __FastIO; + string str; + while (getline(cin, str)) { + for (int i = 0; i < (int)str.size(); i++) { + str[i] = fun(str[i]); + } + cout << str << "\n"; + } + return 0; +} + diff --git a/uva_cpp_clean/10082/10082-15.cpp b/uva_cpp_clean/10082/10082-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb739a61bb3604a91f6514b33d394ba0d2638288 --- /dev/null +++ b/uva_cpp_clean/10082/10082-15.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + char key[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./"; + char map[128]; + + for (int i = 0; key[i] != 0; ++i) + map[key[i]] = key[i - 1]; + + map[' '] = ' '; + map['\n'] = '\n'; + + int c; + + while ((c = cin.get()) != -1) + cout << map[c]; + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10082/10082-21.cpp b/uva_cpp_clean/10082/10082-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3f3719ed77975b680886d55b6f3f8390c51bb82 --- /dev/null +++ b/uva_cpp_clean/10082/10082-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10082 + Name: WERTYU + Problem: https://onlinejudge.org/external/100/10082.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char lines[] = "`1234567890-=\tqwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"; +char m[256]; +int main(){ + for(int i=1; lines[i]; i++) + m[lines[i]]=toupper(lines[i-1]); + + char ch; + while((ch=getchar())!=EOF) + if (m[ch]) cout< +int main() +{ + char c; + while ( scanf("%c",&c)==1) + { + if (c=='1') + printf("`"); + if (c=='2') + printf("1"); + if (c=='3') + printf("2"); + if (c=='4') + printf("3"); + if (c=='5') + printf("4"); + if (c=='6') + printf("5"); + if (c=='7') + printf("6"); + if (c=='8') + printf("7"); + if (c=='9') + printf("8"); + if (c=='0') + printf("9"); + if (c=='-') + printf("0"); + if (c=='=') + printf("-"); + if (c=='W') + printf("Q"); + if (c=='E') + printf("W"); + if (c=='R') + printf("E"); + if (c=='T') + printf("R"); + if (c=='Y') + printf("T"); + if (c=='U') + printf("Y"); + if (c=='I') + printf("U"); + if (c=='O') + printf("I"); + if (c=='P') + printf("O"); + if (c=='[') + printf("P"); + if (c==']') + printf("["); + if (c=='\\') + printf("]"); + if (c=='S') + printf("A"); + if (c=='D') + printf("S"); + if (c=='F') + printf("D"); + if (c=='G') + printf("F"); + if (c=='H') + printf("G"); + if (c=='J') + printf("H"); + if (c=='K') + printf("J"); + if (c=='L') + printf("K"); + if (c==';') + printf("L"); + if (c=='\'') + printf(";"); + if (c=='X') + printf("Z"); + if (c=='C') + printf("X"); + if (c=='V') + printf("C"); + if (c=='B') + printf("V"); + if (c=='N') + printf("B"); + if (c=='M') + printf("N"); + if (c==',') + printf("M"); + if (c=='.') + printf(","); + if (c=='/') + printf("."); + if (c==' ') + printf(" "); + if (c=='\n') + printf("\n"); + } + return 0; +} + diff --git a/uva_cpp_clean/10084/10084-19.cpp b/uva_cpp_clean/10084/10084-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e11ce434b5230d8120923beaa72521614477a69 --- /dev/null +++ b/uva_cpp_clean/10084/10084-19.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long double ld; +typedef complex point, vect; +typedef vector polygon; +typedef polygon::iterator polIter; +const ld inf = 1e9; const ld eps = 1e-9; const ld pi = acos(-1.0); + +ld darea(point a, point b, point c) { + return imag(conj(a)*b+conj(b)*c+conj(c)*a); +} + +ld area(polygon &p) { + ld result = 0.0; + for (int i = 0; i < p.size(); i++) + result += imag(conj(p[i])*p[(i+1)%p.size()]); + return abs(result) / 2.0; +} + +point intersectSeg(point p, point q, point a, point b) { + ld u = abs(imag(conj(a-b)*p-b*conj(a))); + ld v = abs(imag(conj(a-b)*q-b*conj(a))); + return (p*v + q*u) / (u+v); +} + +polygon cutPolygon(point a, point b, polygon &p) { + polygon q; + for (int i = 0; i < p.size(); i++) { + ld left1 = darea(a, b, p[i]); + ld left2 = darea(a, b, p[(i + 1)%p.size()]); + if (left1 > -eps) q.push_back(p[i]); + if (left1*left2 < -eps) + q.push_back(intersectSeg(p[i],p[(i+1)%p.size()],a,b)); + } + return q; +} + +int main() { + point a(0, 0), b; + polygon pol; + bool empty = false; + pol.push_back(point(0, 0)); + pol.push_back(point(10, 0)); + pol.push_back(point(10, 10)); + pol.push_back(point(0, 10)); + ld val = area(pol); + ld x, y; + string s; + while (cin >> x >> y >> s) { + if (!empty) { + b = point(x, y); + if (a == b) { + if (s == "Same") printf("%.2Lf\n", val); + else { + empty = true; + printf("0.00\n"); + } + } + else { + point c = (a + b) * point(0.5, 0); + point d(c.imag() - b.imag(), b.real() - c.real()); + d += c; + if (s == "Colder") pol = cutPolygon(c, d, pol); + else if (s == "Hotter") pol = cutPolygon(d, c, pol); + else empty = true; + val = area(pol); + if (val < 0.0001) empty = true; + if (empty) printf("0.00\n"); + else printf("%.2Lf\n", val); + a = b; + } + } else printf("0.00\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10086/10086-21.cpp b/uva_cpp_clean/10086/10086-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0dd05c838b835da9c27c7c84b8a283a83fd026b --- /dev/null +++ b/uva_cpp_clean/10086/10086-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10086 + Name: Test the Rods + Problem: https://onlinejudge.org/external/100/10086.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 268435456 +using namespace std; + +int M[43], MS[43], C1[43][23], C2[43][23], R[43], n, t1, t2, + DP[43][313], P[43][313]; + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> t1 >> t2 && (t1||t2)) { + cin >> n; + for (int i=0; i> M[i]; + for (int j=1; j<=M[i]; ++j) + cin >> C1[i][j]; + for (int j=1; j<=M[i]; ++j) + cin >> C2[i][j]; + } + + for (int i=1; i=0; --id) { + int ts = t1+t2-MS[id]; + for (int t1=0; t1<=ts; ++t1) { + int t2 = ts - t1; + + int best = INF, besj; + for (int j1=min(M[id], t1); j1>=0; --j1) { + int j2 = M[id] - j1; + if (j2 <= t2) { + int r = DP[id+1][t1-j1] + C1[id][j1] + C2[id][j2]; + if (r < best) { + best = r; + besj = j1; + } + } + } + + DP[id][t1] = best; + P[id][t1] = besj; + } + } + + cout << DP[0][t1] << "\n"; + for (int k=n-1, i=0; k>=0; --k, ++i) { + int j1 = P[i][t1]; + t1 -= j1; + cout << j1; + if (k) cout << ' '; + } + cout << "\n\n"; + } +} diff --git a/uva_cpp_clean/10086/10086-9.cpp b/uva_cpp_clean/10086/10086-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd2514bb712ddc064f11321636080968b08e512e --- /dev/null +++ b/uva_cpp_clean/10086/10086-9.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +const int MAX_N = 30 + 5, MAX_M = 20 + 5, INF = 1e7, MAX_T = 300 + 5; + +int t1, t2, n, m[MAX_N], choice[MAX_N][MAX_T], c1[MAX_N][MAX_M], c2[MAX_N][MAX_M], memo[MAX_N][MAX_T][MAX_T]; + +void printSolution (int id, int a) { + if (id == n) return; + if (id) cout << ' '; + cout << choice[id][a]; + printSolution(id + 1, a - choice[id][a]); +} + +int dp (int id, int a, int b) { + if (a < 0 or b < 0) return INF; + if (id == n) return (a == 0 and b == 0) ? 0 : INF; + if (~memo[id][a][b]) return memo[id][a][b]; + int ret = INF; + for (int k = 0; k <= m[id]; k++) { + int cost = c1[id][k] + c2[id][m[id] - k] + dp(id + 1, a - k, b - m[id] + k); + if (cost < ret) { + ret = cost; + choice[id][a] = k; + } + } + return memo[id][a][b] = ret; +} + +bool read () { + cin >> t1 >> t2; + if (t1 + t2 == 0) return false; + cin >> n; + for (int i = 0; i < n; i++) { + cin >> m[i]; + for (int j = 1; j <= m[i]; j++) cin >> c1[i][j]; + for (int j = 1; j <= m[i]; j++) cin >> c2[i][j]; + } + return true; +} + +int main () { + while (read()) { + memset(memo, -1, sizeof memo); + cout << dp(0, t1, t2) << endl; + printSolution(0, t1); + cout << endl << endl; + } + return (0); +} diff --git a/uva_cpp_clean/10088/10088-19.cpp b/uva_cpp_clean/10088/10088-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37de7a01eec075f4b5cecdc9b0639c388de937ef --- /dev/null +++ b/uva_cpp_clean/10088/10088-19.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long double ld; +typedef long long ll; +typedef complex point, vect; +typedef vector polygon; +typedef polygon::iterator polIter; +const ld inf = 1e9; const ld eps = 1e-9; const ld pi = acos(-1.0); + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +vect toVec(point a, point b) { + return vect(b.real() - a.real(), b.imag() - a.imag()); +} + +ll area(polygon &p) { + ll result = 0; + for (int i = 0; i < p.size(); i++) + result += imag(conj(p[i])*p[(i+1)%p.size()]); + return abs(result) / 2; +} + +int main() { + int N; + while (cin >> N && N != 0) { + polygon pol; + for (int i = 0; i < N; i++) { + ll x, y; + cin >> x >> y; + pol.push_back(point(x, y)); + } + ld a = area(pol); + ll b = N; + vect v = toVec(pol[N - 1], pol[0]); + b += abs(gcd(v.real(), v.imag())) - 1; + for (int i = 1; i < N; i++) { + v = toVec(pol[i], pol[i-1]); + b += abs(gcd(v.real(), v.imag())) - 1; + } + ll i = a + 1 - (b/2); + printf("%lld\n", i); + } + return 0; +} diff --git a/uva_cpp_clean/10090/10090-13.cpp b/uva_cpp_clean/10090/10090-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03b60b8c57d39a7b1569a1c25523b51ad4c7caae --- /dev/null +++ b/uva_cpp_clean/10090/10090-13.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; + +ll n, cx, cy, x, y, d; + +void extendedEuclid(ll a, ll b) +{ + if (b == 0) + { + x = 1; + y = 0; + d = a; + return; + } + + extendedEuclid(b, a % b); + + ll x1 = y; + ll y1 = x - (a / b) * y; + x = x1; + y = y1; +} + +int main() +{ + while (cin >> n && n != 0) + { + ll a, b, ans; + + cin >> cx >> a; + cin >> cy >> b; + + extendedEuclid(a, b); + + if (n % d != 0) + { + cout << "failed" << endl; + } + + else + { + long long nx, ny, ansx, ansy; + + x *= (n / d); + y *= (n / d); + + b /= d; + a /= d; + + nx = ceil(-(double) x / b); + ny = floor((double) y / a); + + if (nx > ny) cout << "failed" << endl; + + else + { + if (((x + (nx * b)) * cx) + ((y - (nx * a)) * cy) < ((x + (ny * b)) * cx) + ((y - (ny * a)) * cy)) + { + ansx = x + (nx * b); + ansy = y - (nx * a); + } + + else + { + ansx = x + (ny * b); + ansy = y - (ny * a); + } + + cout << ansx << " " << ansy << endl; + } + } + } +} diff --git a/uva_cpp_clean/10090/10090-21.cpp b/uva_cpp_clean/10090/10090-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81ecfdfe8f7c68d0087485e40bff3ef3ad9f4754 --- /dev/null +++ b/uva_cpp_clean/10090/10090-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10090 + Name: Marbles + Problem: https://onlinejudge.org/external/100/10090.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct Triple { + long long d, x, y; + Triple( long long q, long long w, long long e ) : d( q ), x( w ), y( e ) {} +}; + +Triple egcd( long long a, long long b ) { + if( !b ) return Triple( a, 1, 0 ); + Triple q = egcd( b, a % b ); + return Triple( q.d, q.y, q.x - a / b * q.y ); +} + +Triple ldioph( long long a, long long b, long long c ) { + Triple t = egcd( a, b ); + if( c % t.d ) return Triple( 0, 0, 0 ); + t.x *= c / t.d; + t.y *= c / t.d; + return t; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long n, a, b, c, d; + while (cin >> n && n>0) { + cin >> c >> a >> d >> b; + Triple t = ldioph(a, b, n); + if (!t.d) cout << "failed\n"; + else { + long long + lo = ceil(-double(t.x*t.d) / b), + x1 = t.x + lo * b / t.d, + y1 = t.y - lo * a / t.d, + + hi = floor(double(t.y*t.d) / a), + x2 = t.x + hi * b / t.d, + y2 = t.y - hi * a / t.d; + + if (lo > hi) + cout << "failed\n"; + else if (x1*c + y1*d < x2*c + y2*d) + cout << x1 << ' ' << y1 << '\n'; + else + cout << x2 << ' ' << y2 << '\n'; + } + } +} diff --git a/uva_cpp_clean/10090/10090-9.cpp b/uva_cpp_clean/10090/10090-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e046a0aab03e062269d82d4014a084b83a0a914 --- /dev/null +++ b/uva_cpp_clean/10090/10090-9.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +long long n, n1, n2, c1, c2, xo, yo, d, x, y, value, L, U, X, Y; + +void extendedEuclid(long long a, long long b){ + if( b == 0 ){ + xo = 1, yo = 0, d = a; + return; + } + extendedEuclid(b, a % b); + long long x1 = yo, y1 = xo - (a / b) * yo; + xo = x1, yo = y1; +} + +int main(){ + while(scanf("%lld", &n), n){ + scanf("%lld %lld", &c1, &n1); + scanf("%lld %lld", &c2, &n2); + extendedEuclid(n1, n2); + if( n % d != 0 ) puts("failed"); + else{ + x = xo * n / d, y = yo * n / d; + n1 /= d, n2 /= d; + L = ceil( -1.0 * x / n2 ), U = floor( 1.0 * y / n1 ); + if( L > U ) puts("failed"); + else{ + value = c1 * n2 - c2 * n1; + if( value * L < value * U ) + X = x + n2 * L, Y = y - n1 * L; + else + X = x + n2 * U, Y = y - n1 * U; + printf("%lld %lld\n", X, Y); + } + } + } + return(0); +} diff --git a/uva_cpp_clean/10092/10092-21.cpp b/uva_cpp_clean/10092/10092-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95802cbc647608aefce11edcfc3f75aac507c4cf --- /dev/null +++ b/uva_cpp_clean/10092/10092-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10092 + Name: The Problem with the Problem Setter + Problem: https://onlinejudge.org/external/100/10092.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +vector adj[23]; +int matchR[1024]; +bool matchL[23][1024], seen[1024]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + if (!matchR[v] || bpm(matchR[v])) { + if (matchR[v]) + matchL[matchR[v]][v] = 0; + matchL[matchR[v] = u][v] = 1; + return true; + } + } + return false; +} + + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int C[23], n, p; + while (cin >> n >> p && (n||p)) { + for (int i=1; i<=n; ++i) { + memset(matchL[i], 0, p); + adj[i].clear(); + cin >> C[i]; + } + + for (int i=0, sz,u; i> sz; + while (sz--) { + cin >> u; + adj[u].push_back(i); + } + } + + for (int i=1; i<=n; ++i) + for (int j=0; j +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + string base = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", n; + int dp[128] = {}, i = 1; + + for (const char& c : base) + dp[c] = ++i; + + dp['0'] = 2; + + while (cin >> n) { + int i; + for (i = dp[*max_element(n.begin(), n.end())]; i < 63; i++) { + long long temp = 0; + + for (const char& c : n) + if (c != '0' and isalnum(c)) + temp += dp[c] - 1; + + if (temp % (i - 1) == 0) + break; + } + + if (i == 63) + cout << "such number is impossible!\n"; + else + cout << i << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10093/10093-21.cpp b/uva_cpp_clean/10093/10093-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7db7d6ca1bc209b43c891a6e37d085ae5c0ddd5e --- /dev/null +++ b/uva_cpp_clean/10093/10093-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10093 + Name: An Easy Problem! + Problem: https://onlinejudge.org/external/100/10093.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int V[128]; +bool check(char s[], int base) { + int M = base - 1; + int R = 0; + for (int i=0; s[i]; ++i) + R = (R*base + V[s[i]]) % M; + + return !R; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i='0'; i<='9'; ++i) + V[i] = i-'0'; + for (int i='A'; i<='Z'; ++i) + V[i] = 10+i-'A'; + for (int i='a'; i<='z'; ++i) + V[i] = 36+i-'a'; + + + char s[100000]; + while (cin >> s) { + int mnbase = 1; + for (int i=0; s[i]; ++i) + mnbase = max(mnbase, V[s[i]]); + + for (int i=mnbase+1; i<63; ++i) + if (check(s, i)) { + cout << i << endl; + goto fin; + } + cout << "such number is impossible!\n"; + fin:; + } +} diff --git a/uva_cpp_clean/10094/10094-21.cpp b/uva_cpp_clean/10094/10094-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2176c4ac5f706397f8393a40ce17fdd65b43de3 --- /dev/null +++ b/uva_cpp_clean/10094/10094-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10094 + Name: Place the Guards + Problem: https://onlinejudge.org/external/100/10094.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int R[1024]; +void fillQueens(int n) { + if (n&1) { + R[n] = n; + fillQueens(n-1); + return; + } + + int hn = n>>1; + if (n%6 != 2) + for (int i=1; i<=hn; ++i) + R[i] = 2*i, + R[hn + i] = 2*i-1; + + else if (n%6 != 0) + for (int i=1; i<=hn; ++i) + R[i] = 1 + (2*i + hn - 3)%n, + R[n+1-i] = n - (2*i + hn - 3)%n; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while (cin >> n) { + if (n == 2 || n == 3) + cout << "Impossible\n"; + + else { + fillQueens(n); + cout << R[1]; + for (int i=2; i<=n; ++i) + cout << ' ' << R[i]; + cout << '\n'; + } + } +} diff --git a/uva_cpp_clean/10097/10097-19.cpp b/uva_cpp_clean/10097/10097-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48f1cd60af429955f1bc4afee5efb9a6c18f65fd --- /dev/null +++ b/uva_cpp_clean/10097/10097-19.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +using namespace std; + +int grid[102][102]; +unordered_set s; + +int main() { + int N, c = 1; + while (cin >> N && N != 0) { + s.clear(); + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + cin >> grid[i][j]; + grid[i][j]--; + } + } + int N1, N2, N3; + cin >> N1 >> N2 >> N3; + N1--, N2--, N3--; + if (N1 > N2) { + int auxi = N2; + N2 = N1; + N1 = auxi; + } + queue q; + q.push(N1*1000 + N2); + s.insert(N1*1000 + N2); + bool ok = false; + printf("Game #%d\n", c); + c++; + while (!q.empty() && !ok) { + int i1 = (q.front()/1000)%1000, i2 = q.front()%1000, d = q.front()/1000000; + q.pop(); + if (i1 == N3 || i2 == N3) { + printf("Minimum Number of Moves = %d\n", d); + ok = true; + break; + } + int n1 = grid[i1][i2], n2 = grid[i2][i1]; + if (n1 != -1) { + int r = i2; + if (n1 > r) { + int auxi = r; + r = n1; + n1 = auxi; + } + int val = n1*1000 + r; + if (!s.count(val)) s.insert(val), val += (d+1)*1000000, q.push(val); + } + if (n2 != -1) { + int r = i1; + if (n2 < r) { + int auxi = r; + r = n2; + n2 = auxi; + } + int val = r*1000 + n2; + if (!s.count(val)) s.insert(val), val += (d+1)*1000000, q.push(val); + } + } + if (!ok) printf("Destination is Not Reachable !\n"); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10098/10098-18.cpp b/uva_cpp_clean/10098/10098-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4a65e9bf350aa10db7f0fccfae0cdf607b2a654a --- /dev/null +++ b/uva_cpp_clean/10098/10098-18.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +using namespace std; + +int main(){ + int n; + char input[20]; + scanf("%d", &n); + cin.get(); + while(n--){ + scanf("%s", &input); + sort(input, input + strlen(input)); + do{ + printf("%s\n", input); + } while(next_permutation(input, input + strlen(input))); + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10098/10098-19.cpp b/uva_cpp_clean/10098/10098-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f68e9a22dbdb85fb2dcad0b0ea50774afe7489ff --- /dev/null +++ b/uva_cpp_clean/10098/10098-19.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +using namespace std; + +char s[12]; +char per[12]; +bool mask[12]; +int len; + +void proc(int i) { + if (i == len) { + for (int j = 0; j < len; j++) { + printf("%c", per[j]); + } + printf("\n"); + } + int last = -1; + for (int j = 0; j < len; j++) { + if (last >= 0 && s[j] == s[last]) continue; + if (!mask[j]) { + mask[j] = true; + per[i] = s[j]; + proc(i+1); + mask[j] = false; + last = j; + } + } +} + +int main() { + int n; + cin >> n; + while (n--) { + scanf("%s", s); + len = strlen(s); + sort(s, s+len); + proc(0); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10098/10098-21.cpp b/uva_cpp_clean/10098/10098-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eff168d0041d6041c46c282e4e604889bd6fb6da --- /dev/null +++ b/uva_cpp_clean/10098/10098-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10098 + Name: Generating Fast + Problem: https://onlinejudge.org/external/100/10098.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +char str[15]; +bool seen[15]; +char perm[15]; +void prec(int ind) { + perm[ind] = 0; + if (str[ind] == 0) { + cout << perm << endl; + return; + } + + for (int i=0; str[i]; i++) + if (!seen[i] && str[i] != perm[ind]) { + seen[i] = true; + perm[ind] = str[i]; + prec(ind + 1); + seen[i] = false; + } +} + +int main() { + int n; + cin >> n; + while (getchar() != '\n'); + while (n--) { + memset(seen, 0, sizeof(seen)); + cin >> str; + int l = strlen(str); + sort(str, str + l); + prec(0); + cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10098/10098-5.cpp b/uva_cpp_clean/10098/10098-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..298dc9c74d78b2e7494c34ee12ace0c6b0ef0e17 --- /dev/null +++ b/uva_cpp_clean/10098/10098-5.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +using namespace std; +int main() +{ + char a[20]; + int n,t,k; + scanf ("%d", &t); + while (t--) + { + scanf("%s",a); + n=strlen(a); + if(n>10) + break; + sort(a,a+n); + puts(a); + while(next_permutation (a,a+n)) + puts(a); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10098/10098-9.cpp b/uva_cpp_clean/10098/10098-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8db120765ae8836cac337f0f2471abd8635851cb --- /dev/null +++ b/uva_cpp_clean/10098/10098-9.cpp @@ -0,0 +1,22 @@ +#include + +#define SIZE 20 + +using namespace std; + +int tc, len; +char s[SIZE]; + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + scanf("%s", s); + len = strlen(s); + sort(s, s + len); + do{ + printf("%s\n", s); + }while(next_permutation(s, s + len)); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10099/10099-10.cpp b/uva_cpp_clean/10099/10099-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14942394af30b2c1a72e2b56ea1c646f884eb92e --- /dev/null +++ b/uva_cpp_clean/10099/10099-10.cpp @@ -0,0 +1,136 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e2+5; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + int count(int u) { + return sz[find(u)]; + } +}; + +vector> adj[N]; +bool vis[N]; +// {weight, a, b} +int minimumSpanningTree(vector> &edges) { + + + sort(edges.rbegin(), edges.rend()); + DSU dsu; + dsu.init(N); + int ans = 1e9,a,b,weight; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if (dsu.find(a) != dsu.find(b)) { + + dsu.merge(a, b); + adj[a].push_back({b,weight}); + adj[b].push_back({a,weight}); + + ans = min(ans,weight); + } + } + return ans; +} + +int dfs(int p,int goal,int sum) { + + if(p==goal) return sum; + + vis[p]=1; + int ans=1e9; + for(auto ch:adj[p]) { + if(!vis[ch.first]) { + ans=min(ans,dfs(ch.first,goal,min(sum,ch.second))); + } + } + + return ans; +} + +int main() { + FAST + + int n,m,a,b,p; + int c=1; + while (cin>>n>>m) { + + if(n==0&& m==0) break; + + vector> arr; + + memset(adj,0,sizeof adj); + memset(vis,0,sizeof vis); + + for (int i = 0; i > a >> b >> p; + arr.push_back({p, a, b}); + } + + cin>>a>>b>>p; + + + minimumSpanningTree(arr); + + int res = dfs(a,b,(int)1e9); + res--; + + res = ceil(p/(res*1.0)); + + cout<<"Scenario #"< +#include + +#define MAX 105 +#define Min(a, b) ((ab)?(a):(b)) + +int wght[MAX][MAX]; + +int main() +{ + int N, R, nCase = 1; + while( scanf("%d %d", &N, &R)==2 && (N || R) ) + { + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10099 + Name: The Tourist Guide + Problem: https://onlinejudge.org/external/100/10099.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define INF 2147483647 +#define MAXN 110 + + +int dids[MAXN]; +int find(int v) { + if (dids[v] != v) + return dids[v]=find(dids[v]); + return v; +} + +void join(int v, int w) { + dids[find(v)] = find(w); +} + + + +struct Edge { + int x, y, w; + bool operator < (const Edge& e) const { + return w > e.w; + } +}; + +Edge es[MAXN*MAXN]; + +int mp[MAXN][MAXN], + adj[MAXN][MAXN], + deg[MAXN], n, d; + +int seen[MAXN], best; +bool findB(int v) { + if (v==d) + return true; + + if (!seen[v]) { + seen[v] = true; + for (int i=0; i>n>>r && (n||r)) { + for (int i=0; i> e.x >> e.y >> e.w; + e.x--; e.y--; + } + cin>>s>>d>>t; + s--; d--; + + int cnt = 0; + sort(es, es+r); + for (int i=0; i minimum spanning tree (MST) > minimax path + difficulty: medium + date: 08/Sep/2020 + problem: maximin path; find the minimum cost of the maximum path from s to t + hint: apply kruskal to get the maximum spanning tree, but stop it when s and t connect + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +int V; +vector> edge_list; // {{w, u, v}, ...}, arestas u -w-> v + +class ufds { + private: + vector parent; // parent[n] = pai do elemento n + vector size; // size[n] = tamanho do conjunto identificado por n + int qty_disjoint_sets; + + public: + ufds() {} + ufds(int N) { + parent.resize(N); + size.assign(N, 1); + qty_disjoint_sets = N; + + // inicialmente, há N conjuntos disjuntos + for (int n = 0; n < N; n++) parent[n] = n; + } + + /* O(1) - retorna a raiz do conjunto de n */ + int find_set(int n) { + if (parent[n] == n) return n; + return parent[n] = find_set(parent[n]); // path compression + } + + /* O(1) - os conjuntos de n e de m são os mesmos? */ + bool is_same_set(int n, int m) { + return find_set(n) == find_set(m); + } + + /* O(1) - conecta os conjuntos de n e de m */ + void union_sets(int n, int m) { + int n_id = find_set(n); + int m_id = find_set(m); + + // union by size: conecta a menor árvore à maior árvore + if (!is_same_set(n_id, m_id)) { + if (size[n_id] > size[m_id]) + swap(n_id, m_id); + + parent[n_id] = m_id; + size[m_id] += size[n_id]; + + qty_disjoint_sets--; + } + } + + /* O(1) - tamanho do conjunto de n */ + int set_size(int n) { + return size[find_set(n)]; + } + + /* O(1) - quantidade de conjuntos disjuntos */ + int count() { + return qty_disjoint_sets; + } +}; + +/* O(E*log(V)) - retorna o peso da aresta mais leve na "MST" que conecta s e t */ +ll kruskal(int s, int t) { + ufds sets(V); + sort(edge_list.rbegin(), edge_list.rend()); // prioriza arestas com maior w + + ll min_w; + for (auto edge: edge_list) { + int u, v; ll w; tie(w,u,v) = edge; + + // evita ciclos + if (!sets.is_same_set(u, v)) { + sets.union_sets(u, v); + if (sets.is_same_set(s, t)) { + min_w = w; + break; + } + } + } + return min_w; +} + +int main() { + int E, test = 1; + while (cin >> V >> E && (V || E)) { + edge_list.clear(); + while (E--) { + int u,v; ll w; cin >> u >> v >> w; u--; v--; w--; + edge_list.push_back({w,u,v}); + } + + int s,t; ll people; cin >> s >> t >> people; s--; t--; + + ll min_w = kruskal(s,t); + ll ans = ceil((double)people/min_w); + + cout << "Scenario #" << (test++) << endl; + cout << "Minimum Number of Trips = " << ans << endl << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10099/10099-9.cpp b/uva_cpp_clean/10099/10099-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..176a6073ed01752becbfa240f94cfff5b8ddf244 --- /dev/null +++ b/uva_cpp_clean/10099/10099-9.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int LIM = 110, SIZE = 10000; + +typedef pair ii; + +struct Edge_{ + int u, v, w; + bool operator < (const Edge_& other) const{ + return ( w > other.w ); + } +}E[SIZE]; + +int tc, N, M, S, D, T, edge, ans; +vector < ii > AdjList[LIM]; +bool vis[LIM]; + +void dfs(int u, int v, int w){ + vis[u] = true; + if( u == v ){ + edge = w; + return; + } + for(int it = 0; it < AdjList[u].size(); it++) + if( not vis[AdjList[u][it].first] ) + dfs(AdjList[u][it].first, v, min(w, AdjList[u][it].second) ); +} + +void Kruskal(){ + UnionFind UF(N + 1); + int setSize = 1; + sort(E, E + M); + for(int it = 0; it < M and setSize != N; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ){ + AdjList[E[it].u].push_back( ii(E[it].v, E[it].w) ); + AdjList[E[it].v].push_back( ii(E[it].u, E[it].w) ); + setSize++, UF.unionSet(E[it].u, E[it].v); + } +} + +void clear_(){ + for(int it = 1; it <= N; it++) AdjList[it].clear(); + fill(vis, vis + N + 1, false); +} + +int main(){ + while(scanf("%d %d", &N, &M), N | M){ + for(int it = 0; it < M; it++) + scanf("%d %d %d", &E[it].u, &E[it].v, &E[it].w); + Kruskal(); + scanf("%d %d %d", &S, &D, &T); + dfs(S, D, INT_MAX); + printf("Scenario #%d\n", ++tc); + printf("Minimum Number of Trips = %d\n\n", ( T + edge - 2 ) / ( edge - 1 ) ); + clear_(); + } + return(0); +} diff --git a/uva_cpp_clean/101/101-17.cpp b/uva_cpp_clean/101/101-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66ff334515d3390fd0cd4cde82ce1784f4a801c9 --- /dev/null +++ b/uva_cpp_clean/101/101-17.cpp @@ -0,0 +1,52 @@ +/** + * AOAPC II Example 5-2 The Blocks Problem + */ +#include +using namespace std; +vector> piles; +void find_block(int a, int &blo, int &pos) { + for (int b = 0; b < piles.size(); ++b) { + auto p = find(piles[b].begin(), piles[b].end(), a); + if (p != piles[b].end()) { + blo = b; + pos = p - piles[b].begin(); + return; + } + } +} +void clear_above(int blo, int pos) { + for_each(piles[blo].begin() + pos + 1, piles[blo].end(), [](int i){ + piles[i].push_back(i); + }); + piles[blo].resize(pos + 1); +} +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n, a, b, i = 0; + cin >> n; + piles = vector>(n); + for (auto &p : piles) + p.push_back(i++); + string cmd1, cmd2; + while (cin >> cmd1 >> a >> cmd2 >> b) { + int bloA, bloB, posA, posB; + find_block(a, bloA, posA); + find_block(b, bloB, posB); + if (bloA == bloB) + continue; + if (cmd1 == "move") + clear_above(bloA, posA); + if (cmd2 == "onto") + clear_above(bloB, posB); + copy(piles[bloA].begin() + posA, piles[bloA].end(), back_inserter(piles[bloB])); + piles[bloA].resize(posA); + } + i = 0; + for (auto &p : piles) { + cout << i++ << ":"; + for (int j : p) + cout << " " << j; + cout << "\n"; + } +} diff --git a/uva_cpp_clean/101/101-18.cpp b/uva_cpp_clean/101/101-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c3b8d4d59db257d08cc0c3dace0bede32147e522 --- /dev/null +++ b/uva_cpp_clean/101/101-18.cpp @@ -0,0 +1,120 @@ +#include +#include +#include + +using namespace std; + +int blocks[30][30], n, ai, aj, bi, bj; + +void set(){ + ai = aj = bi = bj = -1; + for(int i = 0; i < n; i++){ + blocks[i][0] = i; + blocks[i][1] = -1; + } +} + +bool check(int a, int b){ + if(a == b) return false; + for(int i = 0; i < n; i++){ + for(int j = 0; blocks[i][j] != -1; j++){ + if(a == blocks[i][j]){ + for(int k = 0; blocks[i][k] != -1; k++){ + if(b == blocks[i][k]) return false; + } + } + } + } + return true; +} + +void find(int a, int b){ + for(int i = 0; i < n; i++){ + for(int j = 0; blocks[i][j] != -1; j++){ + if(a == blocks[i][j]){ + ai = i; + aj = j; + }else if(b == blocks[i][j]){ + bi = i; + bj = j; + } + } + } +} + +void moveOnto(int a, int b){ + if(check(a, b)){ + find(a, b); + for(int j = aj + 1; blocks[ai][j] != -1; j++){ + blocks[blocks[ai][j]][0] = blocks[ai][j]; + blocks[blocks[ai][j]][1] = -1; + } + for(int j = bj + 1; blocks[bi][j] != -1; j++){ + blocks[blocks[bi][j]][0] = blocks[bi][j]; + blocks[blocks[bi][j]][1] = -1; + } + blocks[ai][aj] = -1; + blocks[bi][bj + 1] = a; + blocks[bi][bj + 2] = -1; + } +} + +void moveOver(int a, int b){ + if(check(a, b)){ + find(a, b); + for(int j = aj + 1; blocks[ai][j] != -1; j++){ + blocks[blocks[ai][j]][0] = blocks[ai][j]; + blocks[blocks[ai][j]][1] = -1; + } + while(blocks[bi][bj] != -1) bj++; + blocks[ai][aj] = -1; + blocks[bi][bj] = a; + blocks[bi][bj + 1] = -1; + } +} + +void pileOnto(int a, int b){ + if(check(a, b)){ + find(a, b); + for(int j = bj + 1; blocks[bi][j] != -1; j++){ + blocks[blocks[bi][j]][0] = blocks[bi][j]; + blocks[blocks[bi][j]][1] = -1; + } + for(int j = aj; blocks[ai][j] != -1; j++) blocks[bi][++bj] = blocks[ai][j]; + blocks[ai][aj] = -1; + blocks[bi][bj + 1] = -1; + } +} + +void pileOver(int a, int b){ + if(check(a, b)){ + find(a, b); + while(blocks[bi][bj] != -1) bj++; + for(int j = aj; blocks[ai][j] != -1; j++) blocks[bi][bj++] = blocks[ai][j]; + blocks[ai][aj] = -1; + blocks[bi][bj] = -1; + } +} + +int main(){ + int a, b; + cin >> n; + string move, dir; + set(); + while(cin >> move && move[0] != 'q'){ + cin >> a >> dir >> b; + if(move == "move"){ + if(dir == "onto") moveOnto(a, b); + else moveOver(a, b); + }else{ + if(dir == "onto") pileOnto(a, b); + else pileOver(a, b); + } + } + for(int i = 0; i < n; i++){ + printf("%d:", i); + for(int j = 0; blocks[i][j] != -1; j++) printf(" %d", blocks[i][j]); + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/101/101-9.cpp b/uva_cpp_clean/101/101-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a823787965ac03d1a76cd6a3534be1b3f7830cee --- /dev/null +++ b/uva_cpp_clean/101/101-9.cpp @@ -0,0 +1,83 @@ +#include + +#define SIZE 30 + +using namespace std; + +int n, from, to, k, pos_from, pos_to; +char word1[SIZE], word2[SIZE], s[SIZE]; +vector < vector > v(SIZE); + +int find(int num){ + for(int i = 0; i < n; i++) + for(int j = 0; j < v[i].size(); j++) + if(v[i][j] == num) return i; +} + +void returning(int pos, int num){ + for(int i = v[pos].size() - 1; i >= 0; i--) + if(v[pos].back() == num) break; + else v[v[pos].back()].push_back(v[pos].back()), v[pos].pop_back(); +} + +void moves(int source, int num, int target){ + vector aux; + for(int i = v[source].size() - 1; i >= 0; i--) + if(v[source].back() == num){ + aux.push_back(v[source].back()); + v[source].pop_back(); + break; + } + else aux.push_back(v[source].back()), v[source].pop_back(); + reverse(aux.begin(), aux.end()); + v[target].insert(v[target].end(), aux.begin(), aux.end()); +} + +int main(){ + scanf("%d\n",&n); + for(int i = 0;i < n; i++) v[i].push_back(i); + while(true){ + cin.getline(s, SIZE); + k = sscanf(s,"%s %d %s %d", word1, &from, word2, &to); + if(k == 1) break; + if(strcmp(word1, "move") == 0){ + if(strcmp(word2, "onto") == 0){ + pos_from = find(from); + pos_to = find(to); + if(pos_from == pos_to) continue; + returning(pos_from, from); + returning(pos_to, to); + v[pos_to].push_back(v[pos_from].back()); + v[pos_from].pop_back(); + } + else{ + pos_from = find(from); + pos_to = find(to); + if(pos_from == pos_to) continue; + returning(pos_from,from); + v[pos_to].push_back(v[pos_from].back()); + v[pos_from].pop_back(); + } + } + else{ + if(strcmp(word2, "onto") == 0){ + pos_from = find(from); + pos_to = find(to); + returning(pos_to, to); + moves(pos_from, from, pos_to); + } + else{ + pos_from = find(from); + pos_to = find(to); + moves(pos_from, from, pos_to); + } + } + } + for(int i = 0; i < n; i++){ + printf("%d:", i); + for(int j = 0; j < v[i].size(); j++) + printf(" %d", v[i][j]); + puts(""); + } + return(0); +} diff --git a/uva_cpp_clean/10100/10100-21.cpp b/uva_cpp_clean/10100/10100-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41936a2c71662db2a72956f97cc06a853a9de04f --- /dev/null +++ b/uva_cpp_clean/10100/10100-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10100 + Name: Longest Match + Problem: https://onlinejudge.org/external/101/10100.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +void tokenize(const string& s, vector &v) { + char ss[1043]; + for (int i=0, j; s[i]; ++i) + if (isalnum(s[i])) { + for (j=i; isalnum(s[j]); ++j) + ss[j-i] = s[j]; + ss[j-i] = 0; + v.push_back(ss); + i = j-1; + } +} + +int DP[1043][1043]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string l1, l2; + for (int cse=1; getline(cin, l1) && getline(cin, l2); ++cse) { + vector v1, v2; + tokenize(l1, v1); + tokenize(l2, v2); + + cout << setw(2) << cse; + if (v1.empty() || v2.empty()) + cout << ". Blank!\n"; + else { + for (int i=1; i<=v1.size(); ++i) + for (int j=1; j<=v2.size(); ++j) + if (v1[i-1]==v2[j-1]) + DP[i][j] = DP[i-1][j-1] + 1; + else + DP[i][j] = max(DP[i-1][j], DP[i][j-1]); + cout << ". Length of longest match: " << DP[v1.size()][v2.size()] << endl; + } + } +} diff --git a/uva_cpp_clean/10100/10100-9.cpp b/uva_cpp_clean/10100/10100-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09d73f9b896c3a2b86dc64898dc07e82522dfc81 --- /dev/null +++ b/uva_cpp_clean/10100/10100-9.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +const int MAX_LEN = 1e3 + 10; + +vector s1, s2; + +int dp[MAX_LEN][MAX_LEN]; + +int lcs () { + int n = s1.size(), m = s2.size(); + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (s1[i - 1] == s2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; + else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[n][m]; +} + +bool read (vector & s) { + s.clear(); + bool endOfFile = true; + string line, token = ""; + if (getline(cin, line)) endOfFile = false; + for (const char ch: line) { + if (isalnum(ch)) token += ch; + else { + if (not token.empty()) s.push_back(token); + token = ""; + } + } + if (not token.empty()) s.push_back(token); + return not endOfFile; +} + +int main () { + int tc = 0; + while (read(s1) and read(s2)) { + if (s1.empty() or s2.empty()) printf("%2d. Blank!\n", ++tc); + else printf("%2d. Length of longest match: %d\n", ++tc, lcs()); + } + return (0); +} diff --git a/uva_cpp_clean/10101/10101-15.cpp b/uva_cpp_clean/10101/10101-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a6096d9e87e71834f73e97232047f4a20d4326b --- /dev/null +++ b/uva_cpp_clean/10101/10101-15.cpp @@ -0,0 +1,48 @@ +/* + 進位制 +*/ +#include +#include + +using namespace std; + +void cut(long long x) { + if (x >= 10000000) { + if (x / 10000000 >= 100) + cut(x / 10000000); + else + cout << " " << x / 10000000; + cout << " kuti"; + x %= 10000000; + } + if (x >= 100000) { + cout << " " << x / 100000 << " lakh"; + x %= 100000; + } + if (x >= 1000) { + cout << " " << x / 1000 << " hajar"; + x %= 1000; + } + if (x >= 100) { + cout << " " << x / 100 << " shata"; + x %= 100; + } + if (x) + cout << " " << x; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + long long n; + int number = 0; + while (cin >> n) { + cout << setw(4) << ++number << "."; + if (n) + cut(n); + else + cout << " 0"; + cout << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10101/10101-20.cpp b/uva_cpp_clean/10101/10101-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63d6d3a0fbd08946a05370facec6f1d8695359e7 --- /dev/null +++ b/uva_cpp_clean/10101/10101-20.cpp @@ -0,0 +1,34 @@ +#include + +const char unit[10][8] = {"", " shata", " hajar", " lakh", " kuti", " shata", " hajar", " lakh", " kuti"}; + +int main() +{ + long long n, nCase = 1; + + while( scanf("%lld", &n)!=EOF ) + { + printf("%4lld.", nCase); + if( n==0 ) printf(" %lld\n", n); + else + { + long long carry, temp = n, div = 100000000000000; + for(int i=8 ; i>=0 ; i--) + { + carry = temp/div; + if( carry>0 ) + { + printf(" %lld%s", carry, unit[i]); + temp -= carry*div; + } + else if( i==4 && carry==0 && n>10000000 ) printf("%s", unit[i]); + if( i==6 || i==2 ) div /= 10; + else div /= 100; + if( div==0 ) break; + } + printf("\n"); + } + ++nCase; + } + return 0; +} diff --git a/uva_cpp_clean/10102/10102-11.cpp b/uva_cpp_clean/10102/10102-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cf6b19bb19ec4880f4a1fd2d1612be1b5fe07af --- /dev/null +++ b/uva_cpp_clean/10102/10102-11.cpp @@ -0,0 +1,81 @@ +#include +/* +Problem: 10102 - The path in the colored field +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1043 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int M; +char ch; + +int main () { + + while ( scanf("%d\n", &M) != EOF ){ + int grid[M][M]; + int threes[M*M], idx = 0; + int steps = INT_MAX, min_steps = INT_MIN; + + for (int i = 0; i < M; i++){ + for (int j = 0; j < M; j++){ + scanf("%c", &ch); + grid[i][j] = ch-'0'; + + if (grid[i][j] == 3){ + threes[idx++] = i; + threes[idx++] = j; + } + } + + scanf("%c", &ch); + } + + for (int i = 0; i < M; i++){ + for (int j = 0; j < M; j++){ + if (grid[i][j] != 1) continue; + + steps = INT_MAX; + for (int k = 0; k < idx-1; k+=2){ + int distance = abs(i-threes[k]) + abs(j-threes[k+1]); + steps = min(steps, distance); + } + + min_steps = max(steps, min_steps); + } + } + + printf("%d\n", min_steps); + } + + return 0; +} +/* +Sample input:- +----------------- +4 +1223 +2123 +2213 +3212 +2 +12 +33 + +Sample output:- +----------------- +3 +1 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10102/10102-13.cpp b/uva_cpp_clean/10102/10102-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..294bec4eac9f68520c878ed57c13ee6e99a26391 --- /dev/null +++ b/uva_cpp_clean/10102/10102-13.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int m, ans = 0; + + while (cin >> m) + { + ans = 0; + + vector < string > square; + vector < pair < int, int > > ones; + vector < pair < int, int > > threes; + + for (int i = 0; i < m; i++) + { + string temp; + + cin >> temp; + + square.push_back(temp); + } + + for (int i = 0; i < m; i++) + { + for (int j = 0; j < m; j++) + { + if (square[i][j] == '1') + { + ones.push_back(make_pair(i, j)); + } + + if (square[i][j] == '3') + { + threes.push_back(make_pair(i, j)); + } + } + } + + for (int i = 0; i < (int)ones.size(); i++) + { + int each = 2147483647; + int x1 = ones[i].first; + int y1 = ones[i].second; + + for (int j = 0; j < (int)threes.size(); j++) + { + int x2 = threes[j].first; + int y2 = threes[j].second; + + int temp = abs(x1 - x2) + abs(y1 - y2); + + each = min(temp, each); + } + + ans = max(each, ans); + } + + cout << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10102/10102-21.cpp b/uva_cpp_clean/10102/10102-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f50377bdd6045497f7df44f9ce48b13e057e013 --- /dev/null +++ b/uva_cpp_clean/10102/10102-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10102 + Name: The path in the colored field + Problem: https://onlinejudge.org/external/101/10102.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 268435456 +using namespace std; + +struct State { + int i, j; + State(int i, int j):i(i),j(j) {}; +}; + +char M[143][143]; +int seen[143][143], n, + di[] = {0,0,1,-1}, + dj[] = {1,-1,0,0}; + +int bfs(int si, int sj, int cse) { + seen[si][sj] = cse; + queue q; + q.push(State(si, sj)); + while (!q.empty()) { + State s = q.front(); q.pop(); + if (M[s.i][s.j] == '3') + return abs(s.i-si) + abs(s.j-sj); + + for (int k=0; k<4; ++k) { + State t(s.i+di[k], s.j+dj[k]); + if (t.i>=0 && t.i=0 && t.j> n) { + for (int i=0; i> M[i]; + + int mx = 0; + for (int i=0; i +#include +#include +#include + +using namespace std; + +string inputL; +int m, tmp,mx, answer; +vector> ones, threes; + +int main () +{ + while ( scanf("%d",&m) != EOF ) + { + ones.clear(); + threes.clear(); + + for ( int l = 0; l < m; l++ ) + { + getline( cin>>ws, inputL); + for ( int x = 0; x < m; x++ ) + { + if ( inputL[x] == '1' ) + ones.push_back( make_pair(x,l) ); + else if ( inputL[x] == '3' ) + threes.push_back( make_pair(x,l) ); + } + } + + answer = -1; + for ( int l = 0; l < ones.size(); l++ ) + { + mx = -1; + for ( int x = 0; x < threes.size(); x++ ) + { + tmp = abs(ones[l].first - threes[x].first) + + abs(ones[l].second - threes[x].second); + if ( mx == -1 || tmp < mx ) + mx = tmp; + + } + if ( mx > answer ) + answer = mx; + } + + cout << answer << endl; + } + + +} diff --git a/uva_cpp_clean/10102/10102-9.cpp b/uva_cpp_clean/10102/10102-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10bf8aeec400bfb1826ffb20a60e0971665015a9 --- /dev/null +++ b/uva_cpp_clean/10102/10102-9.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int n, m[1000][1000], ans, tmp; +vector < pair > v, w; + +int main(){ + while(scanf("%d", &n) == 1){ + ans = 0, v.clear(), w.clear(); + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++){ + scanf("%1d", &m[i][j]); + if(m[i][j] == 1) v.push_back(make_pair(i, j)); + else if(m[i][j] == 3) w.push_back(make_pair(i, j)); + } + for(int i = 0; i < v.size(); i++){ + tmp = INT_MAX; + for(int j = 0; j < w.size(); j++) + tmp = min(tmp, abs(w[j].first - v[i].first) + abs(w[j].second - v[i].second)); + ans = max(ans, tmp); + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10104/10104-13.cpp b/uva_cpp_clean/10104/10104-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de8f4d6926d6e4d7a68b18fc9b847ca6de91c5f1 --- /dev/null +++ b/uva_cpp_clean/10104/10104-13.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; + +ll x, y, d; + +void extendedEuclid(ll a, ll b) +{ + if (b == 0) + { + x = 1; + y = 0; + d = a; + return; + } + + extendedEuclid(b, a % b); + + ll x1 = y; + ll y1 = x - (a / b) * y; + x = x1; + y = y1; +} + +int main() +{ + ll a, b; + + while (cin >> a >> b) + { + extendedEuclid(a, b); + + cout << x << " " << y << " " << d << endl; + } +} diff --git a/uva_cpp_clean/10104/10104-14.cpp b/uva_cpp_clean/10104/10104-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a672d117f37761ac8aa0ef50040f060b3e2a4a1e --- /dev/null +++ b/uva_cpp_clean/10104/10104-14.cpp @@ -0,0 +1,70 @@ +/*************************************************** + * Problem name : 10104 - Euclid Problem.cpp + * Problem Link : https://uva.onlinejudge.org/external/101/10104.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-11-23 + * Problem Type : Number Theory + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +/************************************ Code Start Here ******************************************************/ +int ext_GCD( ll A, ll B, ll *X, ll *Y) { + ll x2, y2, x1, y1, x, y, r2, r1, q, r; + x2 = 1; y2 = 0; + x1 = 0; y1 = 1; + for (r2 = A, r1 = B; r1 != 0; r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y ) { + q = r2 / r1; + r = r2 % r1; + x = x2 - (q * x1); + y = y2 - (q * y1); + } + *X = x2; *Y = y2; + return r2; +} +int main () { + ll a, b; + while (scanf("%lld %lld", &a, &b) == 2) { + ll X, Y; + ext_GCD(a, b, &X, &Y); + printf("%lld %lld %lld\n", X , Y, __gcd(a, b)); + } + return 0; +} diff --git a/uva_cpp_clean/10104/10104-19.cpp b/uva_cpp_clean/10104/10104-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdcb6f9da58473e37764a0f0f26878413b3108d1 --- /dev/null +++ b/uva_cpp_clean/10104/10104-19.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +long long euclides(long long a, long long b, long long &x, long long &y){ + long long xx=y=0, yy=x=1; + while(b) { + long long q=a/b, t=b; + b=a%b; a=t; t=xx; xx=x-q*xx; + x=t; t=yy; yy=y-q*yy; y=t; + } + return a; +} + +int main() { + long long a, b, x, y, d; + while (cin >> a >> b) { + d = euclides(a, b, x, y); + printf("%lld %lld %lld\n", x, y, d); + } + return 0; +} diff --git a/uva_cpp_clean/10104/10104-20.cpp b/uva_cpp_clean/10104/10104-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d07884b6542013429dd1f60567b16df45fefe28 --- /dev/null +++ b/uva_cpp_clean/10104/10104-20.cpp @@ -0,0 +1,34 @@ +#include + +long X, Y, D; + +void Extended_Euclid(long a, long b) +{ + long temp; + + if( b==0 ) + { + D = a; + X = 1; + Y = 0; + return; + } + + Extended_Euclid(b, a%b); + + temp = X-(a/b) * Y; + X = Y; + Y = temp; +} + +int main() +{ + long A, B; + + while( scanf("%ld %ld", &A, &B)!=EOF ) + { + Extended_Euclid(A, B); + printf("%ld %ld %ld\n", X, Y, D); + } + return 0; +} diff --git a/uva_cpp_clean/10104/10104-21.cpp b/uva_cpp_clean/10104/10104-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32da5eb871c0c21df67ae1f95a41d62cfe3a3f11 --- /dev/null +++ b/uva_cpp_clean/10104/10104-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10104 + Name: Euclid Problem + Problem: https://onlinejudge.org/external/101/10104.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +template +struct Triple { + Int d, x, y; + Triple(Int q, Int w, Int e) : d(q), x(w), y(e) {} +}; + +template +Int mod(Int x, Int y) { + return ((x%y)+y)%y; +} + +template +Triple egcd(Int a, Int b) { + if (!b) return Triple(a, 1, 0); + Triple q = egcd(b, mod(a, b)); + return Triple(q.d, q.y, q.x - a/b * q.y); +} + + +int main(){ + long long int a, b; + while (cin>>a>>b) { + auto res = egcd(a, b); + printf("%lld %lld %lld\n", res.x, res.y, res.d); + } +} diff --git a/uva_cpp_clean/10104/10104-9.cpp b/uva_cpp_clean/10104/10104-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2855ebbf63f593063f958b81cfee4b5ca78a4856 --- /dev/null +++ b/uva_cpp_clean/10104/10104-9.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int A, B, xo, yo, d; + +void extendedEuclid(int a, int b){ + if( b == 0 ){ + xo = 1, yo = 0, d = a; + return; + } + extendedEuclid(b, a % b); + int x1 = yo, y1 = xo - ( a / b ) * yo; + xo = x1, yo = y1; +} + +int main(){ + while(~scanf("%d %d", &A, &B)){ + extendedEuclid(A, B); + printf("%d %d %d\n", xo, yo, d); + } + return(0); +} diff --git a/uva_cpp_clean/10105/10105-19.cpp b/uva_cpp_clean/10105/10105-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee47712daea13b46a342a7aaa792cf16e426733b --- /dev/null +++ b/uva_cpp_clean/10105/10105-19.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +using namespace std; + +unordered_map > comb; +long long proc(int n, int m) { + if (m == 0 || m == n) return 1; + if (m < 0 || m > n) return 0; + if (comb[n][m] == 0) comb[n][m] = proc(n-1, m-1) + proc(n-1, m); + return comb[n][m]; +} + +int main() { + int N, K; + while (cin >> N >> K) { + long long coef = 1; + for (int i = 0; i < K; i++) { + int var; + cin >> var; + coef *= proc(N, var); + N -= var; + } + cout << coef << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10105/10105-20.cpp b/uva_cpp_clean/10105/10105-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70f72cd0fbdef6eba3b471d90c8d169793a9d7eb --- /dev/null +++ b/uva_cpp_clean/10105/10105-20.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +long factorial[13] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600}; + +int main() +{ + int n, k; + + while( cin >> n >> k ) + { + int coefficient = factorial[n]; + for(int i=0 ; i> div; + + if( div>1 ) coefficient /= factorial[div]; + } + + cout << coefficient << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10105/10105-21.cpp b/uva_cpp_clean/10105/10105-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4c8e92eb38ca12a23098e1c870d072a6713723e --- /dev/null +++ b/uva_cpp_clean/10105/10105-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10105 + Name: Polynomial Coefficients + Problem: https://onlinejudge.org/external/101/10105.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n, k, x; + while (cin>>n>>k) { + int result = 1; + for (int i=2; i<=n; i++) + result *= i; + + for (int i=0; i> x; + for (int j=2; j<=x; j++) + result /= j; + } + cout << result << endl; + } +} diff --git a/uva_cpp_clean/10105/10105-9.cpp b/uva_cpp_clean/10105/10105-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f53a20094e28970371885ea5730e8a10e421eb06 --- /dev/null +++ b/uva_cpp_clean/10105/10105-9.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +const int SIZE = 15; + +int N, K, ni; +long long ans, F[SIZE]; + +void Factorial(){ + F[0] = 1; + for(int i = 1; i < SIZE; i++) F[i] = i * F[i - 1]; +} + +int main(){ + Factorial(); + while(~scanf("%d %d", &N, &K)){ + ans = F[N]; + for(int i = 0; i < K; i++){ + scanf("%d", &ni); + ans /= F[ni]; + } + printf("%lld\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10106/10106-11.cpp b/uva_cpp_clean/10106/10106-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77b55b0a1499a0874d1aa8642abaa8510434a270 --- /dev/null +++ b/uva_cpp_clean/10106/10106-11.cpp @@ -0,0 +1,81 @@ +#include +/* +Problem: 10106 - Product +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1047 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +string X, Y; +int MAX_SIZE = 600; + +string product(string X, string Y){ + int product_array[MAX_SIZE]; + string product = ""; + + memset(product_array, 0, sizeof(product_array)); //initialize the array with 0's + + reverse(X.begin(), X.end()); // reverse the strings; + reverse(Y.begin(), Y.end()); // so we can work with the smallest digits first + + for (int i = 0; i < X.length(); i++){ + for (int j = 0; j < Y.length(); j++){ + product_array[i + j] += (X[i]-'0') * (Y[j]-'0'); + } + } + + // Now move the carries + for (int i = 0; i < MAX_SIZE; i++){ + product_array[i+1] += product_array[i]/10; + product_array[i] %= 10; + } + + // Detect the number of digits for the resulting number + int end = MAX_SIZE-1; + while (product_array[end] == 0){ + end--; + } + + // Now get the number into one string variable + for (int i = end; i >= 0; i--){ + product += (product_array[i]+'0'); + } + + // if the number is empty.. it means it is 0 + product = (product == "")? "0" : product; + + return product; +} + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + + while ( cin >> X >> Y && !cin.eof() ){ + cout << product(X, Y) << '\n'; + } + return 0; +} +/* +input:- +----------- +12 +12 +2 +222222222222222222222222 + +output:- +----------- +144 +444444444444444444444444 + +Resources:- +------------- +Video (Arabic): https://www.youtube.com/watch?v=KNd6eqRpWqE +*/ diff --git a/uva_cpp_clean/10106/10106-16.cpp b/uva_cpp_clean/10106/10106-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62e088ec95f98895e6ed8cfb5b116eb65c365025 --- /dev/null +++ b/uva_cpp_clean/10106/10106-16.cpp @@ -0,0 +1,41 @@ +//============================================================================ +// Name : 10106 - Product.cpp +// Author : ztrixack +// Copyright : MIT +// Description : 10106 Product in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +#include +#include +using namespace std; + +int main () { + string snumx, snumy, tmp; + int lx, ly, c = 0; + int nx[255] = {0}, ny[255] = {0}, ans[550] = {0}; + while (cin >> snumx >> snumy) { + lx = snumx.length(); + ly = snumy.length(); + for (int i = 0; i < lx; i++) nx[lx - (i + 1)] = snumx[i] - 48; + for (int i = 0; i < ly; i++) ny[ly - (i + 1)] = snumy[i] - 48; + for (int i = 0; i < ly; i++) { + for (int j = 0; j < lx; j++) { + c = i + j; + ans[c] += nx[j] * ny[i]; + while (ans[c] / 10 != 0) { + ans[c + 1] += ans[c] / 10; + ans[c] %= 10; + c++; + } + } + } + + if (snumx == "0" || snumy == "0") c = 0; + for (int i = c; i >= 0; i--) { + cout << ans[i]; + ans[i] = 0; + } + cout << endl; + } +} diff --git a/uva_cpp_clean/10107/10107-11.cpp b/uva_cpp_clean/10107/10107-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8a240e34bfb8d4fcb814fc495a6dedbe29391fb --- /dev/null +++ b/uva_cpp_clean/10107/10107-11.cpp @@ -0,0 +1,76 @@ +#include +/* +Problem: 10107 - What is the Median? +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1048 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +unsigned int arr[10001]; + +int main () { + fastio; + + int idx = 0; + while ( cin >> arr[idx++] && !cin.eof() ) { + int mid = (idx/2); + + int ans = 0; + + nth_element(arr, arr+mid, arr+idx); + + if (idx&1) + ans = arr[mid]; + + else { + nth_element(arr, arr+mid-1, arr+idx); + ans = (arr[mid]+arr[mid-1])/2; + } + + + cout << ans << '\n'; + } + + + return 0; +} +/* +Sample input:- +----------------- +1 +3 +4 +60 +70 +50 +2 + +Sample output:- +----------------- +1 +2 +3 +3 +4 +27 +4 + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10107/10107-14.cpp b/uva_cpp_clean/10107/10107-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2a734301820589fc4bfbcf4aabdd1590da4b603 --- /dev/null +++ b/uva_cpp_clean/10107/10107-14.cpp @@ -0,0 +1,76 @@ +/*************************************************** + * Problem name : 10107 What is the Median?.cpp + * Problem Link : https://uva.onlinejudge.org/external/101/10107.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-05 + * Problem Type : AdHoc(STL) + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100000 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int n; + vectorV; + while (scanf("%d", &n) == 1) { + V.pb(n); + sort(V.begin(), V.end()); + int sz = (int)V.size(); + if (sz == 1) { + printf("%d\n", V[0]); + } else if ( sz % 2 == 1) { + printf("%d\n", V[sz / 2]); + } else { + printf("%d\n", (V[sz / 2] + V[sz / 2 - 1]) / 2); + } + } + return 0; +} diff --git a/uva_cpp_clean/10107/10107-19.cpp b/uva_cpp_clean/10107/10107-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..365f23abe2d11442d3038b341f86664237723064 --- /dev/null +++ b/uva_cpp_clean/10107/10107-19.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +priority_queue pq1, pq2; + +int main() { + long n; + while (scanf("%ld", &n) != EOF) { + if (pq1.empty() || pq1.top() >= n) pq1.push(n); + else pq2.push(-n); + while (pq2.size() > pq1.size()) { + pq1.push(-pq2.top()); + pq2.pop(); + } + while (pq1.size() > pq2.size()) { + pq2.push(-pq1.top()); + pq1.pop(); + } + if (pq1.size() == pq2.size()) { + printf("%ld\n", (pq1.top()-pq2.top())/2); + } else printf("%ld\n", -pq2.top()); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10107/10107-20.cpp b/uva_cpp_clean/10107/10107-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d896632ef0b3ce439c8ac4d12eacf8aae62ddb9 --- /dev/null +++ b/uva_cpp_clean/10107/10107-20.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + long n; + vector v; + + while( scanf("%ld", &n)==1 ) + { + v.push_back(n); + sort(v.begin(), v.end()); + + long len = v.size(), result; + if( len&1 ) result = v[((len+1)/2)-1]; + else result = (v[((len+1)/2)-1] + v[(len+1)/2])/2; + + printf("%ld\n", result); + } + return 0; +} diff --git a/uva_cpp_clean/10107/10107-21.cpp b/uva_cpp_clean/10107/10107-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47448ef4048bef2af11d9d46e3006f9b82c9da54 --- /dev/null +++ b/uva_cpp_clean/10107/10107-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10107 + Name: What is the Median? + Problem: https://onlinejudge.org/external/101/10107.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +int main() { + priority_queue left; + priority_queue, greater> right; + + + int x; + while (cin >> x) { + if (!right.size() || x < right.top()) + left.push(x); + else right.push(x); + + if (left.size() > right.size() + 1) { + right.push(left.top()); + left.pop(); + } + + if (right.size() > left.size() + 1) { + left.push(right.top()); + right.pop(); + } + + if (left.size() == right.size()) + cout << ((left.top() + right.top()) >> 1) << endl; + else if (left.size() > right.size()) + cout << left.top() << endl; + else + cout << right.top() << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10107/10107-3.cpp b/uva_cpp_clean/10107/10107-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9522a243c4abbf9a2c9761f2abcc70563e436f1 --- /dev/null +++ b/uva_cpp_clean/10107/10107-3.cpp @@ -0,0 +1,22 @@ +// Problem : U-Va 10107: What is the Median? + +#include // printf scanf +#include // vec +#include // nth element + +using namespace std; + +int main () { + vector N; + long temp; + + while ( scanf("%ld", &temp) != EOF ){ + N.push_back(temp); + sort(N.begin(),N.end()); + + if ( N.size()%2 == 1 ) + printf("%ld\n", N[N.size()/2]); + else + printf("%ld\n", (N[N.size()/2] + N[N.size()/2 - 1])/2 ); + } +} diff --git a/uva_cpp_clean/10107/10107-5.cpp b/uva_cpp_clean/10107/10107-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3db9a2cab76889858eb15e69903723ecf769f94a --- /dev/null +++ b/uva_cpp_clean/10107/10107-5.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +int main() +{ + int i=0; + int a[100000]; + while(scanf("%d",&a[i])!=EOF) + { + int c; + sort(a,a+i+1); + if(i%2==0) printf("%d\n",a[i/2]); + else printf("%d\n",(a[i/2]+a[i/2+1])/2); + i++; + } + return 0; +} diff --git a/uva_cpp_clean/10107/10107-9.cpp b/uva_cpp_clean/10107/10107-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..974748848c84ede754c0bc575d092375b02cfd89 --- /dev/null +++ b/uva_cpp_clean/10107/10107-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int tmp, pib; +vector v; + +int main(){ + while(scanf("%d", &tmp) == 1){ + v.push_back(tmp); + pib = v.size() >> 1; + nth_element(v.begin(), v.begin() + pib, v.end()); + if(v.size() & 1) printf("%d\n",v[pib]); + else{ + nth_element(v.begin(), v.begin() + pib - 1, v.end()); + printf("%d\n",(v[pib] + v[pib-1]) >> 1); + } + } + return(0); +} diff --git a/uva_cpp_clean/10107/main.cpp b/uva_cpp_clean/10107/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e84ef44d44754f0d5dbe7179dbab57103c6aadc1 --- /dev/null +++ b/uva_cpp_clean/10107/main.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int new_number, length = 0; + long operation; + vector sequence; + + while(cin >> new_number){ + length++; + sequence.push_back(new_number); + sort(sequence.begin(), sequence.end()); + if(length & 1) cout << sequence[length/2] << endl; + else{ + long pos = sequence[length/2]; + long pos2 = sequence[length/2-1]; + cout << (pos+pos2)/2 << endl; + } + } + + +return 0; +} diff --git a/uva_cpp_clean/10110/10110-11.cpp b/uva_cpp_clean/10110/10110-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddb7f2d1bfaceeb88f761b9d144d4abe297cf9eb --- /dev/null +++ b/uva_cpp_clean/10110/10110-11.cpp @@ -0,0 +1,47 @@ +#include +/* +Problem: 10110 - Light, more light +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1051 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +unsigned int N, sq; + +int main () { + + while ( scanf("%d\n", &N) && N ){ + + sq = int(sqrt(N)); + + printf( (sq*sq == N)? "yes\n" : "no\n" ); + + } + + return 0; +} + +/* +input:- +----------- +3 +6241 +8191 +0 + +output:- +----------- +no +yes +no + +Resources:- +------------- +Video (Arabic): https://www.youtube.com/watch?v=6unjJwXC5gI&feature=youtu.be +*/ diff --git a/uva_cpp_clean/10110/10110-13.cpp b/uva_cpp_clean/10110/10110-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..469f9a7914b34c3c8eddc9cf6e2ed7c661a6b52f --- /dev/null +++ b/uva_cpp_clean/10110/10110-13.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +using namespace std; + +#define MAX 1000000 +typedef long long ll; +typedef vector vb; + + +ll n, x; +vb isPrime(MAX, true); +vector prime; + +void findPrimes() +{ + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += i) + { + isPrime[j] = false; + } + + prime.push_back(i); + } + } +} + +bool cal() +{ + ll count = 1; + bool isOn = false; + + ll p_inx = 0, p = prime[p_inx]; + + while (p * p <= n) + { + if (n % p == 0) + { + ll how = 0; + + while (n % p == 0) + { + how++; + + n /= p; + } + + count *= (how + 1); + } + + p = prime[++p_inx]; + } + + if (n != 1) + { + count *= 2; + } + + if (count % 2 == 1) isOn = true; + + return isOn; +} + +int main() +{ + findPrimes(); + + while (cin >> x && x != 0) + { + n = x; + + cal() ? cout << "yes" << endl : cout << "no" << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10110/10110-18.cpp b/uva_cpp_clean/10110/10110-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6552e576c9f01ff5f867f99f09de749975c891a9 --- /dev/null +++ b/uva_cpp_clean/10110/10110-18.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; +typedef long long int lli; + +int main(){ + lli n; + while(scanf("%lld", &n) && n != 0){ + if((int)sqrt(n) == sqrt(n)) printf("yes\n"); + else printf("no\n"); + } + return 0; +} +/* + La estratega a usar es un teorema de la teoria de numeros que dice: + "Si n tiene sqrt(n) perfecta, entonces, n posee un numero impar de factores". +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10110/10110-19.cpp b/uva_cpp_clean/10110/10110-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d63a5075c88e9d01e6813fc4af29039b926312f --- /dev/null +++ b/uva_cpp_clean/10110/10110-19.cpp @@ -0,0 +1,13 @@ +#include +#include +using namespace std; + +int main() { + unsigned int N; + while (scanf("%d", &N) && N != 0) { + int sq = sqrt(N); + if (sq*sq == N) printf("yes\n"); + else printf("no\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10110/10110-21.cpp b/uva_cpp_clean/10110/10110-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09ddcd5c29a7aa6fe117a1ff32d9853dce4ed183 --- /dev/null +++ b/uva_cpp_clean/10110/10110-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10110 + Name: Light, more light + Problem: https://onlinejudge.org/external/101/10110.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main() { + long long int n; + while ((cin>>n) && n) { + long long int sq = sqrt(n); + cout << (sq*sq == n ? "yes" : "no") << endl; + } +} + diff --git a/uva_cpp_clean/10110/10110-3.cpp b/uva_cpp_clean/10110/10110-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd93d8ebe9c431a3f927a1f8d3f375ac25135039 --- /dev/null +++ b/uva_cpp_clean/10110/10110-3.cpp @@ -0,0 +1,34 @@ +#include + +int main () +{ + int primeNumbers[25] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; + int N, tempPointer, tempResult,result; + + while ( scanf("%d",&N), N) + { + result = 0; + tempPointer = 0; + tempResult = 1; + while ( N != 1 ) + { + if ( N % primeNumbers[tempPointer] == 0 ) + { + N /= primeNumbers[tempPointer]; + tempResult *= primeNumbers[tempPointer]; + } + else + { + if ( tempResult > 1 ) + result += tempResult; + tempResult = 0; + tempPointer++; + } + } + + if ( result % 2 == 1 ) + puts("yes"); + else + puts("no"); + } +} diff --git a/uva_cpp_clean/10110/10110-5.cpp b/uva_cpp_clean/10110/10110-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ced0c3dd8c665733e120ca27002c66a4fe20fb84 --- /dev/null +++ b/uva_cpp_clean/10110/10110-5.cpp @@ -0,0 +1,18 @@ +#include +#include +int main() +{ + long long n,a,b; + while(scanf("%lld",&n)==1) + { + if(n==0) + break; + a=sqrt(n); + b=a*a; + if(b==n) + printf("yes\n"); + else + printf("no\n"); + } + return(0); +} diff --git a/uva_cpp_clean/10110/10110-6.cpp b/uva_cpp_clean/10110/10110-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b94a73a160856fc3293bcd4c4e43c919dde4eff9 --- /dev/null +++ b/uva_cpp_clean/10110/10110-6.cpp @@ -0,0 +1,20 @@ +/* + math > ad-hoc + difficulty: easy + date: 09/Jun/2020 + problem: check if the number of divisors of n is odd + hint: check if n is a perfect square + by: @brpapa +*/ +#include +using namespace std; + +int main() { + while (true) { + unsigned int n; cin >> n; if (n == 0) break; + int sn = (int)sqrt(n); + if (n == sn*sn) cout << "yes" << endl; + else cout << "no" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10111/10111-6.cpp b/uva_cpp_clean/10111/10111-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97e34af169b04d5d806b88f1eb498546254a411f --- /dev/null +++ b/uva_cpp_clean/10111/10111-6.cpp @@ -0,0 +1,104 @@ +/* + math > game theory > minimax + difficulty: medium + date: 12/Apr/2020 + problem: given a state of a tic tac toe board, check if X will win independent of the O movement + hint: minimax + memo + backtracking + by: @brpapa +*/ +#include +using namespace std; + +typedef pair pii; +typedef array,4> Tboard; + +const int INF = 1 << 30; +const int pX = -1, pY = 1; + +/* + -1, se pX vence + 1, se pY vence + 0, se empata + INF, se ainda tem jogo +*/ +int checkWinner(Tboard board) { + vector> possibilities = { + { pii(0, 0), pii(0, 1), pii(0, 2), pii(0, 3) }, + { pii(1, 0), pii(1, 1), pii(1, 2), pii(1, 3) }, + { pii(2, 0), pii(2, 1), pii(2, 2), pii(2, 3) }, + { pii(3, 0), pii(3, 1), pii(3, 2), pii(3, 3) }, + { pii(0, 0), pii(1, 0), pii(2, 0), pii(3, 0) }, + { pii(0, 1), pii(1, 1), pii(2, 1), pii(3, 1) }, + { pii(0, 2), pii(1, 2), pii(2, 2), pii(3, 2) }, + { pii(0, 3), pii(1, 3), pii(2, 3), pii(3, 3) }, + { pii(0, 0), pii(1, 1), pii(2, 2), pii(3, 3) }, + { pii(0, 3), pii(1, 2), pii(2, 1), pii(3, 0) }, + }; + + for (auto pos : possibilities) { + char p = board[pos[0].first][pos[0].second]; + + if (p != '.' + && p == board[pos[1].first][pos[1].second] + && p == board[pos[2].first][pos[2].second] + && p == board[pos[3].first][pos[3].second]) + return p == 'x'? pX : pY; + } + + for (int r = 0; r < 4; r++) + for (int c = 0; c < 4; c++) + if (board[r][c] == '.') return INF; + return 0; +} + +map memo; + +// retorna o player vencedor +int minimax(int p, Tboard board) { + // jogador atual p, estado atual do jogo board + + if (memo.count(board)) return memo[board]; + int &ans = memo[board]; + + ans = checkWinner(board); + if (ans != INF) return ans; + + ans *= p * (-1); + + for (int r = 0; r < 4; r++) + for (int c = 0; c < 4; c++) + if (board[r][c] == '.') { + + board[r][c] = (p == pX)? 'x' : 'o'; + int maybe = minimax(p * (-1), board); + board[r][c] = '.'; + + if (p == pX) ans = min(ans, maybe); + else ans = max(ans, maybe); + } + return ans; +} + +int main() { + char c; + while (cin >> c && c != '$') { + Tboard board; + for (int r = 0; r < 4; r++) + for (int c = 0; c < 4; c++) + cin >> board[r][c]; // 'x', 'o' ou '.' + + pii sol = {-1, -1}; + + for (int r = 0; r < 4; r++) + for (int c = 0; c < 4; c++) + if (board[r][c] == '.') { + board[r][c] = 'x'; + if (minimax(pY, board) == pX && sol == pii(-1, -1)) sol = {r, c}; + board[r][c] = '.'; + } + + if (sol == pii(-1, -1)) cout << "#####" << endl; + else cout << "(" << sol.first << "," << sol.second << ")" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10111/10111-9.cpp b/uva_cpp_clean/10111/10111-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c80aa1e43e83b34e16082366ad44fad56a74fbed --- /dev/null +++ b/uva_cpp_clean/10111/10111-9.cpp @@ -0,0 +1,165 @@ +#include + +#define WIN 1 +#define DRAW 0 +#define FAIL -1 +#define CONTINUE -2 + +using namespace std; + +const int MAX_N = 4, SIZE = 6, MAX_ORD = 130; + +bool possible; +char ch, grid[SIZE][SIZE]; +int available, ans_row, ans_col, weight[MAX_ORD]; +int x_row[MAX_N], x_col[MAX_N], x_main_diag, x_sec_diag; +int o_row[MAX_N], o_col[MAX_N], o_main_diag, o_sec_diag; +map vis; + +int minVal(); +int maxVal(); + +void print() { + if (possible) printf("(%d,%d)\n", ans_row, ans_col); + else puts("#####"); +} + +int getHash() { + int ret = 0; + for (int row = 0; row < MAX_N; row++) + for (int col = 0; col < MAX_N; col++) + ret += weight[grid[row][col]], ret *= 10; + return ret; +} + +inline int getIndex(const int& row, const int& col) { + return row * MAX_N + col; +} + +inline void toggleAvailables(const int& row, const int& col) { + available ^= (1 << getIndex(row, col)); +} + +void mark(const int& row, const int& col, const bool is_x, const int variation) { + if (is_x) { + grid[row][col] = (variation == 1) ? 'x' : '.'; + x_row[row] += variation; + x_col[col] += variation; + if (row == col) x_main_diag += variation; + if (row + col == MAX_N - 1) x_sec_diag += variation; + } + else { + grid[row][col] = (variation == 1) ? 'o' : '.'; + o_row[row] += variation; + o_col[col] += variation; + if (row == col) o_main_diag += variation; + if (row + col == MAX_N - 1) o_sec_diag += variation; + } +} + +bool isDraw() { + bool ret = true; + ret &= x_main_diag and x_sec_diag; + ret &= o_main_diag and o_sec_diag; + for (int id = 0; id < MAX_N and ret; id++) + ret &= x_row[id] and x_col[id] and o_row[id] and o_col[id]; + return ret; +} + +int isTerminal() { + int sum = 0; + if (x_main_diag == MAX_N or x_sec_diag == MAX_N) return WIN; + if (o_main_diag == MAX_N or o_sec_diag == MAX_N) return FAIL; + if (isDraw()) return DRAW; + for (int id = 0; id < MAX_N; id++) { + if (x_row[id] == MAX_N or x_col[id] == MAX_N) return WIN; + else if(o_row[id] == MAX_N or o_col[id] == MAX_N) return FAIL; + sum += x_row[id] + o_row[id]; + } + return (sum == MAX_N * MAX_N) ? DRAW : CONTINUE; +} + +int minVal() { + int pib = available, res = WIN, pos, row, col, ret; + int _hash = getHash(); + if (vis.find(_hash) != vis.end()) return vis[_hash]; + while (pib and res != FAIL) { + pos = __builtin_ctz(pib), row = pos / MAX_N, col = pos - MAX_N * row; + toggleAvailables(row, col); + mark(row, col, false, 1); + ret = isTerminal(); + res = min(res, (ret == CONTINUE) ? maxVal() : ret); + toggleAvailables(row, col); + mark(row, col, false, -1); + pib &= pib - 1; + } + return vis[_hash] = res; +} + +int maxVal() { + int pib = available, res = FAIL, pos, row, col, ret; + int _hash = getHash(); + if (vis.find(_hash) != vis.end()) return vis[_hash]; + while (pib and res != WIN) { + pos = __builtin_ctz(pib), row = pos / MAX_N, col = pos - MAX_N * row; + toggleAvailables(row, col); + mark(row, col, true, 1); + ret = isTerminal(); + res = max(res, (ret == CONTINUE) ? minVal() : ret); + toggleAvailables(row, col); + mark(row, col, true, -1); + pib &= pib - 1; + } + return vis[_hash] = res; +} + +int minimax(const int& row, const int& col) { + toggleAvailables(row, col); + mark(row, col, true, 1); + int res = isTerminal(); + if (res == CONTINUE) res = minVal(); + toggleAvailables(row, col); + mark(row, col, true, -1); + return res; +} + +void solve() { + for (int row = 0; row < MAX_N; row++) + for (int col = 0; col < MAX_N; col++) + if (grid[row][col] != '.') mark(row, col, grid[row][col] == 'x', 1); + else available |= (1 << getIndex(row, col)); + int pib = available, pos, row, col, res; + while (pib and not possible) { + pos = __builtin_ctz(pib), row = pos / MAX_N, col = pos - MAX_N * row; + res = minimax(row, col); + if (res == WIN) ans_row = row, ans_col = col, possible = true; + pib &= pib - 1; + } +} + +void read() { + for (int row = 0; row < MAX_N; row++) scanf("%s\n", grid[row]); +} + +inline void clear() { + available = 0; + possible = false; + x_main_diag = x_sec_diag = o_main_diag = o_sec_diag = 0; + for (int id = 0; id < MAX_N; id++) x_row[id] = x_col[id] = o_row[id] = o_col[id] = 0; +} + +inline void init() { + weight['o'] = 1; + weight['x'] = 2; +} + +int main() { + init(); + while (scanf("%s\n", &ch), ch != '$') { + clear(); + read(); + solve(); + print(); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10113/10113-21.cpp b/uva_cpp_clean/10113/10113-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf6a1464f0462555c67380d93e1e6195ba0779c2 --- /dev/null +++ b/uva_cpp_clean/10113/10113-21.cpp @@ -0,0 +1,92 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10113 + Name: Exchange Rates + Problem: https://onlinejudge.org/external/101/10113.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int gcd(int a, int b) { + if (a==0) return b; + return gcd(b%a, a); +} + + +int gr[100], den[100], num[100]; +int find(int u) { + if (gr[u] == u) return u; + + int v = find(gr[u]); + if (gr[u] != v) { + num[u] *= num[gr[u]]; + den[u] *= den[gr[u]]; + int gc = gcd(num[u], den[u]); + num[u] /= gc; + den[u] /= gc; + gr[u] = v; + } + return v; +} + +void join(int x, int u, int y, int v) { + int ug = find(u); + gr[ug] = find(v); + + int nu = num[v] * den[u] * x, + de = num[u] * den[v] * y, + gc = gcd(nu, de); + num[ug] = nu/gc; + den[ug] = de/gc; + +} + + + +int cnt=0; +map ids; +int getId(string name) { + auto it = ids.find(name); + if (it != ids.end()) + return it->second; + return ids[name] = cnt++; +} + + +int main(){ + for (int i=0; i<100; i++) { + den[i] = num[i] = 1; + gr[i] = i; + } + + int x, y, u, v; + char cm, xs[30], ys[30]; + while (cin>>cm && cm!='.') + if (cm == '!') { + cin>>x>>xs >> cm >> y>>ys; + join(x, getId(xs), y, getId(ys)); + } + + else { + cin>>xs >>cm>> ys; + int u = getId(xs), + v = getId(ys); + + if (find(u) != find(v)) + printf("? %s = ? %s\n", xs, ys); + else { + int gc = gcd( x = num[u]*den[v], + y = num[v]*den[u] ); + printf("%d %s = %d %s\n", x/gc, xs, y/gc, ys); + } + } +} diff --git a/uva_cpp_clean/10113/10113-6.cc b/uva_cpp_clean/10113/10113-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..b5d1616496a5223bc895b3b07284c3e7872976ce --- /dev/null +++ b/uva_cpp_clean/10113/10113-6.cc @@ -0,0 +1,53 @@ +// https://uva.onlinejudge.org/external/101/10113.pdf +#include +using namespace std; +using msi=unordered_map; +using iii=tuple; +using vi=vector; +using viii=vector; +using vviii=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + char o; + int a,b,n=0; + string x,y; + msi m; + vviii g; + for(;;){ + cin>>o; + if(o=='.')break; + if(o=='!'){ + cin>>a>>x>>o>>b>>y; + if(!m[x])m[x]=++n; + if(!m[y])m[y]=++n; + while(g.size()>x>>o>>y; + if(m[x]&&m[y]){ + vi c(n),d(n); + functiondfs=[&](int u){ + for(iii x:g[u]){ + int v,a,b; + tie(v,a,b)=x; + if(!c[v]){ + int p=c[u]*a,q=d[u]*b; + int k=__gcd(p,q); + p/=k;q/=k; + c[v]=p;d[v]=q; + dfs(v); + } + } + }; + int u=m[x]-1,v=m[y]-1; + c[u]=d[u]=1; + dfs(u); + if(c[v])cout< +#include + +using namespace std; + +double depreciation[105]; + +int main() +{ + double month, payment, borrow, d, car_value; + + while( cin >> month >> payment >> borrow >> d && month>=0.0 ) + { + for(int i=0 ; i<105 ; i++) depreciation[i] = -1; + + while( d-- ) + { + int drop_val_mon; + cin >> drop_val_mon; + cin >> depreciation[drop_val_mon]; + } + + double car_value = payment+borrow, pay = borrow/month; + + for(int i=0 ; i<=month ; i++) + { + if( depreciation[i]==-1 ) depreciation[i] = depreciation[i-1]; + + car_value *= (1-depreciation[i]); + + if( car_value>=borrow ) + { + cout << i << " month" << (i!=1 ? "s" : "") << endl; + break; + } + + borrow -= pay; + } + } + return 0; +} diff --git a/uva_cpp_clean/10114/10114-21.cpp b/uva_cpp_clean/10114/10114-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79b0d09c7eb4be5d360eaa93dc044df40ecd5891 --- /dev/null +++ b/uva_cpp_clean/10114/10114-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10114 + Name: Loansome Car Buyer + Problem: https://onlinejudge.org/external/101/10114.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + int m, n, j; + double P[143], x, t, p; + while (cin >> n >> x >> t >> m && n>=0) { + fill(P, P+143, -1); + for (int i=0; i> j >> p; + P[j] = p; + } + + for (int i=0; i<143; ++i) + if (P[i] < 0) + P[i] = P[i-1]; + + double v = (t+x)*(1 - P[0]); + x = t / n; + for (j=1; t >= v && j<=n; ++j) { + v -= P[j] * v; + t -= x; + } + + cout << j-1 << " month" << (j==2 ? "\n" : "s\n"); + } +} diff --git a/uva_cpp_clean/10114/10114-3.cpp b/uva_cpp_clean/10114/10114-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a4912bf65bc118b36733798ecdbcd56bc64ac6e --- /dev/null +++ b/uva_cpp_clean/10114/10114-3.cpp @@ -0,0 +1,62 @@ +// Problem: U-Va 10114 - Loansome Car Buyer + +#include + +struct record{ + int month; + double deprecation; +}; + +int main () { + // Input will contain multiple test cases: + // input style: duration downPayment loan recordN (recordN stands for record number) + // record lines below describing deprecation + + int duration, + recordN, + currentMonth, + resultIndic; + + double downPayment, + monthlyPayment, + loan, + carValue, + tempDep; + + while ( scanf("%d %lf %lf %d", &duration, &downPayment, &loan, &recordN) == 4 ){ + if ( duration <= 0 ) return 0; + + // At first collecting input in a record array: + record records[recordN]; + carValue = loan + downPayment; + monthlyPayment = loan / duration; + + for ( int l = 0; l < recordN; l++ ) + scanf ("%d %lf", &records[l].month, &records[l].deprecation); + + // Input collection finished. + currentMonth = 1; + resultIndic = 0; + + carValue -= carValue * records[resultIndic].deprecation; + + while ( loan > carValue ){ + loan -= monthlyPayment; + + if ( currentMonth == records[resultIndic+1].month ) + resultIndic++; + + carValue -= (carValue * records[resultIndic].deprecation ); + + currentMonth++; + } + + currentMonth--; + + if ( currentMonth == 1 ) + printf ("%d month\n", 1); + else + printf ("%d months\n", currentMonth); + + } +} diff --git a/uva_cpp_clean/10114/10114-9.cpp b/uva_cpp_clean/10114/10114-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f46291280dce08968d5a19367b5057b54c2efddf --- /dev/null +++ b/uva_cpp_clean/10114/10114-9.cpp @@ -0,0 +1,27 @@ +#include + +#define MAX_N 110 + +using namespace std; + +int duration, numberOfDepreciation, month, ans; +double payment, amount, depreciation[MAX_N], rate, percentage, current; + +int main(){ + while(scanf("%d %lf %lf %d", &duration, &payment, &amount, &numberOfDepreciation), duration > 0){ + ans = 0; + fill(depreciation, depreciation + MAX_N, -1.0); + for(int i = 0; i < numberOfDepreciation; i++) + scanf("%d %lf", &month, &percentage), depreciation[month] = percentage; + for(int i = 1; i < MAX_N; i++) + if(depreciation[i] < 0.0) depreciation[i] = depreciation[i - 1]; + rate = amount / duration; + current = ( payment + amount ) * ( 1.0 - depreciation[0] ); + while(current < amount){ + current *= ( 1.0 - depreciation[++ans] ); + amount -= rate; + } + printf(ans == 1 ? "%d month\n" : "%d months\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10114/main.cpp b/uva_cpp_clean/10114/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c10b86669fda2b31040a219cc2aa2c5382b85da --- /dev/null +++ b/uva_cpp_clean/10114/main.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +int main(){ + int meses, casos, mes1, mes2, mesA = 0; + double prestamo, base, porcentage, valor, cada_mes; + while(cin >> meses >> base >> prestamo >> casos && meses > 0){ + // cout << meses << ' ' << base << ' ' << prestamo << ' ' << casos << endl; + valor = prestamo + base; + cada_mes = prestamo / meses; + bool bandera = true; + mesA = 0; + cin >> mes1 >> porcentage; + for(casos = casos;casos > 0; casos--){ + if(casos!=1) cin >> mes2; + else mes2 = meses; + if(bandera){ + while(mesA < mes2){ + valor = valor * (1-porcentage); + // cout << valor << ' ' << prestamo << endl; + if(valor > prestamo){ + cout << mesA << " month"; + if(mesA == 1) cout << endl; + else cout << "s\n"; + bandera = false; + break; + } + // cout << mesA << endl; + prestamo -= cada_mes; + mesA ++; + } + } + if(casos != 1)cin >> porcentage; + } + if(bandera) cout << meses << " months\n"; + } + // cout << meses << ' ' << base << ' ' << prestamo << ' ' << casos << endl; + + +return 0; +} diff --git a/uva_cpp_clean/10115/10115-18.cpp b/uva_cpp_clean/10115/10115-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5abb29b246894fe814f264b57db5f8378a63ba81 --- /dev/null +++ b/uva_cpp_clean/10115/10115-18.cpp @@ -0,0 +1,33 @@ +#include +#include + +using namespace std; + +int main(){ + int n; + string cWord; + while(true){ + cin >> n; + if (n == 0) break; + int cont = 0; + getline(cin, cWord); + string words[300]; + string rWords[300]; + while(cont < n){ + getline(cin, words[cont]); + getline(cin, rWords[cont]); + cont++; + } + getline(cin, cWord); + for(int i = 0; i < n; i++){ + int length = words[i].size(); + while(true){ + int index = cWord.find(words[i]); + if(index == -1) break; + cWord.replace(index, length, rWords[i]); + } + } + cout << cWord << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10115/10115-21.cpp b/uva_cpp_clean/10115/10115-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d9d8e5bd8fe8b41c728137064c3e5137a2e8f60 --- /dev/null +++ b/uva_cpp_clean/10115/10115-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10115 + Name: Automatic Editing + Problem: https://onlinejudge.org/external/101/10115.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +void replace(string &str, const string &oldStr, const string &newStr) { + int len = oldStr.length(), pos = 0; + while ((pos = str.find(oldStr, pos)) >= 0) + str.replace(pos, len, newStr); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while (cin >> n && n) { + cin.ignore(100, '\n'); + + string R1[17], R2[17], str; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 13 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +string str[MAX]; +mapmp; +int row, col, vis[MAX][MAX], d[MAX][MAX]; +int x, y, flag, n; +void move(char ch) { + if (ch == 'N') { + x--; + } else if (ch == 'S') { + x++; + } else if (ch == 'E') { + y++; + } else { + y--; + } +} +bool isValid(int tx, int ty) { + if (tx >= 0 && ty >= 0 && tx < row && ty < col) return true; + return false; +} +int BFS_2d(int sx, int sy) { + SET(vis, 0); + SET(d, 0); + memset(vis, 0, sizeof(vis)); + vis[sx][sy] = 1; + d[sx][sy] = 1; + queue q; + q.push(pii(sx, sy)); + while (!q.empty()) { + pii top = q.front(); + q.pop(); + int tx = top.first; + int ty = top.second; + x = tx, y = ty; + move(str[tx][ty]); + if (isValid(x, y)) { + if (vis[x][y] == 0) { + mp[pii(x, y)] = top; + vis[x][y] = 1; + d[x][y] = d[top.first][top.second] + 1; + q.push(pii(x, y)); + } else { + pii p = mp[pii(x, y)]; + int xx = p.first; + int yy = p.second; + flag = 1; + if (x == sx && y == sy) { + n = d[top.first][top.second]; + return d[top.first][top.second]; + } else { + n = d[top.first][top.second]; + return d[top.first][top.second] - d[xx][yy]; + } + } + } else { + return d[top.first][top.second]; + } + } +} +int main () { + //~ __FastIO; + int pos; + while (scanf("%d %d %d", &row, &col, &pos) == 3 && row != 0 && col != 0 && pos != 0) { + for (int i = 0; i < row; i++) { + cin >> str[i]; + } + flag = 0; + int ans = BFS_2d(0, pos - 1); + if (flag) { + int a = n - ans; + printf("%d step(s) before a loop of %d step(s)\n", a, ans); + } else { + printf("%d step(s) to exit\n", ans); + } + + mp.clear(); + } + return 0; +} diff --git a/uva_cpp_clean/10116/10116-21.cpp b/uva_cpp_clean/10116/10116-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85dab33ce2abf1a4e7645ebba4c2fdc755af0e14 --- /dev/null +++ b/uva_cpp_clean/10116/10116-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10116 + Name: Robot Motion + Problem: https://onlinejudge.org/external/101/10116.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int main(){ + int r, c, x, s[20][20]; + char g[20][20]; + while (cin>>r>>c>>x && (r || c || x)) { + for (int i=0; i>g[i]; + for (int j=0; j=0 && cx=0 && cy=c || cy<0 || cy>=r) + printf("%d step(s) to exit\n", cp); + else printf("%d step(s) before a loop of %d step(s)\n", s[cy][cx]-1, cp-s[cy][cx]+1); + } +} diff --git a/uva_cpp_clean/10116/10116-6.cc b/uva_cpp_clean/10116/10116-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..72cbacdc8a10907f1a3266b1091a401946930aef --- /dev/null +++ b/uva_cpp_clean/10116/10116-6.cc @@ -0,0 +1,30 @@ +// https://uva.onlinejudge.org/external/101/10116.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +using vs=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + int n,m,r; + cin>>n>>m>>r; + if(!n)break; + vs a(n); + for(int i=0;i>a[i]; + vvi b(n,vi(m)); + int y=0,x=r-1,k=0; + while(y>=0&&y=0&&x=n||x<0||x>=m)cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10121 + Name: Legendary Pokemon + Problem: https://onlinejudge.org/external/101/10121.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct Move { + int type; + int damage; + int poison; + double confusion; + double accuracy; + + bool operator<(const Move &m) const { + if (type != m.type) + return type > m.type; + + if (type == 2) + return confusion*accuracy > m.confusion*m.accuracy; + + return damage*accuracy + 3*poison > m.damage*m.accuracy + 3*m.poison; + } +}; +Move moves[4]; +double run[5]; +double bp[6] = { 0.2, 0.18, 0.15, 0.1, 0.05, 0 }; +int k, l, g, maxHp, balls[6]; + + +double play1(int i, int hp1, int hp2, int p1, int p2, int sr, int smi, int pr, int poison); +double play2(int i, int hp1, int hp2, int p1, int p2, int sr, int smi, int pr, int poison) { + int hp2b = hp2; + if (pr > 0) hp2 -= poison; + if (run[i]==1 || hp1<=0 || hp2<=0) return 0; + + double pf = 1-run[i]; + int sr2 = sr ? sr-1 : 0, + pr2 = pr ? pr-1 : 0; + + if (hp2b<=150 && p2>0) + return pf*play1(i+1, hp1, hp2+200, p1, p2-1, sr2, smi, pr2, poison); + + double c = moves[smi].confusion; + + double noAttack = play1(i+1, hp1, hp2, p1, p2, sr2, smi, pr2, poison); + if (sr>0 && !c) return pf*noAttack; + + double attackMe = play1(i+1, hp1-300, hp2, p1, p2, sr2, smi, pr2, poison); + if (!sr) return pf * (0.3*attackMe + 0.7*noAttack); + + double attackSelf = play1(i+1, hp1, hp2-300, p1, p2, sr2, smi, pr2, poison); + double res = c*attackSelf + (1-c)*attackMe; + return pf * (0.3*res + 0.7*noAttack); +} + + + +double play1(int i, int hp1, int hp2, int p1, int p2, int sr, int smi, int pr, int poison) { + if (hp1<=0 || hp2<=0) return 0; + double best = 0, val; + + + // If he is going to run, dont bother with these + if (run[i] < 1) { + + // Do Nothing + double nothing = best = play2(i, hp1, hp2, p1, p2, sr, smi, pr, poison); + + // Moves & Attacks + for (int j=0; j best) best = val; + } + + + // Use Hyper Potion + if (p1 > 0 && hp1 < maxHp) { + val = play2(i, min(hp1+200, maxHp), hp2, p1-1, p2, sr, smi, pr, poison); + if (val > best) best = val; + } + } + + + // Use Balls + double bonus = 0; + if (hp2 <= 50) bonus += 0.1; + else if (hp2 <= 100) bonus += 0.05; + if (pr > 0) bonus += 0.1; + if (sr > 0) bonus += 0.05; + + for (int j=0; j<6; j++) + if (balls[j]>0) { + double cp = bp[j] + bonus; + if (cp > 0) { + balls[j]--; + val = cp + (1-cp)*play2(i, hp1, hp2, p1, p2, sr, smi, pr, poison); + balls[j]++; + + if (val > best) best = val; + } + } + + return best; +} + + + +int main(){ + int p1, p2; + while (cin>>l>>g>>maxHp && (l||g||maxHp)) { + for (int i=0; i<5; i++) { + cin >> run[i]; + run[i] /= 100.0; + } + + cin >> p1 >> p2; + for (int i=4; i>=0; i--) + cin >> balls[i]; + + if (g == 0) { + balls[5] = balls[0]; + balls[0] = 0; + } + if (l <=50) { + balls[3] += balls[1]; + balls[1] = 0; + } + + cin >> k; + for (int i=0; i> m.type; + + // Normal Attack + if (m.type == 1) + cin >> m.damage >> m.accuracy; + + // Status Change (confusion=0 -> Sleepy) + else if (m.type == 2) + cin >> m.confusion >> m.accuracy; + + // Poison Attack + else + cin >> m.damage >> m.poison >> m.accuracy; + + m.confusion /= 100.0; + m.accuracy /= 100.0; + moves[i] = m; + } + + printf("%.4f\n", play1(0, maxHp, 999, p1, p2, 0, 0, 0, 0)); + } +} diff --git a/uva_cpp_clean/10122/10122-21.cpp b/uva_cpp_clean/10122/10122-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b628ef32c98de7705b52a0090a14dd249f79a8cd --- /dev/null +++ b/uva_cpp_clean/10122/10122-21.cpp @@ -0,0 +1,210 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10122 + Name: Mysterious Mountain + Problem: https://onlinejudge.org/external/101/10122.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +#define EPS 1e-9 +#define MAXN 110 + + +int abs(int x) { + return x<0 ? -x : x; +} + +struct P { + double x, y; + + P() {}; + P(double q, double w) : x(q), y(w) {}; + + bool operator<(const P &p) const { + return x 0) return false; + + double vcrossu1 = (d.x - c.x)*(a.y - c.y) - (d.y - c.y)*(a.x - c.x); + double vcrossu2 = (d.x - c.x)*(b.y - c.y) - (d.y - c.y)*(b.x - c.x); + return vcrossu1*vcrossu2 <= 0; +} + + +// ---------------------------------------------------------------------------- + + +int n, maxEp, minEp, + c[MAXN], w[MAXN], s[MAXN]; +P coords[MAXN]; + +int memo[MAXN][1005]; +bool collides(int ep, int s) { + if (memo[ep][s] != -1) + return memo[ep][s]; + + if (fabs(coords[ep].x-s) < EPS) + return memo[ep][s]=0; + + P p1 = P(coords[ep].x, coords[ep].y -EPS), + p2 = P(s, -EPS); + + for (int i=ep; i0 && coords[i].x>s; i--) + if (lineSegIntersect(coords[i-1], coords[i], p1, p2)) + return memo[ep][s] = 1; + + return memo[ep][s] = 0; +} + +double moveTime(int ep, int p, int cu) { + double d = abs(s[p] - cu); + double x = fabs(coords[ep].x - cu); + return d/w[p] + sqrt(x*x + coords[ep].y*coords[ep].y)/c[p]; +} + +double dist(int ep, int p) { + if (coords[ep].y < EPS) + return fabs(s[p] - coords[ep].x) / max(w[p], c[p]); + + int cu; + if (w[p] > c[p]) { + double opt = c[p]*coords[ep].y / sqrt((double)w[p]*w[p]-c[p]*c[p]); + if (s[p] > coords[ep].x) + cu = ceil(min(coords[ep].x + opt, (double) s[p])); + else cu = floor(max(coords[ep].x - opt, (double) s[p])); + } + else cu = s[p]; + if (cu > maxEp) cu = maxEp; + if (cu < minEp) cu = minEp; + + int step = cu > coords[ep].x ? -1 : 1; + while (cu>=minEp && cu<=maxEp && collides(ep, cu)) + cu += step; + + if (cu > maxEp) cu = maxEp; + if (cu < minEp) cu = minEp; + + return min(moveTime(ep, p, cu), moveTime(ep, p, cu + step)); +} + +// --------------------------------------------------------------------- + +struct Trip { + int i, j; + double d; + + Trip() {}; + Trip(int a, int b, double w) : i(a), j(b), d(w) { }; + bool operator<(const Trip &t) const { + return d trips; + while (cin>>n && n) { + trips.clear(); + + int maxX = maxEp = 0; + minEp = 10000; + for (int i=0; i> coords[i].x >> coords[i].y; + if (coords[i].x>maxX) maxX = maxEp = coords[i].x; + if (coords[i].x> c[i] >> w[i] >> s[i]; + if (s[i]>maxX) maxX = s[i]; + } + + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10125 + Name: Sumsets + Problem: https://onlinejudge.org/external/101/10125.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include + +using namespace std; + +struct mpair{ + int a,ab; + bool operator<(const mpair &t)const{ + return ab::iterator wh; + multiset sums; + int nums[1000]; + bool found; + + while(scanf("%d",&n)==1 && n>0){ + sums.clear(); + for(i=0;i=0;i--) + for(j=n-1;j>=0;j--) + if(i!=j) + { + tt.ab=nums[i]-nums[j]; + for(wh=sums.find(tt);wh!=sums.end() && wh->ab==tt.ab;wh++){ + if(wh->a==nums[i] || wh->a==nums[j] || + (wh->ab-wh->a==nums[i]) || (wh->ab-wh->a==nums[j]))continue; + if(nums[i]>ans || !found) + ans=nums[i]; + found=1; + break; + } + } + if(found) + printf("%d\n",ans); + else printf("no solution\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10125/10125-9.cpp b/uva_cpp_clean/10125/10125-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d442bb3acba41aa422dcdca0962fdf2da9a2e69b --- /dev/null +++ b/uva_cpp_clean/10125/10125-9.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +long long n, v[1010], ans, d; +bool ok; + +int main(){ + while(scanf("%lld",&n), n){ + ok = false, ans = -1; + for(int i = 0; i < n; i++) scanf("%lld", &v[i]); + sort(v, v + n); + for(int i = 0; i < n; i++) + for(int j = i + 1; j < n; j++) + for(int k = j + 1; k < n; k++){ + d = v[i] + v[j] + v[k]; + if(d > ans && d != v[i] && d != v[j] && d != v[k] && binary_search(v, v + n, d)) + ans = d, ok = true; + } + if(ok) printf("%lld\n", ans); + else printf("no solution\n"); + } + return(0); +} diff --git a/uva_cpp_clean/10126/10126-15.cpp b/uva_cpp_clean/10126/10126-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e9e77bd2a7cb7050130316771f035ce9c87b51b --- /dev/null +++ b/uva_cpp_clean/10126/10126-15.cpp @@ -0,0 +1,49 @@ +/* + 字串計數 +*/ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + string word; + regex pattern("[^A-Za-z]"); + int count; + bool case_n = false; + + while (cin >> count) { + if (case_n) + cout << "\n"; + + bool print = case_n = true; + map order; + cin.ignore(1024, '\n'); + while (getline(cin, word)) { + if (word.length() == 0) + continue; + transform(word.begin(), word.end(), word.begin(), ::tolower); + istringstream input(regex_replace(word, pattern, " ")); + while (input >> word and word != "endoftext") + if (word.length()) + ++order[word]; + if (word == "endoftext") + break; + } + + for (const auto &i : order) + if (i.second == count) { + print = false; + cout << i.first << "\n"; + } + if (print) + cout << "There is no such word.\n"; + } +} diff --git a/uva_cpp_clean/10127/10127-19.cpp b/uva_cpp_clean/10127/10127-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b13959e8da7dfd96f088217e89938873fe9b0745 --- /dev/null +++ b/uva_cpp_clean/10127/10127-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int n; + while (scanf("%d", &n) != EOF) { + int i = 1; + int cont = 1; + while (i % n != 0) i = (i*10 + 1)%n, cont++; + printf("%d\n", cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10127/10127-21.cpp b/uva_cpp_clean/10127/10127-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17b039c8dbc876fcc1a18219285c89e47f3a135b --- /dev/null +++ b/uva_cpp_clean/10127/10127-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10127 + Name: Ones + Problem: https://onlinejudge.org/external/101/10127.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int n, i; + while (cin >> n) { + int r = 1; + for (i = 1; r % n; i++) + r = (r * 10 + 1) % n; + cout << i << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10127/10127-5.cpp b/uva_cpp_clean/10127/10127-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7600a27e8894c8091ac5ab69b9dc7ba11e4d5ec0 --- /dev/null +++ b/uva_cpp_clean/10127/10127-5.cpp @@ -0,0 +1,30 @@ +#include +#include +int main() +{ + char a[10000]; + long long n,s,k; + int i; + while(scanf("%lld",&n)==1) + { + + if(n==0) + { + printf("0\n"); + continue; + } + + s=0; + for(i=1,k=1;;i++) + { + s=k%n; + if(s==0) + { + printf("%d\n",i); + break; + } + k=s*10+1; + } + } + return 0; +} diff --git a/uva_cpp_clean/10128/10128-21.cpp b/uva_cpp_clean/10128/10128-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c2d5f24bb9b390fd95250e91cbd63b0258f77fc --- /dev/null +++ b/uva_cpp_clean/10128/10128-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10128 + Name: Queue + Problem: https://onlinejudge.org/external/101/10128.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +long long DP[14][14][14]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + DP[1][1][1] = 1; + for (int n=2; n<=13; ++n) + for (int l=1; l<=n; ++l) + for (int r=1; r<=n; ++r) + DP[n][l][r] = (n-2)*DP[n-1][l][r] + DP[n-1][l-1][r] + DP[n-1][l][r-1]; + + int T, n, l, r; + cin >> T; + while (T-- && cin >> n >> l >> r) + cout << DP[n][l][r] << '\n'; +} diff --git a/uva_cpp_clean/10129/10129-17.cpp b/uva_cpp_clean/10129/10129-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53a9fb0af8566094038e399276291abd025582a4 --- /dev/null +++ b/uva_cpp_clean/10129/10129-17.cpp @@ -0,0 +1,70 @@ +/** + * AOAPC II Example 6-16 Play on Words + */ +#include +using namespace std; +vector uf(26), sz(26); +int rt; +int root(int a) +{ + while (a != uf[a]) + a = uf[a] = uf[uf[a]]; + return a; +} +void uni(int a, int b) +{ + int ra = root(a), rb = root(b); + if (ra != rb) + { + if (sz[ra] > sz[rb]) + swap(ra, rb); + uf[ra] = rb; + sz[rb] += sz[ra]; + rt = rb; + } +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int T; + cin >> T; + while (T--) + { + int n; + cin >> n; + vector o_degs(26, 0), i_degs(26, 0); + iota(uf.begin(), uf.end(), 0); + fill(sz.begin(), sz.end(), 1); + while (n--) + { + string cur; + cin >> cur; + int from = cur.front() - 'a', to = cur.back() - 'a'; + ++o_degs[from]; + ++i_degs[to]; + uni(from, to); + } + int num = 0, n_beg = 0, n_end = 0; + bool ok = true; + for (int i = 0; i < 26; ++i) + if (o_degs[i] || i_degs[i]) + { + ++num; + int diff = o_degs[i] - i_degs[i]; + if (diff == 1) + ++n_beg; + else if (diff == -1) + ++n_end; + else if (diff != 0) + { + ok = false; + break; + } + } + if (num == sz[rt] && ok && n_beg == n_end && (n_beg == 0 || n_beg == 1)) + cout << "Ordering is possible.\n"; + else + cout << "The door cannot be opened.\n"; + } +} diff --git a/uva_cpp_clean/10129/10129-21.cpp b/uva_cpp_clean/10129/10129-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a46ec3a9dea3f4412098dfe70cdc1809339434d --- /dev/null +++ b/uva_cpp_clean/10129/10129-21.cpp @@ -0,0 +1,112 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10129 + Name: Play on Words + Problem: https://onlinejudge.org/external/101/10129.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +inline int readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return 0; + + int l = 0; + str[l++] = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + str[l++] = ch; + str[l] = 0; + return l; +} + +// ------------------------------------------------------------------------ + +int E[26][26], I[26], O[26]; +int dfs(int u) { + int sum = 0; + for (int v=0; v<26; ++v) + if (E[u][v]) { + --E[u][v]; + sum += dfs(v) + 1; + } + return sum; +} + +bool check(int n) { + int odd=0, st=-1, ste; + for (int i=0; i<26; ++i) + if (O[i] > I[i]) { + if (++odd > 1) + return false; + st = i; + } + else if (O[i]) + ste = i; + + return dfs(st!=-1 ? st : ste) == n; +} + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + char str[1043]; + int T = readUInt(); + while (T--) { + int n = readUInt(); + + memset(E, 0, sizeof(E)); + memset(I, 0, sizeof(I)); + memset(O, 0, sizeof(O)); + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1013 + Name: Island Hopping + Problem: https://onlinejudge.org/external/10/1013.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +int Pr[1001], W[1001]; +int memo[32][1001]; + +int rec(int remW, int i) { + if (i<0 || remW<=0) return 0; + if (memo[remW][i] != -1) + return memo[remW][i]; + + int rr = rec(remW, i-1); + + if (remW < W[i]) + return memo[remW][i] = rr; + + return memo[remW][i] = max( rec(remW-W[i], i-1) + Pr[i], rr ); +} + + + +int main(){ + int T; + cin>>T; + while(T--) { + int n; + cin>>n; + for (int i=0; i>Pr[i]>>W[i]; + for (int j=0; j<32; j++) + memo[j][i] = -1; + } + + int g, mw, res = 0; + cin>>g; + for (int i=0; i> mw; + res += rec(mw, n-1); + } + + cout << res << endl; + } +} diff --git a/uva_cpp_clean/10130/10130-10.cpp b/uva_cpp_clean/10130/10130-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..749175fe4c5c5a150155fdfcc4c43b7ed19c42ff --- /dev/null +++ b/uva_cpp_clean/10130/10130-10.cpp @@ -0,0 +1,77 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + __builtin_popcount(); +*/ +bool isPrime(int x) { + if (x <= 1) return false; + if (x <= 3) return true; + if (x % 2 == 0 || x % 3 == 0) return false; + for (int i = 5; i * i <= x; i = i + 6) + if (x % i == 0 || x % (i + 2) == 0) return false; + return true; +} + + +int n,g; + +int p[1005],w[1005],mw[105]; + +int dp[1005][35]; + + +int solve(int i, int remender) { + + if(i==n) { + return 0; + } + + int &ans= dp[i][remender]; + + if(ans!=-1) return ans; + + ans=-100; + + if(remender >= w[i]) ans=max(ans, p[i]+solve(i+1,remender-w[i])); + + ans=max(ans, solve(i+1,remender)); + + return ans; + +} + + +int32_t main() { + Sakr_ + + int t,i; + + cin>>t; + while (t--) { + cin>>n; + for(i=0;i>p[i]>>w[i]; + } + cin>>g; + for(i=0;i>mw[i]; + } + ll ans=0; + memset(dp,-1,sizeof dp); + for(i=0;i +using namespace std; + +#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) +#define PI(a,b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long +#define S second +#define F first +#define endl '\n' +#define FOR(var,len) for(var = 0;var>n; + int V[n],W[n]; + + for(i = 0;i>V[i]>>W[i]; + } + + memset(dp,0,sizeof(dp)); + for(i = 1;i<) + + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10130/10130-14.cpp b/uva_cpp_clean/10130/10130-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fe35d78f1bbe021c32e8d23b7fd05ebd83a7201a --- /dev/null +++ b/uva_cpp_clean/10130/10130-14.cpp @@ -0,0 +1,56 @@ +/*************************************************** + * Problem name : 10130 - SuperSale.cpp + * OJ : Uva + * Result : AC + * Date : 19-04-17 + * Problem Type : DP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 1005 +using namespace std; +typedef long long ll; +int n,p[MAX],w[MAX],ww; +int dp[MAX][32]; +int knapSack(int i,int W){ + if(i == n+1) return 0; + if(dp[i][W]!=-1) return dp[i][W]; + int pro1 = 0, pro2 = 0; + if(W>=w[i]) pro1 = p[i] + knapSack(i+1, W-w[i]); + else pro1 = 0; + pro2 = knapSack(i+1,W); + + return dp[i][W] = max(pro1,pro2); +} +int main () { + int tc; + scanf("%d", &tc); + for(int t = 1; t<=tc; t++){ + scanf("%d", &n); + for(int i = 1; i<=n; i++){ + scanf("%d %d", &p[i], &w[i]); + } + int N,sum = 0; + scanf("%d", &N); + memset(dp,-1,sizeof(dp)); + for(int i = 1; i<=N; i++){ + scanf("%d",&ww); + sum+=knapSack(1,ww); + } + printf("%d\n", sum); + } + return 0; +} diff --git a/uva_cpp_clean/10130/10130-20.cpp b/uva_cpp_clean/10130/10130-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dfb326d3d2229ab726f84af625f73f7604e4762a --- /dev/null +++ b/uva_cpp_clean/10130/10130-20.cpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +int P[1005], W[1005], max_value[1005][450]; + +int main() +{ + int tc; + + scanf("%d", &tc); + while( tc-- ) + { + int N; + scanf("%d", &N); + for(int i=1 ; i<=N ; i++) scanf("%d %d", &P[i], &W[i]); + + for(int i=1 ; i<=N ; i++) + for(int j=1 ; j<=30 ; j++) + if( W[i]>j ) max_value[i][j] = max_value[i-1][j]; + else max_value[i][j] = max(max_value[i-1][j], max_value[i-1][j-W[i]]+P[i]); + + int G, result = 0; + scanf("%d", &G); + while( G-- ) + { + int temp; + scanf("%d", &temp); + result += max_value[N][temp]; + } + + printf("%d\n", result); + } + return 0; +} diff --git a/uva_cpp_clean/10130/10130-21.cpp b/uva_cpp_clean/10130/10130-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..079543add2bde82edae73aa25ce409f9b6bc1bf3 --- /dev/null +++ b/uva_cpp_clean/10130/10130-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10130 + Name: SuperSale + Problem: https://onlinejudge.org/external/101/10130.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, g; + cin >> T; + while (T--) { + cin >> n; + int DP[31]={}; + for (int i=0, p, w; i> p >> w; + for (int r=30; r>=w; --r) + DP[r] = max(DP[r], DP[r-w] + p); + } + + cin >> g; + int res = 0; + for (int i=0, mw; i> mw; + res += DP[mw]; + } + cout << res << endl; + } +} diff --git a/uva_cpp_clean/10130/10130-5.cpp b/uva_cpp_clean/10130/10130-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c607f3e3f04b9daf0c8bcaa41a1b148c4ab32f10 --- /dev/null +++ b/uva_cpp_clean/10130/10130-5.cpp @@ -0,0 +1,174 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*** http://shipuahamed.blogspot.com ** +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1LL<<60) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int profit[1010][1010],value[1010],weight[1010]; +int main() +{ + + int knapsack_weight,n,t; + cin>>t; + while(t--) + { + cin>>n; + for(int i=1;i<=n;i++) + { + cin>>value[i]>>weight[i]; + } + int a,sum=0; + cin>>a; + cover(profit,0); + for(int i=0;i>knapsack_weight; + for(int i=1;i<=n;i++) + { + for(int w=1;w<=knapsack_weight;w++) + { + if(weight[i]>w) + { + profit[i][w] = profit[i-1][w]; + } + else + { + if(profit[i-1][w]>profit[i-1][w-weight[i]]+value[i]) + { + profit[i][w] = profit[i-1][w]; + } + else + { + profit[i][w] = profit[i-1][w-weight[i]]+value[i]; + } + } + } + } + sum+=profit[n][knapsack_weight]; + } + pf("%d\n",sum); + } + return 0; +} +//************************************* +//******** Team : BUBT_HIDDEN ********** +//************************************** +//*********** Shipu Ahamed ************* +//*** http://shipuahamed.blogspot.com ** +//*************************************/ +// +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +// +//#define sc scanf +//#define pf printf +//#define ll long long +//#define pi 2*acos(0.0) +// +//#define ff first +//#define se second +//#define inf (1<<30) //infinity value +//#define pb push_back +//#define mod 1000000007 +//#define ST(v) sort(v.begin(),v.end()) +//#define cover(a,d) memset(a,d,sizeof(a)) +//#define input freopen("in.txt","r",stdin) +//#define output freopen("out.txt","w",stdout) +//#define maxall(v) *max_element(v.begin(),v.end()) +//#define minall(v) *min_element(v.begin(),v.end()) +//#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) +// +//using namespace std; +//int price[10001],weight[10001]; +//int dp[10001][400]; +//int main() +//{ +// +// int T; +// cin>>T; +// while(T--) +// { +// memset(dp,0,sizeof(dp)); +// int N; cin>>N; +// for(int i=0;i>price[i]>>weight[i]; +// int G; cin>>G; +// int ans=0; +// for(int i=0;i=weight[i]) +// dp[i][j]=max(dp[i-1][j],dp[i-1][j-weight[i]]+price[i]); +// else +// dp[i][j]=dp[i-1][j]; +// while(G--) +// { +// int x; +// cin>>x; +// ans+=dp[N-1][x]; +// } +// cout< + +#define MAX_N 1010 +#define MAX_W 40 + +using namespace std; + +int tc,n,G,mv,ans,memo[MAX_N][MAX_W],V[MAX_N],W[MAX_N]; + +int knapsack(int id,int w){ + if(id==n || w==0) return 0; + if(memo[id][w]!=-1) return memo[id][w]; + if(W[id]>w) return memo[id][w] = knapsack(id+1,w); + return memo[id][w] = max(knapsack(id+1,w),V[id]+knapsack(id+1,w-W[id])); +} + +int main(){ + scanf("%d",&tc); + while(tc--){ + memset(memo,-1,sizeof memo); + scanf("%d",&n); + for(int i=0;i +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define int long long +#define oo 1e15 +using namespace std; +/** + * by:Ahmed Sakr (sakr_) with AZA ; + * freopen("input.txt","r",stdin); + * freopen("output.txt","w",stdout); + * __builtin_popcountll(); +**/ +class utility { + +public : + static int gcd(int a, int b) { return (!b) ? a : gcd(b, a % b); } + static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } +}; + +struct st { + int iq; + int w; + int id; +}arr[1001]; + +int w,iq; +int n; + +bool comp(st &a,st &b) { + if (a.w != b.w) { + return a.w < b.w; + } + return a.iq > b.iq; +} + +int dp[1001]; + +int solve(int i) { + + if(i==n) return 0; + + int &ans = dp[i]; + if(ans!=-1) return ans; + + ans = 1; + + for(int j=i+1;j arr[j].iq) { + ans = max(ans,1+ solve(j)); + } + } + + return ans; +} + + +void solveit() { + + n=1; + while (cin>>w>>iq) { + ///if(w==-1)break; + + arr[n-1]={iq,w,n}; + n++; + } + n--; + sort(arr,arr+n, comp); + /* + for(int i=0;i arr[prev].w && arr[i].iq < arr[prev].iq)) { + cout<>timeOfRepeteLoop; + while (timeOfRepeteLoop--) { + solveit(); + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/10131/10131-19.cpp b/uva_cpp_clean/10131/10131-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4c966d03590573a3d87b8c1a1f7b3132b7d9568 --- /dev/null +++ b/uva_cpp_clean/10131/10131-19.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iii; + +vector v; +vector sol; +unordered_map > dp; + +int proc(int p, int S, int W, bool def) { + int D = 100000*S + W; + if (p >= v.size()) return 0; + if (!(dp.count(p) && dp[p].count(D))) { + int val1 = proc(p+1, S, W, false), val2 = -2; + if (S > v[p].second.first && W < v[p].first) val2 = proc(p+1, v[p].second.first, v[p].first, false); + if (val1 > val2+1) { + dp[p][D].first = val1; + dp[p][D].second = 0; + } else { + dp[p][D].first = val2 + 1; + dp[p][D].second = 1; + } + } + if (def) { + sol[p] = dp[p][D].second; + if (!dp[p][D].second) { + proc(p+1, S, W, true); + } else { + proc(p+1, v[p].second.first, v[p].first, true); + } + } + return dp[p][D].first; +} + +int main() { + int W, S, c = 0; + while (cin >> W >> S) { + v.push_back(iii(W, ii(S, c))); + c++; + } + sort(v.begin(), v.end()); + sol.assign(v.size(), false); + printf("%d\n", proc(0, 100000, 0, true)); + for (int i = 0; i < sol.size(); i++) { + if (sol[i]) printf("%d\n", v[i].second.second+1); + } + return 0; +} diff --git a/uva_cpp_clean/10131/10131-21.cpp b/uva_cpp_clean/10131/10131-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0854f08bff4f7c3e1c013435184e408ecfc995f --- /dev/null +++ b/uva_cpp_clean/10131/10131-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10131 + Name: Is Bigger Smarter? + Problem: https://onlinejudge.org/external/101/10131.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct El { + int w, s, ind; + bool operator < (const El& e) const { + return w == e.w ? s > e.w : w < e.w; + } +}; + +El els[1001]; +bool seen[1001]; +int DP[1001], P[1001]; + +bool lt(int i, int j) { + return els[i].wels[j].s; +} + + + +int main(){ + int n, l; + for (n=0; cin >> els[n].w >> els[n].s; n++) + els[n].ind = n+1; + + sort(els, els+n); + + int bestl=0, besti=0; + for (int i=0; iDP[i] && lt(j, i)) { + DP[i] = DP[j] + 1; + P[i] = j; + } + + if (DP[i]>bestl) { + bestl=DP[i]; + besti=i; + } + } + + cout << bestl << endl; + int *res = new int[bestl]; + int j=bestl-1; + for (int i=besti; i!=-1; i = P[i]) + res[j--] = els[i].ind; + + for (int i=0; i longest increasing subsequence (LIS) + difficulty: medium + date: 25/Jan/2020 + hint: sort elephants based on increasing kg, then apply LDS on iq + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +struct T { + int id, kg, iq; +} e[1010]; + +bool cmp(const T &a, const T &b) { + return a.kg < b.kg; +} + +int N; +vector memo, p; + +// lds(j): tamanho da LDS terminando em e[j], olhando apenas para o iq +int lds(int j) { + if (j == 0) return 1; + + int &ans = memo[j]; + if (ans != -1) return ans; + + ans = 1; + for (int i = 0; i < j; i++) + if (e[i].iq > e[j].iq && e[i].kg < e[j].kg && 1+lds(i) > ans) { + ans = 1+lds(i); + p[j] = i; + } + return ans; +} + +void recover(int j) { + if (j == -1) return; + recover(p[j]); + cout << e[j].id << endl; +} + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + for (N = 0; cin >> e[N].kg >> e[N].iq; N++) e[N].id = N+1; + sort(e, e+N, cmp); // increasing kg + + memo.assign(N, -1); p.assign(N, -1); + + int ans = 1, end; // tamanho da lds ótima e onde termina + for (int j = 0; j < N; j++) + if (lds(j) >= ans) { + ans = lds(j); + end = j; + } + + cout << ans << endl; + recover(end); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10131/10131-9.cpp b/uva_cpp_clean/10131/10131-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd748a2dbf851621c33ebf9ee5e942cdb0620749 --- /dev/null +++ b/uva_cpp_clean/10131/10131-9.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int a, b, LIS, lis[1010], path[1010], idxMax, ct; +vector < tuple > v; + +void printPath(int idx){ + if(path[idx] == -1){ + printf("%d\n", get<2>(v[idx])); + return; + } + printPath(path[idx]); + printf("%d\n", get<2>(v[idx])); +} + +int main(){ + while(scanf("%d %d", &a, &b) != EOF) v.push_back( make_tuple(b, a, ++ct) ); + sort(v.begin(), v.end()); + reverse(v.begin(), v.end()); + for(int i = 0; i < v.size(); i++){ + path[i] = -1, lis[i] = 1; + for(int j = 0; j < i; j++) + if(get<1>(v[i]) > get<1>(v[j]) && lis[i] < lis[j] + 1) + lis[i] = lis[j] + 1, path[i] = j; + if(lis[i] > LIS) LIS = lis[i], idxMax = i; + } + printf("%d\n", LIS); + printPath(idxMax); + return(0); +} diff --git a/uva_cpp_clean/10132/10132-14.cpp b/uva_cpp_clean/10132/10132-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd77c91342e2c3b6ff888686af2ebade599cb509 --- /dev/null +++ b/uva_cpp_clean/10132/10132-14.cpp @@ -0,0 +1,160 @@ +/*************************************************** + * Problem Name : 10132 - File Fragmentation.cpp + * Problem Link : https://onlinejudge.org/external/101/10132.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-11-15 + * Problem Type : + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + cin.ignore(); + cin.ignore(); + + while (tc--) { + vectorvec; + string str; + + while (getline (cin, str) && !str.empty() ) { + vec.push_back (str); + } + + mapmp; + + for (int i = 0; i < (int) vec.size(); i++) { + for (int j = i + 1; j < (int) vec.size(); j++) { + mp[vec[i] + vec[j]]++; + mp[vec[j] + vec[i]]++; + } + } + + string ans; + int mx = 0; + + for (auto it : mp) { + if (it.S > mx) { + ans = it.F; + mx = it.S; + } + } + + cout << ans << "\n"; + + if (tc) cout << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/10132/10132-21.cpp b/uva_cpp_clean/10132/10132-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2508f38dcbf50e41176a809ad54d7a62287e1f59 --- /dev/null +++ b/uva_cpp_clean/10132/10132-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10132 + Name: File Fragmentation + Problem: https://onlinejudge.org/external/101/10132.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + + +int main() { + int T, n; + string line; + scanf("%d", &T); + getline(cin,line); + getline(cin,line); + while (T--) { + int totalLen = 0; + vector strs[300], allstr; + for (n=0; getline(cin,line) && line.length()>0; ++n) { + totalLen += line.length(); + strs[line.length()].push_back(line); + allstr.push_back(line); + } + + int bcnt = 0; + string bstr; + + int resLen = totalLen/(n>>1); + map lens; + for (string &s1: allstr) { + int sl = s1.length(); + + if (bcnt) { + size_t pos = bstr.find(s1); + if (pos==string::npos || (pos!=0 && pos!=bstr.length()-sl)) { + lens[bstr] = bcnt = 0; + } + } + + for (string &s2: strs[resLen - sl]) { + string ss = s1 + s2; + int cnt = ++lens[ss]; + if (cnt > bcnt) { + bcnt = cnt; + bstr = ss; + } + } + } + + cout << bstr << endl; + if (T) putchar('\n'); + } +} diff --git a/uva_cpp_clean/10136/10136-9.cpp b/uva_cpp_clean/10136/10136-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac90e93b030d317a10d8db9844609697dc30430d --- /dev/null +++ b/uva_cpp_clean/10136/10136-9.cpp @@ -0,0 +1,80 @@ +#include + +typedef double ptype; + +using namespace std; + +const int LEN = 30; +const long double R = 2.5L, EPS = 1e-9; + +struct Point { + ptype x, y; + Point() {} + Point(ptype x_, ptype y_): x(x_), y(y_) {} +}; + +int T; +char line[LEN]; +vector points; +Point center; + +bool getCenter(Point p1, Point p2) { + ptype d2 = powl(p1.x - p2.x, 2) + powl(p1.y - p2.y, 2); + ptype det = R * R / d2 - 0.25; + if (det < 0.0) return false; + ptype h = sqrt(det); + center.x = (p1.x + p2.x) * 0.5 + (p1.y - p2.y) * h; + center.y = (p1.y + p2.y) * 0.5 + (p2.x - p1.x) * h; + return true; +} + + +int getAnswer() { + int ret = 0; + for (Point p_i: points) { + ptype d = sqrtl(powl(p_i.x - center.x, 2) + powl(p_i.y - center.y, 2)); + if (d <= R + EPS or fabs(d - R) < EPS) ret++; + } + return ret; +} + +int solve() { + int ret = 1; + for (int i = 0; i < points.size(); i++) { + for (int j = i + 1; j < points.size(); j++) { + if (getCenter(points[i], points[j])) { + ret = max(ret, getAnswer()); + } + if (getCenter(points[j], points[i])) { + ret = max(ret, getAnswer()); + } + } + } + return ret; +} + +bool readCase () { + while (cin.getline(line, LEN), strlen(line)) { + ptype x, y; + sscanf(line, "%lf %lf", &x, &y); + points.push_back(Point(x, y)); + } + return true; +} + +inline void clear () { + points.clear(); +} + +int main () { + cin.getline(line, LEN); + sscanf(line, "%d", &T); + cin.getline(line, LEN); + while (T--) { + readCase(); + printf("%d\n", solve()); + if (T) putchar('\n'); + clear(); + } + return (0); +} diff --git a/uva_cpp_clean/10137/10137-20.cpp b/uva_cpp_clean/10137/10137-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e855623ff92e98fd5611e5b1428933259fd41172 --- /dev/null +++ b/uva_cpp_clean/10137/10137-20.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + +double money[1005]; + +int main() +{ + int n; + + while( cin >> n && n!=0 ) + { + double total = 0.0; + for(int i=0 ; i> money[i]; + total += money[i]; + } + + double avg = total/n, neg_sum = 0.0, pos_sum = 0.0; + for(int i=0 ; ipos_sum ? neg_sum : pos_sum ) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10139/10139-13.cpp b/uva_cpp_clean/10139/10139-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..229ebea5bca211bfc521363b2310272e51cd96f0 --- /dev/null +++ b/uva_cpp_clean/10139/10139-13.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 10000001 + +typedef long long ll; +typedef vector vll; +typedef vector vb; +typedef pair pll; +typedef vector< pll > vpll; + +ll n, m; +vll primes; +vpll factors; +vb isPrime(MAX, true); + +void findPrime() +{ + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += i) + { + isPrime[j] = false; + } + + primes.push_back(i); + } + } +} + +void findFactors(ll n) +{ + ll primes_index = 0, pf = primes[primes_index]; + + while (pf * pf <= n) + { + ll count = 0; + + if (n % pf == 0) + { + while (n % pf == 0) + { + n /= pf; + + count++; + } + + factors.push_back(make_pair(pf, count)); + } + + pf = primes[++primes_index]; + } + + if (n != 1) + { + factors.push_back(make_pair(n, 1)); + } +} + +bool isDivide(ll x, ll y) +{ + if (y == 0) return false; + + else if (x >= y) return true; + + else + { + for (int i = 0; i < factors.size(); ++i) + { + ll how = 0; + ll prime = factors[i].first; + ll count = factors[i].second; + + for (ll j = prime; j <= x ; j *= prime) + { + how += (x / j); + } + + if (count > how) return false; + } + + return true; + } +} + +int main() +{ + findPrime(); + + while (cin >> n >> m) + { + factors.clear(); + + findFactors(m); + + if (isDivide(n, m)) + { + printf("%lld divides %lld!\n", m, n); + } + + else + { + printf("%lld does not divide %lld!\n", m, n); + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10139/10139-19.cpp b/uva_cpp_clean/10139/10139-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf459ae82b0aa3e0f4280f0f7afcbeb937f573d3 --- /dev/null +++ b/uva_cpp_clean/10139/10139-19.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<100000001> bs; +vi primes; + +inline void sieve(ll upperbound = 50000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline void primeFactors(ll n, vii & factors) { + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +int main() { + sieve(); + long long n, m; + vii factors; + while (cin >> n >> m) { + bool ok = true; + if (n < m) { + factors.clear(); + primeFactors(m, factors); + long long k = 2; + for (int i = 0; i < factors.size(); i++) { + long long mult = 1; + for (k = mult * factors[i].first; factors[i].second > 0 && k <= n; k = mult * factors[i].first) { + while (k % factors[i].first == 0 && k > 0 && factors[i].second > 0) { + k /= factors[i].first; + factors[i].second--; + } + mult++; + } + if (factors[i].second > 0) break; + } + ok = true; + for (int i = 0; i < factors.size() && ok; i++) ok = factors[i].second == 0; + } + if (ok) printf("%lld divides %lld!\n", m, n); + else printf("%lld does not divide %lld!\n", m, n); + } + return 0; +} diff --git a/uva_cpp_clean/10139/10139-21.cpp b/uva_cpp_clean/10139/10139-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5b1cc33fd5767749f7a0b62008cf4c5e8c85a7c --- /dev/null +++ b/uva_cpp_clean/10139/10139-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10139 + Name: Factovisors + Problem: https://onlinejudge.org/external/101/10139.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +bool hase(int n, int f, int c) { + int sum = 0; + for (int ff=f; ff<=n; ff*=f) { + sum += n/ff; + if (sum >= c) + return true; + } + + return false; +} + +bool check(int n, int m) { + int sq = sqrt((double)m); + for (int i=2; i<=sq; i++) + if (m%i==0) { + int cnt=0; + do { + cnt++; + + m /= i; + } while (m%i==0); + + if (!hase(n, i, cnt)) + return false; + + sq = sqrt((double)m); + } + + return m<=1 || n>=m; +} + +int main(){ + int n, m; + while (cin>>n>>m) + if (check(n, m)) + printf("%d divides %d!\n", m, n); + else + printf("%d does not divide %d!\n", m, n); +} diff --git a/uva_cpp_clean/10139/10139-6.cpp b/uva_cpp_clean/10139/10139-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1981b1b68cb25087ad5faa98aece301963f61f32 --- /dev/null +++ b/uva_cpp_clean/10139/10139-6.cpp @@ -0,0 +1,57 @@ +/* + math > number theory > prime numbers > prime factorization + difficulty: medium + date: 21/Mar/2020 + problem: do n! is divisible by m? + hint: check if the prime factors of m are contained in the prime factors of n! + by: @brpapa +*/ +#include +#include +#include +#include +#define UB 35000 // sqrt(10^9) +#define ll long long +using namespace std; + +vector primes; +void sieve() { + primes.clear(); + bitset is; is.set(); is[0] = is[1] = 0; + + for (int i = 2; i <= UB; i++) + if (is[i]) { + primes.push_back(i); + for (int j = i*i; j <= UB; j += i) is[j] = 0; + } +} + +int getPowers(int n, int p) { + int count = 0; + for (ll power = p; n >= power; power *= p) + count += n/power; + return count; +} + +int main() { + sieve(); + int N, M; + while (cin >> N >> M) { + bool divide = 1; + + // fatora M + int m = M; + for (int p: primes) { + if (p*p > m || m == 1 || !divide) break; + + int pFreq = 0; + while (m % p == 0) pFreq++, m /= p; + + if (pFreq > getPowers(N, p)) divide = 0; + } + if (m > 1 && 1 > getPowers(N, m)) divide = 0; + + printf("%d %s %d!\n", M, divide? "divides": "does not divide", N); + } + return 0; +} diff --git a/uva_cpp_clean/10139/10139-9.cpp b/uva_cpp_clean/10139/10139-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3187ae77ef2f24f8f5762b4d74397e1f58f16f4 --- /dev/null +++ b/uva_cpp_clean/10139/10139-9.cpp @@ -0,0 +1,53 @@ +#include + +#define SIZE 100010 + +using namespace std; + +bool is_prime[SIZE]; +vector prime; +vector < pair > Factor; +int N, M; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++) + if( is_prime[it1] ){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +void GetFactors(int n){ + Factor.clear(); + if( n == 0 ) return; + for(int it = 0; it < prime.size() and prime[it] * prime[it] <= n and n != 1; it++) + if( n % prime[it] == 0 ){ + int ct = 0; + while( n % prime[it] == 0 ) n /= prime[it], ct++; + Factor.push_back( make_pair(prime[it], ct) ); + } + if( n != 1 ) Factor.push_back( make_pair(n, 1) ); +} + +bool solve(){ + for(int it = 0; it < Factor.size(); it++){ + int ct = 0; + for(long long p = Factor[it].first; N / p; p *= Factor[it].first) + ct += N / p; + if( ct < Factor[it].second ) return false; + } + return true; +} + +int main(){ + sieve(); + while(scanf("%d %d",&N, &M)==2){ + GetFactors(M); + if( solve() ) printf("%d divides %d!\n", M, N); + else printf("%d does not divide %d!\n", M, N); + } + return(0); +} diff --git a/uva_cpp_clean/10140/10140-13.cpp b/uva_cpp_clean/10140/10140-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5eba16231f97805a454264c911d7fa20b2b9beb --- /dev/null +++ b/uva_cpp_clean/10140/10140-13.cpp @@ -0,0 +1,116 @@ +#include +#include + +using namespace std; + +long long l, u; + +#define MAX 10000001 +#define MIN 0 + +vector p; +vector isPrime; + +void setPrime() +{ + p.clear(); + isPrime.clear(); + isPrime.resize(MAX, true); + + isPrime[0] = isPrime[1] = false; + + for (long long i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (long long j = i * i; j < MAX; j += i) + { + isPrime[j] = false; + } + + p.push_back(i); + } + } +} + +bool bePrime(long long x) +{ + if (x < MAX) + { + return isPrime[x]; + } + + else + { + for (long long i = 0; i < p.size() && p[i] * p[i] <= u; ++i) + { + if (x % p[i] == 0) return false; + } + + return true; + } +} + +int main() +{ + setPrime(); + + while (cin >> l >> u) + { + long long p, q, a, b, x, y; + + bool okP = false, okQ = false; + + p = q = a = b = x = y = l; + + long long minDist = MAX, maxDist = MIN; + + for (long long i = l; i <= u; ++i) + { + if (bePrime(i)) + { + if (!okP) + { + okP = true; + + p = i; + } + + else + { + okQ = true; + + q = i; + + if (q - p < minDist) + { + a = p; + b = q; + + minDist = q - p; + } + + if (q - p > maxDist) + { + x = p; + y = q; + + maxDist = q - p; + } + + p = q; + } + } + } + + if (okQ) + { + printf("%lld,%lld are closest, %lld,%lld are most distant.\n", a, b, x, y); + } + + else + { + cout << "There are no adjacent primes." << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10140/10140-20.cpp b/uva_cpp_clean/10140/10140-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c53fcaf6e9ee45dc2f1f386af4e8b86b5b691d3 --- /dev/null +++ b/uva_cpp_clean/10140/10140-20.cpp @@ -0,0 +1,79 @@ +#include +#include +#include + +using namespace std; + +#define MAX 46341 +#define Check(N, pos) ((bool) ((N)&(1<<(pos)))) +#define Set(N, pos) ((N)=((N)|(1<<(pos)))) + +vector < unsigned > primes; +unsigned status[(MAX>>5)+1]; + +void bitSieve() +{ + unsigned sqrtMAX = unsigned (sqrt(MAX)); + for(unsigned i=3 ; i<=sqrtMAX ; i+=2) + if( !Check(status[i>>5], i&31) ) + for(unsigned j=i*i ; j<=MAX ; j+=(i<<1)) Set(status[j>>5], j&31); + + primes.push_back(2); + for(unsigned i=3 ; i<=MAX ; i+=2) + if( !Check(status[i>>5], i&31) ) primes.push_back(i); +} + +bool isPrime(unsigned num) +{ + if( num>5], num&31) ) return true; + else return false; + } + unsigned sqrtNUM = unsigned (sqrt(num)), SZ = primes.size(); + for(unsigned i=0 ; imaximum ) + { + maximum = i-temp; + D1 = temp; + D2 = i; + } + temp = i; + } + + if( !(C1 && C2 && D1 && D2) ) printf("There are no adjacent primes.\n"); + else printf("%u,%u are closest, %u,%u are most distant.\n", C1, C2, D1, D2); + } + return 0; +} diff --git a/uva_cpp_clean/10140/10140-21.cpp b/uva_cpp_clean/10140/10140-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e98e9f60f78f445b787dfc52759e599d101a1602 --- /dev/null +++ b/uva_cpp_clean/10140/10140-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10140 + Name: Prime Distance + Problem: https://onlinejudge.org/external/101/10140.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MAXP 46350 + +bool isNotPrime[MAXP] = { true, true, false }; +vector primes; + +void sieve() { + for (int i=2; i= mx) return -1; + for (int y = x+1; y<=mx; ++y) + if (isPrime(y)) + return y; + return -1; +} + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + sieve(); + + int L, R; + while (cin >> L >> R) { + int ap, aq, bp, bq; + int mn = 1e7; + int mx = 0; + + for ( + int p = nextPrime(L-1, R), q = nextPrime(p, R); + p>0 && q>0; + p = q, q = nextPrime(p, R) + ) { + if (q-p < mn) { + mn = q-p; + ap = p; + aq = q; + } + + if (q-p > mx) { + mx = q-p; + bp = p; + bq = q; + } + } + + if (!mx) cout << "There are no adjacent primes.\n"; + else cout << ap << ',' << aq << " are closest, " + << bp << ',' << bq << " are most distant.\n"; + } +} diff --git a/uva_cpp_clean/10140/10140-8.cpp b/uva_cpp_clean/10140/10140-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7099b60babdf227957315331d6f8b94257010540 --- /dev/null +++ b/uva_cpp_clean/10140/10140-8.cpp @@ -0,0 +1,56 @@ +// Problem name: Prime Distance +// Problem link: https://vjudge.net/problem/UVA-10140 +// Submission link: https://vjudge.net/solution/32946410 + +#include + +using namespace std; + +typedef long long ll; + +vector get_primes(int n){ + vector small_prime(n + 1), primes; + for(int i = 2 ; i <= n ; ++i){ + if(small_prime[ i ] == 0) + small_prime[ i ] = i, primes.push_back(i); + for(int j = 0 ; j < primes.size() && primes[ j ] <= small_prime[ i ] && (ll)i * primes[ j ] <= n ; ++j) + small_prime[ i * primes[ j ] ] = primes[ j ]; + } + return primes; +} + +vector get_primes(int l, int r){ + int lim = sqrt(r); + vector primes_up_to_lim = get_primes(lim); + vector is_prime(r - l + 1, 1); + for(int p : primes_up_to_lim) + for(ll i = max((ll)p * p, ((ll)l + p - 1) / p * p) ; i <= r ; i += p) + is_prime[ i - l ] = 0; + if(l == 1) + is_prime[ 0 ] = false; + vector ans(4, -1); + for(int i = 0, last = -1 ; i < is_prime.size() ; ++i) + if(is_prime[ i ]){ + if(last > -1){ + if(ans[ 0 ] == -1 || i - last < ans[ 1 ] - ans[ 0 ]) + ans[ 0 ] = l + last, ans[ 1 ] = l + i; + if(ans[ 2 ] == -1 || i - last > ans[ 3 ] - ans[ 2 ]) + ans[ 2 ] = l + last, ans[ 3 ] = l + i; + } + last = i; + } + return ans; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + for(int l, r ; cin >> l >> r ; ){ + vector ans = get_primes(l, r); + if(ans[ 0 ] == -1) + cout << "There are no adjacent primes." << endl; + else + cout << ans[ 0 ] << ',' << ans[ 1 ] << " are closest, " << ans[ 2 ] << ',' << ans[ 3 ] << " are most distant." << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10140/10140-9.cpp b/uva_cpp_clean/10140/10140-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69a3d9101bccff3cd330d1f7b8c89747abebfbb5 --- /dev/null +++ b/uva_cpp_clean/10140/10140-9.cpp @@ -0,0 +1,68 @@ +#include + +#define SIZE 100010 + +using namespace std; + +bool is_prime[SIZE]; +vectorprime; +unsigned int L, U; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++) + if(is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +bool isPrime(unsigned int num){ + if(num < SIZE) return is_prime[num]; + for(int it = 0; it < prime.size(); it++) + if( num % prime[it] == 0) return false; + return true; +} + +int main(){ + sieve(); + while(~scanf("%u %u",&L, &U)){ + bool comp[SIZE * 10]; + memset(comp, false, sizeof comp); + if(L==1) L++; + for(int it1 = 0; it1 < prime.size() and (long long) prime[it1] * prime[it1] <= U; it1++){ + long long it = L % prime[it1] == 0 ? L : L + ( prime[it1] - L % prime[it1] ); + if(isPrime(it)) it <<= 1; + for(long long it2 = it; it2 <= U; it2 += prime[it1]) + comp[it2-L] = true; + } + bool foundA = false, foundB = false; + unsigned int a, b, A, B, pib, _min = INT_MAX, _max = 0; + for(unsigned int it = L; it <= U; it++){ + if(comp[it-L]==false){ + if(foundA==false){ + foundA = true; + } + else{ + foundB = true; + if(it - pib < _min){ + _min = it - pib; + a = pib, b = it; + } + if(it - pib > _max){ + _max = it - pib; + A = pib, B = it; + } + } + pib = it; + } + } + if(foundA and foundB) + printf("%u,%u are closest, %u,%u are most distant.\n",a, b, A, B); + else + puts("There are no adjacent primes."); + } + return(0); +} diff --git a/uva_cpp_clean/10141/10141-21.cpp b/uva_cpp_clean/10141/10141-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfa8553cbea2411b5848ea62a08ae63bd8485334 --- /dev/null +++ b/uva_cpp_clean/10141/10141-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10141 + Name: Request for Proposal + Problem: https://onlinejudge.org/external/101/10141.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MAXN 10000 +#define MAXL 200 +char tl[MAXL]; +char names[MAXN][MAXL]; +double p[MAXN]; +int fc[MAXN]; + +int main(){ + int cse = 1, n, f; + bool f1 = true; + while (cin>>f>>n && (f||n)) { + for (int i=0; i<=f; i++) + cin.getline(tl, MAXL); + + int maxf = 0; + for (int i=0; i> p[i] >> fc[i]; + if (fc[i] > maxf) maxf=fc[i]; + for (int j=0; j<=fc[i]; j++) + cin.getline(tl, MAXL); + } + + int si=-1; + for (int i=0; i // i/o functions, printf scanf +#include // string class +#include // cin for strings; +using namespace std ; + +int main () { + // ! Input will contain more than one test cases ! + // Input style : int R (number of reqs) N (number of proposals), then prop-specific + + int N, R; + // Outputed cases shall be numbered: + int rfpNo = 1; + + // Functional variables; + int index, // Will store the position of the ideal proposal + compliance; // Will store the compliance of the ideal proposal (used in comparisons) + + // string temp is simply a variable to save space + string temp; + + + while ( scanf ("%d %d", &R, &N) == 2 ){ + if ( R == 0 ) return 0; + // Get rid of the unnesecary "string form" of the requirements + for (int i = 0; i < R; i++) + getline ( cin, temp ); + // Index and compliance will explicitly be 0 at the beggining of each test case + index = 0; + compliance = 0; + + // Initializing our variables + int requirements [N] ; + double price [N] ; + string names [N]; + + // Case specific code block begins here: + for ( int l = 0; l < N; l++ ) { + cin >> names[l]; + scanf ( "%lf %d" , &price[l], &requirements[l] ); + // Get rid of the reqs: + for ( int p = 0; p < requirements[l]; p++ ) + getline ( cin, temp ); + // Checking eligibility : + if ( requirements[l] >= compliance ){ + if ( requirements[l] > compliance ) + index = l; + else if ( requirements[l] == compliance ) + if ( price[l] < price[index] ) + index = l; + } + + } + + printf ( "%s%d\n", "RFP #", rfpNo ); + cout << names[index] << "\n\n" ; + rfpNo++; + } +} diff --git a/uva_cpp_clean/10141/10141-6.cpp b/uva_cpp_clean/10141/10141-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b37f9943a6b5d9573ef41866416c5a859f10da3 --- /dev/null +++ b/uva_cpp_clean/10141/10141-6.cpp @@ -0,0 +1,54 @@ +/* + ad-hoc + difficulty: easy + date: 17/Feb/2020 + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +struct T { + int idx; + string name; + double compliance, price; + T() {} +}; + +// a < b +bool cmp(const T &a, const T &b) { + if (a.compliance != b.compliance) + return a.compliance > b.compliance; + if (a.price != b.price) + return a.price < b.price; + return a.idx < b.idx; +} + +int main() { + int t = 1; + while (true) { + int R, P; cin >> R >> P >> ws; if (!R && !P) break; + vector requeriments(R); + for (string &r : requeriments) getline(cin, r); + + vector proposals(P); int p = 0; + for (T &proposal : proposals) { + proposal.idx = p++; + + getline(cin, proposal.name); + double d, rMet; cin >> d >> rMet >> ws; + + proposal.price = d; + proposal.compliance = (double)rMet/R; + + string trash; + for (int r = 0; r < rMet; r++) getline(cin, trash); + } + sort(proposals.begin(), proposals.end(), cmp); + + if (t > 1) cout << endl; + printf("RFP #%d\n%s\n", t++, proposals[0].name.c_str()); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10141/main.cpp b/uva_cpp_clean/10141/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5243aabe6b3da45cd7713bc91a20a310d38e095e --- /dev/null +++ b/uva_cpp_clean/10141/main.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +using namespace std; + +int main(){ + int requirements, cases, actual_req, better_req; + double money, better_money; + string req, name, winner; + int num_case = 1; + while(cin >> requirements >> cases && requirements != 0 && cases != 0){ + if(num_case != 1) cout << endl; + cin.ignore(); + better_money = INT_MAX; + better_req = 0; + for(int i = 0; i < requirements; i++){ + getline(cin, req); + } + for(int i= 0; i < cases; i++){ + getline(cin, name); + cin >> money >> actual_req; + if(actual_req > better_req){ + winner = name; + better_req = actual_req; + better_money = money; + }else if(actual_req == better_req && money < better_money){ + winner = name; + better_money = money; + } + cin.ignore(); + for(int c = 0; c < actual_req; c++) getline(cin, req); + } + cout << "RFP #" << num_case << endl << winner << endl; + num_case++; + } + +return 0; +} diff --git a/uva_cpp_clean/10147/10147-21.cpp b/uva_cpp_clean/10147/10147-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..015eb3b8d9b91677e93f39df2245c22521850ba9 --- /dev/null +++ b/uva_cpp_clean/10147/10147-21.cpp @@ -0,0 +1,92 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10147 + Name: Highways + Problem: https://onlinejudge.org/external/101/10147.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 1073741824 +#define MAXN 760 +short gid[MAXN]; +short find(short u) { + return gid[u]==u ? u : gid[u]=find(gid[u]); +} +void join(short u, short v) { gid[find(u)] = find(v); } + + +int X[MAXN], Y[MAXN]; + +struct Edge { + int w; + short u, v; + Edge(){} + Edge(short u, short v):u(u),v(v) { + int xx = X[u]-X[v], yy = Y[u]-Y[v]; + w = xx*xx + yy*yy; + } + bool operator < (const Edge &e) const { return w > e.w; } +}; + +Edge closest[MAXN]; +int main(){ + int T, n, m, u, v; + scanf("%d", &T); + while (T--) { + scanf("%d", &n); + for (int i=0; i q; + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 1000010; + +struct Edge_{ + int u, v, w; + Edge_() {}; + Edge_(int u_, int v_, int w_){ + u = u_, v = v_, w = w_; + } + bool operator < (const Edge_& other) const{ + return ( w < other.w ); + } +}E[SIZE]; + +struct Point_{ + int x, y; +}P[SIZE]; + + +int tc, N, M; + +inline int pow2(int P_){ + return P_ * P_; +} + +inline int dis(int i, int j){ + return pow2(P[i].x - P[j].x) + pow2(P[i].y - P[j].y); +} + +void Kruskal(){ + UnionFind UF(N); + int setSize = 1; + for(int it = 0; it < M; it++) + if( not UF.isSameSet(E[it].u - 1, E[it].v - 1) ) + setSize++, UF.unionSet(E[it].u - 1, E[it].v - 1); + if( setSize == N ){ + puts("No new highways need"); + return; + } + M = 0; + for(int i = 0; i < N; i++) + for(int j = i + 1; j < N; j++) + E[M++] = Edge_(i, j, dis(i, j)); + sort(E, E + M); + for(int it = 0; it < M and setSize != N; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ){ + printf("%d %d\n", E[it].u + 1, E[it].v + 1); + setSize++; + UF.unionSet(E[it].u, E[it].v); + } +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &N); + for(int it = 0; it < N; it++) + scanf("%d %d", &P[it].x, &P[it].y); + scanf("%d", &M); + for(int it = 0; it < M; it++) + scanf("%d %d", &E[it].u, &E[it].v); + Kruskal(); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10150/10150-21.cpp b/uva_cpp_clean/10150/10150-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee700bfca3ecd79cc64f5f812078ddc7a0e89a52 --- /dev/null +++ b/uva_cpp_clean/10150/10150-21.cpp @@ -0,0 +1,140 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10150 + Name: Doublets + Problem: https://onlinejudge.org/external/101/10150.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline int readLine(char *str) { + int len=0; + char ch; + while ((ch=readchar())!='\n' && ch!=EOF) + str[len++] = ch; + + str[len] = 0; + if (ch == EOF && !len) return EOF; + return len; +} + +inline bool readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return 0; + + *str++ = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + *str++ = ch; + *str = 0; + return 1; +} + +// -------------------------------------------------------------------------- + +int seen[25144], pre[25144]; +vector adj[25144]; +char words[25144][17]; + +int S[402304][26], SW[402304], scnt; +int findW(char s[], int cur=0) { + for (int i=0; s[i]; ++i) { + char chi = s[i]-'a'; + + if (!S[cur][chi]) + return 0; + cur = S[cur][chi]; + } + return SW[cur]; +} + +void insertW(int wi) { + char *s = words[wi]; + int cur = 0; + for (int i=0; s[i]; ++i) { + char chi = s[i]-'a'; + + for (int j=0; j<26; ++j) + if (chi != j && S[cur][j]) { + int wj = findW(s+i+1, S[cur][j]); + if (wj > 0) { + adj[wi].push_back(wj); + adj[wj].push_back(wi); + } + } + + if (!S[cur][chi]) + S[cur][chi] = ++scnt; + cur = S[cur][chi]; + } + SW[cur] = wi; +} + + +void printPath(int src, int dst) { + if (src == dst) { + cout << words[src] << endl; + return; + } + + printPath(src, pre[dst]); + cout << words[dst] << endl; +} + + +int main() { + for (int i=1; readLine(words[i]); ++i) + insertW(i); + + char s1[20], s2[20]; + for (int cse=1; readStr(s1) && readStr(s2); ++cse) { + if (cse > 1) cout << endl; + + int src = findW(s1), + dst = findW(s2); + + if (src && dst) { + seen[src] = cse; + queue q; + q.push(src); + while (!q.empty()) { + int u = q.front(); q.pop(); + for (int v: adj[u]) + if (seen[v]!=cse) { + seen[v] =cse; + pre[v] = u; + + if (v == dst) { + printPath(src, dst); + goto fin; + } + q.push(v); + } + } + } + cout << "No solution.\n"; + fin:; + } +} diff --git a/uva_cpp_clean/10152/10152-18.cpp b/uva_cpp_clean/10152/10152-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d5ff8f13bda0566e0c3980a8eb1131e09c78aec3 --- /dev/null +++ b/uva_cpp_clean/10152/10152-18.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +bool cmp(pair p1, pair p2){ + return p1.second > p2.second; +} + +int main(){ + int tc, n; + string desired; + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + getchar(); + vector turtles(n); + map desMap; + for(int i = 0; i < n; i++) getline(cin, turtles[i]); + for(int i = 0; i < n; i++){ + getline(cin, desired); + desMap[desired] = i; + } + vector> ans; + for(int i = n-1, j = n-1; i >= 0; i--){ + if(desMap[turtles[i]] != j) ans.push_back(make_pair(turtles[i], desMap[turtles[i]])); + else j--; + } + sort(ans.begin(), ans.end(), cmp); + for(int i = 0; i < ans.size(); i++) cout< +#include +#include +#include +#include +using namespace std; + +typedef pair is; + +int main() { + int T; + cin >> T; + vector st, st2; + vector res; + unordered_map m; + while (T--) { + int n; + cin >> n; + st.clear(); + st2.clear(); + m.clear(); + res.clear(); + string s; + getline(cin, s); + for (int i = 0; i < n; i++) { + getline(cin, s); + st.push_back(s); + } + for (int i = 0; i < n; i++) { + getline(cin, s); + m[s] = i; + st2.push_back(s); + } + for (int i = n - 1, j = n - 1; i >= 0 && j >= 0; i--) { + for (; j >= 0; j--) { + if (st2[i] == st[j]) break; + else res.push_back(is(m[st[j]], st[j])); + } + j--; + } + sort(res.begin(), res.end()); + for (int i = res.size()-1; i >= 0; i--) { + printf("%s\n", res[i].second.c_str()); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10154/10154-21.cpp b/uva_cpp_clean/10154/10154-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e93c3247b35857bc428ea5b85bc4d9d07d84f15e --- /dev/null +++ b/uva_cpp_clean/10154/10154-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10154 + Name: Weights and Measures + Problem: https://onlinejudge.org/external/101/10154.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + + +int n = 0; +struct Turtle { + int w, s; + bool operator < (const Turtle& o) const { + return s != o.s ? s < o.s : w < o.w; + } +} +X[6000]; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> X[n].w >> X[n].s) + if (X[n].w <= X[n].s) + ++n; + + sort(X, X+n); + + int dp[6000] = {}, c, lc=1; + for (c=1; c<=n; ++c) + dp[c] = INF; + + for (int i=0; i0; --c) + if (dp[c-1]+X[i].w <= X[i].s) { + dp[c] = min(dp[c], dp[c-1]+X[i].w); + lc = max(lc, 1+c); + } + + for (c=n; c>=0; --c) + if (dp[c] != INF) + break; + + cout << c << endl; +} diff --git a/uva_cpp_clean/10156/10156-15.cpp b/uva_cpp_clean/10156/10156-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a54ba9270865cd2bdf1a9876a07ef9903f7b6b4 --- /dev/null +++ b/uva_cpp_clean/10156/10156-15.cpp @@ -0,0 +1,80 @@ +/* + 模擬,注意輸出格式 +*/ +#include +#include +#include +#include + +using namespace std; + +class position { + public: + int x, y; +}; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int row, column, n_turtles, move; + + while (cin >> row >> column >> n_turtles >> move) { + unordered_map turtle_pos; + vector> pond(row, vector(column, false)); + int id, x, y; + string pos; + + for (int i = 0; i < n_turtles; i++) { + cin >> id >> x >> y; + turtle_pos[id].x = x; + turtle_pos[id].y = y; + pond[x][y] = true; + } + + for (int i = 0; i < move; i++) { + cin >> id >> pos; + position &turtle = turtle_pos[id]; + x = turtle.x; + y = turtle.y; + + if (pos == "N") + x--; + else if (pos == "S") + x++; + else if (pos == "E") + y++; + else if (pos == "W") + y--; + else if (pos == "NE") + x--, y++; + else if (pos == "NW") + x--, y--; + else if (pos == "SE") + x++, y++; + else + x++, y--; + + if (x > -1 and y > -1 and x < row and y < column and !pond[x][y]) { + pond[turtle.x][turtle.y] = false; + turtle.x = x; + turtle.y = y; + pond[x][y] = true; + } + } + + for (int i = 0; i < row; i++) { + int j = column - 1; + while (j > -1 and !pond[i][j]) + j--; + + for (int k = 0; k <= j; k++) + if (pond[i][k]) + cout << '*'; + else + cout << ' '; + cout << '\n'; + } + cout << '\n'; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10158/10158-21.cpp b/uva_cpp_clean/10158/10158-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fae672d1a3c4cce9d58bc610072990da8588342b --- /dev/null +++ b/uva_cpp_clean/10158/10158-21.cpp @@ -0,0 +1,107 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10158 + Name: War + Problem: https://onlinejudge.org/external/101/10158.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +int readInt() { + char ch; + while (!isdigit(ch=getchar())); + int r = ch-'0'; + while (isdigit(ch=getchar())) + r = r*10 + ch-'0'; + return r; +} + + +#define MAXN 10017 +short fid[MAXN], eid[MAXN], tmp[MAXN]; +short findFid(short u) { + if (u<0 || fid[u]==u) return u; + + int i=0; + for (; u!=fid[u]; u=fid[u]) + tmp[i++] = u; + + for (int j=0; j +using namespace std; +using vi=vector; +int main(){ + int n,c,u,v; + cin>>n; + vi s=vi(n, 1),p=vi(n),e=vi(n,-1); + for(int i=0;ifind=[&](int i){ + if(i==p[i])return i; + return p[i]=find(p[i]); + }; + functionunite=[&](int i, int j){ + i=find(i); + j=find(j); + if(i==j)return; + if(s[i]>c>>u>>v; + if(!c)break; + if(c==1){ + int a=find(u),b=find(v); + if(e[a]==b){ + cout<<"-1\n"; + continue; + } + if(a==b)continue; + unite(a,b); + int p=find(a); + if(e[a]!=-1&&e[b]!=-1){ + if(e[a]!=e[b]){ + unite(e[a],e[b]); + int q=find(e[a]); + e[e[a]]=e[e[b]]=p; + e[a]=e[b]=q; + } + }else if(e[a]!=-1){ + e[e[a]]=p; + e[p]=e[a]; + }else if(e[b]!=-1){ + e[e[b]]=p; + e[p]=e[b]; + } + }else if(c==2){ + int a=find(u),b=find(v); + if(a==b){ + cout<<"-1\n"; + continue; + } + if(e[a]==b)continue; + if(e[a]!=-1)unite(e[a],b); + if(e[b]!=-1)unite(e[b],a); + a=find(a);b=find(b); + e[a]=b;e[b]=a; + }else if(c==3)cout<<(find(u)==find(v))<<"\n"; + else cout<<(e[find(u)]==find(v))<<"\n"; + } +} diff --git a/uva_cpp_clean/10158/10158-9.cpp b/uva_cpp_clean/10158/10158-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0eeb6b4062f0bd3e314d4259caeaa1c00ae3251 --- /dev/null +++ b/uva_cpp_clean/10158/10158-9.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +struct UnionFind{ + int numSets; + vector path, rank, setSize; + + UnionFind(int N_): numSets(N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + setSize.assign(N_, 1); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + numSets--; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x, setSize[x] += setSize[y]; + else path[x] = y, setSize[y] += setSize[x]; + if( rank[x] == rank[y] ) rank[y]++; + } + + int sizeOfSet(int idx){ + return setSize[ findSet(idx) ]; + } +}; + +int n, c, x, y; + +inline int e(int id){ + return id + n; +} + +int main(){ + scanf("%d", &n); + UnionFind UF(2 * n); + while(scanf("%d %d %d",&c, &x, &y), c | x | y){ + if( c == 1 ){ + if( UF.isSameSet(x, e(y)) ) puts("-1"); + else UF.unionSet(x, y), UF.unionSet( e(x), e(y) ); + } + else if( c == 2 ){ + if( UF.isSameSet(x, y) ) puts("-1"); + else UF.unionSet(x, e(y)), UF.unionSet( e(x), y); + } + else if( c == 3 ) printf("%d\n", UF.isSameSet(x, y)); + else printf("%d\n", UF.isSameSet(x, e(y))); + } + return(0); +} diff --git a/uva_cpp_clean/10161/10161-19.cpp b/uva_cpp_clean/10161/10161-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cfedb05080835ac16f4d2f0efc71b58d7364760 --- /dev/null +++ b/uva_cpp_clean/10161/10161-19.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +using namespace std; + +int main() { + int n, x, y; + while (scanf("%d", &n) && n != 0) { + int s = ceil(sqrt(n)); + int pos = n - (s-1)*(s-1); + int lim = s*s - (s-1)*(s-1); + if (s % 2 == 0) { + x = pos > lim/2? s: pos, y = pos > lim/2? lim - pos + 1: s; + } else { + y = pos > lim/2? s: pos, x = pos > lim/2? lim - pos + 1: s; + } + printf("%d %d\n", x, y); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10161/10161-20.cpp b/uva_cpp_clean/10161/10161-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..980814f911da93538bb16d5874f59738509048b2 --- /dev/null +++ b/uva_cpp_clean/10161/10161-20.cpp @@ -0,0 +1,21 @@ +#include +#include + +int main() +{ + long long N; + + while( scanf("%lld", &N)!=EOF && N ) + { + long long x = ceil(sqrt(N)), y = ceil(sqrt(N)); + + long long dis = N-((x*x)-x+1); + if( x%2==0 ) dis = -dis; + + if( dis>0 ) x -= dis; + if( dis<0 ) y += dis; + + printf("%lld %lld\n", x, y); + } + return 0; +} diff --git a/uva_cpp_clean/10161/10161-21.cpp b/uva_cpp_clean/10161/10161-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2192c81503db613df9c0d1c67b08f56e621491c --- /dev/null +++ b/uva_cpp_clean/10161/10161-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10161 + Name: Ant on a Chessboard + Problem: https://onlinejudge.org/external/101/10161.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + long long int n, sq; + while(cin>>n && n) { + sq = ceil(sqrt((long double)n)); + int x, y, d = sq*sq - n; + + if (d < sq) x=sq, y=d+1; + else y=sq, x=2*sq-d-1; + printf("%d %d\n", (sq&1)?y:x, (sq&1)?x:y); + } +} diff --git a/uva_cpp_clean/10161/10161-6.cpp b/uva_cpp_clean/10161/10161-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1827e0ad82956f28b92e9dbf82039186a6c2a1b3 --- /dev/null +++ b/uva_cpp_clean/10161/10161-6.cpp @@ -0,0 +1,32 @@ +/* + math > ad-hoc > finding pattern + difficulty: medium + date: 15/Feb/2020 + by: @brpapa +*/ +#include +#include +using namespace std; + +int main() { + while (true) { + int n; cin >> n; if (!n) break; + + int cs = (int)ceil(sqrt((double)n)); + int rs = (int)round(sqrt((double)n)); + + int l, c; + if (cs == rs) { + l = cs; + c = cs*cs-n+1; + } + else { + c = cs; + l = n-(cs-1)*(cs-1); + } + + if (cs % 2) swap(l, c); + cout << l << " " << c << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10162/10162-21.cpp b/uva_cpp_clean/10162/10162-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c36cdfb4aa65d7a9e63de71210e713ff2e38474 --- /dev/null +++ b/uva_cpp_clean/10162/10162-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10162 + Name: Last Digit + Problem: https://onlinejudge.org/external/101/10162.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int powmod(int a, int b, int c) { + if (!b) return 1; + int d = powmod(a, b>>1, c) % c; + d = (d*d) % c; + if (b&1) d *= a; + return d%c; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[201] = {}, n; + for (int i=1; i<=200; ++i) + X[i] = (X[i-1] + powmod(i, i, 10)) % 10; + + string s; + while (cin >> s && s!="0") { + if (s.length() > 3) + s = s.substr(s.length()-3); + + stringstream sin(s); sin >> n; + cout << X[n % 200] << endl; + } +} diff --git a/uva_cpp_clean/10162/10162-9.cpp b/uva_cpp_clean/10162/10162-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5c6e43153696fba1fb9e4f9d4b9de5eee2ab367 --- /dev/null +++ b/uva_cpp_clean/10162/10162-9.cpp @@ -0,0 +1,20 @@ +def main(): + + def f(n): + return sum(k ** k for k in range(1, n + 1)) % 10 + + def CycleFinding(): + lambda_ = 1 + while any(f(i) != f(i + lambda_) for i in range(1, lambda_ + 1)): + lambda_ += 1 + return lambda_ + + MOD = CycleFinding() + while True: + N = int(input()) + if N == 0: + break + print(f(N % MOD)) + +if __name__=='__main__': + main() diff --git a/uva_cpp_clean/10165/10165-14.cpp b/uva_cpp_clean/10165/10165-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..eebcb6a9497b60c19a536ac29a7c59f65836baac --- /dev/null +++ b/uva_cpp_clean/10165/10165-14.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; +typedef long long ll; +int main() +{ + ll n,x,sum; + while(scanf("%lld", &n) == 1 && n!=0){ + sum = 0; + for(int i = 1; i<=n ; i++){ + scanf("%lld", &x); + sum^=x; + printf("%lld ", sum); + } + if(sum){ + printf("Yes\n"); + } + else{ + printf("No\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/10165/10165-21.cpp b/uva_cpp_clean/10165/10165-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22ece9a0c9f5ac842765ac712b8d64da73fdeb68 --- /dev/null +++ b/uva_cpp_clean/10165/10165-21.cpp @@ -0,0 +1,26 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10165 + Name: Stone Game + Problem: https://onlinejudge.org/external/101/10165.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + int n, x; + while (cin >> n && n>0) { + int s = 0; + for (int i=0; i> x; + s ^= x; + } + cout << (s ? "Yes\n" : "No\n"); + } +} diff --git a/uva_cpp_clean/10165/10165-9.cpp b/uva_cpp_clean/10165/10165-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05b757e954e40b7aed833ec5af713d608c8a8a51 --- /dev/null +++ b/uva_cpp_clean/10165/10165-9.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int n, num, sum; + +int main() { + while (scanf("%d", &n), n) { + sum = 0; + for (int i = 0; i < n; i++) { + scanf("%d", &num); + sum ^= num; + } + puts(sum == 0 ? "No" : "Yes"); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10167/10167-21.cpp b/uva_cpp_clean/10167/10167-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ed1152fc2122f6d7a2e66f5902ddb9cd10b113a --- /dev/null +++ b/uva_cpp_clean/10167/10167-21.cpp @@ -0,0 +1,84 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10167 + Name: Birthday Cake + Problem: https://onlinejudge.org/external/101/10167.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define PI 3.141592654 +#define EPS 1e-9 +#define K 10 +using namespace std; + + +double ang(double y, double x) { + double t = atan2(y, x); + return t < 0 ? t+2*PI : t; +} + +int n; +double D[143]; +int count(double d) { + int cnt = 0; + for (int i=0; i= d && D[i] <= d+PI) { + if (fabs(D[i]-d) < EPS || fabs(D[i]-(d+PI)) < EPS) + return 0; + cnt++; + } + return cnt; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int x, y, cse=1; cin >> n && n; ++cse) { + n *= 2; + for (int i=0; i> x >> y; + D[i] = ang(y, x); + } + + for (int r=1; r<=500; ++r) + for (int i=0; i PI ? D[i]-PI : D[i])); + x = round(r * cos(D[i])); + double d = ang(y, x); + if ((x||y) && count(d)*2 == n) { + cout << -y << ' ' << x << endl; + goto fin; + } + } + + for (int i=0; i PI ? D[i]-PI : D[i]); + x = (500-K) * cos(D[i]); + for (int xx=x-K; xx<=x+K; xx++) + for (int yy=y-K; yy<=y+K; yy++) { + double d = ang(yy, xx); + if ((xx||yy) && count(d)*2 == n) { + int g = __gcd(yy, xx); + cout << -yy/g << ' ' << xx/g << endl; + goto fin; + } + } + } + + // Complete BruteForce + for (int x=-500; x<=500; ++x) + for (int y=0; y<=500; ++y) if(x||y) { + double d = ang(y, x); + if (count(d)*2 == n) { + cout << -y << ' ' << x << endl; + goto fin; + } + } + fin:; + } +} diff --git a/uva_cpp_clean/10168/10168-19.cpp b/uva_cpp_clean/10168/10168-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e622bbf5d9d167b62fa238012591cd5f30a07e68 --- /dev/null +++ b/uva_cpp_clean/10168/10168-19.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +inline void sieve(ll upperbound = 10000000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline ii check(int n) { + for (int i = 0; i < primes.size(); i++) { + if (n - primes[i] < primes[i]) break; + if (bs[n - primes[i]]) return ii(primes[i], n - primes[i]); + } + return ii(0, 0); +} + +int main() { + sieve(); + int n; + while (cin >> n) { + bool ok = false; + for (int i = 4; !ok; i++) { + int j = n - i; + if (j < i) break; + ii p1 = check(i), p2 = check(j); + if (p1.first != 0 && p2.first != 0) { + ok = true; + printf("%d %d %d %d\n", p1.first, p1.second, p2.first, p2.second); + } + } + if (!ok) printf("Impossible.\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10168/10168-2.cpp b/uva_cpp_clean/10168/10168-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81a93c2fa448646a6b06243327fbdb24da9bb4de --- /dev/null +++ b/uva_cpp_clean/10168/10168-2.cpp @@ -0,0 +1,61 @@ +#include "bits/stdc++.h" + +#define el "\n" +#define all(v) ((v).begin()),((v).end()) +#define rall(v) ((v).rbegin()),((v).rend()) + +using namespace std; +typedef unsigned long long ull; +typedef long long ll; +typedef vector vi; +typedef vector vll; +const int OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +const int M = 20000010; +bitset<20000010> bs; +pair solve(int x) { + int a, b; + for (int i = 2; i <= x / 2; i++) { + if (bs[i] && bs[x - i]) { + a = i; + b = x - i; + return {a, b}; + } + } + return {0,0}; +} +int main() { + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + ios::sync_with_stdio(false); + cin.tie(0); + int n; + bs.set(); + bs[0] = bs[1] = 0; + for (ull i = 2; i <= M; ++i) if (bs[i]) { + for (ull j = i*i; j <= M; j +=i) bs[j] = 0; + } + while (cin >> n) { + if (n <= 7) { + cout << "Impossible.\n"; + continue; + } + if (n&1) { + auto s = solve(n - 5); + cout << "2 3 " << s.first << ' ' << s.second << el; + } else { + auto s = solve(n - 4); + cout << "2 2 " << s.first << ' ' << s.second << el; + } + } + return 0; +} + +/* + + +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10168/10168-20.cpp b/uva_cpp_clean/10168/10168-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a85f3af893f63cdd476dfc9359994b45bf7362ba --- /dev/null +++ b/uva_cpp_clean/10168/10168-20.cpp @@ -0,0 +1,40 @@ +#include + +bool prime[10000005]; + +int main() +{ + prime[0] = prime[1] = false; + for(int i=2 ; i<10000005 ; i++) prime[i] = true; + + for(int i=2 ; i*i<10000005 ; i++) + if( prime[i] ) + for(int j=i+i ; j<10000005 ; j+=i) prime[j] = false; + + int N; + + while( scanf("%d", &N)==1 ) + { + if( N>=8 ) + { + if( N%2 ) + { + printf("2 3 "); + N -= 5; + } + else + { + printf("2 2 "); + N -= 4; + } + for(int i=2 ; i<=N ; i++) + if( prime[i] && prime[N-i] ) + { + printf("%d %d\n", i, N-i); + break; + } + } + else printf("Impossible.\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10168/10168-21.cpp b/uva_cpp_clean/10168/10168-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3943b27c6b7ea45824a1b4ee6fbd29b65cdaac8b --- /dev/null +++ b/uva_cpp_clean/10168/10168-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10168 + Name: Summation of Four Primes + Problem: https://onlinejudge.org/external/101/10168.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +#define MAXP 10000090 + +int cnt = 1; +int primes[670000] = {2}; + +unsigned int prime[MAXP / 64]; + +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) +void sieve() { + memset(prime, -1, sizeof(prime)); + + int i, sqrtN = sqrt((double)MAXP) + 1; + for (i = 3; i < sqrtN; i += 2 ) if(gP(i)) { + primes[cnt++] = i; + int i2 = i<<1; + for(int j = i * i; j < MAXP; j += i2) + prime[j>>6] &= ~(1<<((j>>1)&31)); + } + + for (; i2 && x&1 && (gP(x))); +} + + +int smallerize(int x) { + int xh = x>>1; + for (int i=0; primes[i]<=xh; i++) + if (isPrime(x - primes[i])) + return primes[i]; + + return -1; +} + + +int main(){ + sieve(); + + int n; + while (cin>>n) + if (n<8) cout<<"Impossible.\n"; + else { + int ind = lower_bound(primes, primes+cnt, n-6) - primes; + + int a = primes[ind] <= n-6 ? primes[ind] : primes[ind-1], + b = (n-a)&1 ? 3 : 2, + c = smallerize(n-a-b), + d = n-a-b-c; + + printf ("%d %d %d %d\n", a, b, c, d); + } +} diff --git a/uva_cpp_clean/10168/10168-5.cpp b/uva_cpp_clean/10168/10168-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..49e76d463ecf18a349c6702f2dc2e2d8970771d8 --- /dev/null +++ b/uva_cpp_clean/10168/10168-5.cpp @@ -0,0 +1,146 @@ +/****************************************************************** +*** Problewm : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d:",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) + + +#define ff first +#define se second +#define pb push_back +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +////============ CONSTANT ===============//// +#define mx (10000010) +#define Max 1000000 //infinity value +#define eps 1e-9 +#define mod 10007 +////=====================================//// + +bool prime[mx]; +int p[mx],k=1; +void sieve() +{ + prime[1]=false; + p[0]=2;oi + for(int i=2;i<=mx;i+=2) + prime[i]=true; + int n=sqrt(mx); + for(int i=3;i<=n;i=i+2) + { + if(!prime[i]) + { + p[k++]=i + for(int j=2*i;j<=mx;j+=i) + { + prime[j]=true; + } + } + } +} +void fourprime(int n) +{ + int a,i,f=0; + for(i=0;i + +using namespace std; + +typedef long long ll; + +vector get_primes(int n){ + vector small_prime(n + 1), primes; + for(int i = 2 ; i <= n ; ++i){ + if(small_prime[ i ] == 0) + small_prime[ i ] = i, primes.push_back(i); + for(int j = 0 ; j < primes.size() && primes[ j ] <= small_prime[ i ] && i * primes[ j ] <= n ; ++j) + small_prime[ i * primes[ j ] ] = primes[ j ]; + } + return primes; +} + +pair get_goldbach(const vector &primes, int n){ + for(int i = 0, j ; ; ++i){ + j = lower_bound(primes.begin() + i, primes.end(), n - primes[ i ]) - primes.begin(); + if(primes[ i ] + primes[ j ] == n) + return { primes[ i ], primes[ j ] }; + } +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int N = 10000000; + const vector primes = get_primes(N); + + for(int n ; cin >> n ; ){ + if(n <= 8) + cout << (n == 8 ? "2 2 2 2" : "Impossible.") << endl; + else{ + int lowest = 3 - (n & 1); + int highest = primes[ upper_bound(primes.begin(), primes.end(), n - lowest - 4) - primes.begin() - 1 ]; + pair goldbach = get_goldbach(primes, n - highest - lowest); + cout << lowest << ' ' << highest << ' ' << goldbach.first << ' ' << goldbach.second << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10168/10168-9.cpp b/uva_cpp_clean/10168/10168-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6592b31d95e13664bd86bf2eaab91b31001dee4a --- /dev/null +++ b/uva_cpp_clean/10168/10168-9.cpp @@ -0,0 +1,51 @@ +#include + +const long long LIM = (int)1e7+2; + +using namespace std; + +bitsetprime; +vectorp; +int n,pib; + +void sieve(){ + int crossLimit = sqrt(LIM); + prime[0] = prime[1] = 1; + for(int i=4;i<=LIM;i+=2) + prime[i] = 1; + p.push_back(2); + for(long long i=3;i + +using namespace std; + +inline long long calculate_sum(const long long& start, const long long& x) { + return (x * (x + 1) - start * (start - 1)) / 2; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + long long n_day, start; + while (cin >> start >> n_day) { + long long left = 1, right = 1E8, mid = 5E7; + do { + if (calculate_sum(start, mid) < n_day) + left = mid + 1; + else + right = mid; + mid = (left + right - 1) / 2; + } while (left < right); + cout << right << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10170/10170-19.cpp b/uva_cpp_clean/10170/10170-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca69eaee6fc4debeea9852f091ab7be0876b0ce2 --- /dev/null +++ b/uva_cpp_clean/10170/10170-19.cpp @@ -0,0 +1,12 @@ +#include +#include +using namespace std; + +int main() { + long long S, D; + while (scanf("%lld %lld", &S, &D) != EOF) { + long long n = (-1+ceil(sqrt(1-4*(S-S*S-2*D)))); + printf("%lld\n", n/2 + n%2); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10170/10170-20.cpp b/uva_cpp_clean/10170/10170-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cc6f0e8e3e1652c0b1fb1621d88df1a1e2fe210 --- /dev/null +++ b/uva_cpp_clean/10170/10170-20.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +int main() +{ + long S, D; + + while( cin >> S >> D ) + { + long group = S, sum = 0; + while( true ) + { + sum += group; + if( sum>=D ) + { + cout << group << endl; + break; + } + group++; + } + } + return 0; +} diff --git a/uva_cpp_clean/10170/10170-21.cpp b/uva_cpp_clean/10170/10170-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2029d115deeb547348b438e082bf3a3ed47bfc8 --- /dev/null +++ b/uva_cpp_clean/10170/10170-21.cpp @@ -0,0 +1,24 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10170 + Name: The Hotel with Infinite Rooms + Problem: https://onlinejudge.org/external/101/10170.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + long long int a, n; + while (cin>>a>>n) { + double delta = 4*(a*a-a+2*n)+1; + printf("%d\n", (int) ceil((sqrt(delta)-1)/2)); + } +} diff --git a/uva_cpp_clean/10170/10170-5.cpp b/uva_cpp_clean/10170/10170-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..418c01d8c3fa4c72ac5b4b19f5f74442040746d7 --- /dev/null +++ b/uva_cpp_clean/10170/10170-5.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +ll day(ll s,ll d) +{ + ll sum=0,i; + for(i=s;;i++) + { + sum=sum+i; + if(sum>=d) + { + break; + } + } + return i; +} +int main() +{ + ll s,d; + while(sc("%lld %lld",&s,&d)==2) + { + pf("%d\n",day(s,d)); + } + return 0; +} diff --git a/uva_cpp_clean/10170/10170-9.cpp b/uva_cpp_clean/10170/10170-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73ad6170a662f1f622a5e9757f9b235585743d4d --- /dev/null +++ b/uva_cpp_clean/10170/10170-9.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +double S, D, V, ans; + +int main(){ + while(scanf("%lf %lf", &S, &D) == 2){ + V = D + (S - 1) * S / 2; + ans = ceil( (sqrt(8 * V + 1) + 1) / 2.0 ); + printf("%.0lf\n", ans - 1); + } + return(0); +} diff --git a/uva_cpp_clean/10171/10171-13.cpp b/uva_cpp_clean/10171/10171-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f393d583823c2bec6ac6b8da53045389b4d5c3b2 --- /dev/null +++ b/uva_cpp_clean/10171/10171-13.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 27 +#define INF 1e8 + 7 + +ofstream fout("f.out"); + +int mapY[MAX][MAX]; +int mapO[MAX][MAX]; + +void initilaize() +{ + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + mapY[i][j] = mapO[i][j] = (i == j) ? 0 : INF; + } + } +} + +void setMap(char a, char b, char x, char y, int w) +{ + if (a == 'Y') + { + if (b == 'U') + { + mapY[x - 'A'][y - 'A'] = w; + } + + else if (b == 'B') + { + mapY[x - 'A'][y - 'A'] = w; + mapY[y - 'A'][x - 'A'] = w; + } + } + + else if (a == 'M') + { + if (b == 'U') + { + mapO[x - 'A'][y - 'A'] = w; + } + + else if (b == 'B') + { + mapO[x - 'A'][y - 'A'] = w; + mapO[y - 'A'][x - 'A'] = w; + } + } +} + +int main() +{ + int n; + + while (cin >> n, n != 0) + { + int w; + char a, b, x, y; + + initilaize(); + + for (int i = 0; i < n; i++) + { + cin >> a >> b >> x >> y >> w; + + setMap(a, b, x, y, w); + } + + for (int i = 0; i < MAX; i++) + { + mapO[i][i] = mapY[i][i] = 0; + } + + for (int k = 0; k < MAX; k++) + { + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + mapY[i][j] = min(mapY[i][j], mapY[i][k] + mapY[k][j]); + mapO[i][j] = min(mapO[i][j], mapO[i][k] + mapO[k][j]); + } + } + } + + int ans = INF; + vector places; + + char s, e; + + cin >> s >> e; + + for (int i = 0; i < MAX; i++) + { + if (mapY[s - 'A'][i] != INF && mapO[e - 'A'][i] != INF) + { + if (mapY[s - 'A'][i] + mapO[e - 'A'][i] < ans) + { + ans = mapY[s - 'A'][i] + mapO[e - 'A'][i]; + } + } + } + + if (ans == INF) cout << "You will never meet." << endl; + + else + { + cout << ans; + + for (int i = 0; i < MAX; i++) + { + if (ans == mapY[s - 'A'][i] + mapO[e - 'A'][i]) + { + cout << " " << char(i + 'A'); + } + } + + cout << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10171/10171-21.cpp b/uva_cpp_clean/10171/10171-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ffc52fb87e0a8881042f139a55f5a075e5421a3 --- /dev/null +++ b/uva_cpp_clean/10171/10171-21.cpp @@ -0,0 +1,92 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10171 + Name: Meeting Prof. Miguel... + Problem: https://onlinejudge.org/external/101/10171.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +struct State { + int u, d; + State(int u, int d):u(u),d(d){} + bool operator < (const State &o) const { return d > o.d; } +}; + +int W1[26][26], W2[26][26], D1[26], D2[26]; +vector adj1[26], adj2[26]; + +void connect(vector adj[26], int W[][26], int x, int y, int d) { + adj[x].push_back(y); + W[x][y] = d; +} + +void dijkstra(int n, int src, const vector adj[26], int W[][26], int D[]) { + for (int i=0; i q; + q.push(State(src, D[src]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.u]) continue; + + for (int v: adj[s.u]) + if (s.d + W[s.u][v] < D[v]) + q.push(State(v, D[v]=s.d + W[s.u][v])); + } +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int m; + char src; + while (cin >> m && m) { + for (int i=0; i<26; ++i) { + adj1[i].clear(); + adj2[i].clear(); + } + + for (int i=0, d; i> t >> di >> x >> y >> d; + x -= 'A'; y -= 'A'; + + if (t == 'Y') connect(adj1, W1, x, y, d); + else connect(adj2, W2, x, y, d); + + if (di == 'B') + if (t == 'Y') connect(adj1, W1, y, x, d); + else connect(adj2, W2, y, x, d); + } + + cin >> src; + dijkstra(26, src-'A', adj1, W1, D1); + + cin >> src; + dijkstra(26, src-'A', adj2, W2, D2); + + int mnd = INF; + for (int i=0; i<26; ++i) + mnd = min(mnd, D1[i]+D2[i]); + + if (mnd == INF) + cout << "You will never meet.\n"; + + else { + cout << mnd; + for (int i=0; i<26; ++i) + if (D1[i]+D2[i] == mnd) + cout << ' ' << char(i+'A'); + cout << endl; + } + } +} diff --git a/uva_cpp_clean/10172/10172-14.cpp b/uva_cpp_clean/10172/10172-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d7d0753570704b43811f66fe830cb22c65ee632 --- /dev/null +++ b/uva_cpp_clean/10172/10172-14.cpp @@ -0,0 +1,214 @@ +/*************************************************** + * Problem Name : 10172 - The Lonesome Cargo Distributor.cpp + * Problem Link : https://onlinejudge.org/external/101/10172.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-03-28 + * Problem Type : STL + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +queueQ[105]; +bool check (int n) { + for (int i = 1; i <= n; i++) { + if (!Q[i].empty() ) return false; + } + + return true; +} +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + + while (tc--) { + int n, s_sz, q_sz; + cin >> n >> s_sz >> q_sz; + + for (int i = 1; i <= n; i++) { + int x; + cin >> x; + + while (x--) { + int c; + cin >> c; + Q[i].push (c); + } + } + + stackS; + int sum = 0; + int cur = 1; + + while (1) { + if (S.empty() && check (n) ) { + break; + } + + if (!S.empty() ) { + while (!S.empty() ) { + int x = S.top(); + + if (x == cur) { + S.pop(); + sum++; + + } else { + if ( (int) Q[cur].size() < q_sz) { + Q[cur].push (x); + S.pop(); + sum++; + + } else { + break; + } + } + } + + while (!Q[cur].empty() ) { + int x = Q[cur].front(); + + if ( (int) S.size() < s_sz) { + S.push (x); + Q[cur].pop(); + sum++; + + } else { + break; + } + } + + if (S.empty() && check (n) ) { + break; + } + + cur++; + sum += 2; + + if (cur > n) cur = 1; + + } else { + while (!Q[cur].empty() ) { + int x = Q[cur].front(); + + if ( (int) S.size() < s_sz) { + S.push (x); + Q[cur].pop(); + sum++; + + } else { + break; + } + } + + if (S.empty() && check (n) ) { + break; + } + + cur++; + sum += 2; + + if (cur > n) cur = 1; + } + } + + cout << sum << "\n"; + + for (int i = 1; i <= n; i++) { + while (!Q[i].empty() ) Q[i].pop(); + } + } + + return 0; +} + + diff --git a/uva_cpp_clean/10172/10172-21.cpp b/uva_cpp_clean/10172/10172-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..278677d0c953bc6fc0f2e255a50740f07ddb0adb --- /dev/null +++ b/uva_cpp_clean/10172/10172-21.cpp @@ -0,0 +1,106 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10172 + Name: The Lonesome Cargo Distributor + Problem: https://onlinejudge.org/external/101/10172.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if(p == end) { + if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +// ------------------------------------------------------------------ + +class FastQueue { + int data[128], l, r; + public: + FastQueue():l(0),r(0) {} + void clear() { l=r=0; } + bool empty() { return l==r; } + void push(int x) { data[r++] = x; r&=127; } + int pop(){ int result = data[l++]; l&=127; return result; } + int front() { return data[l]; } + int size() { return r>=l ? r-l : 128-l+r; } +}; + +class FastStack { + int data[128], sz; + public: + FastStack():sz(0){} + void clear() { sz=0; } + bool empty() { return !sz; } + void push(int x) { data[sz++] = x; } + int pop() { return data[--sz]; } + int top() { return data[sz-1]; } + int size() { return sz; } +}; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + FastQueue q[101]; + int T = readUInt(); + while (T--) { + int n = readUInt(), + maxs = readUInt(), + maxq = readUInt(); + + int total = 0; + for (int i=0; i +#include +#include + +using namespace std; + +stack myCar; +queue stationQ[100]; + +int main () +{ + // N: number of stations in the ring + // S: cargo capacity + // Q: maximum number of cargos B queue can accomodate + // cargos: number of cargos existing in each test case + // time: the result + int c, N, S, Q, cargos, time; + int location, temp; + + scanf("%d",&c); + + while ( c-- ) + { + scanf("%d %d %d", &N, &S, &Q); + cargos = 0; + + // Input procedure; + for ( int l = 0; l < N; l++ ) + { + // Checking how many cargos the station stores + scanf ("%d", &location ); + + // While the station has cargos + while ( location-- ) + { + cargos++ ; + scanf ("%d", &temp); + stationQ[l].push(temp-1); + } + } + + // From now on, location will be used to "mark" the current station + int location = 0; + time = 0; + + // This should be interpreted like: While there are cargos to move, proceed: + while ( cargos ) + { + // Check whether unload possible + while ( !myCar.empty() && (stationQ[location].size() < Q + || myCar.top() == location) ) + { + if ( myCar.top() != location) + stationQ[location].push( myCar.top() ); + else + cargos--; + time++; + myCar.pop(); + } + + // If there are any cargos in the station, pick as many as possible + while ( myCar.size() < S && !stationQ[location].empty() ) + { + myCar.push( stationQ[location].front() ); + stationQ[location].pop(); + time++; + } + + // Move to the next station: Distance covered in 2 minutes: + time += 2; + + if ( location == N-1 ) + location = 0; + else + location++ ; + } + // Printing two less due to the fact that the last addition doesn't count + printf("%d\n", time - 2) ; + } +} diff --git a/uva_cpp_clean/10176/10176-13.cpp b/uva_cpp_clean/10176/10176-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a4fe22c73abced5499c1b7e89e3959a923b57b5 --- /dev/null +++ b/uva_cpp_clean/10176/10176-13.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define m 131071 + +bool isOK(string s) +{ + int makeIt = 0; + bool ans = false; + + for (int i = (int)s.size() - 2; i >= 0 ; i--) + { + makeIt = (((makeIt % m) * 2) % m); + + if (s[i] == '1') makeIt = (((makeIt % m) + 1) % m); + } + + if (makeIt == 0) ans = true; + + return ans; +} + +int main() +{ + string s; + + while (cin >> s) + { + while (s[s.size() - 1] != '#') + { + string temp; + + cin >> temp; + + s += temp; + } + + isOK(s) ? cout << "YES" << endl : cout << "NO" << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10176/10176-21.cpp b/uva_cpp_clean/10176/10176-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2654747f226fc86db2e97a3e8131cede3a7e62e2 --- /dev/null +++ b/uva_cpp_clean/10176/10176-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10176 + Name: Ocean Deep! - Make it shallow!! + Problem: https://onlinejudge.org/external/101/10176.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + + +int main() { + char ch; + int d = 0; + while ((ch=readchar())!=EOF) { + if (ch == '#') { + cout << (d ? "NO\n" : "YES\n"); + d = 0; + } + else if (isdigit(ch)) + d = (d*2 + ch-'0') % 131071; + } +} diff --git a/uva_cpp_clean/10176/10176-6.cpp b/uva_cpp_clean/10176/10176-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3fbc2fc9cfc166cc8694f9ef6e67dab0784af49 --- /dev/null +++ b/uva_cpp_clean/10176/10176-6.cpp @@ -0,0 +1,43 @@ +/* + math > number theory > module arithmetic + difficulty: easy + date: 20/Mar/2020 + problem: is a given binary number of 100 digits divisible by 131071? + by: @brpapa +*/ +#include +#include +#define ll long long +#define MOD 131071 +using namespace std; + +int main() { + vector bin; + string str; + + while (cin >> str) { + bin.clear(); for (char c : str) if (c != '#') bin.push_back(c); + while (str.back() != '#') { + cin >> str; + for (char c : str) if (c != '#') bin.push_back(c); + }; + int N = bin.size(); + + // input em bin, de tamanho N + + ll sum = 0; + for (int i = 0; i < N; i++) { + if (bin[i] == '0') continue; + + // p = 2^(N-i-1) + ll p = 1; + int K = N-i-1; while (K--) p = (2*p)%MOD; + p %= MOD; + + sum = (sum+p)%MOD; + } + sum %= MOD; + cout << (sum == 0? "YES":"NO") << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10177/10177-18.cpp b/uva_cpp_clean/10177/10177-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cb06be7fdee1752729192760524ad8e834afd954 --- /dev/null +++ b/uva_cpp_clean/10177/10177-18.cpp @@ -0,0 +1,34 @@ +#include +#include + +using namespace std; + +int main(){ + unsigned long long s2[101], s3[101], s4[101], r2[101], r3[101], r4[101], n; + s2[0] = s3[0] = s4[0] = r2[0] = r3[0] = r4[0] = 0; + for(int i = 1; i <= 100; i++){ + s2[i] = s2[i - 1] + pow(i, 2); + s3[i] = s3[i - 1] + pow(i, 3); + s4[i] = s4[i - 1] + pow(i, 4); + r2[i] = pow(i * (i + 1) / 2, 2) - s2[i]; + r3[i] = pow(i * (i + 1) / 2, 3) - s3[i]; + r4[i] = pow(i * (i + 1) / 2, 4) - s4[i]; + } + while(scanf("%d", &n) == 1) printf("%llu %llu %llu %llu %llu %llu\n", s2[n],r2[n],s3[n],r3[n],s4[n],r4[n]); + return 0; +} +/* + -) Para hallar el patron de las figuras de igual dimension (cuadrados): + Ejemplo usado: En un cuadrado 1x1 solo tenemos un cuadrado, al aumentar a 2x2 obtendriamos 5 que puede ser + hallado multiplicando 2x2 y teniendo en cuenta el cuadrado grande, pero vendria siendo lo mismo que sumar el + cuadrado 1x1 anterior, ahora tengamos en cuenta uno 3x3, aqui seria 14 cuadrados que son 3x3 + 2x2 + 1x1 + que seria igual 9(lenght 1) + 4(lenght 2) + 1(lenght 3) y he aqui el patron y es que 2x2 + 1x1 viene + siendo los cuadrado 2x2, entonces la formula seria C[i] = C[i - 1] + pow(i, 2), para las demas dimensiones + es lo mismo por lo que llevaria a la formula general C[i] = C[i - 1] + pow(i, dimesion). + + -) Para hallar el patron de las figuras de diferente dimesion (rectangulos, GeeksforGeeks): + Ejemplo usado: En una cuadricula 1x1 hay 1 rectangulo de 1x1 (cuadrado), bien, si ahora usamos una de 2x1 + habrian 2 rectangulos 1x1(cuadrados) y uno 2x1 que es igual a 2 + 1 = 3, ahora si colocamos uno 3x1 seria + 3 + 2 + 1 = 6, vemos que se comporta como numeros triangulares para Nx1, entonces para cualquier dimension + seria: R[i] = pow(i * (i + 1) / 2, dimension) - C[i] (debido a que cuenta tambien los cuadrados). +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10177/10177-21.cpp b/uva_cpp_clean/10177/10177-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..969f97b76ea45f0c55c58f67a61efff44b9723f8 --- /dev/null +++ b/uva_cpp_clean/10177/10177-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10177 + Name: (2/3/4)-D Sqr/Rects/Cubes/Boxes? + Problem: https://onlinejudge.org/external/101/10177.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +typedef unsigned long long ULong; +ULong S2[102], S3[102], S4[102], C[102]; +int main(){ + for (int i=1; i<102; i++) { + ULong i2 = i*i; + S2[i] = S2[i-1] + i2; + S3[i] = S3[i-1] + i2*i; + S4[i] = S4[i-1] + i2*i2; + if (i>1) C[i] = i*(i-1)>>1; + } + + int n; + while (scanf("%d", &n)==1) { + ULong c=C[n+1], c2=c*c; + printf("%llu %llu %llu %llu %llu %llu\n", + S2[n], c2-S2[n], S3[n], c2*c-S3[n], S4[n], c2*c2-S4[n]); + } +} diff --git a/uva_cpp_clean/10177/10177-9.cpp b/uva_cpp_clean/10177/10177-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..582726c53a59f7a186c4a822d9115ff741b73f6d --- /dev/null +++ b/uva_cpp_clean/10177/10177-9.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +long long N, S2, R2, S3, R3, S4, R4; + +int main(){ + while(~scanf("%lld", &N)){ + S2 = N * ( N + 1 ) * ( 2 * N + 1 ) / 6; + R2 = N * ( N - 1 ) * ( N + 1 ) * ( 3 * N + 2 ) / 12; + S3 = ( N * ( N + 1 ) / 2 ) * ( N * ( N + 1 ) / 2 ); + R3 = ( ( N - 1 ) * N / 2 ) * ( N * ( N + 1 ) / 2 ) * ( ( N + 1 ) * ( N + 2 ) / 2 ); + S4 = N * ( N + 1 ) * ( 2 * N + 1 ) * ( 3 * N * N + 3 * N - 1 ) / 30; + R4 = S3 * S3 - S4; + printf("%lld %lld %lld %lld %lld %lld\n", S2, R2, S3, R3, S4, R4); + } + return(0); +} diff --git a/uva_cpp_clean/10178/10178-19.cpp b/uva_cpp_clean/10178/10178-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f3eed705167401fb900b7f6256f0f8fb71c92d7 --- /dev/null +++ b/uva_cpp_clean/10178/10178-19.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + void clear(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +ufds ds; + +int main() { + int N, E; + while (cin >> N >> E) { + ds.clear(256); + for (int i = 0; i < E; i++) { + char n1, n2; + cin >> n1 >> n2; + ds.unionSet(n1, n2); + } + printf("%d\n", - 255 + ds.numSets + E); + } + return 0; +} diff --git a/uva_cpp_clean/10178/10178-6.cc b/uva_cpp_clean/10178/10178-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..6e5fb75fc048cf3819e0bb1c234d6f3a636d0fce --- /dev/null +++ b/uva_cpp_clean/10178/10178-6.cc @@ -0,0 +1,43 @@ +// https://uva.onlinejudge.org/external/101/10178.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int n,m; + while(cin>>n>>m){ + vi s(128,-1); + vvi g(n); + char a,b; + int c=0; + for(int i=0;i>a>>b; + if(s[a]==-1)s[a]=c++; + if(s[b]==-1)s[b]=c++; + g[s[a]].push_back(s[b]); + g[s[b]].push_back(s[a]); + } + vi t(n); + int x,y; + functiondfs=[&](int u,int k){ + t[u]=k; + x++; + for(int v:g[u]){ + y++; + if(!t[v]) + dfs(v,k); + } + }; + int z=0; + for(int i=0,k=1;i +#include +#include +#include + +using namespace std; + +#define MAX 10000010 + +typedef long long ll; +typedef vector vll; +typedef vector vb; + +ll n; +vb isPrime(MAX, true); +vll primes; + +void findPrimes() +{ + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; i++) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += i) + { + isPrime[j] =false; + } + + primes.push_back(i); + } + } +} + +ll eulerPhi() +{ + ll ans = n; + + ll p_index = 0, p = primes[p_index]; + + while (p * p <= n) + { + if (n % p == 0) + { + ans -= (ans / p); + + while (n % p == 0) + { + n /= p; + } + } + + p = primes[++p_index]; + } + + if (n != 1) + { + ans -= (ans / n); + } + + return ans; +} + +int main() +{ + findPrimes(); + + while (cin >> n && n != 0) + { + cout << eulerPhi() << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10179/10179-21.cpp b/uva_cpp_clean/10179/10179-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95975a40fefa22536c90921f484e7c612134be6f --- /dev/null +++ b/uva_cpp_clean/10179/10179-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10179 + Name: Irreducable Basic Fractions + Problem: https://onlinejudge.org/external/101/10179.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int n; + while (cin>>n && n) { + int t = n; + int sq = sqrt((double)n); + for (int i=2; i<=sq; i++) + if (n%i == 0) { + while (n%i==0) n/=i; + t = (t/i) * (i-1); + sq = sqrt((double)n); + } + if (n>1) t = (t/n)*(n-1); + + cout << t << endl; + } +} diff --git a/uva_cpp_clean/10179/10179-5.cpp b/uva_cpp_clean/10179/10179-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3624c4a98591c1ee94d73a2507defaebbfa66422 --- /dev/null +++ b/uva_cpp_clean/10179/10179-5.cpp @@ -0,0 +1,115 @@ +/****************************************************************** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** My Blog : shipuahamed.blogspot.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_HIDDEN *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t); +#define ssii(a,b) scanf("%d%d",&a,&b); +#define ssll(a,b) scanf("%lld%lld",&a,&b); + + +#define ff first +#define se second +#define eps 1e-9 +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +templateinline void extended_euclid(T a,T b,T &x,T &y){if(a%b==0)x=0,y=1;else{extended_euclid(b,a%b,x,y);T temp=x;x=y;y=-y*(a/b)+temp;}} +templateinline T bigmod(T b,T p,T m){if(!p)return 1;else if(!(p%2)){T x=bigmod(b,p/2,m);return x*x;}else return ((b%m)*bigmod(b,p-1,m))%m;} + +void ASCII_Chart(){int i,j,k;pf("ASCII Chart:(30-129)\n");for(int i=30;i<50;i++){for(int j=0;j<5;j++){k=i+j*20;pf("%3d---> '%c' ",k,k);}pf("\n");}} + +int stringconvert(string s){int p;istringstream buf(s);buf>>p;return p;} +int SOD(int n){int sum=0;for(int i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + + +//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction +//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction +//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction + +ll phi(ll n) +{ + ll res=0; + ll j; + if (n==1) return 1; + res=n; + if (n%2==0) + { + res-=res/2; + while (n%2==0) n/=2; + } + for (j=3; j*j<=n; j+=2) + { + if (n%j==0) + { + res-=res/j; + while (n%j==0) n/=j; + } + } + if (n>1) res-=res/n; + return res; +} + + +int main() +{ + int n; + while(sii(n)&&n) + { + cout << phi(n) << endl; + } +} diff --git a/uva_cpp_clean/10180/10180-9.cpp b/uva_cpp_clean/10180/10180-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5befa8ff3987a258569d82ca87a464c7a1337d00 --- /dev/null +++ b/uva_cpp_clean/10180/10180-9.cpp @@ -0,0 +1,205 @@ +#include + +using namespace std; + +const double EPS = 1e-9; +const double PI = acos(-1.0); + +struct Vector{ + double x, y; + Vector(): x(0), y(0) {}; + Vector(double x_, double y_): x(x_), y(y_) {}; + Vector(Vector &A, Vector &B): x(B.x - A.x), y(B.y - A.y) {}; + + Vector operator + (const Vector& other) const{ + return Vector(x + other.x, y + other.y); + } + + Vector operator - (const Vector& other) const{ + return Vector(x - other.x, y - other.y); + } + + Vector operator * (const double& K) const{ + return Vector(x * K, y * K); + } + + Vector operator / (const double& K) const{ + return Vector(x / K, y / K); + } + + Vector Ortogonal() const{ + return Vector(-y, x); + } + + Vector Normalize() const{ + double K = ( *this ).Norm(); + return Vector(x / K, y / K); + } + + Vector Rotate(double theta){ + return Vector(x * cos(theta) - y * sin(theta), x * sin(theta) + y * cos(theta) ); + } + + Vector Reflect(Vector& other){ + return (*this) - Vector( (*this), other); + } + + bool operator == (const Vector& other) const{ + return ( fabs(x - other.x) < EPS and fabs(y - other.y) < EPS ); + } + + bool operator < (const Vector& other) const{ + if( not fabs(x - other.x) < EPS ) return x < other.x; + return y < other.y; + } + + bool isCollinear(const Vector& other) const{ + return fabs( ( *this ).CrossProduct(other) ) < EPS; + } + + bool ccw(const Vector other) const{ + return ( ( *this ).CrossProduct(other) > 0.0 ); + } + + bool isInsideCircle(const Vector& other, const double& r) const{ + double dx = x - other.x, dy = y - other.y, R = r * r; + dx *= dx, dy *= dy; + if( dx + dy < R ) return true; + return false; + } + + double DotProduct(const Vector& other) const{ + return x * other.x + y * other.y; + } + + double CrossProduct(const Vector& other) const{ + return x * other.y - y * other.x; + } + + double Norm() const{ + return hypot(x, y); + } + + double Norm2() const{ + return x * x + y * y; + } + + double Distance(const Vector& other) const{ + return hypot(x - other.x, y - other.y); + } + + double Angle(const Vector& other) const{ + return acos( ( *this ).DotProduct(other) / sqrt( ( *this ).Norm2() * other.Norm2() ) ); + } + + double ScalarProyection(const Vector& other) const{ + return ( *this ).DotProduct(other) / other.Norm2(); + } + + void print() const{ + printf("(%.2lf, %.2lf)\n", x, y); + } + +}; + +struct Line{ + double a, b, c; + Vector A, B; + Line(): a(0), b(0), c(0) {}; + Line(double a_, double b_, double c_): a(a_), b(b_), c(c_) {}; + Line(Vector& P1, Vector& P2): A(P1), B(P2){ + if( fabs(P1.x - P2.x) < EPS ) + a = 1.0, b = 0.0, c = -P1.x; + else + a = ( P1.y - P2.y ) / ( P2.x - P1.x ), b = 1.0, c = -a * P1.x - P1.y; + } + + inline bool isParallel(const Line& other) const{ + return ( fabs(a - other.a) < EPS and fabs(b - other.b) < EPS ); + } + + inline bool isEqual(const Line& other) const{ + return ( ( *this ).isParallel(other) and fabs(c - other.c) < EPS ); + } + + int Intersection(Line& other, Vector& P) const{ + if( ( *this ).isEqual(other) ) return 0; + if( ( *this ).isParallel(other) ) return -1; + P.x = (b * other.c - other.b * c ) / (a * other.b - b * other.a); + if( fabs(other.b) < EPS ) + P.y = -c - a * P.x; + else + P.y = -other.c - other.a * P.x; + return 1; + } + + double Distance(Vector& P, Vector& C){ + C = A + Vector(A, B) * Vector(A, P).ScalarProyection( Vector(A, B) ); + return P.Distance(C); + } + + double DistanceLineSegment(Vector& P, Vector& C){ + double u = Vector(A, P).ScalarProyection( Vector(A, B) ); + if( u < 0.0 ){ + C = A; + return A.Distance(P); + } + if( u > 1.0 ){ + C = B; + return B.Distance(P); + } + return ( *this ).Distance(P, C); + } + + bool isOnSegment(const Vector& other) const{ + return ( min(A.x, B.x) <= other.x + EPS and other.x - EPS <= max(A.x, B.x) and + min(A.y, B.y) <= other.y + EPS and other.y - EPS <= max(A.y, B.y) ); + } + + bool LineSegmentIntersection(Line& other, Vector& P){ + int k = ( *this ).Intersection(other, P); + if( k == -1 ) return false; + if( k == 0 ){ + if( ( *this ).isOnSegment(other.A) or ( *this ).isOnSegment(other.B) or + other.isOnSegment(A) or other.isOnSegment(B) ) return true; + return false; + } + if( k == 1 ){ + if( ( *this ).isOnSegment(P) and other.isOnSegment(P) ) return true; + return false; + } + } + +}; + +int tc; +double R, d, alfa, beta, theta1, theta2, theta, ans; +Vector P1, P2, A, B, C, Zero(0.0, 0.0); + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%lf %lf %lf %lf %lf", &P1.x, &P1.y, &P2.x, &P2.y, &R); + Line(P1, P2).DistanceLineSegment( Zero, C ); + if( C.isInsideCircle( Zero, R) ){ + alfa = acos( R / P1.Norm() ); + beta = acos( R / P2.Norm() ); + P1 = P1.Normalize() * R; + P2 = P2.Normalize() * R; + A = P1.Rotate(-alfa); + B = P2.Rotate(beta); + d = A.Distance(B); + theta1 = 2.0 * asin( d / ( 2.0 * R ) ); + A = P1.Rotate(alfa); + B = P2.Rotate(-beta); + d = A.Distance(B); + theta2 = 2.0 * asin( d / ( 2.0 * R ) ); + theta = min(theta1, theta2); + ans = R * ( tan(alfa) + tan(beta) + theta ); + } + else + ans = P1.Distance(P2); + printf("%.3lf\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10183/10183-14.cpp b/uva_cpp_clean/10183/10183-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..caaec4263d48077798dae79a7adae715f39b3a78 --- /dev/null +++ b/uva_cpp_clean/10183/10183-14.cpp @@ -0,0 +1,136 @@ +/*************************************************** + * Problem name : 10183 - How Many Fibs? .cpp + * Problem Link : https://uva.onlinejudge.org/external/101/10183.pdf + * OJ : Uva + * Verdict : AC + * Date : 11.06.2017 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +string fib[MAX]; +string add(string a, string b) { + int al = a.size() - 1; + int bl = b.size() - 1; + + int carry = 0; + string result = ""; + + while (al >= 0 && bl >= 0) { + int temp = (int)(a[al] - '0') + (int)(b[bl] - '0') + carry ; + carry = 0; + if (temp > 9 ) { + carry = 1; + temp = temp - 10; + } + result += char(temp + '0'); + al--; + bl--; + } + while (al >= 0) { + int temp = (int)(a[al] - '0') + carry ; + carry = 0; + if (temp > 9) { + carry = 1; + temp = temp % 10; + } + + result += char(temp + '0'); + al--; + } + while (bl >= 0) { + int temp = (int)(b[bl] - '0') + carry ; + carry = 0; + if (temp > 9) { + carry = 1; + temp = temp % 10; + } + result += char(temp + '0'); + bl--; + } + if (carry) { + result += "1"; + } + + string addition = ""; + reverse(result.begin(), result.end()); + return result; +} + +string trim(string a) { // for removing leading 0s + string res = ""; + int i = 0; + + while (a[i] == '0') { + i++; + } + int sz = a.size(); + for (; i < sz; i++) { + res += a[i]; + } + return res; +} +void preCal() { + string s1 = "0"; + string s2 = "1"; + fib[0] = s1; + fib[1] = s2; + for (int i = 2; i <= MAX; i++) { + string s = trim(add(s1, s2)); + fib[i] = s; + s1 = s2; + s2 = s; + } +} +int main () { + int n; + preCal(); + while (scanf("%d", &n) == 1) { + cout << "The Fibonacci number for " << n << " is " << fib[n] << "\n"; + } + return 0; +} + diff --git a/uva_cpp_clean/10183/10183-19.cpp b/uva_cpp_clean/10183/10183-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ba418a61985f1e94e0a33a76e26b4a2ab90bb43 --- /dev/null +++ b/uva_cpp_clean/10183/10183-19.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +const unsigned NUM=100; +typedef long long int base_t; +const base_t BASE=1000000000; + +struct bigint{ + bigint(long long int i=0) :used(0),sign((i>=0)?1:-1){ + memset(inner,0,NUM*sizeof(base_t)); + i*=sign; + for(;i>=BASE;i=i/BASE) inner[used++]=i%BASE; + inner[used++]=i; + } + bigint& operator+=(const bigint& b){ + used=min(max(b.used,used)+1, NUM); base_t carry=0; + for(int i=0;iused && inner[j-1]!=0) used=j; + return *this; + } + bigint operator+(base_t b) { bigint r(*this); return r+=b; } + + string str() const { + stringstream ss; + ss << sign*inner[used-1]; + for(int i=used-2;i>=0;i--) + ss<<"00000000"+(int)(inner[i] ? floor(log10(inner[i])) : 0) + << inner[i]; + return ss.str(); + } + char sign; + unsigned used; + base_t inner[NUM]; +}; +bigint operator+(long long int i, const bigint& l) { return l+i; } + +int main() { + string fib[500]; + bigint f0(1), f1(2); + fib[0] = string(129, '0') + f0.str(), fib[1] = string(129, '0') + f1.str(); + for (int i = 2; i < 500; i++) { + bigint auxi = f0 + f1; + f0 = f1; + f1 = auxi; + string s = f1.str(); + fib[i] = string(130-s.length(), '0') + s; + } + string a, b; + while (cin >> a >> b && !(a == "0" && b == "0")) { + a = string(130-a.length(), '0') + a; + b = string(130-b.length(), '0') + b; + printf("%d\n", upper_bound(fib, fib + 500, b) - lower_bound(fib, fib + 500, a)); + } + return 0; +} diff --git a/uva_cpp_clean/10183/10183-21.cpp b/uva_cpp_clean/10183/10183-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15821c8cdd25aa2d7c3613cf0b847beac75602e2 --- /dev/null +++ b/uva_cpp_clean/10183/10183-21.cpp @@ -0,0 +1,914 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10183 + Name: How Many Fibs? + Problem: https://onlinejudge.org/external/101/10183.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#ifndef min +#define min(x,y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef max +#define max(x,y) ((x) > (y) ? (x) : (y)) +#endif + +class BigInt +{ +private: + char *digits; + int size; // number of used bytes (digits) + int capacity; // size of digits + int sign; // -1, 0 or +1 + +public: + BigInt(int n, int cap); + BigInt(int n); + BigInt(long double d); + BigInt(); + BigInt(string s); + BigInt(const char s[]); + BigInt(const BigInt &n); + const BigInt &operator=(const BigInt &n); + const BigInt &operator=(int n); + ~BigInt(); + void normalize(); + static int sig(int n); + static int sig(long double n); + inline int length() { return size; } + + BigInt operator++(); + BigInt operator++(int); + BigInt operator--(); + BigInt operator--(int); + BigInt operator-(); + BigInt operator+ (int n); + BigInt operator+ (BigInt n); + BigInt&operator+=(int n); + BigInt&operator+=(BigInt n); + BigInt operator- (int n); + BigInt operator- (BigInt n); + BigInt&operator-=(int n); + BigInt&operator-=(BigInt n); + BigInt operator* (int n); + BigInt operator* (BigInt n); + void operator*=(int n); + void operator*=(BigInt n); + BigInt operator/ (int n); + BigInt operator/ (BigInt n); + void operator/=(int n); + void operator/=(BigInt n); + int operator% (int n); + BigInt operator% (BigInt n); + void operator%=(int n); + void operator%=(BigInt n); + int divide(int n); + BigInt divide(BigInt n); + BigInt operator* (long double n); + void operator*=(long double n); + + + BigInt operator<< (int n); + void operator<<=(int n); + BigInt operator >> (int n); + void operator>>=(int n); + BigInt operator,(int n); + BigInt operator,(BigInt n); + + /** Casting **/ + bool operator!(); + operator bool(); + operator string(); + + bool operator<(BigInt n); + bool operator>(BigInt n); + bool operator==(BigInt n); + bool operator<=(BigInt n); + bool operator>=(BigInt n); + bool operator<(int n); + bool operator>(int n); + bool operator==(int n); + bool operator<=(int n); + bool operator>=(int n); + int compare(BigInt n); + + int toInt(); + string toString(); + void print(); + void printWithCommas(ostream &out); + +private: + void grow(); + friend istream &operator >> (istream &in, BigInt &n); + friend ostream &operator<<(ostream &out, BigInt n); + friend long double log2(BigInt x, long double epsilon); + inline friend long double log(BigInt x, long double epsilon); + inline friend long double log10(BigInt x, long double epsilon); + inline friend long double lg(BigInt x, long double epsilon); + inline friend long double ln(BigInt x, long double epsilon); +}; + +BigInt operator+(int m, BigInt &n) +{ + return n + m; +} + +BigInt operator-(int m, BigInt &n) +{ + return -n + m; +} + +BigInt operator*(int m, BigInt &n) +{ + return n * m; +} + +BigInt operator/(int m, BigInt &n) +{ + return BigInt(m) / n; +} + +BigInt operator%(int m, BigInt &n) +{ + return BigInt(m) % n; +} + +inline bool isDigit(int c) +{ + return(c >= (int)'0' && c <= (int)'9'); +} + +istream &operator >> (istream &in, BigInt &n) // FIXME: see inside +{ + n.size = 0; + n.sign = 1; + int sign = 1; + int c; + while ((c = in.peek()) >= 0 && + (c == ' ' || c == '\t' || c == '\r' || c == '\n')) + in.get(); + if (c < 0 || (c != (int)'-' && !isDigit(c))) + { + in >> c; // XXX: force in.fail() + return in; + } + if (c == (int)'-') { sign = -1; in.get(); } + + // FIXME: Extremely inefficient! Use a string. + while ((c = in.peek()) >= 0 && isDigit(c)) + { + in.get(); + n *= 10; + n += (c - (int)'0'); + } + n.sign = sign; + n.normalize(); + return in; +} + +ostream &operator<<(ostream &out, BigInt n) //FIXME: make more efficient +{ + return out << n.toString(); +} + +BigInt::BigInt(int n, int cap) +{ + cap = max(cap, (int)sizeof(n) * 8); + capacity = cap; + sign = sig(n); + n *= sign; + digits = new char[cap]; + memset(digits, 0, cap); + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(int n) +{ + capacity = 1024; + sign = sig(n); + n *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + while (n) + { + digits[size++] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(long double d) +{ + capacity = 1024; + sign = (d < 0 ? -1 : d > 0 ? 1 : 0); + d *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + d = floor(d); + while (d > 0) + { + digits[size++] = min(max(0, (int)((d - floor(d / 10) * 10) + 0.5)), 9); + d = floor(d / 10); + } +} + +BigInt::BigInt() +{ + capacity = 128; + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; +} + +BigInt::BigInt(string s) +{ + capacity = max((int)s.size(), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const char s[]) +{ + capacity = max((int)strlen(s), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const BigInt &n) +{ + capacity = n.capacity; + sign = n.sign; + size = n.size; + digits = new char[capacity]; + memcpy(digits, n.digits, capacity); +} + +const BigInt &BigInt::operator=(const BigInt &n) +{ + if (&n != this) + { + if (capacity < n.size) + { + capacity = n.capacity; + delete[] digits; + digits = new char[capacity]; + } + sign = n.sign; + size = n.size; + memcpy(digits, n.digits, size); + memset(digits + size, 0, capacity - size); + } + return *this; +} + +const BigInt &BigInt::operator=(int n) +{ + sign = sig(n); + n *= sign; + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } + return *this; +} + +BigInt::~BigInt() +{ + delete[] digits; +} + +void BigInt::normalize() +{ + while (size && !digits[size - 1]) size--; + if (!size) sign = 0; +} + +int BigInt::sig(int n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::sig(long double n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::toInt() +{ + int result = 0; + for (int i = size - 1; i >= 0; i--) + { + result *= 10; + result += digits[i]; + if (result < 0) return sign * 0x7FFFFFFF; + } + return sign * result; +} + +string BigInt::toString() +{ + string s = (sign >= 0 ? "" : "-"); + for (int i = size - 1; i >= 0; i--) + s += (digits[i] + '0'); + if (size == 0) s += '0'; + return s; +} + +void BigInt::print() //FIXME: make more efficient +{ + cout << toString(); +} + +void BigInt::printWithCommas(ostream &out) +{ + if (sign < 0) out.put('-'); + for (int i = size - 1; i >= 0; i--) + { + out.put(digits[i] + '0'); + if (!(i % 3) && i) out.put(','); + } + if (size == 0) out.put('0'); +} + +void BigInt::grow() +{ + char *olddigits = digits; + int oldCap = capacity; + capacity *= 2; + digits = new char[capacity]; + memcpy(digits, olddigits, oldCap); + memset(digits + oldCap, 0, oldCap); + delete[] olddigits; +} + +BigInt BigInt::operator++() +{ + operator+=(1); + return *this; +} + +BigInt BigInt::operator++(int) +{ + return operator++(); +} + +BigInt BigInt::operator--() +{ + operator-=(1); + return *this; +} + +BigInt BigInt::operator--(int) +{ + return operator--(); +} + +BigInt BigInt::operator-() +{ + BigInt result(*this); + result.sign *= -1; + return result; +} + +BigInt BigInt::operator+(int n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt BigInt::operator+(BigInt n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt &BigInt::operator+=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = nsign; + if (sign == nsign) + { + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] + dig + carry; + digits[i] = newdig % 10; + carry = newdig / 10; + n /= 10; + } + size = max(i, size); + } + else operator-=(-n); + return *this; +} + +BigInt &BigInt::operator+=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = n.sign; + if (sign == n.sign) + { + int carry = 0; + int i; + for (i = 0; i < maxS - 1 || carry; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig += n.digits[i]; + digits[i] = newdig % 10; + carry = newdig / 10; + } + size = max(i, size); + } + else + { + n.sign *= -1; + operator-=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator-(int n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt BigInt::operator-(BigInt n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt &BigInt::operator-=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = 1; + if (sign == nsign) + { + BigInt bin = n; + if (sign >= 0 && *this < bin || sign < 0 && *this > bin) + { + // Subtracting a bigger number + operator=(toInt() - n); + return *this; + } + + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] - dig + carry; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + n /= 10; + } + normalize(); + } + else operator+=(-n); + return *this; +} + +BigInt &BigInt::operator-=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = 1; + if (sign == n.sign) + { + if (sign >= 0 && *this < n || sign < 0 && *this > n) + { + // Subtracting a bigger number + BigInt tmp = n; + tmp -= *this; + *this = tmp; + sign = -sign; + return *this; + } + + int carry = 0; + int i; + for (i = 0; i < maxS - 1; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig -= n.digits[i]; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + } + if (carry) // Subtracted a bigger number, need to flip sign + { + if (i) digits[0] = 10 - digits[0]; + size = (i ? 1 : 0); + for (int j = 1; j < i; j++) + { + digits[j] = 9 - digits[j]; + if (digits[i]) size = j + 1; + } + sign *= -1; + } + normalize(); + } + else + { + n.sign *= -1; + operator+=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator*(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + int nsign = sig(n); + n *= nsign; + result.sign = sign * nsign; + if (!result.sign) return result; + + int i, j; + for (i = 0; n; i++) + { + int dig = n % 10; + if (dig) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = result.digits[i + j] + (j < size ? dig * digits[j] : 0) + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + n /= 10; + } + result.size = i + j - 1; + return result; +} + +BigInt BigInt::operator*(BigInt n) +{ + BigInt result(0, size + n.size); + + result.sign = sign * n.sign; + if (!result.sign) return result; + + int i, j; + for (i = 0; i < n.size; i++) + { + if (n.digits[i]) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = + result.digits[i + j] + + (j < size ? n.digits[i] * digits[j] : 0) + + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + } + result.size = i + j - 1; + + return result; +} + +void BigInt::operator*=(int n) +{ + operator=(operator*(n)); +} + +void BigInt::operator*=(BigInt n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator/(int n) +{ + if (!n) n /= n; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +BigInt BigInt::operator/(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +void BigInt::operator/=(int n) +{ + divide(n); +} + +void BigInt::operator/=(BigInt n) +{ + divide(n); +} + +int BigInt::operator%(int n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(int n) +{ + operator=(divide(n)); +} + +BigInt BigInt::operator%(BigInt n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(BigInt n) +{ + operator=(divide(n)); +} + +int BigInt::divide(int n) +{ + if (!n) n /= n; //XXX: force a crash + + int nsign = sig(n); + n *= nsign; + if (!sign) return 0; + sign *= nsign; + + int tmp = 0; + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = tmp / n; + tmp -= digits[i] * n; + } + normalize(); + return tmp; +} + +BigInt BigInt::divide(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + if (!sign) return 0; + sign *= n.sign; + + int oldSign = n.sign; + n.sign = 1; + + BigInt tmp(0, size); + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = 0; + while (tmp >= n) { tmp -= n; digits[i]++; } + } + normalize(); + + n.sign = oldSign; + + return tmp; +} + +// This is only exact to the first 15 or so digits, but it is +// never an over-estimate +BigInt BigInt::operator*(long double n) +{ + // the number of digits after the decimal point to use + #define DIGS_AFTER_DOT 15 + + int nsign = sig(n); + n *= nsign; + int ndigs = n >= 1 ? (int)log10(n) + 1 : 0; + BigInt result(0, size + ndigs); + result.sign = sign * nsign; + if (!result.sign) return result; + + if (n >= 1) for (int i = 0; i < ndigs; i++) n /= 10; + result.size = 0; + + char afterDot[DIGS_AFTER_DOT + 1]; + memset(afterDot, 0, sizeof(afterDot)); + + // Keep going until the DIGS_AFTER_DOT'th digit after the decimal point + for (int i = ndigs - 1; i >= -DIGS_AFTER_DOT; i--) + { + n *= 10; + int dig = (int)floor(n); + n -= dig; + if (!dig) continue; + + int carry = 0; + for (int j = 0; j < size || carry; j++) + { + int newdig = + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) + + dig * digits[j] + + carry; + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) = newdig % 10; + if (i + j >= 0 && result.digits[i + j]) result.size = max(result.size, i + j + 1); + carry = newdig / 10; + } + } + if (!result.size) result.sign = 0; + return result; +} + +void BigInt::operator*=(long double n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator<<(int n) +{ + BigInt result(*this); + result <<= n; + return result; +} + +void BigInt::operator<<=(int n) +{ + if (n < 0) operator>>=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator*=(mult); + } +} + +BigInt BigInt::operator >> (int n) +{ + BigInt result(*this); + result >>= n; + return result; +} + +void BigInt::operator>>=(int n) +{ + if (n < 0) operator<<=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator/=(mult); + } +} + +BigInt BigInt::operator,(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + for (result.size = 0; n; result.size++) + { + result.digits[result.size] = n % 10; + n /= 10; + } + memcpy(result.digits + result.size, digits, size * sizeof(digits[0])); + result.size += size; + result.sign = 1; + result.normalize(); + return result; +} + +BigInt BigInt::operator,(BigInt n) +{ + BigInt result(0, size + n.size); + memcpy(result.digits, n.digits, n.size * sizeof(n.digits[0])); + memcpy(result.digits + n.size, digits, size * sizeof(digits[0])); + result.size = size + n.size; + result.sign = 1; + result.normalize(); + return result; +} + +bool BigInt::operator!() +{ + return !size; +} + +BigInt::operator bool() +{ + return size; +} + +BigInt::operator string() +{ + return toString(); +} + +bool BigInt::operator<(BigInt n) +{ + return(compare(n) < 0); +} + +bool BigInt::operator>(BigInt n) +{ + return(compare(n) > 0); +} + +bool BigInt::operator>=(BigInt n) +{ + return(compare(n) >= 0); +} + +int BigInt::compare(BigInt n) +{ + if (sign < n.sign) return -1; + if (sign > n.sign) return 1; + if (size < n.size) return -sign; + if (size > n.size) return sign; + for (int i = size - 1; i >= 0; i--) + { + if (digits[i] < n.digits[i]) return -sign; + else if (digits[i] > n.digits[i]) return sign; + } + return 0; +} + +int main(){ + BigInt F[500]; + F[0] = BigInt(1); + F[1] = BigInt(2); + for (int i=2; i<500; i++) + F[i] = F[i-1] + F[i-2]; + + char a[105], b[105]; + while (cin>>a>>b && (a[0]!='0' || b[0]!='0')) { + int l = lower_bound(F, F+500, BigInt(a)) - F, + r = lower_bound(F, F+500, BigInt(b)+1) - F; + + cout << r-l << endl; + } +} diff --git a/uva_cpp_clean/10183/10183-9.cpp b/uva_cpp_clean/10183/10183-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ca0e21db5864af638611fd0d7f76d4b916e390a --- /dev/null +++ b/uva_cpp_clean/10183/10183-9.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +#define DIGIT 105 + +int fib[501][DIGIT + 1], ct, l1, l2; +char aux1[DIGIT + 1], aux2[DIGIT+1], a[DIGIT + 1], b[DIGIT + 1]; +bool ok; + +bool cmp_fib(int x){ + ok = true; + for(int i = 0; i <= DIGIT; i++) + if(a[i] - '0' > fib[x][i]) + break; + else if(a[i] - '0' < fib[x][i]){ + ok = false; + break; + } + if(!ok) + return false; + for(int i = 0; i <= DIGIT; i++) + if(b[i] - '0' < fib[x][i]) + break; + else if(b[i] - '0' > fib[x][i]){ + ok = false; + break; + } + return ok; +} + +int main(){ + fib[0][DIGIT] = fib[1][DIGIT] = 1; + for(int i = 2; i <= 500; i++){ + for(int j = DIGIT; j > 0; j--){ + fib[i][j] += fib[i - 1][j] + fib[i - 2][j]; + if(fib[i][j] > 9) + fib[i][j - 1] += 1, fib[i][j] %= 10; + } + } + while(true){ + scanf("%s %s", aux1, aux2); + ct = 0; + memset(a, '0', sizeof a); + memset(b, '0', sizeof b); + l1 = strlen(aux1), l2 = strlen(aux2); + if(l1 == 1 && l2 == 1 && aux1[0] == '0' && aux2[0] == '0') + break; + l1--, l2--; + for(int i = DIGIT; l1 >= 0; i--, l1--) + b[i] = aux1[l1]; + for(int i = DIGIT; l2 >= 0; i--, l2--) + a[i] = aux2[l2]; + for(int i = 1; i <= 500; i++) + if(cmp_fib(i)) + ct++; + printf("%d\n", ct); + } + return(0); +} diff --git a/uva_cpp_clean/10188/10188-15.cpp b/uva_cpp_clean/10188/10188-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa2c03947ffed32b4bc3ef64bfb83c9f85cb155a --- /dev/null +++ b/uva_cpp_clean/10188/10188-15.cpp @@ -0,0 +1,48 @@ +/* + 模擬 +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, m, k = 0; + string in; + while (cin >> n and n) { + vector number1, number2; + vector answer, output; + cout << "Run #" << ++k << ": "; + cin.ignore(1024, '\n'); + while (n--) { + getline(cin, in); + answer.push_back(in); + } + cin >> m; + cin.ignore(1024, '\n'); + while (m--) { + getline(cin, in); + output.push_back(in); + } + if (answer == output) + cout << "Accepted\n"; + else { + for (const auto i : answer) + for (const auto c : i) + if (c <= '9' and c >= '0') + number1.push_back(c); + for (const auto i : output) + for (const auto c : i) + if (c <= '9' and c >= '0') + number2.push_back(c); + if (number1 == number2) + cout << "Presentation Error\n"; + else + cout << "Wrong Answer\n"; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10188/10188-21.cpp b/uva_cpp_clean/10188/10188-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65698e140b93829b8709692bcb1708946052f633 --- /dev/null +++ b/uva_cpp_clean/10188/10188-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10188 + Name: Automated Judge Script + Problem: https://onlinejudge.org/external/101/10188.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char S[3][23] = { + "Accepted", + "Presentation Error", + "Wrong Answer" +}; + +int check(const string &s1, const string &s2) { + if (s1 == s2) return 0; + + int l1 = s1.length(), + l2 = s2.length(); + for (int i=0,j=0; i> n && n>0; ++cse) { + string str1, str2, str; + + cin.ignore(100, '\n'); + for (int i=0; i> m; + cin.ignore(100, '\n'); + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 105 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int fx[] = {1, -1, 0, 0, 1, -1, -1, 1}; +int fy[] = {0, 0, 1, -1, 1, -1, 1, -1}; +int n, m; +string str[MAX]; +bool isValid(int x, int y) { + if ( x >= 0 && y >= 0 && x < n && y < m) return true; + return false; +} +int howMany(int x, int y) { + int cnt = 0; + for (int i = 0; i < 8; i++) { + int xx = x + fx[i]; + int yy = y + fy[i]; + if (isValid(xx, yy) && str[xx][yy] == '*')cnt++; + } + return cnt; +} +int main () { + int t = 1; + while (scanf("%d %d", &n, &m) == 2 && n != 0 && m != 0) { + for(int i = 0; i> str[i]; + } + if(t != 1) nl; + printf("Field #%d:\n", t++); + for(int i = 0; i +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + int n, m, cnt = 0; + char c; + while (cin >> n >> m and n) { + if (cnt) + cout << "\n"; + cout << "Field #" << ++cnt << ":\n"; + cin.ignore(1024, '\n'); + vector> map(n, vector(m, '0')); + for (int row = 0; row < n; row++) { + int column = 0; + while ((c = cin.get()) != '\n') { + if (c == '*') { + map[row][column] = '*'; + if (column != 0) { + if (map[row][column - 1] >= '0') + ++map[row][column - 1]; + if (row != 0) + if (map[row - 1][column - 1] >= '0') + ++map[row - 1][column - 1]; + if (row != n - 1) + if (map[row + 1][column - 1] >= '0') + ++map[row + 1][column - 1]; + } + if (row != 0) + if (map[row - 1][column] >= '0') + ++map[row - 1][column]; + if (row != n - 1) + if (map[row + 1][column] >= '0') + ++map[row + 1][column]; + if (column != m - 1) { + if (map[row][column + 1] >= '0') + ++map[row][column + 1]; + if (row != 0) + if (map[row - 1][column + 1] >= '0') + ++map[row - 1][column + 1]; + if (row < n - 1) + if (map[row + 1][column + 1] >= '0') + ++map[row + 1][column + 1]; + } + } + column++; + } + } + for (const auto& i : map) { + for (const auto& c : i) + cout << c; + cout << "\n"; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10189/10189-19.cpp b/uva_cpp_clean/10189/10189-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42b7dff470e65df3b25e4d85525bf401167d367e --- /dev/null +++ b/uva_cpp_clean/10189/10189-19.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +bool grid[101][101]; + +int main(){ + int cases = 1; + int x, y; + bool first = true; + while (scanf("%d %d", &x, &y) && !(x == 0 && y == 0)) { + if (!first) printf("\n"); + else first = false; + char c; + scanf("%c", &c); + for (int i = 0; i < x; i++){ + for (int j = 0; j < y; j++) { + scanf("%c", &c); + if (c == '*') grid[i][j] = true; + else grid[i][j] = false; + } + scanf("%c", &c); + } + printf("Field #%d:\n", cases++); + for (int i = 0; i < x; i++){ + for (int j = 0; j < y; j++) { + if (grid[i][j]) printf("*"); + else { + int cont = 0; + for (int l = -1; l <= 1; l++) { + for (int m = -1; m <= 1; m++){ + if ((l+i=0 && m+j=0) && grid[l+i][m+j]) cont++; + } + } + printf("%d", cont); + } + } + printf("\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10189/10189-9.cpp b/uva_cpp_clean/10189/10189-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2398e6428fe9b7fd6014afeaa40c44e441343b39 --- /dev/null +++ b/uva_cpp_clean/10189/10189-9.cpp @@ -0,0 +1,36 @@ +#include + +#define SIZE 110 + +using namespace std; + +int tc, n, m, table[SIZE][SIZE]; +char mines[SIZE][SIZE]; +int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; +int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; + +int main(){ + while(scanf("%d %d\n", &n, &m), n | m){ + for(int row = 0; row < n; row++) + scanf("%s", mines[row]); + memset(table, 0, sizeof table); + for(int row = 0; row < n; row++) + for(int col = 0; col < m; col++){ + if( mines[row][col] == '*' ) + for(int it = 0; it < 8; it++){ + int r = row + dy[it], c = col + dx[it]; + if( r >= 0 and c >= 0 and r < n and c < m ) + table[r][c]++; + } + } + if(tc) putchar('\n'); + printf("Field #%d:\n", ++tc); + for(int row = 0; row < n; row++){ + for(int col = 0; col < m; col++) + if( mines[row][col] == '*' ) putchar('*'); + else printf("%d", table[row][col]); + putchar('\n'); + } + } + return(0); +} diff --git a/uva_cpp_clean/10190/10190-14.cpp b/uva_cpp_clean/10190/10190-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eebe70f9ddea143b19d0cda95cd84de3ff88a01d --- /dev/null +++ b/uva_cpp_clean/10190/10190-14.cpp @@ -0,0 +1,89 @@ +/*************************************************** + * Problem name : 10190 Divide, But Not Quite Conquer!.cpp + * Problem Link : https://uva.onlinejudge.org/external/101/10190.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-22 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ll n, m; + vectorar; + while (scanf("%lld %lld", &n, &m) == 2) { + ar.clear(); + ar.push_back(n); + int i = 1, flag = 0; + if (m < 2 || n == 0 || n < m) { + puts("Boring!"); + continue; + } + while (1) { + if (n % m != 0 && n != 1) { + flag = 1; + break; + } + n = ar[i - 1] / m; + ar.push_back(n); + if (n == 1) break; + i++; + } + if (flag) printf("Boring!\n"); + else { + int sz = ar.size(); + for (int i = 0; i < sz; i++) { + if (i != 0) printf(" "); + printf("%lld", ar[i]); + } + nl; + } + } + return 0; +} + diff --git a/uva_cpp_clean/10190/10190-15.cpp b/uva_cpp_clean/10190/10190-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..401f71d10c9a192ef46ad864a6c3eb2721e8ba1d --- /dev/null +++ b/uva_cpp_clean/10190/10190-15.cpp @@ -0,0 +1,31 @@ +/* + 等比級數 +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, m; + while (cin >> n >> m) { + if (m < 2 or n < m) { + cout << "Boring!\n"; + continue; + } + vector sequence; + while (n % m == 0) { + sequence.push_back(n); + n /= m; + } + if (n == 1) { + for (const int &i : sequence) + cout << i << " "; + cout << "1\n"; + } else + cout << "Boring!\n"; + } +} diff --git a/uva_cpp_clean/10190/10190-21.cpp b/uva_cpp_clean/10190/10190-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b863f5cbacfe156b8aef35cad9beb60d9d9f4b1 --- /dev/null +++ b/uva_cpp_clean/10190/10190-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10190 + Name: Divide, But Not Quite Conquer! + Problem: https://onlinejudge.org/external/101/10190.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int a[100000]; +int main(){ + int t,n,m,i,k; + while(scanf("%d%d",&n,&m)==2){ + t=n; + for(i=0;n>1 && m!=1 && m!=0 && n%m==0;i++){ + n/=m; + a[i]=n; + } + if(n!=1 || m==1 || !m || i<1) + printf("Boring!\n"); + else{ + cout< +#include +#include + +int main() +{ + int n, m; + while (std::cin >> n >> m) + { + if (m == 0 || m == 1 || m > n) + { + std::cout << "Boring!" << std::endl; + continue; + } + + std::vector powers; + + for (long i = 1; i <= n; i *= m) + { + powers.push_back(i); + } + + if (powers[powers.size() - 1] != n) + { + std::cout << "Boring!" << std::endl; + continue; + } + + std::cout << powers[powers.size() - 1]; + + for (int i = powers.size() - 2; i >= 0; i--) + { + std::cout << " " << powers[i]; + } + + std::cout << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10191/10191-15.cpp b/uva_cpp_clean/10191/10191-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c06d05ffa19cb808abc93cb8e91d1d240470f37 --- /dev/null +++ b/uva_cpp_clean/10191/10191-15.cpp @@ -0,0 +1,71 @@ +/* + 模擬 +*/ +#include +#include +#include +#include +using namespace std; + +class Nap { + public: + int start, duration; + Nap(int s, int d) { + start = s; + duration = d; + } + bool operator<(const Nap& other) const { + if (duration == other.duration) + return start > other.start; + return duration < other.duration; + } + void print_time() { + int hour = start / 60; + int minute = start % 60; + cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute; + } + void print_duration() { + int hour = duration / 60; + int minute = duration % 60; + if (hour) + cout << hour << " hours and "; + cout << minute << " minutes.\n"; + } +}; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int day = 0, n, hour_start, minute_start, hour_end, minute_end, start, end; + char no_use; + while (cin >> n) { + vector> schedule; + pair head; + + while (n--) { + cin >> hour_start >> no_use >> minute_start >> hour_end >> no_use >> minute_end; + start = hour_start * 60 + minute_start; + end = hour_end * 60 + minute_end; + cin.ignore(1024, '\n'); + schedule.emplace_back(start, end); + } + + sort(schedule.begin(), schedule.end()); + schedule.emplace_back(1080, 1440); + + int last_end = 600; + + Nap ans(0, 0); + + for (const auto& i : schedule) { + ans = max(ans, Nap(last_end, i.first - last_end)); + last_end = i.second; + } + + cout << "Day #" << ++day << ": the longest nap starts at "; + ans.print_time(); + cout << " and will last for "; + ans.print_duration(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10192/10192-10.cpp b/uva_cpp_clean/10192/10192-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..242ef19fb822cdb81f8d76928eea15a914bcfba1 --- /dev/null +++ b/uva_cpp_clean/10192/10192-10.cpp @@ -0,0 +1,59 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + __builtin_popcount(); +*/ +bool isPrime(int x) { + if (x <= 1) return false; + if (x <= 3) return true; + if (x % 2 == 0 || x % 3 == 0) return false; + for (int i = 5; i * i <= x; i = i + 6) + if (x % i == 0 || x % (i + 2) == 0) return false; + return true; +} + +string s1,s2; +int a,b; +int dp[106][106]; + +int solve(int i,int j) { + + if (i == a || j == b) return 0; + + int &ans = dp[i][j]; + if(ans!=-1) return ans; + + ans=-100; + if (s1[i] == s2[j]) + ans = 1 + solve(i + 1, j + 1); + else { + ans = max(solve(i + 1, j),solve(i, j + 1)); + } + + return ans; + +} + +int main() { + Sakr_ + + int counter=1; + while (getline(cin,s1)) { + if(s1[0]=='#')break; + getline(cin,s2); + memset(dp,-1,sizeof dp); + a=s1.length(); + b=s2.length(); + cout<<"Case #"< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define MAX 102 +using namespace std; +typedef long long ll; +int dp[MAX][MAX]; +string A,B; +int LCS(int i, int j){ + if(A[i] == '\0' || B[j] == '\0') return 0; + if(dp[i][j]!=-1) return dp[i][j]; + int ans = 0; + if(A[i] == B[j]) ans = 1+ LCS(i+1, j+1); + else{ + int val1 = LCS(i+1,j); + int val2 = LCS(i,j+1); + ans = max(val1,val2); + } + return dp[i][j] = ans; +} +int main () { + int t = 1; + while(getline(cin,A)){ + if(A[0] == '#') break; + getline(cin,B); + SET(dp); + printf("Case #%d: you can visit at most %d cities.\n",t++,LCS(0,0)); + } + return 0; +} + diff --git a/uva_cpp_clean/10192/10192-19.cpp b/uva_cpp_clean/10192/10192-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48f72a93b7d32c9e3aa3fc854ae2b448d7d1964d --- /dev/null +++ b/uva_cpp_clean/10192/10192-19.cpp @@ -0,0 +1,26 @@ +// This file contains practically the same code as UVA10405.cpp +#include +#include +#include +using namespace std; + +int main() { + int cases = 1; + string s1, s2; + int grid[102][102]; + for (int i = 0; i < 102; i++) grid[0][i] = grid[i][0] = 0; + while (getline(cin, s1) && s1 != "#") { + getline(cin, s2); + bool lock = false; + for (int i = 0; i < s1.length(); i++) { + for (int j = 0; j < s2.length(); j++){ + grid[i+1][j+1] = grid[i+1][j]; + if (s1[i] == s2[j] && !lock) grid[i+1][j+1]++, lock = true; + if (grid[i][j+1] >= grid[i+1][j+1]) grid[i+1][j+1] = grid[i][j+1], lock = false; + } + } + printf("Case #%d: you can visit at most %d cities.\n", cases, grid[s1.length()][s2.length()]); + cases++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10192/10192-20.cpp b/uva_cpp_clean/10192/10192-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01800df76d6db77c75956e1f4f208b4f3afbd47f --- /dev/null +++ b/uva_cpp_clean/10192/10192-20.cpp @@ -0,0 +1,38 @@ +#include +#include + +const int MAX = 1005; +char X[MAX], Y[MAX]; +int m, n, LCS[MAX][MAX]; + +int LCS_length() +{ + m = strlen(X); + n = strlen(Y); + + for(int i=1 ; i<=m ; i++) LCS[i][0]=0; + for(int j=0 ; j<=n ; j++) LCS[0][j]=0; + + for(int i=1 ; i<=m ; i++) + for(int j=1 ; j<=n ; j++) + { + if( X[i-1]==Y[j-1] ) LCS[i][j] = LCS[i-1][j-1]+1; + else if( LCS[i-1][j]>=LCS[i][j-1] ) LCS[i][j] = LCS[i-1][j]; + else LCS[i][j] = LCS[i][j-1]; + } + + return LCS[m][n]; +} + +int main() +{ + int nCase = 1; + while( gets(X) && strcmp(X, "#") ) + { + gets(Y); + + printf("Case #%d: you can visit at most %d cities.\n", nCase, LCS_length()); + nCase++; + } + return 0; +} diff --git a/uva_cpp_clean/10192/10192-21.cpp b/uva_cpp_clean/10192/10192-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f74e5b03423e599cde6b69f95b71e13db6b2463 --- /dev/null +++ b/uva_cpp_clean/10192/10192-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10192 + Name: Vacation + Problem: https://onlinejudge.org/external/101/10192.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXN 105 +int dp[MAXN][MAXN]; +int main(){ + int cse = 0; + char s1[MAXN], s2[MAXN]; + while(cin.getline(s1, MAXN) && s1[0]!='#') { + cin.getline(s2, MAXN); + int l1 = strlen(s1), + l2 = strlen(s2); + + for (int i=max(l1,l2); i>=0; i--) + dp[i][0] = dp[0][i] = 0; + + for (int i=1; i<=l1; i++) + for( int j=1; j<=l2; j++) + if (s1[i-1] == s2[j-1]) + dp[i][j] = dp[i-1][j-1] + 1; + else + dp[i][j] = max(dp[i-1][j], dp[i][j-1]); + + + printf("Case #%d: you can visit at most %d cities.\n", ++cse, dp[l1][l2]); + } +} diff --git a/uva_cpp_clean/10192/10192-5.cpp b/uva_cpp_clean/10192/10192-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b2103ed968cb7f0cedd21e02cff13db5cf073ca7 --- /dev/null +++ b/uva_cpp_clean/10192/10192-5.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int n,m,lcs[2000][2000]; +int main() +{ + string s1,s2; + int no=0; + while(getline(cin,s1)) + { + if(s1.compare("#")==0) + break; + getline(cin,s2); + n=s1.size(); + m=s2.size(); + for(int i=1;i<=n;i++) + for(int j=1;j<=m;j++) + { + if(s1[i-1]==s2[j-1]) + lcs[i][j]=lcs[i-1][j-1]+1; + else + lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]); + } + pf("Case #%d: you can visit at most ",++no); + cout< + +using namespace std; + +const int MAX_LEN = 1e2 + 10; + +string s1, s2; +int dp[MAX_LEN][MAX_LEN]; + +int lcs () { + int n = s1.size(), m = s2.size(); + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (s1[i - 1] == s2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; + else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[n][m]; +} + +int main () { + int tc = 0; + while (getline(cin, s1) and s1[0] != '#' and getline(cin, s2)) + printf("Case #%d: you can visit at most %d cities.\n", ++tc, lcs()); + return (0); +} diff --git a/uva_cpp_clean/10193/10193-21.cpp b/uva_cpp_clean/10193/10193-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f277e658a2ba3d7c8e6d85078be2ee7294b9b97 --- /dev/null +++ b/uva_cpp_clean/10193/10193-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10193 + Name: All You Need Is Love + Problem: https://onlinejudge.org/external/101/10193.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int gcd(int a, int b) { + if (b==0) return a; + return gcd(b, a%b); +} + +int convert(char *b) { + int result = 0; + for (int i=0; b[i]; i++) { + result<<=1; + if (b[i]=='1') + result++; + } + return result; +} + + + +int main() { + char b1[40], b2[40]; + int T; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin >> b1 >> b2; + printf("Pair #%d: ", cse); + if (gcd(convert(b1), convert(b2)) > 1) + puts("All you need is love!"); + else + puts("Love is not all you need!"); + } +} diff --git a/uva_cpp_clean/10194/10194-18.cpp b/uva_cpp_clean/10194/10194-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4a9296e1c701f4f87b9e1944b02076e4106ee330 --- /dev/null +++ b/uva_cpp_clean/10194/10194-18.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include + +using namespace std; + +struct team{ //Usar struct para contener los datos + int points, games, wins, ties, loses, GD, GS, GA; + string name; + char cmpName[40]; //Para poder comparar lexicograficamente + void init(){ + points = 0; games = 0; wins = 0; ties = 0; loses = 0; GD = 0; GS = 0; GA = 0; + name = ""; + } + void game(int goals1, int goals2){ + if(goals1 > goals2){ + wins++; + points += 3; + }else if(goals2 > goals1) loses++; + else{ + ties++; + points++; + } + games++; + GS += goals1; + GA += goals2; + GD = GS - GA; + } +}; + +void toLower(char cmps[40], string word){ + for(int i = 0; i < word.size(); i++){ + cmps[i] = tolower(word[i]); + } +} + +bool cmp(struct team team1, struct team team2) { //Usar un custom comparator para cumplir con el ejercicio + if(team1.points != team2.points) return team1.points > team2.points; + if(team1.wins != team2.wins) return team1.wins > team2.wins; + if(team1.GD != team2.GD) return team1.GD > team2.GD; + if(team1.GS != team2.GS) return team1.GS > team2.GS; + if(team1.games != team2.games) return team1.games < team2.games; + return strcmp(team1.cmpName, team2.cmpName) < 0; +} + +int find(team arr[31], int size, string teamS){ + for(int i = 0; i < size; i++){ + if(strcmp(arr[i].name.c_str(),teamS.c_str())==0) return i; + } + return -1; +} + +int main(){ + int N, T, G, goals1, goals2, cont = 0; + string tournamentName, game, team1, team2, line; + cin >> N; + getline(cin, tournamentName); //Get new line + while(cont < N){ + team teams[32]; + getline(cin, tournamentName); + printf("%s\n", tournamentName.c_str()); + cin >> T; + getline(cin, tournamentName); //Get new line + for(int i = 0; i < T; i++){ + teams[i].init(); + getline(cin, teams[i].name); + toLower(teams[i].cmpName, teams[i].name); + } + cin >> G; + getline(cin, game); //Get new line + for(int i = 0; i < G; i++){ + getline(cin, game); + bool hashF = false; + team1 = ""; + team2 = ""; + goals1 = 0; + goals2 = 0; + int index = 0; + for(index = 0; game[index] != '@'; index++){ //Concatenar caracter a caracter para solucionar errores + if(game[index] == '#'){ + hashF = true; + continue; + } + if(!hashF) team1 += game[index]; + else if(isdigit(game[index])) goals1 = goals1*10 + (game[index] - '0'); + } + hashF = false; + for(int j = index + 1; game[j] != '\0'; j++){ + if(game[j] == '#'){ + hashF = true; + continue; + } + if(hashF) team2 += game[j]; + else if(isdigit(game[j])) goals2 = goals2*10 + (game[j] - '0'); + } + int iTeam1 = find(teams, T, team1); + int iTeam2 = find(teams, T, team2); + teams[iTeam1].game(goals1, goals2); + teams[iTeam2].game(goals2, goals1); + } + sort(teams, teams+T, cmp); + for (int i = 0; i < T; i++) + printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", + (i + 1), teams[i].name.c_str(), teams[i].points, teams[i].games, + teams[i].wins, teams[i].ties, teams[i].loses, teams[i].GD, + teams[i].GS, teams[i].GA); + if (cont != N - 1) + printf("\n"); + cont++; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10194/10194-20.cpp b/uva_cpp_clean/10194/10194-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86296dcb36abf4690a98097f9e52dd1249d2e69d --- /dev/null +++ b/uva_cpp_clean/10194/10194-20.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +const int MAXN = 30; + +struct team { + char name[MAXN+5]; + int point, match_played, win, lose, draw, goal_scored, goal_conceded, goal_difference; + + team() { + point = match_played = win = lose = draw = goal_scored = goal_conceded = goal_difference = 0; + } + void update(int a, int b) { + ++match_played; + if(a > b) { + ++win; + point += 3; + } + else if(a < b) ++lose; + else { + ++draw; + ++point; + } + goal_scored += a; + goal_conceded += b; + goal_difference = goal_scored - goal_conceded; + } +}; + +bool cmp(team a, team b) { + if(a.point != b.point) return (a.point > b.point); + else if(a.win != b.win) return (a.win > b.win); + else if(a.goal_difference != b.goal_difference) return (a.goal_difference > b.goal_difference); + else if(a.goal_scored != b.goal_scored) return (a.goal_scored > b.goal_scored); + else if(a.match_played != b.match_played) return (a.match_played < b.match_played); + else { + for(int i = 0; ; ++i) { + char n1 = a.name[i]; + char n2 = b.name[i]; + if(n1 >= 'A' && n1 <= 'Z') n1 += 32; + if(n2 >= 'A' && n2 <= 'Z') n2 += 32; + + if(n1 == NULL) return true; + if(n2 == NULL) return false; + if(n1 != n2) return (n1 < n2); + } + } +} + +int main() { + #ifndef ONLINE_JUDGE + freopen("in3.txt", "r", stdin); + freopen("out.txt", "w", stdout); + #endif // ONLINE_JUDGE + + int N; + cin >> N >> ws; + while(N--) { + string tournament_name; + getline(cin, tournament_name); + int T; + cin >> T >> ws; + + team data[MAXN+5]; + unordered_map< string, int > m; + for(int i = 0; i < T; ++i) { + gets(data[i].name); + m[data[i].name] = i; + } + + int G; + cin >> G >> ws; + for(int i = 0; i < G; ++i) { + char input[(MAXN*2)+10]; + gets(input); + + char team_name_1[MAXN+5], team_name_2[MAXN+5]; + int goals_1, goals_2; + sscanf(input, "%[^#]#%d@%d#%[^\n]", team_name_1, &goals_1, &goals_2, team_name_2); + data[m[team_name_1]].update(goals_1, goals_2); + data[m[team_name_2]].update(goals_2, goals_1); + } + + sort(data, data + T, cmp); + + cout << tournament_name << "\n"; + for(int i = 0; i < T; ++i) { + printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", (i + 1), data[i].name, data[i].point, data[i].match_played, data[i].win, data[i].draw, data[i].lose, data[i].goal_difference, data[i].goal_scored, data[i].goal_conceded); + } + if(N) cout << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/10194/10194-21.cpp b/uva_cpp_clean/10194/10194-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f70d359a188575d5009f7b3d8d87ced23ecfbd3 --- /dev/null +++ b/uva_cpp_clean/10194/10194-21.cpp @@ -0,0 +1,131 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10194 + Name: Football (aka Soccer) + Problem: https://onlinejudge.org/external/101/10194.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define MAXL 500 + +int readNum() { + int x; cin>>x; + while (getchar()!='\n'); + + return x; +} + + +struct Team { + char name[40]; + int wins, ties, loses, goali, goalj; + Team(const char nm[]) { + strcpy(name, nm); + wins=ties=loses=goali=goalj=0; + } + + bool operator < (const Team& t) const { + int p1 = wins*3 + ties, + p2 = t.wins*3 + t.ties; + if (p1 != p2) + return p1 > p2; + + if (wins != t.wins) + return wins > t.wins; + + int gd1 = goali - goalj, + gd2 = t.goali - t.goalj; + if (gd1 != gd2) + return gd1 > gd2; + + if (goali != t.goali) + return goali > t.goali; + + int gp1 = wins + ties + loses, + gp2 = t.wins + t.ties + t.loses; + if (gp1 != gp2) + return gp1 < gp2; + + + int i; + for (i=0; name[i] && t.name[i]; i++) { + int dif = tolower(name[i]) - tolower(t.name[i]); + if (dif) return dif < 0; + } + return name[i] < t.name[i]; + } +}; + + + + +int main(){ + char tname[MAXL]; + int T = readNum(); + while (T--) { + cin.getline(tname, MAXL); + puts(tname); + + map ids; + vector teams; + + int n = readNum(); + for (int i=0; i vs) { + teams[u].wins++; + teams[v].loses++; + } + else { + teams[u].ties++; + teams[v].ties++; + } + } + + sort(teams.begin(), teams.end()); + for (int i=0; i + +using namespace std; + +struct Team{ + string name; + int b, c, d, e, f, g, h, i; +} tmp; + +int tc, n, g, goals1, goals2, pos1, pos2; +string tournament_name, line, team1, team2, xx, yy; +vector v; + +int findTeam(string x){ + for(int i = 0;i < v.size(); i++) if(v[i].name == x) return i; +} + +bool cmp(Team x,Team y){ + if(x.b != y.b) return (x.b > y.b); + if(x.d != y.d) return (x.d > y.d); + if(x.g != y.g) return (x.g > y.g); + if(x.h != y.h) return (x.h > y.h); + if(x.c != y.c) return (x.c < y.c); + xx = x.name, yy = y.name; + transform(xx.begin(), xx.end(), xx.begin(), ::tolower); + transform(yy.begin(), yy.end(), yy.begin(), ::tolower); + return (xx < yy); +} + +void printResults(){ + cout << tournament_name << endl; + for(int i = 0; i < v.size(); i++) + printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", i + 1, v[i].name.c_str(), v[i].b, v[i].c, v[i].d, v[i].e, v[i].f, v[i].g, v[i].h, v[i].i); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + v.clear(); + getline(cin, tournament_name); + scanf("%d\n", &n); + for(int i = 0;i < n; i++){ + getline(cin, line); + tmp.name = line; + v.push_back(tmp); + } + scanf("%d\n", &g); + while(g--){ + getline(cin, line); + team1 = line.substr(0, line.find("#")); + line.replace(0, line.find("#") + 1, ""); + goals1 = stoi(line.substr(0, line.find("@"))); + line.replace(0, line.find("@") + 1, ""); + goals2 = stoi(line.substr(0, line.find("#"))); + line.replace(0, line.find("#") + 1, ""); + team2 = line; + pos1 = findTeam(team1); + pos2 = findTeam(team2); + v[pos1].c += 1, v[pos2].c += 1; + if(goals1 > goals2) v[pos1].d += 1, v[pos2].f += 1, v[pos1].b += 3; + else if(goals1 < goals2) v[pos1].f += 1, v[pos2].d += 1, v[pos2].b += 3; + else v[pos1].e += 1, v[pos2].e += 1, v[pos1].b += 1, v[pos2].b += 1; + v[pos1].h += goals1, v[pos1].i += goals2; + v[pos2].h += goals2, v[pos2].i += goals1; + v[pos1].g = v[pos1].h-v[pos1].i; + v[pos2].g = v[pos2].h-v[pos2].i; + } + sort(v.begin(), v.end(), cmp); + printResults(); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10195/10195-20.cpp b/uva_cpp_clean/10195/10195-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc0ca509fd8b2e84b98db408915433fa634845d7 --- /dev/null +++ b/uva_cpp_clean/10195/10195-20.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + double a, b, c; + + while( cin >> a >> b >> c ) + { + if( a==0.0 || b==0.0 || c==0.0 ) + cout << "The radius of the round table is: 0.000" << endl; + else + { + double s = (a+b+c)/2; + double r = (sqrt(s*(s-a)*(s-b)*(s-c)))/s; + + cout << "The radius of the round table is: " << fixed << setprecision(3) << r << endl; + } + } + return 0; +} diff --git a/uva_cpp_clean/10195/10195-21.cpp b/uva_cpp_clean/10195/10195-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5739cca0a7e3e26ad80269064448bf4a59ecca0 --- /dev/null +++ b/uva_cpp_clean/10195/10195-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10195 + Name: The Knights Of The Round Table + Problem: https://onlinejudge.org/external/101/10195.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +int main(){ + double a,b,c,s; + while(scanf("%lf%lf%lf",&a,&b,&c)==3){ + s=(a+b+c)/2.0; + if(a>0 && b>0 && c>0) + printf("The radius of the round table is: %.3lf\n",sqrt(((s-a)*(s-b)*(s-c))/s)); + else printf("The radius of the round table is: 0.000\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10195/10195-5.cpp b/uva_cpp_clean/10195/10195-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..230a4cd62d20dd43395470633257c84bb10838fa --- /dev/null +++ b/uva_cpp_clean/10195/10195-5.cpp @@ -0,0 +1,18 @@ +#include +#include +int main () +{ + double a,b,c,s,r; + while(scanf("%lf%lf%lf",&a,&b,&c)==3) + { + if(a<=0||b<=0||c<=0) + { + printf("The radius of the round table is: 0.000\n"); + continue; + } + s=(a+b+c)/2; + r=(sqrt(s*(s-a)*(s-b)*(s-c)))/s; + printf("The radius of the round table is: %.3lf\n",r); + } + return 0; +} diff --git a/uva_cpp_clean/10196/10196-9.cpp b/uva_cpp_clean/10196/10196-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df00fbdd15adc43d36e3ae929cb3740a082e6c1b --- /dev/null +++ b/uva_cpp_clean/10196/10196-9.cpp @@ -0,0 +1,101 @@ +#include + +#define SIZE 10 + +using namespace std; + +int tc, r, c; +char table[SIZE][SIZE]; +bool white, black; +int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; +int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; +int dxx[] = {2, 1, -1, -2, -2, -1, 1, 2}; +int dyy[] = {1, 2, 2, 1, -1, -2, -2, -1}; + +bool empty(){ + for(int row = 0; row < 8; row++) + for(int col = 0; col < 8; col++) + if( table[row][col] != '.' ) return false; + return true; +} + +inline bool isValid(int X, int Y){ + return ( X >= 0 and Y >= 0 and X < 8 and Y < 8 ); +} + +bool proof(char ch, int row, int col, int it, int d){ + if( ch == 'k' ){ + if( islower(table[row][col]) ) return false; + if( table[row][col] == 'P' ){ + if( ( it == 1 or it == 3 ) and d == 1 ) return true; + return false; + } + if( table[row][col] == 'R' ){ + if( it % 2 == 0 ) return true; + return false; + } + if( table[row][col] == 'B' ){ + if( it % 2 == 1 ) return true; + return false; + } + if( table[row][col] == 'Q' ){ + return true; + } + return false; + } + if( ch == 'K' ){ + if( isupper(table[row][col]) ) return false; + if( table[row][col] == 'p' ){ + if( ( it == 5 or it == 7 ) and d == 1 ) return true; + return false; + } + if( table[row][col] == 'r' ){ + if( it % 2 == 0 ) return true; + return false; + } + if( table[row][col] == 'b' ){ + if( it % 2 == 1 ) return true; + return false; + } + if( table[row][col] == 'q' ){ + return true; + } + return false; + } +} + +bool check(char ch){ + for(int row = 0; row < 8; row++) + for(int col = 0; col < 8; col++) + if( table[row][col] == ch ) + r = row, c = col, row = col = 8; + bool ok = false; + for(int it = 0; it < 8; it++){ + for(int d = 1; d < 8; d++){ + int R = r + d * dy[it], C = c + d * dx[it]; + if( isValid(R, C) and table[R][C] != '.' ) + ok |= proof(ch, R, C, it, d), d = 8; + } + } + for(int it = 0; it < 8; it++){ + int R = r + dyy[it], C = c + dxx[it]; + if( isValid(R, C) and ( ( ch == 'k' and table[R][C] == 'N' ) or ( ch == 'K' and table[R][C] == 'n' ) ) ) + ok = true; + } + return ok; +} + +int main(){ + while(true){ + if(tc != 0) getchar(); + for(int row = 0; row < 8; row++) scanf("%s", table[row]); + if( empty() ) break; + white = check('K'); + black = check('k'); + printf("Game #%d: ", ++tc); + if( white ) puts("white king is in check."); + else if( black ) puts("black king is in check."); + else puts("no king is in check."); + } + return(0); +} diff --git a/uva_cpp_clean/10198/10198-19.cpp b/uva_cpp_clean/10198/10198-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fa3e5df1fa330bcd2814749161fece164a6a97e --- /dev/null +++ b/uva_cpp_clean/10198/10198-19.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +const unsigned NUM=100; +typedef long long int base_t; +const base_t BASE=1000000000; + +struct bigint{ + bigint(long long int i=0) :used(0),sign((i>=0)?1:-1){ + memset(inner,0,NUM*sizeof(base_t)); + i*=sign; + for(;i>=BASE;i=i/BASE) inner[used++]=i%BASE; + inner[used++]=i; + } + bigint& operator+=(const bigint& b){ + used=min(max(b.used,used)+1, NUM); base_t carry=0; + for(int i=0;iused && inner[j-1]!=0) used=j; + return *this; + } + bigint operator+(base_t b) { bigint r(*this); return r+=b; } + + string str() const { + stringstream ss; + ss << sign*inner[used-1]; + for(int i=used-2;i>=0;i--) + ss<<"00000000"+(int)(inner[i] ? floor(log10(inner[i])) : 0) + << inner[i]; + return ss.str(); + } + char sign; + unsigned used; + base_t inner[NUM]; +}; +bigint operator+(long long int i, const bigint& l) { return l+i; } + +vector dp; + +void proc() { + dp.push_back(bigint(1)); + dp.push_back(bigint(2)); + dp.push_back(bigint(5)); + for (int i = 3; i <= 1000; i++) { + dp.push_back(dp[i - 1] + dp[i - 1] + dp[i - 2] + dp[i - 3]); + } +} + +/*vector dp2(1001, 0); + +long long proc2(int n){ + if (n < 0) return 0; + if (n == 0) return 1; + if (dp2[n] == 0) { + dp2[n] = 2*proc2(n - 1) + proc2(n - 2) + proc2(n - 3); + } + return dp2[n]; +}*/ + +int main() { + proc(); + int n; + while (cin >> n) { + printf("%s\n", dp[n].str().c_str()); + //printf("%lld\n", proc2(n)); + } + return 0; +} diff --git a/uva_cpp_clean/10198/10198-21.cpp b/uva_cpp_clean/10198/10198-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..281334ae3dfe6f359216558284e879420427e238 --- /dev/null +++ b/uva_cpp_clean/10198/10198-21.cpp @@ -0,0 +1,256 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10198 + Name: Counting + Problem: https://onlinejudge.org/external/101/10198.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + memset(ncells+size, 0, (capacity-size)*CellSize); + + if (cells) delete [] cells; + cells = ncells; + } + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + BigInt(const BigInt& o): cells(0), capacity(0) { + sign = o.sign; + setSize(o.size, 0); + for (int i=0; i= 0) + delete [] cells; + } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { return sign==o.sign ? numLess(o) : sign < o.sign; } + bool operator == (const BigInt &o) const { return sign==o.sign ? numEq(o) : 0; } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + setSize(max(size, o.size)+1); + for (int i=0; i= MAX_C ) { + cells[i] -= MAX_C; + cells[i+1]++; + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + + +int main(){ + BigInt DP[1002]; + DP[0] = BigInt(1); + DP[1] = BigInt(2); + DP[2] = BigInt(5); + for (int i=3; i<1002; i++) + DP[i] = 2*DP[i-1] + DP[i-2] + DP[i-3]; + + int n; + while (cin>>n) + cout << DP[n] << endl; +} diff --git a/uva_cpp_clean/10199/10199-12.cpp b/uva_cpp_clean/10199/10199-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ee8449f69ea2535e0f0ec6208900427afc899ff --- /dev/null +++ b/uva_cpp_clean/10199/10199-12.cpp @@ -0,0 +1,158 @@ +#include +using namespace std; + +#define fastio \ + ios_base::sync_with_stdio(0); \ + cin.tie(NULL); \ + cout.tie(NULL) +#define PI(a, b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long int +#define S second +#define F first +#define FOR(var, len) for (var = 0; var < len; var++) +#define debug1 cout << "debug1" << '\n' +#define debug2 cout << "debug2" << '\n' +#define debug3 cout << "debug3" << '\n' + +// Maths Utils +int binExp(int a, int b, int m) +{ + int r = 1; + while (b) + { + if (b % 2 == 1) + r = (r * a) % m; + + a = (a * a) % m; + b = b / 2; + } + return r; +} + +map indexCity; +map cityIndex; + +const int N = 105; +vector adj[N]; +set camera; +int visited[N], dis[N], low[N]; +int cnt; +void IS_AP(int cam) +{ + camera.insert(indexCity[cam]); +} + +void dfs(int s, int p = -1) +{ + int child = 0; + visited[s] = 1; + dis[s] = low[s] = cnt++; + + for (auto v : adj[s]) + { + if (v == p) + continue; + if (visited[v]) + + low[s] = min(low[s], dis[v]); + + else + { + dfs(v, s); + // update low for s from nodes in its dfs spanning tree + low[s] = min(low[s], low[v]); + // check AP + if (low[v] >= dis[s] and p != -1) + { //p!=-1 for exculding root + IS_AP(s); + } + child++; + } + } + // special case for root + if (p == -1 and child > 1) + { + IS_AP(s); + } +} + +void findCamera(int n, int t) +{ + + + + for (int i = 0; i < n; i++) + if (!visited[i]) + dfs(i); + + cout << "City map #" << t << ": "; + cout << camera.size() << " camera(s) found\n"; + // sort(camera.begin(), camera.end()); + for (auto x : camera) + { + cout << x << '\n'; + } + // cout<<'\n'; +} + +int32_t main() +{ + fastio; + int n, t = 0; + while (1) + { + cin >> n; + if (n == 0) + break; + if(t)cout<<'\n'; + + // preprocessing + cnt = 0; + for (int i = 0; i < N; i++) + { + visited[i] = 0; + dis[i] = -1; + low[i] = -1; + adj[i].clear(); + } + indexCity.clear(); + cityIndex.clear(); + camera.clear(); + + + t++; + string city, a, b; + + for (int i = 0; i < n; i++) + { + cin >> city; + cityIndex.insert(MP(city, i)); + indexCity.insert(MP(i, city)); + } + + int m, x, y; + cin >> m; + + + + for (int i = 0; i < m; i++) + { + cin >> a >> b; + x = cityIndex[a]; + y = cityIndex[b]; + adj[x].EB(y); + adj[y].EB(x); + } + + + + findCamera(n, t); + for(int i = 0;i +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 110 +#define INF -1 + +bool start = false; +int childRoot, root, d = 0, c = 1, n, m; +int parent[MAX], low_time[MAX], enter_time[MAX]; + +vector f; +vector points; +map names; +vector in; +vector > graph; + +void initialize() +{ + d = 0; + + for (int i = 0; i < MAX; ++i) + { + parent[i] = INF; + enter_time[i] = INF; + low_time[i] = 0; + } + + names.clear(); + in.clear(); + f.clear(); + + graph.clear(); + graph.resize((unsigned)n); + + points.clear(); + points.resize((unsigned)n, false); +} + +void findCutVertex(int v) +{ + enter_time[v] = low_time[v] = d++; + + for (int i = 0; i < graph[v].size(); ++i) + { + int u = graph[v][i]; + + if (enter_time[u] == INF) + { + parent[u] = v; + + if (v == root) + { + childRoot++; + } + + findCutVertex(u); + + if (low_time[u] >= enter_time[v]) + { + points[v] = true; + } + + low_time[v] = min(low_time[v], low_time[u]); + } + + else if (u != parent[v]) + { + low_time[v] = min(low_time[v], enter_time[u]); + } + } +} + +int main() +{ + while (cin >> n && n != 0) + { + initialize(); + + for (int i = 0; i < n; ++i) + { + string s; + + cin >> s; + + names[s] = i + 1; + + in.push_back(s); + } + + cin >> m; + + for (int j = 0; j < m; ++j) + { + string v, u; + + cin >> v >> u; + + graph[names[v] - 1].push_back(names[u] - 1); + graph[names[u] - 1].push_back(names[v] - 1); + } + + for (int k = 0; k < n; ++k) + { + if (enter_time[k] == INF) + { + root = k; + childRoot = 0; + findCutVertex(k); + points[k] = (childRoot > 1); + } + } + + int ans = 0; + + for (int l = 0; l < n; ++l) + { + if (points[l]) + { + ans++; + f.push_back(in[l]); + } + } + + sort(f.begin(), f.end()); + + if (!start) start = true; + else cout << endl; + + printf("City map #%d: %lu camera(s) found\n", c++, ans); + + for (int i = 0; i < f.size(); ++i) + { + cout << f[i] << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10199/10199-14.cpp b/uva_cpp_clean/10199/10199-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbbbcd309debd1ef216499e0ae05ae3c39d72d84 --- /dev/null +++ b/uva_cpp_clean/10199/10199-14.cpp @@ -0,0 +1,149 @@ +/*************************************************** + * Problem Name : 10199 Tourist Guide.cpp + * Problem Link : https://uva.onlinejudge.org/external/101/10199.pdf + * OJ : Codeforces + * Verdict : AC + * Date : 2018-08-11 + * Problem Type : Graph (Articulation Point) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 105 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +vectorans; +mapmp; +int n, m, t; +int dis[MAX], low[MAX]; +bool vis[MAX], chk[MAX]; +void dfs (int u, int p) { + vis[u] = true; + dis[u] = low[u] = t++; + int child = 0; + + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + if (vis[v]) { + low[u] = min (low[u], dis[v]); + + } else { + dfs (v, u); + low[u] = min (low[u], low[v]); + + if (dis[u] <= low[v] && p != -1 && !chk[u]) { + ans.pb (mp[u]); + chk[u] = true; + } + + child++; + } + } + + if (p == -1 && child > 1 && !chk[u]) ans.pb (mp[u]); +} +void find_articulation_point() { + t = 0; + SET (vis, false); + SET (chk, false); + SET (dis, -1); + SET (low, -1); + ans.clear(); + + for (int i = 1; i <= n; i++) if (!vis[i]) dfs (i, -1); + + for (int i = 0; i < MAX; i++) adj[i].clear(); + + mp.clear(); +} +int main () { + __FastIO; + int tc = 1; + + while (cin >> n) { + if (n == 0) break; + + mapname; + + for (int i = 1; i <= n; i++) { + string str; + cin >> str; + + if (!name[str]) name[str] = i, mp[i] = str; + } + + cin >> m; + + for (int i = 0; i < m; i++) { + string a, b; + cin >> a >> b; + adj[name[a]].pb (name[b]); + adj[name[b]].pb (name[a]); + } + + if (tc != 1) nl; + + find_articulation_point(); + sort (all (ans) ); + cout << "City map #" << tc++ << ": " << (int) ans.size() << + " camera(s) found\n"; + + for (int i = 0; i < (int) ans.size(); i++) { + cout << ans[i] << "\n"; + } + } + + return 0; +} + diff --git a/uva_cpp_clean/10199/10199-21.cpp b/uva_cpp_clean/10199/10199-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7693915d3a796fb4ae437df556273e03df9cebab --- /dev/null +++ b/uva_cpp_clean/10199/10199-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10199 + Name: Tourist Guide + Problem: https://onlinejudge.org/external/101/10199.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define MAXL 50 +#define MAXN 101 + +string names[MAXN]; +map ids; +bool comp(int u, int v) { return names[u] < names[v]; } + +list adj[MAXN]; +int parent[MAXN], depth[MAXN], low[MAXN]; + +vector arts; +void dfs(int u, int d) { + low[u] = depth[u] = d; + bool isArt = false; + int childs = 0; + + for (int v: adj[u]) + if (depth[v] == -1) { + parent[v] = u; + dfs(v, d+1); + childs++; + if (low[v] >= d) + isArt = true; + else if (low[v] < low[u]) + low[u] = low[v]; + } + else if (v != parent[u]) + low[u] = min(low[u], depth[v]); + + if ((d==0 && childs>1) || (d!=0 && isArt)) + arts.push_back(u); +} + + +int main(){ + int cse=1, n, m; + char w1[MAXL], w2[MAXL]; + while (cin>>n && n) { + ids.clear(); + for (int i=0; i>w1; + ids[w1] = i; + names[i] = w1; + depth[i] = -1; + adj[i].clear(); + } + + cin>>m; + for (int i=0; i>w1>>w2; + int u=ids[w1], v=ids[w2]; + adj[u].push_back(v); + adj[v].push_back(u); + } + + arts.clear(); + for (int i=0; i 1) putchar('\n'); + printf("City map #%d: %lu camera(s) found\n", cse++, arts.size()); + for (int u: arts) + cout << names[u] << endl; + } +} diff --git a/uva_cpp_clean/10199/10199-6.cc b/uva_cpp_clean/10199/10199-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..96e28be417541ab283a803a539e1426afb0edbbb --- /dev/null +++ b/uva_cpp_clean/10199/10199-6.cc @@ -0,0 +1,59 @@ +// https://uva.onlinejudge.org/external/101/10199.pdf +#include +using namespace std; +using ii=tuple; +using vi=vector; +using vii=vector; +using vvi=vector; +using vs=vector; +using msi=unordered_map; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(int T=1;;T++){ + int n,m,u,v; + cin>>n; + if(!n)break; + msi c; + vs x(n); + for(int i=0;i>x[i]; + c[x[i]]=i; + } + cin>>m; + vvi g(n); + for(int i=0;i>s>>t; + u=c[s];v=c[t]; + g[u].push_back(v); + g[v].push_back(u); + } + vi a(n),b(n),d(n); + int k=1; + functiondfs=[&](int u,int p){ + a[u]=k++; + b[u]=a[u]; + int c=0; + for(int v:g[u]) + if(!a[v]){ + c++; + dfs(v,u); + b[u]=min(b[u],b[v]); + if(b[v]>=a[u])d[u]=1; + }else if(v!=p)b[u]=min(b[u],a[v]); + if(p==-1)d[u]=c>1; + }; + for(int i=0;i1)cout<<"\n"; + cout<<"City map #"< + +using namespace std; + +const int MAX_V = 110, LEN = 35; + +int tc, V, E, dfsCounter, dfsRoot, rootChildren, dfs_low[MAX_V], dfs_num[MAX_V], dfs_parent[MAX_V]; +bool Articulation_Point[MAX_V]; +char word[MAX_V][LEN], word1[LEN], word2[LEN]; +vector AdjList[MAX_V]; +map mp; +vector ans; + +void dfs(int u){ + dfs_low[u] = dfs_num[u] = ++dfsCounter; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not dfs_num[v] ){ + dfs_parent[v] = u; + dfs(v); + if( u == dfsRoot ) rootChildren++; + if( dfs_low[v] >= dfs_num[u] ) Articulation_Point[u] = true; + dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + else if( v != dfs_parent[u] ) + dfs_low[u] = min(dfs_low[u], dfs_num[v]); + } +} + +void ArticulationPoints(){ + fill(dfs_low, dfs_low + V, 0); + fill(dfs_num, dfs_num + V, 0); + fill(dfs_parent, dfs_parent + V, -1); + fill(Articulation_Point, Articulation_Point + V, false); + dfsCounter = 0; + for(int u = 0; u < V; u++) + if( not dfs_num[u] ){ + dfsRoot = u, rootChildren = 0; + dfs(dfsRoot); + Articulation_Point[dfsRoot] = ( rootChildren > 1 ); + } +} + +void solve(){ + ArticulationPoints(); + for(int i = 0; i < V; i++) + if( Articulation_Point[i] ) ans.push_back( word[i] ); + sort(ans.begin(), ans.end()); + if( tc++ ) putchar('\n'); + printf("City map #%d: %d camera(s) found\n", tc, (int)ans.size()); + for(int i = 0; i < ans.size(); i++) printf("%s\n", ans[i].c_str()); +} + +void read(){ + for(int i = 0; i < V; i++){ + scanf("%s", word[i]); + mp[word[i]] = i; + } + scanf("%d\n", &E); + for(int i = 0; i < E; i++){ + scanf("%s %s\n", word1, word2); + int u = mp[word1], v = mp[word2]; + AdjList[u].push_back(v); + AdjList[v].push_back(u); + } +} + +void clear(){ + mp.clear(); + for(int u = 0; u < V; u++) AdjList[u].clear(); + ans.clear(); +} + +int main(){ + while(scanf("%d\n", &V), V){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/102/102-14.cpp b/uva_cpp_clean/102/102-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b796747fc83810ab6474c9c48ef21b4d5ee6241 --- /dev/null +++ b/uva_cpp_clean/102/102-14.cpp @@ -0,0 +1,91 @@ +/*************************************************** + * Problem name : 102 Ecological Bin Packing.cpp + * Problem Link : https://uva.onlinejudge.org/external/1/102.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-12-21 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100000 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int ar[15]; + mapmp; + mp[1] = "BGC", mp[2] = "BCG", mp[3] = "GBC", mp[4] = "GCB", mp[5] = "CBG", mp[6] = "CGB"; + //mp[1] = "BCG", mp[2] = "BGC", mp[3] = "CBG", mp[4] = "CGB", mp[5] = "GBC", mp[6] = "GCB"; + while (scanf("%d", &ar[0]) == 1) { + for (int i = 1 ; i < 9; i++) { + scanf("%d", &ar[i]); + } + int arr[7]; + arr[1] = ar[1] + ar[2] + ar[3] + ar[5] + ar[6] + ar[7]; + arr[2] = ar[1] + ar[2] + ar[3] + ar[4] + ar[6] + ar[8]; + arr[3] = ar[0] + ar[2] + ar[4] + ar[5] + ar[6] + ar[7]; + arr[4] = ar[0] + ar[2] + ar[3] + ar[4] + ar[7] + ar[8]; + arr[5] = ar[0] + ar[1] + ar[4] + ar[5] + ar[6] + ar[8]; + arr[6] = ar[0] + ar[1] + ar[3] + ar[5] + ar[7] + ar[8]; + int mn = arr[1], ans = 1; + for (int i = 2; i <= 6; i++) { + if (mn == arr[i]) { + if (mp[ans] > mp[i]) { + mn = arr[i]; + ans = i; + } + } else if (mn > arr[i]) { + mn = arr[i]; + ans = i; + } + } + cout << mp[ans] << " " << mn << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/102/102-16.cpp b/uva_cpp_clean/102/102-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72ff13eea5748ff1f9fe4e8fcea667c29277d771 --- /dev/null +++ b/uva_cpp_clean/102/102-16.cpp @@ -0,0 +1,66 @@ +//============================================================================ +// Name : 102 - Ecological Bin Packing.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 102 Ecological Bin Packing in C++, Ansi-style +// Run Time : 0.120 seconds +//============================================================================ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + int sum, min, ip, ians; + int bottle[3][3]; + string str[] = { + "BCG", "BGC", "CBG", "CGB", "GBC", "GCB" + }; + while (cin >> min) { + int permu[] = {0, 1, 2}; + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + if (i == j && j == 0) { + bottle[i][j] = min; + } else { + cin >> bottle[i][j]; + } + } + swap (bottle[i][1], bottle[i][2]); + } + min = 2147483647; + ians = ip = 0; + do { + sum = 0; + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + if (j != permu[i]) { + sum += bottle[i][j]; + } + } + } + if (min > sum) { + min = sum; + ians = ip; + } + ip++; + } while (next_permutation(permu, permu + 3)); + cout << str[ians] << ' ' << min << endl; + } + return 0; +} diff --git a/uva_cpp_clean/102/102-19.cpp b/uva_cpp_clean/102/102-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36d57dfc16c65cf0f4f2afe86cffc5e58acaecee --- /dev/null +++ b/uva_cpp_clean/102/102-19.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +int main() { + int B[3], G[3], C[3], p[6]; + string s[6] = {"BCG", "BGC", "CBG", "CGB", "GBC", "GCB"}; + while (cin >> B[0] >> G[0] >> C[0] >> B[1] >> G[1] >> C[1] >> B[2] >> G[2] >> C[2]) { + p[0] = G[0] + C[0] + B[1] + G[1] + B[2] + C[2]; + p[1] = G[0] + C[0] + B[1] + C[1] + B[2] + G[2]; + p[2] = G[0] + B[0] + G[1] + C[1] + B[2] + C[2]; + p[3] = G[0] + B[0] + B[1] + C[1] + C[2] + G[2]; + p[4] = B[0] + C[0] + G[1] + C[1] + B[2] + G[2]; + p[5] = B[0] + C[0] + B[1] + G[1] + C[2] + G[2]; + int min = 0; + for (int i = 1; i < 6; i++) if (p[i] +// #include +// using namespace __gnu_pbds; +#define el '\n' +#define F first +#define S second +// #define M_PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI) // Converts radians to degrees. +// #define ordered_set1 tree, rb_tree_tag,tree_order_statistics_node_update> +// typedef tree, null_type, less>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction +/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction + +int main(){ + ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + char first, second, third; + int B[3], G[3], C[3], smallest, tmp; // bottle + while(cin >> B[0] >> G[0] >> C[0] >> B[1] >> G[1] >> C[1] >> B[2] >> G[2] >> C[2]) { + smallest = B[1] + B[2] + C[0] + C[2] + G[0] + G[1]; // BCG + first = 'B'; second = 'C'; third = 'G'; + tmp = B[1] + B[2] + G[0] + G[2] + C[0] + C[1]; // BGC + if(tmp < smallest) { + smallest = tmp; + first = 'B'; second = 'G'; third = 'C'; + } + + tmp = C[1] + C[2] + B[0] + B[2] + G[0] + G[1]; // CBG + if(tmp < smallest) { + smallest = tmp; + first = 'C'; second = 'B'; third = 'G'; + } + + tmp = C[1] + C[2] + G[0] + G[2] + B[0] + B[1]; // CGB + if(tmp < smallest) { + smallest = tmp; + first = 'C'; second = 'G'; third = 'B'; + } + + tmp = G[1] + G[2] + B[0] + B[2] + C[0] + C[1]; // GBC + if(tmp < smallest) { + smallest = tmp; + first = 'G'; second = 'B'; third = 'C'; + } + + tmp = G[1] + G[2] + C[0] + C[2] + B[0] + B[1]; // GCB + if(tmp < smallest) { + smallest = tmp; + first = 'G'; second = 'C'; third = 'B'; + } + cout << first << second << third << " " << smallest << el; + } + return 0; +} + + +/* + + +*/ diff --git a/uva_cpp_clean/102/102-20.cpp b/uva_cpp_clean/102/102-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf26349433ed66907e00a04495fa45cae759d9a8 --- /dev/null +++ b/uva_cpp_clean/102/102-20.cpp @@ -0,0 +1,31 @@ +#include + +const char combination[8][5] = {"BCG", "BGC", "CBG", "CGB", "GBC", "GCB"}; +int b1, g1, c1, b2, g2, c2, b3, g3, c3; +int movement, pos, temp; + +int main() +{ + while( scanf("%d %d %d %d %d %d %d %d %d", &b1, &g1, &c1, &b2, &g2, &c2, &b3, &g3, &c3)!=EOF ) + { + movement = (g1+c1)+(b2+g2)+(b3+c3), pos = 0; + + temp = (g1+c1)+(b2+c2)+(b3+g3); + if( movement>temp ) movement = temp, pos = 1; + + temp = (b1+g1)+(g2+c2)+(b3+c3); + if( movement>temp ) movement = temp, pos = 2; + + temp = (b1+g1)+(b2+c2)+(g3+c3); + if( movement>temp ) movement = temp, pos = 3; + + temp = (b1+c1)+(g2+c2)+(b3+g3); + if( movement>temp ) movement = temp, pos = 4; + + temp = (b1+c1)+(b2+g2)+(g3+c3); + if( movement>temp ) movement = temp, pos = 5; + + printf("%s %d\n", combination[pos], movement); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/102/102-5.cpp b/uva_cpp_clean/102/102-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c700a523856871d4b638356fbdcbc3ae8327350c --- /dev/null +++ b/uva_cpp_clean/102/102-5.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +int main() +{ + long int a[3][3],move[6],sum=0,min; + int p,n,f,i,j; + while(1) + { + sum=0;f=1; + for(i=0;i<3;i++){ + for(j=0;j<3;j++){ + n=sc("%ld",&a[i][j]); + if(n==EOF){ + f=0; + break; + } + sum=sum+a[i][j]; + } + } + if(f==0) + break; + move[0]=a[0][0]+a[1][2]+a[2][1]; + move[1]=a[0][0]+a[1][1]+a[2][2]; + move[2]=a[0][2]+a[1][0]+a[2][1]; + move[3]=a[0][2]+a[1][1]+a[2][0]; + move[4]=a[0][1]+a[1][0]+a[2][2]; + move[5]=a[0][1]+a[1][2]+a[2][0]; + + for(i=0;i<6;i++) + move[i]=sum-move[i]; + + min=move[0]; + p=0; + for(i=1;i<6;i++) + if(min>move[i]) + { + min=move[i]; + p=i; + } + if(p==0) + pf("BCG %ld\n",min); + else if(p==1) + pf("BGC %ld\n",min); + else if(p==2) + pf("CBG %ld\n",min); + else if(p==3) + pf("CGB %ld\n",min); + else if(p==4) + pf("GBC %ld\n",min); + else if(p==5) + pf("GCB %ld\n",min); + } + return 0; +} + diff --git a/uva_cpp_clean/102/102-9.cpp b/uva_cpp_clean/102/102-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cc7975ca06253fbc361c58fc260e08872d56c9e --- /dev/null +++ b/uva_cpp_clean/102/102-9.cpp @@ -0,0 +1,39 @@ +#include +#define INF 2000000000 + +using namespace std; + +int t , ans, aux, tmp; +int cases[] = {0, 2, 1, 0, 1, 2, 2, 0, 1, 2, 1, 0, 1, 0, 2, 1, 2, 0}; +string s = "BGC", ss; +string ans_string; +int a[9]; + +int main(){ + while(scanf("%d", &a[t++]) == 1){ + if(t == 9){ + t = 0; + aux = 0; + ans = INF; + for(int i = 0; i < 9; i++) + aux += a[i]; + for(int i = 0; i < 18; i += 3){ + tmp = aux; + ss.clear(); + for(int j = 0; j < 3; j++){ + ss += s[cases[i + j]]; + if(s[cases[i + j]] == 'B') + tmp -= a[j * 3 + 0]; + else if(s[cases[i + j]] == 'G') + tmp -= a[j * 3 + 1]; + else + tmp -= a[j * 3 + 2]; + } + if(tmp < ans) + ans = tmp, ans_string = ss; + } + cout << ans_string << ' ' << ans << endl; + } + } + return(0); +} diff --git a/uva_cpp_clean/10200/10200-19.cpp b/uva_cpp_clean/10200/10200-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..658ffa3c44498aa0833962415a32db064096c779 --- /dev/null +++ b/uva_cpp_clean/10200/10200-19.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; + +ll sieve_size; +bitset<100010060> bs; +vi eulerPrimes; + +inline void sieve(ll upperbound = 100010050) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + } +} + +int main() { + sieve(); + for (int i = 0; i <= 10000; i++) { + int n = i*i + i + 41; + if (bs[n]) eulerPrimes.push_back(n); + } + int a, b; + while (scanf("%d %d", &a, &b) == 2) { + int val = upper_bound(eulerPrimes.begin(), eulerPrimes.end(), b*b + b + 41) - lower_bound(eulerPrimes.begin(), eulerPrimes.end(), a*a + a + 41); + double ret = round(double(val * 10000) / (b - a + 1)) / 100; + printf("%.2lf\n", ret); + } + return 0; +} diff --git a/uva_cpp_clean/10200/10200-21.cpp b/uva_cpp_clean/10200/10200-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db2ebf29dbaac9c90c9eb4bfefcb139ad127a788 --- /dev/null +++ b/uva_cpp_clean/10200/10200-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10200 + Name: Prime Time + Problem: https://onlinejudge.org/external/102/10200.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool isprime(int n) { + for (int i=2, sq=sqrt(n); i<=sq; ++i) + if (n%i == 0) + return 0; + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[10143] = { 1 }; + for (int i=0; i<10142; ++i) + X[i+1] = X[i] + (isprime(i*i+i+41) ? 1 : 0); + + int a, b; + while (cin >> a >> b) + cout << fixed << setprecision(2) + << round(10000.0*(X[b+1]-X[a]) / (b-a+1))/100 << endl; +} diff --git a/uva_cpp_clean/10201/10201-21.cpp b/uva_cpp_clean/10201/10201-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f0d3afa3ec0cdb909eebfb892e78f63f6978721 --- /dev/null +++ b/uva_cpp_clean/10201/10201-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10201 + Name: Adventures in Moving - Part IV + Problem: https://onlinejudge.org/external/102/10201.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, dist, P[143], D[143], DP[143][286]; + cin >> T; + while (T--) { + cin >> dist; + + int lastd = -100; + bool impo = false; + memset(DP[0], 0, 404); + for (int i=101; i<=20; ++i) + DP[0][i] = INF; + + for (int i=1; cin.ignore(100, '\n') && cin.peek()!='\n' && cin>>D[i]>>P[i]; ++i) { + if (D[i] >= dist) continue; + int d = D[i] - lastd; + if (!(impo |= d > 200)) { + for (int j=0; j<=200; ++j) { + DP[i][j] = INF; + for (int k=min(200, j+d); k>=d; --k) + DP[i][j] = min(DP[i][j], (j-k+d)*P[i] + DP[i-1][k]); + } + } + lastd = D[n=i]; + } + + int d = dist-lastd; + if ((impo |= d > 100)) + cout << "Impossible\n"; + else cout << DP[n][100+d] << "\n"; + if (T) cout << "\n"; + } +} diff --git a/uva_cpp_clean/10203/10203-19.cpp b/uva_cpp_clean/10203/10203-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165f88cb1da03254f9c2345e4b52149dc42be59d --- /dev/null +++ b/uva_cpp_clean/10203/10203-19.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + double v = 20000.0/60.0; + int T; + cin >> T; + while (T--) { + double x0, y0, x1, y1, x2, y2; + cin >> x0 >> y0; + string s; + getline(cin, s); + double d = 0; + while (getline(cin, s) && s != "") { + istringstream iss(s); + iss >> x1 >> y1 >> x2 >> y2; + double x = x2-x1, y = y2-y1; + d += 2*sqrt(x*x + y*y); + } + double t = d/v; + int tmin = rint(t); + int h = tmin/60, min = (tmin - 60*h); + printf("%d:%.2d\n", h, min); + if (T) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10203/10203-21.cpp b/uva_cpp_clean/10203/10203-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2a7a7b6af309da947ae6eabbba1cb130089e04a --- /dev/null +++ b/uva_cpp_clean/10203/10203-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10203 + Name: Snow Clearing + Problem: https://onlinejudge.org/external/102/10203.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, x1, x2, y1, y2; + cin >> T; + while (T--) { + cin >> x1 >> y1; + double sum = 0; + while (cin.ignore(100,'\n') && cin.peek()!='\n') + if (cin >> x1 >> y1 >> x2 >> y2) { + int xx = x1 - x2, + yy = y1 - y2; + sum += sqrt(double(xx*xx + yy*yy)); + } + + int mint = round(60.0 * sum / 10000.0), + hour = mint / 60; + mint %= 60; + + cout << hour << ':' << setw(2) << setfill('0') << mint << "\n"; + if (T) cout << "\n"; + } +} diff --git a/uva_cpp_clean/10205/10205-21.cpp b/uva_cpp_clean/10205/10205-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ffd6b79d02e2c7e19bcc5c98e3a54cb30b01aebd --- /dev/null +++ b/uva_cpp_clean/10205/10205-21.cpp @@ -0,0 +1,57 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10205 + Name: Stack 'em Up + Problem: https://onlinejudge.org/external/102/10205.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +char suits[][9]={"Clubs","Diamonds","Hearts","Spades"}; +char nums[][6]={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"}; + +string name(int x){ + +} + +int main() +{ + int T;cin>>T; + int n,i,j,ay,s,f=0; + int shuffle[100][52]; + char line[10]; + vector cards,tc; + for(i=1;i<53;i++)tc.push_back(i); + + + while(T--){ + if(f)cout<>n; + for(i=0;i>shuffle[i][j]; + cards.clear(); + for(i=1;i<53;i++)cards.push_back(i); + + gets(line); + while(gets(line) && strlen(line)){ + sscanf(line,"%d",&s); + for(i=0;i<52;i++) + tc[i]=cards[shuffle[s-1][i]-1]; + cards=tc; + } + for(i=0;i<52;i++) + printf("%s of %s\n",nums[(cards[i]-1)%13],suits[(int)(cards[i]-1)/13]); + } + + return 0; +} diff --git a/uva_cpp_clean/10209/10209-15.cpp b/uva_cpp_clean/10209/10209-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d784f5075959bdec8088f31d470ee24d31411a9 --- /dev/null +++ b/uva_cpp_clean/10209/10209-15.cpp @@ -0,0 +1,23 @@ +/* + 數學題 +*/ +#include +#include +#define pi 3.1415926535897932 +#define sqrt_3 1.7320508075688773 + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + double edge, striped, dotted, rest; + while (cin >> edge) { + double area = edge * edge; + rest = area * (4 - 2 * pi / 3 - sqrt_3); + dotted = (4 - pi) * area - 2 * rest; + striped = area - dotted - rest; + cout << setprecision(3) << fixed << striped << " " << dotted << " " << rest << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10209/10209-19.cpp b/uva_cpp_clean/10209/10209-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7b2a433e28e228341ce6583ed8b95fe3e753542 --- /dev/null +++ b/uva_cpp_clean/10209/10209-19.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +double pi = 2*acos(0); + +int main() { + double a; + while (cin >> a) { + double area = a*a; + double a1 = 4*area - (2*pi*area/3) - (sqrt(3)*area); + double a2 = (pi*area/2) - area; + double a3 = 2*(area - a1 - a2); + double a4 = area - a1 - a3; + printf("%.3lf %.3lf %.3lf\n", a4, a3, a1); + } + return 0; +} diff --git a/uva_cpp_clean/10209/10209-20.cpp b/uva_cpp_clean/10209/10209-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cda2af17f72798921fb4ce221c71cb1a0916214 --- /dev/null +++ b/uva_cpp_clean/10209/10209-20.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +#define Pi acos(-1) + +using namespace std; + +int main() +{ + double a; + + while( cin >> a ) + { + double area = a*a; + double b = area * (1 - sqrt(3) + Pi / 3); + double c = area * (2 * sqrt(3) - 4 + Pi / 3); + double d = area * (4 - sqrt(3) - 2 * Pi / 3); + + cout << fixed << setprecision(3) << b << " " << fixed << setprecision(3) << c << " " << fixed << setprecision(3) << d << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10209/10209-21.cpp b/uva_cpp_clean/10209/10209-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd51757306a5cd304073304569be14b4e4806e03 --- /dev/null +++ b/uva_cpp_clean/10209/10209-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10209 + Name: Is This Integration ? + Problem: https://onlinejudge.org/external/102/10209.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define PI 3.1415926535897932 + +int main(){ + double k1 = 1 + PI/3 - sqrt(3.0); + double k2 = PI/3 + 2*sqrt(3.0) - 4; + double k3 = 4 - 2*PI/3 - sqrt(3.0); + + double a; + while (cin>>a) { + double a2 = a*a; + printf("%.3f %.3f %.3f\n", k1*a2, k2*a2, k3*a2); + } +} + diff --git a/uva_cpp_clean/10209/10209-9.cpp b/uva_cpp_clean/10209/10209-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b09e19004cf1331a01115360b295224e8409b6d6 --- /dev/null +++ b/uva_cpp_clean/10209/10209-9.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +const double PI = acos(-1.0); + +double L, A, B, C, D, K1, K2, theta, h, ans1, ans2, ans3; + +int main(){ + theta = PI / 6.0; + while(~scanf("%lf", &L)){ + h = sqrt(3) * L / 2.0; + D = L * L * theta / 2.0 - h * L / 4.0; + C = L * ( L - h ) - 2.0 * D; + K1 = PI * L * L / 2.0 - L * L; + K2 = PI * L * L / 4.0 - 2.0 * C; + B = K2 - K1; + A = K1 - 2.0 * B; + ans1 = A, ans2 = 4.0 * B, ans3 = 4.0 * C; + printf("%.3lf %.3lf %.3lf\n", ans1, ans2, ans3); + } + return(0); +} diff --git a/uva_cpp_clean/10212/10212-13.cpp b/uva_cpp_clean/10212/10212-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b36dd1e30111c4c936c283700a8001f7c8bf1507 --- /dev/null +++ b/uva_cpp_clean/10212/10212-13.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +typedef long long ll; + +#define M 1000000000 + +int main() +{ + ll n, m, ans = 1; + + while (cin >> n >> m) + { + ans = 1; + + for (ll i = n; i >= n - m + 1 ; i--) + { + ans *= i; + + while (ans % 10 == 0) + { + ans /= 10; + } + + ans = ans % M; + } + + cout << ans % 10 << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10212/10212-20.cpp b/uva_cpp_clean/10212/10212-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9719835b75a6fc5c649947fc42ae328392cfbb7e --- /dev/null +++ b/uva_cpp_clean/10212/10212-20.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +int main() +{ + long N, M; + + while( cin >> N >> M ) + { + if( M==0 ) cout << "1" << endl; + else + { + long i, result = 1; + for(i=N ; i>=(N-M)+1 ; i--) + { + result *= i; + while( result%10==0 ) result /= 10; + result %= 1000000000; + } + cout << result%10 << endl; + } + } + return 0; +} diff --git a/uva_cpp_clean/10212/10212-21.cpp b/uva_cpp_clean/10212/10212-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4c18b86ffd6efc682c95b85ceb5b1d08f2d353c --- /dev/null +++ b/uva_cpp_clean/10212/10212-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10212 + Name: The Last Non-zero Digit. + Problem: https://onlinejudge.org/external/102/10212.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int powmod(int a, int b) { + if (!b) return 1; + int h = powmod(a, b/2); h *= h; + return (b%2 ? h*a : h) % 10; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m; + while (cin >> n >> m) { + int rs = 1, c2 = 0, c5 = 0; + for (int i=n-m+1; i<=n; ++i) { + int ii = i; + while (ii % 2 == 0) { + ii /= 2; + c2 += 1; + } + while (ii % 5 == 0) { + ii /= 5; + c5 += 1; + } + rs = (rs * ii) % 10; + } + + if (c2 > c5) + rs *= powmod(2, c2-c5); + else + rs *= powmod(5, c5-c2); + + cout << rs%10 << endl; + } +} + diff --git a/uva_cpp_clean/10215/10215-15.cpp b/uva_cpp_clean/10215/10215-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6309341d1ffe9919c629542788f4cee08f0bf04 --- /dev/null +++ b/uva_cpp_clean/10215/10215-15.cpp @@ -0,0 +1,20 @@ +/* + 數學題,記得加1E-9不然會WA +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + double L, W; + while (cin >> L >> W) { + cout << setprecision(3) << fixed << (L + W - sqrt(L * (L - W) + W * W)) / 6 + 1E-9; + cout << " 0.000 " << min(L, W) / 2 + 1E-9 << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10215/10215-9.cpp b/uva_cpp_clean/10215/10215-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99b2d6a8ff63b5a2ce5f933ff0a9d49ac12966ec --- /dev/null +++ b/uva_cpp_clean/10215/10215-9.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +double L, W, A, B, C, D, X1, X2; +const double eps = 1e-7; + +int main(){ + while(scanf("%lf %lf",&L, &W) != EOF){ + A = 12, B = -4 * ( L + W ), C = L * W; + D = sqrt( B * B - 4 * A * C ); + X1 = 2.0 * C / ( -B + D ); + X2 = min( L / 2.0 + eps, W / 2.0 + eps); + printf("%.3lf %.3lf %.3lf\n", X1, 0.000f, X2); + } + return(0); +} diff --git a/uva_cpp_clean/10219/10219-19.cpp b/uva_cpp_clean/10219/10219-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..381edcbd43f3e1dcd0f046583acba7612587518b --- /dev/null +++ b/uva_cpp_clean/10219/10219-19.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include +using namespace std; + +int proc(int n, int m) { + int k = max(m, n-m); + double val = 0; + for (int i = n; i > k; i--) val += log10(i); + for (int i = n - k; i > 0; i--) val -= log10(i); + val += 0.0000005; //Magic touch + return (int) ceil(val); +} + +int main() { + int n, m; + while (cin >> n >> m) printf("%d\n", proc(n, m)); + return 0; +} diff --git a/uva_cpp_clean/10219/10219-21.cpp b/uva_cpp_clean/10219/10219-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4c742978c63054aca0b17788a11b7b90027c108 --- /dev/null +++ b/uva_cpp_clean/10219/10219-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10219 + Name: Find the ways ! + Problem: https://onlinejudge.org/external/102/10219.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAX 21000647 +int l = 2; +double cum[MAX]; +double logf(int n) { + for (; l <= n; l++) + cum[l] = cum[l-1] + log10(l); + return cum[n]; +} + +int main() { + int n, k; + while (cin>>n>>k) + cout << floor(logf(n)-logf(k)-logf(n-k))+1 << endl; +} diff --git a/uva_cpp_clean/10219/10219-5.cpp b/uva_cpp_clean/10219/10219-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..daf11a97a920d0818bd88ed6aeee49d58081b296 --- /dev/null +++ b/uva_cpp_clean/10219/10219-5.cpp @@ -0,0 +1,17 @@ +#include +#include +int main() +{ + long long n, r,i; + long double res; + while(scanf("%lld %lld",&n,&r)==2) + { + res = 0; + for(i=n;i>n-r;i--) + res+=log10(i); + for(i=1;i<=r;i++) + res-=log10(i); + printf("%d\n",(int)floor(res)+1); + } + return 0; +} diff --git a/uva_cpp_clean/10219/10219-9.cpp b/uva_cpp_clean/10219/10219-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aba2dde2bcbf672f9e0a983bb73346a6dbd33d1b --- /dev/null +++ b/uva_cpp_clean/10219/10219-9.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int n, k; + +inline double s(int it, int ct){ + double sum = 0; + while(ct--) sum += log10(it--); + return sum; +} + +inline int solve(){ + return floor( s(n, k) - s(k, k) ) + 1; +} + +int main(){ + while(~scanf("%d %d", &n, &k)) + printf("%d\n", solve()); + return(0); +} diff --git a/uva_cpp_clean/10220/10220-20.cpp b/uva_cpp_clean/10220/10220-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..768996d0dc7ed591e31e179b44be20ca63ec1b23 --- /dev/null +++ b/uva_cpp_clean/10220/10220-20.cpp @@ -0,0 +1,30 @@ +#include + +int factorial[1005][3000]; + +int main() +{ + factorial[0][0] = factorial[1][0] = 1; + for(int i=2 ; i<1005 ; i++) + for(int j=0 ; j<3000 ; j++) + { + factorial[i][j] += factorial[i-1][j]*i; + if( factorial[i][j]>9 ) + { + factorial[i][j+1] += factorial[i][j]/10; + factorial[i][j] %= 10; + } + } + + int n; + + while( scanf("%d", &n)==1 ) + { + int sum = 0; + for(int i=0 ; i<3000 ; i++) + sum += factorial[n][i]; + + printf("%d\n", sum); + } + return 0; +} diff --git a/uva_cpp_clean/10220/10220-21.cpp b/uva_cpp_clean/10220/10220-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..621b445c085add70a2edc7e5af420271c6ab4d57 --- /dev/null +++ b/uva_cpp_clean/10220/10220-21.cpp @@ -0,0 +1,953 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10220 + Name: I Love Big Numbers ! + Problem: https://onlinejudge.org/external/102/10220.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +#ifndef min +#define min(x,y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef max +#define max(x,y) ((x) > (y) ? (x) : (y)) +#endif + +class BigInt +{ +private: + char *digits; + int size; // number of used bytes (digits) + int capacity; // size of digits + int sign; // -1, 0 or +1 + +public: + BigInt(int n, int cap); + BigInt(int n); + BigInt(long double d); + BigInt(); + BigInt(string s); + BigInt(const char s[]); + BigInt(const BigInt &n); + const BigInt &operator=(const BigInt &n); + const BigInt &operator=(int n); + ~BigInt(); + void normalize(); + static int sig(int n); + static int sig(long double n); + inline int length() { return size; } + + BigInt operator++(); + BigInt operator++(int); + BigInt operator--(); + BigInt operator--(int); + BigInt operator-(); + BigInt operator+ (int n); + BigInt operator+ (BigInt n); + BigInt&operator+=(int n); + BigInt&operator+=(BigInt n); + BigInt operator- (int n); + BigInt operator- (BigInt n); + BigInt&operator-=(int n); + BigInt&operator-=(BigInt n); + BigInt operator* (int n); + BigInt operator* (BigInt n); + void operator*=(int n); + void operator*=(BigInt n); + BigInt operator/ (int n); + BigInt operator/ (BigInt n); + void operator/=(int n); + void operator/=(BigInt n); + int operator% (int n); + BigInt operator% (BigInt n); + void operator%=(int n); + void operator%=(BigInt n); + int divide(int n); + BigInt divide(BigInt n); + BigInt operator* (long double n); + void operator*=(long double n); + + + BigInt operator<< (int n); + void operator<<=(int n); + BigInt operator >> (int n); + void operator>>=(int n); + BigInt operator,(int n); + BigInt operator,(BigInt n); + + /** Casting **/ + bool operator!(); + operator bool(); + operator string(); + + bool operator<(BigInt n); + bool operator>(BigInt n); + bool operator==(BigInt n); + bool operator<=(BigInt n); + bool operator>=(BigInt n); + bool operator<(int n); + bool operator>(int n); + bool operator==(int n); + bool operator<=(int n); + bool operator>=(int n); + int compare(BigInt n); + + int toInt(); + string toString(); + void print(); + void printWithCommas(ostream &out); + +private: + void grow(); + friend istream &operator >> (istream &in, BigInt &n); + friend ostream &operator<<(ostream &out, BigInt n); +}; + +BigInt operator+(int m, BigInt &n) +{ + return n + m; +} + +BigInt operator-(int m, BigInt &n) +{ + return -n + m; +} + +BigInt operator*(int m, BigInt &n) +{ + return n * m; +} + +BigInt operator/(int m, BigInt &n) +{ + return BigInt(m) / n; +} + +BigInt operator%(int m, BigInt &n) +{ + return BigInt(m) % n; +} + +inline bool isDigit(int c) +{ + return(c >= (int)'0' && c <= (int)'9'); +} + +istream &operator >> (istream &in, BigInt &n) // FIXME: see inside +{ + n.size = 0; + n.sign = 1; + int sign = 1; + int c; + while ((c = in.peek()) >= 0 && + (c == ' ' || c == '\t' || c == '\r' || c == '\n')) + in.get(); + if (c < 0 || (c != (int)'-' && !isDigit(c))) + { + in >> c; // XXX: force in.fail() + return in; + } + if (c == (int)'-') { sign = -1; in.get(); } + + // FIXME: Extremely inefficient! Use a string. + while ((c = in.peek()) >= 0 && isDigit(c)) + { + in.get(); + n *= 10; + n += (c - (int)'0'); + } + n.sign = sign; + n.normalize(); + return in; +} + +ostream &operator<<(ostream &out, BigInt n) //FIXME: make more efficient +{ + return out << n.toString(); +} + +BigInt::BigInt(int n, int cap) +{ + cap = max(cap, (int)sizeof(n) * 8); + capacity = cap; + sign = sig(n); + n *= sign; + digits = new char[cap]; + memset(digits, 0, cap); + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(int n) +{ + capacity = 1024; + sign = sig(n); + n *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + while (n) + { + digits[size++] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(long double d) +{ + capacity = 1024; + sign = (d < 0 ? -1 : d > 0 ? 1 : 0); + d *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + d = floor(d); + while (d > 0) + { + digits[size++] = min(max(0, (int)((d - floor(d / 10) * 10) + 0.5)), 9); + d = floor(d / 10); + } +} + +BigInt::BigInt() +{ + capacity = 128; + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; +} + +BigInt::BigInt(string s) +{ + capacity = max((int)s.size(), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const char s[]) +{ + capacity = max((int)strlen(s), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const BigInt &n) +{ + capacity = n.capacity; + sign = n.sign; + size = n.size; + digits = new char[capacity]; + memcpy(digits, n.digits, capacity); +} + +const BigInt &BigInt::operator=(const BigInt &n) +{ + if (&n != this) + { + if (capacity < n.size) + { + capacity = n.capacity; + delete[] digits; + digits = new char[capacity]; + } + sign = n.sign; + size = n.size; + memcpy(digits, n.digits, size); + memset(digits + size, 0, capacity - size); + } + return *this; +} + +const BigInt &BigInt::operator=(int n) +{ + sign = sig(n); + n *= sign; + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } + return *this; +} + +BigInt::~BigInt() +{ + delete[] digits; +} + +void BigInt::normalize() +{ + while (size && !digits[size - 1]) size--; + if (!size) sign = 0; +} + +int BigInt::sig(int n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::sig(long double n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::toInt() +{ + int result = 0; + for (int i = size - 1; i >= 0; i--) + { + result *= 10; + result += digits[i]; + if (result < 0) return sign * 0x7FFFFFFF; + } + return sign * result; +} + +string BigInt::toString() +{ + string s = (sign >= 0 ? "" : "-"); + for (int i = size - 1; i >= 0; i--) + s += (digits[i] + '0'); + if (size == 0) s += '0'; + return s; +} + +void BigInt::print() //FIXME: make more efficient +{ + cout << toString(); +} + +void BigInt::printWithCommas(ostream &out) +{ + if (sign < 0) out.put('-'); + for (int i = size - 1; i >= 0; i--) + { + out.put(digits[i] + '0'); + if (!(i % 3) && i) out.put(','); + } + if (size == 0) out.put('0'); +} + +void BigInt::grow() +{ + char *olddigits = digits; + int oldCap = capacity; + capacity *= 2; + digits = new char[capacity]; + memcpy(digits, olddigits, oldCap); + memset(digits + oldCap, 0, oldCap); + delete[] olddigits; +} + +BigInt BigInt::operator++() +{ + operator+=(1); + return *this; +} + +BigInt BigInt::operator++(int) +{ + return operator++(); +} + +BigInt BigInt::operator--() +{ + operator-=(1); + return *this; +} + +BigInt BigInt::operator--(int) +{ + return operator--(); +} + +BigInt BigInt::operator-() +{ + BigInt result(*this); + result.sign *= -1; + return result; +} + +BigInt BigInt::operator+(int n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt BigInt::operator+(BigInt n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt &BigInt::operator+=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = nsign; + if (sign == nsign) + { + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] + dig + carry; + digits[i] = newdig % 10; + carry = newdig / 10; + n /= 10; + } + size = max(i, size); + } + else operator-=(-n); + return *this; +} + +BigInt &BigInt::operator+=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = n.sign; + if (sign == n.sign) + { + int carry = 0; + int i; + for (i = 0; i < maxS - 1 || carry; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig += n.digits[i]; + digits[i] = newdig % 10; + carry = newdig / 10; + } + size = max(i, size); + } + else + { + n.sign *= -1; + operator-=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator-(int n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt BigInt::operator-(BigInt n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt &BigInt::operator-=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = 1; + if (sign == nsign) + { + BigInt bin = n; + if (sign >= 0 && *this < bin || sign < 0 && *this > bin) + { + // Subtracting a bigger number + operator=(toInt() - n); + return *this; + } + + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] - dig + carry; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + n /= 10; + } + normalize(); + } + else operator+=(-n); + return *this; +} + +BigInt &BigInt::operator-=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = 1; + if (sign == n.sign) + { + if (sign >= 0 && *this < n || sign < 0 && *this > n) + { + // Subtracting a bigger number + BigInt tmp = n; + tmp -= *this; + *this = tmp; + sign = -sign; + return *this; + } + + int carry = 0; + int i; + for (i = 0; i < maxS - 1; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig -= n.digits[i]; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + } + if (carry) // Subtracted a bigger number, need to flip sign + { + if (i) digits[0] = 10 - digits[0]; + size = (i ? 1 : 0); + for (int j = 1; j < i; j++) + { + digits[j] = 9 - digits[j]; + if (digits[i]) size = j + 1; + } + sign *= -1; + } + normalize(); + } + else + { + n.sign *= -1; + operator+=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator*(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + int nsign = sig(n); + n *= nsign; + result.sign = sign * nsign; + if (!result.sign) return result; + + int i, j; + for (i = 0; n; i++) + { + int dig = n % 10; + if (dig) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = result.digits[i + j] + (j < size ? dig * digits[j] : 0) + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + n /= 10; + } + result.size = i + j - 1; + return result; +} + +BigInt BigInt::operator*(BigInt n) +{ + BigInt result(0, size + n.size); + + result.sign = sign * n.sign; + if (!result.sign) return result; + + int i, j; + for (i = 0; i < n.size; i++) + { + if (n.digits[i]) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = + result.digits[i + j] + + (j < size ? n.digits[i] * digits[j] : 0) + + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + } + result.size = i + j - 1; + + return result; +} + +void BigInt::operator*=(int n) +{ + operator=(operator*(n)); +} + +void BigInt::operator*=(BigInt n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator/(int n) +{ + if (!n) n /= n; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +BigInt BigInt::operator/(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +void BigInt::operator/=(int n) +{ + divide(n); +} + +void BigInt::operator/=(BigInt n) +{ + divide(n); +} + +int BigInt::operator%(int n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(int n) +{ + operator=(divide(n)); +} + +BigInt BigInt::operator%(BigInt n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(BigInt n) +{ + operator=(divide(n)); +} + +int BigInt::divide(int n) +{ + if (!n) n /= n; //XXX: force a crash + + int nsign = sig(n); + n *= nsign; + if (!sign) return 0; + sign *= nsign; + + int tmp = 0; + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = tmp / n; + tmp -= digits[i] * n; + } + normalize(); + return tmp; +} + +BigInt BigInt::divide(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + if (!sign) return 0; + sign *= n.sign; + + int oldSign = n.sign; + n.sign = 1; + + BigInt tmp(0, size); + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = 0; + while (tmp >= n) { tmp -= n; digits[i]++; } + } + normalize(); + + n.sign = oldSign; + + return tmp; +} + +// This is only exact to the first 15 or so digits, but it is +// never an over-estimate +BigInt BigInt::operator*(long double n) +{ + // the number of digits after the decimal point to use + #define DIGS_AFTER_DOT 15 + + int nsign = sig(n); + n *= nsign; + int ndigs = n >= 1 ? (int)log10(n) + 1 : 0; + BigInt result(0, size + ndigs); + result.sign = sign * nsign; + if (!result.sign) return result; + + if (n >= 1) for (int i = 0; i < ndigs; i++) n /= 10; + result.size = 0; + + char afterDot[DIGS_AFTER_DOT + 1]; + memset(afterDot, 0, sizeof(afterDot)); + + // Keep going until the DIGS_AFTER_DOT'th digit after the decimal point + for (int i = ndigs - 1; i >= -DIGS_AFTER_DOT; i--) + { + n *= 10; + int dig = (int)floor(n); + n -= dig; + if (!dig) continue; + + int carry = 0; + for (int j = 0; j < size || carry; j++) + { + int newdig = + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) + + dig * digits[j] + + carry; + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) = newdig % 10; + if (i + j >= 0 && result.digits[i + j]) result.size = max(result.size, i + j + 1); + carry = newdig / 10; + } + } + if (!result.size) result.sign = 0; + return result; +} + +void BigInt::operator*=(long double n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator<<(int n) +{ + BigInt result(*this); + result <<= n; + return result; +} + +void BigInt::operator<<=(int n) +{ + if (n < 0) operator>>=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator*=(mult); + } +} + +BigInt BigInt::operator >> (int n) +{ + BigInt result(*this); + result >>= n; + return result; +} + +void BigInt::operator>>=(int n) +{ + if (n < 0) operator<<=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator/=(mult); + } +} + +BigInt BigInt::operator,(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + for (result.size = 0; n; result.size++) + { + result.digits[result.size] = n % 10; + n /= 10; + } + memcpy(result.digits + result.size, digits, size * sizeof(digits[0])); + result.size += size; + result.sign = 1; + result.normalize(); + return result; +} + +BigInt BigInt::operator,(BigInt n) +{ + BigInt result(0, size + n.size); + memcpy(result.digits, n.digits, n.size * sizeof(n.digits[0])); + memcpy(result.digits + n.size, digits, size * sizeof(digits[0])); + result.size = size + n.size; + result.sign = 1; + result.normalize(); + return result; +} + +bool BigInt::operator!() +{ + return !size; +} + +BigInt::operator bool() +{ + return size; +} + +BigInt::operator string() +{ + return toString(); +} + +bool BigInt::operator<(BigInt n) +{ + return(compare(n) < 0); +} + +bool BigInt::operator>(BigInt n) +{ + return(compare(n) > 0); +} + +bool BigInt::operator==(BigInt n) +{ + return(compare(n) == 0); +} + +bool BigInt::operator<=(BigInt n) +{ + return(compare(n) <= 0); +} + +bool BigInt::operator>=(BigInt n) +{ + return(compare(n) >= 0); +} + +bool BigInt::operator<(int n) +{ + return(compare(BigInt(n)) < 0); +} + +bool BigInt::operator>(int n) +{ + return(compare(BigInt(n)) > 0); +} + +bool BigInt::operator==(int n) +{ + return(compare(BigInt(n)) == 0); +} + +bool BigInt::operator<=(int n) +{ + return(compare(BigInt(n)) <= 0); +} + +bool BigInt::operator>=(int n) +{ + return(compare(BigInt(n)) >= 0); +} + +int BigInt::compare(BigInt n) +{ + if (sign < n.sign) return -1; + if (sign > n.sign) return 1; + if (size < n.size) return -sign; + if (size > n.size) return sign; + for (int i = size - 1; i >= 0; i--) + { + if (digits[i] < n.digits[i]) return -sign; + else if (digits[i] > n.digits[i]) return sign; + } + return 0; +} + + +int memo[1001]; +int factsum(int n) { + if (n<=1) return 1; + if (memo[n]) return memo[n]; + + BigInt x = 1; + for (int i=2; i<=n; i++) + x *= i; + + string s = x.toString(); + int sum = 0; + for(int i=0; s[i]; i++) + sum += s[i]-'0'; + + return memo[n] = sum; +} + + +int main() { + int n; + while (cin>>n) + cout< +#include +#include +#define pi 3.1415926535897932 + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + double satellite, angle, arc, chord, radius; + char unit; + while (cin >> satellite >> angle >> unit) { + cin.ignore(1024, '\n'); + if (angle > 180) + angle = 360 - angle; + if (unit == 'm') + angle /= 60; + + radius = 6440 + satellite; + arc = 2 * pi * radius * angle / 360; + chord = sqrt(2 * radius * radius * (1 - cos(pi * angle / 180))); + cout << setprecision(6) << fixed << arc << " " << chord << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10221/10221-19.cpp b/uva_cpp_clean/10221/10221-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..455253b1fe2365a4f099ee59a2ce64ce118ee702 --- /dev/null +++ b/uva_cpp_clean/10221/10221-19.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +long double pi = 2*acos(0); + +int main() { + long double d, theta, r = 6440; + string s; + while (cin >> d >> theta >> s) { + d += r; + theta = fabs(theta); + if (s == "min") theta /= 60; + theta = 2*pi*theta / 360; + theta = theta - floor(theta/(2*pi))*2*pi; + if (theta > pi) theta = 2*pi - theta; + printf("%.6Lf ", theta*d); + printf("%.6Lf\n", sqrt(2*d*d - 2*d*d*cos(theta))); + } + return 0; +} diff --git a/uva_cpp_clean/10221/10221-20.cpp b/uva_cpp_clean/10221/10221-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79cf714f97522174f63a9aeb5b4804fb2e3242d9 --- /dev/null +++ b/uva_cpp_clean/10221/10221-20.cpp @@ -0,0 +1,24 @@ +#include +#include + +const double PI = acos(-1), radius = 6440.0; + +int main() +{ + double s, a; + char type[5]; + + while( scanf("%lf %lf %s", &s, &a, type)==3 ) + { + if( type[0]=='m' ) a /= 60; + + if( a>180 ) a = 360-a; + + double angle = (a*PI)/180; + double arc = 2.0*(s+radius)*sin( angle/2.0 ); + double chord = angle*(s+radius); + + printf("%.6lf %.6lf\n", chord, arc); + } + return 0; +} diff --git a/uva_cpp_clean/10221/10221-21.cpp b/uva_cpp_clean/10221/10221-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2a10c13a9a6a6f30cf6cebb9599d8f14a0ad5ec --- /dev/null +++ b/uva_cpp_clean/10221/10221-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10221 + Name: Satellites + Problem: https://onlinejudge.org/external/102/10221.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +const double PI = acos(-1); +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(6); + + long double r, a; + string u; + while (cin >> r >> a >> u) { + if (u[0] == 'm') a /= 60; + a = fmod(a, 360); + if (a > 180) a = 360 - a; + a *= PI/180; + r += 6440; + cout << r*a << ' ' << 2*sqrt(r*r-pow(r*cos(a/2), 2)) << '\n'; + } +} diff --git a/uva_cpp_clean/10221/10221-4.cpp b/uva_cpp_clean/10221/10221-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21991adde87e81b52a613eff9544e7ea12b8f74e --- /dev/null +++ b/uva_cpp_clean/10221/10221-4.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +bool input(int &s, double &a) +{ + std::string mode; + + if (!(std::cin >> s >> a >> mode)) + { + return false; + } + + if (mode == "min") + { + a /= 60; + } + + return true; +} + +void calculate(int s, double a, double &arc, double &chord) +{ + s += 6440; + + if (a > 180) + { + a = 360 - a; + } + + arc = (a / 360.0) * 2 * M_PI * s; + chord = std::sqrt(2 * (s * s) - 2 * s * s * std::cos((a * M_PI) / 180.0)); +} + +int main() +{ + int s; + double a; + + while (input(s, a)) + { + double arc, chord; + calculate(s, a, arc, chord); + std::cout << std::setprecision(6) << std::fixed << arc << " " << chord << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10221/10221-9.cpp b/uva_cpp_clean/10221/10221-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edcfa0d07f2eb56bc9210b5c284b110e143c6a52 --- /dev/null +++ b/uva_cpp_clean/10221/10221-9.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +const double PI = acos(-1.0), r = 6440.0; + +double s, a, R; +char ch; + +int main(){ + while(~scanf("%lf %lf %c%*s", &s, &a, &ch)){ + R = r + s; + if( ch == 'd' ) a *= PI / 180.0; + else a *= PI / 10800.0; + a = min(a, 2.0 * PI - a); + printf("%.6lf %.6lf\n", a * R, 2.0 * R * sin(a / 2.0)); + } + return(0); +} diff --git a/uva_cpp_clean/10222/10222-14.cpp b/uva_cpp_clean/10222/10222-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..185a47394988b6e6e7ec480c8aac0c131bc6c987 --- /dev/null +++ b/uva_cpp_clean/10222/10222-14.cpp @@ -0,0 +1,87 @@ +/*************************************************** + * Problem Name : 10222 Decode the Mad man.cpp + * Problem Link : https://uva.onlinejudge.org/external/102/10222.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : String(easy) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + string txt = "zxcvbnm,.asdfghjkl;'qwertyuiop[]"; + string str; + getline (cin, str); + + for (int i = 0; i < (int) str.size(); i++) { + char ch = str[i]; + + if (isalpha (ch) ) ch = tolower (ch); + + if (ch == ' ') { + cout << " "; + + } else { + int x = txt.find (ch); + cout << txt[x - 2]; + } + } + + nl; + return 0; +} diff --git a/uva_cpp_clean/10222/10222-15.cpp b/uva_cpp_clean/10222/10222-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63192f76c583e686db686d8d75ab5a6c372c7f32 --- /dev/null +++ b/uva_cpp_clean/10222/10222-15.cpp @@ -0,0 +1,22 @@ +/* + 模擬、建表 +*/ +#include + +using namespace std; + +const char keyboard[35] = "qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"; + +int main() { + char decoded[128], c; + for (int i = 0; i < 128; ++i) + decoded[i] = i; + + for (int i = 2; keyboard[i]; ++i) + decoded[keyboard[i]] = keyboard[i - 2]; + while ((c = cin.get()) and !cin.eof()) { + if (c >= 'A' and c <= 'Z') + c |= 32; + cout << decoded[c]; + } +} diff --git a/uva_cpp_clean/10222/10222-20.cpp b/uva_cpp_clean/10222/10222-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f259a2c1fb16a109f8116018ee87fc5273e74256 --- /dev/null +++ b/uva_cpp_clean/10222/10222-20.cpp @@ -0,0 +1,56 @@ +#include +#include + +char line[1005]; + +int main() +{ + gets(line); + + for(int i=0 ; i' ) printf("m"); + else if( line[i]=='/' || line[i]=='?' ) printf(","); + else printf("%c", line[i]); + } + printf("\n"); + + return 0; +} diff --git a/uva_cpp_clean/10222/10222-21.cpp b/uva_cpp_clean/10222/10222-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b921737ef0e3d414a52efed6fb06c247a244c34f --- /dev/null +++ b/uva_cpp_clean/10222/10222-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10222 + Name: Decode the Mad man + Problem: https://onlinejudge.org/external/102/10222.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +char d[256]; +int main() { + char lines[]="`1234567890-=\tqwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+\tQWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"; + for(int i=2; lines[i]; i++) + d[lines[i]] = lines[i-2]; + + char ch; + while((ch=getchar()) != EOF) { + if (ch>='A' && ch<='Z') + ch += 'a'-'A'; + + if (d[ch]) + printf("%c", d[ch]); + else + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + char a[500000],ch; + while(gets(a)) + { + for(int i=0;a[i];i++) + { + switch(a[i]) + { + case 'M':case 'm':ch='b';break; + case 'N':case 'n':ch='v';break; + case 'B':case 'b':ch='c';break; + case 'V':case 'v':ch='x';break; + case 'C':case 'c':ch='z';break; + case 'X':case 'x':ch='\'';break; + case 'Z':case 'z':ch=';';break; + case '\'':ch='l';break; + case ';':ch='k';break; + case 'L':case 'l':ch='j';break; + case 'K':case 'k':ch='h';break; + case 'J':case 'j':ch='g';break; + case 'H':case 'h':ch='f';break; + case 'G':case 'g':ch='d';break; + case 'F':case 'f':ch='s';break; + case 'D':case 'd':ch='a';break; + case 'S':case 's':ch=']';break; + case 'A':case 'a':ch='[';break; + case ']':ch='p';break; + case '[':ch='o';break; + case 'P':case 'p':ch='i';break; + case 'O':case 'o':ch='u';break; + case 'I':case 'i':ch='y';break; + case 'U':case 'u':ch='t';break; + case 'Y':case 'y':ch='r';break; + case 'T':case 't':ch='e';break; + case 'R':case 'r':ch='w';break; + case 'E':case 'e':ch='q';break; + case '/':ch=',';break; + case '.':ch='m';break; + case ',':ch='n';break; + default:ch=a[i];break; + } + printf("%c",ch); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10222/10222-9.cpp b/uva_cpp_clean/10222/10222-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..084da9f0d1540a66092fe74ee9475e219ba8bb5e --- /dev/null +++ b/uva_cpp_clean/10222/10222-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +string ans; +vector words; +map mp; + +void precal() { + words.push_back("1234567890-+"); + words.push_back("qwertyuiop[]"); + words.push_back("asdfghjkl:'"); + words.push_back("zxcvbnm,."); + for(int i = 0; i < words.size(); i++) + for(int j = 2; j < words[i].size(); j++) + mp[ words[i][j] ] = words[i][j - 2]; + mp[' '] = ' ', mp[','] = 'n', mp['.'] = 'm'; + mp[':'] = mp[';'] = 'k'; +} + +int main() { + precal(); + getline(cin, ans); + transform(ans.begin(), ans.end(), ans.begin(), ::tolower); + for(int i = 0; i < ans.size(); i++) ans[i] = mp[ ans[i] ]; + cout << ans << endl; + return(0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10223/10223-19.cpp b/uva_cpp_clean/10223/10223-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5014ecdc104c2aac5a8a3ce488c36ef4acf0212 --- /dev/null +++ b/uva_cpp_clean/10223/10223-19.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main() { + unordered_map Cat; + long long lastC = 1; + for (int i = 1; true; i++) { + unsigned int next = (lastC*(4*i-2))/(i+1); + if (next >= lastC) Cat[next] = i, lastC = next; + else break; + } + unsigned int N; + while (cin >> N) { + printf("%d\n", Cat[N]); + } + return 0; +} diff --git a/uva_cpp_clean/10223/10223-20.cpp b/uva_cpp_clean/10223/10223-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0684ef24be898fbb108a86ccfc7fb1ce12bfa630 --- /dev/null +++ b/uva_cpp_clean/10223/10223-20.cpp @@ -0,0 +1,19 @@ +#include + +const long long numNode[] = {1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, +208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190}; +long long n; + +int main() +{ + while( scanf("%lld", &n)!=EOF ) + { + for(int i=0 ; i<19 ; i++) + if( numNode[i]==n ) + { + printf("%d\n", i+1); + break; + } + } + return 0; +} diff --git a/uva_cpp_clean/10223/10223-9.cpp b/uva_cpp_clean/10223/10223-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4bcc3321bd7fbcd2d557b82bda43443f4e7962f --- /dev/null +++ b/uva_cpp_clean/10223/10223-9.cpp @@ -0,0 +1,28 @@ +#include + +#define LIM 20 + +using namespace std; + +int main(){ + long long N,catalan_number[LIM]; + double ans; + for(int n=1;n +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl cout <<"\n" +//~ #define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ __FastIO; + ll tc; + cin >> tc; + getchar(); + getchar(); + for (ll t = 1; t <= tc; t++) { + ll n = 0; + string str; + mapmp; + while (getline(cin, str)) { + if ((int)str.size() == 0) break; + mp[str]++; + n++; + } + map::iterator it; + for (it = mp.begin(); it != mp.end(); it++) { + cout << it->first << " " << fixed << setprecision(4) << (it->second * 100.0) / n << "\n"; + } + if (t != tc) cout << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/10226/10226-15.cpp b/uva_cpp_clean/10226/10226-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a98f443bf9bbd95023b81ec14a3440171e1d083a --- /dev/null +++ b/uva_cpp_clean/10226/10226-15.cpp @@ -0,0 +1,32 @@ +/* + 字串排序、數量平均 +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_testcase; + string tree; + cin >> n_testcase; + cin.ignore(1024, '\n'); + cin.ignore(1024, '\n'); + for (int i = 0; i < n_testcase; i++) { + if (i) + cout << "\n"; + map forest; + int total = 0; + while (getline(cin, tree) and tree.length()) { + forest[tree] += 100; + ++total; + } + for (const auto &wood : forest) + cout << wood.first << " " << setprecision(4) << fixed << wood.second / total << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10226/10226-19.cpp b/uva_cpp_clean/10226/10226-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b93425b1a368fff70b058661318abbf6e7977475 --- /dev/null +++ b/uva_cpp_clean/10226/10226-19.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + map m; + string tree; + getline(cin, tree); + getline(cin, tree); + while (n--) { + m.clear(); + int count = 0; + while (getline(cin, tree) && tree != "") { + if (!m.count(tree)) m.insert(pair(tree, 0)); + m[tree]++; + count++; + } + for (auto it = m.begin(); it != m.end(); ++it) { + printf("%s %.4f\n", it->first.c_str(), (it->second*100)/count); + } + if (n != 0) printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10226/10226-20.cpp b/uva_cpp_clean/10226/10226-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d02544ddd1be1a6bf3b41d3a07d7085e57b7c2f1 --- /dev/null +++ b/uva_cpp_clean/10226/10226-20.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +string name; +map msi; +map :: iterator mit; + +int main( int argc, char ** argv ) { + // freopen( "in.txt", "r", stdin ); + + int tc; + scanf( "%d ", &tc ); + for( int nCase = 1 ; nCase <= tc ; ++nCase ) { + msi.clear(); + int total = 0; + while( getline( cin, name ) && name.size() ) { + ++msi[name]; + ++total; + } + + if( nCase > 1 ) puts( "" ); + for( mit = msi.begin() ; mit != msi.end() ; ++mit ) { + double result = ((double) (mit->second * 100) ) / ((double) total); + cout << mit->first << " " << fixed << setprecision( 4 ) << result << endl; + } + } + return 0; +} diff --git a/uva_cpp_clean/10226/10226-21.cpp b/uva_cpp_clean/10226/10226-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..882ae3d27590c76c9c39a3ea6dc744ccce638ff9 --- /dev/null +++ b/uva_cpp_clean/10226/10226-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10226 + Name: Hardwood Species + Problem: https://onlinejudge.org/external/102/10226.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include + +using namespace std; + +void toup(char* s){ + for(;*s;s++) + *s=toupper(*s); +} + +int main(){ + int n,count; + string name; + char t[40]; + scanf("%d\n\n",&n); + map list; + map::iterator ii; + while(n--){ + count=0; + while(gets(t) && t[0]){ + name=t; + list[name]++; + count++; + } + for(ii=list.begin();ii!=list.end();ii++) + printf("%s %.4f\n",&ii->first[0],((float)ii->second/count)*100); + if(n)printf("\n"); + list.clear(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10226/10226-3.cpp b/uva_cpp_clean/10226/10226-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08596811d2b1afae283c0ef8da06a63ffdae15ce --- /dev/null +++ b/uva_cpp_clean/10226/10226-3.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +using namespace std; + +map wordMap; + +int main () +{ + int cases; + int total; + string line; + + scanf("%d\n\n", &cases); + + while ( cases-- ) + { + + total = 0; + + scanf("%d\n\n",&cases); + while ( getline(cin,line) && !line.empty() ) + { + if ( wordMap.find(line) == wordMap.end() ) + wordMap[line] = 1; + else + wordMap[line] += 1; + total++; + } + + for ( const auto &pair : wordMap ) + printf("%s %.4lf\n", pair.first.c_str(), pair.second * 100.0 / total ); + + if ( cases > 0 ) + { + getline(cin, line); + wordMap.clear(); + puts(""); + } + } +} + diff --git a/uva_cpp_clean/10226/10226-6.cc b/uva_cpp_clean/10226/10226-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..600d83a64055900eb68b523be45480aa08c1a75d --- /dev/null +++ b/uva_cpp_clean/10226/10226-6.cc @@ -0,0 +1,28 @@ +// http://uva.onlinejudge.org/external/102/10226.pdf +#include +#include +#include + +using namespace std; + +int main() { + int t; + cin >> t; + string s; + getline(cin, s); + getline(cin, s); + for (int i = 0; i < t; i++) { + map m; + int c = 0; + while (true) { + getline(cin, s); + if (s.empty()) break; + if (m.count(s)) m[s]++; + else m[s] = 1; + c++; + } + for (auto it = m.begin(); it != m.end(); it++) + cout << it->first << " " << setprecision(4) << fixed << double(100 * it->second) / double(c) << endl; + if (i < t - 1) cout << endl; + } +} diff --git a/uva_cpp_clean/10227/10227-21.cpp b/uva_cpp_clean/10227/10227-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06d35430830e11789c33f679b94a8b0ebe11f893 --- /dev/null +++ b/uva_cpp_clean/10227/10227-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10227 + Name: Forests + Problem: https://onlinejudge.org/external/102/10227.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +bitset<101> bits[101]; +int main(){ + char line[100]; + int T, n, m, i, j; + scanf("%d", &T); + while (T--) { + scanf("%d%d", &n, &m); + for (int i=0; i + +using namespace std; + +struct UnionFind{ + int numSets; + vector path, rank, setSize; + + UnionFind(int N_): numSets(N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + setSize.assign(N_, 1); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + numSets--; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x, setSize[x] += setSize[y]; + else path[x] = y, setSize[y] += setSize[x]; + if( rank[x] == rank[y] ) rank[y]++; + } + + int sizeOfSet(int idx){ + return setSize[ findSet(idx) ]; + } +}; + +const int SIZE = 110, LIM = 10; +int tc, P, T, a, b; +char line[LIM]; +vector G[SIZE]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d\n", &P, &T); + while(true){ + cin.getline(line, LIM); + if( sscanf(line, "%d %d", &a, &b) == -1) break; + G[a].push_back(b); + } + UnionFind UF(P); + for(int it = 1; it <= P; it++){ + sort(G[it].begin(), G[it].end()); + G[it].erase( unique(G[it].begin(), G[it].end()), G[it].end()); + } + for(int i = 1; i <= P; i++){ + for(int j = i + 1; j <= P; j++) + if( G[i] == G[j] ) + UF.unionSet(i, j); + G[i].clear(); + } + printf("%d\n", UF.numSets); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10229/10229-15.cpp b/uva_cpp_clean/10229/10229-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a75ddf4004dd18ec2d4beb610ce44dc1b6285dcc --- /dev/null +++ b/uva_cpp_clean/10229/10229-15.cpp @@ -0,0 +1,58 @@ +/* + 矩陣快速冪 +*/ +#include +#include + +using namespace std; + +int mod; + +class matrix { + public: + vector> mat; + + matrix(vector>& d) { + mat = d; + } + matrix(int a, int b, int c, int d) { + mat = vector>(2, vector(2, 0)); + mat[0][0] = a; + mat[0][1] = b; + mat[1][0] = c; + mat[1][1] = d; + } + matrix operator*(const matrix& other) { + vector> temp(2, vector(2, 0)); + temp[0][0] = (mat[0][0] * other.mat[0][0] % mod + mat[0][1] * other.mat[1][0] % mod) % mod; + temp[0][1] = (mat[0][0] * other.mat[0][1] % mod + mat[0][1] * other.mat[1][1] % mod) % mod; + temp[1][0] = (mat[1][0] * other.mat[0][0] % mod + mat[1][1] * other.mat[1][0] % mod) % mod; + temp[1][1] = (mat[1][0] * other.mat[0][1] % mod + mat[1][1] * other.mat[1][1] % mod) % mod; + return matrix(temp); + } + matrix operator^(int exp) { + matrix ret(1, 0, 0, 1); + while (exp) { + if (exp & 1) + ret = ret * *this; + *this = *this * *this; + exp >>= 1; + } + return ret; + } +}; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, m; + while (cin >> n >> m) { + if (n == 0) + cout << "0\n"; + else { + mod = 1 << m; + cout << ((matrix(1, 1, 1, 0) ^ (n - 1)) * matrix(1, 1, 1, 1)).mat[1][1] << "\n"; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10229/10229-19.cpp b/uva_cpp_clean/10229/10229-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e47a104cd79b0059eaf1fb3962c16b57ea8c128 --- /dev/null +++ b/uva_cpp_clean/10229/10229-19.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +using namespace std; + +typedef pair ll; +typedef pair mat; + +unordered_map dp; + +int mod = 0x00FFFFFF; + +mat pow(int n) { + if (n == 0) return mat(ll(1, 0), ll(0, 1)); + if (n == 1) return mat(ll(1, 1), ll(1, 0)); + if (!dp.count(n)) { + mat mn1 = pow(n/2); + mat mn2(ll((mn1.first.first*mn1.first.first + mn1.first.second*mn1.second.first)&mod, (mn1.first.first*mn1.first.second + mn1.first.second*mn1.second.second)&mod), ll((mn1.second.first*mn1.first.first + mn1.second.second*mn1.second.first)&mod, (mn1.second.first*mn1.first.second + mn1.second.second*mn1.second.second)&mod)); + if (n%2) { + mn1 = mn2; + mn2 = mat(ll(mn1.first.first+mn1.second.first, mn1.first.second+mn1.second.second), ll(mn1.first.first, mn1.first.second)); + } + dp[n] = mn2; + } + return dp[n]; +} + +int main() { + int n, m; + while (cin >> n >> m) { + long long val = pow(n).first.second & ((1<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10229 + Name: Modular Fibonacci + Problem: https://onlinejudge.org/external/102/10229.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define Long long long + +unsigned int mask; +struct Mat { + int a, b, c, d; + Mat (Long a, Long b, Long c, Long d): a(a&mask),b(b&mask),c(c&mask),d(d&mask) {} + Mat operator *(const Mat& o) const { + return Mat(a*o.a+b*o.c, a*o.b+b*o.d, c*o.a+d*o.c, c*o.b+d*o.d); + } +}; + +Mat powmod(Mat mat, int n) { + if (n==0) return Mat(1,0,1,0); + if (n==1) return mat; + + Mat r = powmod(mat, n>>1); + r = r*r; + + return n&1 ? r*mat : r; +} + +int main() { + int n, m; + while (cin>>n>>m) { + mask = (1< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +////=====================================//// + +ll mod=1; +int row; + +struct Matrix +{ + ll mat[10][10]; +}; + +Matrix base,knw; + +Matrix Mul(Matrix &aa, Matrix &bb) +{ + Matrix ret; + cover(ret.mat,0); + for(int i = 0; i < row; i ++) + { + for(int j = 0; j < row; j ++) + { + for(int k = 0; k < row; k ++) + { + ret.mat[i][j] += (aa.mat[i][k]*bb.mat[k][j]); + ret.mat[i][j] %= mod; + } + } + } + return ret; +} + +Matrix exp(Matrix &a, ll b) +{ + if(b == 1) return a; + if(b&1) + { + Matrix ret = exp(a,b-1); + return Mul(a, ret); + } + else + { + Matrix tmp = exp(a,b/2); + return Mul(tmp,tmp); + } +} + +int main() +{ + row=2; + + int n,m; + while(sii(n,m)==2) + { + if(n==0) + { + P(0); + continue; + } + if(n==1) + { + if(m==0) + P(0); + else + P(1); + continue; + } + mod = (1 << m); + + base.mat[0][0]=0; base.mat[0][1]=1; + base.mat[1][0]=1; base.mat[1][1]=1; + + knw.mat[0][0]=0; + knw.mat[0][1]=1; + + Matrix res =exp(base, n-1); + res=Mul(knw,res); + + + PL(res.mat[0][1]); + + } + + return 0; +} diff --git a/uva_cpp_clean/10229/10229-6.cpp b/uva_cpp_clean/10229/10229-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41cd521632eaecd02e10ac1add63b3d5b10a3671 --- /dev/null +++ b/uva_cpp_clean/10229/10229-6.cpp @@ -0,0 +1,88 @@ +/* + math > matrix exponentiation + difficulty: easy + date: 11/Sep/2020 + hint: transform the fibonacci recurrence into a matrix exponentiation + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; +typedef vector> matrix; +ll MOD = 1e9+7; + +/* O(N^2) - returns I which is NxN */ +matrix identity(int N) { + matrix I(N, vector(N, 0)); + for (int n = 0; n < N; n++) I[n][n] = 1; + return I; +} + +/* O(N^3) - returns A*B which is LxC, where A is LxN and B is NxC */ +matrix mult(const matrix& A, const matrix& B) { + int L = A.size(), C = B[0].size(), N = B.size(); + matrix R(L, vector(C, 0)); + + for (int l = 0; l < L; l++) + for (int c = 0; c < C; c++) + for (int k = 0; k < N; k++) + R[l][c] = (R[l][c] + A[l][k] * B[k][c]) % MOD; + + return R; +} + +/* O(N^3 * log(k)) - returns A^k, where A is NxN */ +matrix power(matrix A, ll k) { + matrix R = identity(A.size()); + + while (k > 0) { + if (k & 1ll) R = mult(R, A); // if k is odd + + k >>= 1ll; + A = mult(A, A); + } + + return R; +} + +int main() { + ll n, m; + while (cin >> n >> m) { + MOD = 1ll << m; + + if (n == 0) { cout << 0%MOD << endl; continue; } + if (n == 1) { cout << 1%MOD << endl; continue; } + + matrix B(2, vector(1)); B[0][0] = 1; B[1][0] = 0; + matrix T(2, vector(2, 1)); T[1][1] = 0; + matrix R = power(T, n-1); + cout << R[0][0] << endl; + } + return 0; +} + +/* +f(n) = f(n-1) + f(n-2), para n >= 2 +f(0) = 0, f(1) = 1 + + | f(n) | = T * | f(n-1) | + | f(n-1) | | f(n-2) | + + | f(n) | = | 1 1 | * | f(n-1) | + | f(n-1) | | 1 0 | | f(n-2) | + +Para n = 2: + | f(2) | = | 1 1 | * | f(1) | + | f(1) | | 1 0 | | f(0) | + +Para n = 3: + | f(3) | = | 1 1 | * | f(2) | + | f(2) | | 1 0 | | f(1) | + + | f(3) | = | 1 1 | * | 1 1 | * | f(1) | + | f(2) | | 1 0 | | 1 0 | | f(0) | + +Para qualquer n >= 2: + | f(n) | = | 1 1 | ^ (n-1) * | f(1) | + | f(n-1) | | 1 0 | | f(0) | +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10229/10229-8.cpp b/uva_cpp_clean/10229/10229-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..883bc909125ab537d62e4f6ac448a23aa1bb6914 --- /dev/null +++ b/uva_cpp_clean/10229/10229-8.cpp @@ -0,0 +1,52 @@ +// Problem name: Modular Fibonacci +// Problem link: https://vjudge.net/problem/UVA-10229 +// Submission link: https://vjudge.net/solution/18073449 + +#include + +#define endl '\n' + +using namespace std; + +typedef long long ll; +typedef vector vll; +typedef vector matrix; + +matrix multiply(matrix &a, matrix &b, ll MOD){ + int n = a.size(), m = a[ 0 ].size(), o = b[ 0 ].size(); + matrix mat(n, vll(o, 0LL)); + for(int i = 0 ; i < n ; ++i) + for(int k = 0 ; k < m ; ++k) + for(int j = 0 ; j < o ; ++j) + mat[ i ][ j ] = (mat[ i ][ j ] + a[ i ][ k ] * b[ k ][ j ]) % MOD; + return mat; +} + +matrix pow(matrix m, ll p, ll MOD){ + int len = m.size(); + matrix ans(len, vll(len, 0)); + for(int i = 0 ; i < len ; ans[ i ][ i ] = 1LL, ++i); + for( ; p ; p >>= 1LL){ + if(p & 1LL) + ans = multiply(ans, m, MOD); + m = multiply(m, m, MOD); + } + return ans; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0); + +#ifdef JoseA132 + freopen("01.in", "r", stdin); +#endif + + ll n, m; + while(cin >> n >> m){ + matrix M(2, vll(2, 1LL)); + M[ 1 ][ 1 ] = 0LL; + M = pow(M, n, 1LL << m); + cout << M[ 0 ][ 1 ] << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10235/10235-15.cpp b/uva_cpp_clean/10235/10235-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1600f92772fe0a21061b10bc087f166bd7bf49cb --- /dev/null +++ b/uva_cpp_clean/10235/10235-15.cpp @@ -0,0 +1,35 @@ +/* + 質數 +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + vector is_prime(1000000, true); + is_prime[0] = is_prime[1] = false; + for (int i = 2; i < 1000; i++) + if (is_prime[i]) + for (int j = i * i; j < 1000000; j += i) + is_prime[j] = false; + int n, k; + while (cin >> n) { + cout << n; + if (is_prime[n]) { + string word = to_string(n); + reverse(word.begin(), word.end()); + k = stoi(word); + if (k != n and is_prime[k]) + cout << " is emirp.\n"; + else + cout << " is prime.\n"; + } else + cout << " is not prime.\n"; + } +} diff --git a/uva_cpp_clean/10235/10235-19.cpp b/uva_cpp_clean/10235/10235-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b58e305193d365e45a55e0c8d9ceb1a3881770db --- /dev/null +++ b/uva_cpp_clean/10235/10235-19.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef unordered_set usi; + +ll sieve_size; +bitset<10000000> bs; +usi primes; + +void sieve(ll upperbound) { + sieve_size=upperbound+1; bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.insert((int)i); + } +} + +int reverse(int i) { + int r = 0; + while (i) { + r = 10*r + i%10; + i /= 10; + } + return r; +} + +int main() { + sieve(1000010); + int n; + while (cin >> n) { + if (primes.count(n)) { + int r = reverse(n); + if (primes.count(r) && n != r) printf("%d is emirp.\n", n); + else printf("%d is prime.\n", n); + } else printf("%d is not prime.\n", n); + } + return 0; +} diff --git a/uva_cpp_clean/10235/10235-21.cpp b/uva_cpp_clean/10235/10235-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..883110a2ee2f339e05502d55b04219c2a1e5b8c9 --- /dev/null +++ b/uva_cpp_clean/10235/10235-21.cpp @@ -0,0 +1,61 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10235 + Name: Simply Emirp + Problem: https://onlinejudge.org/external/102/10235.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define N 1234567 +unsigned int prime[N / 64]; +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) +void sieve() { + memset(prime, -1, sizeof(prime)); + unsigned int i, i2, sqrtN = (unsigned int)sqrt((double)N) + 1; + for(i = 3; i>6] &= ~(1<<((j>>1)&31)); + } +} + +bool isPrime(int n) { + return n==2 || ((n&1) && gP(n)); +} + +int rev(int n) { + int r = 0; + while (n) { + r = r*10 + n%10; + n /= 10; + } + return r; +} + + +int main() { + sieve(); + int n; + while (cin>>n) + if (!isPrime(n)) + printf("%d is not prime.\n", n); + + else { + int r = rev(n); + if(r==n || !isPrime(r)) + printf("%d is prime.\n", n); + + else printf("%d is emirp.\n", n); + } +} diff --git a/uva_cpp_clean/10235/10235-4.cpp b/uva_cpp_clean/10235/10235-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa2055e927bb821ea842de43099efc6e6b44bb5d --- /dev/null +++ b/uva_cpp_clean/10235/10235-4.cpp @@ -0,0 +1,59 @@ +#include +#include + +#define SIZE 1000000 + +bool isEmirp(std::bitset &primes, int n) +{ + int reversed = 0, temp = n; + while (temp != 0) + { + reversed = reversed * 10 + temp % 10; + temp /= 10; + } + + return (reversed != n) && primes[reversed]; +} + +void init(std::bitset &primes) +{ + primes.set(); + primes[0] = false; + primes[1] = false; + + for (size_t i = 2; i * i < primes.size(); i++) + { + if (!primes[i]) + { + continue; + } + + for (size_t j = 2 * i; j < primes.size(); j += i) + { + primes[j] = false; + } + } +} + +int main() +{ + std::bitset primes; + init(primes); + + int n; + while (std::cin >> n) + { + if (!primes[n]) + { + std::cout << n << " is not prime." << std::endl; + } + else if (isEmirp(primes, n)) + { + std::cout << n << " is emirp." << std::endl; + } + else + { + std::cout << n << " is prime." << std::endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10235/10235-5.cpp b/uva_cpp_clean/10235/10235-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5b2a1f231eebbd434b4f30fb3c6b9ab799ca8f69 --- /dev/null +++ b/uva_cpp_clean/10235/10235-5.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +int main() +{ +long long i,c,d,num,j,p,n; +while(scanf("%lld",&num)==1) +{ +p=num; +i=2; +while(i<=num) +{ +if(num%i==0) +break; +i++; +} + +n=0; +while(num!=0) +{ +n=n*10+num%10; +num/=10; +} + + +j=2; +while(j<=n) +{ +if(n%j==0) +break; +j++; +} +if(p==2) +printf("%lld is prime.\n",p); +else if((j==n)&&(i==p)) +printf("%lld is erime.\n",p); +else if(i==p) +printf("%lld is prime.\n",p); +else +printf("%lld is not prime.\n",p); + + +} +return 0; +} + diff --git a/uva_cpp_clean/10235/10235-9.cpp b/uva_cpp_clean/10235/10235-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d09a48ca05381695e0bf838e18246f242294f4e --- /dev/null +++ b/uva_cpp_clean/10235/10235-9.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +const int MAX_N = 1e6 + 10; + +int N; +bool is_prime[MAX_N]; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long i = 2; i < MAX_N; i++) + if( is_prime[i] ) + for(long long j = i * i; j < MAX_N; j += i) + is_prime[j] = false; +} + +int reversed(int num){ + int ret = 0; + while(num) ret = 10 * ret + num % 10, num /= 10; + return ret; +} + +int main(){ + sieve(); + while( scanf("%d", &N) == 1 ){ + if( not is_prime[N] ) printf("%d is not prime.\n", N); + else if( is_prime[reversed(N)] and N != reversed(N) ) printf("%d is emirp.\n", N); + else printf("%d is prime.\n", N); + } + return(0); +} diff --git a/uva_cpp_clean/10242/10242-15.cpp b/uva_cpp_clean/10242/10242-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cba23fe74b1471c3a9540758793252f15c03c79f --- /dev/null +++ b/uva_cpp_clean/10242/10242-15.cpp @@ -0,0 +1,30 @@ +/* + 數學題 +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + double x, y; + while (cin >> x >> y) { + vector> points; + points.emplace_back(x, y); + cin >> x >> y; + points.emplace_back(x, y); + for (int i = 0; i < 2; i++) { + cin >> x >> y; + if (make_pair(x, y) == points[0]) + swap(points[0], points[1]); + else if (make_pair(x, y) != points[1]) + points.emplace_back(x, y); + } + cout << setprecision(3) << fixed << points[0].first + points[2].first - points[1].first << " " + << points[0].second + points[2].second - points[1].second << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10242/10242-18.cpp b/uva_cpp_clean/10242/10242-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..281ca9f1fefbb984f5711b17ad841b90a2e59a0c --- /dev/null +++ b/uva_cpp_clean/10242/10242-18.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; +typedef pair pdd; + +int main(){ + double ax, ay, bx, by, cx, cy, x[4], y[4]; + set st; + set::iterator it; + while(scanf("%lf %lf", &x[0], &y[0]) != EOF){ + for(int i = 1; i < 4; i++) scanf("%lf %lf", &x[i], &y[i]); + st.clear(); + for(int i = 0; i < 4; i++){ + if(st.find({x[i], y[i]}) != st.end()){ + st.erase({x[i], y[i]}); + cx = x[i]; + cy = y[i]; + } else st.insert({x[i], y[i]}); + } + it = st.begin(); + ax = it->first; + ay = it->second; + it++; + bx = it->first; + by = it->second; + printf("%.3lf %.3lf\n", ax + bx - cx, ay + by - cy); + } + return 0; +} +/* + La propiedad del paralelogramo es que sus diagonales son iguales, entonces, usamos la ley del paralelogramo para + obtener las diagonales: + x1 + x3 = x2 + x4 + y1 + y3 = y2 + y4 + Despejando los puntos del cuarto vertice, las ecuaciones quedan asi: + x4 = x1 + x3 - x2 + y4 = y1 + y3 - y2 + Donde (x2, y2) es la coordenada repetida + +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10242/10242-20.cpp b/uva_cpp_clean/10242/10242-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11f39f28bdd5c5caf4d4aadb3f3e62a47ebb8278 --- /dev/null +++ b/uva_cpp_clean/10242/10242-20.cpp @@ -0,0 +1,32 @@ +#include + +double c_x[6], c_y[6]; + +int main() +{ + while( scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &c_x[0], &c_y[0], &c_x[1], &c_y[1], &c_x[2], &c_y[2], &c_x[3], &c_y[3])!=EOF ) + { + double x, y; + + bool flag = false; + for(int i=0 ; i<4 ; i++) + { + for(int j=i+1 ; j<4 ; j++) + { + if( c_x[i]==c_x[j] && c_y[i]==c_y[j] ) + { + x = c_x[i], y = c_y[i]; + flag = true; + break; + } + } + if( flag ) break; + } + + x = (c_x[0]+c_x[1]+c_x[2]+c_x[3])-(3*x); + y = (c_y[0]+c_y[1]+c_y[2]+c_y[3])-(3*y); + + printf("%.3lf %.3lf\n", x, y); + } + return 0; +} diff --git a/uva_cpp_clean/10242/10242-21.cpp b/uva_cpp_clean/10242/10242-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..894e50fe94212b89656f9b620f34fb9f8322cce9 --- /dev/null +++ b/uva_cpp_clean/10242/10242-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10242 + Name: Fourth Point !! + Problem: https://onlinejudge.org/external/102/10242.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 1e-8 +struct Point { + double x, y; + Point():x(0),y(0){} + Point(double x, double y):x(x),y(y){} + bool operator < (const Point &o) const { return x < o.x; } + bool operator == (const Point &o) const { return fabs(x-o.x)> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} +std::ostream& operator<< (std::ostream& stream, const Point p) { + stream << p.x << ' ' << p.y; + return stream; +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(3); + + Point p1, p2, p3, p4; + while (cin >> p1 >> p2 >> p3 >> p4) { + if (p1 == p3) + swap(p1, p2); + else if (p1 == p4) + swap(p1, p2), + swap(p3, p4); + else if (p2 == p4) + swap(p3, p4); + + cout << p1 + p4 - p3 << '\n'; + } +} diff --git a/uva_cpp_clean/10245/10245-21.cpp b/uva_cpp_clean/10245/10245-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34d5db4adf4c45afac444bc91d848431457619fa --- /dev/null +++ b/uva_cpp_clean/10245/10245-21.cpp @@ -0,0 +1,72 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10245 + Name: The Closest Pair Problem + Problem: https://onlinejudge.org/external/102/10245.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + + +#define MAXN 10001 + +int n; +double x[MAXN], y[MAXN+1], sx[MAXN]; + +bool compx(int i, int j) { return x[i] < x[j]; } +bool compy(int i, int j) { return y[i] < y[j]; } + +double dist(int i, int j) { + double xx = x[i]-x[j], + yy = y[i]-y[j]; + return sqrt(xx*xx + yy*yy); +} + +double CP() { + double mn = 1e300; + set M(compy); + for (int i=0; i mn) { + auto it2 = it; it++; + M.erase(it2); + continue; + } + if (fabs(y[ind]-y[iy]) > mn) break; + mn = min(mn, dist(ind, iy)); + it++; + } + + M.insert(ind); + } + return mn; +} + +int main(){ + while (cin>>n && n) { + for (int i=0; i>x[i]>>y[i]; + sx[i] = i; + } + sort(sx, sx+n, compx); + double res = CP(); + if (res < 10000) + printf("%.4lf\n", res); + else puts("INFINITY"); + } +} diff --git a/uva_cpp_clean/10247/10247-21.cpp b/uva_cpp_clean/10247/10247-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e447672eda4fb3eb27cbeb4f582345e85f302933 --- /dev/null +++ b/uva_cpp_clean/10247/10247-21.cpp @@ -0,0 +1,353 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10247 + Name: Complete Tree Labeling + Problem: https://onlinejudge.org/external/102/10247.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + + if (cells) delete [] cells; + cells = ncells; + } + if (!cp) + memset(cells, 0, capacity*CellSize); + else memset(cells+sz, 0, (capacity-sz)*CellSize); + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + // --------------------------------------------------------------------- + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + + BigInt(const char s[]) { + while (s[0]==' ' || s[0]=='\n' || s[0]=='\t') s++; + sign = 1; + if (s[0] == '+') s++; + if (s[0] == '-') { sign=-1; s++; } + while (s[0]=='0') s++; + if (!s[0]) { init(); return; } + + int len = strlen(s); + size = (len+3)/MAX_D; + cells = new CellType[capacity = size+16]; + for (int i=len-1; i>=0; i-=MAX_D) { + int rc = 0; + for (int k=i-MAX_D+1; k<=i; k++) { + if (k<0 || s[k]=='-' || s[k]=='+') continue; + rc = rc*10 + s[k] - '0'; + } + cells[(len-i-1) / MAX_D] = rc; + } + for (int i=size; i= 0) + delete [] cells; + } + + int getSize() { return size; } + int getCell(int i) { return cells[i]; } + bool isZero() { return !sign; } + BigInt& setOne() { sign=size=cells[0]=1; return *this; } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { + return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign; + } + bool operator == (const BigInt &o) const { return sign==o.sign && numEq(o); } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + int osz = size; + setSize(max(size, o.size)+1); + for (int i=osz; i= MAX_C) { + cells[i] -= MAX_C; + ++cells[i+1]; + } + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (*this == o) { + sign = size = 0; + return *this; + } + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i 10000) { + return *this *= BigInt(x); + } + + sign *= sgn(x); + if (!sign) { + size = 0; + return *this; + } + if (x<0) x = -x; + + int cr = 0; + for (int i=0; i < size; i++) { + cells[i] = cells[i] * x + cr; + cr = cells[i] / MAX_C; + cells[i] %= MAX_C; + } + + if (cr) { + int ex = (countDigits(cr)+MAX_D-1)/MAX_D, sz=size; + setSize(size + ex); + size = sz; + for (; cr; cr /= MAX_C) + cells[size++] = cr % MAX_C; + } + + return autoSize(); + } + BigInt operator* (const BigInt &o) const { + if (sign*o.sign == 0) + return *new BigInt(0); + if (o.size == 1) + return *new BigInt(*this) *= o.cells[0]; + + BigInt r(sign*o.sign); + r.setSize(size + o.size + 1, 0); + for (int i=0; i=0; --i) { + t = t*MAX_C + cells[i]; + cells[i] = t / den; + t -= (long long) cells[i] * den; + } + return autoSize(); + } + BigInt operator/ (int den) const { return *new BigInt(*this) *= den; } + + int operator% (int den) const { + int r = 0; + for (int i = size-1; i>=0; --i) + r = (r * MAX_C + cells[i]) % den; + return r; + } + + // --------------------------------------------------------------------- + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + +BigInt pow(const BigInt b, int p) { + if (!p) return BigInt(1); + if (p==1) return BigInt(b); + + BigInt r = pow(b, p>>1); + r *= r; + if (p&1) r*=b; + return r; +} + +// ---------------------------------------------------------------------------------------------- + +BigInt F[3280]; + +BigInt memo[23][23]; +BigInt solve(int b, int d) { + BigInt &r = memo[b][d]; + if (r.isZero()) { + if (!d || b==1) return r.setOne(); + + int bd = (pow(b, d)-1)/(b-1); + r = F[b*bd]; + for (int i=2; i<=bd; ++i) + r /= pow(i, b); + + r *= pow(solve(b, d-1), b); + } + return r; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + F[0].setOne(); + for (int i=1; i<3280; ++i) + F[i] = F[i-1] * i; + + + int b, d; + while (cin>>b>>d) { + cout << solve(b, d) << endl; + } +} diff --git a/uva_cpp_clean/10249/10249-21.cpp b/uva_cpp_clean/10249/10249-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58fc16f6214036a0a3584887fd19a03c59ebf4e7 --- /dev/null +++ b/uva_cpp_clean/10249/10249-21.cpp @@ -0,0 +1,69 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10249 + Name: The Grand Dinner + Problem: https://onlinejudge.org/external/102/10249.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +int n, m, tm[100], otm[100], tb[100], Ar[100][100], readd[100]; +bool comp1(int i, int j) { return tm[i] > tm[j]; } +bool comp2(int i, int j) { return tb[i] < tb[j]; } + + +int main(){ + while (cin>>m>>n && (m||n)) { + for (int i=0; i>tm[i]; + otm[i] = i; + } + sort(otm, otm+m, comp1); + + priority_queue, bool(*)(int, int)> tbq(comp2); + for (int i=0; i> tb[i]; + tbq.push(i); + } + + int possible=1; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define rep(i, a, n) for (int i = a; i < n; i++) +#define erep(i, a, n) for (int i = a; i <= n; i++) +#define per(i, a, n) for (int i = n - 1; i >= a; i--) +#define pb push_back +#define mp make_pair +#define fi first +#define se second +#define all(x) (x).begin(), (x).end() +#define sz(x) (x).size() +#define minof(x, y) (((x) < (y)) ? (x) : (y)) +#define maxof(x, y) (((y) < (x)) ? (x) : (y)) + +//const int INF = numeric_limits::max(); +const double EPS = numeric_limits::epsilon(); +typedef long long ll; +#define MAX 10000 +// end of header + +#define INF 10000 +#define MAX_STATS 51 +#define MAX_TRAINS 50 +#define MAX_TIME 201 + +int main() { + int numOfStats; + int targetTime; + int travelTime[MAX_STATS]; + int leftTrainCount; + int leftStartTime[MAX_TRAINS]; + int rightTrainCount; + int rightStartTime[MAX_TRAINS]; + + int dp[MAX_TIME][MAX_STATS]; + bool hasTrain[MAX_TIME][MAX_STATS][2]; + + int elapsedLeftTravelTime[MAX_STATS][MAX_TRAINS]; // 从左向右开 + int elapsedRightTravelTime[MAX_STATS][MAX_TRAINS]; // 从右向左开 + + int cases = 0; + + while (scanf("%d", &numOfStats) == 1 && numOfStats != 0) { + + // read + scanf("%d", &targetTime); + erep(i, 1, numOfStats - 1) { + scanf("%d", travelTime + i); + } + scanf("%d", &leftTrainCount); + rep(i, 0, leftTrainCount) { + scanf("%d", leftStartTime + i); + } + scanf("%d", &rightTrainCount); + rep(i, 0, rightTrainCount) { + scanf("%d", rightStartTime + i); + } + + // init + erep(j, 1, numOfStats - 1) { + dp[targetTime][j] = INF; + } + dp[targetTime][numOfStats] = 0; + memset(hasTrain, false, sizeof(hasTrain)); + + // judge if [time][station] has trains with 2 auxiliary arrays + rep(i, 0, leftTrainCount) { + elapsedLeftTravelTime[1][i] = leftStartTime[i]; + erep(j, 2, numOfStats) { + elapsedLeftTravelTime[j][i] = elapsedLeftTravelTime[j - 1][i] + travelTime[j - 1]; + } + } + rep(i, 0, rightTrainCount) { + elapsedRightTravelTime[numOfStats][i] = rightStartTime[i]; + per(j, 1, numOfStats) { + elapsedRightTravelTime[j][i] = elapsedRightTravelTime[j + 1][i] + travelTime[j]; + } + } + int time; + erep(i, 1, numOfStats) { + rep(j, 0, leftTrainCount) { + time = elapsedLeftTravelTime[i][j]; + if (time <= targetTime) { + hasTrain[time][i][0] = true; + } + } + rep(j, 0, rightTrainCount) { + time = elapsedRightTravelTime[i][j]; + if (time <= targetTime) { + hasTrain[time][i][1] = true; + } + } + } + + // dp + per(i, 0, targetTime) { + erep(j, 1, numOfStats) { + // wait for 1 unit time + dp[i][j] = dp[i + 1][j] + 1; + // take a train heading for right + if (j < numOfStats && hasTrain[i][j][0] && (i + travelTime[j] <= targetTime)) { + dp[i][j] = minof(dp[i][j], dp[i + travelTime[j]][j + 1]); + } + // take a train heading for left + if (j > 1 && hasTrain[i][j][1] && (i + travelTime[j - 1] <= targetTime)) { + dp[i][j] = minof(dp[i][j], dp[i + travelTime[j - 1]][j - 1]); + } + } + } + + // output + printf("Case Number %d: ", ++cases); + if (dp[0][1] > MAX_TIME) { + printf("impossible\n"); + } else { + printf("%d\n", dp[0][1]); + } + } +} diff --git a/uva_cpp_clean/10250/10250-21.cpp b/uva_cpp_clean/10250/10250-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dff101cd6d98b433c87d7c9ae3166cb803d47688 --- /dev/null +++ b/uva_cpp_clean/10250/10250-21.cpp @@ -0,0 +1,30 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10250 + Name: The Other Two Trees + Problem: https://onlinejudge.org/external/102/10250.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(10); + + double x1, y1, x2, y2; + while (cin >> x1 >> y1 >> x2 >> y2) { + double mx = (x1 + x2) / 2, my = (y1 + y2) / 2; + cout << -(y2 - my) + mx << ' ' + << +(x2 - mx) + my << ' ' + << -(y1 - my) + mx << ' ' + << +(x1 - mx) + my << '\n'; + } +} diff --git a/uva_cpp_clean/10252/10252-19.cpp b/uva_cpp_clean/10252/10252-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f2c4069225a6df431d5445dbaf39a9bfcbb46d7 --- /dev/null +++ b/uva_cpp_clean/10252/10252-19.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main() { + string a, b; + int freqa[30], freqb[30]; + while (getline(cin, a) && getline(cin, b)) { + for (int i = 0; i < 30; i++) freqa[i] = freqb[i] = 0; + for (int i = 0; i < a.length(); i++) freqa[a[i]-'a']++; + for (int i = 0; i < b.length(); i++) freqb[b[i]-'a']++; + for (int i = 0; i < 30; i++) { + for (int j = 0; j < min(freqa[i], freqb[i]); j++) cout << (char) (i+'a'); + } + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10252/10252-20.cpp b/uva_cpp_clean/10252/10252-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40a511e7ab1baf271328b9d77232a2f3bdc8d9dc --- /dev/null +++ b/uva_cpp_clean/10252/10252-20.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +using namespace std; + +char a[1005], b[1005]; +const int S = 27; +int x[S], aCount[S], bCount[S]; + +int main() +{ + while( gets(a) && gets(b) ) + { + memset(x, 0, sizeof(x)); + memset(aCount, 0, sizeof(aCount)); + memset(bCount, 0, sizeof(bCount)); + + int aLen = strlen(a), bLen = strlen(b); + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10252 + Name: Common Permutation + Problem: https://onlinejudge.org/external/102/10252.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include + +using namespace std; + +int min(int x, int y){ + return (x +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#define ll long long +//#define sc scanf +//#define pf printf +//#define pi 2*acos(0.0) +//using namespace std; +//int main() +//{ +// string s1,s2,a; +// while(getline(cin,s1)) +// { +// getline(cin,s2); +// int l=s1.size(); +// int len=s2.size(); +// a=""; +// for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + string s1,s2; + while(getline(cin,s1)) + { + getline(cin,s2); + sort(s1.begin(),s1.end()); + sort(s2.begin(),s2.end()); + s1.erase(set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),s1.begin()),s1.end()); + cout< +/* +Problem: 10258 - Contest Scoreboard +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1199 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int T; + +struct contestant{ + // a struct for the scoreboard contestant data + int id = 0; + int total_solved = 0; + int total_penalty = 0; + + int penalty[11] = {0}; + bool solved[11] = {0}; +}; + +bool compare_contestants(contestant a, contestant b); + +int main () { + fastio; + + string line; + + cin >> T; + + getline(cin, line); + getline(cin, line); + while ( T-- ){ + + bool partisipated[102] = {0}; + contestant scoreboard[102]; + while (getline(cin, line) && line!=""){ + + char verdict; + int id, problem, time; + + istringstream iss(line); + iss >> id >> problem >> time >> verdict; + + contestant c; + + c.id = id; + + // check if the contestant has submitted before + if (!partisipated[id]){ + + scoreboard[id] = c; + partisipated[id] = true; + } + + // if the problem is already solved, don't do anything + if (scoreboard[id].solved[problem]) continue; + + // now check the verdict + + if (verdict == 'I') + scoreboard[id].penalty[problem]+= 20; + + else if (verdict == 'C'){ + // mark as solved + scoreboard[id].solved[problem] = true; + scoreboard[id].total_solved++; + + // update total penalty + scoreboard[id].penalty[problem] += time; + scoreboard[id].total_penalty += scoreboard[id].penalty[problem]; + } + } + + // now sort the scoreboard + sort(scoreboard, scoreboard+101, compare_contestants); + + // now output the scoreboard + for (int i = 0; i < 101; ++i){ + if (scoreboard[i].id){ + cout << scoreboard[i].id << " "; + cout << scoreboard[i].total_solved << " "; + cout << scoreboard[i].total_penalty << "\n"; + } + } + + if (T) cout << "\n"; + } + + return 0; +} + +bool compare_contestants(contestant a, contestant b){ + if (a.total_solved == b.total_solved){ + if (a.total_penalty == b.total_penalty){ + return a.id < b.id; + } else return a.total_penalty < b.total_penalty; + } else return a.total_solved > b.total_solved; + +} + +/* +Sample input:- +----------------- +1 + +1 2 10 I +3 1 11 C +1 2 19 R +1 2 21 C +1 1 25 C + +Sample output:- +----------------- +1 2 66 +3 1 11 + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10258/10258-14.cpp b/uva_cpp_clean/10258/10258-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ac04a365b2fdb6ba513bf44067d436cd7717027 --- /dev/null +++ b/uva_cpp_clean/10258/10258-14.cpp @@ -0,0 +1,183 @@ +/*************************************************** + * Problem Name : 10258 - Contest Scoreboard.cpp + * Problem Link : https://onlinejudge.org/external/102/10258.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-03-28 + * Problem Type : STL + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +vector purse (string str) { + stringstream ss; + ss << str; + vectorvec; + string num; + + while (ss >> num) { + vec.pb (num); + } + + return vec; +} + +bool cmp (pair > a, pair > b) { + if (a.first == b.first) { + if (a.second.first == b.second.first) { + return a.second.second < b.second.second; + + } else { + return a.second.first < b.second.first; + } + + } else { + return a.first > b.first; + } +} + +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + cin.ignore(); + cin.ignore(); + int t = 1; + + while (t <= tc) { + int user, prob_id, sb_time; + string ver; + vector > >vec (105); + map >mp, ac; + mapp_user; + string str; + + while (getline (cin, str) ) { + if (str.size() == 0) break; + + vectorvv = purse (str); + user = toInt (vv[0]), prob_id = toInt (vv[1]), sb_time = toInt (vv[2]), + ver = vv[3]; + + if (ver[0] == 'C') { + if (ac[user][prob_id] == 1) continue; + + int now_time = sb_time + mp[user][prob_id] * 20; + int cnt = vec[user].first + 1; + int total_time = vec[user].second.first + now_time; + vec[user] = {cnt, {total_time, user}}; + ac[user][prob_id] = 1; + + } else if (ver[0] == 'I') { + mp[user][prob_id]++; + } + + int cnt = vec[user].first; + int total_time = vec[user].second.first; + vec[user] = {cnt, {total_time, user}}; + p_user[user] = true; + } + + sort (all (vec), cmp); + + if (t != 1) nl; + + t++; + + for (auto it : vec) { + if (p_user[it.second.second] == false) continue; + + cout << it.second.second << " " << it.first << " " << it.second.first << "\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/10258/10258-20.cpp b/uva_cpp_clean/10258/10258-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fb2cb47a5304323840985e7d11a0c8c20365206 --- /dev/null +++ b/uva_cpp_clean/10258/10258-20.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +const int MAXN = 101; + +struct data { + bool flag; + int id; + int nSolved; + int time; + int penalty[10]; + bool solved[10]; +} contestants[MAXN]; + +bool cmp( data a, data b ) { + if( a.nSolved > b.nSolved ) { + return (true); + } + else if( (a.nSolved == b.nSolved) && (a.time < b.time) ) { + return (true); + } + else if( (a.nSolved == b.nSolved) && (a.time == b.time) && (a.id < b.id) ) { + return (true); + } + else { + return (false); + } +} + +int main( int argc, char ** argv ) { + int tc; + scanf( "%d ", &tc ); + for( int nCase = 1 ; nCase <= tc ; ++nCase ) { + for( int i = 0 ; i < MAXN ; ++i ) { + contestants[i].flag = false; + contestants[i].id = i; + contestants[i].nSolved = 0; + contestants[i].time = 0; + memset( contestants[i].penalty, 0, sizeof( contestants[i].penalty ) ); + memset( contestants[i].solved, false, sizeof( contestants[i].solved ) ); + } + + string input; + while( getline( cin, input ) && input.size() ) { + stringstream ss( input ); + + int x, y, z; + char L; + ss >> x >> y >> z >> L; + + contestants[x].flag = true; + if( L == 'I' && !contestants[x].solved[y] ) { + contestants[x].penalty[y] += 20; + } + if( L == 'C' && !contestants[x].solved[y] ) { + contestants[x].solved[y] = true; + contestants[x].penalty[y] += z; + } + } + + for( int i = 0 ; i < MAXN ; ++i ) { + if( contestants[i].flag ) { + for( int j = 1 ; j <= 9 ; ++j ) { + if( contestants[i].solved[j] ) { + contestants[i].nSolved++; + contestants[i].time += contestants[i].penalty[j]; + } + } + } + } + + sort( contestants, contestants + MAXN, cmp ); + + if( nCase > 1 ) puts( "" ); + for( int i = 0 ; i < MAXN ; ++i ) { + if( contestants[i].flag ) { + printf( "%d %d %d\n", contestants[i].id, contestants[i].nSolved, contestants[i].time ); + } + } + } + return 0; +} diff --git a/uva_cpp_clean/10258/10258-21.cpp b/uva_cpp_clean/10258/10258-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ca5f1e048680fa2aaedc828997fc1f39ad1a422 --- /dev/null +++ b/uva_cpp_clean/10258/10258-21.cpp @@ -0,0 +1,72 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10258 + Name: Contest Scoreboard + Problem: https://onlinejudge.org/external/102/10258.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define ms(a,n) memset(a,n,sizeof(a)) +struct person{ + int id; + int count; + int penalty[10]; +}; + +int main() +{ + int tt;cin>>tt; + char line[100],l; + int m,prob,time,i,j; + + + gets(line);gets(line); + while(tt--){ + vector membs(110); + bool probs[110][15]={{0}}; + + while(gets(line) && sscanf(line,"%d %d %d %c",&m,&prob,&time,&l)==4) + { + if(prob<1 || m<1)continue; + membs[m].id=m; + + if(l=='C' && !probs[m][prob]) + { + membs[m].count++; + membs[m].penalty[prob]+=time; + probs[m][prob]=1; + } else if(l=='I' && !probs[m][prob]) + membs[m].penalty[prob]+=20; + } + + for(i=1;i<101;i++){ + time=0; + for(j=1;j<10;j++) + if(probs[membs[i].id][j])time+=membs[i].penalty[j]; + membs[i].penalty[0]=time; + } + + for(i=1;i<101;i++) + for(j=1;j<101;j++) + if(membs[i].count>membs[j].count)swap(membs[i],membs[j]); + else if(membs[i].count==membs[j].count) + if(membs[i].penalty[0] +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +struct contest +{ + ll penalty=0,solved=0; + int id=110; +}; + +bool cmp(contest a, contest b) +{ + if(a.solved!=b.solved) + return a.solved>b.solved; + else if(a.penalty!=b.penalty) + return a.penaltyv; + while(gets(s)) + { + if(strcmp(s,"")==0) break; + int i,p,t; + char c; + sscanf(s,"%d %d %d %c",&i,&p,&t,&c); +// PPN(i,p),P(t),CP(c); + if(c=='I' && check[i][p]!=-1) + { + check[i][p]+=20; + judge[i].id=i; + } + else if(c=='C' && check[i][p]!=-1) + { + judge[i].penalty+=(t+check[i][p]); + judge[i].solved+=1; + judge[i].id=i; + check[i][p]=-1; + } + else + { + judge[i].id=i; + } + } + sort(judge,judge+105,cmp); + + for0(i,105) + { + if(judge[i].id==110) + break; + PPN(judge[i].id,judge[i].solved),P(judge[i].penalty); + } + if(t) + nl; + } + + return 0; +} + + diff --git a/uva_cpp_clean/10258/10258-9.cpp b/uva_cpp_clean/10258/10258-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40277102b698e81a3372ab8ea293378ade4af542 --- /dev/null +++ b/uva_cpp_clean/10258/10258-9.cpp @@ -0,0 +1,61 @@ +#include + +#define SIZE 100 +#define MAX_CONT 110 +#define MAX_PROB 20 + +using namespace std; + +struct part{ + int c, np, penalty; +} aux; + +int tc, cont, prob, tim, score[MAX_CONT][MAX_PROB]; +char s[SIZE], ch; +bool solved[MAX_CONT][MAX_PROB], submission[MAX_CONT]; +vector ans; + +bool f(part x, part y){ + if(x.np != y.np) return (x.np > y.np); + if(x.penalty != y.penalty) return (x.penalty < y.penalty); + return (x.c < y.c); +} + +void fillRanking(){ + for(int c = 1; c < 110; c++){ + aux.c = c, aux.np = aux.penalty = 0; + for(int p = 1; p <= 12; p++) if(solved[c][p]) aux.penalty += score[c][p], aux.np++; + if(aux.np >= 1 || submission[c]) ans.push_back(aux); + } +} + +void printRanking(){ + for(int i = 0; i < ans.size(); i++) + printf("%d %d %d\n", ans[i].c, ans[i].np, ans[i].penalty); + ans.clear(); + memset(solved, false, sizeof solved); + memset(score, 0, sizeof score); + memset(submission, false, sizeof submission); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + while(cin.getline(s, SIZE)){ + if(sscanf(s,"%d %d %d %c", &cont, &prob, &tim, &ch)==-1) break; + submission[cont] = true; + if(ch == 'R' || ch == 'U' || ch == 'E') continue; + if(solved[cont][prob] == false){ + if(ch == 'C') + score[cont][prob] += tim, solved[cont][prob] = true; + if(ch == 'I') + score[cont][prob] += 20; + } + } + fillRanking(); + sort(ans.begin(),ans.end(),f); + printRanking(); + if(tc) puts(""); + } + return(0); +} diff --git a/uva_cpp_clean/10259/10259-21.cpp b/uva_cpp_clean/10259/10259-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d00cceb3bcf683509e90d4e89b1eee45c8ce4c1c --- /dev/null +++ b/uva_cpp_clean/10259/10259-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10259 + Name: Hippity Hopscotch + Problem: https://onlinejudge.org/external/102/10259.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +typedef pair Pair; +#define ff first +#define ss second + +bool inq[143][143]; +int di[] = {1,-1,0,0}, + dj[] = {0,0,1,-1}; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int D[143][143], X[143][143], T, n, d; + cin >> T; + while (T-- && cin >> n >> d) { + for (int i=0; i> X[i][j]; + D[i][j] = 0; + } + + queue q; + q.push(Pair(0, 0)); + D[0][0] = 0; + + int mxr = 0; + while (!q.empty()) { + Pair s = q.front(); q.pop(); + int score = D[s.ff][s.ss] + X[s.ff][s.ss]; + mxr = max(mxr, score); + inq[s.ff][s.ss] = 0; + + for (int k=1; k<=d; ++k) + for (int dir=0; dir<4; ++dir) { + Pair t(s.ff + k*di[dir], s.ss + k*dj[dir]); + if (t.ff>=0 && t.ff=0 && t.ss X[s.ff][s.ss]) { + if (D[t.ff][t.ss] < score) { + D[t.ff][t.ss] = score; + if (!inq[t.ff][t.ss]) { + inq[t.ff][t.ss] = 1; + q.push(t); + } + } + } + } + } + cout << mxr << '\n'; + if (T) cout << '\n'; + } +} diff --git a/uva_cpp_clean/10260/10260-14.cpp b/uva_cpp_clean/10260/10260-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5052eeb11b1340b5346c816d2440e2a132bba236 --- /dev/null +++ b/uva_cpp_clean/10260/10260-14.cpp @@ -0,0 +1,84 @@ +/*************************************************** + * Problem name : 10260 Soundex.cpp + * OJ : Uva + * Verdict : AC + * Date : 09.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/////////////////////////////////////////////////////////////////////////////////////////////////////////////// +char Number(char ch) { + if (ch == 'B' || ch == 'F' || ch == 'P' || ch == 'V') return '1'; + else if (ch == 'C' || ch == 'G' || ch == 'J' || ch == 'K' || ch == 'Q' || ch == 'S' || ch == 'X' || ch == 'Z') return '2'; + else if (ch == 'D' || ch == 'T') return '3'; + else if (ch == 'L') return '4'; + else if (ch == 'M' || ch == 'N') return '5'; + else if (ch == 'R') return '6'; + else return '0'; +} +int main () { + string str; + + while (cin >> str) { + int sz = str.size(); + char flag = '0'; + string code; + for (int i = 0; i < sz; i++) { + char n = Number(str[i]); + if(n != '0'){ + if(n != flag){ + code+=n; + flag = n; + } + } + else if(n == '0') flag = '0'; + } + cout << code; + nl; + } + return 0; +} + diff --git a/uva_cpp_clean/10260/10260-19.cpp b/uva_cpp_clean/10260/10260-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec6ae679529e05803fcaa1761503db8406bce8b1 --- /dev/null +++ b/uva_cpp_clean/10260/10260-19.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +char soundex(char c) { + if (c == 'B' || c == 'F' || c == 'P' || c == 'V') return '1'; + if (c == 'C' || c == 'G' || c == 'J' || c == 'K' || c == 'Q' || c == 'S' || c == 'X' || c == 'Z') return '2'; + if (c == 'D' || c == 'T') return '3'; + if (c == 'L') return '4'; + if (c == 'M' || c == 'N') return '5'; + if (c == 'R') return '6'; + return ' '; +} + +int main() { + string s; + while (cin >> s) { + char prev = ' ', c; + for (int i = 0; i < s.length(); i++) { + c = soundex(s[i]); + if (c != prev && c != ' ') printf("%c", c); + prev = c; + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10260/10260-21.cpp b/uva_cpp_clean/10260/10260-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d3f9ea0f2a6800cc227775d61248aa0c4c0f49f --- /dev/null +++ b/uva_cpp_clean/10260/10260-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10260 + Name: Soundex + Problem: https://onlinejudge.org/external/102/10260.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int R[] = { 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2 }; +int main(){ + char word[30]; + while(cin>>word) { + int l=-1; + long long int n = 0; + for (int i=0; word[i]; i++) { + int d = R[word[i]-'A']; + if (d && d!=l) + n = n*10 + d; + l = d; + } + + if (!n) cout< +#include +int main() +{ + char a[1000]; + int i,l,b[1000]; + while(gets(a)) + { + l=strlen(a); + for(i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10261 + Name: Ferry Loading + Problem: https://onlinejudge.org/external/102/10261.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int X[243], C[243], len, n, + DP[243][10143]; +bool P[243][10143]; + +int maxCar() { + memset(DP[n], 0, (len+1)*sizeof(int)); + for (int id=n-1; id>=0; --id) + for (int r1=0; r1<=len; ++r1) { + int r2 = 2*len - r1 - C[id]; + + int mx = 0; + if (X[id] <= r1) { + mx = DP[id+1][r1-X[id]] + 1; + P[id][r1] = 0; + } + + if (X[id] <= r2) { + int t = DP[id+1][r1] + 1; + if (t > mx) { + mx = t; + P[id][r1] = 1; + } + } + DP[id][r1] = mx; + } + return DP[0][len]; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + while (T--) { + cin >> len; len*=100; + for (n=0; cin>>X[n] && X[n]; ++n); + for (int i=1; i<=n; ++i) + C[i] = C[i-1] + X[i-1]; + + int sz = maxCar(); + cout << sz << "\n"; + for (int i=0, rem=len, id=0; i 0-1 knapsack + difficulty: medium + date: 15/May/2020 + problem: as if there were 2 knapsacks to fill at the same time; with recovering + by: @brpapa +*/ +#include +using namespace std; +const int INF = 1 << 30; +const int MAX_L = 10001; +const int MAX_C = 202; + +int cars[MAX_C]; int C; + +int memo[MAX_C][MAX_L]; +int dp(int c, int reml, int remr) { + // carro atual c, restando reml na esquerda e remr na direita + + if (c == C) return 0; + + int &ans = memo[c][reml]; + if (ans != -1) return ans; + + int l = -1, r = -1; + if (reml >= cars[c]) l = dp(c+1, reml-cars[c], remr); + if (remr >= cars[c]) r = dp(c+1, reml, remr-cars[c]); + + // anula se não for possível embarcar esse carro (c) em nenhum dos 2 lados + return ans = (l == -1 && r == -1)? 0 : (1 + max(l, r)); +} + +void recover(int L) { + int reml = L, remr = L; + for (int c = 0; c < C; c++) { + int l = -1, r = -1; + if (reml >= cars[c]) l = dp(c+1, reml-cars[c], remr); + if (remr >= cars[c]) r = dp(c+1, reml, remr-cars[c]); + if (l == -1 && r == -1) break; + + if (l >= r) { + cout << "port" << endl; + reml -= cars[c]; + } else { + cout << "starboard" << endl; + remr -= cars[c]; + } + } +} + +int main() { + int t = 0, T; cin >> T; + while (T--) { + if (t++ > 0) cout << endl; + int L; cin >> L; L *= 100; + for (C = 0; true; C++) { + int len; cin >> len; if (len == 0) break; + cars[C] = len; + } + + memset(memo, -1, sizeof memo); + cout << dp(0, L, L) << endl; + recover(L); + } + return 0; +} diff --git a/uva_cpp_clean/10261/10261-9.cpp b/uva_cpp_clean/10261/10261-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e41e32ccbc4df206d95a99e2e335a84649ad60c0 --- /dev/null +++ b/uva_cpp_clean/10261/10261-9.cpp @@ -0,0 +1,51 @@ +#include + +#define MAX_N 1010 +#define MAX_W 10010 + +using namespace std; + +int tc,n,ferry,v[MAX_N],dp[MAX_N][MAX_W],cumSum[MAX_N],path[MAX_N]; +bool take[MAX_N][MAX_W],ok; + +void fillPath(int id,int w){ + if(id==0) return; + if(take[id][w]) + fillPath(id-1,w-v[id]), path[id] = true; + else + fillPath(id-1,w); +} + +int main(){ + scanf("%d",&tc); + while(tc--){ + n = 1, ok = true; + memset(dp,0,sizeof dp); + memset(take,false,sizeof take); + memset(path,false,sizeof path); + scanf("%d",&ferry); + ferry *= 100; + while(scanf("%d",&v[n]),v[n]) cumSum[n] = v[n]+cumSum[n-1], n++; + for(int i=1;idp[i][w]) + dp[i][w] = v[i]+dp[i-1][w-v[i]], take[i][w] = true; + } + for(int i=n-1;i>0;i--){ + int sum = dp[i][ferry]; + if(sum<=ferry && cumSum[i]-sum<=ferry){ + ok = false; + fillPath(i,ferry); + printf("%d\n",i); + for(int it=1;it<=i;it++) + if(path[it]) puts("starboard"); + else puts("port"); + break; + } + } + if(ok) puts("0"); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10262/10262-21.cpp b/uva_cpp_clean/10262/10262-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86a12f5ab4543b40918d585b506d9a739e696a11 --- /dev/null +++ b/uva_cpp_clean/10262/10262-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10262 + Name: Suffidromes + Problem: https://onlinejudge.org/external/102/10262.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool ispalindrome(const char s[], int len) { + for (int i=len/2-1; i>=0; --i) + if (s[i] != s[len-i-1]) + return 0; + return 1; +} + +char* revs(char s[], int len) { + for (int i=len/2-1; i>=0; --i) + swap(s[i], s[len-i-1]); + s[len] = 0; + return s; +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char s1[1024], s2[1024]; + while (cin.getline(s1, 1024) && cin.getline(s2, 1024)) { + if (!strcmp(s1, s2)) + cout << "No solution.\n"; + + else { + int l1 = strlen(s1), + l2 = strlen(s2); + bool eq = 1; + for (int i=0; ; ++i) { + bool f1 = ispalindrome(s1+i, l1-i), + f2 = ispalindrome(s2+i, l2-i); + + if (f1 && !f2) + cout << revs(s1, i) << '\n'; + + else if (f2 && !f1) + cout << revs(s2, i) << '\n'; + + else if (f1 && f2 && !eq) { + revs(s1, i); revs(s2, i); + cout << (strcmp(s1,s2)<0 ? s1 : s2) << '\n'; + } + + else if (i==l2) { + if (f1 && f2) + cout << (s1[i]=='a' ? 'b' : 'a'); + cout << revs(s2, i) << '\n'; + } + + else if (i==l1) { + if (f1 && f2) + cout << (s2[i]=='a' ? 'b' : 'a'); + cout << revs(s1, i) << '\n'; + } + + else { + eq &= s1[i] == s2[i]; + continue; + } + break; + } + } + } +} diff --git a/uva_cpp_clean/10263/10263-21.cpp b/uva_cpp_clean/10263/10263-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de4e97e714d6bc5aa4b53e16925f310c8266f4c --- /dev/null +++ b/uva_cpp_clean/10263/10263-21.cpp @@ -0,0 +1,112 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10263 + Name: Railway + Problem: https://onlinejudge.org/external/102/10263.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 1e-8 + +struct Point { + double x, y; + Point():x(0),y(0){} + Point(double x, double y):x(x),y(y){} + bool operator < (const Point &o) const { return x < o.x; } + double operator * (const Point o) const { return x*o.x + y*o.y; } + Point operator - (const Point o) const { return Point(x-o.x, y-o.y); } +}; + +double dist2(const Point p1, const Point p2) { + double xx = p1.x - p2.x, yy = p1.y - p2.y; + return xx*xx + yy*yy; +} +double dist(const Point p1, const Point p2) { return sqrt(dist2(p1, p2)); } + +struct Line { + double a, b, c; + Line():a(0),b(0),c(1){} + Line(double x, double y):a(x),b(y),c(1){} + Line(double a, double b, double c):a(a),b(b),c(c){} + Line(const Point p1, const Point p2) { + if (fabs(p1.x - p2.x) < EPS) { + b = 0; + if (fabs(p1.x) < EPS) a=1, c=0; + else a = -1/p1.x, c = 1; + } + else { + a = p2.y - p1.y; + b = p1.x - p2.x; + c = -p1.y*b - p1.x*a; + } + } +}; + +int intersect(const Line l1, const Line l2, Point &r) { + r.x = l1.b*l2.c - l2.b*l1.c; + r.y = l2.a*l1.c - l1.a*l2.c; + double cc = l1.a*l2.b - l2.a*l1.b; + if (fabs(cc) < EPS) + return fabs(r.y)> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} +std::ostream& operator<< (std::ostream& stream, const Point p) { + stream << p.x << ' ' << p.y; + return stream; +} + +// ------------------------------------------------------------------------- + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(4); + + int n; + Point mp, lp, cp, bp; + while (cin >> mp >> n) { + double bd = 1e100; + cin >> lp; + for (int i=0; i> cp; + + Point r = nearest(lp, cp, mp); + double d = dist2(r, mp); + if (bd > d) { + bd = d; + bp = r; + } + + lp = cp; + } + + cout << bp.x << '\n' << bp.y << '\n'; + } +} diff --git a/uva_cpp_clean/10264/10264-11.cpp b/uva_cpp_clean/10264/10264-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61dce3e4c3ae23bc1659091f88e1d0c0f3719a81 --- /dev/null +++ b/uva_cpp_clean/10264/10264-11.cpp @@ -0,0 +1,94 @@ +#include +/* +Problem: 10264 - The Most Potent Corner +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1205 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int N; +int corners[1<<15]; +int potencies[1<<15]; + +bool areNeighbors(int i, int j); + +int main () { + fastio; + + while ( cin >> N && !cin.eof() ){ + + int ans = INT_MIN; + // an N-dimensional cube has pow(N, 2) corners + for (int i = 0; i < (1<> corners[i]; + + // calculating the potency of each corner + for (int i = 0; i < (1< +#include +using namespace std; + +int main() { + unordered_map v, p; + int N; + while (cin >> N) { + for (int i = 0; i < (1<> v[i]; + } + for (int i = 0; i < (1< maxi) maxi = p[i] + p[(i ^ (1<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10264 + Name: The Most Potent Corner + Problem: https://onlinejudge.org/external/102/10264.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +int main() { + int n, w, sz, p[65536]; + while (scanf("%d", &n)==1 && n) { + memset(p, 0, (sz=1< mxs) mxs = sum; + } + + printf("%d\n", mxs); + } +} diff --git a/uva_cpp_clean/10264/10264-9.cpp b/uva_cpp_clean/10264/10264-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb700fb487e4aada09cb188debc1c2c7b23b4bba --- /dev/null +++ b/uva_cpp_clean/10264/10264-9.cpp @@ -0,0 +1,23 @@ +#include + +#define SIZE 1<<16 + +using namespace std; + +int n, v[SIZE], u[SIZE], lim, sum, nd, ans; + +int main(){ + while(scanf("%d", &n) != EOF){ + lim = (1< +#include +#include +using namespace std; + +int main() { + string l; + long long val, val2; + while (cin >> val2) { + getline(cin, l); + getline(cin, l); + stringstream ss(l); + vector pol; + while (ss >> val) pol.push_back(val); + long long pot = 1, acum = 0; + for (int i = pol.size() - 2, n = 1; i >= 0; i--, n++) { + acum += pot*pol[i]*n; + pot *= val2; + } + printf("%lld", acum); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10268/10268-9.cpp b/uva_cpp_clean/10268/10268-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..767752ef90c2610734f7ae9272f1cf1371532bde --- /dev/null +++ b/uva_cpp_clean/10268/10268-9.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +const int LEN = 1000000, SIZE = 1000000; + +long long x, n, c[SIZE]; +char line[LEN], *p; + +long long Horner(long long X){ + long long fX = c[0] * --n; + for(int it = 1; it < n; it++) + fX = fX * X + c[it] * ( n - it ); + return fX; +} + +int main(){ + while(~scanf("%lld\n", &x)){ + cin.getline(line, LEN); + p = strtok(line, " "); + n = 0; + while(p) + c[n++] = atoll(p), p = strtok(NULL, " "); + printf("%lld\n", Horner(x)); + } + return(0); +} diff --git a/uva_cpp_clean/10271/10271-14.cpp b/uva_cpp_clean/10271/10271-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..416b0cbe86ecf1b75171472d77987fb6d6763d8c --- /dev/null +++ b/uva_cpp_clean/10271/10271-14.cpp @@ -0,0 +1,129 @@ +/*************************************************** + * Problem Name : 10271 - Chopsticks.cpp + * Problem Link : https://uva.onlinejudge.org/external/102/10271.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-08-31 + * Problem Type : DP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 5005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[MAX], n; +ll dp[MAX][1005]; + +ll fun (int pos, int k) { + if (pos > n || k <= 0) { + return 0LL; + } + + ll &ans = dp[pos][k]; + + if (ans != -1) return ans; + + ans = 0; + ll a = INF, b = INF; + + if ( ( (n - pos) ) >= (k * 3) ) { + a = fun (pos + 1, k); + } + + b = (ll) Sqr (ar[pos] - ar[pos + 1]) + fun (pos + 2, k - 1); + return ans = min (a, b); +} + +int main () { + __FastIO; + int tc, k; + cin >> tc; + + while (tc--) { + cin >> k >> n; + + for (int i = 1; i <= n; i++) { + cin >> ar[i]; + } + + SET (dp, -1); + ll ans = fun (1, k + 8); + cout << ans << "\n"; + } + + return 0; +} + diff --git a/uva_cpp_clean/10276/10276-19.cpp b/uva_cpp_clean/10276/10276-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5025c10bade5c7b61313dfacdddcdc0757f0568 --- /dev/null +++ b/uva_cpp_clean/10276/10276-19.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; + +int main() { + int T, N; + int pegs[50]; + cin >> T; + while (T--) { + cin >> N; + for (int i = 0; i < N; i++) pegs[i] = 0; + bool ok = true; + int i = 1; + for (; ok; i++) { + ok = false; + for (int j = 0; j < N && !ok; j++) { + int sq = sqrt(pegs[j] + i); + if (pegs[j] == 0 || sq*sq == pegs[j] + i) + pegs[j] = i, ok = true; + } + } + i -= 2; + printf("%d\n", i); + } + return 0; +} diff --git a/uva_cpp_clean/10276/10276-21.cpp b/uva_cpp_clean/10276/10276-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c24258c118bf249233a770c9a9116e4253364f05 --- /dev/null +++ b/uva_cpp_clean/10276/10276-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10276 + Name: Hanoi Tower Troubles Again! + Problem: https://onlinejudge.org/external/102/10276.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int DP[60]={0, 1}; +int main(){ + for (int i=2; i<52; i++) + DP[i] = DP[i-1] + i + (i&1); + + int T, n; + cin>>T; + while (T--) { + cin>>n; + cout << DP[n] << endl; + } +} diff --git a/uva_cpp_clean/10276/10276-9.cpp b/uva_cpp_clean/10276/10276-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..543fffdb39a9f1d9ea3e04ea2110b51fefe18c62 --- /dev/null +++ b/uva_cpp_clean/10276/10276-9.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +vector a[50]; +vector m(1000000); +bool v[1000000]; +int n, ans, c; + +void g(){ + for(int i = 1; i < 1000; i++) + m[i * i] = true; +} + +void f(int ct){ + for(int i = 0; i < n; i++) + if(!a[i].size() || ( !v[ct] && m[a[i][a[i].size() - 1] + ct] )){ + a[i].push_back(ct); + v[ct] = true; + ans++; + f(ct + 1); + } +} + +int main(){ + g(); + scanf("%d", &c); + while(c--){ + scanf("%d", &n); + f(1); + for(int i = 0; i < n; i++) + a[i].clear(); + printf("%d\n", ans); + ans = 0; + memset(v, false, sizeof v); + } + return(0); +} diff --git a/uva_cpp_clean/10278/10278-21.cpp b/uva_cpp_clean/10278/10278-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fd5d34f160f948bb8943088161eee9fa44c920f --- /dev/null +++ b/uva_cpp_clean/10278/10278-21.cpp @@ -0,0 +1,92 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10278 + Name: Fire Station + Problem: https://onlinejudge.org/external/102/10278.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +int M[555][555], D[555], E[555], n; +vector adj[555]; + +struct UD { + int u, d; + UD(int u, int d):u(u),d(d){} + bool operator < (const UD& o) const { return d > o.d; } +}; + + +int dijkstra(priority_queue &q, int D[]) { + while (!q.empty()) { + UD ud = q.top(); q.pop(); + int u = ud.u; + if (D[u] == ud.d) + for (int v: adj[u]) + if (D[u]+M[u][v] < D[v]) { + D[v] = D[u] + M[u][v]; + q.push(UD(v, D[v])); + } + } + + int mxd=0; + for (int u=0; u>T; + while (T--) { + cin >> f >> n; + for (int i=0; i q; + for (int i=0, s; i> s; --s; + q.push(UD(s, D[s]=0)); + } + + for (int u, v, d; cin.ignore(1000, '\n') && cin.peek()!='\n'; ) + if (cin>>u>>v>>d) { + if (M[--u][--v] == INF) { + adj[u].push_back(v); + adj[v].push_back(u); + } + M[u][v] = M[v][u] = min(M[u][v], d); + } + + int mxd = dijkstra(q, D), + mxu = 0; + + for (int u=0; u qq; + qq.push(UD(u, E[u]=0)); + + int dd = dijkstra(qq, E); + if (dd < mxd) { + mxd = dd; + mxu = u; + } + } + + cout << mxu+1 << endl; + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/10279/10279-21.cpp b/uva_cpp_clean/10279/10279-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ffc8070aeb5f2db4decd84651fa94eec127dfb0 --- /dev/null +++ b/uva_cpp_clean/10279/10279-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10279 + Name: Mine Sweeper + Problem: https://onlinejudge.org/external/102/10279.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + while (T-- && cin>>n) { + char B[17][17]={}, P[17][17]={}; + + for (int i=1; i<=n; ++i) + cin >> &B[i][1]; + + bool boom = 0; + for (int i=1; i<=n; ++i) { + cin >> &P[i][1]; + for (int j=1; !boom && j<=n; ++j) + if (P[i][j]=='x' && B[i][j]=='*') + boom = 1; + } + + + for (int i=1; i<=n; ++i) { + for (int j=1; j<=n; ++j) + if (boom && B[i][j]=='*') + cout << '*'; + else if (P[i][j] == '.') + cout << '.'; + else { + int sum=0; + for (int l=-1; l<2; ++l) + for (int r=-1; r<2; ++r) + sum += B[i+l][j+r] == '*'; + cout << sum; + } + cout << '\n'; + } + + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/10279/10279-9.cpp b/uva_cpp_clean/10279/10279-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e87e781b07afd7261715afb6d32bd782b66a35ea --- /dev/null +++ b/uva_cpp_clean/10279/10279-9.cpp @@ -0,0 +1,49 @@ +#include + +#define SIZE 110 + +using namespace std; + +int tc, n, m, table[SIZE][SIZE]; +char mines[SIZE][SIZE], s[SIZE][SIZE]; +int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; +int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; +bool mine; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d\n", &n); + for(int row = 0; row < n; row++) + scanf("%s", mines[row]); + for(int row = 0; row < n; row++) + scanf("%s", s[row]); + memset(table, 0, sizeof table); + mine = false; + for(int row = 0; row < n; row++) + for(int col = 0; col < n; col++){ + if( mines[row][col] == '*' ){ + if( s[row][col] == 'x' ) mine = true; + for(int it = 0; it < 8; it++){ + int r = row + dy[it], c = col + dx[it]; + if( r >= 0 and c >= 0 and r < n and c < n ) + table[r][c]++; + } + } + } + for(int row = 0; row < n; row++){ + for(int col = 0; col < n; col++) + if( s[row][col] == 'x' ){ + if( mines[row][col] == '*' and mine ) putchar('*'); + else printf("%d", table[row][col]); + } + else if( mine ) + printf("%c", mines[row][col]); + else + putchar('.'); + putchar('\n'); + } + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10281/10281-11.cpp b/uva_cpp_clean/10281/10281-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a9170d9eedca7f663e75276a999249114969273 --- /dev/null +++ b/uva_cpp_clean/10281/10281-11.cpp @@ -0,0 +1,66 @@ +#include +//#include +/* +Problem: 10281 - Average Speed +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1222 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int main () { + + int hh, mm, ss; + char line[2000]; + double ltime = 0, ctime = 0, speed = 0, distance = 0; + + while ( cin.getline(line, sizeof line) ){ + sscanf(line, "%d:%d:%d", &hh, &mm, &ss); + ctime = (hh + mm/60.0 + ss/3600.0); + + if (line[8] != '\0'){ + distance += speed * (ctime-ltime); + sscanf(line+8, "%lf", &speed); + } + + else { + distance += speed * (ctime-ltime); + printf("%02d:%02d:%02d %0.2lf km\n", hh, mm, ss, distance); + } + + ltime = ctime; + } + + return 0; +} +/* +Sample input:- +----------------- +00:00:01 100 +00:15:01 +00:30:01 +01:00:01 50 +03:00:01 +03:00:05 140 + +Sample output:- +----------------- +00:15:01 25.00 km +00:30:01 50.00 km +03:00:01 200.00 km + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10281/10281-19.cpp b/uva_cpp_clean/10281/10281-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0afa6d25c468d59baac28b25d69b560e057b0bcd --- /dev/null +++ b/uva_cpp_clean/10281/10281-19.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +using namespace std; + +int main() { + double time = 0, speed = 0, dist = 0; + string line; + while (getline(cin, line)) { + double stamp = 60*(60*(line[0]*10 + line[1]) + line[3]*10 + line[4]) + line[6]*10 + line[7]; + if (line.length() > 8) { + dist += (stamp-time)*speed; + time = stamp; + double aux = 0; + for (int i = 9; i < line.length(); i++) aux *= 10, aux += line[i]-'0'; + speed = aux/3600; + } else { + printf("%s %.2f km\n", line.substr(0, 8).c_str(), dist + (stamp-time)*speed); + } + } + return 0; +} diff --git a/uva_cpp_clean/10281/10281-20.cpp b/uva_cpp_clean/10281/10281-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5900aa3f838b1a2bb8be8f64a005ab2142ec089 --- /dev/null +++ b/uva_cpp_clean/10281/10281-20.cpp @@ -0,0 +1,23 @@ +#include + +char line[100005]; +int hh, mm, ss, newSpeed; +int temp, sec, sec1; +double dis, speed; + +int main() +{ + dis = 0.0, sec1 = 0, speed = 0.0; + while( gets(line) ) + { + temp = sscanf(line, "%d:%d:%d %d", &hh, &mm, &ss, &newSpeed); + + sec = (hh*3600)+(mm*60)+ss; + dis += ((sec-sec1)*speed)/3600.0; + sec1 = sec; + + if( temp==4 ) speed = newSpeed; + else printf("%.2d:%.2d:%.2d %.2lf km\n", hh, mm, ss, dis); + } + return 0; +} diff --git a/uva_cpp_clean/10281/10281-21.cpp b/uva_cpp_clean/10281/10281-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18c306bfc326049eb7d36ccd031a9800fdbc04bf --- /dev/null +++ b/uva_cpp_clean/10281/10281-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10281 + Name: Average Speed + Problem: https://onlinejudge.org/external/102/10281.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char line[100]; + double otime=0, offset=0; + int speed=0; + while (cin.getline(line, 100)) if (line[0]) { + int h, m, s, sp; + int tp = sscanf(line, "%d:%d:%d %d", &h, &m, &s, &sp); + double tm = h + m/60.0 + s/3600.0; + + if (tp == 4) { + offset += (tm - otime) * speed; + speed = sp; + otime = tm; + } + + else printf("%02d:%02d:%02d %.2f km\n", h,m,s, offset+(tm - otime)*speed); + } +} diff --git a/uva_cpp_clean/10281/10281-5.cpp b/uva_cpp_clean/10281/10281-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0e0b167b62b8f9eb703c5bb80c7ff504ad10b19f --- /dev/null +++ b/uva_cpp_clean/10281/10281-5.cpp @@ -0,0 +1,23 @@ +#include +int main() +{ + double h1,m1,s1,h,m,s,hour,v,dist=0,left_hour=0; + char a[200]; + while(gets(a)) + { + sscanf(a,"%lf:%lf:%lf",&h1,&m1,&s1); + hour=h1*60+m1+s1/60; + if(a[8]=='\0') + { + dist=dist+(v*(hour-left_hour))/60; + printf("%s %.2lf km\n",a,dist); + } + else + { + dist=dist+(v*(hour-left_hour))/60; + sscanf(a+8,"%lf",&v); + } + left_hour=hour; + } + return 0; +} diff --git a/uva_cpp_clean/10281/10281-9.cpp b/uva_cpp_clean/10281/10281-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b0eb24506697ac1b681a1dd96fbf2b3e407953d --- /dev/null +++ b/uva_cpp_clean/10281/10281-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main(){ + char tmp[100]; + int h, m, s, v = 0, nv, t = 0, l, n; + double d = 0; + while (cin.getline(tmp, 100)){ + n = sscanf(tmp, "%d:%d:%d %d", &h, &m, &s, &nv); + t = h * 3600 + m * 60 + s; + d += (t - l) * v; + l = t; + if (n == 3) + printf("%.2d:%.2d:%.2d %.2f km\n", h, m, s, d / 3600); + else if (n == 4) + v = nv; + } + return(0); +} diff --git a/uva_cpp_clean/10282/10282-14.cpp b/uva_cpp_clean/10282/10282-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e61619e46cfd6ff1338d07e968dd6e8d55b3c33 --- /dev/null +++ b/uva_cpp_clean/10282/10282-14.cpp @@ -0,0 +1,87 @@ +/*************************************************** + * Problem name : 10282 Babelfish.cpp + * Problem Link : https://uva.onlinejudge.org/external/102/10282.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-08-04 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +mapmp; +int main () { + string str; + while (getline(cin, str)) { + int sz = str.size(); + if (sz == 0) break; + int flag = 0; + string a, b; + for (int i = 0; i < sz; i++) { + if (str[i] == ' ') { + flag = 1; + } else { + if (flag == 0) { + a += str[i]; + } else b += str[i]; + } + } + mp[b] = a; + //~ cout << a << " " << b ; + //~ nl; + } + string s; + while(cin >> s){ + if(mp[s].size() == 0){ + cout << "eh\n"; + } + else cout << mp[s]<<"\n"; + } + + return 0; +} + diff --git a/uva_cpp_clean/10282/10282-19.cpp b/uva_cpp_clean/10282/10282-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b21d6f67b3ee5cc92ac29ce8b64f5863f6f06e76 --- /dev/null +++ b/uva_cpp_clean/10282/10282-19.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +using namespace std; + +int main() { + string s; + unordered_map m; + while (getline(cin, s) && s != "") { + istringstream iss(s); + string sub1, sub2; + iss >> sub1 >> sub2; + m[sub2] = sub1; + } + while (getline(cin, s)) { + if (m.count(s)) { + printf("%s\n", m[s].c_str()); + } + else printf("eh\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10282/10282-21.cpp b/uva_cpp_clean/10282/10282-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1037c01ea1b9d6682992a17204e0689300ea4b0f --- /dev/null +++ b/uva_cpp_clean/10282/10282-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10282 + Name: Babelfish + Problem: https://onlinejudge.org/external/102/10282.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + map dic; + char line[1000], wf[500], wen[500]; + while (cin.getline(line, 1000) && line[0]) { + sscanf(line, "%s %s", wen, wf); + dic[wf] = wen; + } + + while (cin >> wf) { + auto it = dic.find(wf); + if (it == dic.end()) + cout << "eh\n"; + else + cout << (it->second) << endl; + } +} diff --git a/uva_cpp_clean/10283/10283-9.cpp b/uva_cpp_clean/10283/10283-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9c915a222241b231cf6d055d9ace1f6987ff418 --- /dev/null +++ b/uva_cpp_clean/10283/10283-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +const double PI = acos(-1.0), EPS = 1e-9; + +double R, N, theta, alfa, r, A, B, blue, green; + +int main(){ + while(~scanf("%lf %lf", &R, &N)){ + theta = PI / N; + alfa = PI - 2.0 * theta; + r = R / ( 1.0 + 1.0 / sin(theta) ); + A = r * cos(theta) * ( R - r * ( 1.0 + sin(theta) ) ); + B = r * r * alfa / 2.0 - r * r * sin(2.0 * theta) / 2.0; + blue = N * ( A - B ); + green = PI * R * R - N * PI * r * r - blue; + if( fabs(N - 1.0) < EPS ) r = R, blue = green = 0.0; + printf("%.10lf %.10lf %.10lf\n", r, blue, green); + } + return(0); +} diff --git a/uva_cpp_clean/10284/10284-18.cpp b/uva_cpp_clean/10284/10284-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8f7ae259266f90f0760f179f46dfdca914991d45 --- /dev/null +++ b/uva_cpp_clean/10284/10284-18.cpp @@ -0,0 +1,142 @@ +#include +#include + +using namespace std; + +char board[8][8] = {'*'}; + +void bPawn(int x, int y, bool ab[8][8]){ + if(y + 1 < 8){ + if(x - 1 >= 0) ab[y + 1][x - 1] = true; + if(x + 1 < 8) ab[y + 1][x + 1] = true; + } +} + +void wPawn(int x, int y, bool ab[8][8]){ + if(y - 1 >= 0){ + if(x - 1 >= 0) ab[y - 1][x - 1] = true; + if(x + 1 < 8) ab[y - 1][x + 1] = true; + } +} + +void knight(int x, int y, bool ab[8][8]){ + if(y + 2 < 8){ + if(x + 1 < 8) ab[y + 2][x + 1] = true; + if(x - 1 >= 0) ab[y + 2][x - 1] = true; + } + if(y - 2 >= 0){ + if(x + 1 < 8) ab[y - 2][x + 1] = true; + if(x - 1 >= 0) ab[y - 2][x - 1] = true; + } + if(x + 2 < 8){ + if(y + 1 < 8) ab[y + 1][x + 2] = true; + if(y - 1 >= 0) ab[y - 1][x + 2] = true; + } + if(x - 2 >= 0){ + if(y + 1 < 8) ab[y + 1][x - 2] = true; + if(y - 1 >= 0) ab[y - 1][x - 2] = true; + } +} + +void rock(int x, int y, bool ab[8][8]){ + for(int i = x + 1; i < 8; i++){ + if(board[y][i] != '*') break; + ab[y][i] = true; + } + for(int i = x - 1; i >= 0; i--){ + if(board[y][i] != '*') break; + ab[y][i] = true; + } + for(int i = y + 1; i < 8; i++){ + if(board[i][x] != '*') break; + ab[i][x] = true; + } + for(int i = y - 1; i >= 0; i--){ + if(board[i][x] != '*') break; + ab[i][x] = true; + } +} + +void alfil(int x, int y, bool ab[8][8]){ + for(int i = x + 1, j = y - 1; i < 8 && j >= 0; i++, j--){ + if(board[j][i] != '*') break; + ab[j][i] = true; + + } + for(int i = x + 1, j = y + 1; i < 8 && j < 8; i++, j++){ + if(board[j][i] != '*') break; + ab[j][i] = true; + + } + for(int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--){ + if(board[j][i] != '*') break; + ab[j][i] = true; + + } + for(int i = x - 1, j = y + 1; i >= 0 && j < 8; i--, j++){ + if(board[j][i] != '*') break; + ab[j][i] = true; + + } +} + +void king(int x, int y, bool ab[8][8]){ + if(y + 1 < 8){ + ab[y + 1][x] = true; + if(x + 1 < 8) ab[y + 1][x + 1] = true; + if(x - 1 >= 0) ab[y + 1][x - 1] = true; + } + if(y - 1 >= 0){ + ab[y - 1][x] = true; + if(x + 1 < 8) ab[y - 1][x + 1] = true; + if(x - 1 >= 0) ab[y - 1][x - 1] = true; + } + if(x + 1 < 8) ab[y][x + 1] = true; + if(x - 1 >= 0) ab[y][x - 1] = true; +} + +int main(){ + char input[100]; + while(scanf("%s", input) != EOF){ + int row = 0, col = 0, cont = 0; + bool attackedBoard[8][8] = {false}; + for(int i = 0; input[i] > 0; i++){ + if(input[i] == '/'){ + row++; + for(int j = 0; j < 8; j++) board[row][j] = '*'; + col = 0; + } + else if(input[i] >= '1' && input[i] <= '9'){ + for(int j = 0; j < input[i] - '0'; j++){ + board[row][col] = '*'; + col++; + } + }else if(isalpha(input[i])){ + board[row][col] = input[i]; + col++; + } + } + bool sw = true; + for(int i = 0; i < 8; i++){ + for(int j = 0; j < 8; j++){ + if(board[i][j] == '*') continue; + attackedBoard[i][j] = true; + if(board[i][j] == 'P') wPawn(j, i, attackedBoard); + else if(board[i][j] == 'p') bPawn(j, i, attackedBoard); + else if(board[i][j] == 'n' || board[i][j] == 'N') knight(j, i, attackedBoard); + else if(board[i][j] == 'r' || board[i][j] == 'R') rock(j, i, attackedBoard); + else if(board[i][j] == 'b' || board[i][j] == 'B') alfil(j, i, attackedBoard); + else if(board[i][j] == 'k' || board[i][j] == 'K') king(j, i, attackedBoard); + else if(board[i][j] == 'q' || board[i][j] == 'Q'){ + alfil(j, i, attackedBoard); + rock(j, i, attackedBoard); + } + } + } + for(int i = 0; i < 8; i++){ + for(int j = 0; j < 8; j++) if(!attackedBoard[i][j]) cont++; + } + printf("%d\n", cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10284/10284-2.cpp b/uva_cpp_clean/10284/10284-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e630814f96e2fd25ba68c8927cabbb05594c4a6 --- /dev/null +++ b/uva_cpp_clean/10284/10284-2.cpp @@ -0,0 +1,137 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define FOR(i,a,b) for(long long i = a; i <= b; ++i) +#define FORD(i,a,b) for(long long i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define all(v) ((v).begin()),((v).end()) +typedef long long ll; +typedef vector vi; +typedef vector vll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +const int nr_rows = 8, nr_columns = 8; +char chessboard[nr_rows][nr_columns], fen[nr_rows * (nr_columns + 1) + 1]; + +bool attack(int r, int c){ + if (r < 0 || r >= nr_rows || c < 0 || c >= nr_columns || + isalpha(chessboard[r][c])) + return false; + chessboard[r][c] = '+'; + return true; +} + +void king_attack(int r, int c){ + for (int ri = r - 1; ri <= r + 1; ri++) + for (int ci = c - 1; ci <= c + 1; ci++) + if (ri != r || ci != c) + attack(ri, ci); +} + +void rook_attack(int r, int c){ + int ri, ci; + for (ri = r - 1; ; ri--) + if (!attack(ri, c)) + break; + for (ri = r + 1; ; ri++) + if (!attack(ri, c)) + break; + for (ci = c - 1; ; ci--) + if (!attack(r, ci)) + break; + for (ci = c + 1; ; ci++) + if (!attack(r, ci)) + break; +} + +void bishop_attack(int r, int c){ + int ri, ci; + for (ri = r - 1, ci = c - 1; ; ri--, ci--) + if (!attack(ri, ci)) + break; + for (ri = r - 1, ci = c + 1; ; ri--, ci++) + if (!attack(ri, ci)) + break; + for (ri = r + 1, ci = c - 1; ; ri++, ci--) + if (!attack(ri, ci)) + break; + for (ri = r + 1, ci = c + 1; ; ri++, ci++) + if (!attack(ri, ci)) + break; +} + +void knight_attack(int r, int c){ + attack(r - 2, c - 1); + attack(r - 2, c + 1); + attack(r - 1, c - 2); + attack(r - 1, c + 2); + attack(r + 1, c - 2); + attack(r + 1, c + 2); + attack(r + 2, c - 1); + attack(r + 2, c + 1); +} + +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + ios::sync_with_stdio(false); + cin.tie(0); + + while (scanf("%s", fen) != EOF) { + memset(chessboard, '-', sizeof(chessboard)); + int r = 0, c = 0; + for (const char* p = fen; *p; p++) { + if (*p == '/') { + r++; c = 0; + } + else if (isdigit(*p)) + c += *p - '0'; + else + chessboard[r][c++] = *p; + } + for (r = 0; r < nr_rows; r++) + for (c = 0; c < nr_columns; c++) + switch (chessboard[r][c]) { + case 'k': case 'K': + king_attack(r, c); + break; + case 'q': case 'Q': + rook_attack(r, c); + bishop_attack(r, c); + break; + case 'b': case 'B': + bishop_attack(r, c); + break; + case 'n': case 'N': + knight_attack(r, c); + break; + case 'r': case 'R': + rook_attack(r, c); + break; + case 'p': + attack(r + 1, c - 1); + attack(r + 1, c + 1); + break; + case 'P': + attack(r - 1, c - 1); + attack(r - 1, c + 1); + break; + } + int nr_unoccupied = 0; + for (r = 0; r < nr_rows; r++) + for (c = 0; c < nr_columns; c++) + if (chessboard[r][c] == '-') + nr_unoccupied++; + printf("%d\n", nr_unoccupied); + } + + return 0; +} diff --git a/uva_cpp_clean/10284/10284-9.cpp b/uva_cpp_clean/10284/10284-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70222ca34c45bcb72f7c37ba6903deb707c6aa99 --- /dev/null +++ b/uva_cpp_clean/10284/10284-9.cpp @@ -0,0 +1,92 @@ +#include + +#define SIZE 10 +#define LIM 100 + +using namespace std; + +char s[LIM], table[SIZE][SIZE]; +int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; +int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; +int dxx[] = {2, 1, -1, -2, -2, -1, 1, 2}; +int dyy[] = {1, 2, 2, 1, -1, -2, -2, -1}; + + +void convertFEN(){ + int row = 0, col = 0; + for(int it = 0; s[it]; it++){ + if( isdigit(s[it]) ){ + int ct = s[it] - '0'; + while(ct--) table[row][col++] = '.'; + } + else if( s[it] != '/' ) + table[row][col++] = s[it]; + else + row++, col = 0; + } +} + +inline bool isValid(int X, int Y){ + return ( X >= 0 and Y >= 0 and X < 8 and Y < 8 ); +} + +bool proof(int row, int col, int it, int d){ + if( table[row][col] == 'p' ){ + if( ( it == 5 or it == 7 ) and d == 1 ) return true; + return false; + } + if( table[row][col] == 'P' ){ + if( ( it == 1 or it == 3 ) and d == 1 ) return true; + + } + if( table[row][col] == 'r' or table[row][col] == 'R' ){ + if( it % 2 == 0 ) return true; + return false; + } + if( table[row][col] == 'b' or table[row][col] == 'B' ){ + if( it % 2 == 1 ) return true; + return false; + } + if( table[row][col] == 'q' or table[row][col] == 'Q' ){ + return true; + } + if( table[row][col] == 'k' or table[row][col] == 'K' ){ + if( d == 1 ) return true; + return false; + } + return false; +} + +bool check(char ch, int r, int c){ + bool ok = false; + for(int it = 0; it < 8; it++){ + for(int d = 1; d < 8; d++){ + int R = r + d * dy[it], C = c + d * dx[it]; + if( isValid(R, C) and table[R][C] != '.' ) + ok |= proof(R, C, it, d), d = 8; + } + } + for(int it = 0; it < 8; it++){ + int R = r + dyy[it], C = c + dxx[it]; + if( isValid(R, C) and ( table[R][C] == 'N' or table[R][C] == 'n' ) ) + ok = true; + } + return ok; +} + +int solution(){ + int ans = 0; + for(int row = 0; row < 8; row++) + for(int col = 0; col < 8; col++) + if( table[row][col] == '.' ) + ans += ( check(table[row][col], row, col) == false ); + return ans; +} + +int main(){ + while(~scanf("%s", s)){ + convertFEN(); + printf("%d\n", solution()); + } + return(0); +} diff --git a/uva_cpp_clean/10285/10285-19.cpp b/uva_cpp_clean/10285/10285-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba11be8c2e3e266e40b40d27bd05801a817d38ae --- /dev/null +++ b/uva_cpp_clean/10285/10285-19.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +int H[101][101]; +int dp[101][101]; +int R, C; + +inline bool limits(int i, int j) { + return i >= 0 && i < R && j >= 0 && j < C; +} + +int proc(int i, int j) { + if (dp[i][j] == -1) { + dp[i][j] = 0; + if (limits(i-1, j) && H[i-1][j] < H[i][j]) dp[i][j] = max(dp[i][j], proc(i-1, j) + 1); + if (limits(i+1, j) && H[i+1][j] < H[i][j]) dp[i][j] = max(dp[i][j], proc(i+1, j) + 1); + if (limits(i, j-1) && H[i][j-1] < H[i][j]) dp[i][j] = max(dp[i][j], proc(i, j-1) + 1); + if (limits(i, j+1) && H[i][j+1] < H[i][j]) dp[i][j] = max(dp[i][j], proc(i, j+1) + 1); + } + return dp[i][j]; +} + +int main() { + int N; + cin >> N; + while (N--) { + string s; + cin >> s >> R >> C; + for (int i = 0; i < R; i++) { + for (int j = 0; j < C; j++) { + cin >> H[i][j]; + dp[i][j] = -1; + } + } + int sol = -1; + for (int i = 0; i < R; i++) + for (int j = 0; j < C; j++) + sol = max(sol, proc(i, j)); + printf("%s: %d\n", s.c_str(), sol + 1); + } + return 0; +} diff --git a/uva_cpp_clean/10285/10285-21.cpp b/uva_cpp_clean/10285/10285-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87eca1d0f271e4641552ebef8ad22872c36cbfec --- /dev/null +++ b/uva_cpp_clean/10285/10285-21.cpp @@ -0,0 +1,61 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10285 + Name: Longest Run on a Snowboard + Problem: https://onlinejudge.org/external/102/10285.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int mat[102][102],c,r; + +int memo[102][102]; +int best(int y,int x){ + if(x<0 || x>=c || y<0 || y>=r)return 0; //out of bound + if(memo[y][x]!=-1)return memo[y][x]; + + int t1,t2,t3,t4; + t1=t2=t3=t4=0; + if(mat[y][x]>mat[y][x+1]) + t1=best(y,x+1); //go right + + if(mat[y][x]>mat[y][x-1]) + t2=best(y,x-1); //go left + + if(mat[y][x]>mat[y+1][x]) + t3=best(y+1,x); //go up + + if(mat[y][x]>mat[y-1][x]) + t4=best(y-1,x); //go down + + return memo[y][x]=max(max(max(t1,t2),t3),t4)+1; +} + +int main(){ + char name[1000]; + int T,i,j,m; + + cin>>T; + while(T--){ + scanf("%s%d%d",name,&r,&c); + for(i=0;i>mat[i][j]; + + memset(memo,-1,sizeof(memo)); + int m=0; + for(i=0;i +#include +using namespace std; + +int main() { + double l, c = sin(108*(acos(0)/90))/sin(63*(acos(0)/90)); + while (cin >> l) printf("%.10lf\n", l*c); + return 0; +} diff --git a/uva_cpp_clean/10286/10286-20.cpp b/uva_cpp_clean/10286/10286-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edf6be17ea75bf66cf34f4dbebc4eb636fd96830 --- /dev/null +++ b/uva_cpp_clean/10286/10286-20.cpp @@ -0,0 +1,16 @@ +#include +#include + +const double PI = acos(-1); + +int main() +{ + double F; + + while( scanf("%lf", &F)==1 ) + { + double convex = sin( 72*PI/180 ) / sin( 63*PI/180 ); + printf("%.10lf\n", F*convex); + } + return 0; +} diff --git a/uva_cpp_clean/10286/10286-21.cpp b/uva_cpp_clean/10286/10286-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f57113d023dc0482d9a7d28363b8b8f4e7dc4bc --- /dev/null +++ b/uva_cpp_clean/10286/10286-21.cpp @@ -0,0 +1,20 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10286 + Name: Trouble with a Pentagon + Problem: https://onlinejudge.org/external/102/10286.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; +int main(){ + double n; + while (cin>>n) + printf("%.10lf\n", 1.067395681711182*n); +} diff --git a/uva_cpp_clean/10288/10288-19.cpp b/uva_cpp_clean/10288/10288-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aadef0187bb7b621186bef78a8dcbc09323f91d9 --- /dev/null +++ b/uva_cpp_clean/10288/10288-19.cpp @@ -0,0 +1,58 @@ +#include +#include +using namespace std; + +typedef long long ll; +typedef pair ii; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +ii sum (ii & a, ii & b) { + ll n = a.first*b.second + b.first*a.second; + ll d = a.second*b.second; + ll g = gcd(n, d); + return ii(n / g, d / g); +} + +ii proc(int n) { + ii res = ii(0, 1), auxi = ii(1, 1); + for (int i = 1; i <= n; i++) { + auxi.second = i; + res = sum (res, auxi); + } + res.first *= n; + ll g = gcd(res.first, res.second); + res.first /= g, res.second /= g; + return res; +} + +int main() { + int n; + while (cin >> n) { + ii res = proc(n); + if (res.second == 1) printf("%lld\n", res.first); + else { + ll n = res.first / res.second, m = res.first % res.second, d = res.second; + int ln = floor(log10(n)), lm = floor(log10(m)), ld = floor(log10(d)); + for (int i = 0; i <= ln + 1; i++) printf(" "); + printf("%lld\n", m); + printf("%lld ", n); + for (int i = 0; i <= max(lm, ld); i++) printf("-"); + printf("\n"); + for (int i = 0; i <= ln + 1; i++) printf(" "); + printf("%lld\n", d); + } + } + return 0; +} diff --git a/uva_cpp_clean/10295/10295-14.cpp b/uva_cpp_clean/10295/10295-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8e7ebcb558abd4346a14cc9ab9ec25fc723ad19 --- /dev/null +++ b/uva_cpp_clean/10295/10295-14.cpp @@ -0,0 +1,165 @@ +/*************************************************** + * Problem Name : 10295 Hay Points.cpp + * Problem Link : https://onlinejudge.org/external/102/10295.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-10-22 + * Problem Type : SET (map) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +vector purse (string str) { + stringstream ss; + ss << str; + vectorvec; + string num; + + while (ss >> num) { + vec.pb (num); + } + + return vec; +} +int main () { + //~ __FastIO; + //~ cout << setprecision (10) << fixed; + int m, n; + + while (cin >> m >> n) { + mapmp; + + for (int i = 0; i < m; i++) { + string str; + int val; + cin >> str >> val; + mp[str] = val; + } + + cin.ignore(); + + while (n--) { + string str; + ll res = 0; + + while (getline (cin, str) ) { + if (str[0] == '.') break; + + vector tmp = purse (str); + + for (string v : tmp) res += mp[v]; + } + + cout << res << "\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/10295/10295-20.cpp b/uva_cpp_clean/10295/10295-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73aabafc5dd8df7bd4509187cb97874194df73bf --- /dev/null +++ b/uva_cpp_clean/10295/10295-20.cpp @@ -0,0 +1,21 @@ +#include +#include + +int m, n, value[1005], sum; +char dictionary[1005][20], jobDes[20]; + +int main() +{ + scanf("%d %d", &m, &n); + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10295 + Name: Hay Points + Problem: https://onlinejudge.org/external/102/10295.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main() { + map vals; + char word[100]; + + int n, c; + cin>>n>>c; + for (int i=0; i> word; + cin >> vals[word]; + } + + for (int i=0; i>word && word[0]!='.') + sum += vals[word]; + + cout << sum << endl; + } +} diff --git a/uva_cpp_clean/10295/10295-5.cpp b/uva_cpp_clean/10295/10295-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2d43a9202c0943d8045dc18f2950a6f85aa0b278 --- /dev/null +++ b/uva_cpp_clean/10295/10295-5.cpp @@ -0,0 +1,80 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main() +{ + int a,b; + cin>>a>>b; + mapmp; + mapmp1; + string s; + + while(a--) + { + int value; + cin>>s; + cin>>value; + mp[s]=value; + mp1[s]++; + } + while(b--) + { + int result=0; + while(cin>>s && s.compare(".")!=0) + { + mp1[s]++; + if(mp1[s]>1) + { + result+=mp[s]; + } + } + cout< +#include +using namespace std; + +double PI = 2*acos(0); + +int main() { + double D, V; + while (cin >> D >> V && !(D == 0 && V == 0)) { + double d = cbrt(D*D*D - (6*V/PI)); + printf("%.3lf\n", d); + } + return 0; +} diff --git a/uva_cpp_clean/10297/10297-21.cpp b/uva_cpp_clean/10297/10297-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d6152452f493ad764c1de826e493b55c0ef0111 --- /dev/null +++ b/uva_cpp_clean/10297/10297-21.cpp @@ -0,0 +1,24 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10297 + Name: Beavergnaw + Problem: https://onlinejudge.org/external/102/10297.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + double pi = 3.1415926535897932384, d, v; + while (cin >> d >> v && (d||v)) + cout << fixed << setprecision(3) + << pow((pi*d*d*d - 6*v)/pi, 1.0/3) << endl; +} diff --git a/uva_cpp_clean/10298/10298-19.cpp b/uva_cpp_clean/10298/10298-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8cd750c9e24027f432fdf9cc4ff960fa1a26d18 --- /dev/null +++ b/uva_cpp_clean/10298/10298-19.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +int main() { + string line; + while (getline(cin, line) && line != ".") { + int pos = 0, comp1 = 0, comp2 = 0, i = 1, n = 1; + bool found = false; + while (!found) { + for (; i < line.length() && !found; i++) { + if (line.length() % i != 0) continue; + else n = line.length() / i; + comp2 = i; + for (; i < line.length() && line[i] == line[comp1]; i++, comp1++) { + if (comp1 == comp2 - 1) { + found = true; + break; + } + } + if (!found) i = comp2; + comp1 = 0; + } + for (; i < line.length() && line[i] == line[comp1]; i++, comp1++); + if (i != line.length()) found = false, comp1 = 0, i = comp2 + 1; + else break; + } + if (!found) cout << 1 << endl; + else cout << n << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10298/10298-21.cpp b/uva_cpp_clean/10298/10298-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e52cb98be02bd585fa575bac4ba4be1fe39eb269 --- /dev/null +++ b/uva_cpp_clean/10298/10298-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10298 + Name: Power Strings + Problem: https://onlinejudge.org/external/102/10298.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char line[1000002]; + for (;;) { + line[0] = getchar(); + if (line[0] == '.') break; + + int i, n = 1; + for (i=1; (line[i]=getchar()) != '\n'; i++) + while (line[i%n] != line[i]) + n++; + if (i % n) n = i; + + cout << i/n << endl; + } +} diff --git a/uva_cpp_clean/10298/10298-5.cpp b/uva_cpp_clean/10298/10298-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7eef55fb7e016ac34fe5b38e7c6f9aadb9352836 --- /dev/null +++ b/uva_cpp_clean/10298/10298-5.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include +char s[10000000]; +using namespace std; +int main() +{ + int a,i,b; + while(gets(s)) + { + if(s[0]=='.'&&!s[1]) + break; + a=strlen(s); + b=1; + for(i=1;s[i];i++) + while(s[i]!=s[i%b]) + b++; + if(i%b==0) + cout< + +using namespace std; + +vector Zfunction (string s) { + int n = (int) s.size(); + vector z(n); + for (int i = 1, l = 0, r = 0; i < n; i++) { + if (i <= r) z[i] = min(r - i + 1, z[i - l]); + while (i + z[i] < n and s[z[i]] == s[i + z[i]]) z[i]++; + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + } + return z; +} + +int main () { + string s; + while (cin >> s, s[0] != '.') { + int n = s.size(); + vector z = Zfunction(s); + int p = -1; + for (int i = 1; i < n and p == -1; i++) + if (n % i == 0 and z[i] + i == n) p = i; + if (p == -1) p = n; + cout << n / p << endl; + } + return (0); +} diff --git a/uva_cpp_clean/10299/10299-19.cpp b/uva_cpp_clean/10299/10299-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ebc53f306d504fcc2e632bc9758578a5553ac54 --- /dev/null +++ b/uva_cpp_clean/10299/10299-19.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +void sieve(ll upperbound = 40000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +ll EulerPhi(ll n) { + ll idx=0, p=primes[0], ans=n; + while (n!=1 && (p*p<=n)) { + if (n%p==0) ans-=ans/p; + while (n%p==0) n/=p; + p=primes[++idx]; + } + if (n!=1) ans-=ans/n; + return ans; +} + +int main() { + sieve(); + ll n; + while (cin >> n && n) { + if (n == 1) printf("0\n"); + else printf("%lld\n", EulerPhi(n)); + } + return 0; +} diff --git a/uva_cpp_clean/10299/10299-21.cpp b/uva_cpp_clean/10299/10299-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba5c1cbcfa86b296c63df48012c2a6c00d5469ce --- /dev/null +++ b/uva_cpp_clean/10299/10299-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10299 + Name: Relatives + Problem: https://onlinejudge.org/external/102/10299.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while (cin >> n && n) { + int r = n>1 ? n : 0; + for (int i=2; i*i<=n; ++i) + if (n%i == 0) { + while (n%i == 0) n/=i; + r = (r / i) * (i-1); + } + if (n > 1) + r = (r / n) * (n-1); + cout << r << endl; + } +} diff --git a/uva_cpp_clean/103/103-21.cpp b/uva_cpp_clean/103/103-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6f22d0a566cc64965dfda71a95d5cec1bd27d6f --- /dev/null +++ b/uva_cpp_clean/103/103-21.cpp @@ -0,0 +1,105 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 103 + Name: Stacking Boxes + Problem: https://onlinejudge.org/external/1/103.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int n, d; +int boxes[32][15]; +bool mp[32][32]; +bool lse(int i, int j) { + for (int k=0; k= boxes[j][k]) + return false; + + return true; +} + +// ------------------------------------------------- + +int order[32], + lpre, pre[32], + lpost, post[32]; + +void dfs(int v) { + if (pre[v]>=0) return; + pre[v] = lpre++; + + for (int i=0; i>n>>d) { + for (int i=0; i>boxes[i][j]; + + sort(boxes[i], boxes[i] + d); + } + + memset(mp, 0, sizeof(mp)); + for (int i=0; i=0; i--) { + int v = order[i]; + befor[v] = -1; + + for (int j=0; j dists[v]) { + dists[v] = dists[j]+1; + befor[v] = j; + } + + if (dists[v] > dists[mxs]) + mxs = v; + } + + cout<< dists[mxs]+1 < +using namespace std; + +int main () { + int n, f, x, y, z, sum; + cin >> n; + for (int i = 0; i < n; i++) { + sum = 0; + cin >> f; + for (int j = 0; j < f; j++) { + cin >> x >> y >> z; + y = x * z; + sum += y; + } + cout << sum << endl; + } +} diff --git a/uva_cpp_clean/10300/10300-18.cpp b/uva_cpp_clean/10300/10300-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..71bd199476b94a33f05dc07f7d56e20921a8bfcb --- /dev/null +++ b/uva_cpp_clean/10300/10300-18.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main(){ + int n, f, size, animals, ef; + long long sum; + cin >> n; + while(n-- > 0){ + cin >> f; + sum = 0; + while(f-- > 0){ + cin >> size >> animals >> ef; + sum += size*ef; + } + cout << sum << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10300/10300-19.cpp b/uva_cpp_clean/10300/10300-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..563fd6969d95b05c171954695b0ca498c3aa62ef --- /dev/null +++ b/uva_cpp_clean/10300/10300-19.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main() { + int n; + scanf("%d", &n); + while (n--) { + int f; + scanf("%d", &f); + int budget = 0, size, animals, EFvalue; + while (f--) { + scanf("%d %d %d", &size, &animals, &EFvalue); + budget += size*EFvalue; + } + printf("%d\n", budget); + } + return 0; +} diff --git a/uva_cpp_clean/10300/10300-2.cpp b/uva_cpp_clean/10300/10300-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6c9b6a2380b617f46158d19df772425b7711522 --- /dev/null +++ b/uva_cpp_clean/10300/10300-2.cpp @@ -0,0 +1,47 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; cin >> tc; + while (tc--) { + int n; cin >> n; + int ans = 0; + while (n--) { + int a, b, c; cin >> a >> b >> c; + ans += (a*c); + } + cout << ans << el; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10300/10300-20.cpp b/uva_cpp_clean/10300/10300-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c348c41e38ae2e874ab65ccd4a63886613a3604b --- /dev/null +++ b/uva_cpp_clean/10300/10300-20.cpp @@ -0,0 +1,27 @@ +#include +#include + +using namespace std; + +int main() +{ + int tc; + + cin >> tc; + while( tc-- ) + { + int f; + cin >> f; + + int sum = 0; + while( f-- ) + { + int a, b, c; + cin >> a >> b >> c; + sum += (a*c); + } + + cout << sum << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10300/10300-21.cpp b/uva_cpp_clean/10300/10300-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c0e412f65a93f0366b43a4b36c528ddbd058f8f --- /dev/null +++ b/uva_cpp_clean/10300/10300-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10300 + Name: Ecological Premium + Problem: https://onlinejudge.org/external/103/10300.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int t; + cin>>t; + while(t--) { + int n; + cin>>n; + long long int sum=0; + while(n--) { + int sz, an, fr; + cin>>sz>>an>>fr; + sum += sz*fr; + } + cout< + +int main() +{ + int cases, farmers; + long long farmsz, animals, friendliness, sum; + + scanf("%d",&cases); + while ( cases-- ) + { + sum = 0; + scanf("%d",&farmers); + while ( farmers-- ) + { + scanf("%lld %lld %lld", &farmsz, &animals, &friendliness); + sum += farmsz * friendliness; + } + printf("%lld\n", sum); + } +} + diff --git a/uva_cpp_clean/10300/10300-5.cpp b/uva_cpp_clean/10300/10300-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..474a9fe3caa469e85d3d8fafe1e03cce397f9de5 --- /dev/null +++ b/uva_cpp_clean/10300/10300-5.cpp @@ -0,0 +1,20 @@ +#include +int main() +{ +long long a,t,n,b,c,i,j,s; +while(scanf("%lld",&t)==1) + { + for(i=0;i + +int main(){ + int N, n, a, b, c, i; + float sum; + scanf("%d", &N); + while(N--){ + sum = 0; + scanf("%d", &n); + for(i = 0; i < n; i++){ + scanf("%d %d %d", &a, &b, &c); + sum += (float)a / b * c * b; + } + printf("%d\n", (int)sum); + } + return(0); +} diff --git a/uva_cpp_clean/10300/main.cpp b/uva_cpp_clean/10300/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6088c0dcdc48692f8f273470276d4de10db6900 --- /dev/null +++ b/uva_cpp_clean/10300/main.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main(){ + unsigned long n, farmers, area, animals, eco_friendly; + cin >> n; + for(int i = 0; i < n; i++){ + unsigned long res = 0; + cin >> farmers; + for(int c = 0; c < farmers; c++){ + cin >> area >> animals >> eco_friendly; + res += area*eco_friendly; + } + cout << res << endl; + } +return 0; +} diff --git a/uva_cpp_clean/10301/10301-6.cc b/uva_cpp_clean/10301/10301-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..9555ce0b7b3a75256ab2104f8b2f1918df503886 --- /dev/null +++ b/uva_cpp_clean/10301/10301-6.cc @@ -0,0 +1,52 @@ +// https://uva.onlinejudge.org/external/103/10301.pdf +#include +using namespace std; +using pt=complex; +using ptd=tuple; +using vptd=vector; +using vi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + int n,u,v; + cin>>n; + if(n==-1)break; + vi s=vi(n, 1),p=vi(n); + for(int i=0;ifind=[&](int i){ + if(i==p[i])return i; + return p[i]=find(p[i]); + }; + functionunite=[&](int i, int j){ + i=find(i); + j=find(j); + if(i==j)return; + if(s[i]>x>>y>>r; + a[i]={{x,y},r}; + } + for(int i=0;ir) + unite(i,j); + } + int m=0; + for(int i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10302 + Name: Summation of Polynomials + Problem: https://onlinejudge.org/external/103/10302.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +long long int DP[50001] = {1}; + +long long p(long long int i) { + return i*i*i; +} + +int main() { + for (int i=0; i<50001; i++) + DP[i] = DP[i-1] + p(i); + + int n; + while(cin>>n) + cout< +#include +int main() +{ +long double n,sum; +while(scanf("%Lf",&n)==1) +{ +sum=(n*n*(n+1)*(n+1))/4; +printf("%.0Lf\n",sum); +} +return 0; +} diff --git a/uva_cpp_clean/10302/10302-9.cpp b/uva_cpp_clean/10302/10302-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4db094c3d047527be040c21bfccf372ebebed5f0 --- /dev/null +++ b/uva_cpp_clean/10302/10302-9.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int n; + +inline long long pow2(long long p){ + return p * p; +} + +int main(){ + while(~scanf("%d", &n)){ + printf("%lld\n", pow2( 1LL * n * ( n + 1 ) / 2 )); + } + return(0); +} diff --git a/uva_cpp_clean/10303/10303-9.cpp b/uva_cpp_clean/10303/10303-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5004804cdcfaaf8206c64068f7a5bd20fa3ce98b --- /dev/null +++ b/uva_cpp_clean/10303/10303-9.cpp @@ -0,0 +1,14 @@ +from sys import stdin,stdout + +def main(): + catalan = [1] + for it in range(1,1005): + catalan.append(catalan[it-1]*((it<<2)-2)//(it+1)) + while True: + idx = stdin.readline() + if len(idx)==0: + break + stdout.write(str(catalan[int(idx)])+"\n") + +if __name__ == "__main__": + main() diff --git a/uva_cpp_clean/10304/10304-21.cpp b/uva_cpp_clean/10304/10304-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b538f1208b159cafd02f70708b09b0724188843 --- /dev/null +++ b/uva_cpp_clean/10304/10304-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10304 + Name: Optimal Binary Search Tree + Problem: https://onlinejudge.org/external/103/10304.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + +// O(n^2) +int F[255], C[255], DP[255][255], P[255][255], n; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=0; i<255; ++i) + P[i][i] = i; + + while (cin >> n) { + for (int i=1; i<=n; ++i) { + cin >> F[i]; + C[i] = C[i-1] + F[i]; + } + + for (int d=1; d +using namespace std; +typedef long long ll; +void fast() { + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); +} + +vector adj[(int)2e2]; +bool vis[(int)2e2]; +bool root[(int)2e2]; + +void dfs(int node) { + cout << node << " "; + vis[node] = 1; + + for (auto ch: adj[node]) { + if (!vis[ch]) + dfs(ch); + } +} + +int main() { + fast(); + + int n,m,a,b; + + while (cin>>n>>m) { + if(n==0 && m==0) break; + + memset(adj,0,sizeof adj); + memset(vis,0,sizeof vis); + memset(root,1,sizeof root); + + + for(int i=0;i>a>>b; + adj[a].push_back(b); + root[b]=0; + } + + for(int i=1;i<=n;i++) { + if(root[i]) dfs(i); + } + cout<<"\n"; + + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/10305/10305-11.cpp b/uva_cpp_clean/10305/10305-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..908388dd8fec42ceccde029f1b0fd8d18753fb7e --- /dev/null +++ b/uva_cpp_clean/10305/10305-11.cpp @@ -0,0 +1,91 @@ +#include +/* +Problem: 10305 - Ordering Tasks +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1246 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int N, M; +int graph[1001][1001]; +int visited[1001]; +int topsort[1001], idx; + +void DFS(int node){ + if (visited[node]) return; + + visited[node] = true; + + for (int child = 0; child < N; ++child){ + if (graph[node][child] && !visited[child]) + + DFS(child); + } + + topsort[--idx] = node+1; +} + +int main () { + fastio; + + while (cin >> N >> M && N){ + + memset(visited, 0, sizeof(visited)); + memset(topsort, 0, sizeof(topsort)); + memset(graph, 0, sizeof(graph[0][0]) * N * N); + + idx = N; + + int i, j; + while (M--){ + cin >> i >> j; + graph[i-1][j-1] = 1; + } + + for (int node = 0; node < N; node++) + if (!visited[node]) DFS(node); + + for (int i = 0; i < N; ++i) + cout << topsort[i] << ' '; + + cout << '\n'; + } + + return 0; +} +/* +Sample input:- +----------------- +5 4 +1 2 +2 3 +1 3 +1 5 +0 0 + +Sample output:- +----------------- +1 4 2 5 3 + +4 1 5 2 3 + + +Resources:- +------------- + + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10305/10305-13.cpp b/uva_cpp_clean/10305/10305-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..601ac2fcebe0972f2cb4ee5acaf7f71ff153e424 --- /dev/null +++ b/uva_cpp_clean/10305/10305-13.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int n, m; +stack topo; +vector visited; +vector < vector > graph; + +void dfs(int start) +{ + visited[start] = true; + + for (int i = 0; i < (int)graph[start].size(); i++) + { + int child = graph[start][i]; + + if (!visited[child]) + { + dfs(child); + } + } + + topo.push(start + 1); +} + +int main() +{ + while (cin >> n >> m , n != 0 || m != 0) + { + graph.clear(); + + visited.clear(); + + graph.resize(n); + + visited.resize(n, 0); + + for (int i = 0; i < m; i++) + { + int x, y; + + cin >> x >> y; + + x--; + + y--; + + graph[x].push_back(y); + } + + for (int i = 0; i < n; i++) + { + if (!visited[i]) dfs(i); + } + + while (!topo.empty()) + { + cout << topo.top() << " "; + + topo.pop(); + } + + cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10305/10305-14.cpp b/uva_cpp_clean/10305/10305-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..835b71d9f7382bc0d453aedf1555959ce56ddcb8 --- /dev/null +++ b/uva_cpp_clean/10305/10305-14.cpp @@ -0,0 +1,84 @@ +/*************************************************** + * Problem name : 10305 - Ordering Tasks.cpp + * OJ : Uva + * Result : AC + * Date : 07-06-17 + * Problem Type : Topological Sort + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 105 +using namespace std; +typedef long long ll; +vectoradj[MAX]; +vectorV; +int color[MAX]; +void dfs(int u) { + color[u] = 1; + int sz = adj[u].size(); + for (int i = 0; i < sz; i++) { + int v = adj[u][i]; + if (color[v] == 0) { + dfs(v); + } + } + V.push_back(u); + return ; +} +void mem() { + V.clear(); + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + color[i] = 0; + } +} +int main () { + int m, n; + while (scanf("%d %d", &n, &m) == 2) { + if(n == 0 && m == 0) break; + if (m == 0) { + for (int i = n; i >= 1; i--) { + if (i == 1) { + printf("%d\n",i); + } else { + printf("%d ", i); + } + } + } else { + for (int i = 1; i <= m; i++) { + int u, v; + scanf("%d %d", &u, &v); + adj[u].push_back(v); + } + for (int i = 1; i <= n; i++) { + if (color[i] == 0) { + dfs(i); + } + } + for (int i = n - 1; i >= 0; i--) { + if (i == 0) { + printf("%d\n", V[i]); + } else { + printf("%d ", V[i]); + } + } + } + mem(); + } + return 0; +} + + diff --git a/uva_cpp_clean/10305/10305-17.cpp b/uva_cpp_clean/10305/10305-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c742bdcfe38fa2c95344b15bf3beb0e427399eef --- /dev/null +++ b/uva_cpp_clean/10305/10305-17.cpp @@ -0,0 +1,40 @@ +/** + * AOAPC II Example 6-15 Ordering Tasks + */ +#include +using namespace std; +vector> graph; +vector vis; +vector ans; +void topo(int i) +{ + vis[i] = true; + for (int j : graph[i]) + if (!vis[j]) + topo(j); + ans.push_back(i); // in reverse order +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n, m; + while ((cin >> n >> m) && n) + { + graph = vector>(n + 1); + while (m--) + { + int a, b; + cin >> a >> b; + graph[a].push_back(b); // may be duplicates + } + vis = vector(n + 1, false); + ans.clear(); + for (int i = 1; i <= n; ++i) + if (!vis[i]) + topo(i); + cout << ans.back(); + for_each(ans.rbegin() + 1, ans.rend(), [](int i){ cout << " " << i; }); + cout << "\n"; + } +} diff --git a/uva_cpp_clean/10305/10305-18.cpp b/uva_cpp_clean/10305/10305-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e1f69701a3e4b1487db4e4889ca71c1208d6a002 --- /dev/null +++ b/uva_cpp_clean/10305/10305-18.cpp @@ -0,0 +1,57 @@ +#include +#include +#include + +using namespace std; //Kahn's algorithm for topological sort + +int matrizA[100][100]; +int inDegree[100]; +vector sortedG; + +void topologicalSort(int n){ + queue q; + for(int i = 1; i <= n; i++){ + if(inDegree[i] == 0) q.push(i); //Push into the queue all vertex with zero in-edges + } + while(!q.empty()){ + int vertex = q.front(); + sortedG.push_back(vertex); //Push all vertex with zero in-egdes to the sorted vector + q.pop(); + for(int i = 1; i <= n; i++){ + inDegree[matrizA[vertex][i]] = inDegree[matrizA[vertex][i]] - 1; //Decrease by 1 adjacent inDegree[] edges + if(inDegree[matrizA[vertex][i]] == 0){ //If equals zero push it into the queue + q.push(i); + } + } + } +} + +void reset(int n){ + for(int i = 1; i<= n; i++){ + inDegree[i] = 0; + for(int j = 1; j <= n; j++){ + matrizA[i][j] = 0; + } + } +} + +int main(){ + int n, m, in, out; + while(true){ + cin >> n >> m; + if(n == 0 && m == 0) break; + sortedG.clear(); + for(int i = 1; i <= m; i++){ + cin >> out >> in; + matrizA[out][in] = in; + inDegree[in] = inDegree[in] + 1; //Calculate in-edges of out-vertex + } + topologicalSort(n); + for(int i = 0; i < n; i++){ + cout << sortedG[i] << " "; + } + cout< +#include +using namespace std; + +typedef vector vi; +vector AdjList; +bool visited[100]; +bool first; + +void toposort(int j) { + for (int i = 0; i < AdjList[j].size(); i++) { + if (!visited[AdjList[j][i]]) { + visited[AdjList[j][i]] = true; + toposort(AdjList[j][i]); + } + } + if (!first) first = true; + else printf(" "); + printf("%d", j+1); +} + +int main() { + int n, m; + while (scanf("%d %d", &n, &m) && !(n == 0 && m == 0)) { + for (int i = 0; i < n; i++) visited[i] = false; + AdjList.assign(n, vi()); + while (m--) { + int i, j; + scanf("%d %d", &i, &j); + AdjList[j-1].push_back(i-1); + } + first = false; + for (int i = 0; i < n; i++) { + if (!visited[i]) { + visited[i] = true; + toposort(i); + } + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10305/10305-20.cpp b/uva_cpp_clean/10305/10305-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e60b8ef4cb26fb5f83985fe1072e8a426e96260 --- /dev/null +++ b/uva_cpp_clean/10305/10305-20.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +const int MAXN = 100; +vector < int > graph[MAXN+2]; +bool color[MAXN+2]; +stack < int > s; + +void dfs( int node ) { + if( color[node] ) { + return; + } + color[node] = true; + for( int i = 0 ; i < graph[node].size() ; ++i ) { + dfs( graph[node][i] ); + } + s.push( node ); +} + +int main( int argc, char ** argv ) { + int n, m; + while( scanf( "%d %d", &n, &m ) == 2 && (n || m) ) { + for( int i = 1 ; i <= m ; ++i ) { + int x, y; + scanf( "%d %d", &x, &y ); + graph[x].push_back( y ); + } + + memset( color, false, sizeof( color ) ); + + for( int i = 1 ; i <= n ; ++i ) { + dfs( i ); + } + + bool flag = false; + while( !s.empty() ) { + if( flag ) printf( " " ); + flag = true; + printf( "%d", s.top() ); + s.pop(); + } + puts( "" ); + + for( int i = 0 ; i <= MAXN ; ++i ) { + graph[i].clear(); + } + } + return 0; +} diff --git a/uva_cpp_clean/10305/10305-21.cpp b/uva_cpp_clean/10305/10305-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09911584852d938fa73fb34c7cc3f5838a33af37 --- /dev/null +++ b/uva_cpp_clean/10305/10305-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10305 + Name: Ordering Tasks + Problem: https://onlinejudge.org/external/103/10305.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +vector adj[101], order; +int pre[101], last; + +void traverse(int v) { + if (pre[v]) return; + pre[v] = ++last; + for (int i = adj[v].size() - 1; i >= 0; i--) + traverse(adj[v][i]); + + order.push_back(v); +} + + +int main() { + + int n, m, x, y; + while ((cin >> n >> m) && n) { + for (int i = 1; i <= n; i++) { + adj[i].clear(); + pre[i] = 0; + } + + for (int i = 0; i < m; i++) { + cin >> x >> y; + adj[y].push_back(x); + } + + + last = 0; + order.clear(); + for (int i = 1; i <= n; i++) + traverse(i); + + for (int i = 0; i < order.size() - 1; i++) + cout << order[i] << " "; + cout << order[order.size() - 1] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10305/10305-5.cpp b/uva_cpp_clean/10305/10305-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e3381949d0705e71b9255e0fda1a527a37d045dc --- /dev/null +++ b/uva_cpp_clean/10305/10305-5.cpp @@ -0,0 +1,188 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// +int p,n,col[110],f[110],g[110][110]; + +void dfs(int u) +{ + col[u]=1; + for1(i,n) + { + if(g[u][i] && !col[i]) + { + dfs(i); + } + } + f[--p]=u; +} +int main() +{ + int t; + while(sii(n,t)) + { + if(!n && !t) break; + p=n; + while(t--) + { + int u,v; + sii(u,v); + g[u][v]=1; + } + +// dbgmat(g,6,6); + + for1(i,n) + { + if(!col[i]) + dfs(i); + } + + for0(i,n-1) + { + PN(f[i]); + } + P(f[n-1]); + cover(col,0); + cover(g,0); + } + + return 0; +} + diff --git a/uva_cpp_clean/10305/10305-6.cc b/uva_cpp_clean/10305/10305-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..f9aac956121bd3fbf4aa5537e0fc53f73dce47ed --- /dev/null +++ b/uva_cpp_clean/10305/10305-6.cc @@ -0,0 +1,32 @@ +// https://uva.onlinejudge.org/external/103/10305.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + for(;;){ + int n,m,u,v; + cin>>n>>m; + if(!n)break; + vvi g(n); + for(int i=0;i>u>>v; + u--;v--; + g[u].push_back(v); + } + vi s(n),p; + functiondfs=[&](int u){ + s[u]=1; + for(int v:g[u]) + if(!s[v]) + dfs(v); + p.push_back(u); + }; + for(int i=0;i + +using namespace std; + +const int MAX_V = 110; + +int V, E, a, b; +bool vis[MAX_V]; +vector AdjList[MAX_V], ts; + +void dfs(int u){ + vis[u] = true; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not vis[v] ) + dfs(v); + } + ts.push_back(u); +} + +void TopologicalSort(){ + ts.clear(); + memset(vis, false, sizeof vis); + for(int u = 1; u <= V; u++) + if( not vis[u] ) + dfs(u); + reverse(ts.begin(), ts.end()); +} + +int main(){ + while(scanf("%d %d", &V, &E), V | E){ + for(int i = 0; i < E; i++){ + scanf("%d %d", &a, &b); + AdjList[a].push_back(b); + } + TopologicalSort(); + for(int i = 0, sz = ts.size(); i < sz; i++) + printf( i == sz - 1 ? "%d\n" : "%d ", ts[i] ); + for(int i = 1; i <= V; i++) AdjList[i].clear(); + } + return(0); +} diff --git a/uva_cpp_clean/10306/10306-13.cpp b/uva_cpp_clean/10306/10306-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0b11e245982ba95ff9dd1046acb19766cf59911 --- /dev/null +++ b/uva_cpp_clean/10306/10306-13.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 310 +#define INF 10000007 + +struct value +{ + int x; + int y; +}; + +int main() +{ + ios::sync_with_stdio(0); + + int t, s, m; + + cin >> t; + + while (t--) + { + cin >> m >> s; + + int dp[MAX][MAX]; + + value coinValue[45]; + + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + dp[i][j] = INF; + } + } + + for (int i = 0; i < m; i++) + { + int X, Y; + + cin >> X >> Y; + + coinValue[i].x = X; + coinValue[i].y = Y; + } + + dp[0][0] = 0; + + for (int i = 0; i < m; i++) + { + for (int x = 0; x < MAX; x++) + { + for (int y = 0; y < MAX; y++) + { + if ((x - coinValue[i].x) >=0 && (y - coinValue[i].y) >= 0) + { + dp[x][y] = min(dp[x - coinValue[i].x][y - coinValue[i].y] + 1, dp[x][y]); + } + } + } + } + + int answer = INF; + + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + if (dp[i][j] != INF && ((i*i + j*j) == s*s)) + { + answer = min(answer, dp[i][j]); + } + } + } + + if (answer == INF) + { + cout << "not possible" << endl; + } + + else + { + cout << answer << endl; + } + + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10306/10306-21.cpp b/uva_cpp_clean/10306/10306-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..106b36104fa2b905efed07f36ea275e3ef37c474 --- /dev/null +++ b/uva_cpp_clean/10306/10306-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10306 + Name: e-Coins + Problem: https://onlinejudge.org/external/103/10306.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int DP[313][313]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, p, c, d; + cin >> T; + while (T--) { + cin >> n >> p; + memset(DP, 127, sizeof(DP)); + DP[0][0] = 0; + while (n--) { + cin >> c >> d; + for (int i=p; i>=0; --i) + for (int j=p; j>=0; --j) + if (DP[i][j] < 1000) { + int cc=c, dd=d; + for (int k=1; i+cc<=p && j+dd<=p; ++k, cc+=c, dd+=d) + DP[i+cc][j+dd] = min(DP[i+cc][j+dd], DP[i][j]+k); + } + } + + int mn = 1000; + for (int i=0; i<=p; ++i) + for (int j=0; j<=p; ++j) + if (i*i+j*j==p*p) + mn = min(mn, DP[i][j]); + if (mn == 1000) + cout << "not possible\n"; + else + cout << mn << endl; + } +} diff --git a/uva_cpp_clean/10306/10306-9.cpp b/uva_cpp_clean/10306/10306-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81c62144df057320352e79d078f4431c0f76af9b --- /dev/null +++ b/uva_cpp_clean/10306/10306-9.cpp @@ -0,0 +1,31 @@ +#include + +#define MAX_N 50 + +using namespace std; + +int tc,ans,v1[MAX_N],v2[MAX_N],m,mod; + +void backtrack(int id,int sum1,int sum2,int ct){ + if(id==m || sum1*sum1+sum2*sum2>mod || ct>=ans) return; + if(sum1*sum1+sum2*sum2==mod){ + ans = ct; + return; + } + backtrack(id,sum1+v1[id],sum2+v2[id],ct+1); + backtrack(id+1,sum1,sum2,ct); +} + +int main(){ + scanf("%d",&tc); + while(tc--){ + ans = INT_MAX; + scanf("%d %d",&m,&mod); + mod *= mod; + for(int i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10308 + Name: Roads in the North + Problem: https://onlinejudge.org/external/103/10308.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct Edge { + int u, d; + Edge(int u, int d):u(u),d(d){} +}; +vector adj[10043]; + +int seen[10043], cse; +Edge dfs(int u) { + seen[u] = cse; + + Edge r(u, 0); + for (Edge &e: adj[u]) + if (seen[e.u] != cse) { + Edge rr = dfs(e.u); + if ((rr.d += e.d) > r.d) + r = rr; + } + return r; +} + +int main() { + for (cse=1; ; ++cse) { + int n = 0; + for (int u,v,d; cin.peek()!='\n' && cin >> u >> v >> d; cin.ignore(100,'\n')) { + int nn = max(n, max(u--, v--)); + for (; n <= nn; ++n) + adj[n].clear(); + + adj[u].push_back(Edge(v, d)); + adj[v].push_back(Edge(u, d)); + } + if (!n) break; + + int u = dfs(0).u; ++cse; + cout << dfs(u).d << endl; + + cin.ignore(100,'\n'); + } +} diff --git a/uva_cpp_clean/10309/10309-19.cpp b/uva_cpp_clean/10309/10309-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25d4a0383582a0dc9d886fe2da1a291bc8b2a56e --- /dev/null +++ b/uva_cpp_clean/10309/10309-19.cpp @@ -0,0 +1,62 @@ +#include +#include +using namespace std; + +bool grid[10][10]; +bool mask[10][10]; + +void press(int i, int j) { + mask[i][j] = !mask[i][j]; + grid[i][j] = !grid[i][j]; + if (i > 0) grid[i-1][j] = !grid[i-1][j]; + if (i < 9) grid[i+1][j] = !grid[i+1][j]; + if (j > 0) grid[i][j-1] = !grid[i][j-1]; + if (j < 9) grid[i][j+1] = !grid[i][j+1]; +} + +void undo() { + for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) + if (mask[i][j]) press(i, j); +} + +int proc2() { + int count = 0; + for (int i = 1; i < 10; i++) { + for (int j = 0; j < 10; j++) { + if (grid[i-1][j]) press(i, j), count++; + } + } + for (int i = 0; i < 10; i++) if (grid[9][i]) return 100000000; + return count; +} + +int proc() { + int min = INT_MAX, count; + for (int i = 0; i < 1024; i++) { + count = 0; + for (int j = 0; j < 10; j++) { + if ((i >> j) % 2) press(0, j), count++; + } + int val = proc2() + count; + if (val < min) min = val; + undo(); + } + return min; +} + +int main() { + string title; + while (getline(cin, title) && title != "end") { + for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) { + char c; + cin >> c; + grid[i][j] = (c == 'O'); + mask[i][j] = false; + } + printf("%s %d\n", title.c_str(), proc()); + getline(cin, title); + } + return 0; +} diff --git a/uva_cpp_clean/10309/10309-21.cpp b/uva_cpp_clean/10309/10309-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..174cacfca85aaf661edd30cd5e284d7302f32d86 --- /dev/null +++ b/uva_cpp_clean/10309/10309-21.cpp @@ -0,0 +1,66 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10309 + Name: Turn the Lights Off + Problem: https://onlinejudge.org/external/103/10309.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool M[10][10]; +int click(int i, int j) { + int cnt = (M[i][j] = !M[i][j]) ? 1 : -1; + if (i) cnt += (M[i-1][j] = !M[i-1][j]) ? 1 : -1; + if (j) cnt += (M[i][j-1] = !M[i][j-1]) ? 1 : -1; + if (i<9) cnt += (M[i+1][j] = !M[i+1][j]) ? 1 : -1; + if (j<9) cnt += (M[i][j+1] = !M[i][j+1]) ? 1 : -1; + return cnt; +} + +int best; +void backtrack(int step, int ocnt, int i=0, int j=0) { + if (j == 10) ++i, j=0; + + int h = (ocnt + 4) / 5; + if (!h) best = min(best, step); + if (step+h >= best || i==10) return; + + if (!i || !M[i-1][j]) + backtrack(step, ocnt, i, j+1); + + if (step+h >= best) return; + if (!i || M[i-1][j]) { + int chng = click(i, j); + backtrack(step + 1, ocnt + chng, i, j+1); + click(i, j); + } +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string title, ln; + while (getline(cin, title) && title!="end") { + int cnt = 0; + for (int i=0; i<10; ++i) { + cin >> ln; + for (int j=0; j<10; ++j) + if ((M[i][j] = ln[j] != '#')) + ++cnt; + } + + best = 101; + backtrack(0, cnt); + + cout << title << ' ' << (best<=100 ? best : -1) << '\n'; + cin.ignore(1000, '\n'); + } +} diff --git a/uva_cpp_clean/10309/10309-9.cpp b/uva_cpp_clean/10309/10309-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0898c2d0c7abd244e4e8c27105ea3f5be120926 --- /dev/null +++ b/uva_cpp_clean/10309/10309-9.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +const int SIZE = 10, LEN = 100; + +int on, ans; +char test_name[LEN], grid[SIZE + 1][SIZE + 1]; +int dr[] = {-1, 0, 0, 0, 1}; +int dc[] = {0, -1, 0, 1, 0}; + +inline void print() { + printf("%s %d\n", test_name, ans == 101 ? -1 : ans); +} + +inline bool isValid(int r, int c) { + return (0 <= r and 0 <= c and r < SIZE and c < SIZE); +} + +void press(int r, int c) { + for (int d = 0; d < 5; d++) { + int nr = r + dr[d], nc = c + dc[d]; + if (isValid(nr, nc)) { + if (grid[nr][nc] == '#') on++, grid[nr][nc] = 'O'; + else if (grid[nr][nc] == 'O') on--, grid[nr][nc] = '#'; + } + } +} + +void backtrack(int r = 0, int c = 0, int pressed = 0) { + if (pressed >= ans) return; + if (on == 0) { + ans = min(ans, pressed); + return; + } + if (r == SIZE) return; + if (not isValid(r - 1, c) or grid[r - 1][c] == 'O') { + press(r, c); + backtrack(c + 1 == SIZE ? r + 1 : r, c + 1 == SIZE ? 0 : c + 1, pressed + 1); + press(r, c); + } + if (not isValid(r - 1, c) or grid[r - 1][c] == '#') + backtrack(c + 1 == SIZE ? r + 1 : r, c + 1 == SIZE ? 0 : c + 1, pressed); +} + +void init() { + on = 0; + ans = 101; +} + +bool read() { + init(); + scanf("%s\n", test_name); + if (strcmp(test_name, "end") == 0) return false; + for (int row = 0; row < SIZE; row++) { + scanf("%s\n", grid[row]); + for (int col = 0; col < SIZE; col++) + if (grid[row][col] == 'O') on++; + } + return true; +} + +int main() { + while (read()) { + backtrack(); + print(); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10310/10310-21.cpp b/uva_cpp_clean/10310/10310-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e4f8ab22a93092e3b0afa9a203cfc20442f5c41 --- /dev/null +++ b/uva_cpp_clean/10310/10310-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10310 + Name: Dog and Gopher + Problem: https://onlinejudge.org/external/103/10310.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +const int X=0,Y=1; + +double dist(double p1[],double p2[2]){ + return sqrt(pow(p1[X]-p2[X],2)+pow(p1[Y]-p2[Y],2)); +} + +int main(){ + double g[2],d[2],h[2]; + int f,i,n; + while(scanf("%d %lf%lf%lf%lf",&n,&g[X],&g[Y],&d[X],&d[Y])==5){ + f=0; + for(i=0;i +#include +#include +#define X first +#define Y second +using namespace std; + +typedef pair TPoint; + +double distPoints(TPoint a, TPoint b){ + return sqrt((a.X-b.X)*(a.X-b.X) + (a.Y-b.Y)*(a.Y-b.Y)); +} + +int main() { + int qteBuracos; + double tmpX, tmpY, distCachorro, distTopeira; + TPoint t0, c0, ans; //topeira, cachorro + vector buracos; + + while (scanf("%d", &qteBuracos) != EOF) { + scanf("%lf %lf", &t0.X, &t0.Y); + scanf("%lf %lf", &c0.X, &c0.Y); + buracos.clear(); + for (int i = 0; i < qteBuracos; i++) { + scanf("%lf %lf", &tmpX, &tmpY); + buracos.push_back(make_pair(tmpX, tmpY)); + } + + bool topeiraEscapa = false; + for (int i = 0; i < qteBuracos && !topeiraEscapa; i++) { + distTopeira = distPoints(t0, buracos[i]); + distCachorro = distPoints(c0, buracos[i]); + + if (distCachorro/2.0 >= distTopeira) { + topeiraEscapa = true; + ans = buracos[i]; + } + } + + if (topeiraEscapa) + printf("The gopher can escape through the hole at (%.3lf,%.3lf).\n", ans.X, ans.Y); + else printf("The gopher cannot escape.\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10311/10311-18.cpp b/uva_cpp_clean/10311/10311-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4c8bf00a3fd856c5077146466014b2cdb648ebfa --- /dev/null +++ b/uva_cpp_clean/10311/10311-18.cpp @@ -0,0 +1,60 @@ +#include +#define pb push_back +#define pf push_front +#define pob pop_back +#define fp first +#define sp second +#define mp make_pair +#define ins insert +#define uEdge(u, v) g[u].pb(v), g[v].pb(u) +#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w}) +#define dEdge(u, v) g[u].pb(v) +#define dwEdge(u, v, w) g[u].pb({v, w}) +#define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define minHeap int, vector, greater + +using namespace std; +typedef long long int lli; +typedef pair pii; +vector g[0]; +bool primes[100000000]; + +lli gcd(lli a, lli b){ + if(b == 0) return a; + a%=b; + return gcd(b, a); +} + +void sieve(){ + memset(primes, 1, sizeof(primes)); + primes[0] = primes[1] = 0; + for(lli i = 2; i*i <= 100000000; i++){ + if(primes[i]) for(lli j = i*i; j <= 100000000; j+=i) primes[j] = 0; + } +} + +int main(){ + int flag; + lli n; + sieve(); + while(scanf("%lld", &n) != EOF){ + if(n&1){ + if(primes[n - 2]) printf("%lld is the sum of 2 and %lld.\n", n, n - 2); + else printf("%lld is not the sum of two primes!\n", n); + }else{ + flag = 1; + for(lli i = (n/2) - 1; i >= 2; i--){ + if(primes[i] && primes[n - i]){ + printf("%lld is the sum of %lld and %lld.\n", n, i, n - i); + flag = 0; + break; + } + } + if(flag) printf("%lld is not the sum of two primes!\n", n); + } + } + return 0; +} +/* + Ignorar casos: 1 + 1 = 2, 1 + 2 = 3. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10311/10311-19.cpp b/uva_cpp_clean/10311/10311-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd1f65c5adc8546ab95a13f9cc1dfaec04fe388b --- /dev/null +++ b/uva_cpp_clean/10311/10311-19.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<100000020> bs; +vi primes; + +inline void sieve(ll upperbound = 100000010) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +int main() { + sieve(); + int n; + while (cin >> n) { + if (n % 2 == 0) { + int i = lower_bound(primes.begin(), primes.end(), n/2) - primes.begin(); + i--; + for (; i >= 0; i--) { + if (n - primes[i] > 0 && bs[n - primes[i]]) { + printf("%d is the sum of %d and %d.\n", n, primes[i], n - primes[i]); + break; + } + } + if (i < 0) printf("%d is not the sum of two primes!\n", n); + } else { // odd + even = odd; odd + odd = even + if (n > 2 && bs[n-2]) printf("%d is the sum of %d and %d.\n", n, 2, n - 2); + else printf("%d is not the sum of two primes!\n", n); + } + } + return 0; +} diff --git a/uva_cpp_clean/10311/10311-20.cpp b/uva_cpp_clean/10311/10311-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e6fe429c26a147f1c554dcbe8d229523b7aaca5 --- /dev/null +++ b/uva_cpp_clean/10311/10311-20.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +#define MAX 10000 +#define Check(N, pos) ((bool) ((N)&(1<<(pos)))) +#define Set(N, pos) ((N)=((N)|(1<<(pos)))) + +vector < unsigned > primes; +unsigned status[(MAX>>5)+1]; + +void bitSieve() +{ + unsigned sqrtMAX = unsigned (sqrt(MAX)); + for(unsigned i=3 ; i<=sqrtMAX ; i+=2) + if( !Check(status[i>>5], i&31) ) + for(unsigned j=i*i ; j<=MAX ; j+=(i<<1)) Set(status[j>>5], j&31); + + primes.push_back(2); + for(unsigned i=3 ; i<=MAX ; i+=2) + if( !Check(status[i>>5], i&31) ) primes.push_back(i); +} + +bool isPrime(unsigned num) +{ + unsigned sqrtNUM = unsigned (sqrt(num)), SZ = primes.size(); + for(unsigned i=0 ; i>1); + for(unsigned i=temp ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10311 + Name: Goldbach and Euler + Problem: https://onlinejudge.org/external/103/10311.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define N 100100100 +unsigned int prime[N / 64]; +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) +void sieve() { + memset(prime, -1, sizeof(prime)); + unsigned int i, i2, sqrtN = (unsigned int)sqrt((double)N) + 1; + for(i = 3; i>6] &= ~(1<<((j>>1)&31)); + } +} + +bool isPrime(int n) { + return n>1 && (n==2 || ((n&1) && gP(n))); +} + +int findPrimes(int n){ + if (n%2 == 1) { + return isPrime(n-2) ? 2 : -1; + } + + int mid = n/2 - 1; + if (mid % 2 == 0) --mid; + for (int i=mid; i>1; i-=2) + if (isPrime(i) && isPrime(n-i)) + return i; + + return -1; +} + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + sieve(); + + int n; + while (cin >> n) { + int ii = findPrimes(n); + if (ii < 0) cout << n << " is not the sum of two primes!" << endl; + else cout << n << " is the sum of " << ii << " and " << (n-ii) << "." << endl; + } +} diff --git a/uva_cpp_clean/10311/10311-5.cpp b/uva_cpp_clean/10311/10311-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..08dbac9cc078f253fdc73c3edb7f70cf7d987d54 --- /dev/null +++ b/uva_cpp_clean/10311/10311-5.cpp @@ -0,0 +1,208 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx8 100000000 +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// +bool prime[mx8]; +ll pd[mx7]; +int k=1; +void seive() +{ + prime[0]=1; + prime[1]=1; + for(int i=4;i<=mx8;i+=2) + prime[i]=1; + + pd[0]=2; + k=1; + for(ll i=3;i<=mx8;i+=2) + { + if(!prime[i]) + { + pd[k++]=i; + ll mul = i<<1; + for(ll j=i*i;j<=mx8;j +=mul) + prime[j]=1; + } + + } + +} + +int main() +{ + seive(); + ll n; + while(sl(n)==1) + { + if(!prime[n] || n<=3) + { + printf("%lld is not the sum of two primes!\n",n); + continue; + } + + int f=0; + for0(i,k) + { + int p = n-pd[i]; + if(!prime[p] && p>0) + { + f=1; + printf("%lld is the sum of %lld and %lld.\n",n,pd[i],n-pd[i]); + break; + } + + } + if(f==0) + { + printf("%d is not the sum of two primes!\n",n); + } + + } + return 0; +} + diff --git a/uva_cpp_clean/10311/10311-8.cpp b/uva_cpp_clean/10311/10311-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2eae1641cfa6c19090b5c9d75bbc17e64a875b20 --- /dev/null +++ b/uva_cpp_clean/10311/10311-8.cpp @@ -0,0 +1,46 @@ +// Problem name: Goldbach and Euler +// Problem link: https://vjudge.net/problem/UVA-10311 +// Submission link: https://vjudge.net/solution/32959031 + +#include + +using namespace std; + +typedef long long ll; + +vector get_primes(int n){ + vector small_prime(n + 1), primes; + for(int i = 2 ; i <= n ; ++i){ + if(small_prime[ i ] == 0) + small_prime[ i ] = i, primes.push_back(i); + for(int j = 0 ; j < primes.size() && primes[ j ] <= small_prime[ i ] && i * primes[ j ] <= n ; ++j) + small_prime[ i * primes[ j ] ] = primes[ j ]; + } + return primes; +} + +pair get_goldbach(int n, const vector &primes){ + for(auto right = upper_bound(primes.begin(), primes.end(), n / 2), left = right - 1 ; left >= primes.begin() && right != primes.end() ; --left){ + while(*left + *right < n) + ++right; + if(*left + *right == n) + return { *left, *right }; + } + return { -1, -1 }; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int N = 100000000; + const vector primes = get_primes(N); + + for(int n ; cin >> n ; ){ + pair ans = n < 4 || (n & 1 && *lower_bound(primes.begin(), primes.end(), n - 2 ) != n - 2) ? make_pair(-1, -1) : (n & 1 ? make_pair(2, n - 2) : get_goldbach(n, primes)); + if(ans.first == -1) + cout << n << " is not the sum of two primes!" << endl; + else + cout << n << " is the sum of " << ans.first << " and " << ans.second << "." << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10311/10311-9.cpp b/uva_cpp_clean/10311/10311-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae173b15a60659505c20ef6eb0d302bdfd33ae14 --- /dev/null +++ b/uva_cpp_clean/10311/10311-9.cpp @@ -0,0 +1,43 @@ +#include + +#define SIZE 100000010 + +using namespace std; + +bool is_prime[SIZE]; +vectorprime; +int n, a, b; +bool found; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++){ + if(is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } + } +} + +int main(){ + sieve(); + while(~scanf("%d",&n)){ + found = false; + if(n&1){ + if( n > 2 and is_prime[ n - 2]){ + found = true; + a = 2, b = n - 2; + } + } + else{ + int pos = lower_bound(prime.begin(), prime.end(), n/2) - prime.begin(); + for(int it = pos; not found and it>=0; it--) + if( is_prime[ n - prime[it] ] and n != 2 * prime[it] ) found = true, a = prime[it], b = n - prime[it]; + } + if(found==false) printf("%d is not the sum of two primes!\n", n); + else printf("%d is the sum of %d and %d.\n", n, min(a, b), max(a, b)); + } + return(0); +} diff --git a/uva_cpp_clean/10312/10312-9.cpp b/uva_cpp_clean/10312/10312-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6caca80148ffea5fac705a5274afb5b538184f4b --- /dev/null +++ b/uva_cpp_clean/10312/10312-9.cpp @@ -0,0 +1,17 @@ +from sys import stdin,stdout + +def main(): + catalan = [1] + for it in range(1,30): + catalan.append(catalan[it-1]*((it<<2)-2)//(it+1)) + superCatalan = [1,1] + for it in range(2,30): + superCatalan.append(((6*it-3)*superCatalan[it-1]-(it-2)*superCatalan[it-2])//(it+1)) + while True: + idx = stdin.readline() + if len(idx)==0: + break + stdout.write(str(superCatalan[int(idx)-1]-catalan[int(idx)-1])+"\n") + +if __name__ == "__main__": + main() diff --git a/uva_cpp_clean/10313/10313-21.cpp b/uva_cpp_clean/10313/10313-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8da82b6811110c736b38d780b437d5c98d8ca1d8 --- /dev/null +++ b/uva_cpp_clean/10313/10313-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10313 + Name: Pay the Price + Problem: https://onlinejudge.org/external/103/10313.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +long long DP[313][313]; +long long sum(int p, int l, int r=300) { + if (l > p) return 0; + if (r > 300) r = 300; + long long res = DP[p][r]; + if (l > 0) res -= DP[p][l-1]; + return res; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + DP[0][0] = 1; + for (int c=1; c<=300; ++c) + for (int i=1; i<=300; ++i) + for (int r=c; r<=300; ++r) + DP[r][i] += DP[r-c][i-1]; + + for (int r=0; r<=300; ++r) + for (int i=1; i<=300; ++i) + DP[r][i] += DP[r][i-1]; + + char line[100]; + while (cin.getline(line, 100)) if (line[0]) { + int p, l, r; + switch (sscanf(line, "%d%d%d", &p, &l, &r)) { + case 1: cout << DP[p][p] << "\n"; break; + case 2: cout << sum(p, 0, l) << "\n"; break; + case 3: cout << sum(p, l, r) << "\n"; + } + } +} diff --git a/uva_cpp_clean/10313/10313-9.cpp b/uva_cpp_clean/10313/10313-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e2c82dfb8d2fe77482d80ffd6acce91941c1ef0 --- /dev/null +++ b/uva_cpp_clean/10313/10313-9.cpp @@ -0,0 +1,31 @@ +#include + +#define MAX_W 310 +#define MAX_COINS 310 +#define SIZE 100 + +using namespace std; + +int k,N,L1,L2; +long long dp[MAX_COINS][MAX_W],ans; +char s[SIZE]; + +int main(){ + dp[0][0] = 1; + for(int id=1;id<=305;id++) + for(int w=0;w<=305;w++){ + dp[id][w] = dp[id-1][w]; + if(w>=id) dp[id][w] = max(dp[id][w],dp[id-1][w]+dp[id][w-id]); + } + while(true){ + cin.getline(s,SIZE); + k = sscanf(s,"%d %d %d",&N,&L1,&L2); + if(k==-1) break; + if(k==1) ans = dp[N][N]; + else if(k==2) ans = dp[min(L1,N)][N]; + else if(k==3 && N==0 && L1==0) ans = 1; + else ans = dp[min(L2,N)][N]-dp[min(N+1,max(L1-1,0))][N]; + printf("%lld\n",ans); + } + return(0); +} diff --git a/uva_cpp_clean/10318/10318-21.cpp b/uva_cpp_clean/10318/10318-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a6a33d70f463dbbb894b8defb418cb2128dafe2 --- /dev/null +++ b/uva_cpp_clean/10318/10318-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10318 + Name: Security Panel + Problem: https://onlinejudge.org/external/103/10318.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int M[7][7], r, c, + di[10], dj[10], dsz; +void press(int ii, int jj) { + for (int k=0; k=0 && i=0 && j=2 && !M[i-1][j-2]) return; + if (j == c) { + if (i && j>=1 && !M[i-1][j-1]) return; + i++; + j=0; + } + + if (i < r) { + backtrack(i, j+1, step); + press(i, j); + pp[step] = i*c + j + 1; + backtrack(i, j+1, step+1); + press(i, j); + return; + } + + for (int k=0; k step) { + best = step; + memcpy(bestp, pp, step*sizeof(int)); + } +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string ln; + for (int cse=1; cin >> r >> c && (r||c); ++cse) { + dsz = 0; + for (int i=0; i<3; ++i) { + cin >> ln; + for (int j=0; j<3; ++j) + if (ln[j] == '*') { + di[dsz] = i-1; + dj[dsz] = j-1; + ++dsz; + } + } + + + best = 100; + backtrack(0, 0); + cout << "Case #" << cse << '\n'; + if (best == 100) cout << "Impossible.\n"; + else { + cout << bestp[0]; + for (int i=1; i + +using namespace std; + +const int SIZE = 5, MAX_MASK = (1 << (SIZE * SIZE)); + +int tc, R, C, ones; +char ch; +bool valid[SIZE][SIZE]; +vector < int > ans, ret; + +void print() { + printf("Case #%d\n", ++tc); + if (ans.empty()) puts("Impossible."); + else for (int i = 0, sz = ans.size(); i < sz; i++) printf(i == sz - 1 ? "%d\n" : "%d ", ans[i] + 1); +} + +inline bool isValid(int r, int c) { + return (0 <= r and 0 <= c and r < R and c < C); +} + +int mark(int mask, int id) { + int r = id / C, c = id % C; + for (int dr = -1; dr <= 1; dr++) + for (int dc = -1; dc <= 1; dc++) { + int nr = r + dr, nc = c + dc, hh = nr * C + nc; + if (valid[dr + 1][dc + 1] and isValid(nr, nc)) + mask ^= (1 << hh); + } + return mask; +} + +bool isTurned(int mask, int id) { + int r = (id / C) - 1, c = (id % C) - 1, hh = r * C + c; + return (not isValid(r, c) or (mask >> hh) & 1); +} + +void backtrack(int used = 0, int id = 0) { + if (used == ones) { + ans = ret; + return; + } + if (id == R * C) return; + if ((ans.empty() or ret.size() + 1 < ans.size()) and isTurned(mark(used, id), id)) { + ret.push_back(id); + backtrack(mark(used, id), id + 1); + ret.pop_back(); + } + backtrack(used, id + 1); +} + +void read() { + for (int row = 0; row < 3; row++, getchar()) + for (int col = 0; col < 3; col++) + if ((ch = getchar()) == '*') valid[row][col] = true; +} + +void init() { + memset(valid, false, sizeof valid); + ones = (1 << (R * C)) - 1; + ans.clear(); +} + +int main() { + while(scanf("%d %d\n", &R, &C), R bitor C) { + init(); + read(); + backtrack(); + print(); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10323/10323-19.cpp b/uva_cpp_clean/10323/10323-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a79c21c9404382ffa81db872d3e0729acf0a7dc0 --- /dev/null +++ b/uva_cpp_clean/10323/10323-19.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main() { + int n; + while (cin >> n) { + if (n > 13 || (n < 0 && n % 2)) printf("Overflow!\n"); + else { + long long fact = 1; + for (int i = 1; i <= n; i++) fact *= i; + if (fact < 10000) printf("Underflow!\n"); + else printf("%lld\n", fact); + } + } + return 0; +} diff --git a/uva_cpp_clean/10323/10323-21.cpp b/uva_cpp_clean/10323/10323-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd8c634ddd2c7a62feb407ef810969e0e5916184 --- /dev/null +++ b/uva_cpp_clean/10323/10323-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10323 + Name: Factorial! You Must be Kidding!!! + Problem: https://onlinejudge.org/external/103/10323.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +long long fac[] = {40320, 362880, 3628800, 39916800, 479001600, 6227020800}; +int main(){ + int n; + while (cin>>n) { + if ((n<0 && n%2) || n>13) + cout << "Overflow!\n"; + else if ((n<0 && n%2==1) || n<8) + cout << "Underflow!\n"; + else + cout << fac[n-8] << endl; + } +} + diff --git a/uva_cpp_clean/10323/10323-5.cpp b/uva_cpp_clean/10323/10323-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a0b60410cb9b354a5c5bc69d55f3e41e2c700b65 --- /dev/null +++ b/uva_cpp_clean/10323/10323-5.cpp @@ -0,0 +1,29 @@ +#include +int main() +{ +long long n; +while(scanf("%lld",&n)==1) +{ +if(n<0&&n%2==0) +printf("Underflow!\n"); +else if(n<0&&n%2!=0) +printf("Overflow!\n"); +else if(n==0||n<=7) +printf("Underflow!\n"); +else if(n==8) +printf("40320\n"); +else if(n==9) +printf("362880\n"); +else if(n==10) +printf("3628800\n"); +else if(n==11) +printf("39916800\n"); +else if(n==12) +printf("479001600\n"); +else if(n==13) +printf("6227020800\n"); +else if(n>13) +printf("Overflow!\n"); +} +return 0; +} diff --git a/uva_cpp_clean/10323/10323-9.cpp b/uva_cpp_clean/10323/10323-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a039f1de2c4b70543a8c15c0aa223bf903003ee0 --- /dev/null +++ b/uva_cpp_clean/10323/10323-9.cpp @@ -0,0 +1,17 @@ +import math + +def solution(): + while True: + try: + n = int(input()) + if 0 <= n < 8 or ( n < 0 and n % 2 == 0 ): + print('Underflow!') + elif n > 13 or ( n < 0 and n % 2 != 0 ): + print('Overflow!') + else: + print(math.factorial(n)) + except EOFError: + break + +if __name__=="__main__": + solution() diff --git a/uva_cpp_clean/10324/10324-18.cpp b/uva_cpp_clean/10324/10324-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..245469ff4ac1736e09f5a3eec8af3805c9277ed3 --- /dev/null +++ b/uva_cpp_clean/10324/10324-18.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +int main(){ + char input[1000010]; + int n, a, b, tc = 1; + bool valid; + while(scanf("%s %d", &input, &n) == 2){ + printf("Case %d:\n", tc); + tc++; + for(int i = 0; i < n; i++){ + scanf("%d %d", &a, &b); + if(a > b) swap(a, b); + valid = true; + for(int i = a; i < b && valid; i++){ + if(input[i] != input[i + 1]) valid = false; + } + if(valid) printf("Yes\n"); + else printf("No\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10324/10324-19.cpp b/uva_cpp_clean/10324/10324-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d63fb506d52131bf30a31602976f5eeb36b4db4d --- /dev/null +++ b/uva_cpp_clean/10324/10324-19.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; + +int main() { + vector v; + string s; + int c = 1; + while (cin >> s) { + v.clear(); + v.push_back(0); + for (int i = 1; i < s.length(); i++) { + if (s[i] != s[i-1]) v.push_back(i); + } + printf("Case %d:\n", c); + int n; + cin >> n; + while (n--) { + int i, j, auxi; + cin >> i >> j; + auxi = min(i, j); + j = max(i, j); + i = auxi; + int p1 = upper_bound(v.begin(), v.end(), i) - v.begin(); + int p2 = upper_bound(v.begin(), v.end(), j) - v.begin(); + if (p1 == p2) printf("Yes\n"); + else printf("No\n"); + } + c++; + } + return 0; +} diff --git a/uva_cpp_clean/10324/10324-2.cpp b/uva_cpp_clean/10324/10324-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e90c0e8df1a2a900102149b557e80926fb5b9c25 --- /dev/null +++ b/uva_cpp_clean/10324/10324-2.cpp @@ -0,0 +1,62 @@ + +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +//const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + string s; + int t = 1; + while (cin >> s) { + printf("Case %d:\n", t++); + int tc; cin >> tc; + while (tc--) { + int i, j; cin >> i >> j; + char x = s[i]; + bool f = 1; + if (j < i) { + int temp = i; + i = j; + j = temp; + } + + for (; i <= j; ++i) + if (s[i] != x) { + f = 0; + break; + } + if (!f) cout << "No\n"; + else cout << "Yes\n"; + } + } + return 0; +} diff --git a/uva_cpp_clean/10324/10324-20.cpp b/uva_cpp_clean/10324/10324-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c14a39ed77a1970f13636992bdcec1319727627 --- /dev/null +++ b/uva_cpp_clean/10324/10324-20.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +using namespace std; + +char str[1000005]; + +int main() +{ + int ncase = 0; + + while( scanf("%s", str)==1 && strcmp(str,"\n") ) + { + printf("Case %d:\n", ++ncase); + + int n; + scanf("%d", &n); + while( n-- ) + { + int a, b; + scanf("%d %d", &a, &b); + + if( a==b ) printf("Yes\n"); + else + { + if( a>b ) swap(a, b); + + bool flag; + for(int i=a ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10324 + Name: Zeros and Ones + Problem: https://onlinejudge.org/external/103/10324.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +int main() { + int cse = 0; + int cum[1000003]; + char line[1000003]; + + while (cin.getline(line, 1000003) && line[0]) { + printf("Case %d:\n", ++cse); + cum[0]=line[0] - '0'; + for (int i=1; line[i]; i++) + cum[i] = cum[i-1] + line[i] - '0'; + + int n; + cin>>n; + while (n--) { + int a, b; + cin >> a >> b; + if (a>b) swap (a, b); + int sum = cum[b] - (a>0 ? cum[a-1] : 0); + cout<< (sum==0 || sum==b-a+1 ? "Yes\n" : "No\n"); + } + cin.getline(line, 1000000); + } +} diff --git a/uva_cpp_clean/10324/10324-5.cpp b/uva_cpp_clean/10324/10324-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0afdf4c31193b6d7b86a7328e62ed94378bac4ad --- /dev/null +++ b/uva_cpp_clean/10324/10324-5.cpp @@ -0,0 +1,47 @@ +#include +#include + +int main() +{ + char s[1000015]; + int t=1,n,p1,p2,i,c,tem; + while(scanf("%s",s)==1) + { + if(strcmp(s,"\n")==0) + break; + scanf("%d",&n); + printf("Case %d:\n",t++); + while(n--) + { + scanf("%d%d",&p1,&p2); + if(p1>p2) + { + tem=p1; + p1=p2; + p2=tem; + } + if(p1==p2) + { + printf("Yes\n"); + continue; + } + for(i=p1;i +#include +using namespace std; + +int main(){ + + string word; + int pos_i,pos_j, cases, maxi, n_counter=1; + while(cin >> word >> cases){ + cout << "Case " << n_counter << ':' << endl; + for(int i = 0; i < cases; i++){ + cin >> pos_i >> pos_j; + bool res = true; + maxi = max(pos_i, pos_j); + for(int c = min(pos_i, pos_j); c < maxi; c++) if(word[c] != word[maxi]) res = false; + if(res) cout << "Yes" << endl; + else cout << "No" << endl; + } + n_counter++; + } + +return 0; +} diff --git a/uva_cpp_clean/10325/10325-14.cpp b/uva_cpp_clean/10325/10325-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..747e350237a7064cf38eb23a194a79d6d8fc5a8e --- /dev/null +++ b/uva_cpp_clean/10325/10325-14.cpp @@ -0,0 +1,89 @@ +/*************************************************** + * Problem name : 10325 The Lottery.cpp + * Problem Link : https://uva.onlinejudge.org/external/103/10325.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-10-23 + * Problem Type : Inclusion/Exclusion(NT) + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 16 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +//~ ll gcd(ll a, ll b) { + //~ if (a % b == 0) return b; + //~ return gcd(b, a % b); +//~ } +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +ll bitMask(ll N, ll k, ll ar[]) { + ll ans = 0; + for (ll mask = 1; mask < (1 << k); mask++) { + ll i = 0, cnt = 0, lc = 1; + ll y = mask; + while (y > 0) { + int rem = y % 2; + y /= 2; + if (rem == 1) { + lc = lcm(ar[i], lc); + cnt++; + } + i++; + } + if (cnt % 2 == 0) { + ans -= N / lc; + } else { + ans += N / lc; + } + } + return ans; +} +int main () { + ll N, k, ar[MAX]; + while (scanf("%lld %lld", &N, &k) == 2) { + for (int i = 0; i < k; i++) { + scanf("%lld", &ar[i]); + } + printf("%lld\n", N - bitMask(N, k, ar)); + } + + return 0; +} + diff --git a/uva_cpp_clean/10325/10325-21.cpp b/uva_cpp_clean/10325/10325-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..823b3834aede8327b5f56ef64325a4cf0939e9ec --- /dev/null +++ b/uva_cpp_clean/10325/10325-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10325 + Name: The Lottery + Problem: https://onlinejudge.org/external/103/10325.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +long long gcd(long long a, long long b) { + return b == 0 ? a : gcd(b, a % b); +} + +int X[23], n, m; +int dfs(long long mm, int sgn, int i) { + if (mm > n) return 0; + if (i == m) { + return mm>1 ? sgn * floor(n/mm) : 0; + } + + return dfs(mm*X[i]/gcd(mm, X[i]), sgn*-1, i+1) + dfs(mm, sgn, i+1); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n >> m) { + for (int i=0; i> X[i]; + } + + cout << (n - dfs(1, -1, 0)) << endl; + } +} diff --git a/uva_cpp_clean/10326/10326-9.cpp b/uva_cpp_clean/10326/10326-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9bf68a03bb808721643db8d73bc578d62b2ebe1 --- /dev/null +++ b/uva_cpp_clean/10326/10326-9.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +struct Polynomial{ + int degree; + vector < long long > coef; + Polynomial() {}; + Polynomial(int degree_): degree(degree_){ + coef.assign(degree + 1, 0); + }; + Polynomial operator * (const Polynomial& other) const{ + Polynomial r(degree + other.degree); + for(int i = 0; i <= degree; i++) + for(int j = 0; j <= other.degree; j++) + r.coef[i + j] += coef[i] * other.coef[j]; + return r; + } +}p, q; + +void print(Polynomial& p){ + for(int it = p.degree; it >= 0; it--){ + if( it and it != p.degree and p.coef[it] != 0 ){ + putchar(' '); + if( p.coef[it] > 0 ) putchar('+'); + else if( p.coef[it] < 0 ) putchar('-'); + putchar(' '); + } + if( it and p.coef[it] != 0 ){ + if( abs(p.coef[it]) != 1 ) printf("%lld", abs(p.coef[it])); + if( it != 1 ) printf("x^%d", it); + else putchar('x'); + } + else if( it == 0 ){ + putchar(' '); + if( p.coef[it] >= 0 ) putchar('+'); + else putchar('-'); + putchar(' '); + printf("%lld", abs(p.coef[it])); + } + } + printf(" = 0\n"); +} + +int n, root; + +int main(){ + while(~scanf("%d", &n)){ + scanf("%d", &root); + p = Polynomial(1); + p.coef[0] = -root, p.coef[1] = 1; + for(int it = 1; it < n; it++){ + scanf("%d", &root); + q = Polynomial(1); + q.coef[0] = -root, q.coef[1] = 1; + p = p * q; + } + print(p); + } + return(0); +} diff --git a/uva_cpp_clean/10327/10327-21.cpp b/uva_cpp_clean/10327/10327-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..269b5be372f16b4ea49f0f4764253a7c4a1e5c99 --- /dev/null +++ b/uva_cpp_clean/10327/10327-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10327 + Name: Flip Sort + Problem: https://onlinejudge.org/external/103/10327.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int a[1001], n; + while (cin >> n) { + int result = 0; + + for (int i = 0; i < n; i++) + cin >> a[i]; + + for (int i = 0; i a[j + 1]) { + result++; + a[j] = a[j] ^ a[j + 1]; + a[j + 1] = a[j] ^ a[j + 1]; + a[j] = a[j] ^ a[j + 1]; + } + + printf("Minimum exchange operations : %d\n", result); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10327/10327-5.cpp b/uva_cpp_clean/10327/10327-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f5b006f965f95035414f319d768aad5de578b29a --- /dev/null +++ b/uva_cpp_clean/10327/10327-5.cpp @@ -0,0 +1,26 @@ +#include +int main() +{ +int s,temp,i,j,a[1020],c; +while(scanf("%d",&s)==1) +{ +c=0; +for(i=0;i=0;i--) +for(j=0;j<=i;j++) +{ +if(a[j]>a[j+1]) +{ +temp=a[j]; +a[j]=a[j+1]; +a[j+1]=temp; +c++; +} +} +printf("Minimum exchange operations : %d\n",c); +} +return 0; +} diff --git a/uva_cpp_clean/10327/10327-9.cpp b/uva_cpp_clean/10327/10327-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c627858336125523318f5f0d9fb583dde708217 --- /dev/null +++ b/uva_cpp_clean/10327/10327-9.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +const int SIZE = 1005; + +int N, v[SIZE]; + +int InsertionSort(){ + int ans = 0; + for(int i = 1; i < N; i++){ + int key = v[i], j; + for(j = i - 1; j >= 0 and v[j] > key; j--) + v[j + 1] = v[j], ans++; + v[j + 1] = key; + } + return ans; +} + +int main(){ + while(~scanf("%d", &N)){ + for(int it = 0; it < N; it++) + scanf("%d", v + it); + printf("Minimum exchange operations : %d\n", InsertionSort()); + } + return(0); +} diff --git a/uva_cpp_clean/10330/10330-19.cpp b/uva_cpp_clean/10330/10330-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a608e70c8c72f535d47535c64834b33fc82ab5a --- /dev/null +++ b/uva_cpp_clean/10330/10330-19.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +int N, mf, f, s, t, INF = INT_MAX; +int res[202][202]; +vi p; + +void augment(int v, int minEdge) { + if (v == s) { + f = minEdge; + return; + } else if (p[v] != -1) { + augment(p[v], min(minEdge, res[p[v]][v])); + res[p[v]][v] -= f; res[v][p[v]] += f; + } +} + +int main() { + while (cin >> N) { + for (int i = 0; i < 2*N + 2; i++) + for (int j = 0; j < 2*N + 2; j++) + res[i][j] = 0; + for (int i = 1; i <= N; i++) { + int c; + cin >> c; + res[2*i][(2*i)+1] = c; + } + int M; + cin >> M; + for (int i = 0; i < M; i++) { + int j, k, c; + cin >> j >> k >> c; + res[(2*j)+1][2*k] = c; + } + int B, D; + cin >> B >> D; + for (int i = 0; i < B; i++) { + int k; + cin >> k; + res[0][2*k] = INF; + } + for (int i = 0; i < D; i++) { + int k; + cin >> k; + res[(2*k)+1][1] = INF; + } + s = mf = 0, t = 1; + while (true) { + f = 0; + vi dist(2*N + 2, INF); dist[s] = 0; + queue q; + q.push(s); + p.assign(2*N + 2, -1); + while (!q.empty()) { + int u = q.front(); q.pop(); + if (u == t) break; + for (int v = 0; v < 2*N + 2; v++) { + if (res[u][v] > 0 && dist[v] == INF) { + dist[v] = dist[u] + 1, q.push(v), p[v] = u; + } + } + } + augment(t, INF); + if (f == 0) break; + mf += f; + } + printf("%d\n", mf); + } + return 0; +} diff --git a/uva_cpp_clean/10330/10330-21.cpp b/uva_cpp_clean/10330/10330-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3b50a5ebd2db3be571e4e89782d5902cedee245 --- /dev/null +++ b/uva_cpp_clean/10330/10330-21.cpp @@ -0,0 +1,102 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10330 + Name: Power Transmission + Problem: https://onlinejudge.org/external/103/10330.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#define MAXINT 2147483647 +#define MAX 202 +using namespace std; + + +int par[MAX]; +int cov[MAX]; +int rcap[MAX]; +int lcap[MAX][MAX]; +int lflw[MAX][MAX]; +int rflw[MAX]; +int n; +deque bfss; + + +int bfs(){ + int node,i,found=0,f; + + memset(cov,0,sizeof(cov)); + memset(par,0,sizeof(par)); + bfss.clear(); + bfss.push_back(0); + while(!bfss.empty() && !found){ + node=bfss.front(); + bfss.pop_front(); + for(i=1;i +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +const unsigned NUM=100; //number of elementes in array + +typedef long long int base_t; //base type used +const base_t BASE=1000000000; //base for numbers, 10^9 + +struct bigint{ + bigint(long long int i=0) :used(0),sign((i>=0)?1:-1){ + memset(inner,0,NUM*sizeof(base_t)); // Change this for speed + i*=sign; + for(;i>=BASE;i=i/BASE) + inner[used++]=i%BASE; + inner[used++]=i; + } + + bigint& operator<<=(unsigned i){ //shift by base + memmove(inner+i,inner,min(used,NUM-i)*sizeof(base_t)); used+=i; + memset(inner,0,i*sizeof(base_t)); + return *this; + } + bigint operator<<(unsigned i) { bigint r(*this); return r<<=i; } + + + bigint& operator+=(const bigint& b){ + if(sign!=b.sign){ sign*=-1; *this-=b; sign*=-1; return *this; } + used=min(max(b.used,used)+1 , NUM); base_t carry=0; + for(int i=0;iused && inner[j-1]!=0) used=j; + return *this; + } + bigint operator+(base_t b){ bigint r(*this); return r+=b; } + + bigint& operator-=(const bigint& b){ + if(b.sign!=sign){ sign*=-1; *this+=b; sign*=-1; return *this; } + base_t carry=0, buf[NUM], *M=inner; base_t const *m=b.inner; + if(usedb.used)? (m=&b,this) : (m=this,&b); + for(unsigned i=0;iused; i++) + for((j=0),carry=0; j<=(min(M->used,max(0U,NUM-i-1))) ; j++){ + carry=(buf[j+i]=(M->inner[j]*(m->inner[i]))+carry)/BASE; + buf[j+i]%=BASE; + } + used=min(used+b.used,NUM); sign*=b.sign; + memcpy(inner,buf,used*sizeof(base_t)); + while(used && !inner[used-1]) --used; + return *this; + } + bigint operator*(bigint b) const{ return b*=*this; } + + + bigint& operator*=(base_t b){ + int j=1; base_t carry=(inner[0]*=b)/BASE; inner[0]%=BASE; + while(carry!=0){carry=(inner[j]+=carry)/BASE; inner[j++]%=BASE;} + if(j>used && inner[j-1]!=0) used=j; + return *this; + } + bigint operator*(base_t b) const{ bigint r(*this); return r*=b; } + + + bigint operator/(base_t d) const{ return div(d).first;} + base_t operator%(base_t d) const{ return div(d).second;} + + pair div (base_t d) const{ + if(d r(*this,0); + for(int i=used-1;i>=0;i--){ + r.second=(r.first.inner[i]+=BASE*r.second)%BASE; + r.first.inner[i]/=BASE; + } + while(r.first.used && !r.first.inner[r.first.used-1]) + --r.first.used; + return r; + } + + string str() const{ + stringstream ss; + ss << sign*inner[used-1]; + for(int i=used-2;i>=0;i--) + ss<<"00000000"+(int)(inner[i] ? floor(log10(inner[i])) : 0) + << inner[i]; + return ss.str(); + } + + char sign; //sign bit + unsigned used; //number of base_t in used + base_t inner[NUM]; //base array +}; + +bigint operator+(long long int i, const bigint& l){ return l+i; } +bigint operator*(long long int i,const bigint& l){ return l*i; } +bigint operator-(long long int i,const bigint& l){ return bigint(i)-l;} + +pair v[1001]; + +int main() { + v[0].first = bigint(1); + v[0].second = bigint(0); + v[1].first = bigint(1); + v[1].second = bigint(1); + for (int i = 2; i <= 1000; i++) { + v[i].first = bigint(); + v[i].second = bigint(); + if (i%2 == 0) { + v[i].first = v[i-1].first + v[i-1].second; + v[i].second = v[i-1].second; + } else { + v[i].first = v[i-1].first; + v[i].second = v[i-1].second + v[i-1].first; + } + } + int n; + while (scanf("%d", &n) != EOF) { + cout << (v[n].first + v[n].second).str() << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10334/10334-20.cpp b/uva_cpp_clean/10334/10334-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ec2a358cf30efeb93ead1c2dbd7158638fdf02a --- /dev/null +++ b/uva_cpp_clean/10334/10334-20.cpp @@ -0,0 +1,31 @@ +#include + +int fibonacci[1005][500]; + +int main() +{ + fibonacci[0][0] = 1, fibonacci[1][0] = 2; + for(int i=2 ; i<1005 ; i++) + { + for(int j=0 ; j<500 ; j++) fibonacci[i][j] = fibonacci[i-1][j]+fibonacci[i-2][j]; + + for(int j=0 ; j<500 ; j++) + { + fibonacci[i][j+1] += fibonacci[i][j]/10; + fibonacci[i][j] %= 10; + } + } + + int n; + + while( scanf("%d", &n)==1 ) + { + int pos; + for(pos=500-1 ; pos>0 ; pos--) + if( fibonacci[n][pos]!=0 ) break; + + for( ; pos>=0 ; pos--) printf("%d", fibonacci[n][pos]); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10334/10334-21.cpp b/uva_cpp_clean/10334/10334-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12b6696ea3633de1dfceb5d6fcba8104c5f91953 --- /dev/null +++ b/uva_cpp_clean/10334/10334-21.cpp @@ -0,0 +1,254 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10334 + Name: Ray Through Glasses + Problem: https://onlinejudge.org/external/103/10334.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + memset(ncells+size, 0, (capacity-size)*CellSize); + + if (cells) delete [] cells; + cells = ncells; + } + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + BigInt(const BigInt& o): cells(0), capacity(0) { + sign = o.sign; + setSize(o.size, 0); + for (int i=0; i= 0) + delete [] cells; + } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { return sign==o.sign ? numLess(o) : sign < o.sign; } + bool operator == (const BigInt &o) const { return sign==o.sign ? numEq(o) : 0; } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + setSize(max(size, o.size)+1); + for (int i=0; i= MAX_C ) { + cells[i] -= MAX_C; + cells[i+1]++; + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + +int main(){ + BigInt outs[1005]; + outs[0] = BigInt(0); + outs[1] = BigInt(1); + for (int i=2; i<1005; i++) + outs[i] = outs[i-1] + outs[i-2]; + + int n; + while(cin>>n) + cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10336 + Name: Rank the Languages + Problem: https://onlinejudge.org/external/103/10336.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +int n, m; +char mp[1000][1000]; +bool seen[1000][1000]; + +void dfs(int x, int y, char ch) { + if (seen[x][y] || x<0 || y<0 || x>=n || y>=m || mp[x][y]!=ch) return; + seen[x][y]=true; + dfs(x+1, y, ch); + dfs(x, y+1, ch); + dfs(x-1, y, ch); + dfs(x, y-1, ch); +} + +struct WD { + char ch; + int cnt; + + WD(char ch, int c): ch(ch), cnt(c) {} + bool operator < (const WD &o) const { + if (cnt==o.cnt) + return cho.cnt; + } +}; + + +int cnt[26]; +int main(){ + int T; + cin>>T; + for (int cse=1; cse<=T; cse++) { + memset(cnt, 0, sizeof(cnt)); + + cin>>n>>m; + for (int i=0; i>mp[i]; + for (int j=0; j items; + for (int i=0; i<26; i++) + if (cnt[i]) + items.push_back(WD(i+'a', cnt[i])); + + sort(items.begin(), items.end()); + + printf("World #%d\n", cse); + for (const WD& w: items) + printf("%c: %d\n", w.ch, w.cnt); + } +} diff --git a/uva_cpp_clean/10336/10336-6.cc b/uva_cpp_clean/10336/10336-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..943dd92f1f584e0cf5522c3fc4a448c3334bc175 --- /dev/null +++ b/uva_cpp_clean/10336/10336-6.cc @@ -0,0 +1,49 @@ +// https://uva.onlinejudge.org/external/103/10336.pdf +#include +using namespace std; +using ii=tuple; +using vii=vector; +using vi=vector; +using vs=vector; +using vb=vector; +using vvb=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t,n,m; + cin>>t; + for(int T=1;T<=t;T++){ + cin>>n>>m; + vs a(n); + for(int i=0;i>a[i]; + vvb s(n,vb(m)); + functiondfs=[&](int y,int x){ + s[y][x]=1; + int r[]={-1,0,1,0}; + int c[]={0,1,0,-1}; + for(int i=0;i<4;i++){ + int u=y+r[i],v=x+c[i]; + if(u>=0&&u=0&&v + +#define SIZE 1000 +#define LIM 30 + +using namespace std; + +struct State{ + int ct; + char name; + State(int _ct, char _name): + ct(_ct), name(_name) {}; + bool operator < ( const State& other) const{ + if( name != other.name ) return ( ct > other.ct ); + return ( name < other.name ); + } +}; + +int tc, n, m, v[LIM]; +char grid[SIZE][SIZE]; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < n and C < m ); +} + +void floodfill(int r, int c, char ch){ + if( not is_possible(r, c) ) return; + if( grid[r][c] != ch ) return; + grid[r][c] = '.'; + for(int d = 0; d < 4; d++) + floodfill(r + dr[d], c + dc[d], ch); +} + +void solve(int t){ + memset(v, 0, sizeof v); + for(int row = 0; row < n; row++) + for(int col = 0; col < m; col++) + if( grid[row][col] != '.' ) + v[grid[row][col] - 'a']++, floodfill(row, col, grid[row][col]); + vector < State > ans; + for(int it = 0; it < LIM; it++) + if( v[it] ) ans.push_back( State( v[it], it + 'a' ) ); + sort(ans.begin(), ans.end()); + printf("World #%d\n", t); + for(int it = 0; it < ans.size(); it++) + printf("%c: %d\n", ans[it].name, ans[it].ct); +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d %d\n", &n, &m); + for(int row = 0; row < n; row++) scanf("%s", grid[row]); + solve(t); + } + return(0); +} diff --git a/uva_cpp_clean/10337/10337-19.cpp b/uva_cpp_clean/10337/10337-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66c6a9f3782a75d366a0dde80c67ede92be3565b --- /dev/null +++ b/uva_cpp_clean/10337/10337-19.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +int grid[10][1001], X; +int cost[10][1001]; + +void proc(int x, int h) { + if (cost[h][x] != -1) return; + cost[h][x] = 90000; + if (x == X - 1) { + cost[0][x] = 30 - grid[0][x]; + cost[1][x] = 20 - grid[1][x]; + return; + } + if (h < 9 && h < X-x-1) { + proc(x + 1, h + 1); + cost[h][x] = 60 - grid[h][x] + cost[h+1][x+1]; + } + int price; + if (h <= X-x-1) { + proc(x + 1, h); + price = 30 - grid[h][x] + cost[h][x+1]; + cost[h][x] = min(price, cost[h][x]); + } + if (h > 0) { + proc(x + 1, h - 1); + price = 20 - grid[h][x] + cost[h-1][x+1]; + cost[h][x] = min(price, cost[h][x]); + } +} + +int main() { + int N; + cin >> N; + while (N--) { + cin >> X; + X /= 100; + for (int i = 9; i >= 0; i--) { + for (int j = 0; j < X; j++) { + cin >> grid[i][j]; + cost[i][j] = -1; + } + } + proc(0, 0); + cout << cost[0][0] << endl << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10337/10337-21.cpp b/uva_cpp_clean/10337/10337-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1536de66fe07668773af8ba5cbf8a7c3e740075b --- /dev/null +++ b/uva_cpp_clean/10337/10337-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10337 + Name: Flight Planner + Problem: https://onlinejudge.org/external/103/10337.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define INF 1073741824 +int X[1002][12], DP[1002][12]; +int min(int a, int b, int c) { + return a=0; j--) + for (int i=0; i +#include +#define min3(a, b, c) ((a) < (b)? min(a, c) : min(b, c)) +#define INF (int)0x7f7f7f7f +using namespace std; + +int N; // voar da posição (0, 0) até (0, N) +int windStrengths[10][1010]; // vento em (h, n) + +int memo[10][1010]; +int dp(int h, int n) { + // posiçao atual n, altura h + + if (h < 0 || h > 9) return INF; // inválido + if (n == N) return (h == 0)? 0: INF; + + int &ans = memo[h][n]; + if (ans != -1) return ans; + + return ans = 0 - windStrengths[h][n] + min3( + 60 + dp(h+1, n+1), // climb + 30 + dp(h, n+1), // hold + 20 + dp(h-1, n+1) // sink + ); +} + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int T; cin >> T; + while (T--) { + cin >> N; N /= 100; + + for (int h = 9; h >= 0; h--) + for (int n = 0; n < N; n++) + cin >> windStrengths[h][n]; + + memset(memo, -1, sizeof memo); + cout << dp(0, 0) << endl << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10337/10337-9.cpp b/uva_cpp_clean/10337/10337-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..235a3037f456d2ce6e34edf1e1ac492f8f932f41 --- /dev/null +++ b/uva_cpp_clean/10337/10337-9.cpp @@ -0,0 +1,29 @@ +#include + +#define MAX_X 1010 +#define INF (int)1e8 + +using namespace std; + +int tc,X,v[15][MAX_X],memo[15][MAX_X]; + +int dp(int x,int y){ + if(x<0 || x>9) return INF; + if(y==X && x==0) return 0; + if(y==X && x!=0) return INF; + if(~memo[x][y]) return memo[x][y]; + return memo[x][y] = min(60-v[x][y]+dp(x+1,y+1),min(30-v[x][y]+dp(x,y+1),20-v[x][y]+dp(x-1,y+1))); +} + +int main(){ + scanf("%d",&tc); + while(tc--){ + memset(memo,-1,sizeof memo); + scanf("%d",&X); + X /= 100; + for(int i=9;i>=0;i--) + for(int j=0;j +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,0,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc; + int aux, cont[26]; + ull N; + string str; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + cin >> str; + SET(cont); + int sz = str.size(); + for (int i = 0; i < sz; i++) { + cont[str[i] - 'A']++; + } + + N = 1; + aux = 0; + + for (int i = 0; i < 26; i++) { + for (int j = 0; j < cont[i]; j++) { + N = N * (aux + 1) / (1 + j); + aux++; + } + } + + cout << "Data set " << t << ": " << N << endl; + } + return 0; +} + diff --git a/uva_cpp_clean/10338/10338-20.cpp b/uva_cpp_clean/10338/10338-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09d1436c1802beb126f8dde163450c8c7a933e5b --- /dev/null +++ b/uva_cpp_clean/10338/10338-20.cpp @@ -0,0 +1,40 @@ +#include +#include + +int char_count[30]; + +unsigned long long factorial(int num) +{ + unsigned long long fact = 1; + for(int i=2 ; i<=num ; i++) + fact *= i; + + return fact; +} + +int main() +{ + int tc, ncase; + char dump; + + scanf("%d%c", &tc, &dump); + ncase = 0; + while( tc-- ) + { + char word[25]; + gets(word); + + for(int i=0 ; i<30 ; i++) char_count[i] = 0; + + int len = strlen(word); + for(int i=0 ; i='A' && word[i]<='Z' ) char_count[word[i]-'A']++; + + unsigned long long ans = factorial(len); + for(int i=0 ; i<26 ; i++) + if( char_count[i]>=2 ) ans /= factorial(char_count[i]); + + printf("Data set %d: %llu\n", ++ncase, ans); + } + return 0; +} diff --git a/uva_cpp_clean/10338/10338-21.cpp b/uva_cpp_clean/10338/10338-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95ff8b37feb4fa4ac5d6de4ae40f550d8f2e06dd --- /dev/null +++ b/uva_cpp_clean/10338/10338-21.cpp @@ -0,0 +1,65 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10338 + Name: Mischievous Children + Problem: https://onlinejudge.org/external/103/10338.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + + +int P[] = {2, 3, 5, 7, 11, 13, 17, 19}; +int F[22][8]; + +unsigned long long int numberize(int r[]) { + unsigned long long result = 1; + for (int i=0; i<8; i++) + if (r[i]) + result *= pow(P[i], r[i]); + return result; +} + +int main(){ + for (int i=2; i<=20; i++) { + int k = i; + for (int j=0; j<8; j++) + for (F[i][j]=F[i-1][j]; k%P[j]==0; k/=P[j]) + F[i][j]++; + } + + int T; + cin>>T; + for (int cse = 1; cse<=T; cse++) { + char word[22]; + cin>>word; + + int res[8], + len = strlen(word); + sort(word, word+len); + + for (int i=0; i<8; i++) + res[i] = F[len][i]; + + int c = 1, i=1; + do { + if (word[i] == word[i-1]) c++; + else if (c>1) { + for (int j=0; j<8 && P[j]<=c; j++) + res[j] -= F[c][j]; + c = 1; + } + } while (word[i++]); + + printf("Data set %d: %llu\n", cse, numberize(res)); + } +} diff --git a/uva_cpp_clean/10338/10338-9.cpp b/uva_cpp_clean/10338/10338-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..caac72f9d466947f260ea892a200ba14ccc51b35 --- /dev/null +++ b/uva_cpp_clean/10338/10338-9.cpp @@ -0,0 +1,13 @@ +import collections +import math + +def solution(): + for tc in range(1, 1 + int(input())): + s = input() + ans = math.factorial(len(s)) + for it in collections.Counter(s).values(): + ans //= math.factorial(it) + print("Data set %d: %d" %(tc, ans)) + +if __name__=="__main__": + solution() diff --git a/uva_cpp_clean/10338/main.cpp b/uva_cpp_clean/10338/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b272b56b543a720861522cb4f80e09ace8749267 --- /dev/null +++ b/uva_cpp_clean/10338/main.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +using namespace std; + +int main(){ +int n; +string palabra; +cin >> n; +for (int i = 1; i <= n; i++){ + map dic; + dic.insert(pair ('A',0)); + dic.insert(pair ('B',0)); + dic.insert(pair ('C',0)); + dic.insert(pair ('D',0)); + dic.insert(pair ('E',0)); + dic.insert(pair ('F',0)); + dic.insert(pair ('G',0)); + dic.insert(pair ('H',0)); + dic.insert(pair ('I',0)); + dic.insert(pair ('J',0)); + dic.insert(pair ('K',0)); + dic.insert(pair ('L',0)); + dic.insert(pair ('M',0)); + dic.insert(pair ('N',0)); + dic.insert(pair ('O',0)); + dic.insert(pair ('P',0)); + dic.insert(pair ('Q',0)); + dic.insert(pair ('R',0)); + dic.insert(pair ('S',0)); + dic.insert(pair ('T',0)); + dic.insert(pair ('U',0)); + dic.insert(pair ('V',0)); + dic.insert(pair ('W',0)); + dic.insert(pair ('X',0)); + dic.insert(pair ('Y',0)); + dic.insert(pair ('Z',0)); + cin >> palabra; + unsigned long total = 1; + int lon = 0; + for(int c = 0; c < palabra.size(); c++){ + dic[palabra[c]]++; + lon++; + total = (total*lon)/dic[palabra[c]]; + } + cout <<"Data set "< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + string A, B; + + while (cin >> A >> B) { + bool f = 0; + + for (int i = 0, j = 0; j < (int) B.size();) { + if (A[i] == B[j]) { + i++, j++; + + } else { + j++; + } + + if (i == (int) A.size() ) { + cout << "Yes\n"; + f = 1; + break; + } + } + + if (!f) cout << "No\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/10340/10340-18.cpp b/uva_cpp_clean/10340/10340-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2a822339982bcc8c05921a7d543099ef0895da5c --- /dev/null +++ b/uva_cpp_clean/10340/10340-18.cpp @@ -0,0 +1,18 @@ +#include +#include + +using namespace std; + +int main(){ + string s, t; + int cont; + while(cin >> s >> t){ + cont = 0; + for(int i = 0; t[i] && cont < s.size(); i++){ + if(s[cont] == t[i]) cont++; + } + if(cont == s.size()) printf("Yes\n"); + else printf("No\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10340/10340-19.cpp b/uva_cpp_clean/10340/10340-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e01efb190d0a0a731baf61cfb94aba3779be02c3 --- /dev/null +++ b/uva_cpp_clean/10340/10340-19.cpp @@ -0,0 +1,15 @@ +#include +#include +using namespace std; + +int main() { + string s, t; + while (cin>>s>>t) { + int j = 0; + for (int i = 0; i < t.size() && j < s.size(); i++){ + if (t[i] == s[j]) j++; + } + cout << (j == s.size()? "Yes": "No") << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10340/10340-20.cpp b/uva_cpp_clean/10340/10340-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..592bf1e10c4e71a2eaf4de92aa5378ff1ff9f1a1 --- /dev/null +++ b/uva_cpp_clean/10340/10340-20.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + char str1[100000], str2[100000]; + + while( cin >> str1 >> str2 ) + { + int index = 0; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10340 + Name: All in All + Problem: https://onlinejudge.org/external/103/10340.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; +int main() +{ + char s[1000000],t[1000000]; + long slen,tlen,i,j; + while (cin>>s>>t) + { + slen=strlen(s);tlen=strlen(t); + i=j=0; + for(;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long lenong +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + long int t,j,i,sum,l1,len,p,l2,d; + char a[100000],b[100000],c[100000]; + while(gets(c)) + { + l2=0; + sum=0; + l1=-1; + len=strlen(c); + for(i=0;i +#include +using namespace std; + +int main(){ + string palabra, word; + bool esta; + while(cin >> palabra >> word){ + esta = true; + int ind = 0; + int tam = palabra.size(); + int len = word.size(); + for(int i = 0; i < tam && esta; i++){ + char caracter = palabra[i]; + for(int c = ind; c +/* +Problem: 10341 - Solve It +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1282 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +#define EPS 1e-15 +#define MAX_ITER 1e+6 + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int P, Q, R, S, T, U; + +double f(double x); +double fd(double x); +double bisection(double l, double r, double default_value); + +int main (){ + + while ( scanf("%d%d%d%d%d%d", &P, &Q, &R, &S, &T, &U) != EOF ){ + + double ans = bisection(0, 1, -1); + if (ans == -1) {puts("No solution"); continue; } + + printf("%.4lf\n", ans); + } + + return 0; +} + +double bisection(double l, double r, double default_value){ + // https://www.youtube.com/watch?v=mztGjg04rV8 + + if (f(l) * f(r) > 0) return default_value; // No solution + + double mid; + while ( l+EPS < r ){ + mid = (r+l)/2; + + if (f(mid) <= EPS) r = mid; + else l = mid; + } + + return mid; +} + +double f(double x){ + return P*exp(-x) + Q*sin(x) + R*cos(x) + S*tan(x) + T*(x*x) + U; +} + +double fd(double x){ + return -P*exp(-x) + Q*cos(x) - R*sin(x) + S/(cos(x)*cos(x)) + 2*T*x; +} + +/* +Sample input:- +----------------- +0 0 0 0 -2 1 +1 0 0 0 -1 2 +1 -1 1 -1 -1 1 + +Sample output:- +----------------- +0.7071 +No solution +0.7554 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10341/10341-13.cpp b/uva_cpp_clean/10341/10341-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7993571e9a93bf7f4e8dc3bec51c6aaf7556ecac --- /dev/null +++ b/uva_cpp_clean/10341/10341-13.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +using namespace std; + +#define EPS 0.00000001 + +double p, q, r, s, t, u; + +double function(double x) +{ + double ans = (p * exp(-x)) + (q * sin(x)) + (r * cos(x)) + (s * tan(x)) + (t * x * x) + u; + + return ans; +} + +double binarySearch() +{ + double low = 0.0, high = 1.0, mid; + + while (low + EPS < high) + { + mid = (low + high) / 2.0; + + double f0, f1; + + f0 = function(low); + + f1 = function(mid); + + if (f0 * f1 <= 0) high = mid; + + else low = mid; + } + + return (low + high) / 2.0; +} + +int main() +{ + while (cin >> p >> q >> r >> s >> t >> u) + { + double f0, f1; + + f0 = function(0.0); + + f1 = function(1.0); + + if (f0 * f1 > 0) cout << "No solution" << endl; + + else printf("%.4lf\n", binarySearch()); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10341/10341-14.cpp b/uva_cpp_clean/10341/10341-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4f53801e0c76762af226684108c5e1db1bdcde41 --- /dev/null +++ b/uva_cpp_clean/10341/10341-14.cpp @@ -0,0 +1,59 @@ +/*************************************************** + * Problem name : 10341 - Solve if.cpp + * OJ : Uva + * Result : AC + * Date : 30-03-17 + * Problem Type : Binary Search + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 10000 +using namespace std; +typedef long long ll; +double p, q, r, s, t, u, E; +double fun(double mid ) { + return (p * exp(-1 * mid) + q * sin(mid) + r * cos(mid) + s * tan(mid) + t * mid * mid + u); +} +double BinarySearch() { + double high = 1.1; + double low = 0.0; + double ans ; + for (int i = 0; i < 200; i++) { + double mid = (high + low) / 2.0; + E = fun(mid); + if (E >= 0.0000001) { + ans = mid; + low = mid; + } else { + high = mid; + } + } + return ans; +} +int main () { + while (scanf("%lf %lf %lf %lf %lf %lf", &p, &q, &r, &s, &t, &u) == 6) { + double ans = BinarySearch(); + if (fun(1) > 0.0000000001 || (p + r + u) < 0) { + printf("No solution\n"); + continue; + } else if (ans >= 0.0 && ans <= 1.0) { + printf("%.4lf\n", ans); + } else { + printf("No solution\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/10341/10341-19.cpp b/uva_cpp_clean/10341/10341-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..671ca1b939e623ba41aa7ae6dcc8a6f5211774b7 --- /dev/null +++ b/uva_cpp_clean/10341/10341-19.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +int p, q, r, s, t, u; + +long double func(long double x) { + return p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u; +} + +bool ss(long double fa, long double fb) { + return (fa > 0 && fb > 0) || (fa < 0 && fb < 0); +} + +bool zero(long double f) { + return fabs(f) < 1e-9; +} + +int main() { + while (cin >> p >> q >> r >> s >> t >> u) { + long double a = 0, b = 1; + while (true) { + long double mid = (a + b)/2; + if (fabs(b - a) < 1e-9) { + + printf("%.4Lf\n", mid); + break; + } + long double fa = func(a), fb = func(b), fm = func(mid); + if (ss(fa, fb)) { + printf("No solution\n"); + break; + } + if (zero(fm)) { + printf("%.4Lf\n", mid); + break; + } + else if (ss(fa, fm)) a = mid; + else b = mid; + } + } + return 0; +} diff --git a/uva_cpp_clean/10341/10341-21.cpp b/uva_cpp_clean/10341/10341-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2f9b72cf95d4cbbb4ba29a35762dadb719c1ee4 --- /dev/null +++ b/uva_cpp_clean/10341/10341-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10341 + Name: Solve It + Problem: https://onlinejudge.org/external/103/10341.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int p,q,r,s,t,u; +double f(double x) { + return p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u; +} + +int main(){ + double mn, mx, mid, fmn, fmx, fmid; + + while(cin>>p>>q>>r>>s>>t>>u) { + fmn = f(mn = 0); + fmx = f(mx = 1); + + if (fmn * fmx <= 0) { + for (int i=0; i<32; i++) { + fmid = f(mid = (mn+mx)/2); + + if (fmn * fmid > 0) { + mn = mid; + fmn = fmid; + } + else { + mx = mid; + fmx = fmid; + } + } + + printf("%.4f\n", mid); + } + else cout << "No solution\n"; + } +} diff --git a/uva_cpp_clean/10341/10341-9.cpp b/uva_cpp_clean/10341/10341-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e811113ed5fb7341e4ec7075791b2c929478877a --- /dev/null +++ b/uva_cpp_clean/10341/10341-9.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int p, q, r, s, t, u; +double lo, hi, mid; + +double solve(double x){ + return p * exp(-x) + q * sin(x) + r * cos(x) + s * tan(x) + t * x * x + u; +} + +int main(){ + while(scanf("%d %d %d %d %d %d", &p, &q, &r, &s, &t, &u)==6){ + lo = 0.0, hi = 1.0; + if(solve(0.0) * solve(1.0) > 0){ + printf("No solution\n"); + continue; + } + for(int it = 0; it < 40; it++){ + mid = (lo + hi) / 2.0; + if(solve(lo) * solve(mid) > 0.0) lo = mid; + else hi = mid; + } + printf("%.4f\n", mid); + } + return(0); +} diff --git a/uva_cpp_clean/10342/10342-21.cpp b/uva_cpp_clean/10342/10342-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fba233f165db858c525cd16987c65dec4101ee28 --- /dev/null +++ b/uva_cpp_clean/10342/10342-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10342 + Name: Always Late + Problem: https://onlinejudge.org/external/103/10342.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + +struct Edge { + int u, w; + Edge(int u, int w):u(u),w(w){} + bool operator < (const Edge &o) const { return w > o.w; } +}; + +int D1[143][143], D2[143][143], n; +vector adj[143]; +void dijkstra(int src, int D1[], int D2[]) { + for (int i=0; i q; + q.push(Edge(src, D1[src]=0)); + while (!q.empty()) { + Edge s = q.top(); q.pop(); + if (s.w > D2[s.u]) continue; + + for (Edge e: adj[s.u]) { + Edge t(e.u, s.w + e.w); + if (D2[t.u] > t.w && t.w != D1[t.u]) { + D2[t.u] = t.w; + if (t.w < D1[t.u]) + swap(D1[t.u], D2[t.u]); + + q.push(t); + } + } + } +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int m, w; + bool seen[143]; + for (int cse=1; cin >> n >> m; ++cse) { + cout << "Set #" << cse << '\n'; + + for (int i=0; i> u >> v >> w; + adj[u].push_back(Edge(v, w)); + adj[v].push_back(Edge(u, w)); + } + + cin >> m; + for (int u,v; m--; ) { + cin >> u >> v; + if (!seen[u]) + dijkstra(u, D1[u], D2[u]); + + if (D2[u][v] == INF) cout << "?\n"; + else cout << D2[u][v] << '\n'; + } + } +} diff --git a/uva_cpp_clean/10344/10344-21.cpp b/uva_cpp_clean/10344/10344-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9bc111a12d04e326de2ff25b12ce0262f420442c --- /dev/null +++ b/uva_cpp_clean/10344/10344-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10344 + Name: 23 out of 5 + Problem: https://onlinejudge.org/external/103/10344.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int a[5]; +int used[5]; +bool BT(int i, int n) +{ + if(i==5) + return (n==23); + + for(int j=0; j<5; j++) + if(!used[j]) + { + used[j]=1; + if(BT(i+1,n+a[j]))return true; + if(i && BT(i+1,n-a[j]))return true; + if(i && BT(i+1,n*a[j]))return true; + used[j]=0; + } + + return false; +} + +int main() +{ + while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4] && (a[0]||a[1]||a[2]||a[3]||a[4])) + { + memset(used,0,sizeof(used)); + if(BT(0,0)) + puts("Possible"); + else + puts("Impossible"); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10344/10344-6.cpp b/uva_cpp_clean/10344/10344-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b098f741bbff8b446fa6a5da7d86a501eb21cf3 --- /dev/null +++ b/uva_cpp_clean/10344/10344-6.cpp @@ -0,0 +1,35 @@ +/* + brute force > recursive backtracking + difficulty: easy + date: 19/Mar/2020 + problem: check if some arithmetic expression of 5 given numbers will result in 23 + hint: check all combination of operators for each permutation of the given numbers + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int a[5]; + +bool bt(int acc, int i) { + // valor total acc, operando-o com a[i] + + if (i == 5) return acc == 23; + return bt(acc+a[i], i+1) || bt(acc-a[i], i+1) || bt(acc*a[i], i+1); +} + +int main() { + while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] && (a[0] || a[1] || a[2] || a[3] || a[4])) { + bool ok = false; + + sort(a, a+5); + do { + if (ok = (ok || bt(a[0], 1))) break; + } while (next_permutation(a, a+5)); + + cout << (ok? "Possible" : "Impossible") << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10344/10344-9.cpp b/uva_cpp_clean/10344/10344-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85ae475ef48e614ba1aa2ec970bb99034c5647f5 --- /dev/null +++ b/uva_cpp_clean/10344/10344-9.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +#define DIGIT 215 + +int n, tmp; +int fib[1003][DIGIT + 1]; + +int main(){ + fib[1][DIGIT] = 1; + for(int i = 2; i <= 1002; i++){ + for(int j = DIGIT; j > 0; j--){ + fib[i][j] += fib[i - 1][j] + fib[i - 2][j]; + if(fib[i][j] > 9) + fib[i][j - 1] += 1, fib[i][j] %= 10; + } + } + while(scanf("%d",&n) == 1){ + n += 2; + for(int i = 0; i <= DIGIT; i++){ + if(fib[n][i] != 0){ + while(i <= DIGIT) + printf("%d", fib[n][i++]); + } + } + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10346/10346-11.cpp b/uva_cpp_clean/10346/10346-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ead5ca00912dacf9d3343415aeb9329e3b047ed --- /dev/null +++ b/uva_cpp_clean/10346/10346-11.cpp @@ -0,0 +1,55 @@ +#include +/* +Problem: 10346 - Peter's Smokes +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1287 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N, K, cigarettes; + +int main () { + + while ( scanf("%d%d", &N, &K) != EOF ){ + + cigarettes = N; + + while ( N/K ){ + cigarettes += N/K; + N = N/K + N%K; + } + + printf("%d\n", cigarettes); + } + + return 0; +} +/* +Sample input:- +----------------- +4 3 +10 3 +100 5 + +Sample output:- +----------------- +5 +14 +124 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10346/10346-13.cpp b/uva_cpp_clean/10346/10346-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87e5cef9c21630fa883bd6a4cec2ed0873413b28 --- /dev/null +++ b/uva_cpp_clean/10346/10346-13.cpp @@ -0,0 +1,43 @@ +#include +#include + +using namespace std; + +int main() +{ + queue list; + + int n, m, r = 0, sum = 0; + + while (cin >> n >> m) + { + sum += n; + + while (n >= m) + { + while (n > 0) + { + r += n % m; + + n /= m; + + sum += n; + } + + n = r; + + r = 0; + } + + list.push(sum); + + sum = m = n = r = 0; + } + + while (!list.empty()) + { + cout << list.front() << endl; + + list.pop(); + } +} diff --git a/uva_cpp_clean/10346/10346-19.cpp b/uva_cpp_clean/10346/10346-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc9e7fa0969f4df98a17a23b853aa70630df4ad6 --- /dev/null +++ b/uva_cpp_clean/10346/10346-19.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int main() { + int n, k; + while (scanf("%d %d", &n, &k) != EOF) { + int sum = n; + while (n >= k) { + sum += n/k; + n = n/k + n%k; + } + printf("%d\n", sum); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10346/10346-21.cpp b/uva_cpp_clean/10346/10346-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a8e67874897c52a9dc4bb30415d4785f3b900c9 --- /dev/null +++ b/uva_cpp_clean/10346/10346-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10346 + Name: Peter's Smokes + Problem: https://onlinejudge.org/external/103/10346.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int n, k; + + while (cin >> n >> k) { + int c = n; + while (n >= k) { + c += n / k; + n = n % k + n / k; + } + cout << c << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10346/10346-5.cpp b/uva_cpp_clean/10346/10346-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3b9d0abd25390157c4c0f25264bc7a50a3c33842 --- /dev/null +++ b/uva_cpp_clean/10346/10346-5.cpp @@ -0,0 +1,16 @@ +#include +int main() +{ +int a,n,k; +while(scanf("%d %d",&n,&k)==2 && k>1) +{ +a=n; +while(n>=k) +{ +a=a+(n/k); +n=(n/k)+(n%k); +} +printf("%d\n",a); +} +return 0; +} diff --git a/uva_cpp_clean/10346/10346-6.cpp b/uva_cpp_clean/10346/10346-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94a34570dcdd5a6d029a9832fc98a33b0d5091de --- /dev/null +++ b/uva_cpp_clean/10346/10346-6.cpp @@ -0,0 +1,25 @@ +/* + math > ad-hoc + difficulty: easy + date: 14/Feb/2020 + by: @brpapa +*/ +#include +using namespace std; + +int main() { + int N, K; + while (cin >> N >> K) { + int toSmoke = N, smoked = 0, butts = 0; + + while (toSmoke > 0) { + smoked += toSmoke; + butts += toSmoke; + + toSmoke = butts/K; + butts %= K; + } + cout << smoked << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10346/10346-9.cpp b/uva_cpp_clean/10346/10346-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9126741fa21bdc60903bb2ef9f13d4faa2c7daf --- /dev/null +++ b/uva_cpp_clean/10346/10346-9.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main(){ + int n, k, ans; + while(scanf("%d %d", &n, &k) == 2){ + ans = n; + while(n / k) + ans += n / k, n = n / k + n % k; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10347/10347-19.cpp b/uva_cpp_clean/10347/10347-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c22a1578273355d87cde9424f5097b8b7205d1da --- /dev/null +++ b/uva_cpp_clean/10347/10347-19.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +const double eps=1e-9; + +int main() { + double ma, mb, mc, v; + while (cin >> ma >> mb >> mc) { + bool invalid = (ma <= 0 || mb <= 0 || mc <= 0); + if (!invalid) { + v = (ma + mb + mc)*(-ma + mb + mc)*(ma - mb + mc)*(ma + mb - mc); + invalid = (v <= 0); + } + if (invalid) printf("-1.000\n"); + else printf("%.3lf\n", (sqrt(v))/3); + } + return 0; +} diff --git a/uva_cpp_clean/10347/10347-20.cpp b/uva_cpp_clean/10347/10347-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bd3c4bdba398b28a21da7847a7209e1f87153f3 --- /dev/null +++ b/uva_cpp_clean/10347/10347-20.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + double a, b, c; + + while( cin >> a >> b >> c ) + { + double s = (a+b+c)/2.0; + double area = (4.0/3.0)*(sqrt(s*(s-a)*(s-b)*(s-c))); + + if( area>0.0 ) cout << fixed << setprecision(3) << area << endl; + else cout << "-1.000" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10347/10347-21.cpp b/uva_cpp_clean/10347/10347-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15bd19eb7d90aea3360ea456249d02a48568a9a1 --- /dev/null +++ b/uva_cpp_clean/10347/10347-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10347 + Name: Medians + Problem: https://onlinejudge.org/external/103/10347.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +double p2(double x) { + double t = x*x; + return t*t; +} + +double trarea(double x, double y, double m) { + x = 2*x/3; + y = 2*y/3; + m = m/3; + + if (x == y) { + if (m>=x) return -1; + return m*sqrt(x*x - m*m); + } + + double x2 = x*x; + double y2 = y*y; + double m2 = m*m; + + double t = (x2*x2-y2*y2)/(2*(x2-y2)); + if (t <= m2) return -1; + double c = 2 * sqrt(t - m2); + + if (x+y<=c || x+c<=y || y+c<=x) return -1; + double s = (x+y+c)/2; + return sqrt(s*(s-x)*(s-y)*(s-c)); +} + +double area(double a, double b, double c) { + if (a<=0 || b<=0 || c<=0) return -1; + + double t1 = trarea(a, b, c); + if (t1<0) return -1; + + double t2 = trarea(a, c, b); + if (t2<0) return -1; + + double t3 = trarea(b, c, a); + if (t3<0) return -1; + + double r = t1+t2+t3; + if (r < 0.0005) return -1; + return r; +} + +int main(){ + double a, b, c; + while (cin>>a>>b>>c) + printf("%.3f\n", area(a,b,c)); +} diff --git a/uva_cpp_clean/10347/10347-5.cpp b/uva_cpp_clean/10347/10347-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..689839c12df4adb00f5f38e9e07437a6dd4ad935 --- /dev/null +++ b/uva_cpp_clean/10347/10347-5.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + double a,b,c,n,s; + while(sc("%lf %lf %lf",&a,&b,&c)==3) + { + s=(a+b+c)/2; + n=(4.0/3.0)*(sqrt(s*(s-a)*(s-b)*(s-c))); + if(n>0) + pf("%.3lf\n",n); + else + pf("-1.000\n",n); + } + return 0; +} diff --git a/uva_cpp_clean/10349/10349-21.cpp b/uva_cpp_clean/10349/10349-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cb2af315db042386b07154f1ab9c900ac0068d8 --- /dev/null +++ b/uva_cpp_clean/10349/10349-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10349 + Name: Antenna Placement + Problem: https://onlinejudge.org/external/103/10349.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +vector adj[416]; +bool seen[416]; +int match[416]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + if (match[v]==-1 || bpm(match[v])) { + match[v] = u; + return true; + } + } + return false; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string M[43]; + int I[43][43], T, r, c; + cin >> T; + while (T-- && cin >> r >> c) { + for (int i=0; i> M[i]; + + int ln=0, rn=0; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10350 + Name: Liftless EME + Problem: https://onlinejudge.org/external/103/10350.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} +inline int readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return EOF; + + int l = 0; + str[l++] = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + str[l++] = ch; + str[l] = 0; + return l; +} + +// ----------------------------------------------------------------------------- + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char title[143]; + int DP[121][16]; + while (readStr(title)>0) { + cout << title << '\n'; + + int n = readUInt(), + m = readUInt(); + + for (int k=0; k>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10354 + Name: Avoiding Your Boss + Problem: https://onlinejudge.org/external/103/10354.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +struct State { + int u, d; + State(int u, int d):u(u),d(d){} + bool operator < (const State &o) const { return d > o.d; } +}; + +vector adj[143], P[143]; +int W[143][143], D[143], S[143], n; +int dijkstra(int src, int dst) { + if (S[src] || S[dst]) return -1; + for (int i=1; i<=n; ++i) { + P[i].clear(); + D[i] = INF; + } + + priority_queue q; + q.push(State(src, D[src]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.u]) continue; + if (s.u == dst) { + return s.d; + } + + for (int v: adj[s.u]) if (!S[v]) { + int d = s.d + W[s.u][v]; + if (d <= D[v]) { + if (d < D[v]) { + q.push(State(v, D[v]=d)); + P[v].clear(); + } + P[v].push_back(s.u); + } + } + } + return -1; +} + +void dfs(int v) { + S[v] = 1; + for (int u: P[v]) + if (!S[u]) + dfs(u); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int m, s1, d1, s2, d2; + while (cin>>n>>m>>s1>>d1>>s2>>d2) { + for (int i=1; i<=n; ++i) { + adj[i].clear(); + S[i] = 0; + } + + for (int i=0, u,v,d; i> u >> v >> d; + adj[u].push_back(v); + adj[v].push_back(u); + W[u][v] = W[v][u] = d; + } + + if (dijkstra(s1, d1) >= 0) + dfs(d1); + + int r = dijkstra(s2, d2); + if (r < 0) cout << "MISSION IMPOSSIBLE.\n"; + else cout << r << endl; + } +} diff --git a/uva_cpp_clean/10356/10356-21.cpp b/uva_cpp_clean/10356/10356-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fa80fc1c1367180417cd5754414255fcc53e46f --- /dev/null +++ b/uva_cpp_clean/10356/10356-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10356 + Name: Rough Roads + Problem: https://onlinejudge.org/external/103/10356.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + + +struct UD { + int u, d, l; + UD(int u, int d, int l=0):u(u),d(d),l(l){} + bool operator < (const UD& o) const { return d > o.d; } +}; +vector adj[10043]; +int D[10043][2]; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + + int n, m, u, v, d; + for (int cse=1; cin >> n >> m; ++cse) { + cout << "Set #" << cse << endl; + + for (int i=0; i> u >> v >> d; + adj[u].push_back(UD(v, d)); + adj[v].push_back(UD(u, d)); + } + + priority_queue q; + q.push(UD(0, D[0][0]=0)); + while (!q.empty()) { + UD ud = q.top(); q.pop(); + if (ud.d != D[ud.u][ud.l&1]) continue; + if (ud.u==n-1 && !(ud.l&1)) { + cout << ud.d << endl; + goto fin; + } + + + for (UD &vd: adj[ud.u]) { + UD t(vd.u, ud.d + vd.d, ud.l+1); + if (t.d < D[t.u][t.l&1]) { + D[t.u][t.l&1] = t.d; + q.push(t); + } + } + } + cout << "?\n"; + fin:; + } +} diff --git a/uva_cpp_clean/10357/10357-21.cpp b/uva_cpp_clean/10357/10357-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43619514d2b10f8418cae5a83247c5d731ce54fa --- /dev/null +++ b/uva_cpp_clean/10357/10357-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10357 + Name: Playball !!! + Problem: https://onlinejudge.org/external/103/10357.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(0); + + int pn, bn; + double X[23], Y[23], V[23], a,b,c,d,e,f,g, delta, t,x,y; + while (cin.ignore(10, '=') && cin>>pn) { + for (int i=0; i> X[i] >> Y[i] >> V[i]; + + cin.ignore(10, '='); + cin >> bn; + for (int cse=1; cse<=bn; ++cse) { + cin >> a >> b >> c >> d >> e >> f >> g; + + cout << "Ball " << cse << " was "; + + if (fabs(a) < 1e-16) t = -c/b; + else { + b /= a; c /= a; + delta = sqrt(b*b - 4*c); + t = ceil((-b + delta) / 2); + } + if (t < 0) t = 0; + x = d*t + e; + y = f*t + g; + + if (x<0 || y<0) cout << "foul"; + else { + bool safe = 1; + for (int i=0; safe && i +#include +#include +#include + +using namespace std; + +struct point +{ + int x; + int y; + int population; +}; + +int ansX, ansY, ansPo; +int TC, n, d, maximum; +vector < point > rats; +int killed[1200][1200]; + +int main() +{ + cin >> TC; + + while (TC--) + { + cin >> d; + + cin >> n; + + rats.clear(); + + maximum = 0; + + ansX = ansY = ansPo = 0; + + for (int i = 0; i < 1200; i++) + { + for (int j = 0; j < 1200; j++) + { + killed[i][j] = 0; + } + } + + for (int i = 0; i < n; i++) + { + point newRat; + + int x, y, population; + + cin >> x >> y >> population; + + x += 55; + y += 55; + + newRat.x = x; + newRat.y = y; + newRat.population = population; + + rats.push_back(newRat); + } + + for (int k = 0; k < (int)rats.size(); k++) + { + int x, y, pop; + + x = rats[k].x; + y = rats[k].y; + pop = rats[k].population; + + for (int i = -d; i <= d; i++) + { + for (int j = -d; j <= d; j++) + { + killed[i + x][j + y] += pop; + } + } + } + + for (int i = 55; i < 1080 ; i++) + { + for (int j = 55; j < 1080; j++) + { + if (killed[i][j] > ansPo) + { + ansX = i - 55; + ansY = j - 55; + ansPo = killed[i][j]; + } + } + } + + cout << ansX << " " << ansY << " " << ansPo << endl; + } + + return 0; +} diff --git a/uva_cpp_clean/10360/10360-18.cpp b/uva_cpp_clean/10360/10360-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2da3bd2d46f3b7f84dbf8c7b3db2d21ad8121945 --- /dev/null +++ b/uva_cpp_clean/10360/10360-18.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int manhattan[1025][1025], n, d, x, y, nest, tam, maxTam; + scanf("%d", &n); + while(n--){ + memset(manhattan, 0, sizeof(manhattan)); + scanf("%d %d", &d, &nest); + for(int i = 0; i < nest; i++){ + scanf("%d %d %d", &x, &y, &tam); + for(int j = max(0, y - d); j <= y + d && j < 1025; j++){ + for(int k = max(0, x - d); k <= x + d && k < 1025; k++) manhattan[j][k]+=tam; + } + } + maxTam = 0; + for(int i = 0; i < 1024; i++){ + for(int j = 0; j < 1024; j++){ + if(manhattan[j][i] > maxTam){ + maxTam = manhattan[j][i]; + x = i; + y = j; + } + } + } + printf("%d %d %d\n", x, y, maxTam); + } + return 0; +} +/* + Si bien se podria buscar casilla a casilla e ir sumando para ver cual ubicacion mata mas ratas, pero eso es muy + complejo y demoraria mucho, lo que hice fue que en cada ubicacion en donde colocaba el nido llenaba la matriz + de ese numero alrededor de esa ubicacion dependiendo el rango de la bomba, eso para simular que la bomba fue + puesta ahi, esto lo hago porque esos rangos se pueden intersectar con otros nidos, por lo que voy a buscar es + la ubicacion donde mas se halla intersectado. PD: buscar de columna en columna, por alguna razon si lo buscas + en fila en fila UVa manda WA. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10360/10360-21.cpp b/uva_cpp_clean/10360/10360-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f18e686c41775f78c2df511ef2f8717b2907ff2 --- /dev/null +++ b/uva_cpp_clean/10360/10360-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10360 + Name: Rat Attack + Problem: https://onlinejudge.org/external/103/10360.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 1026 +int f[MAXN][MAXN]; +int main() { + int T, d, r, x, y, p; + cin>>T; + while (T--) { + memset(f, 0, sizeof(f)); + cin>>d>>r; + + int maxp = 0, maxi=0, maxj=0; + + for (int i=0; i> x >> y >> p; + + int maxx = min(x+d, MAXN-1), + maxy = min(y+d, MAXN-1); + + for (int i=max(0, x-d); i<=maxx; i++) + for (int j=max(0, y-d); j<=maxy; j++) { + int t = (f[i][j] += p); + if (t >= maxp) + if (t > maxp || (i*MAXN+j < maxi*MAXN+j)) { + maxp = t; + maxi = i; + maxj = j; + } + } + } + + printf("%d %d %d\n", maxi, maxj, maxp); + } +} diff --git a/uva_cpp_clean/10360/10360-6.cpp b/uva_cpp_clean/10360/10360-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4432dce54ad299c6da572bfa75880ae5026f281f --- /dev/null +++ b/uva_cpp_clean/10360/10360-6.cpp @@ -0,0 +1,39 @@ +/* + brute force > iterative + difficulty: easy + date: 13/Jan/2020 + by: @brpapa +*/ +#include +#include +#define MAX 1025 +using namespace std; + +int killed[MAX][MAX]; // quantos ratos morreriam se a bomba estive na posicao + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int T; cin >> T; + while (T--) { + int D, N; cin >> D >> N; + memset(killed, 0, sizeof(killed)); + for (int n = 0; n < N; n++) { + int X, Y, P; cin >> X >> Y >> P; + + for (int x = max(X-D, 0); x <= min(X+D, MAX-1); x++) + for (int y = max(Y-D, 0); y <= min(Y+D, MAX-1); y++) + killed[x][y] += P; + } + + int maxKilled = -1, maxX, maxY; + for (int x = 0; x < MAX; x++) + for (int y = 0; y < MAX; y++) + if (killed[x][y] > maxKilled) { + maxKilled = killed[x][y]; + maxX = x; maxY = y; + } + cout << maxX << " " << maxY << " " << maxKilled << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10360/10360-9.cpp b/uva_cpp_clean/10360/10360-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..adc7080e59e021111fe10da4c99869e93d635f75 --- /dev/null +++ b/uva_cpp_clean/10360/10360-9.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +struct tri{ + int x, y, size; +}; + +int m[1100][1100], _x, _y, _max, d, n, tc; +tri v[20010]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + _max = -1, _x = _y = INT_MAX; + scanf("%d", &d); + scanf("%d", &n); + for(int i = 0; i < n; i++) scanf("%d %d %d", &v[i].x, &v[i].y, &v[i].size); + for(int i = 0; i < n; i++) + for(int p = max(1, v[i].x - d); p <= min(1025, v[i].x + d); p++) + for(int q = max(1, v[i].y - d); q <= min(1025, v[i].y + d); q++) + m[p][q] += v[i].size; + for(int i = 1025; i >= 1; i--) + for(int j = 1025; j >= 1; j--){ + if(m[i][j] >= _max) + _max = m[i][j], _x = i, _y = j; + m[i][j] = 0; + } + printf("%d %d %d\n", _x, _y, _max); + } + return(0); +} diff --git a/uva_cpp_clean/10361/10361-16.cpp b/uva_cpp_clean/10361/10361-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fecebf8e2779ff1bfe0c127861b72a431d93d99 --- /dev/null +++ b/uva_cpp_clean/10361/10361-16.cpp @@ -0,0 +1,52 @@ +//============================================================================ +// Name : 10361 - Automatic.cpp +// Author : ztrixack +// Copyright : MIT +// Description : 110361 Automatic in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +#include +#include +using namespace std; + +int main () { + int number; + int n, fst, sec; + string word, word1, word2; + string s2, s3, s4, s5; + cin >> number; + for (int i = 0; i < number; i++) { + word.clear(); + cin >> word1; + getline(cin, word); + word1 += word; + n = word1.length(); + fst = word1.find("<"); + word1.erase(fst, 1); + sec = word1.find(">"); + word1.erase(sec, 1); + s2.assign(word1, fst, sec - fst); + fst = word1.find("<"); + word1.erase(fst, 1); + s3.assign(word1, sec, fst - sec); + sec = word1.find(">"); + word1.erase(sec, 1); + s4.assign(word1, fst, sec - fst); + s5.assign(word1, sec, n - sec); + cout << word1 << endl; + word.clear(); + + cin >> word2; + getline(cin, word); + word2 += word; + fst = word2.find("..."); + word2.erase(fst, 3); + word2.insert(fst, s5); + word2.insert(fst, s2); + word2.insert(fst, s3); + word2.insert(fst, s4); + cout << word2 << endl; + } + +} diff --git a/uva_cpp_clean/10361/10361-21.cpp b/uva_cpp_clean/10361/10361-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91422ca7934459dac69e9c50f12ad8990357443d --- /dev/null +++ b/uva_cpp_clean/10361/10361-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10361 + Name: Automatic Poetry + Problem: https://onlinejudge.org/external/103/10361.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +int main(){ + regex r1("([^<]*)<([^>]*)>([^<]*)<([^>]*)>(.*)"); + regex r2("(.*)\\.\\.\\.$"); + char l1[1000], l2[1000]; + string ss[5]; + + int n; + cin >> n; + cin.getline(l1, 1000); + while (n--) { + cin.getline(l1, 1000); + cin.getline(l2, 1000); + + cmatch m; + regex_match(l1, m, r1); + for (int i=1; i<=5; i++) + cout << (ss[i-1] = m[i]); + cout< +#include +#include + +using namespace std; + +bool dontWon(const vector& grid, char character) +{ + if (grid[0][0] == character && grid[0][1] == character && grid[0][2] == character) return false; + if (grid[1][0] == character && grid[1][1] == character && grid[1][2] == character) return false; + if (grid[2][0] == character && grid[2][1] == character && grid[2][2] == character) return false; + if (grid[0][0] == character && grid[1][0] == character && grid[2][0] == character) return false; + if (grid[0][1] == character && grid[1][1] == character && grid[2][1] == character) return false; + if (grid[0][2] == character && grid[1][2] == character && grid[2][2] == character) return false; + if (grid[0][0] == character && grid[1][1] == character && grid[2][2] == character) return false; + if (grid[0][2] == character && grid[1][1] == character && grid[2][0] == character) return false; + return true; +} + +int main() +{ + int T; + bool valid; + scanf("%d", &T); + vector grid(3); + while (T--) + { + int xCount = 0, oCount = 0; + valid = true; + for (int i = 0; i < 3; ++i) + { + scanf("%d", &grid[i]); + for (int x = 0; x < 3; ++x) + { + if (grid[i][x] == 'X') ++xCount; + else if (grid[i][x] == 'O') ++oCount; + } + } + if (xCount == oCount) valid = dontWon(grid, 'X'); + else if (xCount == oCount + 1) valid = dontWon(grid, 'O'); + else valid = false; + if (valid) printf("yes\n"); + else printf("no\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10364/10364-21.cpp b/uva_cpp_clean/10364/10364-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1819eecc8194052480287b915b751d2526655bf8 --- /dev/null +++ b/uva_cpp_clean/10364/10364-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10364 + Name: Square + Problem: https://onlinejudge.org/external/103/10364.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int seen[1<<20], X[23], n, d, cse; +bool chosen[23]; +bool backtrack(int s, int cnt, int state, int r) { + if (cnt==4) return true; + if (r == 0) return backtrack(0, cnt+1, state, d); + if (seen[state] == cse) return false; + seen[state] = cse; + + for (int i=s; i> T; + for (cse=1; T-- && cin>>n; ++cse) { + int sum = 0; + for (int i=0; i> X[i]; + sum += X[i]; + } + sort(X, X+n, greater()); + d = sum>>2; + + memset(chosen, 0, 23); + if ((sum&3) || !backtrack(0, 0, 0, d)) + cout << "no\n"; + else + cout << "yes\n"; + } +} diff --git a/uva_cpp_clean/10365/10365-21.cpp b/uva_cpp_clean/10365/10365-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa6b3b7e08a10fb3bb443068a41bff928ac1cfdc --- /dev/null +++ b/uva_cpp_clean/10365/10365-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10365 + Name: Blocks + Problem: https://onlinejudge.org/external/103/10365.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector factors[1001]; + for (int i=1; i<1001; ++i) + for (int j=1; j<=i; ++j) + if (i % j == 0) + factors[i].push_back(j); + + int T, n; + cin >> T; + while (T-- && cin>>n) { + int ms = (int)1e9; + for (int i: factors[n]) + for (int j: factors[n/i]) { + int k = (n/i)/j; + ms = min(ms, (i*j + j*k + k*i)<<1); + } + cout << ms << "\n"; + } +} diff --git a/uva_cpp_clean/10365/10365-9.cpp b/uva_cpp_clean/10365/10365-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50bd714798c7b33816fbdbf094a934b996698922 --- /dev/null +++ b/uva_cpp_clean/10365/10365-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int tc, N, S, s, z; + +int main(){ + scanf("%d", &tc); + while(tc--){ + S = INT_MAX; + scanf("%d", &N); + for(int x = 1; x <= N; x++) + for(int y = 1; y <= sqrt(N); y++) + if(N % (x * y) == 0){ + z = N / (x * y); + s = 2 * (x * y + y * z + x * z); + if(s < S) S = s; + } + printf("%d\n", S); + } + return(0); +} diff --git a/uva_cpp_clean/10368/10368-19.cpp b/uva_cpp_clean/10368/10368-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d494b4dfe0320d5a20279ebaf0d2f083aae981b6 --- /dev/null +++ b/uva_cpp_clean/10368/10368-19.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +typedef long long ll; +typedef pair ii; + +int gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + if (n % m == 0) return 0; + int val = gcd(m, n%m); + if (n / m == 1) { + val = 1 - val; + } else val = 0; + return val; +} + +int main() { + int n, m; + while (cin >> n >> m && !(n == 0 && m == 0)) { + int val = gcd(n, m); + if (val) printf("Ollie wins\n"); + else printf("Stan wins\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10368/10368-9.cpp b/uva_cpp_clean/10368/10368-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6a4d0b49898cb0bc9a97201401bd230a27c992a --- /dev/null +++ b/uva_cpp_clean/10368/10368-9.cpp @@ -0,0 +1,40 @@ +#include + +#define WIN 1 +#define FAIL -1 + +using namespace std; + +typedef long long ll; + +ll num_1, num_2; + +int minVal(ll, ll); +int maxVal(ll, ll); + +int minVal(ll _max, ll _min) { + if (_max % _min == 0) return FAIL; + int res = WIN; + for (ll k = (_max / _min) * _min; res != FAIL and k >= 1; k -= _min) + res = min(res, (_max == k) ? FAIL : maxVal(max(_max - k, _min), min(_max - k, _min))); + return res; +} + + +int maxVal(ll _max, ll _min) { + if (_max % _min == 0) return WIN; + int res = FAIL; + for (ll k = (_max / _min) * _min; res != WIN and k >= 1; k -= _min) + res = max(res, (_max == k) ? WIN : minVal(max(_max - k, _min), min(_max - k, _min))); + return res; +} + +int minimax(ll a, ll b) { + return maxVal(a, b); +} + +int main() { + while (scanf("%lld %lld", &num_1, &num_2), num_1 and num_2) + puts(minimax(max(num_1, num_2), min(num_1, num_2)) == WIN ? "Stan wins" : "Ollie wins"); + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10369/10369-13.cpp b/uva_cpp_clean/10369/10369-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2982282917433b2865d3ccacb1b0ee77a0019e6 --- /dev/null +++ b/uva_cpp_clean/10369/10369-13.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 505 + +int n, m, t; +int parent[MAX], sz[MAX]; +vector > points; + +class comparator +{ +public: + + bool operator()(pair, double> &child1, pair, double> &child2) + { + return child1.second > child2.second; + } +}; + +priority_queue< pair< pair< int, int >, double >, vector< pair< pair< int, int >, double > >, comparator> pq; + +double cal(pair a, pair b) +{ + int x1 = a.first; + int x2 = b.first; + + int y1 = a.second; + int y2 = b.second; + + return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))); +} + +void ini() +{ + points.clear(); + + for (int i = 0; i < m; i++) + { + parent[i] = i; + + sz[i] = 1; + } + + while (!pq.empty()) + { + pq.pop(); + } +} + +int findParent(int v) +{ + if (v == parent[v]) return v; + + else return parent[v] = findParent(parent[v]); +} + +bool merge(int x, int y) +{ + if (findParent(x) == findParent(y)) return true; + + else + { + x = findParent(x); + + y = findParent(y); + + if (sz[x] < sz[y]) swap(x, y); + + parent[y] = x; + + sz[x] += sz[y]; + + return false; + } +} + +double msf() +{ + int temp = m; + + double ans = 0; + + while (temp != n) + { + pair< pair , double> head = pq.top(); + + if (!merge(head.first.first, head.first.second)) + { + temp--; + + ans = max(ans, head.second); + } + + pq.pop(); + } + + return ans; +} + +int main() +{ + cin >> t; + + while (t--) + { + cin >> n >> m; + + ini(); + + for (int i = 0; i < m; i++) + { + int v, w; + + cin >> v >> w; + + points.push_back(make_pair(v, w)); + } + + for (int i = 0; i < m; i++) + { + for (int j = i + 1; j < m; j++) + { + double temp = cal(points[i], points[j]); + + pq.push(make_pair(make_pair(i, j), temp)); + } + } + + printf("%.2f\n", msf()); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10369/10369-21.cpp b/uva_cpp_clean/10369/10369-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60c2c026a65d1d5f4a35f42625b800921cd5228f --- /dev/null +++ b/uva_cpp_clean/10369/10369-21.cpp @@ -0,0 +1,83 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10369 + Name: Arctic Network + Problem: https://onlinejudge.org/external/103/10369.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +#define MAXN 501 + +int n; +int X[MAXN], Y[MAXN]; +double D[MAXN][MAXN]; + +double dii; +bool seen[MAXN]; + +void dfs(int u) { + if (seen[u]) return; + seen[u] = 1; + + for (int i=0; i>T; + while (T--) { + cin >> s >> n; + + set spd; + for (int i=0; i>X[i]>>Y[i]; + for (int j=0; j posd(spd.rbegin(), spd.rend()); + int lo=0, hi=posd.size(); + while (lo <= hi) { + int mid = (lo+hi+1)>>1; + if (countCc(posd[mid]) <= s) + lo = mid+1; + else + hi = mid-1; + } + + printf("%.2f\n", posd[lo-1]); + } +} diff --git a/uva_cpp_clean/10369/10369-6.cc b/uva_cpp_clean/10369/10369-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..ed6674a6949622bfce56c0d727a7a2878b9b815c --- /dev/null +++ b/uva_cpp_clean/10369/10369-6.cc @@ -0,0 +1,58 @@ +// https://uva.onlinejudge.org/external/103/10369.pdf +#include +using namespace std; +using pt=complex; +using dii=tuple; +using vi=vector; +using vp=vector; +using vdii=vector; +int main(){ + int t,z,n; + cin>>t; + cout<>z>>n; + vp a(n); + for(int i=0;i>x>>y; + a[i]={x,y}; + } + if(z>=n){ + cout<<"0\n"; + continue; + } + vdii b; + for(int i=0;ifind=[&](int i){ + if(i==p[i])return i; + return p[i]=find(p[i]); + }; + functionunite=[&](int i,int j){ + i=find(i); + j=find(j); + if(i==j)return; + if(s[i] + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 1000010; + +struct Edge_{ + int u, v, w; + Edge_() {}; + Edge_(int u_, int v_, int w_){ + u = u_, v = v_, w = w_; + } + bool operator < (const Edge_& other) const{ + return ( w < other.w ); + } +}E[SIZE]; + +struct Point_{ + int x, y; +}P[SIZE]; + + +int tc, CC, N; + +inline int pow2(int P_){ + return P_ * P_; +} + +inline int dis(int i, int j){ + return pow2(P[i].x - P[j].x) + pow2(P[i].y - P[j].y); +} + +double Kruskal(){ + UnionFind UF(N); + double D, setSize = N; + int M = 0; + for(int i = 0; i < N; i++) + for(int j = i + 1; j < N; j++) + E[M++] = Edge_(i, j, dis(i, j)); + sort(E, E + M); + for(int it = 0; it < M and setSize != CC; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ) + D = sqrt(E[it].w), setSize--, + UF.unionSet(E[it].u, E[it].v); + return D; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &CC, &N); + for(int it = 0; it < N; it++) + scanf("%d %d", &P[it].x, &P[it].y); + printf("%.2lf\n", Kruskal()); + } + return(0); +} diff --git a/uva_cpp_clean/10370/10370-19.cpp b/uva_cpp_clean/10370/10370-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f86addcb8b0e45a6b439958d8bb8a054466fc21 --- /dev/null +++ b/uva_cpp_clean/10370/10370-19.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main() { + int cases; + float grades[1001]; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + int N; + scanf("%d", &N); + float acum = 0; + for (int j = 0; j < N; j++) { + scanf("%f", &grades[j]); + acum += grades[j]; + } + acum /= N; + float cont = 0; + for (int j = 0; j < N; j++) { + if (grades[j]>acum) cont++; + } + printf("%.3f%%\n", float((cont*100)/N)); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10370/10370-5.cpp b/uva_cpp_clean/10370/10370-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cf528289f847ca970b7d49f0d42dd25931a9af01 --- /dev/null +++ b/uva_cpp_clean/10370/10370-5.cpp @@ -0,0 +1,35 @@ +#include + +int main() + +{ + int a[1020],i,c,d,e,m; + float w,k,n,j; + while( scanf("%d",&c)==1) + { + + for(d=1;d<=c; d++) + + { + w=0; + e=0; + k=0; + scanf("%f",&j); + + for(i=0;iw) + e=e+1; + } + n=(100*e)/j; + printf("%.3f%%\n",n); + } + } +return 0; +} diff --git a/uva_cpp_clean/10370/10370-9.cpp b/uva_cpp_clean/10370/10370-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c8a0a45958a8d12e6a330b4bd5bac74bc3cca54 --- /dev/null +++ b/uva_cpp_clean/10370/10370-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int main(){ + int c, n, tmp, ct; + float mean; + vector a; + scanf("%d", &c); + while(c--){ + mean = 0; + ct = 0; + a.clear(); + scanf("%d", &n); + for(int i = 0; i < n; i++) + scanf("%d", &tmp), mean += tmp, a.push_back(tmp); + mean /= n; + for(int i = 0; i < n; i++) + if(a[i] > mean) + ct++; + printf("%.3f%%\n", (float)ct / n * 100); + + } + return(0); +} diff --git a/uva_cpp_clean/10374/10374-19.cpp b/uva_cpp_clean/10374/10374-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbcb6735aa1c50282a4b318138d79630b6f4d120 --- /dev/null +++ b/uva_cpp_clean/10374/10374-19.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; + +int main() { + unordered_map candidates; + unordered_map votes; + int T; + cin >> T; + while (T--) { + candidates.clear(); + votes.clear(); + int n; + cin >> n; + string s; + getline(cin, s); + for (int i = 0; i < n; i++) { + string party; + getline(cin, s); + getline(cin, party); + candidates[s] = party; + } + int m; + cin >> m; + getline(cin, s); + int maxi = -1; + string winner; + for (int i = 0; i < m; i++) { + getline(cin, s); + votes[s]++; + if (votes[s] > maxi) maxi = votes[s], winner = candidates[s]; + else if (votes[s] == maxi) winner = "tie"; + } + printf("%s\n", winner.c_str()); + if (T) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10375/10375-19.cpp b/uva_cpp_clean/10375/10375-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5ca5fe01d162f54f9bb0b5a92f43ee99c976469 --- /dev/null +++ b/uva_cpp_clean/10375/10375-19.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + int p, q, r, s; + while (cin >> p >> q >> r >> s) { + q = min(q, p-q); s = min(s, r-s); + double val = 1; + for (int i = 1, j = 1; i <= q || j <= s; i++, j++) { //We have to mix both loops because binomial coefficients are too big, in other words, we can't try to calculate binomial coefficients separately. + if (i <= q) { + val *= p-q+i; + val /= i; + } if (j <= s) { + val *= j; + val /= r-s+j; + } + } + printf("%.5f\n", val); + } + return 0; +} diff --git a/uva_cpp_clean/10375/10375-21.cpp b/uva_cpp_clean/10375/10375-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9346979e41fa39e5d658cc13d1a0317da0a75e6c --- /dev/null +++ b/uva_cpp_clean/10375/10375-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10375 + Name: Choose and divide + Problem: https://onlinejudge.org/external/103/10375.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int p, q, r, s; + while (scanf("%d%d%d%d", &p, &q, &r, &s)==4) { + q = min(q, p-q); + s = min(s, r-s); + int mx = max(q, s); + long double result = 1; + for (int i=1; i<=mx; ++i) { + if (i <= q) + result *= (p-i+1.0)/i; + + if (i <= s) + result *= i/(r-i+1.0); + } + printf("%.5Lf\n", result); + } +} diff --git a/uva_cpp_clean/10375/10375-9.cpp b/uva_cpp_clean/10375/10375-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87ccca60fa9b2e2a3f064136279293c5083561d4 --- /dev/null +++ b/uva_cpp_clean/10375/10375-9.cpp @@ -0,0 +1,14 @@ +def main(): + SIZE = 10010 + F = [1] + for i in range(1, SIZE): + F += [ i * F[-1] ] + while True: + try: + p, q, r, s = map(int, input().split()) + print("{0:.5f}".format(F[p] * F[s] * F[r - s] / ( F[q] * F[p - q] * F[r] ))) + except EOFError: + break + +if __name__ == '__main__': + main() diff --git a/uva_cpp_clean/10377/10377-13.cpp b/uva_cpp_clean/10377/10377-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..858b5a2db4b6b4a5ae9cb2a1db4be04c7cf1b396 --- /dev/null +++ b/uva_cpp_clean/10377/10377-13.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +using namespace std; + +ofstream fout("a.out"); + +bool sr = false; + +char robot; +int t, n, m, x, y; + +vector graph; + +int dirX[] = { -1, 0, 1, 0 }; +int dirY[] = { 0, 1, 0, -1 }; + +char dir[] = { 'N', 'E', 'S', 'W' }; + +bool safe(int a, int b) +{ + if (a < n && b < m && a >= 0 && b >= 0 && graph[a][b] == ' ') return true; + + else return false; +} + +void move(char command) +{ + switch (command) + { + case 'R': + { + robot = (robot + 1) % 4; + break; + } + + case 'L': + { + robot = ((robot - 1) + 4) % 4; + break; + } + + case 'F': + { + if (safe(x + dirX[robot], y + dirY[robot])) + { + x += dirX[robot]; + y += dirY[robot]; + } + + break; + } + + default: + break; + } +} + +int main() +{ + cin >> t; + + while (t--) + { + robot = 0; + + graph.clear(); + + cin >> n >> m; + + string a; + + getline(cin, a); + + for (int i = 0; i < n; i++) + { + string a; + + getline(cin, a); + + graph.push_back(a); + } + + bool ok = false; + + string command; + + cin >> x >> y; + + x--; + + y--; + + getline(cin, command); + + while (true) + { + cin >> command; + + for (int i = 0; i < command.size(); i++) + { + if (command[i] != 'Q') move(command[i]); + + else + { + ok = true; + + break; + } + } + + if (ok) + { + if (sr) cout << endl; + + else sr = true; + + cout << x + 1 << " " << y + 1 << " " << dir[robot] << endl; + + ok = false; + + break; + } + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10377/10377-21.cpp b/uva_cpp_clean/10377/10377-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b66917b7c6776485ff7b6819341bc3b8a1347630 --- /dev/null +++ b/uva_cpp_clean/10377/10377-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10377 + Name: Maze Traversal + Problem: https://onlinejudge.org/external/103/10377.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +char M[100][100]; +char readchar() { + char ch; + for (;(ch=getchar());) + if (ch!=' ' && ch!='\n' && ch!='\t') + return ch; + return 'Q'; +} + +int RR[127], LL[127]; +int main() { + char alldirs[] = "NESW"; + for (int i=0; i<4; i++) { + RR[alldirs[i]] = alldirs[(i+1)%4]; + LL[alldirs[i]] = alldirs[(i+3)%4]; + } + + char cm; + int T, n, m, cx, cy; + cin>>T; + while (T--) { + cin >> n >> m; + cin.getline(M[0], 100); + for (int i=0; i>cy>>cx; cy--; cx--; + while ((cm=readchar()) != 'Q') + switch (cm) { + case 'R': dir = RR[dir]; break; + case 'L': dir = LL[dir]; break; + case 'F': + int ncx=cx, ncy=cy; + switch (dir) { + case 'N': ncy--; break; + case 'E': ncx++; break; + case 'S': ncy++; break; + case 'W': ncx--; break; + } + + if (ncx>=0 && ncx=0 && ncy +using namespace std; +using vs=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t,n,m,y,x,o,q; + int dy[]={-1,0,1,0}; + int dx[]={0,1,0,-1}; + cin>>t; + for(int T=0;T>n>>m; + string s; + getline(cin,s); + vs a(n); + for(int i=0;i>y>>x; + getline(cin,s); + y--;x--; + o=0;q=0; + for(;;){ + getline(cin,s); + for(char c:s){ + if(c=='L')o=!o?3:o-1; + else if(c=='R')o=o==3?0:o+1; + else if(c=='F'){ + if(a[y+dy[o]][x+dx[o]]!='*'){y+=dy[o];x+=dx[o];} + }else if(c=='Q'){q=1;break;} + } + if(q)break; + } + for(;;){ + getline(cin,s); + if(s.empty())break; + } + if(T)cout<<"\n"; + cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10382 + Name: Watering Grass + Problem: https://onlinejudge.org/external/103/10382.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +struct Range { + long double s, e; + Range(long double s, long double e): s(s), e(e) {} + bool operator < (const Range &r) const { + return s < r.s; + } +}; + + +int minchoose(const vector &vec, long long len) { + int cnt = 0; + long double right=0; + for (int i=0; i vec[bj].e) + bj = j; + + if (bj < 0) return -1; + right = vec[bj].e; + i = j; + } + + return right < len ? -1 : cnt; +} + +int main(){ + long long n, l, w, xi, r; + while (cin>>n>>l>>w) { + vector vec; + long double w24 = w*w/4.0; + for (int i=0; i>xi>>r; + if (2*r > w) { + long double dif = sqrt(r*r - w24); + vec.push_back(Range(xi-dif, xi+dif)); + } + } + sort(vec.begin(), vec.end()); + + cout << minchoose(vec, l) << endl; + } +} diff --git a/uva_cpp_clean/10382/10382-6.cc b/uva_cpp_clean/10382/10382-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..bfdc251466b544bc56e5896b4023de608675c8d0 --- /dev/null +++ b/uva_cpp_clean/10382/10382-6.cc @@ -0,0 +1,63 @@ +// https://uva.onlinejudge.org/external/103/10382.pdf +#include +#include +#include +#include +#include + +using namespace std; + +typedef tuple dd; +typedef vector

vdd; + +int main() { + while (true) { + int n, l, w; + cin >> n >> l >> w; + if (cin.peek() == EOF) break; + vdd v; + for (int i = 0; i < n; i++) { + int x, r; + cin >> x >> r; + if (r * 2 <= w) continue; + double theta = asin(double(w) / 2.0 / double(r)); + double a = x - r * cos(theta); + double b = x + r * cos(theta); + v.push_back(dd(a, b)); + } + sort(v.begin(), v.end(), [](dd x, dd y) { return get<0>(x) < get<0>(y); }); + for (auto x : v) { + double a, b; + tie(a, b) = x; + } + int i = 0; + double pos = 0.0; + double endpos = 0.0; + int count = 0; + while (true) { + if (i >= v.size()) { + if (endpos >= l) count++; + else count = -1; + break; + } + double a, b; + tie(a, b) = v[i]; + if (a <= pos) { + if (b > endpos) { + endpos = b; + } + i++; + continue; + } + if (endpos == 0.0) { + count = -1; + break; + } + count++; + if (endpos >= l) break; + pos = endpos; + endpos = 0.0; + } + cout << count << endl; + } +} diff --git a/uva_cpp_clean/10382/10382-6.cpp b/uva_cpp_clean/10382/10382-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df3b6819a8d166101fdd11a9c1356784ab9cdfe1 --- /dev/null +++ b/uva_cpp_clean/10382/10382-6.cpp @@ -0,0 +1,53 @@ +/* + greedy > interval covering + difficulty: medium + date: 15/Jan/2020 + hint: reduce the problem using pythagoras to one line + by: @brpapa +*/ +#include +#include +#include +#include +#define left first +#define right second +using namespace std; + +vector> intervals; + +int intervalCovering(double start, double end) { + sort(intervals.begin(), intervals.end()); + + int cnt = 0; + double rightMost = start; + + for (int j, i = 0; i < intervals.size(); i = j) { + if (intervals[i].left > rightMost) break; + + for (j = i + 1; j < intervals.size() && intervals[j].left <= rightMost; j++) + if (intervals[j].right > intervals[i].right) + i = j; + + cnt++; + rightMost = intervals[i].right; + + if (rightMost >= end) break; + } + return (rightMost >= end)? cnt : -1; +} + +int main() { + int N, L, W; + while (cin >> N >> L >> W) { + intervals.clear(); + for (int i = 0; i < N; i++) { + double x, r; cin >> x >> r; + if (r > W/2.0) { + double dx = sqrt(pow(r, 2) - pow(W/2.0, 2)); + intervals.push_back(make_pair(x-dx, x+dx)); + } + } + cout << intervalCovering(0.0, L) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10382/10382-9.cpp b/uva_cpp_clean/10382/10382-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78c9e901715b87f79720ea14f55309fefe19f885 --- /dev/null +++ b/uva_cpp_clean/10382/10382-9.cpp @@ -0,0 +1,35 @@ +#include + +#define SIZE 10010 + +using namespace std; + +int n, p, j, ans, ct; +double l, dx, r, w, lo, _max; +pair v[SIZE]; + +int main(){ + while(scanf("%d %lf %lf", &n, &l, &w) == 3){ + ct = 0, w /= 2.0; + for(int i = 0; i < n; i++){ + scanf("%d %lf", &p, &r); + if(r > w){ + dx = sqrt(r * r - w * w); + v[ct++] = make_pair(p - dx, p + dx); + } + } + sort(v, v + ct); + lo = ans = 0; + for(int i = 0; i < ct && lo < l; ans++){ + _max = lo; + for(j = i; j < ct && v[j].first <= lo; j++) + _max = max(_max, v[j].second); + lo = _max; + if(j == i) break; + i = j; + } + ans = (lo < l) ? -1 : ans; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10385/10385-21.cpp b/uva_cpp_clean/10385/10385-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73a388fa0a3cd913282d2147051aa4b14e634e69 --- /dev/null +++ b/uva_cpp_clean/10385/10385-21.cpp @@ -0,0 +1,61 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10385 + Name: Duathlon + Problem: https://onlinejudge.org/external/103/10385.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int n; +double R[43], C[43], t; +double finishTime(int i, double r) { + return (r/R[i] + (t - r) / C[i]) * 60 * 60; +} + +double margin(double r) { + double res = 1e9; + for (int i=0; i> t >> n) { + for (int i=0; i> R[i] >> C[i]; + } + + double lo = 0, hi = t, l, r, ml, mr; + while (hi - lo > 1e-6) { + l = (2*lo + hi)/3; + ml = margin(l); + r = (lo + 2*hi)/3; + mr = margin(r); + + if (ml > mr) { + hi = r; + } else { + lo = l; + } + } + + if (ml < -1e-3 && mr < -1e-3) { + cout << "The cheater cannot win." << endl; + } else { + cout << "The cheater can win by " << fixed << setprecision(0) << fabs(ml) + << " seconds with r = " << setprecision(2) << lo + << "km and k = " << (t - lo) << "km." + << endl; + } + } +} diff --git a/uva_cpp_clean/10387/10387-16.cpp b/uva_cpp_clean/10387/10387-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a757825b842ea196ea5f17be89bc80e747e24886 --- /dev/null +++ b/uva_cpp_clean/10387/10387-16.cpp @@ -0,0 +1,29 @@ +//============================================================================ +// Name : 10387 - Billiard.cpp +// Author : ztrixack +// Copyright : MIT +// Description : 10387 Billiard in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +#define _USE_MATH_DEFINES +#include +#include +using namespace std; + +int main () { + int a, b, s, m, n; + double dis, angle; + cout.precision(2); + cout.setf(ios::fixed); + while (true) { + cin >> a >> b >> s >> m >> n; + if (!a && !b && !s && !m && !n) break; + dis = hypot(b * n, a * m); + angle = fabs((double)atan2((double)b * n, a * m) * 180 / M_PI); + if(angle > 90) + angle = 180 - angle; + dis /= s; + cout << angle << " " << dis << endl; + } +} diff --git a/uva_cpp_clean/10389/10389-13.cpp b/uva_cpp_clean/10389/10389-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1ccb14dc216c5f0a8fb37a7bb08e32bea3ec4ca --- /dev/null +++ b/uva_cpp_clean/10389/10389-13.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define WALK 0.006 +#define SUBW 0.0015 +#define INFI (long double)1e9 + +const long double Eps = 1e-7; + +struct point +{ + long double x; + long double y; + int label; +}; + +int n, query; + +string junk, sep = ""; + +point source, goal, stop; + +vector > > graph; + +inline bool lessThan(long double a, long double b) +{ + return a < b - Eps; +} + +class comparator +{ + public: + + bool operator()(pair &a, pair &b) + { + return lessThan(b.second, a.second); + } +}; + +inline long double calDist(point a, point b) +{ + long double X = a.x - b.x; + long double Y = a.y - b.y; + return (long double)sqrt((long double)(X * X) + (long double)(Y * Y)); +} + + +void init(vector subway) +{ + graph.clear(); + graph.resize(subway.size()); + + for (int i = 0; i < subway.size(); i++) + { + for (int j = 0; j < subway.size(); j++) + { + if (i == j) continue; + + if (subway[i].label == subway[j].label && abs(i - j) == 1) + { + graph[i].push_back({ j, SUBW * calDist(subway[i], subway[j]) }); + } + + else + { + graph[i].push_back({ j, WALK * calDist(subway[i], subway[j]) }); + } + } + } +} + +long double dijkstra(int start, int dist) +{ + vector dp(dist + 1, INFI); + + dp[start] = 0; + + priority_queue, vector >, comparator> pq; + + pq.push({ start, dp[start] }); + + while (!pq.empty()) + { + pair head = pq.top(); + + pq.pop(); + + int u = head.first; + + for (int i = 0; i < graph[u].size(); i++) + { + int v = graph[u][i].first; + long double w = graph[u][i].second; + + if (lessThan(dp[u] + w, dp[v])) + { + dp[v] = dp[u] + w; + + pq.push({ v, dp[v] }); + } + } + } + + return dp[dist]; +} + +int main() +{ + cin >> query; + + while (query--) + { + vector subway; + + cin >> source.x >> source.y >> goal.x >> goal.y; + + source.label = 0; + + subway.push_back(source); + + n = 1; + + while (getline(cin, junk), isdigit(cin.peek())) + { + while (cin >> stop.x >> stop.y, stop.x != -1) + { + stop.label = n; + + subway.push_back(stop); + } + + n++; + } + + goal.label = n; + + subway.push_back(goal); + + init(subway); + + cout << fixed << setprecision(0) << sep << dijkstra(0, subway.size() - 1) << '\n'; + + sep = "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10389/10389-21.cpp b/uva_cpp_clean/10389/10389-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9da8d36c87e6a46c09c8e1ec59d7838b62e3388 --- /dev/null +++ b/uva_cpp_clean/10389/10389-21.cpp @@ -0,0 +1,112 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10389 + Name: Subway + Problem: https://onlinejudge.org/external/103/10389.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int n; +struct Point { + int x, y; + bool operator == (const Point& p) const { return x==p.x && y==p.y; } +} p[300]; + +double dist(int i, int j) { + int xx = p[i].x-p[j].x, + yy = p[i].y-p[j].y; + + return sqrt(xx*xx + yy*yy); +} + + +bool S[300][300], seen[300]; +double M[300][300], D[300], H[300]; + +const double SS = 40000/60.0, + WS = 10000/60.0; + +int readPoint() { + cin >> p[n].x >> p[n].y; + if (p[n].x==-1 && p[n].y==-1) + return -1; + + for (int i=0; i ud.d + H[ud.u]; } +}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + while (T--) { + n = 0; readPoint(); + n = 1; readPoint(); + + n = 2; + while (cin.ignore(1000,'\n')) { + if (cin.peek() == '\n') break; + int lst = readPoint(), cur; + while ((cur=readPoint()) != -1) { + S[lst][cur] = S[cur][lst] = 1; + lst = cur; + } + } + + + for (int i=0; i q; + q.push(UD(0, D[0]=0)); + while (!q.empty()) { + UD ud = q.top(); q.pop(); + int u = ud.u; + if (u == 1) { + cout << round(ud.d) << endl; + break; + } + + if (seen[u]) continue; + seen[u] = 1; + + for (int v=0; v + +using namespace std; + +const int SIZE = 210, LEN = 10000, INF = 1000000000; + +typedef pair ii; + +int tc, a, b, V, start_, end_; +char line[LEN], *p; +double W[SIZE][SIZE], dis[SIZE]; +ii S, T, vert[SIZE]; + +int Dijkstra(int source, int target){ + fill(dis, dis + V, INF); + dis[source] = 0.0; + set < pair > st; + st.insert( make_pair(0.0, source) ); + while( not st.empty() ){ + int u = st.begin() -> second; + st.erase( st.begin() ); + for(int v = 0; v < V; v++){ + if( dis[v] > dis[u] + W[u][v] ){ + dis[v] = dis[u] + W[u][v]; + st.insert( make_pair(dis[v], v) ); + } + } + } + return round(dis[target] + 1e-6); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + V = 0; + fill(W[0], W[SIZE], INF); + cin.getline(line, LEN); + sscanf(line, "%d %d %d %d", &S.first, &S.second, &T.first, &T.second); + vert[V++] = S, vert[V++] = T; + while(cin.getline(line, LEN), strlen(line)){ + p = strtok(line, " "); + start_ = V; + while(p){ + a = atoi(p); + p = strtok(NULL, " "); + b = atoi(p); + p = strtok(NULL, " "); + if( a == -1 and b == -1 ) break; + vert[V++] = ii(a, b); + } + end_ = V; + for(int i = start_ + 1; i < end_; i++) + W[i][i - 1] = W[i - 1][i] = hypot(vert[i].first - vert[i - 1].first, vert[i].second - vert[i - 1].second) * 3.0 / 2000.0; + } + for(int i = 0; i < V; i++) + for(int j = 0; j < V; j++) + W[j][i] = W[i][j] = min(W[i][j], hypot(vert[i].first - vert[j].first, vert[i].second - vert[j].second) * 3.0 / 500.0); + printf("%d\n", Dijkstra(0, 1)); + if( tc ) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/1039/1039-21.cpp b/uva_cpp_clean/1039/1039-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce53daff455be37e9a249f273653b8cf36322443 --- /dev/null +++ b/uva_cpp_clean/1039/1039-21.cpp @@ -0,0 +1,107 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1039 + Name: Simplified GSM Network + Problem: https://onlinejudge.org/external/10/1039.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 268435456 +#define EPS 1e-9 +using namespace std; + +void nearestToLine( + double ax, double ay, + double bx, double by, + double px, double py, + double &cpx, double &cpy ) { + double proj = ( (px - ax) * (bx - ax) + (py - ay) * (by - ay) ) / + ( (bx - ax) * (bx - ax) + (by - ay) * (by - ay) ); + cpx = ax + proj * (bx - ax); + cpy = ay + proj * (by - ay); +} + +bool nearestToLineSegment( + double ax, double ay, + double bx, double by, + double px, double py, + double &cpx, double &cpy ) { + if ( (bx - ax) * (px - ax) + (by - ay) * (py - ay) < EPS ) + return 0; + + if ( (ax - bx) * (px - bx) + (ay - by) * (py - by) < EPS ) + return 0; + + nearestToLine(ax, ay, bx, by, px, py, cpx, cpy); + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector adj[55]; + int W[55][55], bn, cn, m, qn; + double Cx[55], Cy[55], Bx[55], By[55], Px[55], Py[55]; + for (int cse=1; cin >> bn >> cn >> m >> qn && (bn||cn||m||qn); ++cse) { + for (int i=0; i> Bx[i] >> By[i]; + + for (int i=0; i> Cx[i] >> Cy[i]; + for (int j=0; j> u >> v; --u; --v; + + int sz = 2; + Px[0] = Cx[u]; Py[0] = Cy[u]; + Px[1] = Cx[v]; Px[1] = Cy[v]; + for (int i=0; i> u >> v; --u; --v; + if (W[u][v] == INF) + cout << "Impossible\n"; + else + cout << W[u][v] << '\n'; + } + } +} diff --git a/uva_cpp_clean/10391/10391-14.cpp b/uva_cpp_clean/10391/10391-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e33223a93b6d88acb8846c96341438a581daa9d --- /dev/null +++ b/uva_cpp_clean/10391/10391-14.cpp @@ -0,0 +1,174 @@ +/*************************************************** + * Problem Name : 10391 Compound Words.cpp + * Problem Link : https://onlinejudge.org/external/103/10391.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-10-28 + * Problem Type : Data Structure(Trie) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define pis pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +mapmp; +struct TrieTree { + TrieTree *child[27]; + bool isWord; + TrieTree *create_node() { + TrieTree *newNode = new TrieTree; + newNode->isWord = false; + + for (int i = 0; i < 26; i++) { + newNode->child[i] = NULL; + } + + return newNode; + } + bool insert (TrieTree *root, string key) { + TrieTree *tmpNode = root; + bool flag = false; + vectorres; + + for (int i = 0; i < (int) key.size(); i++) { + int indx = key[i] - 'a'; + + if (tmpNode->child[indx] == NULL) { + tmpNode->child[indx] = create_node(); + } + + tmpNode = tmpNode->child[indx]; + + if (tmpNode->isWord == true) { + string ss = key.substr (i + 1, SZ (key) - (i + 1) ); + res.pb (ss); + } + } + + tmpNode->isWord = true; + + for (int i = 0 ; i < (int) res.size(); i++) { + if (mp[res[i]] == true) { + flag = true; + break; + } + } + + return flag; + } +}; + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + vectorvec; + string str; + + while (cin >> str) { + vec.pb ({SZ (str), str}); + mp[str] = true; + } + + sort (all (vec) ); + TrieTree Trie; + TrieTree *root = Trie.create_node(); + vectorans; + + for (int i = 0; i < (int) vec.size(); i++) { + if (Trie.insert (root, vec[i].second) ) ans.pb (vec[i].second); + } + + sort (all (ans) ); + for (int i = 0; i < (int) ans.size(); i++) { + cout << ans[i] << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/10391/10391-17.cpp b/uva_cpp_clean/10391/10391-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c9adcbbdad6e0f17bb75a26ed1f65c0807d078a --- /dev/null +++ b/uva_cpp_clean/10391/10391-17.cpp @@ -0,0 +1,22 @@ +/** + * AOAPC II Exercise 5-5 Compound Words + * 8 TLE 20 AC // sliding words is faster than a huge set + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + set dict; + string s; + while (cin >> s) + dict.insert(s); + for (const string &s : dict) + for (int sz = 1; sz < s.size(); ++sz) + if (dict.count(s.substr(0, sz)) && dict.count(s.substr(sz))) + { + cout << s << "\n"; + break; + } +} diff --git a/uva_cpp_clean/10391/10391-21.cpp b/uva_cpp_clean/10391/10391-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b104fd2fb82c9920cfe376bb1f9bd98e083f5ebc --- /dev/null +++ b/uva_cpp_clean/10391/10391-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10391 + Name: Compound Words + Problem: https://onlinejudge.org/external/103/10391.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +int match(char *s1,char *s2){ //s1 in s2 + int i; + for(i=0;s1[i];i++) + if(s1[i]!=s2[i]) + return i; + return i; +} + +int main(){ + set l; + set::iterator i,j,s,e; + set op; + int k; + char t[40]; + while(gets(t)) + l.insert(t); + for(i=l.begin();i!=l.end();i++){ + s=l.lower_bound(*i);s++; + for(j=s;j!=l.end();j++){ + if(i!=j && (k=match((char*)&(*i)[0],(char*)&(*j)[0]))==strlen((char*)&(*i)[0]) && l.count(j->substr(k))) + op.insert(*j); + if(!k)break; + } + } + for(i=op.begin();i!=op.end();i++) + printf("%s\n",&(*i)[0]); +} \ No newline at end of file diff --git a/uva_cpp_clean/10392/10392-21.cpp b/uva_cpp_clean/10392/10392-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c085f8cd7e6153f730c466cea4ebcd4901762312 --- /dev/null +++ b/uva_cpp_clean/10392/10392-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10392 + Name: Factoring Large Numbers + Problem: https://onlinejudge.org/external/103/10392.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool isnp[1000143]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector primes; + for (int i=2; i<1000143; ++i) + if (!isnp[i]) { + for (int j=i+i; j<1000143; j+=i) + isnp[j] = 1; + primes.push_back(i); + } + + long long n; + while (cin >> n && n>=0) { + for (int p: primes) { + if (p > n) break; + while (n % p == 0) { + cout << " " << p << '\n'; + n /= p; + } + } + if (n > 1) + cout << " " << n << '\n'; + cout << '\n'; + } +} diff --git a/uva_cpp_clean/10392/10392-9.cpp b/uva_cpp_clean/10392/10392-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a505fb9581f77d01b584b076b7bc197a3b56d75 --- /dev/null +++ b/uva_cpp_clean/10392/10392-9.cpp @@ -0,0 +1,39 @@ +#include + +#define SIZE 1000010 + +using namespace std; + +long long n; +bool is_prime[SIZE]; +vector prime; +vector Factors; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++) + if(is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +void primeFactors(long long N){ + Factors.clear(); + for(int it = 0; it < prime.size() and prime[it] * prime[it] <= N and N != 1; it++) + while( N % prime[it] == 0) N /= prime[it], Factors.push_back( prime[it] ); + if( N != 1 ) Factors.push_back( N ); +} + +int main(){ + sieve(); + while(scanf("%lld", &n), n > 0){ + primeFactors( n ); + for(int it = 0; it < Factors.size(); it++) + printf( " %lld\n", Factors[it]); + puts(""); + } + return(0); +} diff --git a/uva_cpp_clean/10393/10393-21.cpp b/uva_cpp_clean/10393/10393-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51b11dcf1f93c746657c7182e996f1c60a104499 --- /dev/null +++ b/uva_cpp_clean/10393/10393-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10393 + Name: The One-Handed Typist + Problem: https://onlinejudge.org/external/103/10393.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char fings[10][10] = { + "qaz", "wsx", "edc", + "rfvtgb", "", "", "yhnujm", + "ik", "ol", "p" +}; + +bool check(string &s , bool cu[]) { + for (int i=0; s[i]; ++i) + if (cu[s[i]]) + return 0; + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m, x; + while (cin >> n >> m) { + bool cu[128] = {}; + for (int i=0; i> x; --x; + for (int k=0; fings[x][k]; ++k) + cu[fings[x][k]] = 1; + } + + string str; + vector r; + int mxlen = 0; + for (int i=0; i> str; + if (str.length() < mxlen || !check(str, cu)) continue; + + if (str.length() > mxlen) + mxlen = str.length(), + r.clear(); + + if (str.length() == mxlen) + r.push_back(str); + } + + sort(r.begin(), r.end()); + int sz = !r.empty(); + for (int i=1; i +#include + +using namespace std; + +#define LMT 18409202 +#define MAX 100001 + +long long n, p = 3; +vector isPrime; +vector> ans; + +void setPrime() +{ + ans.push_back({0, 0}); + isPrime.resize(LMT, true); + + isPrime[0] = isPrime[1] = false; + + for (long long i = 3; ans.size() < MAX; i += 2) + { + if (isPrime[i]) + { + if(i - p == 2) + { + ans.push_back({p, i}); + } + + for (long long j = i * i; j < LMT; j += i) + { + isPrime[j] = false; + } + + p = i; + } + } +} + +int main() +{ + setPrime(); + + while (cin >> n) + { + printf("(%lld, %lld)\n", ans[n].first, ans[n].second); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10394/10394-18.cpp b/uva_cpp_clean/10394/10394-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d7965f7d6fea5f3db8ef54de46313bb2aab1f484 --- /dev/null +++ b/uva_cpp_clean/10394/10394-18.cpp @@ -0,0 +1,44 @@ +#include +#define pb push_back +#define pf push_front +#define pob pop_back +#define fp first +#define sp second +#define mp make_pair +#define ins insert +#define uEdge(u, v) g[u].pb(v), g[v].pb(u) +#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w}) +#define dEdge(u, v) g[u].pb(v) +#define dwEdge(u, v, w) g[u].pb({v, w}) +#define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define minHeap int, vector, greater + +using namespace std; +typedef long long int lli; +typedef pair pii; +vector g[0]; +bool prime[20000000]; +int twin[1000000]; + +lli gcd(lli a, lli b){ + if(b == 0) return a; + a%=b; + return gcd(b, a); +} + +void sieve(){ + int j = 1; + memset(prime, 1, sizeof(prime)); + prime[1] = 0; + for(int i = 2; i*i <= 20000000; i++){ + if(prime[i]) for(int j = i*i; j <= 20000000; j+=i) prime[j] = 0; + } + for(int i = 3; i <= 20000000; i++) if(prime[i] && prime[i + 2]) twin[j++] = i; +} + +int main(){ + int n; + sieve(); + while(scanf("%d", &n) != EOF) printf("(%d, %d)\n", twin[n], twin[n] + 2); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10394/10394-19.cpp b/uva_cpp_clean/10394/10394-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9cbf795d8aec41add5915137289c9ef8fa348ee --- /dev/null +++ b/uva_cpp_clean/10394/10394-19.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +using namespace std; + +typedef long long ll; + +ll sieve_size; bitset<20000004> bs; +vector primes; +void sieve(ll upperbound) { + sieve_size=upperbound+1; bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +int main() { + sieve(20000000); + vector v; + int count = 0; + for (int i = 0; i < primes.size()-1 && count < 100000; i++) + if(primes[i+1] == primes[i] + 2) { + v.push_back(primes[i]); + count++; + } + int n; + while (scanf("%d", &n) != EOF) { + printf("(%d, %d)\n", v[n-1], v[n-1]+2); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10394/10394-2.cpp b/uva_cpp_clean/10394/10394-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e88e4833f0e9cb270606e58d4439f74beaa389d --- /dev/null +++ b/uva_cpp_clean/10394/10394-2.cpp @@ -0,0 +1,46 @@ +#include "bits/stdc++.h" + +#define el "\n" +#define all(v) ((v).begin()),((v).end()) +#define rall(v) ((v).rbegin()),((v).rend()) + +using namespace std; +typedef unsigned long long ull; +typedef long long ll; +typedef vector vi; +typedef vector vll; +const int OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +const int M = 20000010; +bitset<20000010> bs; +vector> vpi; +int main() { + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + ios::sync_with_stdio(false); + cin.tie(0); + int n; + bs.set(); + bs[0] = bs[1] = 0; + for (ull i = 2; i <= M; ++i) if (bs[i]) { + for (ull j = i*i; j <= M; j +=i) bs[j] = 0; + } + + for (ll i = 0; i < M-4; ++i) + if (bs[i] && bs[i+2]) vpi.push_back({i, i+2}); + + while (cin >> n) { + n--; + printf("(%lld, %lld)\n", vpi[n].first, vpi[n].second); + } + return 0; +} + +/* + + +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10394/10394-20.cpp b/uva_cpp_clean/10394/10394-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ff19109faa69c85f5b9d4ab5ac3a654be0367cd --- /dev/null +++ b/uva_cpp_clean/10394/10394-20.cpp @@ -0,0 +1,26 @@ +#include + +bool prime[20000002]; +int twin_prime[100002] = {0}; + +int main() +{ + prime[0] = prime[1] = false; + for(int i=2 ; i<20000002 ; i++) prime[i] = true; + + for(int i=2 ; i*i<20000002 ; i++) + if( prime[i] ) + for(int j=i+i ; j<20000002 ; j+=i) prime[j] = false; + + int pos = 1; + for(int i=2 ; i<20000002 ; i++) + if( prime[i-2] && prime[i] ) twin_prime[pos++] = i-2; + + + int S; + + while( scanf("%d", &S)==1 ) + printf("(%d, %d)\n", twin_prime[S], twin_prime[S]+2); + + return 0; +} diff --git a/uva_cpp_clean/10394/10394-21.cpp b/uva_cpp_clean/10394/10394-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c26dfb9d5d71652ad3755619389a230a541127fd --- /dev/null +++ b/uva_cpp_clean/10394/10394-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10394 + Name: Twin Primes + Problem: https://onlinejudge.org/external/103/10394.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define N 20000000 +unsigned int prime[N / 64]; +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) +#define rP(n) (prime[n>>6]&=~(1<<((n>>1)&31))) +void sieve() { + memset(prime, -1, sizeof(prime)); + + unsigned int i; + unsigned int sqrtN = (unsigned int) sqrt((double)N) + 1; + for (i = 3; i < sqrtN; i += 2) if(gP(i)) { + unsigned int i2 = i + i; + for (unsigned int j = i * i; j < N; j += i2 ) rP(j); + } +} + +bool isP(int x){ return gP(x); } + +int dp[100005]; +int main(){ + sieve(); + + int cnt = 0; + for (int i=3; cnt<100001; i+=2) + if (isP(i) && isP(i+2)) + dp[cnt++] = i; + + int n; + while (cin>>n) + printf("(%d, %d)\n", dp[n-1], dp[n-1]+2); +} diff --git a/uva_cpp_clean/10394/10394-5.cpp b/uva_cpp_clean/10394/10394-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d8f7407016b9efb2af6d46f0d43186c734396ebc --- /dev/null +++ b/uva_cpp_clean/10394/10394-5.cpp @@ -0,0 +1,86 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define st(s) s.size(); +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +using namespace std; + +bool prime[20000010]; +int tprime[20000010]; +void sieve() +{ + cover(prime,true); + prime[1]=false; + int n=sqrt(20000010); + for(int i=3;i<=n;i=i+2) + { + if(prime[i]) + { + for(int j=2*i;j<=20000010;j=j+i) + { + prime[j]=false; + } + } + } +} +void twin_prime() +{ + int j=1; + for(int i = 3;i<=20000000;i++){ + if(prime[i] && prime[i+2]) + { + tprime[j++] = i; + } + } +} +int main() +{ + sieve(); + twin_prime(); + int n; + while(sc("%d",&n)==1) + { + pf("(%d, %d)\n",tprime[n],tprime[n]+2); + } + return 0; +} + diff --git a/uva_cpp_clean/10394/10394-8.cpp b/uva_cpp_clean/10394/10394-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4056bd729ab161f90f7c360a50e20876de48c4f --- /dev/null +++ b/uva_cpp_clean/10394/10394-8.cpp @@ -0,0 +1,35 @@ +// Problem name: Twin Primes +// Problem link: https://vjudge.net/problem/UVA-10394 +// Submission link: https://vjudge.net/solution/32959080 + +#include + +using namespace std; + +typedef long long ll; + +vector> get_twin_primes(int n){ + vector small_prime(n + 1), primes; + vector> twin_primes; + for(int i = 2 ; i <= n ; ++i){ + if(small_prime[ i ] == 0){ + small_prime[ i ] = i, primes.push_back(i); + if(i > 2 && small_prime[ i - 2 ] == i - 2) + twin_primes.emplace_back(i - 2, i); + } + for(int j = 0 ; j < primes.size() && primes[ j ] <= small_prime[ i ] && i * primes[ j ] <= n ; ++j) + small_prime[ i * primes[ j ] ] = primes[ j ]; + } + return twin_primes; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int N = 20000000; // upper-bound found after some runs :) + const vector> twin_primes = get_twin_primes(N); + + for(int n ; cin >> n ; ) + cout << "(" << twin_primes[ n - 1 ].first << ", " << twin_primes[ n - 1 ].second << ")" << endl; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10394/10394-9.cpp b/uva_cpp_clean/10394/10394-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b91447b6f5a99afad17f4fd72f125554270ba3b8 --- /dev/null +++ b/uva_cpp_clean/10394/10394-9.cpp @@ -0,0 +1,30 @@ +#include + +const long long LIM = 2*(int)1e8; + +using namespace std; + +bitsetprime; +vectorp; +vector>twin_prime; +int n; + +void sieve(){ + p.push_back(2); + for(int i=3;i<=LIM;i+=2){ + if(!prime[i]){ + for(long long j=(long long)i*i;j<=LIM;j+=2*i) + prime[j] = 1; + if(i-p.back()==2) + twin_prime.push_back(make_pair(p.back(),i)); + p.push_back(i); + } + } +} + +int main(){ + sieve(); + while(scanf("%d",&n)==1) + printf("(%d, %d)\n",twin_prime[n-1].first,twin_prime[n-1].second); + return(0); +} diff --git a/uva_cpp_clean/10397/10397-20.cpp b/uva_cpp_clean/10397/10397-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6444e34689aff70093dfb082e88225e70f28f8ca --- /dev/null +++ b/uva_cpp_clean/10397/10397-20.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +const int MAXN = 750; +const int MAXM = 1000; + +struct coordinate { + double x, y; +} point[MAXN+2]; + +double cost[MAXN+2][MAXN+2]; +bool visited[MAXN+2][MAXN+2]; + +double distance( double x1, double x2, double y1, double y2 ) { + return ( sqrt( ((x1- x2) * (x1- x2)) + ((y1- y2) * (y1- y2)) ) ); +} + +struct data { + int x, y; + double cost; + bool operator <( const data & t ) const { + return ( cost < t.cost ); + } +}; + +vector < data > edge; +int pr[MAXN+2]; + +int findParent( int node ) { + return ( pr[node] == node ) ? node : findParent( pr[node] ); +} + +int nodeCounter; + +double mst( int n ) { + sort( edge.begin(), edge.end() ); + + double minCost = 0.0; + for( int i = 0 ; i < edge.size() ; ++i ) { + int x = findParent( edge[i].x ); + int y = findParent( edge[i].y ); + if( x != y ) { + pr[x] = y; + ++nodeCounter; + minCost += edge[i].cost; + if( nodeCounter == (n - 1) ) { + break; + } + } + } + return minCost; +} + +int main( int argc, char ** argv ) { + int N, M; + while( scanf( "%d", &N ) != EOF ) { + for( int i = 0 ; i <= MAXN ; ++i ) point[i].x = 0.0, point[i].y = 0.0; + memset( cost, 0.0, sizeof( cost ) ); + memset( visited, false, sizeof( visited ) ); + edge.clear(); + + for( int i = 1 ; i <= N ; ++i ) scanf( "%lf %lf", &point[i].x, &point[i].y ); + + for( int i = 1 ; i <= N ; ++i ) + for( int j = i + 1 ; j <= N ; ++j ) cost[i][j] = cost[j][i] = distance( point[i].x, point[j].x, point[i].y, point[j].y ); + + for( int i = 1 ; i <= N ; ++i ) + for( int j = 1 ; j <= N ; ++j ) + if( i != j && !visited[i][j] ) { + data get; + get.x = i, get.y = j, get.cost = cost[i][j]; + edge.push_back( get ); + + get.x = j, get.y = i, get.cost = cost[j][i]; + edge.push_back( get ); + + visited[i][j] = visited[j][i] = true; + } + + for( int i = 1 ; i <= N ; ++i ) pr[i] = i; + + scanf( "%d", &M ); + nodeCounter = 0; + for( int i = 1 ; i <= M ; ++i ) { + int x, y; + scanf( "%d %d", &x, &y ); + x = findParent( x ); + y = findParent( y ); + if( x != y ) { + pr[x] = y; + ++nodeCounter; + } + } + + double ans = mst( N ); + printf( "%.2f\n", ans ); + } + return 0; +} diff --git a/uva_cpp_clean/10397/10397-21.cpp b/uva_cpp_clean/10397/10397-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..635a64f579c366a974212993ea62e12c0aaa31a0 --- /dev/null +++ b/uva_cpp_clean/10397/10397-21.cpp @@ -0,0 +1,392 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10397 + Name: Connect the Campus + Problem: https://onlinejudge.org/external/103/10397.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + + +#define INF 1073741824 + + +// ------------------------------- FIBONACCI HEAP ------------------------------- // + +template +class FibHeap { +public: + class FibNode { + public: + T key; + bool mark; + int degree; + FibNode *p; + FibNode *left; + FibNode *right; + FibNode *child; + FibNode(T k):key(k),mark(0),p(0),left(0),right(0),child(0),degree(-1) {} + }; + + FibHeap(): n(0), min(0) { + } + + ~FibHeap() { + delete_fibnodes(min); + } + + void delete_fibnodes(FibNode *x) { + if (!x) return; + + FibNode *cur = x; + while (cur) + if (cur->left && cur->left != x) { + FibNode *tmp = cur; + cur = cur->left; + if (tmp->child) + delete_fibnodes(tmp->child); + delete tmp; + } + else { + if (cur->child) + delete_fibnodes(cur->child); + delete cur; + break; + } + } + + void insert(FibNode *x) { + x->degree = 0; + x->p = 0; + x->child = 0; + x->mark = 0; + if ( min == 0) + min = x->left = x->right = x; + else { + min->left->right = x; + x->left = min->left; + min->left = x; + x->right = min; + if (x->key < min->key) + min = x; + } + ++n; + } + + FibNode* minimum() { + return min; + } + + + static FibHeap* union_fibheap(FibHeap *H1, FibHeap *H2) { + FibHeap* H = new FibHeap(); + H->min = H1->min; + if ( H->min != 0 && H2->min != 0 ) { + H->min->right->left = H2->min->left; + H2->min->left->right = H->min->right; + H->min->right = H2->min; + H2->min->left = H->min; + } + if ( H1->min == 0 || ( H2->min != 0 && H2->min->key < H1->min->key ) ) + H->min = H2->min; + H->n = H1->n + H2->n; + return H; + } + + FibNode* extract_min() { + FibNode *z, *x, *next; + FibNode ** childList; + + z = min; + if (z != 0) { + x = z->child; + if (x != 0) { + childList = new FibNode*[z->degree]; + next = x; + for (int i = 0; i < (int)z->degree; i++) { + childList[i] = next; + next = next->right; + } + for (int i = 0; i < (int)z->degree; i++) { + x = childList[i]; + min->left->right = x; + x->left = min->left; + min->left = x; + x->right = min; + x->p = 0; + } + delete [] childList; + } + z->left->right = z->right; + z->right->left = z->left; + if (z == z->right) + min = 0; + else { + min = z->right; + consolidate(); + } + n--; + } + return z; + } + + void consolidate() { + int d, rootSize; + FibNode *w, *next, *x, *y, *temp, + **rootList, **A; + + int max_degree = log((double)n)/0.481212 + 3; + + A = new FibNode*[max_degree]; + for (int i=0; iright; + } while ( next != w ); + + + rootList = new FibNode*[rootSize]; + for (int i = 0; i < rootSize; i++) { + rootList[i] = next; + next = next->right; + } + + for (int i = 0; i < rootSize; i++) { + w = rootList[i]; + x = w; + d = x->degree; + while (A[d] != 0) { + y = A[d]; + if (y->key < x->key) { + temp = x; + x = y; + y = temp; + } + fib_heap_link(y,x); + A[d] = 0; + d++; + } + A[d] = x; + } + delete [] rootList; + + + min = 0; + for (int i = 0; i < max_degree; i++) + if (A[i] != 0) { + if (min == 0) + min = A[i]->left = A[i]->right = A[i]; + else { + min->left->right = A[i]; + A[i]->left = min->left; + min->left = A[i]; + A[i]->right = min; + if (A[i]->key < min->key) + min = A[i]; + } + } + delete [] A; + } + + + void fib_heap_link(FibNode* y, FibNode* x) { + y->left->right = y->right; + y->right->left = y->left; + if (x->child != 0) { + x->child->left->right = y; + y->left = x->child->left; + x->child->left = y; + y->right = x->child; + } + else { + x->child = y; + y->right = y; + y->left = y; + } + y->p = x; + x->degree++; + y->mark = 0; + } + + + + void decreaseKey(FibNode* x, T k) { + FibNode* y; + if (x->key < k) + return; + + x->key = k; + y = x->p; + if (y != 0 && x->key < y->key) { + cut(x,y); + cascading_cut(y); + } + + if (x->key < min->key) + min = x; + } + + + void cut(FibNode* x, FibNode* y) { + if (x->right == x) + y->child = 0; + else { + x->right->left = x->left; + x->left->right = x->right; + if (y->child == x) + y->child = x->right; + } + y->degree--; + + min->right->left = x; + x->right = min->right; + min->right = x; + x->left = min; + x->p = 0; + x->mark = 0; + } + + + void cascading_cut(FibNode* y) { + FibNode* z; + z = y->p; + if (z != 0) { + if (y->mark == 0) + y->mark = true; + else { + cut(y,z); + cascading_cut(z); + } + } + } + + void remove_fibnode(FibNode* x) { + decrease_key(x, -INF); + FibNode *fn = extract_min(); + if (fn) delete fn; + } + + + bool empty() const { + return n == 0; + } + + FibNode* topNode() { + return minimum(); + } + + T top() { + return minimum()->key; + } + + void pop() { + if (empty()) return; + + FibNode *x = extract_min(); + if (x) delete x; + } + + FibNode* push(T k) { + FibNode *x = new FibNode(k); + insert(x); + return x; + } + + unsigned int size() { + return (unsigned int) n; + } + + int n; + FibNode *min; +}; + +// ------------------------------------------------------------------------------ // + +#define MAXN 800 +int gid[MAXN]; +int find(int u) { return gid[u]==u ? u : gid[u]=find(gid[u]); } +void join(int u, int v) { gid[find(u)] = find(v); } + +int X[MAXN], Y[MAXN], d[MAXN]; +int dist2(int u, int v) { + int xx = X[u]-X[v], + yy = Y[u]-Y[v]; + return xx*xx + yy*yy; +} + +struct UD { + int u, d; + UD(int u, int d):u(u),d(d) {} + bool operator < (const UD &ud) { return d < ud.d; } +}; + +FibHeap:: FibNode* pts[MAXN]; + +int main() { + int n, m, u, v; + while (scanf("%d", &n)==1) { + for (int i=0; i q; + int rootId = find(0); + for (int i=0; i minimum spanning tree (MST) > minimum spanning subgraph + difficulty: medium + date: 03/Jun/2020 + problem: given an implicit complete graph and some edges, compute the cost of the minimum spanning subgraph + by: @brpapa +*/ +#include +using namespace std; +struct UFDS { + vector p; // p[i] = pai do item i + + UFDS(int N) { + p.resize(N); + for (int i = 0; i < N; i++) p[i] = i; + } + + int findSet(int i) { + if (p[i] == i) return i; + return p[i] = findSet(p[i]); + } + + bool isSameSet(int i, int j) { + return findSet(i) == findSet(j); + } + + void unionSet(int i, int j) { + int setI = findSet(i); + int setJ = findSet(j); + if (!isSameSet(setI, setJ)) p[setI] = setJ; + } +}; + +typedef pair ii; + +struct Tedge { + int u, v; double w; + Tedge() {} + Tedge(int u, int v, double w) : u(u), v(v), w(w) {} + bool operator<(const Tedge& p) const { return w < p.w; } +}; + +ii coords[777]; +double dist(int u, int v) { + ii cu = coords[u], cv = coords[v]; + int dx = abs(cu.first - cv.first); + int dy = abs(cu.second - cv.second); + return sqrt(dx*dx + dy*dy); +} + +int main() { + int V; + while (cin >> V) { + for (int v = 0; v < V; v++) { + int x, y; cin >> x >> y; coords[v] = ii(x, y); + } + + vector edgeList; // arestas do grafo completo, priorizando as mais leves + for (int u = 0; u < V; u++) + for (int v = u+1; v < V; v++) + edgeList.push_back(Tedge(u, v, dist(u, v))); + + sort(edgeList.begin(), edgeList.end()); + + UFDS uf(V); // conjuntos de componentes conectados + int E; cin >> E; + while (E--) { + int u, v; cin >> u >> v; u--; v--; + uf.unionSet(u, v); + } + + double ans = 0; + for (Tedge e : edgeList) { + if (!uf.isSameSet(e.u, e.v)) { + uf.unionSet(e.u, e.v); + ans += e.w; + } + } + printf("%.2f\n", ans); + } + return 0; +} diff --git a/uva_cpp_clean/10397/10397-9.cpp b/uva_cpp_clean/10397/10397-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbaa155845e2bc5c85e40425f1f6393ac4123f03 --- /dev/null +++ b/uva_cpp_clean/10397/10397-9.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 1000010; + +struct Edge_{ + int u, v, w; + Edge_() {}; + Edge_(int u_, int v_, int w_){ + u = u_, v = v_, w = w_; + } + bool operator < (const Edge_& other) const{ + return ( w < other.w ); + } +}E[SIZE]; + +struct Point_{ + int x, y; +}P[SIZE]; + + +int N, M; + +inline int pow2(int P_){ + return P_ * P_; +} + +inline int dis(int i, int j){ + return pow2(P[i].x - P[j].x) + pow2(P[i].y - P[j].y); +} + +double Kruskal(){ + UnionFind UF(N); + double min_cost = 0; + int setSize = 1; + for(int it = 0; it < M; it++) + if( not UF.isSameSet(E[it].u - 1, E[it].v - 1) ) + setSize++, UF.unionSet(E[it].u - 1, E[it].v - 1); + M = 0; + for(int i = 0; i < N; i++) + for(int j = i + 1; j < N; j++) + E[M++] = Edge_(i, j, dis(i, j)); + sort(E, E + M); + for(int it = 0; it < M and setSize != N; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ) + min_cost += sqrt(E[it].w), setSize++, + UF.unionSet(E[it].u, E[it].v); + return min_cost; +} + +int main(){ + while(~scanf("%d", &N)){ + for(int it = 0; it < N; it++) + scanf("%d %d", &P[it].x, &P[it].y); + scanf("%d", &M); + for(int it = 0; it < M; it++) + scanf("%d %d", &E[it].u, &E[it].v); + printf("%.2lf\n", Kruskal()); + } + return(0); +} diff --git a/uva_cpp_clean/104/104-13.cpp b/uva_cpp_clean/104/104-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df3c59eafc91f48da36567e676a3499a07f6a17e --- /dev/null +++ b/uva_cpp_clean/104/104-13.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n; +vector ans; +vector visited; +float dist[25][25]; + +void initialize() +{ + ans.resize(n + 2); + + visited.clear(); + visited.resize((unsigned)n + 1, false); + + memset(dist, 0, sizeof dist); +} + +void backtrack(int start, int curr, float money) +{ + for (int i = 1; i <= n; ++i) + { + if (!visited[i]) + { + visited[i] = true; + + if (money * dist[curr][i] * dist[i][start] > 1.01) + { + vector temp; + + temp.push_back(start); + + for (int j = 1; j <= n; ++j) + { + if (visited[j] && j != start) temp.push_back(j); + } + + temp.push_back(start); + + if (temp.size() < ans.size()) + { + ans = temp; + } + } + + backtrack(start, i, money * dist[curr][i]); + visited[i] = false; + } + } +} + +int main() +{ + while (cin >> n) + { + initialize(); + + for (int i = 1; i <= n; ++i) + { + for (int j = 1; j <= n; ++j) + { + if (i == j) continue; + + cin >> dist[i][j]; + } + } + + for (int k = 1; k <= n; ++k) + { + visited.clear(); + visited.resize((unsigned)n + 1, false); + + visited[k] = true; + + backtrack(k, k, 1.0); + } + + if (ans.size() == n + 2) cout << "no arbitrage sequence exists" << endl; + + else + { + for (int l = 0; l < ans.size(); ++l) + { + cout << ans[l] << " "; + } + + cout << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/104/104-21.cpp b/uva_cpp_clean/104/104-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7261112ac9bcf42b02f53736a3a2e2e177c630ff --- /dev/null +++ b/uva_cpp_clean/104/104-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 104 + Name: Arbitrage + Problem: https://onlinejudge.org/external/1/104.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define INF 1e100 +double d[22][22], M[22][22]; +int p[22][22], path[22]; + +int main(){ + int n; + double x; + while (cin>>n) { + for (int i=0; i>x; + M[i][j] = -log(x); + } + } + + int mn = n; + for (int s=0; s1.01) { + mn = i; + int u = s; + for (int j=i; j>=0; j--) { + path[j+1] = u+1; + u = p[j][u]; + } + path[0] = s+1; + break; + } + } + } + + if (mn == n) + puts("no arbitrage sequence exists"); + else { + cout << path[0]; + for (int i=1; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10400 + Name: Game Show Math + Problem: https://onlinejudge.org/external/104/10400.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int t,n,A[101]; +char P[64009][101]; +bool f(int b,int i){ + if(b<-32000 || b>32000)return false; + if(i==n) + if(b==t) + return true; + else + return false; + + char &ans=P[b+32000][i]; + if(ans==-1){ + if(f(b+A[i],i+1)) + ans='+'; + else if(f(b-A[i],i+1)) + ans='-'; + else if(f(b*A[i],i+1)) + ans='*'; + else if(b%A[i]==0 && f(b/A[i],i+1)) + ans='/'; + else + ans=0; + } + return ans; +} + +int main(){ + int T,i,b; + cin>>T; + while(T--){ + cin>>n; + for(i=0;i>A[i]; + cin>>t; + memset(P,-1,sizeof(P)); + if(f(A[0],1)){ + cout< + +#define MAX_N 110 +#define SIZE 64010 + +typedef long long ll; + +using namespace std; + +const int lower = -32000, upper = 32000; +int tc,p,v[MAX_N],target; +bool visited[MAX_N][SIZE],memo[MAX_N][SIZE]; +short take[MAX_N][SIZE]; + +void printPath(int pos,int current){ + if(pos==p){ + printf("%d=%d\n",v[pos-1],v[pos]); + return; + } + + if(take[pos][current]==1){ + printf("%d+",v[pos-1]); + printPath(pos+1,current+v[pos]); + } + + else if(take[pos][current]==2){ + printf("%d-",v[pos-1]); + printPath(pos+1,current-v[pos]); + } + + else if(take[pos][current]==3){ + printf("%d*",v[pos-1]); + printPath(pos+1,current*v[pos]); + } + + else{ + printf("%d/",v[pos-1]); + printPath(pos+1,current/v[pos]); + } +} + +bool dp(int pos,int current){ + if(pos==p) return (current==v[p]); + if(visited[pos][current]) return memo[pos][current]; + visited[pos][current] = true; + + if(current+v[pos]lower && !memo[pos][current]){ + take[pos][current] = 2; + memo[pos][current] |= dp(pos+1,current-v[pos]); + } + + if(lower<(ll)current*v[pos] && (ll)current*v[pos]>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10401 + Name: Injured Queen Problem + Problem: https://onlinejudge.org/external/104/10401.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +long long C[17][17]; +void inc(long long X[], int n, long long x, int hole) { + for (int i=0; ihole+1) + X[i] += x; +} + +char nums[] = "123456789ABCDEF", nid[127]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i=0; nums[i]; ++i) + nid[nums[i]] = i; + + string s; + while (cin >> s) { + int n = s.length(); + for (int i=0; i +#include +using namespace std; + +int main() { + int n, moves[10]; + bitset<1000004> bs; + while (cin >> n) { + int m; + cin >> m; + bs.reset(); + for (int i = 0; i < m; i++) cin >> moves[i], bs.set(moves[i]); + for (int i = 1; i <= n; i++) + for (int j = 0; j < m && !bs[i]; j++) + if (i+moves[j] <= n) bs.set(i+moves[j]); + if (bs[n]) printf("Stan wins\n"); + else printf("Ollie wins\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10404/10404-21.cpp b/uva_cpp_clean/10404/10404-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df59cbbec968eeca49a94e47b5cd889ffe19af76 --- /dev/null +++ b/uva_cpp_clean/10404/10404-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10404 + Name: Bachet's Game + Problem: https://onlinejudge.org/external/104/10404.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +short DP[1000001]; +int d[15]; + +int main(){ + int m,n,i,j; + + while(scanf("%d%d",&n,&m)==2){ + for(i=0;i>d[i]; + + DP[0]=0; + for(i=1;i=0) && DP[i-d[j]]==0) + DP[i]=1; + } + + if(DP[n]) + puts("Stan wins"); + else + puts("Ollie wins"); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10404/10404-9.cpp b/uva_cpp_clean/10404/10404-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16d95681c2d80370d4fdee1513595a1bddafede2 --- /dev/null +++ b/uva_cpp_clean/10404/10404-9.cpp @@ -0,0 +1,49 @@ +#include + +#define P_POS 1 +#define N_POS 2 +#define U_POS 3 + +using namespace std; + +const int MAX_M = 12, MAX_N = 1e6 + 10; + +int n, m, s[MAX_M], dp[MAX_N]; + +void print() { + puts(dp[n] == N_POS ? "Stan wins" : "Ollie wins"); +} + +bool isNposition(int id) { + bool ret = false; + for (int k = 0; k < m and not ret; k++) if (id - s[k] >= 0) ret |= dp[id - s[k]] == P_POS; + return ret; +} + +void setNpositions(int id) { + for (int k = 0; k < m; k++) if (id + s[k] <= n) dp[id + s[k]] = N_POS; +} + +void solve() { + dp[0] = P_POS; + setNpositions(0); + for (int id = 1; id <= n; id++) if (dp[id] == U_POS) dp[id] = isNposition(id) ? N_POS : P_POS; +} + +void read() { + for (int i = 0; i < m; i++) scanf("%d", s + i); +} + +void clear() { + fill(dp, dp + n + 1, U_POS); +} + +int main() { + while (~scanf("%d %d", &n, &m)) { + clear(); + read(); + solve(); + print(); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10405/10405-14.cpp b/uva_cpp_clean/10405/10405-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de693258b218587916f2d86e73597d930b3828ac --- /dev/null +++ b/uva_cpp_clean/10405/10405-14.cpp @@ -0,0 +1,114 @@ +/*************************************************** + * Problem Name : 10405 - Longest Common Subsequence(Iterative).cpp + * Problem Link : https://uva.onlinejudge.org/external/104/10405.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-04-04 + * Problem Type : DP(iterative) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 1005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +string A, B; +int dp[MAX][MAX]; +int lcsIterative (int sizeA, int sizeB) { + string AA, BB; + AA.resize (sizeA + 1); + BB.resize (sizeB + 1); + + for (int i = 0; i < sizeA; i++) { + AA[i + 1] = A[i]; + } + + for (int i = 0; i < sizeB; i++) { + BB[i + 1] = B[i]; + } + + sizeA = (int) AA.size(); + sizeB = (int) BB.size(); + + for (int i = 0; i < sizeA; i++) { + dp[0][i] = 0; + } + + for (int i = 0; i < sizeB; i++) { + dp[i][0] = 0; + } + + for (int j = 1; j < sizeB; j++) { + for (int i = 1; i < sizeA; i++) { + if (AA[i] == BB[j]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + + } else { + dp[i][j] = max (dp[i][j - 1], dp[i - 1][j]); + } + } + } + + return dp[sizeA - 1][sizeB - 1]; +} + +int main () { + __FastIO; + while (getline (cin, A) ) { + getline (cin, B); + int ans = lcsIterative ( (int) A.size(), (int) B.size() ); + cout << ans << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/10405/10405-19.cpp b/uva_cpp_clean/10405/10405-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e637082d04470eeb80b1ce76bf032d3002d238cb --- /dev/null +++ b/uva_cpp_clean/10405/10405-19.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +int main() { + string s1, s2; + int grid[1002][1002]; + for (int i = 0; i < 1002; i++) grid[0][i] = grid[i][0] = 0; + while (getline(cin, s1) && getline(cin, s2)) { + bool lock = false; + for (int i = 0; i < s1.length(); i++) { + for (int j = 0; j < s2.length(); j++){ + grid[i+1][j+1] = grid[i+1][j]; + if (s1[i] == s2[j] && !lock) grid[i+1][j+1]++, lock = true; + if (grid[i][j+1] >= grid[i+1][j+1]) grid[i+1][j+1] = grid[i][j+1], lock = false; + } + } + cout << grid[s1.length()][s2.length()] << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10405/10405-20.cpp b/uva_cpp_clean/10405/10405-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2c70b6f223034c6be1fad272f889e62327017ec --- /dev/null +++ b/uva_cpp_clean/10405/10405-20.cpp @@ -0,0 +1,33 @@ +#include +#include + +#define MAX 1005 + +char X[MAX], Y[MAX]; +int c[MAX][MAX]; + +int LCS() +{ + int m = strlen(X); + int n = strlen(Y); + + for(int i=1 ; i<=m ; i++) + for(int j=0 ; j<=n ; j++) c[i][j] = 0; + + for(int i=1 ; i<=m ; i++) + for(int j=1 ; j<=n ; j++) + { + if( X[i-1]==Y[j-1] ) c[i][j] = c[i-1][j-1]+1; + else if( c[i-1][j]>=c[i][j-1] ) c[i][j] = c[i-1][j]; + else c[i][j] = c[i][j-1]; + } + + return c[m][n]; +} + +int main() +{ + while( gets(X) && gets(Y) ) + printf("%d\n", LCS()); + return 0; +} diff --git a/uva_cpp_clean/10405/10405-21.cpp b/uva_cpp_clean/10405/10405-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ada7839092098e8f87b043300a20fdb12e00d816 --- /dev/null +++ b/uva_cpp_clean/10405/10405-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10405 + Name: Longest Common Subsequence + Problem: https://onlinejudge.org/external/104/10405.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int max(int a,int b){ + return (a>b)?a:b; +} + +int main() +{ + char line1[1003],line2[1003]; + short c[1003][1003]; + int len1,len2,i,j; + + for(i=0;i<1003;i++) + c[i][0]=c[0][i]=0; + + while (gets(line1+1) && gets(line2+1)) + { + len1=strlen(line1+1); + len2=strlen(line2+1); + for(i=1;i<=len1;i++) + for(j=1;j<=len2;j++) + if(line1[i]==line2[j]) + c[i][j]=c[i-1][j-1]+1; + else + c[i][j]=max(c[i-1][j],c[i][j-1]); + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int n,m,lcs[2000][2000]; +int main() +{ + string s1,s2; + + while(getline(cin,s1)) + { + getline(cin,s2); + n=s1.size(); + m=s2.size(); + for(int i=1;i<=n;i++) + for(int j=1;j<=m;j++) + { + if(s1[i-1]==s2[j-1]) + lcs[i][j]=lcs[i-1][j-1]+1; + else + lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]); + } + cout< longest common subsequence (LCS) + difficulty: none + date: none + by: @brpapa +*/ +#include +#include +using namespace std; + +int memo[1010][1010]; +string a, b; + +int lcsTD(int i, int j) { + //estado: indice i de a, indice j de b + + if (i == a.size() || j == b.size()) + return 0; + + int &m = memo[i][j]; + if (m != -1) return m; + + if (a[i] == b[j]) + return m = 1+lcsTD(i+1, j+1); + + return m = max( + lcsTD(i+1, j), + lcsTD(i, j+1) + ); +} + +//preenche tab de baixo para cima, da direita para a esquerda +int lcsBU() { + int tab[1010][1010]; //tabulation + + for (int i = a.size(); i >= 0; i--) { + for (int j = b.size(); j >= 0; j--) { + + if (i == a.size() || j == b.size()) //casos base + tab[i][j] = 0; + + else if (a[i] == b[j]) + tab[i][j] = 1+tab[i+1][j+1]; + else { + tab[i][j] = max( + tab[i+1][j], + tab[i][j+1] + ); + } + } + } + return tab[0][0]; +} + +int main() { + while (getline(cin, a) && getline(cin, b)) { + // memset(memo, -1, sizeof(memo)); //caro para >>> 1010 + // cout << lcsTD(0,0) << endl; + + cout << lcsBU() << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10405/10405-9.cpp b/uva_cpp_clean/10405/10405-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03caa32ed7588167780f2c916c7b1c1352b18d19 --- /dev/null +++ b/uva_cpp_clean/10405/10405-9.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +const int MAX_LEN = 1e3 + 10; + +string s1, s2; +int dp[MAX_LEN][MAX_LEN]; + +int lcs () { + int n = s1.size(), m = s2.size(); + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (s1[i - 1] == s2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; + else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[n][m]; +} + +int main () { + while (getline(cin, s1), getline(cin, s2)) cout << lcs() << endl; + return (0); +} diff --git a/uva_cpp_clean/10407/10407-19.cpp b/uva_cpp_clean/10407/10407-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c38884d89a5512866eb0b8a60bce928b2ffb5144 --- /dev/null +++ b/uva_cpp_clean/10407/10407-19.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +typedef long long ll; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +int main() { + ll n, m, g; + while (cin >> n && n != 0) { + cin >> m; + g = n - m; + n = m; + while (cin >> m && m != 0) { + g = gcd(g, n - m); + n = m; + } + if (g < 0) g = -g; + printf("%lld\n", g); + } + return 0; +} diff --git a/uva_cpp_clean/10407/10407-21.cpp b/uva_cpp_clean/10407/10407-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..036b40e000fe4c1ee2296da1074c53cf8de2e758 --- /dev/null +++ b/uva_cpp_clean/10407/10407-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10407 + Name: Simple division + Problem: https://onlinejudge.org/external/104/10407.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int gcd(int a, int b) { + return a ? gcd(b%a, a) : b; +} + +int main() { + int a, b; + while (cin>>a && a) { + int rs = 0; + while (cin>>b && b) + if (!rs) rs = fabs(a-b); + else if (a-b) + rs = gcd(rs, fabs(a-b)); + cout << rs << endl; + } +} diff --git a/uva_cpp_clean/10407/10407-9.cpp b/uva_cpp_clean/10407/10407-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..446d4c3a50b5dc70157d2ca5c886ff0a4c02aafb --- /dev/null +++ b/uva_cpp_clean/10407/10407-9.cpp @@ -0,0 +1,23 @@ +#include + +#define SIZE 1010 + +using namespace std; + +int pib, ct, num, v[SIZE], ans; + +int gcd(int a, int b){ + return b == 0 ? a : gcd(b , a % b); +} + +int main(){ + while(scanf("%d", &pib), pib){ + ct = 0; + while(scanf("%d", &num), num) v[ct++] = num; + for(int it = 0; it < ct; it++) v[it] = abs(v[it] - pib); + ans = v[0]; + for(int it = 1; it < ct; it++) ans = gcd( ans, v[it]); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10408/10408-19.cpp b/uva_cpp_clean/10408/10408-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fe53580bbc7e6622690acbce69a6565d7e68144 --- /dev/null +++ b/uva_cpp_clean/10408/10408-19.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef pair ii; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +struct comp { + bool operator() (const ii& lhs, const ii& rhs) const { + return lhs.first * rhs.second < rhs.first * lhs.second; + } +}; + +int main() { + set Farey; + Farey.insert(ii(1,1)); + for (int i = 2; i <= 1000; i++) { + for (int j = 1; j < i; j++) { + if (gcd(j, i) == 1) { + Farey.insert(ii(j, i)); + } + } + } + int n, k; + while (cin >> n >> k) { + auto it = Farey.begin(); + while (true) { + if (it->second <= n) { + k--; + if (k == 0) break; + } + ++it; + } + printf("%d/%d\n", it->first, it->second); + } + return 0; +} diff --git a/uva_cpp_clean/10408/10408-21.cpp b/uva_cpp_clean/10408/10408-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b42b3daa1b96c6fe6e304ca3d71d8edb7ff5d768 --- /dev/null +++ b/uva_cpp_clean/10408/10408-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10408 + Name: Farey sequences + Problem: https://onlinejudge.org/external/104/10408.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +int gcd(int a, int b) { + return !a ? b : gcd(b%a, a); +} + +int phi(int n) { + int sq = sqrt(n); + int m = n; + for (int i=2; i<=sq; i++) + if (n%i == 0) { + while (n%i==0) n /= i; + m /= i; + m *= i - 1; + sq = sqrt(n); + } + if (n > 1) m = (m/n) * (n-1); + return m; +} + +void forward(int n, int &a, int &b, int &c, int &d) { + int r = (n+b)/d, + p = r*c-a, q = r*d-b; + a = c; b = d; + c = p; d = q; +} + +int szs[1001] = {0, 2}; +int main() { + for (int i=2; i<1001; i++) + szs[i] = szs[i-1] + phi(i); + + int n, k; + while (scanf("%d %d", &n, &k)==2) { + int a=0,b=1, c=1,d=n, rev=0; + if (k == szs[n]-1) c=d=1; + else { + if (k*2 > szs[n]) { + k = szs[n] - k - 1; + rev = 1; + } + for (int i=1; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10409 + Name: Die Game + Problem: https://onlinejudge.org/external/104/10409.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +void roll(int &x, int &y) { + int t = 7 - y; + y = x; + x = t; +} + +int main(){ + int n, t; + char dir[10]; + while(cin>>n && n) { + int x=1, y=2, z=3; + for (int i=0; i> dir; + switch(dir[0]) { + case 'n': + roll(x, y); + break; + case 's': + roll(y, x); + break; + case 'w': + roll(x, z); + break; + case 'e': + roll(z, x); + } + } + cout << x << endl; + } +} diff --git a/uva_cpp_clean/10409/10409-9.cpp b/uva_cpp_clean/10409/10409-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57c3d2c4571b62a138927f35ce2eee9f11c17f44 --- /dev/null +++ b/uva_cpp_clean/10409/10409-9.cpp @@ -0,0 +1,47 @@ +#include + +#define SIZE 10 + +using namespace std; + +int n, die[SIZE], tmp; +char command[SIZE]; + +void move1(){ + tmp = die[1]; + die[1] = die[5], die[5] = die[6], die[6] = die[2]; + die[2] = tmp; +} + +void move2(){ + tmp = die[1]; + die[1] = die[2], die[2] = die[6], die[6] = die[5]; + die[5] = tmp; +} + +void move3(){ + tmp = die[1]; + die[1] = die[3], die[3] = die[6], die[6] = die[4]; + die[4] = tmp; +} + +void move4(){ + tmp = die[1]; + die[1] = die[4], die[4] = die[6], die[6] = die[3]; + die[3] = tmp; +} + +int main(){ + while(scanf("%d\n", &n), n){ + for(int it = 1; it <= 6; it++) die[it] = it; + while(n--){ + scanf("%s", command); + if( command[0] == 'n' ) move1(); + else if( command[0] == 's' ) move2(); + else if( command[0] == 'e' ) move3(); + else move4(); + } + printf("%d\n", die[1]); + } + return(0); +} diff --git a/uva_cpp_clean/10420/10420-14.cpp b/uva_cpp_clean/10420/10420-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8de823d04079a38a3dbc6882c9fb6033df72ada9 --- /dev/null +++ b/uva_cpp_clean/10420/10420-14.cpp @@ -0,0 +1,95 @@ +/*************************************************** + * Problem Name : 10420 List of Conquests.cpp + * Problem Link : https://uva.onlinejudge.org/external/104/10420.pdf + * OJ : UVa + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : Easy + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + int n; + + while (cin >> n) { + mapmp; + cin.ignore(); + + for (int i = 0; i < n; i++) { + string str, tmp = ""; + getline (cin, str); + + for (int j = 0; j < (int) str.size(); j++) { + if (str[j] == ' ') { + break; + + } else { + tmp += str[j]; + } + } + + mp[tmp]++; + } + + for (auto it = mp.begin(); it != mp.end(); it++) { + cout << it->first << " " << it->second << "\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/10420/10420-16.cpp b/uva_cpp_clean/10420/10420-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c818c7e08cdda6dabcafd9b197bf8f36f7bda60 --- /dev/null +++ b/uva_cpp_clean/10420/10420-16.cpp @@ -0,0 +1,58 @@ +//============================================================================ +// Name : 10420 - List of Conquests.cpp +// Author : ztrixack +// Copyright : MIT +// Description : 10420 List of Conquests in C++, Ansi-style +// Run Time : 0.016 seconds +//============================================================================ + +#include +#include +#include +using namespace std; + +struct cty { + string locale; + int num; +}cty[2001]; + +int compare (const void *a, const void *b) { + if ((*(struct cty*)a).locale > (*(struct cty*)b).locale) + return 1; + else if ((*(struct cty*)a).locale < (*(struct cty*)b).locale) + return -1; + else + return 0; +} + +int main () { + int number, m; + int n = 0; + bool no; + string nation, tmp; + cin >> number; + getline(cin, tmp); + for (int i = 0; i < number; i++) { + nation.clear(); + tmp.clear(); + no = true; + getline(cin, tmp); + m = tmp.find(' '); + nation.append(tmp, 0, m + 1); + for (int j = 0; j < n; j++) { + if (cty[j].locale == nation) { + cty[j].num++; + no = false; + break; + } + } + if (no) { + cty[n].locale = nation; + cty[n].num++; + n++; + } + } + qsort (cty, n, sizeof(struct cty), compare); + for (int i = 0; i < n; i++) + cout << cty[i].locale << cty[i].num << endl; +} diff --git a/uva_cpp_clean/10420/10420-18.cpp b/uva_cpp_clean/10420/10420-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fb9bd9f95d177be4556b2b5d4fb957e52c7e6731 --- /dev/null +++ b/uva_cpp_clean/10420/10420-18.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int n; + map db; //A map sorts its keys internally + string line, country; + cin >> n; + getline(cin, line); + while(n-- > 0){ + cin >> country; + getline(cin, line); + db[country] +=1; + } + for(auto iterator = db.begin(); iterator != db.end(); iterator++){ + cout << iterator->first << " " << iterator->second << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10420/10420-19.cpp b/uva_cpp_clean/10420/10420-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0fe1c0df2b30d1fbb5688d7dbd7493db44be754 --- /dev/null +++ b/uva_cpp_clean/10420/10420-19.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +using namespace std; + +int main() { + map m; + string name, trash; + int N; + cin >> N; + while (N--) { + cin >> name; + getline(cin, trash); + if (!m.count(name)) m.insert(pair(name, 0)); + m[name]++; + } + for (auto it = m.begin(); it != m.end(); ++it) { + cout << it->first << " " << it->second << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10420/10420-21.cpp b/uva_cpp_clean/10420/10420-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e7855b2cea4c0835ae259cc740cbfe4f3209332 --- /dev/null +++ b/uva_cpp_clean/10420/10420-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10420 + Name: List of Conquests + Problem: https://onlinejudge.org/external/104/10420.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int a[1001], n; + while (cin >> n) { + int result = 0; + + for (int i = 0; i < n; i++) + cin >> a[i]; + + for (int i = 0; i a[j + 1]) { + result++; + a[j] = a[j] ^ a[j + 1]; + a[j + 1] = a[j] ^ a[j + 1]; + a[j] = a[j] ^ a[j + 1]; + } + + printf("Minimum exchange operations : %d\n", result); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10420/10420-5.cpp b/uva_cpp_clean/10420/10420-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..02d345ad25e54d3df1a825d8d70e4dd557dde422 --- /dev/null +++ b/uva_cpp_clean/10420/10420-5.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int n; + vectorv; + mapmp; + string s,a; + cin>>n; + getchar(); + while(n--) + { + cin>>s; + getchar(); + getline(cin,a); + if(mp[s]) + { + mp[s]++; + } + else + { + mp[s]++; + v.push_back(s); + } + } + sort(v.begin(),v.end()); + for(int i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10422 + Name: Knights in FEN + Problem: https://onlinejudge.org/external/104/10422.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +string finalS = "111110111100 110000100000"; +struct State { + string ss; + int d, x, y; + void calcw() { + int id = ss.find_first_of(' '); + y = id / 5; + x = id % 5; + } + int toInt() { + int xc=0, xy; + int result = 0; + for (int i=0; i<25; ++i) { + if (ss[i]=='0') ++xc; + else if (ss[i]==' ') xy=xc; + result = (result << 1) | (ss[i]=='1'); + } + return (result<<4)|xy; + } +}; + +char S[536870913]; +bool isg[] = { 0, 0, 1, 1, 1, 1, 1, 0, 0 }; +int dx[] = { 1, -1, 1, -1, 2, -2, 2, -2}, + dy[] = { 2, 2, -2, -2, 1, 1, -1, -1}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + cin.ignore(100, '\n'); + for (int cse=1; cse<=T; ++cse) { + State st; + string str; + for (int i=0; i<5; ++i) { + getline(cin, str); + while (str.length() < 5) + str += ' '; + st.ss += str.substr(0, 5); + } + st.calcw(); + st.d = 0; + + queue q; + q.push(st); + while (!q.empty()) { + State s = q.front(); q.pop(); + if (s.ss == finalS) { + cout << "Solvable in " << s.d << " move(s).\n"; + goto fin; + } + + if (s.d < 10) + for (int k=0; k<8; ++k) + if (isg[s.x+dx[k]+2] && isg[s.y+dy[k]+2]) { + State t(s); + t.x += dx[k]; + t.y += dy[k]; + swap(t.ss[s.y*5 + s.x], t.ss[t.y*5 + t.x]); + ++t.d; + + int id = t.toInt(); + if (S[id] != cse) { + S[id] = cse; + q.push(t); + } + } + } + cout << "Unsolvable in less than 11 move(s).\n"; + fin:; + } +} diff --git a/uva_cpp_clean/10422/10422-9.cpp b/uva_cpp_clean/10422/10422-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cedab15144f467b6368a07bb44cef91b0b78aeff --- /dev/null +++ b/uva_cpp_clean/10422/10422-9.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int tc, m[50], p, ans; +char s[10][10]; +vector G[50]; +int x[] = {-2,-2,2,2,-1,-1,1,1}; +int y[] = {-1,1,-1,1,-2,2,-2,2}; +int solution[] = {1,1,1,1,1, + 0,1,1,1,1, + 0,0,-1,1,1, + 0,0,0,0,1, + 0,0,0,0,0}; +int target = 12; + +bool isSolved(){ + for(int i = 0; i < 25; i++) + if(solution[i] != m[i]) return false; + return true; +} + +void xor_swap(int *a, int *b){ + *a ^= *b, *b ^= *a, *a ^= *b; +} + +void backtrack(int source,int steps){ + if(steps == 11) return; + if(source == target && isSolved()){ + ans = min(ans, steps); + return; + } + for(int i = 0; i < G[source].size(); i++){ + xor_swap(&m[source], &m[G[source][i]]); + backtrack(G[source][i], steps + 1); + xor_swap(&m[source], &m[G[source][i]]); + } +} + +inline bool isValid(int row, int column){ + return (row >= 0 && column >= 0 && row < 5 && column < 5); +} + +void initGraph(){ + for(int i = 0; i < 5; i++) + for(int j = 0; j < 5; j++) + for(int k = 0; k < 8; k++){ + int p = i + x[k], q = j + y[k]; + if(isValid(p, q)){ + int pp = i * 5 + j, qq = p * 5 + q; + G[pp].push_back(qq); + } + } +} + +void initMatrix(){ + for(int i = 0; i < 5; i++) + for(int j = 0; j < 5; j++) + if(s[i][j] == '1') m[i * 5 + j] = 1; + else if(s[i][j] == '0') m[i * 5 + j] = 0; + else if(s[i][j] == ' ') m[i * 5 + j] = -1, p = i * 5 + j; +} + +int main(){ + initGraph(); + scanf("%d\n", &tc); + while(tc--){ + ans = 12; + for(int i = 0; i < 5; i++) cin.getline(s[i], 10); + initMatrix(); + backtrack(p, 0); + if(ans == 12) printf("Unsolvable in less than 11 move(s).\n"); + else printf("Solvable in %d move(s).\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10424/10424-14.cpp b/uva_cpp_clean/10424/10424-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2dfe53a961c4b24291d3c5d8780eedf1435f1386 --- /dev/null +++ b/uva_cpp_clean/10424/10424-14.cpp @@ -0,0 +1,102 @@ +/*************************************************** + * Problem name : 10424 - Love Calculator.cpp + * Problem Link : https://uva.onlinejudge.org/external/104/10424.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-08-04 + * Problem Type : String (Adhoc) + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int make_num(string str) { + int sz = str.size(), sum = 0; + for (int i = 0; i < sz; i++) { + if (isalpha(str[i])) { + if (str[i] >= 'A' && str[i] <= 'Z') { + str[i] = tolower(str[i]); + } + sum = sum + (str[i] - 'a' + 1); + } + } + return sum; +} +int add(int n) { + int sum = 0; + while (n != 0) { + sum += n % 10; + n /= 10; + } + return sum; +} +int make_one(int n) { + if (n <= 9) return n; + while (n >= 10) { + n = add(n); + } + return n; +} +int main () { + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + string a, b; + while (getline(cin, a)) { + getline(cin, b); + int aa = make_one(make_num(a)); + int bb = make_one(make_num(b)); + //~ printf("%d %d\n", aa, bb); + double love; + if (aa > bb) { + love = (double)bb / (double)aa * 100.0; + } else { + love = (double)aa / (double)bb * 100.0; + } + printf("%.2lf %%\n", love); + } + return 0; +} + diff --git a/uva_cpp_clean/10424/10424-2.cpp b/uva_cpp_clean/10424/10424-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..806bdb9788668715a34e2c56c706193214b91631 --- /dev/null +++ b/uva_cpp_clean/10424/10424-2.cpp @@ -0,0 +1,88 @@ + +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int num_of_dig(int n) { + int ret = 0; + while (n!=0) { + ret++; + n/=10; + } + return ret; +} + +int sum_to_digit(int n) { + if (num_of_dig(n) == 1) return n; + int ret = 0; + while (n != 0) { + ret = ret + n % 10; + n = n/10; + } + //cout << ret << el; + return sum_to_digit(ret); +} + +int calc_val(string& s1) { + int ret = 0; + for (int i = 0; i < (int) s1.size(); ++i) { + if (s1[i] == ' ') continue; + else if ((s1[i] >= 'a' && s1[i] <= 'z') || (s1[i] >= 'A' && s1[i] <= 'Z')) { + char x = tolower(s1[i]); + string ch = ""; + ch += x; + size_t found = ALPHA.find(ch); + if (found != string::npos) ret += (found+1); + } + } + return sum_to_digit(ret); +} + +double solve(string s1, string s2) { + double x = calc_val(s1), y = calc_val(s2) * 1.0; + //cout << x << ' ' << y << el; + return y < x ? (double) y / x : (double) x / y; +} + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + string s1, s2; + //cin.ignore(); + while (getline(cin, s1)) { + getline(cin, s2); + printf("%0.2lf", (solve(s1, s2)*100)); + cout << " %\n"; + } + return 0; +} + diff --git a/uva_cpp_clean/10424/10424-21.cpp b/uva_cpp_clean/10424/10424-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc6d38291061058467d62635a9fce9939d581475 --- /dev/null +++ b/uva_cpp_clean/10424/10424-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10424 + Name: Love Calculator + Problem: https://onlinejudge.org/external/104/10424.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int dec(int x) { + if (x<10) return x; + + int s = 0; + for(; x; x/=10) + s+=x%10; + return dec(s); +} + +int val(char s[]) { + int r = 0; + for(int i=0; s[i]; i++) + if (s[i]>='A' && s[i]<='Z') + r+=s[i]-'A'+1; + else if (s[i]>='a' && s[i]<='z') + r+=s[i]-'a'+1; + + return dec(r); +} + +int main(){ + char nx[100], ny[100]; + while(cin.getline(nx, 100) && cin.getline(ny, 100) && (nx[0] || ny[0])) { + int x = val(nx), + y = val(ny); + + printf("%.2f \%\n", 100.0*min(x,y)/max(x,y)); + } +} diff --git a/uva_cpp_clean/10424/10424-5.cpp b/uva_cpp_clean/10424/10424-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..52ad49d21451a8bbdb306b435c68f3a82e2cf0a3 --- /dev/null +++ b/uva_cpp_clean/10424/10424-5.cpp @@ -0,0 +1,68 @@ +#include +#include +int main() +{ + char n1[30],n2[30]; + int i,l1,l2,t1,t2,s1,s2; + float s; + while(gets(n1)) + { + gets(n2); + l1=strlen(n1); + l2=strlen(n2); + t1=0; + for(i=0;i='a'&&n1[i]<='z') + t1+=n1[i]-96; + else if(n1[i]>='A'&&n1[i]<='Z') + t1+=n1[i]-64; + } + s1=0; + while(t1!=0) + { + s1+=t1%10; + t1/=10; + } + if(s1>9) + { + t1=s1; + s1=0; + while(t1!=0) + { + s1+=t1%10; + t1/=10; + } + } + t2=0; + for(i=0;i='a'&&n2[i]<='z') + t2+=n2[i]-96; + else if(n2[i]>='A'&&n2[i]<='Z') + t2+=n2[i]-64; + } + s2=0; + while(t2!=0) + { + s2+=t2%10; + t2/=10; + } + if(s2>9) + { + t2=s2; + s2=0; + while(t2!=0) + { + s2+=t2%10; + t2/=10; + } + } + if(s1>s2) + s=(float)s2*100/(float)s1; + else + s=(float)s1*100/(float)s2; + printf("%0.2f %%\n",s); + } +} + diff --git a/uva_cpp_clean/10424/main.cpp b/uva_cpp_clean/10424/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ecc7aefc6e5058089041fd8eaf3cf542146435c --- /dev/null +++ b/uva_cpp_clean/10424/main.cpp @@ -0,0 +1,51 @@ +#include +#include +using namespace std; + +string lower(string s){ + string res; + for (int i = 0; i < s.size(); i++){ + if((int)s[i] <= (int)'Z' && (int)s[i] >= (int)'A') res += (char)((int)(s[i])+32); + else res+= s[i]; + } + return res; +} + +int simplify(int counter){ + if(counter < 10) return counter; + int res = 0; + while(counter > 0){ + res += counter%10; + counter /= 10; + } + return simplify(res); +} + +int rate_counter(string s){ + int counter = 0; + for (int i = 0; i < s.size(); i++){ + if((int)s[i] <= (int)'z' && (int)s[i] >= (int)'a') counter += (int)s[i] - 96; + } + return simplify(counter); +} + +int main(){ + + string name, lover; + int c_name, c_lover; + double res1, res2; + while(getline(cin, name) && getline(cin, lover)){ + c_name = rate_counter(lower(name)); + c_lover = rate_counter(lower(lover)); + if(c_name == 0 && c_lover == 0) cout << endl; + else{ + res1 = (double)(c_name) / c_lover; + res2 = (double)(c_lover) / c_name; + if(max(res1, res2) > 1) printf("%4.2f ",min(res1, res2)*100); + else printf("%4.2f ",max(res1, res2)*100); + cout << '%' << endl; + } + } + +return 0; +} diff --git a/uva_cpp_clean/10430/10430-21.cpp b/uva_cpp_clean/10430/10430-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31273a87dd29c4d11bf7d685c9dcd9c920c9c93b --- /dev/null +++ b/uva_cpp_clean/10430/10430-21.cpp @@ -0,0 +1,325 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10430 + Name: Dear GOD + Problem: https://onlinejudge.org/external/104/10430.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + + if (cells) delete [] cells; + cells = ncells; + } + if (!cp) + memset(cells, 0, capacity*CellSize); + else memset(cells+sz, 0, (capacity-sz)*CellSize); + return size = sz; + } + + /* + BigInt& shl(int q) { + setSize(size + q); + for (int i=size-1; i>=0; i--) + cells[i+q] = cells[i]; + for (int i=0; i0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + // --------------------------------------------------------------------- + /* + BigInt(CellType* cells, int size): cells(cells), size(size) { + sign = sgn(size); + capacity = -1; + autoSize(); + };*/ + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + BigInt(const BigInt& o): cells(0), capacity(0) { + sign = o.sign; + setSize(o.size, 0); + for (int i=0; i= 0) + delete [] cells; + } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { + return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign; + } + bool operator == (const BigInt &o) const { return sign==o.sign ? numEq(o) : 0; } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + for (int i=size; i= MAX_C ) { + cells[i] -= MAX_C; + cells[i+1]++; + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i=0; --i) { + t = t*MAX_C + cells[i]; + cells[i] = t / den; + t -= cells[i] * den; + } + return autoSize(); + } + BigInt operator/ (int den) const { return *new BigInt(*this) *= den; } + + // --------------------------------------------------------------------- + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + +// tostring, input from char + + + + +bool powseen[10][100]; +BigInt pmemo[10][100]; +BigInt pow(int a, int b) { + if (!b) return BigInt(1); + if (b==1 || a<=1) + return BigInt(a); + + if (powseen[a][b]) + return pmemo[a][b]; + + pmemo[a][b] = pow(a, b>>1); + pmemo[a][b] *= pmemo[a][b]; + if (b&1) pmemo[a][b] *= a; + + powseen[a][b] = 1; + return pmemo[a][b]; +} + + +int main() { + puts("Dear GOD, Pardon Me"); + + int t, n; + bool frst = 1; + while (scanf("%d%d", &t, &n)==2) { + if (frst) frst = 0; + else putchar('\n'); + + if (t == 1) + printf("X = %d\nK = 1\n", n); + else { + BigInt k = pow(t, n); + BigInt x = k - 1; + x /= t-1; + + cout << "X = " << x << endl; + cout << "K = " << k << endl; + } + } +} diff --git a/uva_cpp_clean/10432/10432-19.cpp b/uva_cpp_clean/10432/10432-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eaf2f78e25aa9634f44650a80902f0149bca3aa4 --- /dev/null +++ b/uva_cpp_clean/10432/10432-19.cpp @@ -0,0 +1,12 @@ +#include +#include +using namespace std; + +int main() { + double r, n, pi = 2*acos(0); + while (cin >> r >> n) { + double a = n * sin(2*pi / n) * r * r * 0.5; + printf("%.3f\n", a); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10432/10432-21.cpp b/uva_cpp_clean/10432/10432-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b1b25b9457b7fefbd9d2aed0cd86ee715f061b5 --- /dev/null +++ b/uva_cpp_clean/10432/10432-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10432 + Name: Polygon Inside A Circle + Problem: https://onlinejudge.org/external/104/10432.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define PI 3.1415926535897932 +int main(){ + double r, n; + while (cin>>r>>n) { + double alpha = PI/n; + printf("%.3f\n", n*r*r*sin(alpha)*cos(alpha)); + } +} diff --git a/uva_cpp_clean/10432/10432-4.cpp b/uva_cpp_clean/10432/10432-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa8bde9a2e828b4ae72fb434bbbae95c2c3afcb6 --- /dev/null +++ b/uva_cpp_clean/10432/10432-4.cpp @@ -0,0 +1,16 @@ +#include +#include +#include + +int main() +{ + double r, n; + + while (std::cin >> r >> n) + { + double area = n * r * r / 2 * sin(2 * M_PI / n); + std::cout << std::setprecision(3) << std::fixed << area << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10432/10432-5.cpp b/uva_cpp_clean/10432/10432-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ecc3279c3107f35f146502291434c766ef1cd559 --- /dev/null +++ b/uva_cpp_clean/10432/10432-5.cpp @@ -0,0 +1,39 @@ +/*-------------------------------------------------------------*/ +//Poblem Setter : Suman Mahbub +//Uva Problem No: 10432 +//Problem Name : Polygon Inside A Circle +//Type : Geometry, Area of a regular convex polygon. +//Author : Shipu Ahamed +//University : BUBT +//E-mail : shipuahamed01@gmail.com +/*--------------------------------------------------------------*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + double r,s,p; + while(sc("%lf %lf",&r,&s)==2) + pf("%.3lf\n",r*r*s*sin((2*pi)/s)/2); + return 0; +} diff --git a/uva_cpp_clean/10432/10432-9.cpp b/uva_cpp_clean/10432/10432-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e735bef5bd81aacdc2898acbcaa93e5a5b5fe8c6 --- /dev/null +++ b/uva_cpp_clean/10432/10432-9.cpp @@ -0,0 +1,11 @@ +#include + +const double PI = acos(-1.0); + +double r, n; + +int main(){ + while(~scanf("%lf %lf", &r, &n)) + printf("%.3lf\n", n * r * r * sin(2.0 * PI / n) / 2.0); + return(0); +} diff --git a/uva_cpp_clean/10440/10440-21.cpp b/uva_cpp_clean/10440/10440-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b993524710c2dacc13e41b27ce9001f470e6968a --- /dev/null +++ b/uva_cpp_clean/10440/10440-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10440 + Name: Ferry Loading II + Problem: https://onlinejudge.org/external/104/10440.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, c, t, m, x[2000]; + cin>>T; + while (T--) { + cin>>c>>t>>m; + for (int i=0; i>x[i]; + + int i, cnt, tm=-t, fr=m%c; + if (fr) fr = c-fr; + for (i=cnt=0; ifr)) { + tm = max(tm, x[i]); + cu++; + i++; + } + + fr -= c-cu; + tm += t; + } + + printf("%d %d\n", tm, cnt); + } +} diff --git a/uva_cpp_clean/10443/10443-19.cpp b/uva_cpp_clean/10443/10443-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37a97407165f1b34f502fbdaf0c98cfb0b3a11dc --- /dev/null +++ b/uva_cpp_clean/10443/10443-19.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +int mat1[101][101], mat2[101][101]; +int r, c, n; + +inline bool limits(int i, int j) { + return i < r && j < c && i >= 0 && j >= 0; +} + +int main() { + int T; + cin >> T; + while (T--) { + cin >> r >> c >> n; + for (int i = 0; i < r; i++) { + for (int j = 0; j < c; j++) { + char c; + cin >> c; + if (c == 'R') mat1[i][j] = 0; + else if (c == 'S') mat1[i][j] = 1; + else if (c == 'P') mat1[i][j] = 2; + } + } + auto mat = mat1, next = mat2; + for (int t = 0; t < n; t++) { + for (int i = 0; i < r; i++) { + for (int j = 0; j < c; j++) { + next[i][j] = mat[i][j]; + } + } + for (int i = 0; i < r; i++) { + for (int j = 0; j < c; j++) { + if (limits(i+1, j) && (mat[i][j]+1)%3 == mat[i+1][j]) next[i+1][j] = mat[i][j]; + if (limits(i-1, j) && (mat[i][j]+1)%3 == mat[i-1][j]) next[i-1][j] = mat[i][j]; + if (limits(i, j+1) && (mat[i][j]+1)%3 == mat[i][j+1]) next[i][j+1] = mat[i][j]; + if (limits(i, j-1) && (mat[i][j]+1)%3 == mat[i][j-1]) next[i][j-1] = mat[i][j]; + } + } + auto auxi = mat; + mat = next; + next = auxi; + } + for (int i = 0; i < r; i++) { + for (int j = 0; j < c; j++) { + char ch; + if (mat[i][j] == 0) ch = 'R'; + else if (mat[i][j] == 1) ch = 'S'; + else if (mat[i][j] == 2) ch = 'P'; + printf("%c", ch); + } + printf("\n"); + } + if (T) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10443/10443-21.cpp b/uva_cpp_clean/10443/10443-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d523c98307e9db938db5735910680fcb68a69474 --- /dev/null +++ b/uva_cpp_clean/10443/10443-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10443 + Name: Rock, Scissors, Paper + Problem: https://onlinejudge.org/external/104/10443.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +bool canDefeat[200][200]; +int main(){ + canDefeat['P']['R'] = + canDefeat['R']['S'] = + canDefeat['S']['P'] = 1; + + int T, n, m, r; + char s1[101][101], s2[101][101]; + scanf("%d", &T); + while (T--) { + scanf("%d%d%d", &n, &m, &r); + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10446 + Name: The Marriage Interview :-) + Problem: https://onlinejudge.org/external/104/10446.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + unsigned long long DP[61][61]; + for (int n=0; n<61; ++n) + for (int b=0; b<61; ++b) { + DP[n][b] = 1; + if (n>1) + for (int i=1; i<=b; ++i) + DP[n][b] += i> n >> k && (n<61); ++cse) + cout << "Case " << cse << ": " << (n<0 ? 1 : DP[n][k]) << "\n"; +} diff --git a/uva_cpp_clean/10446/10446-9.cpp b/uva_cpp_clean/10446/10446-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a4dac1e3312f7ead62354c9711ad9c2458885b3 --- /dev/null +++ b/uva_cpp_clean/10446/10446-9.cpp @@ -0,0 +1,24 @@ +#include + +#define SIZE 70 + +using namespace std; + +int tc,n,back; +unsigned long long memo[SIZE][SIZE]; + +unsigned long long trib(int n,int back){ + unsigned long long sum = 0; + if(n<=1) return 0; + if(~memo[n][back]) return memo[n][back]; + for(int i=1;i<=back;i++) + sum += 1+trib(n-i,back); + return memo[n][back] = sum; +} + +int main(){ + memset(memo,-1,sizeof memo); + while(scanf("%d %d",&n,&back),n<=60) + printf("Case %d: %llu\n",++tc,trib(n,back)+1); + return(0); +} diff --git a/uva_cpp_clean/10449/10449-13.cpp b/uva_cpp_clean/10449/10449-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c0d891a4c93a51a57783004692a2c6bcd53665 --- /dev/null +++ b/uva_cpp_clean/10449/10449-13.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, m, t = 1, q; +#define MAX 100000007 + +ofstream fout("f.out"); + +vector label, dist; +vector > graph; + +void bellman_ford() +{ + dist[1] = 0; + + for (int k = 0; k < n - 1; k++) + { + for (int i = 1; i <= n; i++) + { + for (int j = 0; j < graph[i].size(); j++) + { + int child = graph[i][j]; + + int weight = label[child] - label[i]; + + weight = weight * weight * weight; + + if (dist[i] + weight < dist[child] && dist[i] != MAX) + { + dist[child] = weight + dist[i]; + } + } + } + } + + for (int i = 1; i <= n; i++) + { + for (int j = 0; j < graph[i].size(); j++) + { + int child = graph[i][j]; + + int weight = label[child] - label[i]; + + weight = weight * weight * weight; + + if (dist[i] + weight < dist[child] && dist[i] != MAX) + { + dist[child] = -MAX; + } + } + } +} + +void initialize() +{ + label.clear(); + label.resize(n + 3); + + graph.clear(); + graph.resize(n + 3); + + dist.clear(); + dist.resize(n + 3, MAX); +} + +int main() +{ + while (cin >> n) + { + initialize(); + + for (int i = 1; i <= n; i++) + { + cin >> label[i]; + } + + cin >> m; + + for (int i = 0; i < m; i++) + { + int x, y; + + cin >> x >> y; + + graph[x].push_back(y); + } + + bellman_ford(); + + cin >> q; + + cout << "Set #" << t++ << endl; + + for (int i = 0; i < q; i++) + { + int x; + + cin >> x; + + if (dist[x] < 3 || dist[x] == MAX) cout << '?' << endl; + + else cout << dist[x] << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10449/10449-19.cpp b/uva_cpp_clean/10449/10449-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11d8de3c309cf04e5d06aa4c9699a57f7227c968 --- /dev/null +++ b/uva_cpp_clean/10449/10449-19.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + + +bool bellman_ford(int s, vector &adjList, vi &dist, vi &relax) { + relax.assign(adjList.size(), 0); + dist.assign(adjList.size(), ll(1) << 40); dist[s] = 0; + bool relaxed; + for (int i = 0; i < adjList.size(); i++) { + relaxed = false; + for (int u = 0; u < adjList.size(); u++) + for (int j = 0; j < adjList[u].size(); j++) { + ii v = adjList[u][j]; + if (dist[v.first] > dist[u] + v.second) { + dist[v.first] = dist[u] + v.second; + relaxed = true; + if (i == adjList.size() - 1) relax[v.first] = 1; + } + } + if (!relaxed) return false; + } + return relaxed; +} + +void proc(vector &adjList, vi &relax) { + int n = adjList.size(); + vi visited(n, 0); + queue q; + for (int i = 0; i < n; i++) { + if (relax[i] && !visited[i]) { + q.push(i); + while (!q.empty()) { + int node = q.front(); + q.pop(); + if (!visited[node]) { + visited[node] = 1; + relax[node] = 1; + for (int j = 0; j < adjList[node].size(); j++) { + q.push(adjList[node][j].first); + } + } + } + } + } +} + +int main() { + int n, c = 1; + vi busy, dist, relax; + vector adjList; + while (cin >> n) { + printf("Set #%d\n", c++); + if (n != 0) { + busy.assign(n, 0); + adjList.assign(n, vii()); + } + for (int i = 0; i < n; i++) cin >> busy[i]; + int r; + cin >> r; + for (int i = 0; i < r; i++) { + int s, d; + cin >> s >> d; + s--, d--; + int b = busy[d] - busy[s]; + adjList[s].push_back(ii(d, b*b*b)); + } + bool relaxed; + if (n != 0) relaxed = bellman_ford(0, adjList, dist, relax); + if (relaxed) proc(adjList, relax); + int q; + cin >> q; + for (int i = 0; i < q; i++) { + int val; + cin >> val; + if (dist[val - 1] < 3 || relax[val - 1] || dist[val - 1] > (ll(1) << 32)) printf("?\n"); + else printf("%lld\n", dist[val - 1]); + } + } +} diff --git a/uva_cpp_clean/10449/10449-21.cpp b/uva_cpp_clean/10449/10449-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b96dc85fc611b585a415f7e49a7fc4bc4d2bd3c2 --- /dev/null +++ b/uva_cpp_clean/10449/10449-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10449 + Name: Traffic + Problem: https://onlinejudge.org/external/104/10449.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + + +int sq3(int x) { return x*x*x; } +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector adj[212]; + int C[212], B[212], D[212][212]; + for (int cse=1, n, m; cin>>n; ++cse) { + cout << "Set #" << cse << endl; + + for (int i=0; i> B[i]; + adj[i].clear(); + C[i] = INF; + } + C[0] = 0; + + cin >> m; + for (int i=0, u, v; i> u >> v; --u; --v; + D[u][v] = sq3(B[v] - B[u]); + adj[u].push_back(v); + } + + for (int i=0, som=1; som-- && i<=n; ++i) + for (int u=0; u> m; + for (int i=0, u; i> u; --u; + + if (C[u] < 3 || C[u]==INF) + cout << "?\n"; + else + cout << C[u] << endl; + } + } +} diff --git a/uva_cpp_clean/10449/10449-9.cpp b/uva_cpp_clean/10449/10449-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaf6d878237b966128c2d430ff356216c2307ce9 --- /dev/null +++ b/uva_cpp_clean/10449/10449-9.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +const int MAX_V = 210, MAX_E = 40000, INF = 1000000000; + +int tc, V, E, Q, x, fw[MAX_V], dis[MAX_V], u[MAX_E], v[MAX_E], w[MAX_E]; +bool relax; + +inline int pow3(int p){ + return p * p * p; +} + +void BellmanFord(int source){ + fill(dis, dis + V + 1, INF); + dis[source] = 0; + for(int i = 1; i < V; i++){ + relax = false; + for(int j = 0; j < E; j++) + if( dis[u[j]] != INF and dis[v[j]] > dis[u[j]] + w[j] ) + dis[v[j]] = dis[u[j]] + w[j], relax = true; + if( not relax ) break; + } + for(int j = 0; j < E; j++) + if( dis[u[j]] != INF and dis[v[j]] > dis[u[j]] + w[j] ) + dis[v[j]] = -INF; +} + +int main(){ + while( scanf("%d", &V) == 1 ){ + for(int i = 1; i <= V; i++) scanf("%d", fw + i); + scanf("%d", &E); + for(int i = 0; i < E; i++){ + scanf("%d %d", u + i, v + i); + w[i] = pow3(fw[v[i]] - fw[u[i]]); + } + printf("Set #%d\n", ++tc); + BellmanFord(1); + scanf("%d", &Q); + while(Q--){ + scanf("%d", &x); + if( dis[x] < 3 or dis[x] == INF ) puts("?"); + else printf("%d\n", dis[x]); + } + } + return(0); +} diff --git a/uva_cpp_clean/10450/10450-19.cpp b/uva_cpp_clean/10450/10450-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b84f171f8fe1e1eecd9458f1b720d51a30a63bd9 --- /dev/null +++ b/uva_cpp_clean/10450/10450-19.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +unordered_map m; + +long long proc(int val, int last) { + if (!m.count(val*10 + last)) { + m[val*10 + last] = proc(val-1, 0) + (last? 0: proc(val-1, 1)); + } + return m[val*10 + last]; +} + +int main() { + m[10] = 2; + m[11] = 1; + int n; + cin >> n; + for (int c = 1; c <= n; c++) { + int val; + cin >> val; + printf("Scenario #%d:\n", c); + printf("%lld\n", proc(val, 0)); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10450/10450-20.cpp b/uva_cpp_clean/10450/10450-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe569e30c067354badb4ed6585a84a71c60a6717 --- /dev/null +++ b/uva_cpp_clean/10450/10450-20.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +long long n[55]; + +int main() +{ + n[0] = 1, n[1] = 2; + for(int i=2 ; i<52 ; i++) n[i] = n[i-1]+n[i-2]; + + int tc, ncase; + + cin >> tc; + ncase = 0; + while( tc-- ) + { + int num; + cin >> num; + + cout << "Scenario #" << ++ncase << ":\n" << n[num] << "\n" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10450/10450-21.cpp b/uva_cpp_clean/10450/10450-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50dd2e3ac89fb82ecae9af2a358112bd3d37ec0a --- /dev/null +++ b/uva_cpp_clean/10450/10450-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10450 + Name: World Cup Noise + Problem: https://onlinejudge.org/external/104/10450.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + long long int fib[52] = {1, 2}; + for (int i=2; i<52; i++) + fib[i] = fib[i-1] + fib[i-2]; + fib[0]=0; + + int T, n; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin>>n; + printf("Scenario #%d:\n%lld\n\n", cse, fib[n]); + } +} diff --git a/uva_cpp_clean/10450/10450-5.cpp b/uva_cpp_clean/10450/10450-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..447427e0ea2bb2b0a78437aa64552585815935d4 --- /dev/null +++ b/uva_cpp_clean/10450/10450-5.cpp @@ -0,0 +1,63 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; +int main() +{ + int t,no=0; + sc("%d",&t); + while(t--) + { long long n,a[600]; + sc("%lld",&n); + a[1]=2; + a[2]=3; + for(int i=3;i<=n;i++) + { + a[i]=a[i-1]+a[i-2]; + } + pf("Scenario #%d:\n%lld\n\n",++no,a[n]); + } + +return 0; +} diff --git a/uva_cpp_clean/10450/10450-9.cpp b/uva_cpp_clean/10450/10450-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f5617fa0106ffdfd07d802a5e3007468b9bc620 --- /dev/null +++ b/uva_cpp_clean/10450/10450-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int tc, n, t; +vector fib; + +int main(){ + fib.push_back(1); + fib.push_back(2); + for(int i = 2; i <= 50; i++) + fib.push_back(fib[i - 1] + fib[i - 2]); + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + printf("Scenario #%d:\n%ld\n\n", ++t, fib[n]); + } + + return(0); +} diff --git a/uva_cpp_clean/10451/10451-20.cpp b/uva_cpp_clean/10451/10451-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e094758a4bf1d559852f865b6235fe91d893c444 --- /dev/null +++ b/uva_cpp_clean/10451/10451-20.cpp @@ -0,0 +1,22 @@ +#include +#include + +const double PI = 2*acos(0.0); + +int main() +{ + double n, A; + + int ncase = 0; + while( scanf("%lf %lf", &n, &A)==2 && n>=3.0 ) + { + double r1 = sqrt( (2*A/n)/(sin( 2*PI/n )) ); + double r2 = r1*(cos( (2*PI)/(2*n) )); + + double area1 = (PI*r1*r1)-A; + double area2 = A-(PI*r2*r2); + + printf("Case %d: %.5lf %.5lf\n", ++ncase, area1, area2); + } + return 0; +} diff --git a/uva_cpp_clean/10451/10451-21.cpp b/uva_cpp_clean/10451/10451-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d2e80abd00b05bcad66676c73678f0a9155bd2e --- /dev/null +++ b/uva_cpp_clean/10451/10451-21.cpp @@ -0,0 +1,27 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10451 + Name: Ancient Village Sports + Problem: https://onlinejudge.org/external/104/10451.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +const double PI = acos(-1); +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + double n, s; + for (int cse=1; cin >> n >> s && n>2; ++cse) { + double a = PI / n, ca = cos(a); + double r = sqrt(s / (n*ca*sin(a))); + double h = r * ca; + printf("Case %d: %.5f %.5f\n", cse, PI*r*r - s, s - PI*h*h); + } +} diff --git a/uva_cpp_clean/10451/10451-9.cpp b/uva_cpp_clean/10451/10451-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa5c165e4a34cbd7dbe2375cdac6fbabbf5d4df7 --- /dev/null +++ b/uva_cpp_clean/10451/10451-9.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +const double PI = acos(-1.0); + +int tc, N; +double A, R, theta, ans1, ans2; + +int main(){ + while(~scanf("%d %lf", &N, &A), N >= 3){ + theta = 2.0 * PI / N; + R = sqrt( 2.0 * A / ( 1.0 * N * sin(theta) ) ); + ans1 = PI * R * R - A; + ans2 = A - PI * R * R * cos(theta / 2.0) * cos(theta / 2.0); + printf("Case %d: %.5lf %.5lf\n", ++tc, ans1, ans2); + } + return(0); +} diff --git a/uva_cpp_clean/10452/10452-11.cpp b/uva_cpp_clean/10452/10452-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..685265fff7db98531839cd6c01adb95f1daf22c2 --- /dev/null +++ b/uva_cpp_clean/10452/10452-11.cpp @@ -0,0 +1,101 @@ +#include +/* +Problem: 10452 - Marcus +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1393 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + + +int T, N, M; +char stones[8][8]; + +int I, J; +string path[8] = {}; +string word = "@IEHOVA#"; + +void dfs(int i, int j, int k, string direction){ + if (i < 0 || j < 0 || j >= N || i >= M) return; + if (stones[i][j] != word[k]) return; + + path[k] = direction; + if (word[k] == '#') return; + k++; + + // (i-1, j) + // (i, j-1) (i, j) (i, j+1) + dfs(i-1, j, k, "forth"); + dfs(i, j-1, k, "left"); + dfs(i, j+1, k, "right"); +} + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> T; + + while ( T-- ){ + cin >> M >> N; + cin.ignore(); + + for (int i = 0; i < M; i++){ + for (int j = 0; j < N; j++){ + cin.get(stones[i][j]); + + if (stones[i][j] == '@') { + I = i; + J = j; + } + } + cin.ignore(); + } + + dfs(I, J, 0, "start"); + + for (int i = 1; i < 7; i++){ + cout << path[i] << ' '; + } cout << path[7] << '\n'; + + } + + return 0; +} +/* +Sample input:- +----------------- +2 +6 5 +PST#T +BTJAS +TYCVM +YEHOF +XIBKU +N@RJB +5 4 +JA#X +JVBN +XOHD +DQEM +T@IY + +Sample output:- +----------------- +forth forth right right forth forth forth +right forth forth left forth forth right + +Resources:- +------------- +Video Solution (Arabic) (c++) [Eng Ayman Salah]: https://www.youtube.com/watch?v=HtaczlDLylk + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10452/10452-18.cpp b/uva_cpp_clean/10452/10452-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f60c4c6c053677e8236bc868f6f45cbdf49b346e --- /dev/null +++ b/uva_cpp_clean/10452/10452-18.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +vector g; +bool vist[9][9]; +int posI[] = {-1, 0, 0}, posJ[] = {0, -1, 1}, n, m; + +bool words(char l){ + return l == 'I' || l == 'E' || l == 'H' || l == 'O' || l == 'V' || l == 'A' || l == '#'; +} + +void DFS(int i, int j, int spc){ + vist[i][j] = true; + if(g[i][j] == '#') return; + if(spc) printf(" "); + for(int k = 0; k < 3; k++){ + int nI = i + posI[k]; + int nJ = j + posJ[k]; + if(nI >= 0 && nI < n && nJ >= 0 && nJ < m && !vist[nI][nJ] && words(g[nI][nJ])){ + if(k == 0) printf("forth"); + if(k == 1) printf("left"); + if(k == 2) printf("right"); + DFS(nI, nJ, 1); + } + } +} + +int main(){ + int tc; + string inp; + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &n, &m); + g.clear(); + memset(vist, false, sizeof(vist)); + for(int i = 0; i < n; i++) cin >> inp, g.push_back(inp); + for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(g[i][j] == '@') DFS(i, j, 0); + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10452/10452-21.cpp b/uva_cpp_clean/10452/10452-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af1602c3c40060d0c58c485e73d06bf1697f60f6 --- /dev/null +++ b/uva_cpp_clean/10452/10452-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10452 + Name: Marcus + Problem: https://onlinejudge.org/external/104/10452.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char ph[] = "IEHOVA#", M[17][17]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m; + cin >> T; + while (T--) { + cin >> n >> m; + for (int i=1; i<=n; ++i) + cin >> (M[i]+1); + + for (int j=1; j<=m; ++j) + if (M[n][j] == '@') { + for (int ci=n, cj=j, d=0; M[ci][cj]!='#'; ++d) { + if (M[ci][cj-1] == ph[d]) { + cout << "left"; --cj; + } + else if (M[ci][cj+1] == ph[d]) { + cout << "right"; ++cj; + } + else { + cout << "forth"; --ci; + } + if (M[ci][cj]!='#') cout << ' '; + } + cout << '\n'; + break; + } + } +} diff --git a/uva_cpp_clean/10452/10452-9.cpp b/uva_cpp_clean/10452/10452-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e435bdc5f7ebe49f211da9f392bed4648f8ea91 --- /dev/null +++ b/uva_cpp_clean/10452/10452-9.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define N 1000 + +int n, m, f, l; +string a[N]; +vector < pair > b; + +int first(){ + for(int i = 0; i < m; i++) + if(a[n - 1][i] == '@') + return i; +} + +int last(){ + for(int i = 0; i < m; i++) + if(a[0][i] == '#') + return i; + +} + +int main(){ + int t; + cin >> t; + while(t--){ + cin >> n >> m; + for(int i = 0; i < n; i++) + cin >> a[i]; + f = first(); + b.push_back( make_pair(n - 1, f) ); + + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + if(a[i][j] == 'I' && (abs(b[0].first - i) == 0 || abs(b[0].first - i) == 1) && (abs(b[0].second - j) == 0 || abs(b[0].second - j) == 1)) + b.push_back( make_pair(i, j) ),j = m, i = n; + + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + if(a[i][j] == 'E' && (abs(b[1].first - i) == 0 || abs(b[1].first - i) == 1) && (abs(b[1].second - j) == 0 || abs(b[1].second - j) == 1)) + b.push_back( make_pair(i, j) ),j = m, i = n; + + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + if(a[i][j] == 'H' && (abs(b[2].first - i) == 0 || abs(b[2].first - i) == 1) && (abs(b[2].second - j) == 0 || abs(b[2].second - j) == 1)) + b.push_back( make_pair(i, j) ), j = m, i = n; + + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + if(a[i][j] == 'O' && (abs(b[3].first - i) == 0 || abs(b[3].first - i) == 1) && (abs(b[3].second - j) == 0 || abs(b[3].second - j) == 1)) + b.push_back( make_pair(i, j) ), j = m, i = n; + + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + if(a[i][j] == 'V' && (abs(b[4].first - i) == 0 || abs(b[4].first - i) == 1) && (abs(b[4].second - j) == 0 || abs(b[4].second - j) == 1)) + b.push_back( make_pair(i, j) ), j = m, i = n; + + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + if(a[i][j] == 'A' && (abs(b[5].first - i) == 0 || abs(b[5].first - i) == 1) && (abs(b[5].second - j) == 0 || abs(b[5].second - j) == 1)) + b.push_back( make_pair(i, j) ), j = m, i = n; + l = last(); + b.push_back( make_pair(0, l) ); + + for(int i = 1; i <= 7; i++){ + if(b[i - 1].first == b[i].first && b[i - 1].second > b[i].second) + cout << "left"; + else if(b[i - 1].first == b[i].first && b[i - 1].second < b[i].second) + cout << "right"; + else + cout << "forth"; + if(i != 7) + cout << ' '; + else + putchar('\n'); + } + b.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/10453/10453-9.cpp b/uva_cpp_clean/10453/10453-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..076562f4cfb223af0192230b7c4a2c315753954d --- /dev/null +++ b/uva_cpp_clean/10453/10453-9.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +const int MAX_LEN = 1e3 + 10; + +string s; +int memo[MAX_LEN][MAX_LEN], step[MAX_LEN * MAX_LEN]; + +int hash_ (int i, int j) { + return (i + 1) * MAX_LEN + (j + 1); +} + +string construct () { + string ret = s; + int i = 0, j = s.size() - 1, shift = 0; + while (i <= j) { + int next = step[hash_(i, j)]; + int ni = (next / MAX_LEN) - 1, nj = (next % MAX_LEN) - 1; + if (make_pair(ni, nj) == make_pair(i + 1, j)) ret.insert(begin(ret) + j + 1 + shift, s[i]); + if (make_pair(ni, nj) == make_pair(i, j - 1)) ret.insert(begin(ret) + i + shift, s[j]), shift++; + i = ni, j = nj; + } + return ret; +} + +int dp (int i, int j) { + if (i > j) return 0; + if (~memo[i][j]) return memo[i][j]; + if (s[i] == s[j]) { + step[hash_(i, j)] = hash_(i + 1, j - 1); + return memo[i][j] = dp(i + 1, j - 1); + } + int ret1 = dp(i, j - 1); + int ret2 = dp(i + 1, j); + step[hash_(i, j)] = (ret1 < ret2) ? hash_(i, j - 1) : hash_(i + 1, j); + return memo[i][j] = 1 + min(ret1, ret2); +} + +void clearCase () { + for (int i = 0; i <= s.size(); i++) { + for (int j = 0; j <= s.size(); j++) { + memo[i][j] = step[i * MAX_LEN + j] = -1; + } + } +} + +int main () { + while (cin >> s) { + clearCase(); + int add = dp(0, s.size() - 1); + cout << add << ' ' << construct() << endl; + } + return (0); +} diff --git a/uva_cpp_clean/10459/10459-21.cpp b/uva_cpp_clean/10459/10459-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d745a9b836d9fb1a92860d7cb124c901f01d9437 --- /dev/null +++ b/uva_cpp_clean/10459/10459-21.cpp @@ -0,0 +1,116 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10459 + Name: The Tree Root + Problem: https://onlinejudge.org/external/104/10459.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------------- + +vector adj[5543]; +struct Edge { + int u, d; + Edge(int u, int d):u(u),d(d){} +}; + + +bool inside[5543]; +Edge dfs(int u) { + inside[u] = true; + + Edge r(u, 0); + for (int v: adj[u]) + if (!inside[v]) { + Edge rr = dfs(v); + if (++rr.d > r.d) + r = rr; + } + + inside[u] = false; + return r; +} + + +int D[5543]; +void dfs(int u, int d) { + inside[u] = true; + + D[u] = max(D[u], d); + for (int v: adj[u]) + if (!inside[v]) + dfs(v, d+1); + + inside[u] = false; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while ((n=readUInt()) != EOF) { + for (int u=0; u>1, dm2 = (e.d+1)>>1; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10460 + Name: Find the Permuted String + Problem: https://onlinejudge.org/external/104/10460.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +long long F[43]; +char S[43], P[43], n; +void bt(int idx, int ind) { + if (idx == n) { + if (ind > 0) cout << "E: "; + cout << P << '\n'; + return; + } + + long long ff = 1; + for (int j=idx+2; j<=n && ff<=ind; ++j) ff *= j; + + int i = 0; + while (i= 0) ind -= ff, ++i; + for (int j=idx; j>i; --j) P[j] = P[j-1]; + + P[i] = S[idx]; + bt(idx+1, ind); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + memset(F, 127, sizeof(F)); + F[1] = 1; + for (int i=2; i<16; ++i) + F[i] = F[i-1] * i; + + int T, ind; + cin >> T; + while (T--) { + cin.ignore(100, '\n'); + cin.getline(S, 43); + cin >> ind; + P[n = strlen(S)] = 0; + + bt(0, ind-1); + } +} diff --git a/uva_cpp_clean/10462/10462-10.cpp b/uva_cpp_clean/10462/10462-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df74a3e7bd80a6c6944858ad1716d0c208047b0b --- /dev/null +++ b/uva_cpp_clean/10462/10462-10.cpp @@ -0,0 +1,133 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +const int N= (int)2e2+5; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + int count(int u) { + return sz[find(u)]; + } +}; + +// {weight, a, b,use or not} +vector IsTheresASolutionUsingMinimumSpanningTree(vector>&edges) { + DSU dsu; + dsu.init(N); + vector ans; + int a,b,weight,index; + for (auto e: edges) { + tie(weight,a,b,index) = e; + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + ans.push_back(index); + } + } + return ans; +} + + +// {weight, a, b,use or not} +pair SecondminimumSpanningTree(vector>&edges , int notused) { + DSU dsu; + dsu.init(N); + int ans=0,edge=0,a,b,weight,index; + for (auto e: edges) { + tie(weight,a,b,index) = e; + if (notused!=index && dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + ans+=weight; + edge++; + } + } + + pair res={ans,edge}; + + return res; +} + +int main() { + FAST + + int t;cin>>t; + int n,m,a,b,w; + int c=1; + + while (t--) { + cin>>n>>m; + vector> edges; + + for(int i=0;i>a>>b>>w; + edges.push_back({w,a,b,i}); + } + + sort(edges.begin(),edges.end()); + + vector ans = IsTheresASolutionUsingMinimumSpanningTree(edges); + + if(ans.size() != (n-1)) { + cout<<"Case #"<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10462 + Name: Is There A Second Way Left? + Problem: https://onlinejudge.org/external/104/10462.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +#define INF 1073741824 +typedef unsigned char byte; +char NOWAY[] = "No way", + NO2WAY[] = "No second way"; +byte gid[117]; + +byte find(byte u) { return gid[u]==u ? u : gid[u]=find(gid[u]); } +void join(byte u, byte v) { gid[find(u)] = find(v); } + +struct Edge { + int w; + byte u, v; + Edge(byte u, byte v, int w):u(u),v(v),w(w) {} + bool operator < (const Edge &e) const { return w > e.w; } +}; + +int d[117][117], depth[117], parent[117]; +list adj[117]; +void reparent(int u, int de) { + depth[u] = de; + byte mypar = parent[u]; + for (byte v: adj[u]) + if (v != mypar) { + parent[v] = u; + reparent(v, de+1); + } +} + +int maxlen(int u, int v) { + int mx = 0; + while (u != v) { + if (depth[u] >= depth[v]) { + mx = max(mx, d[u][parent[u]]); + u = parent[u]; + } + + if (depth[v] > depth[u]) { + mx = max(mx, d[v][parent[v]]); + v = parent[v]; + } + } + return mx; +} + +int main(){ + srand(rand()-1); + int T, n, m, u, v, w; + + scanf("%d", &T); + for (int cse=1; cse<=T; ++cse) { + printf("Case #%d : ", cse); + scanf("%d%d", &n, &m); + for (int i=0; i q2; + priority_queue q; + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 210; + +struct Edge_{ + int u, v, w; + bool operator < (const Edge_& other) const{ + return ( w < other.w ); + } +}E[SIZE]; + +int tc, N, M, min_cost, min_cost1, min_cost2, setSize; + +void Kruskal(){ + UnionFind UF(N + 1); + min_cost1 = 0, min_cost2 = INT_MAX, setSize = 1; + vector used; + sort(E, E + M); + for(int it = 0; it < M and setSize != N; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ){ + min_cost1 += E[it].w, setSize++; + UF.unionSet(E[it].u, E[it].v), used.push_back(it); + } + if( setSize != N ){ + min_cost1 = -1; + return; + } + for(int i = 0; i + 1 < N; i++){ + setSize = 1; + UnionFind UF(N + 1); + min_cost = 0; + for(int it = 0; it < M and setSize != N; it++) + if( it != used[i] and not UF.isSameSet(E[it].u, E[it].v) ) + min_cost += E[it].w, setSize++, UF.unionSet(E[it].u, E[it].v); + if( setSize == N ) + min_cost2 = min(min_cost2, min_cost); + } + if( min_cost2 == INT_MAX ) min_cost2 = -1; +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d %d", &N, &M); + for(int it = 0; it < M; it++) + scanf("%d %d %d", &E[it].u, &E[it].v, &E[it].w); + Kruskal(); + if( min_cost1 == - 1) printf("Case #%d : No way\n", t); + else if( min_cost2 == -1 ) printf("Case #%d : No second way\n", t); + else printf("Case #%d : %d\n", t, min_cost2); + } + return(0); +} diff --git a/uva_cpp_clean/10465/10465-18.cpp b/uva_cpp_clean/10465/10465-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5ca897e234e852e6f549c85c78875c84b6a26fb3 --- /dev/null +++ b/uva_cpp_clean/10465/10465-18.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; //Programacion dinamica + +int main(){ + int m, n, t, DP[10000]; + while(true){ + cin >> m >> n >> t; + if(cin.fail()) break; + DP[0] = 0; + for(int i = 1; i <= t; i++){ + DP[i] = -1; + if(m <= i && DP[i - m] != -1) DP[i] = DP[i - m] + 1; //Calcular con una hamburguesa + if(n <= i && DP[i - n] != -1) DP[i] = max(DP[i - n] + 1, DP[i]); //Calcular con la otra y ver cual es el mayor + } + if(DP[t] != -1) cout << DP[t] << endl; //Significa que ocupó todo el tiempo comiendo + else{ + for(int i = t; i >= 0; i--){ //Significa que le sobró tiempo, entonces hay que buscar el tiempo maximo + if(DP[i] != -1){ + cout << DP[i] << " " << t - i << endl; + break; + } + } + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10465/10465-21.cpp b/uva_cpp_clean/10465/10465-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27a69f0935d8229ad99d12e426b14bceec982ddb --- /dev/null +++ b/uva_cpp_clean/10465/10465-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10465 + Name: Homer Simpson + Problem: https://onlinejudge.org/external/104/10465.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m, t; + while (cin >> n >> m >> t) { + if (n > m) swap(n, m); + + int bt = 100000, bs = 0; + for (int i=t/n, x=i*n; bt && i>=0; --i, x-=n) { + int j = (t-x) / m, + y = j*m; + + if (bt > t-x-y) { + bt = t-x-y; + bs = i + j; + } + } + cout << bs; + if (bt) cout << ' ' << bt; + cout << "\n"; + } +} diff --git a/uva_cpp_clean/10465/10465-9.cpp b/uva_cpp_clean/10465/10465-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..568c9e97f1d3b2cf489c37df12dd99d42d94a1c7 --- /dev/null +++ b/uva_cpp_clean/10465/10465-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int m,n,t,dp[10010]; + +int main(){ + while(scanf("%d %d %d",&m,&n,&t)==3){ + memset(dp,-1,sizeof dp); + dp[0] = 0; + for(int it=1;it<=t;it++){ + if(it>=m && ~dp[it-m]) dp[it] = dp[it] = 1+dp[it-m]; + if(it>=n && ~dp[it-n]) dp[it] = max(dp[it],1+dp[it-n]); + } + if(~dp[t]) printf("%d\n",dp[t]); + else{ + for(int it=t-1;it>=0;it--) + if(~dp[it]) printf("%d %d\n",dp[it],t-it), it = 0; + } + } + return(0); +} diff --git a/uva_cpp_clean/10466/10466-21.cpp b/uva_cpp_clean/10466/10466-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8b2290c8f7fdeba17364c6da4c4ddf7de72916f --- /dev/null +++ b/uva_cpp_clean/10466/10466-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10466 + Name: How Far? + Problem: https://onlinejudge.org/external/104/10466.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +const double PI = acos(-1); +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(4); + + int n, t, ri, ti; + while (cin >> n >> t) { + double x = 0, y = 0; + for (int i=0; i> ri >> ti; + double deg = 2*PI*double(t % ti) / ti; + x += ri * cos(deg); + y += ri * sin(deg); + + if (i) cout << ' '; + cout << sqrt(x*x + y*y); + } + cout << '\n'; + } +} diff --git a/uva_cpp_clean/10469/10469-11.cpp b/uva_cpp_clean/10469/10469-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27d2b5aab31d0710e2adc13634718cd67e0d140c --- /dev/null +++ b/uva_cpp_clean/10469/10469-11.cpp @@ -0,0 +1,47 @@ +#include +/* +Problem: 10469 - To Carry or not to Carry +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1410 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +unsigned int A, B; + +int main () { + + while ( scanf("%u%u", &A, &B) != EOF ){ + + printf("%u\n", A^B); + + } + + return 0; +} +/* +Sample input:- +----------------- +4 6 +6 9 + +Sample output:- +----------------- +2 +15 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10469/10469-19.cpp b/uva_cpp_clean/10469/10469-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c81dc1edf70b036aa1f9e90ed6ac35d35cdefee5 --- /dev/null +++ b/uva_cpp_clean/10469/10469-19.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; + +int main() { + unsigned int a, b; + while (scanf("%u %u", &a, &b) != EOF) printf("%d\n", a^b); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10469/10469-21.cpp b/uva_cpp_clean/10469/10469-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4b5b91d87e26d7ad69318790e846ff987e98622 --- /dev/null +++ b/uva_cpp_clean/10469/10469-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10469 + Name: To Carry or not to Carry + Problem: https://onlinejudge.org/external/104/10469.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + long int x, y; + while (cin >> x >> y) + cout << (x ^ y) << endl; +} \ No newline at end of file diff --git a/uva_cpp_clean/10469/10469-5.cpp b/uva_cpp_clean/10469/10469-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a6d0698fddc09a1957e4a5f3a4badf9c392d5399 --- /dev/null +++ b/uva_cpp_clean/10469/10469-5.cpp @@ -0,0 +1,10 @@ +#include +int main() +{ + int a,b; + while(scanf("%d %d",&a,&b)==2) + { + printf("%d\n",a^b); + } + return 0; +} diff --git a/uva_cpp_clean/10469/10469-9.cpp b/uva_cpp_clean/10469/10469-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3455282a04c65cb45d2361d08db5f7bf1dcd1ce7 --- /dev/null +++ b/uva_cpp_clean/10469/10469-9.cpp @@ -0,0 +1,10 @@ +#include + +using namespace std; + +int main(){ + int a, b; + while(scanf("%d %d", &a, &b) == 2) + printf("%d\n", a ^ b); + return(0); +} diff --git a/uva_cpp_clean/1047/1047-13.cpp b/uva_cpp_clean/1047/1047-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86987c1436d0d48687bc35f0e4cbeb4efbefded9 --- /dev/null +++ b/uva_cpp_clean/1047/1047-13.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +bool first = false; + +int n, m, howData, howAns, testCase = 1; + +vector visit; +vector > datas; +vector ans, tow, combination, number; + +int inclusion_exclusion(vector ones) +{ + int sum = 0; + + visit.clear(); + visit.resize(howData, false); + + for (int i = 0; i < (int)ones.size(); i++) + { + int cond = ones[i]; + + sum += tow[cond]; + + for (int j = 0; j < howData; j++) + { + if (!visit[j]) + { + bool found = binary_search(datas[j].begin(), datas[j].end(), cond); + + if (found) + { + visit[j] = true; + + sum += number[j]; + } + } + } + } + + return sum; +} + +void go(int offset, int k) +{ + if (k == 0) + { + int per = inclusion_exclusion(combination); + + if (per > howAns) + { + howAns = per; + + ans = combination; + } + + return; + } + + for (int i = offset; i <= tow.size() - k; ++i) + { + combination.push_back(i); + + go(i + 1, k - 1); + + combination.pop_back(); + } +} + +void initialize() +{ + ans.clear(); + + tow.clear(); + tow.resize(n); + + combination.clear(); + + howAns = 0; +} + +int main() +{ + while (cin >> n >> m , n != 0 || m != 0) + { + initialize(); + + for (int i = 0; i < n; i++) + { + cin >> tow[i]; + } + + cin >> howData; + + datas.clear(); + datas.resize(howData); + + number.clear(); + number.resize(howData); + + for (int i = 0; i < howData; i++) + { + int x, num; + + cin >> x; + + for (int j = 0; j < x; j++) + { + cin >> num; + + num--; + + datas[i].insert(num); + } + + cin >> number[i]; + + set ::iterator it; + + for (it = datas[i].begin(); it != datas[i].end(); it++) + { + tow[*it] -= number[i]; + } + } + + go(0, m); + + printf("Case Number %d\n", testCase++); + printf("Number of Customers: %d\n", howAns); + printf("Locations recommended:"); + + for (int i = 0; i < (int)ans.size(); i++) cout << " " << ans[i] + 1; + + cout << endl << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1047/1047-21.cpp b/uva_cpp_clean/1047/1047-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c63abfc953f543d81ea15a5e090ab349c433e768 --- /dev/null +++ b/uva_cpp_clean/1047/1047-21.cpp @@ -0,0 +1,81 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1047 + Name: Zones + Problem: https://onlinejudge.org/external/10/1047.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int countbit(int x) { + int c = 0; + while (x) { + x ^= x & -x; + ++c; + } + return c; +} + +int snoob(int x) { + if (!x) throw 1; + int rightOne = x & -x, + nextHigherOneBit = x + rightOne; + return ((x ^ nextHigherOneBit)/rightOne) >> 2 | nextHigherOneBit; +} + + +int X[23], Y[1<<21], B[1<<21], n, m, bn; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; cin >> n >> m && (n||m); ++cse) { + for (int i=0; i> X[i]; + + cin >> bn; + for (int i=0, sz, b; i> sz; + while (sz--) { + cin >> b; + B[i] |= 1<<(b-1); + } + cin >> Y[i]; + } + + int best = 0, bestc; + for (int mask=(1< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +ll pow(ll n) { + if (n == 0) return 1; + ll sum = 1; + for (int i = 1; i <= n; i++) { + sum *= 16; + } + return sum; +} +ll stringToNum(string str){ + ll num = 0, sz = str.size(); + for(int i = 0; i=2; i--, j++){ + if(str[i] == 'A') n = 10; + else if(str[i] == 'B') n = 11; + else if(str[i] == 'C') n = 12; + else if(str[i] == 'D') n = 13; + else if(str[i] == 'E') n = 14; + else if(str[i] == 'F') n = 15; + else n = (str[i] - '0'); + + num+=(pow(j) * n); + } + return num; +} +int main () { + string str; + while (cin >> str) { + if (str[0] == '-') break; + if (str[0] == '0' && str[1] == 'x') { + //~ cout << hexaTodeci(str) << "\n"; + printf("%lld\n", hexaTodeci(str)); + } else { + printf("0x%X\n", stringToNum(str)); + } + } + return 0; +} + diff --git a/uva_cpp_clean/10473/10473-19.cpp b/uva_cpp_clean/10473/10473-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5d2bb08907132b3e54016db383b868f0ad93947 --- /dev/null +++ b/uva_cpp_clean/10473/10473-19.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + string s; + char buffer[20]; + while (getline(cin, s) && s[0] != '-') { + if (s[0] == '0' && s[1] == 'x') { + int val = stoi (s,nullptr,16); + printf("%d\n", val); + } else { + int dec = stoi (s,nullptr,10); + sprintf(buffer, "0x%X", dec); + printf("%s\n", buffer); + } + } + return 0; +} diff --git a/uva_cpp_clean/10473/10473-20.cpp b/uva_cpp_clean/10473/10473-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..704e0643b845940917329a6e9940a18165a066a7 --- /dev/null +++ b/uva_cpp_clean/10473/10473-20.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + char num[1005]; + + while( cin >> num && num[0]!='-' ) + { + long long decimal, index, hexadecimal; + char hex[105]; + + if( num[0]=='0' && num[1]=='x' ) + { + decimal = 0; + for(int i=2 ; i=0 ; i--) + cout << hex[i]; + cout << endl; + } + } + return 0; +} diff --git a/uva_cpp_clean/10473/10473-21.cpp b/uva_cpp_clean/10473/10473-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94e2fae06de612cdf0c4944ec991f1de4fb0e3f4 --- /dev/null +++ b/uva_cpp_clean/10473/10473-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10473 + Name: Simple Base Conversion + Problem: https://onlinejudge.org/external/104/10473.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; +int main(){ + char str[20]; + long long int num; + while(cin>>str && str[0]!='-') + if (str[0]=='0' && str[1]=='x') { + sscanf(str+2, "%llx", &num); + printf("%lld\n", num); + } + else { + sscanf(str, "%lld", &num); + printf("0x%llX\n", num); + } +} diff --git a/uva_cpp_clean/10474/10474-17.cpp b/uva_cpp_clean/10474/10474-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcc043db2eea87ddd5df4f842de33bbf4cc207d4 --- /dev/null +++ b/uva_cpp_clean/10474/10474-17.cpp @@ -0,0 +1,26 @@ +/** + * AOAPC II Example 5-1 Where's the Marble + */ +#include +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n, q, kase = 0; + while ((cin >> n >> q) && n && q) { + cout << "CASE# " << ++kase << ":\n"; + vector mar(n); + for (int &i : mar) + cin >> i; + sort(mar.begin(), mar.end()); + while (q--) { + int x; + cin >> x; + auto pos = lower_bound(mar.begin(), mar.end(), x); + if (pos != mar.end() && *pos == x) + cout << x << " found at " << pos - mar.begin() + 1 << "\n"; + else + cout << x << " not found\n"; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10474/10474-18.cpp b/uva_cpp_clean/10474/10474-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..14aba974c7587bf8b023361b34754fde6d4f7c84 --- /dev/null +++ b/uva_cpp_clean/10474/10474-18.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int N, Q, cases = 0, marble; + vector marbles; + while(true){ + cin >> N >> Q; + if(N == 0 && Q == 0) break; + marbles.clear(); + for(int i = 0; i < N; i++){ + cin >> marble; + marbles.push_back(marble); + } + sort(marbles.begin(), marbles.end()); + cases++; + printf("CASE# %d:\n", cases); + for(int i = 0; i < Q; i++){ + cin >> marble; + int index = -1; + for(int i = 0; i < marbles.size(); i++){ + if(marbles.at(i) == marble){ + index = i; + break; + } + } + if(index != -1) printf("%d found at %d\n", marble, (index+1)); + else printf("%d not found\n", marble); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10474/10474-2.cpp b/uva_cpp_clean/10474/10474-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..390989c0210d544913bdaf4de0ba77a63518756d --- /dev/null +++ b/uva_cpp_clean/10474/10474-2.cpp @@ -0,0 +1,50 @@ +#include "bits/stdc++.h" + +#define el "\n" +#define all(v) ((v).begin()),((v).end()) + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +const int OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + ios::sync_with_stdio(false); + cin.tie(0); + + int n, q; + int t = 1; + while(cin >> n >> q) { + if (n == 0 && q == 0) break; + vi v(n); + for (int i = 0; i < n; ++i) cin >> v[i]; + sort(all(v)); + cout << "CASE# " << t++ << ":\n"; + while(q--) { + int x; + cin >> x; + auto ans = lower_bound(all(v), x); + cout << x; + int i = ans-v.begin(); + if (i >= n || v[i] != x) + cout << " not found\n"; + else + cout << " found at " << i+1 << el; + + } + } + + return 0; +} + +/* +*/ diff --git a/uva_cpp_clean/10474/10474-21.cpp b/uva_cpp_clean/10474/10474-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaac17bbdb613692cbfc816564b71f2b88c9cf94 --- /dev/null +++ b/uva_cpp_clean/10474/10474-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10474 + Name: Where is the Marble? + Problem: https://onlinejudge.org/external/104/10474.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int ar[10001]; +int main(){ + int n, q, x; + for(int cse=1; cin>>n>>q && (n||q); cse++) { + for (int i=0; i>ar[i]; + sort(ar, ar+n); + + cout<<"CASE# "<>x; + auto tt = equal_range(ar, ar+n, x); + if (tt.first + +using namespace std; + +int n, m, t, q; +vector a; + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + a.resize(n); + for(int i = 0; i < n; i++) scanf("%d", &a[i]); + printf("CASE# %d:\n", ++t); + sort(a.begin(), a.end()); + for(int i = 0; i < m; i++){ + scanf("%d", &q); + if(!binary_search(a.begin(), a.end(), q)) printf("%d not found\n", q); + else printf("%d found at %d\n", q, (int)(lower_bound(a.begin(), a.end(), q) - a.begin() + 1)); + } + a.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/10475/10475-21.cpp b/uva_cpp_clean/10475/10475-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19e02d1351c1e1b13a5210baafde51819c6117b7 --- /dev/null +++ b/uva_cpp_clean/10475/10475-21.cpp @@ -0,0 +1,88 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10475 + Name: Help the Leaders + Problem: https://onlinejudge.org/external/104/10475.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int adj[17], n; +bool chosed[17]; +string names[17]; +void dfs(int idx, int cnt, int bs) { + if (n-idx < cnt) return; + if (!cnt) { + bool frst = 1; + for (int i=0; i s2.length(); +} + +void toupper(string &s) { + for (int j=s.length()-1; j>=0; --j) + s[j] = toupper(s[j]); +} + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, c, p; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n >> p >> c; + for (int i=0; i> names[i]; + toupper(names[i]); + adj[i] = 1< ids; + for (int i=0; i> s1 >> s2; + toupper(s1); toupper(s2); + int u = ids[s1], + v = ids[s2]; + + adj[u] |= 1< +#include +using namespace std; + +typedef unordered_map uli; + +uli map; + +int N; +int candies[34]; + +int proc(int w1, int w2, int w3, int i) { + if (i >= N) { + int maxi = max(w1, max(w2, w3)), mini = min(w1, min(w2, w3)); + return maxi - mini; + } + if (!map.count(i + 100*(w3 + 1000*(w2 + 1000*(w1))))) { + int d1 = proc(w1 + candies[i], w2, w3, i+1); + int d2 = proc(w1, w2 + candies[i], w3, i+1); + int d3 = proc(w1, w2, w3 + candies[i], i+1); + map[i + 100*(w3 + 1000*(w2 + 1000*(w1)))] = min(d1, min(d2, d3)); + } + return map[i + 100*(w3 + 1000*(w2 + 1000*(w1)))]; +} + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + cin >> N; + map.clear(); + for (int i = 0; i < N; i++) cin >> candies[i]; + printf("Case %d: %d\n", c, proc(0, 0, 0, 0)); + } + return 0; +} diff --git a/uva_cpp_clean/10483/10483-21.cpp b/uva_cpp_clean/10483/10483-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e68d70f1edcd5e91a25979368df723bdf79cedfc --- /dev/null +++ b/uva_cpp_clean/10483/10483-21.cpp @@ -0,0 +1,122 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10483 + Name: The Sum Equals the Product + Problem: https://onlinejudge.org/external/104/10483.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool Sa[26001], Sb[26001]; +void _printABs() { + int dm = 10000; + for (int a=1; a+a+a<=25600; ++a) + for (int b=a; a+b+b<=25600; ++b) if (a*b != dm) { + int c = (a+b)*dm / (a*b-dm), + s = a+b+c; + + if (c>=b && s<=25600 && s*dm==a*b*c) + Sa[a] = Sb[b] = 1; + } + + cout << "int AA[] = { "; + for (int i=0; i<=25600; ++i) + if (Sa[i]) cout << i << ", "; + cout << "\n};\nint BB[] = { "; + for (int i=0; i<=25600; ++i) + if (Sb[i]) cout << i << ", "; + cout << "\n};"; +} + +int AA[] = { + 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, + 24, 25, 26, 28, 29, 30, 32, 33, 34, 35, 36, 40, 41, 42, 43, 44, 45, 46, 48, + 50, 51, 52, 54, 55, 56, 58, 59, 60, 63, 65, 66, 68, 69, 70, 71, 72, 74, 75, + 76, 80, 81, 82, 84, 85, 88, 90, 91, 94, 96, 98, 100, 105, 107, 110, 120, 121, + 125, 136, 140, 150 +}; +int BB[] = { + 101, 102, 104, 105, 108, 110, 111, 116, 120, 121, 124, 125, 130, 132, 135, + 136, 138, 140, 144, 145, 147, 150, 156, 160, 162, 166, 170, 172, 175, 176, + 178, 180, 185, 195, 200, 204, 205, 208, 210, 212, 216, 220, 225, 228, 240, + 246, 250, 254, 255, 260, 270, 275, 279, 280, 288, 300, 304, 308, 317, 319, + 325, 339, 340, 345, 348, 350, 360, 366, 375, 380, 395, 400, 406, 408, 410, + 414, 416, 417, 420, 425, 434, 436, 440, 445, 450, 468, 475, 480, 485, 500, + 513, 515, 516, 520, 525, 526, 532, 536, 540, 550, 552, 565, 570, 580, 600, + 604, 605, 620, 624, 625, 630, 637, 650, 660, 672, 700, 708, 740, 750, 760, + 800, 803, 825, 875, 900, 916, 939, 975, 1000, 1036, 1050, 1080, 1100, 1101, + 1120, 1125, 1177, 1200, 1202, 1212, 1250, 1256, 1300, 1324, 1335, 1350, 1375, + 1398, 1400, 1404, 1420, 1435, 1500, 1505, 1539, 1590, 1620, 1625, 1675, 1750, + 1770, 1800, 1808, 1840, 1875, 1879, 1924, 1990, 2000, 2100, 2175, 2200, 2250, + 2310, 2312, 2400, 2401, 2500, 2503, 2625, 2696, 2750, 2802, 2900, 3000, 3125, + 3126, 3250, 3275, 3300, 3500, 3604, 3750, 3752, 4000, 4065, 4500, 5000, 7000, + 7050, 7440, 7500, 7501, 8050, 8280, 9100, 9880, 10000, 2813, 202, 112 +}; + +struct Triplet { + int a, b, c; + int sum() const { return a+b+c; } + bool operator < (const Triplet &o) const { + int s1 = sum(), s2 = o.sum(); + if (s1 != s2) return s1 < s2; + if (a != o.a) return a < o.a; + if (b != o.b) return b < o.b; + return c < o.c; + } +} TR[360]; +int TRn; + +void generateAll() { + int dm = 10000; + for (int i=(sizeof(AA)>>2)-1; i>=0; --i) { + int a = AA[i]; + for (int j=(sizeof(BB)>>2)-1; j>=0; --j) { + int b = BB[j]; + if (b>=a && a*b != dm) { + int c = (a+b)*dm / (a*b-dm), + s = a+b+c; + + if (c>=b && s<=25600 && s*dm==a*b*c) { + TR[TRn].a = a; + TR[TRn].b = b; + TR[TRn].c = c; + ++TRn; + } + } + } + } + sort(TR, TR+TRn); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + generateAll(); + int la, lb, ra, rb; + char ch; + while (cin >> la>>ch>>lb >> ra>>ch>>rb) { + int l = la*100+lb, + r = ra*100+rb; + + for (int i=0; i=l && s<=r) + printf("%.02f = %.02f + %.02f + %.02f = %.02f * %.02f * %.02f\n", + (TR[i].a+TR[i].b+TR[i].c)/100.0, + TR[i].a/100.0, + TR[i].b/100.0, + TR[i].c/100.0, + TR[i].a/100.0, + TR[i].b/100.0, + TR[i].c/100.0 + ); + } + } +} diff --git a/uva_cpp_clean/10483/10483-9.cpp b/uva_cpp_clean/10483/10483-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d52011890918092ae5ace18fba3e1ec024167b12 --- /dev/null +++ b/uva_cpp_clean/10483/10483-9.cpp @@ -0,0 +1,27 @@ +#include + +#define SIZE 300 + +using namespace std; + +float L, U; +long long L1, U1, c, sum, p; +vector < tuple > v; + +int main(){ + scanf("%f %f", &L, &U); + L1 = L * 100, U1 = U * 100; + for(long long a = 0; a < U1; a++) + for(long long b = a; a + b < U1; b++){ + if( a * b == 10000 ) continue; + c = round( (a + b ) / ( a * b / 10000.0 - 1 ) ); + sum = a + b + c; + p = a * b * c; + if( L1 <= sum and sum <= U1 and c >= b and p == 10000 * sum ) + v.push_back( make_tuple( sum / 100.0, a / 100.0, b / 100.0, c / 100.0) ); + } + sort(v.begin(), v.end()); + for(int it = 0; it < v.size(); it++) + printf("%.2f = %.2f + %.2f + %.2f = %.2f * %.2f * %.2f\n", get<0>(v[it]), get<1>(v[it]), get<2>(v[it]), get<3>(v[it]), get<1>(v[it]), get<2>(v[it]), get<3>(v[it])); + return(0); +} diff --git a/uva_cpp_clean/10484/10484-9.cpp b/uva_cpp_clean/10484/10484-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fc28edfc52cc39c6d58dfdacf9740fedf9484f8 --- /dev/null +++ b/uva_cpp_clean/10484/10484-9.cpp @@ -0,0 +1,65 @@ +#include + +#define SIZE 110 + +using namespace std; + +bool is_prime[SIZE], ok; +vector prime; +int FFactors[SIZE][SIZE], N, D, v[SIZE]; +vector< pair > Factors; +long long ans; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + for(int it1 = 2; it1 < SIZE; it1++) + if( is_prime[it1] ){ + for(int it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +void FactorialFactors(){ + for(int num = 2; num < SIZE; num++){ + int pib = num; + for(int it = 0; it < prime.size(); it++){ + int ct = 0; + while( pib % prime[it] == 0 ) pib /= prime[it], ct++; + FFactors[num][ prime[it] ] = FFactors[num - 1][ prime[it] ] + ct; + } + } +} + +void GetFactors(int n){ + Factors.clear(); + for(int it = 0; it < prime.size(); it++) + if( n % prime[it] == 0 ){ + int ct = 0; + while( n % prime[it] == 0 ) n /= prime[it], ct++; + Factors.push_back( make_pair(prime[it], ct) ); + } + if( n != 1 ) ok = false; +} + +long long solve(){ + if( ok == false ) return 0; + ans = 1; + copy(FFactors[N], FFactors[N] + SIZE, v); + for(int it = 0; it < Factors.size(); it++) + v[ Factors[it].first ] -= Factors[it].second; + for(int it = 2; it < SIZE; it++) + if( v[it] < 0 ) return 0; + else ans *= v[it] + 1; + return ans; +} +int main(){ + sieve(); + FactorialFactors(); + while( scanf("%d %d", &N, &D), N | D){ + ok = true; + GetFactors(D); + printf("%lld\n", solve()); + } + return(0); +} diff --git a/uva_cpp_clean/10487/10487-13.cpp b/uva_cpp_clean/10487/10487-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1489852cce352c2a46d13c1f7d1cd7e9d9b46010 --- /dev/null +++ b/uva_cpp_clean/10487/10487-13.cpp @@ -0,0 +1,63 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int n, m, c = 1; + + cin >> n; + + while (n != 0) + { + int* a = new int[n]; + + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + + cin >> m; + + int* q = new int[m]; + + for (int i = 0; i < m; i++) + { + cin >> q[i]; + } + + int sum = 0, ans = 0, minDis = 900000000; + + printf("Case %d:\n", c); + + for (int k = 0; k < m; k++) + { + sum = 0; + ans = 0; + minDis = 900000000; + + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + sum = a[i] + a[j]; + + if (abs(q[k] - a[i] - a[j]) < minDis) + { + minDis = abs(q[k] - a[i] - a[j]); + + ans = sum; + } + } + } + + printf("Closest sum to %d is %d.\n", q[k], ans); + } + + c++; + + cin >> n; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10487/10487-19.cpp b/uva_cpp_clean/10487/10487-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68a1711e73670ee182fed5072e2560b9e14802b0 --- /dev/null +++ b/uva_cpp_clean/10487/10487-19.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +using namespace std; + +int main() { + int set[1000]; + int n, cases = 1; + while (scanf("%d", &n) && n != 0) { + for (int i = 0; i < n; i++) scanf("%d", &set[i]); + int m; + scanf("%d", &m); + printf("Case %d:\n", cases++); + while (m--) { + int val, dist = INT_MAX, sum = 0; + scanf("%d", &val); + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + int d = fabs((set[i]+set[j])-val); + if (d < dist) dist = d, sum = set[i]+set[j]; + } + } + printf("Closest sum to %d is %d.\n", val, sum); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10487/10487-2.cpp b/uva_cpp_clean/10487/10487-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0694ec41dfd35a30ba160e3f0533f83d0f595ea --- /dev/null +++ b/uva_cpp_clean/10487/10487-2.cpp @@ -0,0 +1,91 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int getClosest(int val1, int val2, int target) { + if (target - val1 >= val2 - target) + return val2; + else + return val1; +} + +int findClosest(vector& arr, int target) { + int n = (int) arr.size(); + + if (target <= arr[0]) + return arr[0]; + if (target >= arr[n - 1]) + return arr[n - 1]; + + int i = 0, j = n, mid = 0; + while (i < j) { + mid = (i + j) / 2; + if (arr[mid] == target) + return arr[mid]; + if (target < arr[mid]) { + if (mid > 0 && target > arr[mid - 1]) + return getClosest(arr[mid - 1], arr[mid], target); + j = mid; + } else { + if (mid < n - 1 && target < arr[mid + 1]) + return getClosest(arr[mid], arr[mid + 1], target); + i = mid + 1; + } + } + return arr[mid]; +} + + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + + int n; + int tc = 0; + while (scanf("%d", &n)) { + if (!n) break; + vector v(n); + for(int& i: v) scanf("%d", &i); + int m; scanf("%d", &m); + printf("Case %d:\n", ++tc); + vector pairs; + for (int i = 0; i < n; ++i) + for (int j = i+1; j < n; ++j) + pairs.push_back(v[i]+v[j]); + sort(all(pairs)); + while (m--) { + int val; scanf("%d", &val); + int clos = findClosest(pairs, val); + printf("Closest sum to %d is %d.\n", val, clos); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10487/10487-21.cpp b/uva_cpp_clean/10487/10487-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30e5f7369dba7275453261d334c3bdf5d0977c92 --- /dev/null +++ b/uva_cpp_clean/10487/10487-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10487 + Name: Closest Sums + Problem: https://onlinejudge.org/external/104/10487.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 2147483647 + +int abs(int x) { + return x<0 ? -x : x; +} + +int main(){ + int cse=1, n, q, d, X[2000]; + while (cin>>n && n) { + printf("Case %d:\n", cse++); + for (int i=0; i>X[i]; + sort(X, X+n); + + cin>>q; + for (int j=0; j> d; + long long int msd = INF, ms, diff, xt; + for (int i=0; i= 0 && ib!=i) { + xt = X[ib]; + diff = abs(X[i]+xt-d); + if (diff <= msd) { + msd = diff; + ms = X[i] + xt; + } + } + } + printf("Closest sum to %d is %lld.\n", d, ms); + } + } +} diff --git a/uva_cpp_clean/10487/10487-3.cpp b/uva_cpp_clean/10487/10487-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ee7dc11af7b7c69f6741bbdb89042ee4bbbb416 --- /dev/null +++ b/uva_cpp_clean/10487/10487-3.cpp @@ -0,0 +1,33 @@ +#include // sort +#include // printf scanf + +using std :: sort: + +int main () +{ + // Integer caseCounter: secondary variable to help us print the data in the given way + // Integer n: primary variable (stores input value) to determine size of input array + // Integer l: primary variable (stores input value) to determine number of requirements + // Integer temp: holds the required value + int caseCounter = 0; + int n, l, temp, result; + + while (scanf ("%d", &n) == 1 ) + { + // Input ends when n == 0 + if ( n == 0 ) return 0; + + int a[n]; + for ( int l = 0; l < n; l++ ) + scanf("%d",&a[l]); + + sort(a, a+n); + + scanf("%d", &l); + + while ( l-- ) + { + scanf("%d", &temp); + result = temp; + + diff --git a/uva_cpp_clean/10487/10487-5.cpp b/uva_cpp_clean/10487/10487-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..85283c44f375721f112c9836cdc7b606f7310406 --- /dev/null +++ b/uva_cpp_clean/10487/10487-5.cpp @@ -0,0 +1,206 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + + +int main() +{ + int n,no=0; + while(si(n)==1) + { + if(n==0) break; + int a[mx]; + + for0(i,n) + { + si(a[i]); + } + + vi v; + for0(i,n) + { + forab(j,i+1,n-1) + { + v.pb(a[i]+a[j]); + } + } + ST(v); + int m,k; + si(m); + k=sz(v); + + Case(no); + int ans; + while(m--) + { + int p,res=inf; + + si(p); + + for0(i,k) + { + int Min=abs(v[i]-p); + if(res>Min) + { + res=Min; + ans=v[i]; + } + + } + printf("Closest sum to %d is %d.\n",p,ans); + } + + } + + + + return 0; +} + diff --git a/uva_cpp_clean/10487/10487-9.cpp b/uva_cpp_clean/10487/10487-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c325869eea87a764bcd26800caf46272486edb6e --- /dev/null +++ b/uva_cpp_clean/10487/10487-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int n, m, q, ans, _min, tc; +vector v; + +int main(){ + while(scanf("%d", &n), n){ + v.resize(n); + for(int i = 0; i < n; i++) + scanf("%d", &v[i]); + scanf("%d", &m); + printf("Case %d:\n", ++tc); + while(m--){ + scanf("%d", &q); + _min = INT_MAX; + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + if(i != j && abs(q- (v[i] + v[j])) < _min) + _min = abs(q - (v[i] + v[j])), ans = v[i] + v[j]; + printf("Closest sum to %d is %d.\n", q, ans); + } + } +} diff --git a/uva_cpp_clean/10489/10489-19.cpp b/uva_cpp_clean/10489/10489-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29a031613706bea1b0d08926d4a1fac6dd76d42f --- /dev/null +++ b/uva_cpp_clean/10489/10489-19.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() { + int T; + cin >> T; + while (T--) { + int N, B; + cin >> N >> B; + int extras = 0; + for (int i = 0; i < B; i++) { + int K, c, choco = 1; + cin >> K; + for (int j = 0; j < K; j++) { + int a; + cin >> a; + choco *= a; + choco %= N; + } + extras += choco; + extras %= N; + } + printf("%d\n", extras); + } + return 0; +} diff --git a/uva_cpp_clean/10489/10489-21.cpp b/uva_cpp_clean/10489/10489-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70db566d2dd81aa39fcc819b676294c1d45dc93c --- /dev/null +++ b/uva_cpp_clean/10489/10489-21.cpp @@ -0,0 +1,65 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10489 + Name: Boxes of Chocolates + Problem: https://onlinejudge.org/external/104/10489.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + while (T--) { + int n = readUInt(), + b = readUInt(), + sum = 0; + while (b--) { + int k = readUInt(), x = 1; + while (k--) + x = (x * readUInt()) % n; + sum += x; + } + cout << (sum % n) << endl; + } +} diff --git a/uva_cpp_clean/10490/10490-14.cpp b/uva_cpp_clean/10490/10490-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..351fa5abd6824bccd42c7141e971cb6655af5116 --- /dev/null +++ b/uva_cpp_clean/10490/10490-14.cpp @@ -0,0 +1,92 @@ +/*************************************************** + * Problem name : 10490 Mr. Azad and his Son!!!!!.cpp + * Problem Link : https://uva.onlinejudge.org/external/104/10490.pdf + * OJ : Uva + * Verdict : AC + * Date : 06.06.2017 + * Problem Type : Number Theory + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 35 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +////////////////////////////////////////////////////////////////////////////////////// +ll ar[MAX]; +mapmp; +void sieve() { + ll sqrtMAX = sqrt(MAX); + for (ll i = 2; i <= sqrtMAX; i++) { + if (ar[i] == 0) { + for (ll j = i + i; j <= MAX; j += i) { + ar[j] = -1; + } + } + } +} +void perfectNum() { + mp[2] = 6; + mp[3] = 28; + mp[5] = 496; + mp[7] = 8128; + mp[13] = 33550336; + mp[17] = 8589869056; + mp[19] = 137438691328; + mp[31] = 2305843008139952128; +} +int main () { + sieve(); + perfectNum(); + int n; + while (scanf("%d", &n) == 1 && n != 0) { + if (mp[n] != 0 ) { + printf("Perfect: %lld!\n", mp[n]); + } else { + if (ar[n] == 0) { + printf("Given number is prime. But, NO perfect number is available.\n"); + } else { + printf("Given number is NOT prime! NO perfect number is available.\n"); + } + } + } + return 0; +} + diff --git a/uva_cpp_clean/10490/10490-21.cpp b/uva_cpp_clean/10490/10490-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5950e8824cf52158a5a6fb579b6a15d58b4aae2 --- /dev/null +++ b/uva_cpp_clean/10490/10490-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10490 + Name: Mr. Azad and his Son!!!!! + Problem: https://onlinejudge.org/external/104/10490.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int P[] = { 2, 3, 5, 7, 13, 17, 19, 31 }; +bool ip[32]; + +bool isprime(int x) { + for (int i=2; i> n && n) { + if (!ip[n]) + if (isprime(n)) + cout << "Given number is prime. But, NO perfect number is available.\n"; + else + cout << "Given number is NOT prime! NO perfect number is available.\n"; + else + printf("Perfect: %lld!\n", (1LL<<(n-1)) * ((1LL< +int main() +{ + int n; + while(scanf("%d",&n)==1) + { + if(n==0) + break; + else if (n==1) + printf("Perfect: 1!\n"); + else if (n==2) + printf("Perfect: 6!\n"); + else if (n==3) + printf("Perfect: 28!\n"); + else if (n==5) + printf("Perfect: 496!\n"); + else if (n==7) + printf("Perfect: 8128!\n"); + else if (n==13) + printf("Perfect: 33550336!\n"); + else if (n==17) + printf("Perfect: 8589869056!\n"); + else if (n==19) + printf("Perfect: 137438691328!\n"); + else if (n==31) + printf("Perfect: 2305843008139952128!\n"); + else if (n==11||n==23||n==29) + printf("Given number is prime. But, NO perfect number is available.\n"); + else + printf("Given number is NOT prime! NO perfect number is available.\n"); + } +} diff --git a/uva_cpp_clean/10490/10490-8.cpp b/uva_cpp_clean/10490/10490-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e97eca90537509bce4d4b6b4b1b1f0719da39e8d --- /dev/null +++ b/uva_cpp_clean/10490/10490-8.cpp @@ -0,0 +1,27 @@ +// Problem name: Mr. Azad and his Son!!!!! +// Problem link: https://vjudge.net/problem/UVA-10490 +// Submission link: https://vjudge.net/solution/32962433 + +#include + +using namespace std; + +typedef long long ll; + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const set prime_numbers{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + const set perfect_numbers{ 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128 }; + + for(int n ; cin >> n && n ; ){ + ll p = (1LL << n - 1) * ((1LL << n) - 1); + if(perfect_numbers.find(p) != perfect_numbers.end()) + cout << "Perfect: " << p << "!" << endl; + else if(prime_numbers.find(n) != prime_numbers.end()) + cout << "Given number is prime. But, NO perfect number is available." << endl; + else + cout << "Given number is NOT prime! NO perfect number is available." << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10490/10490-9.cpp b/uva_cpp_clean/10490/10490-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..71c1406a3f6ce89467d6de270c50d93a4d1eaa4b --- /dev/null +++ b/uva_cpp_clean/10490/10490-9.cpp @@ -0,0 +1,44 @@ +#include + +#define SIZE 100010 + +using namespace std; + +int n; +long long sd, num; +bool is_prime[SIZE]; +vectorprime; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + for(long long it1 = 2; it1 < SIZE; it1++){ + if(is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } + } +} + +int main(){ + sieve(); + while(scanf("%d",&n), n){ + sd = (1LL< num) break; + if( num % prime[it] == 0){ + long long p = prime[it]; + while( num % prime[it] == 0) p *= prime[it], num /= prime[it]; + sd *= ( p - 1 ) / ( prime[it] - 1 ); + } + } + if( num != 1 ) sd *= ( num * num - 1 ) / ( num - 1 ); + num = ( 1LL<<(n - 1) )* ( (1LL< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ solution is from online + //~ https://github.com/ajahuang/UVa/blob/master/UVa%2010491%20-%20Cows%20and%20Cars.cpp + __FastIO; + cout << setprecision (5) << fixed; + int NCOWS, NCARS, NSHOW; + + while (cin >> NCOWS >> NCARS >> NSHOW) { + // Two cases. + // 1.Pick a cow and switch to a car: + // NCOWS / (NCOWS + NCARS) + // * NCARS / (NCOWS + NCARS - NSHOW - 1) + // 2.Pick a car and switch to a car: + // NCARS / (NCOWS + NCARS) + // * (NCARS - 1) / (NCOWS + NCARS - NSHOW - 1) + cout << ( (double) NCARS * (NCOWS + NCARS - 1) ) / ( (NCOWS + NCARS) * + (NCOWS + NCARS - NSHOW - 1) ) << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/10491/10491-19.cpp b/uva_cpp_clean/10491/10491-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3679a93e16998807312e6820b8bdde184e16a469 --- /dev/null +++ b/uva_cpp_clean/10491/10491-19.cpp @@ -0,0 +1,11 @@ +#include +#include +using namespace std; + +int main() { + double ncows, ncars, nshow; //It's necessary to use double. + while (cin >> ncows >> ncars >> nshow/*scanf("%f %f %f", &ncows, &ncars, &nshow) != EOF*/) { + printf("%.5f\n", (ncars*(ncars-1+ncows))/((ncows+ncars)*(ncows+ncars-nshow-1))); + } + return 0; +} diff --git a/uva_cpp_clean/10491/10491-21.cpp b/uva_cpp_clean/10491/10491-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcedcf0284e70fd96840ca20800bc0d1b8fe4bcf --- /dev/null +++ b/uva_cpp_clean/10491/10491-21.cpp @@ -0,0 +1,23 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10491 + Name: Cows and Cars + Problem: https://onlinejudge.org/external/104/10491.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + double c,w,s; + while (cin>>w>>c>>s) { + double sum=c+w; + printf("%.5f\n", c*(sum-1)/(sum*(sum-s-1))); + } +} diff --git a/uva_cpp_clean/10494/10494-19.cpp b/uva_cpp_clean/10494/10494-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8f7f5c551598addbf230a436535bc3b989ecb00 --- /dev/null +++ b/uva_cpp_clean/10494/10494-19.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +using namespace std; + +int main() { + string S; + char * D; + while (cin >> S) { + char * D = new char[S.length() + 3]; + strcpy (D, S.c_str()); + D[S.length()] = 0; + char * C = new char[S.length() + 3]; + int posC = 0; + for (int i = 0; i < S.length() + 3; i++) C[i] = 0; + char sign; + long long d; + cin >> sign >> d; + int l = floor(log10(d)); + int ini = 0, i = l; + while (i < S.length()) { + long long val = 0; + for (int j = ini; j <= i; j++) { + val *= 10; + val += D[j] - '0'; + } + if (val < d) { + if (i + 1 < S.length()) { + i++; + val *= 10; + val += D[i] - '0'; + if (val < d) { + C[posC] = '0'; + posC++; + continue; + } + } else break; + } + C[posC] = (val / d) + '0'; + posC++; + long long rem = val % d; + int prev = ini; + ini = i; + while (rem != 0) { + D[ini] = (rem % 10) + '0'; + rem /= 10; + ini--; + } + ini++; + } + if (sign == '/') { + if (posC != 0) printf("%s\n", C); + else printf("0\n"); + } + else { + for (int j = ini; j < S.length(); j++) printf("%c", D[j]); + if (ini >= S.length()) printf("0"); + printf("\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/10494/10494-20.cpp b/uva_cpp_clean/10494/10494-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02f1d7979624f421c5b416a01d915d59cfa14637 --- /dev/null +++ b/uva_cpp_clean/10494/10494-20.cpp @@ -0,0 +1,40 @@ +#include +#include + +char m[10005]; + +int main() +{ + char sign; + long n; + + while( scanf("%s %c %ld", m, &sign, &n)==3 ) + { + int len = strlen(m); + if( sign=='/' ) + { + long rem = 0; + bool flag = true; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10494 + Name: If We Were a Child Again + Problem: https://onlinejudge.org/external/104/10494.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +#ifndef min +#define min(x,y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef max +#define max(x,y) ((x) > (y) ? (x) : (y)) +#endif + +class BigInt +{ +private: + char *digits; + int size; // number of used bytes (digits) + int capacity; // size of digits + int sign; // -1, 0 or +1 + +public: + BigInt(int n, int cap); + BigInt(int n); + BigInt(long double d); + BigInt(); + BigInt(string s); + BigInt(const char s[]); + BigInt(const BigInt &n); + const BigInt &operator=(const BigInt &n); + const BigInt &operator=(int n); + ~BigInt(); + void normalize(); + static int sig(int n); + static int sig(long double n); + inline int length() { return size; } + + BigInt operator++(); + BigInt operator++(int); + BigInt operator--(); + BigInt operator--(int); + BigInt operator-(); + BigInt operator+ (int n); + BigInt operator+ (BigInt n); + BigInt&operator+=(int n); + BigInt&operator+=(BigInt n); + BigInt operator- (int n); + BigInt operator- (BigInt n); + BigInt&operator-=(int n); + BigInt&operator-=(BigInt n); + BigInt operator* (int n); + BigInt operator* (BigInt n); + void operator*=(int n); + void operator*=(BigInt n); + BigInt operator/ (int n); + BigInt operator/ (BigInt n); + void operator/=(int n); + void operator/=(BigInt n); + int operator% (int n); + BigInt operator% (BigInt n); + void operator%=(int n); + void operator%=(BigInt n); + int divide(int n); + BigInt divide(BigInt n); + BigInt operator* (long double n); + void operator*=(long double n); + + + BigInt operator<< (int n); + void operator<<=(int n); + BigInt operator >> (int n); + void operator>>=(int n); + BigInt operator,(int n); + BigInt operator,(BigInt n); + + /** Casting **/ + bool operator!(); + operator bool(); + operator string(); + + bool operator<(BigInt n); + bool operator>(BigInt n); + bool operator==(BigInt n); + bool operator<=(BigInt n); + bool operator>=(BigInt n); + bool operator<(int n); + bool operator>(int n); + bool operator==(int n); + bool operator<=(int n); + bool operator>=(int n); + int compare(BigInt n); + + int toInt(); + string toString(); + void print(); + void printWithCommas(ostream &out); + +private: + void grow(); + friend istream &operator >> (istream &in, BigInt &n); + friend ostream &operator<<(ostream &out, BigInt n); + friend long double log2(BigInt x, long double epsilon); + inline friend long double log(BigInt x, long double epsilon); + inline friend long double log10(BigInt x, long double epsilon); + inline friend long double lg(BigInt x, long double epsilon); + inline friend long double ln(BigInt x, long double epsilon); +}; + +BigInt operator+(int m, BigInt &n) +{ + return n + m; +} + +BigInt operator-(int m, BigInt &n) +{ + return -n + m; +} + +BigInt operator*(int m, BigInt &n) +{ + return n * m; +} + +BigInt operator/(int m, BigInt &n) +{ + return BigInt(m) / n; +} + +BigInt operator%(int m, BigInt &n) +{ + return BigInt(m) % n; +} + +inline bool isDigit(int c) +{ + return(c >= (int)'0' && c <= (int)'9'); +} + +istream &operator >> (istream &in, BigInt &n) // FIXME: see inside +{ + n.size = 0; + n.sign = 1; + int sign = 1; + int c; + while ((c = in.peek()) >= 0 && + (c == ' ' || c == '\t' || c == '\r' || c == '\n')) + in.get(); + if (c < 0 || (c != (int)'-' && !isDigit(c))) + { + in >> c; // XXX: force in.fail() + return in; + } + if (c == (int)'-') { sign = -1; in.get(); } + + // FIXME: Extremely inefficient! Use a string. + while ((c = in.peek()) >= 0 && isDigit(c)) + { + in.get(); + n *= 10; + n += (c - (int)'0'); + } + n.sign = sign; + n.normalize(); + return in; +} + +ostream &operator<<(ostream &out, BigInt n) //FIXME: make more efficient +{ + return out << n.toString(); +} + +BigInt::BigInt(int n, int cap) +{ + cap = max(cap, (int)sizeof(n) * 8); + capacity = cap; + sign = sig(n); + n *= sign; + digits = new char[cap]; + memset(digits, 0, cap); + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(int n) +{ + capacity = 1024; + sign = sig(n); + n *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + while (n) + { + digits[size++] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(long double d) +{ + capacity = 1024; + sign = (d < 0 ? -1 : d > 0 ? 1 : 0); + d *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + d = floor(d); + while (d > 0) + { + digits[size++] = min(max(0, (int)((d - floor(d / 10) * 10) + 0.5)), 9); + d = floor(d / 10); + } +} + +BigInt::BigInt() +{ + capacity = 128; + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; +} + +BigInt::BigInt(string s) +{ + capacity = max((int)s.size(), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const char s[]) +{ + capacity = max((int)strlen(s), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const BigInt &n) +{ + capacity = n.capacity; + sign = n.sign; + size = n.size; + digits = new char[capacity]; + memcpy(digits, n.digits, capacity); +} + +const BigInt &BigInt::operator=(const BigInt &n) +{ + if (&n != this) + { + if (capacity < n.size) + { + capacity = n.capacity; + delete[] digits; + digits = new char[capacity]; + } + sign = n.sign; + size = n.size; + memcpy(digits, n.digits, size); + memset(digits + size, 0, capacity - size); + } + return *this; +} + +const BigInt &BigInt::operator=(int n) +{ + sign = sig(n); + n *= sign; + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } + return *this; +} + +BigInt::~BigInt() +{ + delete[] digits; +} + +void BigInt::normalize() +{ + while (size && !digits[size - 1]) size--; + if (!size) sign = 0; +} + +int BigInt::sig(int n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::sig(long double n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::toInt() +{ + int result = 0; + for (int i = size - 1; i >= 0; i--) + { + result *= 10; + result += digits[i]; + if (result < 0) return sign * 0x7FFFFFFF; + } + return sign * result; +} + +string BigInt::toString() +{ + string s = (sign >= 0 ? "" : "-"); + for (int i = size - 1; i >= 0; i--) + s += (digits[i] + '0'); + if (size == 0) s += '0'; + return s; +} + +void BigInt::print() //FIXME: make more efficient +{ + cout << toString(); +} + +void BigInt::printWithCommas(ostream &out) +{ + if (sign < 0) out.put('-'); + for (int i = size - 1; i >= 0; i--) + { + out.put(digits[i] + '0'); + if (!(i % 3) && i) out.put(','); + } + if (size == 0) out.put('0'); +} + +void BigInt::grow() +{ + char *olddigits = digits; + int oldCap = capacity; + capacity *= 2; + digits = new char[capacity]; + memcpy(digits, olddigits, oldCap); + memset(digits + oldCap, 0, oldCap); + delete[] olddigits; +} + +BigInt BigInt::operator++() +{ + operator+=(1); + return *this; +} + +BigInt BigInt::operator++(int) +{ + return operator++(); +} + +BigInt BigInt::operator--() +{ + operator-=(1); + return *this; +} + +BigInt BigInt::operator--(int) +{ + return operator--(); +} + +BigInt BigInt::operator-() +{ + BigInt result(*this); + result.sign *= -1; + return result; +} + +BigInt BigInt::operator+(int n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt BigInt::operator+(BigInt n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt &BigInt::operator+=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = nsign; + if (sign == nsign) + { + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] + dig + carry; + digits[i] = newdig % 10; + carry = newdig / 10; + n /= 10; + } + size = max(i, size); + } + else operator-=(-n); + return *this; +} + +BigInt &BigInt::operator+=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = n.sign; + if (sign == n.sign) + { + int carry = 0; + int i; + for (i = 0; i < maxS - 1 || carry; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig += n.digits[i]; + digits[i] = newdig % 10; + carry = newdig / 10; + } + size = max(i, size); + } + else + { + n.sign *= -1; + operator-=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator-(int n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt BigInt::operator-(BigInt n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt &BigInt::operator-=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = 1; + if (sign == nsign) + { + BigInt bin = n; + if (sign >= 0 && *this < bin || sign < 0 && *this > bin) + { + // Subtracting a bigger number + operator=(toInt() - n); + return *this; + } + + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] - dig + carry; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + n /= 10; + } + normalize(); + } + else operator+=(-n); + return *this; +} + +BigInt &BigInt::operator-=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = 1; + if (sign == n.sign) + { + if (sign >= 0 && *this < n || sign < 0 && *this > n) + { + // Subtracting a bigger number + BigInt tmp = n; + tmp -= *this; + *this = tmp; + sign = -sign; + return *this; + } + + int carry = 0; + int i; + for (i = 0; i < maxS - 1; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig -= n.digits[i]; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + } + if (carry) // Subtracted a bigger number, need to flip sign + { + if (i) digits[0] = 10 - digits[0]; + size = (i ? 1 : 0); + for (int j = 1; j < i; j++) + { + digits[j] = 9 - digits[j]; + if (digits[i]) size = j + 1; + } + sign *= -1; + } + normalize(); + } + else + { + n.sign *= -1; + operator+=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator*(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + int nsign = sig(n); + n *= nsign; + result.sign = sign * nsign; + if (!result.sign) return result; + + int i, j; + for (i = 0; n; i++) + { + int dig = n % 10; + if (dig) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = result.digits[i + j] + (j < size ? dig * digits[j] : 0) + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + n /= 10; + } + result.size = i + j - 1; + return result; +} + +BigInt BigInt::operator*(BigInt n) +{ + BigInt result(0, size + n.size); + + result.sign = sign * n.sign; + if (!result.sign) return result; + + int i, j; + for (i = 0; i < n.size; i++) + { + if (n.digits[i]) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = + result.digits[i + j] + + (j < size ? n.digits[i] * digits[j] : 0) + + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + } + result.size = i + j - 1; + + return result; +} + +void BigInt::operator*=(int n) +{ + operator=(operator*(n)); +} + +void BigInt::operator*=(BigInt n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator/(int n) +{ + if (!n) n /= n; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +BigInt BigInt::operator/(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +void BigInt::operator/=(int n) +{ + divide(n); +} + +void BigInt::operator/=(BigInt n) +{ + divide(n); +} + +int BigInt::operator%(int n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(int n) +{ + operator=(divide(n)); +} + +BigInt BigInt::operator%(BigInt n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(BigInt n) +{ + operator=(divide(n)); +} + +int BigInt::divide(int n) +{ + if (!n) n /= n; //XXX: force a crash + + int nsign = sig(n); + n *= nsign; + if (!sign) return 0; + sign *= nsign; + + int tmp = 0; + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = tmp / n; + tmp -= digits[i] * n; + } + normalize(); + return tmp; +} + +BigInt BigInt::divide(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + if (!sign) return 0; + sign *= n.sign; + + int oldSign = n.sign; + n.sign = 1; + + BigInt tmp(0, size); + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = 0; + while (tmp >= n) { tmp -= n; digits[i]++; } + } + normalize(); + + n.sign = oldSign; + + return tmp; +} + +// This is only exact to the first 15 or so digits, but it is +// never an over-estimate +BigInt BigInt::operator*(long double n) +{ + // the number of digits after the decimal point to use + #define DIGS_AFTER_DOT 15 + + int nsign = sig(n); + n *= nsign; + int ndigs = n >= 1 ? (int)log10(n) + 1 : 0; + BigInt result(0, size + ndigs); + result.sign = sign * nsign; + if (!result.sign) return result; + + if (n >= 1) for (int i = 0; i < ndigs; i++) n /= 10; + result.size = 0; + + char afterDot[DIGS_AFTER_DOT + 1]; + memset(afterDot, 0, sizeof(afterDot)); + + // Keep going until the DIGS_AFTER_DOT'th digit after the decimal point + for (int i = ndigs - 1; i >= -DIGS_AFTER_DOT; i--) + { + n *= 10; + int dig = (int)floor(n); + n -= dig; + if (!dig) continue; + + int carry = 0; + for (int j = 0; j < size || carry; j++) + { + int newdig = + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) + + dig * digits[j] + + carry; + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) = newdig % 10; + if (i + j >= 0 && result.digits[i + j]) result.size = max(result.size, i + j + 1); + carry = newdig / 10; + } + } + if (!result.size) result.sign = 0; + return result; +} + +void BigInt::operator*=(long double n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator<<(int n) +{ + BigInt result(*this); + result <<= n; + return result; +} + +void BigInt::operator<<=(int n) +{ + if (n < 0) operator>>=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator*=(mult); + } +} + +BigInt BigInt::operator >> (int n) +{ + BigInt result(*this); + result >>= n; + return result; +} + +void BigInt::operator>>=(int n) +{ + if (n < 0) operator<<=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator/=(mult); + } +} + +BigInt BigInt::operator,(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + for (result.size = 0; n; result.size++) + { + result.digits[result.size] = n % 10; + n /= 10; + } + memcpy(result.digits + result.size, digits, size * sizeof(digits[0])); + result.size += size; + result.sign = 1; + result.normalize(); + return result; +} + +BigInt BigInt::operator,(BigInt n) +{ + BigInt result(0, size + n.size); + memcpy(result.digits, n.digits, n.size * sizeof(n.digits[0])); + memcpy(result.digits + n.size, digits, size * sizeof(digits[0])); + result.size = size + n.size; + result.sign = 1; + result.normalize(); + return result; +} + +bool BigInt::operator!() +{ + return !size; +} + +BigInt::operator bool() +{ + return size; +} + +BigInt::operator string() +{ + return toString(); +} + +bool BigInt::operator<(BigInt n) +{ + return(compare(n) < 0); +} + +bool BigInt::operator>(BigInt n) +{ + return(compare(n) > 0); +} + +bool BigInt::operator>=(BigInt n) +{ + return(compare(n) >= 0); +} + +int BigInt::compare(BigInt n) +{ + if (sign < n.sign) return -1; + if (sign > n.sign) return 1; + if (size < n.size) return -sign; + if (size > n.size) return sign; + for (int i = size - 1; i >= 0; i--) + { + if (digits[i] < n.digits[i]) return -sign; + else if (digits[i] > n.digits[i]) return sign; + } + return 0; +} + +int main(){ + char op, n1[10000], n2[50]; + while (scanf("%s %c %s", n1, &op, n2) == 3) { + BigInt x(n1); + BigInt y(n2); + + if (op == '/') cout << x/y << endl; + else cout << (x%y) << endl; + } +} diff --git a/uva_cpp_clean/10494/10494-5.cpp b/uva_cpp_clean/10494/10494-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5e446f12fcaa85efb4b8a70cfbaa77aef714c54a --- /dev/null +++ b/uva_cpp_clean/10494/10494-5.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +char res[100000]; + +void dev(char s[],long long int n) +{ + long long int rem=0,i,j=0; + char a[100000]; + int f=1; + for(i=0;i +#include +#include +#include +#include +#include + +using namespace std; + +int n, minimum; + +int dist[30][30]; +vector< pair > points; + +void per(string s, int start, int size) +{ + static int counter = 0; + + if (start == size) + { + counter++; + + int temp = 0; + + for (int i = 1; i < n + 1; i++) + { + int pre = (int)(s[i - 1] - 'a'); + int cur = (int)(s[i] - 'a'); + + temp += dist[pre][cur]; + } + + temp += dist[(int)(s[0] - 'a')][(int)(s[n] - 'a')]; + + if (temp < minimum) + { + minimum = temp; + } + } + + else + { + for (int i = start; i <= size; i++) + { + char c = s[i]; + + s[i] = s[start]; + + s[start] = c; + + per(s, start + 1, size); + } + } +} + +int main() +{ + int t, sizeX, sizeY; + + cin >> t; + + while (t--) + { + cin >> sizeX >> sizeY; + + int x0, y0; + + cin >> x0 >> y0; + + points.clear(); + + points.push_back(make_pair(x0, y0)); + + cin >> n; + + string s = "abcdefghijklmnopqrstuvwxyz"; + + s = s.substr(0, n + 1); + + for (int i = 0; i < n; i++) + { + int x, y; + + cin >> x >> y; + + points.push_back(make_pair(x, y)); + } + + memset(dist, 0, sizeof 0); + + minimum = 99999999; + + for (int i = 0; i < n + 1; i++) + { + for (int j = 0; j < n + 1; j++) + { + int x1, y1, x2, y2; + + x1 = points[i].first; + y1 = points[i].second; + + x2 = points[j].first; + y2 = points[j].second; + + dist[i][j] = (abs)(x1 - x2) + (abs)(y1 - y2); + } + } + + per(s, 0, n); + + printf("The shortest path has length %d\n", minimum); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10496/10496-19.cpp b/uva_cpp_clean/10496/10496-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73575fac12c462aace3a7c4ea99e6219bc612d7c --- /dev/null +++ b/uva_cpp_clean/10496/10496-19.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; + +int X, Y, sx, sy, beepers; +ii pos[12]; +unordered_map > dp; + +int func(int x, int y, int visited) { + if (!dp.count(100*x + y) || !dp[100*x + y].count(visited)) { + int ret = 400000; + bool last = true; + for (int i = 0; i < beepers; i++) { + if ((visited >> i) % 2 == 0) { + last = false; + int val = func(pos[i].first, pos[i].second, (visited | (1<> cases; + while (cases--) { + dp.clear(); + cin >> X >> Y >> sx >> sy >> beepers; + for (int i = 0; i < beepers; i++) { + cin >> pos[i].first >> pos[i].second; + } + printf("The shortest path has length %d\n", func(sx, sy, 0)); + } + return 0; +} diff --git a/uva_cpp_clean/10496/10496-21.cpp b/uva_cpp_clean/10496/10496-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d5ab8189a641dbe0d4de6e083799f5d802cd694 --- /dev/null +++ b/uva_cpp_clean/10496/10496-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10496 + Name: Collecting Beepers + Problem: https://onlinejudge.org/external/104/10496.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +struct point{ + int x,y; +}; + +point beeper[12]; +int dist(int a, int b){ + return abs(beeper[a].x-beeper[b].x)+abs(beeper[a].y-beeper[b].y); +} + +int used[12],n,best; +bool check(int lb, int d, int i){ + if(used[i])return 0; + if(d+dist(lb,i)>=best)return 0; + return 1; +} + +void BT(int lb, int d, int c){ + if(c==n) + { + if(d+dist(lb,0)>cse; + while(cse--){ + cin>>width>>height; + cin>>beeper[0].x>>beeper[0].y; + cin>>n; + for(int i=1; i<=n; i++) + cin>>beeper[i].x>>beeper[i].y; + + best=100000; + BT(0,0,0); + printf("The shortest path has length %d\n",best); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10496/10496-6.cpp b/uva_cpp_clean/10496/10496-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3a819d300280b659c1ee7da8e99df21950d6cf8 --- /dev/null +++ b/uva_cpp_clean/10496/10496-6.cpp @@ -0,0 +1,53 @@ +/* + dynamic programming > traveling salesman problem (TSP) + difficulty: easy + date: 24/Jan/2020 + by: @brpapa +*/ +#include +#include +#include +#define INF (int)0x7f7f7f7f +#define MAX_N 11 +using namespace std; + +int N, dist[MAX_N][MAX_N]; + +int memo[MAX_N][1 << MAX_N]; +int tsp(int i, int bm) { + // cidade atual i, conjunto de cidades visitadas bm (inclui-se i) + + if ((1 << N) - 1 == bm) // todas cidades foram visitadas + return dist[i][0]; // volta + + int &ans = memo[i][bm]; + if (ans != -1) return memo[i][bm]; + + ans = INF; + for (int n = 0; n < N; n++) + if (n != i && !(bm & (1 << n))) // para cada cidade não visitada + ans = min(ans, dist[i][n] + tsp(n, bm | (1 << n))); + return ans; +} + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int T; cin >> T; + while (T--) { + int sizeX, sizeY; cin >> sizeX >> sizeY; + int x[11], y[11]; cin >> x[0] >> y[0]; + + cin >> N; ++N; + for (int i = 1; i < N; i++) + cin >> x[i] >> y[i]; + + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + dist[i][j] = abs(x[i]-x[j]) + abs(y[i]-y[j]); + + memset(memo, -1, sizeof memo); + printf("The shortest path has length %d\n", tsp(0, 1)); + } + return 0; +} diff --git a/uva_cpp_clean/10496/10496-9.cpp b/uva_cpp_clean/10496/10496-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9609f81fdeeedd2a506a34b9ab316c116250936a --- /dev/null +++ b/uva_cpp_clean/10496/10496-9.cpp @@ -0,0 +1,34 @@ +#include + +#define SIZE 12 + +using namespace std; + +int tc,X,Y,n,x[SIZE],y[SIZE],d[SIZE][SIZE],memo[SIZE][1<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10497 + Name: Sweet Child Makes Trouble + Problem: https://onlinejudge.org/external/104/10497.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + memset(ncells+size, 0, (capacity-size)*CellSize); + + if (cells) delete [] cells; + cells = ncells; + } + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + BigInt(const BigInt& o): cells(0), capacity(0) { + sign = o.sign; + setSize(o.size, 0); + for (int i=0; i= 0) + delete [] cells; + } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { + return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign; + } + bool operator == (const BigInt &o) const { return sign==o.sign ? numEq(o) : 0; } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + + for (int i=size; i= MAX_C ) { + cells[i] -= MAX_C; + cells[i+1]++; + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator*= (int x) { + sign *= sgn(x); + if (!sign) { + size=sign=0; + return *this; + } + if (x<0) x = -x; + + int cr = 0; + for (int i=0; i < size; i++) { + cells[i] = cells[i] * x + cr; + cr = cells[i] / MAX_C; + cells[i] %= MAX_C; + } + + if (cr) { + int ex = (countDigits(cr)+MAX_D-1)/MAX_D, sz=size; + setSize(size + ex); + size = sz; + for (; cr; cr /= MAX_C) + cells[size++] = cr % MAX_C; + } + + return autoSize(); + } + + BigInt operator* (int x) const { return *new BigInt(*this) *= x; } + + // --------------------------------------------------------------------- + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; + +int main(){ + BigInt DP[801]; + DP[0] = DP[1] = 0; + DP[2] = 1; + for (int i=3; i<801; i++) + DP[i] = (DP[i-1]+DP[i-2])*(i-1); + + int n; + while(scanf("%d", &n)==1 && n>=0) + cout << DP[n] << endl; +} diff --git a/uva_cpp_clean/10497/10497-9.cpp b/uva_cpp_clean/10497/10497-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1805ad436a7008e9e8e0af43fcdb0979e944cfeb --- /dev/null +++ b/uva_cpp_clean/10497/10497-9.cpp @@ -0,0 +1,84 @@ +#include + +#define LIM 800 +#define DIGITS 1978 + +using namespace std; + +int seq[LIM + 1][DIGITS], n, z, carry, m, k, pib, ind; + +void print_seq(int p){ + if(p == 1){ + printf("0\n"); + return; + } + for(int i = 0; i < DIGITS; i++) + if(seq[p][i] != 0){ + z = i; + break; + } + for(int i = z; i < DIGITS; i++) + printf("%d", seq[p][i]); + putchar('\n'); + return; +} + +void sum_one(int p){ + for(int r = DIGITS - 1; r > 0; r--){ + seq[p][r] += 1; + if(seq[p][r] < 10){ + break; + } + seq[p][r] %= 10; + } +} + +void subt_one(int p){ + for(int r= DIGITS - 1; r > 0; r--){ + seq[p][r] -= 1; + if(seq[p][r] < 0) + seq[p][r] += 10; + else{ + break; + } + } + return; +} + +void mult(int p){ + m = p; + pib = 0; + while(m){ + carry = 0; + k = m % 10; + ind = DIGITS - 1 - pib; + for(int r = DIGITS - 1; r > 0; r--){ + seq[p][ind] += seq[p - 1][r] * k + carry; + carry = seq[p][ind] / 10; + seq[p][ind] %= 10; + ind--; + } + m /= 10; + pib++; + } + return; +} + +void generateSeq(){ + seq[0][DIGITS - 1] = 1; + for(int i = 1; i <= LIM; i++){ + mult(i); + if(i & 1) + subt_one(i); + else + sum_one(i); + } + return; +} + +int main(){ + generateSeq(); + while(scanf("%d", &n), n != -1) + print_seq(n); + return(0); +} diff --git a/uva_cpp_clean/10498/10498-19.cpp b/uva_cpp_clean/10498/10498-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e330e0336b718dca3e92f947d363fa37cb4120f6 --- /dev/null +++ b/uva_cpp_clean/10498/10498-19.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +using namespace std; + +typedef vector vd; + +int n, m; +vector c, b, w, bauxi; +vector A, B_1; +double z; +double EPS = 1e-9; + +void simplex() { + B_1.assign(m, vd(m, 0)); + for (int i = 0; i < m; i++) B_1[i][i] = 1; + w.assign(m, 0); + bauxi.assign(m, 0); + + while (true) { + /*for (int i = 0; i < m; i++) printf("%f ", w[i]); + printf("%f\n", z); + for (int i = 0; i < m; i++) { + for (int j = 0; j < m; j++) { + printf("%f ", B_1[i][j]); + } + printf("%f\n", b[i]); + } + printf("-------------------------------------------------\n");*/ + + double maxi = 0; + int var = -1; + for (int i = 0; i < n + m; i++) { + double cauxi = 0; + for (int j = 0; j < m; j++) { + cauxi += w[j]*A[j][i]; + } + cauxi -= c[i]; + if (cauxi > maxi) maxi = cauxi, var = i; + } + if (var == -1 || maxi < EPS) break; + + for (int i = 0; i < m; i++) { + bauxi[i] = 0; + for (int j = 0; j < m; j++) { + bauxi[i] += B_1[i][j]*A[j][var]; + } + } + double mini; + int pos = -1; + bool first = false; + for (int i = 0; i < m; i++) { + if (bauxi[i] > EPS) { + if (first) { + double auxi = b[i]/bauxi[i]; + if (auxi < mini) mini = auxi, pos = i; + } else mini = b[i]/bauxi[i], pos = i, first = true; + } + } + if (pos == -1) break; + + for (int i = 0; i < m; i++) B_1[pos][i] /= bauxi[pos]; + b[pos] /= bauxi[pos]; + for (int i = 0; i < m; i++) { + w[i] -= B_1[pos][i]*maxi; + if (i == pos) continue; + for (int j = 0; j < m; j++) { + B_1[i][j] -= B_1[pos][j]*bauxi[i]; + } + b[i] -= b[pos]*bauxi[i]; + } + z -= b[pos]*maxi; + } +} + +int main() { + while (cin >> n >> m) { + c.assign(n + m, 0); + for (int i = 0; i < n; i++) cin >> c[i], c[i] = -c[i]; + A.assign(m, vd(n+m, 0)); + b.assign(m, 0); + z = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) cin >> A[i][j]; + A[i][i + n] = 1; + cin >> b[i]; + } + simplex(); + printf("Nasa can spend %d taka.\n", (int) ceil(-m*z)); + } + return 0; +} diff --git a/uva_cpp_clean/10499/10499-18.cpp b/uva_cpp_clean/10499/10499-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e9855cff9ede3ec672d25baa16c05833b29d393d --- /dev/null +++ b/uva_cpp_clean/10499/10499-18.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main(){ + long long int n; + while(scanf("%lld", &n) && n >= 0){ + if(n != 1) printf("%lld%%\n", 100 * n / 4); + else printf("0%%\n"); + } + return 0; +} +/* + Al cortar la esfera se nos genera una semiesfera, nos piden que calculemos la ganancia por area superficial, + asi que no consideraremos la semiesfera, sino, los dos semicirculos, dicho esto se divide el area del circulo + con la de la esfera inicial: pi*r^2/4*pi*r^2 = 1/4 = 25%, es decir, por cada parte se va generando una + ganacia del 25%, digo esto debido a que la parte base es el area de la esfera, de aqui se puede generar una + funcion de coste: C(x) = 4*pi*r^2 + (x-1)*(4*pi*0.25*r^2). +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10499/10499-19.cpp b/uva_cpp_clean/10499/10499-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c25cedbcd34aebb179db841ab8a2d1733e521b3 --- /dev/null +++ b/uva_cpp_clean/10499/10499-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + long long n; + while (scanf("%lld", &n) && n >= 0) { + if (n == 1) printf("0%%\n"); + else { + printf("%lld%%\n", (n*25)); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10499/10499-20.cpp b/uva_cpp_clean/10499/10499-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e928f2364bf6ceac607c33d9f9611d46c8a18db --- /dev/null +++ b/uva_cpp_clean/10499/10499-20.cpp @@ -0,0 +1,13 @@ +#include + +int main() +{ + long N; + + while( scanf("%ld", &N)==1 && N>0 ) + { + if( N==1 ) printf("0%%\n"); + else printf("%ld%%\n", N*25); + } + return 0; +} diff --git a/uva_cpp_clean/10499/10499-21.cpp b/uva_cpp_clean/10499/10499-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11725ad8ccc2dc94924e6120638940f099b35e96 --- /dev/null +++ b/uva_cpp_clean/10499/10499-21.cpp @@ -0,0 +1,22 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10499 + Name: The Land of Justice + Problem: https://onlinejudge.org/external/104/10499.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int main() { + long long int n; + while (cin >> n && n > -1) + printf("%lld%%\n", n>1 ? n * 25 : 0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10499/10499-5.cpp b/uva_cpp_clean/10499/10499-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4a621d2edead0601559555ae95aa7b356bcd43d7 --- /dev/null +++ b/uva_cpp_clean/10499/10499-5.cpp @@ -0,0 +1,13 @@ +#include +int main() +{ +long long int n; +while(scanf("%lld",&n)==1 && n>=0) +{ +if(n==1) +printf("0%%\n"); +else +printf("%lld%%\n",n*100/4); +} +return 0; +} diff --git a/uva_cpp_clean/10499/10499-9.cpp b/uva_cpp_clean/10499/10499-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d51599a3dea26d076833d00bc1469b898d38af88 --- /dev/null +++ b/uva_cpp_clean/10499/10499-9.cpp @@ -0,0 +1,11 @@ +#include + +using namespace std; + +long n; + +int main(){ + while(scanf("%ld", &n), n > 0) + printf("%ld%%\n", n!=1 ? n * 25 : n * 0); + return(0); +} diff --git a/uva_cpp_clean/105/105-13.cpp b/uva_cpp_clean/105/105-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7128ca786d74a6a578f714adc80e46e83845e921 --- /dev/null +++ b/uva_cpp_clean/105/105-13.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 10005 + +int main() +{ + int a[MAX]; + + memset(a, 0, sizeof a); + + int l, h, r; + + while (cin >> l >> h >> r) + { + for (int i = l; i < r; i++) + { + a[i] = max(a[i], h); + } + } + + int H = 0; + + bool flag = false; + + for (int i = 0; i < MAX; i++) + { + if (H != a[i]) + { + if (flag) + { + cout << " "; + } + + cout << i << " " << a[i]; + + flag = true; + + H = a[i]; + } + } + + cout << endl; +} \ No newline at end of file diff --git a/uva_cpp_clean/105/105-15.cpp b/uva_cpp_clean/105/105-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4698a627fc8ecf0e678e42b81dd05f8c50ac7ee1 --- /dev/null +++ b/uva_cpp_clean/105/105-15.cpp @@ -0,0 +1,30 @@ +/* + 天際線問題 + 時間複雜度: O(N) +*/ +#include +#include + +using namespace std; + +int main() { + int height[10001] = {}; + int i, l, h, r, end = 0; + + while (cin >> l >> h >> r) { + // 每一個點只儲存該點最高高度 + for (i = l; i < r; i++) + height[i] = max(height[i], h); + // 紀錄最右邊座標 + end = max(end, r); + } + + // 高度變換時就印出一組答案 + for (i = 1; i < end; i++) + if (height[i] != height[i - 1]) + cout << i << " " << height[i] << " "; + + // 最後記得補上0 + cout << i << " 0\n"; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/105/105-2.cpp b/uva_cpp_clean/105/105-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cde72caa995f5ee87e26d072f2d66337e1edf96 --- /dev/null +++ b/uva_cpp_clean/105/105-2.cpp @@ -0,0 +1,56 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +#define in cin +#define out cout +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +int main(){ + ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int height[10001] = {0}; + int l, r, h; + while(in >> l >> h >> r) { + for(int i = l; i < r; ++i) { + if (h > height[i]) height[i] = h; + } + } + bool f = 0; + int cur = 0; + for(int i = 0; i != 10000; ++i) { + if(height[i] != cur) { + if(f) cout << ' '; + else f = 1; + out << i << ' ' << height[i]; + cur = height[i]; + } + } + out << el; + return 0; + +} diff --git a/uva_cpp_clean/105/105-6.cpp b/uva_cpp_clean/105/105-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0baf16be0c86097633e64c509f94a2bdf91caca8 --- /dev/null +++ b/uva_cpp_clean/105/105-6.cpp @@ -0,0 +1,31 @@ +/* + ad-hoc > implementation + difficulty: easy + date: 26/Jun/2020 + problem: skyline + by: @brpapa +*/ +#include +using namespace std; + +int main() { + vector maxH(10001, 0); + + int l, h, r; + int maxR = -1; + while (cin >> l >> h >> r) { + maxR = max(maxR, r); + for (int i = l; i < r; i++) + maxH[i] = max(maxH[i], h); + } + + int prev = 0; + for (int i = 0; i < maxR; i++) { + int curr = maxH[i]; + if (curr != prev) cout << i << " " << curr << " "; + + prev = curr; + } + cout << maxR << " 0\n"; + return 0; +} diff --git a/uva_cpp_clean/105/105-9.cpp b/uva_cpp_clean/105/105-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..359a3089823b6ea60e39ac4cc19e60da28ba9c6e --- /dev/null +++ b/uva_cpp_clean/105/105-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int x, y, z, X = INT_MAX, Z = 0, h[10010]; + +int main(){ + while(scanf("%d %d %d", &x, &y, &z) == 3){ + for(int i = x; i < z; i++) + h[i] = max(h[i], y); + X = min(x, X); + Z = max(z, Z); + } + for(int i = X; i < Z; i++){ + printf("%d %d ",i, h[i]); + while(h[i] == h[i + 1]) i++; + } + printf("%d 0\n", Z); + return(0); +} diff --git a/uva_cpp_clean/10502/10502-21.cpp b/uva_cpp_clean/10502/10502-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..146607c9f7a829c10b11bf1a8c99e7f9b4c52670 --- /dev/null +++ b/uva_cpp_clean/10502/10502-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10502 + Name: Counting Rectangles + Problem: https://onlinejudge.org/external/105/10502.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; +// O(n^3) +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string line; + int n, m, M[143][143], D[143]; + while (cin >> n >> m && (n || m)) { + for (int i=0; i> line; + for (int j=0; j + +using namespace std; + +int m, n, ct, v[100][100]; + +inline bool isAllOne(int i, int j, int x, int y){ + for(int p = i; p <= x; p++) + for(int q = j; q <= y; q++) + if(!v[p][q]) return false; + return true; +} + +int main(){ + while(scanf("%d %d", &m, &n) == 2){ + ct = 0; + for(int i = 0; i < m; i++) + for(int j = 0; j < n; j++) scanf("%1d", &v[i][j]); + for(int i = 0; i < m; i++) + for(int j = 0; j < n; j++) + for(int r1 = 0; r1 < n - j; r1++) + for(int r2 = 0; r2 < m - i; r2++) + if(isAllOne(i, j, i + r2, j + r1)) ct++; + printf("%d\n", ct); + } + return(0); +} diff --git a/uva_cpp_clean/10503/10503-13.cpp b/uva_cpp_clean/10503/10503-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81a97f697dd86bd98152aeec9c2f8ebf3cc37d58 --- /dev/null +++ b/uva_cpp_clean/10503/10503-13.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, m; +bool found; +pair row[20]; +vector< pair > dominos; + +bool backTrack(int place, bool visit[20]) +{ + if (place == n && row[place].second == row[place + 1].first) + { + found = true; + } + + if (place < n) + { + for (int i = 0; i < m; i++) + { + pair temp; + pair swapTemp; + + temp = dominos[i]; + + swapTemp.first = temp.second; + swapTemp.second = temp.first; + + if (!visit[i]) + { + if (temp.first == row[place].second) + { + row[place + 1] = temp; + + visit[i] = true; + + backTrack(place + 1, visit); + + visit[i] = false; + } + + else if (swapTemp.first == row[place].second) + { + row[place + 1] = swapTemp; + + visit[i] = true; + + backTrack(place + 1, visit); + + visit[i] = false; + } + } + } + } + + return found; +} + +int main() +{ + while (cin >> n , n != 0) + { + cin >> m; + + found = false; + bool visit[20]; + dominos.clear(); + memset(row, 0, sizeof row); + memset(visit, 0, sizeof visit); + + pair f; + pair l; + + cin >> f.first >> f.second; + cin >> l.first >> l.second; + + row[0] = f; + row[n + 1] = l; + + for (int i = 0; i < m; i++) + { + int x, y; + + cin >> x >> y; + + dominos.push_back(make_pair(x, y)); + } + + if (n == 0) + { + if (l.first == f.second) cout << "NO" << endl; + + else cout << "YES" << endl; + } + + else + { + if (backTrack(0, visit)) + { + cout << "YES" << endl; + } + + else + { + cout << "NO" << endl; + } + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10503/10503-21.cpp b/uva_cpp_clean/10503/10503-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c23b07963a20120b76930095953560103f025d81 --- /dev/null +++ b/uva_cpp_clean/10503/10503-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10503 + Name: The dominoes solitaire + Problem: https://onlinejudge.org/external/105/10503.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int x[20], y[20], n, m, e1, + seen[32768][100][20], cse=0; + +bool canPlace(int used, int last, int ind) { + if (ind == n) return last == e1; + if (seen[used][last][ind] == cse) + return false; + seen[used][last][ind] = cse; + + int msk = 1; + for (int i=0; i recursive backtracking + difficulty: medium + date: 14/Apr/2020 + problem: given domino pieces, check if it is possible to arrive at a target piece from an initial piece using N intermediate pieces (possibly rotating them) + hint: DFS + backtracking + by: @brpapa +*/ +#include +using namespace std; + +typedef pair pii; // node +const int UNVISITED = -1; +const int VISITED = 0; + +int N; +map state; +pii target; +bool hasSolution; + +void dfs(pii u, int lvl, bool rotate) { + // rotate: peça u foi colocada invertida? + + if (hasSolution) return; + state[u] = VISITED; + + if (u == target && !rotate && lvl == N+2) { + hasSolution = true; + return; + } + + int last = rotate? u.first : u.second; + + // para cada nó ligável à last + for (auto s : state) { + pii v = s.first; + if (state[v] == UNVISITED) { + if (last == v.first) dfs(v, lvl+1, 0); + if (last == v.second) dfs(v, lvl+1, 1); + } + } + + state[u] = UNVISITED; // backtracking +} + +int main() { + while (cin >> N && N) { + int M; cin >> M; + pii init; cin >> init.first >> init.second; + cin >> target.first >> target.second; + + hasSolution = 0; + state.clear(); + state[target] = UNVISITED; + + while (M--) { + pii node; cin >> node.first >> node.second; + state[node] = UNVISITED; + } + + dfs(init, 1, 0); + cout << (hasSolution? "YES":"NO") << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10505/10505-21.cpp b/uva_cpp_clean/10505/10505-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8a6c57f655aa9cdfe56d590d7af44d753c062f7 --- /dev/null +++ b/uva_cpp_clean/10505/10505-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10505 + Name: Montesco vs Capuleto + Problem: https://onlinejudge.org/external/105/10505.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 1073741824 +list adj[202]; +int color[202], cnt; + +int dfs(int u) { + int c = 1-color[u], + sum = c; + for (int v: adj[u]) + if (color[v]==-1) { + cnt++; + color[v] = c; + sum += dfs(v); + } + else if (color[v] != c) + sum = -INF; + return sum; +} + + +int main(){ + int T, n, p, u; + cin>>T; + while (T--) { + cin>>n; + for (int i=0; i>p; + for (int j=0; j> u; + adj[i].push_back(u-1); + adj[u-1].push_back(i); + } + } + + int result = 0; + for (int i=0; i 0) + result += max(r, cnt-r); + } + cout << result << endl; + } +} diff --git a/uva_cpp_clean/10505/10505-9.cpp b/uva_cpp_clean/10505/10505-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3aa7543b4edd0d6988b9088ab8fcd2c13ac2bb20 --- /dev/null +++ b/uva_cpp_clean/10505/10505-9.cpp @@ -0,0 +1,71 @@ +#include + +#define WHITE -1 +#define RED 0 +#define BLACK 1 + +using namespace std; + +const int MAX_V = 210; + +int tc, V, E, a, b, black, red, ans, color[MAX_V]; +vector AdjList[MAX_V]; + +bool isBipartite(int source){ + bool isOK = true; + queue Q; + Q.push(source); + color[source] = RED; + red = black = 0; + while( not Q.empty() ){ + int u = Q.front(); Q.pop(); + if( color[u] == RED ) red++; + if( color[u] == BLACK ) black++; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( color[v] == WHITE ) + color[v] = color[u] ^ 1, Q.push(v); + else if( color[v] == color[u] ) + isOK = false; + } + } + return isOK; +} + +int t; + +void solve(){ + fill(color, color + V + 1, WHITE), ans = 0; + for(int i = 1; i <= V; i++) + if( color[i] == WHITE and isBipartite(i) ) + ans += max(red, black); + printf("%d\n", ans); +} + +void read(){ + scanf("%d", &V); + for(int i = 1; i <= V; i++){ + scanf("%d", &E); + for(int j = 0; j < E; j++){ + scanf("%d", &b); + if( b <= V ){ + AdjList[i].push_back(b); + AdjList[b].push_back(i); + } + } + } +} + +void clear(){ + for(int u = 1; u <= V; u++) AdjList[u].clear(); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/10506/10506-21.cpp b/uva_cpp_clean/10506/10506-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f26af835c82b85800d9c4c99dca52a5bb1ccc0e9 --- /dev/null +++ b/uva_cpp_clean/10506/10506-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10506 + Name: The Ouroboros problem + Problem: https://onlinejudge.org/external/105/10506.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int n, m, nmn, rem; +char R[68000]; +bool S[68000]; +bool dfs(int idx, int x) { + if (idx == nmn+m-1) + return 1; + + if (idx >= nmn) { + int xx = (x*n) % rem; + if (!S[xx]) { + S[xx] = 1; + if (dfs(idx+1, xx)) + return 1; + S[xx] = 0; + } + return 0; + } + + for (int d=0; d> m >> n) { + rem = pow(n, m); + nmn = pow(n, m); + memset(R, '0', nmn); + R[nmn] = 0; + + memset(S, 0, sizeof(S)); + S[0] = 1; + + if (dfs(m, 0)) + cout << R << '\n'; + } +} diff --git a/uva_cpp_clean/10507/10507-19.cpp b/uva_cpp_clean/10507/10507-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6fff6555acbb52e9b09201a80db1b6a0d87f59b6 --- /dev/null +++ b/uva_cpp_clean/10507/10507-19.cpp @@ -0,0 +1,66 @@ +#include +#include +using namespace std; + +typedef vector vi; + +int N, M; +vector AdjList; +vi * awake, * auxi; + +int main() { + awake = new vi(); + auxi = new vi(); + while (cin >> N >> M) { + AdjList.assign(30, vi()); + awake->assign(30, 0); + auxi->assign(30, 0); + string s; + getline(cin, s); + getline(cin, s); + (*awake)[s[0] - 'A'] = 1; + (*awake)[s[1] - 'A'] = 1; + (*awake)[s[2] - 'A'] = 1; + for (int i = 0; i < M; i++) { + getline(cin, s); + AdjList[s[0] - 'A'].push_back(s[1] - 'A'); + AdjList[s[1] - 'A'].push_back(s[0] - 'A'); + } + if (N <= 3) printf("WAKE UP IN, 0, YEARS\n"); + else { + bool stop = false; + int t = 0; + while (!stop) { + bool change = false; + for (int i = 0; i < 30; i++) { + (*auxi)[i] = (*awake)[i]; + if (!(*awake)[i]) { + int act = 0; + for (int j = 0; j < AdjList[i].size(); j++) { + int v = AdjList[i][j]; + if ((*awake)[v]) act++; + } + if (act >= 3) { + (*auxi)[i] = 1; + change = true; + } + } + } + stop = !change; + vi * sw = awake; + awake = auxi; + auxi = sw; + t++; + } + int count = 0; + for (int i = 0; i < 30; i++) { + count += (*awake)[i]; + } + if (count >= N && t > 1) printf("WAKE UP IN, %d, YEARS\n", t - 1); + else printf("THIS BRAIN NEVER WAKES UP\n"); + } + } + delete awake; + delete auxi; + return 0; +} diff --git a/uva_cpp_clean/10507/10507-21.cpp b/uva_cpp_clean/10507/10507-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1a9f403ddfb2f71bf3324c3f8223311be5b4c02 --- /dev/null +++ b/uva_cpp_clean/10507/10507-21.cpp @@ -0,0 +1,57 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10507 + Name: Waking up brain + Problem: https://onlinejudge.org/external/105/10507.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +bool has3(int x) { + for (int i=0; i<3; i++) { + if (!x) return false; + x -= x & -x; + } + return true; +} + +int main(){ + int n, m, cons[26]; + char w[100]; + while (scanf("%d%d %s", &n, &m, w)==3) { + int iter, naw=1, aw = 0; + memset(cons, 0, sizeof(cons)); + for (int i=0; i<3; i++) + aw |= 1 << (w[i] - 'A'); + + for (int i=0; i +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + int n,m; + while(cin>>n>>m){ + int e=0; + vvi a(n); + vi b(n),d(128,-1); + string s; + cin>>s; + for(char c:s){ + if(d[c]==-1)d[c]=e++; + b[d[c]]=1; + } + for(int i=0;i>s; + if(d[s[0]]==-1)d[s[0]]=e++; + if(d[s[1]]==-1)d[s[1]]=e++; + int u=d[s[0]],v=d[s[1]]; + a[u].push_back(v); + a[v].push_back(u); + } + int k=0; + while(1){ + vi c; + for(int i=0;i=3)c.push_back(i); + } + if(c.empty())break; + for(int x:c)b[x]=1; + k++; + } + int o=1; + for(int x:b)if(!x)o=0; + if(o)cout<<"WAKE UP IN, "< + +using namespace std; + +const int SIZE = 30; +int N, M, ans, idx, ct[SIZE]; +char ch1, ch2, ch3; +bool vis[SIZE], wake_up; +vector G[SIZE]; +map mp; + +void clear_(){ + for(int it = 0; it < N; it++) G[it].clear(); + idx = ans = 0; + memset(vis, false, sizeof vis); + mp.clear(); + wake_up = false; +} + +int main(){ + while(~scanf("%d", &N)){ + clear_(); + scanf("%d\n", &M); + scanf("%c%c%c\n", &ch1, &ch2, &ch3); + vis[0] = vis[1] = vis[2] = true; + mp[ch3] = idx++; + if( mp[ch2] == 0 and ch2 != ch3) mp[ch2] = idx++; + if( mp[ch1] == 0 and ch1 != ch3) mp[ch1] = idx++; + for(int it = 0; it < M; it++){ + scanf("%c%c\n", &ch1, &ch2); + if( mp[ch1] == 0 and ch1 != ch3) mp[ch1] = idx++; + if( mp[ch2] == 0 and ch2 != ch3) mp[ch2] = idx++; + G[mp[ch1]].push_back(mp[ch2]); + G[mp[ch2]].push_back(mp[ch1]); + } + while(true){ + if(all_of(vis, vis + N, [](int id){ return id; })){ + wake_up = true; + break; + } + ans++; + memset(ct, 0, sizeof ct); + for(int it = 0; it < N; it++){ + if( vis[it] ) continue; + for(int u = 0; u < G[it].size(); u++) + if( vis[G[it][u]] ) + ct[it]++; + } + bool possible = false; + for(int it = 0; it < N; it++){ + if( ct[it] >= 3 ) + possible = true, vis[it] = true; + } + if( not possible ) break; + } + if( wake_up ) printf("WAKE UP IN, %d, YEARS\n", ans); + else puts("THIS BRAIN NEVER WAKES UP"); + } + return(0); +} diff --git a/uva_cpp_clean/10508/10508-4.cpp b/uva_cpp_clean/10508/10508-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f8fb811805380878026fd5a5ccc07737874636b --- /dev/null +++ b/uva_cpp_clean/10508/10508-4.cpp @@ -0,0 +1,42 @@ +#include +#include + +using namespace std; + +int CalculateDifference(string input, string _template) +{ + int count = 0; + for (unsigned int i = 0; i < input.size(); i++) + if (input[i] != _template[i]) + count++; + + return count; +} + +int main() +{ + int word, len; + string x; + + while (cin >> word >> len) + { + vector words(word); + + cin >> x; + words[0] = x; + + cin >> x; + words[word - 1] = x; + + for (int i = 1; i < word - 1; i++) + { + cin >> x; + words[CalculateDifference(x, words[0])] = x; + } + + for (unsigned int i = 0; i < words.size(); i++) + cout << words[i] << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10509/10509-20.cpp b/uva_cpp_clean/10509/10509-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8b301262903a570f47e678dc99f1cb33a6e61ec --- /dev/null +++ b/uva_cpp_clean/10509/10509-20.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + double num; + + while( cin >> num && num ) + { + double a; + for(int i=1 ; ; i++) + if( i*i*i>num ) + { + a = i-1; + break; + } + + double dx = (num-(a*a*a))/(3*a*a); + + cout << fixed << setprecision(4) << a+dx << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10509/10509-21.cpp b/uva_cpp_clean/10509/10509-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e82fd1a6fc48609fbfaac3020b1af4f957842332 --- /dev/null +++ b/uva_cpp_clean/10509/10509-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10509 + Name: R U Kidding Mr. Feynman? + Problem: https://onlinejudge.org/external/105/10509.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(4); + + double n; + while (cin >> n && n!=0) { + int a = pow(n, 1/3.0); + if ((a+1)*(a+1)*(a+1) <= n) ++a; + double dx = (n - a*a*a) / (3*a*a); + cout << a + dx << endl; + } +} diff --git a/uva_cpp_clean/10515/10515-20.cpp b/uva_cpp_clean/10515/10515-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..500c3ef0e94db8ec915111a4ef448d15b757dcf6 --- /dev/null +++ b/uva_cpp_clean/10515/10515-20.cpp @@ -0,0 +1,22 @@ +#include +#include + +const int S = 105, lastDigit[][4] = {{0,0,0,0}, {1,1,1,1}, {6,2,4,8}, {1,3,9,7}, {6,4,6,4}, + {5,5,5,5}, {6,6,6,6}, {1,7,9,3}, {6,8,4,2}, {1,9,1,9}}; +char m[S], n[S]; + +int main() +{ + while( scanf("%s %s", m, n)==2 && (strcmp(m,"0") || strcmp(n,"0")) ) + { + int mLen = strlen(m), nLen = strlen(n); + if( nLen==1 && !strcmp(n,"0") ) printf("1\n"); + else + { + int pos = m[mLen-1]-'0', last = 0; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10515 + Name: Powers Et Al. + Problem: https://onlinejudge.org/external/105/10515.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int rr[] = {1, 1, 4, 4, 2, 1, 1, 4, 4, 2}; +int main(){ + char w1[200], w2[200]; + + while (cin>>w1>>w2 && (w1[0]!='0' || w2[0]!='0')) { + if (w2[0]=='0' && !w2[1]) puts("1"); + else { + int m = w1[strlen(w1)-1] - '0'; + if (rr[m] == 1) cout << m << endl; + else { + int n = 0; + for (int i=0; w2[i]; i++) + n = (n*10 + w2[i] - '0') % rr[m]; + if (!n) n = rr[m]; + + int x = 1; + while (n--) x *= m; + cout << (x%10) << endl; + } + } + } +} diff --git a/uva_cpp_clean/10515/10515-9.cpp b/uva_cpp_clean/10515/10515-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a28c86794cac8833fdcbd6fe47c9ff96864abf01 --- /dev/null +++ b/uva_cpp_clean/10515/10515-9.cpp @@ -0,0 +1,9 @@ +def main(): + while True: + m, n = map(int, input().split()) + if m == n == 0: + break + print(pow(m, n, 10)) + +if __name__=="__main__": + main() diff --git a/uva_cpp_clean/1052/1052-21.cpp b/uva_cpp_clean/1052/1052-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd011e87bb7d6ae74d029b27de782cadb136f142 --- /dev/null +++ b/uva_cpp_clean/1052/1052-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1052 + Name: Bit Compressor + Problem: https://onlinejudge.org/external/10/1052.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int L, N; +string S; +int bt(int pos, int l, int n) { + if (l > L || n > N) return 0; + if (pos == S.length()) + return l==L && n==N; + + int x = S[pos]-'0'; + if (!x) return bt(pos+1, l+1, n); + + int r = S[pos+1]!='1' ? bt(pos+1, l+1, n+1) : + S[pos+2]!='1' ? bt(pos+2, l+2, n+2) : 0; + if (r > 1) return 2; + + for (int i=pos+1; iL || n+x>N) break; + if (S[i+1] != '1' && x>2) { + r += bt(i+1, l+x, n+x); + if (r > 1) return 2; + } + } + return r; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; cin >> L >> N >> S; ++cse) { + int r = N<=L ? bt(0, 0, 0) : 0; + cout << "Case #" << cse << ": " << (!r ? "NO\n" : r < 2 ? "YES\n" : "NOT UNIQUE\n"); + } +} diff --git a/uva_cpp_clean/1052/1052-9.cpp b/uva_cpp_clean/1052/1052-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06ae609be8d0461a5f88021ca1a2d6e91cea63a3 --- /dev/null +++ b/uva_cpp_clean/1052/1052-9.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const int MAX_L = 43; + +int T, L, N, len, message; +char compressed[MAX_L]; + +void backtracking(int idx, int ones, int zeros) { + if (ones < 0 or zeros < 0) return; + if (idx == len) { + if (ones == 0 and zeros == 0) message++; + return; + } + if (compressed[idx] == '0') backtracking(idx + 1, ones, zeros - 1); + else { + long long num = 0; + for (int i = idx; i < len; i++) { + num = num * 2 + (compressed[i] - '0'); + if (num != 2 and (i + 1 == len or compressed[i + 1] == '0')) { + if (num == 3) backtracking(i + 1, ones - 2, zeros); + backtracking(i + 1, ones - num, zeros); + } + } + } +} + +int main() { + while (scanf("%d %d\n", &L, &N) == 2 and L | N) { + message = 0; + scanf("%s\n", compressed); + len = strlen(compressed); + backtracking(0, N, L - N); + printf("Case #%d: ", ++T); + if (message == 0) puts("NO"); + else if (message == 1) puts("YES"); + else puts("NOT UNIQUE"); + } + return (0); +} diff --git a/uva_cpp_clean/10520/10520-21.cpp b/uva_cpp_clean/10520/10520-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..894b542b81ccfd492c0e21a796196b65a4799f80 --- /dev/null +++ b/uva_cpp_clean/10520/10520-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10520 + Name: Determine it + Problem: https://onlinejudge.org/external/105/10520.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +unsigned long long DP[21][21], n, a; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n >> a) { + DP[n][1] = a; + for (int j=1; j<=n; ++j) + for (int i=n; i>=j; --i) + if (i!=n || j!=1) { + unsigned long long mx1 = 0; + for (int k=i+1; k<=n; ++k) + mx1 = max(mx1, DP[k][1] + DP[k][j]); + + unsigned long long mx2 = 0; + for (int k=1; k0; --i) + for (int j=i+1; j<=n; ++j) { + unsigned long long mx = 0; + for (int k=i; k + +#define SIZE 25 + +using namespace std; + +int n,v; +long memo[SIZE][SIZE]; + +long dp(int i,int j){ + if(~memo[i][j]) return memo[i][j]; + long a = 0, b = 0; + if(i>=j){ + if(i==n) a = 0; + else + for(int k=i+1;k<=n;k++) a = max(a,dp(k,1)+dp(k,j)); + if(j==0) b = 0; + else + for(int k=1;k +#include +using namespace std; + +const double eps=1e-9; + +int main() { + int n; + cin >> n; + while (n) { + double ha, hb, hc, v; + cin >> ha >> hb >> hc; + bool invalid = (ha <= 0 || hb <= 0 || hc <= 0); + if (!invalid) { + v = (1/ha + 1/hb + 1/hc)*(-1/ha + 1/hb + 1/hc)*(1/ha - 1/hb + 1/hc)*(1/ha + 1/hb - 1/hc); + invalid = (v <= eps); + } + if (invalid) printf("These are invalid inputs!\n"), n--; + else printf("%.3lf\n", sqrt(1/v)); + } + return 0; +} diff --git a/uva_cpp_clean/10523/10523-20.cpp b/uva_cpp_clean/10523/10523-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47d7b2e64758c223b48bf68366c2d0c827fc60e7 --- /dev/null +++ b/uva_cpp_clean/10523/10523-20.cpp @@ -0,0 +1,60 @@ +#include +#include + +int N, A, sum[155][200], carry; + +int main() +{ + while( scanf("%d %d", &N, &A)!=EOF ) + { + memset(sum, 0, sizeof(sum)); + + sum[0][0] = 1; + for(int i=1 ; i<=N ; i++) + for(int j=0 ; j<200 ; j++) + { + sum[i][j] += sum[i-1][j]*A; + if( sum[i][j]>9 ) + { + sum[i][j+1] += sum[i][j]/10; + sum[i][j] %= 10; + } + } + + for(int i=1 ; i<=N ; i++) + { + carry = 0; + for(int j=0 ; j<200 ; j++) + { + sum[i][j] = (sum[i][j]*i)+carry; + carry = 0; + if( sum[i][j]>9 ) + { + carry = sum[i][j]/10; + sum[i][j] %= 10; + } + } + } + + sum[0][0] = 0; + for(int i=1 ; i<=N ; i++) + for(int j=0 ; j<200 ; j++) + { + sum[i][j] += sum[i-1][j]; + if( sum[i][j]>9 ) + { + sum[i][j+1] += sum[i][j]/10; + sum[i][j] %= 10; + } + } + + int pos; + for(pos=200-1 ; pos>0 ; pos--) + if( sum[N][pos]!=0 ) break; + + for( ; pos>=0 ; pos--) printf("%d", sum[N][pos]); + printf("\n"); + + } + return 0; +} diff --git a/uva_cpp_clean/10523/10523-21.cpp b/uva_cpp_clean/10523/10523-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba7fc144a7b49fe18ad6a1b40770fda5207f1813 --- /dev/null +++ b/uva_cpp_clean/10523/10523-21.cpp @@ -0,0 +1,254 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10523 + Name: Very Easy !!! + Problem: https://onlinejudge.org/external/105/10523.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + memset(ncells+size, 0, (capacity-size)*CellSize); + + if (cells) delete [] cells; + cells = ncells; + } + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + BigInt(const BigInt& o): cells(0), capacity(0) { + sign = o.sign; + setSize(o.size, 0); + for (int i=0; i= 0) + delete [] cells; + } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { return sign==o.sign ? numLess(o) : sign < o.sign; } + bool operator == (const BigInt &o) const { return sign==o.sign ? numEq(o) : 0; } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + setSize(max(size, o.size)+1); + for (int i=0; i= MAX_C ) { + cells[i] -= MAX_C; + cells[i+1]++; + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + +int main(){ + int n, a; + while(cin>>n>>a) { + BigInt an(a), result(0); + for (int i=1; i<=n; i++) { + result += i*an; + if (i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10525 + Name: New to Bangladesh? + Problem: https://onlinejudge.org/external/105/10525.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m, u, v, + D[143][143], C[143][143]; + cin >> T; + while (T--) { + cin >> n >> m; + for (int i=0; i> u >> v >> t >> d;--u;--v; + if (t < C[u][v] || (t==C[u][v] && d < D[u][v])) { + D[u][v] = D[v][u] = d; + C[u][v] = C[v][u] = t; + } + } + + for (int k=0; k> m; + while (m--) { + cin >> u >> v; --u; --v; + if (D[u][v] == INF) cout << "No Path.\n"; + else cout << "Distance and time to reach destination is " << D[u][v] << " & " << C[u][v] << ".\n"; + } + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/10527/10527-9.cpp b/uva_cpp_clean/10527/10527-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ed009993942b542ef32c58e597afaa9982cf135 --- /dev/null +++ b/uva_cpp_clean/10527/10527-9.cpp @@ -0,0 +1,24 @@ +import sys + +def solution(): + while True: + N = int(sys.stdin.readline()) + if N == -1 : + break + if N == 0 or N == 1: + sys.stdout.write(str(10 + N) + '\n') + continue + ans = '' + for it in reversed(range(2,10)): + while N % it == 0 : + ans += str( it ) + N //= it + if N != 1 : + sys.stdout.write('There is no such number.\n') + else : + if len( ans ) == 1 : + ans += '1' + sys.stdout.write(ans[::-1] + '\n') + +if __name__=="__main__": + solution() diff --git a/uva_cpp_clean/10530/10530-14.cpp b/uva_cpp_clean/10530/10530-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fdaa4692f8e7ce4d3cddb690a80738c2117fe79a --- /dev/null +++ b/uva_cpp_clean/10530/10530-14.cpp @@ -0,0 +1,95 @@ +/*************************************************** + * Problem name : 10530 Guessing Game.cpp + * OJ : Uva + * Verdict : AC + * Date : 18.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 10 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int n, ans; + string str; + vectorH, L; + while (scanf("%d", &n) == 1 && n != 0) { + while (getline(cin, str)) { + if (str == "right on") { + ans = n; + break; + }else if (str == "too high") { + H.push_back(n); + } else if (str == "too low") { + L.push_back(n); + } + scanf("%d", &n); + } + //printf("%d\n", ans); + bool flag = false; + int sz = H.size(); + for (int i = 0; i < sz; i++) { + if (ans >= H[i]) { + flag = true ; + break; + } + } + if (flag) printf("Stan is dishonest\n"); + else { + sz = L.size(); + for (int i = 0; i < sz; i++) { + if (L[i] >= ans) { + flag = true; + } + } + if (flag) printf("Stan is dishonest\n"); + else printf("Stan may be honest\n"); + + } + L.clear(); + H.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/10530/10530-21.cpp b/uva_cpp_clean/10530/10530-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58eadb815fdc1f0b44001915984a582a8524b607 --- /dev/null +++ b/uva_cpp_clean/10530/10530-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10530 + Name: Guessing Game + Problem: https://onlinejudge.org/external/105/10530.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char w1[10], w2[10]; + int x, mx=11, mn=0; + + while (cin>>x>>w1>>w2 && x) { + if (w1[0]=='r') { + if (x>mn && xmn ? x : mn; + else + mx = x +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main() +{ + string s,c; + int n; + int low = 0; + int high= 11; + while(cin>>n) + { + if(n==0) break; + cin>>c>>s; + if(s[0]=='o') + { + if(n > low && n < high) + puts("Stan may be honest"); + else + puts("Stan is dishonest"); + low = 0; + high = 11; + } + else if(s[0]=='l'){ + low = max(low,n);} + else if(s[0]=='h'){ + high = min(high,n);} + } + return 0; +} diff --git a/uva_cpp_clean/10530/10530-9.cpp b/uva_cpp_clean/10530/10530-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2e2cc0810908235afb0ce6950bc343e678ffe06 --- /dev/null +++ b/uva_cpp_clean/10530/10530-9.cpp @@ -0,0 +1,22 @@ +#include + +#define SIZE 20 + +using namespace std; + +int x, lo = -1, hi = 11; +char response[SIZE]; + +int main(){ + while(scanf("%d\n", &x), x){ + scanf("%[^\n]s", response); + if( response[0] == 'r' ){ + if( lo < x and x < hi ) puts("Stan may be honest"); + else puts("Stan is dishonest"); + lo = -1, hi = 11; + } + else if( response[4] == 'h' ) hi = min(hi, x); + else lo = max(lo, x); + } + return(0); +} diff --git a/uva_cpp_clean/10532/10532-19.cpp b/uva_cpp_clean/10532/10532-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d48ead49c4c48b0f4a26f9111c866f374c3f5413 --- /dev/null +++ b/uva_cpp_clean/10532/10532-19.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +using namespace std; + +vector labels; +unordered_map > counts; + +int main() { + int cases = 1, n, m; + while (cin >> n >> m && !(n == 0 && m == 0)) { + labels.assign(n + 1, 0); + printf("Case %d:\n", cases++); + for (int i = 0; i < n; i++) { + int aux; + cin >> aux; + labels[aux]++; + } + for (int i = 0; i < m; i++) { + int r; + cin >> r; + counts.clear(); + counts[0][0] = 1; + for (int j = 1; j <= n; j++) { + for (int k = 0; k <= r; k++) counts[j][k] = counts[j-1][k]; + for (int k = 0; k <= r; k++) { + for (int l = 1; l <= labels[j] && l+k <= r; l++) { + counts[j][k+l] += counts[j-1][k]; + } + } + } + printf("%llu\n", counts[n][r]); + } + } + return 0; +} diff --git a/uva_cpp_clean/10532/10532-9.cpp b/uva_cpp_clean/10532/10532-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..056fb63fd1ad32b3b6537057e72a67e9dcc70061 --- /dev/null +++ b/uva_cpp_clean/10532/10532-9.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +const int SIZE = 55; + +int tc, n, m, r, v, ct[SIZE]; +long long memo[SIZE][SIZE]; + +long long dp(int i, int j){ + if( j == 0 ) return 1; + if( i == n + 1 ) return 0; + if(~memo[i][j]) return memo[i][j]; + long long ans = 0; + for(int k = 0; k <= min(j, ct[i]); k++) + ans += dp(i + 1, j - k); + return memo[i][j] = ans; +} + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + memset(memo, -1, sizeof memo); + memset(ct, 0, sizeof ct); + for(int it = 0; it < n; it++){ + scanf("%d", &v); + ct[v]++; + } + printf("Case %d:\n", ++tc); + while(m--){ + scanf("%d", &r); + printf("%lld\n", dp(0, r)); + } + } + return(0); +} diff --git a/uva_cpp_clean/10533/10533-19.cpp b/uva_cpp_clean/10533/10533-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bd94ba4520b22e39222e5e1639df530a4580b65 --- /dev/null +++ b/uva_cpp_clean/10533/10533-19.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<100000001> bs; +vi primes, dprimes; +unordered_set s; + +inline void sieve(ll upperbound = 1000000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + s.insert((int)i); + } +} + +int main() { + sieve(); + for (int i = 0; i < primes.size(); i++) { + int val = primes[i], sum = 0; + while (val) sum += val%10, val /= 10; + if (s.count(sum)) dprimes.push_back(primes[i]); + } + int N; + cin >> N; + while (N--) { + int t1, t2; + cin >> t1 >> t2; + printf("%ld\n", upper_bound(dprimes.begin(), dprimes.end(), t2) - lower_bound(dprimes.begin(), dprimes.end(), t1)); + } + return 0; +} diff --git a/uva_cpp_clean/10533/10533-20.cpp b/uva_cpp_clean/10533/10533-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..407e16f09192b8c03046fc9c9d9a746e0e94269a --- /dev/null +++ b/uva_cpp_clean/10533/10533-20.cpp @@ -0,0 +1,67 @@ +#include +#include + +unsigned n = 1000005, digitPrimes[1000005]; +bool primes[1000005]; + +void sieve_atkins() +{ + primes[2] = primes[3] = true; + for(unsigned i=5 ; iy ) + { + num = (3*x*x - y*y); + if( num<=n && (num%12==11) ) primes[num] = true; + } + } + + for(unsigned i=5 ; i<=lim ; i++) + if( primes[i] ) + for(unsigned j=i*i ; j<=n ; j+=i) primes[j] = false; +} + +unsigned digitSum(unsigned num) +{ + unsigned sum = 0; + while( num ) + { + sum += num%10; + num /= 10; + } + return sum; +} + +void digitPrimeGen() +{ + digitPrimes[0] = 0; + for(unsigned i=1 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10533 + Name: Digit Primes + Problem: https://onlinejudge.org/external/105/10533.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + + +#define MAXP 1000064 +int P[5+MAXP] = {0,0,1}; + +int dsum(int x) { + int r = 0; + for (; x; x/=10) + r += x%10; + return r; +} + +unsigned int prime[MAXP / 64]; +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) + +bool isPrime(int x) { + return x==2 || (x>2 && x&1 && (gP(x))); +} + +void sieve() { + memset(prime, -1, sizeof(prime)); + + int i, sqrtN = sqrt((double)MAXP) + 1; + for (i = 3; i < sqrtN; i += 2 ) { + if (gP(i)) { + P[i] = P[i-1] + isPrime(dsum(i)); + + int i2 = i<<1; + for(int j = i * i; j < MAXP; j += i2) + prime[j>>6] &= ~(1<<((j>>1)&31)); + } + else P[i] = P[i-1]; + P[i+1] = P[i]; + } + + for (; i>n; + while(n--) { + scanf("%d%d", &l, &r); + printf("%d\n", P[r] - P[l-1]); + } +} diff --git a/uva_cpp_clean/10533/10533-5.cpp b/uva_cpp_clean/10533/10533-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a8064b6bef81c2b9dd39e6f6ba8ce3ee457ef971 --- /dev/null +++ b/uva_cpp_clean/10533/10533-5.cpp @@ -0,0 +1,148 @@ +/****************************************************************** +*** Problewm : Digit Primes *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d:",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define CP(a) cout< +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + __builtin_popcountll(); +*/ +bool isPrime(int x) { + if (x <= 1) return false; if (x <= 3) return true; + if (x % 2 == 0 || x % 3 == 0) return false; + for (int i = 5; i * i <= x; i = i + 6) if (x % i == 0 || x % (i + 2) == 0) return false; + return true; +} + +int binarysearsch(vector& v, int l, int r, int key) +{ + while (r - l > 1) { + int m = l + (r - l) / 2; + if (v[m] >= key) + r = m; + else + l = m; + } + return r; +} + + +int n; + +vectorarr; + +vector lis() +{ + + vector liss(n, 0); + vector dp(n,1); + int l = 1,pos; + + liss[0] = arr[0]; + for (int i = 1; i < n; i++) { + + if (arr[i] > liss[l - 1]) { + liss[l] = arr[i]; + l++; + dp[i] = l; + } + else { + pos = binarysearsch(liss, -1, l - 1, arr[i]); + liss[pos] = arr[i]; + dp[i] = pos+1; + } + } + return dp; +} + + +void solveit() { + + while (cin>>n) { + arr = vector(n); + for (int i = 0; i < n; ++i) { + cin >> arr[i]; + } + + vector a = lis(); + + reverse(arr.begin(), arr.end()); + vector b = lis(); + + int ans = 0; + for (int i = 0; i < n; ++i) { + ans = max(ans, 2 * min(a[i], b[n-1-i])-1); + } + cout << ans << "\n"; + } +} + +int main() { + Sakr_ + + int t = 1; + ///cin>>t; + while (t--) { + solveit(); + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/10534/10534-14.cpp b/uva_cpp_clean/10534/10534-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f40161d990b0d0ae4a8a6132d58f0b1cb60b217 --- /dev/null +++ b/uva_cpp_clean/10534/10534-14.cpp @@ -0,0 +1,162 @@ +/*************************************************** + * Problem Name : 10534 - Wavio Sequence.cpp + * Problem Link : https://uva.onlinejudge.org/external/105/10534.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-02-06 + * Problem Type : dp(LIS) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 10005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int L[MAX], I[MAX]; +int L1[MAX], I1[MAX]; +vectorar, ar1; +void LIS_NlogK (int n) { + I[0] = -100000000; + + for (int i = 1; i <= n; i++) { + I[i] = 100000000; + } + + int Maxlen = 0; + + for (int i = 0; i < n; i++) { + int low = 0, high = Maxlen, mid; + + while (high >= low) { + mid = (low + high) / 2; + + if (I[mid] < ar[i]) { + low = mid + 1; + + } else { + high = mid - 1; + } + } + + I[low] = ar[i]; + L[i] = low; + + if (Maxlen < low) { + Maxlen = low; + } + } +} + +void LDS_NlogK (int n) { + I1[0] = -100000000; + + for (int i = 1; i <= n; i++) { + I1[i] = 100000000; + } + + int Maxlen = 0; + + for (int i = 0; i < n; i++) { + int low = 0, high = Maxlen, mid; + + while (high >= low) { + mid = (low + high) / 2; + + if (I1[mid] < ar1[i]) { + low = mid + 1; + + } else { + high = mid - 1; + } + } + + I1[low] = ar1[i]; + L1[i] = low; + + if (Maxlen < low) { + Maxlen = low; + } + } +} +int main () { + __FastIO; + int n; + + while (cin >> n) { + for (int i = 0; i < n; i++) { + int x; + cin >> x; + ar.pb (x); + ar1.pb (x); + } + + LIS_NlogK (n); + reverse (all (ar1) ); + LDS_NlogK (n); + int LL[MAX]; + + for (int i = 0, j = n - 1; i < n; i++, j--) { + LL[j] = L1[i]; + } + + int mx = 0; + + for (int i = 0; i < n; i++) { + int mn = min (L[i], LL[i]); + mx = max (mx, mn); + } + + cout << (mx * 2) - 1 << "\n"; + ar.clear(); + ar1.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/10534/10534-18.cpp b/uva_cpp_clean/10534/10534-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e5f7de17600a6c82030a732dcb24a747a56c547d --- /dev/null +++ b/uva_cpp_clean/10534/10534-18.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; +//http://www.csie.ntnu.edu.tw/~u91029/LongestIncreasingSubsequence.html#3 + +int n, num[10000], lis[10000], lds[10000]; +vector v; + +void LIS(){ + int size = 1; + v.push_back(num[0]); + lis[0] = 1; + for (int i = 1; i < n; i++) + { + if (num[i] > v.back()) + { + v.push_back(num[i]); + lis[i] = ++size; + } + else + { + int temp = lower_bound(v.begin(), v.end(), num[i]) - v.begin(); + v[temp] = num[i]; + lis[i] = temp + 1; + } + } +} + +void LDS(){ + int size = 1; + v.push_back(num[n - 1]); + lds[n - 1] = 1; + for (int i = n - 2; i >= 0; i--) + { + if (num[i] > v.back()) + { + v.push_back(num[i]); + lds[i] = ++size; + } + else + { + int temp = lower_bound(v.begin(), v.end(), num[i]) - v.begin(); + v[temp] = num[i]; + lds[i] = temp + 1; + } + } +} + +int main(){ + while (scanf("%d", &n) != EOF) + { + for (int i = 0; i < n; i++) scanf("%d", &num[i]); + v.clear(); + LIS(); + v.clear(); + LDS(); + int max = 0; + for (int i = 0; i < n; i++) + { + int temp = min(lis[i], lds[i]); + if (max < temp) max = temp; + } + printf("%d\n", 2 * max - 1); + } + return 0; +} +/* + La estrategia es usar una combinacion de LIS (Longest Increasing Sequence) y LDS (Longest Decreasing Sequence). + Ir iterando por todo el vector de LIS y LDS buscando el minimo entre ellos dos, luego, con ese numero, ver + si es el maximo. Todo esto se hace para buscar el n central de toda la secuencia +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10534/10534-21.cpp b/uva_cpp_clean/10534/10534-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92cca50a730c733b8c27103df7ea5d6f156e9529 --- /dev/null +++ b/uva_cpp_clean/10534/10534-21.cpp @@ -0,0 +1,87 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10534 + Name: Wavio Sequence + Problem: https://onlinejudge.org/external/105/10534.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// -------------------------------------------------------------- + +int X[10043], Y[10043], M[10043], L[10043], R[10043], n; +int LIS(int X[], int R[]) { + int L = 0; + for (int i=0; i>1; + if (M[mid] < X[i]) { + lo = mid + 1; + if (mid==L || M[lo]>=X[i]) break; + } + else hi = mid - 1; + } + + R[i] = lo; + M[lo] = X[i]; + L = max(L, lo); + } + return L; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while ((n=readUInt()) != EOF) { + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (10010) +#define mod 1000000007 +////=====================================//// + +ll a[mx],dp[mx]; + +int bs(int len,int i) +{ + int low, high, mid; + low=0; + high=len; + + while(low<=high) + { + mid = ( low+high ) >> 1; + if(dp[mid] < a[i]) + { + low = mid+1; + } + else + high = mid-1; + } + + dp[low] = a[i]; + + return low; +} + +int main() +{ + int n; + while(si(n)==1) + { + + int lis1[mx],lis2[mx]; + + for(int i=0;i=0;i--) + { + res = bs(m2,i);// lower_bound( dp , dp+n+1 , a[i] ) - dp; + dp[res] = a[i] ; + m2=max(m2,res); + lis2[i]=m2; + } + +// P(m2); + int ans=0; + for ( int i = 0 ; i < n ; i++ ) + ans = max ( ans , min(lis1[i],lis2[i])); + + P(2*ans-1); + } + return 0; +} +/* +7 +1 4 2 3 2 4 1 +*/ diff --git a/uva_cpp_clean/10534/10534-9.cpp b/uva_cpp_clean/10534/10534-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5432a59f63b9a566cf892708ee1e2cd22656e03e --- /dev/null +++ b/uva_cpp_clean/10534/10534-9.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int n, alis[10010], alds[10010], lis[10010], lds[10010], LIS, LDS, pos, ans; +vector v; + +int main(){ + while(scanf("%d", &n) != EOF){ + v.resize(n), LIS = LDS = 0; + for(int i = 0; i < n; i++) scanf("%d", &v[i]); + for(int i = 0; i < n; i++){ + pos = lower_bound(alis, alis + LIS, v[i]) - alis; + alis[pos] = v[i]; + lis[i] = pos + 1; + if(pos + 1 > LIS) LIS = pos + 1; + } + reverse(v.begin(), v.end()); + for(int i = 0; i < n; i++){ + pos = lower_bound(alds, alds + LDS, v[i]) - alds; + alds[pos] = v[i]; + lds[i] = pos + 1; + if(pos + 1 > LDS) LDS = pos + 1; + } + ans = 0; + for(int i = 0; i < n; i++) ans = max(ans, min(lis[i], lds[n - 1 - i])); + printf("%d\n", (ans << 1) - 1); + } + return(0); +} diff --git a/uva_cpp_clean/10539/10539-19.cpp b/uva_cpp_clean/10539/10539-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25731a84a6fc0f866fa5c72dc01649fe4821f3d0 --- /dev/null +++ b/uva_cpp_clean/10539/10539-19.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +using namespace std; + + +typedef long long ll; +typedef vector vi; +typedef vector vl; + +ll almost_primes_limit = 1000000000000; +ll sieve_size = 1000000; +bitset<10000010> bs; +vi primes; +vl almost_primes; + + +void sieve() { + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + for (ll j=i*i; j<=almost_primes_limit; j*=i) almost_primes.push_back(j); + primes.push_back((int)i); + } +} + +int main(){ + sieve(); + sort(almost_primes.begin(), almost_primes.end()); + //for(int i = 0; i < almost_primes.size(); i++) cout << almost_primes[i] << endl; + int cases; + cin >> cases; + for(int i = 0; i < cases; i++){ + ll low, high; + cin >> low >> high; + cout << upper_bound (almost_primes.begin(), almost_primes.end(), high)-lower_bound (almost_primes.begin(), almost_primes.end(), low) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10539/10539-21.cpp b/uva_cpp_clean/10539/10539-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c38140faa277f2b83af11df4807e2e9cfb9957d --- /dev/null +++ b/uva_cpp_clean/10539/10539-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10539 + Name: Almost Prime Numbers + Problem: https://onlinejudge.org/external/105/10539.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool isnp[1000143]; +long long v[80070]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int sz = 0; + for (long long j=4; j<1e12; j*=2) + v[sz++] = j; + + for (int i=3; i<1000143; i+=2) + if (!isnp[i]) { + for (long long j=(long long)i*i; j<1e12; j*=i) + v[sz++] = j; + + for (int j=i+i; j<1000143; j+=i) + isnp[j] = 1; + } + sort(v, v+sz); + + long long T, l, h; + cin >> T; + while (T-- && cin >> l >> h) { + cout << upper_bound(v, v+sz, h)-lower_bound(v, v+sz, l) << endl; + } +} diff --git a/uva_cpp_clean/10539/10539-5.cpp b/uva_cpp_clean/10539/10539-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0965632bf63c6a6706b217ad6f10bd0724830c3c --- /dev/null +++ b/uva_cpp_clean/10539/10539-5.cpp @@ -0,0 +1,227 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%I64d",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%I64d%I64d",&a,&b) + +#define sfi(t) scanf("%d",&t) +#define sfii(a,b) scanf("%d %d",&a,&b) +#define sfiii(a,b,c) scanf("%d %d %d",&a,&b,&c) +#define sfll(t) scanf("%I64d",&t) +#define sfllll(a,b) scanf("%I64d %I64d",&a,&b) +#define sfllllll(a,b,c) scanf("%I64d %I64d %I64d",&a,&b,&c) +#define sfd(t) scanf("%lf",&t) +#define sfc(c) scanf("%c",&c) +#define sfs(s) scanf("%s",s) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%I64d\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%I64d ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%I64d %I64d\n",a,b) +#define PPLN(a,b) printf("%I64d %I64d ",a,b) + +#define pfi(a) printf("%d\n",a) +#define pfii(a,b) printf("%d %d\n",a,b) +#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c) +#define pfll(a) printf("%I64d\n",a) +#define pfllll(a,b) printf("%I64d %I64d\n",a,b) +#define pfllllll(a,b,c) printf("%I64d %I64d %I64d\n",a,b,c) +#define pfd(a) printf("%lf\n",a) +#define pfs(a) printf("%s\n",a) +#define pfis(a) printf("%d ",a) +#define pflls(a) printf("%I64d ",a) +#define pfds(a) printf("%lf ",a) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<= l && almostprime[i] <= r) + cnt++; + else if(almostprime[i]>r) + break; +// P(cnt); + } + + P(cnt); + } + return 0; +} + diff --git a/uva_cpp_clean/10539/10539-6.cpp b/uva_cpp_clean/10539/10539-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a602bf0e88bd8cc6b9dceb6350bf17aa9fb818b --- /dev/null +++ b/uva_cpp_clean/10539/10539-6.cpp @@ -0,0 +1,52 @@ +/* + math > number theory > prime numbers > sieve of eratosthenes + difficulty: medium + date: 18/Mar/2020 + problem: compute the quantity of non-prime numbers in [lo .. hi] which are divisible by only a single prime number, 0 < lo <= hi < 10^12 + hint: generate all powers of each prime number + by: @brpapa +*/ +#include +#include +#include +#define ll long long +#define UB 1000100 // sqrt(10^12) +using namespace std; + +vector primes; +bitset is; + +void sieve() { + primes.clear(); + is.set(); is[0] = is[1] = false; + + for (ll i = 2; i <= UB; i++) + if (is[i]) { + primes.push_back(i); + for (ll j = i*i; j <= UB; j += i) is[j] = false; + } +} + +int main() { + sieve(); + + int T; cin >> T; + while (T--) { + ll lo, hi; cin >> lo >> hi; + + ll ans = 0; + + // para cada primo p em [0 .. sqrt(hi)] + for (ll p : primes) { + if (p*p > hi) break; + + // para cada potência de p, totalizando O(logp(hi)) iterações + for (ll j = p*p; j <= hi; j *= p) { + if (j >= lo) ans++; + } + } + + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10541/10541-9.cpp b/uva_cpp_clean/10541/10541-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99a246d80ab226b0edfe8977939215d3abaf2506 --- /dev/null +++ b/uva_cpp_clean/10541/10541-9.cpp @@ -0,0 +1,15 @@ +def main(): + SIZE = 300 + C = [[0 for i in range(SIZE)] for j in range(SIZE) ] + for i in range(SIZE): + C[i][0], C[i][i] = 1, 1 + for i in range(2, SIZE): + for k in range(1, i): + C[i][k] = C[i - 1][k] + C[i - 1][k - 1] + for tc in range( int( input() ) ): + N, K, *V = map(int, input().split()) + N -= sum(V) + print(C[N + 1][K]) + +if __name__ == '__main__': + main() diff --git a/uva_cpp_clean/10543/10543-21.cpp b/uva_cpp_clean/10543/10543-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc9c405c12f3ccc41f4c566abffdbeda00e4ee39 --- /dev/null +++ b/uva_cpp_clean/10543/10543-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10543 + Name: Traveling Politician + Problem: https://onlinejudge.org/external/105/10543.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct State { + int u, d; + State(int u, int d):u(u),d(d){} +}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m, k; + vector adj[55]; + while (cin>>n>>m>>k && (n||m||k)) { + bool seen[55][21]={}; + for (int i=0; i> u >> v; + adj[u].push_back(v); + } + + queue q; + q.push(State(0, 1)); + while (!q.empty()) { + State s = q.front(); q.pop(); + if (s.u==n-1 && s.d>=k) { + cout << s.d << "\n"; + goto fin; + } + + if (s.d < 20) + for (int v: adj[s.u]) + if (!seen[v][s.d+1]) { + seen[v][s.d+1] = 1; + q.push(State(v, s.d+1)); + } + } + cout << "LOSER\n"; + fin:; + } +} diff --git a/uva_cpp_clean/10550/10550-19.cpp b/uva_cpp_clean/10550/10550-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f731f8df610104b22d4fd5da010630a1597b0bd --- /dev/null +++ b/uva_cpp_clean/10550/10550-19.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main() { + int ini, c1, c2, c3; + while (scanf("%d %d %d %d", &ini, &c1, &c2, &c3) && !(ini == 0 && c1 == 0 && c2 == 0 && c3 == 0)) { + int grades = 0; + grades += c1 > ini? ini + 40 - c1: ini - c1; + grades += c2 < c1? c2 + 40 - c1: c2 - c1; + grades += c3 > c2? c2 + 40 - c3: c2 - c3; + grades *= 9; + grades += 1080; + printf("%d\n", grades); + } + return 0; +} diff --git a/uva_cpp_clean/10550/10550-21.cpp b/uva_cpp_clean/10550/10550-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba829137ad6cc1f4daccb198fbe94b01a7bd576b --- /dev/null +++ b/uva_cpp_clean/10550/10550-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10550 + Name: Combination Lock + Problem: https://onlinejudge.org/external/105/10550.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int sub(int a, int b) { + return (a>i>>a>>b>>c && (i||a||b||c)){ + int cm = 3*40 + sub(a, i) + sub(a, b) + sub(c, b); + cout << cm*9 < +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +#define mod 1000000007 +using namespace std; +int main() +{ + int a,b,c,s,n; + while(cin>>n>>a>>b>>c) + { + if(n==0&&a==0&&b==0&&c==0) break; + s = 0; + if (n b) + s+=40-(a-b); + else + s+=(b-a); + + if (b > c) + s +=(b-c); + else + s +=40-(c-b); + + cout<< (1080 + (9*s))< + +int main() { + int init, c1, c2, c3, ans; + while (scanf("%d %d %d %d", &init, &c1, &c2, &c3), init || c1 || c2 || c3) { + ans = 1080; + ans += (init - c1) > 0 ? (init - c1) * 9 : (init - c1 + 40) * 9; + ans += (c2 - c1) > 0 ? (c2 - c1) * 9 : (c2 - c1 + 40) * 9; + ans += (c2 - c3) > 0 ? (c2 - c3) * 9 : (c2 - c3 + 40) * 9; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10550/main.cpp b/uva_cpp_clean/10550/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8feaaed3ed5fcbe2294183d17d8e8dc811e72c7 --- /dev/null +++ b/uva_cpp_clean/10550/main.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int gradosD(int ini, int fin){ + int dif = fin - ini; + if (dif > 0){ + return (40-dif)*9; + }else if(dif < 0){ + return (-dif)*9; + } + return 0; +} + +int gradosI(int ini, int fin){ + int dif = fin - ini; + if (dif > 0){ + return (dif)*9; + }else if(dif < 0){ + return (40 + dif)*9; + } + return 0; +} + +int main(){ + int ini, p1, p2, p3; + while(cin >> ini >> p1 >> p2 >> p3){ + + if (ini == 0 && p1 == 0 && p2 == 0 && p3 == 0) break; + + int suma = 1080; + suma += gradosD(ini, p1); + suma += gradosI(p1, p2); + suma += gradosD(p2, p3); + cout << suma << '\n'; + } + return 0; +} diff --git a/uva_cpp_clean/10551/10551-21.cpp b/uva_cpp_clean/10551/10551-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01b704a5d6dc741ae041542010e0230cde74a8cc --- /dev/null +++ b/uva_cpp_clean/10551/10551-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10551 + Name: Basic Remains + Problem: https://onlinejudge.org/external/105/10551.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int b; + char ps[1024], ms[17], rs[17]; + while (cin >> b >> ps >> ms) { + int m = 0; + for (int i=0; ms[i]; ++i) + m = m*b + ms[i] - '0'; + + long long r = 0; + for (int i=0; ps[i]; ++i) + r = (r*b + ps[i] - '0') % m; + + char *rr = rs+17; + *(--rr) = 0; + if (!r) *(--rr) = '0'; + else while (r) + *(--rr) = (r%b) + '0', + r /= b; + + cout << rr << '\n'; + } +} diff --git a/uva_cpp_clean/10554/10554-9.cpp b/uva_cpp_clean/10554/10554-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4733dcdc317b2c65f66296cc893306e82a69f377 --- /dev/null +++ b/uva_cpp_clean/10554/10554-9.cpp @@ -0,0 +1,20 @@ +#include + +int n, p, i, j; +char s[100][100]; +char c[16][100]={"Happy", "birthday", "to", "you", "Happy", "birthday", "to", "you", "Happy", "birthday", "to", "Rujia", "Happy", "birthday", "to", "you"}; + +int main(){ + while(scanf("%d", &n) == 1){ + int k = 0, p = 1; + for(i = 0; i < n; i++) + scanf("%s", s[i]); + if(n > 16) p = (n / 16) + 1; + for(j = 0, i = 0; j < 16 * p; j++, i++, k++){ + if(i == n) i = 0; + if(j == 16) k = 0; + printf("%s: %s\n", s[i], c[k]); + } + } + return 0; +} diff --git a/uva_cpp_clean/10557/10557-13.cpp b/uva_cpp_clean/10557/10557-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad529b577e00d705eeb9d537f8af9bb9ca767a57 --- /dev/null +++ b/uva_cpp_clean/10557/10557-13.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +ofstream fout("f.out"); + +int n; +vector dist, label; +vector visited; +vector < vector > graph; + +bool dfs(int start) +{ + if (start == n) return true; + + visited[start] = true; + + for (int i = 0; i < graph[start].size(); i++) + { + int child = graph[start][i]; + + if (!visited[child] && dfs(child)) return true; + } + + return false; +} + +bool heisenberg() +{ + dist[1] = 100; + + for (int k = 0; k < n - 1; k++) + { + for (int head = 1; head <= n; head++) + { + for (int i = 0; i < graph[head].size(); i++) + { + int child = graph[head][i]; + + if (dist[head] > 0 && dist[head] + label[child] > dist[child]) + { + dist[child] = dist[head] + label[child]; + } + } + } + } + + bool pos = false; + + for (int head = 1; head <= n; head++) + { + for (int i = 0; i < graph[head].size(); i++) + { + int child = graph[head][i]; + + if (dist[head] > 0) + { + pos |= (dist[head] + label[child] > dist[child] && dfs(head)); + } + } + } + + if (pos || dist[n] > 0) return true; + + else return false; +} + +void initialize() +{ + label.clear(); + label.resize(n + 2); + + graph.clear(); + graph.resize(n + 2); + + visited.clear(); + visited.resize(n + 2, false); + + dist.clear(); + dist.resize(n + 2, INT_MIN); +} + +int main() +{ + while (cin >> n, n != -1) + { + initialize(); + + for (int i = 1; i <= n; i++) + { + int m, x; + + cin >> label[i]; + + cin >> m; + + for (int j = 0; j < m; j++) + { + cin >> x; + + graph[i].push_back(x); + } + } + + if (heisenberg()) cout << "winnable" << endl; + + else cout << "hopeless" << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10557/10557-19.cpp b/uva_cpp_clean/10557/10557-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0a816e793e07a913d95372c3143e36c26708707 --- /dev/null +++ b/uva_cpp_clean/10557/10557-19.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vi; +typedef vector vii; + +bool bellmanford(vector & AdjList, vi & energy, int V) { + int s = 0; + vi dist(V, 100000000); dist[s] = -100; + for (int i = 0; i < V - 1; i++) { + for (int u = 0; u < V; u++) { + for (int j = 0; j < (int)AdjList[u].size(); j++) { + ii v = AdjList[u][j]; + int value = min(dist[v.first], dist[u] - energy[v.first]); + if (value < 0) dist[v.first] = value; + } + } + } + if (dist[V-1] < 0) return true; + + vi nodes; + for (int u = 0; u < V; u++) { + for (int j = 0; j < (int)AdjList[u].size(); j++) { + ii v = AdjList[u][j]; + if (dist[u] - energy[v.first] < dist[v.first] && dist[u] - energy[v.first] < 0) nodes.push_back(v.first); + } + } + + queue reachable; + for (int i = 0; i < nodes.size(); i++) { + unordered_set visited; + visited.insert(nodes[i]); + reachable.push(nodes[i]); + while (!reachable.empty()) { + int u = reachable.front(); + for (int j = 0; j < (int)AdjList[u].size(); j++) { + if (AdjList[u][j].first == V-1) return true; + else if (!visited.count(AdjList[u][j].first)) reachable.push(AdjList[u][j].first), visited.insert(AdjList[u][j].first); + } + reachable.pop(); + } + } + return false; +} + +int main() { + int n; + while (cin >> n && n != -1) { + vector AdjList(n, vii()); + vi energy(n, 0); + for (int i = 0; i < n; i++) { + int r; + cin >> energy[i] >> r; + for (int j = 0; j < r; j++) { + int d; + cin >> d; + AdjList[i].push_back(make_pair(d - 1, 0)); + } + } + bool neg = bellmanford(AdjList, energy, n); + if (neg) printf("winnable\n"); + else printf("hopeless\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10557/10557-21.cpp b/uva_cpp_clean/10557/10557-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc1c6b1121985f4c25766d94f52f8a3a10cc4bda --- /dev/null +++ b/uva_cpp_clean/10557/10557-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10557 + Name: XYZZY + Problem: https://onlinejudge.org/external/105/10557.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + + +int E[114], D[114], n; +vector adj[114]; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin>>n && n>0) { + for (int i=0, d; i> E[i] >> d; + adj[i].resize(d); + for (int j=0; j> adj[i][j]; + --adj[i][j]; + } + D[i]=0; + } + + D[0] = 100; + + + for (int i=0, som=1; som && i<=n; ++i) { + som = 0; + for (int u=0; u 0) + for (int v: adj[u]) + if (D[u] + E[v] > D[v]) { + if (v == n-1) { + cout << "winnable\n"; + goto fin; + } + D[v] = i + +using namespace std; + +const int MAX_V = 1000, INF = 1000000000; + +int V, b, c, r, dis[MAX_V], W[MAX_V]; +vector AdjList[MAX_V]; +bool vis[MAX_V]; + +bool dfs(int x, int y){ + if( x == y ) return true; + vis[x] = true; + bool check = false; + for(int j = 0; j < AdjList[x].size(); j++) + if( not vis[AdjList[x][j]] ) + check |= dfs(AdjList[x][j], y); + return check; +} + +bool reachable(int x, int y){ + memset(vis, false, sizeof vis); + return dfs(x, y); +} + +bool NegativeCycle(){ + for(int u = 0; u < V; u++) + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j], w = W[v]; + if( dis[v] > dis[u] + w and dis[u] + w < 0 and reachable(u, V) ) + return true; + } + return false; +} + +void BellmanFord(int source){ + fill(dis, dis + V + 1, INF); + dis[source] = -100; + for(int i = 0; i < V - 1; i++) + for(int u = 1; u <= V; u++) + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j], w = W[v]; + if( dis[v] > dis[u] + w and dis[u] + w < 0 ) + dis[v] = dis[u] + w; + } +} + +void clear_(){ + for(int i = 1; i <= V; i++) AdjList[i].clear(); +} + +int main(){ + while(scanf("%d", &V), V != -1){ + clear_(); + for(int i = 1; i <= V; i++){ + scanf("%d %d", &c, &r); + for(int j = 0; j < r; j++){ + scanf("%d", &b); + AdjList[i].push_back(b); + } + W[i] = -c; + } + BellmanFord(1); + if( dis[V] < 0 or NegativeCycle() ) puts("winnable"); + else puts("hopeless"); + } + return(0); +} diff --git a/uva_cpp_clean/1056/1056-13.cpp b/uva_cpp_clean/1056/1056-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..800def1166e8d585db0ff3ee0995134949bc6a9f --- /dev/null +++ b/uva_cpp_clean/1056/1056-13.cpp @@ -0,0 +1,199 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, q, v, w, degree = 0; + +string firstName, secondName; + +vector> list; + +queue endof; + +vector marked; + +void dfs(int b) +{ + marked[b] = true; + + for (int i = 0; i < list[b].size(); i++) + { + if (!marked[list[b][i]]) + { + dfs(list[b][i]); + } + } +} + +int bfs(int a) +{ + int m = 0, counter = 0; + + vector parent; + + vector visited; + + queue bfsq; + + parent.resize(n + 1); + + visited.resize(n + 1); + + visited[a] = true; + + bfsq.push(a); + + parent[a] = -1; + + while (!bfsq.empty()) + { + int temp = bfsq.front(); + + bfsq.pop(); + + for (int i = 0; i < list[temp].size(); i++) + { + if (!visited[list[temp][i]]) + { + bfsq.push(list[temp][i]); + + visited[list[temp][i]] = true; + + parent[list[temp][i]] = temp; + } + } + } + + for (int i = 1; i <= n; i++) + { + int j = i; + + while (parent[j] != -1) + { + counter++; + + j = parent[j]; + } + + m = max(counter, m); + + counter = 0; + } + + return m; +} + +int main() +{ + int test = 0; + + bool ok = true; + + cin >> n >> q; + + while (n != 0 && q != 0) + { + test++; + + map names; + + list.resize(n + 1); + + marked.resize(n + 1); + + int index = 1; + + for (int i = 1; i <= q; i++) + { + cin >> firstName >> secondName; + + if (names[firstName] == 0) + { + v = index++; + + names[firstName] = v; + } + + else + { + v = names[firstName]; + } + + if (names[secondName] == 0) + { + w = index++; + + names[secondName] = w; + } + + else + { + w = names[secondName]; + } + + list[v].push_back(w); + + list[w].push_back(v); + } + + dfs(1); + + for (int i = 1; i <= n; i++) + { + if (!marked[i]) + { + ok = false; + } + } + + if (ok) + { + for (int i = 1; i <= n; i++) + { + degree = max(bfs(i), degree); + } + + endof.push(degree); + } + + else + { + endof.push(-1); + } + + for (int i = 1; i <= n; i++) + { + list.pop_back(); + + marked[i] = false; + } + + cin >> n >> q; + + ok = true; + + degree = 0; + } + + for (int i = 1; i <= test ; i++) + { + cout << "Network " << i << ": "; + + if (endof.front() != -1) + { + cout << endof.front() << endl << endl; + } + + else + { + cout << "DISCONNECTED" << endl << endl; + } + + endof.pop(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1056/1056-19.cpp b/uva_cpp_clean/1056/1056-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..130c43c9f14dbc2ce2e1a348487734210fc2cee5 --- /dev/null +++ b/uva_cpp_clean/1056/1056-19.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + void clear(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +ufds ds; + +vector AdjList; +unordered_map m; + +int main() { + int P, R, c = 1, visited[51]; + while (cin >> P >> R && !(P == 0 && R == 0)) { + AdjList.assign(P, vi()); + ds.clear(P); + m.clear(); + int count = 0; + for (int i = 0; i < R; i++) { + string s1, s2; + cin >> s1 >> s2; + if (!m.count(s1)) m[s1] = count, count++; + if (!m.count(s2)) m[s2] = count, count++; + AdjList[m[s1]].push_back(m[s2]); + AdjList[m[s2]].push_back(m[s1]); + ds.unionSet(m[s1], m[s2]); + } + printf("Network %d: ", c); + if (ds.numSets > 1) printf("DISCONNECTED\n\n"); + else { + int maxi = 0; + for (int i = 0; i < P; i++) { + for (int j = 0; j < P; j++) visited[j] = 0; + queue q; + q.push(i); + visited[i] = 1; + while (!q.empty()) { + int u = q.front()%100, d = q.front()/100; q.pop(); + if (d > maxi) maxi = d; + for (int j = 0; j < AdjList[u].size(); j++) { + if (!visited[AdjList[u][j]]) q.push(AdjList[u][j] + (d+1)*100), visited[AdjList[u][j]] = 1; + } + } + } + printf("%d\n\n", maxi); + } + c++; + } + return 0; +} diff --git a/uva_cpp_clean/1056/1056-21.cpp b/uva_cpp_clean/1056/1056-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..680a21efb56a1df4623e0d93d8a278f2e71eedbe --- /dev/null +++ b/uva_cpp_clean/1056/1056-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1056 + Name: Degrees of Separation + Problem: https://onlinejudge.org/external/10/1056.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +int idcnt; +unordered_map ids; +int readId() { + string s; + cin >> s; + auto r = ids.emplace(s, idcnt); + if (r.second) ++idcnt; + return r.first->second; +} + +int M[55][55]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m; + for (int cse=1; cin>>n>>m && (n||m); ++cse) { + cout << "Network " << cse << ": "; + + for (int i=0; i +#include +#include +#include + +#define MAX INT_MAX >> 1 + +int main() +{ + int caseNr = 0; + while (++caseNr) + { + int p, r; + std::cin >> p >> r; + + if (p == 0 && r == 0) + { + break; + } + + int *connections = new int[p * p]; + + for (int i = 0; i < p; i++) + { + for (int j = 0; j < p; j++) + { + if (i == j) + { + connections[i * p + j] = 0; + } + else + { + connections[i * p + j] = MAX; + } + } + } + + int index = 0; + std::unordered_map people; + + for (int i = 0; i < r; i++) + { + std::string from, to; + std::cin >> from >> to; + + if (people.find(from) == people.end()) + { + people[from] = index++; + } + + if (people.find(to) == people.end()) + { + people[to] = index++; + } + + connections[people[from] * p + people[to]] = 1; + connections[people[to] * p + people[from]] = 1; + } + + for (int k = 0; k < p; k++) + { + for (int i = 0; i < p; i++) + { + for (int j = 0; j < p; j++) + { + connections[i * p + j] = std::min(connections[i * p + j], connections[i * p + k] + connections[k * p + j]); + } + } + } + + int max = 0; + bool connected = true; + + for (int i = 0; i < p && connected; i++) + { + for (int j = 0; j < p && connected; j++) + { + if (connections[i * p + j] == MAX) + { + connected = false; + } + + max = std::max(max, connections[i * p + j]); + } + } + + std::cout << "Network " << caseNr << ": "; + + if (connected) + { + std::cout << max; + } + else + { + std::cout << "DISCONNECTED"; + } + + std::cout << std::endl + << std::endl; + + delete[] connections; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10562/10562-17.cpp b/uva_cpp_clean/10562/10562-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32dc10cd05402e78e7d4b8ea7f9f4ebe6b8d60c2 --- /dev/null +++ b/uva_cpp_clean/10562/10562-17.cpp @@ -0,0 +1,39 @@ +/** + * AOAPC II Example 6-17 Under the Trees + */ +#include +using namespace std; +vector graph; +void draw(int r, int c) +{ + cout << graph[r][c] << "("; + if (r + 1 < graph.size() && graph[r + 1][c] == '|') + { + int i = c; + while (0 <= i - 1 && graph[r + 2][i - 1] == '-') + --i; + for (; i < graph[r + 3].size() && graph[r + 2][i] == '-'; ++i) + if (graph[r + 3][i] != ' ') + draw(r + 3, i); + } + cout << ")"; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int T; + cin >> T; + string line; + getline(cin, line); + while (T--) + { + graph.clear(); + while (getline(cin, line) && line[0] != '#') + graph.push_back(line); + cout << "("; + if (graph.size()) + draw(0, graph[0].find_first_not_of(' ')); + cout << ")\n"; + } +} diff --git a/uva_cpp_clean/10563/10563-21.cpp b/uva_cpp_clean/10563/10563-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae719b94328297e78950b542ffd3650d9c8ae091 --- /dev/null +++ b/uva_cpp_clean/10563/10563-21.cpp @@ -0,0 +1,113 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10563 + Name: Least Squares + Problem: https://onlinejudge.org/external/105/10563.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int n, m; +char sq[200][200]; +bool isEmpty(int r, int c, int d) { + for (int i=0; i<=d; i++) + if (sq[r+d][c+i]!='?' || sq[r+i][c+d]!='?') + return false; + return true; +} + +int getCharId(int i, int j) { + if (i<0 || i>=n || j<0 || j>=m || sq[i][j]=='?' || sq[i][j]=='.') + return -1; + + return sq[i][j]-'A'; +} + +char nextChar(int r, int c, int d) { + bool used[]={0,0,0,0}; + int ind, cnt=0; + for (int i=0; i<=d; i++) { + ind=getCharId(r-1, c+i); + if (ind>=0 && !used[ind]) { + used[ind]=1; + cnt++; + } + + ind=getCharId(r+d+1, c+i); + if (ind>=0 && !used[ind]) { + used[ind]=1; + cnt++; + } + + ind=getCharId(r+i, c-1); + if (ind>=0 && !used[ind]) { + used[ind]=1; + cnt++; + } + + ind=getCharId(r+i, c+d+1); + if (ind>=0 && !used[ind]) { + used[ind]=1; + cnt++; + } + + if (cnt == 4) return 'E'; + } + + for (int i=0; i<4; i++) + if (!used[i]) + return 'A'+i; + return 'E'; +} + +void color(int r, int c, int d, char col) { + for (int i=0; i<=d; i++) + sq[r+d][c+i] = sq[r+i][c+d] = col; +} + +int nextEmpty(int ij) { + for (; ij col) break; + + int ij = nextEmpty(m*i + j + bd); + char ncol = ij>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10565 + Name: Matrix + Problem: https://onlinejudge.org/external/105/10565.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, n, x, t; + cin>>T; + while (T--) { + cin>>n; + x = 0; + for (int i=0; i>t; + if (t) x = t; + } + + if (!x) puts("-1"); + else { + puts("2"); + printf("%d 0\n0 %d\n", -x, -x); + } + } +} diff --git a/uva_cpp_clean/10566/10566-19.cpp b/uva_cpp_clean/10566/10566-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47f59daca188f2fe4a99ec15e8cc1048d7367e85 --- /dev/null +++ b/uva_cpp_clean/10566/10566-19.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; + +double proc(double x, double y, double c) { + double ini = 0, end = min(x, y), eps = 0.000001; + while (end - ini > eps) { + double mid = (ini + end)/2; + double hx = sqrt(x*x - mid*mid); + double hy = sqrt(y*y - mid*mid); + double dx = (mid*c/hx); + double dy = (mid*c/hy); + if (dx + dy < mid) ini = mid; + else if(dx + dy > mid) end = mid; + else ini = end; + } + return ini; +} + +int main() { + double x, y, c; + while (cin >> x >> y >> c) { + printf("%.3lf\n", proc(x, y, c)); + } + return 0; +} diff --git a/uva_cpp_clean/10567/10567-11.cpp b/uva_cpp_clean/10567/10567-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2905fc5aaef4599914f96b62ed3e15e20e5444c1 --- /dev/null +++ b/uva_cpp_clean/10567/10567-11.cpp @@ -0,0 +1,79 @@ +#include +/* +Problem: 10567 - Helping Fill Bates +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1508 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int Q; +string s, ss; +unordered_map> freq; + +int main () { + fastio; + + cin >> s >> Q; + + int len = s.size(); + + for (int i = 0; i < len; ++i) + freq[s[i]].push_back(i); + + while (Q--){ + cin >> ss; + + int last = 0, start = -1, end = -1; + bool matched = true; + + for (int i = 0; i < ss.size() && matched; ++i){ + int pos = lower_bound(freq[ss[i]].begin(), freq[ss[i]].end(), last) - freq[ss[i]].begin(); + + if (pos == freq[ss[i]].size()) + matched = false; + + pos = freq[ss[i]][pos]; + + if (i == 0) start = pos; + if (i == ss.size()-1) end = pos; + + last = pos+1; + } + + if (matched) cout << "Matched " << start << " " << end << '\n'; + else cout << "Not matched" << '\n'; + } + + return 0; +} +/* +Sample input:- +----------------- +aaaaaaaaaaaaaabbbbbbbbbdddddddddddccccccccccccc +3 +aaaaaaaaaaaaaaaaaaa +aaaaaaaaaaabbbbbbbbbbbc +abccc + +Sample output:- +----------------- + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10567/10567-13.cpp b/uva_cpp_clean/10567/10567-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2aed82153d6e160ca7bb2d2ca60c9b96b1cd47d --- /dev/null +++ b/uva_cpp_clean/10567/10567-13.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int q; +string s; +bool notFound; +vector ans; +map > myMap; + +void initialize() +{ + ans.clear(); + notFound = false; + + cin >> s >> q; + + for (int i = 0; i < (int)s.size(); i++) myMap[s[i]].push_back(i); +} + +int main() +{ + initialize(); + + for (int i = 0; i < q; i++) + { + string query; + + cin >> query; + + ans.clear(); + + int pre = 0; + + notFound = false; + + for (int j = 0; j < (int)query.size(); j++) + { + vector temp = myMap[query[j]]; + + vector::iterator it = lower_bound(temp.begin(), temp.end(), pre); + + if (it != temp.end()) + { + ans.push_back(*it); + + pre = *it + 1; + } + + else + { + notFound = true; + + break; + } + } + + if (notFound) cout << "Not matched" << endl; + + else printf("Matched %d %d\n", ans[0], ans[ans.size() - 1]); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10567/10567-21.cpp b/uva_cpp_clean/10567/10567-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c168910b20fbcf8c0cd2f264b325093ac460c14 --- /dev/null +++ b/uva_cpp_clean/10567/10567-21.cpp @@ -0,0 +1,57 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10567 + Name: Helping Fill Bates + Problem: https://onlinejudge.org/external/105/10567.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +char line[1000017], + qs[3517][117], + ch, nch; + +int aw[127][3517], n, + deg[127], index[3517], + strt[3517], endd[3517]; + + +int main(){ + cin>>line>>n; + + for (int i=0; i>qs[i]; ch=qs[i][0]; + aw[ch][deg[ch]++] = i; + endd[i]=-1; + } + + for (int k=0; (ch=line[k]); k++) { + int j=deg[ch]; + deg[ch] = 0; + for (int i=0; i + +using namespace std; + +char s[1000100]; +vector v[130]; +int q, ans1, ans2, p; +bool ok; + +int main(){ + scanf("%s\n", s); + for(int i = 0, j = strlen(s); i < j; i++) v[s[i]].push_back(i); + scanf("%d", &q); + while(q--){ + scanf("%s", s); + ans2 = -69; + ok = true; + for(int i = 0, j = strlen(s); i < j; i++){ + p = upper_bound(v[s[i]].begin(), v[s[i]].end(), ans2)-v[s[i]].begin(); + if(p == v[s[i]].size()){ + ok = false; + break; + } + ans2 = v[s[i]][p]; + if(i==0) ans1 = v[s[i]][p]; + } + if(ok) printf("Matched %d %d\n", ans1, ans2); + else printf("Not matched\n"); + } + return(0); +} diff --git a/uva_cpp_clean/10573/10573-20.cpp b/uva_cpp_clean/10573/10573-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8097f04c9f47bd1e5fdb28e1067f84140827082d --- /dev/null +++ b/uva_cpp_clean/10573/10573-20.cpp @@ -0,0 +1,31 @@ +#include +#include + +const double PI = 2*acos(0); + +int main() +{ + int tc; + + scanf("%d", &tc); + while( tc-- ) + { + double r1, r2; + scanf("%lf", &r1); + + if( getchar()=='\n' ) + { + double t = r1/4.0; + double area = ((t*2.0)*(t*2.0)*PI)-(2.0*(t*t)*PI); + printf("%.4lf\n", area); + } + else + { + scanf("%lf", &r2); + + double area = ((r1+r2)*(r1+r2)*PI)-(r1*r1*PI)-(r2*r2*PI); + printf("%.4lf\n", area); + } + } + return 0; +} diff --git a/uva_cpp_clean/10573/10573-21.cpp b/uva_cpp_clean/10573/10573-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..025d89b11f3a8c2ccb8b1de06d1d447da63dfd98 --- /dev/null +++ b/uva_cpp_clean/10573/10573-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10573 + Name: Geometry Paradox + Problem: https://onlinejudge.org/external/105/10573.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +const double PI = acos(-1); +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(4); + + int T, a, b; + cin >> T; + char str[100]; + cin.ignore(100, '\n'); + while (T--) { + cin.getline(str, 100); + if (sscanf(str, "%d%d", &a, &b)==2) + cout << PI*((a+b)*(a+b)-a*a-b*b) << '\n'; + else + cout << PI*a*a/8 << '\n'; + } +} diff --git a/uva_cpp_clean/10573/10573-9.cpp b/uva_cpp_clean/10573/10573-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e233e0549563b2745423f957061078c172333c5 --- /dev/null +++ b/uva_cpp_clean/10573/10573-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +const int LEN = 30; +const double PI = acos(-1.0); + +int tc, k; +double a, b, S; +char line[LEN]; + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + cin.getline(line, LEN); + k = sscanf(line, "%lf %lf", &a, &b); + if( k == 1 ) S = PI * a * a / 8.0; + else S = 2.0 * PI * a * b; + printf("%.4lf\n", S); + } + return(0); +} diff --git a/uva_cpp_clean/10576/10576-13.cpp b/uva_cpp_clean/10576/10576-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..949fbe650fb1b983cff1607845296e6fabea7b9a --- /dev/null +++ b/uva_cpp_clean/10576/10576-13.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +using namespace std; + +long long s, d, maximum, per; + +bool mustNegetive(bool month[12]) +{ + for (int i = 4; i < 12; i++) + { + long long sum = 0; + + for (int j = i; j > i - 5; j--) sum += (month[j] ? s : d); + + if (sum >= 0) return false; + } + + return true; +} + +int main() +{ + while (cin >> s >> d) + { + d *= -1; + + maximum = per = 0; + + for (int bitmask = 4095; bitmask >= 0; bitmask--) + { + int temp = bitmask; + + bool month[12]; + + for (int i = 0; i < 12; i++) + { + month[i] = (temp % 2); + + temp /= 2; + } + + if (mustNegetive(month)) + { + per = 0; + + for (int i = 0; i < 12; i++) per += (month[i] ? s : d); + + maximum = max(per, maximum); + } + } + + if (maximum == 0) cout << "Deficit" << endl; + + else cout << maximum << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10576/10576-21.cpp b/uva_cpp_clean/10576/10576-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86995f0afc1efde7b0f2b5403cfb94761969fd16 --- /dev/null +++ b/uva_cpp_clean/10576/10576-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10576 + Name: Y2K Accounting Bug + Problem: https://onlinejudge.org/external/105/10576.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int s, d; + while (cin >> s >> d) { + int m[12], sum = 0, res = 0, k; + for (k=0; k*d < (5-k)*s; ++k); + for (int i=0; i<12; ++i) { + if (i < 5) + m[i] = i<5-k ? s : -d; + else { + sum -= m[i-5]; + if (sum + s <= 0) + m[i] = s; + else m[i] = -d; + } + res += m[i]; + sum += m[i]; + } + + if (res < 0) cout << "Deficit\n"; + else cout << res << '\n'; + } +} diff --git a/uva_cpp_clean/10576/10576-9.cpp b/uva_cpp_clean/10576/10576-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c6066de86e3dd888ab79ca2e8dd6722f0bf6902 --- /dev/null +++ b/uva_cpp_clean/10576/10576-9.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int s, d, ans, sum; +bool ok; + +int main(){ + while(scanf("%d %d",&s, &d) == 2){ + ans = 0; + for(int i = 0, j = (1 << 12); i < j; i++){ + ok = true; + for(int j = 4; j < 12; j++){ + sum = 0; + for(int k = j - 4; k <= j; k++) + (i >> k) & 1 ? sum += s : sum -= d; + if(sum >= 0){ + ok = false; + break; + } + } + if(ok){ + sum = 0; + for(int j = 0; j < 12; j++) + (i >> j) & 1 ? sum += s : sum -= d; + if(sum > ans) + ans = sum; + } + } + ans > 0 ? printf("%d\n", ans) : printf("Deficit\n"); + } + return(0); +} diff --git a/uva_cpp_clean/10578/10578-19.cpp b/uva_cpp_clean/10578/10578-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce23f03e24b89d1e80002db149910ca15d3b453f --- /dev/null +++ b/uva_cpp_clean/10578/10578-19.cpp @@ -0,0 +1,34 @@ +#include +#include +using namespace std; + +int avaible[6]; + +int proc(int player, int points) { + if (points > 31) return player; + int i = 5; + bool ret = false; + while (i >= 0) { + if(avaible[i]) { + avaible[i]--; + ret = proc(1-player, points + i + 1) == player; + avaible[i]++; + if (ret) return player; + } + i--; + } + return 1 - player; +} + +int main() { + string s; + while (cin >> s) { + int points = 0; + for (int i = 0; i < 6; i++) avaible[i] = 4; + for (int i = 0; i < s.length(); i++) avaible[s[i]-'1']--, points += s[i]-'0'; + cout << s << " "; + if (proc(s.length()%2, points)) cout << "B\n"; + else cout << "A\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10578/10578-9.cpp b/uva_cpp_clean/10578/10578-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4953a7d2402342ff59d72f59858930eb79ddcff4 --- /dev/null +++ b/uva_cpp_clean/10578/10578-9.cpp @@ -0,0 +1,55 @@ +#include + +#define WIN 1 +#define FAIL -1 + +using namespace std; + +const int SIZE = 100, MAX_CARD = 10; + +char moves[SIZE]; +int sum, ct[MAX_CARD]; + +int minVal(); +int maxVal(); + +int minVal() { + int res = WIN; + for (int card = 1; card <= 6 and res != FAIL; card++) + if (ct[card]) { + sum += card, ct[card]--; + res = min(res, sum > 31 ? WIN : maxVal()); + sum -= card, ct[card]++; + } + return res; +} + +int maxVal() { + int res = FAIL; + for (int card = 1; card <= 6 and res != WIN; card++) + if (ct[card]) { + sum += card, ct[card]--; + res = max(res, sum > 31 ? FAIL : minVal()); + sum -= card, ct[card]++; + } + return res; +} + +int minimax(bool a_turn) { + sum = 0; + for (int id = 0; moves[id]; id++) sum += moves[id] - '0', ct[moves[id] - '0']--; + int ret = (a_turn) ? maxVal() : minVal(); + return ret; +} + +void init() { + for (int i = 0; i < MAX_CARD; i++) ct[i] = 4; +} + +int main() { + while (~scanf("%s\n", moves)) { + init(); + printf("%s %c\n", moves, minimax((strlen(moves) & 1) == 0) == WIN ? 'A' : 'B'); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10579/10579-14.cpp b/uva_cpp_clean/10579/10579-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ddf926e4636dabd3ec95c0de22c92bce04baca5 --- /dev/null +++ b/uva_cpp_clean/10579/10579-14.cpp @@ -0,0 +1,138 @@ +/*************************************************** + * Problem name : 10579 Fibonacci Numbers.cpp + * Problem Link : https://uva.onlinejudge.org/external/105/10579.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-26 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 5003 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +string fib[MAX]; +string add(string a, string b) { + int al = a.size() - 1; + int bl = b.size() - 1; + + int carry = 0; + string result = ""; + + while (al >= 0 && bl >= 0) { + int temp = (int)(a[al] - '0') + (int)(b[bl] - '0') + carry ; + carry = 0; + if (temp > 9 ) { + carry = 1; + temp = temp - 10; + } + result += char(temp + '0'); + al--; + bl--; + } + while (al >= 0) { + int temp = (int)(a[al] - '0') + carry ; + carry = 0; + if (temp > 9) { + carry = 1; + temp = temp % 10; + } + + result += char(temp + '0'); + al--; + } + while (bl >= 0) { + int temp = (int)(b[bl] - '0') + carry ; + carry = 0; + if (temp > 9) { + carry = 1; + temp = temp % 10; + } + result += char(temp + '0'); + bl--; + } + if (carry) { + result += "1"; + } + + string addition = ""; + reverse(result.begin(), result.end()); + return result; +} + +string trim(string a) { // for removing leading 0s + string res = ""; + int i = 0; + + while (a[i] == '0') { + i++; + } + int sz = a.size(); + for (; i < sz; i++) { + res += a[i]; + } + return res; +} +void preCal() { + string s1 = "0"; + string s2 = "1"; + fib[0] = s1; + fib[1] = s2; + for (int i = 2; i <= MAX; i++) { + string s = trim(add(s1, s2)); + fib[i] = s; + s1 = s2; + s2 = s; + } +} +int main () { + int n; + preCal(); + while (scanf("%d", &n) == 1) { + cout << fib[n] << "\n"; + } + return 0; +} + diff --git a/uva_cpp_clean/10579/10579-20.cpp b/uva_cpp_clean/10579/10579-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..870401c692f9db09cde2f4af0b7719731ad16ad4 --- /dev/null +++ b/uva_cpp_clean/10579/10579-20.cpp @@ -0,0 +1,34 @@ +#include + +int fibonacci[5005][1005]; + +int main() +{ + fibonacci[0][0] = 0, fibonacci[1][0] = 1; + for(int i=2 ; i<5005 ; i++) + { + for(int j=0 ; j<1005 ; j++) + { + fibonacci[i][j] += fibonacci[i-1][j]+fibonacci[i-2][j]; + + if( fibonacci[i][j]>9 ) + { + fibonacci[i][j+1] += fibonacci[i][j]/10; + fibonacci[i][j] %= 10; + } + } + } + + int n; + + while( scanf("%d", &n)==1 ) + { + int pos; + for(pos=1005-1 ; pos>0 ; pos--) + if( fibonacci[n][pos]!=0 ) break; + + for( ; pos>=0 ; pos--) printf("%d", fibonacci[n][pos]); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10579/10579-21.cpp b/uva_cpp_clean/10579/10579-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..479aacff07b3b03cf170cb71ce798c6a2c423dd3 --- /dev/null +++ b/uva_cpp_clean/10579/10579-21.cpp @@ -0,0 +1,93 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10579 + Name: Fibonacci Numbers + Problem: https://onlinejudge.org/external/105/10579.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +class largeint{ +public: + short int *nums; + int n_size; + #define nlen nums[0] + largeint(int n){ + int i; + nums=(short int *)malloc((n+1)*sizeof(short int)); + memset(nums,0,(n+1)*sizeof(short int)); + n_size=n; + nums[0]=0; + } + ~largeint(){ + free(nums); + } + + void print(){ + if(nlen<0)cout<<'-'; + for(int i=abs(nlen);i>0;i--)cout<0;i--); + nlen=i*s; + } + + largeint& operator=(const largeint &b){ + int i; + for(i=0;i<=abs(b.nlen);i++)nums[i]=b.nums[i]; + return *this; + } + largeint& operator=(const char* b){ + nlen=strlen(b); + for(int i=0;b[i];i++)nums[nlen-i]=b[i]-48; + return *this; + } + + largeint& operator+=(const largeint &b){ + if(n_size!=b.n_size)throw; + int i; + for(i=1;i<=abs(b.nlen);i++){ + nums[i]+=b.nums[i]; + nums[i+1]+=nums[i]/10; + nums[i]%=10; + } + for(;nums[i];i++){ + nums[i+1]+=nums[i]/10; + nums[i]%=10; + } + fixlen(1); + return *this; + } +}; + + +int main(){ + char t[5]; + int i,n; + while(scanf("%d",&n)==1) + { largeint t1(1500),t2(1500),t3(1500); + t1="0"; + t2="1"; + for(i=0;i + +#define LIMIT 5001 +#define MAX 1050 + +using namespace std; + +int n, l1, l2, minl, maxl, aux, carry, i, j, k; +char fibo[LIMIT][MAX], tmp[MAX], final_tmp[MAX]; + +char* sum(char *s1, char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + minl = l1 > l2 ? l2 : l1; + maxl = l1 > l2 ? l1 : l2; + carry = 0; + for(k = 0, i = l1 - 1, j = l2 - 1; k < minl; k++, i--, j--){ + aux = s1[i] - '0' + s2[j] - '0' + carry; + tmp[k] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l1){ + aux = s1[i] - '0' + carry; + tmp[k] = aux % 10+ '0'; + carry = aux / 10; + k++, i--; + } + if(carry) + tmp[maxl++] = carry + '0', tmp[maxl] = '\0'; + else + tmp[maxl] = '\0'; + i = 0; + while(final_tmp[i++] = tmp[--maxl]) ; + final_tmp[i] = '\0'; + return final_tmp; +} + +void generateFibo(){ + fibo[0][0] = '0', fibo[0][1] = '\0'; + fibo[1][0] = '1', fibo[0][1] = '\0'; + for(int i = 2; i <= LIMIT; i++) + strcpy(fibo[i],sum(fibo[i - 1], fibo[i - 2])); +} + +int main(){ + generateFibo(); + while(scanf("%d",&n) == 1) + printf("%s\n", fibo[n]); + return(0); +} diff --git a/uva_cpp_clean/10582/10582-21.cpp b/uva_cpp_clean/10582/10582-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da0726b8ab91ed1f73b8b4678115b8adce518b7d --- /dev/null +++ b/uva_cpp_clean/10582/10582-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10582 + Name: ASCII Labyrinth + Problem: https://onlinejudge.org/external/105/10582.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +#define TOP 0 +#define RIGHT 1 +#define BOTTOM 2 +#define LEFT 3 + +int di[] = { -1, 0, 1, 0 }, + dj[] = { 0, 1, 0, -1 }; + +int M[77][77], r, c; +int dfs(int i, int j, int dir) { + if (i<0 || i>=r || j<0 || j>=c || !M[i][j]) return 0; + if (i==r-1 && j==c-1) return 1; + + int o = M[i][j]; + M[i][j] = 0; + + int r = 0; + if (o == 1) + r += dfs(i-di[dir], j-dj[dir], dir); + + else if (dir==TOP || dir==BOTTOM) { + r += dfs(i+di[LEFT], j+dj[LEFT], RIGHT); + r += dfs(i+di[RIGHT], j+dj[RIGHT], LEFT); + } + + else if (dir==LEFT || dir==RIGHT) { + r += dfs(i+di[TOP], j+dj[TOP], BOTTOM); + r += dfs(i+di[BOTTOM], j+dj[BOTTOM], TOP); + } + + M[i][j] = o; + return r; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + char S[3][512]; + while (T-- && cin >> r >> c) { + cin.ignore(512, '\n'); + for (int i=0; i +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)5e4+5; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; + +int minimumSpanningTree(vector> &edges) { + + + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + int ans = 0,a,b,weight; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + } else { + ans +=weight; + } + } + return ans; +} + + +int main() { + FAST + + int n,m,a,b; + int c=1; + + while (cin>>n>>m) { + if(n==0 && m==0) break; + + DSU dsu; + + dsu.init(n+2); + + for(int i=0;i>a>>b; + + dsu.merge(a,b); + } + + set ans; + + for(int i=1;i<=n;i++) { + ans.insert(dsu.find(i)); + } + + cout<<"Case "< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 50005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int par[MAX]; +//~ int size[MAX]; +void init (int n) { + for (int i = 1; i <= n; i++) { + par[i] = i; + //~ size[i] = 1; + } +} +int find (int r) { + if (r == par[r]) return r; + else return par[r] = find (par[r]); +} +void makeSet (int a, int b) { + a = find (a); + b = find (b); + + //~ debug; + if (a != b) { + //~ if (size[a] < size[b]) { + //~ swap (a, b); + //~ } + + par[b] = a; + //~ size[a] += size[b]; + } +} +int main () { + __FastIO; + int n, m; + int t = 1; + + while (cin >> n >> m) { + if (n == 0 && m == 0) break; + + init (n); + + for (int i = 0; i < m; i++) { + int a, b; + cin >> a >> b; + makeSet (a, b); + } + + int chk[MAX], cnt = 0; + SET (chk, 0); + + for (int i = 1; i <= n; i++) { + int p = find (i); + + if (chk[p] == 0) { + cnt++; + chk[p] = 1; + } + } + + cout << "Case " << t++ << ": " << cnt << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/10583/10583-19.cpp b/uva_cpp_clean/10583/10583-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d8e3b38bcf10d400f4c3fc153fae47ef8a46f19 --- /dev/null +++ b/uva_cpp_clean/10583/10583-19.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vi; +typedef vector vii; + +struct ufds { + vii p; vi r; int numSets, BSS = 1; + ufds(int N) { + p.assign(N, ii(0, 1)); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i].first = i; + } + int subSetSize(int i) { + int x = findSet(i); + return p[x].second; + } + int biggestSubSetSize() { return BSS; } + int findSet(int i) { return (p[i].first==i)? i: (p[i].first=findSet(p[i].first)); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y].first = x, p[x].second += p[y].second, BSS = max(BSS, p[x].second); + else { + p[x].first = y; + p[y].second += p[x].second; + BSS = max(BSS, p[y].second); + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +int main() { + int N, M, cases = 1; + while (cin >> N >> M && !(N == 0 && M == 0)) { + ufds religions(N); + while (M--) { + int i, j; + cin >> i >> j; + religions.unionSet(i-1, j-1); + } + printf("Case %d: %d\n", cases++, religions.numSets); + } + return 0; +} diff --git a/uva_cpp_clean/10583/10583-21.cpp b/uva_cpp_clean/10583/10583-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a6fc9b19229b89774f153c521535ff15f62b446 --- /dev/null +++ b/uva_cpp_clean/10583/10583-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10583 + Name: Ubiquitous Religions + Problem: https://onlinejudge.org/external/105/10583.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int ids[50005]; +int find(int i) { + if (ids[i]==i) return i; + return ids[i]=find(ids[i]); +} +void merge(int x, int y) { + ids[find(x)]=ids[find(y)]; +} + +int main(){ + int cse=1, n, m, x, y; + while(cin>>n>>m && (n||m)) { + for (int i=0; i>x>>y; + merge(x-1, y-1); + } + + int cnt=0; + for (int i=0; i +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + for(int t=1;;t++){ + int n,m,u,v; + cin>>n>>m; + if(!n)break; + vvi g(n); + for(int i=0;i>u>>v; + u--;v--; + g[u].push_back(v); + g[v].push_back(u); + } + vi c(n); + functiondfs=[&](int u,int k){ + c[u]=k; + for(int v:g[u]) + if(!c[v]) + dfs(v,k); + }; + int k=1; + for(int i=0;i + +using namespace std; + +struct UnionFind{ + int numSets; + vector path, rank, setSize; + + UnionFind(int N_): numSets(N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + setSize.assign(N_, 1); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + numSets--; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x, setSize[x] += setSize[y]; + else path[x] = y, setSize[y] += setSize[x]; + if( rank[x] == rank[y] ) rank[y]++; + } + + int sizeOfSet(int idx){ + return setSize[ findSet(idx) ]; + } +}; + +int tc, n , m , a , b ; + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + UnionFind UF(n); + while(m--){ + scanf("%d %d", &a, &b); + UF.unionSet(a - 1, b - 1); + } + printf("Case %d: %d\n", ++tc, UF.numSets); + } + return(0); +} diff --git a/uva_cpp_clean/10585/10585-21.cpp b/uva_cpp_clean/10585/10585-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b52f4e72aa40663a5aefcc5bb46021329eb2fd61 --- /dev/null +++ b/uva_cpp_clean/10585/10585-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10585 + Name: Center of symmetry + Problem: https://onlinejudge.org/external/105/10585.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct Point { + int x, y; + bool operator < (const Point &o) const { + return x==o.x ? y> T; + while (T-- && cin >> n) { + long long sx = 0, sy = 0; + for (int i=0; i> P[i].x >> P[i].y; + sx += P[i].x; + sy += P[i].y; + } + + bool result = 0; + if ((2*sx)%n==0 && (2*sy)%n==0) { + long long tx = 2*sx/n, ty = 2*sy/n; + sort(P, P+n); + result = 1; + for (int i=0; result && i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (10010) +#define mod 1000000007 +////=====================================//// + +struct point +{ + int x,y; +}; + +bool cmp(point a,point b) +{ + if(a.x!=b.x) + return a.x + +using namespace std; + +struct Polynomial{ + int degree; + vector < long long > coef; + Polynomial() {}; + Polynomial(int degree_): degree(degree_){ + coef.assign(degree + 1, 0); + }; +}p; + +int n, k; + +void print(Polynomial& x){ + if( x.degree == -1 ) puts("0"); + for(int it = 0; it <= x.degree; it++) + printf( it == x.degree ? "%lld\n" : "%lld ", x.coef[it]); +} + +int main(){ + while(scanf("%d %d", &n, &k), ~n and ~k){ + p = Polynomial(n); + for(int it = 0; it <= n; it++) scanf("%lld", &p.coef[it]); + while( p.degree >= k ) p.coef[p.degree - k] -= p.coef[p.degree], p.degree--; + print(p); + } + return(0); +} diff --git a/uva_cpp_clean/10589/10589-19.cpp b/uva_cpp_clean/10589/10589-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9710eb715259802d9eddee37b717a449931d055 --- /dev/null +++ b/uva_cpp_clean/10589/10589-19.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main() { + int N; + double a; + while (cin >> N >> a && N != 0) { + int M = 0; + for (int i = 0; i < N; i++) { + double x, y; + cin >> x >> y; + if (x*x + y*y <= a*a && (x-a)*(x-a) + y*y <= a*a && + x*x + (y-a)*(y-a) <= a*a && (x-a)*(x-a) + (y-a)*(y-a) <= a*a) M++; + } + printf("%.5lf\n", (M*a*a)/N); + } + return 0; +} diff --git a/uva_cpp_clean/10589/10589-20.cpp b/uva_cpp_clean/10589/10589-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1ce2f405b1598990235b1c4b0c13cde63fed4eb --- /dev/null +++ b/uva_cpp_clean/10589/10589-20.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int N, a; + + while( cin >> N >> a && N+a!=0 ) + { + int M = 0; + for(int i=1 ; i<=N ; i++) + { + double x, y; + cin >> x >> y; + + bool flag = true; + if( (x-0)*(x-0)+(y-0)*(y-0)>a*a ) flag = false; + if( (x-a)*(x-a)+(y-a)*(y-a)>a*a ) flag = false; + if( (x-0)*(x-0)+(y-a)*(y-a)>a*a ) flag = false; + if( (x-a)*(x-a)+(y-0)*(y-0)>a*a ) flag = false; + + if( flag ) M++; + } + + cout << fixed << setprecision(5) << (double) M*(a*a)/N << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10589/10589-21.cpp b/uva_cpp_clean/10589/10589-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04c6ac9127ec9e6219424a66ea3d71430dc5adee --- /dev/null +++ b/uva_cpp_clean/10589/10589-21.cpp @@ -0,0 +1,97 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10589 + Name: Area + Problem: https://onlinejudge.org/external/105/10589.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) return EOF; + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} +inline bool readDouble(double &r) { + char ch; + int sgn=1; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) + return 0; + + r = 0; + if (ch!='.') { + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = r*10 + ch-'0'; + } + + if (ch=='.') { + double p = 1; + while (isdigit(ch=readchar())) + r += (ch-'0') * (p *= 0.1); + } + + if (ch=='e' || ch=='E') { + double p10 = readInt(); + r *= pow(10, p10); + } + + return 1; +} + +// --------------------------------------------------------------- + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(5); + + int n, a; + while ((n = readInt())>0) { + int a = readInt(); + double x, y; + int a2 = a*a, m = 0; + for (int i=0; i + +using namespace std; + +const double EPS = 1e-12; + +int N, M, a; +double X, Y; + +bool inCircle(const double&px, const double& py, const int& cx, const int &cy){ + double dx = px - cx, dy = py - cy, d = dx * dx + dy * dy; + if( d - EPS < a * a ) return true; + if( fabs(a * a - d) < EPS ) return true; + return false; +} + +bool isInside(const double& x, const double& y){ + return ( inCircle(x, y, 0, 0) and inCircle(x, y, a, 0) and + inCircle(x, y, a, a) and inCircle(x, y, 0, a) ); +} + +int main(){ + while(scanf("%d %d", &N, &a), N ){ + M = 0; + for(int it = 0; it < N; it++){ + scanf("%lf %lf", &X, &Y); + if( isInside(X, Y) ) M++; + } + printf("%.5lf\n", 1.0 * M * a * a / N); + } + return(0); +} diff --git a/uva_cpp_clean/10591/10591-19.cpp b/uva_cpp_clean/10591/10591-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..284b923197c45ec9a2b05c4bdf8e26614d2dee02 --- /dev/null +++ b/uva_cpp_clean/10591/10591-19.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; + +int main() { + int cases; + cin >> cases; + for (int i = 1; i <= cases; i++) { + unordered_set s; + long long N; + cin >> N; + printf("Case #%d: %lld", i, N); + while (N != 1 && !s.count(N)) { + s.insert(N); + long long auxi = 0; + while (N) { + auxi += (N%10)*(N%10); + N/=10; + } + N = auxi; + } + if (N == 1) printf(" is a Happy number.\n"); + else printf(" is an Unhappy number.\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10591/10591-21.cpp b/uva_cpp_clean/10591/10591-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..657425fb263467c9610f261892c57dd72d7d0862 --- /dev/null +++ b/uva_cpp_clean/10591/10591-21.cpp @@ -0,0 +1,54 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10591 + Name: Happy Number + Problem: https://onlinejudge.org/external/105/10591.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define sqr(x) ((x)*(x)) + +#define MAX 100000000 +char memo[MAX]; +bool isHappy(int x) { + if (x == 1) return true; + if (x < MAX) { + if (memo[x]) + return memo[x] == 1; + memo[x] = -1; + } + + int n = 0; + for (int k = x; k; k /= 10) { + int m = k % 10; + n += m*m; + } + + bool r = isHappy(n); + if (r && x < MAX) memo[x] = 1; + return r; +} + + +int main() { + int n; + cin >> n; + for (int cse = 1; cse <= n; cse++) { + int x; + cin >> x; + if (isHappy(x)) + printf("Case #%d: %d is a Happy number.\n", cse, x); + else + printf("Case #%d: %d is an Unhappy number.\n", cse, x); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10591/10591-5.cpp b/uva_cpp_clean/10591/10591-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6d6182c1deb059ee935d8b58af13f26eb0c43a10 --- /dev/null +++ b/uva_cpp_clean/10591/10591-5.cpp @@ -0,0 +1,87 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; + +ll happy(ll n) +{ + ll s=0; + //n=n*n; + if(n<=9) + return n; + while(n!=0) + { + + s+=((n%10)*(n%10)); + n/=10; + } + happy(s); +} + + + +int main() +{ + int t,no=0; + sc("%d",&t); + while(t--) + { + ll n; + sc("%lld",&n); + if(n==7) + { + pf("Case #%d: %lld is a Happy number.\n",++no,n); + } + else if(happy(n)==1) + { + pf("Case #%d: %lld is a Happy number.\n",++no,n); + } + else + { + pf("Case #%d: %lld is an Unhappy number.\n",++no,n); + } + + } + return 0; +} diff --git a/uva_cpp_clean/10591/10591-9.cpp b/uva_cpp_clean/10591/10591-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c258c317639bced78a6e965f393341ded9f4e66 --- /dev/null +++ b/uva_cpp_clean/10591/10591-9.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int tc, n; + +int sumDigits(int N){ + int sum = 0; + while(N) sum += ( N % 10 ) * ( N % 10 ), N /= 10; + return sum; +} + +bool Happy_Number(int N){ + set vis; + vis.insert(N); + while( N != 1 ){ + N = sumDigits(N); + if( vis.find(N) != vis.end() ) return false; + vis.insert(N); + } + return true; +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d", &n); + if( Happy_Number(n) ) printf("Case #%d: %d is a Happy number.\n", t, n); + else printf("Case #%d: %d is an Unhappy number.\n", t, n); + } + return(0); +} diff --git a/uva_cpp_clean/10594/10594-6.cpp b/uva_cpp_clean/10594/10594-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dddea54ab95612b750b9160609de001b50b3e2f0 --- /dev/null +++ b/uva_cpp_clean/10594/10594-6.cpp @@ -0,0 +1,124 @@ +/* + graphs > maximum flow > minimum cost + difficulty: easy + date: 06/Aug/2020 + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +/* input */ +vector>> adj_list; // adj_list[u] = {{v, weight, capacity}, ...} +int V; + +/* */ +vector>> new_adj_list; // new_adj_list[u] = {{v, weight, remaining_capacity, flow}, ...} +vector> parent; // parent[v] = {u, índice de new_adj_list[u]} +vector> rev_idx; + +/* O(V*E) - find the shortest augmenting path (in terms of weight) and returns your bottleneck, or 0, if there is no more an augmenting path */ +ll belmman_ford(int s, int t) { + parent.assign(V, {-1, -1}); + + vector sd(V, INT_MAX); // sd[u] = shortest distance de s à u + sd[s] = 0; + bool was_relaxed = true; + + // se não há ciclos negativos no grafo, deveria levar no máximo V-1 iterações para sd ser ótimo + // se for possível relaxar ainda mais, então há ciclos negativos + for (int _ = 0; was_relaxed && _ < V; _++) { + was_relaxed = false; + + // para cada aresta u --(w,c)--> v + for (int u = 0; u < V; u++) + for (int i = 0; i < new_adj_list[u].size(); i++) { + int v; ll w,c,__; tie(v,w,c,__) = new_adj_list[u][i]; + + if (sd[u] < INT_MAX && c > 0) { + // tenta relaxar sd[v] + if (sd[u] + w < sd[v]) { + sd[v] = sd[u] + w; + parent[v] = {u,i}; + was_relaxed = true; + } + } + } + } + + // se tem ciclo negativo ou t não é alcançável a partir de s + if (was_relaxed || sd[t] == INT_MAX) return 0; + + // procura pela menor capacidade restante (gargalo), iterando cada aresta u -> v do caminho aumentativo encontrado + ll min_c = INT_MAX; + for (int v = t, u; parent[v].first != -1; v = u) { + int i; tie(u,i) = parent[v]; + min_c = min(min_c, get<2>(new_adj_list[u][i])); + // printf("%2d %2d\n", u, v); + } + return min_c; +} + +/* O((V^2) * (E^2)) - returns {max flow, min cost}, where the max flow is bounded by K */ +pair min_cost_max_flow(int s, int t, ll K) { + new_adj_list.assign(V, vector>()); + rev_idx.assign(V, vector()); + + // para cada aresta u -(w,c)-> v do grafo original + for (int u = 0; u < V; u++) + for (auto adj : adj_list[u]) { + int v; ll w,c; tie(v,w,c) = adj; + + rev_idx[u].push_back(new_adj_list[v].size()); + rev_idx[v].push_back(new_adj_list[u].size()); + + new_adj_list[u].push_back({v, w, c, 0}); // forward edge + new_adj_list[v].push_back({u, -w, 0, 0}); // backward edge + } + + ll acc_flow = 0, f; + ll acc_cost = 0; + + // enquanto existir augmenting path + while (K > 0 && (f = belmman_ford(s, t))) { + f = min(f, K); + acc_flow += f; + K -= f; + + // atualiza os grafos com o bottleneck, para cada aresta u -> v do caminho aumentativo encontrado + for (int v = t, u; parent[v].first != -1; v = u) { + u = parent[v].first; + int i = parent[v].second; + int j = rev_idx[u][i]; + + get<2>(new_adj_list[u][i]) -= f; // remaining capacity of u -> v + get<2>(new_adj_list[v][j]) += f; // remaining capacity of v -> u + + get<3>(new_adj_list[u][i]) += f; // flow of u -> v + get<3>(new_adj_list[v][j]) -= f; // flow of v -> u + + acc_cost += get<1>(new_adj_list[u][i]) * f; + } + } + return {acc_flow, acc_cost}; +} + +int main() { + int E; + while (cin >> V >> E) { + adj_list.assign(V, vector>()); + while (E--) { + int u, v; cin >> u >> v; u--; v--; + ll w; cin >> w; + adj_list[u].push_back({v, w, 0}); + adj_list[v].push_back({u, w, 0}); + } + ll d, k; cin >> d >> k; + for (int u = 0; u < V; u++) for (auto &adj : adj_list[u]) get<2>(adj) += k; + + ll max_flow, min_cost; tie(max_flow, min_cost) = min_cost_max_flow(0, V-1, d); + if (max_flow != d) cout << "Impossible.\n"; + else cout << min_cost << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10596/10596-19.cpp b/uva_cpp_clean/10596/10596-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d48b5dee415b3fb4c435b5334b8562cb9a15d20 --- /dev/null +++ b/uva_cpp_clean/10596/10596-19.cpp @@ -0,0 +1,52 @@ +#include +#include +using namespace std; + +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +vector AdjList; + +int main() { + int N, R; + while (cin >> N >> R) { + AdjList.assign(N, vi()); + ufds ds(N); + for (int i = 0; i < R; i++) { + int s, d; + cin >> s >> d; + AdjList[s].push_back(d); + AdjList[d].push_back(s); + ds.unionSet(s, d); + } + bool eulerian = R > 0; + int f = -1; + for (int i = 0; i < N && eulerian; i++) { + if (AdjList[i].size()%2 != 0) eulerian = false; + if (f == -1 && AdjList[i].size() > 0) f = i; + else if (AdjList[i].size() > 0) eulerian = eulerian && ds.isSameSet(f, i); + } + if (eulerian) printf("Possible\n"); + else printf("Not Possible\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10596/10596-21.cpp b/uva_cpp_clean/10596/10596-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42954fbc11f5c041c76c1ff53d8c3fbfd9714a88 --- /dev/null +++ b/uva_cpp_clean/10596/10596-21.cpp @@ -0,0 +1,102 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10596 + Name: Morning Walk + Problem: https://onlinejudge.org/external/105/10596.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// --------------------------------------------------------------- + +int E[243][243], S[243], n, m; +vector adj[243]; + +int dfs(int u) { + int sum = 0; + for (int v: adj[u]) + if (E[u][v]) { + --E[u][v]; + --E[v][u]; + sum += dfs(v) + 1; + } + + return sum; +} + + +bool check() { + int st = -1; + + for (int i=0; i=0 && dfs(st) == m; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + + while ((n=readUInt())!=EOF) { + m = readUInt(); + for (int i=0; i +#include + +bool p[1000002]; + +unsigned GCD(unsigned a, unsigned b) +{ + while( b ) b ^= a ^= b ^= a %= b; + return (a); +} + +int main() +{ + unsigned N; + while( scanf("%u", &N)!=EOF ) + { + for(unsigned i=0 ; i<1000002 ; i++) p[i] = false; + + unsigned triple = 0, sqrtN = sqrt(N); + for(unsigned x=1 ; x<=sqrtN ; x++) + for(unsigned y=x+1 ; y<=sqrtN ; y+=2) + { + if( GCD(x, y)!=1 ) continue; + + unsigned c = (y*y) + (x*x); + if( c>N ) break; + unsigned a = (y*y) - (x*x); + unsigned b = 2*x*y; + triple++; + + unsigned newA = a, newB = b, newC = c; + while( newC<=N ) + { + p[newA] = p[newB] = p[newC] = true; + newA += a; + newB += b; + newC += c; + } + } + + unsigned notTriple = 0; + for(unsigned i=1 ; i<=N ; i++) + if( !p[i] ) notTriple++; + + printf("%u %u\n", triple, notTriple); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/106/106-21.cpp b/uva_cpp_clean/106/106-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8e9bec8868a1bf6bc915e59d24fce7b25406d65 --- /dev/null +++ b/uva_cpp_clean/106/106-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 106 + Name: Fermat vs. Pythagoras + Problem: https://onlinejudge.org/external/1/106.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int gcd(int a, int b) { + if (b == 0) return a; + return gcd(b, a % b); +} + +#define MAXN 1000001 +int rp[MAXN], us[MAXN], uss[MAXN]; +void setUsm(int i, int v) { + if (us[i]==0 || v < us[i]) + us[i] = v; +} + +int main(){ + for (int m=2; m<=1000; m++) { + bool me = m&1; + int m2 = m*m; + for (int n=1; n>n) + printf("%d %d\n", rp[n], n-uss[n]); +} diff --git a/uva_cpp_clean/106/106-9.cpp b/uva_cpp_clean/106/106-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0805c511f424293648ec0a0acd542479cba8dc6 --- /dev/null +++ b/uva_cpp_clean/106/106-9.cpp @@ -0,0 +1,33 @@ +#include + +#define SIZE 1000010 + +using namespace std; + +int N, ans1, ans2, x, y, z; +bool v[SIZE]; + +int gcd(int a, int b){ + return b == 0 ? a : gcd(b, a % b); +} + +int main(){ + while(scanf("%d", &N)==1){ + ans1 = ans2 = 0; + memset(v, false, sizeof v); + for(int m = 1; m * m < N; m++) + for(int n = 1; n < m and n * n + m * m <= N; n++) + if(gcd(m, n) == 1){ + x = 2 * m * n; + y = m * m - n * n; + z = m * m + n * n; + if( gcd(x, z) == 1 ) ans1++; + for(int d = 1; d * z <= N; d++){ + v[d * z] = v[d * y] = v[d * x] = true; + } + } + for(int it = 1; it <= N; it++) if(v[it] == false) ans2++; + printf("%d %d\n", ans1, ans2); + } + return(0); +} diff --git a/uva_cpp_clean/10600/10600-13.cpp b/uva_cpp_clean/10600/10600-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39fff17be194ea6f238f3d2991a6d1a492afa669 --- /dev/null +++ b/uva_cpp_clean/10600/10600-13.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int test, n, m, best, secondBest; +vector choosen, finalChoosen; +vector parent, sz; + +ofstream fout("f.out"); + +class comparator +{ +public: + + bool operator()(pair , int> &x, pair , int> &y) + { + return x.second > y.second; + } +}; + +priority_queue , int>, vector, int>>, comparator> pq, temp; + +int findParent(int v) +{ + if (parent[v] == v) return parent[v]; + + else return parent[v] = findParent(parent[v]); +} + +bool merge(int x, int y) +{ + int parentX = findParent(x); + int parentY = findParent(y); + + if (parentX == parentY) return true; + + else + { + x = parentX; + y = parentY; + + if (sz[x] < sz[y]) swap(x, y); + + parent[y] = x; + + sz[x] += sz[y]; + + return false; + } +} + +int mst() +{ + int ans = 0, num = 0; + + while (!pq.empty()) + { + pair, int> head = pq.top(); + + int x = head.first.first; + int y = head.first.second; + + int w = head.second; + + if (!choosen[num]) + { + if (!merge(x, y)) + { + ans += w; + + choosen[num] = true; + } + } + + num++; + + pq.pop(); + } + + return ans; +} + +void initialize() +{ + choosen.clear(); + choosen.resize(m, false); + + parent.clear(); + parent.resize(n + 1); + + sz.clear(); + sz.resize(n + 1); + + for (int i = 0; i <= n; i++) + { + parent[i] = i; + + sz[i] = 1; + } +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n >> m; + + initialize(); + + while (!pq.empty()) pq.pop(); + + while (!temp.empty()) temp.pop(); + + for (int i = 0; i < m; i++) + { + int x, y, w; + + cin >> x >> y >> w; + + pq.push(make_pair(make_pair(x, y), w)); + + temp.push(make_pair(make_pair(x, y), w)); + } + + best = mst(); + + secondBest = INT_MAX; + + finalChoosen = choosen; + + for (int i = 0; i < m; i++) + { + if (finalChoosen[i]) + { + initialize(); + + choosen[i] = true; + + pq = temp; + + int g = mst(); + + if (g < secondBest && g >= best) secondBest = g; + } + } + + cout << best << " " << secondBest << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10600/10600-14.cpp b/uva_cpp_clean/10600/10600-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dfdb688614e53aac2a20deae69cf3394d2d1630b --- /dev/null +++ b/uva_cpp_clean/10600/10600-14.cpp @@ -0,0 +1,161 @@ +/*************************************************** + * Problem Name : 10600 ACM contest and Blackout.cpp + * Problem Link : https://uva.onlinejudge.org/external/106/10600.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-09-14 + * Problem Type : Graph (MST) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 105 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +struct edge { + int u, v, w; +}; +vectorE; +vectorV; + +int par[MAX]; + +bool cmp ( edge a, edge b ) { + return a.w < b.w; +} + +void makeSet (int n) { + for (int i = 1; i <= n; i++) { + par[i] = i; + } +} + +int find (int r ) { + if ( par[r] == r ) return r; + + return par[r] = find ( par[r] ); +} + +int MST (int n) { + makeSet (n); + sort (all (E), cmp); + int sum = 0, cnt = 0; + + for (int i = 0; i < (int) E.size(); i++) { + int u = find (E[i].u ); + int v = find (E[i].v ); + + if ( u != v ) { + par[u] = v; + V.push_back (i); + sum += E[i].w; + cnt++; + + if (cnt == n - 1) break; + } + } + + return sum; +} + +int MST_1 (int x, int n) { + makeSet (n); + int sum = 0, cnt = 0; + + for (int i = 0; i < (int) E.size(); i++) { + if (i == x) continue; + + int u = find (E[i].u ); + int v = find (E[i].v ); + + if ( u != v ) { + par[u] = v; + sum += E[i].w; + cnt++; + + if (cnt == n - 1) break; + } + } + + return sum; +} +int secondMST (int n, int sum) { + int mn = 30005; + + for (int i = 0; i < (int) V.size(); i++) { + int ans = MST_1 (V[i], n); + + if (ans >= sum) mn = min (mn, ans); + } + + return mn; +} +int main () { + __FastIO; + int tc, N, M; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> N >> M; + + for (int i = 1; i <= M; i++) { + edge get ; + cin >> get.u >> get.v >> get.w; + E.pb (get); + } + + int sum1 = MST (N); + int sum2 = secondMST (N, sum1); + cout << sum1 << " " << sum2 << "\n"; + E.clear(), V.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/10600/10600-21.cpp b/uva_cpp_clean/10600/10600-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..633740cf5fcf2ad89106781e730c7e262aefd77c --- /dev/null +++ b/uva_cpp_clean/10600/10600-21.cpp @@ -0,0 +1,102 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10600 + Name: ACM Contest and Blackout + Problem: https://onlinejudge.org/external/106/10600.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int ids[1000]; +int find(int u) { + return ids[u]==u ? u : ids[u] = find(ids[u]); +} +void join(int u, int v) { + ids[find(u)] = find(v); +} + +struct Edge { + int u, v, w; + Edge(int u, int v, int w):u(u),v(v),w(w){} + bool operator < (const Edge &e) const { + return w > e.w; + } +}; + +bool seen[101]; +int dist[101][101]; +list adj[101]; +int maxLen(int u, int t) { + seen[u] = 1; + for (int v: adj[u]) + if (v == t) { + seen[u] = 0; + return dist[u][v]; + } + else if (!seen[v]) { + int r = maxLen(v, t); + if (r >= 0) { + seen[u] = 0; + return max(r, dist[u][v]); + } + } + seen[u] = 0; + return -1; +} + + +int main(){ + int T, n, m, u, v, w; + scanf("%d", &T); + while (T--) { + scanf("%d%d", &n, &m); + for (int i=0; i q; + for (int i=0; i q2; + for (int cnt=0; cnt= 0 && mlen <= e.w) + s2 = min(s2, s1 + e.w - mlen); + } + } + printf("%d %d\n", s1, s2); + } +} diff --git a/uva_cpp_clean/10600/10600-9.cpp b/uva_cpp_clean/10600/10600-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0203e1d94e9795eaa1d0dd6b869a607d9ccad1cb --- /dev/null +++ b/uva_cpp_clean/10600/10600-9.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 10010; + +struct Edge_{ + int u, v, w; + bool operator < (const Edge_& other) const{ + return ( w < other.w ); + } +}E[SIZE]; + +int tc, N, M, min_cost, min_cost1, min_cost2, setSize; + +void Kruskal(){ + UnionFind UF(N + 1); + min_cost1 = 0, min_cost2 = INT_MAX, setSize = 1; + sort(E, E + N); + vector used; + sort(E, E + M); + for(int it = 0; it < M and setSize != N; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ){ + min_cost1 += E[it].w, setSize++; + UF.unionSet(E[it].u, E[it].v), used.push_back(it); + } + for(int i = 0; i + 1 < N; i++){ + setSize = 1; + UnionFind UF(N + 1); + min_cost = 0; + for(int it = 0; it < M and setSize != N; it++) + if( it != used[i] and not UF.isSameSet(E[it].u, E[it].v) ) + min_cost += E[it].w, setSize++, UF.unionSet(E[it].u, E[it].v); + if( setSize == N ) + min_cost2 = min(min_cost2, min_cost); + } + if( min_cost2 == 0 or min_cost2 == INT_MAX ) min_cost2 = min_cost1; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &N, &M); + for(int it = 0; it < M; it++) + scanf("%d %d %d", &E[it].u, &E[it].v, &E[it].w); + Kruskal(); + printf("%d %d\n", min_cost1, min_cost2); + } + return(0); +} diff --git a/uva_cpp_clean/10602/10602-21.cpp b/uva_cpp_clean/10602/10602-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..381b88853e25cb739b536f5c6c625474ee8a39df --- /dev/null +++ b/uva_cpp_clean/10602/10602-21.cpp @@ -0,0 +1,58 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10602 + Name: Editor Nottoobad + Problem: https://onlinejudge.org/external/106/10602.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include + +using namespace std; + +int comp(const string &a,const string &b){ + int i; + for(i=0;i>word; + list[i]=word; + } + count=list[0].length(); + for(i=1;it){ + t=t2; + str=list[i]; + list[i]=list[j]; + list[j]=str; + } + } + count+=list[i].length()-t; + } + cout< +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iiii; + +int a, b, c, d; + +void proc(int & i1, int & i2, int l1, int l2, int & move) { + int auxi = l2 - i2; + if (i1 > auxi) i1 -= auxi, i2 = l2, move -= auxi; + else i2 += i1, move -= i1, i1 = 0; +} + +int main() { + int T; + cin >> T; + while (T--) { + cin >> a >> b >> c >> d; + priority_queue q; + unordered_set s; + q.push(iiii(ii(0, 0), ii(0, c))); + int dt = c <= d? c: 0, steps = 0; + while (!q.empty() && dt < d) { + iiii state = q.top(); q.pop(); + if (state.first.second <= d && state.first.second > dt) dt = state.first.second, steps = -state.first.first; + if (state.second.first <= d && state.second.first > dt) dt = state.second.first, steps = -state.first.first; + if (state.second.second <= d && state.second.second > dt) dt = state.second.second, steps = -state.first.first; + if (dt == d) break; + if (!s.count(state.first.second*1000000 + state.second.first*1000 + state.second.second)) { + s.insert(state.first.second*1000000 + state.second.first*1000 + state.second.second); + iiii co = iiii(state); + proc(co.first.second, co.second.first, a, b, co.first.first); + q.push(co); + co = iiii(state); + proc(co.second.first, co.first.second, b, a, co.first.first); + q.push(co); + co = iiii(state); + proc(co.first.second, co.second.second, a, c, co.first.first); + q.push(co); + co = iiii(state); + proc(co.second.second, co.first.second, c, a, co.first.first); + q.push(co); + co = iiii(state); + proc(co.second.first, co.second.second, b, c, co.first.first); + q.push(co); + co = iiii(state); + proc(co.second.second, co.second.first, c, b, co.first.first); + q.push(co); + } + } + printf("%d %d\n", steps, dt); + } + return 0; +} diff --git a/uva_cpp_clean/10603/10603-21.cpp b/uva_cpp_clean/10603/10603-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1573e75bb48c5cc3295e688545830b1dbaa8b78c --- /dev/null +++ b/uva_cpp_clean/10603/10603-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10603 + Name: Fill + Problem: https://onlinejudge.org/external/106/10603.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + + +struct State { + int X[3], d; + State(int a, int b, int c, int d):d(d){ + X[0] = a; X[1] = b; X[2] = c; + } + State(int Y[], int d):d(d){ + memcpy(X, Y, 3*sizeof(int)); + } + bool operator < (const State& o) const { return d > o.d; } +}; + + +int D[243][243][243], S[243][243][243]; +int getD(int A[]) { + return D[A[0]][A[1]][A[2]]; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin>>T; + for (int cse=1; cse<=T; ++cse) { + int C[3], m; + cin>>C[0]>>C[1]>>C[2]>>m; + if (m > C[2]) m = C[2]; + + int dp=0, dt=0; + priority_queue q; + q.push(State(0,0,C[2], D[0][0][C[2]] = 0)); + while (!q.empty()) { + State u = q.top(); q.pop(); + if (u.d != getD(u.X)) continue; + for (int i=0; i<3; ++i) + if (u.X[i] <= m && u.X[i]>dp) { + dp = u.X[i]; dt = u.d; + if (dp == m) goto fin; + } + + for (int i=0; i<3; ++i) if (u.X[i]>0) + for (int j=0; j<3; ++j) if (i!=j && u.X[j] +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vi; +typedef vector vii; + +struct ufds { + vii p; vi r; int numSets, BSS = 1; + ufds(int N) { + p.assign(N, ii(0, 1)); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i].first = i; + } + int subSetSize(int i) { + int x = findSet(i); + return p[x].second; + } + int biggestSubSetSize() { return BSS; } + int findSet(int i) { return (p[i].first==i)? i: (p[i].first=findSet(p[i].first)); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y].first = x, p[x].second += p[y].second, BSS = max(BSS, p[x].second); + else { + p[x].first = y; + p[y].second += p[x].second; + BSS = max(BSS, p[y].second); + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +int main() { + int cases; + cin >> cases; + while (cases--) { + int N, M; + cin >> N >> M; + ufds friends(N); + while (M--) { + int A, B; + cin >> A >> B; + friends.unionSet(A-1, B-1); + } + int r = friends.biggestSubSetSize(); + if (r == 1) r = 0; + printf("%d\n", r); + } + return 0; +} diff --git a/uva_cpp_clean/10608/10608-20.cpp b/uva_cpp_clean/10608/10608-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b6b8d7a58b518d1f9f80c350b7528bb45bba3bf --- /dev/null +++ b/uva_cpp_clean/10608/10608-20.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +const int MAXN = 30000; +vector edge[MAXN+2]; +bool visited[MAXN+2]; +int counter; + +void dfs( int node ) { + if( visited[node] ) return; + visited[node] = true; + ++counter; + for( int i = 0 ; i < edge[node].size() ; ++i ) { + int now = edge[node][i]; + dfs( now ); + } +} + +int main( int argc, char ** argv ) { + int T; + scanf( "%d", &T ); + for( int nCase = 1 ; nCase <= T ; ++nCase ) { + int N, M; + scanf( "%d %d", &N, &M ); + for( int i = 1 ; i <= M ; ++i ) { + int A, B; + scanf( "%d %d", &A, &B ); + + edge[A].push_back( B ); + edge[B].push_back( A ); + } + + memset( visited, false, sizeof( visited ) ); + + int result = 0; + for( int i = 1 ; i <= N ; ++i ) { + counter = 0; + if( !visited[i] ) dfs( i ); + result = max( result, counter ); + } + + printf( "%d\n", result ); + + for( int i = 0 ; i <= N ; ++i ) edge[i].clear(); + } + return 0; +} diff --git a/uva_cpp_clean/10608/10608-21.cpp b/uva_cpp_clean/10608/10608-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86a3de6191232b791e01101861dc19b2ab5e85ea --- /dev/null +++ b/uva_cpp_clean/10608/10608-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10608 + Name: Friends + Problem: https://onlinejudge.org/external/106/10608.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +vector mp[30001]; +int res[30001]; +int count(int v) { + if (res[v] != -1) return 0; + + res[v] = 1; + for (auto w: mp[v]) + res[v] += count(w); + + return res[v]; +} + + +int main(){ + int cse; + cin>>cse; + while(cse--) { + int n, m; + cin >> n >> m; + for (int i=0; i>a>>b; + mp[a-1].push_back(b-1); + mp[b-1].push_back(a-1); + } + + int mx=0; + for(int i=0; i mx) mx = r; + } + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define CP(a) cout<>s1>>s2; + m=max(m,Union(s1,s2)); + } + P(m); + } + return 0; +} + diff --git a/uva_cpp_clean/10608/10608-6.cc b/uva_cpp_clean/10608/10608-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..dd89f1c198b049562606d0e5ff99d33a796c72c8 --- /dev/null +++ b/uva_cpp_clean/10608/10608-6.cc @@ -0,0 +1,36 @@ +// https://uva.onlinejudge.org/external/106/10608.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + int t,n,m,u,v; + cin>>t; + while(t--){ + cin>>n>>m; + vvi g(n); + for(int i=0;i>u>>v; + u--;v--; + g[u].push_back(v); + g[v].push_back(u); + } + vi c(n); + functiondfs=[&](int u,int k){ + c[u]=k; + for(int v:g[u]) + if(!c[v]) + dfs(v,k); + }; + int k=1; + for(int i=0;i union-find disjoint sets (UFDS) + difficulty: easy + date: 20/Jul/2020 + by: @brpapa +*/ +#include +using namespace std; + +struct UFDS { + vector p; // p[n] = pai do elemento n + vector size; // size[n] = tamanho do conjunto identificado por n + int count; // quantidade de conjuntos + + UFDS(int N) { + p.resize(N); + size.assign(N, 1); + count = N; + + // inicialmente, o elemento identificador de cada conjunto é ele próprio + for (int n = 0; n < N; n++) p[n] = n; + } + + // retorna o elemento identificador do conjunto de n + int findSet(int n) { + if (p[n] == n) return n; + + // comprime o caminho, durante a busca + return p[n] = findSet(p[n]); + } + + // os conjuntos de n e de m são os mesmos? + bool isSameSet(int n, int m) { + return findSet(n) == findSet(m); + } + + // conecta os conjuntos de n e de m + void unionSet(int n, int m) { + int id_n = findSet(n); + int id_m = findSet(m); + + // union by size: anexa a menor árvore (em profundidade) à maior árvore + if (!isSameSet(id_n, id_m)) { + if (size[id_n] > size[id_m]) + swap(id_n, id_m); + + p[id_n] = id_m; + size[id_m] += size[id_n]; + + count--; + } + } + + int getSetSize(int n) { + return size[findSet(n)]; + } +}; + +int main() { + int T; cin >> T; + while (T--) { + int N, M; cin >> N >> M; + UFDS uf(N); + + int ans = 1; + while (M--) { + int a, b; cin >> a >> b; a--; b--; + uf.unionSet(a, b); + + ans = max(ans, uf.getSetSize(a)); + ans = max(ans, uf.getSetSize(b)); + } + + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10608/10608-9.cpp b/uva_cpp_clean/10608/10608-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bd1c25d9c412d91801daf636c16b6e7283afaaa --- /dev/null +++ b/uva_cpp_clean/10608/10608-9.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +struct UnionFind{ + int numSets, largestSet; + vector path, rank, setSize; + + UnionFind(int N_): numSets(N_), largestSet(1){ + rank.assign(N_, 0); + path.assign(N_, 0); + setSize.assign(N_, 1); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + numSets--; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x, setSize[x] += setSize[y]; + else path[x] = y, setSize[y] += setSize[x]; + if( rank[x] == rank[y] ) rank[y]++; + largestSet = max(largestSet, (int)max(setSize[x], setSize[y])); + } + + int sizeOfSet(int idx){ + return setSize[ findSet(idx) ]; + } +}; + +int tc, n, m, a, b; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &n, &m); + UnionFind UF(n); + while(m--){ + scanf("%d %d", &a, &b); + UF.unionSet(a - 1, b - 1); + } + printf("%d\n", UF.largestSet); + } + return(0); +} diff --git a/uva_cpp_clean/1061/1061-21.cpp b/uva_cpp_clean/1061/1061-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57c1918a445d7aad17a876676b97e74295a452e8 --- /dev/null +++ b/uva_cpp_clean/1061/1061-21.cpp @@ -0,0 +1,107 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1061 + Name: Consanguine Calculations + Problem: https://onlinejudge.org/external/10/1061.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char toStr[8][5] = { + "O-", + "A-", + "B-", + "AB-", + "O+", + "A+", + "B+", + "AB+", +}; + +int P[4][2] = { + { 0, 0 }, + { 0, 1 }, + { 0, 2 }, + { 1, 2 } +}; + +int toInt(string s) { + if (s == "?") return -1; + int r = 0; + for (int i=0; s[i]; ++i) + switch(s[i]) { + case 'A': r |= 1; break; + case 'B': r |= 2; break; + case '+': r |= 4; break; + } + return r; +} + +void printVec(const vector &vec) { + if (vec.empty()) + cout << "IMPOSSIBLE"; + + else if (vec.size() == 1) + cout << toStr[vec[0]]; + + else { + cout << '{' << toStr[vec[0]]; + for (int i=1; i>a>>b>>c && a!="E"; ++cse) { + cout << "Case " << cse << ": "; + + int x = toInt(a), + y = toInt(b), + z = toInt(c); + + if (z < 0) { + cout << a << ' ' << b << ' '; + vector vec; + for (int sg=(x|y)>>2; sg>=0; --sg) + for (int i=0; i<2; ++i) + for (int j=0; j<2; ++j) { + int u = (sg<<2) | P[x&3][i] | P[y&3][j]; + if (find(vec.begin(), vec.end(), u) == vec.end()) + vec.push_back(u); + } + + printVec(vec); + cout << '\n'; + } + + else { + vector vec; + int o = (x<0 ? y : x); + for (int c=0; c<8; ++c) + if (((c|o)>>2) || !(z>>2)) + for (int i=0; i<2; ++i) + for (int j=0; j<2; ++j) { + int u = P[o&3][i] | P[c&3][j]; + if (u == (z&3) && find(vec.begin(), vec.end(), c) == vec.end()) + vec.push_back(c); + } + + if (y < 0) cout << a << ' '; + printVec(vec); + cout << ' '; + if (x < 0) cout << b << ' '; + cout << c << '\n'; + } + } +} diff --git a/uva_cpp_clean/1061/1061-4.cpp b/uva_cpp_clean/1061/1061-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acbf5682c0f478b73240bb2d3a50905477fe3c24 --- /dev/null +++ b/uva_cpp_clean/1061/1061-4.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + +std::vector calculateChild(std::string p1, std::string p2) +{ + std::vector results; + + if ((p1[0] == 'A' && p1[1] == 'B') || (p2[0] == 'A' && p2[1] == 'B')) + { + if (p1[0] == 'O' || p2[0] == 'O') + { + results.push_back("A"); + results.push_back("B"); + } + else + { + results.push_back("A"); + results.push_back("AB"); + results.push_back("B"); + } + } + else if ((p1[0] == 'A' && p2[0] == 'B') || (p1[0] == 'B' && p2[0] == 'A')) + { + results.push_back("A"); + results.push_back("AB"); + results.push_back("B"); + results.push_back("O"); + } + else if (p1[0] == 'A' || p2[0] == 'A') + { + results.push_back("A"); + results.push_back("O"); + } + else if (p1[0] == 'B' || p2[0] == 'B') + { + results.push_back("B"); + results.push_back("O"); + } + else if (p1[0] == 'O' && p2[0] == 'O') + { + results.push_back("O"); + } + + if (p1[p1.length() - 1] == '-' && p2[p2.length() - 1] == '-') + { + for (auto &x : results) + { + x += '-'; + } + } + else + { + std::vector temp; + + for (auto &x : results) + { + temp.push_back(x + "+"); + temp.push_back(x + "-"); + } + + results = temp; + } + + return results; +} + +std::vector calculateParent(std::string p1, std::string child) +{ + std::vector results; + std::string tab[] = {"A+", "A-", "AB+", "AB-", "B+", "B-", "O+", "O-"}; + + for (auto &x : tab) + { + auto temp = calculateChild(p1, x); + + if (std::find(temp.begin(), temp.end(), child) != temp.end()) + { + results.push_back(x); + } + } + + return results; +} + +int main() +{ + int caseNr = 0; + + while (true) + { + std::string p1, p2, child; + std::cin >> p1 >> p2 >> child; + + if (p1 == "E" && p2 == "N" && child == "D") + { + break; + } + + if (p1 != "?" && p2 != "?") + { + auto results = calculateChild(p1, p2); + + std::cout << "Case " << ++caseNr << ": " << p1 << " " << p2 << " "; + + if (results.size() == 1) + { + std::cout << results[0]; + } + else + { + std::cout << "{"; + + for (unsigned int i = 0; i < results.size(); i++) + { + std::cout << results[i]; + + if (i + 1 < results.size()) + { + std::cout << ", "; + } + } + + std::cout << "}"; + } + + std::cout << std::endl; + } + else + { + if (p1 == "?") + { + std::swap(p1, p2); + } + + auto results = calculateParent(p1, child); + + std::cout << "Case " << ++caseNr << ": " << p1 << " "; + + if (results.size() == 0) + { + std::cout << "IMPOSSIBLE"; + } + else if (results.size() == 1) + { + std::cout << results[0]; + } + else + { + std::cout << "{"; + + for (unsigned int i = 0; i < results.size(); i++) + { + std::cout << results[i]; + + if (i + 1 < results.size()) + { + std::cout << ", "; + } + } + + std::cout << "}"; + } + + std::cout << " " << child << std::endl; + } + } +} diff --git a/uva_cpp_clean/10610/10610-21.cpp b/uva_cpp_clean/10610/10610-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36b6085f2ef25dca8aa9f99fcc452b8cf9c03906 --- /dev/null +++ b/uva_cpp_clean/10610/10610-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10610 + Name: Gopher and Hawks + Problem: https://onlinejudge.org/external/106/10610.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +double X[1017], Y[1017]; +vector adj[1017]; +int dist[1017]; + +double dist2(int i, int j) { + double xx = X[i]-X[j], + yy = Y[i]-Y[j]; + return xx*xx + yy*yy; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, v, m; + while (cin >> v >> m && (v||m)) { + int threshold = v*m*60; + threshold*=threshold; + + for (n=0; cin.ignore(1000, '\n'); ++n) { + if (cin.peek() == '\n') break; + cin >> X[n] >> Y[n]; + adj[n].clear(); + dist[n] = -1; + for (int i=0; i q; + q.push(0); + dist[0]=0; + while (!q.empty()) { + int u = q.front(); q.pop(); + for (int v: adj[u]) + if (dist[v] < 0) { + if (v == 1) { + cout << "Yes, visiting " << dist[u] << " other holes.\n"; + goto fin; + } + + dist[v] = dist[u] + 1; + q.push(v); + } + } + cout << "No.\n"; + fin:; + } +} diff --git a/uva_cpp_clean/10610/10610-9.cpp b/uva_cpp_clean/10610/10610-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5a55636873c1054c8a3aa5cd394805ef9855a40 --- /dev/null +++ b/uva_cpp_clean/10610/10610-9.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int _speed, _time, ct, d[1010], ans, a1, a2, b1, b2; +pair pos[1010]; +char s[20]; +vector G[1010]; + +bool relation(pair p1, pair p2){ + float d = sqrt( (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second ) * (p1.second - p2.second) ); + return (d / _speed <= _time * 60); +} + +int bfs(int source, int target){ + if(relation(pos[0], pos[1])) return 0; + for(int i = 0; i < ct; i++) d[i] = -1; + queue Q; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + int u = Q.front(); Q.pop(); + if(u == target) return d[u]-1; + for(int i = 0; i < G[u].size(); i++){ + int v = G[u][i]; + if(d[v] == -1) + d[v] = d[u] + 1, Q.push(v); + } + } + return -1; +} + +int main(){ + while(scanf("%d %d", &_speed, &_time), _speed | _time){ + for(int i = 0; i < ct; i++) G[i].clear(); + ct = 2; + scanf("%f %f", &pos[0].first, &pos[0].second); + scanf("%f %f", &pos[1].first, &pos[1].second); + getchar(); + while(true){ + cin.getline(s, 20); + if(sscanf(s,"%d.%d %d.%d", &a1, &a2, &b1, &b2) != 4) break; + pos[ct++] = make_pair(a1 + (float)a2 / 1000, b1 + (float)b2 / 1000); + } + for(int i = 0; i < ct; i++) + for(int j = i + 1; j < ct; j++) + if(relation(pos[i], pos[j])) + G[i].push_back(j), G[j].push_back(i); + ans = bfs(0, 1); + if(ans == -1) puts("No."); + else printf("Yes, visiting %d other holes.\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10611/10611-14.cpp b/uva_cpp_clean/10611/10611-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..140b7fe748157edc00aa5d4902af3944456755f5 --- /dev/null +++ b/uva_cpp_clean/10611/10611-14.cpp @@ -0,0 +1,73 @@ +/*************************************************** + * Problem name : 10611 The Playboy Chimp.cpp + * Problem Link : https://uva.onlinejudge.org/external/106/10611.pdf + * OJ : Uva + * Verdict : AC(Time = 1230ms) + * Date : 13.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 50005 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ull n, Q, ar[MAX], x; + scanf("%llu", &n); + for (ull i = 0; i < n ; i++) scanf("%llu", &ar[i]); + scanf("%llu", &Q); + for (ull q = 1; q <= Q; q++) { + scanf("%llu", &x); + ull mn = 0; + ull mx = 100000000000; + for (ull i = 0; i < n; i++) { + if (ar[i] < x && mn <= ar[i]) mn = ar[i]; + if (ar[i] > x && mx >= ar[i]) mx = ar[i]; + } + if (mn == 0 && mx == 100000000000) printf("X X\n"); + else if (mn == 0 && mx != 100000000000) printf("X %llu\n", mx); + else if (mn != 0 && mx == 100000000000) printf("%llu X\n", mn); + else if (mn != 0 && mx != 100000000000) printf("%llu %llu\n", mn, mx); + } + return 0; +} diff --git a/uva_cpp_clean/10611/10611-19.cpp b/uva_cpp_clean/10611/10611-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60adeea16c4b133144656121ae661023b6b90ac7 --- /dev/null +++ b/uva_cpp_clean/10611/10611-19.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +using namespace std; + +int main() { + int N; + cin >> N; + vector v(N); + for (int i = 0; i < N; i++) cin >> v[i]; + int Q; + cin >> Q; + for (int i = 0; i < Q; i++) { + int val; + cin >> val; + auto low = lower_bound(v.begin(), v.end(), val); + auto up = upper_bound(v.begin(), v.end(), val); + int l = low - v.begin() - 1, h = up - v.begin(); + if (l >= 0 && l < v.size()) printf("%d ", v[l]); + else printf("X "); + if (h >= 0 && h < v.size()) printf("%d\n", v[h]); + else printf("X\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10611/10611-21.cpp b/uva_cpp_clean/10611/10611-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40817981b5d28ece9315dafdfe5595dc03db304c --- /dev/null +++ b/uva_cpp_clean/10611/10611-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10611 + Name: The Playboy Chimp + Problem: https://onlinejudge.org/external/106/10611.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int n, q, h, ladies[50001]; + cin>>n; + for (int i=0; i>ladies[i]; + + cin>>q; + for (int i=0; i>h; + auto r = equal_range(ladies, ladies+n, h); + int lo=r.first-ladies-1, hi=r.second-ladies; + if (lo>=0) cout << ladies[lo] << " "; + else cout << "X "; + + if (hi + +using namespace std; + +int main(){ + int n, q, m, u, v, l, r, mid, tmp; + bool ok; + scanf("%d", &n); + vector a; + for(int i = 0; i < n; i++){ + ok = true; + scanf("%d", &tmp); + for(int j = 0; j < a.size(); j++) + if(a[j] == tmp){ + ok = false; + break; + } + if(ok) + a.push_back(tmp); + } + scanf("%d", &q); + while(q--){ + scanf("%d", &m); + l = 0, r = 2 * a.size() - 2; + while(r - l > 1){ + mid = (l + r) / 2; + if(a[mid] < m) + l = mid; + else + r = mid; + } + if(a[l] == m) + l--; + if(a[r] == m) + r++; + if(m > a[a.size() - 1]) + printf("%d ", a[a.size() - 1]); + else if(l < 0 || m < a[0]) + printf("X "); + else + printf("%d ", a[l]); + if(m < a[0]) + printf("%d\n", a[0]); + else if(r >= a.size() || m > a[a.size() - 1]) + printf("X\n"); + else + printf("%d\n", a[r]); + } + return(0); +} diff --git a/uva_cpp_clean/10616/10616-13.cpp b/uva_cpp_clean/10616/10616-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df7c0ad645d1d259837610fc39b759d134d29b01 --- /dev/null +++ b/uva_cpp_clean/10616/10616-13.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 300 + +int n, q, m, d, SET, QUERY; + +int a[MAX]; + +int dp[MAX][MAX][30]; + +int cal(int i, int j, int k) +{ + if (k == m) + { + if (j == 0) + { + return 1; + } + + else + { + return 0; + } + } + + if (i >= n) + { + return 0; + } + + if (dp[i][j][k] != -1) + { + return dp[i][j][k]; + } + + return dp[i][j][k] = cal(i + 1, (j + a[i]) % d , k + 1) + cal(i + 1, j % d, k); +} + +int main() +{ + cin >> n >> q; + + SET = QUERY = 1; + + while (n != 0 && q != 0) + { + memset(a, 0, sizeof a); + + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + + cout << "SET " << SET << ":" << endl; + + QUERY = 1; + + while (q--) + { + cin >> d >> m; + + memset(dp, -1, sizeof dp); + + int ans = cal(0, 0, 0); + + cout << "QUERY " << QUERY << ": " << ans << endl; + + QUERY++; + } + + SET++; + + cin >> n >> q; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10616/10616-19.cpp b/uva_cpp_clean/10616/10616-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d7e30fddd6008c5f981956630f624d725662096 --- /dev/null +++ b/uva_cpp_clean/10616/10616-19.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef unordered_map uiii; +typedef unordered_map uiv; +typedef unordered_map uv; +typedef unordered_map uvi; + +int N; +int val[201]; +uvi reg; + +int func(int m, int d, int mod, int p) { + if (reg[m][d][mod][p].second == 1) return reg[m][d][mod][p].first; + int r = 0; + if (m != 1) { + for (int i = p; i <= N - m; i++) { + r += func(m-1, d, (d+mod-(val[i]%d))%d, i+1); + } + } else { + for (int i = p; i <= N - m; i++) { + r += ((val[i]%d)+d)%d == mod? 1: 0; + } + } + reg[m][d][mod][p].first = r; + reg[m][d][mod][p].second = 1; + return r; +} + +int main() { + int Q, m, d, set = 1; + while (cin >> N >> Q && !(N == 0 && Q == 0)) { + reg.clear(); + printf("SET %d:\n", set); + for (int i = 0; i < N; i++) cin >> val[i]; + for (int i = 1; i <= Q; i++) { + cin >> d >> m; + printf("QUERY %d: %d\n", i, func(m, d, 0, 0)); + } + set++; + } + return 0; +} diff --git a/uva_cpp_clean/10616/10616-21.cpp b/uva_cpp_clean/10616/10616-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cca4840db8eb9894f0014ee0f233ee88155c669 --- /dev/null +++ b/uva_cpp_clean/10616/10616-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10616 + Name: Divisible Group Sums + Problem: https://onlinejudge.org/external/106/10616.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +long long DP[205][11][21]; +int d[205]; + +#define mod(a,b) ((a)%(b)<0?(a)%(b)+(b):(a)%(b)) + +int main(){ + int N,Q,D,M,cse=1,qi,cnt,s,i; + + while(scanf("%d%d",&N,&Q)==2 && (N||Q)) + { + for(i=0;i>d[i]; + + for(i=0;i<=N;i++) + for(s=0;s<21;s++) + DP[i][0][s]=(s==0?1:0); + + printf("SET %d:\n",cse++); + for(qi=1;qi<=Q;qi++){ + scanf("%d%d",&D,&M); + + for(cnt=1;cnt<=M;cnt++) + for(s=0;s=0;i--) + for(cnt=1;cnt<=M;cnt++) + for(s=0;s subset sum + difficulty: medium + date: 24/Jan/2020 + problem: given an array of size N, count how many subsets of size M have their sum divisible by D + hint: use module arithmetic + by: @brpapa +*/ +#include +#include +#define ll long long +using namespace std; + +ll A[222]; +int N, D, M; + +ll memo[222][22][22]; +ll dp(int n, int m, int sum) { + // número atual A[n], soma sum (até D) do subconjunto de tamanho m + + if (m == M) return (ll) sum == 0; // subconjunto e tamanho M com soma divisível por D + if (n == N) return 0ll; + + ll &ans = memo[n][m][sum]; + if (ans != -1) return ans; + + // como sum é indice de memo, não posso passar com valor negativo! + int newSum = (sum+A[n]) % D; + if (newSum < 0) newSum += D; // equivale + + return ans = dp(n+1, m+1, newSum) + dp(n+1, m, sum); +} + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int T = 0; + while (true) { + int Q; cin >> N >> Q; + if (!N && !Q) return 0; + + for (int i = 0; i < N; i++) cin >> A[i]; + + printf("SET %d:\n", ++T); + for (int i = 1; i <= Q; i++) { + cin >> D >> M; + memset(memo, -1, sizeof memo); + printf("QUERY %d: %lld\n", i, dp(0, 0, 0)); + } + + } +} diff --git a/uva_cpp_clean/10616/10616-9.cpp b/uva_cpp_clean/10616/10616-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d954781d4f0e578626b2aa10a65452faf61472d1 --- /dev/null +++ b/uva_cpp_clean/10616/10616-9.cpp @@ -0,0 +1,30 @@ +#include + +#define MAX_N 210 +#define MAX_M 20 +#define MAX_SUM 30 + +using namespace std; + +int n,q,v[MAX_N],tc,d,m,memo[MAX_N][MAX_M][MAX_SUM]; + +int dp(int it,int ct,long long sum){ + if(ct==0 && sum==0) return 1; + if(ct==0 && sum!=0) return 0; + if(it==0) return 0; + if(memo[it][ct][sum]!=-1) return memo[it][ct][sum]; + return memo[it][ct][sum] = dp(it-1,ct,sum)+dp(it-1,ct-1,(d+(sum+v[it])%d)%d); +} + +int main(){ + while(scanf("%d %d",&n,&q),n|q){ + for(int i=1;i<=n;i++) scanf("%d",&v[i]); + printf("SET %d:\n",++tc); + for(int t=1;t<=q;t++){ + memset(memo,-1,sizeof memo); + scanf("%d %d",&d,&m); + printf("QUERY %d: %d\n",t,dp(n,m,0)); + } + } + return(0); +} diff --git a/uva_cpp_clean/10617/10617-10.cpp b/uva_cpp_clean/10617/10617-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/uva_cpp_clean/10617/10617-10.cpp differ diff --git a/uva_cpp_clean/10617/10617-21.cpp b/uva_cpp_clean/10617/10617-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fa5bebf340d3875462b8c755c9aba2baeca1eae --- /dev/null +++ b/uva_cpp_clean/10617/10617-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10617 + Name: Again Palindrome + Problem: https://onlinejudge.org/external/106/10617.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +string ln; +long long memo[64][64]; +long long dfs(int i, int j) { + if (j-i<0) return 1; + if (memo[i][j] != -1) { + return memo[i][j]; + } + + // remove i-th + long long res = dfs(i+1, j); + + // preserve i-th + for (int k=i; k<=j; ++k) + if (ln[k] == ln[i]) { + res += dfs(i+1, k-1); + } + + return memo[i][j] = res; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + while(T--) { + cin >> ln; + memset(memo, -1, sizeof(memo)); + cout << dfs(0, ln.size()-1)-1 << endl; + } +} diff --git a/uva_cpp_clean/10617/10617-9.cpp b/uva_cpp_clean/10617/10617-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d785e5b6d44a6d578bdfffc1b09c376d9c6bd93 --- /dev/null +++ b/uva_cpp_clean/10617/10617-9.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +typedef long long ll; + +const int MAX_LEN = 60 + 5; + +ll memo[MAX_LEN][MAX_LEN]; +string s; + +ll dp (int i, int j) { + if (i > j) return 1; + if (~memo[i][j]) return memo[i][j]; + ll ret = dp(i, j - 1); + for (int k = i; k <= j; k++) if (s[k] == s[j]) ret += dp(k + 1, j - 1); + return memo[i][j] = ret; +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + memset(memo, -1, sizeof memo); + cin >> s; + cout << dp(0, s.size() - 1) - 1 << endl; + } + return (0); +} diff --git a/uva_cpp_clean/1062/1062-20.cpp b/uva_cpp_clean/1062/1062-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..813d6678ef94908fac458574859b5093edb103fb --- /dev/null +++ b/uva_cpp_clean/1062/1062-20.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +using namespace std; + +char line[1005]; +int dp[1005]; + +int main() +{ + int ncase = 0; + + while( gets(line) && strcmp(line, "end") ) + { + for(int I=0 ; I<1005 ; I++) dp[I] = 0; + + int stacks = 0; + for(int I=0 ; I>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1062 + Name: Containers + Problem: https://onlinejudge.org/external/10/1062.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char line[2000]; + for (int cse=1; cin>>line && line[0]!='e'; ++cse) { + char stks[] = "ZZZZZZZZZZZZZZZZZZZZZZZZZZ"; + int sz = 1; + for (int i=0; line[i]; ++i) { + int ind = lower_bound(stks, stks+sz, line[i]) - stks; + stks[ind] = line[i]; + if (ind >= sz) sz = ind+1; + } + printf("Case %d: %d\n", cse, sz); + } +} diff --git a/uva_cpp_clean/1062/1062-5.cpp b/uva_cpp_clean/1062/1062-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bb4b390319c8d8f49b9d5653351e0cd5d31d092f --- /dev/null +++ b/uva_cpp_clean/1062/1062-5.cpp @@ -0,0 +1,115 @@ +/****************************************************************** +*** Problewm : Uva 1062 - Containers *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define CP(a) cout<>s) + { + if(s=="end") break; + + int len=s.size(); + int a[10010]; + for(int i=0;i +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef vector vi; +typedef vector vii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +void sieve(ll upperbound = 70000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +vii primeFactors(ll n) { + vii factors; int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; + while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); + return factors; +} + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m > 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +int main() { + sieve(); + long long n; + while (cin >> n && n != 0) { + bool neg = (n < 0); + if (neg) n = -n; + vii factors = primeFactors(n); + int p = 0; + for (int i = 0; i < factors.size(); i++) { + p = gcd(factors[i].second, p); + } + while (neg && p%2 == 0) p /= 2; + printf("%d\n", p); + } + return 0; +} diff --git a/uva_cpp_clean/10622/10622-9.cpp b/uva_cpp_clean/10622/10622-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdbe84dde3b8306ae149c8e06ff23ba45f00e2a8 --- /dev/null +++ b/uva_cpp_clean/10622/10622-9.cpp @@ -0,0 +1,51 @@ +#include + +#define SIZE 100010 + +using namespace std; + +bool is_prime[SIZE], ok; +vector prime, Factor; +int ans; +long long N; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++) + if( is_prime[it1] ){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +void GetFactors(){ + Factor.clear(); + for(int it = 0; it < prime.size() and prime[it] * prime[it] <= N and N != 1; it++) + if( N % prime[it] == 0 ){ + int ct = 0; + while( N % prime[it] == 0 ) N /= prime[it], ct++; + Factor.push_back(ct); + } + if( N != 1 ) Factor.push_back( 1 ); +} + +int gcd(int a, int b){ + return b == 0 ? a : gcd(b, a % b); +} + +int main(){ + sieve(); + while(scanf("%lld", &N), N){ + ok = false; + if( N < 0 ) N *= -1, ok = true; + GetFactors(); + ans = Factor[0]; + for(int it = 1; it < Factor.size(); it++) + ans = gcd(ans, Factor[it]); + if( ok ) while( ans % 2 == 0 ) ans /= 2; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10625/10625-21.cpp b/uva_cpp_clean/10625/10625-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a7fd5776b60f88ee484e12a858fa0ed7fdfd299 --- /dev/null +++ b/uva_cpp_clean/10625/10625-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10625 + Name: GNU = GNU'sNotUnix + Problem: https://onlinejudge.org/external/106/10625.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct CharCount { + char ch; + int cnt; + CharCount(char ch, int cnt):ch(ch),cnt(cnt){} +}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char ch; + string str; + int T, n, r; + cin >> T; + while (T-- && cin>>n) { + vector rules[127]; + for (int i=0; i> str; + int C[127] = {}; + for (int j=3; str[j]; ++j) + ++C[str[j]]; + + vector &v = rules[str[0]]; + for (int i=0; i<127; ++i) if (C[i]) + v.push_back(CharCount(i, C[i])); + } + + cin >> n; + while (n--) { + unsigned long long C[127] = {}, CT[127] = {}; + cin >> str >> ch >> r; + for (int i=0; str[i]; ++i) + ++C[str[i]]; + + while (r--) { + for (char ch=33; ch<127; ++ch) { + if (rules[ch].size()) + for (const CharCount &cc: rules[ch]) + CT[cc.ch] += C[ch]*cc.cnt; + else CT[ch] += C[ch]; + C[ch] = 0; + } + + swap(CT, C); + } + cout << C[ch] << "\n"; + } + } +} diff --git a/uva_cpp_clean/10626/10626-10.cpp b/uva_cpp_clean/10626/10626-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad0fe559fed877482077ad9813e07d30f0c2702a --- /dev/null +++ b/uva_cpp_clean/10626/10626-10.cpp @@ -0,0 +1,70 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + __builtin_popcountll(); +*/ + +int c,n1,n5,n10; + +int dp[60][105][505]; + +int solve(int i,int a,int b,int f) { + + if(i==c) { + return 0; + } + + int &ans=dp[a][b][f]; + + if(ans!=-1) return ans; + + ans =1e9; + if(a>=1) { + ans = min(ans,1+ solve(i+1,a-1,b,f+2)); + } + + if(b>=2) { + ans = min(ans,2+ solve(i+1,a,b-2,f+2)); + } + + if(b>=1 && f>=3) { + ans = min(ans ,4+ solve(i+1,a,b-1,f-3)); + } + + if(f>=8) { + ans = min(ans,8+ solve(i+1,a,b,f-8)); + } + + if (f>= 3 && a>= 1 ) { + ans = min(ans , 4+solve(i + 1, a-1, b+1, f-3)); + } + return ans; +} + +void solveit() { + + cin>>c>>n1>>n5>>n10; + + memset(dp,-1,sizeof dp); + cout<>t; + while (t--) { + solveit(); + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/10633/10633-19.cpp b/uva_cpp_clean/10633/10633-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d379f570566d00dec39115568c7ca09671dbc8a0 --- /dev/null +++ b/uva_cpp_clean/10633/10633-19.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +void proc(long long n, long long & can, long long pot, int carry) { + if (n / (pot) == 0) return; + can += (((can/pot)%10 - carry - (n/pot)%10 + 20) % 10)*10*pot; + proc(n, can, 10*pot, ((can/pot)%10) - carry < (can/(10*pot))%10? 1: 0); +} + +int main() { + long long n; + while (cin >> n && n != 0) { + bool first = false; + set ret; + for (int i = 0; i < 10; i++) { + long long can = (i)*10 + (n%10 + i)%10; + proc(n, can, 10, (n%10 + i)%10 < i? 1: 0); + if (can - can/10 == n) ret.insert(can); + } + for (auto it = ret.begin(); it != ret.end(); ++it) { + if (first) printf(" "); + else first = true; + printf("%lld", *it); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10633/10633-20.cpp b/uva_cpp_clean/10633/10633-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91d259198aacf074d5c1a746157a4b66a17313ff --- /dev/null +++ b/uva_cpp_clean/10633/10633-20.cpp @@ -0,0 +1,15 @@ +#include + +int main() +{ + unsigned long long N_M; + + while( scanf("%llu", &N_M)==1 && N_M ) + { + long long N = (N_M*10)/9; + + if( N_M%9==0 ) printf("%llu %llu\n", N-1, N); + else printf("%llu\n", N); + } + return 0; +} diff --git a/uva_cpp_clean/10633/10633-21.cpp b/uva_cpp_clean/10633/10633-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abc4e6a00825b59f0ac2fbc05acf679a43f668e1 --- /dev/null +++ b/uva_cpp_clean/10633/10633-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10633 + Name: Rare Easy Problem + Problem: https://onlinejudge.org/external/106/10633.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + long long nm; + while (cin>>nm && nm) { + vector v; + for (int i=0; i<10; i++) + if ((nm-i)%9 == 0) + v.push_back(10*((nm-i)/9) + i); + + if (!v.empty()) { + sort(v.begin(), v.end()); + cout << v[0]; + for (int i=1; i + +using namespace std; + +long long N_M; + +int main(){ + while(scanf("%llu", &N_M), N_M) + for(long long L = ( N_M + 8 )/ 9 - 1, U = N_M / 9; L <= U; L++) + printf( L != U ? "%lld " : "%lld\n", L + N_M); + return(0); +} diff --git a/uva_cpp_clean/10635/10635-21.cpp b/uva_cpp_clean/10635/10635-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40a0328e67b9a4cffcc20714f0bf635002334264 --- /dev/null +++ b/uva_cpp_clean/10635/10635-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10635 + Name: Prince and Princess + Problem: https://onlinejudge.org/external/106/10635.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int I[73000], X[73000], M[73000], n, p, q, T; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + cin >> T; + for (int cse=1; cse <= T; ++cse) { + cin >> n >> p >> q; + n = n*n; ++p; ++q; + + memset(I, -1, sizeof(I)); + for (int i=0, t; i> t; + I[t] = i; + } + + int L = 0; + for (int j=0; j> X[j]; + if (I[X[j]] == -1) continue; + + int lo = 1, hi = L; + while (lo <= hi) { + int mid = (lo + hi + 1) >> 1; + if (I[X[M[mid]]] < I[X[j]]) + lo = mid + 1; + else + hi = mid - 1; + } + + M[lo] = j; + L = max(L, lo); + } + + cout << "Case " << cse << ": " << L << '\n'; + } +} diff --git a/uva_cpp_clean/10635/10635-6.cpp b/uva_cpp_clean/10635/10635-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8142c0eda75ba3648aa3438d0d7bb22cb9c31ea --- /dev/null +++ b/uva_cpp_clean/10635/10635-6.cpp @@ -0,0 +1,51 @@ +/* + dynamic programming > LCS reduced to LIS + difficulty: medium + date: 17/Oct/2020 + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; +const int INF = 1 << 30; + +vector C; int N; + +int lis() { + vector size(N); + vector last(N+1, INF); + last[0] = -INF; + + int biggest_s = 0; + + for (int j = 0; j < N; j++) { + int s = lower_bound(last.begin(), last.end(), C[j]) - last.begin(); + + if (C[j] < last[s]) + last[s] = C[j]; + + size[j] = s; + biggest_s = max(biggest_s, s); + } + + return biggest_s; +} + +int main() { + int T, t = 1; cin >> T; + while (T--) { + int _, P, Q; cin >> _ >> P >> Q; P++; Q++; + vectorA(P); for (int &a: A) cin >> a; + vectorB(Q); for (int &b: B) cin >> b; + + N = Q; + C.resize(N); + for (int i = 0; i < N; i++) { + int p = find(A.begin(), A.end(), B[i]) - A.begin(); + C[i] = p < P? p : -1; + } + + cout << "Case " << (t++) << ": " << lis() << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10635/10635-9.cpp b/uva_cpp_clean/10635/10635-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edced44a78da5d52905165f0181813b5d80812ff --- /dev/null +++ b/uva_cpp_clean/10635/10635-9.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +const int MAX_N = 250 * 250 + 10; + +int n, n1, n2, idx[MAX_N], L[MAX_N]; +vector arr; + +int lis () { + int ret = 0; + for (int i = 0; i < arr.size(); i++) { + int pos = lower_bound(L, L + ret, arr[i]) - L; + L[pos] = arr[i]; + if (pos + 1 > ret) ret = pos + 1; + } + return ret; +} + +void readCase () { + cin >> n >> n1 >> n2; + for (int i = 0, a; i <= n1; i++) { + cin >> a; + idx[a] = i; + } + for (int i = 0, a; i <= n2; i++) { + cin >> a; + if (~idx[a]) arr.push_back(idx[a]); + } +} + +void clearCase () { + arr.clear(); + memset(idx, -1, sizeof idx); +} + +int main () { + int tc; + cin >> tc; + for (int t = 1; t <= tc; t++) { + clearCase(); + readCase(); + printf("Case %d: %d\n", t, lis()); + } +} diff --git a/uva_cpp_clean/1064/1064-21.cpp b/uva_cpp_clean/1064/1064-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8710a0b45ae67e101791c20893d094d673c14c97 --- /dev/null +++ b/uva_cpp_clean/1064/1064-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1064 + Name: Network + Problem: https://onlinejudge.org/external/10/1064.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct Packet { + int st, ed; + Packet(int a, int b):st(a),ed(b){} + bool operator < (const Packet &o) const { return st > o.st; } +}; + + +int O[7], Ml[7], Pi[1024], Ps[1024], Pe[1024], n, m, bsum; +int calcSize() { + priority_queue pp[7]; + int wl[7]={}, clen=0, mxlen = 0; + + for (int j=0, i=0; j &q = pp[id]; + + while (!q.empty() && wl[id]==q.top().st) { + wl[id] = q.top().ed; + clen -= q.top().ed - q.top().st; + q.pop(); + } + + for (; wl[id]!=Ml[id]; ++i) + if (Pi[i]==id && Ps[i]==wl[id]) { + wl[id] = Pe[i]; + while (!q.empty() && wl[id]==q.top().st) { + wl[id] = q.top().ed; + clen -= q.top().ed - q.top().st; + q.pop(); + } + } + else { + pp[Pi[i]].push(Packet(Ps[i], Pe[i])); + clen += Pe[i] - Ps[i]; + mxlen = max(mxlen, clen); + } + } + return mxlen; +} + +bool used[7]; +void bt(int idx) { + if (idx == m) { + bsum = min(bsum, calcSize()); + return; + } + + for (int i=0; i> m >> n && (m||n); ++cse) { + for (int i=0; i> Ml[i]; + + for (int i=0; i> Pi[i] >> Ps[i] >> Pe[i]; + --Pi[i]; --Ps[i]; + } + + bsum = (int) 1e9; bt(0); + cout << "Case " << cse << ": " << bsum << "\n\n"; + } +} diff --git a/uva_cpp_clean/10642/10642-19.cpp b/uva_cpp_clean/10642/10642-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d9e4319c63465f6d42134e69102db1a111daba9 --- /dev/null +++ b/uva_cpp_clean/10642/10642-19.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +inline int gauss(int n) { + return n*(n+1)/2; +} + +int main() { + int n; + cin >> n; + for (int c = 1; c <= n; c++) { + int x1, y1, x2, y2; + cin >> x1 >> y1 >> x2 >> y2; + printf("Case %d: %d\n", c, x2 + gauss(x2 + y2) - gauss(x1 + y1) - x1); + } + return 0; +} diff --git a/uva_cpp_clean/10643/10643-9.cpp b/uva_cpp_clean/10643/10643-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0620279af3b8257cc7e9f9f71418482e101d174 --- /dev/null +++ b/uva_cpp_clean/10643/10643-9.cpp @@ -0,0 +1,14 @@ +def main(): + SIZE = 1010 + C = [[0 for i in range(SIZE)] for j in range(SIZE) ] + for i in range(SIZE): + C[i][0], C[i][i] = 1, 1 + for i in range(2, SIZE): + for k in range(1, i): + C[i][k] = C[i - 1][k] + C[i - 1][k - 1] + for tc in range(1, 1 + int( input() )): + n = ( int( input() ) - 2 ) // 2 + print('Case %d: %d' %(tc, C[2 * n + 1][n + 1])) + +if __name__ == '__main__': + main() diff --git a/uva_cpp_clean/10646/10646-21.cpp b/uva_cpp_clean/10646/10646-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a06740888afc9cc151d73330a8c3f9be2be02d9 --- /dev/null +++ b/uva_cpp_clean/10646/10646-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10646 + Name: What is the Card? + Problem: https://onlinejudge.org/external/106/10646.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + char C[52][3]; + for (int i=0; i<52; ++i) + cin >> C[i]; + + int top = 26, y=0; + for (int i=0; i<3; ++i) { + if (top < 0) throw 1; + int x = isdigit(C[top][0]) ? C[top][0]-'0' : 10; + y += x; + top -= 10-x+1; + } + + cout << "Case " << cse << ": " + << (y<=top ? C[y] : C[25+y-top]) << endl; + } +} diff --git a/uva_cpp_clean/10646/10646-9.cpp b/uva_cpp_clean/10646/10646-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e5bc2ea6b5685986bf39faa9533a5b44d258e8c --- /dev/null +++ b/uva_cpp_clean/10646/10646-9.cpp @@ -0,0 +1,50 @@ +/** + * > Author : TISparta + * > Date : 11-08-18 + * > Tags : Implementation + * > Difficulty : 3 / 10 + */ + +#include + +using namespace std; + +int T, Y; +vector pile, hand; + +void clear () { + pile.clear(); + hand.clear(); + Y = 0; +} + +void read () { + string line; + getline(cin, line); + istringstream ss(line); + string s; + while (ss >> s) pile.size() == 27 ? hand.push_back(s) : pile.push_back(s); +} + +void exec () { + int X = isdigit(pile.back()[1]) ? stoi(pile.back()) : 10; + Y += X; + pile.pop_back(); + X = 10 - X; + while (X--) pile.pop_back(); +} + +void solveCase (int t) { + clear(); + read(); + for (int it = 0; it < 3; it++) exec(); + pile.insert(end(pile), begin(hand), end(hand)); + cout << "Case " << t << ": " << pile[Y - 1] << endl; +} + +int main () { + cin >> T; + cin.ignore(); + for (int t = 1; t <= T; t++) solveCase(t); + return (0); +} diff --git a/uva_cpp_clean/10650/10650-18.cpp b/uva_cpp_clean/10650/10650-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..44723cbb5dc2942a011b094191dcedfb675b81d9 --- /dev/null +++ b/uva_cpp_clean/10650/10650-18.cpp @@ -0,0 +1,70 @@ +#include +#define pb push_back +#define pf push_front +#define pob pop_back +#define fp first +#define sp second +#define mp make_pair +#define ins insert +#define uEdge(u, v) g[u].pb(v), g[v].pb(u) +#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w}) +#define dEdge(u, v) g[u].pb(v) +#define dwEdge(u, v, w) g[u].pb({v, w}) +#define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define minHeap int, vector, greater + +using namespace std; +typedef long long int lli; +typedef pair pii; +vector g[0]; +int primes[33000]; +vector p; + +lli gcd(lli a, lli b){ + if(b == 0) return a; + a%=b; + return gcd(b, a); +} + +void sieve(){ + memset(primes, 1, sizeof(primes)); + primes[0] = primes[1] = 0; + for(int i = 2; i*i <= 33000; i++){ + if(primes[i]) for(int j = i*i; j <= 33000; j+=i) primes[j] = 0; + } + for(int i = 2; i <= 33000; i++) if(primes[i]) p.pb(i); +} + +int main(){ + sieve(); + int x, y, low, i, j, delta; + while(scanf("%d %d", &x, &y)){ + if(!x && !y) break; + if(x > y) swap(x, y); + low = 0; + while(p[low] < x) low++; + while(p[low + 2] <= y){ + if(p[low + 2] - p[low + 1] == p[low + 1] - p[low]){ + i = low; + j = low + 2; + delta = p[low + 1] - p[low]; + while(low + 3 < p.size() && p[low + 3] - p[low + 2] == delta){ + j++; + low++; + } + if(p[j] <= y){ + if(!i || p[i] - p[i - 1] != delta){ + printf("%d", p[i]); + for(int k = i + 1; k <= j; k++) printf(" %d", p[k]); + printf("\n"); + } + } else break; + } + low++; + } + } + return 0; +} +/* + Brute force para hallar los primos uni-distantes. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10650/10650-8.cpp b/uva_cpp_clean/10650/10650-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a78d04ee9454ea656afb8043ee196973921b4a99 --- /dev/null +++ b/uva_cpp_clean/10650/10650-8.cpp @@ -0,0 +1,61 @@ +// Problem name: Determinate Prime +// Problem link: https://vjudge.net/problem/UVA-10650 +// Submission link: https://vjudge.net/solution/32958906 + +#include + +using namespace std; + +vector get_primes(int n){ + vector is_prime(n + 1, 1); + vector primes; + is_prime[ 0 ] = is_prime[ 1 ] = 0; + if(n >= 2){ + primes.push_back(2); + for(int i = 4 ; i <= n ; i += 2) + is_prime[ i ] = 0; + } + for(int i = 3 ; i <= n ; i += 2) + if(is_prime[ i ]){ + primes.push_back(i); + for(int j = i * i ; j <= n ; j += 2 * i) + is_prime[ j ] = 0; + } + return primes; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int MIN_SIZE = 3; + const int N = 32500; + const vector primes = get_primes(N); + + for(int l, r ; cin >> l >> r && l != 0 || r != 0 ; ){ + if(l > r) + swap(l, r); + l = lower_bound(primes.begin(), primes.end(), l) - primes.begin(); + r = upper_bound(primes.begin(), primes.end(), r) - primes.begin(); + for(int i = l ; i + MIN_SIZE <= r ; ++i){ + vector ans{ primes[ i ], primes[ i + 1 ]}; + for(int k = 2 ; k < MIN_SIZE ; ++k){ + if(primes[ i + k ] - primes[ i + k - 1 ] != primes[ i + k - 1 ] - primes[ i + k - 2 ]) + break; + ans.push_back(primes[ i + k ]); + } + if(ans.size() == MIN_SIZE){ + bool ok = i != l || (i > 0 && primes[ i + 1 ] - primes[ i ] != primes[ i ] - primes[ i - 1 ]); + for(i += MIN_SIZE - 1 ; i + 1 < r && primes[ i + 1 ] - primes[ i ] == primes[ i ] - primes[ i - 1 ] ; ++i) + ans.push_back(primes[ i + 1 ]); + if(ok && (i + 1 != r || primes[ i + 1 ] - primes[ i ] != primes[ i ] - primes[ i - 1 ])){ + cout << ans[ 0 ]; + for(int j = 1 ; j < ans.size() ; ++j) + cout << ' ' << ans[ j ]; + cout << endl; + } + --i; + } + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10650/10650-9.cpp b/uva_cpp_clean/10650/10650-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a717e221031f1a4ffba6ca0e2dccf746008a5727 --- /dev/null +++ b/uva_cpp_clean/10650/10650-9.cpp @@ -0,0 +1,47 @@ +#include + +#define SIZE 32000 +#define LIM 200 + +using namespace std; + +bool is_prime[SIZE]; +vectorprime; +vectordeterminate[LIM]; +int x, y, z; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + for(long long it1 = 2; it1 < SIZE; it1++){ + if(is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } + } +} + +int main(){ + sieve(); + for(int it = 0; it < prime.size() - 1; ){ + int dis = prime[it + 1] - prime[it], j; + for(j = it + 1; j < prime.size() - 1; ) + if(prime[j + 1] - prime[j] == dis) j++; + else break; + if(j - it >=2){ + for(int i = it; i <= j; i++) + determinate[z].push_back( prime[i] ); + z++; + } + it = j; + } + while(scanf("%d %d",&x, &y), x|y){ + if(x>y) swap(x, y); + for(int it = 0; it < z; it++){ + if(determinate[it][0] >= x and determinate[it].back() <= y) + for(int i = 0; i < determinate[it].size(); i++) + printf(i!=determinate[it].size() - 1 ? "%d " : "%d\n", determinate[it][i]); + } + } + return(0); +} diff --git a/uva_cpp_clean/10651/10651-21.cpp b/uva_cpp_clean/10651/10651-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fd5d64d14466852814d49fa4934b2c40e9012e3 --- /dev/null +++ b/uva_cpp_clean/10651/10651-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10651 + Name: Pebble Solitaire + Problem: https://onlinejudge.org/external/106/10651.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int DP[4096]; +int rec(int state) { + if (!state) return 0; + if (DP[state]) return DP[state]; + + int result = 1000; + for (int i=0, x=state; x && i<10; ++i, x>>=1) { + int t = x&7; + if (t == 3 || t == 6) + result = min(result, rec(state ^ (7 << i))); + } + + if (result == 1000) { + result = 0; + for (; state; state>>=1) + result += state&1; + } + return DP[state]=result; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + string line; + while (T-- && cin >> line) { + int num = 0; + for (int i=0; i<12; ++i) + num = num<<1 | (line[i]=='o'); + + cout << rec(num) << '\n'; + } +} diff --git a/uva_cpp_clean/10651/10651-6.cpp b/uva_cpp_clean/10651/10651-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5b0c3540c066728ab6d88ec3004597f5d2800b4 --- /dev/null +++ b/uva_cpp_clean/10651/10651-6.cpp @@ -0,0 +1,46 @@ +/* + dynamic programming + difficulty: medium + date: 08/Feb/2020 + hint: use bitmask + by: @brpapa +*/ +#include +#include +#define isOn(S, i) (S & (1 << (i))) +#define INF 13 +using namespace std; + +int memo[1 << 13]; +int dp(int mask) { + // mask: conjunto atual + + int &ans = memo[mask]; + if (ans != -1) return ans; + + ans = INF; + + for (int i = 0; i <= 9; i++) + if (!isOn(mask, i) && isOn(mask, i+1) && isOn(mask, i+2)) + ans = min(ans, dp(mask & ~(1 << (i+1)) & ~(1 << (i+2)) | (1 << i))); + + for (int i = 11; i >= 2; i--) + if (!isOn(mask, i) && isOn(mask, i-1) && isOn(mask, i-2)) + ans = min(ans, dp(mask & ~(1 << (i-1)) & ~(1 << (i-2)) | (1 << i))); + + return ans = (ans < INF)? ans : __builtin_popcount(mask); +} + +int main() { + int T; cin >> T; + memset(memo, -1, sizeof memo); + while (T--) { + int mask = 0; + for (int i = 0; i < 12; i++) { + char c; cin >> c; + if (c == 'o') mask |= (1 << i); + } + cout << dp(mask) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10653/10653-10.cpp b/uva_cpp_clean/10653/10653-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/uva_cpp_clean/10653/10653-10.cpp differ diff --git a/uva_cpp_clean/10653/10653-13.cpp b/uva_cpp_clean/10653/10653-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1be85a2121b8ad9304e07cb12b34614c93574a8b --- /dev/null +++ b/uva_cpp_clean/10653/10653-13.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 1005 + +int n, m, sX, sY, tX, tY; +int dis[MAX][MAX]; +int maze[MAX][MAX]; + +char disX[4] = { 0, 1, 0, -1 }; +char disY[4] = { 1, 0, -1, 0 }; + +class comparator +{ +public: + + bool operator()(pair, int> &child1, pair, int> &child2) + { + return child1.second > child2.second; + } +}; + +void initialize() +{ + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + dis[i][j] = INT_MAX; + + if (i == 0 || j == 0 || i > n || j > m) + { + maze[i][j] = 0; + } + + else + { + maze[i][j] = 1; + } + } + } +} + +int travel(int startX, int startY) +{ + dis[startX][startY] = 0; + + priority_queue < pair, int>, vector, int>>, comparator>pq; + + pq.push(make_pair(make_pair(startX, startY), dis[startX][startY])); + + while (!pq.empty()) + { + pair, int> head = pq.top(); + + pq.pop(); + + int i = head.first.first; + int j = head.first.second; + + int headDis = head.second; + + for (int k = 0; k < 4; k++) + { + int x = i + disX[k]; + int y = j + disY[k]; + + if (maze[x][y] == 1) + { + if (1 + headDis < dis[x][y]) + { + dis[x][y] = headDis + 1; + + pq.push(make_pair(make_pair(x, y), dis[x][y])); + } + } + } + } + + return dis[tX][tY]; +} + +int main() +{ + while (cin >> n >> m, n != 0 || m != 0) + { + initialize(); + + int how, r, c, howC; + + cin >> how; + + for (int i = 0; i < how; i++) + { + cin >> r >> howC; + + r++; + + for (int j = 0; j < howC; j++) + { + cin >> c; + + c++; + + maze[r][c] = 0; + } + } + + cin >> sX >> sY >> tX >> tY; + + sX++; sY++; tX++; tY++; + + cout << travel(sX, sY) << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10653/10653-14.cpp b/uva_cpp_clean/10653/10653-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..449f3a3760d5aaefaa7d8d465982ac26c5106a41 --- /dev/null +++ b/uva_cpp_clean/10653/10653-14.cpp @@ -0,0 +1,74 @@ +/* Problem name : 10653 - Bombs! NO they are Mines!! + * OJ : UVA + * Result : Accepted + * Date : 24-03-17 + * Problem Type : Graph(BFS 2D) + */ +#include +#include +#include +#include +#include +#define P pair +#define MAX 1005 +using namespace std; +typedef long long ll; +int fx[] = {1, -1, 0, 0}; +int fy[] = {0, 0, 1, -1}; +//~ vectorW[MAX]; +int W[MAX][MAX]; +bool visit[MAX][MAX]; +int cost[MAX][MAX]; +int R,C; +int BFS_2D(int x,int y,int dx,int dy) { + queue

Q; + visit[x][y] = true; + cost[x][y] = 0; + Q.push(P(x,y)); + while(!Q.empty()) { + P top = Q.front(); + Q.pop(); + for(int i = 0; i<4; i++) { + int xx = top.first + fx[i]; + int yy = top.second + fy[i]; + if(xx>=0 && yy>=0 && xx>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10653 + Name: Bombs! NO they are Mines!! + Problem: https://onlinejudge.org/external/106/10653.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct Cell { + int r, c, d; + Cell(int r, int c, int d): r(r), c(c), d(d) {} +}; + +int dr[]={0, 0, 1, -1}, + dc[]={1, -1, 0, 0}; + +bool mp[1001][1001]; + + +int main(){ + int R, C, r, c, rn, cn, r0, c0, r1, c1; + while (cin>>R>>C && (R||C)) { + for (int i=0; i> rn; + for (int i=0; i> r >> cn; + for (int j=0; j> c; + mp[r][c] = 1; + } + } + cin>>r0>>c0>>r1>>c1; + + int result=-1; + queue q; + q.push(Cell(r0, c0, 0)); + while (!q.empty()) { + Cell p = q.front(); q.pop(); + if (p.r==r1 && p.c==c1) { + result = p.d; + break; + } + + mp[p.r][p.c] = 1; + for (int i=0; i<4; i++) { + int nr = p.r + dr[i], nc = p.c + dc[i]; + + if (nr>=0 && nr=0 && nc + +using namespace std; + +int n, m, b, br, bc, bn, mt[1005][1005], d[10000000]; +int sx, sy, tx, ty, _source, _target; +int xx[] = {-1, 0, 0, 1}; +int yy[] = {0, 1, -1, 0}; + +int bfs(int source){ + for(int i = 0;i < n * m; i++) d[i] = -1; + queue Q; + Q.push(source); + d[source] = 0; + while(!Q.empty()){ + int u = Q.front(); + Q.pop(); + int x = u / m, y = u % m; + for(int i = 0; i < 4; i++){ + int _x = x + xx[i], _y = y + yy[i], v = _x * m + _y; + if(_x >= 0 && _y >= 0 && _x < n && _y < m && mt[_x][_y] && d[v] == -1) + d[v] = d[u] + 1, Q.push(v); + } + if(d[_target] != -1) return d[_target]; + } +} + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) mt[i][j] = true; + scanf("%d", &b); + for(int i = 0; i < b; i++){ + scanf("%d %d", &br, &bn); + for(int j = 0; j < bn; j++) scanf("%d", &bc), mt[br][bc] = false; + } + scanf("%d %d", &sx, &sy); + scanf("%d %d", &tx, &ty); + _source = sx * m + sy; + _target = tx * m + ty; + printf("%d\n", bfs(_source)); + } + return(0); +} diff --git a/uva_cpp_clean/10656/10656-13.cpp b/uva_cpp_clean/10656/10656-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d3d16f31f10df077842b4cf49f9984885d13b51 --- /dev/null +++ b/uva_cpp_clean/10656/10656-13.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int n, x; + + while (cin >> n , n != 0) + { + vector a; + + for (int i = 0; i < n; i++) + { + cin >> x; + + if (x > 0) a.push_back(x); + } + + if (a.size() == 0) cout << 0; + + else + { + for (int i = 0; i < a.size(); i++) + { + cout << a[i]; + + if (i != a.size() - 1) cout << " "; + } + } + + cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10656/10656-19.cpp b/uva_cpp_clean/10656/10656-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2bf02b4a239a4d2ad475e88558e684d51d45ceee --- /dev/null +++ b/uva_cpp_clean/10656/10656-19.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main() { + int n, val; + while (scanf("%d", &n) && n != 0) { + bool output = false; + while (n--) { + scanf("%d", &val); + if (val != 0) { + if (output) printf(" "); + printf("%d", val); + output = true; + } + } + if (!output) printf("0\n"); + else printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10656/10656-21.cpp b/uva_cpp_clean/10656/10656-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11e4a7cc9368c298e7fd7000553acbba479a2536 --- /dev/null +++ b/uva_cpp_clean/10656/10656-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10656 + Name: Maximum Sum (II) + Problem: https://onlinejudge.org/external/106/10656.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n, x; + while (cin>>n && n) { + bool f = 1; + for (int i=0; i>x; + if (x>0) { + if (f) f=0; + else cout << " "; + cout << x; + } + } + if (f) cout << 0; + cout << endl; + } +} diff --git a/uva_cpp_clean/10656/10656-5.cpp b/uva_cpp_clean/10656/10656-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..caec2e390f4a64eb98bbb945d326b75472ef116f --- /dev/null +++ b/uva_cpp_clean/10656/10656-5.cpp @@ -0,0 +1,184 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%I64d",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%I64d%I64d",&a,&b) + +#define sfi(t) scanf("%d",&t) +#define sfii(a,b) scanf("%d %d",&a,&b) +#define sfiii(a,b,c) scanf("%d %d %d",&a,&b,&c) +#define sfll(t) scanf("%I64d",&t) +#define sfllll(a,b) scanf("%I64d %I64d",&a,&b) +#define sfllllll(a,b,c) scanf("%I64d %I64d %I64d",&a,&b,&c) +#define sfd(t) scanf("%lf",&t) +#define sfc(c) scanf("%c",&c) +#define sfs(s) scanf("%s",s) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%I64d\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%I64d ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%I64d %I64d\n",a,b) +#define PPLN(a,b) printf("%I64d %I64d ",a,b) + +#define pfi(a) printf("%d\n",a) +#define pfii(a,b) printf("%d %d\n",a,b) +#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c) +#define pfll(a) printf("%I64d\n",a) +#define pfllll(a,b) printf("%I64d %I64d\n",a,b) +#define pfllllll(a,b,c) printf("%I64d %I64d %I64d\n",a,b,c) +#define pfd(a) printf("%lf\n",a) +#define pfs(a) printf("%s\n",a) +#define pfis(a) printf("%d ",a) +#define pflls(a) printf("%I64d ",a) +#define pfds(a) printf("%lf ",a) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "< +#include + +bool fit(int size, std::string paragraphs[], int m, int x, int y) +{ + int xx = 0, yy = 0; + + for (int i = 0; i < m; i++) + { + for (int j = 0; j < (int)paragraphs[i].size(); j++) + { + if (xx + size <= x) + { + xx += size; + } + else + { + for (int k = 0; j >= 0 && k <= x / size; j--, k++) + { + if (paragraphs[i][j] == ' ') + { + break; + } + } + + if (j < 0 || paragraphs[i][j] != ' ') + { + return false; + } + + xx = 0; + yy += size; + + if (yy > y) + { + return false; + } + } + } + + xx = 0; + yy += size; + } + + if (yy > y) + { + return false; + } + + return true; +} + +int main() +{ + int n; + std::cin >> n; + + while (n--) + { + int m; + std::cin >> m; + std::string paragraphs[m]; + + std::getline(std::cin, paragraphs[0]); + + for (int i = 0; i < m; i++) + { + std::getline(std::cin, paragraphs[i]); + } + + int x, y, max = 0; + std::cin >> x >> y; + + for (int i = 8; i <= 28; i++) + { + if (fit(i, paragraphs, m, x, y)) + { + max = i; + } + else + { + break; + } + } + + if (max != 0) + { + std::cout << max << std::endl; + } + else + { + std::cout << "No solution" << std::endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10660/10660-11.cpp b/uva_cpp_clean/10660/10660-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..914caa5bf841774a57c69fbcb6a081681a0df653 --- /dev/null +++ b/uva_cpp_clean/10660/10660-11.cpp @@ -0,0 +1,115 @@ +#include +/* +Problem: 10660 - Citizen attention offices +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1601 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int T, N; +int k = 25; + +int main () { + + scanf("%d\n", &T); + while ( T-- ){ + scanf("%d\n", &N); + + int I, J, P; + int areas[25][25] = {}; + + for (int i = 0; i < N; i++){ + scanf("%d %d %d\n", &I, &J, &P); + areas[I][J] = P; + } + + int total_sum = INT_MAX; + int offices[5] = {}; + + for (int a = 0; a < k - 4; a++) + for (int b = a+1; b < k - 3; b++) + for (int c = b+1; c < k - 2; c++) + for (int d = c+1; d < k - 1; d++) + for (int e = d+1; e < k - 0; e++){ + + int d_sum = 0; + int points[5] = {a, b, c, d, e}; + + for (int i = 0; i < k; i++){ + for (int j = 0; j < k; j++){ + if (!areas[i][j]) continue; + int min_dist = INT_MAX; + + for (int d = 0; d < 5; d++){ + int distance = abs(i - (points[d]-(points[d]%5))/5) + abs(j - points[d]%5); + + min_dist = (distance < min_dist)? distance : min_dist; + } + + d_sum += min_dist * areas[i][j]; + } + } + + if (d_sum < total_sum){ + total_sum = d_sum; + offices[0] = points[0]; + offices[1] = points[1]; + offices[2] = points[2]; + offices[3] = points[3]; + offices[4] = points[4]; + } + } + + printf("%d %d %d %d %d\n", offices[0], offices[1], offices[2], offices[3], offices[4]); + } + + return 0; +} + +/* +Sample input:- +----------------- +4 +1 +2 2 1 +4 +0 0 1 +4 4 1 +0 4 1 +4 0 1 +5 +0 0 1 +1 1 1 +2 2 1 +3 3 1 +4 4 1 +7 +4 2 2 +3 3 1 +2 4 3 +2 1 1 +1 3 4 +1 2 2 +1 0 1 + +Sample output:- +----------------- +0 1 2 3 12 +0 1 4 20 24 +0 6 12 18 24 +5 7 8 14 22 + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10660/10660-13.cpp b/uva_cpp_clean/10660/10660-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dc169f59436d051d56546cd93bd0a10f166085f --- /dev/null +++ b/uva_cpp_clean/10660/10660-13.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include + +using namespace std; + +ofstream fout("f.out"); + +#define MAX 1000000000 + +int n, test; +long long minAns; +int ans[5], town[25]; + +long long cal(int office, int area) +{ + long long result = 0; + + int officeX, officeY, areaX, areaY; + + officeX = office / 5; + officeY = office % 5; + + areaX = area / 5; + areaY = area % 5; + + result = (abs(areaX - officeX) + abs(areaY - officeY)) * town[area]; + + return result; +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n; + + bool first = true; + minAns = MAX; + memset(ans, 0, sizeof ans); + memset(town, 0, sizeof town); + + for (int i = 0; i < n; i++) + { + int x, y, pop; + + cin >> x >> y >> pop; + + town[(x * 5) + y] = pop; + } + + for (int a = 0; a < 25; a++) + { + for (int b = a + 1; b < 25; b++) + { + for (int c = b + 1; c < 25; c++) + { + for (int d = c + 1; d < 25; d++) + { + for (int e = d + 1; e < 25; e++) + { + int office[5] = { a, b, c, d, e }; + + long long minSum = 0; + + for (int i = 0; i < 25; i++) + { + if (town[i] > 0) + { + long long minDis = MAX; + + for (int j = 0; j < 5; j++) + { + minDis = min(minDis, cal(office[j], i)); + } + + minSum += minDis; + } + } + + if (minSum < minAns) + { + minAns = minSum; + + for (int k = 0; k < 5; k++) + { + ans[k] = office[k]; + } + } + } + } + } + } + } + + cout << ans[0]; + + for (int i = 1; i < 5; i++) + { + cout << " " << ans[i]; + } + + cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10660/10660-21.cpp b/uva_cpp_clean/10660/10660-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3074038724852914febcfdc7ab6485d87c899295 --- /dev/null +++ b/uva_cpp_clean/10660/10660-21.cpp @@ -0,0 +1,68 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10660 + Name: Citizen attention offices + Problem: https://onlinejudge.org/external/106/10660.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int D[6][5][5], R[30], C[30], P[30]; +void calcs(int p, int B[5][5], int A[5][5]) { + int r=p/5, c=p%5; + for (int i=0; i<5; ++i) + for (int j=0; j<5; ++j) + A[i][j] = min(B[i][j], abs(r-i)+abs(c-j)); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=0; i<5; ++i) + for (int j=0; j<5; ++j) + D[0][i][j] = (int)1e9; + + int T, n, r, c, p; + cin >> T; + while (T-- && cin>>n) { + for (int i=0; i> R[i] >> C[i] >> P[i]; + + char o[5], bo[5]; + int bsum = (int)1e9; + for (o[0]=0; o[0]<25; ++o[0]) { + calcs(o[0], D[0], D[1]); + for (o[1]=o[0]+1; o[1]<25; ++o[1]) { + calcs(o[1], D[1], D[2]); + for (o[2]=o[1]+1; o[2]<25; ++o[2]) { + calcs(o[2], D[2], D[3]); + for (o[3]=o[2]+1; o[3]<25; ++o[3]) { + calcs(o[3], D[3], D[4]); + for (o[4]=o[3]+1; o[4]<25; ++o[4]) { + calcs(o[4], D[4], D[5]); + + int sum = 0; + for (int i=0; i sum) { + bsum = sum; + memcpy(bo, o, 5); + } + } + } + } + } + } + cout << int(bo[0]); + for (int i=1; i<5; ++i) + cout << ' ' << int(bo[i]); + cout << '\n'; + } +} diff --git a/uva_cpp_clean/10660/10660-9.cpp b/uva_cpp_clean/10660/10660-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..519fd6670dfda6af7b564b53b7f74a5c9aa30c59 --- /dev/null +++ b/uva_cpp_clean/10660/10660-9.cpp @@ -0,0 +1,46 @@ +#include + +#define SIZE 8 + +using namespace std; + +int tc, n, r, c, v, grid[SIZE][SIZE], r1, c1, r2, c2, r3, c3, r4, c4, r5, c5, sum, _min, tmp, ans1, ans2, ans3, ans4, ans5; + +inline int dis(int x, int y, int X, int Y){ + return abs(X - x) + abs(Y - y); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + memset(grid, 0, sizeof grid); + scanf("%d", &n); + while(n--){ + scanf("%d %d %d", &r, &c, &v); + grid[r][c] = v; + } + _min = INT_MAX; + for(int p1 = 0; p1 < 25; p1++) for(int p2 = p1 + 1; p2 < 25; p2++) + for(int p3 = p2 + 1; p3 < 25; p3++) for(int p4 = p3 + 1; p4 < 25; p4++) + for(int p5 = p4 + 1; p5 < 25; p5++){ + r1 = p1 / 5, c1 = p1 % 5; + r2 = p2 / 5, c2 = p2 % 5; + r3 = p3 / 5, c3 = p3 % 5; + r4 = p4 / 5, c4 = p4 % 5; + r5 = p5 / 5, c5 = p5 % 5; + sum = 0; + for(int row = 0; row < 5; row++) + for(int col = 0; col < 5; col++){ + tmp = dis(row, col, r1, c1) * grid[row][col]; + tmp = min(tmp, dis(row, col, r2, c2) * grid[row][col]); + tmp = min(tmp, dis(row, col, r3, c3) * grid[row][col]); + tmp = min(tmp, dis(row, col, r4, c4) * grid[row][col]); + tmp = min(tmp, dis(row, col, r5, c5) * grid[row][col]); + sum += tmp; + } + if( sum < _min ) _min = sum, ans1 = p1, ans2 = p2, ans3 = p3, ans4 = p4, ans5 = p5; + } + printf("%d %d %d %d %d\n", ans1, ans2, ans3, ans4, ans5); + } + return(0); +} diff --git a/uva_cpp_clean/10662/10662-21.cpp b/uva_cpp_clean/10662/10662-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..489a88dc48768bbb14ea44ddf4935e377df2409e --- /dev/null +++ b/uva_cpp_clean/10662/10662-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10662 + Name: The Wedding + Problem: https://onlinejudge.org/external/106/10662.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MaxInt 2147483647 + +int T,R,H; +int cT[20],cR[20],cH[20]; +bool mT[20][20], mR[20][20], mH[20][20]; + +int main() +{ + int i,j,k; + int chT,chR,chH; + int bT,bR,bH,m; + + while(cin>>T>>R>>H) + { + for(i=0; i>cT[i]; + for(j=0; j>mT[i][j]; + } + for(i=0; i>cR[i]; + for(j=0; j>mR[i][j]; + } + for(i=0; i>cH[i]; + for(j=0; j>mH[i][j]; + } + + m=MaxInt; + for(k=0; k + +#define MAX 25 + +using namespace std; + +int T, R, H, TR[MAX][MAX], RH[MAX][MAX], HT[MAX][MAX]; +int ans_T, ans_R, ans_H, ans_P, p, _min; +bool ok; + +int main(){ + while(scanf("%d %d %d", &T, &R, &H) == 3){ + ok = false, _min = INT_MAX; + for(int i = 0; i < T; i++) + for(int j = 0; j <= R; j++) + scanf("%d", &TR[i][j]); + for(int i = 0; i < R; i++) + for(int j = 0; j <= H; j++) + scanf("%d", &RH[i][j]); + for(int i = 0; i < H; i++) + for(int j = 0; j <= T; j++) + scanf("%d", &HT[i][j]); + for(int t = 0; t < T; t++) + for(int r = 0; r < R; r++) + for(int h = 0; h < H; h++) + if(!TR[t][r + 1] && !RH[r][h + 1] && !HT[h][t + 1]){ + ok = true; + p = TR[t][0] + RH[r][0] + HT[h][0]; + if(p < _min) + _min = p, ans_T = t, ans_R = r, ans_H = h, ans_P = p; + } + if(ok) printf("%d %d %d:%d\n", ans_T, ans_R, ans_H, ans_P); + else printf("Don't get married!\n"); + } + return(0); +} diff --git a/uva_cpp_clean/10664/10664-14.cpp b/uva_cpp_clean/10664/10664-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4800b4c14b7de316379078679a8ab725e4cba803 --- /dev/null +++ b/uva_cpp_clean/10664/10664-14.cpp @@ -0,0 +1,152 @@ +/*************************************************** + * Problem Name : 10664 - Luggage.cpp + * Problem Link : https://uva.onlinejudge.org/external/106/10664.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-02-06 + * Problem Type : dp + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 205 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorcoin, cap; +int C[MAX]; +int n, need; +int dp[23][205][205]; + +int stringToNum (string str) { + int num = 0; + int sz = str.size(); + + for (int i = 0; i < sz; i++) { + num = (num * 10) + (str[i] - '0'); + } + + return num; +} + +int coin_change (int i, int make, int taken) { + if (i >= n) { + if (make == need) return 1; + else return 0; + } + + if (dp[i][taken][make] != -1) return dp[i][taken][make]; + + int ret1 = 0, ret2 = 0; + + if ( (make + coin[i] <= need) && taken <= cap[i]) { + ret1 = coin_change (i, make + coin[i], taken + 1); + } + + ret2 = coin_change (i + 1, make, 1); + return dp[i][taken][make] = (ret1 | ret2); +} +int main () { + __FastIO; + int tc; + cin >> tc; + string str; + cin.ignore(); + + while (tc--) { + vectorar; + string tmp; + getline (cin, str); + stringstream ss; + ss << str; + + while (ss >> tmp) { + ar.pb (stringToNum (tmp) ); + } + + setst; + SET (C, 0); + int sum = 0; + + for (int i = 0; i < (int) ar.size(); i++) { + int x = ar[i]; + C[x]++; + st.insert (x); + sum += x; + } + + for (auto it = st.begin(); it != st.end(); it++) { + int x = *it; + coin.pb (x); + x = C[x]; + cap.pb (x); + } + + if (sum % 2 == 1) { + cout << "NO\n"; + + } else { + n = (int) coin.size(); + need = sum / 2; + SET (dp, -1); + int flag = coin_change (0, 0, 1); + + if (flag == 1) { + cout << "YES\n"; + + } else { + cout << "NO\n"; + } + } + + coin.clear(), cap.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/10664/10664-19.cpp b/uva_cpp_clean/10664/10664-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eda782e8b74755d709c00600faed3d46d6e6944 --- /dev/null +++ b/uva_cpp_clean/10664/10664-19.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; + +int N, sum; +int weights[20]; +int DP[201][20]; + +bool proc(int r, int i) { + if (i >= N) return (r == sum - r); + else if (DP[r][i] == -1) { + DP[r][i] = (proc(r, i+1) || proc(r+weights[i], i+1))? 1: 0; + } + return DP[r][i]; +} + +int main() { + int T; + cin >> T; + string s; + getline(cin, s); + while (T--) { + getline(cin, s); + istringstream iss(s); + N = sum = 0; + int val; + while (iss >> val) weights[N] = val, N++, sum += val; + for (int j = 0; j < sum; j++) + for (int i = 0; i < N; i++) + DP[j][i] = -1; + if (proc(0, 0)) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10664/10664-21.cpp b/uva_cpp_clean/10664/10664-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bab2d3caecf9dc4c48474e186154aa3440071619 --- /dev/null +++ b/uva_cpp_clean/10664/10664-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10664 + Name: Luggage + Problem: https://onlinejudge.org/external/106/10664.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXL 10000 + +int A[22], n; +bool memo[414][22]; +bool mindiff(int dif, int ind) { + if (ind>=n) return !dif; + + int mid = 202+dif; + if (memo[mid][ind]) return false; + memo[mid][ind] = 1; + + if (mindiff(dif-A[ind], ind+1) || mindiff(dif+A[ind], ind+1)) + return true; + + return false; +} + + + +int main(){ + int T; + cin>>T; + + char line[MAXL]; + cin.getline(line, MAXL); + while (T--) { + cin.getline(line, MAXL); + stringstream sin(line); + + for (n=0; sin>>A[n]; n++); + memset(memo, 0, sizeof(memo)); + puts(mindiff(0, 0) ? "YES" : "NO"); + } +} diff --git a/uva_cpp_clean/10664/10664-9.cpp b/uva_cpp_clean/10664/10664-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a30ab300ec2cf937db7cc750926c1abcafd09f5a --- /dev/null +++ b/uva_cpp_clean/10664/10664-9.cpp @@ -0,0 +1,32 @@ +#include + +#define MAX_N 30 +#define MAX_W 210 + +using namespace std; + +int tc,W[MAX_N],dp[MAX_N][MAX_W],n,sum; +char s[200],*p; +bool ok; + +int main(){ + scanf("%d\n",&tc); + for(int t=1;t<=tc;t++){ + ok = false, n = sum = 0; + cin.getline(s,200); + p = strtok(s," "); + while(p) n++, W[n] = atoi(p), sum += W[n], p = strtok(NULL," "); + if((sum&1)==0){ + sum >>= 1; + for(int i=0;i<=n;i++) dp[i][0] = 0; + for(int w=0;w<=sum;w++) dp[0][w] = 0; + for(int i=1;i<=n;i++) + for(int w=1;w<=sum;w++) + if(W[i]>w) dp[i][w] = dp[i-1][w]; + else dp[i][w] = max(dp[i-1][w],W[i]+dp[i-1][w-W[i]]); + ok = (dp[n][sum]==sum); + } + puts( ok ? "YES" : "NO"); + } + return(0); +} diff --git a/uva_cpp_clean/10667/10667-19.cpp b/uva_cpp_clean/10667/10667-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f02a3022b801c28862e489286aa799d8d69e05bf --- /dev/null +++ b/uva_cpp_clean/10667/10667-19.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +using namespace std; + +pair grid[100][100]; +pair bound[100][100]; +int s; +int maxi; + +void proc(){ + int ac = 1; + for (int x = 0; x < s; x++) { + if (grid[x][0].first == -1) { + grid[x][0].first = ac; + grid[x][0].second = 1; + bound[x][0].first = ac; + bound[x][0].second = 1; + if (ac > maxi) maxi = ac; + ac++; + } else ac = 1; + } + ac = 1; + for (int y = 0; y < s; y++) { + if (grid[0][y].first == -1 || grid[0][y].first == 1) { + grid[0][y].first = 1; + grid[0][y].second = ac; + bound[0][y].first = 1; + bound[0][y].second = ac; + if (ac > maxi) maxi = ac; + ac++; + } else ac = 1; + } + for (int x = 1; x < s; x++) { + for (int y = 1; y < s; y++) { + if (grid[x][y].first == -1) { + bound[x][y].first = bound[x-1][y].first + 1; + bound[x][y].second = bound[x][y-1].second + 1; + if (grid[x-1][y].first == 0) { + grid[x][y].first = 1; + grid[x][y].second = bound[x][y].second; + } else if (grid[x][y-1].first == 0) { + grid[x][y].first = bound[x][y].first; + grid[x][y].second = 1; + } else { + int x1 = grid[x-1][y].first + 1, y1 = min(grid[x-1][y].second, bound[x][y].second), x2 = min(grid[x][y-1].first, bound[x][y].first), y2 = grid[x][y-1].second + 1; + if ((x1*y1) > (x2*y2)) { + grid[x][y].first = x1; + grid[x][y].second = y1; + } else { + grid[x][y].first = x2; + grid[x][y].second = y2; + } + } + if (grid[x][y].first * grid[x][y].second > maxi) maxi = grid[x][y].first * grid[x][y].second; + } + } + } +} + +int main(){ + int cases; + scanf("%d\n", &cases); + for (int i = 0; i < cases; i++) { + int blocks; + maxi = 0; + scanf("%d\n%d", &s, &blocks); + for (int x = 0; x < s; x++) { + for (int y = 0; y < s; y++) { + grid[x][y].first = -1; + grid[x][y].second = -1; + } + } + for (int j = 0; j < blocks; j++) { + int r1, r2, c1, c2; + scanf("%d %d %d %d", &r1, &c1, &r2, &c2); + for (int x = r1-1; x <= r2-1; x++) { + for (int y = c1-1; y <= c2-1; y++) { + grid[x][y].first = 0; + grid[x][y].second = 0; + bound[x][y].first = 0; + bound[x][y].second = 0; + } + } + } + proc(); + printf("%d\n", maxi); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10667/10667-21.cpp b/uva_cpp_clean/10667/10667-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad58ee652bdce0dede0d4240054aa1676b4558ab --- /dev/null +++ b/uva_cpp_clean/10667/10667-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10667 + Name: Largest Block + Problem: https://onlinejudge.org/external/106/10667.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + + bool B[143][143]; + int T, n, m, C[143]; + cin >> T; + while (T--) { + cin >> n >> m; + for (int i=0; i> r1 >> c1 >> r2 >> c2; + for(int i=r1-1; i + +using namespace std; + +int tc, r1, r2, c1, c2, n, b, ans, area, v[105][105]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + memset(v, 0, sizeof v); + scanf("%d", &n); + scanf("%d", &b); + while(b--){ + scanf("%d %d %d %d", &r1, &c1, &r2, &c2); + for(int i = r1; i <= r2; i++) + for(int j = c1; j <= c2; j++) v[i][j] = 1; + } + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) + v[i][j] += v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1]; + ans = 0; + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) + for(int ii = i; ii <= n; ii++) + for(int jj = j; jj <= n; jj++){ + if((ii - i + 1) * (jj - j + 1) > ans){ + area = v[ii][jj] - v[ii][j - 1] - v[i - 1][jj] + v[i - 1][j - 1]; + if(area == 0) ans = (ii - i + 1) * (jj - j + 1); + } + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10668/10668-21.cpp b/uva_cpp_clean/10668/10668-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65aa39bb7a04309e247194600febccbe148a77a7 --- /dev/null +++ b/uva_cpp_clean/10668/10668-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10668 + Name: Expanding Rods + Problem: https://onlinejudge.org/external/106/10668.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +const double PI = acos(-1); +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(3); + + double l, t, c, lp; + while (cin >> l >> t >> c && l>=0) { + lp = (1 + t*c) * l; + if (fabs(lp-l) < 1e-6) cout << 0.0 << endl; + else { + double lo=0, hi=PI; + while (hi-lo > 1e-13) { + double a = (lo + hi) / 2; + if (a*l/(2*sin(a/2)) < lp) + lo = a; + else + hi = a; + } + + double r = lp/lo; + cout << (r - r*cos(lo/2)) << '\n'; + } + } +} diff --git a/uva_cpp_clean/10669/10669-21.cpp b/uva_cpp_clean/10669/10669-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e404b40088970fc24ad89ea4d370028d5ffc3638 --- /dev/null +++ b/uva_cpp_clean/10669/10669-21.cpp @@ -0,0 +1,212 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10669 + Name: Three powers + Problem: https://onlinejudge.org/external/106/10669.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + + int capacity, size, sign; + CellType *cells; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + + if (cells) delete [] cells; + cells = ncells; + } + + if (!cp) + memset(cells, 0, capacity*CellSize); + else memset(cells+sz, 0, (capacity-sz)*CellSize); + return size = sz; + } + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + // --------------------------------------------------------------------- + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + + BigInt(const BigInt& o): cells(0), capacity(0) { + sign = o.sign; + setSize(o.size, 0); + for (int i=0; i= 0) + delete [] cells; + } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + // --------------------------------------------------------------------- + + BigInt& operator*= (int x) { + sign *= sgn(x); + if (!sign) { + size=sign=0; + return *this; + } + if (x<0) x = -x; + + int cr = 0; + for (int i=0; i < size; i++) { + cells[i] = cells[i] * x + cr; + cr = cells[i] / MAX_C; + cells[i] %= MAX_C; + } + + if (cr) { + int ex = (countDigits(cr)+MAX_D-1)/MAX_D, sz=size; + setSize(size + ex); + size = sz; + for (; cr; cr /= MAX_C) + cells[size++] = cr % MAX_C; + } + + return autoSize(); + } + BigInt operator* (const BigInt &o) const { + if (sign*o.sign == 0) + return *new BigInt(0); + if (o.size == 1) + return *new BigInt(*this) *= o.cells[0]; + if (size == 1) + return *new BigInt(o) *= cells[0]; + + BigInt r(*this); + r.setSize(r.size + o.size + 1, 0); + for (int i=0; i=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; + + +bool powseen[10][10000]; +BigInt pmemo[10][10000]; +BigInt pow(int a, int b) { + if (!b) return BigInt(1); + if (b==1 || a<=1) + return BigInt(a); + + if (a<10 && b<10000 && powseen[a][b]) + return pmemo[a][b]; + + BigInt c = pow(a, b>>1); + c *= c; + if (b&1) c *= a; + + if (a<10 && b<10000) { + powseen[a][b] = 1; + pmemo[a][b] = c; + } + return c; +} + + +int main(){ + unsigned long long n; + while (scanf("%llu", &n)==1 && n) { + if (--n == 0) + puts("{ }"); + else { + printf("{ "); + bool frst = 1; + for (int i=0; n; i++, n>>=1) + if (n & 1) { + if (frst) frst = 0; + else printf(", "); + cout << pow(3, i); + } + printf(" }\n"); + } + } +} diff --git a/uva_cpp_clean/10670/10670-21.cpp b/uva_cpp_clean/10670/10670-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..736c5b76767f06d946301ab6f33f2723baa42a02 --- /dev/null +++ b/uva_cpp_clean/10670/10670-21.cpp @@ -0,0 +1,58 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10670 + Name: Work Reduction + Problem: https://onlinejudge.org/external/106/10670.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +struct agency{ + int a,b; + int price; + string name; + bool operator<(const agency &b)const{ + if(price!=b.price) + return price list; + set::iterator o; + agency k; + string l2; + scanf("%d",&nn); + for(nni=1;nni<=nn;nni++){ + list.clear(); + printf("Case %d\n",nni); + scanf("%d%d%d%*1c",&S,&T,&L); + for(i=0;i=T && ceil(j/2.0)*a>b;j/=2,c+=b); + c+=(j-T)*a; + k.a=a; + k.b=b; + k.price=c; + k.name.assign(line); + list.insert(k); + } + for(o=list.begin();o!=list.end();o++) + cout<name<<" "<price<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10672 + Name: Marbles on a tree + Problem: https://onlinejudge.org/external/106/10672.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +list childs[10001]; +int par[10001], tush[10001]; + +int findRoot(int n) { + for (int i=0; i>n && n) { + for (int i=0; i> u; u--; + cin >> tush[u] >> m; + for (int i=0; i> v; v--; + childs[u].push_back(v); + par[v] = u; + } + } + + int root = findRoot(n); + cout << dfs(root) << endl; + } +} diff --git a/uva_cpp_clean/10673/10673-13.cpp b/uva_cpp_clean/10673/10673-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc9f1226962585204d0e5f15012aee480082965b --- /dev/null +++ b/uva_cpp_clean/10673/10673-13.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; + +typedef long long ll; + +ll c, k, x, y, d; + +void extendedEuclid(ll a, ll b) +{ + if (b == 0) + { + x = 1; + y = 0; + d = a; + return; + } + + extendedEuclid(b, a % b); + + ll x1 = y; + ll y1 = x - (a / b) * y; + x = x1; + y = y1; +} + +int main() +{ + int t; + + cin >> t; + + while (t--) + { + cin >> c >> k; + + extendedEuclid(floor((double)c / k), ceil((double)c / k)); + + x *= (c / d); + y *= (c / d); + + cout << x << " " << y << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10673/10673-20.cpp b/uva_cpp_clean/10673/10673-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b39e850a65ac8f2f50e326ed2da80bd020bc59d --- /dev/null +++ b/uva_cpp_clean/10673/10673-20.cpp @@ -0,0 +1,28 @@ +#include +#include + +int main() +{ + int tc; + + scanf("%d", &tc); + while( tc-- ) + { + long long x, k; + scanf("%lld %lld", &x, &k); + + long long a = floor((double) x/k); + long long b = ceil((double) x/k); + + long long p, q; + for(p=0 ; p*a<=x ; p++) + if( (x-p*a)%b==0 ) + { + q = (x-p*a)/b; + break; + } + + printf("%lld %lld\n", p, q); + } + return 0; +} diff --git a/uva_cpp_clean/10673/10673-21.cpp b/uva_cpp_clean/10673/10673-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37795285b99e5359000e3ac8617f80e73e13e1bd --- /dev/null +++ b/uva_cpp_clean/10673/10673-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10673 + Name: Play with Floor and Ceil + Problem: https://onlinejudge.org/external/106/10673.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct Triple { + int d, x, y; + Triple(int q, int w, int e):d(q),x(w),y(e) {} +}; + +Triple egcd(int a, int b) { + if (!b) return Triple(a, 1, 0); + Triple q = egcd(b, a % b); + return Triple(q.d, q.y, q.x - a/b*q.y ); +} + +Triple ldioph(int a, int b, int c) { + Triple t = egcd(a, b); + if (c % t.d) return Triple(0, 0, 0); + t.x *= c / t.d; t.y *= c / t.d; + return t; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, x, k; + cin >> T; + while (T-- && cin >> x >> k) { + int a = x / k, + b = (x + k - 1) / k; + + Triple r = ldioph(a, b, x); + cout << r.x << ' ' << r.y << endl; + } +} diff --git a/uva_cpp_clean/10673/10673-9.cpp b/uva_cpp_clean/10673/10673-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea9bef9ad7cb82b4b07ca5a31be86d0178b5d726 --- /dev/null +++ b/uva_cpp_clean/10673/10673-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int tc, x, k, xo, yo, d; + +void extendedEuclid(int a,int b){ + if( b == 0 ){ + xo = 1, yo = 0, d = a; + return; + } + extendedEuclid(b, a % b); + int x1 = yo, y1 = xo - (a / b) * yo; + xo = x1, yo = y1; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &x, &k); + extendedEuclid(x / k, (x + k - 1) / k); + printf("%lld %lld\n", 1LL * xo * ( x / d ), 1LL * yo * ( x / d )); + } + return(0); +} diff --git a/uva_cpp_clean/10678/10678-19.cpp b/uva_cpp_clean/10678/10678-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd326fc4634e119018f9ec509fc08e0a619a8b2d --- /dev/null +++ b/uva_cpp_clean/10678/10678-19.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +double PI = 2*acos(0); + +int main() { + int N; + cin >> N; + while (N--) { + double D, L; + cin >> D >> L; + D /= 2; L /= 2; + double b = sqrt(L*L - D*D); + printf("%.3lf\n", PI*b*L); + } + return 0; +} diff --git a/uva_cpp_clean/10678/10678-21.cpp b/uva_cpp_clean/10678/10678-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..767195d1ff25e74c0b3d1ed0df3b8429258d2a77 --- /dev/null +++ b/uva_cpp_clean/10678/10678-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10678 + Name: The Grazing Cow + Problem: https://onlinejudge.org/external/106/10678.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +const double PI = acos(-1); +int main() { + cout << fixed << setprecision(3); + + int T; + cin >> T; + double d, l; + while (T-- && cin >> d >> l) + cout << PI*l*sqrt(l*l-d*d)/4 << '\n'; +} diff --git a/uva_cpp_clean/10678/10678-5.cpp b/uva_cpp_clean/10678/10678-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6379b58ef089ac96117a2c723852c4ea499eec40 --- /dev/null +++ b/uva_cpp_clean/10678/10678-5.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + + unsigned long long i; + cin>>t; + while(t--){ + double x,y; + cin>>x>>y; + double t; + t=sqrt((y/2)*(y/2)-(x/2)*(x/2)); + printf("%.3lf\n",pi*y/2*t); + + } + + +return 0; +} diff --git a/uva_cpp_clean/10678/10678-9.cpp b/uva_cpp_clean/10678/10678-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92de406e4b59800618b0a46efc3a3ec7992ad0d1 --- /dev/null +++ b/uva_cpp_clean/10678/10678-9.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +const double PI = acos(-1.0); + +int tc; +double D, L; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%lf %lf", &D, &L); + printf("%.3lf\n", PI * L * sqrt( L * L - D * D ) / 4.0 ); + } + return(0); +} diff --git a/uva_cpp_clean/10679/10679-21.cpp b/uva_cpp_clean/10679/10679-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e15ada2b2ac25cfe76867c689189be0a94893129 --- /dev/null +++ b/uva_cpp_clean/10679/10679-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10679 + Name: I Love Strings!! + Problem: https://onlinejudge.org/external/106/10679.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +// KMP +int lps[1001]; +int find(const char str[], const char pat[]) { + int pl, l=0; + lps[0] = 0; + for (pl=1; pat[pl];) + if (pat[pl] == pat[l]) + lps[pl++] = ++l; + else if (l>0) + l = lps[l-1]; + else + lps[pl++] = 0; + + + int j=0; + for (int i=0; str[i];) + if (str[i] == pat[j]) { + i++; j++; + if (j == pl) return i-pl; + } + else if (j > 0) j = lps[j-1]; + else i++; + + return -1; +} + + +int main(){ + char s[100001], p[1001]; + int T, q; + cin>>T; + while (T--) { + cin >> s >> q; + for (int i=0; i> p; + puts(find(s, p) < 0 ? "n" : "y"); + } + } +} diff --git a/uva_cpp_clean/10680/10680-13.cpp b/uva_cpp_clean/10680/10680-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58e261c953045b7875df58e10e9d1ddaeabf08eb --- /dev/null +++ b/uva_cpp_clean/10680/10680-13.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 20000001 + +typedef long long ll; +typedef vector vll; +typedef vector vb; + +vll primes; +vb isPrime(MAX, true); + +void findPrime() +{ + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += i) + { + isPrime[j] = false; + } + + primes.push_back(i); + } + } + + swap(primes[1], primes[2]); +} + +int main() +{ + ll n, ans; + + findPrime(); + + while (cin >> n && n != 0) + { + ans = 1; + + ll count_2 = 0, count_5 = 0; + + for (ll i = 2; i <= n; i *= 2) + { + count_2++; + } + + for (ll j = 5; j <= n; j *= 5) + { + count_5++; + } + + for (ll k = 0; k < (count_2 - count_5) ; ++k) + { + ans = ((ans * 2) % 10); + } + + for (ll l = 2; l < primes.size(); ++l) + { + ll p = primes[l]; + + if (p > n) break; + + else + { + for (ll i = p; i <= n ; i *= p) + { + ans = ((ans * p) % 10); + } + } + } + + cout << ans << endl; + } +} + diff --git a/uva_cpp_clean/10680/10680-19.cpp b/uva_cpp_clean/10680/10680-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d0ef5f13e16d855aad20bbef0d8ae2fb3e87a96 --- /dev/null +++ b/uva_cpp_clean/10680/10680-19.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef unordered_map mii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +void sieve(long long upperbound = 1000001) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +mii primeFactors(ll n) { + mii factors; int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.insert(ii(p,c)); p=primes[++idx]; + } + if (n!=1) factors.insert(ii(n,1)); + return factors; +} + + +int main() { + sieve(); + mii last; + long long values[1000002]; + values[0] = 0; + values[1] = 1; + for (int i = 2; i <= 1000000; i++) { + values[i] = values[i-1]; + mii auxi = primeFactors(i); + for (auto it = auxi.begin(); it != auxi.end(); ++it) { + int ex = last[it->first]; + if (it->second > ex) { + last[it->first] = it->second; + values[i] *= pow(it->first, it->second - ex); + while (values[i] % 10 == 0) values[i] /= 10; + values[i] %= 10000000; + } + } + values[i-1] %= 10; + } + values[1000000] %= 10; + int N; + while (scanf("%d", &N) && N != 0) { + printf("%lld\n", values[N]); + } + return 0; +} diff --git a/uva_cpp_clean/10680/10680-9.cpp b/uva_cpp_clean/10680/10680-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cacd49a367db48da8000a75f9ba3e9954dd2e20 --- /dev/null +++ b/uva_cpp_clean/10680/10680-9.cpp @@ -0,0 +1,23 @@ +#include + +#define SIZE 1000010 + +using namespace std; + +long long table[SIZE]; +int n; +const int MOD = 1000; + +int main(){ + for(int it = 1; it < SIZE; it++) table[it] = it; + for(int it1 = 2; it1 < SIZE; it1++) + for(int it2 = 2 * it1; it2 < SIZE; it2 += it1) + table[it2] /= table[it1]; + for(int it = 2; it < SIZE; it++){ + table[it] *= table[it - 1]; + while( table[it] % 10 == 0 ) table[it] /= 10; + table[it] %= MOD; + } + while(scanf("%d",&n), n) + printf("%lld\n", table[n] % 10 ); +} diff --git a/uva_cpp_clean/10681/10681-21.cpp b/uva_cpp_clean/10681/10681-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7454e4f9147ee9b83f089e4ba8fc23faafacc326 --- /dev/null +++ b/uva_cpp_clean/10681/10681-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10681 + Name: Teobaldo's Trip + Problem: https://onlinejudge.org/external/106/10681.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int n, m, s, e, d; +vector adj[143]; +bool seen[143][286]; +bool dfs(int u, int depth) { + seen[u][depth] = true; + if (depth == d) + return u == e-1; + + for (int v: adj[u]) + if (!seen[v][depth+1]) + if (dfs(v, depth+1)) + return true; + + return false; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n >> m && (n||m)) { + for (int i=0; i> u >> v; + adj[u-1].push_back(v-1); + adj[v-1].push_back(u-1); + } + + cin >> s >> e >> d; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10683 + Name: The decadary watch + Problem: https://onlinejudge.org/external/106/10683.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +typedef long double ld; +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int hh, mm, ss, cc; + while (scanf("%2d%2d%2d%2d", &hh, &mm, &ss, &cc)==4) { + long long t = hh; + t = t * 60 + mm; + t = t * 60 + ss; + t = t * 100+ cc; + t = t * 10; + + hh = t / 8640000; t %= 8640000; + mm = t / 86400; t %= 86400; + ss = t / 864; t %= 864; + cc = 100 * t / 864; + printf("%d%02d%02d%02d\n", hh, mm, ss, cc); + } +} diff --git a/uva_cpp_clean/10684/10684-13.cpp b/uva_cpp_clean/10684/10684-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f46e72a5514b1c9137f528737ce3fc1b65d2bcb3 --- /dev/null +++ b/uva_cpp_clean/10684/10684-13.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int kadane1D(int *a, int n) +{ + int ans = -10000000, sum = 0; + + for (int i = 0; i < n; i++) + { + sum += a[i]; + + ans = max(ans, sum); + + if (sum < 0) + { + sum = 0; + } + } + + return ans; +} + +int main() +{ + int n; + + cin >> n; + + while (n != 0) + { + int* a = new int[n]; + + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + + int result = kadane1D(a, n); + + if (result > 0) + { + cout << "The maximum winning streak is " << result << "." << endl; + } + + else + { + cout << "Losing streak." << endl; + } + + cin >> n; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10684/10684-14.cpp b/uva_cpp_clean/10684/10684-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..35e7df0d28769baa11f6e1680178b084cf05ad24 --- /dev/null +++ b/uva_cpp_clean/10684/10684-14.cpp @@ -0,0 +1,78 @@ +/*************************************************** + * Problem name : 10684 The jackpot.cpp + * Problem Link : https://uva.onlinejudge.org/external/106/10684.pdf + * OJ : Uva + * Verdict : AC + * Date : 31.05.2017 + * Problem Type : Maximum Sum + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 10005 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} + +int kadane(int ar[], int N) { + int sum = 0, mx = INT_MIN; + for (int i = 0; i < N; i++) { + sum = sum + ar[i]; + if (sum < 0) sum = 0; + if (mx < sum) mx = sum; + } + return mx; +} +int main () { + int N, ar[MAX]; + while (scanf("%d", &N) == 1 && N != 0) { + for (int i = 0; i < N; i++) { + scanf("%d", &ar[i]); + } + int mx = kadane(ar, N); + if (mx > 0) { + printf("The maximum winning streak is %d.\n", mx); + } else { + printf("Losing streak.\n"); + } + } + return 0; +} + diff --git a/uva_cpp_clean/10684/10684-18.cpp b/uva_cpp_clean/10684/10684-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3a4dfb702578a80711e5ddc7b280f50ab1d2c418 --- /dev/null +++ b/uva_cpp_clean/10684/10684-18.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main(){ + int n; + while(scanf("%d", &n) && n != 0){ + int input[n + 1], sum = 0, sumA = 0; + for(int i = 0; i < n; i++) scanf("%d", &input[i]); + for(int i = 0; i < n; i++){ + sumA += input[i]; + if (sumA > sum) sum = sumA; + if (sumA < 0) sumA = 0; + } + if(sum > 0) printf("The maximum winning streak is %d.\n", sum); + else printf("Losing streak.\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10684/10684-19.cpp b/uva_cpp_clean/10684/10684-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c1436a89b916f3146977c3acc96295ee7af9887 --- /dev/null +++ b/uva_cpp_clean/10684/10684-19.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +int main() { + int N; + scanf("%d", &N); + while (N != 0) { + int current = 0, best = 0; + for (int i = 0; i < N; i++) { + int bet; + scanf("%d", &bet); + if (bet > current + bet) current = bet; + else current += bet; + if (current > best) best = current; + } + if (best == 0) printf("Losing streak.\n"); + else printf("The maximum winning streak is %d.\n", best); + scanf("%d", &N); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10684/10684-21.cpp b/uva_cpp_clean/10684/10684-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35fc0f6d890c4c286a3b79b30346c94d9667f7ce --- /dev/null +++ b/uva_cpp_clean/10684/10684-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10684 + Name: The jackpot + Problem: https://onlinejudge.org/external/106/10684.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n; + while (cin>>n && n) { + int maxs = 0, sum=0, x; + while (n--) { + cin >> x; + sum += x; + if (sum < 0) sum = 0; + if (sum > maxs) maxs = sum; + } + + if (maxs > 0) + printf("The maximum winning streak is %d.\n", maxs); + else + printf("Losing streak.\n"); + } +} diff --git a/uva_cpp_clean/10684/10684-4.cpp b/uva_cpp_clean/10684/10684-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a33365f798ca66a3583c7dc4a8df1a3f88c42d6 --- /dev/null +++ b/uva_cpp_clean/10684/10684-4.cpp @@ -0,0 +1,50 @@ +#include +#include + +int main() +{ + while (true) + { + int n; + std::cin >> n; + + if (n == 0) + { + break; + } + + int streak = 0, sum; + std::vector bets; + + for (int i = 0; i < n; i++) + { + int bet; + std::cin >> bet; + bets.push_back(bet); + } + + for (int i = 0; i < n; i++) + { + sum = 0; + + for (int j = i; j < n; j++) + { + sum += bets[j]; + + if (sum > streak) + { + streak = sum; + } + } + } + + if (streak <= 0) + { + std::cout << "Losing streak." << std::endl; + } + else + { + std::cout << "The maximum winning streak is " << streak << "." << std::endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10684/10684-5.cpp b/uva_cpp_clean/10684/10684-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..692ceb89553b843e1a532432c5444169b840f929 --- /dev/null +++ b/uva_cpp_clean/10684/10684-5.cpp @@ -0,0 +1,176 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int main() +{ + int n; + while(si(n)==1) + { + if(n==0) break; + ll sum=0,res=0; + for0(i,n) + { + int a; + si(a); + sum += a; + if(sum<0) + { + sum=0; + } + res = max(res,sum); + + } + if(res<=0) + puts("Losing streak."); + else + printf("The maximum winning streak is %lld.\n",res); + + } + return 0; +} diff --git a/uva_cpp_clean/10684/10684-6.cpp b/uva_cpp_clean/10684/10684-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bf6f4b59659de13db8e1a808a2299a47ee340e6 --- /dev/null +++ b/uva_cpp_clean/10684/10684-6.cpp @@ -0,0 +1,32 @@ +/* + dynamic programming > max 1D range sum > kadane + difficulty: easy + date: 17/Jan/2020 + by: @brpapa +*/ +#include +#include +#define INF (int)0x3f3f3f3f +using namespace std; + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + while (true) { + int N; cin >> N; + if (!N) break; + + vector arr(N); + for (int &a : arr) cin >> a; + + int ans = -INF, sumAcc = 0; + for (int i = 0; i < N; i++) { + sumAcc = max(arr[i], sumAcc+arr[i]); + ans = max(ans, sumAcc); + } + if (ans > 0) cout << "The maximum winning streak is " << ans; + else cout << "Losing streak"; + cout << ".\n"; + } + return 0; +} diff --git a/uva_cpp_clean/10684/10684-9.cpp b/uva_cpp_clean/10684/10684-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cc19c46e5f432083315ad962c266f3d18d38c97 --- /dev/null +++ b/uva_cpp_clean/10684/10684-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int n, ans, sum, v; + +int main(){ + while(scanf("%d",&n), n){ + ans = sum = 0; + for(int i = 0; i < n; i++){ + scanf("%d", &v); + sum += v; + if(sum > ans) ans = sum; + if(sum < 0) sum = 0; + } + if(sum == 0) puts("Losing streak."); + else printf("The maximum winning streak is %d.\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10685/10685-10.cpp b/uva_cpp_clean/10685/10685-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/uva_cpp_clean/10685/10685-10.cpp differ diff --git a/uva_cpp_clean/10685/10685-14.cpp b/uva_cpp_clean/10685/10685-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..05a5b5645a85796d41168160b168cae19e816c52 --- /dev/null +++ b/uva_cpp_clean/10685/10685-14.cpp @@ -0,0 +1,114 @@ +/*************************************************** + * Problem name : 10685 Nature.cpp + * Problem Link : https://uva.onlinejudge.org/external/106/10685.pdf + * OJ : Uva + * Verdict : AC + * Date : 11.06.2017 + * Problem Type : Union Find + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 5005 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +mapmp; +int par[MAX], gm[MAX],mx; +void makeSet(int u) { + par[u] = u; +} +int findRepresentative(int r) { + if (par[r] == r) { + return r; + } else { + par[r] = findRepresentative(par[r]); + } + return par[r]; +} +void setUnion(int a, int b) { + int u = findRepresentative(a); + int v = findRepresentative(b); + int r = gm[u]; + int rr = gm[v]; + if (u != v) { + par[u] = v; + int x = findRepresentative(u); + int ans = r + rr; + mx = max(mx,ans); + gm[x] = ans; + } + mx = max(mx,rr); +} +void initUnion(int N) { + for (int i = 1; i <= N; i++) { + makeSet(i); + } +} +int main () { + int C, R; + while (scanf("%d %d", &C, &R) == 2) { + if(C == 0 && R == 0) { + break; + } + mx = 1; + for (int i = 1; i <= C; i++) { + string s; + cin >> s; + mp[s] = i; + gm[i] = 1; + } + initUnion(C); + for (int i = 1; i <= R; i++) { + string a , b; + cin >> a >> b; + setUnion(mp[a], mp[b]); + } + //~ int mx = 0; + //~ for (int i = 1; i <= C; i++) { + //~ mx = max(mx, gm[i]); + //~ } + printf("%d\n", mx); + mp.clear(); + //~ nl; + } + return 0; +} diff --git a/uva_cpp_clean/10685/10685-21.cpp b/uva_cpp_clean/10685/10685-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afe434139bb54e234ca7a167322f720221fe4015 --- /dev/null +++ b/uva_cpp_clean/10685/10685-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10685 + Name: Nature + Problem: https://onlinejudge.org/external/106/10685.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int gid[6000], gcn[6000]; +int find(int u) { + return u==gid[u] ? u : gid[u]=find(gid[u]); +} +int join(int u, int v) { + u=find(u); v=find(v); + if (u != v) { + gid[u] = v; + gcn[v] += gcn[u]; + } + return gcn[v]; +} + +int main() { + int n, m; + char w1[100], w2[100]; + while (scanf("%d %d", &n, &m)==2 && (n||m)) { + map ids; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int par[mx5]; + +int Find(int n) +{ + if(par[n]==n) + return n; + return par[n]=Find(par[n]); +} + +int Union(int a,int b) +{ + int u= Find(a); + int v= Find(b); + if(u!=v) + { + par[u]=v; + } + +} + +int main() +{ + int n,m; + while(sii(n,m)==2) + { + if(n==0 && m==0) break; + + string s,s1; + int cnt=0; + msi mp; + for0(i,n) + { + cin>>s; + mp[s]=cnt++; + par[i]=i; + } + + while(m--) + { + cin>>s>>s1; + int a=mp[s],b=mp[s1]; + Union(a,b); + } + + mii m; + for0(i,n) + { + m[ Find(i) ]++; + } + + int res=0; + forstl(it,m) + { + int p= it->se; + res =max(res,p); + } + + P(res); + + } + + return 0; +} diff --git a/uva_cpp_clean/10685/10685-6.cc b/uva_cpp_clean/10685/10685-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..e109827fcd05484175b8d58a677e9d87035ed41b --- /dev/null +++ b/uva_cpp_clean/10685/10685-6.cc @@ -0,0 +1,45 @@ +// https://uva.onlinejudge.org/external/106/10685.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +using msi=unordered_map; +int main(){ + int n,m,u,v; + while(1){ + cin>>n>>m; + if(!n)break; + vvi g(n); + int k=0; + msi d; + for(int i=0;i>s; + d[s]=k++; + } + for(int i=0;i>s>>t; + u=d[s];v=d[t]; + g[u].push_back(v); + g[v].push_back(u); + } + vi c(n); + functiondfs=[&](int u,int k){ + c[u]=k; + for(int v:g[u]) + if(!c[v]) + dfs(v,k); + }; + k=1; + for(int i=0;i + +using namespace std; + +struct UnionFind{ + int numSets, largestChain; + vector path, rank, setSize; + + UnionFind(int N_): numSets(N_), largestChain(1){ + rank.assign(N_, 0); + path.assign(N_, 0); + setSize.assign(N_, 1); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + numSets--; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x, setSize[x] += setSize[y]; + else path[x] = y, setSize[y] += setSize[x]; + if( rank[x] == rank[y] ) rank[y]++; + largestChain = max(largestChain, (int)max(setSize[x], setSize[y])); + } + + int sizeOfSet(int idx){ + return setSize[ findSet(idx) ]; + } +}; + +const int LIM = 35; +int C, R; +char word[LIM], s1[LIM], s2[LIM]; +map < string, int > mp; + +int main(){ + while(scanf("%d %d\n", &C, &R), C | R){ + UnionFind UF(C); + for(int it = 0; it < C; it++){ + scanf("%s", word); + mp[(string)word] = it; + } + for(int it = 0; it < R; it++){ + scanf("%s %s", s1, s2); + UF.unionSet(mp[(string)s1], mp[(string)s2]); + } + printf("%d\n", UF.largestChain); + } + return(0); +} diff --git a/uva_cpp_clean/10686/10686-21.cpp b/uva_cpp_clean/10686/10686-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fee51764277c3a950fc85e6291391a1d61b6203 --- /dev/null +++ b/uva_cpp_clean/10686/10686-21.cpp @@ -0,0 +1,120 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10686 + Name: SQF Problems + Problem: https://onlinejudge.org/external/106/10686.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + + +char toid(char ch) { return ch<='Z' ? ch-'A' : ch-'a'+26; } + +struct Trie { + int *vals, vc; + Trie *nxt[52]; + Trie():vals(0), vc(0) { memset(nxt, 0, sizeof(nxt)); } + ~Trie() { + if (vals) delete [] vals; + for (int i=0; i<52; ++i) + if (nxt[i]) + delete nxt[i]; + } +}; + +Trie *root; +void setVal(char str[], int val) { + Trie *cur = root; + for (int i=0; str[i]; ++i) { + char ch = toid(str[i]); + if (!cur->nxt[ch]) + cur->nxt[ch] = new Trie(); + cur = cur->nxt[ch]; + } + if (!cur->vals) cur->vals = new int[31]; + cur->vals[cur->vc++] = val; +} + +Trie* getVal(char str[]) { + Trie *cur = root; + for (int i=0; str[i]; ++i) { + char ch = toid(str[i]); + cur = cur->nxt[ch]; + if (!cur) return NULL; + } + return cur->vc ? cur : NULL; +} + +// ----------------------------------------------------------------------------------------- + +bool seen[31]; +char cat[31][1000], str[150]; +int main(){ + int T, n, m, cap[31], catc[31]; + scanf("%d", &T); + while (T--) { + if (root) delete root; + root = new Trie(); + + scanf("%d", &n); + vector cats; + for (int i=0; ivc; ++i) { + int catid = catids->vals[i]; + if (seen[catid]) + catids->vals[j++] = catid; + else { + seen[catid] = true; + if (++catc[catid] == cap[catid]) + cats.push_back(catid); + } + } + catids->vc = j; + } + } + else if (!p[0]) break; + p += l + 1; + while (*p && !isalpha(*p)) p++; + } + } + + if (cats.empty()) puts("SQF Problem."); + else { + sort(cats.begin(), cats.end()); + printf("%s", cat[cats[0]]); + for (int i=1; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10687 + Name: Monitoring the Amazon + Problem: https://onlinejudge.org/external/106/10687.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 1073741824 +#define MAXN 1001 + +int X[MAXN], Y[MAXN], n; +int dist(int i, int j) { + int xx = X[i] - X[j], + yy = Y[i] - Y[j]; + + return xx*xx + yy*yy; +} + +bool iless(int i, int di, int j, int dj) { + if (j < 0) return true; + if (di != dj) return di < dj; + if (X[i] != X[j]) return X[i] < X[j]; + return Y[i] < Y[j]; +} + +// -------------------------------------------------------- + + +list adj[MAXN]; +bool seen[MAXN]; +int dfs(int u) { + seen[u] = true; + int result = 1; + for (int v: adj[u]) + if (!seen[v]) + result += dfs(v); + + return result; +} + + + +int main(){ + while (cin>>n && n) { + for (int i=0; i> X[i] >> Y[i]; + adj[i].clear(); + seen[i]=0; + } + + for (int i=0; i +#include +using namespace std; + +unordered_map m; + +int proc(int n, int k) { + if (n <= 1) return 0; + if (!m[(n*1000)+k]) { + int mini = 1000000000; + for (int i = 1; i <= n; i++) { + int auxi = proc(i-1, k) + proc(n-i, k+i) + (k + i)*n; + if (auxi < mini) mini = auxi; + } + m[(n*1000)+k] = mini; + } + return m[(n*1000)+k]; +} + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + int n, k; + cin >> n >> k; + printf("Case %d: %d\n", c, proc(n, k)); + } + return 0; +} diff --git a/uva_cpp_clean/10688/10688-21.cpp b/uva_cpp_clean/10688/10688-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a5962be3d2f6464a1ac121a342143c23d262728 --- /dev/null +++ b/uva_cpp_clean/10688/10688-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10688 + Name: The Poor Giant + Problem: https://onlinejudge.org/external/106/10688.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 268435456 +using namespace std; + +int DP[1000][1000]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int n=2; n<=500; ++n) + for (int k=0; n+k<=500; ++k) { + int mn = INF; + for (int i=0; i> T; + for (int cse=1, n, k; cse<=T && cin >> n >> k; ++cse) + cout << "Case " << cse << ": " << DP[n][k] << "\n"; +} + diff --git a/uva_cpp_clean/10688/10688-9.cpp b/uva_cpp_clean/10688/10688-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87f7bd75cd98103205a2c12d9e763189c84bd37f --- /dev/null +++ b/uva_cpp_clean/10688/10688-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +const int MAX_N = 510, INF = 1e7; + +int n, k, memo[MAX_N][MAX_N]; + +int dp (int l, int r) { + if (l >= r) return 0; + if (~memo[l][r]) return memo[l][r]; + int ret = INF; + for (int i = l; i <= r; i++) { + ret = min(ret, (i + k) * (r - l + 1) + dp(l, i - 1) + dp(i + 1, r)); + } + return memo[l][r] = ret; +} + +int main () { + int tc; + cin >> tc; + for (int t = 1; t <= tc; t++) { + memset(memo, -1, sizeof memo); + cin >> n >> k; + cout << "Case " << t << ": " << dp(1, n) << endl; + } + return (0); +} diff --git a/uva_cpp_clean/10689/10689-13.cpp b/uva_cpp_clean/10689/10689-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5add4bc257c19b5bf5379dda8e1b5ba4559da5d --- /dev/null +++ b/uva_cpp_clean/10689/10689-13.cpp @@ -0,0 +1,61 @@ +#include +#include + +using namespace std; + +int main() +{ + int a, b, m, f, n, t; + + cin >> t; + + while (t--) + { + cin >> a >> b >> n >> m; + + switch (m) + { + case 1: + n = n % 60; + break; + case 2: + n = n % 300; + break; + case 3: + n = n % 1500; + break; + case 4: + n = n % 15000; + break; + + default: + break; + } + + m = (int)pow(10.0, m * 1.0); + + if (n == 0) + { + cout << (a % m) << endl; + } + + else if (n == 1) + { + cout << (b % m) << endl; + } + + else + { + for (int i = 2; i <= n; ++i) + { + f = ((a % m) + (b % m) % m); + + a = b; + + b = f; + } + + cout << (f % m) << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10689/10689-21.cpp b/uva_cpp_clean/10689/10689-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..467dc2a65f79b497c70beb80d84e1484d5e3d5fa --- /dev/null +++ b/uva_cpp_clean/10689/10689-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10689 + Name: Yet another Number Sequence + Problem: https://onlinejudge.org/external/106/10689.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MXM 10000 +struct Mat2 { + int a, b, c, d; + Mat2(int a, int b, int c, int d):a(a%MXM),b(b%MXM),c(c%MXM),d(d%MXM){} + Mat2 operator *(const Mat2 m) const { + return Mat2(a*m.a+b*m.c, a*m.b+b*m.d, c*m.a+d*m.c, c*m.b+d*m.d); + } +}; + +Mat2 one(1,0,1,0); +Mat2 powmod(Mat2 x, int n) { + if (n == 0) return one; + if (n == 1) return x; + + Mat2 x2 = powmod(x, n>>1); + Mat2 tx2 = x2 * x2; + return (n&1) ? tx2*x : tx2; +} + + +Mat2 fib(1,1,1,0); +int mm[] = {1, 10, 100, 1000, 10000}; +int main(){ + int T, n, a, b, m, res; + scanf("%d", &T); + while (T--) { + scanf("%d%d%d%d", &a, &b, &n, &m); + + if (!n) printf("%d\n", a); + else { + Mat2 an = powmod(fib, n - 1); + int result = (an.a*b + an.b*a) % mm[m]; + printf("%d\n", result); + } + } +} diff --git a/uva_cpp_clean/10689/10689-5.cpp b/uva_cpp_clean/10689/10689-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7b7384f15a27b7513660ce316b205eb6261dd9e6 --- /dev/null +++ b/uva_cpp_clean/10689/10689-5.cpp @@ -0,0 +1,210 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +////=====================================//// + +int mod=1000; +int row; + +struct Matrix +{ + ll mat[10][10]; +}; + +Matrix zero; + +Matrix Mul(Matrix &aa, Matrix &bb) +{ + Matrix ret=zero; + for(int i = 0; i < row; i ++) + { + for(int j = 0; j < row; j ++) + { + for(int k = 0; k < row; k ++) + { + ret.mat[i][j] += (aa.mat[i][k]*bb.mat[k][j]); + ret.mat[i][j] %= mod; + } + } + } + return ret; +} + +Matrix exp(Matrix a, ll b) +{ + Matrix xx=zero; + for0(i,row) + { + xx.mat[i][i]=1; + } + + while(b) + { + if(b&1) + xx=Mul(xx,a); + a=Mul(a,a); + b>>=1; + } + return xx; +} + +int main() +{ + row=2; + int t,no=0; + si(t); + while(t--) + { + int a,b,n,m; + sii(a,b),sii(n,m); + mod=1; + for(int i=0;i + +#define MOD 10000 + +using namespace std; + +int ans, tc, a, b, n, m, mod, M[2][2] = {{ 1, 1}, {1, 0}}, FIB[2][2], w, x, y , z; + +void multiply_FIB_FIB(){ + w = FIB[0][0] * FIB[0][0] + FIB[0][1] * FIB[1][0]; + x = FIB[0][0] * FIB[0][1] + FIB[0][1] * FIB[1][1]; + y = FIB[1][0] * FIB[0][0] + FIB[1][1] * FIB[1][0]; + z = FIB[1][0] * FIB[0][1] + FIB[1][1] * FIB[1][1]; + FIB[0][0] = w % MOD; + FIB[0][1] = x % MOD; + FIB[1][0] = y % MOD; + FIB[1][1] = z % MOD; +} + +void multiply_FIB_M(){ + w = FIB[0][0] * M[0][0] + FIB[0][1] * M[1][0]; + x = FIB[0][0] * M[0][1] + FIB[0][1] * M[1][1]; + y = FIB[1][0] * M[0][0] + FIB[1][1] * M[1][0]; + z = FIB[1][0] * M[0][1] + FIB[1][1] * M[1][1]; + FIB[0][0] = w % MOD; + FIB[0][1] = x % MOD; + FIB[1][0] = y % MOD; + FIB[1][1] = z % MOD; +} + +void power(int n){ + if(n == 0 || n == 1) + return; + power(n / 2); + multiply_FIB_FIB(); + if(n & 1) + multiply_FIB_M(); +} + +void quick_fib(int n){ + FIB[0][0] = 1 , FIB[0][1] = 1; + FIB[1][0] = 1 , FIB[1][1] = 0; + power(n - 1); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d %d", &a, &b, &n, &m); + quick_fib(n); + mod = pow(10, m); + if(n > 1) + ans = (FIB[0][0] * b + FIB[0][1] * a) % mod; + else if(n ==1) + ans = b % mod; + else + ans = a % mod; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10693/10693-20.cpp b/uva_cpp_clean/10693/10693-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f9fabfcc1790b767423fce2f800e8f79e230cb3 --- /dev/null +++ b/uva_cpp_clean/10693/10693-20.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + double L, f; + + while( cin >> L >> f && L+f!=0.0 ) + { + double v = sqrt( 2.0*L*f ); + double volume = (v*3600.0)/(2.0*L); + + cout << fixed << setprecision(8) << v << " " << volume << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10693/10693-21.cpp b/uva_cpp_clean/10693/10693-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..205d9ed9080062f29d4c4161dafc637e6691ea8d --- /dev/null +++ b/uva_cpp_clean/10693/10693-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10693 + Name: Traffic Volume + Problem: https://onlinejudge.org/external/106/10693.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(8); + + long double l, f; + while (cin >> l >> f && (l||f)) { + long double + v = sqrt(2.0*l*f), + d = v*(v/(2*f)), + x = 3600*v / (l + d); + cout << v << ' ' << x << endl; + } +} diff --git a/uva_cpp_clean/10696/10696-19.cpp b/uva_cpp_clean/10696/10696-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14a751224eccf5a6f53d72df292244abc518c681 --- /dev/null +++ b/uva_cpp_clean/10696/10696-19.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; + +int main() { + int n; + while (scanf("%d", &n) && n != 0) printf("f91(%d) = %d\n", n,(n >= 102? n-10: 91)); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10696/10696-21.cpp b/uva_cpp_clean/10696/10696-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f2c8298a34aa27f6da8118cc10c6c7b66ac78bf --- /dev/null +++ b/uva_cpp_clean/10696/10696-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10696 + Name: f91 + Problem: https://onlinejudge.org/external/106/10696.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int p[101]; +int f(int x) { + if (x > 100) return x - 10; + if (p[x]) return p[x]; + return p[x] = f(f(x + 11)); +} + +int main() { + int n; + while ((cin >> n) && n) + printf("f91(%d) = %d\n", n, f(n)); +} diff --git a/uva_cpp_clean/10696/10696-5.cpp b/uva_cpp_clean/10696/10696-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..30dea9446d9b86f86e6df77883a7fc037e306838 --- /dev/null +++ b/uva_cpp_clean/10696/10696-5.cpp @@ -0,0 +1,13 @@ +#include +int main() +{ + int n; + while(scanf("%d",&n)==1&&n!=0) + { + if(n <= 101) + printf("f91(%d) = 91\n",n); + else + printf("f91(%d) = %d\n",n,n-10); + } + return 0; +} diff --git a/uva_cpp_clean/10696/10696-9.cpp b/uva_cpp_clean/10696/10696-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..178e0b1300b12fc8a429e1dcde8f6231aa356e5e --- /dev/null +++ b/uva_cpp_clean/10696/10696-9.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main(){ + int n, ans; + while(scanf("%d", &n), n){ + if(n <= 101) + ans = 91; + else + ans = n - 10; + printf("f91(%d) = %d\n", n, ans); + } + return(0); +} diff --git a/uva_cpp_clean/10698/10698-21.cpp b/uva_cpp_clean/10698/10698-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cebcd6f3decaa5f5e98b6db55bd507e9c1c0f0a3 --- /dev/null +++ b/uva_cpp_clean/10698/10698-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10698 + Name: Football Sort + Problem: https://onlinejudge.org/external/106/10698.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char tname[30][30], t1[30], t2[30], tmp[30]; +int ord[30], pts[30], played[30], goals[30], suff[30], g1, g2, n, m; +bool comp(int i, int j) { + if (pts[i] != pts[j]) return pts[i] > pts[j]; + int d1 = goals[i]-suff[i], d2 = goals[j]-suff[j]; + if (d1 != d2) return d1 > d2; + if (goals[i] != goals[j]) return goals[i] > goals[j]; + return strcasecmp(tname[i], tname[j]) < 0; +} + +bool eqteam(int i, int j) { + return pts[i]==pts[j] && goals[i]==goals[j] && suff[i] == suff[j]; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + bool frst = 1; + while (cin>>n>>m && (n||m)) { + if (frst) frst = 0; + else cout << endl; + + unordered_map tid; + + for (int i=0; i> tname[i]; + tid[tname[i]] = ord[i] = i; + pts[i] = played[i] = goals[i] = suff[i] = 0; + } + + int total = 0; + for (int i=0; i> t1 >> g1 >> tmp >> g2 >> t2; + int id1 = tid[t1], id2 = tid[t2]; + ++played[id1]; + ++played[id2]; + goals[id1] += g1; + goals[id2] += g2; + suff[id1] += g2; + suff[id2] += g1; + if (g1 == g2) { + ++pts[id1]; + ++pts[id2]; + --total; + } + else if (g1 > g2) + pts[id1] += 3; + else pts[id2] += 3; + total += 3; + } + + sort(ord, ord+n, comp); + for (int i=0; i + +#define SIZE 20 + +using namespace std; + +struct Team{ + char name[SIZE], lower_name[SIZE]; + int points, games, goals, sgoals, dif; +}aux; + +int tc, t, g, pos1, pos2, g1, g2, j; +char s[SIZE], team1[SIZE], team2[SIZE]; +vector v; + +inline bool equals(Team x, Team y){ + return (x.points == y.points && x.dif == y.dif && x.goals == y.goals); +} + +bool cmp(Team x, Team y){ + if(x.points != y.points) return (x.points > y.points); + if(x.dif != y.dif) return (x.dif > y.dif); + if(x.goals != y.goals) return (x.goals > y.goals); + return (strcmp(x.lower_name, y.lower_name) < 0); +} + +void lowerNames(){ + for(int i = 0; i < v.size(); i++){ + for(j = 0; v[i].name[j]; j++) v[i].lower_name[j] = tolower(v[i].name[j]); + v[i].lower_name[j] = '\0'; + } +} + +int findTeam(char x[]){ + for(int i = 0; i < v.size(); i++) if(strcmp(v[i].name, x)==0) return i; +} + +void printResults(){ + int it = 1; + for(int i = 0; i < v.size(); i++, it++){ + if(i == 0 || equals(v[i], v[i - 1]) == false) printf("%2d.", it); + else printf(" "); + printf("%16s %3d %3d %3d %3d %3d ", v[i].name, v[i].points, v[i].games, v[i].goals, v[i].sgoals, v[i].dif); + if(v[i].games) printf("%6.2f\n", 100.0 * v[i].points / (3.0 * v[i].games)); + else printf(" N/A\n"); + } +} + +int main(){ + while(scanf("%d %d\n", &t, &g),t | g){ + if(tc++) putchar('\n'); + v.clear(); + for(int i = 0; i < t; i++) scanf("%s", s), strcpy(aux.name, s), v.push_back(aux); + for(int i = 0;i < g; i++){ + scanf("%s %d - %d %s", team1, &g1, &g2, team2); + pos1 = findTeam(team1); + pos2 = findTeam(team2); + if(g1 > g2) v[pos1].points += 3; + else if(g1 < g2) v[pos2].points += 3; + else v[pos1].points += 1, v[pos2].points += 1; + v[pos1].games += 1, v[pos2].games += 1; + v[pos1].goals += g1, v[pos2].goals += g2; + v[pos1].sgoals += g2, v[pos2].sgoals += g1; + v[pos1].dif += g1 - g2; + v[pos2].dif += g2 - g1; + } + lowerNames(); + sort(v.begin(), v.end(), cmp); + printResults(); + } + return(0); +} diff --git a/uva_cpp_clean/10699/10699-13.cpp b/uva_cpp_clean/10699/10699-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5a29ee1fc53aa30517ae804164995c2b4aec658 --- /dev/null +++ b/uva_cpp_clean/10699/10699-13.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 1000001 + +vector numDiff(MAX, 0); + +void numDif() +{ + for (int i = 2; i < MAX ; ++i) + { + if (numDiff[i] == 0) + { + for (int j = i; j < MAX; j += i) + { + numDiff[j]++; + } + } + } +} + +int main() +{ + numDif(); + + int n; + + while (cin >> n && n != 0) + { + cout << n << " : " << numDiff[n] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10699/10699-14.cpp b/uva_cpp_clean/10699/10699-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebe1f0a742075d51e07b7661270e0cc1ab04886d --- /dev/null +++ b/uva_cpp_clean/10699/10699-14.cpp @@ -0,0 +1,172 @@ +/*************************************************** + * Problem Name : 10699 Count the factors.cpp + * Problem Link : https://onlinejudge.org/external/106/10699.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-10-12 + * Problem Type : NT + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 1000005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +vectorprime; +char mark[MAX]; +void sieve (int n) { + int sq = sqrt (n) + 1; + mark[0] = mark[1] = 1; + + for (int i = 4; i < n; i += 2) { + mark[i] = 1; + } + + for (int i = 3; i <= sq; i++) { + for (int j = i * i; j < n; j += (2 * i) ) { + mark[j] = 1; + } + } + + for (int i = 1; i < n; i++) { + if (!mark[i]) prime.pb (i); + } +} +setfactors; +void prime_factorize (int n) { + int sq = (int) sqrt (n); + + for (int i = 0; i < (int) prime.size() && prime[i] <= sq; i++) { + //~ if (mark[n] == 0) break; + while (! (n % prime[i]) ) { + n /= prime[i]; + factors.insert (prime[i]); + } + + sq = (int) sqrt (n); + } + + if (n != 1) { + factors.insert (n); + } +} +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + sieve (MAX); + int n; + while (cin >> n) { + if (n == 0) break; + + prime_factorize (n); + int cnt = factors.size(); + cout << n << " : " << cnt << "\n"; + factors.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/10699/10699-19.cpp b/uva_cpp_clean/10699/10699-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0da3cda0b70e77eba1c1e5b714df6e5b7c95b60 --- /dev/null +++ b/uva_cpp_clean/10699/10699-19.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<100000001> bs; +vi primes; + +inline void sieve(ll upperbound = 50000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline void primeFactors(ll n, vii & factors) { + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +int main() { + sieve(); + ll n; + vii factors; + while (cin >> n && n != 0) { + factors.clear(); + primeFactors(n, factors); + printf("%lld : %ld\n", n, factors.size()); + } + return 0; +} diff --git a/uva_cpp_clean/10699/10699-20.cpp b/uva_cpp_clean/10699/10699-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36e5d3881d23a57a8b7e282b56482dc0b4a220ee --- /dev/null +++ b/uva_cpp_clean/10699/10699-20.cpp @@ -0,0 +1,33 @@ +#include +#include + +const int S = 10000005; +bool prime[S]; + +int main() +{ + prime[0] = prime[1] = false; + for(int i=2 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10699 + Name: Count the factors + Problem: https://onlinejudge.org/external/106/10699.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int count(int n) { + int cnt = 0; + int sq = sqrt((double)n); + for (int i=2; i<=sq; i++) + if (n%i==0) { + cnt++; + while (n%i==0)n/=i; + sq = sqrt((double)n); + } + return cnt+(n>1); +} + +int main(){ + int n; + while(cin>>n && n) + printf("%d : %d\n", n, count(n)); +} diff --git a/uva_cpp_clean/10699/10699-9.cpp b/uva_cpp_clean/10699/10699-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f740038c11b7381bd3e1bd4065bf5a1cca89a0e --- /dev/null +++ b/uva_cpp_clean/10699/10699-9.cpp @@ -0,0 +1,21 @@ +#include + +#define SIZE 1000010 + +using namespace std; + +int numFactors[SIZE], N; + +void sieve(){ + for(int it1 = 2; it1 < SIZE; it1++) + if( numFactors[it1] == 0 ) + for(int it2 = it1; it2 < SIZE; it2 += it1) + numFactors[it2]++; +} + +int main(){ + sieve(); + while(scanf("%d", &N), N) + printf("%d : %d\n", N, numFactors[N]); + return(0); +} diff --git a/uva_cpp_clean/107/107-18.cpp b/uva_cpp_clean/107/107-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b07ecc1e4d12b4a0aa1df30a84f36ad301c45182 --- /dev/null +++ b/uva_cpp_clean/107/107-18.cpp @@ -0,0 +1,46 @@ +#include +#include + +using namespace std; + +int binarySearch(int a, int w){ + int mid, l, r; + double quotient=log(a)/log(w); + l=1, r=2147483645, mid=(l+r)/2; + while(l!=r){ + if(log(mid+1)/log(mid)-quotient>0.000000000001) l=mid+1; + else if(log(mid+1)/log(mid)-quotient<-0.000000000001) r=mid; + else break; + mid=(l+r)/2; + } + return mid; +} + +int main(){ + int a, w; + while(scanf("%d %d", &a, &w)!=EOF){ + if(a==0 && w==0) break; + int n=binarySearch(a, w), x=round(log(a)/log(n+1)), mul=1; + long long int notWork=0, totalH=0; + for(int i=0; i a = (N+1)^x, por lo que llegamos al nivel donde se encuentra los gatos que trabajaran. + De la anterior deduccion obtenemos las siguientes formulas: + w = N^x (2) + a = (N+1)^x (3) + Usé busqueda binaria para tantear el valor de N, tener en cuenta el error, debe ser de 1e-12, sino te puede joder + en algunos casos, por ultimo, para hallar el valor de x es conveniente usar (3) debido a casos especiales cuyo + despeje de esta variable usando (2) da division 0/0. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/107/107-21.cpp b/uva_cpp_clean/107/107-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24d4bbb5dec974382c8b4bee4f003729f720d761 --- /dev/null +++ b/uva_cpp_clean/107/107-21.cpp @@ -0,0 +1,54 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 107 + Name: The Cat in the Hat + Problem: https://onlinejudge.org/external/1/107.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define EPS 1e-8 + +bool eq(double a, double b) { + return (a> h >> n && (h||n)) { + if (n == 1) { + a = log2(h); + b = (1LL << (a+1)) - 1; + } + else { + k = findk(h, n, N); + a = round(sigma(N, k-1)); + b = round(h * sigma((double)N/(N+1.0), k)); + } + printf("%lld %lld\n", a, b); + } +} diff --git a/uva_cpp_clean/10700/10700-21.cpp b/uva_cpp_clean/10700/10700-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c58b50d79650ccf273a79123268d7043f4df8130 --- /dev/null +++ b/uva_cpp_clean/10700/10700-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10700 + Name: Camel trading + Problem: https://onlinejudge.org/external/107/10700.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + char line[200], op[20]; + int T, n, x[20]; + cin>>T; + while(T--) { + cin >> line; + stringstream sin(line); + + sin>>x[n=0]; + while (sin>>op[++n]) + sin>>x[n]; + + vector v1, v2; + long long mx = x[0], mn = mx; + for (int i=1; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10701 + Name: Pre, in and post + Problem: https://onlinejudge.org/external/107/10701.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + char s1[143], s2[143]; + int T, n, P[143]; + cin >> T; + while (T--) { + cin >> n >> s1 >> s2; + for (int i=0; i st; + for (int i=0; i P[s2[i]]) { + cout << st.top(); + st.pop(); + } + + if (i P[s2[i+1]]) + cout << s2[i]; + else st.push(s2[i]); + } + + while (!st.empty()) { + cout << st.top(); + st.pop(); + } + + cout << endl; + } +} diff --git a/uva_cpp_clean/10702/10702-21.cpp b/uva_cpp_clean/10702/10702-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10db37dd975ee56f1dc679bb16e8c2ace802ca89 --- /dev/null +++ b/uva_cpp_clean/10702/10702-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10702 + Name: Travelling Salesman + Problem: https://onlinejudge.org/external/107/10702.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------------ + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int D[1043][143], W[143][143], n; + while ((n=readUInt())>0) { + int src = readUInt(), + dn = readUInt(), + m = readUInt(); + + for (int i=0; i +#include + +using namespace std; + +int main(){ + int w, h, n, x1, x2, y1, y2, cont; + bool matriz[507][507]; + while(true){ + cin >> w >> h >> n; + if(w == 0 && h == 0 && n == 0) break; + for(int i = 0; i < n; i++){ + cin >> x1 >> y1 >> x2 >> y2; + if(x1 > x2) swap(x1, x2); + if(y1 > y2) swap(y1, y2); + for(int j = y1 - 1; j <= y2 - 1; j++){ + for(int k = x1 - 1; k <= x2 - 1; k++) matriz[j][k] = true; + } + } + cont = 0; + for(int i = 0; i < h; i++){ + for(int j = 0; j < w; j++){ + if(!matriz[i][j]) cont++; + else matriz[i][j] = false; + } + } + if(cont == 0) printf("There is no empty spots.\n"); + else if(cont == 1) printf("There is one empty spot.\n"); + else printf("There are %d empty spots.\n", cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10703/10703-21.cpp b/uva_cpp_clean/10703/10703-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69ba68fb986534be572842325dfac1a63ddfb347 --- /dev/null +++ b/uva_cpp_clean/10703/10703-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10703 + Name: Free spots + Problem: https://onlinejudge.org/external/107/10703.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool F[1000][1000]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int w, h, n, x1, x2, y1, y2; + while (cin>>w>>h>>n && (w || h || n)) { + for (int i=0; i> x1 >> y1 >> x2 >> y2; + if (x1 > x2) swap(x1, x2); + if (y1 > y2) swap(y1, y2); + + for (int x=x1-1; x + +#define SIZE 510 + +using namespace std; + +int w, h, n, a1, a2, b1, b2, ans; +bool v[SIZE][SIZE]; + +int main(){ + while(scanf("%d %d %d", &w, &h, &n), w | h | n){ + memset(v, true, sizeof v); + ans = 0; + while(n--){ + scanf("%d %d %d %d", &a1, &b1, &a2, &b2); + if(a1 > a2) swap(a1, a2); + if(b1 > b2) swap(b1, b2); + for(int i = a1; i <= a2; i++) + for(int j = b1; j <= b2; j++) + v[i][j] = false; + } + for(int i = 1; i <= w; i++) + for(int j = 1; j <= h; j++) + ans += v[i][j]; + if(ans == 0) puts("There is no empty spots."); + else if(ans == 1) puts("There is one empty spot."); + else printf("There are %d empty spots.\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10706/10706-19.cpp b/uva_cpp_clean/10706/10706-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1660959765e98103bf637fbac4f5a11fd22d1435 --- /dev/null +++ b/uva_cpp_clean/10706/10706-19.cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; + +int main() { + int T; + int v[10]; + cin >> T; + while (T--) { + long long n; + cin >> n; + long long acum = 1, val = 2, vlen = 1; + for (; acum < n; val++) { + vlen += floor(log10(val)) + 1; + acum += vlen; + } + n -= acum - vlen; + int i = 1; + acum = 0, vlen = 0; + for (; acum < n; i++) { + vlen = floor(log10(i)) + 1; + acum += vlen; + } + i--, acum -= vlen; + int j = 0; + while (i) { + v[j] = i % 10; + i /= 10; + j++; + } + int res = -1; + while (acum < n) { + j--; + res = v[j]; + acum++; + } + printf("%d\n", res); + } + return 0; +} diff --git a/uva_cpp_clean/10706/10706-21.cpp b/uva_cpp_clean/10706/10706-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fd39e6c0cba7f93cc462a0cb943b7eaf79a04d0 --- /dev/null +++ b/uva_cpp_clean/10706/10706-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10706 + Name: Number Sequence + Problem: https://onlinejudge.org/external/107/10706.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +unsigned int X[210000]={1}, Y[60000]={1}; +int main() { + int cnt; + for (int i=2, ind=1, digits=1; ;i++) { + if (i==10 || i==100 || i==1000 || i==10000 || i==100000) digits++; + int x = i; + for (int j=digits-1; j>=0; j--) { + X[ind+j] = x%10; + x /= 10; + } + ind += digits; + Y[i-1] = Y[i-2] + ind; + if (Y[i-1]>T; + while (T--) { + cin >> n; + int yi = lower_bound(Y, Y+cnt, n) - Y; + if (Y[yi] >= n) yi--; + cout << X[n-Y[yi]-1] << endl; + } +} diff --git a/uva_cpp_clean/10706/10706-6.cpp b/uva_cpp_clean/10706/10706-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9312a37c05ac3d7660c3c1040c2d083d22479d1 --- /dev/null +++ b/uva_cpp_clean/10706/10706-6.cpp @@ -0,0 +1,60 @@ +/* + math > ad-hoc > sequences + difficulty: hard + date: 31/Mar/2020 + hint: pre-process the number sequence and f(k), so use binary search on f + by: @brpapa +*/ +#include +#include +#include +#include +#define ll long long +#define MAX_K ((int)1e5) // pois f[MAX_K] > max i +#define MAX_N ((int)5e5) // maior que f[MAX_K-1]-f[MAX_K-2] +using namespace std; + +// total de digitos do grupo Sk = [1, 2, ..., k] +ll g(ll k) { + ll ans = 0; + + for (int i = 0; true; i++) { + ll p = pow(10, i)-1; + if (k <= p) return ans; + + ans += (i+1) * (min(k, (ll)pow(10,i+1)-1) - p); + } + return -1; +} + +vector digs(int n) { + vector ans; + while (n > 0) { + ans.insert(ans.begin(), n%10); + n /= 10; + } + return ans; +} + +int main() { + // numSeq: [0, 1, ..., 9, 1, 0, 1, 1, 1, 2, ...] + vector numSeq(1, 0); + for (int n = 1; n < MAX_N; n++) for (int d : digs(n)) numSeq.push_back(d); + + // f[k] = índice do número 1 no k-ésimo grupo (k >= 1) + vector f(MAX_K); f[1] = 1; + for (int k = 2; k < MAX_K; k++) f[k] = f[k-1]+g(k-1); + + + int T; cin >> T; + while (T--) { + int i; cin >> i; + + // i-ésimo número está no grupo k + int k = upper_bound(f.begin(), f.end(), i) - f.begin()-1; + + int n = i-(f[k]-1); // i em relação à f[k] >= 1 + cout << numSeq[n] << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10706/10706-9.cpp b/uva_cpp_clean/10706/10706-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f667b7cc905e7026fe28ee85d46fec85017e59de --- /dev/null +++ b/uva_cpp_clean/10706/10706-9.cpp @@ -0,0 +1,32 @@ +#include + +#define LIM 31500 + +using namespace std; + +int tc, n, tmp, aux, l, d = 1; +vector sum; +string ans; + +int main(){ + sum.push_back(0); + for(int i = 1; i < LIM; i++){ + if(!(i % 10) && i / (int)pow(10, d) == 1) d++; + tmp += d; + sum.push_back(sum[i - 1] + tmp); + } + for(int i = 1; i <= tmp; i++){ + aux = i; + l = 0; + while(aux) l++, aux /= 10; + aux = i; + while(l) + ans.push_back(aux / (int)pow(10, l - 1) + 48), aux %= (int)pow(10, l - 1), l--; + } + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + printf("%c\n", ans[n - 1 - sum[lower_bound(sum.begin(), sum.end(), n) - sum.begin() - 1]]); + } + return(0); +} diff --git a/uva_cpp_clean/10707/10707-21.cpp b/uva_cpp_clean/10707/10707-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1bbe82db47f07a07368cf8274cd2a81fbb86c777 --- /dev/null +++ b/uva_cpp_clean/10707/10707-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10707 + Name: 2D-Nim + Problem: https://onlinejudge.org/external/107/10707.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int w, h; +bool M1[101][101], M2[101][101]; +int a1[4000], a2[4000], + S[101][101]; +void makeUids(bool M[101][101], int res[]) { + for (int i=0; i=0; --i) + for (int j=w-1, c=0; j>=0; --j) + S[i][j] += M[i][j] ? ++c : c=0; + + int k = 0; + for (int j=w-1; j>=0; --j) + for (int i=h-1, c=0; i>=0; --i) + if (S[i][j] += M[i][j] ? ++c : c=0) { + res[k++] = S[i][j]; + S[i][j] = 0; + } + sort(res, res+k); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, T; cin >> T; + while (T--) { + cin >> w >> h >> n; + + for (int i=0; i> x >> y; + M1[y][x] = 1; + } + makeUids(M1, a1); + + for (int i=0, x, y; i> x >> y; + M2[y][x] = 1; + } + makeUids(M2, a2); + + bool eq = 1; + for (int i=0; eq && i + +#define SIZE 110 + +using namespace std; + +int tc, W, H, n, a, b, L[SIZE][SIZE], R[SIZE][SIZE], D[SIZE][SIZE], U[SIZE][SIZE]; +bool grid[SIZE][SIZE]; +vector v1, v2; + +void Probability(vector & v){ + memset(L, 0, sizeof L), memset(R, 0, sizeof R); + memset(D, 0, sizeof D), memset(U, 0, sizeof U); + for(int x = 0; x < W; x++) + for(int y = 0, ac = 0; y < H; y++) + ac = ac * grid[x][y] + grid[x][y], L[x][y] = ac; + for(int x = 0; x < W; x++) + for(int y = H - 1, ac = 0; y >= 0; y--) + ac = ac * grid[x][y] + grid[x][y], R[x][y] = ac; + for(int y = 0; y < H; y++) + for(int x = 0, ac = 0; x < W; x++) + ac = ac * grid[x][y] + grid[x][y], U[x][y] = ac; + for(int y = 0; y < H; y++) + for(int x = W - 1, ac = 0; x >= 0; x--) + ac = ac * grid[x][y] + grid[x][y], D[x][y] = ac; + for(int x = 0; x < W; x++) + for(int y = 0; y < H; y++) + if( grid[x][y] ) + v.push_back( L[x][y] + R[x][y] + U[x][y] + D[x][y] ); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d", &W, &H, &n); + memset(grid, false, sizeof grid); + for(int it = 0; it < n; it++){ + scanf("%d %d", &a, &b); + grid[a][b] = true; + } + Probability(v1); + memset(grid, false, sizeof grid); + for(int it = 0; it < n; it++){ + scanf("%d %d", &a, &b); + grid[a][b] = true; + } + Probability(v2); + sort(v1.begin(), v1.end()); + sort(v2.begin(), v2.end()); + puts( v1 == v2 ? "YES" : "NO" ); + v1.clear(), v2.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/10714/10714-21.cpp b/uva_cpp_clean/10714/10714-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..231f8159d2a5650b4ae5efbb81ceb9951ffe46bb --- /dev/null +++ b/uva_cpp_clean/10714/10714-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10714 + Name: Ants + Problem: https://onlinejudge.org/external/107/10714.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, l, n, x; + cin>>T; + while (T--) { + cin>>l>>n; + int mn=0, mx=0; + for (int i=0; i>x; + mn = max(mn, min(x, l-x)); + mx = max(mx, max(x, l-x)); + } + + printf("%d %d\n", mn, mx); + } +} diff --git a/uva_cpp_clean/10717/10717-21.cpp b/uva_cpp_clean/10717/10717-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02771e5e0b96358df33757e1748350050dce7811 --- /dev/null +++ b/uva_cpp_clean/10717/10717-21.cpp @@ -0,0 +1,54 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10717 + Name: Mint + Problem: https://onlinejudge.org/external/107/10717.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +long long gcd(long long a, long long b) { + return b == 0 ? a : gcd(b, a % b); +} + +long long lcm(long long a, long long b) { + return (a*b) / gcd(a, b); +} + +long long N, T, X[143], S[4], t, mn, mx; +void dfs(int i, int j) { + if (i == 4) { + long long g = lcm(lcm(lcm(S[0], S[1]), S[2]), S[3]); + mn = max(mn, (long long)(floor(t/double(g))*g)); + mx = min(mx, (long long)(ceil(t/double(g))*g)); + return; + } + + for (int k=j; k> N >> T) { + for (int i=0; i> X[i]; + } + + for (int i=0; i> t; + dfs(0, 0); + cout << mn << " " << mx << endl; + } + } +} diff --git a/uva_cpp_clean/10718/10718-21.cpp b/uva_cpp_clean/10718/10718-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94edd6ecbe728dfd90b421352ce0dad689058021 --- /dev/null +++ b/uva_cpp_clean/10718/10718-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10718 + Name: Bit Mask + Problem: https://onlinejudge.org/external/107/10718.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + unsigned int N, L, U, R, b, r; + while (cin >> N >> L >> U) { + R = 0; + for (int i=31; i>=0; --i) { + b = 1< + +using namespace std; + +typedef unsigned long long ull; + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int size = 32; + + for(ull n, l, r, m = 0 ; cin >> n >> l >> r ; cout << m << endl, m = 0) + for(ull k = 1ULL << (size - 1) ; k > 0 ; k >>= 1) + if(((~n & k) && (m | k) <= r) || ((n & k) && (m | k) <= l)) + m |= k; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10719/10719-18.cpp b/uva_cpp_clean/10719/10719-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6518b16323a1e01465859d5e768a64406aac754a --- /dev/null +++ b/uva_cpp_clean/10719/10719-18.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main(){ + int k; + while(scanf("%d", &k) == 1){ + cin.get(); + string polinomyS; + vector polinomy; + getline(cin, polinomyS); + stringstream ss(polinomyS); + long int quotient; + while(ss >> quotient) polinomy.push_back(quotient); + for(int i = 1; i < polinomy.size(); i++) polinomy[i] += polinomy[i - 1] * k; + printf("q(x):"); + for(int i = 0; i < polinomy.size() - 1; i++) printf(" %d", polinomy[i]); + printf("\nr = %d\n\n", polinomy[polinomy.size() - 1]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10719/10719-9.cpp b/uva_cpp_clean/10719/10719-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a623026c2d0446de30c0351c0fec55f1e094c4ab --- /dev/null +++ b/uva_cpp_clean/10719/10719-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +const int SIZE = 10010; + +long long k, a[SIZE], n, taken; +string line; + +int main(){ + while(scanf("%lld\n", &k) != EOF){ + getline(cin, line); + istringstream s(line); + n = 0; + while( s >> taken ) a[n++] = taken; + for(int it = 1; it < n; it++) + a[it] += a[it - 1] * k; + printf("q(x):"); + for(int it = 0; it + 1 < n; it++) + printf(" %lld", a[it]); + putchar('\n'); + printf("r = %lld\n\n", a[n - 1]); + } + return(0); +} diff --git a/uva_cpp_clean/10720/10720-6.cc b/uva_cpp_clean/10720/10720-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..06b444e5be73837f699a565f06a1b2c9b6315399 --- /dev/null +++ b/uva_cpp_clean/10720/10720-6.cc @@ -0,0 +1,22 @@ +// https://uva.onlinejudge.org/external/107/10720.pdf +#include +using namespace std; +using vi=vector; +int main(){ + int n; + while(1){ + cin>>n; + if(!n)break; + vi a(n); + int o=1; + for(int i=0;i>a[i]; + sort(&a[0],&a[n],greater()); + if(accumulate(&a[0],&a[n],0)%2)o=0; + for(int k=1;k<=n;k++){ + int s=0; + for(int i=k;ik*(k-1)+s)o=0; + } + cout<<(o?"Possible\n":"Not possible\n"); + } +} diff --git a/uva_cpp_clean/10721/10721-13.cpp b/uva_cpp_clean/10721/10721-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..124f6222becbd747eeb7127da2ab3552b60d4da2 --- /dev/null +++ b/uva_cpp_clean/10721/10721-13.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +using namespace std; + +int n, m, k; + +long long dp[55][55]; + +long long ways(int n, int k) +{ + if (n <= 0 || k == 0) + { + return 0; + } + + if (k == 1 && n <= m) + { + return 1; + } + + if (dp[n][k] != -1) + { + return dp[n][k]; + } + + long long ans = 0; + + for (int i = 1; i <= m; i++) + { + if (n - i < 0) + { + break; + } + + ans += ways(n - i, k - 1); + } + + return dp[n][k] = ans; +} + +int main() +{ + ios::sync_with_stdio(0); + + while (cin >> n >> k >> m) + { + memset(dp, -1, sizeof dp); + + cout << ways(n, k) << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10721/10721-19.cpp b/uva_cpp_clean/10721/10721-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5f48dc4ebe6b2e14f944687e715d55236c4b861 --- /dev/null +++ b/uva_cpp_clean/10721/10721-19.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int main() { + long long grid[51][51][51]; + for (int n = 1; n < 51; n++) { + for (int k = 1; k < 51; k++) { + for (int m = 1; m < 51; m++) { + if (n < k) grid[n][k][m] = 0; + else if (n < m) grid[n][k][m] = grid[n][k][n]; + else if (k == 1) { + if (n <= m) grid[n][k][m] = 1; + else grid[n][k][m] = 0; + } + else if (n == k) grid[n][k][m] = grid[n-1][k-1][m]; + else { + grid[n][k][m] = 0; + for (int i = 1; i <= m && i < n; i++) { + grid[n][k][m] += grid[n-i][k-1][m]; + } + } + } + } + } + int N, K, M; + while (cin >> N >> K >> M) { + cout << grid[N][K][M] << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10721/10721-20.cpp b/uva_cpp_clean/10721/10721-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fd87e8ef91b35658259d74c906c04e45cdc38d2 --- /dev/null +++ b/uva_cpp_clean/10721/10721-20.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +const int MAXN = 50; +long long dp[MAXN+2][MAXN+2][MAXN+2]; + +long long BC( int n, int k, int m ) { + if( !n && !k ) return 1; + if( n <= 0 || k <= 0 ) return 0; + long long &ret = dp[n][k][m]; + if( ret != -1 ) return ret; + ret = 0; + for( int i = 1 ; i <= m ; ++i ) ret += BC( (n - i), (k - 1), m ); + return ret; +} + +int main( int argc, char ** argv ) { + int n, k, m; + while( cin >> n >> k >> m ) { + memset( dp, -1, sizeof( dp ) ); + cout << BC( n, k, m ) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10721/10721-21.cpp b/uva_cpp_clean/10721/10721-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc6da52075bbecc5be33f14205438d5213be6d9c --- /dev/null +++ b/uva_cpp_clean/10721/10721-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10721 + Name: Bar Codes + Problem: https://onlinejudge.org/external/107/10721.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + long long DP[60]; + int N,K,M,n,k,i; + + while(scanf("%d%d%d",&N,&K,&M)==3) + { + DP[0]=1; + for(n=1;n<=N;n++) + DP[n]=0; + + for(k=1;k<=K;k++) + for(n=N;n>=0;n--) + { + DP[n]=0; + for(i=1;i<=min(n,M);i++) + DP[n]+=DP[n-i]; + } + + + cout< +using namespace std; +typedef long long ll; + +int N, K, M; // unidades, barras, tamanho máximo de cada barra + +ll memo[55][55][55]; +ll dp(int n, int k, int m) { + // n-ésima unidade, k-ésima barra, m-ésima unidade da barra k + + if (n < N && (m == M || k == K)) return 0; // inválido + if (n == N) return k == K; // válido apenas se o k excedeu + + ll &ans = memo[n][k][m]; + if (ans != -1) return ans; + + return ans = dp(n+1, k, m+1) + dp(n+1, k+1, 0); +} + +int main() { + while (cin >> N >> K >> M) { + memset(memo, -1, sizeof memo); + cout << dp(0, 0, 0) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10721/10721-9.cpp b/uva_cpp_clean/10721/10721-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff19066cc6e601b56387996a6334d4209e70bf02 --- /dev/null +++ b/uva_cpp_clean/10721/10721-9.cpp @@ -0,0 +1,26 @@ +#include + +#define SIZE 60 + +using namespace std; + +int N,K,M; +unsigned long long memo[SIZE][SIZE][SIZE]; + +unsigned long long dp(int n,int k,int m){ + if(k==0 && n==0) return 1; + if(k==0) return 0; + if(n<=0) return 0; + if(~memo[n][k][m]) return memo[n][k][m]; + unsigned long long ans = 0; + for(int it=1;it<=m;it++) + ans += dp(n-it,k-1,m); + return memo[n][k][m] = ans; +} + +int main(){ + memset(memo,-1,sizeof memo); + while(scanf("%d %d %d",&N,&K,&M)==3) + printf("%lld\n",dp(N,K,M)); + return(0); +} diff --git a/uva_cpp_clean/10724/10724-21.cpp b/uva_cpp_clean/10724/10724-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d05e6d542828602683d31b7a20e82ebc4f291ef --- /dev/null +++ b/uva_cpp_clean/10724/10724-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10724 + Name: Road Construction + Problem: https://onlinejudge.org/external/107/10724.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int X[143], Y[143], n; +bool S[143][143]; +double W[143][143]; +int dist2(int i, int j) { + int xx = X[i] - X[j], + yy = Y[i] - Y[j]; + return xx*xx + yy*yy; +} + +double C(int u, int v, double d) { + double s = 0; + for (int i=0; i>n>>m && (n||m)) { + for (int i=0; i> X[i] >> Y[i]; + for (int j=0; j> u >> v; --u;--v; + if (!S[u][v]) { + S[u][v] = S[v][u] = 1; + W[u][v] = W[v][u] = sqrt((double)dist2(u, v)); + } + } + + for (int k=0; kbs || (s==bs && d vi; +typedef vector vll; +const int OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int seen[10004]; + +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + //ios::sync_with_stdio(false); + //cin.tie(0); + + int n; + while (scanf("%d: ", &n), n) { + vi v(n); + for (int i = 0; i < n; ++i) cin >> v[i]; + bool t = 1; + + for (int i = 0; i < n; ++i) { + memset(seen, 0, sizeof seen); + for (int j = 0; j <= i; ++j) seen[v[j]] = 1; + for (int j = i+1; j < n; ++j) { + seen[v[j]] = 1; + int nx = v[j]+v[j]-v[i]; + if ((nx >= 0 && nx < n && j < n-1) && !seen[nx]) t = 0; + } + } + + if (!t) cout << "no\n"; + else cout << "yes\n"; + } + return 0; +} + +/* +*/ diff --git a/uva_cpp_clean/10730/10730-21.cpp b/uva_cpp_clean/10730/10730-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4baaf424e001f5671affc6e0b080b5acb4c10a95 --- /dev/null +++ b/uva_cpp_clean/10730/10730-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10730 + Name: Antiarithmetic? + Problem: https://onlinejudge.org/external/107/10730.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int X[10143], Y[10143], n; +bool check() { + int hn = n>>1; + for (int i=0; i>1)); x+2*d < n; ++d) if (d) { + int j = Y[x + d]; + if (j > i && Y[x + 2*d] > j) + return 0; + } + } + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n && n) { + cin.ignore(1); + for (int i=0; i> X[i]; + Y[X[i]] = i; + } + + cout << (check() ? "yes\n" : "no\n"); + } +} diff --git a/uva_cpp_clean/10730/10730-9.cpp b/uva_cpp_clean/10730/10730-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cdbf70ef178cc9620827fd9565efee760a473f6 --- /dev/null +++ b/uva_cpp_clean/10730/10730-9.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +vector v(10010); +bool visited[10010]; +int n, pib; + +bool isAntiarithmetic(){ + memset(visited, false, sizeof visited); + for(int i = 0; i < n; i++){ + for(int j = i + 1;j < n; j++){ + visited[v[j]] = true; + pib = v[i] + v[j]; + if(!(pib&1) && visited[pib >> 1]) + return false; + } + for(int j = i + 1; j < n; j++) + visited[v[j]] = false; + } + return true; +} + +int main(){ + while(scanf("%d:", &n), n){ + for(int i = 0; i < n; i++) scanf("%d", &v[i]); + puts( isAntiarithmetic() ? "yes" : "no"); + } + return(0); +} diff --git a/uva_cpp_clean/10731/10731-21.cpp b/uva_cpp_clean/10731/10731-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86302a5287024c85caf91afc57bccb5d08b294fd --- /dev/null +++ b/uva_cpp_clean/10731/10731-21.cpp @@ -0,0 +1,97 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10731 + Name: Test + Problem: https://onlinejudge.org/external/107/10731.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +list adj[26]; +int pre[26], low[26], pcnt; +bool onstack[26]; +stack S; + +vector result; +void scc(int u) { + pre[u] = low[u] = pcnt++; + onstack[u] = true; + S.push(u); + + for (int v: adj[u]) + if (pre[v] < 0) { + scc(v); + low[u] = min(low[u], low[v]); + } + else if (onstack[v]) + low[u] = min(low[u], pre[v]); + + + if (pre[u] == low[u]) { + vector vec; + for (int v; (v=S.top()) != u; S.pop()) { + vec.push_back(v); + onstack[v] = 0; + } + S.pop(); + onstack[u] = 0; + vec.push_back(u); + sort(vec.begin(), vec.end()); + + char rr[100]; + rr[0] = 'A' + vec[0]; + for (int i=1; i>n && n) { + if (frst) frst=0; + else putchar('\n'); + + for (int i=0; i<26; i++) { + adj[i].clear(); + pre[i] = 11111; + } + + for (int i=0; i>a[j]; + a[j]-='A'; + pre[a[j]]=-1; + } + + for (int j=0; j<5; j++) + adj[a[5]].push_back(a[j]); + } + + pcnt=0; + result.clear(); + for (int i=0; i<26; i++) + if (pre[i] < 0) + scc(i); + + sort(result.begin(), result.end()); + for (string s: result) + cout << s << endl; + } +} diff --git a/uva_cpp_clean/10731/10731-6.cc b/uva_cpp_clean/10731/10731-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..8a003734fa6935e2b7a737fbd0743c48e4d4f0fe --- /dev/null +++ b/uva_cpp_clean/10731/10731-6.cc @@ -0,0 +1,80 @@ +// https://uva.onlinejudge.org/external/107/10731.pdf +#include +using namespace std; +using ii=tuple; +using vii=vector; +using vi=vector; +using vvi=vector; +using vs=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(int T=0;;T++){ + int n,l,u,v; + cin>>l; + if(!l)break; + vi m(128); + vii e; + n=0; + for(int i=0;i>c; + if(!m[c])m[c]=++n; + x.push_back(m[c]-1); + } + cin>>c; + if(!m[c])m[c]=++n; + u=m[c]-1; + for(int v:x) + if(u!=v)e.emplace_back(u,v); + } + vi r(n); + for(int i=0;i<128;i++) + if(m[i]) + r[m[i]-1]=i; + vvi g(n),h(n),c; + for(ii x:e){ + tie(u,v)=x; + g[u].push_back(v); + h[v].push_back(u); + } + vi a,s(n),t(n); + function dfs1=[&](int i){ + s[i]=1; + for(int j:g[i]) + if(!s[j])dfs1(j); + a.push_back(i); + }; + for(int i=0;i dfs2=[&](int i){ + t[i]=1; + c.back().push_back(i); + for(int j:h[i]) + if(!t[j])dfs2(j); + }; + for(int i=0;i + +using namespace std; + +const int MAX_V = 30, LEN = 60; + +int N, a, len, dfsCounter, dfs_low[MAX_V], dfs_num[MAX_V]; +char line[LEN]; +bool vis[MAX_V], Adj[MAX_V][MAX_V], in_stack[MAX_V], first = true; +vector path; +vector < vector < char > > ans; + +void dfs(int u){ + dfs_low[u] = dfs_num[u] = ++dfsCounter; + in_stack[u] = true; + path.push_back(u); + for(int v = 0; v < MAX_V; v++) + if( Adj[u][v] ){ + if( not dfs_num[v] ) dfs(v); + if( in_stack[v] ) dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + if( dfs_low[u] == dfs_num[u] ){ + vector s; + while(true){ + int v = path.back(); path.pop_back(); + in_stack[v] = false; + s.push_back(v + 'A'); + if( u == v ) break; + } + sort(s.begin(), s.end()); + ans.push_back(s); + } +} + +void solve(){ + if( not first ) putchar('\n'); + first = false; + memset(dfs_num, 0, sizeof dfs_num); + dfsCounter = 0; + for(int u = 0; u < MAX_V; u++) + if( vis[u] and not dfs_num[u] ) dfs(u); + sort(ans.begin(), ans.end()); + for(int i = 0; i < ans.size(); i++) + for(int j = 0, sz = ans[i].size(); j < sz; j++) + printf( j == sz - 1 ? "%c\n" : "%c ", ans[i][j] ); +} + +void read(){ + for(int i = 0; i < N; i++){ + cin.getline(line, LEN); + len = strlen(line); + a = line[len - 1] - 'A'; + len -= 2; + for(int j = 0; j < len; j += 2) + vis[line[j] - 'A'] = true, Adj[a][line[j] - 'A'] = true; + } +} + +void clear(){ + ans.clear(); + memset(vis, false, sizeof vis); + memset(Adj, false, sizeof Adj); +} + +int main(){ + while(scanf("%d\n", &N), N){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/10738/10738-13.cpp b/uva_cpp_clean/10738/10738-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ffdf441704d8c5488534f0b04769db5af450dce --- /dev/null +++ b/uva_cpp_clean/10738/10738-13.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +#define MAX 1000010 + +typedef long long ll; +typedef vector vll; +typedef vector vb; + +vll numDiff(MAX, 0), mu(MAX, 0),M(MAX, 0); +vb isPrime(MAX, true), seqFree(MAX, true); + +void calPrime() +{ + seqFree[0] = false; + seqFree[1] = true; + + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += (i * i)) + { + seqFree[j] = false; + } + + for (ll k = i * i; k < MAX; k += i) + { + isPrime[k] = false; + } + + for (ll l = i; l < MAX; l += i) + { + numDiff[l]++; + } + } + } +} + +void calAns() +{ + mu[1] = M[1] = 1; + + for (int i = 2; i < MAX; ++i) + { + mu[i] = (!seqFree[i]) ? 0 : (numDiff[i] % 2 == 0) ? 1 : -1; + + M[i] = M[i - 1] + mu[i]; + } +} +int main() +{ + calPrime(); + + calAns(); + + ll n; + + while (cin >> n && n != 0) + { + cout << setw(8) << n << setw(8) << mu[n] << setw(8) << M[n] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10738/10738-9.cpp b/uva_cpp_clean/10738/10738-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a0bb997a0dd5760980c724441336166d7c188d3 --- /dev/null +++ b/uva_cpp_clean/10738/10738-9.cpp @@ -0,0 +1,33 @@ +#include + +#define SIZE 1000010 + +using namespace std; + +int Mobius[SIZE], M[SIZE], N; +bool is_prime[SIZE]; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + fill(Mobius, Mobius + SIZE, 1); + is_prime[0] = is_prime[1] = false; + M[1] = 1; + for(long long it1 = 2; it1 < SIZE; it1++){ + if( is_prime[it1] ){ + for(long long it2 = it1 + it1; it2 < SIZE; it2 += it1){ + is_prime[it2] = false; + Mobius[it2] *= -1; + if( it2 % ( it1 * it1 ) == 0 ) Mobius[it2] = 0; + } + Mobius[it1] = -1; + } + M[it1] = M[it1 - 1] + Mobius[it1]; + } +} + +int main(){ + sieve(); + while(scanf("%d", &N), N) + printf("%8d%8d%8d\n", N, Mobius[N], M[N]); + return(0); +} diff --git a/uva_cpp_clean/10739/10739-19.cpp b/uva_cpp_clean/10739/10739-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d08b32e2f7028597962fdfa765ce7835491487cf --- /dev/null +++ b/uva_cpp_clean/10739/10739-19.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +using namespace std; + +string s; +unordered_map > dp; + +int proc(int i, int j) { + if (i >= j) return 0; + if (!dp[i].count(j)) { + if (s[i] == s[j]) dp[i][j] = proc(i+1, j-1); + else dp[i][j] = min(proc(i+1, j) + 1, min (proc(i, j-1) + 1, proc(i+1, j-1) + 1)); + } return dp[i][j]; +} + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + cin >> s; + dp.clear(); + printf("Case %d: %d\n", c, proc(0, s.length() - 1)); + } + return 0; +} diff --git a/uva_cpp_clean/10739/10739-21.cpp b/uva_cpp_clean/10739/10739-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94e7771f0e10712506ec3d2e10c778b7d6f310bd --- /dev/null +++ b/uva_cpp_clean/10739/10739-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10739 + Name: String to Palindrome + Problem: https://onlinejudge.org/external/107/10739.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char s[1001]; +int DP[1001][1001]; +int main(){ + int T,cse=1,l,i,j; + cin>>T; + while(T--){ + cin>>s; + l=strlen(s); + for(i=l-1;i>=0;i--) + for(j=i+1;j +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; + +string s; +int mem[1005][1005]; +int pl(int i,int j) +{ + if(mem[i][j]!=-1) return mem[i][j]; + if(i>j) return 0; + if(s[i]==s[j]) + return pl(i+1,j-1); + else + mem[i][j]=1+ min(pl(i+1,j),min(pl(i,j-1),pl(i+1,j-1))); + + return mem[i][j]; +} +int main() +{ + int t,no=0; + cin>>t; + while(t--) + { + cover(mem,-1); + cin>>s; + int l=s.size(); + pf("Case %d: %d\n",++no,pl(0,l-1)); + } + return 0; +} + diff --git a/uva_cpp_clean/10739/10739-9.cpp b/uva_cpp_clean/10739/10739-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45ac1fe5d52aa012b66b89a96adcd3f3fdfc9641 --- /dev/null +++ b/uva_cpp_clean/10739/10739-9.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +const int MAX_LEN = 1e3 + 5; + +int memo[MAX_LEN][MAX_LEN]; +string s; + +int dp (int i, int j) { + if (i > j) return memo[i][j] = 0; + if (~memo[i][j]) return memo[i][j]; + if (s[i] == s[j]) return memo[i][j] = dp(i + 1, j - 1); + return memo[i][j] = 1 + min(dp(i + 1, j - 1), min(dp(i + 1, j), dp(i, j - 1))); +} + +int main () { + int tc; + cin >> tc; + for (int t = 1; t <= tc; t++) { + memset(memo, -1, sizeof memo); + cin >> s; + printf("Case %d: %d\n", t, dp(0, s.size() - 1)); + } + return (0); +} diff --git a/uva_cpp_clean/10742/10742-21.cpp b/uva_cpp_clean/10742/10742-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b83a7ddecdb325149b7b6819ddcaf6512c22a56 --- /dev/null +++ b/uva_cpp_clean/10742/10742-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10742 + Name: The New Rule in Euphomia + Problem: https://onlinejudge.org/external/107/10742.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MAXP 1000001 +int primes[80000], priment[MAXP], pnt; +bool notp[MAXP]={1,1}; + +int main(){ + for (int i=2; i>n && n) { + unsigned int result = 0, nh = n>>1; + for (int i=0; primes[i] <= nh; i++) + result += priment[n-primes[i]] - priment[primes[i]]; + + printf("Case %d: %u\n", cse++, result); + } +} diff --git a/uva_cpp_clean/10742/10742-9.cpp b/uva_cpp_clean/10742/10742-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3da0c34f6c4436de3e2154e8118b752902eb467 --- /dev/null +++ b/uva_cpp_clean/10742/10742-9.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int n, l, r, mid, ans, k, num, t; +bool p[1000000]; +vector prime; + +int binary(int x){ + num = prime[x]; + l = x; + r = 78498; + while(r - l > 1){ + mid = (l + r) / 2; + if(num + prime[mid] <= n) + l = mid; + else + r = mid; + } + return l - x; +} + +int main(){ + prime.push_back(2); + for(int i = 3; i < 1000000; i += 2) + if(!p[i]){ + for(int j = i + i; j < 1000000; j += i) + p[j] = true; + prime.push_back(i); + } + while(scanf("%d", &n), n){ + ans = 0; + for(int i = 0; i < 78498; i++){ + k = binary(i); + if(k == 0) + break; + ans += k; + } + printf("Case %d: %d\n", ++t, ans); + } + return(0); +} diff --git a/uva_cpp_clean/10746/10746-21.cpp b/uva_cpp_clean/10746/10746-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a220e45aeb02dd2b6285ec1d0ffb15e7d1272a5 --- /dev/null +++ b/uva_cpp_clean/10746/10746-21.cpp @@ -0,0 +1,104 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10746 + Name: Crime Wave - The Sequel + Problem: https://onlinejudge.org/external/107/10746.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +#define CLR(a, x) memset( a, x, sizeof( a ) ) +#define Inf (INT_MAX/2) +#define eps 1e-5 +// the maximum number of vertices + 1 +#define NN 1024 +int cap[NN][NN]; +double cost[NN][NN]; +int fnet[NN][NN], adj[NN][NN], deg[NN]; +int par[NN], d[NN]; +int pi[NN]; + +#define Pot(u,v) (d[u] + pi[u] - pi[v]) +bool dijkstra( int n, int s, int t ) +{ + for( int i = 0; i < n; i++ ) d[i] = Inf, par[i] = -1; + d[s] = 0; + par[s] = -n - 1; + while( 1 ) + { + int u = -1, bestD = Inf; + for( int i = 0; i < n; i++ ) if( par[i] < 0 && d[i] < bestD ) + bestD = d[u = i]; + if( bestD == Inf ) break; + + // relax edge (u,i) or (i,u) for all i; + par[u] = -par[u] - 1; + for( int i = 0; i < deg[u]; i++ ) + { + int v = adj[u][i]; + if( par[v] >= 0 ) continue; + if( fnet[v][u] && d[v] > Pot(u,v) - cost[v][u] - eps) + d[v] = Pot( u, v ) - cost[v][u] - eps, par[v] = -u-1; + if( fnet[u][v] < cap[u][v] && d[v] > Pot(u,v) + cost[u][v] + eps) + d[v] = Pot(u,v) + cost[u][v] + eps, par[v] = -u - 1; + } + } + for( int i = 0; i < n; i++ ) if( pi[i] < Inf ) pi[i] += d[i]; + return par[t] >= 0; +} +#undef Pot + +int mcmf3( int n, int s, int t, double &fcost ) +{ + CLR( deg, 0 ); + for( int i = 0; i < n; i++ ) + for( int j = 0; j < n; j++ ) + if( cap[i][j] || cap[j][i] ) adj[i][deg[i]++] = j; + CLR( fnet, 0 ); + CLR( pi, 0 ); + int flow = fcost = 0; + while( dijkstra( n, s, t ) ) + { + int bot = INT_MAX; + for( int v = t, u = par[v]; v != s; u = par[v = u] ) + bot = min(bot , fnet[v][u] ? fnet[v][u] : ( cap[u][v] - fnet[u][v] )); + for( int v = t, u = par[v]; v != s; u = par[v = u] ) + if( fnet[v][u] ) { fnet[v][u] -= bot; fcost -= bot * cost[v][u]; } + else { fnet[u][v] += bot; fcost += bot * cost[u][v]; } + flow += bot; + } + return flow; +} + + + +int main(){ + int n,m,i,j; + double t; + while(scanf("%d%d",&n,&m)==2 && n+m){ + memset(cap,0,sizeof(cap)); + for(i=0;i +/* +Problem: 10751 - Chessboard +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1692 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define PI acos(-1) +#define EPS 1e-10 + +int T, N; + +int main () { + scanf("%d", &T); + + double sqrt2 = sqrt(2.0); + + while ( T-- ){ + scanf("%d", &N); + + int x1 = (N-1)*4; + int x2 = N*N - x1; + + double ans = x1 + x2*sqrt2; + + printf("%.3f\n\n", (N==1)? 0 : ans); + } + + + return 0; +} +/* +Sample input:- +----------------- +3 +1 +2 +3 + +Sample output:- +----------------- +0.000 +4.000 +9.414 + +Resources:- +------------- + +Explanation: +--------------- + +length = x1*(1) + x2*(sqrt(2)) +length = x1 + x2*sqrt(2) --->> Obj. func. +Max(Z) = x1 + x2*sqrt(2) + +x1 --> no. moves to an edge adjacent cell +x2 --> no. moves to a corner adjacent cell + +distance to an edge adjacent cell equals 1, [try dist( (1,1), (1,0) )] +distance to a corner adjacent cell equals sqrt(2), [try dist( (1,1), (0,0) )] + +so, the total length is: + len = x1*1 + x2 * sqrt(2) + = x1 + x2 * sqrt(2) ==============>>> [1] + +knowing that the maximum number of moves is N*N +(as it's a squared board and we have to visit each cell only once) +So, + x1 + x2 = N*N ==============>>> [2] + +By analyzing the problem.. + ______________________________________ +|N :| 1 | 2 | 3 | 4 | 5 | +|X1 :| 0 | 4 | 8 | 12 | 16 | +|X2 :| 0 | 0 | 1 | 4 | 9 | +|ans:| 0 | 4 | 9.414 | 17.657 | 28.729 | + -------------------------------------- + +So, + X1 = N-1 * 4 ==============>>> [3] + +And from there we can easily calculate the length from [1], [2] and [3]. +*/ diff --git a/uva_cpp_clean/10751/10751-9.cpp b/uva_cpp_clean/10751/10751-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8e28cd99fd3925519dcacadde7f121757a2f9ab --- /dev/null +++ b/uva_cpp_clean/10751/10751-9.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int tc,n; +const double r2 = 2 * sin(acos(-1) / 4); + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + if(n == 1) puts("0.000"); + else printf("%.3lf\n",(n - 1) * 4 + (n - 2) * (n - 2) * r2); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10754/10754-8.cpp b/uva_cpp_clean/10754/10754-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a8ecf601071556599c00a9e5dde87da2ca8aec9 --- /dev/null +++ b/uva_cpp_clean/10754/10754-8.cpp @@ -0,0 +1,75 @@ +// Problem name: Fantastic Sequence +// Problem link: https://vjudge.net/problem/UVA-10754 +// Submission link: https://vjudge.net/solution/18143043 + +#include + +#define endl '\n' + +using namespace std; + +typedef long long ll; +typedef vector vll; +typedef vector matrix; + +matrix multiply(matrix &a, matrix &b, ll mod){ + int n = a.size(), m = a[ 0 ].size(), o = b[ 0 ].size(); + matrix mat(n, vll(o, 0)); + for(int i = 0 ; i < n ; ++i) + for(int k = 0 ; k < m ; ++k) + for(int j = 0 ; j < o ; ++j) + mat[ i ][ j ] = (mat[ i ][ j ] + a[ i ][ k ] * b[ k ][ j ]) % mod; + return mat; +} + +matrix pow(matrix m, ll p, ll mod){ + int len = m.size(); + matrix ans(len, vll(len, 0)); + for(int i = 0 ; i < len ; ans[ i ][ i ] = 1, ++i); + for( ; p ; p >>= 1){ + if(p & 1) + ans = multiply(ans, m, mod); + m = multiply(m, m, mod); + } + return ans; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + +#ifdef JoseA132 + freopen("01.in", "r", stdin); +#endif + + int t; + for(cin >> t ; t-- ; ){ + ll k, m, n; + cin >> k >> m >> n; + + matrix M(k + 1, vll(k + 1, 0)); + for(int i = 0 ; i <= k ; ++i) + cin >> M[ 0 ][ i ], M[ 0 ][ i ] = (M[ 0 ][ i ] % m + m) % m; + + for(int i = 1 ; i < k ; ++i) + M[ i ][ i - 1 ] = 1; + + if(k) + M[ k ][ k ] = 1; + + matrix F(k + 1, vll(1, 1)); + for(int i = k - 1 ; i >= 0 ; --i) + cin >> F[ i ][ 0 ], F[ i ][ 0 ] = (F[ i ][ 0 ] % m + m) % m; + + if(n < k) + cout << F[ k - n - 1 ][ 0 ] << endl; + else{ + M = pow(M, n - k + 1, m); + M = multiply(M, F, m); + cout << M[ 0 ][ 0 ] << endl; + } + + if(t) + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10755/10755-13.cpp b/uva_cpp_clean/10755/10755-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f56ae8a931cdabc85f64fcf854a9c2f5c62417d3 --- /dev/null +++ b/uva_cpp_clean/10755/10755-13.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 25 + +int main() +{ + ios::sync_with_stdio(0); + + long long a, b, c, t, result; + + cin >> t; + + while (t--) + { + cin >> a >> b >> c; + + long long map[MAX][MAX][MAX]; + + result = -1000000000000000; + + memset(map, 0, sizeof map); + + for (long long i = 1; i <= a; i++) + { + for (long long j = 1; j <= b; j++) + { + for (long long k = 1; k <= c; k++) + { + cin >> map[i][j][k]; + + map[i][j][k] += map[i][j][k - 1]; + map[i][j][k] += map[i][j - 1][k]; + map[i][j][k] += map[i - 1][j][k]; + + map[i][j][k] -= map[i][j - 1][k - 1]; + map[i][j][k] -= map[i - 1][j - 1][k]; + map[i][j][k] -= map[i - 1][j][k - 1]; + + map[i][j][k] += map[i - 1][j - 1][k - 1]; + } + } + } + + for (int i = 1; i <= a; i++) + { + for (int j = 1; j <= b; j++) + { + for (int k = 1; k <= c; k++) + { + for (int x = i; x <= a; x++) + { + for (int y = j; y <= b; y++) + { + for (int z = k; z <= c; z++) + { + long long sum = map[x][y][z]; + + sum -= map[x][y][k - 1]; + sum -= map[x][j - 1][z]; + sum -= map[i - 1][y][z]; + + sum += map[x][j - 1][k - 1]; + sum += map[i - 1][j - 1][z]; + sum += map[i - 1][y][k - 1]; + + sum -= map[i - 1][j - 1][k - 1]; + + result = max(result, sum); + } + } + } + } + } + } + + cout << result << endl; + + if (t) cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10755/10755-21.cpp b/uva_cpp_clean/10755/10755-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd95b0d50666389f34bf4d9476bc005718bca390 --- /dev/null +++ b/uva_cpp_clean/10755/10755-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10755 + Name: Garbage Heap + Problem: https://onlinejudge.org/external/107/10755.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +long long int X[30][30][30], S[30][30][30], x; +int main(){ + int T, A, B, C; + cin>>T; + while (T--) { + cin >> A >> B >> C; + long long mx = -(1LL<<63); + for (int i=1; i<=A; i++) + for (int j=1; j<=B; j++) + for (int k=1; k<=C; k++) { + cin >> X[i][j][k]; + if ((x = X[i][j][k]) > mx) + mx = x; + S[i][j][k] = x + S[i-1][j][k] + S[i][j-1][k] - S[i-1][j-1][k]; + } + if (mx < 0) + cout << mx << endl; + + else { + for (int i1=0; i1 mx) mx = s; + if (s < 0) s = 0; + } + } + cout << mx << endl; + } + if (T) putchar('\n'); + } +} diff --git a/uva_cpp_clean/10755/10755-6.cpp b/uva_cpp_clean/10755/10755-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18cb173713bf466bc26663c8a92beb2ba67a01ee --- /dev/null +++ b/uva_cpp_clean/10755/10755-6.cpp @@ -0,0 +1,54 @@ +/* + dynamic programming > max 2D range sum + difficulty: hard + date: 18/Jan/2020 + problem: max 3D range sum + hint: use max 2D range sum in two of the three dimensions and max 1D range sum (kadane) on the third dimension + by: @brpapa +*/ +#include +#define ll long long +#define INF (ll)0x3f3f3f3f3f3f3f3f +using namespace std; + +ll cube[22][22][22]; +ll accSum[22][22][22]; // accSum[a][i][j] = soma acumulada da a-ésima sub-matriz 2D [(0,0) .. (i,j)] + +// soma da a-ésima sub-matriz 2D [(l, c) .. (i, j)] em O(1) +ll rsq(int a, int l, int c, int i, int j) { + return accSum[a][i][j] + - (l > 0 ? accSum[a][l-1][j] : 0) + - (c > 0 ? accSum[a][i][c-1] : 0) + + (l > 0 && c > 0 ? accSum[a][l-1][c-1] : 0); +} + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int T; cin >> T; + while (T--) { + int A, B, C; cin >> A >> B >> C; + for (int a = 0; a < A; a++) + for (int b = 0; b < B; b++) + for (int c = 0; c < C; c++) { + cin >> cube[a][b][c]; + accSum[a][b][c] = cube[a][b][c] + + (b > 0? accSum[a][b-1][c] : 0) + + (c > 0? accSum[a][b][c-1] : 0) + - (b > 0 && c > 0? accSum[a][b-1][c-1] : 0); + } + + ll ans = -INF; + for (int l = 0; l < B; l++) for (int c = 0; c < C; c++) + for (int i = l; i < B; i++) for (int j = c; j < C; j++) { + // kadane nas A rsq's [(l,c) .. (i, j)] + ll sum = 0; + for (int a = 0; a < A; a++) { + sum = max(rsq(a, l, c, i ,j), sum + rsq(a, l, c, i, j)); + ans = max(ans, sum); + } + } + cout << ans << (T > 0? "\n\n": "\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10755/10755-9.cpp b/uva_cpp_clean/10755/10755-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af8ce0a33b097283bdb7e561a11cde3c7d68adaf --- /dev/null +++ b/uva_cpp_clean/10755/10755-9.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int tc, A, B, C; +long long v[25][25][25], cs[25][25][25], ans, sum; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d", &A, &B, &C); + for(int i = 1; i <= A; i++) + for(int j = 1; j <= B; j++) + for(int k = 1; k <= C; k++) + scanf("%lld", &v[i][j][k]), cs[i][j][k] = cs[i][j][k - 1] + cs[i][j - 1][k] - cs[i][j - 1][k - 1] + v[i][j][k]; + ans = v[1][1][1]; + for(int i = 1; i <= A; i++) + for(int j = 1; j <= B; j++) + for(int k = 1; k <= C; k++) + for(int rz = 0; rz <= C - k; rz++) + for(int ry = 0; ry <= B - j; ry++) + for(int rx = 0; rx <= A - i; rx++){ + sum = 0; + for(int x = i; x <= i + rx; x++) + sum += cs[x][j + ry][k + rz] - cs[x][j + ry][k - 1] - cs[x][j - 1][k + rz] + cs[x][j - 1][k - 1]; + ans = max(ans, sum); + } + printf("%lld\n", ans); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10759/10759-19.cpp b/uva_cpp_clean/10759/10759-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..181088bb3769229ef532f8fcd3f818fef76220d9 --- /dev/null +++ b/uva_cpp_clean/10759/10759-19.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; + +int main() { + unsigned long long grid[25][150]; + unsigned long long div[25][150]; + for (int i = 0; i < 25; i++) + for (int j = 0; j < 150; j++) + grid[i][j] = 0, div[i][j] = 1; + grid[1][1] = grid[1][2] = grid[1][3] = grid[1][4] = grid[1][5] = grid[1][6] = 1; + for (int i = 1, sum = 6; i < 24; i++, sum += 6) { + for (int j = 0; j < 150 && j <= sum; j++) { + for (int l = 1; l <= 6; l++) { + grid[i+1][j+l] += grid[i][j]; + } + } + } + + for (int i = 1, sum = 6; i < 25; i++, sum += 6) { + for (int j = sum; j >= 0; j--) { + int div2 = i, div3 = i; + if (j != 0) grid[i][j-1] += grid[i][j]; + while (grid[i][j] != 0 && div2 > 0 && grid[i][j]%2 == 0) grid[i][j] /= 2, div2--; + while (grid[i][j] != 0 && div2 > 0 && grid[i][j]%3 == 0) grid[i][j] /= 3, div3--; + div[i][j] = pow(2, div2)*pow(3,div3); + if (div[i][j] == grid[i][j]) div[i][j] = grid[i][j] = 1; + } + } + + int n, x; + while (cin >> n >> x && !(n == 0 && x == 0)) { + cout << grid[n][x]; + if (grid[n][x] != 0 && div[n][x] != 1) cout << "/" << div[n][x]; + cout << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10763/10763-17.cpp b/uva_cpp_clean/10763/10763-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f645a2acf1a14f46acaa3312de806994aa057ad --- /dev/null +++ b/uva_cpp_clean/10763/10763-17.cpp @@ -0,0 +1,25 @@ +/** + * AOAPC II Exercise 5-4 Foreign Exchange + * 11 WA 20 AC // use multiset + */ +#include +using namespace std; +int main() +{ + int n; + while ((cin >> n) && n) + { + multiset> tar_ori; + while(n--) + { + int ori, tar; + cin >> ori >> tar; + multiset>::iterator iter; + if ((iter = tar_ori.find({tar, ori})) != tar_ori.end()) + tar_ori.erase(iter); + else + tar_ori.insert({ori, tar}); + } + cout << (tar_ori.empty() ? "YES" : "NO") << "\n"; + } +} diff --git a/uva_cpp_clean/10763/10763-19.cpp b/uva_cpp_clean/10763/10763-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b1f4f10a12674086d8b940170f3def6483666ca --- /dev/null +++ b/uva_cpp_clean/10763/10763-19.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; + +int main() { + unordered_map exchanges; + int n; + while (cin >> n && n != 0) { + exchanges.clear(); + for (int i = 0; i < n; i++) { + int origin, target; + cin >> origin >> target; + exchanges[origin]++; + exchanges[target]--; + } + bool ok = true; + for (auto it = exchanges.begin(); it != exchanges.end() && ok; ++it) { + ok = ok && (it->second == 0); + } + if (ok) printf("YES\n"); + else printf("NO\n"); + } +} diff --git a/uva_cpp_clean/10763/10763-21.cpp b/uva_cpp_clean/10763/10763-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f1c5b9407f6be8bcc3b603ec52fbdec85a42a85 --- /dev/null +++ b/uva_cpp_clean/10763/10763-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10763 + Name: Foreign Exchange + Problem: https://onlinejudge.org/external/107/10763.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +map > mp; +int main(){ + int n, u, v; + while (cin>>n && n) { + mp.clear(); + int cnt = 0; + for (int i=0; i>u>>v; + + auto t = mp[v]; + if (t[u]>0) { t[u]--; cnt--; } + else { mp[u][v]++; cnt++; } + } + + puts(cnt ? "NO" : "YES"); + } +} diff --git a/uva_cpp_clean/10765/10765-13.cpp b/uva_cpp_clean/10765/10765-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21678a9f76faef7fed952c23f51ac5c1723c3034 --- /dev/null +++ b/uva_cpp_clean/10765/10765-13.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 10010 + +class comparator +{ + +public: + + bool operator()(pair &child1, pair &child2) + { + if (child1.first == child2.first) + { + return child1.second > child2.second; + } + + else + { + return child1.first < child2.first; + } + } +}; + +int n, m, dfsCount; +int root, rootChildren; +bool visited[MAX]; +vector < vector > graph; +int points[MAX], num[MAX], low[MAX], parent[MAX]; +priority_queue < pair , vector < pair >, comparator > pq; + +void initilalize(int n) +{ + rootChildren = root = dfsCount = 0; + + graph.clear(); + graph.resize(n); + + while (!pq.empty()) pq.pop(); + + memset(num, 0, sizeof num); + memset(low, 0, sizeof low); + memset(points, 0, sizeof points); + memset(parent, -1, sizeof parent); + memset(visited, 0, sizeof visited); +} + +void dfs(int start) +{ + visited[start] = true; + + low[start] = num[start] = dfsCount++; + + for (int i = 0; i < graph[start].size(); i++) + { + int child = graph[start][i]; + + if (!visited[child]) + { + parent[child] = start; + + if (start == root) rootChildren++; + + dfs(child); + + if (low[child] >= num[start]) points[start]++; + + low[start] = min(low[start], low[child]); + } + + else if (child != parent[start]) low[start] = min(low[start], num[child]); + } +} + +int main() +{ + while (cin >> n >> m, n != 0 || m != 0) + { + initilalize(n); + + int x, y; + + while (cin >> x >> y, x != -1 || y != -1) + { + graph[x].push_back(y); + + graph[y].push_back(x); + } + + for (int i = 0; i < n; i++) + { + if (!visited[i]) + { + root = i; + + dfs(i); + + points[i] = (rootChildren > 1); + } + } + + for (int i = 0; i < n; i++) + { + pq.push(make_pair(points[i] + 1, i)); + } + + for (int i = 0; i < m; i++) + { + cout << pq.top().second << " " << pq.top().first << endl; + + pq.pop(); + } + + cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10765/10765-21.cpp b/uva_cpp_clean/10765/10765-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d068833f03e3c1c82b814a026d64689ea85ae81 --- /dev/null +++ b/uva_cpp_clean/10765/10765-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10765 + Name: Doves and bombs + Problem: https://onlinejudge.org/external/107/10765.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXN 10001 +list adj[MAXN]; +int low[MAXN], depth[MAXN], par[MAXN], pigv[MAXN], ord[MAXN]; +bool comp(int i, int j) { return pigv[i] > pigv[j]; } + +void dfs(int u, int d) { + low[u] = depth[u] = d; + int childs = 0, + pigval = 0; + + for (int v: adj[u]) + if (par[v] < 0) { + childs++; + par[v] = u; + dfs(v, d+1); + if (low[v] >= depth[u]) + pigval++; + else if (low[v] < low[u]) + low[u] = low[v]; + } + else if (v != par[u]) + low[u] = min(low[u], depth[v]); + + pigv[u] = !d ? childs-1 : pigval; +} + + +int main(){ + int n, b, u, v; + while (scanf("%d%d", &n, &b)==2 && (n||b)) { + for (int i=0; i=0 && v>=0)) { + adj[u].push_back(v); + adj[v].push_back(u); + } + + int cnt = 0; + for (int i=0; i + +using namespace std; + +const int MAX_V = 10010; + +int V, M, a, b, dfsRoot, rootChildren, dfsCounter, dfs_low[MAX_V], dfs_num[MAX_V], dfs_parent[MAX_V], Articulation_Point[MAX_V]; +vector AdjList[MAX_V]; +vector < pair > ans; + +void dfs(int u){ + dfs_low[u] = dfs_num[u] = ++dfsCounter; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not dfs_num[v] ){ + dfs_parent[v] = u; + dfs(v); + if( u == dfsRoot ) rootChildren++; + if( dfs_low[v] >= dfs_num[u] ) Articulation_Point[u]++; + dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + else if( v != dfs_parent[u] ) + dfs_low[u] = min(dfs_low[u], dfs_num[v]); + } +} + +void ArticulationPoints(){ + fill(dfs_low, dfs_low + V, 0); + fill(dfs_num, dfs_num + V, 0); + fill(dfs_parent, dfs_parent + V, -1); + fill(Articulation_Point, Articulation_Point + V, 0); + dfsCounter = 0; + dfsRoot = 0, rootChildren = 0; + dfs(dfsRoot); + Articulation_Point[dfsRoot] = ( rootChildren > 1 ); +} + +void solve(){ + ArticulationPoints(); + for(int u = 0; u < V; u++) + ans.push_back( make_pair(u, Articulation_Point[u] + 1) ); + sort(ans.begin(), ans.end(), [](const pair & x, const pair & y){ + if( x.second != y.second ) return x.second > y.second; + return x.first < y.first; + }); + for(int i = 0; i < M; i++) printf("%d %d\n", ans[i].first, ans[i].second); + putchar('\n'); +} + +void read(){ + while(scanf("%d %d", &a, &b), ~a and ~b){ + AdjList[a].push_back(b); + AdjList[b].push_back(a); + } +} + +void clear(){ + ans.clear(); + for(int u = 0; u < V; u++) AdjList[u].clear(); +} + +int main(){ + while(scanf("%d %d", &V, &M), V | M){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/10773/10773-11.cpp b/uva_cpp_clean/10773/10773-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8792b0577664d18dc7bcbea76f76a358259e7e2 --- /dev/null +++ b/uva_cpp_clean/10773/10773-11.cpp @@ -0,0 +1,63 @@ +#include +/* +Problem: 10773 - Back to Intermediate Math +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1714 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int T, D, V, U, testcase = 0; + +int main () { + + scanf("%d", &T); + + while ( T-- ){ + scanf("%d%d%d", &D, &V, &U); + + printf("Case %d: ", ++testcase); + + double time_diff = D/(double)U - (double)D/sqrt(U*U - V*V); + + if (U-V <= 0 || U == 0 || time_diff == 0) { + printf("can't determine\n"); + continue; + } + + + printf("%.3lf\n", fabs(time_diff)); + } + + return 0; +} +/* +Sample input:- +----------------- +3 +8 5 6 +1 2 3 +1 5 6 + +Sample output:- +----------------- +Case 1: 1.079 +Case 2: 0.114 +Case 3: 0.135 + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10773/10773-14.cpp b/uva_cpp_clean/10773/10773-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d94e6933d41196173f4ddab0aa72eb52a9cd7914 --- /dev/null +++ b/uva_cpp_clean/10773/10773-14.cpp @@ -0,0 +1,70 @@ +/*************************************************** + * Problem name : 10773 Back to Intermediate Math.cpp + * Problem Link : https://uva.onlinejudge.org/external/107/10773.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-27 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc; + double d, u, v; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%lf %lf %lf", &d, &v, &u); + if(u == 0 || v == 0 || v>=u) printf("Case %d: can't determine\n", t); + else { + double short_time = (d*1.0)/(u*1.0); + double short_path = (1.0* d)/((sqrt(u*u - v*v))*1.0); + printf("Case %d: %.3lf\n", t, fabs(short_path - short_time)); + } + } + return 0; +} diff --git a/uva_cpp_clean/10773/10773-19.cpp b/uva_cpp_clean/10773/10773-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a09e7bbfa2a46c925f8bf074ba377fdf34c9625 --- /dev/null +++ b/uva_cpp_clean/10773/10773-19.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +using namespace std; + +int main() { + int N; + scanf("%d", &N); + for (int i = 1; i <= N; i++) { + double t, t1, t2, d, u, v; + scanf("%lf %lf %lf", &d, &v, &u); + bool ok = (u > v) && (u != 0) && (v != 0); + if (ok) { + t1 = d/u; + t2 = d/sqrt(u*u - v*v); + t = fabs(t1-t2); + } + printf("Case %d: ", i); + if (ok) printf("%.3lf\n", t); + else printf("can't determine\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10773/10773-21.cpp b/uva_cpp_clean/10773/10773-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a27362467cc9a92aa0092fd23b171669c4f4822 --- /dev/null +++ b/uva_cpp_clean/10773/10773-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10773 + Name: Back to Intermediate Math + Problem: https://onlinejudge.org/external/107/10773.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int T; + double d, u, v; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin>>d>>v>>u; + printf("Case %d: ", cse); + if (v>=u || v==0) puts("can't determine"); + else printf("%.3f\n", d*(1/sqrt(u*u-v*v) - 1/u)); + } +} diff --git a/uva_cpp_clean/10773/10773-9.cpp b/uva_cpp_clean/10773/10773-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c45d14181828c181fcfc53f2cedc0258ece6ff0d --- /dev/null +++ b/uva_cpp_clean/10773/10773-9.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main(){ + int c, t = 0; + float t1, t2, d, v, u; + bool ok; + scanf("%d", &c); + while(c--){ + scanf("%f %f %f", &d, &v, &u); + ok = true; + if(v >= u) + ok = false; + else{ + t1 = d / u; + t2 = d / sqrt(u * u - v * v); + if(fabs(t1 - t2) < 1e-12) + ok = false; + } + if(ok) + printf("Case %d: %.3f\n", ++t, abs(t1 - t2)); + else + printf("Case %d: can't determine\n", ++t); + } + return(0); +} diff --git a/uva_cpp_clean/10779/10779-21.cpp b/uva_cpp_clean/10779/10779-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c080171c0a486ec7eb45fca11f39199669d7d2e2 --- /dev/null +++ b/uva_cpp_clean/10779/10779-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10779 + Name: Collectors Problem + Problem: https://onlinejudge.org/external/107/10779.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 1000 +#define inf 1000000000 + +int max_flow(int n,int mat[][MAXN],int source,int sink,int flow[][MAXN]){ + int pre[MAXN],que[MAXN],d[MAXN],p,q,t,i,j; + if (source==sink) return inf; + for (i=0;i0) + flow[pre[i]-1][i]+=d[sink],i=pre[i]-1; + else + flow[i][-pre[i]-1]-=d[sink],i=-pre[i]-1; + } + for (j=i=0;i maximum flow + difficulty: medium + date: 10/Aug/2020 + hint: each friend receives 1 flow unit of a sticker and offers a flow of other sticker; Bob also offers, but he receives 1 flow unit of each sticker; maximized it + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +vector>> adj_list; // adj_list[u] = {{v, capacity}, ...} +int V; + +/* */ +vector>> new_adj_list; // new_adj_list[u] = {{v, remaining_capacity, flow}, ...} +vector> parent; // parent[v] = {u, índice de new_adj_list[u]} +vector> rev_idx; + +/* O(V+E) - find the shortest augmenting path (in terms of edges) and returns your bottleneck, or 0, if there is no more an augmenting path */ +ll bfs(int s, int t) { + vector seen(V, false); + parent.assign(V, {-1, -1}); + + queue q; q.push(s); + + while (!q.empty()) { + int u = q.front(); q.pop(); + seen[u] = true; + + if (u == t) break; + + // para cada aresta u -c-> v, com c > 0 + for (int i = 0; i < new_adj_list[u].size(); i++) { + int v; ll c, _; tie(v, c, _) = new_adj_list[u][i]; + + if (!seen[v] && c > 0) { + parent[v] = {u,i}; + q.push(v); + } + } + } + + // t não foi alcançado + if (parent[t].first == -1) return 0; + + // procura pela menor capacidade restante (gargalo), iterando cada aresta u -> v do caminho aumentativo encontrado + ll min_c = INT_MAX; + for (int v = t, u; parent[v].first != -1; v = u) { + u = parent[v].first; + int i = parent[v].second; + min_c = min(min_c, get<1>(new_adj_list[u][i])); + } + return min_c; +} + +/* O(V*E*E) - returns the max flow */ +ll edmonds_karp(int s, int t) { + new_adj_list.assign(V, vector>()); + rev_idx.assign(V, vector()); + + // para cada aresta u -c-> v do grafo original + for (int u = 0; u < V; u++) + for (auto adj : adj_list[u]) { + int v, c; tie(v, c) = adj; + + rev_idx[u].push_back(new_adj_list[v].size()); + rev_idx[v].push_back(new_adj_list[u].size()); + + new_adj_list[u].push_back({v, c, 0}); // forward edge + new_adj_list[v].push_back({u, 0, 0}); // backward edge + } + + ll acc_flow = 0, f; + + // enquanto existir augmenting path + while ((f = bfs(s, t))) { + acc_flow += f; + + // atualiza os grafos com o min_c, para cada aresta u -> v do caminho aumentativo encontrado + for (int v = t, u; parent[v].first != -1; v = u) { + u = parent[v].first; + int i = parent[v].second; + int j = rev_idx[u][i]; + + get<1>(new_adj_list[u][i]) -= f; // remaining capacity of u -> v + get<1>(new_adj_list[v][j]) += f; // remaining capacity of v -> u + + get<2>(new_adj_list[u][i]) += f; // flow of u -> v + get<2>(new_adj_list[v][j]) -= f; // flow of v -> u + } + } + return acc_flow; +} + +int main() { + int t = 1, T; cin >> T; + while (T--) { + int N, M; cin >> N >> M; + V = M+N+1; + adj_list.assign(V, vector>()); + + // vértice 0: oferta do Bob + // vértices [1..M] = stickers + // vértice M+1: demanda do Bob + // vértices [M+2, V-1] = amigos de Bob + + vector qty(30, 0); int k; cin >> k; while (k--) { int m; cin >> m; qty[m]++; } + for (int m = 1; m <= M; m++) { + adj_list[m].push_back({M+1, 1}); + if (qty[m] > 0) adj_list[0].push_back({m, qty[m]}); + } + + for (int n = M+2; n < V; n++) { + vector qty(30, 0); int k; cin >> k; while (k--) { int m; cin >> m; qty[m]++; } + for (int m = 1; m <= M; m++) { + if (qty[m] == 0) adj_list[m].push_back({n, 1}); + else adj_list[n].push_back({m, qty[m]-1}); + } + } + printf("Case #%d: %ld\n", t++, edmonds_karp(0, M+1)); + } + return 0; +} diff --git a/uva_cpp_clean/10780/10780-19.cpp b/uva_cpp_clean/10780/10780-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39cc26393452ee9ec242de1675ca10e69c419f38 --- /dev/null +++ b/uva_cpp_clean/10780/10780-19.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; +typedef unordered_map uii; + +ll sieve_size; +bitset<100000001> bs; +vi primes; +uii fact[10001]; +uii des[10001]; + +inline void sieve(ll upperbound = 10002) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline void primeFactors(ll n, uii & factors) { + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors[p] = c; + p=primes[++idx]; + } + if (n!=1) factors[n] = 1; +} + + +int main() { + sieve(); + primeFactors(2, des[2]); + fact[2] = des[2]; + for (int i = 3; i < 10000; i++) { + primeFactors(i, des[i]); + for (auto it = fact[i-1].begin(); it != fact[i-1].end(); ++it) { + fact[i][it->first] = it->second; + } + for (auto it = des[i].begin(); it != des[i].end(); ++it) { + fact[i][it->first] += it->second; + } + } + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + int m, n; + cin >> m >> n; + int mini = INT_MAX; + for (auto it = des[m].begin(); it != des[m].end(); ++it) { + mini = min(mini, fact[n][it->first] / it->second); + } + printf("Case %d:\n", c); + if (mini != 0) printf("%d\n", mini); + else printf("Impossible to divide\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10780/10780-9.cpp b/uva_cpp_clean/10780/10780-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39ab26cf568db38accb1935ed134f5ca1c4ad623 --- /dev/null +++ b/uva_cpp_clean/10780/10780-9.cpp @@ -0,0 +1,62 @@ +#include + +#define SIZE 10010 + +using namespace std; + +bool is_prime[SIZE]; +vector prime; +int tc, M, N, FFactors[SIZE][SIZE], ans; +vector < pair > Factor; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + for(long long it1 = 2; it1 < SIZE; it1++) + if( is_prime[it1] ){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +void FactorialFactors(){ + for(int num = 2; num < SIZE; num++){ + int pib = num; + for(int it = 0; it < prime.size(); it++){ + int ct = 0; + while( pib % prime[it] == 0 ) pib /= prime[it], ct++; + FFactors[num][ prime[it] ] = FFactors[num - 1][ prime[it] ] + ct; + } + } +} + +void GetFactors(int n){ + Factor.clear(); + for(int it = 0; it < prime.size() and prime[it] <= n; it++) + if( n % prime[it] == 0 ){ + int ct = 0; + while( n % prime[it] == 0 ) n /= prime[it], ct++; + Factor.push_back( make_pair( prime[it], ct ) ); + } +} + +void solve(){ + ans = INT_MAX; + for(int it = 0; it < Factor.size(); it++) + ans = min(ans, FFactors[N][Factor[it].first] / Factor[it].second); + if( ans == 0 ) puts("Impossible to divide"); + else printf("%d\n", ans); +} + +int main(){ + sieve(); + FactorialFactors(); + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d %d", &M, &N); + GetFactors(M); + printf("Case %d:\n", t); + solve(); + } + return(0); +} diff --git a/uva_cpp_clean/10783/10783-19.cpp b/uva_cpp_clean/10783/10783-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff4e607dd0e90141b3813d05cee138ec89fde038 --- /dev/null +++ b/uva_cpp_clean/10783/10783-19.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +int proc(int n) { + return (((n+1)*n)/2) - ((((n/2)+1)*(n/2))/2)*2; +} + +int main() { + int n; + scanf("%d", &n); + for (int i = 1; i <= n; i++) { + int a, b; + scanf("%d", &a); + scanf("%d", &b); + int sum = proc(b)-proc(a) + (a%2)*a; + printf("Case %d: %d\n", i, sum); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10783/10783-21.cpp b/uva_cpp_clean/10783/10783-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1249e6b4d8a529ec3c4275edbc42b70297b2cf49 --- /dev/null +++ b/uva_cpp_clean/10783/10783-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10783 + Name: Odd Sum + Problem: https://onlinejudge.org/external/107/10783.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int N, n; + cin >> N; + for (int n = 1; n <= N; n++) { + int a, b, result; + cin >> a >> b; + if (a % 2 == 0) a++; + if (b % 2 == 0) b--; + if (a > b) result = 0; + else if (a == b) result = a; + else result = ((a + b) * (b - a + 2)) >> 2; + + printf("Case %d: %d\n", n, result); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10783/10783-3.cpp b/uva_cpp_clean/10783/10783-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bac73dc878da3e4218a38b31577704e34cf2623 --- /dev/null +++ b/uva_cpp_clean/10783/10783-3.cpp @@ -0,0 +1,27 @@ +#include + +int main() +{ + int t,a,b; + int sum, cases; + + scanf("%d", &t); + cases = 0; + while ( t-- ) + { + scanf("%d %d",&a,&b); + sum = 0; + if ( a % 2 == 0 ) + a++; + + while ( a <= b ) + { + sum += a; + a += 2; + } + + printf("Case %d: %d\n", ++cases, sum); + } +} + + diff --git a/uva_cpp_clean/10783/10783-5.cpp b/uva_cpp_clean/10783/10783-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b2d8ab7d77f819e7ce0a00232d03f3e04912c88e --- /dev/null +++ b/uva_cpp_clean/10783/10783-5.cpp @@ -0,0 +1,24 @@ +#include +int main() +{ + int a,b,c,s,i,j; + while (scanf("%d",&c)==1) + { + for(i=1;i<=c;i++) + { + scanf("%d %d",&a,&b); + + s=0; + if(a%2==0) + a=a+1; + + for(j=a;j<=b;j=j+2) + s+=j; + + printf("Case %d: %d\n",i,s); + } + } + return 0; +} + + diff --git a/uva_cpp_clean/10783/10783-6.cc b/uva_cpp_clean/10783/10783-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..dbdb67224e86a584ee7172bd037cd22e76411a52 --- /dev/null +++ b/uva_cpp_clean/10783/10783-6.cc @@ -0,0 +1,14 @@ +// https://uva.onlinejudge.org/external/107/10783.pdf +#include +using namespace std; +int main(){ + int t,a,b; + cin>>t; + for(int i=1;i<=t;i++){ + cin>>a>>b; + int s=0; + for(int j=a;j<=b;j++) + if(j%2)s+=j; + cout<<"Case "< + +using namespace std; + +int main(){ + int c, a, b, t = 0, oddsum; + scanf("%d", &c); + while(c--){ + scanf("%d\n%d", &a, &b); + oddsum = 0; + for(int i = a; i <= b; i++) + if(i & 1) + oddsum += i; + printf("Case %d: %d\n", ++t, oddsum); + } + return(0); +} diff --git a/uva_cpp_clean/10784/10784-13.cpp b/uva_cpp_clean/10784/10784-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..588345d5c132c39ce043546084533a94067b7c5c --- /dev/null +++ b/uva_cpp_clean/10784/10784-13.cpp @@ -0,0 +1,17 @@ +#include +#include +#include + +using namespace std; + +typedef long long ll; + +int main() +{ + ll n, c = 1; + + while (cin >> n && n != 0) + { + cout << "Case " << c++ << ": " << (ll)ceil((sqrt((long double)((8 * n) + 9)) + 3) / 2) << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10784/10784-19.cpp b/uva_cpp_clean/10784/10784-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2c46ea0c95e8a311c0e31ff98482b1ce36f7ec2 --- /dev/null +++ b/uva_cpp_clean/10784/10784-19.cpp @@ -0,0 +1,14 @@ +#include +#include +using namespace std; + +int main() { + long long N; + int c = 1; + while (cin >> N && N != 0) { + long long n = ceil((3 + sqrt(9+8*N))/2); + printf("Case %d: %lld\n", c, n); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/10784/10784-21.cpp b/uva_cpp_clean/10784/10784-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c4e7789d00aa61c23eb664fabb6a057bf7bb6f --- /dev/null +++ b/uva_cpp_clean/10784/10784-21.cpp @@ -0,0 +1,23 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10784 + Name: Diagonal + Problem: https://onlinejudge.org/external/107/10784.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int cse=1; + long long n; + while (cin>>n && n) + printf("Case %d: %d\n", cse++, (int)ceil(1+(1+sqrt(9+8*n))/2)); +} diff --git a/uva_cpp_clean/10784/10784-5.cpp b/uva_cpp_clean/10784/10784-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a20eef1396127dee78ac17a94859d0ade5dddf6d --- /dev/null +++ b/uva_cpp_clean/10784/10784-5.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + ll n,no=0,s; + while(sc("%lld",&n)==1&&n>0) + { + s=ceil((3+sqrt(9+8*n))/2); + pf("Case %lld: %lld\n",++no,s); + } + + return 0; +} diff --git a/uva_cpp_clean/10785/10785-18.cpp b/uva_cpp_clean/10785/10785-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..751cbbd979ac69901549d642529881c38b371447 --- /dev/null +++ b/uva_cpp_clean/10785/10785-18.cpp @@ -0,0 +1,43 @@ +#include +#include + +using namespace std; + +const char table[]="AJAJAJAJAJASASASASASABABABABABAKAKAKAKAKATUTUTUTUTUCUCUCUCUCULULULULULUDUDUDUDUDUMUMEMEMEMEVEVEVEVEVENENENENENEWEWEWEWEWEFEFEFOFOFOXOXOXOXOXOGOGOGOGOGOPOPOPOPOPOYOYOYOYIYIHIHIHIHIHIQIQIQIQIQIZIZIZIZIZIRIRIRIRIR"; + +int main(){ + + int N, L, cases = 0; + cin >> N; + while(N-- > 0){ + cin >> L; + char ans[300]; + char vo[200]; + char co[200]; + int index; + index = 0; + for(int i = 1; i < L; i+=2){ + co[index++] = table[i]; + } + co[index] = '\0'; + sort(co, co+index); + index = 0; + for(int i = 0; i < L; i+=2){ + vo[index++] = table[i]; + } + vo[index] = '\0'; + sort(vo, vo+index); + index = 0; + for(int i = 1; i < L; i+=2){ + ans[i] = co[index++]; + } + index = 0; + for(int i = 0; i < L; i+=2){ + ans[i] = vo[index++]; + } + ans[L] = '\0'; + cases++; + printf("Case %d: %s\n", cases, ans); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10785/10785-21.cpp b/uva_cpp_clean/10785/10785-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..652de58ee9545a0cf2aa6748d3cb74b883be9591 --- /dev/null +++ b/uva_cpp_clean/10785/10785-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10785 + Name: The Mad Numerologist + Problem: https://onlinejudge.org/external/107/10785.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char cons[] = "JSBKTCLDMVNWFXGPYHQZR", // 5 + vows[] = "AUEOI"; // 21 +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cout << "Case " << cse << ": "; + cin >> n; + int cn=0, vn=0; + vector v1, v2; + for (int k=0, i=0, j=0; k +#include +#include +using namespace std; + +typedef long long ll; + +ll sieve_size; +bitset<10000010> bs; +unordered_set primes; + +void sieve(ll upperbound = 2002) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.insert((int)i); + } +} + +int main() { + sieve(); + int bucket[128], T; + cin >> T; + for (int c = 1; c <= T; c++) { + for (int i = 0; i < 128; i++) bucket[i] = 0; + string s; + cin >> s; + for (int i = 0; i < s.length(); i++) bucket[s[i]]++; + printf("Case %d: ", c); + bool empty = true; + for (int i = 0; i < 128; i++) if (primes.count(bucket[i])) printf("%c", i), empty = false; + if (empty) printf("empty"); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10789/10789-20.cpp b/uva_cpp_clean/10789/10789-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fadd67f00a4dbc870483d84fac5dbc4d8c037c01 --- /dev/null +++ b/uva_cpp_clean/10789/10789-20.cpp @@ -0,0 +1,52 @@ +#include +#include + +const char alpha_numerals[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; +int counter[65]; +char line[2005]; + +bool is_prime(int num) +{ + if( num==0 || num==1 ) return false; + for(int J=2 ; J*J<=num ; J++) + if( num%J==0 ) + return false; + return true; +} + +int main() +{ + int tc; + char dump; + + scanf("%d%c", &tc, &dump); + int ncase = 0; + while( tc-- ) + { + gets(line); + + for(int I=0 ; I<65 ; I++) counter[I] = 0; + + for(int I=0 ; I='0' && line[I]<='9' ) counter[line[I]-'0']++; + if( line[I]>='A' && line[I]<='Z' ) counter[line[I]-'A'+10]++; + if( line[I]>='a' && line[I]<='z' ) counter[line[I]-'a'+10+26]++; + } + + printf("Case %d: ", ++ncase); + bool flag = true; + for(int I=0 ; I<65 ; I++) + { + if( is_prime(counter[I]) ) + { + printf("%c", alpha_numerals[I]); + flag = false; + } + } + + if( flag ) printf("empty"); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10789/10789-21.cpp b/uva_cpp_clean/10789/10789-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e730e3477f4d00d2d630f40daf2786da5ac179b --- /dev/null +++ b/uva_cpp_clean/10789/10789-21.cpp @@ -0,0 +1,61 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10789 + Name: Prime Frequency + Problem: https://onlinejudge.org/external/107/10789.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define N 3000 +unsigned int prime[N / 64]; +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) +#define rP(n) (prime[n>>6]&=~(1<<((n>>1)&31))) +void sieve() +{ + memset( prime, -1, sizeof( prime ) ); + + unsigned int i; + unsigned int sqrtN = ( unsigned int )sqrt( ( double )N ) + 1; + for( i = 3; i < sqrtN; i += 2 ) if( gP( i ) ) + { + unsigned int i2 = i + i; + for( unsigned int j = i * i; j < N; j += i2 ) rP( j ); + } +} + + +int main(){ + sieve(); + char line[3000]; + int cc[128]; + int T, f; + cin>>T; + for(int cse=1; cse<=T; cse++) { + cin>>line; + for (int i=0; i<26; i++) + cc['a'+i] = cc['A'+i] = cc['0'+i] = 0; + + for (int i=0; line[i]; i++) + cc[line[i]]++; + + bool empty = true; + printf("Case %d: ", cse); + for (int i='0'; i<='z'; i++) + if ((f = cc[i])>1) + if (f==2 || (f&1 && gP(f))) { + cout << (char)i; + empty = false; + } + cout << (empty ? "empty\n" : "\n"); + } +} diff --git a/uva_cpp_clean/10790/10790-18.cpp b/uva_cpp_clean/10790/10790-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e17c9ea606865b0a5adc079c77c1cdd8f57ba3b2 --- /dev/null +++ b/uva_cpp_clean/10790/10790-18.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int main(){ + int cont = 0, a , b; + while(scanf("%d %d", &a, &b) && a != 0 && b!= 0){ + cont++; + printf("Case %d: %lld\n", cont, (long long)a*b*(a-1)*(b-1)/4); + } + return 0; +} +/* + Para entender la solucion hay que explicarlo con un ejemplo, imagina que en la linea de arriba tienes un punto + llamado A1 (ubicalo en el centro, cosa que este abajo de B2), en la linea de abajo tienes 4 puntos + llamados: B1, B2, B3, B4. Si los conectas te daras cuenta de que no se intersectan ninguna linea, bien, ahora + agrega un punto A2 a la linea de arriba y observa que en una linea intersecta 1 vez, con otra intersecta 2 veces y + en la tercera intersecta 3 veces, si no lo viste, entonces agregar otro punto A3 a la linea de arriba y observa + de nuevo, aqui obtendras una linea que intersecta 6 veces, otra con 4, otra con 2, entonces obtenemos esto: + Suma de las intersecciones: + 0 + 3 + 2 + 1 = (3 + 2 + 1) * 1 + 6 + 4 + 2 = (3 + 2 + 1) * 2 + son secuencias triangulares de manera vertical y horizontal!. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10790/10790-21.cpp b/uva_cpp_clean/10790/10790-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c371be1fd6bac66ede9caacbc7920d2c9b97a8c8 --- /dev/null +++ b/uva_cpp_clean/10790/10790-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10790 + Name: How Many Points of Intersection? + Problem: https://onlinejudge.org/external/107/10790.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + long long n, m; + for (int cse=1; cin>>n>>m && (n||m); ++cse) + cout << "Case " << cse << ": " << (n*(n-1)/2*m*(m-1)/2) << endl; +} diff --git a/uva_cpp_clean/10791/10791-19.cpp b/uva_cpp_clean/10791/10791-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b454f382691aca5d73cf128c07c85e6c8e272710 --- /dev/null +++ b/uva_cpp_clean/10791/10791-19.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<100000001> bs; +vi primes; + +inline void sieve(ll upperbound = 50000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline void primeFactors(ll n, vii & factors) { + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +int main() { + sieve(); + ll N, c = 1; + vii factors; + while (cin >> N && N != 0) { + factors.clear(); + primeFactors(N, factors); + ll s = 0; + for (int i = 0; i < factors.size(); i++) { + ll p = 1; + for (int j = 0; j < factors[i].second; j++) + p *= factors[i].first; + s += p; + } + if (factors.size() == 1) s++; + if (N == 1) s = 2; + printf("Case %lld: %lld\n", c++, s); + } + return 0; +} diff --git a/uva_cpp_clean/10791/10791-21.cpp b/uva_cpp_clean/10791/10791-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4b90e98ef1103e904f570780c0fce308c947544 --- /dev/null +++ b/uva_cpp_clean/10791/10791-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10791 + Name: Minimum Sum LCM + Problem: https://onlinejudge.org/external/107/10791.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int solve(int n) { + if (n == 1) return 1; + + int sq = sqrt(n), sum = 0; + for (int i=2; i<=sq; ++i) + if (n % i == 0) { + int x = 1; + while (n % i == 0) + x *= i, + n /= i; + sum += x; + sq = sqrt(n); + } + return n>1 ? sum + n : sum; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + for (int cse=1; cin >> n && n; ++cse) { + unsigned int sum = solve(n); + if (sum == n) ++sum; + cout << "Case " << cse << ": " << sum << endl; + } +} diff --git a/uva_cpp_clean/10791/10791-9.cpp b/uva_cpp_clean/10791/10791-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c8add43aa2787baa5dca82fbcf298290758c9b8 --- /dev/null +++ b/uva_cpp_clean/10791/10791-9.cpp @@ -0,0 +1,42 @@ +#include + +#define SIZE 100010 + +using namespace std; + +bool is_prime[SIZE]; +vector prime; +int tc, N; +long long ans; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++) + if( is_prime[it1] ){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +long long solve(int n){ + if( n == 1 ) return 2; + ans = 0; + for(int it = 0; it < prime.size() and prime[it] * prime[it] <= n and n != 1; it++) + if( n % prime[it] == 0 ){ + int p = 1; + while( n % prime[it] == 0) p *= prime[it], n /= prime[it]; + ans += p; + } + if( n != 1 ) ans += n; + if( ans == N ) ans++; + return ans; +} + +int main(){ + sieve(); + while(scanf("%d",&N), N) + printf("Case %d: %lld\n", ++tc, solve(N)); + return(0); +} diff --git a/uva_cpp_clean/10793/10793-21.cpp b/uva_cpp_clean/10793/10793-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..add352da922c5152166714dc17609674d29f89be --- /dev/null +++ b/uva_cpp_clean/10793/10793-21.cpp @@ -0,0 +1,97 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10793 + Name: The Orc Attack + Problem: https://onlinejudge.org/external/107/10793.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +int max(int X[], int n) { + int r = X[0]; + for (int i=1; i o.d; } +}; + +vector adj[143]; +int W[143][143], D[143][143], S[143][143]; +void dijkstra(int n, int src, int D[]) { + for (int i=0; i q; + q.push(State(src, D[src]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d == D[s.u]) + for (int v: adj[s.u]) + if (s.d + W[s.u][v] < D[v]) + q.push(State(v, D[v]=s.d+W[s.u][v])); + } +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cout << "Map " << cse << ": "; + + cin >> n >> m; + for (int i=0; i> u >> v >> d; --u; --v; + if (S[u][v]!=cse) { + S[u][v] = S[v][u] = cse; + W[u][v] = W[v][u] = d; + adj[u].push_back(v); + adj[v].push_back(u); + } + else + W[u][v] = W[v][u] = min(W[u][v], d); + } + + + dijkstra(n, 0, D[0]); + if (max(D[0], n)==INF) cout << "-1\n"; + else { + for (int i=1; i<5; ++i) + dijkstra(n, i, D[i]); + + vector eqd; + for (int i=0; i +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; + freopen("input.txt","r",stdin); + freopen("output.txt","w",stdout); + __builtin_popcountll(); +*/ + + +int maxContinSubArr(vector &v) { + + int x=v.size(); + int ma = v[0]; + int dpv = v[0]; + + for(int i=1;i> arr; +vectorsol ; + +void solveit() { + + int i,j; + while (cin>>n) { + arr = vector>(n, vector(n, 0)); + + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + cin >> arr[i][j]; + } + } + + int ans = arr[0][0]; + for (int i = 0; i < n; i++) { + sol = vector(n, 0); + for (j = i; j < n; j++) { + for (int x = 0; x < n; x++) { + sol[x] += arr[x][j]; + ///cout<>t; + while (t--) { + solveit(); + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/108/108-13.cpp b/uva_cpp_clean/108/108-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90ce54c86a629ca7907457002accf614ecd73b01 --- /dev/null +++ b/uva_cpp_clean/108/108-13.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +using namespace std; + +#define MAX 110 + +int main() +{ + int n, result = -100000000; + + cin >> n; + + int a[MAX][MAX]; + + memset(a, 0, sizeof a); + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + cin >> a[i][j]; + + a[i][j] += a[i][j - 1]; + a[i][j] += a[i - 1][j]; + + a[i][j] -= a[i - 1][j - 1]; + } + } + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + for (int x = i; x <= n; x++) + { + for (int y = j; y <= n; y++) + { + int sum = a[x][y]; + + sum -= a[x][j - 1]; + sum -= a[i - 1][y]; + + sum += a[i - 1][j - 1]; + + result = max(result, sum); + } + } + } + } + + cout << result << endl; +} \ No newline at end of file diff --git a/uva_cpp_clean/108/108-14.cpp b/uva_cpp_clean/108/108-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e06b5175687d7a069daf202f707b6a0df3299e16 --- /dev/null +++ b/uva_cpp_clean/108/108-14.cpp @@ -0,0 +1,86 @@ +/*************************************************** + * Problem name : 108 - Maximum Sum.cpp + * OJ : Uva + * Verdict : AC + * Date : 26.05.2017 + * Problem Type : kadane Algo + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define debug printf("#########\n") +#define nl printf("\n") +#define MAX 500 +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +using namespace std; +typedef long long ll; +int ar[MAX][MAX], N; +void kadane(int input[], int n, int &max) { + int cur; + max = 0; + cur = 0; + for (int i = 0; i < n; i++) { + cur = cur + input[i]; + if (cur > max) { + max = cur; + } + if (cur < 0) { + cur = 0; + } + } +} +int find_max_sum() { + int tmp[100]; + int cur, max_sum; + int i, j, k; + max_sum = cur = INT_MIN; + + for (i = 0; i < N; i++) { + for (k = 0; k < N; k++) + tmp[k] = 0; + + for (j = i; j < N; j++) { + for (k = 0; k < N; k++) + tmp[k] += ar[j][k]; + kadane(tmp, N, cur); + + if (cur > max_sum) { + max_sum = cur; + } + } + } + return max_sum; +} +int main () { + while (scanf("%d", &N) == 1) { + for (int i = 0; i < N; i++) { + for (int j = 0 ; j < N; j++) { + scanf("%d", &ar[i][j]); + } + } + int sum = find_max_sum(); + printf("%d\n", sum); + } + return 0; +} + diff --git a/uva_cpp_clean/108/108-15.cpp b/uva_cpp_clean/108/108-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39e45f8db73dd6f349f5b91cb30fb9517cbbb3e5 --- /dev/null +++ b/uva_cpp_clean/108/108-15.cpp @@ -0,0 +1,47 @@ +/* + 最小子矩陣問題(升維的最大連續和問題) + 時間複雜度: O(N^3) +*/ +#include +#include +#include + +using namespace std; + +int main() { + int n; + + cin >> n; + vector> array(n, vector(n, 0)); + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + cin >> array[i][j]; + + int ans = 0x80000000; + + for (int i = 0; i < n; i++) { + vector dp(n, 0); + // 壓縮二維陣列(把 i ~ n 的陣列壓成一維) + // 就可以當作最大連續和(一維)問題計算 + for (int j = i; j < n; j++) { + int sum_now = 0; + + for (int k = 0; k < n; k++) { + dp[k] += array[j][k]; + + // 當前和 >=0,那就試著加入下一個 + if (sum_now >= 0) + sum_now += dp[k]; + // 當前和<0,那就當作第一個重新開始 + else + sum_now = dp[k]; + + ans = max(sum_now, ans); + } + } + } + + cout << ans << "\n"; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/108/108-19.cpp b/uva_cpp_clean/108/108-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8a36a4a42216629be4feef22b30b9f9d3c4ec67 --- /dev/null +++ b/uva_cpp_clean/108/108-19.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +int matrix[100][100]; +int v[100]; +int N; + +int kadane1() { + int max = -1270000, sum = 0; + for (int i = 0; i < N; i++) { + if (v[i] > v[i] + sum) sum = v[i]; + else sum += v[i]; + if (sum > max) max = sum; + } + return max; +} + +int kadane2() { + int max = -1270000; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) v[j] = 0; + for (int j = i; j < N; j++) { + for (int l = 0; l < N; l++) v[l] += matrix[l][j]; + int sum = kadane1(); + if (sum > max) max = sum; + } + } + return max; +} + +int main() { + scanf("%d", &N); + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + scanf("%d", &matrix[i][j]); + printf("%d\n", kadane2()); + return 0; +} diff --git a/uva_cpp_clean/108/108-20.cpp b/uva_cpp_clean/108/108-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..480013ceb557a5024698d8a00f7b45db8ff3cb4f --- /dev/null +++ b/uva_cpp_clean/108/108-20.cpp @@ -0,0 +1,34 @@ +#include + +int arr[105][105]; + +int main() +{ + int N; + + while( scanf("%d", &N)!=EOF ) + { + for(int i=0 ; isum ) sum = temp; + } + } + + printf("%d\n", sum); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/108/108-21.cpp b/uva_cpp_clean/108/108-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afbe9409d3a24dcc6d121ea66167299a8708b5d0 --- /dev/null +++ b/uva_cpp_clean/108/108-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 108 + Name: Maximum Sum + Problem: https://onlinejudge.org/external/1/108.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int X[102][102], Y[102][102]; + +// ------ O(n^3) ------ +int main(){ + int n; + while (cin>>n) { + for (int i=1; i<=n; i++) + for (int j=1; j<=n; j++) { + cin >> X[i][j]; + Y[i][j] = X[i][j] + Y[i-1][j]; + } + + int res = -10000; + for (int i1=0; i1 res) res = s; + if (s < 0) s = 0; + } + } + + cout << res << endl; + } +} + + + +/* +// ------ O(n^4) ------ +int main(){ + int n; + while (cin>>n) { + for (int i=1; i<=n; i++) + for (int j=1; j<=n; j++) { + cin >> X[i][j]; + Y[i][j] = X[i][j] + Y[i-1][j] + Y[i][j-1] - Y[i-1][j-1]; + } + + int res = -10000; + for (int i1=0; i1 res) res = s; + } + + cout << res << endl; + } +} +*/ + diff --git a/uva_cpp_clean/108/108-5.cpp b/uva_cpp_clean/108/108-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a9b364f392bee66976f24590dc07ec703f071ef4 --- /dev/null +++ b/uva_cpp_clean/108/108-5.cpp @@ -0,0 +1,183 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int main() +{ + int n; + while(si(n)==1) + { + int cs[110][110]={0}; + for1(i,n) + { + for1(j,n) + { + int x; + si(x); + cs[i][j] = (cs[i][j-1] + cs[i-1][j] - cs[i-1][j-1]) + x; + } + + } + + int res=0; + for1(i,n) + { + for1(j,n) + { + for1(k,i) + { + for1(l,j) + { + int ans = cs[i][j] - cs[i][l-1] - cs[k-1][j] + cs[k-1][l-1]; + res = max(ans,res); + } + + } + } + + } + + P(res); + + } + + return 0; +} + diff --git a/uva_cpp_clean/108/108-6.cc b/uva_cpp_clean/108/108-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..f30bc95f82b16aab794263becdbf74e5f4af48e2 --- /dev/null +++ b/uva_cpp_clean/108/108-6.cc @@ -0,0 +1,30 @@ +// https://uva.onlinejudge.org/external/1/108.pdf +#include + +using namespace std; + +int a[101][101]; + +int main() { + int n; + cin >> n; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) { + cin >> a[i][j]; + if (i) a[i][j] += a[i - 1][j]; + if (j) a[i][j] += a[i][j - 1]; + if (i && j) a[i][j] -= a[i - 1][j - 1]; + } + int m = - 127 * 101 * 101; + for (int i = 0; i < n - 1; i++) + for (int j = 0; j < n - 1; j++) + for (int k = i + 1; k < n; k++) + for (int l = j + 1; l < n; l++) { + int r = a[k][l]; + if (i) r -= a[i - 1][l]; + if (j) r -= a[k][j - 1]; + if (i && j) r += a[i - 1][j - 1]; + m = max(m, r); + } + cout << m << endl; +} diff --git a/uva_cpp_clean/108/108-6.cpp b/uva_cpp_clean/108/108-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98081da28947231a4d30947910089cf989c70bca --- /dev/null +++ b/uva_cpp_clean/108/108-6.cpp @@ -0,0 +1,34 @@ +/* + dynamic programming > max 2D range sum + difficulty: easy + date: 17/Jan/2020 + by: @brpapa +*/ +#include +#define INF (1 << 30) +using namespace std; + +int sumAcc[110][110]; + +int main() { + int N; cin >> N; + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) { + cin >> sumAcc[i][j]; + sumAcc[i][j] = sumAcc[i][j] + + ((i > 0)? sumAcc[i-1][j] : 0) + + ((j > 0)? sumAcc[i][j-1] : 0) + - ((i > 0 && j > 0)? sumAcc[i-1][j-1] : 0); + } + + int ans = -INF; + for (int l = 0; l < N; l++) for (int c = 0; c < N; c++) + for (int i = l; i < N; i++) for (int j = c; j < N; j++) + ans = max(ans, sumAcc[i][j] + - ((l > 0)? sumAcc[l-1][j] : 0) + - ((c > 0)? sumAcc[i][c-1] : 0) + + ((l > 0 && c > 0)? sumAcc[l-1][c-1] : 0) + ); + cout << ans << endl; + return 0; +} diff --git a/uva_cpp_clean/108/108-9.cpp b/uva_cpp_clean/108/108-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..309ae08175f87952329bf932d313e2eccc56ad1c --- /dev/null +++ b/uva_cpp_clean/108/108-9.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int n, ans, v[105][105]; + +int main(){ + while(scanf("%d", &n) == 1){ + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) + scanf("%d", &v[i][j]), v[i][j] = v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1] + v[i][j]; + ans = v[1][1]; + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) + for(int ii = i; ii <= n; ii++) + for(int jj = j; jj <= n; jj++) + ans = max(ans, v[ii][jj] - v[ii][j - 1] - v[i - 1][jj] + v[i - 1][j - 1]); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10800/10800-19.cpp b/uva_cpp_clean/10800/10800-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eebdb8ff29ca5cb52f136084231a9302a0cf6c61 --- /dev/null +++ b/uva_cpp_clean/10800/10800-19.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +using namespace std; + +int main() { + unordered_map > graph; + int cases; + cin >> cases; + for (int i = 1; i <= cases; i++) { + graph.clear(); + string line; + cin >> line; + int pos = 0, max = 1, min = -1; + char last = 'C'; + bool first = true; + for (int j = 0; j < line.length(); j++) { + if (!graph.count(pos)) graph.insert(pair >(pos, vector())); + if (line[j] == 'R') { + if (last == 'F') pos++; + graph[pos].push_back(j); + pos++; + } else if (line[j] == 'F') { + if (last == 'R' || (last == 'C' && !first)) pos--; + graph[pos].push_back(j); + pos--; + } else { + if (last == 'F') pos++; + graph[pos].push_back(j); + } + last = line[j]; + if (pos > max) max = pos; + if (pos < min) min = pos; + first = false; + } + printf("Case #%d:\n", i); + for (int j = max; j >= min; j--) { + if (graph[j].empty()) continue; + int size = graph[j].back()+1; + string row(size+2, ' '); + row[0] = '|'; + for (int k = 0; k < graph[j].size(); k++) { + int c = graph[j][k]; + row[c + 2] = line[c] == 'R'? '/': line[c] == 'C'? '_': '\\'; + } + cout << row << endl; + } + cout << "+"; + for(int j = 0; j <= line.length()+1; j++) cout << "-"; + printf("\n\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10800/10800-20.cpp b/uva_cpp_clean/10800/10800-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f867704e2210cead77d700d3351f6ef7d7f4ba28 --- /dev/null +++ b/uva_cpp_clean/10800/10800-20.cpp @@ -0,0 +1,54 @@ +#include +#include + +const int S = 105; +char str[S], graph[S][S]; + +int main() +{ + int tc, nCase; + char dump; + + scanf("%d%c", &tc, &dump); + nCase = 1; + while( tc-- ) + { + scanf("%s", str); + + memset(graph, ' ', sizeof(graph)); + + int len = strlen(str), pos = 50; + for(int i=0 ; i=0 ; pos--) + if( graph[i][pos]!=' ' ) break; + + if( pos>=0 ) + { + printf("| "); + for(int k=2 ; k<=pos ; k++) printf("%c", graph[i][k]); + printf("\n"); + flag = true; + } + else if( pos<0 && flag ) + { + printf("+"); + for(int k=0 ; k<=len+1 ; k++) printf("-"); + printf("\n"); + break; + } + } + printf("\n"); + ++nCase; + } + return 0; +} diff --git a/uva_cpp_clean/10800/10800-21.cpp b/uva_cpp_clean/10800/10800-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c8bbad09f046a0088feeb2c5694507be65da579 --- /dev/null +++ b/uva_cpp_clean/10800/10800-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10800 + Name: Not That Kind of Graph + Problem: https://onlinejudge.org/external/108/10800.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int mxx[104]; +char board[104][52]; +int main() { + int T; + char s[100]; + scanf("%d", &T); + for (int cse=1; cse<=T; cse++) { + scanf("%s", s); + memset(mxx, 0, sizeof(mxx)); + memset(board, ' ', sizeof(board)); + + int x=0, y=50, mny=y, mxy=y-1; + for (int i=0; s[i]; i++) + if (s[i] == 'R') { + mxx[y] = x + 1; + if (y > mxy) mxy = y; + board[y++][x++] = '/'; + } + + else if (s[i] == 'F') { + board[--y][x++] = '\\'; + if (y < mny) mny = y; + mxx[y] = x; + } + + else { + board[y][x++] = '_'; + mxx[y] = x; + if (y > mxy) mxy = y; + } + + + printf("Case #%d:\n", cse); + for (int i=mxy; i>=mny; i--) { + printf("| "); + for (int j=0; j + +using namespace std; + +int main() +{ + int tests, casenr = 0; + cin >> tests; + + while (tests--) + { + string data; + cin >> data; + + char tab[110][60]; + + for (int i = 0; i < 110; i++) + for (int j = 0; j < 60; j++) + tab[i][j] = ' '; + + int index = 55; + for (unsigned int i = 0; i < data.length(); i++) + { + if (data[i] == 'R') + { + if (i > 0 && data[i - 1] == 'R') + index--; + tab[index][i] = '/'; + } + else if (data[i] == 'F') + { + if (i > 0 && (data[i - 1] == 'F' || data[i - 1] == 'C')) + index++; + tab[index][i] = '\\'; + } + else + { + if (i > 0 && data[i - 1] == 'R') + index--; + tab[index][i] = '_'; + } + } + + for (int i = 0; i < 110; i++) + { + int d = 0; + for (int j = 0; j < 60; j++) + if (tab[i][j] != ' ') + d = j + 1; + tab[i][d] = '\0'; + } + + cout << "Case #" << ++casenr << ":" << endl; + + for (int i = 0; i < 110; i++) + { + if (tab[i][0] == '\0') + continue; + + cout << "| "; + + for (int j = 0; j < 60; j++) + { + if (tab[i][j] == '\0') + break; + cout << tab[i][j]; + } + cout << endl; + } + + cout << '+'; + for (unsigned int i = 0; i < data.length() + 2; i++) + cout << '-'; + cout << endl; + + cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10801/10801-13.cpp b/uva_cpp_clean/10801/10801-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c40132639440e89a2bdbc5cb3a1a9dff47bd36c2 --- /dev/null +++ b/uva_cpp_clean/10801/10801-13.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, k; +map label; +vector > floors; +vector times, dis; +vector > > graph; + +ofstream fout("f.out"); + +int dijkstra() +{ + queue q; + + dis[0] = 0; + + q.push(0); + + while (!q.empty()) + { + int head = q.front(); + + q.pop(); + + for (int i = 0; i < graph[head].size(); i++) + { + int childVer = graph[head][i].first; + + int weight = graph[head][i].second; + + int totalWeight = (int)(abs(head - childVer) * weight); + + if (totalWeight + dis[head] + 60 < dis[childVer]) + { + dis[childVer] = dis[head] + totalWeight + 60; + + q.push(childVer); + } + } + } + + return dis[k]; +} + +void initialize() +{ + label.clear(); + + times.clear(); + times.resize(n); + + floors.clear(); + floors.resize(n); + + graph.clear(); + graph.resize(100); + + dis.clear(); + dis.resize(100, INT_MAX); +} + +int main() +{ + while (cin >> n >> k) + { + initialize(); + + for (int i = 0; i < n; i++) + { + cin >> times[i]; + } + + cin.ignore(); + + for (int i = 0; i < n; i++) + { + int t; + + string s; + + getline(cin, s); + + stringstream ss(s); + + while (ss >> t) + { + floors[i].push_back(t); + + label[t] = i; + } + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < floors[i].size(); j++) + { + for (int k = j + 1; k < floors[i].size(); k++) + { + int x = floors[i][j]; + int y = floors[i][k]; + int w = times[i]; + + graph[x].push_back(make_pair(y, w)); + graph[y].push_back(make_pair(x, w)); + } + } + } + + int ans = dijkstra(); + + if (ans == INT_MAX) cout << "IMPOSSIBLE" << endl; + + else if (ans < 60) cout << ans << endl; + + else cout << ans - 60 << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10801/10801-19.cpp b/uva_cpp_clean/10801/10801-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..617be7a0150f0ea7c7c39e2826033f79e8660f55 --- /dev/null +++ b/uva_cpp_clean/10801/10801-19.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef pair ii; +typedef pair iii; +vector AdjList; +vi times; + +int n, k; +int T[5], F[5][100]; + +inline int abs(int x) { + return x > 0? x: -x; +} + +int dijkstra() { + times.assign(100*n, 100000000); + priority_queue pq; + for (int i = 0; i < n; i++) { + if (F[i][0]) pq.push(iii(0, ii(i, 0))), times[i] = 0; + } + while (!pq.empty()) { + iii p = pq.top(); pq.pop(); + int t = -p.first, u = p.second.second, e = p.second.first; + if (t > times[n*u + e]) continue; + if (u == k) return t; + for (int j = 0; j < AdjList[e].size(); j++) { + int v = AdjList[e][j]; + if (t + abs(v-u)*T[e] < times[n*v + e]) { + times[n*v + e] = t + abs(v-u)*T[e]; + pq.push(iii(-times[n*v + e], ii(e, v))); + } + } + for (int i = 0; i < n; i++) + if (i != e && F[i][u] && t + 60 < times[n*u + i]) { + times[n*u + i] = t + 60; + pq.push(iii(-times[n*u + i], ii(i, u))); + } + } + return -1; +} + +int main() { + while (cin >> n >> k) { + AdjList.assign(n, vi()); + for (int i = 0; i < n; i++) cin >> T[i]; + string s; + getline(cin, s); + memset(F, 0, n*400); + for (int i = 0; i < n; i++) { + getline(cin, s); + istringstream iss(s); + int f; + while (iss >> f) F[i][f] = 1, AdjList[i].push_back(f); + } + int ret = dijkstra(); + if (ret != -1) printf("%d\n", ret); + else printf("IMPOSSIBLE\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10801/10801-21.cpp b/uva_cpp_clean/10801/10801-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0136e45574f14efb2c92532b10b19b050c64d454 --- /dev/null +++ b/uva_cpp_clean/10801/10801-21.cpp @@ -0,0 +1,82 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10801 + Name: Lift Hopping + Problem: https://onlinejudge.org/external/108/10801.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool S[5][100]; +int adj[5][100][2], + D[5][100], T[5], n, k; + +struct State { + int i, j, d; + State(int i, int j, int d):i(i),j(j),d(d){} + bool operator < (const State &s) const { return d > s.d; } +}; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string line; + while (cin >> n >> k) { + for (int i=0; i> T[i]; + + priority_queue q; + + memset(S, 0, 100*n); + memset(D, 127, 100*n*sizeof(int)); + cin.ignore(100, '\n'); + for (int i=0, x, l; i> l) { + if (!l) q.push(State(i, 0, D[i][0]=0)); + adj[i][l][0] = -1; S[i][l]=1; + for (int j=0; sin>>x; ++j) { + adj[i][x][0] = l; + adj[i][l][1] = x; + S[i][l = x] = 1; + } + adj[i][l][1] = -1; + } + } + + while (!q.empty()) { + State s = q.top(); q.pop(); + if (D[s.i][s.j] != s.d) continue; + if (s.j == k) { + cout << s.d << endl; + goto fin; + } + + for (int k=0; k<2; ++k) { + int v = adj[s.i][s.j][k]; + if (v!=-1) { + int d = s.d + T[s.i]*(s.j>v ? s.j-v : v-s.j); + if (d < D[s.i][v]) + q.push(State(s.i, v, D[s.i][v]=d)); + } + } + + for (int k=0; k>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10803 + Name: Thunder Mountain + Problem: https://onlinejudge.org/external/108/10803.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------------ + +struct Point { + int x, y; + bool operator < (const Point &p) const { return x < p.x; } +} P[143]; int n; +vector adj[143]; +double W[143][143], D[143]; + +int dist2(int i, int j) { + int xx = P[i].x - P[j].x, + yy = P[i].y - P[j].y; + return xx*xx + yy*yy; +} + +struct State { + int u; double d; + State(int u, double d):u(u),d(d){} + bool operator < (const State& o) const { return d > o.d; } +}; + +double dijkstra(int src) { + for (int i=0; i q; + q.push(State(src, D[src]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.u]) continue; + + for (int v: adj[s.u]) { + double d = s.d + W[s.u][v]; + if (d < D[v]) + q.push(State(v, D[v]=d)); + } + } + + double mxd = D[0]; + for (int i=1; mxd<1e10 && i 1e10) cout << "Send Kurdy\n\n"; + else { + for (int i=1; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10804 + Name: Gopher Strategy + Problem: https://onlinejudge.org/external/108/10804.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +vector adj[55]; +int match[55], n, m; +double Xg[55], Yg[55], Xh[55], Yh[55], D[55][55]; + +int seen[55], sse; +bool dfs(int u) { + for (int v: adj[u]) + if (seen[v]!= sse) { + seen[v] = sse; + if (match[v]==-1 || dfs(match[v])) { + match[v] = u; + return 1; + } + } + return 0; +} + +bool bpm(double h, int mk) { + if (mk >= n) return 1; + + memset(match, -1, m*sizeof(int)); + for (int i=0; i= n-i) return 1; + ++sse; + if (!dfs(i) && (--mk) < 0) + return 0; + } + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(3); + + int T, k; + cin>>T; + for (int cse=1; cse<=T; ++cse) { + cin >> n >> m >> k; + for (int i=0; i> Xg[i] >> Yg[i]; + + for (int i=0; i> Xh[i] >> Yh[i]; + + vector V; + for (int i=0; i 1e-4) + V[hi++] = V[i]; + + while (lo <= hi) { + int mid = (lo + hi) >> 1; + if (bpm(V[mid], k)) + hi = mid-1; + else + lo = mid+1; + } + cout << sqrt(V[hi+1]) << '\n'; + } + cout << '\n'; + } +} diff --git a/uva_cpp_clean/10805/10805-21.cpp b/uva_cpp_clean/10805/10805-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c62bcd7934f362cb73089bc2f5b1f6657633a471 --- /dev/null +++ b/uva_cpp_clean/10805/10805-21.cpp @@ -0,0 +1,69 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10805 + Name: Cockroach Escape Networks + Problem: https://onlinejudge.org/external/108/10805.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +vector adj[414]; +int S[414], D[414], Q[414], ccse; +void bfs(int n, int src, int ccse) { + memset(D, -1, n*sizeof(int)); + D[Q[0] = src] = 0; + + int qsz = 1, qi = 0; + while (qi < qsz) { + int u = Q[qi++]; + for (int v: adj[u]) + if (S[v] != ccse) { + S[v] = ccse; + D[v] = D[u] + 1; + Q[qsz++] = v; + } + } +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n >> m; + + for (int i=n+m; i>=0; --i) + adj[i].clear(); + + int N = n; + for (int i=0, u,v; i> u >> v; + adj[u].push_back(N); + adj[v].push_back(N); + adj[N].push_back(u); + adj[N].push_back(v); + ++N; + } + + int mn = 10000; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10806 + Name: Dijkstra, Dijkstra. + Problem: https://onlinejudge.org/external/108/10806.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define Inf (INT_MAX/2) + +// the maximum number of vertices + 1 +#define NN 400 +int cap[NN][NN]; +int cost[NN][NN]; +int fnet[NN][NN], adj[NN][NN], deg[NN]; +int par[NN], d[NN]; +int pi[NN]; + +#define Pot(u,v) (d[u] + pi[u] - pi[v]) +bool dijkstra( int n, int s, int t ) +{ + for( int i = 0; i < n; i++ ) d[i] = Inf, par[i] = -1; + d[s] = 0; + par[s] = -n - 1; + while( 1 ) + { + int u = -1, bestD = Inf; + for( int i = 0; i < n; i++ ) if( par[i] < 0 && d[i] < bestD ) + bestD = d[u = i]; + if( bestD == Inf ) break; + + // relax edge (u,i) or (i,u) for all i; + par[u] = -par[u] - 1; + for( int i = 0; i < deg[u]; i++ ) + { + int v = adj[u][i]; + if( par[v] >= 0 ) continue; + if( fnet[v][u] && d[v] > Pot(u,v) - cost[v][u] ) + d[v] = Pot( u, v ) - cost[v][u], par[v] = -u-1; + if( fnet[u][v] < cap[u][v] && d[v] > Pot(u,v) + cost[u][v] ) + d[v] = Pot(u,v) + cost[u][v], par[v] = -u - 1; + } + } + for( int i = 0; i < n; i++ ) if( pi[i] < Inf ) pi[i] += d[i]; + return par[t] >= 0; +} +#undef Pot + +int mcmf3( int n, int s, int t, int &fcost ) +{ + int i,j; + for(i=0;i0){ + scanf("%d",&m); + for(i=0;i=2) + printf("%d\n",f); + else + printf("Back to jail\n"); + + + for(i=0;i<=n;i++) + for(j=0;j<=n;j++) + cost[i][j]=cap[i][j]=0; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10806/10806-6.cpp b/uva_cpp_clean/10806/10806-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b5e82bbcdf67252c2620ba60e2baf09a9d7c32d --- /dev/null +++ b/uva_cpp_clean/10806/10806-6.cpp @@ -0,0 +1,97 @@ +/* + graphs > shortest path > single-source > weighted graph + difficulty: medium + date: 23/Feb/2020 + problem: find the global shortest path from 0 to V-1 (round trip), without repeting edges + by: @brpapa +*/ +#include +#include +#include +#include +#define MAX_V 110 +#define INF (1 << 30) +using namespace std; + +int V; +struct Tadj { + int id, w; + Tadj() {} + Tadj(int id, int w) : id(id), w(w) {} +}; +vector adjList[MAX_V]; + +struct Tpq { + int id, dist; + Tpq(int id, int dist) : id(id), dist(dist) {} + bool operator<(const Tpq &p) const { return dist < p.dist; } +}; +bool usedEdge[MAX_V][MAX_V]; + +vector shortestDist, parent; +void dijk(int s) { + shortestDist.assign(V, INF); shortestDist[0] = 0; parent.assign(V, -1); + priority_queue pq; pq.push(Tpq(s, 0)); + + while (!pq.empty()) { + Tpq v = pq.top(); pq.pop(); + if (v.dist > shortestDist[v.id]) continue; + + // para cada aresta v.id --u.w--> u.id + for (Tadj u : adjList[v.id]) { + if (usedEdge[v.id][u.id]) continue; + + int newDist = shortestDist[v.id] + u.w; + if (newDist < shortestDist[u.id]) { + shortestDist[u.id] = newDist; + pq.push(Tpq(u.id, shortestDist[u.id])); + parent[u.id] = v.id; + } + } + } +} + +int main() { + while (cin >> V && V) { + for (int v = 0; v < V; v++) adjList[v].clear(); + + int E; cin >> E; + while (E--) { + int v, u, w; cin >> v >> u >> w; v--; u--; + adjList[v].push_back(Tadj(u, w)); + adjList[u].push_back(Tadj(v, w)); + } + + memset(usedEdge, 0, sizeof usedEdge); + + dijk(0); int he = shortestDist[V-1]; + + for (int v = V-1; v != 0; v = parent[v]) { + usedEdge[parent[v]][v] = 1; + for (Tadj &u : adjList[v]) + if (u.id == parent[v]) + u.w *= -1; + } + + dijk(0); int you = shortestDist[V-1]; + if (you == INF) { cout << "Back to jail" << endl; continue; } + + cout << he+you << endl; + } + return 0; +} + +// edge case: ans = 22 +/* +6 +8 +1 2 1 +2 3 1 +3 6 1 +1 4 100 +4 5 100 +5 6 100 +1 3 10 +2 6 10 +0 +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10810/10810-21.cpp b/uva_cpp_clean/10810/10810-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065c91a7d6e6bf0711f336f92fe95474f65d655b --- /dev/null +++ b/uva_cpp_clean/10810/10810-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10810 + Name: Ultra-QuickSort + Problem: https://onlinejudge.org/external/108/10810.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int a[500011], b[500011]; +long long qs(int s, int e) { + if (s+1>=e) return 0; + long long c=0; + int piv = a[s], + j=s, k=0; + + for (int i=s; i>n && n) { + for (int i=0; i> a[i]; + cout << qs(0, n) << endl; + } +} diff --git a/uva_cpp_clean/10810/10810-9.cpp b/uva_cpp_clean/10810/10810-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..976b83cf1000dc4e1ffc84d0dadcbd4f7b1f02ed --- /dev/null +++ b/uva_cpp_clean/10810/10810-9.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +const int SIZE = 100005; + +int n, v[SIZE]; + +long long Merge(int lo, int mid, int hi){ + long long inv = 0; + vector L, R; + int l1 = mid - lo + 1, l2 = hi - mid; + for(int it = 0; it < l1; it++) L.push_back(v[lo + it]); + for(int it = 0; it < l2; it++) R.push_back(v[mid + 1 + it]); + L.push_back(INT_MAX), R.push_back(INT_MAX); + int i = 0, j = 0; + for(int it = lo; it <= hi; it++) + if( L[i] <= R[j] ) + v[it] = L[i++]; + else + v[it] = R[j++], inv += l1 - i; + return inv; +} + +long long MergeSort(int lo, int hi){ + long long inv = 0; + if( lo < hi ){ + int mid = ( lo + hi ) / 2; + inv += MergeSort(lo, mid); + inv += MergeSort(mid + 1, hi); + inv += Merge(lo, mid, hi); + } + return inv; +} + +int main(){ + while(scanf("%d", &n), n){ + for(int it = 0; it < n; it++) + scanf("%d", v + it); + printf("%lld\n", MergeSort(0, n - 1)); + } + return(0); +} diff --git a/uva_cpp_clean/10812/10812-19.cpp b/uva_cpp_clean/10812/10812-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faaa9caa847f6b6550e85973e7bd6200b3e87ccc --- /dev/null +++ b/uva_cpp_clean/10812/10812-19.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + long long s, d, r1, r2; + scanf("%lld %lld", &s, &d); + r1 = (s+d)/2; + r2 = s-r1; + if (r2 >=0 && r1 - r2 == d) printf("%lld %lld\n", r1, r2); + else printf("impossible\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10812/10812-21.cpp b/uva_cpp_clean/10812/10812-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c83ca6e2d47b0f690d9312f3ded565f7f0788de --- /dev/null +++ b/uva_cpp_clean/10812/10812-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10812 + Name: Beat the Spread! + Problem: https://onlinejudge.org/external/108/10812.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + + while(n--) { + int s, d; + cin >> s >> d; + if (d > s || ((s + d) % 2)) + cout << "impossible\n"; + else + cout << (s + d)/2 << " " << (s - d)/2 << endl; + } +} diff --git a/uva_cpp_clean/10812/10812-5.cpp b/uva_cpp_clean/10812/10812-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..32c150b21e240c68fe789c73ce87f4f62e7309b4 --- /dev/null +++ b/uva_cpp_clean/10812/10812-5.cpp @@ -0,0 +1,23 @@ +#include +int main() +{ + long int t,a,b,c,d,i; + while(scanf("%ld",&t)==1) + { + i=0; + while(ia||(a-b)%2!=0) + printf("impossible\n"); + else + { + c=(a-b)/2; + d=c+b; + printf("%ld %ld\n",d,c); + } + i++; + } + } + return 0; +} diff --git a/uva_cpp_clean/10812/10812-6.cpp b/uva_cpp_clean/10812/10812-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..152f8f5499ac96483070c10c2f59db82282915d6 --- /dev/null +++ b/uva_cpp_clean/10812/10812-6.cpp @@ -0,0 +1,25 @@ +/* + math > ad-hoc + difficulty: easy + date: 17/Feb/2020 + by: @brpapa +*/ +#include +using namespace std; + +int main() { + int T; cin >> T; + while (T--) { + int s, d; cin >> s >> d; + + int a = s+d; + int b = s-d; + + if (a < 0 || b < 0 || a%2 || b%2) + cout << "impossible" << endl; + else + cout << a/2 << " " << b/2 << endl; + + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10814/10814-19.cpp b/uva_cpp_clean/10814/10814-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..297b1207245163a0d2c743c7adb8c485fd8a085b --- /dev/null +++ b/uva_cpp_clean/10814/10814-19.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +using namespace std; + +typedef __int128 ll; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +string tostr(ll n) { + char str[40]; + int i = 0; + while (n) { + str[i] = n%10 + '0'; + n /= 10; + i++; + } + str[i] = 0; + reverse(str, str + i); + string s(str); + return s; +} + +ll toll(string s) { + ll ret = 0; + for (int i = 0; i < s.length(); i++) { + ret *= 10; + ret += s[i] - '0'; + } + return ret; +} + +int main() { + string ps, qs; + ll p, q; + char c; + int T; + cin >> T; + while (T--) { + cin >> ps >> c >> qs; + p = toll(ps), q = toll(qs); + ll g = gcd(p, q); + p /= g, q /= g; + cout << tostr(p) << " / " << tostr(q) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10814/10814-5.cpp b/uva_cpp_clean/10814/10814-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9990ebdc466df8274cfef51784af24908842345f --- /dev/null +++ b/uva_cpp_clean/10814/10814-5.cpp @@ -0,0 +1,396 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + + + + +const int MAXD = 10000, DIG = 9, BASE = 1000000000; + +const unsigned long long BOUND = numeric_limits :: max () - (unsigned long long) BASE * BASE; + +class BigInteger{ +private: + int digits[MAXD]; + int D; +public: + + friend ostream &operator<<(ostream &out,BigInteger &c); + + inline void trim() + { + while(D > 1 && digits[D-1] == 0 ) + D--; + } + + inline void dealint(long long x) + { + memset(digits,0,sizeof(digits)); + D = 0; + do{ + digits[D++] = x % BASE; + x /= BASE; + }while(x > 0); + } + + inline void dealstr(char *s) + { + memset(digits,0,sizeof(digits)); + int len = strlen(s),first = (len + DIG -1)%DIG + 1; + + D = (len+DIG-1)/DIG; + + for(int i = 0;i < first;i++) + digits[D-1] = digits[D-1]*10 + s[i] - '0'; + + for(int i = first, d = D-2; i < len;i+=DIG,d--) + for(int j = i;j < i+DIG;j++) + digits[d] = digits[d]*10 + s[j]-'0'; + + trim(); + } + + inline char *print() + { + trim(); + + char *cdigits = new char[DIG * D + 1]; + + int pos = 0,d = digits[D-1]; + + do{ + cdigits[pos++] = d % 10 + '0'; + d/=10; + }while(d > 0); + + reverse(cdigits,cdigits+pos); + + for(int i = D - 2;i >= 0;i--,pos += DIG) + for(int j = DIG-1,t = digits[i];j >= 0;j--) + { + cdigits[pos+j] = t%10 + '0'; + t /= 10; + } + + cdigits[pos] = '\0'; + + return cdigits; + } + + + BigInteger(){dealint(0);} + + BigInteger(long long x){ + dealint(x); + } + + BigInteger(int x){ + dealint(x); + } + + BigInteger(char *s){ + dealstr(s); + } + + + + inline bool operator < (const BigInteger &o) const + { + if(D != o.D) + return D < o.D; + + for(int i = D-1;i>=0;i--) + if(digits[i] != o.digits[i]) + return digits[i] < o.digits[i]; + return false; //equal + + } + + bool operator > (const BigInteger & o)const {return o < *this;} + bool operator <= (const BigInteger & o)const {return !(o < *this);} + bool operator >= (const BigInteger & o)const {return !(*this < o);} + bool operator != (const BigInteger & o)const {return o < *this || *this < o;} + bool operator == (const BigInteger & o)const {return !(o < *this) && !(*this < o);} + + + BigInteger &operator++() + { + *this = *this + 1; + return *this; + } + + + BigInteger operator ++(int) + { + BigInteger old = *this; + ++(*this); + return old; + } + + inline BigInteger operator << (int p) const + { + BigInteger temp; + temp.D = D + p; + + for (int i = 0; i < D; i++) + temp.digits [i + p] = digits [i]; + + for (int i = 0; i < p; i++) + temp.digits [i] = 0; + + return temp; + } + + inline BigInteger operator >> (int p) const + { + BigInteger temp; + temp.D = D - p; + + for (int i = 0; i < D - p; i++) + temp.digits [i] = digits [i + p]; + + for (int i = D - p; i < D; i++) + temp.digits [i] = 0; + + return temp; + } + + + BigInteger &operator += (const BigInteger &b) + { + *this = *this + b; + return *this; + } + + BigInteger &operator -= (const BigInteger &b) + { + *this = *this - b; + return *this; + } + + BigInteger &operator *= (const BigInteger &b) + { + *this = *this * b; + return *this; + } + + BigInteger &operator /= (const BigInteger &b) + { + *this = *this / b; + return *this; + } + + BigInteger &operator %= (const BigInteger &b) + { + *this = *this % b; + return *this; + } + + inline BigInteger operator + (const BigInteger &o) const + { + BigInteger sum = o; + int carry = 0; + + for (sum.D = 0; sum.D < D || carry > 0; sum.D++) + { + sum.digits [sum.D] += (sum.D < D ? digits [sum.D] : 0) + carry; + + if (sum.digits [sum.D] >= BASE) + { + sum.digits [sum.D] -= BASE; + carry = 1; + } + else + carry = 0; + } + + sum.D = max (sum.D, o.D); + sum.trim (); + return sum; + } + inline BigInteger operator - (const BigInteger &o) const + { + BigInteger diff = *this; + + for (int i = 0, carry = 0; i < o.D || carry > 0; i++) + { + diff.digits [i] -= (i < o.D ? o.digits [i] : 0) + carry; + + if (diff.digits [i] < 0) + { + diff.digits [i] += BASE; + carry = 1; + } + else + carry = 0; + } + + diff.trim (); + return diff; + } + inline BigInteger operator * (const BigInteger &o) const + { + BigInteger prod = 0; + unsigned long long sum = 0, carry = 0; + + for (prod.D = 0; prod.D < D + o.D - 1 || carry > 0; prod.D++) + { + sum = carry % BASE; + carry /= BASE; + + for (int j = max (prod.D - o.D + 1, 0); j <= min (D - 1, prod.D); j++) + { + sum += (unsigned long long) digits [j] * o.digits [prod.D - j]; + + if (sum >= BOUND) + { + carry += sum / BASE; + sum %= BASE; + } + } + + carry += sum / BASE; + prod.digits [prod.D] = sum % BASE; + } + + prod.trim (); + return prod; + } + inline BigInteger range (int a, int b) const + { + BigInteger temp = 0; + temp.D = b - a; + + for (int i = 0; i < temp.D; i++) + temp.digits [i] = digits [i + a]; + + return temp; + } + + + inline double double_div (const BigInteger &o) const + { + double val = 0, oval = 0; + int num = 0, onum = 0; + + for (int i = D - 1; i >= max (D - 3, 0); i--, num++) + val = val * BASE + digits [i]; + + for (int i = o.D - 1; i >= max (o.D - 3, 0); i--, onum++) + oval = oval * BASE + o.digits [i]; + + return val / oval * (D - num > o.D - onum ? BASE : 1); + } + + inline pair divmod (const BigInteger &o) const + { + BigInteger quot = 0, rem = *this, temp; + + for (int i = D - o.D; i >= 0; i--) + { + temp = rem.range (i, rem.D); + int div = (int) temp.double_div (o); + BigInteger mult = o * div; + + while (div > 0 && temp < mult) + { + mult = mult - o; + div--; + } + + while (div + 1 < BASE && !(temp < mult + o)) + { + mult = mult + o; + div++; + } + + rem = rem - (o * div << i); + + if (div > 0) + { + quot.digits [i] = div; + quot.D = max (quot.D, i + 1); + } + } + + quot.trim (); + rem.trim (); + return make_pair (quot, rem); + } + + inline BigInteger operator / (const BigInteger &o) const + { + return divmod (o).first; + } + + inline BigInteger operator % (const BigInteger &o) const + { + return divmod (o).second; + } + + + inline BigInteger power (int exp) const + { + BigInteger p = 1, temp = *this; + + while (exp > 0) + { + if (exp & 1) p = p * temp; + if (exp > 1) temp = temp * temp; + exp >>= 1; + } + + return p; + } + + inline BigInteger factorial() const + { + BigInteger ans = 1, num = *this; + + if (num == 0 || num == 1) + return ans; + while (!(num < 0 || num == 0)) { + ans = ans * num; + num = num - 1; + } + return ans; + } +}; + +ostream &operator<<(ostream &out, BigInteger &c){ + out<> (istream &in,BigInteger &c) +{ + char s[10000]; + in>>s; + c = s; + return in; +} +BigInteger gcd(BigInteger a,BigInteger b) +{ + if (b==0) return a; + return gcd(b, a%b); +} +BigInteger a,b,c; +int main() +{ + char k; + int T; + scanf("%d",&T); + while(T--) + { + cin>>a>>k>>b; + BigInteger p=gcd(a,b); + a/=p,b/=p; + cout< +/* +Problem: 10815 - Andy's First Dictionary +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1756 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +string word, line; +set dictionary; +set::iterator it; + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + + while ( getline(cin, line) && !cin.eof() ){ + + word = ""; + + for (int i = 0; i < line.length(); i++){ + if ( ispunct(line[i]) || isspace(line[i]) || isdigit(line[i]) ) { + if (word != "") dictionary.insert(word); + word = ""; + continue; + } + + word += tolower(line[i]); + } + + if (word != "") dictionary.insert(word); + } + + it = dictionary.begin(); + while (it != dictionary.end()){ + cout << *it << '\n'; + it++; + } + + return 0; +} +/* +input:- +----------- +Adventures in Disneyland +Two blondes were going to Disneyland when they came to a fork in the +road. The sign read: "Disneyland Left." +So they went home. + +output:- +----------- +a +adventures +blondes +came +disneyland +fork +going +home +in +left +read +road +sign +so +the +they +to +two +went +were +when + +Resources:- +------------- +http://www.cplusplus.com/reference/set/set/ +https://www.geeksforgeeks.org/set-in-cpp-stl/ +http://www.cplusplus.com/reference/cctype/ispunct/ +http://www.cplusplus.com/reference/cctype/isspace/ +http://www.cplusplus.com/reference/cctype/isalnum/ +*/ diff --git a/uva_cpp_clean/10815/10815-14.cpp b/uva_cpp_clean/10815/10815-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0890257894f61efde556c3e2d3ad997e57cd7bdc --- /dev/null +++ b/uva_cpp_clean/10815/10815-14.cpp @@ -0,0 +1,84 @@ +/*************************************************** + * Problem name : 10815 - Andy's First Dictionary.cpp + * Problem Link : https://uva.onlinejudge.org/external/108/10815.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-23 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + string str; + setst; + while (cin >> str) { + int sz = str.size(); + //~ if(sz == 5) break; + string s; + for (int i = 0; i < sz; i++) { + if (isalpha(str[i])) { + //~ debug; + s.push_back(tolower(str[i])); + } else { + st.insert(s); + s.clear(); + } + } + if (s != "") st.insert(s); + } + set::iterator it; + for (it = st.begin(); it != st.end(); it++) { + string ss = *it; + if (ss.size() != 0) cout << ss << "\n"; + //~ cout << *it << '\n'; + } + return 0; +} + diff --git a/uva_cpp_clean/10815/10815-17.cpp b/uva_cpp_clean/10815/10815-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bac23895cc4af43f2e4da5f7389ea7aef0f9b992 --- /dev/null +++ b/uva_cpp_clean/10815/10815-17.cpp @@ -0,0 +1,21 @@ +/** + * AOAPC II Example 5-3 Andy's First Dictionary + */ +#include +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + set dict; + string line; + while (getline(cin, line)) { + for (char &c : line) + c = isalpha(c) ? tolower(c) : ' '; + stringstream ss(line); + string word; + while (ss >> word) + dict.insert(word); + } + for (string s : dict) + cout << s << "\n"; +} diff --git a/uva_cpp_clean/10815/10815-18.cpp b/uva_cpp_clean/10815/10815-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..50cef103ff9955ba76cdc8fce443fa5780f8e7ad --- /dev/null +++ b/uva_cpp_clean/10815/10815-18.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +using namespace std; + +char toLower(char letter){ + if(letter >= 'A' && letter <= 'Z') letter += 32; + return letter; +} + +int main(){ + string line, word; + vector words; + while(getline(cin, line)){ + word = ""; + for(int i = 0; i <= line.length(); i++){ + if(isalpha(line[i])) word += toLower(line[i]); + else if(word != ""){ + words.push_back(word); + word = ""; + } + } + } + sort(words.begin(), words.end()); + cout << words[0] << endl; + for(int i = 1; i < words.size(); i++) if(words[i-1] != words[i]) cout << words[i] << endl; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10815/10815-21.cpp b/uva_cpp_clean/10815/10815-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f25c70be649d44f6af689f81572d9d38bce10ae --- /dev/null +++ b/uva_cpp_clean/10815/10815-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10815 + Name: Andy's First Dictionary + Problem: https://onlinejudge.org/external/108/10815.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + + +char tolower(char ch) { + return ch>='a' && ch<='z' ? ch-32 : ch; +} + + +int main(){ + set dic; + char ch, word[202]; + int i=0; + while ((ch=tolower(getchar())) != EOF) { + if (ch>='a' && ch<='z') + word[i++] = ch; + + else if (i > 0) { + word[i] = 0; + dic.insert(word); + i = 0; + } + } + + for (auto it=dic.begin(); it!=dic.end(); it++) + cout << (*it) << endl; +} diff --git a/uva_cpp_clean/10815/10815-5.cpp b/uva_cpp_clean/10815/10815-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c97ddd3744eeec28681e1a67d9a815e6eb208f50 --- /dev/null +++ b/uva_cpp_clean/10815/10815-5.cpp @@ -0,0 +1,68 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; +int main() +{ + setres; + string s; + while(cin>>s) + { + string p=""; + int l=s.size(); + for(int i=0;i<=l;i++){ + if(isalpha(s[i])){ + p+=tolower(s[i]); + } + else if(p!=""){ + res.insert(p); + p=""; + } + } + } + for (std::set::iterator it=res.begin(); it!=res.end();it++) + cout<<*it< + +using namespace std; + +string s, ss; +set v; + +int main(){ + while(getline(cin, s)){ + for(int i = 0; i < s.size(); i++){ + ss = ""; + if(isalpha(s[i])){ + while(i < s.size() && isalpha(s[i])) ss += tolower(s[i]), i++; + } + if(!ss.empty()) v.insert(ss); + } + } + for(set ::iterator it = v.begin(); it != v.end(); ++it) + printf("%s\n", (*it).c_str()); + return(0); +} diff --git a/uva_cpp_clean/10816/10816-19.cpp b/uva_cpp_clean/10816/10816-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..273fb0094a20882ffcaf338a995a947d6212e6c4 --- /dev/null +++ b/uva_cpp_clean/10816/10816-19.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair dd; +typedef pair ii; +typedef pair di; +typedef pair id; +typedef pair ddii; +typedef vector vddii; +typedef vector vid; +typedef vector vd; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + void clear(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +ufds ds; + +vector AdjList; + +int N, E; + +stack st; +vd dist; + +void dijkstra(int s, int Dest){ + dist.assign(N, 1000000000); dist[s]=0; + vi pred(N, -1); + priority_queue, greater > pq; + pq.push(di(0,s)); + while(!pq.empty()){ + di front=pq.top(); pq.pop(); + double d=front.first; int u=front.second; + if (d > dist[u]) continue; + for(int j=0; j< (int)AdjList[u].size(); j++){ + id v = AdjList[u][j]; + if (dist[u] + v.second < dist[v.first]){ + dist[v.first] = dist[u] + v.second; + pred[v.first] = u; + pq.push(di(dist[v.first],v.first)); + }}} + int i=Dest; + st.push(Dest); + while (i != s) {i = pred[i]; st.push(i);} +} + +int main() { + while (cin >> N >> E) { + int S, T; + cin >> S >> T; + S--, T--; + AdjList.assign(N, vid()); + ds.clear(N); + priority_queue pq; + for (int i = 0; i < E; i++) { + int X, Y; + double R, D; + cin >> X >> Y >> R >> D; + ddii val = make_pair(make_pair(-R, -D), make_pair(X-1, Y-1)); + pq.push(val); + } + double maxTemp = -100; + while (!pq.empty()) { + if (ds.isSameSet(S, T) && -pq.top().first.first != maxTemp) break; + ds.unionSet(pq.top().second.first, pq.top().second.second); + maxTemp = - pq.top().first.first; + AdjList[pq.top().second.first].push_back(make_pair(pq.top().second.second, -pq.top().first.second)); + AdjList[pq.top().second.second].push_back(make_pair(pq.top().second.first, -pq.top().first.second)); + pq.pop(); + } + dijkstra(S, T); + bool first = false; + while (!st.empty()) { + if (first) printf(" "); + else first = true; + printf("%d", st.top()+1); + st.pop(); + } + printf("\n%.1lf %.1lf\n", dist[T], maxTemp); + } + return 0; +} diff --git a/uva_cpp_clean/10816/10816-21.cpp b/uva_cpp_clean/10816/10816-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1590cccba2446a4c05aca93ebdb7308d956a53dd --- /dev/null +++ b/uva_cpp_clean/10816/10816-21.cpp @@ -0,0 +1,105 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10816 + Name: Travel in Desert + Problem: https://onlinejudge.org/external/108/10816.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct Edge { + int u, r, d; + Edge(int u, int r, int d):u(u),r(r),d(d){} +}; + +struct State { + int u, d; + State(int u, int d):u(u),d(d){} + bool operator < (const State &o) const { return d > o.d; } +}; + + +int D[143], R[143], P[143], PP[143], n, m, src, dst; +vector adj[143]; +int dijkstra1() { + for (int i=1; i<=n; ++i) + D[i] = (int)1e9; + + priority_queue q; + q.push(State(src, D[src]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.u]) continue; + if (s.u == dst) return s.d; + + for (Edge e: adj[s.u]) { + State t(e.u, max(s.d, e.r)); + if (D[e.u] > t.d) { + D[e.u] = t.d; + q.push(t); + } + } + } + return 0; +} + +void dijkstra2(int mxd) { + for (int i=1; i<=n; ++i) + D[i] = (int)1e9; + + priority_queue q; + q.push(State(src, D[src]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.u]) continue; + if (s.u == dst) { + int sz = 0; + for (int v=s.u; v!=src; v=P[v]) + PP[sz++] = v; + cout << src; + while (--sz >= 0) + cout << ' ' << PP[sz]; + cout << '\n'; + + cout << s.d/10.0 << ' ' << mxd/10.0 << '\n'; + return; + } + + for (Edge e: adj[s.u]) if (e.r <= mxd) { + State t(e.u, s.d + e.d); + if (t.d < D[e.u]) { + D[e.u] = t.d; + P[e.u] = s.u; + q.push(t); + } + } + } +} + +char BUF[1000000], ch; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(1); + + while (cin >> n >> m >> src >> dst) { + for (int i=1; i<=n; ++i) + R[i] = D[i] = (int)1e9, + adj[i].clear(); + + for (int i=0, u,v,r1,r2,d1,d2; i> u >> v >> r1>>ch>>r2 >> d1>>ch>>d2; + adj[u].push_back(Edge(v, r1*10+r2, d1*10+d2)); + adj[v].push_back(Edge(u, r1*10+r2, d1*10+d2)); + } + + dijkstra2(dijkstra1()); + } +} diff --git a/uva_cpp_clean/10817/10817-7.cpp b/uva_cpp_clean/10817/10817-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..131c4c41550d9e5068ad52afdcadf224d3401a7d --- /dev/null +++ b/uva_cpp_clean/10817/10817-7.cpp @@ -0,0 +1,72 @@ +/** + * Created by LonelyEnvoy on 2017-8-18. + * Headmaster's Headache + * Keywords: DP (complex / state compression) + * See also: Rujia Liu's Book, page 286 + */ + +#include +#include +#include +#include +#include +using namespace std; + +#define rep(i,a,n) for(int i=a;i=a;--i) + +#define INF 0x3f3f3f3f + +#define MAXN 121 +#define MAXS 9 + +int s, m, n; +int dp[MAXN][1<= 0) return ans; + ans = INF; + if (i >= m) ans = solve(i+1, s0, s1, s2); + int lastS0 = s0; + int lastS1 = s1; + s0 ^= teach[i] & s0; + s1 = ((teach[i] ^ s1) & s2) ^ (teach[i] ^ s1); + // or + //s1 = ((s1 ^ (teach[i] & s1)) | (teach[i] & lastS0)); + s2 |= teach[i] & lastS1; + return ans = min(ans, solve(i+1, s0, s1, s2) + c[i]); +} + +int main() { + char buf[50]; + char* token; + char delim[] = " "; + while (~scanf("%d%d%d", &s, &m, &n) && s) { + getchar(); // eat "\n" + init(); + bool first; + rep(i,0,m+n) { + fgets(buf, sizeof(buf), stdin); + token = strtok(buf, delim); + first = true; + while (token != NULL) { + first ? (c[i] = atoi(token)) : (teach[i] |= 1 << (atoi(token) - 1)); + first = false; + token = strtok(NULL, delim); + } + } + printf("%d\n", solve(0, (1<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10818 + Name: Dora Trip + Problem: https://onlinejudge.org/external/108/10818.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct State { + int i, j; + State(int i, int j):i(i),j(j){} +}; + +char M[23][23]; +int I[23][23], Sy[13], Sx[13], sz; +string D[23][23], bestp; + +char dc[] = "ESNW"; +int di[] = {0,1,-1,0}, + dj[] = {1,0,0,-1}, + dk[128]; +void bfs(int sy, int sx, string D[]) { + for (int i=0; i q; + q.push(State(sy, sx)); + P[sy][sx] = -1; + while (!q.empty()) { + State s = q.front(); q.pop(); + if (M[s.i][s.j] == '*' || M[s.i][s.j] == 'S') { + string r; + for (int i=s.i,j=s.j; P[i][j]!=-1; ) { + char ch = P[i][j]; + r = ch + r; + i -= di[ dk[ch] ]; + j -= dj[ dk[ch] ]; + } + D[I[s.i][s.j]] = r; + } + + for (int k=0; k<4; ++k) { + State t(s.i+di[k], s.j+dj[k]); + if (!P[t.i][t.j]) { + char ch = M[t.i][t.j]; + if (ch!='X' && ch!='#') { + P[t.i][t.j] = dc[k]; + q.push(t); + } + } + } + } +} + + +int Mno[23]; +bool visited[23]; +int H() { + int sum = 0; + for (int i=0; i bestl) return; + if (cnt == sz) { + int nlen = len+D[li][0].length(); + if (bestl >= nlen) { + string np; + int ci = 0; + for (int i=0; i> r >> c && (r||c)) { + sz = 1; + cin.ignore(100, '\n'); + for (int i=0; i +#include +#include +#include + +using namespace std; + +#define MAX 10205 + +int main() +{ + long long W, n, U = 0; + + while (cin >> W >> n) + { + U = W; + + if (W > 1800) + { + U += 200; + } + + int w[105]; + int v[105]; + int d[MAX]; + + memset(w, 0, sizeof w); + memset(v, 0, sizeof v); + memset(d, 0, sizeof d); + + for (int i = 1; i <= n; i++) + { + cin >> w[i] >> v[i]; + } + + for (int i = 1; i <= n; i++) + { + for (int j = U; j >= w[i]; j--) + { + if (j == w[i] || d[j - w[i]] > 0) + { + d[j] = max(d[j], d[j - w[i]] + v[i]); + } + } + } + + int m = 0; + + if (W > 1800 && W <= 2000) + { + for (int i = 0; i <= W; i++) + { + m = max(m, d[i]); + } + + for (int i = 2001; i <= U; i++) + { + m = max(m, d[i]); + } + } + + else + { + for (int i = 0; i <= U + 1; i++) + { + m = max(m, d[i]); + } + } + + cout << m << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10819/10819-21.cpp b/uva_cpp_clean/10819/10819-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a3b99a16315e6940c971b688286a7addb977482 --- /dev/null +++ b/uva_cpp_clean/10819/10819-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10819 + Name: Trouble of 13-Dots + Problem: https://onlinejudge.org/external/108/10819.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int cse=1, p[101], f[101], m, + memoid[101][12001], + memo[101][12001]; + +int ks(int ind, int spent) { + if (spent>m+200) return -100; + if (spent>=m) { + if (spent <= 2000) { + if (spent<=1800) + return spent>m ? -100 : 0; + } + else if (spent == m+200) + return 0; + } + if (ind < 0) + return spent>m && spent<=2000 ? -100 : 0; + + if (memoid[ind][spent] == cse) + return memo[ind][spent]; + + int res = max( + ks(ind-1, spent), + ks(ind-1, spent + p[ind]) + f[ind] + ); + + memoid[ind][spent] = cse; + return memo[ind][spent] = res; +} + +int main() { + int n; + while (scanf("%d%d", &m, &n)==2) { + for (int i=0; i + +#define MAX_N 110 +#define MAX_M 11000 + +using namespace std; + +int m,n,p[MAX_N],f[MAX_N],memo[MAX_N][MAX_M]; + +int dp(int it,int price){ + if(price>m){ + if(m<=1800) return -INT_MAX; + if(price>m+200) return -INT_MAX; + } + if(it==n){ + if(price>m && price<=2000) return -INT_MAX; + return 0; + } + if(memo[it][price]!=-1) return memo[it][price]; + return memo[it][price] = max(dp(it+1,price),f[it]+dp(it+1,price+p[it])); +} + +int main(){ + while(scanf("%d %d",&m,&n)==2){ + for(int i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10820 + Name: Send a Table + Problem: https://onlinejudge.org/external/108/10820.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +long long phi[50005]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i=1; i<50005; i++) + phi[i] = i; + for (int p=2; p<50005; ++p) + if (phi[p] == p) { + phi[p] = p-1; + for (int i=2*p; i<50005; i += p) + phi[i] = (phi[i]/p) * (p-1); + } + + phi[1] = 1; + for (int p=2; p<50005; ++p) + phi[p] = phi[p]*2 + phi[p-1]; + + int n; + while (cin >> n && n) + cout << phi[n] << endl; +} diff --git a/uva_cpp_clean/10821/10821-19.cpp b/uva_cpp_clean/10821/10821-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6662234e21782d76087e1d5eec31b0efdc2cdb6 --- /dev/null +++ b/uva_cpp_clean/10821/10821-19.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +void proc(int N, int H, int offset) { + if (H != 0 && offset <= N) { + int k = H-1; + int next = N - ((1 << k) - 1); + if (next < offset) next = offset; + printf(" %d", next); + proc(next - 1, H-1, offset); + proc(N, H-1, next + 1); + } +} + +int main() { + int N, H, c = 1; + while (cin >> N >> H && !(N == 0 && H == 0)) { + printf("Case %d:", c); + if ((1 << H) - 1 < N) printf(" Impossible.\n"); + else { + if (H > N) H = N; + proc(N, H, 1); + printf("\n"); + } + c++; + } + return 0; +} diff --git a/uva_cpp_clean/10827/10827-13.cpp b/uva_cpp_clean/10827/10827-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bac7a4cc5711ce9a331685e9df830e7e0cb7a0b3 --- /dev/null +++ b/uva_cpp_clean/10827/10827-13.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 160 + +int findMax(int a[MAX][MAX], int n) +{ + int result = -100000000; + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + for (int x = i; x < n + i && x <= 2 * n; x++) + { + for (int y = j; y < n + j && y <= 2 * n; y++) + { + int sum = a[x][y]; + + sum -= a[x][j - 1]; + sum -= a[i - 1][y]; + + sum += a[i - 1][j - 1]; + + result = max(result, sum); + } + } + } + } + + return result; +} + +int main() +{ + int t; + + cin >> t; + + while (t--) + { + int n; + + cin >> n; + + int a[MAX][MAX]; + + memset(a, 0, sizeof a); + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + cin >> a[i][j]; + + a[i][j + n] = a[i + n][j + n] = a[i + n][j] = a[i][j]; + } + } + + for (int i = 1; i <= n * 2; i++) + { + for (int j = 1; j <= n * 2; j++) + { + a[i][j] += a[i][j - 1]; + a[i][j] += a[i - 1][j]; + + a[i][j] -= a[i - 1][j - 1]; + } + } + + cout << findMax(a, n) << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10827/10827-19.cpp b/uva_cpp_clean/10827/10827-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4000d19b9fc33fec512527c1189897fd75829a18 --- /dev/null +++ b/uva_cpp_clean/10827/10827-19.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +int matrix[75][75]; +int v[75]; +int N; + +int kadane1() { + int max = -1000000, sum, start; + bool stop = false; + for (int k = 0; k < N; k++) { + start = k; + sum = 0; + for (int i = start; /*i < 2*N &&*/ i < start + N && start < N; i++) { + if (v[i%N] > v[i%N] + sum) { sum = v[i%N]; start = i; } + else sum += v[i%N]; + if (sum > max) max = sum; + } + } + return max; +} + +int kadane2() { + int max = -1000000, start = 0; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) v[j] = 0; + for (int j = i; j < i + N; j++) { + for (int l = 0; l < N; l++) v[l] += matrix[l][j%N]; + int sum = kadane1(); + if (sum > max) max = sum; + } + } + return max; +} + +int main() { + int cases; + scanf("%d", &cases); + while (cases--) { + scanf("%d", &N); + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + scanf("%d", &matrix[i][j]); + printf("%d\n", kadane2()); + } + return 0; +} diff --git a/uva_cpp_clean/10827/10827-21.cpp b/uva_cpp_clean/10827/10827-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14882d6e78d71c1f21d4872fd37f62ddc08208d0 --- /dev/null +++ b/uva_cpp_clean/10827/10827-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10827 + Name: Maximum sum on a torus + Problem: https://onlinejudge.org/external/108/10827.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + +int C[143][143], n; +int CS(int i1, int i2, int j) { + if (i2 > n) i2 -= n; + if (i1 == i2) return -INF; + int result = C[i2][j] - C[i1][j]; + if (i2 < i1) result += C[n][j]; + return result; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + while (T--) { + cin >> n; + for (int i=1; i<=n; ++i) + for (int j=0, x; j> x; + C[i][j] = C[i-1][j] + x; + } + + int bs = -INF; + for (int i1=0; i1=0; ++j2) + bs = max(bs, sum += CS(i1, i2, j2%n)); + } + + cout << bs << endl; + } +} diff --git a/uva_cpp_clean/10827/10827-9.cpp b/uva_cpp_clean/10827/10827-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..796250b9defa9b24565b9575ee122256437bf25a --- /dev/null +++ b/uva_cpp_clean/10827/10827-9.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int tc, n, ans, v[230][230]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + for(int i = n + 1; i <= (n << 1); i++) + for(int j = n + 1; j <= (n << 1); j++) scanf("%d", &v[i][j]); + for(int i = (n << 1) + 1; i < 3 * n; i++) + for(int j = n + 1; j <= (n << 1); j++) v[i][j] = v[i - n][j]; + for(int i = n + 1; i <= (n << 1); i++) + for(int j = (n << 1) + 1; j < 3 * n; j++) v[i][j] = v[i][j - n]; + for(int i = 1; i <= n; i++) + for(int j = n + 1; j <= (n << 1); j++) v[i][j] = v[i + n][j]; + for(int i = n + 1; i <= (n << 1); i++) + for(int j = 1; j <= n; j++) v[i][j] = v[i][j + n]; + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) v[i][j] = v[i + n][j + n]; + for(int i = 1; i <= n; i++) + for(int j = (n << 1) + 1; j < 3 * n; j++) v[i][j] = v[i + n][j - n]; + for(int i = (n << 1) + 1; i < 3 * n; i++) + for(int j = 1; j <= n; j++) v[i][j] = v[i - n][j + n]; + for(int i = (n << 1) + 1; i < 3 * n; i++) + for(int j = (n << 1) + 1; j < 3 * n; j++) v[i][j] = v[i - n][j - n]; + for(int i = 1 ; i < 3 * n; i++) + for(int j = 1; j < 3 * n; j++) + v[i][j] += v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1]; + ans = v[1][1]; + for(int i = 1; i < 3 * n; i++) + for(int j = 1; j < 3 * n; j++) + for(int ii = i; ii < min(i + n, 3 * n); ii++) + for(int jj = j; jj < min(j + n, 3 * n); jj++) + ans = max(ans, v[ii][jj] - v[ii][j - 1] - v[i - 1][jj] + v[i - 1][j - 1]); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10842/10842-13.cpp b/uva_cpp_clean/10842/10842-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34b8e8c59f529d03699e96a02eb2cd36c422afb6 --- /dev/null +++ b/uva_cpp_clean/10842/10842-13.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include + +typedef long long ll; +using namespace std; + +ll n, m, c = 1; +#define MAX 110 + +priority_queue >, vector > > > pq; + +ll parent[MAX], sz[MAX]; + +ll find(ll x) +{ + if (x == parent[x]) return x; + + else return find(parent[x]); +} + +bool merge(ll x, ll y) +{ + ll px = find(x); + ll py = find(y); + + if (px == py) + { + return false; + } + + else + { + if (sz[px] < sz[py]) + { + swap(px, py); + } + + sz[px] += sz[py]; + parent[py] = px; + + return true; + } +} + +ll maximum_spanning_tree() +{ + ll ans = -1; + + while (!pq.empty()) + { + ll u = pq.top().second.second; + ll v = pq.top().second.first; + ll w = pq.top().first; + + pq.pop(); + + if (merge(u, v)) + { + ans = w; + } + } + + return ans; +} + +void initialization() +{ + for (ll i = 0; i < MAX; ++i) + { + sz[i] = 1; + parent[i] = i; + } +} + +int main() +{ + ll t; + + cin >> t; + + while (t--) + { + cin >> n >> m; + + initialization(); + + for (ll i = 0; i < m; ++i) + { + ll v, u, w; + + cin >> v >> u >> w; + + pq.push({w, {v, u}}); + } + + cout << "Case #" << c++ << ": " << (ll)maximum_spanning_tree() << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10842/10842-19.cpp b/uva_cpp_clean/10842/10842-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14b2f34a4ee8e4c98943140c7ac47388abcef72f --- /dev/null +++ b/uva_cpp_clean/10842/10842-19.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef pair ii; +typedef pair iii; + +struct ufds { + vi p, r; int numSets; + void clear(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +ufds ds; + +int main() { + int N; + cin >> N; + for (int c = 1; c <= N; c++) { + int n, m; + cin >> n >> m; + priority_queue pq; + ds.clear(n); + for (int i = 0; i < m; i++) { + int u, v, c; + cin >> u >> v >> c; + pq.push(iii(c, ii(u, v))); + } + int min = 10000; + while (!pq.empty() && ds.numSets > 1) { + iii road = pq.top(); pq.pop(); + ds.unionSet(road.second.first, road.second.second); + min = road.first; + } + printf("Case #%d: %d\n", c, min); + } + return 0; +} diff --git a/uva_cpp_clean/10842/10842-21.cpp b/uva_cpp_clean/10842/10842-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15663fb6d1b119b1fb3c67ea9f03b7e9395f8d6c --- /dev/null +++ b/uva_cpp_clean/10842/10842-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10842 + Name: Traffic Flow + Problem: https://onlinejudge.org/external/108/10842.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 117 +char gid[MAXN]; +char find(char u) { + return gid[u]==u ? u : gid[u]=find(gid[u]); +} +void join(char u, char v) { gid[find(u)] = find(v); } + + +struct Edge { + short w; + char u, v; + Edge(char u, char v, short w):u(u),v(v),w(w){} + bool operator < (const Edge &e) const { return w < e.w; } +}; + +int main(){ + int T, n, m, u, v, w; + scanf("%d", &T); + for (int cse=1; cse<=T; ++cse) { + scanf("%d%d", &n, &m); + for (int i=0; i q; + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 10010; + +struct Edge_{ + int u, v, w; + bool operator < (const Edge_& other) const{ + return ( w > other.w ); + } +}E[SIZE]; + +int tc, n, m; + +int Kruskal(){ + UnionFind UF(n); + int road, setSize = 1; + sort(E, E + m); + for(int it = 0; it < m and setSize != n; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ) + road = E[it].w, setSize++, UF.unionSet(E[it].u, E[it].v); + return road; +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d %d", &n, &m); + for(int it = 0; it < m; it++) + scanf("%d %d %d", &E[it].u, &E[it].v, &E[it].w); + printf("Case #%d: %d\n", t, Kruskal()); + } + return(0); +} diff --git a/uva_cpp_clean/10849/10849-14.cpp b/uva_cpp_clean/10849/10849-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a58c40da0d2efc19f5df086d02f4fc8c3b59c402 --- /dev/null +++ b/uva_cpp_clean/10849/10849-14.cpp @@ -0,0 +1,86 @@ +/*************************************************** + * Problem name : 10849 Move the bishop.cpp + * Problem Link : https://uva.onlinejudge.org/external/108/10849.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-26 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd (ll a, ll b) { + if (a % b == 0) return b; + + return gcd (b, a % b); +} +ll lcm (ll a, ll b) { + return a * b / gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ll tc, n, M; + scanf ("%lld", &tc); + + for (ll t = 1; t <= tc; t++) { + scanf ("%lld %lld", &n, &M); + + for (ll q = 1; q <= n; q++) { + ll x, y, a, b; + scanf ("%lld %lld %lld %lld", &x, &y, &a, &b); + ll xx = x + a, yy = y + b; + + if (x == a && y == b) printf ("0\n"); + else if ( (xx % 2 == 0 && yy % 2 == 0) || (xx % 2 == 1 && yy % 2 == 1) ) { + if ( ( x == y && a == b) || ( (a != b) + && ( (x + y) == (a + b) ) ) ) printf ("1\n"); + else if ( (xx % 2 == 1 && yy % 2 == 1 && (abs (x - a) == abs (y - b) ) ) + || (xx % 2 == 0 && yy % 2 == 0 + && (abs (x - a) == abs (y - b) ) ) ) printf ("1\n"); + else printf ("2\n"); + + } else { + printf ("no move\n"); + } + } + } + + return 0; +} + diff --git a/uva_cpp_clean/10849/10849-19.cpp b/uva_cpp_clean/10849/10849-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e899525bdb8c98587df93f7139cb984f5dfb8b3 --- /dev/null +++ b/uva_cpp_clean/10849/10849-19.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + int C; + cin >> C; + while (C--) { + int T, N; + cin >> T >> N; + while (T--) { + int x1, y1, x2, y2, x, y; + cin >> x1 >> y1 >> x2 >> y2; + x = x2 - x1, y = y2 - y1; + if (x < 0) x = -x; + if (y < 0) y = -y; + if ((x1 % 2 == y1 % 2 && x2 % 2 == y2 % 2) || (x1 % 2 != y1 % 2 && x2 % 2 != y2 % 2)) { + if (x1 == x2 && y1 == y2) printf("0\n"); + else if (x == y) printf("1\n"); + else printf("2\n"); + } else printf("no move\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/10849/10849-5.cpp b/uva_cpp_clean/10849/10849-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..79b7a9bfc82f4a5dcd61533f9ff1eb7678aaca62 --- /dev/null +++ b/uva_cpp_clean/10849/10849-5.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int t,no=0; + sc("%d",&t); + while(t--) + { + ll m,n; + sc("%lld",&m); + sc("%lld",&n); + while(m--) + { + ll r1,c1,r2,c2; + sc("%lld%lld%lld%lld",&r1,&c1,&r2,&c2); + if ( r1<1||r1>n || r2<1||r2>n || c2<1||c2>n || c1<1||c1>n) + pf("no move\n"); + else if(r1==r2&&c1==c2) + pf("0\n"); + else if(abs(r1-r2)==abs(c1-c2)) + pf("1\n"); + else if((r1+r2)%2==(c1+c2)%2) + pf("2\n"); + else + pf("no move\n"); + } + + } + return 0; +} diff --git a/uva_cpp_clean/10849/10849-9.cpp b/uva_cpp_clean/10849/10849-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8047edeb8edd808b3e67b90c5174da5443be6824 --- /dev/null +++ b/uva_cpp_clean/10849/10849-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int tc, t, N, r1, c1, r2, c2; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &t); + scanf("%d", &N); + while(t--){ + scanf("%d %d %d %d", &r1, &c1, &r2, &c2); + if( r1 == r2 and c1 == c2 ) + puts("0"); + else if( abs(r1 - r2) == abs(c1 - c2) ) + puts("1"); + else if( ( abs(r1 - r2) - abs(c1 - c2) ) % 2 == 0 ) + puts("2"); + else + puts("no move"); + } + } + return(0); +} diff --git a/uva_cpp_clean/10851/10851-19.cpp b/uva_cpp_clean/10851/10851-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09bce41b1129a47a1fb974ec6e9e0265052afd77 --- /dev/null +++ b/uva_cpp_clean/10851/10851-19.cpp @@ -0,0 +1,22 @@ +#include +#include +using namespace std; + +int main() { + string matrix[10]; + int N; + cin >> N; + while (N--) { + for (int i = 0; i < 10; i++) cin >> matrix[i]; + for (int j = 1; j < matrix[0].length()-1; j++) { + int pot = 1, c = 0; + for (int i = 1; i < 9; i++) { + c += matrix[i][j] == '/'? 0: pot; + pot *= 2; + } + cout << (char)c; + } + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10851/10851-21.cpp b/uva_cpp_clean/10851/10851-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d02ab1cc8f1d5a81559a9053b2f25c58bf12b62 --- /dev/null +++ b/uva_cpp_clean/10851/10851-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10851 + Name: 2D Hieroglyphs decoder + Problem: https://onlinejudge.org/external/108/10851.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +char lines[100][100]; +int main(){ + int T; + scanf("%d", &T); + while (T--) { + cin.getline(lines[0], 100); + for (int i=0; i<10; i++) + cin.getline(lines[i], 100); + + char msg[100]; + for (int i=1; lines[0][i+1]; i++) { + int ch = msg[i] = 0; + for (int j=8; j>0; j--) + ch = (ch<<1) + (lines[j][i]=='\\' ? 1 : 0); + msg[i-1] = ch; + } + puts(msg); + } +} diff --git a/uva_cpp_clean/10851/10851-9.cpp b/uva_cpp_clean/10851/10851-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..152a3b01d3175218684c6e61f206fa7b71ef264e --- /dev/null +++ b/uva_cpp_clean/10851/10851-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +const int N_ROWS = 20, N_COLS = 90; + +int tc, M; +char grid[N_ROWS][N_COLS]; + +char getLetter(int col) { + int ret = 0; + for (int row = 1, power = 1; row < 9; row++, power <<= 1) + if (grid[row][col] == '\\') ret += power; + return ret; +} + +int main() { + scanf("%d\n", &tc); + while (tc--) { + for (int row = 0; row < 10; row++) scanf("%s\n", grid[row]); + M = strlen(grid[0]); + for (int col = 1; col + 1 < M; col++) putchar(getLetter(col)); putchar('\n'); + } + return(0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10852/10852-21.cpp b/uva_cpp_clean/10852/10852-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a40fe7a465b69deedf749311ec38c8ec85e4b93 --- /dev/null +++ b/uva_cpp_clean/10852/10852-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10852 + Name: Less Prime + Problem: https://onlinejudge.org/external/108/10852.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool isnp[10143]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i=2; i<10143; ++i) + if (!isnp[i]) + for (int j=i+i; j<10143; j += i) + isnp[j] = 1; + + int T, n; + cin >> T; + while (T-- && cin >> n) { + int p = n/2 + 1; + while (isnp[p]) ++p; + cout << p << endl; + } +} diff --git a/uva_cpp_clean/10852/10852-8.cpp b/uva_cpp_clean/10852/10852-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0288dceaca249d40824482c11e6da9b4421bc43f --- /dev/null +++ b/uva_cpp_clean/10852/10852-8.cpp @@ -0,0 +1,49 @@ +// Problem name: Less Prime +// Problem link: https://vjudge.net/problem/UVA-10852 +// Submission link: https://vjudge.net/solution/32960150 + +#include + +using namespace std; + +typedef long long ll; + +vector get_primes(int n){ + vector is_prime(n + 1, 1); + vector primes; + is_prime[ 0 ] = is_prime[ 1 ] = 0; + if(n >= 2){ + primes.push_back(2); + for(int i = 4 ; i <= n ; i += 2) + is_prime[ i ] = 0; + } + for(int i = 3 ; i <= n ; i += 2) + if(is_prime[ i ]){ + primes.push_back(i); + for(ll j = (ll)i * i ; j <= n ; j += 2 * i) + is_prime[ j ] = 0; + } + return primes; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int N = 10000; + const vector primes = get_primes(N); + + int t; + for(cin >> t ; t-- ; ){ + int n; + cin >> n; + int ans = 0, value = 0; + for(int i = 0, p, v ; i < primes.size() && primes[ i ] <= n ; ++i){ + p = n / primes[ i ]; + v = n - p * primes[ i ]; + if(v > value) + value = v, ans = primes[ i ]; + } + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10852/10852-9.cpp b/uva_cpp_clean/10852/10852-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48eee6f4d1cb97b3cb49d7bedbf716b4412aa507 --- /dev/null +++ b/uva_cpp_clean/10852/10852-9.cpp @@ -0,0 +1,36 @@ +#include + +#define SIZE 10000 + +using namespace std; + +bool is_prime[SIZE]; +int M, N, _max, pib, ans; +vectorprime; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + for(int it1 = 2; it1 < SIZE; it1++){ + if(is_prime[it1]){ + for(int it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } + } + +} + +int main(){ + sieve(); + scanf("%d",&M); + while(M--){ + _max = -1; + scanf("%d",&N); + for(int it = 0; it < prime.size() and prime[it] < N; it++){ + pib = N % prime[it]; + if( pib > _max) _max = pib, ans = prime[it]; + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10854/10854-19.cpp b/uva_cpp_clean/10854/10854-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d81dd206df8e6064d610b46ba5cad55e04c2da79 --- /dev/null +++ b/uva_cpp_clean/10854/10854-19.cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; + +int proc() { + string line; + getline(cin, line); + if (line == "S") return 1; + else if (line == "ENDPROGRAM" || line == "END_IF" || line == "ELSE") return -1; + else if (line == "IF") { + int paths1 = 1, paths2 = 1, p = 1; + while (p != -1) { + paths1 *= p; + p = proc(i+1); + } + p = 1; + while (p != -1) { + paths2 *= p; + p = proc(i+1); + } + return paths1 + paths2; + } + return -1; +} + +int main() { + int N; + string trash; + cin >> N; + getline(cin, trash); + while (N--) { + int paths = 1, p = 1; + while (p != -1) { + paths *= p; + p = proc(); + } + cout << paths << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10855/10855-11.cpp b/uva_cpp_clean/10855/10855-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..393f450c218a53592bb06c7bac7298cc8199e253 --- /dev/null +++ b/uva_cpp_clean/10855/10855-11.cpp @@ -0,0 +1,152 @@ +#include +/* +Problem: 10855 - Rotated square +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1796 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int N, n; + +//char matrix[500][500], subMatrix[500][500]; +vector> matrix, subMatrix; + +void Rotate2DMatrixClockWise(); + +template +int CountSubMatrixOccurrences(vector> matrix, vector> subMatrix); + +template +void Rotate2DMatrixClockWise(vector> &matrix); + +int main () { + fastio; + + while ( cin >> N >> n && (N|n) ){ + int c1 = 0, c2 = 0, c3 = 0, c4 = 0; + + matrix = vector> (N, vector(N)); + subMatrix = vector> (n, vector(n)); + + for (int i = 0; i < N; ++i) + for (int j = 0; j < N; ++j) + cin >> matrix[i][j]; + + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + cin >> subMatrix[i][j]; + + + c1 = CountSubMatrixOccurrences(matrix, subMatrix); + Rotate2DMatrixClockWise(subMatrix); + + c2 = CountSubMatrixOccurrences(matrix, subMatrix); + Rotate2DMatrixClockWise(subMatrix); + + c3 = CountSubMatrixOccurrences(matrix, subMatrix); + Rotate2DMatrixClockWise(subMatrix); + + c4 = CountSubMatrixOccurrences(matrix, subMatrix); + + cout << c1 << " " << c2 << " " << c3 << " " << c4 << "\n"; + } + + return 0; +} + +template +void Rotate2DMatrixClockWise(vector> &matrix){ + /* Rotates a 2d matrix 90 degrees in clockwise direction. + O(1) space + + ABC GDA + DEF ----> HEB + GHI IFC + + */ + + size_t n = matrix.size(); + + for (size_t x = 0; x < n / 2; x++){ + for (size_t y = x; y < n-x-1; y++){ + T tmp = matrix[x][y]; + + matrix[x][y] = matrix[n-y-1][x]; + matrix[n-y-1][x] = matrix[n-x-1][n-y-1]; + matrix[n-x-1][n-y-1] = matrix[y][n-x-1]; + matrix[y][n-x-1] = tmp; + } + } +} + +template +int CountSubMatrixOccurrences(vector> matrix, vector> subMatrix){ + // Time Complexity: O(K^2 . M^2) + int ans = 0; + size_t N = matrix.size(); + size_t M = subMatrix.size(); + size_t K = N-M+1; + + + for (size_t x = 0; x < K; ++x){ + for (size_t y = 0; y < K; ++y){ + + bool found = true; + for (size_t i = 0; i < M; ++i){ + for (size_t j = 0; j < M; ++j){ + if (matrix[i+x][j+y] != subMatrix[i][j]) + found = false; + } + } + + if (found) ans++; + } + } + + return ans; +} + +/* +Sample input:- +----------------- +4 2 +ABBA +ABBB +BAAA +BABB +AB +BB +6 2 +ABCDCD +BCDCBD +BACDDC +DCBDCA +DCBABD +ABCDBA +BC +CD +0 0 + +Sample output:- +----------------- +0 1 0 0 +1 0 1 0 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10855/10855-21.cpp b/uva_cpp_clean/10855/10855-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aefb25c235972cc596be88da54c316ab43dda9ab --- /dev/null +++ b/uva_cpp_clean/10855/10855-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10855 + Name: Rotated square + Problem: https://onlinejudge.org/external/108/10855.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int n, m; +char X[500][500], Y[500][500], tmp[500][500]; +void rotate() { + for (int i=0; i + +int main () +{ + int N, n; + while ( scanf("%d %d", &N, &n) && N ) + { + +} diff --git a/uva_cpp_clean/10855/10855-4.cpp b/uva_cpp_clean/10855/10855-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22eb27843bc99ef1f3bbbe6850e4c070bfc11da5 --- /dev/null +++ b/uva_cpp_clean/10855/10855-4.cpp @@ -0,0 +1,83 @@ +#include + +void get_table(char *tab, int size) +{ + for (int i = 0; i < size; i++) + { + std::cin >> tab[i]; + } +} + +void rotate(char *tab, int n) +{ + char temp[n * n]; + + for (int i = 0; i < n * n; i++) + { + temp[i] = tab[i]; + } + + for (int y = 0; y < n; y++) + { + for (int x = 0; x < n; x++) + { + tab[x * n + n - 1 - y] = temp[y * n + x]; + } + } +} + +int fit(char *big, int bigSize, char *small, int smallSize) +{ + int count = 0; + + for (int startY = 0; startY <= bigSize - smallSize; startY++) + { + for (int startX = 0; startX <= bigSize - smallSize; startX++) + { + bool ok = true; + + for (int y = startY; y < startY + smallSize; y++) + { + for (int x = startX; x < startX + smallSize; x++) + { + if (big[y * bigSize + x] != small[(y - startY) * smallSize + x - startX]) + { + ok = false; + break; + } + } + } + + if (ok) + { + count++; + } + } + } + + return count; +} + +int main() +{ + int N, n; + + while (std::cin >> N >> n && N && n) + { + char big[N * N], small[n * n]; + + get_table(big, N * N); + get_table(small, n * n); + + int result[4]; + for (int i = 0; i < 4; i++) + { + result[i] = fit(big, N, small, n); + rotate(small, n); + } + + std::cout << result[0] << " " << result[1] << " " << result[2] << " " << result[3] << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10855/10855-9.cpp b/uva_cpp_clean/10855/10855-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99542fbb11ed0436af1dabe30a9cbd1619568174 --- /dev/null +++ b/uva_cpp_clean/10855/10855-9.cpp @@ -0,0 +1,42 @@ +#include + +#define SIZE 1010 + +using namespace std; + +int n, m; +char u[SIZE][SIZE], v[SIZE][SIZE], w[SIZE][SIZE]; + +bool check(int x, int y){ + for(int i = x; i < x + m; i++) + for(int j = y; j < y + m; j++) + if(v[i][j] != u[i - x][j - y]) return false; + return true; +} + +int appearances(){ + int ct = 0; + for(int i = 0; i < n - m + 1; i++) + for(int j = 0; j < n - m + 1; j++) + if(check(i, j)) ct++; + return ct; +} + +void rotate(){ + for(int i = 0; i < m; i++) + for(int j = 0; j < m; j++) w[i][j] = u[m - j - 1][i]; + for(int i = 0; i < m; i++) + for(int j = 0; j < m; j++) u[i][j] = w[i][j]; +} + +int main(){ + while(scanf("%d %d\n",&n,&m),n|m){ + for(int i = 0; i < n; i++) scanf("%s", v[i]); + for(int i = 0; i < m; i++) scanf("%s", u[i]); + for(int it = 0; it < 4; it++){ + printf( it != 3 ? "%d " : "%d\n", appearances()); + rotate(); + } + } + return(0); +} diff --git a/uva_cpp_clean/10858/10858-21.cpp b/uva_cpp_clean/10858/10858-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b91c2c7aed3ee7ef9a3464a0e62eb98ddb053a6 --- /dev/null +++ b/uva_cpp_clean/10858/10858-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10858 + Name: Unique Factorization + Problem: https://onlinejudge.org/external/108/10858.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + + +int cnt, outs[1000000][30], deg[1000000], nums[30]; +void gen(int n, int mn, int len) { + if (n == 1) { + if (len>1) { + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10862 + Name: Connect the Cable Wires + Problem: https://onlinejudge.org/external/108/10862.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + + if (cells) delete [] cells; + cells = ncells; + } + if (!cp) + memset(cells, 0, capacity*CellSize); + else memset(cells+size, 0, (capacity-size)*CellSize); + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + // --------------------------------------------------------------------- + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + + BigInt(const char s[]) { + while (s[0]==' ' || s[0]=='\n' || s[0]=='\t') s++; + sign = 1; + if (s[0] == '+') s++; + if (s[0] == '-') { sign=-1; s++; } + while (s[0]=='0') s++; + if (!s[0]) { init(); return; } + + int len = strlen(s); + size = (len+3)/MAX_D; + cells = new CellType[capacity = size+16]; + for (int i=len-1; i>=0; i-=MAX_D) { + int rc = 0; + for (int k=i-MAX_D+1; k<=i; k++) { + if (k<0 || s[k]=='-' || s[k]=='+') continue; + rc = rc*10 + s[k] - '0'; + } + cells[(len-i-1) / MAX_D] = rc; + } + for (int i=size; i= 0) + delete [] cells; + } + + int getSize() { return size; } + int getCell(int i) { return cells[i]; } + bool isZero() { return !sign; } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { + return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign; + } + bool operator == (const BigInt &o) const { return sign==o.sign && numEq(o); } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + int osz = size; + setSize(max(size, o.size)+1); + for (int i=osz; i= MAX_C) { + cells[i] -= MAX_C; + ++cells[i+1]; + } + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (*this == o) { + sign = size = 0; + return *this; + } + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i=0; --i) { + t = t*MAX_C + cells[i]; + cells[i] = t / den; + t -= cells[i] * den; + } + return autoSize(); + } + BigInt operator/ (int den) const { return *new BigInt(*this) *= den; } + + int operator% (int den) const { + int r = 0; + for (int i = size-1; i>=0; --i) + r = (r * MAX_C + cells[i]) % den; + return r; + } + + // --------------------------------------------------------------------- + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + +// tostring, input from char + +bool powseen[10][10000]; +BigInt pmemo[10][10000]; +BigInt pow(int a, int b) { + if (!b) return BigInt(1); + if (b==1 || a<=1) + return BigInt(a); + + if (a<10 && b<10000 && powseen[a][b]) + return pmemo[a][b]; + + BigInt c = pow(a, b>>1); + c *= c; + if (b&1) c *= a; + + if (a<10 && b<10000) { + powseen[a][b] = 1; + pmemo[a][b] = c; + } + return c; +} + +BigInt pow(int a, BigInt b) { + if (b.getSize() <= 1) + return pow(a, b.getCell(0)); + + bool has2 = b % 2; + b /= 2; + BigInt c = pow(a, b); + c *= c; + if (b%2) c*= a; + + return c; +} + + +BigInt DP[2001]; +int main(){ + BigInt c(1); + for (int i=1; i<2001; i++) { + DP[i] = DP[i-1]; + DP[i] += c; + c += DP[i]; + } + + int n; + while (scanf("%d", &n)==1 && n) + cout << DP[n] << endl; +} diff --git a/uva_cpp_clean/10862/10862-9.cpp b/uva_cpp_clean/10862/10862-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba327515b573b5eee715986d9123fdd4886b06a2 --- /dev/null +++ b/uva_cpp_clean/10862/10862-9.cpp @@ -0,0 +1,49 @@ +#include + +#define LIMIT 5001 +#define MAX 1050 + +using namespace std; + +int n, l1, l2, minl, maxl, aux, carry, i, j, k, t; +char fibo[LIMIT][MAX], tmp[MAX], final_tmp[MAX]; + +char* sum(char *s1, char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + minl = l1 > l2 ? l2 : l1; + maxl = l1 > l2 ? l1 : l2; + carry = 0; + for(k = 0, i = l1 - 1, j = l2 - 1; k < minl; k++, i--, j--){ + aux = s1[i] - '0' + s2[j]- '0' + carry; + tmp[k] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l1){ + aux = s1[i] - '0' + carry; + tmp[k] = aux % 10 + '0'; + carry = aux / 10; + k++, i--; + } + if(carry) + tmp[maxl++] = carry + '0', tmp[maxl] = '\0'; + else + tmp[maxl] = '\0'; + i = 0; + while(final_tmp[i++] = tmp[--maxl]) ; + final_tmp[i] = '\0'; + return final_tmp; +} + +void generateFibo(){ + fibo[0][0] = '0', fibo[0][1] = '\0'; + fibo[1][0] = '1', fibo[0][1] = '\0'; + for(t = 2; t <= LIMIT; t++) + strcpy(fibo[t], sum(fibo[t - 1], fibo[t - 2])); +} + +int main(){ + generateFibo(); + while(scanf("%d", &n), n) + printf("%s\n", fibo[2 * n]); + return(0); +} diff --git a/uva_cpp_clean/10865/10865-21.cpp b/uva_cpp_clean/10865/10865-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3b8e2289bd7d9b1b0c31ca50ea7e8bfa0455ee2 --- /dev/null +++ b/uva_cpp_clean/10865/10865-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10865 + Name: Brownie Points + Problem: https://onlinejudge.org/external/108/10865.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[200143], Y[200143], n; + while (cin>>n && n) { + for (int i=0; i> X[i] >> Y[i]; + + int cnt1=0, cnt2=0; + int xx = X[n>>1], yy = Y[n>>1]; + for (int i=0; ixx && Y[i]>yy) || (X[i] +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +////=====================================//// + +int row; +ll mod; + +struct Matrix +{ + ll mat[20][20]; +}; + +Matrix zero; + +Matrix Mul(Matrix &aa, Matrix &bb) +{ + Matrix ret=zero; + for(int i = 0; i < row; i ++) + { + for(int j = 0; j < row; j ++) + { + for(int k = 0; k < row; k ++) + { + ret.mat[i][j] += (aa.mat[i][k]*bb.mat[k][j]); + ret.mat[i][j] %= mod; + } + } + } + return ret; +} + +Matrix exp(Matrix a, ll b) +{ + Matrix res=zero; + for0(i,row) + { + res.mat[i][i]=1; + } + + while(b) + { + if(b&1) + res=Mul(res,a); + a=Mul(a,a); + b>>=1; + } + return res; +} + + + +int main() +{ + int d,n; + while(sii(d,n)) + { + sl(mod); + row=d; + + if(d==0 && n==0 && mod==0) break; + + Matrix base=zero; + + for0(i,d) + { + sl(base.mat[0][i]); + } + + Matrix f=zero; + int k=d-1; + for0(i,d) + { + sl(f.mat[k][0]); + k--; + } + for1(i,d-1) + { + base.mat[i][i-1]=1; + } + +// for0(i,d) +// { +// for0(j,d) +// { +// PLN(base.mat[i][j]); +// } +// nl; +// } + + Matrix res =exp(base, n-1); + res = Mul(res,f); + +// for0(i,d) +// { +// for0(j,d) +// { +// PLN(res.mat[i][j]); +// } +// nl; +// } + + + PL(res.mat[d-1][0]); + + } + + return 0; +} + diff --git a/uva_cpp_clean/10870/10870-8.cpp b/uva_cpp_clean/10870/10870-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7390733d53823e523fdffb8161ac888751e0ede8 --- /dev/null +++ b/uva_cpp_clean/10870/10870-8.cpp @@ -0,0 +1,65 @@ +// Problem name: Recurrences +// Problem link: https://vjudge.net/problem/UVA-10870 +// Submission link: https://vjudge.net/solution/18055469 + +#include + +#define endl '\n' + +using namespace std; + +typedef long long ll; +typedef vector vll; +typedef vector matrix; + +matrix multiply(matrix &a, matrix &b, ll MOD){ + int n = a.size(), m = a[ 0 ].size(), o = b[ 0 ].size(); + matrix mat(n, vll(o, 0LL)); + for(int i = 0 ; i < n ; ++i) + for(int k = 0 ; k < m ; ++k) + for(int j = 0 ; j < o ; ++j) + mat[ i ][ j ] = (mat[ i ][ j ] + a[ i ][ k ] * b[ k ][ j ]) % MOD; + return mat; +} + +matrix pow(matrix m, ll p, ll MOD){ + int len = m.size(); + matrix ans(len, vll(len, 0)); + for(int i = 0 ; i < len ; ans[ i ][ i ] = 1LL, ++i); + for( ; p ; p >>= 1LL){ + if(p & 1LL) + ans = multiply(ans, m, MOD); + m = multiply(m, m, MOD); + } + return ans; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0); + +#ifdef JoseA132 + freopen("01.in", "r", stdin); +#endif + + ll d, n, mod; + while(cin >> d >> n >> mod && d){ + matrix m(d, vll(d, 0LL)); + for(int i = 0 ; i < d ; ++i) + cin >> m[ 0 ][ i ], m[ 0 ][ i ] %= mod; + for(int i = 1 ; i < d ; ++i) + m[ i ][ i - 1 ] = 1LL; + + matrix f(d, vll(1, 0)); + for(int i = 0 ; i < d ; ++i) + cin >> f[ d - i - 1 ][ 0 ], f[ d - i - 1 ][ 0 ] %= mod; + + if(n <= d) + cout << f[ d - n ][ 0 ] << endl; + else{ + m = pow(m, n - d, mod); + m = multiply(m, f, mod); + cout << m[ 0 ][ 0 ] << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10874/10874-21.cpp b/uva_cpp_clean/10874/10874-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b9f738ee41cf95f6a50f446297fefdb4e751277 --- /dev/null +++ b/uva_cpp_clean/10874/10874-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10874 + Name: Segments + Problem: https://onlinejudge.org/external/108/10874.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 268435456 +using namespace std; + +int calc(int l, int r, int p, bool f) { + return f ? + (p<=l ? r-p : p-l + r-l): + (p>=r ? p-l : r-p + r-l); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int DP[20043][2], P[20043][2], n; + DP[0][0] = INF; + while (cin >> n && n) { + for (int i=0; i> P[i][0] >> P[i][1]; + } + + P[n][0] = P[n][1] = n; + DP[0][1] = P[0][1]-1; + + for (int i=1; i<=n; ++i) + for (int j=0; j<2; ++j) + DP[i][j] = min( + DP[i-1][0] + calc(P[i][0], P[i][1], P[i-1][0], j), + DP[i-1][1] + calc(P[i][0], P[i][1], P[i-1][1], j) + ); + cout << DP[n][0]+n-1 << endl; + } +} diff --git a/uva_cpp_clean/10878/10878-18.cpp b/uva_cpp_clean/10878/10878-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d255738c3259bc6c2f0dade6623aa72671dd326a --- /dev/null +++ b/uva_cpp_clean/10878/10878-18.cpp @@ -0,0 +1,17 @@ +#include +#include + +using namespace std; + +int main(){ + int binary[] = {256, 128, 64, 32, 16, 8, 0, 4, 2, 1}; + string line; + while(getline(cin, line)){ + if(line[0] == '_') continue; + int ans = 0; + for(int i = 1; i <= 9; i++){ + if(line[i] == 'o') ans += binary[i]; + } + cout << (char)ans; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10878/10878-19.cpp b/uva_cpp_clean/10878/10878-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04e7222c3b9093b0f16e72365feef54ce87bbf27 --- /dev/null +++ b/uva_cpp_clean/10878/10878-19.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main() { + string c; + getline(cin, c); + while (getline(cin, c) && c != "___________") { + int pot = 1, code = 0; + for (int i = 9; i > 1; i--) { + if (c[i] == '.') continue; + code += c[i] == 'o'? pot: 0; + pot *= 2; + } + cout << (char)code; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10878/10878-20.cpp b/uva_cpp_clean/10878/10878-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fef528ff6c41047d11de67fd5eadc042ebe714e9 --- /dev/null +++ b/uva_cpp_clean/10878/10878-20.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +char tape[105]; +int bits[15]; + +int main() +{ + while( gets(tape) ) + { + int pos = 0; + if( tape[0]=='|' ) + { + for(int i=1 ; i=0 ; i--) + sum += bits[i]*pow(2, pos-1-i); + + printf("%c", sum); + } + } + return 0; +} diff --git a/uva_cpp_clean/10878/10878-21.cpp b/uva_cpp_clean/10878/10878-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f00f40cc7db5c0fec2431fa7239ff4abad77a72a --- /dev/null +++ b/uva_cpp_clean/10878/10878-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10878 + Name: Decode the tape + Problem: https://onlinejudge.org/external/108/10878.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char line[100]; + bool tape = false; + while (cin.getline(line, 100)) { + if (line[0]=='-') + if (!(tape=!tape)) cout< + +using namespace std; + +const int LEN = 50; + +char line[LEN]; +string ans; +int decNumber; +vector idx = {9, 8, 7, 5, 4, 3, 2, 1}; + +int getDecimal() { + int power = 1, ret = 0; + for (int i = 0; i < idx.size(); i++) { + if (line[ idx[i] ] == 'o') ret += power; + power <<= 1; + } + return ret; +} + +int main() { + cin.getline(line, LEN); + while (true) { + cin.getline(line, LEN); + decNumber = getDecimal(); + if (decNumber == 0) break; + ans.push_back( (char) decNumber ); + } + printf("%s", ans.c_str()); + return(0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10879/10879-21.cpp b/uva_cpp_clean/10879/10879-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b3d0cc99876f19c91dd636f66a9467851560bd1 --- /dev/null +++ b/uva_cpp_clean/10879/10879-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10879 + Name: Code Refactoring + Problem: https://onlinejudge.org/external/108/10879.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int T, n, sq; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin>>n; + + int x=n, f1=0, f2=0; + for (int i=2; !f1 || !f2; i++) + if (x%i==0) { + if (!f1) f1 = i; + else f2 = f1*i; + x /= i--; + } + + printf("Case #%d: %d = %d * %d = %d * %d\n", cse, n, f2, n/f2, f1, n/f1); + } +} diff --git a/uva_cpp_clean/10879/10879-9.cpp b/uva_cpp_clean/10879/10879-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3f2085a9d4297bc563d7e3813a79aae7b9eb92f --- /dev/null +++ b/uva_cpp_clean/10879/10879-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main(){ + int c, n, t = 0, ct; + vector a; + scanf("%d", &c); + while(c--){ + ct = 0; + a.clear(); + scanf("%d", &n); + for(int i = 2; ; i++){ + if(!(n % i)) + a.push_back(i), a.push_back(n / i), ct++; + if(ct == 2) + break; + } + printf("Case #%d: %d = %d * %d = %d * %d\n", ++t, n, a[0], a[1], a[2], a[3]); + } + return(0); +} diff --git a/uva_cpp_clean/10880/10880-21.cpp b/uva_cpp_clean/10880/10880-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4721dda905fa0ddd3d1977816ee1c02c23aaf28d --- /dev/null +++ b/uva_cpp_clean/10880/10880-21.cpp @@ -0,0 +1,82 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10880 + Name: Colin and Ryan + Problem: https://onlinejudge.org/external/108/10880.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-'); + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +char buf[50]; +void print(long long x) { + int i=48; + if (!x) buf[i--] = '0'; + for (; x; x/=10) + buf[i--] = x%10 + '0'; + buf[i--] = ' '; + fwrite_unlocked(buf+i+1, 1, 48-i, stdout); +} + +// -------------------------------------------------------------- + +int stk[100000]; +int main() { + int T = readInt(); + for (int cse=1; cse<=T; ++cse) { + printf("Case #%d:", cse); + int c = readInt(), + r = readInt(); + + if (c == r) print(0); + else if (c) { + int n = c - r, + sq = sqrt(n), + si = 0; + + if (!r) print(1); + if (n>1) stk[si++] = n; + for (int i=2; i<=sq; ++i) + if (n%i == 0) { + if (c%i == r) + print(i); + stk[si++] = n/i; + } + + if (stk[si-1] && n/stk[si-1] == stk[si-1]) si--; + for (int i=si-1; i>=0; --i) + if (c % stk[i] == r) + print(stk[i]); + } + putchar('\n'); + } +} diff --git a/uva_cpp_clean/10880/10880-9.cpp b/uva_cpp_clean/10880/10880-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..642cbeb8979ca96b240df3c5f4c7b04eca1cc8dc --- /dev/null +++ b/uva_cpp_clean/10880/10880-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int tc, C, R, pib, it; +vector ans; + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + ans.clear(); + scanf("%d %d", &C, &R); + pib = C - R; + for(it = 1; it * it < pib; it++) + if(pib % it==0){ + if(it > R) ans.push_back(it); + if(pib / it >R) ans.push_back(pib / it); + } + if(it * it==pib && it > R) ans.push_back(it); + if(R - C > R) ans.push_back(R - C); + sort(ans.begin(), ans.end()); + printf("Case #%d:", t); + for(int i = 0;i < ans.size(); i++) printf(" %d", ans[i]); + if(R == C) printf(" 0"); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10890/10890-21.cpp b/uva_cpp_clean/10890/10890-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b238b4fe902a258cdbf969103bbb33a303795c6 --- /dev/null +++ b/uva_cpp_clean/10890/10890-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10890 + Name: Maze + Problem: https://onlinejudge.org/external/108/10890.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int adj[43][43], X[43], Y[43], n, t, s, best; +bool visited[43]; +void bt(int li, int cnt, int len) { + int h = abs(X[li]-n+1) + abs(Y[li]-n+1); + if (len + h >= best) return; + if (cnt == s) { best = len + h; return; } + + for (int i=0; i> n >> t >> s && (n||t||s); ++cse) { + for (int i=1; i<=t; ++i) + cin >> X[i] >> Y[i]; + + for (cuc=0; cuc<=t; ++cuc) { + for (int j=0; j + +using namespace std; + +const int MAX_N = 35; + +int tc, N, S, T, ans; +int dr[] = {1, -1, 0, 0}; +int dc[] = {0, 0, 1, -1}; +vector < pair > treasures; + +inline int dis(pair A, pair B) { + return abs(A.first - B.first) + abs(A.second - B.second); +} + +void backtrack(int used = 0, int cost = 0, pair last_point = {0, 0}) { + if (__builtin_popcount(used) == S) { + ans = min(ans, cost + dis(last_point, {N - 1, N - 1})); + return; + } + if (__builtin_popcount(used) == treasures.size()) return; + for (int i = 0; i < treasures.size(); i++) + if (((used >> i) & 1) == 0) { + int n_cost = cost + dis(last_point, treasures[i]); + if (n_cost < ans ) backtrack(used | (1 << i), n_cost, treasures[i]); + } +} + +void read() { + int a, b; + for (int i = 0; i < T; i++) { + scanf("%d %d", &a, &b); + treasures.push_back({a, b}); + } +} + +void init() { + treasures.clear(); + ans = INT_MAX; +} + +int main() { + while (scanf("%d %d %d", &N, &T, &S), N bitor T bitor S) { + init(); + read(); + backtrack(); + printf("Case %d: %d\n", ++tc, ans); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10891/10891-21.cpp b/uva_cpp_clean/10891/10891-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4da7b600d006686a8c985e234e79ae44997235a --- /dev/null +++ b/uva_cpp_clean/10891/10891-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10891 + Name: Game of Sum + Problem: https://onlinejudge.org/external/108/10891.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int d[105],s[105]; +int DP[105][105]; +int values(int x,int y){ + return s[y]-s[x-1]; +} +int main(){ + int n,i,j,k; + + while(scanf("%d",&n)==1 && n) + { + for(i=1;i<=n;i++){ + cin>>d[i]; + s[i]=s[i-1]+d[i]; + } + + for(j=1;j<=n;j++) + for(i=j;i>0;i--){ + DP[i][j]=-100000000; + for(k=0;k<=j-i;k++) + DP[i][j]=max(DP[i][j],max( + values(i,i+k)-DP[i+k+1][j], + values(j-k,j)-DP[i][j-k-1])); + } + + cout< +#include +#include + +using namespace std; + +long long n; + +long long gcd(long long a, long b) +{ + if (b == 0) return a; + + else return gcd(b, a % b); +} + +long long lcm(long long a, long long b) +{ + return (a / gcd(a, b)) * b; +} + +int main() +{ + while (cin >> n, n != 0) + { + int ans = 1; + + vector div; + + for (long long i = 1; i * i <= n; i++) + { + if (n % i == 0) + { + div.push_back(i); + + if (i != (n / i)) div.push_back(n / i); + } + } + + for (int i = 0; i < div.size(); i++) + { + for (int j = i; j < div.size(); j++) + { + if (i != j) + { + if (lcm (max(div[i], div[j]), min(div[i], div[j])) == n) + { + ans++; + } + } + } + } + + cout << n << " " << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10892/10892-19.cpp b/uva_cpp_clean/10892/10892-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3059d0318a0384ad4b8f935fe91dcc7e722320e8 --- /dev/null +++ b/uva_cpp_clean/10892/10892-19.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<100000001> bs; +vi primes; + +inline void sieve(ll upperbound = 50000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline void primeFactors(ll n, vii & factors) { + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +int main() { + sieve(); + ll N; + while (cin >> N && N != 0) { + vii factors; + primeFactors(N, factors); + int l = 1 << ((int)factors.size()); + int count = 0; + for (int i = 0; i < l; i++) { + int auxi = 1; + for (int j = 0; j < factors.size(); j++) { + if ((i >> j) & 1) auxi *= factors[j].second; + else auxi *= factors[j].second + 1; + } + count += auxi; + } + printf("%lld %d\n", N, (count+1)/2); + } + return 0; +} diff --git a/uva_cpp_clean/10892/10892-21.cpp b/uva_cpp_clean/10892/10892-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25d36264a040d2fcb3838dac5eb7b08b6fe7e0e7 --- /dev/null +++ b/uva_cpp_clean/10892/10892-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10892 + Name: LCM Cardinality + Problem: https://onlinejudge.org/external/108/10892.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + int n; + while (cin >> n && n) { + vector v; + int sq = sqrt(n); + for (int i=1; i<=sq; ++i) + if (n%i == 0) + v.push_back(i), + v.push_back(n/i); + if (sq*sq == n) + v.pop_back(); + + int card = 0; + for (int i=0, sz=v.size(); i + +using namespace std; + +int n; + +long long gcd(long long a, long long b){ + return b == 0 ? a : gcd(b, a % b); +} + +long long lcm(long long a, long long b){ + return a * ( b / gcd(a, b)); +} + +int cardinality(int num){ + int ct = 1, i; + vector div; + for(i = 1; i * i < num; i++) + if(num % i == 0) div.push_back(i), div.push_back(n/i); + if(i * i == num) div.push_back(i); + for(int i = 0; i < div.size(); i++) + for(int j = i + 1; j < div.size(); j++) + if( lcm(div[i], div[j]) == n) ct++; + return ct; +} + +int main(){ + while(scanf("%d",&n),n) printf("%d %d\n",n,cardinality(n)); + return(0); +} diff --git a/uva_cpp_clean/10895/10895-21.cpp b/uva_cpp_clean/10895/10895-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48f6dedc38e67028802b763fe5e025706ebf7f21 --- /dev/null +++ b/uva_cpp_clean/10895/10895-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10895 + Name: Matrix Transpose + Problem: https://onlinejudge.org/external/108/10895.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +#define MAXN 10017 +short deg[MAXN], adj[MAXN][MAXN], mat[MAXN][MAXN], ind[MAXN], off[MAXN]; + +int main(){ + int n, m, d, x, y; + while (scanf("%d%d", &n, &m)==2) { + memset(deg, 0, m*sizeof(short)); + for (int i=0; i +using namespace std; +using ii=tuple; +using vi=vector; +using vii=vector; +using vvii=vector; +int main(){ + int n,m,c,x; + while(cin>>n>>m){ + vvii a(m); + for(int i=0;i>c; + vi b(c); + for(int j=0;j>b[j]; + for(int j=0;j>x; + a[b[j]-1].push_back({i, x}); + } + } + cout<(a[i][j])+1; + cout<<"\n"; + if(c>0)for(int j=0;j(a[i][j])<<" \n"[j==c-1]; + else cout<<"\n"; + } + } +} diff --git a/uva_cpp_clean/10895/10895-9.cpp b/uva_cpp_clean/10895/10895-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40d8246f895b84a686aad7b4f2ddd9ffe752b1a4 --- /dev/null +++ b/uva_cpp_clean/10895/10895-9.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +vector < pair > a[10005]; +int aux[10005]; + +int main(){ + int n, m, r, tmp, k; + while(scanf("%d %d", &n, &m) != EOF){ + k = 1; + for(int i = 0; i < n; i++){ + scanf("%d", &r); + for(int j = 0;j < r; j++) + scanf("%d", &aux[j]); + for(int j = 0;j < r; j++){ + scanf("%d", &tmp); + a[aux[j]].push_back( make_pair(tmp, k) ); + } + k++; + } + printf("%d %d\n", m, n); + for(int i = 1; i <= m; i++){ + cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10898 + Name: Combo Deal + Problem: https://onlinejudge.org/external/108/10898.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + + +int Ci[143][6], Cp[143], csz, + S[10][10][10][10][10][10], DP[10][10][10][10][10][10], cse; +int rec(int r0, int r1, int r2, int r3, int r4, int r5) { + if (r0<0 || r1<0 || r2<0 || r3<0 || r4<0 || r5<0) return INF; + int &ss = S[r0][r1][r2][r3][r4][r5]; + if (ss == cse) return DP[r0][r1][r2][r3][r4][r5]; + + int mn = INF; + for (int i=0; i> n && n; ++cse) { + S[0][0][0][0][0][0] = cse; + csz = 0; + + for (int i=0; i> Cp[csz++]; + } + + cin >> m; + while (m--) { + memset(Ci[csz], 0, 24); + for (int i=0; i> Ci[csz][i]; + cin >> Cp[csz++]; + } + + cin >> m; + while (m--) { + int R[6] = {}; + for (int i=0; i> R[i]; + cout << rec(R[0], R[1], R[2], R[3], R[4], R[5]) << endl; + } + } +} diff --git a/uva_cpp_clean/109/109-21.cpp b/uva_cpp_clean/109/109-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fd0d4436f5231d1954a3ae641ae2db2b8dae87f --- /dev/null +++ b/uva_cpp_clean/109/109-21.cpp @@ -0,0 +1,115 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 109 + Name: SCUD Busters + Problem: https://onlinejudge.org/external/1/109.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 1e-8 +struct Point { + long long x, y; + Point():x(0),y(0){} + Point(long long x, long long y):x(x),y(y){} + bool operator < (const Point &o) const { return y == o.y ? x < o.x : y < o.y; } + Point operator - (const Point o) const { return Point(x-o.x, y-o.y); } + Point operator + (const Point o) const { return Point(x+o.x, y+o.y); } + long long operator * (const Point o) const { return x*o.x + y*o.y; } +}; + +std::istream& operator>> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} + +std::ostream& operator<< (std::ostream& stream, const Point p) { + stream << p.x << ' ' << p.y; + return stream; +} + +long long cross(const Point p1, const Point p2) { return p1.x*p2.y - p1.y*p2.x; } + +// leftTurn is positive +long long angle(const Point p1, const Point p2, const Point p3) { return cross(p2-p1, p3-p2); } + +// ----------------------------------------------------------------------------------------------- + +vector convex_hull(vector P) { + // Step 1: find lowest left point + int k = 0; + for (int i=P.size()-1; i>0; --i) + if (P[i] < P[k]) + k = i; + swap(P[k], P[0]); + + // Step 2: sort by polar angle to first point + sort(++P.begin(), P.end(), [=](const Point &a, const Point &b) { + return angle(a, P[0], b) < 0; + }); + P.push_back(P[0]); + + // Step 3: select all points with left turns + vector v; + for (Point p: P) { + while (v.size()>1 && angle(v[v.size()-2], v.back(), p) <= 0) + v.pop_back(); + v.push_back(p); + } + + return v; +} + + +bool inPoly(const vector P, const Point &p) { + for (int i=P.size()-1; i>0; --i) + if (angle(P[i-1], P[i], p) < 0) + return 0; + return 1; +} + +// area of convex polygon wehre v[0] == v[n] +double area(vector v) { + double sum = 0; + for (int i=v.size()-1; i>0; --i) + sum += cross(v[i-1], v[i]); + return sum / 2; +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, sz; + vector P[23]; + for (n=0; cin >> sz && sz>=0; ++n) { + P[n].resize(sz); + for (int i=0; i> P[n][i]; + + P[n] = convex_hull(P[n]); + } + + + Point p; + bool bombed[23] = {}; + while (cin >> p) { + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + int tt = 1; + + while (tc--) { + int n, t, m; + cin >> n >> t >> m; + queueleft, right; + + for (int i = 0; i < m; i++) { + int av_time; + string str; + cin >> av_time >> str; + + if (str == "left") { + left.push ({av_time, i}); + + } else { + right.push ({av_time, i}); + } + } + + int cur_time = 0; + bool pos = false; + vectorvec (m); + + while (1) { + if (left.empty() && right.empty() ) { + break; + } + + if (!pos) { + int cnt = 0; + + while (!left.empty() ) { + if (left.front().first <= cur_time && cnt < n) { + cnt++; + int x = left.front().second; + vec[x] = (cur_time + t); + left.pop(); + + } else { + break; + } + } + + if (cnt == 0) { + int l = INF, r = INF; + + if (!left.empty() ) { + l = left.front().first; + } + + if (!right.empty() ) { + r = right.front().first; + } + + if (l <= r) { + pos = false; + cur_time = max (l, cur_time); + + } else { + pos = true; + cur_time = max (r, cur_time) + t; + } + + } else { + pos = true; + cur_time += t; + } + + } else { + int cnt = 0; + + while (!right.empty() ) { + if (right.front().first <= cur_time && cnt < n) { + cnt++; + int x = right.front().second; + vec[x] = (cur_time + t); + right.pop(); + + } else { + break; + } + } + + if (cnt == 0) { + int l = INF, r = INF; + + if (!left.empty() ) { + l = left.front().first; + } + + if (!right.empty() ) { + r = right.front().first; + } + + if (l < r) { + pos = false; + cur_time = max (l, cur_time) + t; + + } else { + pos = true; + cur_time = max (r, cur_time); + } + + } else { + pos = false; + cur_time += t; + } + } + } + + if (tt != 1) nl; + + tt++; + + for (int i = 0; i < (int) vec.size(); i++) { + cout << vec[i] << "\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/10901/10901-19.cpp b/uva_cpp_clean/10901/10901-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9fa79c2fe721541f33d5820372103edb797c38b --- /dev/null +++ b/uva_cpp_clean/10901/10901-19.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; + +int main() { + int T; + cin >> T; + while (T--) { + int n, t, m; + cin >> n >> t >> m; + queue left, right; + for (int i = 0; i < m; i++) { + int val; + string s; + cin >> val >> s; + if (s == "left") left.push(ii(i, val)); + else right.push(ii(i, val)); + } + int site = 0, tc = 0; + vector v; + while (!(left.empty() && right.empty())) { + int count = 0; + bool move = false; + if (site == 0) { + while (!left.empty() && count < n && left.front().second <= tc) { + ii val = left.front(); + left.pop(); + val.second = tc + t; + v.push_back(val); + move = true; + count++; + } + if (move) { + tc += t; + site = 1; + } + } else if (site == 1) { + while (!right.empty() && count < n && right.front().second <= tc) { + ii val = right.front(); + right.pop(); + val.second = tc + t; + v.push_back(val); + move = true; + count++; + } + if (move) { + tc += t; + site = 0; + } + } + if (!move) { + int t1 = -1, t2 = -1; + if (!right.empty()) t1 = max(tc + (site == 1? 0: t), right.front().second + (site == 1? 0: t)); + if (!left.empty()) t2 = max(tc + (site == 0? 0: t), left.front().second + (site == 0? 0: t)); + if (t1 == -1) tc = t2, site = 0; + else if (t2 == -1) tc = t1, site = 1; + else { + if (right.front().second < left.front().second) tc = t1, site = 1; + else if (right.front().second > left.front().second) tc = t2, site = 0; + else tc = min(t1, t2); + } + } + } + sort(v.begin(), v.end()); + for (int i = 0; i < v.size(); i++) { + printf("%d\n", v[i].second); + } + if (T) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10901/10901-20.cpp b/uva_cpp_clean/10901/10901-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7a190c4a2ac4e30962ab784f9a227b42723c9ee --- /dev/null +++ b/uva_cpp_clean/10901/10901-20.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +const int MAXN = 10000; +string direction; +int time_left[MAXN+5]; +queue< int > l, r; +int unload_time[MAXN+5]; + +int main() { + #ifndef ONLINE_JUDGE + freopen("in.txt", "r", stdin); + freopen("out.txt", "w", stdout); + #endif // ONLINE_JUDGE + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + int c; + cin >> c; + for(int nCase = 1; nCase <= c; ++nCase) { + int n, t, m; + cin >> n >> t >> m; + + memset(time_left, 0, sizeof(time_left)); + while(!l.empty()) l.pop(); + while(!r.empty()) r.pop(); + for(int i = 0; i < m; ++i) { + int arrival_time; + cin >> arrival_time >> direction; + + time_left[i] = arrival_time; + if(direction == "left") l.push(i); + else r.push(i); + } + + memset(unload_time, 0, sizeof(unload_time)); + direction = "left"; + int current_time = 0, wait_time; + while(!l.empty() || !r.empty()) { + if(!l.empty() && time_left[l.front()] > 0 && r.empty()) wait_time = time_left[l.front()]; + else if(l.empty() && !r.empty() && time_left[r.front()] > 0) wait_time = time_left[r.front()]; + else if((!l.empty() && time_left[l.front()] > 0) && (!r.empty() && time_left[r.front()] > 0)) wait_time = min(time_left[l.front()], time_left[r.front()]); + else wait_time = 0; + + current_time += wait_time; + for(int i = 0; i < m; ++i) time_left[i] -= wait_time; + + int car_counter = 0; + if(direction == "left") { + while(car_counter < n && !l.empty() && time_left[l.front()] <= 0) { + unload_time[l.front()] = current_time + t; + l.pop(); + ++car_counter; + } + direction = "right"; + } + else { + while(car_counter < n && !r.empty() && time_left[r.front()] <= 0) { + unload_time[r.front()] = current_time + t; + r.pop(); + ++car_counter; + } + direction = "left"; + } + + current_time += t; + for(int i = 0; i < m; ++i) time_left[i] -= t; + } + + for(int i = 0; i < m; ++i) cout << unload_time[i] << "\n"; + if(nCase != c) cout << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/10901/10901-21.cpp b/uva_cpp_clean/10901/10901-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe9e73ffb9a7f4c36d8be97947437788b3a0d351 --- /dev/null +++ b/uva_cpp_clean/10901/10901-21.cpp @@ -0,0 +1,71 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10901 + Name: Ferry Loading III + Problem: https://onlinejudge.org/external/109/10901.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define INF 1073741824 +#define MAXN 10017 +int xl[MAXN], xr[MAXN], tl[MAXN], tr[MAXN], res[MAXN]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, t, m, tm; + cin>>T; + while (T--) { + cin>> n >> t >> m; + int lc=0, lr=0; + for (int i=0; i> tm >> dir; + if (dir[0]=='l') { + xl[lc] = i; + tl[lc++] = tm; + } + else { + xr[lr] = i; + tr[lr++] = tm; + } + } + + tr[lr] = tl[lc] = INF; + tm = 0; + int i=0, j=0, + *ic=&i, *io=&j, + *xc=xl, *xo=xr, + *tc=tl, *to=tr; + while (i +#include + +using namespace std; + +queue ferry; + +int main () +{ + // Int c: Cases + // Int n: Number of cars + // Int t: Time from one side to the other + // Int time: (actual) time + // Int side: (0 -> left while 1 -> right) + // Int tempTime: (input/passed) time + int c, n, t, m, time, side, tempTime; + // Character array tempSide: (input/passed) side + char tempSide[5]; + + scanf("%d",&c); + printf ("%d", c); + + while ( c-- ) + { + scanf("%d %d %d", &n, &t, &m); + printf ("%d %d %d", n, t, m); + // For simplier code (loop): + time = -t; + side = 0; + tempTime = -1; + + while ( n ) + { + time += t; + side ^= 1; + + while ( !ferry.empty() ) + { + printf ("%d\n", time); + ferry.pop(); + n--; + } + + if ( tempTime == -1 ) + scanf("%d %s", &tempTime, tempSide); + + // Checking if the ferry is on the right side: (meaning the correct one :P) + if ( ( ( tempSide == "right" ) && ( side == 1 ) ) + || ( ( tempSide == "left") && ( side == 0 ) ) ) + time = tempTime; + + while ( scanf ("%d %s", &tempTime, tempSide) && + ( ( ( tempSide == "right" ) && ( side == 1 ) ) + || ( ( tempSide == "left") && ( side == 0 ) ) ) && + tempTime == time ) + { + ferry.push(1); + tempTime = -1; + } + } + + puts(""); + } +} + diff --git a/uva_cpp_clean/10902/10902-21.cpp b/uva_cpp_clean/10902/10902-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5458c749b3335dfca38cdd017adc10fa02253cc0 --- /dev/null +++ b/uva_cpp_clean/10902/10902-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10902 + Name: Pick-up Sticks + Problem: https://onlinejudge.org/external/109/10902.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct Point { + double x, y; + Point():x(0),y(0){} + Point(double x, double y):x(x),y(y){} + bool operator < (const Point &o) const { return x < o.x; } + double operator * (const Point o) const { return x*o.x + y*o.y; } + Point operator - (const Point o) const { return Point(x-o.x, y-o.y); } + Point operator + (const Point o) const { return Point(x+o.x, y+o.y); } +}; + +double cross(const Point p1, const Point p2) { return p1.x*p2.y - p1.y*p2.x; } +double angle(const Point p1, const Point p2, const Point p3) { return cross(p2-p1, p3-p2); } +bool intersect(const Point p1, const Point p2, const Point p3, const Point p4) { + if (angle(p1, p2, p3)*angle(p1, p2, p4) > 0) return 0; + return angle(p3, p4, p1)*angle(p3, p4, p2) <= 0; +} + +std::istream& operator>> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} + +// ------------------------------------------------------------------------------- + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int R[1143], n; + Point Pu[100143], Pv[100143]; + while (cin >> n && n) { + int sz = 0; + for (int i=0; i> Pu[i] >> Pv[i]; + int k = 0; + for (int j=0; j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10903 + Name: Rock-Paper-Scissors Tournament + Problem: https://onlinejudge.org/external/109/10903.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 41943040; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +char readFirstChar() { + char ch, th; + while ((ch=readchar())==' '); + while ((th=readchar())!=' ' && th!='\n'); + return ch; +} + +// ----------------------------------------------------------------------- + + +char BUF[8000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 8000000); + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(3); + + bool wins[127][127] = {}; + wins['r']['s'] = + wins['s']['p'] = + wins['p']['r'] = true; + + bool frst = 1; + while (true) { + int n = readUInt(), + k = readUInt(); + if (n<0 || k<0) break; + + if (frst) frst = 0; + else cout << '\n'; + + int sz = n*(n-1)*k/2; + int W[101] = {}, L[101] = {}; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 51 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + int N; + vectornum; + while (scanf("%d", &N) == 1 && N != 0) { + getchar(); + for (int i = 0; i < N; i++) { + string s; + cin >> s; + num.push_back(s); + } + string str; + for (int i = 0; i < N; i++) { + for (int j = i + 1; j < N; j++) { + if ((num[i] + num[j]) < (num[j] + num[i])) { + string s = num[j]; + num[j] = num[i]; + num[i] = s; + } + } + str+=num[i]; + } + cout << str << "\n"; + num.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/10905/10905-21.cpp b/uva_cpp_clean/10905/10905-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1a6bdf78d0b3b6d49f0f39ac4b37d5c8cca347a --- /dev/null +++ b/uva_cpp_clean/10905/10905-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10905 + Name: Children's Game + Problem: https://onlinejudge.org/external/109/10905.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +bool com (const string& s1, const string& s2) { + return s1+s2 > s2+s1; +} + +int main(){ + int n; + string nums[55]; + + while (cin>>n && n) { + for (int i=0; i>nums[i]; + + sort(nums, nums+n, com); + for (int i=0; i + +#define SIZE 60 + +using namespace std; + +int n; +string v[SIZE], s1, s2; + +bool f(string x, string y){ + s1 = x + y, s2 = y + x; + return (s1 > s2); +} + +int main(){ + while(scanf("%d", &n), n!=0){ + for(int i = 0;i < n; i++) cin >> v[i]; + sort(v, v + n, f); + for(int i = 0; i < n; i++) cout << v[i]; cout << endl; + } + return(0); +} diff --git a/uva_cpp_clean/10908/10908-21.cpp b/uva_cpp_clean/10908/10908-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04fab161cca01e93df4e46381b472a20c1fcd7d3 --- /dev/null +++ b/uva_cpp_clean/10908/10908-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10908 + Name: Largest Square + Problem: https://onlinejudge.org/external/109/10908.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +unsigned char X[200][200], ch; +int LM[200][200], RM[200][200], TM[200][200], BM[200][200]; +int ru[200]; + + +int main(){ + int T, m, n, q; + scanf("%d", &T); + while (T--) { + scanf("%d%d%d", &m, &n, &q); + printf("%d %d %d\n", m, n, q); + + for (int j=0; j<=n; j++) ru[j]=0; + for (int i=0; i<=m; i++) { + if (i + +using namespace std; + +int tc, M, N, Q, x, y, ct; +char s[100][100]; + +bool isAllOne(int i, int j, int x, int y, char ch){ + if(i < 0 || j < 0 || x >= M || y >= N) return false; + for(int p = i; p <= x; p++) + for(int q = j; q <= y; q++) + if(s[p][q] != ch) return false; + return true; +} + +void solution(int p, int q, char ch){ + for(int r = 0; r < min(M, N); r++) + if(isAllOne(p - r, q - r, p + r, q + r, ch)) ct = 2 * r + 1; + printf("%d\n", ct); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d", &M, &N, &Q); + for(int i = 0; i < M; i++) scanf("%s", s[i]); + printf("%d %d %d\n", M, N, Q); + while(Q--){ + scanf("%d %d", &x, &y); + solution(x, y, s[x][y]); + } + } + return(0); +} diff --git a/uva_cpp_clean/10910/10910-19.cpp b/uva_cpp_clean/10910/10910-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..189aa87c031627d2d5abfcbd386e168d050d8c12 --- /dev/null +++ b/uva_cpp_clean/10910/10910-19.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int dp[71][71][71]; +bool mask[71][71][71]; + +int F(int N, int T, int P) { + if (T < P) return 0; + if (N == 1) return 1; + if (!mask[N][T][P]) { + for (int i = P; i < T; i++) + dp[N][T][P] += F(N-1, T-i, P); + mask[N][T][P] = true; + } + return dp[N][T][P]; +} + +int main() { + int K, N, T, P; + cin >> K; + while (cin >> N >> T >> P) { + printf("%d\n", F(N, T, P)); + } + return 0; +} diff --git a/uva_cpp_clean/10910/10910-9.cpp b/uva_cpp_clean/10910/10910-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3070141382b692cda27f03366ddd0e7cb8d22f84 --- /dev/null +++ b/uva_cpp_clean/10910/10910-9.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +const int MAX_N = 70 + 5, MAX_T = 70; + +int n, t, p; +long long memo[MAX_N][MAX_T]; + +long long dp (int id, int sum) { + if (id == n) return sum == t; + if (~memo[id][sum]) return memo[id][sum]; + long long ret = 0; + for (int i = p; (n - id - 1) * p <= t - sum - i; i++) { + ret += dp(id + 1, sum + i); + } + return memo[id][sum] = ret; +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + cin >> n >> t >> p; + memset(memo, -1, sizeof memo); + cout << dp(0, 0) << endl; + } + return (0); +} diff --git a/uva_cpp_clean/10911/10911-21.cpp b/uva_cpp_clean/10911/10911-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19a9c3f39c8ea5ecd5fbf4544a5da375a82038eb --- /dev/null +++ b/uva_cpp_clean/10911/10911-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10911 + Name: Forming Quiz Teams + Problem: https://onlinejudge.org/external/109/10911.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int S[1<<16], cse, n; +bool chosen[20]; +double D[20][20], DP[1<<16]; +double rec(int s, int state) { + if (!state) return 0; + if (S[state] == cse) + return DP[state]; + + double result = 1e100; + while (chosen[s]) ++s; + chosen[s] = 1; + + int nst = state ^ (1<> n && n; ++cse) { + n <<= 1; + for (int i=0; i> name >> X[i] >> Y[i]; + + for (int i=0; i +#define bit_is_on(S, i) (S & (1 << i)) +using namespace std; +typedef long long ll; +const double INF = 1 << 30; + +int N; +vector> coords; + +double dist(pair s, pair t) { + double dx = s.first - t.first; + double dy = s.second - t.second; + return sqrt(dx*dx + dy*dy); +} + +vector memo; +double dp(int mask) { + // conjunto de estudantes que já estão em um time + + if (mask == (1 << N)-1) return 0; + + double &ans = memo[mask]; + if (ans != -1) return ans; + + ans = INF; + int i = 0; while (bit_is_on(mask, i)) i++; + for (int j = i+1; j < N; j++) + if (!bit_is_on(mask, j)) + ans = min(ans, dist(coords[i], coords[j]) + dp(mask | (1 << i) | (1 << j))); + + return ans; +} + +int main() { + int t = 1; + while (cin >> N && N) { + N <<= 1; + coords.resize(N); + for (int n = 0; n < N; n++) { + string _; cin >> _; + cin >> coords[n].first >> coords[n].second; + } + memo.assign(1 << N, -1); + printf("Case %d: %.2f\n", t++, dp(0)); + } + return 0; +} diff --git a/uva_cpp_clean/10912/10912-9.cpp b/uva_cpp_clean/10912/10912-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9aca2c028ad95b38795cee009743888acdbd142a --- /dev/null +++ b/uva_cpp_clean/10912/10912-9.cpp @@ -0,0 +1,21 @@ +#include + +#define SIZE 10010 + +using namespace std; + +int tc,L,S,memo[30][SIZE][30]; + +int dp(int ct,int sum,char ch){ + if(ct==0 && sum==0) return 1; + if(!('a'<=ch && ch<='z') || sum<0 || ct<0 || ct>26 || sum>351) return 0; + if(~memo[ct][sum][ch-'a']) return memo[ct][sum][ch-'a']; + return memo[ct][sum][ch-'a'] = dp(ct-1,sum-(ch-'a'+1),ch+1)+dp(ct,sum,ch+1); +} + +int main(){ + memset(memo,-1,sizeof memo); + while(scanf("%d %d",&L,&S),L|S) + printf("Case %d: %d\n",++tc,dp(L,S,'a')); + return(0); +} diff --git a/uva_cpp_clean/10913/10913-21.cpp b/uva_cpp_clean/10913/10913-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4adcefe62c6c4ebb1987ffae99d1dd5ddad23182 --- /dev/null +++ b/uva_cpp_clean/10913/10913-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10913 + Name: Walking on a Grid + Problem: https://onlinejudge.org/external/109/10913.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +int dif[]={1, -1}, + mxj[]={1, 0}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int DP[143][143][7], X[143][143], n, m; + bool V[143][143][7]; + for (int k=0; k<7; ++k) + V[0][1][k] = 1; + + for (int cse=1; cin >> n >> m && (n||m); ++cse) { + for (int i=1; i<=n; ++i) + for (int j=1; j<=n; ++j) + cin >> X[i][j]; + + for (int i=1; i<=n; ++i) + for (int j=1; j<=n; ++j) { + for (int k=0; k<=m; ++k) + DP[i][j][k] = -INF, + V[i][j][k] = 0; + + for (int d=0; d<2; ++d) { + int sum=0, neg=0; + for (int jj=j; (mxj[d] ? jj<=n : jj>=0) && neg<=m; jj+=dif[d]) { + sum += X[i][jj]; + if (X[i][jj] < 0) ++neg; + for (int k=neg; k<=m; ++k) + if (V[i-1][jj][k-neg]) { + V[i][j][k] = 1; + DP[i][j][k] = max(DP[i][j][k], sum + DP[i-1][jj][k-neg]); + } + } + } + } + + bool flag = 0; + int mx = -INF; + for (int k=0; k<=m; ++k) + if (V[n][n][k]) { + mx = max(mx, DP[n][n][k]); + flag = true; + } + + cout << "Case " << cse << ": "; + if (!flag) + cout << "impossible\n"; + else + cout << mx << "\n"; + } +} diff --git a/uva_cpp_clean/10916/10916-18.cpp b/uva_cpp_clean/10916/10916-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..063ae31d36b589f76ec76efc33384b56c97c9c3a --- /dev/null +++ b/uva_cpp_clean/10916/10916-18.cpp @@ -0,0 +1,27 @@ +#include +#include + +using namespace std; + +int main(){ + int year, exp, n, bit; + double num; + while(scanf("%d", &year) && year != 0){ + num = 0; + exp = (year - 1960) / 10 + 2; + bit = pow(2, exp); + n = 0; + while(num < bit){ + n++; + num+= log2(n); + } + n--; + printf("%d\n", n); + } + return 0; +} +/* + El numero mas grande es 2^k donde "k" es el numero de bits, en este ejercicio k = 2^[(year-1960)/10]+2. Del + ejercicio tenemos que 2^k > n!, entonces, aligerando calculos la condicion quedaria asi: + k > log2(n) + log2(n-1) + ... + log2(1). +*/ \ No newline at end of file diff --git a/uva_cpp_clean/10916/10916-19.cpp b/uva_cpp_clean/10916/10916-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1007d7af7764243f1bec48965b5a035bff535bd --- /dev/null +++ b/uva_cpp_clean/10916/10916-19.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +using namespace std; + +int n[23]; + +void prep() { + long long fact = 1, acum = 0, expo = 0, pot = 1, i = 2; + while (expo < 23) { + fact *= i; + while (fact % 2 == 0 || fact >= INT_MAX) { //Prevent overflow + fact >>= 1; + acum++; + } + int bits = acum + ceil(log2(fact)); + if (bits > pot) { + n[expo] = i - 1; + expo++; + pot *= 2; + } + i++; + } + n[2]--; +} + +int main() { + int y; + prep(); + while (scanf("%d", &y) && y != 0) { + printf("%d\n", n[(y/10) - 194]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10916/10916-21.cpp b/uva_cpp_clean/10916/10916-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41a77315ea3af9a0d00abd3e7db87811a6e404ab --- /dev/null +++ b/uva_cpp_clean/10916/10916-21.cpp @@ -0,0 +1,23 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10916 + Name: Factstone Benchmark + Problem: https://onlinejudge.org/external/109/10916.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int R[] = { 3, 5, 8, 12, 20, 34, 57, 98, 170, 300, 536, 966, 1754, 3210, 5910, 10944, 20366, 38064, 71421, 134480, 254016 }; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while (cin >> n && n) + cout << R[n/10 - 196] << endl; +} diff --git a/uva_cpp_clean/10917/10917-17.cpp b/uva_cpp_clean/10917/10917-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae7dd9d64d591281b9432fb4945513b1a6c33f2b --- /dev/null +++ b/uva_cpp_clean/10917/10917-17.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; +using VI = vector; +using VVI = vector; +using VD = vector; +using VVD = vector; +using VS = vector; +using VVS = vector; +using VB = vector; +using VVB = vector; +using PII = pair; +using PDD = pair; +using PDI = pair; +constexpr double pi = 3.1415926535897932385; +constexpr double EPS = 1e-12; + +struct Edge +{ + int to, weight; + Edge() {}; + Edge(int t, int w) : to(t), weight(w) {}; +}; + +vector> adj; +vector dist; + +void dijkstra(int s, int t) +{ + dist = vector(adj.size(), INFINITY); // use INFINITY in double if dist might be added + dist[s] = 0; + priority_queue, greater> q; // weight, vertex + q.emplace(0, s); + while (!q.empty()) + { + PII cur = q.top(); + q.pop(); + if (cur.second == t) + break; + if (dist[cur.second] != cur.first) + continue; + for (Edge& edge : adj[cur.second]) + if (cur.first + edge.weight < dist[edge.to]) + { + dist[edge.to] = cur.first + edge.weight; + q.emplace(dist[edge.to], edge.to); + } + } +} + +vector path_num; +int num(int u) +{ + if (u == 1) + return 1; + if (path_num[u]) + return path_num[u]; + for (Edge& edge : adj[u]) + if (dist[edge.to] < dist[u]) + path_num[u] += num(edge.to); + return path_num[u]; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int N, M; + while (cin >> N >> M) + { + adj = vector>(N); + for (int i = 0; i < M; ++i) + { + int a, b, d; + cin >> a >> b >> d; + --a; + --b; + adj[a].emplace_back(b, d); + adj[b].emplace_back(a, d); + } + dijkstra(1, 0); + path_num = vector(N, 0); + cout << num(0) << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/10918/10918-20.cpp b/uva_cpp_clean/10918/10918-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d626e43cae28773c9b165466e1c00a6618d6e449 --- /dev/null +++ b/uva_cpp_clean/10918/10918-20.cpp @@ -0,0 +1,28 @@ +#include + +const int MAXN = 30; +int dp[MAXN+2]; + +int f( int n ); + +int g( int n ) { + if( n == 0 ) return 0; + if( n == 1 ) return 1; + return f( n - 1 ) + g( n - 2 ); +} + +int f( int n ) { + if( n == 0 ) return 1; + if( n == 1 ) return 0; + if( dp[n] != -1 ) return dp[n]; + return dp[n] = f( n - 2 ) + (2 * g( n - 1 )); +} + +int main( int argc, char ** argv ) { + memset( dp, -1, sizeof( dp ) ); + + int n; + while( scanf( "%d", &n ) && n != -1 ) printf( "%d\n", f( n ) ); + + return 0; +} diff --git a/uva_cpp_clean/10918/10918-21.cpp b/uva_cpp_clean/10918/10918-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f81e982bbcc38160890b2fd5f15b6500abc8f581 --- /dev/null +++ b/uva_cpp_clean/10918/10918-21.cpp @@ -0,0 +1,30 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10918 + Name: Tri Tiling + Problem: https://onlinejudge.org/external/109/10918.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int f[31],g[31]; +int main(){ + int i; + + f[0]=g[1]=1; + for(i=2;i<31;i++){ + f[i]=f[i-2]+2*g[i-1]; + g[i]=g[i-2]+f[i-1]; + } + + int n; + while(scanf("%d",&n)==1 && n!=-1) + cout< + +#define MAX ((int)16) +#define endl '\n' + +using namespace std; + +typedef long long ll; + +ll f[ MAX ] = { 1 }; + +void preprocess(){ + ll sum = 0; + for(int i = 1 ; i < MAX ; i++) + f[ i ] = 3 * (f[ i - 1 ]) + sum, sum += (f[ i - 1 ] << 1); +} + +int main(){ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + preprocess(); + int n; + while(cin >> n && ~n) + cout << ((n & 1) ? 0 : f[ n >> 1 ]) << endl; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10919/10919-2.cpp b/uva_cpp_clean/10919/10919-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eccb05b6a9d463a7dac2275f55f0c6dfb72c844 --- /dev/null +++ b/uva_cpp_clean/10919/10919-2.cpp @@ -0,0 +1,61 @@ +#include "bits/stdc++.h" + + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + + int k, m; + while(cin >> k && k) { + cin >> m; + map courses; + REP(i, k) { + string c; cin >> c; + ++courses[c]; + } + bool f = 1; + REP(i, m) { + int c, r; cin >> c >> r; + REP(j, c) { + string cc; cin >> cc; + if (courses.find(cc) != courses.end()) --r; + } + if (r > 0) f = 0; + } + cout << (f?"yes":"no") << '\n'; + } + + return 0; +} + diff --git a/uva_cpp_clean/10919/10919-20.cpp b/uva_cpp_clean/10919/10919-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c879d280206568af5d52fff5574255f8afc81d8 --- /dev/null +++ b/uva_cpp_clean/10919/10919-20.cpp @@ -0,0 +1,37 @@ +#include + +int k, m, temp, c, r, courseNumber, courseTaken; +bool courseSelected[10005], flag; + +int main() +{ + while( scanf("%d", &k)==1 && k ) + { + scanf("%d", &m); + + for(int i=0 ; i<10002 ; i++) courseSelected[i] = false; + + for(int i=1 ; i<=k ; i++) + { + scanf("%d", &temp); + courseSelected[temp] = true; + } + + flag = true; + for(int i=1 ; i<=m ; i++) + { + scanf("%d %d", &c, &r); + courseTaken = 0; + for(int j=1 ; j<=c ; j++) + { + scanf("%d", &courseNumber); + if( courseSelected[courseNumber] ) courseTaken++; + } + if( courseTaken>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10919 + Name: Prerequisites? + Problem: https://onlinejudge.org/external/109/10919.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if(p == end) { + if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + + + +int main() { + int taken[10000]; + for (int cse=1, n; (n = readUInt()); ++cse) { + int m = readUInt(); + for (int i=0; i 0) valid = false; + } + + puts(valid ? "yes" : "no"); + } +} + diff --git a/uva_cpp_clean/10919/main.cpp b/uva_cpp_clean/10919/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c60b6372223a4196c6bb85b83f490ae97d88bead --- /dev/null +++ b/uva_cpp_clean/10919/main.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + +bool is_in(vector choosen, int n){ + bool res = false; + for(int i=0; i < choosen.size(); i++){ + if(choosen[i] == n) res = true; + } + return res; +} + +int main(){ + int n_curses, n_categories, curse, number_following_curses, requisite, lenght; + vector choosen; + bool res; + while(cin >> n_curses >> n_categories){ + choosen.clear(); + res = true; + for(int i=0; i < n_curses; i++){ + cin >> curse; + choosen.push_back(curse); + lenght++; + } + for(int i=0; i> number_following_curses >> requisite; + if(requisite > lenght){ + res = false; + break; + } + for(int c=0; c < number_following_curses; c++){ + cin >> curse; + if(is_in(choosen, curse)) acepted++; + } + if(acepted < requisite) res = false; + } + if(res) cout << "yes" << endl; + else cout << "no" << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/10920/10920-11.cpp b/uva_cpp_clean/10920/10920-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe9cd9dc2718a2ab4d44deff9755efb4dd15016c --- /dev/null +++ b/uva_cpp_clean/10920/10920-11.cpp @@ -0,0 +1,90 @@ +#include +/* +Problem: 10920 - Spiral Tap +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1861 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int SZ, P; + +int main(){ + fastio; + + while (scanf("%d%d", &SZ, &P) && (SZ|P)){ + int l = 0, r = 1+SZ/2, u = 1+SZ/2, d = 0; + + int p = 1; + int step = 1; + + while (p != P){ + p += step; u+= step; + if (P<=p) {u-= p-P; break;} + + p += step; l+= step; + if (P<=p) {l-= p-P; break;} + + step = min(step+1, SZ-1); + + p += step; d+= step; + if (P<=p) {d-= p-P; break;} + + p += step; r+= step; + if (P<=p) {r-= p-P; break;} + + step = min(step+1, SZ-1); + } + + printf("Line = %d, column = %d.\n", abs(u-d), abs(l-r)); + } + + return 0; +} +/* +Sample input:- +----------------- +3 1 +3 3 +3 9 +5 9 +5 10 +0 0 + +Sample output:- +----------------- +Line = 2, column = 2. +Line = 3, column = 1. +Line = 3, column = 3. +Line = 4, column = 4. +Line = 5, column = 4. + +Resources:- +------------- + +Explanation: +--------------- +5: 13 12 11 10 25 +4: 14 3 2 9 24 +3: 15 4 1 8 23 +2: 16 5 6 7 22 +1: 17 18 19 20 21 + -------------- + 1 2 3 4 5 + + u d l r + 2 0 0 2 + 3 0 0 2 + 3 0 1 2 + +*/ diff --git a/uva_cpp_clean/10920/10920-19.cpp b/uva_cpp_clean/10920/10920-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d6eabbac06b02992d7aa137ef572aafe2b894ae --- /dev/null +++ b/uva_cpp_clean/10920/10920-19.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +int main() { + long long S, P; + while (cin >> S >> P && !(S == 0 && P == 0)) { + long long val = floor(sqrt(P)); + if (P == val*val) val--; + long long x, y; + if (val % 2 == 1) { + x = val + 1, y = val + 1; + x -= P - 1 - val*val; + if (x < 1) y += x - 1, x = 1; + val++, y++; + } else { + y = 1, x = P - val*val; + if (x > val + 1) y = x - val, x = val + 1; + } + x += (S - val - 1)/2, y += (S - val - 1)/2; + printf("Line = %lld, column = %lld.\n", y, x); + } + return 0; +} diff --git a/uva_cpp_clean/10920/10920-21.cpp b/uva_cpp_clean/10920/10920-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07555400601ea2373616d0ffa74f84936596d632 --- /dev/null +++ b/uva_cpp_clean/10920/10920-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10920 + Name: Spiral Tap + Problem: https://onlinejudge.org/external/109/10920.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +int sdi[] = {1, 0, -1, 0}, + sdj[] = {0, -1, 0, 1}; + +int main() { + long long sz, n, t, i, j; + while (scanf("%lld%lld", &sz, &n)==2 && sz) { + if ( n > 1) { + int m = (sz+1)>>1, + l = int(ceil(sqrt(n))) | 1, // 5 + hl = (l+1)>>1, // 3 + o = (l-2)*(l-2) + 1, // 10 + d = n - o, + s = d / (l - 1), // side + so = d % (l - 1); // side offset + i = m + hl - 1, + j = m + (hl>1 ? hl-2 : 0) - so; + + for (int k=0; k>1; + + printf("Line = %lld, column = %lld.\n", i, j); + } +} diff --git a/uva_cpp_clean/10920/10920-3.cpp b/uva_cpp_clean/10920/10920-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c750bb85a5b7f80b079164cff357038feef38b6 --- /dev/null +++ b/uva_cpp_clean/10920/10920-3.cpp @@ -0,0 +1,66 @@ +#include //scanf printf + +#define sqr(i) i*i + +int main () +{ + // List of variables. + int resultX, resultY, currentX, currentY, currentValue, + squareLength, SZ, P; + + bool notFound; + while ( scanf("%d %d", &SZ, &P) == 2, SZ ) + { + // The location of the starting cell + currentX = SZ/2 + 1; + currentY = currentX; + notFound = true; + + for ( squareLength = 1; ; squareLength+=2 ) + if ( P < sqr(squareLength) ) + break; + + currentX += squareLength / 2; + currentY = currentX; + currentValue = sqr(squareLength) - squareLength + 1; + + if ( P >= currentValue ) + { + resultX = currentX; + resultY = currentY - ( P - currentValue ); + notFound = false; + } + + currentValue -= squareLength - 1; + currentY -= squareLength -1; + + if ( notFound && P >= ( sqr(squareLength) - 2*squareLength + 2) ) + { + resultY = currentY; + resultX = currentX - ( P - currentValue) ; + notFound = false; + } + + currentValue -= squareLength - 1; + currentX -= squareLength - 1; + + if ( notFound && P >= ( currentValue - squareLength + 1 ) ) + { + resultX = currentX; + resultY = currentY - ( P - currentValue ); + notFound = false; + } + + currentY += squareLength - 1; + currentValue -= squareLength - 1; + + if ( notFound ) + { + resultY = currentY; + resultX = currentX - ( P - currentValue ); + } + + printf ("%s = %d, %s = %d.\n","Line", resultY, "column", resultX); + } +} + diff --git a/uva_cpp_clean/10920/10920-9.cpp b/uva_cpp_clean/10920/10920-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77dc307e7b2001f6b574c49fefedf38a0e1518a8 --- /dev/null +++ b/uva_cpp_clean/10920/10920-9.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int n, px, py, pib; +long long num, d; + +int main(){ + while(scanf("%d %lld", &n, &num), n | num){ + d = ( sqrt(num) + 1) / 2; + px = py = ( n + 1 ) / 2 + d; + if( (2 * d - 1) * (2 * d - 1) == num ) px -= 1, py -= 1; + else{ + pib = (2 * d + 1) * (2 * d + 1); + d *= 2; + if(num >= pib - d) px -= pib - num; + else if(num >= pib - 2 * d) py -= pib - d - num, px -= d; + else if(num >= pib - 3 * d) py -= d, px -= d, px += pib - 2 * d - num; + else py -= d, py += pib - 3 * d - num; + } + printf("Line = %d, column = %d.\n", px, py); + } + return(0); +} diff --git a/uva_cpp_clean/10921/10921-14.cpp b/uva_cpp_clean/10921/10921-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5733a0282f5e3f3165fc6c467f9c23c61040f42b --- /dev/null +++ b/uva_cpp_clean/10921/10921-14.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#define MAX 35 +int num(char ch){ + if(ch == 'A' || ch == 'B' || ch == 'C') + return 2; + else if(ch == 'D' || ch == 'E' || ch == 'F') + return 3; + else if(ch == 'G' || ch == 'H' || ch == 'I') + return 4; + else if(ch == 'J' || ch == 'K' || ch == 'L') + return 5; + else if(ch == 'M' || ch == 'N' || ch == 'O') + return 6; + else if(ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S') + return 7; + else if(ch == 'T' || ch == 'U' || ch == 'V') + return 8; + else if(ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z') + return 9; +} +int main() +{ + char str[MAX]; + while(scanf("%s", str) == 1){ + int len = strlen(str); + for(int i = 0; i +using namespace std; +#define FRI(i, a, b) for(i = a; i < b; ++i) + +char map[] = {'2', '2', '2', '3', '3', '3', '4', '4', '4', + '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', + '8', '8', '8', '9', '9', '9', '9'}; + +int main() { + int i, length; + string text; + while (getline (cin, text)) { + length = text.length(); + FRI(i, 0, length) { + if (text.at(i) >= 'A' && text.at(i) <= 'Z') + text.replace(i, 1, 1, map[text.at(i) - 'A']); + } + cout << text << endl; + } + return 0; +} diff --git a/uva_cpp_clean/10921/10921-19.cpp b/uva_cpp_clean/10921/10921-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96260c0a26dfb4e6aef6ac9838a0a1509fd0cf7c --- /dev/null +++ b/uva_cpp_clean/10921/10921-19.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +char charToNum(char c) { + switch(c) { + case 'A': + case 'B': + case 'C': return '2'; + case 'D': + case 'E': + case 'F': return '3'; + case 'G': + case 'H': + case 'I': return '4'; + case 'J': + case 'K': + case 'L': return '5'; + case 'M': + case 'N': + case 'O': return '6'; + case 'P': + case 'Q': + case 'R': + case 'S': return '7'; + case 'T': + case 'U': + case 'V': return '8'; + case 'W': + case 'X': + case 'Y': + case 'Z': return '9'; + } + return c; +} + +int main() { + char c; + while(scanf("%c", &c) != EOF) { + printf("%c", charToNum(c)); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10921/10921-20.cpp b/uva_cpp_clean/10921/10921-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..899edff99db7c486be7634a2074d0869149c3317 --- /dev/null +++ b/uva_cpp_clean/10921/10921-20.cpp @@ -0,0 +1,26 @@ +#include +#include + +char line[35]; + +int main() +{ + while( scanf("%s", line)!=EOF ) + { + int len = strlen(line); + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10921 + Name: Find the Telephone + Problem: https://onlinejudge.org/external/109/10921.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int codes[] = { 2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9 }; + +int main() { + char ch; + while (scanf("%c", &ch) == 1) { + if (ch >= 'A' && ch <= 'Z') + cout << codes[ch - 'A']; + else if (ch >= 'a' && ch <= 'z') + cout << codes[ch - 'a']; + else cout << ch; + } + if (ch != '\n') cout << endl; +} diff --git a/uva_cpp_clean/10921/10921-5.cpp b/uva_cpp_clean/10921/10921-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..18967736ee87c66e0fb8a90f4aec8bf0f2277933 --- /dev/null +++ b/uva_cpp_clean/10921/10921-5.cpp @@ -0,0 +1,76 @@ +/*#include +#include +int main() +{ + char s[50],l; + int i,j; + while(scanf("%s",&s)==1) + { + l=strlen(s); + for(i=0;i +#include + +int main() +{ + char a[1000]; + +int len,i,j; +while(gets(a)) +{ +len=strlen(a); +for(i=0;i + +using namespace std; + +const int LEN = 35; + +char line[LEN]; +map mp; + +int main(){ + for(int i = 0; i < 15; i++) + mp[ 'A' + i ] = '0' + ( 2 + i / 3 ); + for(char ch = 'P'; ch <= 'S'; ch++) + mp[ch] = '7'; + for(char ch = 'T'; ch <= 'V'; ch++) + mp[ch] = '8'; + for(char ch = 'W'; ch <= 'Z'; ch++) + mp[ch] = '9'; + mp['0'] = '0', mp['1'] = '1', mp['-'] = '-'; + while(~scanf("%s", line)){ + for(int it = 0; line[it]; it++) + putchar( mp[line[it]] ); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10922/10922-19.cpp b/uva_cpp_clean/10922/10922-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc44a61cfbfb45437ee61949bf4c1f370702d64d --- /dev/null +++ b/uva_cpp_clean/10922/10922-19.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +int proc(int sum, bool & mul) { + if (sum < 10) { + mul = sum == 9; + return 1; + } + int nsum = 0; + while (sum != 0) { + nsum += sum % 10; + sum /= 10; + } + return proc(nsum, mul) + 1; +} + +int main() { + string s; + while (cin >> s && s != "0") { + int sum = 0; + for (int i = 0; i < s.length(); i++) { + sum += s[i]-'0'; + } + bool mul; + int degree = proc(sum, mul); + printf("%s ", s.c_str()); + if (mul) printf("is a multiple of 9 and has 9-degree %d.\n", degree); + else printf("is not a multiple of 9.\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10922/10922-21.cpp b/uva_cpp_clean/10922/10922-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9d5d5c2af18c06690948a799502e53dce94c0f5 --- /dev/null +++ b/uva_cpp_clean/10922/10922-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10922 + Name: 2 the 9s + Problem: https://onlinejudge.org/external/109/10922.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char line[2000]; + while (cin>>line && (line[0]!='0' || line[1])) { + int sum = 0, cnt = 1; + for (int i=0; line[i]; i++) + sum += line[i] - '0'; + + if (sum % 9) + cout << line << " is not a multiple of 9.\n"; + + else { + int cnt = 1; + while (sum != 9) { + int n=sum; + sum = 0; + cnt++; + for (; n; n /= 10) + sum += n%10; + } + + printf("%s is a multiple of 9 and has 9-degree %d.\n", line, cnt); + } + } +} diff --git a/uva_cpp_clean/10922/10922-5.cpp b/uva_cpp_clean/10922/10922-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cce86c73bc5057323461f55082b70616372e3703 --- /dev/null +++ b/uva_cpp_clean/10922/10922-5.cpp @@ -0,0 +1,82 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define sort(v) sort(v.begin(),v.end()) +#define un(v) sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; + +int main() +{ + char s[1005]; + while(sc("%s",s)) + { + int l=strlen(s); + if(strcmp(s,"0")==0) + break; + int sum=0; + for(int i=0;i9) + { + sum=0; + c++; + while(d>0) + { + sum+=(d%10); + d/=10; + } + //pf("%d\n",sum); + d=sum; + } + pf("%s is a multiple of 9 and has 9-degree %d.\n",s,c); + } + } +return 0; +} diff --git a/uva_cpp_clean/10924/10924-19.cpp b/uva_cpp_clean/10924/10924-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2afa4086cdaae5e0aa81c05a98e43cfd82547e84 --- /dev/null +++ b/uva_cpp_clean/10924/10924-19.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; + +ll sieve_size; +bitset<10000010> bs; +unordered_set primes; +void sieve(ll upperbound) { + sieve_size=upperbound+1; bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.insert((int)i); + } +} + +int main() { + sieve(2000); + primes.insert(1); + string s; + while (cin >> s) { + int val = 0; + for (int i = 0; i < s.length(); i++) { + if (isupper(s[i])) val += 27 + (s[i]-'A'); + else if (islower(s[i])) val += 1 + (s[i]-'a'); + } + if (primes.count(val)) cout << "It is a prime word." << endl; + else cout << "It is not a prime word." << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10924/10924-21.cpp b/uva_cpp_clean/10924/10924-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..468f4c3a3ada49ad2fc2f152cf23e222b945922d --- /dev/null +++ b/uva_cpp_clean/10924/10924-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10924 + Name: Prime Words + Problem: https://onlinejudge.org/external/109/10924.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define N 51000000 +unsigned int prime[N / 64]; +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) +#define rP(n) (prime[n>>6]&=~(1<<((n>>1)&31))) +void sieve() +{ + memset( prime, -1, sizeof( prime ) ); + + unsigned int i; + unsigned int sqrtN = ( unsigned int )sqrt( ( double )N ) + 1; + for( i = 3; i < sqrtN; i += 2 ) if( gP( i ) ) + { + unsigned int i2 = i + i; + for( unsigned int j = i * i; j < N; j += i2 ) rP( j ); + } +} + +bool isPrime(int x) { + return x==2 || (x%2==1 && gP(x)); +} + +int main() { + sieve(); + + char word[100]; + while(cin>>word) { + int x = 0; + for (int i=0; word[i]; i++) + x+=word[i]-(word[i]<='Z' ? 'A'-26 : 'a')+1; + + cout << "It is " << (isPrime(x) ? "" : "not ") << "a prime word.\n"; + } +} diff --git a/uva_cpp_clean/10924/10924-5.cpp b/uva_cpp_clean/10924/10924-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bc98b24d248ae63c3091121c6d31402f4b941181 --- /dev/null +++ b/uva_cpp_clean/10924/10924-5.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +int main() +{ +int i,l,sum,n,p,j; +char c[100]; +while(gets(c)) + { + sum=0; + l=strlen(c); + for(i=0;i='a'&&c[i]<='z') + { + n=toascii(c[i])-96; + sum=sum+n; + } + else + { + n=toascii(c[i])-38; + sum=sum+n; + } + } + //printf("%d",sum); + p=1; + for(j=2; j<=sqrt(sum); j++) + { + if(sum%j==0) + { + p=0; + break; + } + } + if(p==1 || sum==1) + printf("It is a prime word.\n"); + + else if(p==0) + printf("It is not a prime word.\n"); + } + return 0; +} + diff --git a/uva_cpp_clean/10924/10924-9.cpp b/uva_cpp_clean/10924/10924-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84323fe7d44fa051e8d78bf34439bb36547d68b4 --- /dev/null +++ b/uva_cpp_clean/10924/10924-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int sum; +char word[25]; +map mp; + +bool isPrime(int num){ + for(int i = 2; i * i <= num; i++) + if( num % i == 0 ) return false; + return true; +} + +int main(){ + for(char ch = 'a', ct = 1; ch <= 'z'; ch++, ct++) mp[ch] = ct; + for(char ch = 'A', ct = 27; ch <= 'Z'; ch++, ct++) mp[ch] = ct; + while(~scanf("%s", word)){ + sum = 0; + for(int i = 0; word[i]; i++) sum += mp[word[i]]; + if( isPrime(sum) ) puts("It is a prime word."); + else puts("It is not a prime word."); + } + return(0); +} diff --git a/uva_cpp_clean/10926/10926-21.cpp b/uva_cpp_clean/10926/10926-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90806d4760a21c8b9a2c54a7620bf70992290f8d --- /dev/null +++ b/uva_cpp_clean/10926/10926-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10926 + Name: How Many Dependencies? + Problem: https://onlinejudge.org/external/109/10926.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +vector adj[143]; +int n; bool seen[143]; +int dfs(int u) { + seen[u] = true; + int result = 0; + for (int v: adj[u]) + if (!seen[v]) + result += dfs(v) + 1; + return result; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n && n) { + for (int i=0, sz; i> sz; + adj[i].resize(sz); + for (int j=0; j> adj[i][j]; + --adj[i][j]; + } + } + + int mx=-1, mxi; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() +#define coff ios_base::sync_with_stdio(0); + +#define ff first +#define se second +#define pb push_back +#define sz(a) ((int)a.size()) +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one. in long long use __builtin_popcountll(i) +#define parity(i) __builtin_parity(i) //evenparity 0 and odd parity 1 +#define btz(a) __builtin_ctz(a) //count binary trailling zero +#define un(v) ST(v), (v).erase(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +typedef long long ll; +typedef unsigned long long ull; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Debug +#define dbg(x) cout<<#x<<'='<>= 1; } return res; } +ll modInverse(ll a, ll m){return bigmod(a,m-2,m);} + +////============ CONSTANT ===============//// +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx 100010 +#define mod 1000000007 +////=====================================//// + +int cnt,col[110]; +vectorg[110]; + +int dfs(int n) +{ + col[n]=1; + int len=sz(g[n]); + for(int i=0;i>n) + { + if(n==0) + break; + + int t; + for(int i=1;i<=n;i++) + { + cin>>t; + int p; + for(int j=0;j>p; + g[i].pb(p); + } + } + + int res=0,get,resi; + //nl; + for(int i=1;i<=n;i++) + { + cnt=0; + cover(col,0); + get=dfs(i); + if(get>res) + { + resi=i; + res=get; + // cout< +using namespace std; +using vi=vector; +using vvi=vector; +using si=unordered_set; +using vsi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + int n,m,u,v; + cin>>n; + if(!n)break; + vvi g(n); + for(int i=0;i>m; + g[i]=vi(m); + for(int j=0;j>u; + g[i][j]=u-1; + } + } + vsi a(n); + functiondfs=[&](int u){ + a[u].insert(u); + for(int v:g[u]){ + if(a[v].empty())dfs(v); + for(int x:a[v])a[u].insert(x); + } + }; + for(int i=0;iM){ + M=a[i].size(); + k=i+1; + } + cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10927 + Name: Bright Lights + Problem: https://onlinejudge.org/external/109/10927.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) return EOF; + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +// -------------------------------------------------------------------------------------- + +#define EPS 1e-16 +double A[100143]; +long long R[100143]; +int I[100143], J[100143], X[100143], Y[100143], L[100143]; + +bool comp2(int i, int j) { return X[i]==X[j] ? Y[i] < Y[j] : X[i] < X[j]; } +bool same(int i, int j) { return X[i]*Y[j] == X[j]*Y[i]; } +bool comp1(int i, int j) { + int xy1 = X[i]*Y[j], xy2 = X[j]*Y[i]; + return xy1==xy2 ? R[i] < R[j] : xy1 < xy2; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + for (int cse=1; (n=readInt())>0; ++cse) { + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10928 + Name: My Dear Neighbours + Problem: https://onlinejudge.org/external/109/10928.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if(p == end) { + if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF; + p = buf; + } + return *p++; +} + +int readCount() { + int c = 0; + char ch, lastCh = ' '; + while ((ch = readchar()) != '\n' && ch>=0) { + if (lastCh==' ' && ch!=' ') + ++c; + lastCh = ch; + } + return c; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +int readInt() { + int r = 0; + char ch; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = r*10 + ch-'0'; + + while (ch!='\n') ch=readchar(); + return r; +} + + + +int A[2000]; +int main(){ + int T, n, c; + T = readInt(); + while (T--) { + n = readInt(); + + int mn=100000, c=0; + for (int i=0; i +using namespace std; +using vi=vector; +int main(){ + int t,n,x; + cin>>t; + while(t--){ + cin>>n; + string s; + getline(cin,s); + int m=n; + vi r; + for(int i=0;i>x)k++; + if(k + +using namespace std; + +string a[1000]; +char s[1000]; +int ans[1000]; +vector aux; + +int main(){ + int c, n, mini; + scanf("%d", &c); + while(c--){ + mini = 1000000; + scanf("%d\n", &n); + for(int i = 0; i < n; i++){ + getline(cin, a[i]); + ans[i] = 1; + for(int j = 0; j < a[i].size(); j++) + if(a[i][j] == ' ') ans[i]++; + mini = min(mini,ans[i]); + } + for(int i = 0; i < n; i++) + if(ans[i] == mini) aux.push_back(i+1); + for(int i = 0; i < aux.size(); i++){ + if(i) putchar(' '); + cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10929 + Name: You can say 11 + Problem: https://onlinejudge.org/external/109/10929.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char line[1005]; + while(cin>>line && (line[1] || line[0]!='0')) { + int sum = 0; + for(int i=0; line[i]; i++) + sum += (i%2 ? -1 : 1) * (line[i]-'0'); + cout << line << " is" << (sum%11 ? " not" : "") <<" a multiple of 11.\n"; + } +} diff --git a/uva_cpp_clean/10929/10929-5.cpp b/uva_cpp_clean/10929/10929-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d6f66ef8bb70b292db3cb5ac9333fd7290595a5f --- /dev/null +++ b/uva_cpp_clean/10929/10929-5.cpp @@ -0,0 +1,24 @@ +#include +#include +int main() +{ + int i,sum,r,l; + char s[1020]; + while(gets(s)) + { + r=0; + l=strlen(s); + if(l==1 && s[0]=='0') + break; + for(i=0;i +#include +#include +using namespace std; + +int main() { + int D, c = 1; + unordered_set set; + vector v, v1; + while (cin >> D) { + set.clear(); + v1.assign(D, 0); + int last = -1; + bool ok = true; + int i = 0; + for (; i < D && ok; i++) { + int val; + cin >> val; + v1[i] = val; + if (val > last && !set.count(val)) { + last = val; + v.clear(); + for (auto it = set.begin(); it != set.end(); ++it) { + v.push_back((*it) + val); + } + for (int j = 0; j < v.size(); j++) set.insert(v[j]); + set.insert(val); + } else ok = false; + } + for (; i < D; i++) cin >> v1[i]; + printf("Case #%d:", c++); + for (int i = 0; i < v1.size(); i++) printf(" %d", v1[i]); + printf("\n"); + if (ok) printf("This is an A-sequence.\n"); + else printf("This is not an A-sequence.\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10930/10930-21.cpp b/uva_cpp_clean/10930/10930-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ca69ebc73c015b27ca3ca9e9f5996d5e55031d1 --- /dev/null +++ b/uva_cpp_clean/10930/10930-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10930 + Name: A-Sequence + Problem: https://onlinejudge.org/external/109/10930.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +bool DP[35][1005]; +int d[35],n; +int check(){ + for(int i=2;i<=n;i++) + if(DP[i-1][d[i]]) + return 0; + return 1; +} + +int main(){ + int i,s,cse=1;; + + DP[0][0]=1; + while(scanf("%d",&n)==1) + { + for(i=1;i<=n;i++) + cin>>d[i]; + + + for(i=1;i<=n;i++) + for(s=0;s<=d[n];s++){ + DP[i][s]=DP[i-1][s]; + if(s>=d[i])DP[i][s]=DP[i][s]||DP[i-1][s-d[i]]; + } + + + printf("Case #%d:",cse++); + for(i=1;i<=n;i++) + cout<<" "< +#include +using namespace std; + +int main() { + int n; + stack s; + while (scanf("%d", &n) && n != 0) { + int bits = 0, aux = n; + while (aux != 0) { + bits += aux % 2; + s.push(aux % 2 != 0); + aux >>= 1; + } + printf("The parity of "); + while (!s.empty()) printf("%d", s.top()), s.pop(); + printf(" is %d (mod 2).\n", bits); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10931/10931-21.cpp b/uva_cpp_clean/10931/10931-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b6fc3f18373f43f29f896a0b02f2b0eddbe8657 --- /dev/null +++ b/uva_cpp_clean/10931/10931-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10931 + Name: Parity + Problem: https://onlinejudge.org/external/109/10931.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int parity(int n) { + if (!n) return 0; + + int r = (n & 1) + parity(n >> 1); + cout << (n & 1); + return r; +} + + +int main() { + int n; + while (cin >> n && n) { + cout << "The parity of "; + int r = parity(n); + cout << " is " << r << " (mod 2).\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10931/10931-5.cpp b/uva_cpp_clean/10931/10931-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2d3b2ffdd01fa358a91040e56d6072930b8d19de --- /dev/null +++ b/uva_cpp_clean/10931/10931-5.cpp @@ -0,0 +1,29 @@ +#include +int main() +{ + unsigned long int n; + int i,c,b[1000]; + while(scanf("%lu",&n)==1) + { + if(n==0) + break; + i=0; + while(n!=0) + { + b[i]=n%2; + n/=2; + i++; + } + c=0; + printf("The parity of "); + for(i=i-1;i>=0;i--) + { + printf("%d",b[i]); + if(b[i]==1) + c++; + } + printf(" is %d (mod 2).\n",c); + + } + return 0; +} diff --git a/uva_cpp_clean/10935/10935-10.cpp b/uva_cpp_clean/10935/10935-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3d0d56cd8d39b20440a912e7c8342b8a24854ca --- /dev/null +++ b/uva_cpp_clean/10935/10935-10.cpp @@ -0,0 +1,52 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) +#define ll long long int +using namespace std; + +/* +by:Ahmed Sakr (sakr_) with AZA ;l + +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + + __builtin_popcount(x); + +*/ + + +int main() +{Sakr_; + + int n,i; + + while(cin>>n) + { + if(n==0)break; + dequeq; + for(i=1;i<=n;i++)q.push_back(i); + + if(q.size()==1)cout<<"Discarded cards:"; + else + cout<<"Discarded cards: "; + if(q.size()>=2) + { + cout<1) + { + cout<<", "< +/* +Problem: 10935 - Throwing cards away I +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1876 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int N = 0; + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + + while ( cin >> N && N != 0 ){ + //initialize the queue + queue deck; + for (int i = 1; i <= N; i++){ + deck.push(i); + } + + printf("Discarded cards:"); + + if (deck.size() >= 2){ + printf(" %d", deck.front()); + deck.pop(); + deck.emplace(deck.front()); + deck.pop(); + } + + while (deck.size() >= 2){ + printf(", %d", deck.front()); + deck.pop(); + deck.emplace(deck.front()); + deck.pop(); + } + printf("\nRemaining card: %d\n", deck.front()); + + } + return 0; +} +/* +input:- +----------- +7 +19 +10 +6 +0 + +output:- +----------- +Discarded cards: 1, 3, 5, 7, 4, 2 +Remaining card: 6 +Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14 +Remaining card: 6 +Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8 +Remaining card: 4 +Discarded cards: 1, 3, 5, 2, 6 +Remaining card: 4 + +Resources:- +------------- +http://www.cplusplus.com/reference/queue/queue/ +https://www.geeksforgeeks.org/queue-cpp-stl/ +https://www.tutorialspoint.com/cpp_standard_library/queue.htm + +*/ diff --git a/uva_cpp_clean/10935/10935-14.cpp b/uva_cpp_clean/10935/10935-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..46a57d342109df7b31e97fef32119658587c61b4 --- /dev/null +++ b/uva_cpp_clean/10935/10935-14.cpp @@ -0,0 +1,85 @@ +/*************************************************** + * Problem name : 10935 Throwing cards away I.cpp + * Problem Link : https://uva.onlinejudge.org/external/109/10935.pdf + * OJ : Uva + * Verdict : AC + * Date : 03.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 51 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +int main () { + int N; + vectorcard, ans; + while (scanf("%d", &N) == 1 && N != 0) { + if (N == 1) { + printf("Discarded cards:\n"); + printf("Remaining card: %d\n", N); + continue; + } + for (int i = 1; i <= N; i++) { + card.push_back(i); + } + for (int i = 0; i < N - 1; i++) { + ans.push_back(card[0]); + card.erase(card.begin() + 0); + int x = card[0]; + card.erase(card.begin() + 0); + card.push_back(x); + } + int x = card[0]; + int sz = ans.size(); + printf("Discarded cards:"); + for (int i = 0; i < sz; i++) { + if (i == sz - 1) printf(" %d\n", ans[i]); + else printf(" %d,", ans[i]); + } + printf("Remaining card: %d\n", x); + ans.clear(); + card.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/10935/10935-17.cpp b/uva_cpp_clean/10935/10935-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59cd202ef3e195e9abfdbf3ce882c5c70c711700 --- /dev/null +++ b/uva_cpp_clean/10935/10935-17.cpp @@ -0,0 +1,33 @@ +/** + * AOAPC II Exercise 5-3 Throwing Cards Away I + * 10 RE 15 AC // Boundaries! + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n; + while ((cin >> n) && n) + { + queue cards; + for (int i = 1; i <= n; ++i) + cards.push(i); + cout << "Discarded cards:"; + bool first = true; + while (cards.size() >= 2) + { + if (first) + first = false; + else + cout << ","; + cout << " " << cards.front(); + cards.pop(); + cards.push(cards.front()); + cards.pop(); + } + cout << "\n" + << "Remaining card: " << cards.front() << "\n"; + } +} diff --git a/uva_cpp_clean/10935/10935-18.cpp b/uva_cpp_clean/10935/10935-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c1b823c91d067f891203e3215d0ad14861fe2344 --- /dev/null +++ b/uva_cpp_clean/10935/10935-18.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; + +int main(){ + int n, arr[55], aux; + while(scanf("%d", &n) && n != 0){ + aux = n; + bool space = true; + for(int i = 0; i < n; i++) arr[i] = aux, aux--; + printf("Discarded cards:"); + while(n > 1){ + if(space) printf(" "), space = false; + printf("%d", arr[n - 1]); + if(n > 2) printf(", "); + n--; + for(int i = n - 1; i - 1 >= 0; i--) swap(arr[i], arr[i - 1]); + } + printf("\nRemaining card: %d\n", arr[0]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10935/10935-19.cpp b/uva_cpp_clean/10935/10935-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20a591ee06db97ba130e7b243bc6351d43d0de34 --- /dev/null +++ b/uva_cpp_clean/10935/10935-19.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; + +int main() { + queue q; + int n; + while (scanf("%d", &n) && n != 0) { + for (int i = 1; i <= n; i++) q.push(i); + printf("Discarded cards:"); + bool first = false; + while (q.size() > 1) { + if (first) printf(", "); + else first = true, printf(" "); + printf("%d", q.front()); + q.pop(); + q.push(q.front()); q.pop(); + } + printf("\nRemaining card: %d\n", q.front()); + q.pop(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10935/10935-21.cpp b/uva_cpp_clean/10935/10935-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e7ed726cbdc930fe45215af39bfe3b6b3d3c0f8 --- /dev/null +++ b/uva_cpp_clean/10935/10935-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10935 + Name: Throwing cards away I + Problem: https://onlinejudge.org/external/109/10935.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n, ar[50]; + while (cin>>n && n) { + cout << "Discarded cards:"; + + for (int i=0; i +#include +using namespace std; + +int main(){ +int n; +queue cola; +while(cin>>n && n!=0){ + if(n == 1){ + cout << "Discarded cards:" << endl << "Remaining card: 1" << endl; + } + else{ + + while(!cola.empty()){ + cola.pop(); + } + //Llenado de la cola 1,2,3,4...,n + for(int i = 1; i<=n; i++){ + cola.push(i); + } + int mover; + cout << "Discarded cards: "; + for(int i = 2; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 5005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +int level[MAX], par[MAX], dis[MAX]; +int table[MAX][15]; + +void dfs (int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + level[v] = level[u] + 1; + par[v] = u; + dis[v] = dis[u] + 1; + dfs (v, u); + } +} + +void LCA (int n) { + SET (table, -1); + + for (int i = 1; i <= n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 1; i <= n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + } + } + } +} + +int LCA_Quary (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} +int main () { + __FastIO; + int n; + + while (cin >> n) { + if (n == 0) break; + + for (int i = 0; i < n - 1; i++) { + int u, v; + cin >> u >> v; + adj[u].pb (v); + adj[v].pb (u); + } + + SET (level, 0); + SET (par, 0); + SET (dis, 0); + par[1] = -1; + dfs (1, -1); + LCA (n); + int L; + cin >> L; + + for (int q = 1; q <= L; q++) { + int u, v; + cin >> u >> v; + int x = LCA_Quary (u, v); + int ans = (dis[u] + dis[v]) - 2 * dis[x]; + + if (ans % 2 == 1) { + x = ans / 2; + + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (x); + + for (int i = lim; i >= 0; i--) { + if ( (1 << i) <= x) { + u = table[u][i]; + x -= (1 << i); + } + + if (x == 0) break; + } + + cout << "The fleas jump forever between " << par[u] << " and " << u << ".\n"; + + } else { + x = ans / 2; + + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (x); + + for (int i = lim; i >= 0; i--) { + if ( (1 << i) <= x) { + u = table[u][i]; + x -= (1 << i); + } + + if (x == 0) break; + } + + cout << "The fleas meet at " << u << ".\n"; + } + } + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + } + } + + return 0; +} diff --git a/uva_cpp_clean/10938/10938-9.cpp b/uva_cpp_clean/10938/10938-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..085c61adfceaa53ca91ffe4f6e0781b82b7251c9 --- /dev/null +++ b/uva_cpp_clean/10938/10938-9.cpp @@ -0,0 +1,90 @@ +#include + +#define pb(A) push_back(A) + +using namespace std; + +const int MAX_V = 5e3 + 10, LG = 15; + +int V, H[MAX_V], ANC[LG][MAX_V]; +vector G[MAX_V]; + +void dfs (int u, int p = -1) { + for (int v: G[u]) { + if (v != p) { + H[v] = H[u] + 1; + ANC[0][v] = u; + dfs(v, u); + } + } +} + +void buildTable () { + for (int bit = 1; bit < LG; bit++) { + for (int u = 1; u <= V; u++) { + int v = ANC[bit - 1][u]; + if (~v) ANC[bit][u] = ANC[bit - 1][v]; + } + } +} + +int walk (int u, int k) { + for (int bit = LG - 1; bit >= 0; bit--) + if ((k >> bit) bitand 1) u = ANC[bit][u]; + return u; +} + +int getLCA (int u, int v) { + if (H[u] != H[v]) u = walk(u, H[u] - H[v]); + if (u == v) return u; + for (int bit = LG - 1; bit >= 0; bit--) + if (~ANC[bit][u] and ANC[bit][u] != ANC[bit][v]) + u = ANC[bit][u], v = ANC[bit][v]; + return ANC[0][u]; +} + +void processQueries () { + int Q; + scanf("%d", &Q); + while (Q--) { + int u, v; + scanf("%d %d", &u, &v); + if (H[u] < H[v]) swap(u, v); + int lca = getLCA(u, v); + int d = H[u] + H[v] - 2 * H[lca]; + if (d & 1) { + int ans1 = walk(u, d / 2); + int ans2 = ANC[0][ans1]; + if (ans1 > ans2) swap(ans1, ans2); + printf("The fleas jump forever between %d and %d.\n", ans1, ans2); + } + else { + printf("The fleas meet at %d.\n", walk(u, d / 2)); + } + } +} + +void readTree () { + int u, v; + for (int i = 1; i < V; i++) { + scanf("%d %d", &u, &v); + G[u].pb(v); + G[v].pb(u); + } +} + +void clear () { + for (int bit = 0; bit < LG; bit++) for (int v = 1; v <= V; v++) ANC[bit][v] = -1; + for (int v = 1; v <= V; v++) G[v].clear(); +} + +int main () { + while (scanf("%d", &V), V) { + clear(); + readTree(); + dfs(1); + buildTable(); + processQueries(); + } + return (0); +} diff --git a/uva_cpp_clean/10940/10940-11.cpp b/uva_cpp_clean/10940/10940-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa59cda08f767bd72e88beeb718b772585962898 --- /dev/null +++ b/uva_cpp_clean/10940/10940-11.cpp @@ -0,0 +1,55 @@ +#include +/* +Problem: 10940 - Throwing cards away II +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1881 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N; + +int main () { + + while ( scanf("%d\n", &N) && N ){ + + int k = (int)log2(N); //MSB pos + int ans = (N%(1< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int solve (int n) { + int nn = log2 (n); + int num = n - (1 << nn); + + if (num == 0) return n; + else return num * 2; +} + +int main () { + __FastIO; + int n; + + while (cin >> n) { + if (n == 0) break; + + cout << solve (n) << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/10940/10940-19.cpp b/uva_cpp_clean/10940/10940-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42de3c42c461ad92e4808b742f05d5941a2c1b64 --- /dev/null +++ b/uva_cpp_clean/10940/10940-19.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main() { + int n; + while (scanf("%d", &n) && n != 0) { + if (n == 1) printf("1\n"); + else if (n == 2) printf("2\n"); + else { + int l = floor(log2(n)); + int sol = (n & ~(1< +#include + +using namespace std; + +int last_card[500005]; + +int main() +{ + last_card[0] = 0; + last_card[1] = 1; + last_card[2] = 2; + + int card = 2; + for(int i=3 ; i<500005 ; i++) + { + if( i> n && n ) + cout << last_card[n] << endl; + + return 0; +} diff --git a/uva_cpp_clean/10940/10940-21.cpp b/uva_cpp_clean/10940/10940-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3c56ceddf4350483f3c068be7ab73b3d1e104a9 --- /dev/null +++ b/uva_cpp_clean/10940/10940-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10940 + Name: Throwing cards away II + Problem: https://onlinejudge.org/external/109/10940.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int f(int n) { + if (n==1) return 1; + + int t = (f(n>>1) + (n&1)) << 1; + return ((t-1)%(n&(~1))) + 1; +} + +int main() { + int n; + while (cin>>n && n) + cout << f(n) << endl; +} diff --git a/uva_cpp_clean/10940/10940-9.cpp b/uva_cpp_clean/10940/10940-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19b489455fca70cf71c5c379fd2b1207df416b36 --- /dev/null +++ b/uva_cpp_clean/10940/10940-9.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int n; + +int main(){ + while(scanf("%d", &n), n){ + if(n <= 2){ + printf("%d\n", n); + continue; + } + for(int p = 0; ; p++) + if( (1 << p) >= n){ + printf("%d\n", 2 * (n - ( 1<<(p - 1) ))); + goto jump; + } + jump: ; + } + return(0); +} diff --git a/uva_cpp_clean/10943/10943-13.cpp b/uva_cpp_clean/10943/10943-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d056d665a50f2170a380f2d02420716890f70a5e --- /dev/null +++ b/uva_cpp_clean/10943/10943-13.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 105 +#define MOD 1000000 + +int dp[MAX][MAX]; + +int ways(int n, int k) +{ + if (k == 1) + { + return 1; + } + + if (n == 0) + { + return 1; + } + + if (dp[n][k] != 0) + { + return dp[n][k]; + } + + for (int i = 0; i <= n; i++) + { + dp[n][k] += ways(n - i, k - 1); + + dp[n][k] = dp[n][k] % MOD; + } + + return dp[n][k]; +} + +int main() +{ + int n, k; + + cin >> n >> k; + + while (n != 0 && k != 0) + { + memset(dp, 0, sizeof dp); + + cout << ways(n, k) << endl; + + cin >> n >> k; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10943/10943-19.cpp b/uva_cpp_clean/10943/10943-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24fc0344e0482553d1c840811899308aef76dff7 --- /dev/null +++ b/uva_cpp_clean/10943/10943-19.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int main() { + int grid[101][101]; + for (int i = 0; i < 101; i++) grid[i][0] = grid[0][i] = 1; + for (int i = 1; i < 101; i++) + for (int j = 1; j < 101; j++) + grid[i][j] = (grid[i-1][j] + grid[i][j-1]) % 1000000; + int N, K; + while (cin >> N >> K && !(N == 0 && K == 0)) { + cout << grid[K-1][N] << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10943/10943-5.cpp b/uva_cpp_clean/10943/10943-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..03afa1ef7bc9ed14a8c7561d9be1dd90cf467f5b --- /dev/null +++ b/uva_cpp_clean/10943/10943-5.cpp @@ -0,0 +1,189 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%I64d",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%I64d%I64d",&a,&b) + +#define sfi(t) scanf("%d",&t) +#define sfii(a,b) scanf("%d %d",&a,&b) +#define sfiii(a,b,c) scanf("%d %d %d",&a,&b,&c) +#define sfll(t) scanf("%I64d",&t) +#define sfllll(a,b) scanf("%I64d %I64d",&a,&b) +#define sfllllll(a,b,c) scanf("%I64d %I64d %I64d",&a,&b,&c) +#define sfd(t) scanf("%lf",&t) +#define sfc(c) scanf("%c",&c) +#define sfs(s) scanf("%s",s) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%I64d\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%I64d ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%I64d %I64d\n",a,b) +#define PPLN(a,b) printf("%I64d %I64d ",a,b) + +#define pfi(a) printf("%d\n",a) +#define pfii(a,b) printf("%d %d\n",a,b) +#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c) +#define pfll(a) printf("%I64d\n",a) +#define pfllll(a,b) printf("%I64d %I64d\n",a,b) +#define pfllllll(a,b,c) printf("%I64d %I64d %I64d\n",a,b,c) +#define pfd(a) printf("%lf\n",a) +#define pfs(a) printf("%s\n",a) +#define pfis(a) printf("%d ",a) +#define pflls(a) printf("%I64d ",a) +#define pfds(a) printf("%lf ",a) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "< +#include +using namespace std; + +int memo[110][110]; +int dp(int n, int k) { + if (k == 1) return 1; + + int &ans = memo[n][k]; + if (ans != 0) return ans; + + for (int i = 0; i <= n; i++) + ans = (ans + dp(n-i, k-1)) % (int)1e6; + return ans; +} + +int main() { + memset(memo, 0, sizeof memo); + + while (true) { + int N, K; cin >> N >> K; + if (!N && !K) return 0; + cout << dp(N, K) << endl; + } +} diff --git a/uva_cpp_clean/10943/10943-9.cpp b/uva_cpp_clean/10943/10943-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e385c3b7e4413d3904d5d51d0d963f894c165a08 --- /dev/null +++ b/uva_cpp_clean/10943/10943-9.cpp @@ -0,0 +1,23 @@ +#include + +#define MAX_N 110 +#define MAX_K 110 +const int MOD = 1000000; +int N,K,memo[MAX_N][MAX_K]; + +using namespace std; + +int ways(int n,int k){ + if(k==1) return 1; + if(~memo[n][k]) return memo[n][k]; + int ans = 0; + for(int it=0;it<=n;it++) + ans = (ans+ways(n-it,k-1))%MOD; + return memo[n][k] = ans; +} + +int main(){ + memset(memo,-1,sizeof memo); + while(scanf("%d %d",&N,&K),N|K) printf("%d\n",ways(N,K)); + return(0); +} diff --git a/uva_cpp_clean/10945/10945-18.cpp b/uva_cpp_clean/10945/10945-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9fd5adf9a73ddc17d6b44de9a149af2d0d1d7001 --- /dev/null +++ b/uva_cpp_clean/10945/10945-18.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +int main(){ + char word[550], inverse[550]; + while(gets(word) && strcmp("DONE", word) != 0){ + int index = 0, aIndex = 0; + for(int i = 0; word[i]; i++){ + if(word[i]>='a' && word[i] <='z'){ + word[index] = word[i]; + index++; + } + if(word[i]>='A' && word[i]<='Z'){ + word[index] = word[i] + 32; + index++; + } + } + word[index] = '\0'; + for(int i = index - 1; i>= 0; i--){ + inverse[aIndex] = word[i]; + aIndex++; + } + inverse[aIndex] = '\0'; + if(strcmp(word, inverse) == 0) printf("You won't be eaten!\n"); + else printf("Uh oh..\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10945/10945-19.cpp b/uva_cpp_clean/10945/10945-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36824ff0f8ab7e41a6b3de72876eb3f83b6ee70f --- /dev/null +++ b/uva_cpp_clean/10945/10945-19.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +using namespace std; + +bool isnotalpha(char c) { + return !isalpha(c); +} + +void proc(string & word, bool & pal) { + for (int i = 0, j = word.length() - 1; i <= j; i++, j--) { + if (word[i] != word[j]) pal = false; + } +} + +int main() { + string word; + while (getline(cin, word) && word != "DONE") { + bool pal = true; + word.erase(remove_if(word.begin(), word.end(), isnotalpha), word.end()); + transform(word.begin(), word.end(), word.begin(), ::tolower); + proc(word, pal); + if(pal) cout << "You won't be eaten!"; + else cout << "Uh oh.."; + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10945/10945-21.cpp b/uva_cpp_clean/10945/10945-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ccc11ed3f01e7e717a01e832896ccd623b97103b --- /dev/null +++ b/uva_cpp_clean/10945/10945-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10945 + Name: Mother bear + Problem: https://onlinejudge.org/external/109/10945.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char line[1000000]; +char toUpper(char ch) { + if (ch>='a') return ch - 'a' + 'A'; + return ch; +} + + +bool check(int l) { + for (int i=l/2; i>=0; i--) + if (toUpper(line[i]) != toUpper(line[l-i-1])) + return false; + + return true; +} + +int main(){ + char ch; + while (true) { + int i = 0; + bool o = false; + while ((ch=getchar()) != '\n') + if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) + line[i++] = ch; + else o = true; + + line[i] = 0; + if ((!o && strcmp(line, "DONE")==0)) return 0; + + if (check(i)) cout << "You won't be eaten!\n"; + else cout << "Uh oh..\n"; + } +} + diff --git a/uva_cpp_clean/10945/10945-5.cpp b/uva_cpp_clean/10945/10945-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..84ee8f2204abce4ecc329cc718639e3c494f244a --- /dev/null +++ b/uva_cpp_clean/10945/10945-5.cpp @@ -0,0 +1,80 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; + +int main() +{ + string s,a; + while(getline(cin,s)) + { + a=""; + if(s.compare("DONE")==0) + break; + int l=s.size(); + for(int i=0;i + +#define SIZE 1010 + +using namespace std; + +char s[SIZE], ch; +int len; + +bool is_palindrome(){ + for(int i = 0, j = len - 1; i < j; i++, j--) + if( tolower(s[i]) != tolower(s[j]) ) return false; + return true; +} + +int main(){ + while(true){ + len = 0; + while( ( ch = getchar() ) != '\n' ) + if( isalpha(ch) ) + s[len++] = ch; + s[len] = '\0'; + if( strcmp(s, "DONE") == 0 ) break; + puts( is_palindrome() ? "You won't be eaten!" : "Uh oh.."); + } + return(0); +} diff --git a/uva_cpp_clean/10946/10946-21.cpp b/uva_cpp_clean/10946/10946-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..783141c019b7f1a22963f6abf94b95bb017276c8 --- /dev/null +++ b/uva_cpp_clean/10946/10946-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10946 + Name: You want what filled? + Problem: https://onlinejudge.org/external/109/10946.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +typedef pair Pair; +bool comp(const Pair &p1, const Pair &p2) { + return p1.first != p2.first + ? p1.first > p2.first + : p1.second < p2.second; +} + +int n, m; +char M[100][100]; +int dfs(int y, int x, char ch) { + if (M[y][x] != ch) return 0; + M[y][x] = '.'; + + int result = 1; + if (x>0) result += dfs(y, x-1, ch); + if (x0) result += dfs(y-1, x, ch); + if (y>n>>m && (n||m)) { + for (int i=0; i> M[i]; + + vector vec; + for (int i=0; i +using namespace std; +using ii=tuple; +using vii=vector; +using vb=vector; +using vvb=vector; +using vs=vector; +int main(){ + for(int T=1;;T++){ + int n,m; + cin>>n>>m; + if(!n)break; + vs a(n); + for(int i=0;i>a[i]; + vvb s(n,vb(m)); + functiondfs=[&](int y,int x){ + s[y][x]=1; + int r[]={-1,0,1,0}; + int c[]={0,1,0,-1}; + int k=1; + for(int l=0;l<4;l++){ + int u=y+r[l],v=x+c[l]; + if(u>=0&&u=0&&v + +#define SIZE 60 + +using namespace std; + +struct Point{ + char ch; + int ct; + Point(char _ch, int _ct): + ch(_ch), ct(_ct) {} + bool operator < ( const Point& other) const{ + if( ct != other.ct ) return ( ct > other.ct ); + return ( ch < other.ch ); + } +}; + +int tc, N, M; +char grid[SIZE][SIZE], ch; +vector < Point > ans; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M ); +} + +int floodfill(int r, int c){ + if( not is_possible(r, c) ) return 0; + if( grid[r][c] != ch ) return 0; + grid[r][c] = '.'; + int ct = 1; + for(int d = 0; d < 4; d++) + ct += floodfill(r + dr[d], c + dc[d]); + return ct; +} + +int main(){ + while(scanf("%d %d\n", &N, &M), N | M){ + ans.clear(); + for(int row = 0; row < N; row++) scanf("%s", grid[row]); + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col++) + if( grid[row][col] != '.' ){ + ch = grid[row][col]; + ans.push_back( Point( ch, floodfill(row, col)) ); + } + sort(ans.begin(), ans.end()); + printf("Problem %d:\n", ++tc); + for(int it = 0; it < ans.size(); it++) + printf("%c %d\n", ans[it].ch, ans[it].ct); + } + return(0); +} diff --git a/uva_cpp_clean/10947/10947-21.cpp b/uva_cpp_clean/10947/10947-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4452fc981bd98177cabb1528dc33f764930da7bc --- /dev/null +++ b/uva_cpp_clean/10947/10947-21.cpp @@ -0,0 +1,112 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10947 + Name: Bear with me, again.. + Problem: https://onlinejudge.org/external/109/10947.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ----------------------------------------------------------------------------- + +struct Island { + int x, y, r; + bool operator < (const Island &o) const { return x-r < o.x-o.r; } + bool operator ==(const Island &o) const { return x==o.x && y==o.y && r==o.r; } +} P[143], srcI, dstI; + + +void readIsland(Island &I) { + I.x = readUInt(); + I.y = readUInt(); + I.r = readUInt(); +} + +vector adj[143]; +int src, dst, seen[143]; +bool dfs(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + if (v == dst || dfs(v)) + return 1; + } + return 0; +} + +int main() { + ios_base::sync_with_stdio(0); + + int k; + while ((k=readUInt())!=EOF) { + int t = readUInt(); + int maxd = k * t, + mxd2 = maxd*maxd; + + readIsland(srcI); P[0] = srcI; + readIsland(dstI); P[1] = dstI; + int n = readUInt(); + for (int i=0; i +#include + +const int S = 1000005; +bool prime[S]; +int N, p1, p2; + +int main() +{ + prime[0] = prime[1] = false; + for(int i=2 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10948 + Name: The primary problem + Problem: https://onlinejudge.org/external/109/10948.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +#define MAXP 1000009 + +int cnt = 1; +int primes[670000] = {2}; + +unsigned int prime[MAXP / 64]; + +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) +void sieve() { + memset(prime, -1, sizeof(prime)); + + int i, sqrtN = sqrt((double)MAXP) + 1; + for (i = 3; i < sqrtN; i += 2 ) if(gP(i)) { + primes[cnt++] = i; + int i2 = i<<1; + for(int j = i * i; j < MAXP; j += i2) + prime[j>>6] &= ~(1<<((j>>1)&31)); + } + + for (; i2 && x&1 && (gP(x))); +} + + +int main(){ + sieve(); + int n, i; + while (cin>>n && n) { + for (i=0; primes[i]*2 <= n; i++) + if (isPrime(n - primes[i])) + break; + + printf("%d:\n", n); + if (2*primes[i] > n) puts("NO WAY!"); + else printf("%d+%d\n", primes[i], n-primes[i]); + } +} diff --git a/uva_cpp_clean/10948/10948-5.cpp b/uva_cpp_clean/10948/10948-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..04cf44e152152c922a5688ad40785131c15c2221 --- /dev/null +++ b/uva_cpp_clean/10948/10948-5.cpp @@ -0,0 +1,94 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define st(s) s.size(); +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +using namespace std; + +bool prime[10000010]; +void sieve() +{ + prime[1]=false; + int n=sqrt(10000000); + for(int i=2;i<=n;i++) + { + if(prime[i]!=false) + { + for(int j=2*i;j<=10000000;j=j+i) + { + prime[j]=false; + } + } + } +} +int main() +{ + cover(prime,true); + sieve(); + int n; + while(sc("%d",&n)==1) + { + if(n==0) break; + int a,i; + int f=0; + for(i=2;i<=n;i++) + { + if(prime[i]) + { + a=n-i; + if(prime[a]){ + f=1; + break; + } + } + } + if(f==0||a==0) + { + pf("%d:\nNO WAY!\n",n); + } + else + pf("%d:\n%d+%d\n",n,i,a); + + } + return 0; +} + diff --git a/uva_cpp_clean/10948/10948-8.cpp b/uva_cpp_clean/10948/10948-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26ebdfb7a3d89a6c17dd16a24d18d9724a5b817b --- /dev/null +++ b/uva_cpp_clean/10948/10948-8.cpp @@ -0,0 +1,47 @@ +// Problem name: The primary problem +// Problem link: https://vjudge.net/problem/UVA-10948 +// Submission link: https://vjudge.net/solution/32960353 + +#include + +using namespace std; + +typedef long long ll; + +vector get_primes(int n){ + vector small_prime(n + 1), primes; + for(int i = 2 ; i <= n ; ++i){ + if(small_prime[ i ] == 0) + small_prime[ i ] = i, primes.push_back(i); + for(int j = 0 ; j < primes.size() && primes[ j ] <= small_prime[ i ] && i * primes[ j ] <= n ; ++j) + small_prime[ i * primes[ j ] ] = primes[ j ]; + } + return primes; +} + +pair get_goldbach(int n, const vector &primes){ + for(auto left = primes.begin(), right = primes.end() - 1 ; left <= right ; ++left){ + while(right > left && *left + *right > n) + --right; + if(*left + *right == n) + return { *left, *right }; + } + return { -1, -1 }; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int N = 1000000; + const vector primes = get_primes(N); + + for(int n ; cin >> n && n ; ){ + pair ans = n < 4 || (n & 1 && *lower_bound(primes.begin(), primes.end(), n - 2 ) != n - 2) ? make_pair(-1, -1) : (n & 1 ? make_pair(2, n - 2) : get_goldbach(n, primes)); + cout << n << ':' << endl; + if(ans.first == -1) + cout << "NO WAY!" << endl; + else + cout << ans.first << '+' << ans.second << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10948/10948-9.cpp b/uva_cpp_clean/10948/10948-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5d546f88ddfe216ae61cb4fcc80387688dd9b81 --- /dev/null +++ b/uva_cpp_clean/10948/10948-9.cpp @@ -0,0 +1,37 @@ +#include + +#define SIZE 1000000 + +using namespace std; + +bool is_prime[SIZE], found; +vectorprime; +int N, a, b; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++){ + if(is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } + } +} + +int main(){ + sieve(); + while(scanf("%d",&N), N){ + found = false; + for(int it = 0; it < prime.size() and prime[it] < N; it++) + if( is_prime[ N - prime[it] ] ){ + found = true; + a = prime[it], b = N - prime[it]; + break; + } + if(not found) printf("%d:\nNO WAY!\n",N); + else printf("%d:\n%d+%d\n",N,a,b); + } + return(0); +} diff --git a/uva_cpp_clean/10950/10950-21.cpp b/uva_cpp_clean/10950/10950-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05b3a1328cad25463dce4e0c3e50030f6b3da330 --- /dev/null +++ b/uva_cpp_clean/10950/10950-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10950 + Name: Bad Code + Problem: https://onlinejudge.org/external/109/10950.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int cnt, n; +char Ch[43], X[128][7], S[143], P[143]; + +void bt(int si, int di) { + if (cnt == 100) return; + if (!S[si]) { + ++cnt; + P[di] = 0; + cout << P << '\n'; + return; + } + + while (S[si] == '0') ++si; + if (!S[si]) return; + + for (int i=0, j; i> n && n; ++cse) { + cout << "Case #" << cse << '\n'; + + for (int i=0, x; i> Ch[i] >> x; + sprintf(X[Ch[i]], "%d", x); + } + sort(Ch, Ch+n); + + cin >> S; + cnt = 0; + bt(0, 0); + cout << '\n'; + } +} diff --git a/uva_cpp_clean/10954/10954-11.cpp b/uva_cpp_clean/10954/10954-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c6b539be8e393ba66f39d2c28938714b01a220b --- /dev/null +++ b/uva_cpp_clean/10954/10954-11.cpp @@ -0,0 +1,60 @@ +#include +/* +Problem: 10954 - Add All +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1895 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int N; +priority_queue numbers; + +int main () { + + while ( scanf("%d\n", &N) && N != 0){ + int cost = 0, number, n1, n2; + + while (N--){ + scanf("%d ", &number); + numbers.push(-number); + } + + while (numbers.size() > 1){ + n1 = -numbers.top(); numbers.pop(); + n2 = -numbers.top(); numbers.pop(); + numbers.push(-(n1+n2)); + cost += (n1+n2); + } + + numbers.pop();// Just to clear the queue for the next testcase + + printf("%d\n", cost); + } + + return 0; +} +/* +input:- +----------- +3 +1 2 3 +4 +1 2 3 4 +0 + +output:- +----------- +9 +19 + +Resources:- +------------- +http://www.cplusplus.com/reference/queue/priority_queue/ +https://stackoverflow.com/questions/2852140/priority-queue-clear-method +*/ diff --git a/uva_cpp_clean/10954/10954-19.cpp b/uva_cpp_clean/10954/10954-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68a7ff87984b49111348ca42201ff46ed1cddfb0 --- /dev/null +++ b/uva_cpp_clean/10954/10954-19.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int main() { + priority_queue pq; + int N; + while (scanf("%d", &N) && N != 0) { + while (N--) { + long long m; + scanf("%lld", &m); + pq.push(-m); + } + long long cost = 0; + while (pq.size() > 1) { + long long sum = pq.top(); pq.pop(); + sum += pq.top(); pq.pop(); + cost -= sum; + pq.push(sum); + } + pq.pop(); + printf("%lld\n", cost); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10954/10954-20.cpp b/uva_cpp_clean/10954/10954-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ee56dd57d67f0707390a17aa04f3a36f8d509c6 --- /dev/null +++ b/uva_cpp_clean/10954/10954-20.cpp @@ -0,0 +1,33 @@ +#include +#include + +using namespace std; + +long num[5005]; + +int main() +{ + int N; + + while( scanf("%d", &N)==1 && N ) + { + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10954 + Name: Add All + Problem: https://onlinejudge.org/external/109/10954.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +int main(){ + int n, x; + while (cin>>n && n) { + priority_queue, greater > nums; + + for (int i=0; i>x; + nums.push(x); + } + + long long int res=0, sum; + while (nums.size() > 1) { + sum = nums.top(); nums.pop(); + sum += nums.top(); nums.pop(); + nums.push(sum); + res += sum; + } + + cout< +#include + +using namespace std; + +// The reason why I chose priority_queue vs simple queue is the fact that priority queue remains +// after every insertion. +// Syntax here: type, container_type, comparisonFunction +priority_queue, greater> input; + +int main() +{ + int N, + runningSum, + inputInt, + tempInt; + + while ( scanf("%d",&N), N ) + { + runningSum = 0; + while ( !input.empty() ) + input.pop(); + + for ( int l = 0; l < N; l++ ) + { + scanf("%d",&inputInt); + input.push(inputInt); + } + + while ( input.size() > 1 ) + { + tempInt = input.top(); + input.pop(); + tempInt += input.top(); + input.pop(); + runningSum += tempInt; + input.push(tempInt); + } + + printf("%d\n", runningSum ); + } +} diff --git a/uva_cpp_clean/10954/10954-5.cpp b/uva_cpp_clean/10954/10954-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..929fd2780eca5bbde77c464efed6508e14d97bae --- /dev/null +++ b/uva_cpp_clean/10954/10954-5.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main () +{ + int n,a; + while (sc("%d",&n)) + { + if(n==0) + break; + priority_queuepq; + int sum=0,i,c=0; + for (i=0;i +using namespace std; +typedef long long ll; + +int main() { + int N; + while (cin >> N && N) { + priority_queue q; + while (N--) { int a; cin >> a; q.push(-a); }; + + ll cost = 0, sum; + while (q.size() > 1) { + sum = -q.top(); q.pop(); + sum += -q.top(); q.pop(); + q.push(-sum); + + cost += sum; + } + cout << cost << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10954/10954-9.cpp b/uva_cpp_clean/10954/10954-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90b1d6f04ed990ae5aaf886231f6e7d54f29b184 --- /dev/null +++ b/uva_cpp_clean/10954/10954-9.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int n, num, sum, a, b; + +int main(){ + while(scanf("%d",&n), n){ + priority_queue v; + sum = 0; + for(int i = 0; i < n; i++) scanf("%d", &num), v.push(-num); + for(int i = 1;i < n; i++){ + a = v.top(), v.pop(); + b = v.top(), v.pop(); + sum -= a + b; + v.push(a + b); + } + printf("%d\n", sum); + } + return(0); +} diff --git a/uva_cpp_clean/10957/10957-21.cpp b/uva_cpp_clean/10957/10957-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b35a19515e483e5eb667b46a2ff4b510125fcd8 --- /dev/null +++ b/uva_cpp_clean/10957/10957-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10957 + Name: So Doku Checker + Problem: https://onlinejudge.org/external/109/10957.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char msg[4][17] = { + "Impossible.\n", + "Unique.\n", + "Ambiguous.\n", + "Illegal.\n" +}; + + +int X[9][9]; +bool R[9][9], C[9][9], S[3][3][9]; +int backtrack(int i, int j) { + if (j == 9) ++i, j=0; + if (i == 9) return 1; + if (X[i][j]) return backtrack(i, j+1); + + int cc = 0; + int si = i/3, sj = j/3; + for (int k=0; k<9; ++k) + if (!R[i][k] && !C[j][k] && !S[si][sj][k]) { + R[i][k] = 1; + C[j][k] = 1; + S[si][sj][k] = 1; + + int rr = backtrack(i, j+1); + if (rr > 1) return rr; + if (rr > 0) ++cc; + if (cc > 1) return cc; + + R[i][k] = 0; + C[j][k] = 0; + S[si][sj][k] = 0; + } + + return cc; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; ; ++cse) { + for (int i=0; i<9; ++i) + for (int j=0; j<9; ++j) + if (!(cin >> X[i][j])) + return 0; + memset(R, 0, sizeof(R)); + memset(C, 0, sizeof(C)); + memset(S, 0, sizeof(S)); + + int result = 0; + for (int i=0; i<9; ++i) + for (int j=0; j<9; ++j) { + int x = X[i][j] - 1; + if (x < 0) continue; + if (R[i][x]++ || C[j][x]++ || S[i/3][j/3][x]++) + result = 3; + } + + if (result < 3) result = backtrack(0, 0); + cout << "Case " << cse << ": " << msg[result]; + } +} diff --git a/uva_cpp_clean/10957/10957-9.cpp b/uva_cpp_clean/10957/10957-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4581b92b1a06e313100b84aaeda25d26541730a --- /dev/null +++ b/uva_cpp_clean/10957/10957-9.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +const int MAX_N_N = 10; + +int tc, n, n_n, filled, ones, solutions, mat[MAX_N_N][MAX_N_N], row[MAX_N_N], col[MAX_N_N], sq[MAX_N_N]; +bool valid; + +void backtrack(int r, int c, int res) { + if (res == 0) { + solutions++; + return; + } + if (r == n_n or solutions >= 2) return; + int p = (r / n) * n + c / n; + int pos = ones bitand (compl (row[r] bitor col[c] bitor sq[p])); + if (mat[r][c] == 0) { + while (pos and solutions < 2) { + int bin_num = pos bitand -pos; + row[r] |= bin_num, col[c] |= bin_num, sq[p] |= bin_num; + backtrack(c + 1 == n_n ? r + 1 : r, c + 1 == n_n ? 0 : c + 1, res - 1); + row[r] ^= bin_num; col[c] ^= bin_num, sq[p] ^= bin_num; + pos -= bin_num; + } + } + else backtrack(c + 1 == n_n ? r + 1 : r, c + 1 == n_n ? 0 : c + 1, res); +} + +void solve() { + if (not valid) { + printf("Case %d: Illegal.\n", ++tc); + return; + } + backtrack(0, 0, n_n * n_n - filled); + if (solutions == 0) printf("Case %d: Impossible.\n", ++tc); + else if (solutions == 1) printf("Case %d: Unique.\n", ++tc); + else printf("Case %d: Ambiguous.\n", ++tc); +} + +void check(int r, int c) { + if (mat[r][c] == 0) return; + int pib = (1 << mat[r][c]), p = (r / n) * n + c / n; + if ( (row[r] bitand pib) != 0 or + (col[c] bitand pib) != 0 or + (sq[p] bitand pib) != 0 ) valid = false; + row[r] |= pib; + col[c] |= pib; + sq[p] |= pib; + filled++; +} + +void read() { + for (int i = 0; i < n_n; i++) row[i] = col[i] = sq[i] = 1; + check(0, 0); + for (int i, j, p, pib, k = 1; k < n_n * n_n; k++) { + i = k / n_n, j = k % n_n; + scanf("%d", &mat[i][j]); + check(i, j); + } +} + +int main() { + n = 3, n_n = 9, ones = (1 << (n_n + 1)) - 1; + while (scanf("%d", &mat[0][0]) == 1) { + filled = solutions = 0; + valid = true; + read(); + solve(); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/10959/10959-14.cpp b/uva_cpp_clean/10959/10959-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..18a137d14a27ef47c1a947bf5d3a4ca97e754bdf --- /dev/null +++ b/uva_cpp_clean/10959/10959-14.cpp @@ -0,0 +1,77 @@ +/*************************************************** + * Problem name : 10959 - The Party, Part I(DFS).cpp + * OJ : Uva + * Result : AC + * Date : 18-04-17 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 1005 +using namespace std; +typedef long long ll; +vectoradj[MAX]; +int cost[MAX],flag; +void dfs(int u) { + if(flag == 0){ + cost[u] = 0; + flag = 1; + } + int sz = adj[u].size(); + for (int i = 0; i < sz; i++) { + int v = adj[u][i]; + if (cost[u] + 1 < cost[v]) { + cost[v] = cost[u] + 1; + dfs(v); + } + } + return ; +} +void mem() { + for (int i = 0; i < MAX; i++) { + cost[i] = 100000; + } +} +void g_mem() { + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + } +} +int main () { + int P, D; + int tc; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d %d", &P, &D); + for (int i = 1; i <= D; i++) { + int u, v; + scanf("%d %d", &u, &v); + adj[u].push_back(v); + adj[v].push_back(u); + } + mem(); + flag = 0; + dfs(0); + for (int i = 1; i < P; i++) { + printf("%d\n", cost[i]); + } + if (t != tc) { + printf("\n"); + } + g_mem(); + } + return 0; +} diff --git a/uva_cpp_clean/10959/10959-19.cpp b/uva_cpp_clean/10959/10959-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fea369bdff753012fdb485a49eb5317bbbcb421e --- /dev/null +++ b/uva_cpp_clean/10959/10959-19.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef pair ii; + +vector AdjList; +int visited[1002]; + +void bfs(int i, int j, int & acum) { + acum = -1; + queue q; + q.push(make_pair(i, 0)); + while (!q.empty()) { + ii n = q.front(); + q.pop(); + if (n.first == j) { + acum = n.second; + break; + } + if (visited[n.first]) continue; + visited[n.first] = true; + for (int j = 0; j < AdjList[n.first].size(); j++) + q.push(make_pair(AdjList[n.first][j], n.second + 1)); + } +} + +int main() { + int cases; + cin >> cases; + for (int c = 1; c <= cases; c++) { + int p, d; + cin >> p >> d; + AdjList.assign(p, vi()); + for (int k = 0; k < d; k++) { + int i1, i2; + cin >> i1 >> i2; + AdjList[i1].push_back(i2); + AdjList[i2].push_back(i1); + } + for (int k = 1; k < p; k++) { + int num; + for (int j = 0; j < p; j++) visited[j] = false; + bfs(k, 0, num); + printf("%d\n", num); + } + if (c != cases) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/10959/10959-21.cpp b/uva_cpp_clean/10959/10959-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e08ccf327c9d3d908c3f831586871b773d61ab0a --- /dev/null +++ b/uva_cpp_clean/10959/10959-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10959 + Name: The Party, Part I + Problem: https://onlinejudge.org/external/109/10959.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int dist[1002]; +vector adj[1002]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin>>T; + while (T--) { + int n, m; cin>>n>>m; + for (int i=0; i> u >> v; + adj[u].push_back(v); + adj[v].push_back(u); + } + + memset(dist+1, -1, n*sizeof(int)); + queue q; + q.push(0); + while (!q.empty()) { + int u = q.front(); q.pop(); + for (int v: adj[u]) + if (dist[v] < 0) { + dist[v] = dist[u] + 1; + q.push(v); + } + } + for (int i=1; i + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + int p, d; + std::cin >> p >> d; + + int *tab = new int[p * p]; + + for (int i = 0; i < p; i++) + { + for (int j = 0; j < p; j++) + { + if (i == j) + { + tab[i * p + j] = 0; + } + else + { + tab[i * p + j] = 10000; + } + } + } + + for (int i = 0; i < d; i++) + { + int p1, p2; + std::cin >> p1 >> p2; + + tab[p1 * p + p2] = 1; + tab[p2 * p + p1] = 1; + } + + for (int k = 0; k < p; k++) + { + for (int i = 0; i < p; i++) + { + for (int j = 0; j < p; j++) + { + tab[i * p + j] = std::min(tab[i * p + j], tab[i * p + k] + tab[k * p + j]); + } + } + } + + for (int i = 1; i < p; i++) + { + std::cout << tab[i] << std::endl; + } + + if (tests != 0) + { + std::cout << std::endl; + } + + delete[] tab; + } +} diff --git a/uva_cpp_clean/10959/10959-9.cpp b/uva_cpp_clean/10959/10959-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..939f6b705a12e560d28bb279d3fe7b9fca638090 --- /dev/null +++ b/uva_cpp_clean/10959/10959-9.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int tc, n, m, u, v, d[1010]; +bool Adj[1010][1010]; + +void bfs(int source){ + for(int i = 0; i < n; i++) d[i] = -1; + queue Q; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + u = Q.front(), Q.pop(); + for(int i = 0; i < n; i++) + if(Adj[u][i] && d[i] == -1) + d[i] = d[u] + 1, Q.push(i); + } +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &n, &m); + memset(Adj, false, sizeof Adj); + for(int i = 0; i < m; i++) scanf("%d %d", &u, &v), Adj[u][v] = Adj[v][u] = true; + bfs(0); + for(int i = 1; i < n; i++) printf("%d\n", d[i]); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/10963/10963-2.cpp b/uva_cpp_clean/10963/10963-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ea7ae9ebd586e45a4e89b0d00dc3d89e1cc046f --- /dev/null +++ b/uva_cpp_clean/10963/10963-2.cpp @@ -0,0 +1,54 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; cin >> tc; + while (tc--) { + int w; cin >> w; + int a, b; cin >> a >> b; + int gap = a - b; + w--; + bool f = 1; + while (w--) { + cin >> a >> b; + if (gap != a - b) { + f = 0; + } + } + if (f) cout << "yes\n"; + else cout << "no\n"; + if (tc) cout << el; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10963/10963-21.cpp b/uva_cpp_clean/10963/10963-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8b7607ab658364951d873ba489489f484114072 --- /dev/null +++ b/uva_cpp_clean/10963/10963-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10963 + Name: The Swallowing Ground + Problem: https://onlinejudge.org/external/109/10963.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool solve(int n) { + if (!n) return true; + + int y1, y2, gap; + cin >> y1 >> y2; + gap = y1 - y2; + + bool result = true; + for (int i=1; i> y1 >> y2; + result &= y1-y2 == gap; + } + return result; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + while (T-- && cin >> n) { + cout << (solve(n) ? "yes\n" : "no\n"); + if (T) cout << '\n'; + } +} diff --git a/uva_cpp_clean/10963/10963-9.cpp b/uva_cpp_clean/10963/10963-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4acd4d74b16d4382b8a24821c0489af3c2f43d1 --- /dev/null +++ b/uva_cpp_clean/10963/10963-9.cpp @@ -0,0 +1,22 @@ +#include +#define abs(x) ( x >= 0 ? x : -x ) + +int main(){ + int N, n, ind, a, b, i, dist1[1000], dist2; + scanf("%d",&N); + while(N--){ + ind = 1; + scanf("%d", &n); + for(i = 0; i < n; i++){ + scanf("%d %d", &a, &b); + dist1[i] = abs(a - b); + } + dist2 = dist1[1]; + for(i = 0; i < n; i++) + if(dist1[i] != dist2) ind = 0; + if(ind) printf("yes\n"); + else printf("no\n"); + if(N) printf("\n"); + } + return(0); +} diff --git a/uva_cpp_clean/10963/main.cpp b/uva_cpp_clean/10963/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70fb7f7a21d2e83a141e8bd744bf9fc7e34449bc --- /dev/null +++ b/uva_cpp_clean/10963/main.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main(){ + int n, w, y1, y2, diff_actual, diff; + cin >> n; + for(int i=0; i> w; + bool res = true; + for(int c=0; c < w; c++){ + cin >> y1 >> y2; + if(c == 0) diff = y1 - y2; + else{ + diff_actual = y1 - y2; + if(diff_actual != diff && diff_actual != -diff) res = false; + } + } + if(res) cout << "yes" << endl; + else cout << "no" << endl; + if(i != n-1) cout << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/10967/10967-21.cpp b/uva_cpp_clean/10967/10967-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a937a93b91a59a29e66778750c612cd32654ec99 --- /dev/null +++ b/uva_cpp_clean/10967/10967-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10967 + Name: The Great Escape + Problem: https://onlinejudge.org/external/109/10967.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + +int X[143][143], D[143][143]; +char M[143][143], tch; +struct State { + int i, j, d; + State(int i, int j, int d):i(i),j(j),d(d){} + bool operator < (const State &o) const { return d > o.d; } +}; + + +char dd[] = "NWSE"; +int dk[128], + di[] = {1, 0, -1, 0}, + dj[] = {0, 1, 0, -1}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + dk['N']=2; dk['S']=0; + dk['E']=1; dk['W']=3; + + int T, n, m; + cin >> T; + while (T--) { + cin >> n >> m; + for (int i=0; i> M[i]; + + for (int i=0; i> X[i][j]; + } + + priority_queue q; + q.push(State(n-1, 0, D[n-1][0]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (D[s.i][s.j] != s.d) continue; + + if (!s.i && s.j==m-1) { + cout << s.d << '\n'; + goto fin; + } + + int ski = dk[M[s.i][s.j]]; + for (int k=0; k<4; ++k) { + State t(s.i+di[k], s.j+dj[k], s.d+1); + if (t.i>=0 && t.i=0 && t.j2) kdd = 4-kdd; + t.d += kdd * X[s.i][s.j]; + if (D[t.i][t.j] > t.d) { + D[t.i][t.j] = t.d; + q.push(t); + } + } + } + } + cout << "Poor Kianoosh\n"; + fin:; + } +} diff --git a/uva_cpp_clean/10968/10968-21.cpp b/uva_cpp_clean/10968/10968-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22aa51d684781fd0e9ef4a6b0f4834f1a8d2b92a --- /dev/null +++ b/uva_cpp_clean/10968/10968-21.cpp @@ -0,0 +1,75 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10968 + Name: KuPellaKeS + Problem: https://onlinejudge.org/external/109/10968.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +#define MAXN 1001 +int adj[MAXN][MAXN], deg[MAXN], d[MAXN]; +int tr[MAXN][MAXN]; + +int main(){ + int n, m, u, v; + while (cin>>n>>m && (n||m)) { + for (int i=0; i> u >> v; + adj[u-1][deg[u-1]++] = v-1; + adj[v-1][deg[v-1]++] = u-1; + } + + bool p=true; + int s=-1, t=-1; + for (int i=0; p && i Q; + Q.push(s); d[s]=0; + + while (!Q.empty()) { + u = Q.front(); Q.pop(); + if (u == t) break; + + for (int i=0; i +#include +#include +#include +#include +#include + +using namespace std; + +int main(){ + long long int m,n; + while(scanf("%lld %lld", &m, &n) != EOF){ + printf("%d\n", m*n-1); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10970/10970-19.cpp b/uva_cpp_clean/10970/10970-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e27dc6a0661d2e09b974239118c894a37578b411 --- /dev/null +++ b/uva_cpp_clean/10970/10970-19.cpp @@ -0,0 +1,10 @@ +#include +using namespace std; + +int main() { + int N, M; + while (scanf("%d %d", &N, &M) != EOF) { + printf("%d\n", N*M - 1); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/10970/10970-21.cpp b/uva_cpp_clean/10970/10970-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9794222c8e902fea74856ac9b64c90a538485d02 --- /dev/null +++ b/uva_cpp_clean/10970/10970-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10970 + Name: Big Chocolate + Problem: https://onlinejudge.org/external/109/10970.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int m, n; + while (scanf("%d%d", &n, &m) == 2) + cout << n*m - 1 << endl; +} \ No newline at end of file diff --git a/uva_cpp_clean/10970/10970-5.cpp b/uva_cpp_clean/10970/10970-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..98f4fc85ec67feeda3163aac99081c26381e2f16 --- /dev/null +++ b/uva_cpp_clean/10970/10970-5.cpp @@ -0,0 +1,9 @@ +#include +int main() +{ + int m,n,c; + while(scanf("%d%d",&m,&n)==2) + printf("%d\n",m*n-1); + + return 0; +} diff --git a/uva_cpp_clean/10970/main.cpp b/uva_cpp_clean/10970/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57f2d25e18a60ac4064a03b0d2eb0524fa90d752 --- /dev/null +++ b/uva_cpp_clean/10970/main.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main(){ + + int m,n; + while(cin >> m >> n) + cout << m*n - 1 << '\n'; + + + return 0; +} diff --git a/uva_cpp_clean/10973/10973-21.cpp b/uva_cpp_clean/10973/10973-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e8951f3ef18d90d81a07682c3ca4cba997b107a --- /dev/null +++ b/uva_cpp_clean/10973/10973-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10973 + Name: Triangle Counting + Problem: https://onlinejudge.org/external/109/10973.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------------------ + +int mat[3013][3013], used[3013][3013]; +vector adj[3013]; +int main() { + int T = readUInt(); + for (int cse=1; cse<=T; ++cse) { + int n = readUInt(), + m = readUInt(); + + for (int i=0; i v) swap(u, v); + adj[u].push_back(v); + mat[u][v] = cse; + used[u][v] = 0; + } + + int result = 0; + for (int u=0; u= 2) + break; + } + } + cout << result << '\n'; + } +} diff --git a/uva_cpp_clean/10973/10973-9.cpp b/uva_cpp_clean/10973/10973-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edb86ff42ea88adb9ce2a6faaa3b275345323c54 --- /dev/null +++ b/uva_cpp_clean/10973/10973-9.cpp @@ -0,0 +1,26 @@ +#include + +#define SIZE 3010 + +using namespace std; + +int tc, V, E, a, b, ans; +bool Adj[SIZE][SIZE]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + memset(Adj, false, sizeof Adj); + scanf("%d %d", &V, &E); + for(int it = 0; it < E; it++) scanf("%d %d", &a, &b), Adj[a][b] = Adj[b][a] = true; + ans = 0; + for(int v1 = 1; v1 <= V; v1++) + for(int v2 = v1 + 1; v2 <= V; v2++){ + if( Adj[v1][v2] ) + for(int v3 = v2 + 1; v3 <= V; v3++) + if( Adj[v2][v3] and Adj[v3][v1]) ans++; + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/10976/10976-11.cpp b/uva_cpp_clean/10976/10976-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a9d370fe47928c1ebaa8364f45c1ca4129465c7 --- /dev/null +++ b/uva_cpp_clean/10976/10976-11.cpp @@ -0,0 +1,68 @@ +#include +/* +Problem: 10976 - Fractions Again?! +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1917 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int K; +double y; +int xs[10001], ys[10001], idx = 0; + +int main () { + + while ( scanf("%d\n", &K) != EOF ){ + idx = 0; + + for (int x = K+1; x <= K*2; x++){ + y = x*K; + y /= (x-K); + if (y == int(y)){ + xs[idx] = x; + ys[idx] = (int)y; + idx++; + } + } + + printf("%d\n", idx); + for (int i = 0; i < idx; i++) + printf("1/%d = 1/%d + 1/%d\n", K, ys[i], xs[i]); + } + + return 0; +} +/* +Sample input:- +----------------- +2 +12 + +Sample output:- +----------------- +2 +1/2 = 1/6 + 1/3 +1/2 = 1/4 + 1/4 +8 +1/12 = 1/156 + 1/13 +1/12 = 1/84 + 1/14 +1/12 = 1/60 + 1/15 +1/12 = 1/48 + 1/16 +1/12 = 1/36 + 1/18 +1/12 = 1/30 + 1/20 +1/12 = 1/28 + 1/21 +1/12 = 1/24 + 1/24 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/10976/10976-13.cpp b/uva_cpp_clean/10976/10976-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aff66757a237a9cc4ba9c0d79d3c7b310a77a916 --- /dev/null +++ b/uva_cpp_clean/10976/10976-13.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int k; + + vector > ans; + + while (cin >> k) + { + ans.clear(); + + for (int y = k + 1; y <= 2 * k; y++) + { + int x = ((k * y) / (y - k)); + + if ((k * y) % (y - k) == 0) + { + ans.push_back(make_pair(x, y)); + } + } + + cout << (int)ans.size() << endl; + + for (int i = 0; i < (int)ans.size(); i++) + { + printf("1/%d = 1/%d + 1/%d\n", k, ans[i].first, ans[i].second); + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10976/10976-17.cpp b/uva_cpp_clean/10976/10976-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f917b8583d9c1127eaa3146b84f8a2229201c192 --- /dev/null +++ b/uva_cpp_clean/10976/10976-17.cpp @@ -0,0 +1,25 @@ +/** + * Fractions Again?! + * AOAPC II Example 7-3 + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int k; + while (cin >> k) + { + vector ys; + for (int y = k + 1; y <= 2 * k; ++y) + if (k * y % (y - k) == 0) + ys.push_back(y); + cout << ys.size() << "\n"; + for (int i = 0; i < ys.size(); ++i) + { + int y = ys[i], x = k * y / (y - k); + cout << "1/" << k << " = 1/" << x << " + 1/" << y << "\n"; + } + } +} diff --git a/uva_cpp_clean/10976/10976-19.cpp b/uva_cpp_clean/10976/10976-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68f6c3320ab4643c2c10cef6798f4fc93bef71ed --- /dev/null +++ b/uva_cpp_clean/10976/10976-19.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +using namespace std; + +typedef pair ll; + +int main() { + long long k; + queue q; + while (cin >> k) { + long long x, y = k; + while (true) { + y++; + x = (k*y)/(y-k); + if (x < y) break; + if (k == (x*y)/(x+y) && (x*y)%(x+y) == 0) q.push(ll(x, y)); + } + printf("%lu\n", q.size()); + while (!q.empty()) { + printf("1/%lld = 1/%lld + 1/%lld\n", k, q.front().first, q.front().second); + q.pop(); + } + } + return 0; +} diff --git a/uva_cpp_clean/10976/10976-2.cpp b/uva_cpp_clean/10976/10976-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcb90446ffaa6e200d123a30f935d35a44640abf --- /dev/null +++ b/uva_cpp_clean/10976/10976-2.cpp @@ -0,0 +1,47 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +#define in cin +#define out cout +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +int main(){ + ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int k; + while (in >> k) { + vpii v; + // sol lies between k and 2*k + for(int i = k+1; i <= 2*k; ++i) if ((i*k)%(i-k)==0) + v.push_back({(i*k)/(i-k), i}); + // out << v.size() << el; + printf("%d\n", (int) v.size()); + for(auto x: v) printf("1/%d = 1/%d + 1/%d\n", k, x.first, x.second); + } + return 0; +} diff --git a/uva_cpp_clean/10976/10976-21.cpp b/uva_cpp_clean/10976/10976-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e09ffa053df0b0b350a12235a0d2493b17d0fe25 --- /dev/null +++ b/uva_cpp_clean/10976/10976-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10976 + Name: Fractions Again?! + Problem: https://onlinejudge.org/external/109/10976.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int k; + while (cin>>k) { + vector bs; + for (int b = k<<1; b > k; b--) + if ((k*b) % (b-k) == 0) + bs.push_back(b); + + cout << bs.size() << endl; + for (int i=bs.size()-1; i>=0; i--) { + int b = bs[i]; + printf("1/%d = 1/%d + 1/%d\n", k, k*b/(b-k), b); + } + } +} diff --git a/uva_cpp_clean/10976/10976-3.cpp b/uva_cpp_clean/10976/10976-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05cf21a1268e753161e6fb629efe891b0b90e76b --- /dev/null +++ b/uva_cpp_clean/10976/10976-3.cpp @@ -0,0 +1,35 @@ +// Problem: U-Va 10976 - Fractions Again?! + +#include +#include + +using std :: bitset; + +int main () +{ + // Bitset will be used to save time in the second loop + bitset < 10001 > isTrue; + int n, + count; + + while ( scanf("%d", &n) == 1 ) + { + //reseting the variables: + count = 0; + isTrue.reset(); + + for ( int l = n + 1; l <= 2*n; l++ ) + { + if ( (l*n)%(l-n) == 0 ) + { + isTrue[l-n] = 1; + count++; + } + } + printf ("%d\n", count); + + for ( int l = 1; l <=n; l++ ) + if ( isTrue[l] ) + printf ("1/%d = 1/%d + 1/%d\n", n, ((n+l)*n)/(l) , n+l ); + } +} diff --git a/uva_cpp_clean/10976/10976-9.cpp b/uva_cpp_clean/10976/10976-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7e118664e9d0dfd67b50afc2d11554dc536de13 --- /dev/null +++ b/uva_cpp_clean/10976/10976-9.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +vector < pair > v; +int k, lim; +bool proof; + +int main(){ + while(scanf("%d",&k) == 1){ + lim = 10000 * k; + for(int y = k + 1; y < lim; y++) + if((k * y) % (y - k)==0) + v.push_back( make_pair(k * y/ (y - k), y)), lim = k * y / (y - k); + sort(v.begin(), v.end()); + cout << v.size() << endl; + for(int i = v.size() - 1; i >= 0; i--) + printf("1/%d = 1/%lld + 1/%lld\n", k, v[i].first, v[i].second); + v.clear(); + } +} diff --git a/uva_cpp_clean/10977/10977-21.cpp b/uva_cpp_clean/10977/10977-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2e1e10ec6d407f4f935120c2f56f03da58fb6de --- /dev/null +++ b/uva_cpp_clean/10977/10977-21.cpp @@ -0,0 +1,122 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10977 + Name: Enchanted Forest + Problem: https://onlinejudge.org/external/109/10977.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------- + +int dist2(int x1, int y1, int x2, int y2) { + int xx = x1-x2, + yy = y1-y2; + + return xx*xx + yy*yy; +} + +struct State { + int i, j, d; + State(int i, int j, int d):i(i),j(j),d(d) {} +}; + +int dy[] = {0,0,1,-1}, + dx[] = {1,-1,0,0}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m; + bool B[217][217]; + memset(B, -1, sizeof(B)); + while ((n=readUInt())>0 && (m=readUInt())>0) { + memset(B[n+1], -1, m+2); + for (int i=1; i<=n; ++i) { + memset(B[i]+1, 0, m); + B[i][m+1] = 1; + } + + int c = readUInt(); + for (int k=0; k q; + q.push(State(1,1,0)); + while (!q.empty()) { + State s = q.front(); q.pop(); + for (int k=0; k<4; ++k) { + State t(s.i+dy[k], s.j+dx[k], s.d+1); + if (!B[t.i][t.j]) { + if (t.i==n && t.j==m) { + cout << t.d << endl; + goto fin; + } + B[t.i][t.j] = 1; + q.push(t); + } + } + } + } + cout << "Impossible.\n"; + fin:; + } +} diff --git a/uva_cpp_clean/10978/10978-21.cpp b/uva_cpp_clean/10978/10978-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..759844356d2c56ffdc724fa848d03a79130334e4 --- /dev/null +++ b/uva_cpp_clean/10978/10978-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10978 + Name: Let's Play Magic! + Problem: https://onlinejudge.org/external/109/10978.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + bool fl[55]; + string cards[55], cd, word; + while (cin >> n && n) { + int cur = -1; + memset(fl, 0, n); + for (int i=0; i> cd >> word; + for (int j=word.length(); j>0; --j) + for (cur = (cur + 1) % n; fl[cur]; cur = (cur + 1) % n); + cards[cur] = cd; + fl[cur] = 1; + } + cout << cards[0]; + for (int i=1; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10980 + Name: Lowest Price in Town + Problem: https://onlinejudge.org/external/109/10980.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char tmp; +string line; +int DP[202], n, m, d, d1, d2; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; cin >> d1 >> tmp >> d2 >> m; ++cse) { + cout << "Case " << cse << ":\n"; + + int d = d1*100 + d2; + for (int i=1; i<202; ++i) + DP[i] = i * d; + + while (m--) { + cin >> n >> d1 >> tmp >> d2; + int d = d1*100 + d2; + for (int i=n; i<202; ++i) + DP[i] = min(DP[i], DP[i-n]+d); + } + + for (int i=200; i>0; --i) + DP[i] = min(DP[i], DP[i+1]); + + cin.ignore(100, '\n'); + getline(cin, line); + for (int i=0, n=0; i<=line.length(); ++i) + if (line[i] >= '0') + n = n*10 + line[i]-'0'; + else { + cout << "Buy " << n << " for $" + << fixed << setprecision(2) << (DP[n]/100.0) + << "\n"; + n = 0; + } + } +} diff --git a/uva_cpp_clean/10980/10980-9.cpp b/uva_cpp_clean/10980/10980-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b828612bb7a0c2b382d89f6e1b3019cffa9329c --- /dev/null +++ b/uva_cpp_clean/10980/10980-9.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +const int MAX_N = 100 + 10, MAX_BUY = 10000 + 10, INF = 1e7; + +int tc, buy, memo[MAX_N][MAX_BUY]; +vector > arr; + +int dp (int id, int cnt) { + if (id == arr.size()) return buy <= cnt ? 0 : INF; + if (cnt < MAX_BUY and ~memo[id][cnt]) return memo[id][cnt]; + int ret = INF, get = -1; + do { + get++; + ret = min(ret, get * arr[id].second + dp(id + 1, cnt + get * arr[id].first)); + } while (get * arr[id].first <= buy); + if (cnt < MAX_BUY) memo[id][cnt] = ret; + return ret; +} + +bool solve () { + int a, b, m, ct; + if (scanf("%d.%d %d", &a, &b, &m) != 3) return false; + arr.clear(); + arr.push_back({1, a * 100 + b}); + for (int i = 0; i < m; i++) { + scanf("%d %d.%d", &ct, &a, &b); + arr.push_back({ct, a * 100 + b}); + } + getchar(); + string line; + getline(cin, line); + istringstream token(line); + printf("Case %d:\n", ++tc); + while (token >> buy) { + for (int i = 0; i < arr.size(); i++) + for (int j = 0; j < MAX_BUY; j++) + memo[i][j] = -1; + int res = dp(0, 0); + printf("Buy %d for $%d.%.2d\n", buy, res / 100, res % 100); + } + return true; +} + +int main () { + while (solve()) ; + return (0); +} diff --git a/uva_cpp_clean/10986/10986-10.cpp b/uva_cpp_clean/10986/10986-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6be4b15dab8dadc74f916cc67835b19b8581b78c --- /dev/null +++ b/uva_cpp_clean/10986/10986-10.cpp @@ -0,0 +1,101 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e4+9; + + +void solve() { + + int n, m, s, t, a, b, w; + cin >> n >> m >> s >> t; + + + vector> adj[n + 5]; + + + for (int i = 0; i < m; i++) { + + cin >> a >> b >> w; + adj[a].push_back({b, w}); + adj[b].push_back({a, w}); + + } + + int dis[n + 5]; + bool vis[n + 5]; + + for (int i = 0; i <= n; i++) { + dis[i] = inf; + vis[i] = 0; + } + + priority_queue> pq; + dis[s] = 0; + pq.push({0, s}); + + while (!pq.empty()) { + int a = pq.top().second; + pq.pop(); + + if (vis[a]) continue; + vis[a] = 1; + + for (auto u: adj[a]) { + int b = u.first, w = u.second; + + if (dis[a] + w < dis[b]) { + dis[b] = dis[a] + w; + pq.push({-1 * dis[b], b}); + } + } + + } + if (dis[t] == inf) cout << "unreachable\n"; + else + cout << dis[t] << "\n"; + + +} + +int main() { + FAST + + int t; cin>>t; + int c=1; + while (t--){ + cout<<"Case #"< +#include +#include +#include +#include +#include + +using namespace std; + +int n, m, s, t; +int test, c = 1; +vector dis; +vector visited; +vector > > graph; + +class comparator +{ +public: + + bool operator()(pair &child1, pair &child2) + { + return child1.second > child2.second; + } +}; + +priority_queue , vector >, comparator> pq; + +int dijkstra() +{ + visited[s] = true; + + dis[s] = 0; + + pq.push(make_pair(s, dis[s])); + + while (!pq.empty()) + { + pair head = pq.top(); + + pq.pop(); + + int headVer = head.first; + int headDis = head.second; + + int childWeight, childVer; + + for (int i = 0; i < (int)graph[headVer].size(); i++) + { + childVer = graph[headVer][i].first; + + childWeight = graph[headVer][i].second; + + if (!visited[childVer]) + { + if (childWeight + headDis < dis[childVer]) + { + dis[childVer] = childWeight + headDis; + + pq.push(make_pair(childVer, dis[childVer])); + } + } + } + } + + return dis[t]; +} + +void initialize(int n) +{ + dis.clear(); + dis.resize(n, INT_MAX); + + graph.clear(); + graph.resize(n); + + visited.clear(); + visited.resize(n, 0); +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n >> m >> s >> t; + + initialize(n); + + for (int i = 0; i < m; i++) + { + int x, y, w; + + cin >> x >> y >> w; + + graph[x].push_back(make_pair(y, w)); + graph[y].push_back(make_pair(x, w)); + } + + int ans = dijkstra(); + + cout << "Case #" << c++ << ": "; + + if (ans == INT_MAX) cout << "unreachable" << endl; + + else cout << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10986/10986-14.cpp b/uva_cpp_clean/10986/10986-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..83e32497d3cb588271d15cad5bce40bfc4ed2589 --- /dev/null +++ b/uva_cpp_clean/10986/10986-14.cpp @@ -0,0 +1,93 @@ +/*************************************************** + * Problem name : 10986 Sending Mail.cpp + * OJ : Uva + * Result : Accepted + * Date : 11-04-17 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 20005 +using namespace std; +typedef long long ll; +vectoradj[MAX]; +mapmp; +//~ int cost[MAX][MAX]; +int dis[MAX]; +int dijkstra(int s, int d) { + //~ priority_queueQ; + queueQ; + Q.push(s); + dis[s] = 0; + while (!Q.empty()) { + int u = Q.front(); + Q.pop(); + int sz = adj[u].size(); + for (int i = 0; i < sz; i++) { + int v = adj[u][i]; + pii p = make_pair(u, v); + if (dis[u] + mp[p] < dis[v]) { + dis[v] = dis[u] + mp[p]; + Q.push(v); + } + } + } + return dis[d]; +} +void mem() { + for (int i = 0; i < MAX; i++) { + dis[i] = 1000000; + } +} +int main () { + int tc, n, m, s, d; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d %d %d %d", &n, &m, &s, &d); + if (m == 0) { + printf("Case #%d: unreachable\n", t); + } else { + for (int i = 0; i < m; i++) { + int u, v, w; + scanf("%d %d %d", &u, &v, &w); + adj[u].push_back(v); + adj[v].push_back(u); + pii p = make_pair(u, v); + mp[p] = w; + pii q = make_pair(v, u); + mp[q] = w; + //~ cost[u][v] = w; + //~ cost[v][u] = w; + } + mem(); + int ans = dijkstra(s, d); + if (ans == 1000000) { + printf("Case #%d: unreachable\n", t); + } else { + printf("Case #%d: %d\n", t, dijkstra(s, d)); + } + } + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + //~ for(int j = 0; j +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vii; +typedef vector vi; + +vector adjList; + +int dijkstra(int S, int T) { + vi dist(adjList.size(), INT_MAX); + dist[S] = 0; + priority_queue > pq; + pq.push(ii(0, S)); + while (!pq.empty()) { + ii front = pq.top(); pq.pop(); + int d = front.first, u = front.second; + if (d > dist[u]) continue; + for (int j = 0; j < adjList[u].size(); j++) { + ii v = adjList[u][j]; + if (dist[u] + v.second < dist[v.first]) { + dist[v.first] = dist[u] + v.second; + pq.push(ii(dist[v.first], v.first)); + } + } + } + return dist[T]; +} + +int main() { + int N; + cin >> N; + for (int i = 1; i <= N; i++) { + int n, m, S, T; + cin >> n >> m >> S >> T; + adjList.assign(n, vii()); + for (int j = 0; j < m; j++) { + int s1, s2, lat; + cin >> s1 >> s2 >> lat; + adjList[s1].push_back(make_pair(s2, lat)); + adjList[s2].push_back(make_pair(s1, lat)); + } + int d = dijkstra(S, T); + printf("Case #%d: ", i); + if (d == INT_MAX) printf("unreachable\n"); + else printf("%d\n", d); + } + return 0; +} diff --git a/uva_cpp_clean/10986/10986-20.cpp b/uva_cpp_clean/10986/10986-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eadb3ec7c7b82f0506072cc3a5a274e9b208a1e9 --- /dev/null +++ b/uva_cpp_clean/10986/10986-20.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +const int INF = ((1<<31) - (1)); +const int MAXN = 50000; +vector < int > edge[MAXN+2]; +vector < int > cost[MAXN+2]; + +struct data { + int city, dist; + bool operator < ( const data & p ) const { + return dist > p.dist; + } +} ; + +int dijkstra( int source, int destination ) { + data u, v; + priority_queue < data > q; + int d[MAXN+2]; + for( int i = 0 ; i < MAXN ; ++i ) { + d[i] = INF; + } + + u.city = source, u.dist = 0; + q.push( u ), d[source] = 0; + while( !q.empty() ) { + u = q.top(); + q.pop(); + for( int i = 0 ; i < edge[u.city].size() ; ++i ) { + v.city = edge[u.city][i]; + v.dist = cost[u.city][i] + d[u.city]; + if( d[v.city] > v.dist ) { + d[v.city] = v.dist; + q.push( v ); + } + } + } + return d[destination]; +} + +int main( int argc, char ** argv ) { + int N; + scanf( "%d", &N ); + for( int nCase = 1 ; nCase <= N ; ++nCase ) { + int n, m, S, T; + scanf( "%d %d %d %d", &n, &m, &S, &T ); + + for( int i = 1 ; i <= m ; ++i ) { + int x, y, c; + scanf( "%d %d %d", &x, &y, &c ); + + edge[x].push_back( y ); + edge[y].push_back( x ); + + cost[x].push_back( c ); + cost[y].push_back( c ); + } + + int dis = dijkstra( S, T ); + if( dis == INF ) { + printf( "Case #%d: unreachable\n", nCase ); + } + else { + printf( "Case #%d: %d\n", nCase, dis ); + } + + for( int i = 0 ; i < m ; ++i ) { + edge[i].clear(); + cost[i].clear(); + } + } + return 0; +} diff --git a/uva_cpp_clean/10986/10986-21.cpp b/uva_cpp_clean/10986/10986-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84a0765d0f18d98d887419c0816fd4863a1aea03 --- /dev/null +++ b/uva_cpp_clean/10986/10986-21.cpp @@ -0,0 +1,108 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10986 + Name: Sending email + Problem: https://onlinejudge.org/external/109/10986.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------- +#define INF 2147483647 + +struct UD { + int u, d; + UD(int u, int d):u(u),d(d){} + bool operator < (const UD& ud) const { return d > ud.d; } +}; + +vector adj[20017]; +int D[20017]; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + for (int cse=1; cse<=T; ++cse) { + cout << "Case #" << cse << ": "; + + int n = readUInt(), + m = readUInt(), + src = readUInt(), + dst = readUInt(); + + for (int i=0; i q; + q.push(UD(src, D[src]=0)); + while (!q.empty()) { + UD ud = q.top(); q.pop(); + int u = ud.u; + if (D[u] != ud.d) continue; + if (u == dst) { + cout << ud.d << endl; + goto fin; + } + + for (UD vd: adj[u]) { + UD t(vd.u, ud.d+vd.d); + if (t.d < D[vd.u]) { + D[t.u] = t.d; + q.push(t); + } + } + } + cout << "unreachable\n"; + fin:; + } +} diff --git a/uva_cpp_clean/10986/10986-9.cpp b/uva_cpp_clean/10986/10986-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98ae886b05b7bfe1bb0615d66563448cad900c7d --- /dev/null +++ b/uva_cpp_clean/10986/10986-9.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +const int SIZE = 20010, INF = 1000000000; + +typedef pair ii; + +int tc, V, E, S, T, a, b, c, d, dis[SIZE]; +vector AdjList[SIZE], W[SIZE]; + +int Dijkstra(int source, int target){ + fill(dis, dis + V, INF), dis[source] = 0; + set < ii > st; + st.insert( ii(0, source) ); + while( not st.empty() ){ + ii front = *st.begin(); st.erase( st.begin() ); + int d = front.first, u = front.second; + if( u == target ) return dis[u]; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j], w = W[u][j]; + if( dis[v] > dis[u] + w ){ + dis[v] = dis[u] + w; + st.insert( ii(dis[v], v) ); + } + } + } + return dis[target]; +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d %d %d %d", &V, &E, &S, &T); + for(int i = 0; i < E; i++){ + scanf("%d %d %d", &a, &b, &c); + AdjList[a].push_back(b); + W[a].push_back(c); + AdjList[b].push_back(a); + W[b].push_back(c); + } + d = Dijkstra(S, T); + if( d != INF ) + printf("Case #%d: %d\n", t, d); + else + printf("Case #%d: unreachable\n", t); + for(int i = 0; i < V; i++) + AdjList[i].clear(), W[i].clear(); + } + return(0); +} diff --git a/uva_cpp_clean/10990/10990-13.cpp b/uva_cpp_clean/10990/10990-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ebac460120f172305a271b91f2ae5a7ae2d4c0d --- /dev/null +++ b/uva_cpp_clean/10990/10990-13.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 2000002 + +typedef long long ll; +typedef vector vb; + +int phi[MAX]; + +ll dpMAX[MAX] = {0}; +ll delPhi[MAX] = {0}; +vb isPrime(MAX, true); + +void initialize() +{ + isPrime[0] = isPrime[1] = false; + + for (int i = 0; i < MAX; ++i) + { + phi[i] = i; + } +} + +void calPhi() +{ + for (int i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (int j = i; j < MAX; j += i) + { + phi[j] -= (phi[j] / i); + + isPrime[j] = false; + } + + isPrime[i] = true; + } + } +} + +void calDelPhi() +{ + delPhi[2] = phi[2]; + delPhi[3] = phi[3]; + + dpMAX[2] = delPhi[2]; + dpMAX[3] = dpMAX[2] + delPhi[3]; + + for (int i = 4; i < MAX; ++i) + { + delPhi[i] = delPhi[phi[i]] + 1; + dpMAX[i] = dpMAX[i - 1] + delPhi[i]; + } +} + +int main() +{ + initialize(); + + calPhi(); + + calDelPhi(); + + int n; + + cin >> n; + + while (n--) + { + int l, h; + + cin >> l >> h; + + cout << dpMAX[h] - dpMAX[l - 1] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/10990/10990-19.cpp b/uva_cpp_clean/10990/10990-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..706bec1bd5b168d5e33efc8cda7a5fe1e922c7f4 --- /dev/null +++ b/uva_cpp_clean/10990/10990-19.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +inline void sieve(ll upperbound = 2000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline ll EulerPhi(ll n) { + ll idx=0, p=primes[0], ans=n; + while (n!=1 && (p*p<=n)) { + if (n%p==0) ans-=ans/p; + while (n%p==0) n/=p; + p=primes[++idx]; + } + if (n!=1) ans-=ans/n; + return ans; +} + +ll phi[2000001]; +ll depth[2000001]; +ll sum[2000001]; + +int main() { + sieve(); + for (int i = 2; i <= 2000000; i++) { + phi[i] = EulerPhi(i); + if (phi[i] == 1) depth[i] = 1; + else depth[i] = 1 + depth[phi[i]]; + sum[i] = sum[i-1] + depth[i]; + } + int N; + cin >> N; + while (N--) { + int m, n; + cin >> m >> n; + printf("%lld\n", sum[n] - sum[m-1]); + } + return 0; +} diff --git a/uva_cpp_clean/10990/10990-9.cpp b/uva_cpp_clean/10990/10990-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34d9fea8329f09fcbbc09fb8c53279920a4aa717 --- /dev/null +++ b/uva_cpp_clean/10990/10990-9.cpp @@ -0,0 +1,42 @@ +#include + +#define SIZE 2000010 + +using namespace std; + +int EulerPhi[SIZE], depthPhi[SIZE], dp[SIZE], tc, m, n; + +void sieve(){ + for(int it = 2; it < SIZE; it++) EulerPhi[it] = it; + for(int it1 = 2; it1 < SIZE; it1++) + if( EulerPhi[it1] == it1 ) + for(int it2 = it1; it2 < SIZE; it2 += it1) + EulerPhi[it2] -= EulerPhi[it2] / it1; +} + +void fillDepth(){ + for(int it = 2; it < SIZE; it++){ + int ct = 1, num = it; + while( EulerPhi[num] != 1 ){ + num = EulerPhi[num]; + if( num < it ){ + ct += depthPhi[num]; + break; + } + ct++; + } + depthPhi[it] = ct; + dp[it] = dp[it - 1] + depthPhi[it]; + } +} + +int main(){ + sieve(); + fillDepth(); + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &m, &n); + printf("%d\n", dp[n] - dp[m - 1]); + } + return(0); +} diff --git a/uva_cpp_clean/10991/10991-20.cpp b/uva_cpp_clean/10991/10991-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3328987ba64eb502e1b51dcb90d4c6e32d1bfc8c --- /dev/null +++ b/uva_cpp_clean/10991/10991-20.cpp @@ -0,0 +1,34 @@ +#include +#include + +using namespace std; + +int main() +{ + int tc; + + scanf("%d", &tc); + while( tc-- ) + { + double R1, R2, R3; + scanf("%lf %lf %lf", &R1, &R2, &R3); + + double a = R2+R3; + double b = R1+R3; + double c = R1+R2; + + double theta1 = acos( (b*b+c*c-a*a)/(2.0*b*c) ); + double theta2 = acos( (a*a+c*c-b*b)/(2.0*a*c) ); + double theta3 = acos( (a*a+b*b-c*c)/(2.0*a*b) ); + + double arc1 = 0.5*theta1*R1*R1; + double arc2 = 0.5*theta2*R2*R2; + double arc3 = 0.5*theta3*R3*R3; + + double s = (a+b+c)/2.0; + double area = sqrt(s*(s-a)*(s-b)*(s-c)); + + printf("%.6lf\n", area-arc1-arc2-arc3); + } + return 0; +} diff --git a/uva_cpp_clean/10992/10992-21.cpp b/uva_cpp_clean/10992/10992-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fb46305f5712ada07b0afe5e893b71dbce7524b --- /dev/null +++ b/uva_cpp_clean/10992/10992-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 10992 + Name: The Ghost of Programmers + Problem: https://onlinejudge.org/external/109/10992.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char names[9][50] = { + "Tanveer Ahsan", + "Shahriar Manzoor", + "Adrian Kugel", + "Anton Maydell", + "Derek Kisman", + "Rezaul Alam Chowdhury", + "Jimmy Mardell", + "Monirul Hasan", +}; +int ds[11] = { 2, 5, 7, 11, 15, 20, 28, 36, 4, 100, 400 }, rs[11]; +int parseInt(const char s[]) { + int r = 0; + for (int i=0; s[i]; i++) + r = r*10 + s[i] - '0'; + return r; +} + + +int main(){ + char x[100]; + bool frst = 1; + while (scanf("%s", x)==1 && (x[1] || x[0]!='0')) { + if (frst) frst = 0; + else putchar('\n'); + printf("%s\n", x); + + bool noGhost = 1; + if (x[1] && x[2] && x[3] && (x[4] || parseInt(x) >= 2148)) { + memset(rs, 0, sizeof(rs)); + for (int i=0; x[i]; i++) + for (int j=0; j<11; j++) + rs[j] = (rs[j]*10 + x[i] - '0') % ds[j]; + + for (int i=0; i<8; i++) + if ((rs[i]-2148) % ds[i] == 0) { + printf("Ghost of %s!!!\n", names[i]); + noGhost = 0; + } + + if ((!rs[8] && rs[9]) || !rs[10]) { + puts("Ghost of K. M. Iftekhar!!!"); + noGhost = 0; + } + } + + if (noGhost) + puts("No ghost will come in this year"); + } +} diff --git a/uva_cpp_clean/10994/10994-19.cpp b/uva_cpp_clean/10994/10994-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..255664710c2b64f5bce2621925198e17e897d0a5 --- /dev/null +++ b/uva_cpp_clean/10994/10994-19.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +long long proc(long long n) { + if (n <= 0) return 0; + long long ret = 0; + ret += 45 * (n / 10); + ret += (n % 10 + 1) * (n % 10) / 2; + ret += proc(n/10); + return ret; +} + +int main() { + long long p, q; + while (cin >> p >> q && !(p < 0 && q < 0)) { + printf("%lld\n", proc(q) - proc(p - 1)); + } + return 0; +} diff --git a/uva_cpp_clean/110/110-21.cpp b/uva_cpp_clean/110/110-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..441876ef281cf1d1a5ebe56f62c0c050d9d1f05d --- /dev/null +++ b/uva_cpp_clean/110/110-21.cpp @@ -0,0 +1,69 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 110 + Name: Meta-Loopless Sorts + Problem: https://onlinejudge.org/external/1/110.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char ord[20]; +void dfs(int depth, int n) { + string indent(2*depth, ' '); + + if (depth == n) { + cout << indent << "writeln(" << ord[0]; + for (int i=1; i=0; --i) { + cout << indent; + if (i < depth) { + cout << "else"; + if (i > 0) cout << ' '; + } + if (i > 0) cout << "if " << ord[i-1] << " < " << char('a'+depth) << " then\n"; + else cout << '\n'; + dfs(depth + 1, n); + swap(ord[i-1], ord[i]); + } + + for (int i=0; ipubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + while (T--) { + cin >> n; + + cout << "program sort(input,output);\nvar\na"; + for (int i=1; i +using namespace std; + +int main() { + int N; + while (cin >> N && N != -1) { + unsigned int m = 0, f = 1; + for (int i = 0; i < N; i++) { + unsigned int aux = f; + f += m + 1; + m = aux; + } + printf("%u %u\n", m, f); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11000/11000-21.cpp b/uva_cpp_clean/11000/11000-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b54894885b5c79c248805d4f5a7df11781224b3 --- /dev/null +++ b/uva_cpp_clean/11000/11000-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11000 + Name: Bee + Problem: https://onlinejudge.org/external/110/11000.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MAXN 60 +long long int mr[MAXN], tr[MAXN]; +int main(){ + long long int f=1, m=0; + long long int f2, m2, n; + + for (int i=0; i>n && n>-1) + printf("%lld %lld\n", mr[n], tr[n]); +} diff --git a/uva_cpp_clean/11000/11000-4.cpp b/uva_cpp_clean/11000/11000-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a596b8165bf20b63cec876344aade35f07e5f04 --- /dev/null +++ b/uva_cpp_clean/11000/11000-4.cpp @@ -0,0 +1,29 @@ +#include + +int main() +{ + while (true) + { + int years; + std::cin >> years; + + if (years == -1) + { + break; + } + + unsigned int male = 0, female = 1, femaleOld = 0; + + for (int i = 0; i < years; i++) + { + unsigned int tempMale = female + male; + unsigned int tempFemale = female + femaleOld; + + femaleOld = female; + female = tempFemale; + male = tempMale; + } + + std::cout << male << " " << male + female << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11000/11000-5.cpp b/uva_cpp_clean/11000/11000-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cbf4e88dff938939b2c7a8525858afea23e103b5 --- /dev/null +++ b/uva_cpp_clean/11000/11000-5.cpp @@ -0,0 +1,17 @@ +#include +int main() +{ +long long int n,i,s[100000],f[100000]; +while(scanf("%lld",&n)==1 && n>=0) +{ +f[0]=0; +f[1]=1; +s[0]=1; +for(i=2;i<=n+1;i++) + f[i]=f[i-1]+f[i-2]; +for(i=1;i<=n;i++) + s[i]=s[i-1]+f[i+1]; +printf("%lld %lld\n",s[n]-f[i],s[n]); +} +return 0; +} diff --git a/uva_cpp_clean/11000/11000-6.cpp b/uva_cpp_clean/11000/11000-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cec0ec219a153d0779731f048109cdaf1f74794 --- /dev/null +++ b/uva_cpp_clean/11000/11000-6.cpp @@ -0,0 +1,52 @@ +/* + dynamic programming + difficulty: easy + date: 03/Oct/2020 + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; +const int MAX_N = 50; + +/* + 1f -> 1m + 1m -> 1m 1f + + 0: 1f+0f 0m + 1: 1f+0f 1m + 2: 1f+1f 2m + 3: 1f+2f 4m + 4: 1f+4f 7m +*/ + +vector memo_m; +ll m(int n) { + if (n == 0) return 0; + if (n == 1) return 1; + + ll &ans = memo_m[n]; + if (ans != -1) return ans; + + return ans = m(n-1) + m(n-2) + 1; // m(n-1) + f(n-1) +} + +vector memo_f; +ll f(int n) { + if (n == 0) return 1; + + ll &ans = memo_f[n]; + if (ans != -1) return ans; + + return ans = m(n-1)+1; +} + +int main() { + memo_m.assign(MAX_N, -1); memo_f.assign(MAX_N, -1); + + int n; + while (cin >> n && n != -1) { + cout << m(n) << " " << m(n)+f(n) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11000/11000-9.cpp b/uva_cpp_clean/11000/11000-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2767f3d1b470b892311c884a19a9b1cf3fae7010 --- /dev/null +++ b/uva_cpp_clean/11000/11000-9.cpp @@ -0,0 +1,17 @@ +#include + +#define LIM 46 + +using namespace std; + +int n; +vector seq; + +int main(){ + seq.push_back(0), seq.push_back(1), seq.push_back(2); + for(int i = 3; i < LIM; i++) + seq.push_back(seq[i - 1]+seq[i - 2] + 1); + while(scanf("%d", &n), n != -1) + printf("%ld %ld\n", seq[n], seq[n + 1]); + return(0); +} diff --git a/uva_cpp_clean/11001/11001-2.cpp b/uva_cpp_clean/11001/11001-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e51f6d329bb9d5f94b4cc0ee1df8b841ac60fa76 --- /dev/null +++ b/uva_cpp_clean/11001/11001-2.cpp @@ -0,0 +1,55 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +int main(){ + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int vt, vo; + while (1) { + scanf("%d %d", &vt, &vo); + if (!vt && !vo) break; + int p = 0; + double Max = 0; + + for (int i = 1; i < vt; i++) { + if ((double)vt/i - vo < 0) break; + double sum = 0.3*sqrt((double)vt/i - vo)*i; + if (fabs(Max - sum) <= 1e-10) { + p = 0; + break; + } else if (sum > Max) { + Max = sum; + p = i; + } + } + printf("%d\n", p); + } + return 0; +} diff --git a/uva_cpp_clean/11001/11001-21.cpp b/uva_cpp_clean/11001/11001-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..624ea3a057aca58519187ee77a39534a5a088c0a --- /dev/null +++ b/uva_cpp_clean/11001/11001-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11001 + Name: Necklace + Problem: https://onlinejudge.org/external/110/11001.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int v, v0; +long long tlen(int n) { return n*(v-n*v0); } + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> v >> v0 && v0) { + if (v <= v0) cout << "0\n"; + else { + int n1 = ceil(v/(2.0*v0)), + n2 = floor(v/(2.0*v0)); + long long + t1 = tlen(n1), + t2 = tlen(n2); + + if (n1!=n2 && t1==t2) cout << "0\n"; + else cout << (t1 > t2 ? n1 : n2) << endl; + } + } +} diff --git a/uva_cpp_clean/11001/11001-9.cpp b/uva_cpp_clean/11001/11001-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66a246196f26aa7771af7740f8553d0a42712f6b --- /dev/null +++ b/uva_cpp_clean/11001/11001-9.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main(){ + int v, vo, ans; + double tmp; + vector x; + const double k = 0.3; + bool ind; + while(scanf("%d%d", &v, &vo),v || vo){ + ans = 0; + for(int i = 1; i <= v; i++){ + if((double)v / i <= vo) + break; + tmp = i * k * sqrt((double)v / i - vo); + x.push_back(tmp); + } + tmp = 0; + for(int i = 0; i < x.size(); i++) + if(tmp <= x[i]) + tmp = x[i], ans = i + 1; + for(int i = 0; i < x.size(); i++) + if(ans != i + 1 && fabs(x[i] - tmp) < 1e-12) + ans = 0; + printf("%d\n", ans); + x.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11003/11003-21.cpp b/uva_cpp_clean/11003/11003-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cf67190cc92ff7e15274f52b689b2d3989f766b --- /dev/null +++ b/uva_cpp_clean/11003/11003-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11003 + Name: Boxes + Problem: https://onlinejudge.org/external/110/11003.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int DP[6002][1001],W[1001],L[1001]; +int main(){ + int n,i,l; + while(scanf("%d",&n)==1 && n!=0) + { + for(i=0;i=0;i--) + for(l=0;l<6002;l++){ + DP[l][i]=DP[l][i+1]; + if(l-W[i]>=0 && L[i]>=0) + DP[l][i]=max(DP[l][i],1+DP[min(L[i],l-W[i])][i+1]); + } + cout< + +#define MAX_N 1010 +#define MAX_LOAD 6010 + +using namespace std; + +int n,w[MAX_N],load[MAX_N],W,ans,memo[MAX_N][MAX_LOAD]; + +int dp(int id,int cload){ + if(id==n || cload<0) return 0; + if(memo[id][cload]!=-1) return memo[id][cload]; + if(w[id]>cload) return memo[id][cload] = dp(id+1,cload); + return memo[id][cload] = max(dp(id+1,cload),1+dp(id+1,min(cload-w[id],load[id]))); +} + +int main(){ + while(scanf("%d",&n),n){ + W = 0; + memset(memo,-1,sizeof memo); + for(int i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11005 + Name: Cheapest Base + Problem: https://onlinejudge.org/external/110/11005.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + + +int C[36]; +int cost(int n, int b) { + if (!n) return C[0]; + + int r = 0; + while (n) + r += C[n % b], + n /= b; + return r; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, x; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + if (cse > 1) cout << endl; + cout << "Case " << cse << ":\n"; + for (int i=0; i<36; ++i) + cin >> C[i]; + + cin >> n; + while (n--) { + cin >> x; + vector r; + int cc = INF; + for (int i=2; i<=36; ++i) { + int c = cost(x, i); + if (c == cc) r.push_back(i); + else if (c < cc) { + cc = c; + r.clear(); + r.push_back(i); + } + } + + cout << "Cheapest base(s) for number " << x << ':'; + for (int b: r) + cout << ' ' << b; + cout << endl; + } + } +} diff --git a/uva_cpp_clean/11015/11015-21.cpp b/uva_cpp_clean/11015/11015-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68b3e1659ffafe61be41d05a8195dd50fb187416 --- /dev/null +++ b/uva_cpp_clean/11015/11015-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11015 + Name: 05-2 Rendezvous + Problem: https://onlinejudge.org/external/110/11015.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +string names[23]; +int M[23][23], S[23]; +int main() { + for (int cse=1, n,m; cin>>n>>m && (n||m); ++cse) { + cout << "Case #" << cse << " : "; + + for (int i=0; i> names[i]; + for (int j=0; j> u >> v >> d; + M[u-1][v-1] = M[v-1][u-1] = d; + } + + for (int k=0; k +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%I64d",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%I64d%I64d",&a,&b) + +#define sfi(t) scanf("%d",&t) +#define sfii(a,b) scanf("%d %d",&a,&b) +#define sfiii(a,b,c) scanf("%d %d %d",&a,&b,&c) +#define sfll(t) scanf("%I64d",&t) +#define sfllll(a,b) scanf("%I64d %I64d",&a,&b) +#define sfllllll(a,b,c) scanf("%I64d %I64d %I64d",&a,&b,&c) +#define sfd(t) scanf("%lf",&t) +#define sfc(c) scanf("%c",&c) +#define sfs(s) scanf("%s",s) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%I64d\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%I64d ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%I64d %I64d\n",a,b) +#define PPLN(a,b) printf("%I64d %I64d ",a,b) + +#define pfi(a) printf("%d\n",a) +#define pfii(a,b) printf("%d %d\n",a,b) +#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c) +#define pfll(a) printf("%I64d\n",a) +#define pfllll(a,b) printf("%I64d %I64d\n",a,b) +#define pfllllll(a,b,c) printf("%I64d %I64d %I64d\n",a,b,c) +#define pfd(a) printf("%lf\n",a) +#define pfs(a) printf("%s\n",a) +#define pfis(a) printf("%d ",a) +#define pflls(a) printf("%I64d ",a) +#define pfds(a) printf("%lf ",a) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<sum) + { + res=sum; + index=i; + } + } + return index; +} +struct Place +{ + string name; +}; +int main() +{ + int n,m,no=0; + while(sii(n,m)) + { + if(n==0&&m==0) return 0; + Place a[100]; + for0(i,n) + { + cin>>a[i].name; + } + cover(M,63); + while(m--) + { + int x,y,cost; + si(x),sii(y,cost); + x--,y--; + M[x][y]=min(M[x][y],cost); + M[y][x]=min(M[y][x],cost); + } + FloydWarshall(n); +// db_mat(M,n,n); + Case(no); + CP(a[Min(n)].name); + } + return 0; +} diff --git a/uva_cpp_clean/11022/11022-12.cpp b/uva_cpp_clean/11022/11022-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..432ab839cb57a984c1d2821324a1fd964087ff29 --- /dev/null +++ b/uva_cpp_clean/11022/11022-12.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; + +#define fastio ios_base::sync_with_stdio(0);cin.tie(0) +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long int +#define S second +#define F first +#define debug1 cout << "debug1" << '\n' +#define debug2 cout << "debug2" << '\n' +#define debug3 cout << "debug3" << '\n' + + +// int dr[] = {0,-1,0,1,-1,-1,1,1}; +// int dc[] = {-1,0,1,0,-1,1,1,-1}; + + +// Maths Utils +int binExp(int a, int b, int m){int r = 1;while (b){if (b % 2 == 1)r = (r * a) % m;a = (a * a) % m;b = b / 2;}return r;} + +int modinv(int x,int m){ + return binExp(x,m-2,m); +} + +int dp[90][90]; + +// this function uses dp with pi function in kmp +int solve(string s,int l,int r){ + if(dp[l][r]!=-1)return dp[l][r]; + if(l == r)return 1; + dp[l][r] = INT_MAX; + + for(int i = l;ir + for(int i = l+1;i<=r;i++){ + int j = pi[i-l-1]; + while(j>0 and s[i]!=s[l+j]) + j = pi[j-1]; + if(s[i]==s[l+j]) + j++; + pi[i-l] = j; + } + + int n = r-l+1; + int m = n - pi[n-1]; + if(n%m == 0) + dp[l][r] = min(dp[l][r],solve(s,l,l+m-1)); + return dp[l][r]; +} + + +int32_t main() +{ + // freopen("input.txt","r",stdin); + // freopen("output.txt","w",stdout); + fastio; + while (1) + { + string s; + cin>>s; + if(s == "*")break; + memset(dp,-1,sizeof dp); + cout< + +using namespace std; + +const int MAX_LEN = 80 + 5; + +int dp(int, int); + +string s; +int memo[MAX_LEN][MAX_LEN]; + +vector Zfunction (const string s) { + int n = (int) s.size(); + vector z(n); + for (int i = 1, l = 0, r = 0; i < n; i++) { + if (i <= r) z[i] = min(r - i + 1, z[i - l]); + while (i + z[i] < n and s[z[i]] == s[i + z[i]]) z[i]++; + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + } + return z; +} + +int getFactorization (int i, int j) { + if (i > j) return 0; + int n = j - i + 1; + string x = s.substr(i, j - i + 1); + vector z = Zfunction(x); + int p = -1; + for (int i = 1; i < n and p == -1; i++) + if (n % i == 0 and z[i] + i == n) p = i; + if (p == -1) return n; + return dp(i, i + p - 1); +} + +int dp (int i, int j) { + if (~memo[i][j]) return memo[i][j]; + int ret = getFactorization(i, j); + for (int k = i; k < j; k++) + ret = min(ret, dp(i, k) + dp(k + 1, j)); + return memo[i][j] = ret; +} + +int main () { + while (cin >> s, s[0] != '*') { + memset(memo, -1, sizeof memo); + cout << dp(0, s.size() - 1) << endl; + } + return (0); +} diff --git a/uva_cpp_clean/11026/11026-21.cpp b/uva_cpp_clean/11026/11026-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8baa3f18e72141e8ec974638b614e9328b1d78b --- /dev/null +++ b/uva_cpp_clean/11026/11026-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11026 + Name: A Grouping Problem + Problem: https://onlinejudge.org/external/110/11026.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long x; + int DP[1001][1001], n, m; + for (int i=0; i<1001; ++i) + DP[i][0] = 1; + + while (cin >> n >> m && (n||m)) { + for (int i=1; i<=n; ++i) { + cin >> x; x %= m; + for (int j=1; j<=i; ++j) + DP[i][j] = (x * DP[i-1][j-1] + DP[i-1][j]) % m; + } + + int mx = 0; + for (int j=1; j<=n; ++j) + mx = max(mx, DP[n][j]); + + cout << mx << '\n'; + } +} diff --git a/uva_cpp_clean/11026/11026-9.cpp b/uva_cpp_clean/11026/11026-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a9c05b3f18e0f270c88bdf63948c5d440f909b0 --- /dev/null +++ b/uva_cpp_clean/11026/11026-9.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +typedef long long ll; + +const int MAX_N = 1000 + 10; + +ll n, m, arr[MAX_N], memo[MAX_N][MAX_N]; + +ll dp () { + for (int i = 0; i <= n; i++) memo[i][0] = 1; + for (int i = 1; i <= n + 1; i++) + for (int j = 0; j <= n; j++) + memo[i][j] = (memo[i - 1][j] + (memo[i - 1][j - 1] * arr[i - 1]) % m) % m; + ll ans = 0; + for (int j = 1; j <= n; j++) ans = max(ans, memo[n][j] % m); + return ans; +} + +int main () { + while (cin >> n >> m, n + m) { + for (int i = 0; i < n; i++) { + cin >> arr[i]; + arr[i] %= m; + } + cout << dp() << endl; + } + return (0); +} diff --git a/uva_cpp_clean/11029/11029-21.cpp b/uva_cpp_clean/11029/11029-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94893e76ca305a7b3cf7458e9dac7d94e9f3de36 --- /dev/null +++ b/uva_cpp_clean/11029/11029-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11029 + Name: Leading and Trailing + Problem: https://onlinejudge.org/external/110/11029.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int powmod(long long a, long long b, int modul) { + if (!b) return 1; + int h = powmod(a, b/2, modul); h *= h; + return (b%2 ? h*a : h) % modul; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + while (T--) { + long long n, k; + cin >> n >> k; + + int digits = ceil(k*log10(n)); + int hi = floor(pow(10, k*log10(n) - (digits - 3))); + int lo = powmod(n, k, 1000); + printf("%03d...%03d\n", hi, lo); + } +} diff --git a/uva_cpp_clean/1103/1103-17.cpp b/uva_cpp_clean/1103/1103-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f02bf1a7b2baa3454b429472aebf462a79238af --- /dev/null +++ b/uva_cpp_clean/1103/1103-17.cpp @@ -0,0 +1,90 @@ +/** + * AOAPC II Example 6-13 Ancient Messages + * ICPC WF 2011 + */ +#include +using namespace std; +vector> pic; +const vector dr { -1, 0, 1, 0 }, + dc { 0, 1, 0, -1 }; +int H, W; +void paint(int i, int j, int color) +{ + int ori = pic[i][j]; + pic[i][j] = color; + for (int x = 0; x < 4; ++x) + { + int r = i + dr[x], c = j + dc[x]; + if (0 <= r && r <= H + 1 && 0 <= c && c <= W + 1 && pic[r][c] == ori) + paint(r, c, color); + } +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + const vector> conv + { + {0, 0, 0, 0}, + {0, 0, 0, 1}, + {0, 0, 1, 0}, + {0, 0, 1, 1}, + {0, 1, 0, 0}, + {0, 1, 0, 1}, + {0, 1, 1, 0}, + {0, 1, 1, 1}, + {1, 0, 0, 0}, + {1, 0, 0, 1}, + {1, 0, 1, 0}, + {1, 0, 1, 1}, + {1, 1, 0, 0}, + {1, 1, 0, 1}, + {1, 1, 1, 0}, + {1, 1, 1, 1} + }; + const string rec = "WAKJSD"; + int K = 1; + while ((cin >> H >> W) && H && W) + { + W *= 4; + pic = vector>(H + 2); + pic.front() = pic.back() = vector(W + 2, 0); + for (int row = 1; row <= H; ++row) + { + auto &line = pic[row]; + line.reserve(W + 2); + line.push_back(0); + string cur; + cin >> cur; + for (char c : cur) + { + int id = isdigit(c) ? c - '0' : c - 'a' + 10; + copy(conv[id].begin(), conv[id].end(), back_inserter(line)); + } + line.push_back(0); + } + paint(0, 0, -1); // background color -1 + int char_color = 2, space_color = -2; + for (int i = 1; i <= H; ++i) + for (int j = 1; j <= W; ++j) + if (pic[i][j] == 1) + paint(i, j, char_color++); + else if (pic[i][j] == 0) + paint(i, j, space_color--); + vector> colors(char_color); + for (int i = 1; i <= H; ++i) + for (int j = 1; j <= W; ++j) + if (pic[i][j] >= 2) + for (int x = 0; x < 4; ++x) + { + int r = i + dr[x], c = j + dc[x]; + if (r >= 1 && r <= H && c >= 1 && c <= W && pic[r][c] < -1) + colors[pic[i][j]].insert(pic[r][c]); + } + string ans = ""; + for (int i = 2; i < char_color; ++i) + ans += rec[colors[i].size()]; + sort(ans.begin(), ans.end()); + cout << "Case " << K++ << ": " << ans << "\n"; + } +} diff --git a/uva_cpp_clean/1103/1103-21.cpp b/uva_cpp_clean/1103/1103-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd1e5eec411fb102c3add21962742b41d4cbff6e --- /dev/null +++ b/uva_cpp_clean/1103/1103-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1103 + Name: Ancient Messages + Problem: https://onlinejudge.org/external/11/1103.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int C[212][212], htoi[127], sz[55555], h, w; +char hexb[16][4], itoh[]="0123456789abcdef", hname[]="WAKJSD", out[55555]; + +bool B[212][212]; +int di[]={0,1,0,-1}, dj[]={1,0,-1,0}; +void dfill(int i, int j, int c) { + if (C[i][j]) return; + C[i][j] = c; + + bool cc = B[i][j]; + for (int k=0; k<4; ++k) { + int ni=i+di[k], nj=j+dj[k]; + if (ni>=0 && ni=0 && nj>h>>w && (h||w); ++cse) { + w <<= 2; + for (int i=0; i> s; + for (int j=0; s[j]; ++j) + memcpy(&B[i][j<<2], hexb[htoi[s[j]]], 4); + } + + for (int i=0; i traversal > flood fill + difficulty: hard + date: 01/Feb/2020 + hint: consider each pixel as a vertex of an implicit graph, then identify each hieroglyph counting the number of white CCs within it + by: @brpapa +*/ +#include +#include +#include +#include +using namespace std; + +#define INF 0x7f7f7f7f +#define WHITE -1 +#define BLACK -2 +#define CHECKED -3 + +int R, C; +int grid[222][222]; + +int dr[4] = {1, 0, -1, 0}; +int dc[4] = {0, 1, 0, -1}; +int floodFill(int r, int c, int lookFor, int replaceBy) { + if (r < 0 || r > R || c < 0 || c > C) + return INF; // não extourou em figura, deve ser prioridade + if (grid[r][c] != lookFor) + return grid[r][c]; // id da figura em que extourou + + grid[r][c] = replaceBy; + + int ans = -1; + for (int d = 0; d < 4; d++) + ans = max(ans, floodFill(r+dr[d], c+dc[d], lookFor, replaceBy)); + return ans; +} + +int main() { + int T = 0; + while (cin >> R >> C) { + if (!R && !C) break; C *= 4; + memset(grid, WHITE, sizeof(grid)); + + string str; + for (int r = 0; r < R; r++) { + cin >> str; + for (int c = 0; c < C/4; c++) { + int mask = stoi(string(1, str[c]), 0, 16); + + for (int i = 0; i < 4; i++) + if (mask & (1 << i)) grid[r][4*c+3-i] = BLACK; + } + } + + // identifica cada figura (borda) com um número >= 0 + int N = 0; + for (int r = 0; r < R; r++) + for (int c = 0; c < C; c++) + if (grid[r][c] == BLACK) + floodFill(r, c, BLACK, N++); + + vector internalCC(N, 0); // internalCC[n] = quantidade de componentes conectados dentro da figura n + for (int r = 0; r < R; r++) + for (int c = 0; c < C; c++) + if (grid[r][c] == WHITE) { + int n = floodFill(r, c, WHITE, CHECKED); + if (n != INF) internalCC[n]++; // não é white do background + } + + vector ans; + for (int qty : internalCC) { + switch (qty) { + case 1: ans.push_back('A'); break; + case 3: ans.push_back('J'); break; + case 5: ans.push_back('D'); break; + case 4: ans.push_back('S'); break; + case 0: ans.push_back('W'); break; + case 2: ans.push_back('K'); break; + } + } + cout << "Case " << (++T) << ": "; + sort(ans.begin(), ans.end()); + for (char c : ans) cout << c; cout << endl; + } + return 0; +} diff --git a/uva_cpp_clean/1103/1103-9.cpp b/uva_cpp_clean/1103/1103-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a44707698734313a55d2fbb09c1252b69b0e777 --- /dev/null +++ b/uva_cpp_clean/1103/1103-9.cpp @@ -0,0 +1,73 @@ +#include + +#define SIZE 210 + +using namespace std; + +int tc, n, m, CC; +char line[SIZE], ch; +int dr[] = {1, 0, -1, 0}; +int dc[] = {0, 1, 0, -1}; +string grid[SIZE]; +string hexa[16] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"}; +string w = "WAKJSD", ans; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < n and C < m and grid[R][C] != '*'); +} + +void floodfill(int r, int c, char ch1, char ch2){ + if( grid[r][c] != ch1 ) return; + grid[r][c] = ch2; + for(int d = 0; d < 4; d++){ + int R = r + dr[d], C = c + dc[d]; + if( is_possible(R, C) ){ + if( ch1 == '1' and grid[R][C] == '0' ) + floodfill(R, C, '0', '*'), CC++; + else + floodfill(R, C, ch1, ch2); + } + } +} + +void clear_(){ + for(int row = 0; row < n; row++) + grid[row].clear(); + ans.clear(); +} + +int main(){ + while(scanf("%d %d\n", &n, &m), n | m){ + for(int it = 0; it < m; it++) + grid[0] += "0000"; + grid[0] += "00"; + for(int row = 1; row <= n; row++){ + scanf("%s", line); + grid[row] += "0"; + for(int it = 0; it < m; it++){ + ch = line[it]; + if(isdigit(ch)) + grid[row] += hexa[ch - '0']; + else + grid[row] += hexa[ch - 'a' + 10]; + } + grid[row] += "0"; + } + for(int it = 0; it < m; it++) + grid[n + 1] += "0000"; + grid[n + 1] += "00"; + m *= 4, m += 2, n += 2; + floodfill(0, 0, '0', '*'); + for(int row = 1; row < n; row++) + for(int col = 1; col < m; col++) + if( grid[row][col] == '1' ){ + CC = 0; + floodfill(row, col, '1', '*'); + ans += w[CC]; + } + sort(ans.begin(), ans.end()); + printf("Case %d: %s\n", ++tc, ans.c_str()); + clear_(); + } + return(0); +} diff --git a/uva_cpp_clean/11034/11034-14.cpp b/uva_cpp_clean/11034/11034-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc8dc49524995d76760e1ce5dade942906c81a5f --- /dev/null +++ b/uva_cpp_clean/11034/11034-14.cpp @@ -0,0 +1,169 @@ +/*************************************************** + * Problem Name : 11034 - Ferry Loading IV.cpp + * Problem Link : https://onlinejudge.org/external/110/11034.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-03-28 + * Problem Type : STL + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + + while (tc--) { + queueleft, right; + int l, m; + cin >> l >> m; + l *= 100; + + for (int i = 0; i < m; i++) { + int x; + string s; + cin >> x >> s; + + if (s == "left") { + left.push (x); + + } else { + right.push (x); + } + } + + int cnt = 0; + bool f = false; + + while (1) { + if (left.empty() && right.empty() ) { + break; + } + + if (!f) { + int sum = 0; + + while (!left.empty() ) { + if (sum + left.front() <= l) { + sum += left.front(); + left.pop(); + + } else { + break; + } + } + + cnt++; + f = true; + + } else { + int sum = 0; + + while (!right.empty() ) { + if (sum + right.front() <= l) { + sum += right.front(); + right.pop(); + + } else { + break; + } + } + + cnt++; + f = false; + } + } + cout << cnt << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/11034/11034-21.cpp b/uva_cpp_clean/11034/11034-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3230e0e68cba9c5759c0fa246eb1a266f8f97e26 --- /dev/null +++ b/uva_cpp_clean/11034/11034-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11034 + Name: Ferry Loading IV + Problem: https://onlinejudge.org/external/110/11034.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MAXN 1000000 +int ql[MAXN], qr[MAXN]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + int T, n, l, cl; + cin>>T; + while (T--) { + cin >> l >> n; l*=100; + int lc=0, rc=0, lcc=0, rcc=0; + for (int i=0; i> cl >> dir; + if (dir[0]=='l') { + if (lc + cl > l) { + lc = 0; + ++lcc; + } + lc += cl; + } + else { + if (rc + cl > l) { + rc = 0; + ++rcc; + } + rc += cl; + } + } + if (lc) ++lcc; + if (rc) ++rcc; + + if (lcc > rcc) cout << 2*lcc-1 << endl; + else cout << 2*rcc << endl; + } +} diff --git a/uva_cpp_clean/11036/11036-9.cpp b/uva_cpp_clean/11036/11036-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff329476abac6a51a1bcafb3a9cfa658b5b77a4d --- /dev/null +++ b/uva_cpp_clean/11036/11036-9.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +const int SIZE = 110, LEN = 110; + +int N, n, m; +long long a, b; +char line[LEN], *p_, v[SIZE][9]; + +int f(long long x){ + stack st; + for(int i = 0; i < m; i++){ + if( v[i][0] == 'x' ) st.push(x % N); + else if( v[i][0] == 'N' ) st.push(N); + else if( isdigit(v[i][0]) ) st.push(atoll(v[i]) % N); + else{ + a = st.top(), st.pop(); + b = st.top(), st.pop(); + if( v[i][0] == '+' ) st.push( ( a + b ) % N); + else if( v[i][0] == '*' ) st.push( ( a * b ) % N); + else return b % N; + } + } +} + +int Brent_Cycle_Finding(long long xo){ + long long p = 1, lambda = 1, tortoise = f(xo), hare = f(f(xo)); + while( tortoise != hare ){ + if( p == lambda ) tortoise = hare, p <<= 1, lambda = 0; + hare = f(hare); + lambda++; + } + return lambda; +} + +int main(){ + while(scanf("%d %d ", &N, &n), N | n){ + cin.getline(line, LEN); + p_ = strtok(line, " "), m = 0; + while(p_) strcpy(v[m++], p_), p_ = strtok(NULL, " "); + printf("%d\n", Brent_Cycle_Finding(n)); + } + return(0); +} diff --git a/uva_cpp_clean/11039/11039-19.cpp b/uva_cpp_clean/11039/11039-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3c2c07d874de069e55d2dbbaa9c82b9ebea1548 --- /dev/null +++ b/uva_cpp_clean/11039/11039-19.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; + +int main() { + int p; + cin >> p; + vector v; + while (p--) { + v.clear(); + int n; + cin >> n; + for (int i = 0; i < n; i++) { + int val; + cin >> val; + if (val < 0) v.push_back(ii(-val, 0)); + else v.push_back(ii(val, 1)); + } + sort(v.begin(), v.end()); + int c1 = 0, c2 = 0; + for (int i = 0; i < n; i++) { + if (v[i].second == c1%2) c1++; + } + for (int i = 0; i < n; i++) { + if (v[i].second != c2%2) c2++; + } + printf("%d\n", max(c1, c2)); + } + return 0; +} diff --git a/uva_cpp_clean/11039/11039-21.cpp b/uva_cpp_clean/11039/11039-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42e79cac702484ea426f725725b30cc2454908a8 --- /dev/null +++ b/uva_cpp_clean/11039/11039-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11039 + Name: Building designing + Problem: https://onlinejudge.org/external/110/11039.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-'); + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +char buf[50]; +void print(long long x) { + int i=48, neg=0; + buf[i--] = '\n'; + if (!x) buf[i--] = '0'; + else if (x < 0) { + x = -x; + neg = 1; + } + for (; x; x/=10) + buf[i--] = x%10 + '0'; + + if (neg) buf[i--] = '-'; + fwrite_unlocked(buf+i+1, 1, 48-i, stdout); +} + +// ------------------------------------------------------------------ + +inline int abs(int x) { return x<0 ? -x : x; } +inline bool comp(int x, int y) { return abs(x) > abs(y); } + +int X[500017]; +int main() { + int T = readInt(); + while (T--) { + int n = readInt(); + for (int i=0; i + + +using namespace std; + +int tc, n, tmp, j, ans; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + vector < pair > v; + for(int i = 0; i < n; i++) scanf("%d", &tmp), v.push_back( make_pair(abs(tmp), (tmp > 0)) ); + sort(v.begin(), v.end()); + ans = 0; + for(int i = 0; i < n;){ + for(j = i; j < n;j++) + if(v[i].second != v[j].second){ + ans++; + break; + } + i = j; + } + if(n != 0) ans++; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11040/11040-21.cpp b/uva_cpp_clean/11040/11040-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf3d00bdf878afb067bfdd766e1a3966484a23bf --- /dev/null +++ b/uva_cpp_clean/11040/11040-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11040 + Name: Add bricks in the wall + Problem: https://onlinejudge.org/external/110/11040.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + int T, X[10][10]; + cin >> T; + while (T--) { + for (int i=0; i<9; i+=2) + for (int j=0; j<=i; j+=2) + cin >> X[i][j]; + + for (int j=1; j<8; j+=2) + X[8][j] = (X[6][j-1] - X[8][j-1] - X[8][j+1])>>1; + + for (int i=7; i>=0; --i) + for (int j=0; j<=i; ++j) + X[i][j] = X[i+1][j] + X[i+1][j+1]; + + for (int i=0; i<9; ++i) { + cout << X[i][0]; + for (int j=1; j<=i; ++j) + cout << ' ' << X[i][j]; + cout << endl; + } + } +} diff --git a/uva_cpp_clean/11040/11040-9.cpp b/uva_cpp_clean/11040/11040-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f273fbd2eabdef8ee3fbf8d3aa9c80c5a7afede7 --- /dev/null +++ b/uva_cpp_clean/11040/11040-9.cpp @@ -0,0 +1,34 @@ +#include + +#define SIZE 15 + +using namespace std; + +int tc, v[SIZE][SIZE]; + +void solve(int i,int j){ + if(v[i][j] != -1) return; + if(v[i + 1][j] != -1 && v[i + 1][j + 1] == -1){ + v[i + 1][j + 1] = (v[i - 1][j] - v[i + 1][j] - v[i + 1][j + 2]) / 2; + v[i][j] = v[i + 1][j] + v[i + 1][j + 1]; + v[i][j + 1] = v[i + 1][j + 1] + v[i + 1][j + 2]; + return; + } +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + memset(v, -1, sizeof v); + for(int i = 1; i <= 5; i++) + for(int j = 1; j <= i; j++) + scanf("%d", &v[2 * i - 1][2 * j - 1]); + for(int i = 2; i <= 8; i++) + for(int j = 1; j <= i; j++) + solve(i, j); + for(int i = 1; i <= 9; i++) + for(int j = 1; j <= i; j++) + printf( j != i ? "%d " : "%d\n", v[i][j]); + } + return(0); +} diff --git a/uva_cpp_clean/11044/11044-18.cpp b/uva_cpp_clean/11044/11044-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0ad3826988d089171d224a890bb02d2aeda0b207 --- /dev/null +++ b/uva_cpp_clean/11044/11044-18.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main(){ + int n, m, cases; + scanf("%d", &cases); + while(cases-- > 0){ + scanf("%d %d", &n, &m); + printf("%d\n", (n/3)*(m/3)); + } + return 0; +} +/* + La estrategia es usar algo de algebra lineal (matrices particionadas), cada sonar maneja un rango de una matriz 3x3, + por lo tanto, lo que nos pide es cuantas matrices particionadas de 3x3 contiene la matriz que representa el rio, + eso se obtiene dividiendo el numero de columnas entre 3, el numero de filas entre 3 y luego multiplicarlos. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/11044/11044-19.cpp b/uva_cpp_clean/11044/11044-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..332b415d3049de4f1025f2c2e6fb3622dd84a317 --- /dev/null +++ b/uva_cpp_clean/11044/11044-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int N; + scanf("%d", &N); + for (int i = 0; i < N; i++) { + int n, m; + scanf("%d %d", &n, &m); + printf("%d\n", (((n-2)/3)+((n-2)%3==0?0:1))*(((m-2)/3)+((m-2)%3==0?0:1))); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11044/11044-21.cpp b/uva_cpp_clean/11044/11044-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07e1cf356e8a856e70c19d653de653beb074e84b --- /dev/null +++ b/uva_cpp_clean/11044/11044-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11044 + Name: Searching for Nessy + Problem: https://onlinejudge.org/external/110/11044.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + while (n--) { + int a, b; + cin >> a >> b; + cout << (a / 3) * (b / 3) << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11044/11044-4.cpp b/uva_cpp_clean/11044/11044-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..124452df5ab684b960e613b2ef8f377aad4eb0a2 --- /dev/null +++ b/uva_cpp_clean/11044/11044-4.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +int main() +{ + int tests; + cin >> tests; + + while (tests--) + { + int n, m; + cin >> n >> m; + int tab[n][m]; + + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + tab[i][j] = 0; + + int sonars = 0; + for (int i = 1; i < n - 1; i += 3) + { + for (int j = 1; j < m - 1; j += 3) + { + if (tab[i][j] == 0) + { + sonars++; + tab[i][j] = 1; + tab[i][j + 1] = 1; + tab[i][j - 1] = 1; + + tab[i - 1][j] = 1; + tab[i - 1][j + 1] = 1; + tab[i - 1][j - 1] = 1; + + tab[i + 1][j] = 1; + tab[i + 1][j + 1] = 1; + tab[i + 1][j - 1] = 1; + } + } + } + + cout << sonars << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11044/11044-5.cpp b/uva_cpp_clean/11044/11044-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f2f64124c437dd6778d81d290db065bb67c34f37 --- /dev/null +++ b/uva_cpp_clean/11044/11044-5.cpp @@ -0,0 +1,11 @@ +#include + int main() + { + int t,n,m; + scanf("%d",&t); + while(t--) + { + scanf("%d %d",&n,&m); + printf("%d\n",(n/3)*(m/3)); + } + } diff --git a/uva_cpp_clean/11044/11044-9.cpp b/uva_cpp_clean/11044/11044-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da89c2cb010cde3b07422ce76074d6e6c9d87fb9 --- /dev/null +++ b/uva_cpp_clean/11044/11044-9.cpp @@ -0,0 +1,12 @@ +#include + +int main(){ + int n, a, b, ans; + scanf("%d", &n); + while(n--){ + scanf("%d %d", &a, &b); + ans = ( a / 3 ) * ( b / 3 ); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11044/main.cpp b/uva_cpp_clean/11044/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b46c09e0e0c025b6be271d49ccc0d98abc67f980 --- /dev/null +++ b/uva_cpp_clean/11044/main.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +int main() { + + int t, m, n; + cin >> t; + for(t; t > 0; t--){ + cin >> m >> n; + m += -2; + n += -2; + m = (int) ceil(m/3.0); + n = (int) ceil(n/3.0); + cout << m*n << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11045/11045-19.cpp b/uva_cpp_clean/11045/11045-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9ca91ef6601b832af9bc72e9fd132e92e1b6323 --- /dev/null +++ b/uva_cpp_clean/11045/11045-19.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +int N, M, mf, f, s, t, INF = 2000000; +int res[40][40]; +vi p; + +void augment(int v, int minEdge) { + if (v == s) { + f = minEdge; + return; + } else if (p[v] != -1) { + augment(p[v], min(minEdge, res[p[v]][v])); + res[p[v]][v] -= f; res[v][p[v]] += f; + } +} + +inline int toi(string s) { + if (s == "XXL") return 1; + if (s == "XL") return 2; + if (s == "L") return 3; + if (s == "M") return 4; + if (s == "S") return 5; + if (s == "XS") return 6; + return -1; +} + +int main() { + int T; + cin >> T; + while (T--) { + cin >> N >> M; + s = 0, t = 7 + M; + for (int i = 0; i < 8 + M; i++) + for (int j = 0; j < 8 + M; j++) + res[i][j] = 0; + for (int i = 1; i <= 6; i++) res[0][i] = res[i][0] = N/6; + for (int i = 0; i < M; i++) { + string s1, s2; + int s_1, s_2; + cin >> s1 >> s2; + s_1 = toi(s1), s_2 = toi(s2); + res[s_1][i+7] = res[i+7][s_1] = 1; + res[s_2][i+7] = res[i+7][s_2] = 1; + res[t][i+7] = res[i+7][t] = 1; + } + N = 8 + M; + mf = 0; + while (true) { + f = 0; + vi dist(N, INF); dist[s] = 0; + queue q; + q.push(s); + p.assign(N, -1); + while (!q.empty()) { + int u = q.front(); q.pop(); + if (u == t) break; + for (int v = 0; v < N; v++) { + if (res[u][v] > 0 && dist[v] == INF) { + dist[v] = dist[u] + 1, q.push(v), p[v] = u; + } + } + } + augment(t, INF); + if (f == 0) break; + mf += f; + } + if (mf == M) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11045/11045-21.cpp b/uva_cpp_clean/11045/11045-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5f1cbb854e3c1c57057f3590f6a9a247d1db137 --- /dev/null +++ b/uva_cpp_clean/11045/11045-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11045 + Name: My T-shirt suits me + Problem: https://onlinejudge.org/external/110/11045.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; +#define MAXN 100 +#define inf 1000000000 + +int max_flow(int n,int mat[][MAXN],int source,int sink,int flow[][MAXN]){ + int pre[MAXN],que[MAXN],d[MAXN],p,q,t,i,j; + if (source==sink) return inf; + for (i=0;i0) + flow[pre[i]-1][i]+=d[sink],i=pre[i]-1; + else + flow[i][-pre[i]-1]-=d[sink],i=-pre[i]-1; + } + for (j=i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11047 + Name: The Scrooge Co Problem + Problem: https://onlinejudge.org/external/110/11047.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +string names[143]; +int D[143][143], P[143][143]; +void printPath(int u, int v) { + if (P[u][v] == u) { + cout << names[u] << ' '; + return; + } + printPath(u, P[u][v]); + printPath(P[u][v], v); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string s1, s2, emp; + int T, n, m; + cin >> T; + while (T--) { + cin >> n; + unordered_map ids; + for (int i=0; i> names[i]; + ids[names[i]] = i; + } + + for (int i=0; i> D[i][j]; + P[i][j] = i; + } + + for (int k=0; k> m; + while (m--) { + cin >> emp >> s1 >> s2; + int u = ids[s1], + v = ids[s2]; + if (D[u][v] == -1) + cout << "Sorry Mr " << emp << " you can not go from " << s1 << " to " << s2 << endl; + + else { + cout << "Mr " << emp << " to go from " << s1 << " to " << s2 << ", you will receive " << D[u][v] << " euros\n"; + cout << "Path:"; + printPath(u, v); + cout << s2 << endl; + } + } + } +} diff --git a/uva_cpp_clean/11048/11048-21.cpp b/uva_cpp_clean/11048/11048-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96febc7964bbde25ef6cbf14630f4e620f59ca7b --- /dev/null +++ b/uva_cpp_clean/11048/11048-21.cpp @@ -0,0 +1,112 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11048 + Name: Automatic Correction of Misspellings + Problem: https://onlinejudge.org/external/110/11048.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define NAN 1073741824 + +struct Node { + int id; + Node* childs[26]; + Node(): id(NAN) { + memset(childs, 0, sizeof(childs)); + } + ~Node() { + for (int i=0; i<26; i++) + if (childs[i]) + delete childs[i]; + } +}; + +Node* root; +int insertWord(const char str[], int nid) { + Node *cur = root; + for (int i=0; str[i]; i++) { + int ch = str[i]-'a'; + if (!cur->childs[ch]) + cur->childs[ch] = new Node(); + cur = cur->childs[ch]; + } + if (cur->id == NAN) + cur->id = nid; + + return cur->id; +} + +int findWord(const char str[], Node *cur=root) { + if (!cur) return NAN; + for (int i=0; str[i]; i++) { + cur = cur->childs[str[i]-'a']; + if (!cur) return NAN; + } + return cur->id; +} + +int findFuzzy(const char str[]) { + int bid = NAN; + Node *cur = root; + for (int i=0; cur; i++) { + // missing + for (int j=0; j<26; j++) + bid = min(bid, findWord(str+i, cur->childs[j])); + + if (!str[i]) break; + + // too much + bid = min(bid, findWord(str+i+1, cur)); + + // letter is wrong + for (int j=0; j<26; j++) + if ('a'+j != str[i]) + bid = min(bid, findWord(str+i+1, cur->childs[j])); + + // swap adjacent + if (str[i+1]) { + Node *cur2 = cur->childs[str[i+1]-'a']; + if (cur2) + bid = min(bid, findWord(str+i+2, cur2->childs[str[i]-'a'])); + } + + cur = cur->childs[str[i]-'a']; + } + return bid; +} + + +char words[10017][31]; +int main() { + root = new Node(); + + int n, q; + char word[100]; + scanf("%d", &n); + for (int i=0; i + +using namespace std; + +const int SIZE = 10010, LEN = 30; + +int n, q, l, ct, len[SIZE]; +char w[SIZE][LEN], word[LEN], s[LEN], correct[LEN]; +set st; +bool ok; + +bool check1(int id){ + if( abs(l - len[id]) != 1 ) return false; + for(int k = 0; k < max(l, len[id]); k++){ + ok = true; + for(int i = 0, j = 0; i < l and j < len[id] and ok; i++, j++) + if( l < len[id] and j == k ) i--; + else if( l > len[id] and i == k ) j--; + else if( word[i] != w[id][j] ) ok = false; + if(ok) return true; + } + return false; +} + +bool check2(int id){ + if( l != len[id] ) return false; + ct = 0; + for(int i = 0; i < l; i++) + if( word[i] != w[id][i] ) ct++; + return ( ct == 1 ); +} + +bool check3(int id){ + if( l != len[id] ) return false; + vector idx; + for(int i = 0; i < l; i++) + if( word[i] != w[id][i] ) idx.push_back(i); + if( idx.size() != 2 ) return false; + int a = idx[0], b = idx[1]; + return ( a + 1 == b and word[a] == w[id][b] and word[b] == w[id][a] ); +} + +bool misspelling(){ + l = strlen(word); + for(int it = 0; it < n; it++){ + if( check1(it) or check2(it) or check3(it) ){ + strcpy(correct, w[it]); + return true; + } + } + return false; +} + +int main(){ + scanf("%d\n", &n); + for(int it = 0; it < n; it++){ + scanf("%s", w[it]); + len[it] = strlen(w[it]); + st.insert(w[it]); + } + scanf("%d\n", &q); + for(int it = 0; it < q; it++){ + scanf("%s", word); + if( st.find(word) != st.end() ) printf("%s is correct\n", word); + else if( misspelling() ) printf("%s is a misspelling of %s\n", word, correct); + else printf("%s is unknown\n", word); + } + return(0); +} diff --git a/uva_cpp_clean/11049/11049-21.cpp b/uva_cpp_clean/11049/11049-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63a84c182ca592bdf92a7fd93a5638b98105214a --- /dev/null +++ b/uva_cpp_clean/11049/11049-21.cpp @@ -0,0 +1,103 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11049 + Name: Basic wall maze + Problem: https://onlinejudge.org/external/110/11049.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct State { + int x, y; + State(int x, int y):x(x),y(y){} +}; + +char S[7][7]; +void printPath(int sx, int sy, int tx, int ty) { + if (sx==tx && sy==ty) return; + switch (S[tx][ty]) { + case 'E': + printPath(sx, sy, tx-1, ty); + break; + case 'S': + printPath(sx, sy, tx, ty-1); + break; + case 'W': + printPath(sx, sy, tx+1, ty); + break; + case 'N': + printPath(sx, sy, tx, ty+1); + break; + } + cout << S[tx][ty]; +} + + +bool RB[7][7], DB[7][7]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int sx, sy, tx, ty; + while (cin >> sx >> sy && (sx||sy)) { + cin >> tx >> ty; + + memset(S, 0, sizeof(S)); + memset(DB, 0, sizeof(DB)); + memset(RB, 0, sizeof(RB)); + for (int i=0; i<3; ++i) { + int x1, x2, y1, y2; + cin >> x1 >> y1 >> x2 >> y2; + if (y1 == y2) { + if (x1 > x2) + swap(x1, x2); + for (int x=x1+1; x<=x2; ++x) + DB[x][y1] = 1; + } + + if (x1 == x2) { + if (y1 > y2) + swap(y1, y2); + for (int y=y1+1; y<=y2; ++y) + RB[x1][y] = 1; + } + } + + queue q; + q.push(State(sx, sy)); + S[sx][sy] = 1; + while (!q.empty()) { + State s = q.front(); q.pop(); + if (s.x == tx && s.y == ty) + break; + + if (s.x>1 && !S[s.x-1][s.y] && !RB[s.x-1][s.y]) { + q.push(State(s.x-1, s.y)); + S[s.x-1][s.y] = 'W'; + } + + if (s.y>1 && !S[s.x][s.y-1] && !DB[s.x][s.y-1]) { + q.push(State(s.x, s.y-1)); + S[s.x][s.y-1] = 'N'; + } + + if (s.x<6 && !S[s.x+1][s.y] && !RB[s.x][s.y]) { + q.push(State(s.x+1, s.y)); + S[s.x+1][s.y] = 'E'; + } + + if (s.y<6 && !S[s.x][s.y+1] && !DB[s.x][s.y]) { + q.push(State(s.x, s.y+1)); + S[s.x][s.y+1] = 'S'; + } + } + + printPath(sx, sy, tx, ty); + cout << endl; + } +} diff --git a/uva_cpp_clean/11053/11053-19.cpp b/uva_cpp_clean/11053/11053-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31762a1a1ed28655dead0b8b3eda4c77817c34d0 --- /dev/null +++ b/uva_cpp_clean/11053/11053-19.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +using namespace std; + +unordered_map soldiers; + +int main() { + int N; + scanf("%d", &N); + while (N != 0){ + int a, b; + scanf("%d %d", &a, &b); + soldiers.clear(); + int i = 0; + unsigned long next = 0; + while (true) { + if (soldiers.count(next)) break; + else soldiers.insert(pair(next, i)); + i++; + next = (((a*next)%N)*next + b) % N; + } + printf("%d\n", N - (i-soldiers[next])); + scanf("%d", &N); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11053/11053-9.cpp b/uva_cpp_clean/11053/11053-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8919e7af6b6208550cab4a69ad84723e5b7b9f7e --- /dev/null +++ b/uva_cpp_clean/11053/11053-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int N, a, b; + +inline int f(int xo){ + xo %= N; + return ( ( ( 1LL * a * xo ) % N ) * xo % N + b ) % N; +} + +int Brent_Cycle_Finding(int xo){ + int p = 1, lambda = 1, tortoise = xo, hare = f(xo); + while( tortoise != hare ){ + if( p == lambda ) tortoise = hare, p <<= 1, lambda = 0; + hare = f(hare); + lambda++; + } + return lambda; +} + +int main(){ + while(scanf("%d %d %d", &N, &a, &b) == 3){ + a %= N, b %= N; + printf("%d\n", N - Brent_Cycle_Finding(0)); + } + return(0); +} diff --git a/uva_cpp_clean/11054/11054-19.cpp b/uva_cpp_clean/11054/11054-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6c3382e7eee57c646742f4e10c4873152c3cff6 --- /dev/null +++ b/uva_cpp_clean/11054/11054-19.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +//long long wine[100010]; + +int main() { + int n; + while (cin >> n && n != 0) { + /*for (int i = 0; i < n; i++) { + cin >> wine[i]; + } + long long acum = 0; + for (int i = 0; i < n - 1; i++) { + wine[i+1] += wine[i]; + acum += abs(wine[i]); + }*/ + long long acum = 0; + long long wine; + cin >> wine; + for (int i = 1; i < n; i++) { + acum += abs(wine); + long long auxi; + cin >> auxi; + wine += auxi; + } + printf("%lld\n", acum); + } + return 0; +} diff --git a/uva_cpp_clean/11054/11054-21.cpp b/uva_cpp_clean/11054/11054-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7279f3dce91648a9e038ec4497b04e4e70e9d05d --- /dev/null +++ b/uva_cpp_clean/11054/11054-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11054 + Name: Wine trading in Gergovia + Problem: https://onlinejudge.org/external/110/11054.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline long long readInt() { + char ch; + long long sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) return EOF; + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +// --------------------------------------------------------------------------- + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while ((n=readInt()) > 0) { + long long b = readInt(), s = 0; + for (int i=1; i +#include +#include +#include +using namespace std; + +struct pilot{ + string name, upper; + int min, sec, ms; + bool operator<(const pilot & rhs) const { + if (min > rhs.min) return true; + else if (min < rhs.min) return false; + else { + if (sec > rhs.sec) return true; + else if (sec < rhs.sec) return false; + else { + if (ms > rhs.ms) return true; + else if (ms < rhs.ms) return false; + else { + return upper > rhs.upper; + } + } + } + return false; + } +}; + +int main() { + int N; + priority_queue grid; + while (cin >> N) { + while (N--) { + pilot p; + string trash; + cin >> p.name >> trash >> p.min >> trash >> p.sec >> trash >> p.ms >> trash; + p.upper = p.name; + for (int i=0; i < p.upper.length(); i++) p.upper[i] = toupper(p.upper[i]); + grid.push(p); + } + int row = 1; + while (!grid.empty()) { + cout << "Row " << row << endl; + cout << grid.top().name << endl; + grid.pop(); + if (!grid.empty()) { + cout << grid.top().name << endl; + grid.pop(); + } + row++; + } + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11056/11056-21.cpp b/uva_cpp_clean/11056/11056-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f2f36c662106d8b5fa6afe21e36af17a329a336 --- /dev/null +++ b/uva_cpp_clean/11056/11056-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11056 + Name: Formula 1 + Problem: https://onlinejudge.org/external/110/11056.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +struct Pilot { + int tm; + char name[30]; + bool operator < (const Pilot &o) const { + if (tm != o.tm) return tm < o.tm; + return strcasecmp(name, o.name) < 0; + } +}; + +Pilot ps[200]; + +int main(){ + int n, mn, sc, ms; + while (scanf("%d", &n)==1) { + for (int i=0; i>1)+1); + printf("%s\n", ps[i].name); + if (++i < n) + printf("%s\n", ps[i].name); + } + putchar('\n'); + } +} diff --git a/uva_cpp_clean/11056/11056-9.cpp b/uva_cpp_clean/11056/11056-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..982387412bef11d65ff6e6ce31a95b11155b2481 --- /dev/null +++ b/uva_cpp_clean/11056/11056-9.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +const int SIZE = 110, LEN = 100; + +struct Pilot{ + char name[LEN], s[LEN]; + int min, sec, ms; + Pilot() {}; + void to_lower(){ + strcpy(s, name); + for(int it = 0; s[it]; it++) + s[it] = tolower(s[it]); + } + bool operator < (Pilot& other){ + if( min != other.min ) return min < other.min; + if( sec != other.sec ) return sec < other.sec; + if( ms != other.ms ) return ms < other.ms; + return strcmp(s, other.s) < 0; + } +}; + +int n; +char line[LEN]; +Pilot v[SIZE]; + +int main(){ + while(~scanf("%d\n", &n)){ + for(int it = 0; it < n; it++){ + cin.getline(line, LEN); + sscanf(line, "%s : %d min %d sec %d ms", v[it].name, &v[it].min, &v[it].sec, &v[it].ms); + v[it].to_lower(); + } + sort(v, v + n); + for(int it = 0; it < n; it++){ + if( it % 2 == 0 ) printf("Row %d\n", it / 2 + 1); + printf("%s\n", v[it].name); + } + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/11057/11057-11.cpp b/uva_cpp_clean/11057/11057-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1bbbee0a60714c44e3122863e886eee6bb18d6ef --- /dev/null +++ b/uva_cpp_clean/11057/11057-11.cpp @@ -0,0 +1,84 @@ +#include +/* +Problem: 11057 - Exact Sum +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1998 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N, M, prices[10001]; +int I, J; + +int lower_bound_search(int v){ + + int idx = 0; + for (int b = N-1; b >= 1; b /= 2){ + while (idx+b < N && prices[idx+b] <= v) idx += b; + } + + return idx; +} + +int main () { + + while ( scanf("%d\n", &N) != EOF ){ + int I = 0, J = 0; + + for (int i = 0; i < N; i++) + scanf("%d", &prices[i]); + + scanf("%d\n", &M); + sort(prices, prices+N); + + // search for the lower bound index to M/2 + int start_idx = lower_bound_search(M/2); + + for (int i = start_idx+1; i >= 0; i--){ + + int A = lower_bound_search(M-prices[i]); + + if (prices[A] == M-prices[i] && A != i){ + I = min(prices[A], M-prices[A]); + J = M-I; + break; + } + } + + printf("Peter should buy books whose prices are %d and %d.\n\n", I, J); + } + + return 0; +} +/* +Sample input:- +----------------- +2 +40 40 +80 + +5 +10 2 6 8 4 +10 + +Sample output:- +----------------- +Peter should buy books whose prices are 40 and 40. + +Peter should buy books whose prices are 4 and 6. + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11057/11057-13.cpp b/uva_cpp_clean/11057/11057-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df6d29b63ce1954ae072e1d03f2b2bc6bcdd3f7f --- /dev/null +++ b/uva_cpp_clean/11057/11057-13.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int n, sum = 0; + + while (cin >> n) + { + int minimum = 100000000; + + int one = 0, two = 0; + + vector price; + + for (int i = 0; i < n; i++) + { + int x; + + cin >> x; + + price.push_back(x); + } + + cin >> sum; + + sort(price.begin(), price.end()); + + while (!price.empty()) + { + int first = price.front(); + + int second = sum - first; + + price.erase(price.begin()); + + vector :: iterator pos = find(price.begin(), price.end(), second); + + if (pos != price.end()) + { + if (abs(first - second) < minimum) + { + one = min(first, second); + two = max(first, second); + + minimum = abs(first - second); + } + } + } + + printf("Peter should buy books whose prices are %d and %d.\n\n", one, two); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11057/11057-14.cpp b/uva_cpp_clean/11057/11057-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3145bdf2b4c8f44b7a449276a598ae1307cdfdd1 --- /dev/null +++ b/uva_cpp_clean/11057/11057-14.cpp @@ -0,0 +1,84 @@ +/*************************************************** + * Problem name : 11057 Exact Sum.cpp + * Problem Link : https://uva.onlinejudge.org/external/110/11057.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-25 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,0,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 10003 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ll N, M, ar[MAX]; + mapmp; + //~ ll mp[MAX]; + while (scanf("%lld", &N) == 1) { + //~ SET(mp); + for (int i = 0; i < N; i++) { + scanf("%lld", &ar[i]); + mp[ar[i]] = 1; + } + scanf("%lld", &M); + sort(ar, ar + N); + ll a, b, mn = 10000000; + for (int i = 0; i < N; i++) { + ll ans = M - ar[i]; + ll dif = abs(ans - ar[i]); + if (mp[ans] == 1 && (dif < mn)) { + a = ar[i]; + b = ans; + mn = dif; + } + } + printf("Peter should buy books whose prices are %lld and %lld.\n", a, b); + nl; + mp.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/11057/11057-19.cpp b/uva_cpp_clean/11057/11057-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73502e5a25d06f3151556c111337a6f476468efc --- /dev/null +++ b/uva_cpp_clean/11057/11057-19.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; + +int main() { + int N; + vector v; + while (cin >> N) { + v.assign(N, 0); + for (int i = 0; i < N; i++) { + cin >> v[i]; + } + sort(v.begin(), v.end()); + int M; + cin >> M; + auto low = lower_bound(v.begin(), v.end(), M/2); + while (low != v.end()) { + bool ok = binary_search(v.begin(), low, M - (*low)); + if (ok) { + printf("Peter should buy books whose prices are %d and %d.\n\n", M - (*low), (*low)); + break; + } + low++; + } + } + return 0; +} diff --git a/uva_cpp_clean/11057/11057-21.cpp b/uva_cpp_clean/11057/11057-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..610459985ba1a9204e70db4565750f98566e0737 --- /dev/null +++ b/uva_cpp_clean/11057/11057-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11057 + Name: Exact Sum + Problem: https://onlinejudge.org/external/110/11057.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int nums[10005]; +int find(int n, int m) { + int k = lower_bound(nums, nums + n, m >> 1) - nums; + if (nums[k] << 1 == m) + if (nums[k + 1] == nums[k]) + return nums[k]; + else k--; + + for (int i = k; i >= 0; i--) + if (binary_search(nums, nums + n, m - nums[i])) + return nums[i]; + return 0; +} + + +int main() { + int n, m; + while (cin >> n) { + for (int i = 0; i < n; i++) + cin >> nums[i]; + cin >> m; + sort(nums, nums + n); + int res = find(n, m); + res = min(res, m - res); + printf("Peter should buy books whose prices are %d and %d.\n\n", res, m - res); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11057/11057-5.cpp b/uva_cpp_clean/11057/11057-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..516f697a19d538488c8b3a60589b761b486de495 --- /dev/null +++ b/uva_cpp_clean/11057/11057-5.cpp @@ -0,0 +1,122 @@ +/****************************************************************** +*** Problewm : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define CP(a) cout<=0;i--) + { + if(a[i]dis) + { + book1=res1; + book2=res2; + ans=dis; + } + } + cout<<"Peter should buy books whose prices are "< + +using namespace std; + +int n, m, min_dif; +vector a; +pair ans; + +int main(){ + while(scanf("%d", &n) == 1){ + a.resize(n); + for(int i = 0; i < n; i++) scanf("%d", &a[i]); + sort(a.begin(), a.end()); + scanf("%d", &m); + min_dif = INT_MAX; + for(int i = 0; i < n; i++){ + if(abs(a[i] - (m - a[i])) < min_dif && binary_search(a.begin(), a.end(), m - a[i])) + ans = make_pair(a[i], m - a[i]), min_dif = abs(a[i] - (m - a[i])); + } + printf("Peter should buy books whose prices are %d and %d.\n\n", ans.first, ans.second); + a.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11059/11059-17.cpp b/uva_cpp_clean/11059/11059-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e869cd4806543ed0b3b6538c7157f20a7b436753 --- /dev/null +++ b/uva_cpp_clean/11059/11059-17.cpp @@ -0,0 +1,33 @@ +/** + * Maximum product + * AOAPC II Example 7-2 + */ +#include +using namespace std; +int main() +{ + int n, T = 1; + while (cin >> n) + { + long long cur = 0, neg_cur = 0, ans = 0; + while (n--) + { + int i; + cin >> i; + if (i) + { + cur = cur ? cur * i : i; + neg_cur *= i; + if (i < 0) + swap(neg_cur, cur); + ans = max(ans, cur); + } + else + { + cur = 0; + neg_cur = 0; + } + } + cout << "Case #" << T++ << ": The maximum product is " << ans << ".\n\n"; + } +} diff --git a/uva_cpp_clean/11059/11059-19.cpp b/uva_cpp_clean/11059/11059-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dba12117026bdc08b7b512cb70cfba003166b14b --- /dev/null +++ b/uva_cpp_clean/11059/11059-19.cpp @@ -0,0 +1,22 @@ +#include +#include +using namespace std; + +int main() { + int N, c = 1; + long long S[20]; + while (cin >> N) { + long long maxi = 0; + for (int i = 0; i < N; i++) cin >> S[i], maxi = max(maxi, S[i]); + for (int i = 0; i < N; i++) { + long long prod = S[i]; + for (int j = i+1; j < N; j++) { + prod *= S[j]; + if (prod > maxi) maxi = prod; + } + } + printf("Case #%d: The maximum product is %lld.\n\n", c, maxi); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/11059/11059-20.cpp b/uva_cpp_clean/11059/11059-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47c7d66fa0b76027d611ec7296c0f0759279e1e4 --- /dev/null +++ b/uva_cpp_clean/11059/11059-20.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +int S[20]; + +int main() +{ + int N, ncase; + + ncase = 0; + while( cin >> N ) + { + for(int i=0 ; i> S[i]; + + long long P = 0; + for(int i=0 ; iP ) P = temp; + if( temp==0 ) break; + } + } + + cout << "Case #" << ++ncase << ": The maximum product is " << P << ".\n" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11059/11059-21.cpp b/uva_cpp_clean/11059/11059-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f06b3b71ccb715f74c74c50c1d58eedf4e7293a1 --- /dev/null +++ b/uva_cpp_clean/11059/11059-21.cpp @@ -0,0 +1,66 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11059 + Name: Maximum Product + Problem: https://onlinejudge.org/external/110/11059.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int abs(int x) { + return x<0?-x:x; +} + +long long max(long long a, long long b) { + return a>b?a:b; +} + +int main(){ + int cse = 1, + n, x[20], negs[20]; + long long p[20] = {1}; + + while (cin>>n) { + long long mx = 0; + + bool done = 0; + while (!done) { + int nn = n, + ncnt = 0; + + done = 1; + for (int i=1; i<=n; i++) { + cin>>x[i]; + if (!x[i]) { + done = 0; + nn = i-1; + n -= i; + break; + } + + p[i] = p[i-1]*abs(x[i]); + if (x[i]<0) negs[ncnt++] = i; + } + + if (nn>0) { + if (!(ncnt&1)) mx = max(mx, p[nn]); + else for (int i=0; i1) + mx = max(mx, p[ind-1]); + if (ind < nn) + mx = max(mx, p[nn]/p[ind]); + } + } + } + + printf("Case #%d: The maximum product is %lld.\n\n", cse++, mx); + } +} diff --git a/uva_cpp_clean/11059/11059-9.cpp b/uva_cpp_clean/11059/11059-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be0730ce7f160e7d6b509f6fab15c98da126c4d3 --- /dev/null +++ b/uva_cpp_clean/11059/11059-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +long long n, v[20], _max, tmp, tc; + +int main(){ + while(scanf("%lld",&n) == 1){ + _max = 0; + for(int i = 0; i < n; i++) scanf("%lld", &v[i]); + for(int i = 0; i < n; i++){ + tmp = v[i]; + _max = max(_max, tmp); + for(int j = i + 1; j < n; j++){ + tmp *= v[j]; + _max = max(_max, tmp); + } + } + printf("Case #%lld: The maximum product is %lld.\n\n", ++tc, _max); + } + return(0); +} diff --git a/uva_cpp_clean/11060/11060-13.cpp b/uva_cpp_clean/11060/11060-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a7b373cfe5d746d56ca8a5cab77c95b4bf382ae --- /dev/null +++ b/uva_cpp_clean/11060/11060-13.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, m, test = 1; +queue ans; +vector visited; +map nam; +map inDegree; +vector names; +vector < vector > graph; + +int main() +{ + while (cin >> n) + { + names.clear(); + + names.resize(n + 1); + + graph.clear(); + + graph.resize(n + 1); + + visited.clear(); + + visited.resize(n + 1, 0); + + for (int i = 1; i <= n; i++) + { + string s; + + cin >> s; + + names[i] = s; + + nam[s] = i; + + inDegree[s] = 0; + } + + cin >> m; + + for (int i = 0; i < m; i++) + { + string v, w; + + cin >> v >> w; + + graph[nam[v]].push_back(nam[w]); + + inDegree[w]++; + } + + for (int i = 0; i < n; i++) + { + map ::iterator it; + + for (it = inDegree.begin(); it != inDegree.end(); it++) + { + int number = nam[it->first]; + + string child = it->first; + + if (it->second == 0 && !visited[number]) + { + visited[number] = 1; + + ans.push(it->first); + + for (int j = 0; j < (int)graph[number].size(); j++) + { + inDegree[names[graph[number][j]]]--; + } + + break; + } + } + } + + cout << "Case #" << test++ << ": " << "Dilbert should drink beverages in this order:"; + + while (!ans.empty()) + { + cout << " " << ans.front(); + + ans.pop(); + } + + cout << "." << endl << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11060/11060-14.cpp b/uva_cpp_clean/11060/11060-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..788f980d758134bc6b129b68777207879d27a34a --- /dev/null +++ b/uva_cpp_clean/11060/11060-14.cpp @@ -0,0 +1,84 @@ +/*************************************************** + * Problem name : 11060 - Beverages.cpp + * OJ : Uva + * Result : AC + * Date : 00-03-17 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 105 +using namespace std; +typedef long long ll; +vectoradj[MAX]; +mapmp; +int indegree[MAX], taken[MAX]; +vectorlist; +int N; +string str[MAX]; +void topsort() { + for (int i = 1; i <= N; i++) { + int j ; + for (j = 1; i <= N; j++) { + if (!indegree[j] && !taken[j]) { + taken[j] = 1; + list.push_back(str[j]); + int sz = adj[j].size(); + for (int k = 0; k < sz; k++) { + indegree[adj[j][k]]--; + } + indegree[j]--; + break; + } + } + } +} +void mem() { + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + taken[i] = 0; + indegree[i] = 0; + } + mp.clear(); + list.clear(); +} +int main () { + int M, t = 1; + while (scanf("%d", &N) == 1 ) { + for (int i = 1; i <= N; i++) { + cin >> str[i]; + mp[str[i]] = i; + } + cin >> M; + for (int i = 1; i <= M; i++) { + string u, v; + cin >> u >> v; + adj[mp[u]].push_back(mp[v]); + indegree[mp[v]]++; + } + topsort(); + printf("Case #%d: Dilbert should drink beverages in this order: ", t++); + for (int i = 0; i < N; i++) { + if (i == N-1) { + cout << list[i] << "." << endl< +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef vector vvi; + +unordered_map m; +string ts[100]; + +bool kahn(vvi &adjList, vi &topo) { + vi inDeg(adjList.size(), 0); + for (int u = 0; u < adjList.size(); u++) + for (int v = 0; v < adjList[u].size(); v++) + inDeg[adjList[u][v]]++; + priority_queue q; + for (int u = 0; u < inDeg.size(); u++) + if (inDeg[u] == 0) q.push(-u); + while (!q.empty()) { + int u = -q.top(); q.pop(); + topo.push_back(u); + for (int v = 0; v < adjList[u].size(); v++) + if (--inDeg[adjList[u][v]] == 0) q.push(-adjList[u][v]); + } + for (int u = 0; u < inDeg.size(); u++) + if (inDeg[u] != 0) return true; + return false; +} + +int main() { + int N, c = 1; + while (cin >> N) { + m.clear(); + for (int i = 0; i < N; i++) { + cin >> ts[i]; + m[ts[i]] = i; + } + int M; + cin >> M; + vvi adjList(N, vi()); + for (int i = 0; i < M; i++) { + string s1, s2; + cin >> s1 >> s2; + adjList[m[s1]].push_back(m[s2]); + } + vi topo; + kahn(adjList, topo); + printf("Case #%d: Dilbert should drink beverages in this order:", c); + for (auto it = topo.begin(); it != topo.end(); ++it) { + printf(" %s", ts[*it].c_str()); + } + printf(".\n\n"); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/11060/11060-21.cpp b/uva_cpp_clean/11060/11060-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f554fd02bac045270cf8e3d63b31ed8576a41f79 --- /dev/null +++ b/uva_cpp_clean/11060/11060-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11060 + Name: Beverages + Problem: https://onlinejudge.org/external/110/11060.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +string names[110]; +map ids; + +list adj[110]; +int ins[110], order[110]; + +int main(){ + int cse=1, n, m; + string s1, s2; + while (cin>>n) { + for (int i=0; i>names[i]; + ids[names[i]] = i; + adj[i].clear(); + ins[i] = 0; + } + + cin>>m; + for (int i=0; i>s1>>s2; + adj[ids[s1]].push_back(ids[s2]); + ins[ids[s2]]++; + } + + + priority_queue, greater > q; + for (int i=0; i +#include + +using namespace std; + +struct Drink +{ + string name; + + vector strongerThan; + + Drink(string n) + { + name = n; + } +}; + +Drink *Find(vector &drinks, string name) +{ + for (unsigned int i = 0; i < drinks.size(); i++) + { + if (drinks[i].name == name) + { + return &drinks[i]; + } + } + + return NULL; +} + +int main() +{ + int drinkCount, caseNr = 0; + + while (cin >> drinkCount) + { + vector drinks; + + for (int i = 0; i < drinkCount; i++) + { + string name; + cin >> name; + + drinks.push_back(Drink(name)); + } + + int count; + cin >> count; + + for (int i = 0; i < count; i++) + { + string light, strong; + cin >> light >> strong; + + Find(drinks, strong)->strongerThan.push_back(Find(drinks, light)->name); + } + + cout << "Case #" << ++caseNr << ": Dilbert should drink beverages in this order:"; + + int good = 0; + unsigned int i; + + while (good != drinkCount) + { + string name; + + for (i = 0; i < drinks.size(); i++) + { + if (drinks[i].strongerThan.size() == 0) + { + cout << " " << drinks[i].name; + name = drinks[i].name; + break; + } + } + + drinks.erase(drinks.begin() + i); + + for (i = 0; i < drinks.size(); i++) + { + for (unsigned int j = 0; j < drinks[i].strongerThan.size(); j++) + { + if (drinks[i].strongerThan[j] == name) + { + drinks[i].strongerThan.erase(drinks[i].strongerThan.begin() + j); + j--; + } + } + } + + good++; + } + + cout << "." << endl + << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11060/11060-5.cpp b/uva_cpp_clean/11060/11060-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6ece45a2812b1ff87fa8b9ba225533b0549224cc --- /dev/null +++ b/uva_cpp_clean/11060/11060-5.cpp @@ -0,0 +1,195 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int n,g[110][110],col[110],in[110],f[110],p; +void topsort() +{ + int flag=0; + while(!flag) + { + flag=1; + for1(i,n) + { + if(!col[i] && !in[i]) + { + flag=0; + col[i]=1; + f[p++]=i; + for1(j,n) + { + if(g[i][j]) + in[j]--; + } + break; + } + } + } +} + +int main() +{ + int no=0; + while(si(n)==1) + { + p=0; + string s; + msi mp; + string mp1[200]; + for1(i,n) + { + cin>>s; + mp[s]=i; + mp1[i]=s; + } + + int t; + si(t); + while(t--) + { + string b1,b2; + cin>>b1>>b2; + if(!g[mp[b1]][mp[b2]]) + { + in[mp[b2]]++; + } + g[mp[b1]][mp[b2]]=1; + } + + Case(no); + topsort(); + + for0(i,n) + { + CPN(mp1[f[i]]); + } + printf(".\n\n"); + + cover(g,0); + cover(col,0); + cover(in,0); + cover(f,0); + } + + return 0; +} diff --git a/uva_cpp_clean/11060/11060-6.cpp b/uva_cpp_clean/11060/11060-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e97d6dc71939a90eab543254fdb7528d15a70ab --- /dev/null +++ b/uva_cpp_clean/11060/11060-6.cpp @@ -0,0 +1,47 @@ +/* + graphs > traversal > topological sorting + difficulty: easy + date: 31/Jan/2020 + by: @brpapa +*/ +#include +#include +#include +#include +using namespace std; + +int main() { + int V, T = 0; + while (cin >> V) { + string str[110]; + map id; + vector adjList[110]; + vector inDegree(V, 0); + + for (int v = 0; v < V; v++) + cin >> str[v], id[str[v]] = v; + + int E; cin >> E; + while (E--) { + string s1, s2; cin >> s1 >> s2; + adjList[id[s1]].push_back(id[s2]); + inDegree[id[s2]]++; + } + + priority_queue pq; + for (int v = 0; v < V; v++) if (inDegree[v] == 0) pq.push(-v); + + printf("Case #%d: Dilbert should drink beverages in this order:", ++T); + while (!pq.empty()) { + int v = 0-pq.top(); pq.pop(); + + cout << " " << str[v]; + + for (int u : adjList[v]) + if (--inDegree[u] == 0) + pq.push(-u); + } + cout << "." << endl << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11060/11060-9.cpp b/uva_cpp_clean/11060/11060-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fa081ae193ee40339254113ef03a6572f1feafa --- /dev/null +++ b/uva_cpp_clean/11060/11060-9.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +const int MAX_V = 110, LEN = 60; + +int tc, V, E, inDegree[MAX_V]; +char word[MAX_V][LEN], word1[LEN], word2[LEN]; +vector AdjList[MAX_V], ts; +map mp; + +void TopologicalSort(){ + priority_queue , greater > pq; + for(int u = 0; u < V; u++) if( inDegree[u] == 0 ) pq.push(u); + while( not pq.empty() ){ + int u = pq.top(); pq.pop(); + ts.push_back(u); + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + inDegree[v]--; + if( inDegree[v] == 0 ) pq.push(v); + } + } +} + +void solve(){ + TopologicalSort(); + printf("Case #%d: Dilbert should drink beverages in this order:", ++tc); + for(int i = 0; i < V; i++) printf( i == V - 1 ? " %s.\n\n" : " %s", word[ts[i]]); +} + +void read(){ + for(int i = 0; i < V; i++){ + scanf("%s", word[i]); + mp[word[i]] = i; + } + scanf("%d\n", &E); + for(int i = 0; i < E; i++){ + scanf("%s %s", word1, word2); + int u = mp[word1], v = mp[word2]; + AdjList[u].push_back(v); + inDegree[v]++; + } +} + +void clear(){ + mp.clear(); + ts.clear(); + for(int i = 0; i < V; i++) AdjList[i].clear(), inDegree[i] = 0; +} + +int main(){ + while(scanf("%d\n", &V) == 1){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11062/11062-14.cpp b/uva_cpp_clean/11062/11062-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c977ae1f2a1f98d3ce63961f477cc05ab75901c5 --- /dev/null +++ b/uva_cpp_clean/11062/11062-14.cpp @@ -0,0 +1,162 @@ +/*************************************************** + * Problem Name : 11062 Andy’s Second Dictionary.cpp + * Problem Link : https://onlinejudge.org/external/110/11062.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-10-14 + * Problem Type : STL (set) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ __FastIO; + //~ cout << setprecision (10) << fixed; + //~ __FileRead; + string str = ""; + char in; + setst; + + while (scanf("%c", &in) == 1) { + if ((in >= 'a' && in <= 'z') || (in >= 'A' && in <= 'Z') ) { + //~ debug; + str.pb (tolower (in) ); + + } else if (in == '-') { + char ch = getchar();; + + + if (ch == '\n') { + continue; + } + + str.pb ('-'); + str.pb (tolower (ch) ); + + } else { + //~ cout << str << "\n"; + if (!str.empty() ) { + st.insert (str); + str.clear(); + } + } + } + + //~ debug; + //~ cout << (int)st.size() << "\n"; + //~ debug; + + + for (auto it : st) { + cout << it << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/11062/11062-21.cpp b/uva_cpp_clean/11062/11062-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..662cb950e5aa28d1e43549b1cd53bcd4c6955696 --- /dev/null +++ b/uva_cpp_clean/11062/11062-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11062 + Name: Andy's Second Dictionary + Problem: https://onlinejudge.org/external/110/11062.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +#define MAXL 100000 + +int wcnt=0, ccnt=0; +char line[MAXL], carry[MAXL]; +string words[1000]; + +void addWord() { + if (ccnt > 0) { + carry[ccnt] = 0; + words[wcnt++] = carry; + ccnt = 0; + } +} + +int main() { + int i; + while (cin.getline(line, MAXL)) { + for (i=0; line[i]; ++i) { + if (isalpha(line[i]) || (line[i]=='-' && line[i+1])) + carry[ccnt++] = tolower(line[i]); + + else if (line[i]!='-') + addWord(); + } + if (i>0 && line[i-1]!='-') + addWord(); + } + if (ccnt) addWord(); + + sort(words, words+wcnt); + cout << words[0] << endl; + for (int i=1; i + +using namespace std; + +string s, ss; +set v; + +int main(){ + while(getline(cin, s)){ + for(int i = 0; i < s.size(); i++){ + if(isalpha(s[i]) || s[i] == '-'){ + while(i < s.size() && (isalpha(s[i]) || s[i] == '-') ) + if(i == s.size() - 1 && s[i] == '-') goto jump; + else ss += tolower(s[i]), i++; + } + if(!ss.empty()) v.insert(ss), ss = ""; + jump: ; + } + } + for(set ::iterator it = v.begin(); it != v.end(); ++it) + printf("%s\n", (*it).c_str()); + return(0); +} diff --git a/uva_cpp_clean/11063/11063-14.cpp b/uva_cpp_clean/11063/11063-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee8cc82e9f122e8699662d2dcb490b093cd614d1 --- /dev/null +++ b/uva_cpp_clean/11063/11063-14.cpp @@ -0,0 +1,94 @@ +/*************************************************** + * Problem name : 11063 - B2-Sequence.cpp + * Problem Link : https://uva.onlinejudge.org/external/110/11063.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-29 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 1000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int n, ar[MAX], t = 1; + while (scanf("%d", &n) == 1) { + int chk = 0, f = 0; + for (int i = 0; i < n; i++) { + scanf("%d", &ar[i]); + if (chk < ar[i]) { + chk = ar[i]; + } else { + f = 1; + } + } + if (f == 1) { + printf("Case #%d: It is not a B2-Sequence.\n", t++); + nl; + continue; + } + mapmp; + bool flag = false; + for (int i = 0; i < n - 1; i++) { + for (int j = i; j < n; j++) { + int x = ar[i] + ar[j]; + //~ printf("%d ", x); + if (mp[x] != 0) { + flag = true; + break; + } else { + mp[x] = 1; + } + } + } + if (!flag) printf("Case #%d: It is a B2-Sequence.\n", t++); + else printf("Case #%d: It is not a B2-Sequence.\n", t++); + nl; + mp.clear(); + + } + return 0; +} diff --git a/uva_cpp_clean/11063/11063-19.cpp b/uva_cpp_clean/11063/11063-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..958f603ad59baf92a6c9599e0456b4310891127b --- /dev/null +++ b/uva_cpp_clean/11063/11063-19.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +using namespace std; + +int main() { + unordered_set s; + vector v; + int N, c = 1; + while (cin >> N) { + v.clear(); + s.clear(); + bool ok = true; + int prev = 0; + while (N--) { + int val; + cin >> val; + v.push_back(val); + if (ok) ok = val > prev; + prev = val; + for (int i = 0; i < v.size() && ok; i++) { + ok = !s.count(v[i] + val); + s.insert(v[i] + val); + } + } + printf("Case #%d: ", c++); + if (ok) printf("It is a B2-Sequence.\n\n"); + else printf("It is not a B2-Sequence.\n\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11063/11063-21.cpp b/uva_cpp_clean/11063/11063-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e44ed53e64942a6d6b5312aac1c92278a86a52f --- /dev/null +++ b/uva_cpp_clean/11063/11063-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11063 + Name: B2-Sequence + Problem: https://onlinejudge.org/external/110/11063.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool check(int X[], int n) { + bool seen[20143] = {}; + for (int i=0; i 0 && X[i] <= X[i-1]) + return false; + + for (int j=0; j<=i; ++j) + if (seen[X[i]+X[j]]) + return false; + else + seen[X[i]+X[j]] = true; + } + return true; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[143]; + for (int cse=1, n; cin>>n; ++cse) { + cout << "Case #" << cse; + + for (int i=0; i> X[i]; + + if (check(X, n)) + cout << ": It is a B2-Sequence.\n\n"; + else + cout << ": It is not a B2-Sequence.\n\n"; + } +} diff --git a/uva_cpp_clean/11064/11064-19.cpp b/uva_cpp_clean/11064/11064-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8035f73a9ed16c3f4164ce86ff610ab5d20f63b --- /dev/null +++ b/uva_cpp_clean/11064/11064-19.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +inline void sieve(ll upperbound = 50000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline void primeFactors(ll n, vii & factors) { + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +inline ll EulerPhi(ll n) { + ll idx=0, p=primes[0], ans=n; + while (n!=1 && (p*p<=n)) { + if (n%p==0) ans-=ans/p; + while (n%p==0) n/=p; + p=primes[++idx]; + } + if (n!=1) ans-=ans/n; + return ans; +} + +int main() { + sieve(); + ll n; + vii factors; + while (cin >> n) { + int val = n - EulerPhi(n); + factors.clear(); + primeFactors(n, factors); + int div = 1; + for (int i = 0; i < factors.size(); i++) { + div *= factors[i].second + 1; + } + div--; + printf("%d\n", val - div); + } + return 0; +} diff --git a/uva_cpp_clean/11065/11065-21.cpp b/uva_cpp_clean/11065/11065-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33dc878d89727338fb56b352d5022650a3d69439 --- /dev/null +++ b/uva_cpp_clean/11065/11065-21.cpp @@ -0,0 +1,82 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11065 + Name: A Gentlemen's Agreement + Problem: https://onlinejudge.org/external/110/11065.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +typedef long long BSet; +BSet N[100]; +int n, mx; +int bk(BSet R, BSet P, BSet X, int sz) { + if (!P) { + if (!X && sz>mx) mx = sz; + return X ? 0 : 1; + } + + bool f = 1; + int result = 0; + long long sv = 1, u=-1; + for (int v=0; v deg[j]; } + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, m, u, v; + cin >> T; + while (T--) { + cin >> n >> m; + BSet fl = (1LL<> u >> v; + adj[u][deg[u]++] = v; + adj[v][deg[v]++] = u; + } + + sort(ord, ord+n, comp); + for (int i=0; i + +using namespace std; + +typedef long long ll; + +const int MAX_V = 65; + +int tc, V, E, u, v, nSet, mxSet; +ll ones, Adj[MAX_V]; + +void backtrack(int id = 0, ll used = 0, int depth = 0) { + if (used == ones) { + nSet++; + mxSet = max(mxSet, depth); + return; + } + for (int i = id; i < V; i++) + if (not (used bitand (1LL << i))) + backtrack(i + 1, used bitor Adj[i], depth + 1); +} + +void read() { + scanf("%d %d", &V, &E); + for (int i = 0; i < V; i++) Adj[i] = (1LL << i); + for (int i = 0; i < E; i++) { + scanf("%d %d", &u, &v); + Adj[u] |= (1LL << v); + Adj[v] |= (1LL << u); + } + nSet = mxSet = 0; + ones = (1LL << V) - 1; +} + +int main() { + scanf("%d", &tc); + while (tc--) { + read(); + backtrack(); + printf("%d\n%d\n", nSet, mxSet); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/11067/11067-19.cpp b/uva_cpp_clean/11067/11067-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..828b38276620f71e00fc8810e592024c2f0a7663 --- /dev/null +++ b/uva_cpp_clean/11067/11067-19.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +long long dp[103][103]; + +int main() { + int w, h; + while (cin >> w >> h && !(w == 0 && h == 0)) { + for (int i = 0; i <= h + 1; i++) { + for (int j = 0; j <= w + 1; j++) { + dp[j][i] = 0; + } + } + int n; + cin >> n; + for (int i = 0; i < n; i++) { + int x, y; + cin >> x >> y; + dp[x][y] = -1; + } + dp[w][h] = 1; + for (int i = w - 1; i >= 0; i--) { + int x = i, y = h; + while (y >= 0 && x <= w) { + if (dp[x][y] == -1) dp[x][y] = 0; + else { + dp[x][y] = dp[x+1][y] + dp[x][y+1]; + } + x++, y--; + } + } + for (int i = h - 1; i >= 0; i--) { + int x = 0, y = i; + while (y >= 0 && x <= w) { + if (dp[x][y] == -1) dp[x][y] = 0; + else { + dp[x][y] = dp[x+1][y] + dp[x][y+1]; + } + x++, y--; + } + } + if (dp[0][0] > 1) printf("There are %lld paths from Little Red Riding Hood's house to her grandmother's house.\n", dp[0][0]); + else if (dp[0][0] == 1) printf("There is one path from Little Red Riding Hood's house to her grandmother's house.\n"); + else if (dp[0][0] == 0) printf("There is no path.\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11067/11067-21.cpp b/uva_cpp_clean/11067/11067-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5cdbe85590b9c2e3aa2580ab054959d3a7557d3 --- /dev/null +++ b/uva_cpp_clean/11067/11067-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11067 + Name: Little Red Riding Hood + Problem: https://onlinejudge.org/external/110/11067.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + bool B[143][143]; + unsigned int C[143][143], w, h, n; + while (cin >> w >> h >> n) { + for (int i=0; i<=h; ++i) + memset(B[i], 0, (w+1)), + memset(C[i], 0, (w+1)*sizeof(int)); + + for (int i=0, x,y; i> x >> y; + B[y][x] = 1; + } + + C[0][0] = 1; + for (int i=0; i<=h; ++i) + for (int j=0; j<=w; ++j) + if (!B[i][j]) + C[i+1][j] += C[i][j], + C[i][j+1] += C[i][j]; + + int res = C[h][w]; + if (res > 1) + cout << "There are " << res << " paths from Little Red Riding Hood's house to her grandmother's house.\n"; + + else if (res == 1) + cout << "There is one path from Little Red Riding Hood's house to her grandmother's house.\n"; + + else cout << "There is no path.\n"; + } +} diff --git a/uva_cpp_clean/11068/11068-21.cpp b/uva_cpp_clean/11068/11068-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..539881a0b2ca02d5f18f5bbb68416ee35d24eb56 --- /dev/null +++ b/uva_cpp_clean/11068/11068-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11068 + Name: An Easy Task + Problem: https://onlinejudge.org/external/110/11068.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(2); + + int a1,a2,b1,b2,c1,c2; + while (cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2 && (a1||b1||c1||a2||b2||c2)) + if (a1*b2 == a2*b1) + cout << "No fixed point exists.\n"; + else { + if (!b2) { + swap(a1, a2); + swap(b1, b2); + swap(c1, c2); + } + + double x; + if (!b1) x = double(c1) / a1; + else x = (double(c2)/b2-double(c1)/b1) / + (double(a2)/b2 - double(a1)/b1); + double y = (c2 - a2*x)/b2; + cout << "The fixed point is at " << x << ' ' << y << ".\n"; + } +} diff --git a/uva_cpp_clean/11069/11069-13.cpp b/uva_cpp_clean/11069/11069-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ccda5b73c9d0a1f3ef5c17b661dafab14e03e10a --- /dev/null +++ b/uva_cpp_clean/11069/11069-13.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +typedef long long ll; + +ll n, a[77], b[77], c[77]; + +int main() +{ + a[0] = 0; + a[1] = 1; + a[2] = 2; + a[3] = 2; + + b[0] = 0; + b[1] = 1; + c[0] = c[1] = 0; + + b[2] = b[3] = 1; + c[2] = c[3] = 1; + + for (int i = 4; i < 77; ++i) + { + a[i] = b[i - 2] + (2 * c[i - 2]) + b[i - 4]; + + b[i] = c[i - 2] + b[i - 2]; + c[i] = c[i - 2] + b[i - 4]; + } + + while (cin >> n) + { + cout << a[n] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11069/11069-20.cpp b/uva_cpp_clean/11069/11069-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..152c17994dc25165018e1c30ac95f73b866ace49 --- /dev/null +++ b/uva_cpp_clean/11069/11069-20.cpp @@ -0,0 +1,20 @@ +#include +#include + +using namespace std; + +int ans[80]; + +int main() +{ + ans[1] = 1, ans[2] = 2, ans[3] = 2; + for(int i=4 ; i<80 ; i++) + ans[i] = ans[i-2] + ans[i-3]; + + int n; + + while( cin >> n ) + cout << ans[n] << endl; + + return 0; +} diff --git a/uva_cpp_clean/11069/11069-21.cpp b/uva_cpp_clean/11069/11069-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4d51bf5dc9880aa50459a7e9a4989231163e456 --- /dev/null +++ b/uva_cpp_clean/11069/11069-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11069 + Name: A Graph Problem + Problem: https://onlinejudge.org/external/110/11069.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + int DP[77] = { 0, 1, 2, 2 }; + for (int i=4; i<77; ++i) + DP[i] = DP[i-2] + DP[i-3]; + + int n; + while (cin >> n) + cout << DP[n] << endl; +} diff --git a/uva_cpp_clean/11078/11078-17.cpp b/uva_cpp_clean/11078/11078-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4bc1265c05d71c0f1c3ccdd16fc7cec795c5e9c0 --- /dev/null +++ b/uva_cpp_clean/11078/11078-17.cpp @@ -0,0 +1,27 @@ +/** + * Open Credit System + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int t; + cin >> t; + while (t--) + { + int n; + cin >> n; + int ans = numeric_limits::min(), curMax = ans; + for (int i = 0; i < n; ++i) + { + int cur; + cin >> cur; + if (i) + ans = max(ans, curMax - cur); + curMax = max(curMax, cur); + } + cout << ans << "\n"; + } +} diff --git a/uva_cpp_clean/11078/11078-2.cpp b/uva_cpp_clean/11078/11078-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da3379be0b71e02e42e9916c02dd4cf2b7eeafb8 --- /dev/null +++ b/uva_cpp_clean/11078/11078-2.cpp @@ -0,0 +1,48 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. + +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +int main(){ + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; scanf("%d", &tc); + while(tc--) { + int n; scanf("%d", &n); + vector v(n); + for(int& i: v) cin >> i; + int mx = -OO, mx_d = -OO; + for(int i = 0; i < n-1; ++i) { + if (v[i]>mx) mx = v[i]; + if(mx-v[i+1]>mx_d) mx_d = mx-v[i+1]; + } + printf("%d\n", mx_d); + } + return 0; +} diff --git a/uva_cpp_clean/11078/11078-21.cpp b/uva_cpp_clean/11078/11078-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d301fe3ee0c57e9a502e9a7a89d82c7bd76f9d5 --- /dev/null +++ b/uva_cpp_clean/11078/11078-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11078 + Name: Open Credit System + Problem: https://onlinejudge.org/external/110/11078.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-'); + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +char buf[50]; +void print(long long x) { + int i=48, neg=0; + buf[i--] = '\n'; + if (!x) buf[i--] = '0'; + else if (x < 0) { + x = -x; + neg = 1; + } + for (; x; x/=10) + buf[i--] = x%10 + '0'; + + if (neg) buf[i--] = '-'; + fwrite_unlocked(buf+i+1, 1, 48-i, stdout); +} + +// ----------------------------------------------------------- + +int main() { + int T = readInt(); + while (T--) { + int n = readInt(), + x = readInt(), + mx = readInt(), + di = x - mx; + + if (x > mx) mx = x; + for (int i=2; i di) + di = mx - x; + + if (x > mx) mx = x; + } + + print(di); + } +} diff --git a/uva_cpp_clean/11078/11078-9.cpp b/uva_cpp_clean/11078/11078-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b7124fa63d4ba33d70fd926b87fae630ce73796 --- /dev/null +++ b/uva_cpp_clean/11078/11078-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main(){ + int c, n, ans, tmp; + vector x(100000); + scanf("%d", &c); + while(c--){ + scanf("%d", &n); + for(int i = 0; i < n; i++) + scanf("%d", &x[i]); + tmp = x[0]; + ans = -1600000; + for(int i = 0; i < n - 1; i++) + tmp = max(tmp, x[i]), ans = max(ans, tmp - x[i + 1]); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11080/11080-13.cpp b/uva_cpp_clean/11080/11080-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..630deb12db842c7a402278ea07b3fdb443c668e4 --- /dev/null +++ b/uva_cpp_clean/11080/11080-13.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int v, e, ans = 0; +vector guarded; +vector< vector > town; + +ofstream fout("f.out"); + +int bfs(int start) +{ + int b = 0, w = 0; + + queue q; + + q.push(start); + + guarded[start] = 0; + + while (!q.empty()) + { + int top = q.front(); + + guarded[top] ? w++ : b++; + + q.pop(); + + for (int i = 0; i < town[top].size(); i++) + { + if (guarded[town[top][i]] == -1) + { + guarded[town[top][i]] = !guarded[top]; + + q.push(town[top][i]); + } + + else if (guarded[town[top][i]] == guarded[top]) + { + return -1; + } + } + } + + return (w == 0) ? b : min(b, w); +} + +int main() +{ + int t; + + cin >> t; + + while (t--) + { + ans = 0; + + bool notAns = true; + + cin >> v >> e; + + town.clear(); + town.resize(v); + + guarded.clear(); + guarded.resize(v, -1); + + for (int i = 0; i < e; i++) + { + int x, y; + + cin >> x >> y; + + town[x].push_back(y); + town[y].push_back(x); + } + + for (int i = 0; i < v; i++) + { + int temp = 0; + + if (guarded[i] == -1) + { + temp = bfs(i); + + if (temp == -1) + { + notAns = false; + + break; + } + + else + { + ans += temp; + } + } + } + + if (notAns) + { + cout << ans << endl; + } + + else + { + cout << -1 << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11080/11080-19.cpp b/uva_cpp_clean/11080/11080-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..550728e58ee5e945ecb693373d5005a8e777067c --- /dev/null +++ b/uva_cpp_clean/11080/11080-19.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +vector adjList; +vi color; + +int proc() { + bool ok = true; + int count = 0; + queue q; + for (int j = 0; j < adjList.size() && ok; j++) { + int black = 0, white = 0; + if (color[j] == -1) { + q.push(j); + color[j] = 0; + } else continue; + while (!q.empty() && ok) { + if (color[q.front()] == 0) black++; + else white++; + for (int i = 0; i < adjList[q.front()].size() && ok; i++) { + if (color[adjList[q.front()][i]] == -1) { + color[adjList[q.front()][i]] = 1-color[q.front()]; + q.push(adjList[q.front()][i]); + } else if (color[adjList[q.front()][i]] == color[q.front()]) ok = false; + } + q.pop(); + } + count += min(black, white); + if (black == 0 || white == 0) count++; + } + return ok? count: -1; +} + +int main() { + int T; + cin >> T; + while (T--) { + int v, e; + cin >> v >> e; + adjList.assign(v, vi()); + color.assign(v, -1); + for (int i = 0; i < e; i++) { + int a1, a2; + cin >> a1 >> a2; + adjList[a1].push_back(a2); + adjList[a2].push_back(a1); + } + printf("%d\n", proc()); + } + return 0; +} diff --git a/uva_cpp_clean/11080/11080-20.cpp b/uva_cpp_clean/11080/11080-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70b991bba4581f9aa4de8d695439f864675de23a --- /dev/null +++ b/uva_cpp_clean/11080/11080-20.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +const int MAXN = 200; +vector edge[MAXN+2]; +int visited[MAXN+2]; + +int bfs_bipartite( int source ) { + queue < int > q; + q.push( source ), visited[source] = 1; + int totalGuards = 0, nWhite = 0; + while( !q.empty() ) { + int from = q.front(); + q.pop(); + ++totalGuards; + if( visited[from] == 1 ) ++nWhite; + for( int i = 0 ; i < edge[from].size() ; ++i ) { + int to = edge[from][i]; + if( visited[from] == visited[to] ) return 0; + if( !visited[to] ) { + visited[to] = 3 - visited[from]; + q.push( to ); + } + } + } + if( totalGuards == 1 ) return 1; + int nBlack = totalGuards - nWhite; + return ( min( nWhite, nBlack ) ); +} + +int main( int argc, char ** argv ) { + int tc; + scanf( "%d", &tc ); + for( int nCase = 1 ; nCase <= tc ; ++nCase ) { + int v, e; + scanf( "%d %d", &v, &e ); + for( int i = 1 ; i <= e ; ++i ) { + int x, y; + scanf( "%d %d", &x, &y ); + edge[x].push_back( y ); + edge[y].push_back( x ); + } + + memset( visited, 0, sizeof( visited ) ); + + int guards = 0; + for( int i = 0 ; i < v ; ++i ) { + if( !visited[i] ) { + int temp = bfs_bipartite( i ); + + if( !temp ) { + guards = -1; + break; + } + else guards += temp; + } + } + + printf( "%d\n", guards ); + + for( int i = 0 ; i < v ; ++i ) edge[i].clear(); + } + return 0; +} diff --git a/uva_cpp_clean/11080/11080-21.cpp b/uva_cpp_clean/11080/11080-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a60af9cce66586c2d2b64b52137ccba12f9e52c6 --- /dev/null +++ b/uva_cpp_clean/11080/11080-21.cpp @@ -0,0 +1,72 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11080 + Name: Place the Guards + Problem: https://onlinejudge.org/external/110/11080.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 1073741824 + +list adj[202]; +int color[202], cnt; + +int dfs(int u) { + int c = 1-color[u], + sum = c; + + for (int v: adj[u]) + if (color[v]==-1) { + cnt++; + color[v] = c; + sum += dfs(v); + if (sum < 0) + return -INF; + } + else if (color[v]!=c) + return -INF; + return sum; +} + + +int main(){ + int T, n, m, u, v; + cin>>T; + while (T--) { + cin>>n>>m; + for (int i=0; i>u>>v; + adj[u].push_back(v); + adj[v].push_back(u); + } + + int result = 0; + for (int i=0; i= 0) + result += max(1, min(r, cnt-r)); + else { + result = -1; + break; + } + } + cout << result << endl; + } +} diff --git a/uva_cpp_clean/11080/11080-9.cpp b/uva_cpp_clean/11080/11080-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22d0839ae11d67f1b6ccad48670a843a612a8fa9 --- /dev/null +++ b/uva_cpp_clean/11080/11080-9.cpp @@ -0,0 +1,70 @@ +#include + +#define WHITE -1 +#define RED 0 +#define BLACK 1 + +using namespace std; + +const int MAX_V = 210; + +int tc, V, E, a, b, black, red, ans, color[MAX_V]; +vector AdjList[MAX_V]; + +bool isBipartite(int source){ + bool isOK = true; + queue Q; + Q.push(source); + color[source] = RED; + red = black = 0; + while( not Q.empty() and isOK ){ + int u = Q.front(); Q.pop(); + if( color[u] == RED ) red++; + if( color[u] == BLACK ) black++; + for(int j = 0; j < AdjList[u].size() and isOK; j++){ + int v = AdjList[u][j]; + if( color[v] == WHITE ) + color[v] = color[u] ^ 1, Q.push(v); + else if( color[v] == color[u] ) + isOK = false; + } + } + return isOK; +} + +int t; + +void solve(){ + bool ret = true; + fill(color, color + V, WHITE), ans = 0; + for(int i = 0; i < V and ret; i++) + if( color[i] == WHITE ){ + ret &= isBipartite(i); + ans += ( min(red, black) != 0 ) ? min(red, black) : max(red, black); + } + if( not ret ) ans = -1; + printf("%d\n", ans); +} + +void read(){ + scanf("%d %d", &V, &E); + for(int i = 0; i < E; i++){ + scanf("%d %d", &a, &b); + AdjList[a].push_back(b); + AdjList[b].push_back(a); + } +} + +void clear(){ + for(int u = 0; u < V; u++) AdjList[u].clear(); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11085/11085-13.cpp b/uva_cpp_clean/11085/11085-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4577ef3fd40afad05069a2578fd2021ba7fd3608 --- /dev/null +++ b/uva_cpp_clean/11085/11085-13.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + +using namespace std; + +int cc = 1, minimum, temp, num = 0, test[8], row[8], dp[92][8]; + +bool place(int r, int c) +{ + for (int pre = 0; pre < c; pre++) + { + if (row[pre] == r || (abs(row[pre] - r) == abs(c - pre))) return false; + } + + return true; +} + +void backtrack(int c) +{ + if (c == 8) + { + for (int i = 0; i < 8; i++) + { + dp[num][i] = row[i] + 1; + } + + num++; + } + + for (int r = 0; r < 8; r++) + { + if (place(r, c)) + { + row[c] = r; + + backtrack(c + 1); + } + } +} + +int main() +{ + memset(dp, 0, sizeof dp); + memset(row, 0, sizeof row); + + backtrack(0); + + while (cin >> test[0] >> test[1] >> test[2] >> test[3] >> test[4] >> test[5] >> test[6] >> test[7]) + { + temp = 0; + minimum = 100000000; + + for (int i = 0; i < 92; i++) + { + temp = 0; + + for (int j = 0; j < 8; j++) + { + if (dp[i][j] != test[j]) + { + temp++; + } + } + + minimum = min(minimum, temp); + } + + cout << "Case " << cc << ": " << minimum << endl; + + cc++; + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/11085/11085-21.cpp b/uva_cpp_clean/11085/11085-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c63d144d1c990611de9bac0021ce1bb3bdc76993 --- /dev/null +++ b/uva_cpp_clean/11085/11085-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11085 + Name: Back to the 8-Queens + Problem: https://onlinejudge.org/external/110/11085.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int P[8], best; +bool check(int j) { + for (int i=0; i> P[0]; ++cse) { + for (int i=1; i<8; ++i) + cin >> P[i]; + + int mn = 10; + for (int i=0; i + +using namespace std; + +vector ans[1000]; +vector x; +int a[8], t; + +void f(int col){ + if(col == 8){ + ans[t++] = x; + return; + } + for(int i = 0 ; i < 8 ; i++){ + bool ok = true; + for(int j = 0; j < x.size(); j++) + if(x[j] == i || abs(col - j) == abs(x[j] - i)){ + ok = false; + break; + } + if(ok == true){ + x.push_back(i); + f(col + 1); + x.pop_back(); + } + } +} + +int main(){ + int m, s, l = 1; + f(0); + while(scanf("%d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7]) == 8){ + m = 9; + for(int k = 0; k < t; k++){ + s = 0; + for(int j = 0; j < 8; j++) + if((a[j] - 1) != ans[k][j]) + s++; + m = min(s, m); + } + cout << "Case " << l << ": " << m << endl; + l++; + } + return(0); +} diff --git a/uva_cpp_clean/11088/11088-21.cpp b/uva_cpp_clean/11088/11088-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2847551d5388729071f1ddc005e209fe993dd7f6 --- /dev/null +++ b/uva_cpp_clean/11088/11088-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11088 + Name: End up with More Teams + Problem: https://onlinejudge.org/external/110/11088.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define bit(i,n) (n&(1<=20) + ans=max(ans,1+rec(avail & ~(1<>v[i]; + memset(memo,-1,sizeof memo); + printf("Case %d: %d\n",cse++,rec((1< + +#define LIMIT 69 + +using namespace std; + +int tc, ct, carry, l1, l2, minl, maxl, k, i, j, aux; +char fibo[LIMIT][LIMIT], ans[LIMIT], tmp[LIMIT], final_tmp[LIMIT], fibinary[LIMIT]; +bool ok; + +char* subt(char *s1, char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + carry = 0; + for(k = 0, i = l1 - 1, j = l2 - 1; k < l2; k++, i--, j--){ + aux = (s1[i] - '0') - (s2[j] - '0') - carry; + if(aux >= 0) + tmp[k] = aux + '0', carry = 0; + else + tmp[k] = (aux + 10) + '0', carry = 1; + } + while(k < l1){ + aux = (s1[i--] - '0') - carry; + if(aux >= 0) + tmp[k++] = aux + '0', carry = 0; + else + tmp[k++] = aux + '0', carry = 1; + } + while(tmp[k - 1] == '0') + k--; + tmp[k] = '\0'; + i = 0; + while(k > 0) + final_tmp[i++] = tmp[--k]; + final_tmp[i] = '\0'; + return final_tmp; +} + +bool less_or_equal(char* s1, char* s2){ + l1 = strlen(s1), l2 = strlen(s2); + if(l1 < l2) + return true; + if(l1 > l2) + return false; + for(i = 0; i < l1; i++) + if(s1[i] < s2[i]) + return true; + else if(s1[i] > s2[i]) + return false; + return true; +} + +char* sum(char *s1, char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + minl = l1 > l2 ? l2 : l1; + maxl = l1 > l2 ? l1 : l2; + carry = 0; + for(k = 0,i = l1 - 1,j = l2 - 1; k < minl; k++, i--, j--){ + aux = s1[i]-'0' + s2[j] - '0' + carry; + tmp[k] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l1){ + aux = s1[i--] - '0' + carry; + tmp[k++] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l2){ + aux = s2[j--] - '0' + carry; + tmp[k++] = aux % 10 + '0'; + carry = aux / 10; + } + if(carry) + tmp[maxl++] = carry + '0', tmp[maxl] = '0'; + else + tmp[maxl] = '\0'; + i = 0; + while(maxl > 0) + final_tmp[i++] = tmp[--maxl]; + final_tmp[i] = '\0'; + return final_tmp; +} + +void generateFibo(){ + fibo[0][0] = '0', fibo[0][1] = '\0'; + fibo[1][0] = '1', fibo[1][1] = '\0'; + for(int t = 2; t < LIMIT; t++) + strcpy(fibo[t], sum(fibo[t - 1], fibo[t - 2])); +} + +int main(){ + generateFibo(); + scanf("%d", &tc); + while(tc--){ + scanf("%s", fibinary); + getchar(); + ok = false; + ct = 0; + for(int x = LIMIT - 1; x >= 2; x--) + if(less_or_equal(fibo[x], fibinary)) + strcpy(fibinary, subt(fibinary, fibo[x])), ans[ct++] = '1', ok = true; + else if(ok) + ans[ct++] = '0'; + ans[ct] = '\0'; + printf("%s\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11090/11090-17.cpp b/uva_cpp_clean/11090/11090-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ee57e4e6bbb5541dbd868cb1c45626b3774e2b1 --- /dev/null +++ b/uva_cpp_clean/11090/11090-17.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; +using VI = vector; +using VVI = vector; +using VD = vector; +using VVD = vector; +using VS = vector; +using VVS = vector; +using VB = vector; +using VVB = vector; +using PII = pair; +using PDD = pair; +using PDI = pair; +constexpr double pi = 3.1415926535897932385; +constexpr double EPS = 1e-3; + +struct Edge +{ + int to, weight; + Edge() {}; + Edge(int t, int w) : to(t), weight(w) {}; +}; + +bool bellman_ford(vector>& adj, double test) +{ + int n = adj.size(); + vector dist(n, 0); + vector in_queue(n, true); + vector bit(n, 1); + + queue q; + for (int i = 0; i < n; ++i) + q.push(i); + while (!q.empty()) + { + int cur = q.front(); + q.pop(); + in_queue[cur] = false; + for (Edge& edge : adj[cur]) + if (dist[cur] + edge.weight - test < dist[edge.to]) + { + dist[edge.to] = dist[cur] + edge.weight - test; + if (!in_queue[edge.to]) + { + if (++bit[edge.to] > n) + return true; + q.push(edge.to); + in_queue[edge.to] = true; + } + } + } + return false; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + cout << fixed << setprecision(2); + + int N; + cin >> N; + for (int x = 1; x <= N; ++x) + { + int n, m; + cin >> n >> m; + vector> adj(n); + double l = 0, r = 0; + for (int i = 0; i < m; ++i) + { + int a, b, c; + cin >> a >> b >> c; + adj[--a].emplace_back(--b, c); + if (c > r) + r = c; + } + cout << "Case #" << x << ": "; + if (!bellman_ford(adj, r + 1)) + { + cout << "No cycle found.\n"; + continue; + } + while (r - l > EPS) + { + double mid = (r - l) / 2 + l; + if (bellman_ford(adj, mid)) + r = mid; + else + l = mid; + } + cout << l << '\n'; + } + return 0; +} diff --git a/uva_cpp_clean/11090/11090-18.cpp b/uva_cpp_clean/11090/11090-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c447dd62de394618ec83bd41091efb31575620e8 --- /dev/null +++ b/uva_cpp_clean/11090/11090-18.cpp @@ -0,0 +1,80 @@ +#include +#include +#include + +using namespace std; + +vector> digraph[105]; +bool recStack[105]; +double dist[105]; +long long total; +int n, m; + +bool DFS(int u, double avg){ + recStack[u] = true; + for(pair p : digraph[u]){ + int v = p.first; + double w = p.second; + if(dist[v] > dist[u] + w - avg){ + dist[v] = dist[u] + w - avg; + if(recStack[v] || DFS(v, avg)) return true; + } + } + recStack[u] = false; + return false; +} + +bool negativeCycle(double avg){ + memset(dist, 0, n); + memset(recStack, false, sizeof(recStack)); + for(int i = 1; i <= n; i++) if(DFS(i, avg)) return true; + return false; +} + +void binarySearch(){ + double l = 0, r = total + 1, mid; + while(r - l > 1e-3){ + mid = (l + r)/2.0; + if(negativeCycle(mid)) r = mid; + else l = mid; + } + if(negativeCycle(r)) printf("%.2lf\n", l); + else printf("No cycle found.\n"); +} + +int main() { + int tc, count = 1, a, b, c; + double minAvg; + scanf("%d", &tc); + while(count <= tc){ + total = 0; + scanf("%d %d", &n, &m); + for(int i = 1; i <= n; i++) digraph[i].clear(); + for(int i = 0; i < m; i++){ + scanf("%d %d %d", &a, &b, &c); + digraph[a].push_back(make_pair(b, c)); + total+=c; + } + printf("Case #%d: ", count); + binarySearch(); + count++; + } + return 0; +} +/* + La idea principal es buscar el ciclo con menor promedio de peso. Por mi parte opté por usar DFS para poder + buscar los ciclos y por esto, se me ocurrió primero obtener los SCC del digrafo, luego, ir en cada componente + y comparando los promedios de los ciclos pero esto no funcionó del todo. Para poder hallar la respuesta tuve + que analizar demasiado el texto y enfatizar en "Hallar el minimo promedio del peso de un ciclo" y aqui empezé a + descubrir como resolver este problema. Se sabe que la formula de promedio del peso de un ciclo es: + Avg = (w1 + w2 + w3 + ... + wx)/x (1) + Donde x = # de aristas del ciclo + Entonces, al analizar lo que estaba haciendo con las SCC -comparar promedios con otros- y plasmarlo en (1), se + crea la siguiente inecuación: + Avg > (w1 + w2 + w3 + ... + wx)/x + x*Avg > w1 + w2 + w3 + ... + wx + 0 > (w1 - Avg) + (w2 - Avg) + (w3 - Avg) + ... + (wx - Avg) + Donde Avg es un promedio hallado anteriormente y el otro lado es el Avg del ciclo actual + De esto se toma en cuenta de que el problema se resume en encontrar ciclos negativos que genere un Avg y aqui + uso busqueda binaria, para buscar esos Avg que me genere ciclos negativos y escoger el menor. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/11090/11090-21.cpp b/uva_cpp_clean/11090/11090-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd2f26ca5f699942df6fb61a169e331e603ba356 --- /dev/null +++ b/uva_cpp_clean/11090/11090-21.cpp @@ -0,0 +1,90 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11090 + Name: Going in Cycle!! + Problem: https://onlinejudge.org/external/110/11090.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; +#define oo 0xfffffff + +int W[64][64], dist[64][64], n, m; +vector E[64]; +bool seen[64][64]; + +double spfa(int st) { + for (int i=1; i<=n; ++i) { + for (int l=0; l<=n; ++l) { + dist[i][l] = oo; + seen[i][l] = false; + } + } + + dist[st][0] = 0; + queue > Q; + Q.push(make_pair(st, 0)); + + while (!Q.empty()) { + auto p = Q.front(); Q.pop(); + int u = p.first, l = p.second; + seen[u][l] = false; + for (int v: E[u]) { + if (dist[v][l+1] > dist[u][l] + W[u][v]) { + dist[v][l+1] = dist[u][l] + W[u][v]; + if (!seen[v][l+1]) { + seen[v][l+1] = true; + Q.push(make_pair(v, l+1)); + } + } + } + } + + double res = 1e9; + for (int l=1; l<=n; ++l) { + if (dist[st][l] != oo) { + res = min(res, double(dist[st][l])/l); + } + } + return res; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n >> m; + for (int i=1; i<=n; ++i) { + E[i].clear(); + for (int j=1; j<=n; ++j) { + W[i][j] = oo; + } + } + + for (int i=0; i> p >> q >> w; + E[p].push_back(q); + W[p][q] = min(W[p][q], w); + } + + double res = 1e9; + for (int i=1; i<=n; ++i) { + res = min(res, spfa(i)); + } + + printf("Case #%d: ", cse); + if (res < 1e9) { + printf("%.2lf\n", res); + } else { + printf("No cycle found.\n"); + } + } +} diff --git a/uva_cpp_clean/11093/11093-19.cpp b/uva_cpp_clean/11093/11093-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf6f85803e7f737447707327cca321bb9ba6af0a --- /dev/null +++ b/uva_cpp_clean/11093/11093-19.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; + +int p[100010]; +int q[100010]; + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + int N; + cin >> N; + for (int i = 0; i < N; i++) + cin >> p[i]; + for (int i = 0; i < N; i++) + cin >> q[i]; + int count = 0, prev = -1, l = 0; + bool check = true; + for (int i = 0; i < N; i++) { + count += p[i] - q[i]; + if (count < 0) { + if (!check) count -= l; + check = true; + } + if (check && p[i] - q[i] >= 0) { + l = (p[i] - q[i]) - count; + count = p[i] - q[i]; + check = false; + prev = i; + } + } + if (check || l > count) printf("Case %d: Not possible\n", c); + else printf("Case %d: Possible from station %d\n", c, prev + 1); + } + return 0; +} diff --git a/uva_cpp_clean/11093/11093-21.cpp b/uva_cpp_clean/11093/11093-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58ef332d5b857d4157f976f0fd0c3d7aef3cbc66 --- /dev/null +++ b/uva_cpp_clean/11093/11093-21.cpp @@ -0,0 +1,77 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11093 + Name: Just Finish it up + Problem: https://onlinejudge.org/external/110/11093.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------- + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(), + p[100043], q[100043]; + for (int cse=1; cse<=T; ++cse) { + int n = readUInt(); + for (int i=0; i +#include +#include +#include +#include +#include + +using namespace std; + +ofstream fout("f.out"); + +char gold; +int m, n, x, y; +int maximum = 0, dfsCount = 0; + +int comp[25][25]; +vector world; + +void dfs(int i, int j, int& temp) +{ + temp++; + + comp[i][j] = dfsCount; + + if (i - 1 >= 0 && world[i - 1][j] == gold && comp[i - 1][j] == -1) + { + dfs(i - 1, j, temp); + } + + if (i + 1 < m && world[i + 1][j] == gold && comp[i + 1][j] == -1) + { + dfs(i + 1, j, temp); + } + + if (world[i][(j + 1 + n) % n] == gold && comp[i][(j + 1 + n) % n] == -1) + { + dfs(i, (j + 1 + n) % n, temp); + } + + if (world[i][(j - 1 + n) % n] == gold && comp[i][(j - 1 + n) % n] == -1) + { + dfs(i, (j - 1 + n) % n, temp); + } +} + +int main() +{ + while (cin >> m >> n) + { + maximum = dfsCount = 0; + + world.clear(); + + memset(comp, -1, sizeof comp); + + for (int i = 0; i < m; i++) + { + string s; + + cin >> s; + + world.push_back(s); + } + + cin >> x >> y; + + gold = world[x][y]; + + int temp = 0; + + dfs(x, y, temp); + + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + temp = 0; + + if (world[i][j] == gold && comp[i][j] == -1) + { + dfs(i, j, temp); + + maximum = max(temp, maximum); + + dfsCount++; + } + } + } + + cout << maximum << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11094/11094-21.cpp b/uva_cpp_clean/11094/11094-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c76b861b71323ecb5ba939e17eb8e2422b3915a --- /dev/null +++ b/uva_cpp_clean/11094/11094-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11094 + Name: Continents + Problem: https://onlinejudge.org/external/110/11094.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int n, m; +char M[100][100], land; +int dfs(int y, int x) { + if (x < 0) x += m; + if (M[y][x] != land) + return 0; + M[y][x] = 0; + + int result = 1; + result += dfs(y, (x-1)%m); + result += dfs(y, (x+1)%m); + if (y > 0) result += dfs(y-1, x); + if (y> n >> m && (n||m)) { + for (int i=0; i> M[i]; + + cin >> y >> x; + land = M[y][x]; + dfs(y, x); + + int mx = 0; + for (int i=0; i traversal > flood fill + difficulty: easy + date: 31/Jan/2020 + by: @brpapa +*/ +#include +using namespace std; + +int R, C; +char grid[22][22]; + +int dr[4] = {1, 0,-1, 0}; +int dc[4] = {0, 1, 0,-1}; + +int floodFill(int r, int c, char target, char checked) { + if (c == -1) c = C-1; + if (c == C) c = 0; + + if (r < 0 || r == R || grid[r][c] != target) + return 0; + + grid[r][c] = checked; + + int ans = 1; + for (int d = 0; d < 4; d++) + ans += floodFill(r+dr[d], c+dc[d], target, checked); + return ans; +} + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + while (cin >> R >> C) { + for (int r = 0; r < R; r++) + for (int c = 0; c < C; c++) + cin >> grid[r][c]; + + int x, y; cin >> x >> y; + + char target = grid[x][y]; + floodFill(x, y, target, ' '); // invalida o componente conectado em que ele já está + + int ans = 0; + for (int r = 0; r < R; r++) + for (int c = 0; c < C; c++) + ans = max(ans, floodFill(r, c, target, ' ')); + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11094/11094-9.cpp b/uva_cpp_clean/11094/11094-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54ef1d02487a1e33e077c54f607439aa437e0f4e --- /dev/null +++ b/uva_cpp_clean/11094/11094-9.cpp @@ -0,0 +1,44 @@ +#include + +#define SIZE 25 + +using namespace std; + +int N, M, rpos, cpos, ans; +char grid[SIZE][SIZE], ch1, ch2; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +int floodfill(int r, int c){ + if( grid[r][c] == ch2 ) return 0; + grid[r][c] = ch2; + int ct = 1; + for(int d = 0; d < 4; d++) + if( 0 <= r + dr[d] and r + dr[d] < N ) + ct += floodfill( r + dr[d], ( c + dc[d] + M ) % M); + return ct; +} + +char WaterRepresentation(){ + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col++) + if( grid[row][col] != ch1 ) return grid[row][col]; + return '*'; +} + +int main(){ + while(~scanf("%d %d\n", &N, &M)){ + ans = 0; + for(int row = 0; row < N; row++) scanf("%s", grid[row]); + scanf("%d %d", &rpos, &cpos); + ch1 = grid[rpos][cpos]; + ch2 = WaterRepresentation(); + floodfill(rpos, cpos); + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col++) + if( grid[row][col] == ch1 ) + ans = max(ans, floodfill(row, col)); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/111/111-21.cpp b/uva_cpp_clean/111/111-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b02fba9fdab020d7377412199358d5aad989ed1 --- /dev/null +++ b/uva_cpp_clean/111/111-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 111 + Name: History Grading + Problem: https://onlinejudge.org/external/1/111.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int X[30], Y[30], DP[30][30]; + +int main(){ + int n, t; + cin>>n; + for (int i=1; i<=n; i++) { + cin>>t; + X[t] = i; + } + + while (cin>>t) { + Y[t]=1; + for (int i=2; i<=n; i++) { + cin>>t; + Y[t]=i; + } + + for (int i=1; i<=n; i++) + for (int j=1; j<=n; j++) + if (X[i] == Y[j]) + DP[i][j] = DP[i-1][j-1] + 1; + else + DP[i][j] = max(DP[i-1][j], DP[i][j-1]); + + cout << DP[n][n] << endl; + } +} diff --git a/uva_cpp_clean/111/111-4.cpp b/uva_cpp_clean/111/111-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52394502779f2d51701d6f8aeed559311bd3abe2 --- /dev/null +++ b/uva_cpp_clean/111/111-4.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int c[21]; +int good[21]; +int tab[21][21]; + +int lcs(int i, int j) +{ + if (i == 0 || j == 0) + return 0; + if (tab[i][j] != -1) + return tab[i][j]; + if (good[i] == c[j]) + { + tab[i][j] = 1 + lcs(i - 1, j - 1); + return tab[i][j]; + } + + tab[i][j] = max(lcs(i - 1, j), lcs(i, j - 1)); + return tab[i][j]; +} + +int main() +{ + int n, temp; + cin >> n; + + for (int i = 1; i <= n; i++) + { + cin >> temp; + good[temp] = i; + } + + while (cin >> temp) + { + c[temp] = 1; + for (int i = 2; i <= n; i++) + { + cin >> temp; + c[temp] = i; + } + + for (int i = 0; i < 21; i++) + for (int j = 0; j < 21; j++) + tab[i][j] = -1; + + cout << lcs(n, n) << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/111/111-5.cpp b/uva_cpp_clean/111/111-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..265d6deab8c9a59e94d9f0e4d67e6d8f5bc9a35e --- /dev/null +++ b/uva_cpp_clean/111/111-5.cpp @@ -0,0 +1,81 @@ + /*********************************************************** + ******************* Team : BUBT_HIDDEN ********************* + ************************************************************ + ***** ☺ ☺ ☺ ********** Shipu Ahamed ********** ☺ ☺ ☺ ******* + ***********************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main(){ + int n,c[20],r[20],lcs[21][21],x; + + scanf("%d\n",&n); + + for(int i=0;i + +using namespace std; + +int n, num, u[25], v[25], lis[25], LIS; + +int main(){ + scanf("%d", &n); + for(int i = 0; i < n; i++) scanf("%d", &u[i]); + while(scanf("%d", &num) == 1){ + v[num - 1] = u[0]; + for(int i = 1; i < n; i++) scanf("%d", &num), v[num - 1] = u[i]; + LIS = 0; + for(int i = 0; i < n; i++){ + lis[i] = 1; + for(int j = 0; j < i; j++) + if(v[i] > v[j] && lis[i] < lis[j] + 1) + lis[i] = lis[j] + 1; + if(lis[i] > LIS) LIS = lis[i]; + } + printf("%d\n", LIS); + } + return(0); +} diff --git a/uva_cpp_clean/11100/11100-13.cpp b/uva_cpp_clean/11100/11100-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92ec691279207a2cbd6d67176d7bbd51eab979e4 --- /dev/null +++ b/uva_cpp_clean/11100/11100-13.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int n; + + bool first = false; + + while (cin >> n , n != 0) + { + int maximum = 0, lastInsert = 0; + + map how; + + int* a = new int[n]; + + vector< set > ans; + vector< set > repeat(n + 1); + + for (int i = 0; i < n; i++) + { + int x; + + cin >> x; + + a[i] = x; + + how[x]++; + + maximum = max(how[x], maximum); + } + + for (int i = 0; i < n; i++) repeat[how[a[i]]].insert(a[i]); + + ans.resize(maximum); + + for (int i = n; i >= 0; i--) + { + while (!repeat[i].empty()) + { + int top = *repeat[i].begin(); + + repeat[i].erase(repeat[i].begin()); + + for (int j = 0; j < i; j++) + { + ans[lastInsert].insert(top); + + lastInsert = (lastInsert + 1) % maximum; + } + } + } + + if (first) cout << endl; + + else first = true; + + cout << maximum << endl; + + for (int i = 0; i < maximum; i++) + { + cout << *ans[i].begin(); + + ans[i].erase(ans[i].begin()); + + set::iterator it; + + for (it = ans[i].begin(); it != ans[i].end(); it++) cout << " " << *it; + + cout << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11100/11100-21.cpp b/uva_cpp_clean/11100/11100-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e6ea56dd1ed7f33da8c3d5c6dcd1151d4c3ede1 --- /dev/null +++ b/uva_cpp_clean/11100/11100-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11100 + Name: The Trip, 2007 + Problem: https://onlinejudge.org/external/111/11100.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[10143], n; + vector vec[10143]; + + bool frst = 1; + while (cin >> n && n) { + if (frst) frst = 0; + else cout << "\n"; + + for (int i=0; i> X[i]; + sort(X, X+n); + + int sz=1, cnt=1; + for (int i=1; i +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 2005 + +int dis[MAX][MAX]; + +int disX[4] = { 1, 0, -1, 0 }; +int disY[4] = { 0, 1, 0, -1 }; + +int n, m, ans, how = 0; + +bool safe(int x, int y) +{ + if (x >= 0 && y >= 0 && x <= 2000 && y <= 2000) return true; + + else return false; +} + +void initialize() +{ + ans = INT_MAX; + + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + dis[i][j] = INT_MAX; + } + } +} + +int main() +{ + while (cin >> n, n != 0) + { + initialize(); + + queue < pair > q, p; + + for (int i = 0; i < n; i++) + { + int x, y; + + cin >> x >> y; + + dis[x][y] = 0; + + q.push(make_pair(x, y)); + } + + cin >> m; + + for (int i = 0; i < m; i++) + { + int x, y; + + cin >> x >> y; + + p.push(make_pair(x, y)); + } + + while (!q.empty()) + { + pair head = q.front(); + + q.pop(); + + how++; + + int x = head.first; + + int y = head.second; + + for (int i = 0; i < 4; i++) + { + if (safe(x + disX[i], y + disY[i])) + { + if (dis[x + disX[i]][y + disY[i]] > dis[x][y] + 1) + { + dis[x + disX[i]][y + disY[i]] = dis[x][y] + 1; + + q.push(make_pair(x + disX[i], y + disY[i])); + } + } + } + } + + while (!p.empty()) + { + pair head = p.front(); + + p.pop(); + + int x = head.first; + + int y = head.second; + + int state = dis[x][y]; + + ans = min(ans, state); + } + + cout << ans << endl; + } +} diff --git a/uva_cpp_clean/11101/11101-21.cpp b/uva_cpp_clean/11101/11101-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c5d7e2989f966abc54f85c71cdd42e5ca3c033b --- /dev/null +++ b/uva_cpp_clean/11101/11101-21.cpp @@ -0,0 +1,88 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11101 + Name: Mall Mania + Problem: https://onlinejudge.org/external/111/11101.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------------ + +#define MAXN 100001 + +struct Point { + int x, y; +}; + +vector a, b; +int dist(int i, int j) { + return abs(a[i].x-b[j].x) + abs(a[i].y-b[j].y); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1, n; (n=readUInt()); ++cse) { + a.resize(n); + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11103 + Name: WFF 'N PROOF + Problem: https://onlinejudge.org/external/111/11103.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char str[143]; + while (cin >> str && str[0]!='0') { + vector cons, wffs; + int nots = 0; + for (int i=0; str[i]; ++i) + if (str[i]=='N') + ++nots; + else if (str[i]<='Z') + cons.push_back(str[i]); + else + wffs.push_back(str[i]); + + if (wffs.empty()) cout << "no WFF possible\n"; + else { + string result; + result += wffs.back(); + wffs.pop_back(); + while (!cons.empty() && !wffs.empty()) { + result = cons.back() + result + wffs.back(); + cons.pop_back(); + wffs.pop_back(); + } + while (nots--) + result = "N" + result; + cout << result << "\n"; + } + } +} diff --git a/uva_cpp_clean/11105/11105-19.cpp b/uva_cpp_clean/11105/11105-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3b206a0e3ecfb1b9194fb6bd8ed734ae4c77b1e --- /dev/null +++ b/uva_cpp_clean/11105/11105-19.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; + +int H[1000010], N = 0; +int Type[1000010]; +int semi[1000010], M = 0; + +void sieve() { + for (int i = 1; i < N; i++) { + for (int j = 1; j < i && H[j]*H[j] <= H[i]; j++) { + if (H[i]%H[j] == 0) { + int val = H[i]/H[j]; + val--; val /= 4; + if (Type[val] == 0) { + Type[i] = 1; + semi[M] = H[i]; + M++; + } + else Type[i] = 2; + break; + } + } + } +} + +int main() { + for (int i = 1; i < 1000010; i += 4) { + H[N] = i; + N++; + } + sieve(); + int h; + while (cin >> h && h != 0) { + printf("%d %ld\n", h, upper_bound(semi, semi+M, h) - semi); + } + return 0; +} diff --git a/uva_cpp_clean/11108/11108-21.cpp b/uva_cpp_clean/11108/11108-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..651b9498cbddda309035ebd1854e305adc3e9312 --- /dev/null +++ b/uva_cpp_clean/11108/11108-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11108 + Name: Tautology + Problem: https://onlinejudge.org/external/111/11108.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +string s; +int vals, p; +bool calc() { + char ch = s[p++]; + switch (ch) { + case 'p': case 'q': + case 'r': case 's': case 't': + return (1<<(ch-'p')) & vals; + case 'N': return !calc(); + case 'E': return calc() ==calc(); + case 'K': return calc() & calc(); + case 'A': return calc() | calc(); + case 'C': return !calc() | calc(); + } + return 0; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> s && s[0]!='0') { + for (vals=(1<<5)-1; vals>=0; --vals) { + p = 0; + if (!calc()) { + cout << "not\n"; + goto fin; + } + } + cout << "tautology\n"; + fin:; + } +} diff --git a/uva_cpp_clean/11108/11108-9.cpp b/uva_cpp_clean/11108/11108-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cca9ea6682e0470f50a39353f5c18807fc3b8262 --- /dev/null +++ b/uva_cpp_clean/11108/11108-9.cpp @@ -0,0 +1,32 @@ +#include + +#define SIZE 110 + +using namespace std; + +int p, q, r, s, t, idx; +char wff[SIZE]; +bool ans; + +bool is_tautology(int mask){ + char ch = wff[idx++]; + if( 'p' <= ch and ch <= 't' ) return ( mask >> ( ch - 'p' ) ) & 1 ; + if( ch == 'N' ) return not is_tautology(mask); + bool a = is_tautology(mask); + bool b = is_tautology(mask); + if( ch == 'K' ) return ( a and b ); + if( ch == 'A' ) return ( a or b ); + if( ch == 'C' ) return ( (not a) or b ); + if( ch == 'E' ) return ( a == b ); +} + +int main(){ + while(scanf("%s", wff), wff[0] != '0'){ + ans = true; + for(int mask = 0; ans and mask < 32; mask++) + idx = 0, ans &= is_tautology(mask); + if( ans ) puts("tautology"); + else puts("not"); + } + return(0); +} diff --git a/uva_cpp_clean/11110/11110-21.cpp b/uva_cpp_clean/11110/11110-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0b9b2c079ec7f5d4db4962583c48698bc6e2a52 --- /dev/null +++ b/uva_cpp_clean/11110/11110-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11110 + Name: Equidivisions + Problem: https://onlinejudge.org/external/111/11110.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int n; char A[120][120]; +int dfs(int y, int x, char c) { + if (A[y][x] != c) return 0; + A[y][x] = -1; + int r = 1; + + if (x > 0) r += dfs(y, x-1, c); + if (x 0) r += dfs(y-1, x, c); + if (y=0) { + if (seen[A[i][j]]) + return 0; + else { + seen[A[i][j]] = 1; + dfs(i, j, A[i][j]); + } + } + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string line; + while (cin >> n && n) { + for (int i=0; i>y>>x) + A[y-1][x-1] = i; + } + + cout << (check() ? "good\n" : "wrong\n"); + } +} diff --git a/uva_cpp_clean/11110/11110-6.cc b/uva_cpp_clean/11110/11110-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..c4243aae2bcc4ba057fc9bb7b037450d42fad6d2 --- /dev/null +++ b/uva_cpp_clean/11110/11110-6.cc @@ -0,0 +1,47 @@ +// https://uva.onlinejudge.org/external/111/11110.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + for(;;){ + int n,y,x; + cin>>n; + if(!n)break; + vvi a(n,vi(n,n)),s(n,vi(n)); + string t; + getline(cin,t); + for(int i=0;i>y>>x;j++){ + y--;x--; + a[y][x]=i+1; + } + } + functiondfs=[&](int y,int x,int k){ + s[y][x]=k; + int i[]={-1,0,0,1}; + int j[]={0,-1,1,0}; + for(int l=0;l<4;l++){ + int u=y+i[l],v=x+j[l]; + if(u>=0&&u=0&&v + +#define SIZE 110 +#define LIM 1000 + +using namespace std; + +int n, r, c, ct, grid[SIZE][SIZE], CC; +char line[LIM], *p; +bool ok; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < n and C < n ); +} + +void floodfill(int r, int c, int p){ + if( not is_possible(r, c) ) return; + if( grid[r][c] != p ) return; + grid[r][c] = -1; + for(int d = 0; d < 4; d++) + floodfill(r + dr[d], c + dc[d], p); +} +int main(){ + while(scanf("%d\n", &n), n){ + CC = 0, ok = true; + memset(grid, 0, sizeof grid); + for(int it1 = 1; it1 < n; it1++){ + ct = 0; + cin.getline(line, LIM); + p = strtok(line, " "); + while(p){ + r = atoi(p); + p = strtok(NULL, " "); + c = atoi(p); + p = strtok(NULL, " "); + if( grid[r - 1][c - 1] == 0 ) ct++; + grid[r - 1][c - 1] = it1; + } + if( ct != n ) ok = false; + } + for(int row = 0; ok and row < n; row++) + for(int col = 0; col < n; col++) + if( grid[row][col] != -1 ) + floodfill(row, col, grid[row][col]), CC++; + puts( ok and CC == n ? "good" : "wrong" ); + } + return(0); +} diff --git a/uva_cpp_clean/11115/11115-19.cpp b/uva_cpp_clean/11115/11115-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a5698b5519e20c3c875a155343557711a055ae7 --- /dev/null +++ b/uva_cpp_clean/11115/11115-19.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +using namespace std; + + + +int main() { + int N, D; + while (cin >> N >> D && !(N == 0 && D == 0)) { + __int128_t p = 1; + for (int i = 0; i < D; i++) p *= N; + stack s; + while (p) { + s.push(p%10); + p /= 10; + } + while (!s.empty()) { + printf("%d", s.top()); + s.pop(); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/1112/1112-10.cpp b/uva_cpp_clean/1112/1112-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..249dc5ddfd3950ad33484ea713197c77f914154b --- /dev/null +++ b/uva_cpp_clean/1112/1112-10.cpp @@ -0,0 +1,100 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e4+9; + +int dx[]={-1,1,0,0}; +int dy[]={0,0,-1,1}; + +void solve() { + + int n,e,t,m,a,b,w; + + cin>>n>>e>>t>>m; + + vector> adj[n+2]; + + for(int i=0;i>a>>b>>w; + adj[b].push_back({a,w}); + } + + int vis[n+2]; + int dis[n+2]; + priority_queue> pq; + + for(int i=0;i<=n;i++) { + dis[i]=inf; + vis[i]=0; + } + + dis[e]=0; + pq.push({0,e}); + + while (!pq.empty()){ + + a = pq.top().second; pq.pop(); + + if(vis[a]) continue; + vis[a]=1; + + for(auto u:adj[a]) { + b=u.first; + w=u.second; + + if(dis[a]+w < dis[b]) { + dis[b]=dis[a]+w; + pq.push({-1*dis[b],b}); + } + } + + } + + int ans=0; + for(int i=1;i<=n;i++) { + if(dis[i]<=t) ans++; + } + + cout<>t; + while (t--) { + solve(); + if(t!=0) cout<<"\n"; + } + +} + diff --git a/uva_cpp_clean/1112/1112-13.cpp b/uva_cpp_clean/1112/1112-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17b9a5d5ced5235f2b12b0fc717e81399ffacb66 --- /dev/null +++ b/uva_cpp_clean/1112/1112-13.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, m, t, e, ans = 0, lim; + +ofstream fout("f.out"); + +vector dis; +vector > > graph; + +class comparator +{ +public: + + bool operator()(pair &child1, pair &child2) + { + return child1.second > child2.second; + } +}; + +void dijkstra() +{ + priority_queue , vector >, comparator>pq; + + dis[e] = 0; + + pq.push(make_pair(e, dis[e])); + + while (!pq.empty()) + { + pair head = pq.top(); + + pq.pop(); + + int headVer = head.first; + int headDis = head.second; + + int childVer, childWeight; + + for (int i = 0; i < (int)graph[headVer].size(); i++) + { + childVer = graph[headVer][i].first; + + childWeight = graph[headVer][i].second; + + if (headDis + childWeight < dis[childVer]) + { + dis[childVer] = headDis + childWeight; + + pq.push(make_pair(childVer, dis[childVer])); + } + } + } +} + +void initialize(int n) +{ + ans = 0; + + graph.clear(); + graph.resize(n + 1); + + dis.clear(); + dis.resize(n + 1, INT_MAX); +} + +int main() +{ + bool first = true; + + cin >> t; + + while (t--) + { + cin >> n >> e >> lim >> m; + + initialize(n); + + for (int i = 0; i < m; i++) + { + int x, y, w; + + cin >> x >> y >> w; + + graph[y].push_back(make_pair(x, w)); + } + + dijkstra(); + + for (int i = 1; i <= n; i++) + { + if (dis[i] <= lim) ans++; + } + + if (!first) cout << endl; + + else first = false; + + cout << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1112/1112-14.cpp b/uva_cpp_clean/1112/1112-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f750db787a3a956bf5b214360416b05dbee8fc3 --- /dev/null +++ b/uva_cpp_clean/1112/1112-14.cpp @@ -0,0 +1,119 @@ +/*************************************************** + * Problem name : 1112 Mice and Maze.cpp + * Problem Link : https://uva.onlinejudge.org/external/11/1112.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-09 + * Problem Type : Graph(dijkstra) + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 105 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +struct node { + int u, w; + node(int __u, int __w) { + u = __u; + w = __w; + } +}; +bool operator <(node a , node b) { + return a.w > b.w; +} +vectoradj[MAX]; +int cost[MAX][MAX], dis[MAX]; +int dijkstra(int s, int d) { + priority_queueQ; + dis[s] = 0; + Q.push(node(s, dis[s])); + while (!Q.empty()) { + node top = Q.top(); + Q.pop(); + int u = top.u; + for (int i = 0; i < (int)adj[u].size(); i++) { + int v = adj[u][i]; + int w = cost[u][v]; + if (dis[u] + w < dis[v]) { + dis[v] = dis[u] + w; + Q.push(node(v, dis[v])); + } + } + } + return dis[d]; +} +void clear() { + for (int i = 0; i <= MAX; i++) { + dis[i] = 1000000; + } +} +int main () { + //~ __FastIO; + int tc; + int N, E, T, M; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d %d %d %d", &N, &E, &T, &M); + for (int i = 0; i < M; i++) { + int u, v, w; + scanf("%d %d %d", &u, &v, &w); + adj[u].pb(v); + cost[u][v] = w; + } + int cnt = 0; + for (int i = 1; i <= N; i++) { + clear(); + int chk = dijkstra(i, E); + if(chk<=T) cnt++; + } + printf("%d\n", cnt); + if (t != tc) nl; + SET(cost, 0); + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + } + } + return 0; +} diff --git a/uva_cpp_clean/1112/1112-20.cpp b/uva_cpp_clean/1112/1112-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ed8b00b8b90f933c4ca0a2f655cd5ef9b20c9d5 --- /dev/null +++ b/uva_cpp_clean/1112/1112-20.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +const int INF = ((1<<31) - (1)); +const int MAXN = 100; +vector < int > edge[MAXN+2]; +vector < int > cost[MAXN+2]; +int d[MAXN+2]; + +struct data { + int city, dist; + bool operator < ( const data & p ) const { + return dist > p.dist; + } +} ; + +void dijkstra( int source ) { + data u, v; + priority_queue < data > q; + for( int i = 0 ; i <= MAXN ; ++i ) { + d[i] = INF; + } + + u.city = source, u.dist = 0; + q.push( u ), d[source] = 0; + while( !q.empty() ) { + u = q.top(); + q.pop(); + for( int i = 0 ; i < edge[u.city].size() ; ++i ) { + v.city = edge[u.city][i]; + v.dist = cost[u.city][i] + d[u.city]; + if( d[v.city] > v.dist ) { + d[v.city] = v.dist; + q.push( v ); + } + } + } +} + +int main( int argc, char ** argv ) { + int tc; + scanf( "%d", &tc ); + for( int nCase = 1 ; nCase <= tc ; ++nCase ) { + int N, E, T, M; + scanf( "%d %d %d %d", &N, &E, &T, &M ); + for( int i = 1 ; i <= M ; ++i ) { + int a, b, c; + scanf( "%d %d %d", &a, &b, &c ); + + edge[b].push_back( a ); + + cost[b].push_back( c ); + } + + dijkstra( E ); + + int mice = 0; + for( int i = 1 ; i <= N ; ++i ) { + if( d[i] <= T ) { + ++mice; + } + } + + if( nCase > 1 ) { + puts( "" ); + } + printf( "%d\n", mice ); + + for( int i = 1 ; i <= N ; ++i ) { + edge[i].clear(); + cost[i].clear(); + } + } + return 0; +} diff --git a/uva_cpp_clean/1112/1112-21.cpp b/uva_cpp_clean/1112/1112-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be0de2560fcfc79e05c380681f7b704314b59cd2 --- /dev/null +++ b/uva_cpp_clean/1112/1112-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1112 + Name: Mice and Maze + Problem: https://onlinejudge.org/external/11/1112.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct UD { + int u, d; + UD(int u, int d):u(u), d(d){} + bool operator < (const UD& u) const { return d > u.d; } +}; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector adj[117]; + bool S[117]; + int u, v, tm; + int T; cin >> T; + while (T--) { + int n, m, t, s; + cin >> n >> s >> t >> m; + + for (int i=0; i> u >> v >> tm; + adj[v-1].push_back(UD(u-1, tm)); + } + + priority_queue q; + q.push(UD(s-1, 0)); + int cnt = 0; + while (!q.empty()) { + UD ud = q.top(); q.pop(); + int u = ud.u; + if (S[u]) continue; + S[u] = ++cnt; + + for (UD vd: adj[u]) if (!S[vd.u]) { + int v = vd.u; + UD tt(v, ud.d+vd.d); + if (tt.d <= t) + q.push(tt); + } + } + cout << cnt << endl; + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/1112/1112-4.cpp b/uva_cpp_clean/1112/1112-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d4a4d7a0cd3915fb0c9bf7c40f249620bffc92f --- /dev/null +++ b/uva_cpp_clean/1112/1112-4.cpp @@ -0,0 +1,72 @@ +#include + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + int n, e, t, m; + std::cin >> n >> e >> t >> m; + e--; + + int *tab = new int[n * n]; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + { + tab[i * n + j] = 0; + } + else + { + tab[i * n + j] = 1000; + } + } + } + + for (int i = 0; i < m; i++) + { + int from, to, time; + std::cin >> from >> to >> time; + + from--; + to--; + + tab[from * n + to] = time; + } + + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + tab[i * n + j] = std::min(tab[i * n + j], tab[i * n + k] + tab[k * n + j]); + } + } + } + + int count = 0; + + for (int i = 0; i < n; i++) + { + if (tab[i * n + e] <= t) + { + count++; + } + } + + std::cout << count << std::endl; + + if (tests != 0) + { + std::cout << std::endl; + } + + delete[] tab; + } +} diff --git a/uva_cpp_clean/1112/1112-9.cpp b/uva_cpp_clean/1112/1112-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c56c516679e08183707cec28b170ba4f54488675 --- /dev/null +++ b/uva_cpp_clean/1112/1112-9.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +const int SIZE = 110, INF = 1000000000; + +typedef pair ii; + +int tc, V, S, T, E, a, b, c, ans, dis[SIZE]; +vector AdjList[SIZE], W[SIZE]; + +void Dijkstra(int source){ + fill(dis, dis + V + 1, INF); + dis[source] = 0; + set st; + st.insert( ii(0, source) ); + while( not st.empty() ){ + int u = st.begin() -> second; + st.erase( st.begin() ); + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j], w = W[u][j]; + if( dis[v] > dis[u] + w ) + dis[v] = dis[u] + w, st.insert( ii(dis[v], v) ); + } + } +} + +void clear_(){ + for(int i = 1; i <= V; i++) AdjList[i].clear(), W[i].clear(); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d %d", &V, &S, &T, &E); + for(int i = 0; i < E; i++){ + scanf("%d %d %d", &a, &b, &c); + AdjList[b].push_back(a); + W[b].push_back(c); + } + Dijkstra(S); + ans = 0; + for(int i = 1; i <= V; i++) + if( dis[i] <= T ) ans++; + printf("%d\n", ans); + if( tc ) putchar('\n'); + clear_(); + } + return(0); +} diff --git a/uva_cpp_clean/11121/11121-21.cpp b/uva_cpp_clean/11121/11121-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db423261c55d606f84b0a6e1fffb76b34e70b53e --- /dev/null +++ b/uva_cpp_clean/11121/11121-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11121 + Name: Base -2 + Problem: https://onlinejudge.org/external/111/11121.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int V[10000], T, n; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cout << "Case #" << cse << ": "; + cin >> n; + + unsigned int r = (n + 0xAAAAAAAA) ^ 0xAAAAAAAA; + int sz = 0; + while (r) { + V[sz++] = r&1; + r >>= 1; + } + + if (sz>0) { + int i = sz-1; + while (!V[i]) --i; + while (i>=0) cout << V[i--]; + } + else cout << 0; + cout << '\n'; + } +} diff --git a/uva_cpp_clean/11127/11127-21.cpp b/uva_cpp_clean/11127/11127-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a40fedbc75833badd056bd35494773c45cbcb05b --- /dev/null +++ b/uva_cpp_clean/11127/11127-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11127 + Name: Triple-Free Binary Strings + Problem: https://onlinejudge.org/external/111/11127.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int n; +char str[43]; +bool check(int pos, int len) { + for (int i=0; i memo; +int backtrack(int pos) { + for (int len=pos/3; len>0; --len) + if (check(pos-3*len, len)) + return 0; + + if (pos == n) return 1; + if (str[pos] != '*') + return backtrack(pos+1); + + //auto rr = memo.emplace(str, 0); + //if (!rr.second) return rr.first->second; + + str[pos] = '0'; + int sum = backtrack(pos+1); + str[pos] = '1'; + sum += backtrack(pos+1); + str[pos] = '*'; + //rr.first->second = sum; + return sum; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; cin >> n >> str; ++cse) + cout << "Case " << cse << ": " << backtrack(0) << '\n'; +} diff --git a/uva_cpp_clean/11127/11127-9.cpp b/uva_cpp_clean/11127/11127-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c9bf04ae732ce231e58074ac153488836cf79a8 --- /dev/null +++ b/uva_cpp_clean/11127/11127-9.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +const int LEN = 35; + +int tc, n, ans; +char P[LEN], line[LEN + 5]; + +inline void print() { + printf("Case %d: %d\n", ++tc, ans); +} + +bool check(int mask, int sz) { + int power = (1 << sz) - 1; + int s1 = mask bitand power; + mask >>= sz; + int s2 = mask bitand power; + mask >>= sz; + int s3 = mask bitand power; + return (s1 == s2 and s2 == s3); +} + +bool isValid(int mask, int len) { + ++len; + for (int sz = 1; sz * 3 <= len; sz++) + if (check(mask, sz)) return false; + return true; +} + +inline int mark(int mask, int bit) { + return ((mask << 1) bitor bit); +} + +void backtrack(int id = 0, int mask = 0) { + if (id == n) { + ans++; + return; + } + int mask_0 = mark(mask, 0); + int mask_1 = mark(mask, 1); + if (P[id] == '*') { + P[id] = '0'; + if (isValid(mask_0, id)) backtrack(id + 1, mask_0); + P[id] = '1'; + if (isValid(mask_1, id)) backtrack(id + 1, mask_1); + P[id] = '*'; + } + else if (P[id] == '0') { + if (isValid(mask_0, id)) backtrack(id + 1, mask_0); + } + else { + if (isValid(mask_1, id)) backtrack(id + 1, mask_1); + } +} + +inline void init() { + ans = 0; +} + +bool read() { + init(); + scanf("%d", &n); + if (n == 0) return false; + scanf("%s\n", P); + return true; +} + +int main() { + while (read()) { + backtrack(); + print(); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/11130/11130-11.cpp b/uva_cpp_clean/11130/11130-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12927b83a3e244e901c19a219ae22294e6c1dbe9 --- /dev/null +++ b/uva_cpp_clean/11130/11130-11.cpp @@ -0,0 +1,65 @@ +#include +/* +Problem: 11130 - Billiard bounces +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2071 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define PI acos(-1) + +int a, b, v, A, s; + +int main () { + + while ( scanf("%d%d%d%d%d", &a, &b, &v, &A, &s) && s ){ + + double xi = a/2, yi = b/2; // initial position + + double dist = v*s/2.0; // distance in total + double xd = dist*cos(A*PI/180.0), yd = dist*sin(A*PI/180.0); // distance in the x, y components + + printf("%d %d\n", (int)((xd+xi)/a), (int)((yd+yi)/b)); + } + + return 0; +} +/* +Sample input:- +----------------- +100 50 10 90 10 +100 50 10 0 40 +100 100 10 45 15 +100 50 10 1 200 +100 50 10 89 200 +100 50 10 45 1000 +100 100 10 30 200 +0 0 0 0 0 + +Sample output:- +----------------- +0 1 +2 0 +1 1 +10 0 +0 20 +35 71 +9 5 + +Resources:- +------------- + +Explanation: +--------------- + +*/ + diff --git a/uva_cpp_clean/11130/11130-19.cpp b/uva_cpp_clean/11130/11130-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12a72ae53bf20a032c1e23b9c26f3da689557a31 --- /dev/null +++ b/uva_cpp_clean/11130/11130-19.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +const double pi = 2*asin(1); + +int main() { + double a, b, v, A, s; + while (scanf("%lf %lf %lf %lf %lf", &a, &b, &v, &A, &s) && !(a == 0 && b == 0 && v == 0 && A == 0 && s == 0)) { + double d = (v*s)/2; + int x = floor((d*cos(pi*A/180) - a/2)/a); + if (x >= 0) x += 1; + else x = 0; + int y = floor((d*sin(pi*A/180) - b/2)/b); + if (y >= 0) y += 1; + else y = 0; + printf("%d %d\n", x, y); + } + return 0; +} diff --git a/uva_cpp_clean/11131/11131-21.cpp b/uva_cpp_clean/11131/11131-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a29a1df64d9453e9585a2ea87e3b2cb60ef1a500 --- /dev/null +++ b/uva_cpp_clean/11131/11131-21.cpp @@ -0,0 +1,58 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11131 + Name: Close Relatives + Problem: https://onlinejudge.org/external/111/11131.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +unordered_map ids; +string names[5555], line; +vector adj[5555]; + +void dfs1(int u) { + for (int v: adj[u]) + dfs1(v); + cout << names[u] << '\n'; +} + +void dfs2(int u) { + for (auto it=adj[u].rbegin(); it!=adj[u].rend(); ++it) + dfs2(*it); + cout << names[u] << '\n'; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int cnt = 1, n = 0; + while (getline(cin, line)) { + int u = -1; + for (int i=0, j=0; i<=line.length(); ++i) + if (line[i]==',' || !line[i]) { + names[n] = line.substr(j, i-j); + auto r = ids.emplace(names[n], n); + if (r.second) ++n; + if (u < 0) u = r.first->second; + else adj[u].push_back(r.first->second); + j = i+1; + } + if (u>=0 && adj[u].size()>1) + cnt = 2; + } + + cout << cnt << "\n\n"; + dfs1(0); + if (cnt==2) { + cout << '\n'; + dfs2(0); + } +} diff --git a/uva_cpp_clean/11136/11136-14.cpp b/uva_cpp_clean/11136/11136-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d96c6529896f1cfa476c72c6f8b400f6e5f5d12d --- /dev/null +++ b/uva_cpp_clean/11136/11136-14.cpp @@ -0,0 +1,85 @@ +/*************************************************** + * Problem name : 11136 Hoax or what.cpp + * Problem Link : https://uva.onlinejudge.org/external/111/11136.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-25 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ll n, k; + mapmp; + while (scanf("%lld", &n) == 1 && n != 0) { + ll ans = 0; + for (int d = 1; d <= n; d++) { + scanf("%lld", &k); + for (int c = 1; c <= k; c++) { + ll x; + scanf("%lld", &x); + mp[x]++; + } + map::iterator it; + it = mp.begin(); + ll mn = it->first; + it = mp.end(); + it--; + ll mx = it->first; + mp[mn]--; + mp[mx]--; + if (mp[mn] == 0) mp.erase(mn); + if (mp[mx] == 0) mp.erase(mx); + ans += (mx - mn); + } + printf("%lld\n", ans); + mp.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/11136/11136-21.cpp b/uva_cpp_clean/11136/11136-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1104fa867abb14041f86f355b8b0a240e156477d --- /dev/null +++ b/uva_cpp_clean/11136/11136-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11136 + Name: Hoax or what + Problem: https://onlinejudge.org/external/111/11136.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +int main(){ + int n, m, x; + while (scanf("%d", &n)==1 && n) { + int elems = 0; + priority_queue qmx; + priority_queue, greater > qmn; + + + long long sum = 0; + for (int i=0; i(); + qmn = priority_queue, greater >(); + } + } + + printf("%lld\n", sum); + } +} diff --git a/uva_cpp_clean/11136/11136-3.cpp b/uva_cpp_clean/11136/11136-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78a61c73adf01169717db39f4698231f943e9ee9 --- /dev/null +++ b/uva_cpp_clean/11136/11136-3.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +multiset bills; +multiset::iterator myEnd; + +int main () +{ + int days, records, recordsPerDay, tempInt; + long long result; + while ( scanf("%d",&days), days ) + { + result = 0; + bills.clear(); + while ( days-- ) + { + scanf("%d",&recordsPerDay); + while ( recordsPerDay-- ) + { + scanf("%d",&tempInt); + bills.insert(tempInt); + } + myEnd = bills.end(); myEnd--; + result += *myEnd - *bills.begin(); + bills.erase(myEnd); + bills.erase(bills.begin()); + } + printf("%lld\n",result); + } +} diff --git a/uva_cpp_clean/11136/11136-6.cc b/uva_cpp_clean/11136/11136-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..e570424db9551db8f6a715ccfadaece954c1d21f --- /dev/null +++ b/uva_cpp_clean/11136/11136-6.cc @@ -0,0 +1,30 @@ +// http://uva.onlinejudge.org/external/111/11136.pdf +#include +#include + +using namespace std; + +int main() { + while (true) { + int n; + cin >> n; + if (!n) break; + multiset s; + long long c = 0; + for (int i = 0; i < n; i++) { + int k; + cin >> k; + for (int j = 0; j < k; j++) { + int x; + cin >> x; + s.insert(x); + } + auto a = s.begin(); + auto b = s.rbegin(); + c += *b - *a; + s.erase(a); + s.erase(s.find(*b)); + } + cout << c << endl; + } +} diff --git a/uva_cpp_clean/11136/11136-9.cpp b/uva_cpp_clean/11136/11136-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a2d3a67b2ab89da9fdf28faea1de696cce33503 --- /dev/null +++ b/uva_cpp_clean/11136/11136-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +long long n, bills, num, _min, _max, ans; +multiset ::iterator it; + +int main(){ + while(scanf("%lld", &n), n){ + multiset v; + ans = 0; + while(n--){ + scanf("%lld", &bills); + while(bills--) scanf("%lld", &num), v.insert(num); + _min = *v.begin(), v.erase(v.begin()); + it = --v.end(), _max = *it, v.erase(it); + ans += _max - _min; + } + printf("%lld\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11137/11137-14.cpp b/uva_cpp_clean/11137/11137-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2e8a24b8c8a607fe184760d86fd927bb2db8234b --- /dev/null +++ b/uva_cpp_clean/11137/11137-14.cpp @@ -0,0 +1,52 @@ +/*************************************************** + * Problem name : 11137 - Ingenuous Cubrency.cpp + * Problem Link : https://uva.onlinejudge.org/external/111/11137.pdf + * OJ : Uva + * Verdict : AC + * Date : 13.05.2017 + * Problem Type : DP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define debug printf("#########\n") +#define nl printf("\n") +#define MAX 100000 +using namespace std; +typedef long long ll; +int coin[] = {1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 4913, 5832, 6859, 8000, 9261}; +ll make; +ll dp[22][10002]; +ll call(ll i, ll amount) { + if (i >= 21) { + if (amount == 0) return 1; + else return 0; + } + if (dp[i][amount] != -1) return dp[i][amount]; + ll ret1 = 0, ret2 = 0; + if (amount - coin[i] >= 0) ret1 = call(i, amount - coin[i]); + ret2 = call(i + 1, amount); + //~ return dp[i][amount] = ret1 | ret2; ///check make or not... + return dp[i][amount] = ret1 + ret2; ///check how many away... +} +int main () { + memset(dp, -1, sizeof(dp)); + while (scanf("%lld", &make) == 1) { + printf("%lld\n", call(0, make)); + } + return 0; +} + diff --git a/uva_cpp_clean/11137/11137-19.cpp b/uva_cpp_clean/11137/11137-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5bdc19ac938b82ebb3a860c950409b26d2f722b --- /dev/null +++ b/uva_cpp_clean/11137/11137-19.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main() { + long long ways[10003]; + for (int i = 0; i < 10003; i++) ways[i] = 1; + for (int j = 2; j <= 21; j++) { + int pot = j*j*j; + for (int i = pot; i < 10003; i++) ways[i] += ways[i-pot]; + } + int n; + while (scanf("%d", &n) != EOF) { + printf("%lld\n", ways[n]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11137/11137-21.cpp b/uva_cpp_clean/11137/11137-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1bb80c2a4a15302abb7f1d00f386130e8d46b61 --- /dev/null +++ b/uva_cpp_clean/11137/11137-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11137 + Name: Ingenuous Cubrency + Problem: https://onlinejudge.org/external/111/11137.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +long long DP[20000]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + DP[0] = 1; + for (int i=1, c; (c=i*i*i)<10000; ++i) + for (int j=0; j<=10000; ++j) + DP[j+c] += DP[j]; + + int n; + while (cin >> n) + cout << DP[n] << "\n"; +} diff --git a/uva_cpp_clean/11137/11137-5.cpp b/uva_cpp_clean/11137/11137-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8a60e068479d6c334718135ccc2d94165ee2d862 --- /dev/null +++ b/uva_cpp_clean/11137/11137-5.cpp @@ -0,0 +1,101 @@ +/****************************************************************** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_HIDDEN *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d",a) +#define PLN(a) printf("%lld",a) + + +#define ff first +#define se second +#define pb push_back +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +////============ CONSTANT ===============//// +#define mx (2000010) +#define inf (1<<30) //infinity value +#define eps 1e-9 +#define mod 1000000007 +////====================================//// +int main() +{ + ll coin[100],way[20000]={0},i,j,a,p; + + for(i=1,j=0;i<22;i++) + { + coin[j++]=i*i*i; + } + way[0]=1; + for(i=0;i<22;i++) + { + for(j=coin[i];j<=10010;j++) + { + way[j]+=way[j-coin[i]]; + } + } + while(sii(a)==1) + { + PL(way[a]/2); + } + + return 0; +} diff --git a/uva_cpp_clean/11137/11137-6.cpp b/uva_cpp_clean/11137/11137-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fedbf83e6e5bad0d2fc11fb10f6b8dc1b0e3a8e --- /dev/null +++ b/uva_cpp_clean/11137/11137-6.cpp @@ -0,0 +1,37 @@ +/* + dynamic programming > subset sum > with repetition + difficulty: easy + date: 21/Feb/2020 + by: @brpapa +*/ +#include +#include +#include +#define ll long long +using namespace std; + +vector coins; + +ll memo[25][10000]; +ll cc(int c, int v) { + // c-ésima moeda, valor restante para trocar v + + if (v == 0) return 1ll; + if (v < 0 || c == coins.size()) return 0ll; + + ll &ans = memo[c][v]; + if (ans != -1) return ans; + + return ans = cc(c, v-coins[c]) + cc(c+1, v); +} + +int main() { + for (int c = 1; c <= 21; c++) + coins.push_back(c*c*c); + + memset(memo, -1, sizeof memo); // pois as moedas não mudam + int V; + while (cin >> V) + cout << cc(0, V) << endl; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11137/11137-9.cpp b/uva_cpp_clean/11137/11137-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e4772d60f61dcc3eeb05361f606c69bbf0750df --- /dev/null +++ b/uva_cpp_clean/11137/11137-9.cpp @@ -0,0 +1,24 @@ +#include + +#define MAX_N 30 +#define MAX_W 10010 + +using namespace std; + +int v[MAX_N],n; +long long memo[MAX_N][MAX_W]; + +long long ways(int id,int sum){ + if(id==22 || sum<0) return 0; + if(sum==0) return 1; + if(~memo[id][sum]) return memo[id][sum]; + return memo[id][sum] = ways(id+1,sum)+ways(id,sum-v[id]); +} + +int main(){ + for(int i=1;i<22;i++) v[i] = i*i*i; + memset(memo,-1,sizeof memo); + while(scanf("%d",&n)==1) + printf("%lld\n",ways(1,n)); + return(0); +} diff --git a/uva_cpp_clean/11138/11138-19.cpp b/uva_cpp_clean/11138/11138-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e0ea702b5d6f6f1b2f2827563a65f9cbc445756 --- /dev/null +++ b/uva_cpp_clean/11138/11138-19.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +int Bolts, Nuts, mf, f, s, t, INF = 2000000; +int res[1004][1004]; +vi p; + +void augment(int v, int minEdge) { + if (v == s) { + f = minEdge; + return; + } else if (p[v] != -1) { + augment(p[v], min(minEdge, res[p[v]][v])); + res[p[v]][v] -= f; res[v][p[v]] += f; + } +} + +int main() { + int N; + cin >> N; + for (int cases = 1; cases <= N; cases++) { + cin >> Bolts >> Nuts; + for (int i = 0; i < Bolts+Nuts+2; i++) + for (int j = 0; j < Bolts+Nuts+2; j++) + res[i][j] = 0; + + for (int i = 1; i <= Bolts; i++) + for (int j = 1; j <= Nuts; j++) + cin >> res[i][j+Bolts]; + + for (int i = 1; i <= Bolts; i++) res[0][i] = 1; + for (int i = 1; i <= Nuts; i++) res[i+Bolts][Bolts+Nuts+1] = 1; + s = 0, t = Bolts+Nuts+1; + mf = 0; + while (true) { + f = 0; + vi dist(Bolts+Nuts+2, INF); dist[s] = 0; + queue q; + q.push(s); + p.assign(Bolts+Nuts+2, -1); + while (!q.empty()) { + int u = q.front(); q.pop(); + if (u == t) break; + for (int v = 0; v < Bolts+Nuts+2; v++) { + if (res[u][v] > 0 && dist[v] == INF) { + dist[v] = dist[u] + 1, q.push(v), p[v] = u; + } + } + } + augment(t, INF); + if (f == 0) break; + mf += f; + } + printf("Case %d: a maximum of %d nuts and bolts can be fitted together\n", cases, mf); + } + return 0; +} diff --git a/uva_cpp_clean/11138/11138-21.cpp b/uva_cpp_clean/11138/11138-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aae5f606290e4c7b4dbb6bb9a0de38b344a649d4 --- /dev/null +++ b/uva_cpp_clean/11138/11138-21.cpp @@ -0,0 +1,91 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11138 + Name: Nuts and Bolts + Problem: https://onlinejudge.org/external/111/11138.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------- + +vector adj[555]; +int matchR[555]; +bool seen[555]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = true; + if (matchR[v]<0 || bpm(matchR[v])) { + matchR[v] = u; + return true; + } + } + return false; +} + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + for (int cse=1; cse<=T; ++cse) { + int n = readUInt(), + m = readUInt(); + + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11147 + Name: KuPellaKeS BST + Problem: https://onlinejudge.org/external/111/11147.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int X[1143], Y[1143], n; + +void solve(int l, int r) { + if (r - l == 0 || l >= n) { + return; + } + if (r - l == 1) { + cout << X[l]; + return; + } + + int bdiff = 0xfffffff, idx = l, mxl = 0; + for (int i=l; i mxl) { + mxl = sl; + idx = i; + } + } + + cout << X[idx] << "("; + solve(l, idx); + if (idx != l && idx != r-1) { + cout << ","; + } + solve(idx+1, r); + cout << ")"; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n; + for (int i=0; i> X[i]; + } + + sort(X, X+n); + for (int i=1; i<=n; ++i) { + Y[i] = Y[i-1] + X[i-1]; + } + Y[n+1] = Y[n]; + + cout << "Case #" << cse << ": "; + solve(0, n); + cout << endl; + } +} diff --git a/uva_cpp_clean/11149/11149-5.cpp b/uva_cpp_clean/11149/11149-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..895c75664528df7cc28df2c562287e74e99c3548 --- /dev/null +++ b/uva_cpp_clean/11149/11149-5.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +using namespace std; +#define maxn 44 +#define mod 10 +int n; +struct matrix{ + int f[maxn][maxn]; +}; +matrix sum(matrix a,matrix b) +{ + int i,j; + matrix s; + for(i=0;i>1; + } + return s; +} +matrix work(matrix e,int k) +{ + matrix s,a,b; + if(k==1) + return e; + a=work(e,k/2); + s=sum(a,mul(a,pows(e,k/2))); + if(k&1) + s=sum(s,pows(e,k)); + return s; +} +int main() +{ + int k; + while(cin>>n>>k) + { + if(n==0) + break; + int i,j,a; + matrix e; + for(i=0;i>a; + e.f[i][j]=a%10; + } + } + e=work(e,k); + for(i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11150 + Name: Cola + Problem: https://onlinejudge.org/external/111/11150.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int DP[201] = { 0, 1, 3 }; +int main() { + for (int i=3; i<201; i++) + DP[i] = i + DP[int(i/3) + i%3] - i%3; + + int n; + while(cin>>n) + cout << DP[n] < +int main() +{ + int a,n; + while(scanf("%d",&n)==1) + { + a=n; + while(n>=3) + { + a=a+(n/3); + n=(n/3)+(n%3); + } + if(n==2) + { + a++; + } + printf("%d\n",a); + } + return 0; +} + diff --git a/uva_cpp_clean/11150/11150-9.cpp b/uva_cpp_clean/11150/11150-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..71a40c13074c315c20672b615fdcf54f9af145d2 --- /dev/null +++ b/uva_cpp_clean/11150/11150-9.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main(){ + int n, ans; + while(scanf("%d", &n) == 1){ + ans = n; + while(n >= 3) + ans += n / 3, n = n / 3 + n % 3; + if(!(n & 1)) + ans++; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11151/11151-21.cpp b/uva_cpp_clean/11151/11151-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c18f4bd68610e0ce7c1a74666b567b491fb31b5 --- /dev/null +++ b/uva_cpp_clean/11151/11151-21.cpp @@ -0,0 +1,65 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11151 + Name: Longest Palindrome + Problem: https://onlinejudge.org/external/111/11151.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char s[1005]; + +//---------------------------------------------------------- + +int saved[1002][1002]; +int rec(int i,int j){ + if(i>j)return 0; + if(i==j)return 1; + if(saved[i][j]!=-1)return saved[i][j]; + if(s[i]==s[j])return saved[i][j]=2+rec(i+1,j-1); + return saved[i][j]=max(rec(i+1,j),rec(i,j-1)); +} + +//---------------------------------------------------------- + +int dp[1002][1002]; +int f(register int n){ + register int i,j,l; + + for(l=1;l>T; + gets(s); //better if always get input lines like this + //there are problems with --> scanf("%d\n");gets(s); + while(T--){ + gets(s); + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define in freopen("in.txt","r",stdin) +#define out freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define cover(a,b) memset(a,b,sizeof(a)) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; + +string s; +int mem[2000][2000]; +int pl(int i,int j) +{ + if(mem[i][j]!=-1) return mem[i][j]; + if(i>j) return 0; + if(s[i]==s[j]) + return mem[i][j]=pl(i+1,j-1); + else + mem[i][j]=1+ min(pl(i,j-1),pl(i+1,j)); + + return mem[i][j]; +} +int main() +{ + int t,no=0; + cin>>t; + getchar(); + while(t--) + { + cover(mem,-1); + getline(cin,s); + if(s.compare("")==0){ + pf("0\n"); + continue; + } + int l=s.size(); + int n=pl(0,l-1); + pf("%d\n",abs(n-l)); + } + return 0; +} diff --git a/uva_cpp_clean/11151/11151-6.cpp b/uva_cpp_clean/11151/11151-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31ed302690e4e7834731afead9cf73def8937216 --- /dev/null +++ b/uva_cpp_clean/11151/11151-6.cpp @@ -0,0 +1,34 @@ +/* + dynamic programming > longest palindromic subsequence (LPS) + difficulty: easy + date: 06/Nov/2020 + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +string A; int N; +vector> memo; + +int dp(int l, int r) { + if (l == r) return 1; + if (l > r) return 0; + + int &ans = memo[l][r]; + if (ans != -1) return ans; + + if (A[l] == A[r]) return ans = 2 + dp(l+1, r-1); + return ans = max(dp(l+1, r), dp(l, r-1)); +} + +int main() { + int T; cin >> T; + cin.ignore(); + while (T--) { + getline(cin, A); N = A.size(); + memo.assign(N, vector(N, -1)); + cout << dp(0, N-1) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11151/11151-9.cpp b/uva_cpp_clean/11151/11151-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f2fbd6c71b0dee099ea96a249483fce74aeae4a --- /dev/null +++ b/uva_cpp_clean/11151/11151-9.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +const int MAX_LEN = 1e3 + 10; + +string s; +int memo[MAX_LEN][MAX_LEN]; + +int dp (int i, int j) { + if (i > j) return memo[i][j] = 0; + if (~memo[i][j]) return memo[i][j]; + if (s[i] == s[j]) return memo[i][j] = (i == j ? 1 : 2) + dp(i + 1, j - 1); + return memo[i][j] = max(dp(i, j - 1), dp(i + 1, j)); +} + +void clearCase () { + for (int i = 0; i < s.size(); i++) for (int j = 0; j < s.size(); j++) memo[i][j] = -1; +} + +int main () { + int tc; + cin >> tc; + cin.ignore(); + while (tc--) { + getline(cin, s); + clearCase(); + cout << dp(0, s.size() - 1) << endl; + } + return (0); +} diff --git a/uva_cpp_clean/11152/11152-14.cpp b/uva_cpp_clean/11152/11152-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d7fcb156b5d93fdde2ac4332ad2028a3c3aa963c --- /dev/null +++ b/uva_cpp_clean/11152/11152-14.cpp @@ -0,0 +1,72 @@ +/*************************************************** + * Problem name : 11152 Colourful Flowers.cpp + * Problem Link : https://uva.onlinejudge.org/external/111/11152.pdf + * OJ : Uva + * Verdict : AC + * Date : 13.06.2017 + * Problem Type : Geometry + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +#define pi acos(-1) +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + double a, b, c; + double S, p, r, R, S1, S2, S3; + while (scanf("%lf %lf %lf", &a, &b, &c) == 3) { + p = (a + b + c) * 0.5; + S = sqrt(p * (p - a) * (p - b) * (p - c)); + R = a * b * c / 4 / S; + r = sqrt((p - a) * (p - b) * (p - c) / p); + + S1 = pi * R * R - S; + S3 = pi * r * r; + S2 = S - S3; + + printf("%.4f %.4f %.4f\n", S1, S2, S3); + } + return 0; +} + diff --git a/uva_cpp_clean/11152/11152-18.cpp b/uva_cpp_clean/11152/11152-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6c4c369490f61c8a3b4933dba688db66f0e7cf13 --- /dev/null +++ b/uva_cpp_clean/11152/11152-18.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +using namespace std; + +int main(){ + double a, b, c, areaTri, areaCirG, areaCirP, s, radio, pi = acos(-1); + while(true){ + cin >> a >> b >> c; + if(cin.fail()) break; + s = (a+b+c)/2; + areaTri = sqrt(s*(s-a)*(s-b)*(s-c)); //Formula de Heron + radio = a*b*c/(4*areaTri); //Formula de Heron (Triangulo inscrito) + areaCirG = pow(radio, 2) * pi; + radio = areaTri / s; //Formula de Heron (Triangulo circunscrito) + areaCirP = pow(radio, 2) * pi; + areaCirG -= areaTri; + areaTri -= areaCirP; + cout << fixed; + cout << setprecision(4); + cout << areaCirG << " " << areaTri << " " << areaCirP << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11152/11152-20.cpp b/uva_cpp_clean/11152/11152-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38b3725cc55ebdad729553dd5f3f4da41b5b7f1e --- /dev/null +++ b/uva_cpp_clean/11152/11152-20.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#define Pi 2*acos(0.0) + +using namespace std; + +int main() +{ + double a, b, c; + + while( cin >> a >> b >> c ) + { + double s = (a+b+c)/2; + double area = sqrt(s*(s-a)*(s-b)*(s-c)); + double circumscribed_radius = (a*b*c)/(4.0*area); + double inscribed_radius = area/s; + + double sunflower = Pi*circumscribed_radius*circumscribed_radius - area; + double violet = area - Pi*inscribed_radius*inscribed_radius; + double rose = Pi*inscribed_radius*inscribed_radius; + + cout << fixed << setprecision(4) << sunflower << " " << violet << " " << rose << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11152/11152-21.cpp b/uva_cpp_clean/11152/11152-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..597c204905cdd9e62118a145f6b5c5f5443fbc63 --- /dev/null +++ b/uva_cpp_clean/11152/11152-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11152 + Name: Colourful Flowers + Problem: https://onlinejudge.org/external/111/11152.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define PI 3.1415926535897932 +int main(){ + double a, b, c; + while (cin>>a>>b>>c) { + double s = (a+b+c)/2; + double tr = sqrt(s*(s-a)*(s-b)*(s-c)); + double m = (a+b-c)*(a-b+c)*(-a+b+c); + + double R = sqrt((a*a*b*b*c*c)/(2*m*s)); + double r = sqrt(m/(8*s)); + + double C = PI * R*R; + double c = PI * r*r; + + printf("%.4f %.4f %.4f\n", C-tr, tr-c, c); + } +} diff --git a/uva_cpp_clean/11152/11152-5.cpp b/uva_cpp_clean/11152/11152-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..12ca34da519f0f4408cc3c35ead999be20c07235 --- /dev/null +++ b/uva_cpp_clean/11152/11152-5.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int a, b, c; + while (sc("%d %d %d",&a,&b,&c)==3) + { + double s=(a+b+c)/2.0; + double A=sqrt(s*(s-a)*(s - b)*(s - c)); + double R=(a*b*c)/(4*A); + double r=A/s; + pf("%.4lf %.4lf %.4lf\n",Pi*R*R-A,A-Pi*r*r,Pi*r*r); + + } + return 0; +} diff --git a/uva_cpp_clean/11152/11152-6.cpp b/uva_cpp_clean/11152/11152-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e386fcad8d09bf5c5d84b90edf42072c8b5fda15 --- /dev/null +++ b/uva_cpp_clean/11152/11152-6.cpp @@ -0,0 +1,28 @@ +/* + geometry + difficulty: easy + date: 25/Jun/2020 + by: @brpapa +*/ +#include +using namespace std; + +double circleArea(double r) { + return M_PI*r*r; +} +double triangleArea(double a, double b, double c) { + double sp = (a+b+c)/2; // semi perimeter + return sqrt(sp*(sp-a)*(sp-b)*(sp-c)); +} + +int main() { + double a, b, c; + while (cin >> a >> b >> c) { + double triangle = triangleArea(a,b,c); + double inCircle = circleArea(triangle/((a+b+c)/2)); + double circumCircle = circleArea(a*b*c/(4*triangle)); + + printf("%.4lf %.4lf %.4lf\n", circumCircle-triangle, triangle-inCircle, inCircle); + } + return 0; +} diff --git a/uva_cpp_clean/11157/11157-13.cpp b/uva_cpp_clean/11157/11157-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f0444b8b1bfba1f5f459ebab664f0c65f8bea80 --- /dev/null +++ b/uva_cpp_clean/11157/11157-13.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int test, t = 1, ans = 0, howSmall = 0; + + cin >> test; + + while (test--) + { + howSmall = ans = 0; + + long long n, d; + + cin >> n >> d; + + map rock; + + vector numbers; + + numbers.push_back(0); + + rock[0] = 'B'; + + for (int i = 0; i < n; i++) + { + int temp; + + string s; + + cin >> s; + + temp = stoi(s.substr(2)); + + rock[temp] = (char)s[0]; + + numbers.push_back(temp); + } + + numbers.push_back(d); + + rock[d] = 'B'; + + for (int i = 0; i < 2; i++) + { + int pre = 0, howSmall = 0; + + for (int j = 1; j < numbers.size(); j++) + { + char mod = rock[numbers[j]]; + + if (mod == 'B') + { + ans = max(numbers[j] - pre, ans); + + pre = numbers[j]; + } + + else if (mod == 'S') + { + if ((howSmall % 2) == i) + { + ans = max(numbers[j] - pre, ans); + + pre = numbers[j]; + } + + howSmall++; + } + } + } + + cout << "Case " << t++ << ": " << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11157/11157-21.cpp b/uva_cpp_clean/11157/11157-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e1223114240ec81e181aec022548b0af4670a9d --- /dev/null +++ b/uva_cpp_clean/11157/11157-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11157 + Name: Dynamic Frog + Problem: https://onlinejudge.org/external/111/11157.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char tp[143], ch; + int X[143]={}, T, n, d; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n >> d; + + int sz = 1; + for (int i=0; i> tp[sz] >> ch >> X[sz]; + if (X[i] < d) ++sz; + } + tp[0] = tp[sz] = 'B'; + X[sz++] = d; + + int lx=0, mxd=0; + bool used[101] = {}; + for (int i=0; i=0; --i) + if (!used[i]) { + mxd = max(mxd, lx-X[i]); + lx = X[i]; + } + + cout << "Case " << cse << ": " << mxd << '\n'; + } +} diff --git a/uva_cpp_clean/11159/11159-21.cpp b/uva_cpp_clean/11159/11159-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..611faccfeeb78306829479925c6107fd71d1e84c --- /dev/null +++ b/uva_cpp_clean/11159/11159-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11159 + Name: Factors and Multiples + Problem: https://onlinejudge.org/external/111/11159.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +vector adj[143]; +int matchR[143]; +bool seen[143]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = true; + if (matchR[v]<0 || bpm(matchR[v])) { + matchR[v] = u; + return true; + } + } + return false; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m, X[143]; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n; + for (int i=0; i> X[i]; + adj[i].clear(); + } + + cin >> m; + for (int j=0, y; j> y; + for (int i=0; i + +#define LIMIT 1501 +#define DIGITS 315 + +using namespace std; + +int tc, ct, n, carry, l1, l2, minl, maxl, k, i, j, aux, t; +char fibo[LIMIT][DIGITS], lessfibo[LIMIT][DIGITS], ans[DIGITS], tmp[DIGITS], final_tmp[DIGITS], d[DIGITS]; +bool ok; + +char* div2(char *s1){ + l1 = strlen(s1); + k = aux = 0; + for(i = 0; i < l1; i++){ + aux = 10 * aux + s1[i] - '0'; + tmp[k++] = aux / 2 + '0'; + aux %= 2; + } + i = 0; + for(j = 0; j < l1; j++) + if(tmp[j] != '0'){ + i = j; + break; + } + k = 0; + for( ; i < l1; i++) + final_tmp[k++] = tmp[i]; + final_tmp[k] = '\0'; + return final_tmp; +} + +char* subt2(char *s1){ + l1 = strlen(s1); + carry = 2; + for(k = 0, i = l1 - 1; k < l1; k++, i--){ + aux = (s1[i] - '0') - carry; + if(aux >= 0) + tmp[k] = aux + '0', carry = 0; + else + tmp[k] = (aux + 10) + '0', carry = 1; + } + while(tmp[k - 1] == '0') + k--; + tmp[k] = '\0'; + i = 0; + while(k > 0) + final_tmp[i++] = tmp[--k]; + final_tmp[i] = '\0'; + return final_tmp; +} + +char* sum(char *s1, char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + minl = l1 > l2 ? l2 : l1; + maxl = l1 > l2 ? l1 : l2; + carry = 0; + for(k = 0, i = l1 - 1, j = l2 - 1; k < minl; k++, i--, j--){ + aux = s1[i] - '0' + s2[j] - '0' + carry; + tmp[k] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l1){ + aux = s1[i--] - '0' + carry; + tmp[k++] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l2){ + aux = s2[j--] - '0' + carry; + tmp[k++] = aux % 10 + '0'; + carry = aux / 10; + } + if(carry) + tmp[maxl++] = carry + '0', tmp[maxl] = '0'; + else + tmp[maxl] = '\0'; + i = 0; + while(maxl > 0) + final_tmp[i++] = tmp[--maxl]; + final_tmp[i] = '\0'; + return final_tmp; +} + +void generateFiboLessOne(){ + for(t = 1; t < LIMIT; t++){ + strcpy(lessfibo[t], sum(lessfibo[t], subt2(fibo[t]))); + strcpy(d,div2(fibo[t])); + if((fibo[t][strlen(fibo[t]) - 1] - '0') & 1) + strcpy(d, sum(d, fibo[1])); + strcpy(lessfibo[t + 1], d); + } + lessfibo[1][0] = '0', lessfibo[1][1] = '\0'; +} + +void generateFibo(){ + fibo[0][0] = '1', fibo[0][1] = '\0'; + fibo[1][0] = '1', fibo[1][1] = '\0'; + for(t = 2; t < LIMIT; t++) + strcpy(fibo[t], sum(fibo[t - 1], fibo[t - 2])); +} + +int main(){ + generateFibo(); + generateFiboLessOne(); + while(scanf("%d", &n), n) + printf("Set %d:\n%s\n", ++tc, lessfibo[n]); + return(0); +} diff --git a/uva_cpp_clean/11172/11172-13.cpp b/uva_cpp_clean/11172/11172-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0f4091e1c80f4bf64173d1e39abcc73828ed8eb --- /dev/null +++ b/uva_cpp_clean/11172/11172-13.cpp @@ -0,0 +1,40 @@ +#include +#include + +using namespace std; + +int main() +{ + int number, a, b; + + queue op; + + cin >> number; + + for (int i = 0; i < number; i++) + { + cin >> a >> b; + + if (a > b) + { + op.push('>'); + } + + else if (a < b) + { + op.push('<'); + } + + else if (a == b) + { + op.push('='); + } + } + + while (!op.empty()) + { + cout << op.front() << endl; + + op.pop(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11172/11172-21.cpp b/uva_cpp_clean/11172/11172-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0579cb2d7b5e5a132b2407a9f45eca38a4ad410c --- /dev/null +++ b/uva_cpp_clean/11172/11172-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11172 + Name: Relational Operator + Problem: https://onlinejudge.org/external/111/11172.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +void main() { + int n; + cin >> n; + while (n--) { + long long int x, y; + cin >> x >> y; + cout << (x y ? ">" : "=") << endl; + } +} diff --git a/uva_cpp_clean/11172/11172-3.cpp b/uva_cpp_clean/11172/11172-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..142d5fdbf9b0683e214bb337b9c18a7061b514b4 --- /dev/null +++ b/uva_cpp_clean/11172/11172-3.cpp @@ -0,0 +1,25 @@ +#include + +int main(){ + // Int N is the number of comparisons i have to make + int N; + scanf ("%d", &N); + + // Integers a and b are those I am gonna compare + int a,b; + int c = 1; + for ( int l = 0; l < N; l++ ) { + scanf ("%d %d", &a, &b); + if ( a == b) + printf ( "%s\n", "=" ); + else + if ( a > b ) + printf ( "%s\n", ">"); + else + printf ( "%s\n", "<"); + + } + + return 0; +} + diff --git a/uva_cpp_clean/11172/11172-5.cpp b/uva_cpp_clean/11172/11172-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c09fc972f715e80ea7f36c85de127c3aa2b6e21c --- /dev/null +++ b/uva_cpp_clean/11172/11172-5.cpp @@ -0,0 +1,20 @@ +#include +int main() +{ +int a,b,c,i; +while(scanf("%d",&c)==1) + { + for(i=0;ib) + printf(">\n"); + else + printf("=\n"); + } + } + +return(0); +} diff --git a/uva_cpp_clean/11172/11172-9.cpp b/uva_cpp_clean/11172/11172-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f007d17cb8f6e67f3af671f05b6bccd6a18294ed --- /dev/null +++ b/uva_cpp_clean/11172/11172-9.cpp @@ -0,0 +1,13 @@ +#include + +int main(){ + int n, a, b; + scanf("%d", &n); + while(n--){ + scanf("%d %d", &a, &b); + if(a > b) printf(">\n"); + else if(a < b) printf("<\n"); + else printf("=\n"); + } + return(0); +} diff --git a/uva_cpp_clean/11172/main.cpp b/uva_cpp_clean/11172/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..569f743cf6befde46b8ac110c8d0261170b29617 --- /dev/null +++ b/uva_cpp_clean/11172/main.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main(){ + + int t, a, b; + cin >> t; + for (t; t > 0; t--){ + cin >> a >> b; + int c = a-b; + if(c > 0) cout << '>' << endl; + else if(c == 0) cout << '=' << endl; + else cout << '<' << endl; + + } + return 0; +} diff --git a/uva_cpp_clean/11173/11173-19.cpp b/uva_cpp_clean/11173/11173-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d91e1c4cb365fcd21af3ee1c08226d0f1d7534e8 --- /dev/null +++ b/uva_cpp_clean/11173/11173-19.cpp @@ -0,0 +1,22 @@ +#include +#include +using namespace std; + +int gray(int k) { + if (k == 0) return 0; + if (k == 1) return 1; + int l = floor(log2(k)); + int p = (1 << (l+1)) - k - 1; + return gray(p) + (1<> N; + while (N--) { + int n, k; + cin >> n >> k; + printf("%d\n", gray(k)); + } + return 0; +} diff --git a/uva_cpp_clean/11173/11173-21.cpp b/uva_cpp_clean/11173/11173-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28a58cbf09e198a5c0b9f4adc9d8492fcb629a74 --- /dev/null +++ b/uva_cpp_clean/11173/11173-21.cpp @@ -0,0 +1,57 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11173 + Name: Grey Codes + Problem: https://onlinejudge.org/external/111/11173.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if(p == end) { + if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF; + p = buf; + } + return *p++; +} +inline bool isdigit(char ch) { return ch>>4 == 3; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +char buf[50]; +void print(int x) { + if (x) { + int i=30; + buf[i--] = '\n'; + for (; x; x/=10) + buf[i--] = x%10 + '0'; + fwrite_unlocked(buf+i+1, 1, 30-i, stdout); + } + else fwrite_unlocked(buf+40, 1, 2, stdout); +} + +int main() { + buf[40]='0'; + buf[41]='\n'; + int T = readUInt(); + while (T--) { + readUInt(); + int k = readUInt(); + print(k^(k>>1)); + } +} diff --git a/uva_cpp_clean/11173/11173-9.cpp b/uva_cpp_clean/11173/11173-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdcb1c90a9134d4e5271835c4cbb8dd9219192f1 --- /dev/null +++ b/uva_cpp_clean/11173/11173-9.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int tc, n, k; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &n, &k); + printf("%d\n",k ^ (k>>1)); + } + return(0); +} diff --git a/uva_cpp_clean/11181/11181-19.cpp b/uva_cpp_clean/11181/11181-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87a39bff760ad780a8001289a375cc7cc38e8fce --- /dev/null +++ b/uva_cpp_clean/11181/11181-19.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +double P[20]; +bool mask[20]; +int N, r; + +double proc(int ri, int pos) { + if (ri < 0) return 0.0; + if (pos >= N) { + if (ri == 0) return 1.0; + else return 0.0; + } + if (ri == 0) { + double p = 1.0; + for (int i = pos; i < N; i++) { + if (!mask[i]) p *= (1.0 - P[i]); + } + return p; + } + else if (ri != 0) { + if (mask[pos]) return proc(ri, pos + 1); + double p = proc(ri, pos + 1)*(1.0 - P[pos]) + + proc(ri - 1, pos + 1)*(P[pos]); + return p; + } + return 0.0; +} + +int main() { + int c = 1; + while (cin >> N >> r && !(N == 0 && r == 0)) { + for (int i = 0; i < N; i++) { + cin >> P[i]; + mask[i] = false; + } + printf("Case %d:\n", c++); + double PB = proc(r, 0); + for (int i = 0; i < N; i++) { + mask[i] = true; + double PA_B = (proc(r - 1, 0)*P[i]) / PB; + mask[i] = false; + printf("%.6lf\n", PA_B); + } + } + return 0; +} diff --git a/uva_cpp_clean/11185/11185-19.cpp b/uva_cpp_clean/11185/11185-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd787f4ad7b92a136dc3a0289b9492885dd3a894 --- /dev/null +++ b/uva_cpp_clean/11185/11185-19.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +int main() { + int n; + stack s; + while (cin >> n && n >= 0) { + while (n >= 3) { + s.push(n%3 + '0'); + n /= 3; + } + s.push(n + '0'); + while (!s.empty()) { + printf("%c", s.top()); + s.pop(); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11185/11185-21.cpp b/uva_cpp_clean/11185/11185-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9ae4df12643e489da12a314050bf1ee9b3021e2 --- /dev/null +++ b/uva_cpp_clean/11185/11185-21.cpp @@ -0,0 +1,30 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11185 + Name: Ternary + Problem: https://onlinejudge.org/external/111/11185.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +void print3(long long int n) { + if (!n) return; + print3(n / 3); + cout << (n%3); +} + +int main(){ + long long int n; + while (cin>>n && n>=0) { + if (!n) cout<<0; + else print3(n); + cout< +int main() +{ + long long i,n,j,b[10000]; + while(scanf("%lld",&n)) + { + if(n<0) + break; + else if(n==0) + { + printf("0\n"); + } + else + { + i=0; + while(n>0) + { + b[i]=n%3; + n=n/3; + i++; + } + + i=i-1; + for(j=i;j>=0;j--) + printf("%lld",b[j]); + + printf("\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/11192/11192-18.cpp b/uva_cpp_clean/11192/11192-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..eaa1630344eae88d02c9834717290f19f6ff4221 --- /dev/null +++ b/uva_cpp_clean/11192/11192-18.cpp @@ -0,0 +1,21 @@ +#include +#include + +using namespace std; + +int main(){ + int N; + string input; + while(true){ + cin >> N; + if(N == 0) break; + cin >> input; + int groups = input.size()/N; + for(int i = 0; i < input.size(); i += groups){ + for(int j = i + groups - 1; j >= i; j--){ + printf("%c", input[j]); + } + } + printf("\n"); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11192/11192-19.cpp b/uva_cpp_clean/11192/11192-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2b9918f9aec714c518fc7fcc7654d46b435b9f9 --- /dev/null +++ b/uva_cpp_clean/11192/11192-19.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +int main() { + int n; + string s; + while (cin >> n && n != 0) { + cin >> s; + n = s.length()/n; + for (int i = 0; i < s.length(); i += n) { + for (int j = 0; j < n/2; j++) { + char aux = s[j+i]; + s[j+i] = s[i+n-1-j]; + s[i+n-1-j] = aux; + } + } + cout << s << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11192/11192-20.cpp b/uva_cpp_clean/11192/11192-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a24c57f563de2b7e451b249340c2d13880829bf --- /dev/null +++ b/uva_cpp_clean/11192/11192-20.cpp @@ -0,0 +1,23 @@ +#include +#include + +char str[105]; + +int main() +{ + int G; + + while( scanf("%d", &G)==1 && G ) + { + scanf("%s", str); + + int len = strlen(str), gLen = len/G; + for(int i=1 ; i<=G ; i++) + { + int temp = i*gLen; + for(int j=temp-1 ; j>=temp-gLen ; j--) printf("%c", str[j]); + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11192/11192-21.cpp b/uva_cpp_clean/11192/11192-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a24e9853f753146120cd1cd796f94360b3480b86 --- /dev/null +++ b/uva_cpp_clean/11192/11192-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11192 + Name: Group Reverse + Problem: https://onlinejudge.org/external/111/11192.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +int main() { + int n; + char str[110]; + while (cin >> n && n) { + cin >> str; + int l = strlen(str); + int gl = l / n; + for (int i = 0; i < n; i++) { + char *gs = str + i*gl; + for (int j = (gl-1) >> 1; j >= 0; j--) { + char t = gs[j]; + gs[j] = gs[gl - j - 1]; + gs[gl - j - 1] = t; + } + } + cout << str << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11192/11192-5.cpp b/uva_cpp_clean/11192/11192-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9f8400263aaf1b111cfe1d270a59478c40861493 --- /dev/null +++ b/uva_cpp_clean/11192/11192-5.cpp @@ -0,0 +1,25 @@ +#include +#include +int main() +{ + char a[100]; + int i,j,n,p,s,l; + while(scanf("%d",&p)==1) + { + if(p==0) break; + scanf(" "); + gets(a); + l=strlen(a); + n=l/p; + s=0;j=0; + while(j=j;i--) + printf("%c",a[i]); + j=j+n; + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11195/11195-21.cpp b/uva_cpp_clean/11195/11195-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16cabee46893061d93c0a039900af12aed2362b2 --- /dev/null +++ b/uva_cpp_clean/11195/11195-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11195 + Name: Another n-Queen Problem + Problem: https://onlinejudge.org/external/111/11195.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char M[16][16]; +int C[17][17], Row[16], n; +bool Col[16]; +bool check(int i, int j) { + for (int k=0; k> n && n; ++cse) { + memset(C, 0, sizeof(C)); + for (int i=0; i> M[i]; + for (int j=n-1; j>=0; --j) + C[i][j] = C[i][j+1] + (M[i][j] == '.'); + } + + cout << "Case " << cse << ": " << backtrack(0) << '\n'; + } +} diff --git a/uva_cpp_clean/11195/11195-6.cpp b/uva_cpp_clean/11195/11195-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e32d476f60e458c7fbe00ad3443218d6c23748e --- /dev/null +++ b/uva_cpp_clean/11195/11195-6.cpp @@ -0,0 +1,48 @@ +/* + brute force > recursive backtracking > n-queens + difficulty: medium + date: 12/Jan/2020 + hint: use bitmask + by: @brpapa +*/ +#include +#include +#define setBit(S, i) (S |= (1 << (i))) +#define clearBit(S, i) (S &= ~(1 << (i))) +#define bitIsOn(S, i) (S & (1 << (i))) +using namespace std; + +int ur = 0, uld = 0, urd = 0; // bitmaskk + +char board[15][15]; +int ans, N; + +void bt(int c) { + if (c == N) { + ans++; return; + } + + for (int r = 0; r < N; r++) + if (board[r][c] != '*' && !bitIsOn(ur, r) && !bitIsOn(uld, r-c+N-1) && !bitIsOn(urd,r+c)) { + setBit(ur, r); setBit(uld, r-c+N-1); setBit(urd, r+c); + bt(c + 1); + clearBit(ur, r); clearBit(uld, r-c+N-1); clearBit(urd, r+c); // restaura + } +} + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int T = 0; + while (true) { + cin >> N; if (N == 0) break; + for (int r = 0; r < N; r++) + for (int c = 0; c < N; c++) + cin >> board[r][c]; + + ans = 0; + bt(0); + cout << "Case " << ++T << ": " << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11195/11195-9.cpp b/uva_cpp_clean/11195/11195-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d332ec1a22c9720ce7ebbc52db3d5cbb48804733 --- /dev/null +++ b/uva_cpp_clean/11195/11195-9.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +const int MAX_N = 20; + +int n, tc, ans, ones, notValid[MAX_N]; +char c; + +void backtrack(int row = 0, int upperD = 0, int lowerD = 0, int col = 0) { + if (row == ones) { ans++; return; } + int pos = ones bitand ( compl (row bitor upperD bitor lowerD bitor notValid[col]) ); + while (pos) { + int pib = pos bitand -pos; + pos -= pib; + backtrack(row bitor pib, (upperD bitor pib) << 1, (lowerD bitor pib) >> 1, col + 1); + } +} + +void read() { + for (int col = 0; col < n; col++) notValid[col] = 0; + for (int row = 0; row < n; row++, getchar()) + for (int col = 0; col < n; col++) + if ( ( c = getchar() ) == '*' ) notValid[col] |= 1 << row; +} + +int main() { + while (scanf("%d\n", &n), n) { + ans = 0; + ones = (1 << n) - 1; + read(); + backtrack(); + printf("Case %d: %d\n", ++tc, ans); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/11201/11201-21.cpp b/uva_cpp_clean/11201/11201-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ea48a0a38de65ff9a9a60cfb97caf8f1a78d67b --- /dev/null +++ b/uva_cpp_clean/11201/11201-21.cpp @@ -0,0 +1,58 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11201 + Name: The problem of the crazy linguist + Problem: https://onlinejudge.org/external/112/11201.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char chargs[2][26] = { "bcdfghjklmnpqrstvwxyz", "aeiou" }, ch1; + +double prob[128] = { + 12.53, 1.42, 4.68, 5.86, 13.68, 0.69, 1.01, 0.70, 6.25, 0.44, 0.00, 4.97, 3.15, + 6.71, 8.68, 2.51, 0.88, 6.87, 7.98, 4.63, 3.93, 0.90, 0.02, 0.22, 0.90, 0.52 +}; + +double P[128][8]; +int seen[128], C[128][8]; +void init(int l=0, double sum=0) { + C[ch1][l]++; + P[ch1][l] += sum; + if (l >= 7) return; + + char *chs = chargs[l & 1]; + for (int i=0; chs[i]; ++i) + if (seen[chs[i]] < 2) { + seen[chs[i]]++; + if (!l) ch1 = chs[i]; + init(l+1, sum + (l+1) * prob[chs[i]]); + seen[chs[i]]--; + } +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i='a'; i<='z'; ++i) + prob[i] = prob[i-'a']; + init(); + + int T; + cin >> T; + string str; + while (T-- && cin >> str) { + double al = 0; + for (int i=0; str[i]; ++i) + al += (i+1) * prob[ str[i] ]; + + cout << (al >= P[str[0]][str.length()]/C[str[0]][str.length()] ? "above or equal\n" : "below\n"); + } +} diff --git a/uva_cpp_clean/11202/11202-9.cpp b/uva_cpp_clean/11202/11202-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9eb896aedbb8c026f7262c90359d7f61b6b16b81 --- /dev/null +++ b/uva_cpp_clean/11202/11202-9.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int tc, n, m, v1, v2; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &n, &m); + v1 = (n + 1) / 2; + v2 = (m + 1) / 2; + if(n == m) + printf("%d\n", v1 * (v1 + 1) / 2); + else + printf("%d\n",v1 * v2); + } + return(0); +} diff --git a/uva_cpp_clean/11203/11203-19.cpp b/uva_cpp_clean/11203/11203-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bcb05f339fce92225e7efb04feea2e88198eafcb --- /dev/null +++ b/uva_cpp_clean/11203/11203-19.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +int main() { + int cases; + cin >> cases; + for(int i = 0; i < cases; i++){ + int cont1 = 0, cont2 = 0, cont3 = 0; + bool M = false, E = false, fail = false; + string s; + cin >> s; + for(int i = 0; i < s.size(); i++){ + if(s[i] == '?') { + if(E) cont3++; + else if (M) cont2++; + else cont1++; + } + else if(s[i] == 'M') { + if(M || E) { + fail = true; + break; + } + M = true; + } + else if(s[i] == 'E') { + if(E) { + fail = true; + break; + } + E = true; + } + else { + fail = true; + break; + } + } + if(cont3 == 0 || cont2 == 0 || cont1 == 0 || cont3 != cont1 + cont2) fail = true; + if (fail) { + cout << "no-"; + } + cout << "theorem" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11203/11203-21.cpp b/uva_cpp_clean/11203/11203-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11db5a13ce2a374b17e1581a3d6714f0e9c73f18 --- /dev/null +++ b/uva_cpp_clean/11203/11203-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11203 + Name: Can you decide it for ME? + Problem: https://onlinejudge.org/external/112/11203.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +bool check(char word[]) { + int i, a=0, b=0; + + for (i=0; word[i]=='?'; i++) a++; + if (a <= 0 || word[i++]!='M') return 0; + + for (; word[i]=='?'; i++) b++; + if (b <= 0 || word[i++]!='E') return 0; + + a += b; + for (; word[i]=='?'; i++) + if (--a < 0) + return 0; + + return !a && !word[i]; +} + +int main(){ + int T; + char word[100]; + scanf("%d", &T); + while (T--) { + scanf("%s", word); + puts(check(word) ? "theorem" : "no-theorem"); + } +} diff --git a/uva_cpp_clean/11205/11205-19.cpp b/uva_cpp_clean/11205/11205-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..205ccfa4bb8b599f715cb3bbe5035ff9b7c53082 --- /dev/null +++ b/uva_cpp_clean/11205/11205-19.cpp @@ -0,0 +1,48 @@ +#include +#include +using namespace std; + +typedef unordered_set ui; + +int maxOff, P, N; + +void proc(ui & sym, int c, int mask, int off) { + ui newSym; + for (int i = c; i < P; i++) { + newSym.clear(); + int newMask = mask & (~(1 << i)); + for (auto it = sym.begin(); it != sym.end(); ++it) { + int val = (*it) & newMask; + if (!newSym.count(val)) newSym.insert(val); + else break; + } + if (newSym.size() == sym.size()) { + maxOff = max(maxOff, off + 1); + if (i + 1 < P) proc(newSym, i + 1, newMask, off + 1); + } + } +} + +int main() { + int T; + cin >> T; + while (T--) { + cin >> P >> N; + ui symbol; + for (int i = 0; i < N; i++) { + int sym = 0; + for (int j = 0; j < P; j++) { + sym <<= 1; + int val; + cin >> val; + if (val) sym |= 1; + } + symbol.insert(sym); + } + int off = maxOff = 0; + int mask = (1 << P) - 1; + proc(symbol, 0, mask, off); + printf("%d\n", P - maxOff); + } + return 0; +} diff --git a/uva_cpp_clean/11205/11205-21.cpp b/uva_cpp_clean/11205/11205-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa5aaf8741ebd7944024535aa395a7952cbdb760 --- /dev/null +++ b/uva_cpp_clean/11205/11205-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11205 + Name: The broken pedometer + Problem: https://onlinejudge.org/external/112/11205.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int snoob(int x) { + if (!x) throw 1; + int rightOne = x & -x, + nextHigherOneBit = x + rightOne; + return ((x ^ nextHigherOneBit)/rightOne) >> 2 | nextHigherOneBit; +} + + +int X[143], n, m, + seen[1<<16], cse; +bool check(int bm) { + ++cse; + for (int i=0; i> T; + while (T-- && cin >> n >> m) { + for (int i=0; i> b; + X[i] = X[i]<<1|b; + } + } + cout << (m<=1 ? 0 : solve()) << '\n'; + } +} diff --git a/uva_cpp_clean/11207/11207-19.cpp b/uva_cpp_clean/11207/11207-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd3c8f7b08afdd90bf87d443219f9f974d66f895 --- /dev/null +++ b/uva_cpp_clean/11207/11207-19.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +int main() { + int N; + while (cin >> N && N != 0) { + int best = 0; + double db = 0; + for (int i = 1; i <= N; i++) { + double w, h; + cin >> w >> h; + double mini = min(w, h), maxi = max(w, h); + double t = max(mini/2, min(maxi/4, mini)); + if (t > db) db = t, best = i; + } + printf("%d\n", best); + } + return 0; +} diff --git a/uva_cpp_clean/1121/1121-11.cpp b/uva_cpp_clean/1121/1121-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9f9256ce6745b1f7fbfe4a4c5b6bd1f9a47a5f1 --- /dev/null +++ b/uva_cpp_clean/1121/1121-11.cpp @@ -0,0 +1,67 @@ +#include +/* +Problem: 1121 - Subsequence +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3562 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +long long N, S; +long long seq[100001]; + +int main () { + + while ( scanf("%lld %lld\n", &N, &S) != EOF ){ + int optimal_len = INT_MAX; + + for (int i = 0; i < N; i++) + scanf("%lld", &seq[i]); + + int p1 = 0, p2 = 0, seq_sum = 0; + while ( p1 <= p2 && p2 < N ){ + if (seq_sum < S){ + seq_sum += seq[p2]; + p2++; + } + + while ( seq_sum >= S ){ + optimal_len = min(optimal_len, p2-p1); + seq_sum -= seq[p1]; + p1++; + } + } + + // if the whole sequence sum is less than S + optimal_len = (optimal_len == INT_MAX)? 0 : optimal_len; + + printf("%d\n", optimal_len); + } + + return 0; +} +/* +Sample input:- +----------------- +10 15 +5 1 3 5 10 7 4 9 2 8 +5 11 +1 2 3 4 5 + +Sample output:- +----------------- +2 +3 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/1121/1121-14.cpp b/uva_cpp_clean/1121/1121-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e647f3b89d318c9ee0304d9b6d6be141e8d57ad1 --- /dev/null +++ b/uva_cpp_clean/1121/1121-14.cpp @@ -0,0 +1,82 @@ +/*************************************************** + * Problem name : 1121 Subsequence.cpp + * Problem Link : https://uva.onlinejudge.org/external/11/1121.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-19 + * Problem Type : Two Pointer + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,0,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100003 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ll ar[MAX], N, S, ans; + while (scanf("%lld %lld", &N, &S) == 2) { + for (int i = 0; i < N; i++) { + scanf("%lld", &ar[i]); + } + ll sum = 0, mn = 100005; + for (int L = 0, R = 0; R <= N; ) { + sum = sum + ar[R]; + if (S <= sum) { + while (sum <= S) { + ans = ( R - L ) + 1; + mn = min(mn, ans); + sum = sum - ar[L]; + L++; + } + } + R++; + } + if(mn == 100005) mn = 0; + printf("%lld\n", mn); + SET(ar); + } + + return 0; +} + diff --git a/uva_cpp_clean/1121/1121-19.cpp b/uva_cpp_clean/1121/1121-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c10282b268505741b8a435ab27df08b634c52b8 --- /dev/null +++ b/uva_cpp_clean/1121/1121-19.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; + +int main() { + int N, S; + while (cin >> N >> S) { + queue q; + int sum = 0, len = N + 2; + for (int i = 0; i < N; i++) { + int val; + cin >> val; + sum += val; + q.push(val); + while (sum >= S) { + len = min(len, (int) q.size()); + if (q.empty()) break; + sum -= q.front(); + q.pop(); + } + } + if (len == N + 2) len = 0; + printf("%d\n", len); + } + return 0; +} diff --git a/uva_cpp_clean/1121/1121-21.cpp b/uva_cpp_clean/1121/1121-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81f8ad76755a26e1133e56cbe95df4c09c2d0dd2 --- /dev/null +++ b/uva_cpp_clean/1121/1121-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1121 + Name: Subsequence + Problem: https://onlinejudge.org/external/11/1121.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int N, S, X[100143], C[100143] = {0}; + while (cin >> N >> S) { + for (int i=1; i<=N; ++i) { + cin >> X[i]; + C[i] = X[i] + C[i-1]; + } + + int best = N+1; + for (int i=1; i<=N; ++i) { + int *p = lower_bound(C+i, C+N, C[i-1]+S); + if (*p - C[i-1] >= S) { + int len = p - (C+i) + 1; + best = min(best, len); + } + } + cout << (best>N ? 0 : best) << endl; + } +} diff --git a/uva_cpp_clean/1121/1121-4.cpp b/uva_cpp_clean/1121/1121-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ea9cf25aaa047f64f840944709ffa4e679618c9 --- /dev/null +++ b/uva_cpp_clean/1121/1121-4.cpp @@ -0,0 +1,37 @@ +#include +#include + +int main() +{ + int n, s; + + while (std::cin >> n >> s) + { + int min = 0, start = 0; + int *tab = new int[n](); + + for (int i = 0; i < n; i++) + { + int t; + std::cin >> t; + + for (int j = start; j <= i; j++) + { + tab[j] += t; + + if (tab[j] >= s) + { + start = j + 1; + + if (i - j + 1 < min || min == 0) + { + min = i - j + 1; + } + } + } + } + + std::cout << min << std::endl; + delete[] tab; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11218/11218-19.cpp b/uva_cpp_clean/11218/11218-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ceabe8f70295a4f57c2c133218d947aa52e1ce43 --- /dev/null +++ b/uva_cpp_clean/11218/11218-19.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iiii; + +int main() { + int n, test = 1; + bool mask[9]; + for (int i = 0; i < 9; i++) mask[i] = false; + while (cin >> n && n != 0) { + vector v; + for (int i = 0; i < n; i++) { + int a, b, c, s; + cin >> a >> b >> c >> s; + v.push_back(iiii(ii(a-1, b-1), ii(c-1, s))); + } + int score = -1; + for (int i = 0; i < n; i++) { + iiii pc1 = v[i]; + int acum = pc1.second.second; + mask[pc1.first.first] = mask[pc1.first.second] = mask[pc1.second.first] = true; + for (int j = i; j < n; j++) { + iiii pc2 = v[j]; + if (!(mask[pc2.first.first] || mask[pc2.first.second] || mask[pc2.second.first])) { + acum += pc2.second.second; + mask[pc2.first.first] = mask[pc2.first.second] = mask[pc2.second.first] = true; + for (int k = j; k < n; k++) { + iiii pc3 = v[k]; + if (!(mask[pc3.first.first] || mask[pc3.first.second] || mask[pc3.second.first])) { + score = max(score, acum + pc3.second.second); + } + } + mask[pc2.first.first] = mask[pc2.first.second] = mask[pc2.second.first] = false; + acum -= pc2.second.second; + } + } + mask[pc1.first.first] = mask[pc1.first.second] = mask[pc1.second.first] = false; + } + printf("Case %d: %d\n", test++, score); + } + return 0; +} diff --git a/uva_cpp_clean/11218/11218-21.cpp b/uva_cpp_clean/11218/11218-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc6d29bb9265c861a004fd105a15ebedd4da233d --- /dev/null +++ b/uva_cpp_clean/11218/11218-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11218 + Name: KTV + Problem: https://onlinejudge.org/external/112/11218.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int S[88], B[88], n; + for (int cse=1; cin >> n && n; ++cse) { + int A[1<<9] = {}, mxs = 0; + for (int i=0; i> x; + b |= 1 << (x-1); + } + cin >> S[i]; + A[b] = max(A[b], S[i]); + mxs = max(mxs, S[i]); + } + + int best = -1; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11219 + Name: How old are you? + Problem: https://onlinejudge.org/external/112/11219.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int conv(int x[]) { + return x[0]-1 + (x[1]-1)*31 + x[2]*31*12; +} + +int main(){ + int T, cu[4], bd[4]; + cin>>T; + for (int cse=1; cse<=T; cse++) { + scanf("%d/%d/%d %d/%d/%d", &cu[0],&cu[1],&cu[2], &bd[0],&bd[1],&bd[2]); + float diff = (conv(cu) - conv(bd))/(31*12.0); + printf("Case #%d: ", cse); + if (diff<0) cout << "Invalid birth date\n"; + else if (diff>=131) cout << "Check birth date\n"; + else cout << (int)diff << endl; + } +} diff --git a/uva_cpp_clean/11219/11219-5.cpp b/uva_cpp_clean/11219/11219-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ebfea5731896ff9c558ff4b41f515dfb19c5b8fb --- /dev/null +++ b/uva_cpp_clean/11219/11219-5.cpp @@ -0,0 +1,28 @@ +#include +int main() +{ + int a,b,c,e,g,f,k,i,j,t; + while(scanf("%d",&t)==1) + { + for(i=1;i<=t;i++) + { + scanf("%d/%d/%d",&a, &b, &c); + scanf( "%d/%d/%d",&e, &f, &g); + k=(c-g); + if(b130) + printf("Case #%d: Check birth date\n", i); + else + printf("Case #%d: %d\n",i,k); + } + } + return 0; +} diff --git a/uva_cpp_clean/11220/11220-18.cpp b/uva_cpp_clean/11220/11220-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..acedceb1b465c6d95bd19c7ff70dbca3ff9f5b71 --- /dev/null +++ b/uva_cpp_clean/11220/11220-18.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; + +int main(){ + int t, cont = 0, index; + char input[1200]; + scanf("%d ", &t); + while(cont < t){ + printf("Case #%d:\n", cont + 1); + while(gets(input)){ + if(input[0] == '\0') break; + stringstream ss(input); + string word; + index = 1; + while(ss >> word){ + if(word.size() < index) continue; + printf("%c", word[index - 1]); + index++; + } + printf("\n"); + } + cont++; + if(cont < t) printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11220/11220-20.cpp b/uva_cpp_clean/11220/11220-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..970abdb0ab437370712abfb5b139d381b9ecc92f --- /dev/null +++ b/uva_cpp_clean/11220/11220-20.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +int tc, nCase, len1, len2, serial; +char message[1005], decode[35]; +bool flag; + +int main() +{ + scanf("%d ", &tc); + nCase = 1; + while( tc-- ) + { + printf("Case #%d:\n", nCase); + while( gets(message) && strlen(message) ) + { + strcat(message, " "); + len1 = strlen(message), len2 = 0, serial = 0, flag = false; + for(int i=0 ; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + char s[40000],b[40000]; + int t,no=0; + sc("%d",&t); + getchar(); + int i=0; + while(t--) + { + for(i=0;;i++) + { + gets(s); + + } + + int j=0; + b[j++]=s[j]; + + for(int i=1;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11221 + Name: Magic square palindromes. + Problem: https://onlinejudge.org/external/112/11221.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char M[143][143]; +bool check(char *s, int sz) { + for (int i=0; s[i]; ++i) + M[i/sz][i%sz] = s[i]; + + for (int i=0; i> T; + cin.ignore(100, '\n'); + + char str[10143]; + for (int cse=1; cse<=T; ++cse) { + cout << "Case #" << cse << ":\n"; + + cin.getline(str, 10143); + int l = 0; + for (int i=0; str[i]; ++i) + if (isalpha(str[i])) + str[l++] = str[i]; + str[l] = 0; + + int sq = sqrt(l); + if (sq*sq == l && check(str, sq)) + cout << sq << '\n'; + else + cout << "No magic :(\n"; + } +} diff --git a/uva_cpp_clean/11221/11221-9.cpp b/uva_cpp_clean/11221/11221-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..936b4df76132c6eb97651bc07d0bb32e22af9075 --- /dev/null +++ b/uva_cpp_clean/11221/11221-9.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int ch, tc, t, tmp, k; +string a; +vector p(10001); +bool ind; + +void perfect_square(){ + for(int i = 1; i <= 100; i++) + p[i * i] = true; + return; +} + +int main(){ + perfect_square(); + scanf("%d\n", &tc); + while(tc--){ + printf("Case #%d:\n", ++t); + while( ( ch = getchar() ) != '\n' ) + if(isalpha(ch)) + a += ch; + if(not p[a.size()]) + printf("No magic :(\n"); + else{ + tmp = sqrt(a.size()); + ind = true; + k = a.size(); + for(int i = 0; i < a.size() / 2; i++) + if(a[i] != a[--k]) + ind = false; + k = a.size() - 1; + for(int i = 0; i < tmp; i++, k--) + for(int j = 0; j < tmp; j++) + if(a[i + j * tmp] != a[k - j * tmp]) + ind = false; + if(ind) + printf("%d\n", tmp); + else + printf("No magic :(\n"); + } + a.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11222/11222-21.cpp b/uva_cpp_clean/11222/11222-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4fcd049c4362e8a09c6de7ba5c9e16c52873654 --- /dev/null +++ b/uva_cpp_clean/11222/11222-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11222 + Name: Only I did it! + Problem: https://onlinejudge.org/external/112/11222.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +typedef bitset<10001> Bitset; + +int main(){ + int T, n, p; + scanf("%d", &T); + for (int cse=1; cse<=T; cse++) { + printf("Case #%d:\n", cse); + + Bitset bs[3]; + vector probs[3]; + + for (int i=0; i<3; i++) { + scanf("%d", &n); + for (int j=0; j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11223 + Name: O: dah dah dah! + Problem: https://onlinejudge.org/external/112/11223.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +const char codes[143][17] = { + "A", ".-", "J", ".---", "S", "...", "1", ".----", ".", ".-.-.-", ":", "---...", + "B", "-...", "K", "-.-", "T", "-", "2", "..---", ",", "--..--", ";", "-.-.-.", + "C", "-.-.", "L", ".-..", "U", "..-", "3", "...--", "?", "..--..", "=", "-...-", + "D", "-..", "M", "--", "V", "...-", "4", "....-", "'", ".----.", "+", ".-.-.", + "E", ".", "N", "-.", "W", ".--", "5", ".....", "!", "-.-.--", "-", "-....-", + "F", "..-.", "O", "---", "X", "-..-", "6", "-....", "/", "-..-.", "_", "..--.-", + "G", "--.", "P", ".--.", "Y", "-.--", "7", "--...", "(", "-.--.", "\"", ".-..-.", + "H", "....", "Q", "--.-", "Z", "--..", "8", "---..", ")", "-.--.-", "@", ".--.-.", + "I", "..", "R", ".-.", "0", "-----", "9", "----.", "&", ".-..." +}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + map C; + for (int i=0; codes[i][0]; i += 2) + C[codes[i+1]] = codes[i]; + + int T; cin >> T; + cin.ignore(100, '\n'); + for (int cse=1; cse<=T; ++cse) { + if (cse > 1) cout << endl; + cout << "Message #" << cse << endl; + + string line, s; + getline(cin, line); + int i=0; + while (line[i] == ' ') ++i; + for (; line[i]; ++i) + if (line[i] == ' ') { + cout << C[s]; + if (line[i+1] == ' ') { + cout << ' '; + while (line[i+1] == ' ') + ++i; + } + s = ""; + } + else s += line[i]; + if (s.size() > 0) + cout << C[s]; + cout << endl; + } +} diff --git a/uva_cpp_clean/11228/11228-13.cpp b/uva_cpp_clean/11228/11228-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3637f9dcf8343bc1054a9c5501fa7bc6103e1131 --- /dev/null +++ b/uva_cpp_clean/11228/11228-13.cpp @@ -0,0 +1,178 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +ofstream fout("f.out"); + +#define SIZE 1005 + +double r, ansR1, ansR2; +int n, test, t = 1, state = 0; + +vector > points; + +int parent[SIZE], sz[SIZE]; + +int findParent(int vertex) +{ + if (vertex == parent[vertex]) + { + return parent[vertex]; + } + + else + { + return parent[vertex] = findParent(parent[vertex]); + } +} + +bool merge(pair pairVertex) +{ + int x = pairVertex.first; + + int y = pairVertex.second; + + if (findParent(x) == findParent(y)) + { + return true; + } + + else + { + x = findParent(x); + + y = findParent(y); + + if (sz[x] < sz[y]) + { + swap(x, y); + } + + parent[y] = x; + + sz[x] += sz[y]; + + return false; + } +} + +class comparator +{ +public: + + bool operator()(pair, double> &child1, pair, double> &child2) + { + return child1.second > child2.second; + } +}; + +priority_queue< pair< pair< int, int >, double >, vector< pair< pair< int, int >, double > >, comparator> pq; + +void MST() +{ + int how = 0; + + while (!pq.empty() && how < n - 1) + { + pair , double> head = pq.top(); + + if (!merge(make_pair(head.first.first, head.first.second))) + { + how++; + + double temp = head.second; + + if (temp > r) + { + ansR2 += temp; + + state++; + } + + else + { + ansR1 += temp; + } + } + + pq.pop(); + } +} + +void initialize(int n) +{ + points.clear(); + + points.resize(n); + + state = ansR1 = ansR2 = 0; + + while (!pq.empty()) + { + pq.pop(); + } + + for (int i = 0; i < n; i++) + { + parent[i] = i; + + sz[i] = 1; + } +} + +double cal(pair a, pair b) +{ + int x1, x2, y1, y2; + + x1 = a.first; + x2 = b.first; + + y1 = a.second; + y2 = b.second; + + return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))); +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n >> r; + + initialize(n); + + for (int i = 0; i < n; i++) + { + int x, y; + + cin >> x >> y; + + points[i] = make_pair(x, y); + } + + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + double dis = cal(points[i], points[j]); + + pq.push(make_pair(make_pair(i, j), dis)); + } + } + + MST(); + + cout << "Case #" << t++ << ": " << state + 1 << " " << round(ansR1) << " " << round(ansR2) << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11228/11228-21.cpp b/uva_cpp_clean/11228/11228-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03664d63c211bb2d37bff4e1660e085a98d04a11 --- /dev/null +++ b/uva_cpp_clean/11228/11228-21.cpp @@ -0,0 +1,83 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11228 + Name: Transportation system. + Problem: https://onlinejudge.org/external/112/11228.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int uds[1001]; +int find(int u) { + return u==uds[u] ? u : uds[u] = find(uds[u]); +} +void join(int u, int v) { + uds[find(u)] = find(v); +} + +struct Edge { + int u, v, w; + Edge(int u, int v, int w): u(u),v(v),w(w){} + bool operator < (const Edge& e) const { + return w > e.w; + } +}; + +int X[1001], Y[1001]; +int dist2(int i, int j) { + int xx = X[i] - X[j], + yy = Y[i] - Y[j]; + return xx*xx + yy*yy; +} + +int main(){ + int T, n, r; + scanf("%d", &T); + for (int cse=1; cse<=T; cse++) { + scanf("%d%d", &n, &r); r*=r; + + priority_queue q1, q2; + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 1010; + +typedef pair ii; + +int tc, n, r, x[SIZE], y[SIZE], CC; +double roads, railroads; +vector < pair < int, ii > > EdgeList; + +inline int pow2(int p){ + return p * p; +} + +void Kruskal(){ + roads = railroads = 0, CC = 1; + UnionFind UF(n); + EdgeList.clear(); + for(int i = 0; i < n; i++) + for(int j = i + 1; j < n; j++) + EdgeList.push_back( make_pair( pow2(x[i] - x[j]) + pow2(y[i] - y[j]), ii(i, j) ) ); + sort(EdgeList.begin(), EdgeList.end()); + for(int it = 0; it < EdgeList.size(); it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ){ + UF.unionSet(front.second.first, front.second.second); + if( front.first <= r ) roads += sqrt(front.first); + else railroads += sqrt(front.first), CC++; + } + } +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d %d", &n, &r), r *= r; + for(int it = 0; it < n; it++) scanf("%d %d", x + it, y + it); + Kruskal(); + printf("Case #%d: %d %d %d\n", t, CC, (int)round(roads), (int)round(railroads)); + } + return(0); +} diff --git a/uva_cpp_clean/11230/11230-21.cpp b/uva_cpp_clean/11230/11230-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a03ebb008285a56a85918f6c5fc52704ade92677 --- /dev/null +++ b/uva_cpp_clean/11230/11230-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11230 + Name: Annoying painting tool + Problem: https://onlinejudge.org/external/112/11230.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char M[101][101]; +int n, m, r, c; +int solve() { + int cnt = 0; + bool R[101][101] = {}; + for (int i=0; in || j+c>m) return -1; + for (int di=0; di> n >> m >> r >> c && (n||m||r||c)) { + for (int i=0; i> M[i]; + + cout << solve() << '\n'; + } +} diff --git a/uva_cpp_clean/11231/11231-11.cpp b/uva_cpp_clean/11231/11231-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cc419a0056a3de3268f73cebbff2d1ff1a5a477 --- /dev/null +++ b/uva_cpp_clean/11231/11231-11.cpp @@ -0,0 +1,46 @@ +#include +/* +Problem: 11231 - Black and white painting +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2172 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int N, M, C, rem; +int main () { + + while ( scanf("%d %d %d\n", &N, &M, &C) && N ){ + rem = (C == 0)? 0 : 1; + printf("%d\n", ((N-7)*(M-7)+rem)/2); + + } + + return 0; +} + +/* +Sample input:- +---------------- +8 8 0 +8 8 1 +9 9 1 +40000 39999 0 +0 0 0 + +Sample output:- +---------------- +0 +1 +2 +799700028 + +Resources:- +------------- +Video Solution [Eng Amr Saud] (Arabic): https://www.youtube.com/watch?v=UNUD8qp33ic&feature=youtu.be +*/ diff --git a/uva_cpp_clean/11231/11231-18.cpp b/uva_cpp_clean/11231/11231-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b97974ec75dddb72357857364fb08f7ca7173da2 --- /dev/null +++ b/uva_cpp_clean/11231/11231-18.cpp @@ -0,0 +1,11 @@ +#include + +using namespace std; + +int main(){ + int n, m, c; + while(scanf("%d %d %d", &n, &m, &c) && n != 0){ + printf("%d\n", ((n - 7)*(m - 7) + c)/2); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11231/11231-19.cpp b/uva_cpp_clean/11231/11231-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e37b6b43e2d187143110e04a80a7a268af6a673a --- /dev/null +++ b/uva_cpp_clean/11231/11231-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int n, m, c; + while (scanf("%d %d %d", &n, &m, &c) && !(n == 0 && m == 0 && c == 0)) { + int dim = ((n-7)*(m-7))/2; + if ((n-7)%2 && (m-7)%2 && c == 1) { + printf("%d\n", dim + 1); + } else printf("%d\n", dim); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11231/11231-20.cpp b/uva_cpp_clean/11231/11231-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28443b05499b1f9ce3dc9eedc376acc2c9d660c9 --- /dev/null +++ b/uva_cpp_clean/11231/11231-20.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +int main() +{ + int n, m, c; + + while( cin >> n >> m >> c && n+m+c!=0 ) + { + int chess_board = ((n-7)*(m-7))/2; + + if( ((n-7)*(m-7))%2==1 && c==1 ) chess_board++; + + cout << chess_board << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11231/11231-21.cpp b/uva_cpp_clean/11231/11231-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2042460fe6e6cb9a32e2e91b64191212aa985698 --- /dev/null +++ b/uva_cpp_clean/11231/11231-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11231 + Name: Black and white painting + Problem: https://onlinejudge.org/external/112/11231.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int calc(int n, int m) { + return (n/2-3) * (m/2-3); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m, c; + while (cin>>n>>m>>c && n) { + if (c) + cout << calc(n, m) + calc(n-1, m-1) << endl; + else + cout << calc(n, m-1) + calc(n-1, m) << endl; + } +} diff --git a/uva_cpp_clean/11233/11233-15.cpp b/uva_cpp_clean/11233/11233-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4e640b36e1ac394698448d3c4fc9030c0b2df7c --- /dev/null +++ b/uva_cpp_clean/11233/11233-15.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + unordered_map match; + int n, m; + string a, b; + + cin >> n >> m; + + while (n--) { + cin >> a >> b; + match[a] = b; + } + + while (m--) { + cin >> a; + auto temp = match.find(a); + if (temp == match.end()) + switch (a.back()) { + case 'y': + switch (a[a.length() - 2]) { + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': + cout << a << "s\n"; + break; + default: + a.pop_back(); + cout << a << "ies\n"; + break; + } + break; + case 'o': + case 'h': + case 'x': + case 's': + cout << a << "es\n"; + break; + default: + cout << a << "s\n"; + break; + } + else + cout << temp->second << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11233/11233-19.cpp b/uva_cpp_clean/11233/11233-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b6925aaab14399278f2a52531ddab14e243da14 --- /dev/null +++ b/uva_cpp_clean/11233/11233-19.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include +using namespace std; + +unordered_map m; + +int main() { + int L, N; + cin >> L >> N; + while (L--) { + string s, p; + cin >> s >> p; + m.insert(pair(s, p)); + } + while (N--) { + string s; + cin >> s; + if (m.count(s)) cout << m[s] << endl; + else { + if ((!(s[s.length()-2] == 'a' || s[s.length()-2] == 'e' || s[s.length()-2] == 'i' || s[s.length()-2] == 'o' || s[s.length()-2] == 'u') && s[s.length()-1] == 'y') || s[s.length()-1] == 'o'|| s[s.length()-1] == 's' || s[s.length()-1] == 'x' || ((s[s.length()-2] == 's' || s[s.length()-2] == 'c') && s[s.length()-1] == 'h')) { + if (s[s.length()-1] == 'y') s[s.length()-1] = 'i'; + s.append("es"); + } else s.append("s"); + cout << s << endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11233/11233-21.cpp b/uva_cpp_clean/11233/11233-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea1a56ba07e0ac61ffee07ecafe80d4254edfc06 --- /dev/null +++ b/uva_cpp_clean/11233/11233-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11233 + Name: Deli Deli + Problem: https://onlinejudge.org/external/112/11233.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool isVowel[127]; +map irr; +string makePlural(const string &str) { + int len = str.length(); + char last = str[len-1]; + + auto it = irr.find(str); + if (it != irr.end()) + return it->second; + + else if (len>=2 && last=='y' && !isVowel[str[len-2]]) + return str.substr(0, len-1) + "ies"; + + else if (last=='o' || last=='s' || last=='x' || + (len>=2 && last=='h' && (str[len-2]=='c' || str[len-2]=='s'))) + return str + "es"; + + else return str + "s"; +} + +int main() { + isVowel['a'] = isVowel['e'] = + isVowel['o'] = isVowel['i'] = + isVowel['u'] = true; + + int n, m; + cin >> m >> n; + + string str, str2; + while (m--) { + cin >> str >> str2; + irr[str] = str2; + } + + while (n--) { + cin >> str; + cout << makePlural(str) << endl; + } +} diff --git a/uva_cpp_clean/11233/11233-9.cpp b/uva_cpp_clean/11233/11233-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80711fc33d6084f3a142daf14b5dab816f1f789a --- /dev/null +++ b/uva_cpp_clean/11233/11233-9.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +const int LEN = 30; + +int n, m; +map Prural; +char word[LEN], word1[LEN], word2[LEN]; + +bool isvowel(char ch){ + return ( ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' ); +} + +bool check(char* s){ + int l = strlen(s) - 1; + if( s[l] == 'y' and l and not isvowel(s[l - 1]) ) return true; + return false; +} + +bool ending(char* s){ + int l = strlen(s) - 1; + if( s[l] == 'o' or s[l] == 's' or s[l] == 'x' ) return true; + if( l > 0 and s[l] == 'h' and ( s[l - 1] == 'c' or s[l - 1] == 's' ) ) return true; + return false; +} + +int main(){ + scanf("%d %d\n", &n, &m); + for(int it = 0; it < n; it++){ + scanf("%s %s", word1, word2); + Prural[word1] = word2; + } + for(int it = 0; it < m; it++){ + scanf("%s", word); + if( Prural.find(word) != Prural.end() ) puts(Prural[word].c_str()); + else if( check(word) ) word[strlen(word) - 1] = 'i', printf("%ses\n", word); + else if( ending(word) ) printf("%ses\n", word); + else printf("%ss\n", word); + } + return(0); +} diff --git a/uva_cpp_clean/11235/11235-21.cpp b/uva_cpp_clean/11235/11235-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8abb5d654b3f799b8b9755b3ebdacdce2575da32 --- /dev/null +++ b/uva_cpp_clean/11235/11235-21.cpp @@ -0,0 +1,109 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11235 + Name: Frequent values + Problem: https://onlinejudge.org/external/112/11235.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-'); + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +char buf[50]; +void print(long long x) { + int i=48; + buf[i--] = '\n'; + if (!x) buf[i--] = '0'; + for (; x; x/=10) + buf[i--] = x%10 + '0'; + fwrite_unlocked(buf+i+1, 1, 48-i, stdout); +} + +// -------------------------------------------------------------------- + +#define MAXN 100017 +int F[MAXN][17], S[MAXN], E[MAXN], I[MAXN], cnt; +void initRMQ() { + for (int j=1, tj; (tj=1< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%I64d",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%I64d%I64d",&a,&b) + +#define sfi(t) scanf("%d",&t) +#define sfii(a,b) scanf("%d %d",&a,&b) +#define sfiii(a,b,c) scanf("%d %d %d",&a,&b,&c) +#define sfll(t) scanf("%I64d",&t) +#define sfllll(a,b) scanf("%I64d %I64d",&a,&b) +#define sfllllll(a,b,c) scanf("%I64d %I64d %I64d",&a,&b,&c) +#define sfd(t) scanf("%lf",&t) +#define sfc(c) scanf("%c",&c) +#define sfs(s) scanf("%s",s) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%I64d\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%I64d ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%I64d %I64d\n",a,b) +#define PPLN(a,b) printf("%I64d %I64d ",a,b) + +#define pfi(a) printf("%d\n",a) +#define pfii(a,b) printf("%d %d\n",a,b) +#define pfiii(a,b,c) printf("%d %d %d\n",a,b,c) +#define pfll(a) printf("%I64d\n",a) +#define pfllll(a,b) printf("%I64d %I64d\n",a,b) +#define pfllllll(a,b,c) printf("%I64d %I64d %I64d\n",a,b,c) +#define pfd(a) printf("%lf\n",a) +#define pfs(a) printf("%s\n",a) +#define pfis(a) printf("%d ",a) +#define pflls(a) printf("%I64d ",a) +#define pfds(a) printf("%lf ",a) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "< +#include +#include +#include + +#define CONST 1000000 +#define MAX 2000000000 + +int main() +{ + for (int x = 1; x <= 2000; x++) + { + if (x * x * x * x > MAX) break; + + for (int y = x; y <= 2000 - x; y++) + { + if (x * y * y * y > MAX) break; + + for (int z = y; z <= 2000 - x - y; z++) + { + if (x * y * z * z > MAX) break; + + else + { + int a, b, t; + + a = x + y + z; + b = x * y * z; + + if (b > CONST && ((a * CONST) % (b - CONST)) == 0) + { + t = ((a * CONST) / (b - CONST)); + + if (t <= (2000 - x - y - z) && t >= z && (x + y + z + t) * CONST == (x * y * z * t)) + { + double xx = x / 100.0; + double yy = y / 100.0; + double zz = z / 100.0; + double tt = t / 100.0; + + printf("%.2f %.2f %.2f %.2f\n", xx, yy, zz, tt); + } + } + } + } + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11236/11236-21.cpp b/uva_cpp_clean/11236/11236-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d243f2164dafdec14b195149eeadcf7471d1a17 --- /dev/null +++ b/uva_cpp_clean/11236/11236-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11236 + Name: Grocery store + Problem: https://onlinejudge.org/external/112/11236.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int dm = 1000000; + for (int i=1; i<=125; ++i) + for (int j=i; j<=600; ++j) + for (int k=j; i+j+k+k<=2000; ++k) if (i*j*k!=dm) { + int l = dm*(i+j+k)/(i*j*k-dm); + if (l>=k && i+j+k+l<=2000 && (i+j+k+l)*dm == i*j*k*l) { + cout << fixed << setprecision(2) + << i/100.0 << ' ' << j/100.0 << ' ' << k/100.0 << ' ' << l/100.0 << '\n'; + } + } +} diff --git a/uva_cpp_clean/11236/11236-9.cpp b/uva_cpp_clean/11236/11236-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c83f6bfde36496e38ff6b34d7e4e14e7543f2ce --- /dev/null +++ b/uva_cpp_clean/11236/11236-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +long long d, S, P; + +int main(){ + for(int a = 1; a < 2000 - a; a++) + for(int b = a; b < 2000 - a - b; b++) + for(int c= b ; c < 2000 - a - b; c++){ + P = a * b * c, S = a + b + c; + if(P <= 1000000) continue; + d = 1000000 * S / (P - 1000000); + S += d, P *= d; + if(d < c || S > 2000) continue; + if(fabs(S / 1e2 - P / 1e8) < 1e-9) + printf("%.2f %.2f %.2f %.2f\n", a / 100.0, b / 100.0, c / 100.0, d / 100.0); + } + return(0); +} diff --git a/uva_cpp_clean/11237/11237-19.cpp b/uva_cpp_clean/11237/11237-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a59aef95b2d1d63af33ece79753976f336b5d8c8 --- /dev/null +++ b/uva_cpp_clean/11237/11237-19.cpp @@ -0,0 +1,34 @@ +#include +#include +using namespace std; + +int main() { + int c, n; + int sweets[100002]; + int pos[100002]; + while (cin >> c >> n && !(c == 0 && n == 0)) { + memset(pos, 0, c*4); + bool first = false; + bool stop = false; + for (int i = 0; i < n; i++) { + int auxi; + cin >> auxi; + if (stop) continue; + sweets[i] = auxi; + sweets[i] += i > 0? sweets[i-1]: 0; + sweets[i] %= c; + if (pos[sweets[i]] != 0 || sweets[i] == 0) { + for (int j = pos[sweets[i]]+1; j <= i+1; j++) { + if (first) printf(" "); + else first = true; + printf("%d", j); + } + stop = true; + continue; + } + pos[sweets[i]] = i+1; + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11239/11239-14.cpp b/uva_cpp_clean/11239/11239-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..314fcc9ba98fbcd89d88e13846ae24b8252d72f7 --- /dev/null +++ b/uva_cpp_clean/11239/11239-14.cpp @@ -0,0 +1,113 @@ +/*************************************************** + * Problem name : 11239 Open Source.cpp + * Problem Link : https://uva.onlinejudge.org/external/112/11239.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-24 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,0,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 103 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + string str; + mapmp; + mapuser, chk; + while (1) { + int k = 1, ar[MAX]; + SET(ar); + while (getline(cin, str)) { + if (str[0] == '0' && str.size() == 1) return 0; + if (str[0] == '1' && str.size() == 1) break; + if (str[0] >= 'A' && str[0] <= 'Z') { + mp[k++] = str; + } else { + if (user[str] == 0) { + ar[k - 1]++; + user[str] = 1; + chk[str] = k - 1; + } else { + if (chk[str] != k - 1 && user[str] != 2) { + ar[chk[str]]--; + user[str] = 2; + } + } + } + } + for (int i = 1; i < k; i++) { + for (int j = i + 1; j < k; j++) { + if (ar[j] > ar[i]) { + int tmp = ar[i]; + ar[i] = ar[j]; + ar[j] = tmp; + + string tm = mp[i]; + mp[i] = mp[j]; + mp[j] = tm; + } else if (ar[j] == ar[i]) { + if (mp[i] > mp[j]) { + int tmp = ar[i]; + ar[i] = ar[j]; + ar[j] = tmp; + + string tm = mp[i]; + mp[i] = mp[j]; + mp[j] = tm; + } + } + } + } + for (int i = 1; i < k; i++) { + cout << mp[i] << " " << ar[i] << "\n"; + } + mp.clear(), user.clear(), chk.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/11239/11239-21.cpp b/uva_cpp_clean/11239/11239-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb9427eb556c8a7504df10ef266eccf47e93781d --- /dev/null +++ b/uva_cpp_clean/11239/11239-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11239 + Name: Open Source + Problem: https://onlinejudge.org/external/112/11239.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + + +int main() { + map::iterator pit; + map projs; + + unordered_map users; + + string line; + while (getline(cin, line) && line[0]!='0') { + if (line[0] == '1') { + map > pp; + for (auto tt: projs) + pp[tt.second].push_back(tt.first); + + for (auto it=pp.rbegin(); it!=pp.rend(); ++it) { + int sz = it->first; + for (string &s: it->second) + cout << s << ' ' << sz << endl; + } + + projs.clear(); + users.clear(); + continue; + } + + if (line[0] <= 'Z') { + projs[line] = 0; + pit = projs.find(line); + } + + else { + auto uit = users.find(line); + if (uit == users.end()) { + users[line] = pit->first; + ++pit->second; + } + else if (uit->second[0] && (uit->second != pit->first)) { + --projs[uit->second]; + uit->second[0] = 0; + } + } + } +} diff --git a/uva_cpp_clean/1124/1124-19.cpp b/uva_cpp_clean/1124/1124-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8590bb83957064aae28fbbf62ef623eb2039ba16 --- /dev/null +++ b/uva_cpp_clean/1124/1124-19.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; + +int main() { //That's a joke + string s; + while (getline(cin, s)) cout << s << endl; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1124/1124-21.cpp b/uva_cpp_clean/1124/1124-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1915bc4f0f836fe84784337fadfa71db4b935797 --- /dev/null +++ b/uva_cpp_clean/1124/1124-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1124 + Name: Celebrity jeopardy + Problem: https://onlinejudge.org/external/11/1124.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char ch; + while((ch=getchar())!=EOF) + cout< +int main() +{ +char a[100]; +while(gets(a)) +puts(a); +return 0; +} diff --git a/uva_cpp_clean/1124/1124-9.cpp b/uva_cpp_clean/1124/1124-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00c6dd9a083671f23b8478a087c9f0a8505feeea --- /dev/null +++ b/uva_cpp_clean/1124/1124-9.cpp @@ -0,0 +1,7 @@ +#include + +int main(){ + char cad[100]; + while(gets(cad)) puts(cad); + return(0); +} diff --git a/uva_cpp_clean/1124/main.cpp b/uva_cpp_clean/1124/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d55d2af6b88039e55fb025004c2faccf3931b063 --- /dev/null +++ b/uva_cpp_clean/1124/main.cpp @@ -0,0 +1,12 @@ +#include +#include + +using namespace std; + +int main(){ + string s; + while(getline(cin, s)){ + cout << s << '\n'; + } + return 0; +} diff --git a/uva_cpp_clean/11240/11240-21.cpp b/uva_cpp_clean/11240/11240-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3a6854111067155033d528b03af864eaf8844b7 --- /dev/null +++ b/uva_cpp_clean/11240/11240-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11240 + Name: Antimonotonicity + Problem: https://onlinejudge.org/external/112/11240.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) return EOF; + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +// -------------------------------------------------------------------------------- + +int sgn(int x) { + return x<0 ? -1 : 1; +} + +int main() { + int X[30313]; + int T = readInt(); + while (T--) { + int n = readInt(); + for (int i=0; i +using namespace std; +typedef long long ll; + +int main() { + int T; cin >> T; + while (T--) { + int n; cin >> n; + + int prev = 0, curr; + int hi = 0; + int lo = 0; + + for (int i = 0; i < n; i++) { + cin >> curr; + if (curr > prev) hi = lo+1; + if (curr < prev) lo = hi+1; + prev = curr; + } + cout << max(lo, hi) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11242/11242-11.cpp b/uva_cpp_clean/11242/11242-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8cb7a1e22e113b7c87e4147f5942ab4298f327f1 --- /dev/null +++ b/uva_cpp_clean/11242/11242-11.cpp @@ -0,0 +1,65 @@ +#include +/* +Problem: 11242 - Tour de France +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2183 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int F, R, Fs[11], Rs[11]; +double max_spread, ratios[121]; + +int main () { + + while ( scanf("%d", &F) && F ){ + scanf("%d", &R); + max_spread = INT_MIN; + + for (int i = 0; i < F; i++) + scanf("%d", &Fs[i]); + + for (int i = 0; i < R; i++) + scanf("%d", &Rs[i]); + + int idx = 0; + for (int n = 0; n < R; n++) + for (int m = 0; m < F; m++) + ratios[idx++] = (double)Rs[n]/Fs[m]; + + sort(ratios, ratios+R*F); + + for (int i = 0; i < F*R-1; i++){ + double d1 = ratios[i+1]/ratios[i]; + max_spread = max(d1, max_spread); + } + + printf("%.2lf\n", max_spread); + } + + return 0; +} +/* +Sample input:- +----------------- +2 4 +40 50 +12 14 16 19 +0 + +Sample output:- +----------------- +1.19 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11242/11242-13.cpp b/uva_cpp_clean/11242/11242-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..379f8ab2fc70cedbfd0f48328eaf2a6b0195b312 --- /dev/null +++ b/uva_cpp_clean/11242/11242-13.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int f, r; + + cin >> f; + + while (f != 0) + { + cin >> r; + + double m = 0.0; + vector nums; + double* fc = new double[f]; + double* rc = new double[r]; + + for (int i = 0; i < f; i++) + { + cin >> fc[i]; + } + + for (int i = 0; i < r; i++) + { + cin >> rc[i]; + } + + for (int i = 0; i < f; i++) + { + for (int j = 0; j < r; j++) + { + nums.push_back(rc[j] / fc[i]); + } + } + + sort(nums.begin(), nums.end()); + + for (int i = 1; i < nums.size(); i++) + { + m = max(m, nums[i] / nums[i - 1]); + } + + printf("%.2f\n", m); + + cin >> f; + } +} + diff --git a/uva_cpp_clean/11242/11242-2.cpp b/uva_cpp_clean/11242/11242-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6b25095074d7fe84da38799f5248f7e24a292c2 --- /dev/null +++ b/uva_cpp_clean/11242/11242-2.cpp @@ -0,0 +1,65 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + + int f, r; + while (scanf("%d", &f)) { + if (f == 0) break; + scanf("%d", &r); + vector front(f), rear(r); + for(double& i: front) scanf("%lf", &i); + for(double& i: rear) scanf("%lf", &i); + bool first = true; + double last; + double maximum_spread = -1; + vector drive_ratio; + for(int i = 0; i < f; ++i) + for(int j = 0; j < r; ++j) + drive_ratio.push_back((double) rear[j]/front[i]); + sort(all(drive_ratio)); + for (auto dri: drive_ratio) { + if (first) + first = false; + else { + double spread = dri / last; + maximum_spread = max(spread, maximum_spread); + } + last = dri; + } + printf("%.2f\n", maximum_spread); + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11242/11242-21.cpp b/uva_cpp_clean/11242/11242-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5befbfbfef24332676b11515c359f4c0fe1fbc1c --- /dev/null +++ b/uva_cpp_clean/11242/11242-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11242 + Name: Tour de France + Problem: https://onlinejudge.org/external/112/11242.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +int main(){ + int f[1000], m, n, r; + double dr[100000]; + while (scanf("%d%d", &m, &n)==2) { + for (int i=0; i //printf scanf +#include //sort, max + +using namespace std; + +int main () +{ + // Integer f: Number of sprockets in the front cluster + // Integer r: Number of sprockets in the rear cluster + int f,r; + int fa[10]; + // Double spread: aka result: the number finally printed + double spread, tempR, + ratios[100]; + + while ( scanf("%d %d",&f,&r), f) + { + spread = 0; + for ( int l = 0; l < f; l++ ) + scanf ("%d", &fa[l]); + // Now we have already read the front cluster input, there is no need for + // us to make additional loops when we can solve ( at least partly ) the problem + // in the second one. ( less code -> less bugs to fix ) + for ( int rReps = 0; rReps < r; rReps++ ) + { + scanf("%lf", &tempR); + for ( int fReps = 0; fReps < f; fReps++ ) + ratios[f*rReps+fReps] = tempR / fa[fReps]; + } + + sort ( ratios, ratios + (f*r) ); + + for ( int l = 1; l < f * r; l++ ) + spread = max(spread, ratios[l]/ratios[l-1]); + + printf ("%.2lf\n", spread ); + } +} + + diff --git a/uva_cpp_clean/11242/11242-9.cpp b/uva_cpp_clean/11242/11242-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2161d9c71180b96ad914fd38d2fecdf6c6a5c54d --- /dev/null +++ b/uva_cpp_clean/11242/11242-9.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int n, m; +vector v, w; +float _max; +vector x; + +int main(){ + while(scanf("%d %d", &n, &m) == 2){ + v.resize(n), w.resize(m), _max = 0, x.clear(); + for(int i = 0; i < n; i++) scanf("%d", &v[i]); + for(int i = 0; i < m; i++) scanf("%d", &w[i]); + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + x.push_back( (float)w[j] / v[i] ); + sort(x.begin(), x.end()); + for(int i = 0; i < x.size() - 1; i++) + _max = max(_max, x[i+1] / x[i]); + printf("%.2f\n", _max); + } + return(0); +} diff --git a/uva_cpp_clean/11244/11244-19.cpp b/uva_cpp_clean/11244/11244-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63ff13c512ec1fcdf90a70929283f13ab7e5abe0 --- /dev/null +++ b/uva_cpp_clean/11244/11244-19.cpp @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +char grid[102][102]; + +int limx, limy; +int area(int x, int y){ + if(x<0 || x>=limx || y<0 || y>=limy || grid[x][y] != '*') return 0; + else{ + grid[x][y]='.'; + return 1 + area(x+1, y) + area(x-1, y) + area(x, y+1) + area(x, y-1) + area(x+1, y+1) + area(x-1, y-1) + area(x-1, y+1) + area(x+1, y-1); + } +} + +int main(){ + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + cin >> limy >> limx; + while(limx != 0 || limy != 0){ + for(int y = 0; y < limy; y++){ + for(int x = 0; x < limx; x++){ + cin >> grid[x][y]; + } + } + int count = 0; + for(int y = 0; y < limy; y++){ + for(int x = 0; x < limx; x++){ + if(area(x, y)==1) count++; + } + } + cout<> limy >> limx; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11244/11244-21.cpp b/uva_cpp_clean/11244/11244-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8233199b7bdec65385bc537c562355d7a69423e5 --- /dev/null +++ b/uva_cpp_clean/11244/11244-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11244 + Name: Counting Stars + Problem: https://onlinejudge.org/external/112/11244.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char M[200][200]; +bool isstar(int y, int x) { + if (M[y][x]!='*') return 0; + for (int i=-1; i<2; ++i) + for (int j=-1; j<2; ++j) + if ((i || j) && M[y+i][x+j]=='*') + return 0; + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + + int n, m; + while (cin>>n>>m && (n||m)) { + memset(M[n+1], 0, m+2); + for (int i=1; i<=n; ++i) + cin >> (M[i]+1); + + int cnt = 0; + for (int i=1; i<=n; ++i) + for (int j=1; j<=m; ++j) + if (isstar(i, j)) + ++cnt; + cout << cnt << endl; + } +} diff --git a/uva_cpp_clean/11244/11244-9.cpp b/uva_cpp_clean/11244/11244-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba07e31c8a5320ee8d1d7c186c453da981c76afc --- /dev/null +++ b/uva_cpp_clean/11244/11244-9.cpp @@ -0,0 +1,37 @@ +#include + +#define SIZE 110 + +using namespace std; + +int n, m, ans; +char grid[SIZE][SIZE]; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < n and C < m ); +} + +int floodfill(int r, int c){ + if( not is_possible(r, c) ) return 0; + if( grid[r][c] == '.' ) return 0; + grid[r][c] = '.'; + int ct = 1; + for(int d = 0; d < 8; d++) + ct += floodfill(r + dr[d], c + dc[d]); + return ct; +} + +int main(){ + while(scanf("%d %d\n", &n, &m), n | m ){ + ans = 0; + for(int row = 0; row < n; row++) scanf("%s", grid[row]); + for(int row = 0; row < n; row++) + for(int col = 0; col < m; col++) + if( grid[row][col] == '*' and floodfill(row, col) == 1 ) + ans++; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11246/11246-19.cpp b/uva_cpp_clean/11246/11246-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8480ec29f2999702b735a51266e08e55a3c18f9b --- /dev/null +++ b/uva_cpp_clean/11246/11246-19.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +int n, k; + +int proc(int n, bool plus) { + if (n < k) return plus? n: 0; + return (plus? n - (n / k): 0) + proc(n/k, !plus); +} + +int main() { + int T; + cin >> T; + while (T--) { + cin >> n >> k; + printf("%d\n", proc(n, true)); + } + return 0; +} diff --git a/uva_cpp_clean/11247/11247-21.cpp b/uva_cpp_clean/11247/11247-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50485674b95549f746037731736465735c533df3 --- /dev/null +++ b/uva_cpp_clean/11247/11247-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11247 + Name: Income Tax + Problem: https://onlinejudge.org/external/112/11247.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main() { + int m, x, v; + while (scanf("%d%d", &m, &x)==2 && (m||x)) { + long long v = (m-1)/(1-x/100.0) - 1e-3; + if (v < m) puts("Not found"); + else printf("%lld\n", v); + } +} diff --git a/uva_cpp_clean/11247/11247-9.cpp b/uva_cpp_clean/11247/11247-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4cd83bb260f9520ffe2c808937d70aa8e616391 --- /dev/null +++ b/uva_cpp_clean/11247/11247-9.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main(){ + long long m, x, v, V; + while(scanf("%lld %lld", &m, &x), m | x){ + if(x == 100 || x == 0 ) + printf("Not found\n"); + else{ + V = 0; + v = (m - 1) * 100 / (100 - x); + if(v - 1 >= m && (v - 1) * (100 - x) / 100 < 100 * m - 100) + V = v - 1; + if(v >= m && v * (100 - x) < 100 * m - 100) + V = v; + if(V) + printf("%lld\n", V); + else + printf("Not found\n"); + } + } + return(0); +} diff --git a/uva_cpp_clean/11254/11254-11.cpp b/uva_cpp_clean/11254/11254-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5f5a45fe20d013499b8284dc47cae21a7462893 --- /dev/null +++ b/uva_cpp_clean/11254/11254-11.cpp @@ -0,0 +1,78 @@ +#include +/* +Problem: 11254 - Consecutive Integers +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2221 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +ll N; + +int main () { + + while ( scanf("%lld\n", &N) && N != -1 ){ + + ll Xans = N, Yans = N, last_series_len = 0; + ll N2 = 2*N, L, R, X, Y; + // N = X + ... + Y + // (X+Y)*(Y-X+1)/2 = N (arithmetic series sum formula) + // X+Y = L, Y-X+1 = R + // N = L*R + // L = X + Y + // R = Y - X + 1 + // Y = (R+L-1)/2 + // X = (L-R+1)/2 + // So now, we need to find L, R, S.T: L*R = 2*N + + for (ll i = 2; i * i <= N2; ++i){ + if (N2%i != 0) continue; + + L = N2/i, R = i; + + Y = (R+L-1)/2; + X = (L-R+1)/2; + + if ((X+Y)*(Y-X+1) == N2 && Y-X+1 > last_series_len){ + Xans = X; + Yans = Y; + last_series_len = Y-X+1; + } + } + + printf("%lld = %lld + ... + %lld\n", N, Xans, Yans); + + } + + return 0; +} + +/* +Sample input:- +----------------- +8 +15 +35 +-1 + +Sample output:- +----------------- +8 = 8 + ... + 8 +15 = 1 + ... + 5 +35 = 2 + ... + 8 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11254/11254-19.cpp b/uva_cpp_clean/11254/11254-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..146dbb7b6c0b1b668732ec62629b8f9e1a8ade60 --- /dev/null +++ b/uva_cpp_clean/11254/11254-19.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + +unsigned long long inv(unsigned long long N) { + return ceil((sqrt(8*N + 1) - 1)/2); +} + +/*unsigned long long gauss(unsigned long long N) { + return (N+1)*N/2; +} + +void proc(unsigned long long N) { + unsigned long long low = 0, top; + while (true) { + top = inv(N + gauss(low)); + if (N + gauss(low) == gauss(top)) { + printf("%lld = %lld + ... + %lld\n", N, low+1, top); + break; + } + printf("T%lld: %lld %lld %lld\n", N, low, top, gauss(top) - gauss(low)); + low = inv(gauss(top) - N); + printf("Q%lld: %lld %lld %lld\n", N, low, top, gauss(top) - gauss(low)); + } +}*/ + +int main() { + unsigned long long N; + while (cin >> N && N != -1) { + //proc(N); + int sol = 1; + for (int i = 2; i <= inv(N); i++) { + if (i % 2 == 1) { + if (N % i == 0) sol = i; + } else { + if (N % i == i / 2) sol = i; + } + } + long long low = N / sol - sol / 2, top = N / sol + sol / 2; + if (sol % 2 == 0) low++; + printf("%lld = %lld + ... + %lld\n", N, low, top); + } + return 0; +} diff --git a/uva_cpp_clean/11254/11254-6.cpp b/uva_cpp_clean/11254/11254-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85ae4dad711f0ac01b122dcfdd72058676e756bb --- /dev/null +++ b/uva_cpp_clean/11254/11254-6.cpp @@ -0,0 +1,28 @@ +/* + math > ad-hoc > arithmetic progression + difficulty: medium + date: 14/Feb/2020 + by: @brpapa +*/ +#include +#include +using namespace std; + +int main() { + while (true) { + double sn; cin >> sn; if (sn == -1) break; + printf("%.lf = ", sn); + + // força bruta no tamanho (n) da P.A. de d=1 + // o maior n possível é quando a1=1, logo sn = (1+n)*n/2 + for (int n = (int)sqrt(2*sn); n >= 1; n--) { + double a1 = (2.0*sn - n*n + n)/(2.0*n); + + if (a1 == (double)(int)a1) { + printf("%.lf + ... + %.lf\n", a1, a1+n-1); // a1, an + break; + } + } + } + return 0; +} diff --git a/uva_cpp_clean/11258/11258-9.cpp b/uva_cpp_clean/11258/11258-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ba97f0a61f8a10ae2f0fa4e5432c6cbc05d53ff --- /dev/null +++ b/uva_cpp_clean/11258/11258-9.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +typedef long long ll; + +const ll MAX_VAL = 1LL << 31, MAX_N = 200 + 10; + +string s; +int id[MAX_N]; +ll num[MAX_N][MAX_N], memo[MAX_N]; + +ll dp (int i) { + if (i == s.size()) return 0; + if (~memo[i]) return memo[i]; + ll ret = 0; + for (int k = i; k < id[i]; k++) + ret = max(ret, num[i][k] + dp(k + 1)); + return memo[i] = ret; +} + +void preprocess () { + for (int i = 0; i < s.size(); i++) { + memo[i] = -1; + ll val = 0; + id[i] = s.size(); + for (int j = i; j < s.size(); j++) { + val = val * 10 + (s[j] - '0'); + if (val < MAX_VAL) num[i][j] = val; + else { + id[i] = j; + break; + } + } + } +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + cin >> s; + preprocess(); + cout << dp(0) << endl; + } + return (0); +} diff --git a/uva_cpp_clean/11262/11262-21.cpp b/uva_cpp_clean/11262/11262-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2cb27d7c8ed867de716260d76234ec3352cdea2 --- /dev/null +++ b/uva_cpp_clean/11262/11262-21.cpp @@ -0,0 +1,103 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11262 + Name: Weird Fence + Problem: https://onlinejudge.org/external/112/11262.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + + +vector adj[143]; +int X1[143], Y1[143], X2[143], Y2[143], n, m, + match[143], D[143][143], + seen[143], sse; +bool dfs(int u) { + for (int v: adj[u]) + if (seen[v] != sse) { + seen[v] = sse; + if (match[v]==-1 || dfs(match[v])) { + match[v] = u; + return 1; + } + } + return 0; +} + +bool bpm(int mx, int k) { + if (!k) return 1; + for (int i=0; i> T; + string s; + while (T--) { + cin >> p >> k; + n=m=0; + for (int i=0, x,y; i> x >> y >> s; + if (s[0] == 'b') + X1[n] = x, Y1[n] = y, ++n; + else + X2[m] = x, Y2[m] = y, ++m; + } + + vector V; + for (int i=0; i>1; + if (bpm(V[mid]*V[mid], k)) + hi = mid - 1; + else + lo = mid + 1; + } + + cout << V[hi+1] << '\n'; + } + } +} diff --git a/uva_cpp_clean/11262/11262-6.cpp b/uva_cpp_clean/11262/11262-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f11a37d07a4d88a2161ec796ffc4ea0190ec880c --- /dev/null +++ b/uva_cpp_clean/11262/11262-6.cpp @@ -0,0 +1,81 @@ +/* + graphs > specials > bipartite > max cardinality bipartite matching + difficulty: medium + date: 19/Aug/2020 + hint: binary search on answer + MCBM + by: @brpapa +*/ +#include +using namespace std; + +vector> adj_list; int V, LV; +vector> reds, blues; +int K; + +vector match; // match[r] = vértice l já combinado com r +vector seen; + +/* O(E) - returns 1 if an augmenting path is found */ +int has_aug(int l) { + if (seen[l]) return 0; + seen[l] = true; + + for (int r : adj_list[l]) { + if (match[r] == -1 || has_aug(match[r])) { + match[r] = l; + return 1; + } + } + return 0; +} + +/* O(V*E) - return the max qty of matchings */ +int mcbm() { + int ans = 0; + match.assign(V, -1); + for (int l = 0; l < LV; l++) { + seen.assign(LV, false); + ans += has_aug(l); + } + return ans; +} + +// com correntes de comprimento ans, há pelo menos K cercas? +bool can(int ans) { + adj_list.assign(V, vector()); + LV = reds.size(); + for (int i = 0; i < reds.size(); i++) + for (int j = 0; j < blues.size(); j++) { + int dx = reds[i].first - blues[j].first; + int dy = reds[i].second - blues[j].second; + if (sqrt(dx*dx + dy*dy) <= (double)ans) + adj_list[i].push_back(LV+j); + } + return mcbm() >= K; +} + +int main() { + int T; cin >> T; + while (T--) { + cin >> V >> K; + reds.clear(); blues.clear(); + for (int u = 0; u < V; u++) { + int x,y; cin >> x >> y; + string color; cin >> color; + if (color == "red") reds.push_back({x,y}); + else blues.push_back({x,y}); + } + + int low = 1, high = 2002; + int ans = -1; + while (low <= high) { + int mid = (low+high)/2; + if (can(mid)) ans = mid, high = mid-1; + else low = mid+1; + } + + if (ans == -1) cout << "Impossible\n"; + else cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11264/11264-13.cpp b/uva_cpp_clean/11264/11264-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fe52d5a9df1b998af9ca9a097b62d7335020994 --- /dev/null +++ b/uva_cpp_clean/11264/11264-13.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int test, n; + + cin >> test; + + while (test--) + { + long long how = 0, sum = 0; + + cin >> n; + + long long* a = new long long[n]; + + memset(a, 0, sizeof a); + + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + + for (int i = 0; i < n; i++) + { + if ((i == n - 1 && sum < a[i]) || sum < a[i] && sum + a[i] < a[i + 1]) + { + sum += a[i]; + + how++; + } + } + + cout << how << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11264/11264-21.cpp b/uva_cpp_clean/11264/11264-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbf3ae36b02b85575e9fda555d39514aa425f7bc --- /dev/null +++ b/uva_cpp_clean/11264/11264-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11264 + Name: Coin Collector + Problem: https://onlinejudge.org/external/112/11264.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, n, x, y; + cin>>T; + while(T--) { + cin>>n>>x; + int rs=0, cnt=1; + for (int i=1; i>y; + if (rs + x < y) { + rs += x; + cnt++; + } + x = y; + } + + cout << cnt << endl; + } +} diff --git a/uva_cpp_clean/11264/11264-9.cpp b/uva_cpp_clean/11264/11264-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c2bd7c0d2c708e157681b0ee5b5847a67f153ff --- /dev/null +++ b/uva_cpp_clean/11264/11264-9.cpp @@ -0,0 +1,22 @@ +#include + +#define SIZE 1005 + +using namespace std; + +int tc, n, ans, v[SIZE]; +long long sum; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + for(int i = 0; i < n; i++) scanf("%d", &v[i]); + sum = 1; + ans = (n == 1) ? 1 : 2; + for(int i = 1; i < n - 1; i++) + if(sum+v[i] < v[i + 1]) sum += v[i], ans++; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11265/11265-19.cpp b/uva_cpp_clean/11265/11265-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f59fd6d0383c05e54ef9f29a3c26250d36ef44ba --- /dev/null +++ b/uva_cpp_clean/11265/11265-19.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long double ld; +typedef complex point, vect; +typedef vector polygon; +typedef polygon::iterator polIter; +const ld inf = 1e9; const ld eps = 1e-9; const ld pi = acos(-1.0); + +ld area(polygon &p) { + ld result = 0.0; + for (int i = 0; i < p.size(); i++) + result += imag(conj(p[i])*p[(i+1)%p.size()]); + return abs(result) / 2.0; +} + +ld darea(point & a, point & b, point & c) { + return imag(conj(a)*b+conj(b)*c+conj(c)*a); +} + +point intersectSeg(point & p, point & q, point & a, point & b) { + ld u = abs(imag(conj(a-b)*p-b*conj(a))); + ld v = abs(imag(conj(a-b)*q-b*conj(a))); + return (p*v + q*u) / (u+v); +} + +polygon cutPolygon(point & a, point & b, polygon &p) { + polygon q; + for (int i = 0; i < p.size(); i++) { + ld left1 = darea(a, b, p[i]); + ld left2 = darea(a, b, p[(i + 1)%p.size()]); + if (left1 > -eps) q.push_back(p[i]); + if (left1*left2 < -eps) + q.push_back(intersectSeg(p[i],p[(i+1)%p.size()],a,b)); + } + return q; +} + +int main() { + int N, W, H, x, y, c = 1; + while (cin >> N >> W >> H >> x >> y) { + polygon garden; + point fountain(x, y); + garden.push_back(point(0,0)); + garden.push_back(point(W,0)); + garden.push_back(point(W,H)); + garden.push_back(point(0,H)); + for (int i = 0; i < N; i++) { + int x1, y1, x2, y2; + cin >> x1 >> y1 >> x2 >> y2; + point a(x1, y1), b(x2, y2); + if (darea(a, fountain, b) > 0) garden = cutPolygon(b, a, garden); + else garden = cutPolygon(a, b, garden); + } + printf("Case #%d: %.3Lf\n", c++, area(garden)); + } + return 0; +} diff --git a/uva_cpp_clean/11269/11269-21.cpp b/uva_cpp_clean/11269/11269-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77ac906183a1f19f2238d7b89ccb1c25fb4a6691 --- /dev/null +++ b/uva_cpp_clean/11269/11269-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11269 + Name: Setting Problems + Problem: https://onlinejudge.org/external/112/11269.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct Prob { int x, y; } P[43]; +bool cmp1(Prob a, Prob b) { return a.x < b.x; } +bool cmp2(Prob a, Prob b) { return a.y > b.y; } + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int D[43], n; + while (cin >> n) { + for (int i=0; i> P[i].x; + + for (int i=0; i> P[i].y; + + int sz1=0, sz2=20; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11278 + Name: One-Handed Typist + Problem: https://onlinejudge.org/external/112/11278.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +char qwerty[] = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?\n\t ", + dvorak[] = "`123qjlmfp/[]456.orsuyb;=\\789aehtdck-0zx,inwvg'~!@#QJLMFP?{}$%^>ORSUYB:+|&*(AEHTDCK_)ZX +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; + +ll gcd(ll a, ll b) +{ + if (b==0) return a; + return gcd(b, a%b); +} + +ll lcm(ll a, ll b) +{ + return a/gcd(a, b)*b; +} + +bool cmp(ll a,ll b) +{ + return a > b; +} +int main() +{ + string a="`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"; + string ares="`123qjlmfp/[]456.orsuyb;=\\789aehtdck-0zx,inwvg'"; + string b="~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"; + string bres="~!@#QJLMFP?{}$%^>ORSUYB:+|&*(AEHTDCK_)ZX + +using namespace std; + +const int LEN = 1010; + +char line[LEN]; +map mp; + +int main(){ + mp['`'] = '`', mp['1'] = '1', mp['2'] = '2', mp['3'] = '3', mp['4'] = 'q', mp['5'] = 'j', mp['6'] = 'l', mp['7'] = 'm', mp['8'] = 'f', mp['9'] = 'p', mp['0'] = '/', mp['-'] = '[', mp['='] = ']'; + mp['q'] = '4', mp['w'] = '5', mp['e'] = '6', mp['r'] = '.', mp['t'] = 'o', mp['y'] = 'r', mp['u'] = 's', mp['i'] = 'u', mp['o'] = 'y', mp['p'] = 'b', mp['['] = ';', mp[']'] = '=', mp['\\'] = '\\'; + mp['a'] = '7', mp['s'] = '8', mp['d'] = '9', mp['f'] = 'a', mp['g'] = 'e', mp['h'] = 'h', mp['j'] = 't', mp['k'] = 'd', mp['l'] = 'c', mp[';'] = 'k', mp['\''] = '-'; + mp['z'] = '0', mp['x'] = 'z', mp['c'] = 'x', mp['v'] = ',', mp['b'] = 'i', mp['n'] = 'n', mp['m'] = 'w', mp[','] = 'v', mp['.'] = 'g'; + + mp['~'] = '~', mp['!'] = '!', mp['@'] = '@', mp['#'] = '#', mp['$'] = 'Q', mp['%'] = 'J', mp['^'] = 'L', mp['&'] = 'M', mp['*'] = 'F', mp['('] = 'P', mp[')'] = '?', mp['_'] = '{', mp['+'] = '}'; + mp['Q'] = '$', mp['W'] = '%', mp['E'] = '^', mp['R'] = '>', mp['T'] = 'O', mp['Y'] = 'R', mp['U'] = 'S', mp['I'] = 'U', mp['O'] = 'Y', mp['P'] = 'B', mp['{'] = ':', mp['}'] = '+', mp['|'] = '|'; + mp['A'] = '&', mp['S'] = '*', mp['D'] = '(', mp['F'] = 'A', mp['G'] = 'E', mp['H'] = 'H', mp['J'] = 'T', mp['K'] = 'D', mp['L'] = 'C', mp[':'] = 'K', mp['"'] = '_'; + mp['Z'] = ')', mp['X'] = 'Z', mp['C'] = 'X', mp['V'] = '<', mp['B'] = 'I', mp['N'] = 'N', mp['M'] = 'W', mp['<'] = 'V', mp['>'] = 'G', mp['?'] = '"'; + mp[' '] = ' '; + + while(cin.getline(line, LEN)){ + for(int it = 0; line[it]; it++){ + if( line[it] == '/' ) putchar('\''); + else putchar( mp[line[it]] ); + } + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/11280/11280-21.cpp b/uva_cpp_clean/11280/11280-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8d8b702cc4562202ed829bdd6ee81e731b6495b --- /dev/null +++ b/uva_cpp_clean/11280/11280-21.cpp @@ -0,0 +1,87 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11280 + Name: Flying to Fredericton + Problem: https://onlinejudge.org/external/112/11280.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector adj[114]; + int C[114][114], D[114][114], Q[12], R[114], + T; cin >> T; + + string s1, s2; + for (int cse=1; cse<=T; ++cse) { + cout << (cse>1 ? "\n" : "") << "Scenario #" << cse << endl; + unordered_map ids; + + int n; cin >> n; + for (int i=0; i> s1; + ids[s1] = i; + adj[i].clear(); + for (int j=0; j> m; + for (int i=0, c; i> s1 >> s2 >> c; + int u = ids[s1], v = ids[s2]; + if (C[u][v] == INF) + adj[u].push_back(v); + + if (c < C[u][v]) + C[u][v] = c; + } + + cin >> m; + int mxq = 0; + for (int i=0; i> Q[i]; + if (Q[i] >= n-1) + Q[i] = n-2; + if (Q[i] > mxq) + mxq = Q[i]; + } + + int i=1; + for (int som=1; som-- && i<=mxq+1; ++i) { + for (int u=0; u + +using namespace std; + +const int MAX_V = 110, MAX_E = 1010, LEN = 25, INF = 1000000000; + +int tc, V, E, Q, u, v, w, mx, dis, W[MAX_V][MAX_V], memo[MAX_V][MAX_V]; +char word[LEN], word1[LEN], word2[LEN]; +map mp; + +int dp(int id, int ct){ + if( id == V ) return 0; + if( ct < 0 ) return INF; + if( ~memo[id][ct] ) return memo[id][ct]; + int ret = INF; + for(int j = 1; j <= V; j++) + if( W[id][j] != INF ) + ret = min(ret, W[id][j] + dp(j, ct - 1)); + return memo[id][ct] = ret; +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + printf("Scenario #%d\n", t); + scanf("%d\n", &V); + memset(memo, -1, sizeof memo); + fill(W[1], W[V + 1], INF); + for(int i = 1; i <= V; i++){ + scanf("%s\n", word); + mp[word] = i; + } + scanf("%d\n", &E); + for(int i = 0; i < E; i++){ + scanf("%s %s %d\n", word1, word2, &w); + u = mp[word1], v = mp[word2]; + W[u][v] = min(W[u][v], w); + } + scanf("%d", &Q); + while(Q--){ + scanf("%d", &mx); + dis = dp(1, mx); + if( dis == INF ) puts("No satisfactory flights"); + else printf("Total cost of flight(s) is $%d\n", dis); + } + if( t != tc ) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/11282/11282-19.cpp b/uva_cpp_clean/11282/11282-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e74a92ce298182df32af040c2157dbefa6e9b4cb --- /dev/null +++ b/uva_cpp_clean/11282/11282-19.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef unordered_map uill; + +unordered_map map; +ll fact[21]; +ll dp[22][22]; + +ll comb(int n, int m) { + if (m < 0 || m > n) return 0; + if (n == m || m == 0) return 1; + if (!map[n].count(m)) { + map[n][m] = comb(n-1, m-1) + comb(n-1, m); + } + return map[n][m]; +} + +int main() { + fact[0] = 1; + for (int i = 1; i < 21; i++) fact[i] = i*fact[i-1]; + dp[0][0] = 1, dp[1][0] = 0, dp[1][1] = 1; + for (int i = 2; i < 21; i++) { + dp[i][0] = fact[i] - 1; + for (int j = i-1; j >= 2; j--) dp[i][0] -= dp[j][0]*comb(i, i-j); + for (int j = 1; j <= i; j++) { + dp[i][j] = comb(i, j)*dp[i-j][0]; + } + } + int N, M; + while (cin >> N >> M) { + ll ret = 0; + for (int i = 0; i <= M; i++) ret += dp[N][i]; + printf("%lld\n", ret); + } + return 0; +} diff --git a/uva_cpp_clean/11283/11283-19.cpp b/uva_cpp_clean/11283/11283-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63975a9929f0844086107c27ebc9555800afe0c2 --- /dev/null +++ b/uva_cpp_clean/11283/11283-19.cpp @@ -0,0 +1,62 @@ +#include +#include +using namespace std; + +string grid[4]; +bool mask[4][4]; + +bool inside(int x, int y) { + return x < 4 && x >= 0 && y < 4 && y >= 0; +} + +bool search(const string & word, int x, int y, int pos) { + if(word[pos] == grid[x][y] && !mask[x][y]) { + mask[x][y] = true; + if (pos < word.length()-1) { + int found = false; + for (int i = -1; i <= 1 && !found; i++) + for (int j = -1; j <= 1 && !found; j++) + if (inside(x+i, y+j)) found |= search(word, x+i, y+j, pos+1); + mask[x][y] = false; + return found; + } else { + mask[x][y] = false; + return true; + } + } else return false; +} + +int points(const string & word) { + switch(word.length()) { + case 7: return 5; + case 6: return 3; + case 5: return 2; + case 4: + case 3: return 1; + case 2: + case 1: + case 0: return 0; + default: return 11; + } +} + +int main() { + int N; + cin >> N; + for (int i = 1; i <= N; i++) { + for (int j = 0; j < 4; j++) cin >> grid[j]; + int M; + cin >> M; + string word; + int count = 0; + for (int j = 0; j < M; j++) { + cin >> word; + bool stop = false; + for (int x = 0; x < 4 && !stop; x++) + for (int y = 0; y < 4 && !stop; y++) + if (search(word, x, y, 0)) count += points(word), stop = true; + } + printf("Score for Boggle game #%d: %d\n", i, count); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11283/11283-9.cpp b/uva_cpp_clean/11283/11283-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddf87aea441be63147cec263a0184a9bc5b5f578 --- /dev/null +++ b/uva_cpp_clean/11283/11283-9.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +const int N = 4; +const char FILLED = '#'; + +vector grid(N); +string s; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool isValid (const int r, const int c) { + return 0 <= r and 0 <= c and r < N and c < N; +} + +bool backtrack (int r, int c, int id = 1) { + if (grid[r][c] != s[id - 1]) return false; + if (id == s.size()) return true; + char ch = grid[r][c]; + grid[r][c] = FILLED; + bool ret = false; + for (int d = 0; d < 8; d++) { + int nr = r + dr[d]; + int nc = c + dc[d]; + if (isValid(nr, nc) and grid[nr][nc] != FILLED) { + ret |= backtrack(nr, nc, id + 1); + } + } + grid[r][c] = ch; + return ret; +} + +bool findInGrid () { + for (int r = 0; r < N; r++) + for (int c = 0; c < N; c++) + if (backtrack(r, c)) return true; + return false; +} + +int getScore () { + if (s.size() < 3) return 0; + if (s.size() < 5) return 1; + if (s.size() == 5) return 2; + if (s.size() == 6) return 3; + if (s.size() == 7) return 5; + return 11; +} + +int processQueries () { + int k, score = 0; + cin >> k; + while (k--) { + cin >> s; + if (findInGrid()) score += getScore(); + } + return score; +} + +int main () { + int tc; + cin >> tc; + for (int t = 1; t <= tc; t++) { + for (int r = 0; r < N; r++) cin >> grid[r]; + cout << "Score for Boggle game #" << t << ": " << processQueries() << endl; + } + return (0); +} diff --git a/uva_cpp_clean/11284/11284-21.cpp b/uva_cpp_clean/11284/11284-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4805e27f7874f70633ab67dd8297d71385a6e37 --- /dev/null +++ b/uva_cpp_clean/11284/11284-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11284 + Name: Shopping Trip + Problem: https://onlinejudge.org/external/112/11284.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 1073741824 +int cse=1, mat[100][100], ope[100], ids[100], d[100][100], dn; +int memoid[8193][13], + memo[8193][13]; + +int rec(int remp, int u) { + if (memoid[remp][u] == cse) + return memo[remp][u]; + + int best = -d[u][0]; + if (remp) { + int bit = 1; + for (int v=0; v<=dn; v++, bit<<=1) + if (remp & bit) + best = max(best, rec(remp&(~bit), v) - d[u][v]); + } + + memoid[remp][u] = cse; + return memo[remp][u] = ope[ids[u]]+best; +} + + + +int main(){ + double w; + int T, n, m, p, u, v; + scanf("%d", &T); + for (cse=1; cse<=T; cse++) { + scanf("%d%d", &n, &m); + for (int i=0; i<=n; i++) { + mat[i][i] = ope[i] = 0; + for (int j=i+1; j<=n; j++) + mat[i][j] = mat[j][i] = INF; + } + + for (int i=0; i + +using namespace std; + +const int MAX_V = 50 + 5, MAX_P = 12 + 5, MAX_MASK = (1 << 12) + 1, INF = 1e7; + +int v, e, p, pos[MAX_P], dis[MAX_V][MAX_V], save[MAX_P], memo[MAX_V][MAX_MASK][MAX_P]; + +int dp (int u, int taken, int id) { + if (id == p) return -dis[u][0]; + if (memo[u][taken][id] != INF) return memo[u][taken][id]; + int ret = dp(u, taken, id + 1); + for (int dvd = 0; dvd < p; dvd++) { + if ((taken >> dvd) & 1) { + int val = save[dvd] - dis[u][pos[dvd]] + dp(pos[dvd], taken xor (1 << dvd), id + 1); + ret = max(ret, val); + } + } + return memo[u][taken][id] = ret; +} + +void FloydWarshall () { + for (int u = 0; u <= v; u++) dis[u][u] = 0; + for (int k = 0; k <= v; k++) + for (int i = 0; i <= v; i++) + for (int j = 0; j <= v; j++) + dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); +} + +void readCase () { + cin >> v >> e; + for (int i = 0; i < e; i++) { + int u, v, w, a, b; + scanf("%d %d %d.%d", &u, &v, &a, &b); + w = a * 100 + b; + dis[u][v] = min(dis[u][v], w); + dis[v][u] = min(dis[v][u], w); + } + cin >> p; + for (int i = 0; i < p; i++) { + int _pos, a, b; + scanf("%d %d.%d", &_pos, &a, &b); + pos[i] = _pos; + save[i] = a * 100 + b; + } +} + +void clearCase () { + fill(dis[0], dis[MAX_V], INF); + for (int i = 0; i < MAX_V; i++) + for (int j = 0; j < MAX_MASK; j++) + for (int k = 0; k < MAX_P; k++) + memo[i][j][k] = INF; +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + clearCase(); + readCase(); + FloydWarshall(); + int ans = dp(0, (1 << p) - 1, 0); + if (ans <= 0) puts("Don't leave the house"); + else printf("Daniel can save $%d.%.2d\n", ans / 100, ans % 100); + } + return (0); +} diff --git a/uva_cpp_clean/11286/11286-14.cpp b/uva_cpp_clean/11286/11286-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfdd644e6717dca1603668fb85535c9504a46ab5 --- /dev/null +++ b/uva_cpp_clean/11286/11286-14.cpp @@ -0,0 +1,133 @@ +/*************************************************** + * Problem Name : 11286 - Conformity.cpp + * Problem Link : https://onlinejudge.org/external/112/11286.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-03-05 + * Problem Type : STL + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int n; + + while (cin >> n) { + if (n == 0) break; + + vectorvec[n]; + map >mp; + map, int>mm; + int mx = 0; + + for (int i = 0; i < n; i++) { + setst; + + for (int j = 0; j < 5; j++) { + int x; + cin >> x; + st.insert (x); + } + + mp[i] = st; + mm[st]++; + mx = max (mx, mm[st]); + } + int cnt = 0; + for(auto it : mp){ + if(mm[it.second] == mx)cnt++; + } + cout << cnt << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/11286/11286-21.cpp b/uva_cpp_clean/11286/11286-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6fb1438d09be5de206731a406d4b67a4c757133f --- /dev/null +++ b/uva_cpp_clean/11286/11286-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11286 + Name: Conformity + Problem: https://onlinejudge.org/external/112/11286.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main() { + int n, x, z[5]; + while (scanf("%d", &n)==1 && n) { + unordered_map freq; + vector vec; + + int mx = 0; + for (int i=0; i +#include +#include +#include + +using namespace std; + +// In this data structure we are to pass the "courses" in the string field, +// and their frequency in the second field +map courses; + +int main() +{ + int N, selc[5]; + string token; + char ctoken[20]; + int totalStudents, maxCourseStudents; + + while ( scanf("%d", &N), N) + { + courses.clear(); + while ( N-- ) + { + // Simple procedure: (impressively implemented by morris821028) + // to turn the input in string + scanf("%d %d %d %d %d", &selc[0], &selc[1], &selc[2], &selc[3], &selc[4]); + sort ( selc, selc+5 ); + sprintf(ctoken, "%d %d %d %d %d", selc[0],selc[1],selc[2],selc[3],selc[4]); + + token = ctoken; + // Passing the frequency + // courses[y] students taking part in that class + courses[token]++; + } + + maxCourseStudents = 0; + for ( map :: iterator l = courses.begin(); l != courses.end(); l++ ) + { + if ( l->second > maxCourseStudents ) + maxCourseStudents = l->second, + totalStudents = 0; + if ( l->second == maxCourseStudents ) + totalStudents += maxCourseStudents; + } + printf("%d\n",totalStudents); + } +} diff --git a/uva_cpp_clean/11286/11286-6.cc b/uva_cpp_clean/11286/11286-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..f0dff6e787066d468628b33c408a24f907bb4e64 --- /dev/null +++ b/uva_cpp_clean/11286/11286-6.cc @@ -0,0 +1,34 @@ +// http://uva.onlinejudge.org/external/112/11286.pdf +#include +#include +#include +#include + +using namespace std; + +typedef vector vi; +typedef map mvii; + +int main() { + while (true) { + int n; + cin >> n; + if (!n) break; + mvii m; + int M = 0; + for (int i = 0; i < n; i++) { + vi x(5); + for (int j = 0; j < 5; j++) cin >> x[j]; + sort(x.begin(), x.end()); + int c = m.count(x); + if (c) m[x]++; + else m[x] = 1; + M = max(M, m[x]); + } + int c = 0; + for (auto it = m.begin(); it != m.end(); it++) { + if (it->second == M) c += M; + } + cout << c << endl; + } +} diff --git a/uva_cpp_clean/11287/11287-21.cpp b/uva_cpp_clean/11287/11287-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21e3ca3ed304245bfb8b43c0ef654929665b1896 --- /dev/null +++ b/uva_cpp_clean/11287/11287-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11287 + Name: Pseudoprime Numbers + Problem: https://onlinejudge.org/external/112/11287.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool isPrime(int x) { + for (int i=2; i*i <= x; ++i) + if (x%i == 0) + return 0; + return 1; +} + +int powmod(int a, int b, int m) { + if (!b) return 1; + if (b==1) return a%m; + + long long r = powmod(a, b>>1, m); + r = (r*r) % m; + if (b&1) r = (r*a) % m; + return r; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int p, m; cin >> p >> m && (p||m); ) + cout << (isPrime(p) || powmod(m, p, p)!=m%p ? "no\n" : "yes\n"); +} diff --git a/uva_cpp_clean/11287/11287-9.cpp b/uva_cpp_clean/11287/11287-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c13f179209c5c5bd537a7232811c52959fb62ff --- /dev/null +++ b/uva_cpp_clean/11287/11287-9.cpp @@ -0,0 +1,12 @@ +def solution(): + + isPrime = lambda n: p > 1 and all(n % i != 0 for i in range(2, int(n ** 0.5 + 1))) + + while True: + p, a = map(int, input().split()) + if p == 0 and a == 0: + break + print('yes' if pow(a, p, p) == a and not isPrime(p) else 'no') + +if __name__ == '__main__': + solution() diff --git a/uva_cpp_clean/11291/11291-19.cpp b/uva_cpp_clean/11291/11291-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83c2fe067b9579013969dd84ae15a837270ad26a --- /dev/null +++ b/uva_cpp_clean/11291/11291-19.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; + +double expected(stringstream & ss) { + char trash; + while (!(ss.peek() == '(' || ss.peek() == '-' || isdigit(ss.peek()))) ss.get(trash); + if (ss.peek() == '(') { + ss.get(trash); + double p, e1, e2; + ss >> p; + e1 = expected(ss); + e2 = expected(ss); + ss >> trash; + return p*(e1 + e2) + (1 - p)*(e1 - e2); + } else { + double d; + ss >> d; + return d; + } + return 0.0; +} + +int main() { + string s; + while (getline(cin, s) && s != "()") { + stringstream ss(s); + double e = expected(ss); + printf("%.2lf\n", e); + } + return 0; +} diff --git a/uva_cpp_clean/11292/11292-13.cpp b/uva_cpp_clean/11292/11292-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9ea4437de79ef39321eb9a088c8ae9e233d2f8a --- /dev/null +++ b/uva_cpp_clean/11292/11292-13.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int n, m; + + while (cin >> n >> m, n != 0 || m != 0) + { + bool found = true; + long long sum = 0; + vector dragons(n, 0); + vector knights(m, 0); + + for (int i = 0; i < n; i++) cin >> dragons[i]; + for (int i = 0; i < m; i++) cin >> knights[i]; + + if (n > m) cout << "Loowater is doomed!" << endl; + + else + { + sort(knights.begin(), knights.end()); + + for (int i = 0; i < n; i++) + { + int ind = lower_bound(knights.begin(), knights.end(), dragons[i]) - knights.begin(); + + if (ind == knights.size()) + { + found = false; + + break; + } + + else + { + sum += knights[ind]; + + knights.erase(knights.begin() + ind); + } + } + + if (found) cout << sum << endl; + + else cout << "Loowater is doomed!" << endl; + } + } +} diff --git a/uva_cpp_clean/11292/11292-14.cpp b/uva_cpp_clean/11292/11292-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c5a1878bcd4ce516008e1b3795afa9276ac8ac38 --- /dev/null +++ b/uva_cpp_clean/11292/11292-14.cpp @@ -0,0 +1,85 @@ +/*************************************************** + * Problem name : 11292 The Dragon of Loowater.cpp + * Problem Link : https://uva.onlinejudge.org/external/112/11292.pdf + * OJ : Uva + * Verdict : AC + * Date : 08.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 20005 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +int main () { + int n, m; + int ar[MAX], am[MAX]; + while (scanf("%d %d", &n, &m) == 2 && n != 0 && m != 0) { + for (int i = 0; i < n; i++) scanf("%d", &ar[i]); + for (int i = 0; i < m; i++) scanf("%d", &am[i]); + if (n > m) printf("Loowater is doomed!\n"); + else { + int flag ,sum = 0; + sort(am, am+m); + for (int i = 0; i < n; i++) { + int x = ar[i]; + flag = 0; + for (int j = 0 ; j < m; j++) { + if(x<=am[j] && am[j] != -1){ + flag = 1; + sum +=am[j]; + am[j] = -1; + break; + } + } + if(flag == 0){ + break; + } + } + if(flag == 0) printf("Loowater is doomed!\n"); + else printf("%d\n", sum); + } + } + return 0; +} + diff --git a/uva_cpp_clean/11292/11292-17.cpp b/uva_cpp_clean/11292/11292-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c74c6f16d6d1776c4a97d98d07e657dd82b338d --- /dev/null +++ b/uva_cpp_clean/11292/11292-17.cpp @@ -0,0 +1,31 @@ +/** + * The Dragon of Loowater + */ +#include +using namespace std; +int main() +{ + int n, m; + while ((cin >> n >> m) && n && m) + { + vector heads(n), knights(m); + for (int &i : heads) + cin >> i; + for (int &i : knights) + cin >> i; + sort(heads.begin(), heads.end()); + sort(knights.begin(), knights.end()); + int h = 0, ans = 0; + for (int k : knights) + if (k >= heads[h]) + { + ans += k; + if (++h == n) + break; + } + if (h < n) + cout << "Loowater is doomed!\n"; + else + cout << ans << "\n"; + } +} diff --git a/uva_cpp_clean/11292/11292-19.cpp b/uva_cpp_clean/11292/11292-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..922114cfc7e0e531e0142bf8e0b400e8edf955b4 --- /dev/null +++ b/uva_cpp_clean/11292/11292-19.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +int main() { + int heads[20001]; + int knights[20001]; + int n, m; + while (scanf("%d %d", &n, &m) && !(n == 0 && m == 0)) { + int cost = 0; + for (int i = 0; i < n; i++) scanf("%d", &heads[i]); + for (int i = 0; i < m; i++) scanf("%d", &knights[i]); + sort(heads, heads+n); + sort(knights, knights+m); + int i = 0, j = 0; + bool stop = false; + while (i < n && j < m) { + if (heads[i] <= knights[j]) { + cost += knights[j]; + i++; + } + j++; + if (n - i > m - j) stop = true; + } + if (stop) printf("Loowater is doomed!\n"); + else printf("%d\n", cost); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11292/11292-21.cpp b/uva_cpp_clean/11292/11292-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d721514d4cdb6014e33a4df21a9408900734bcd2 --- /dev/null +++ b/uva_cpp_clean/11292/11292-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11292 + Name: Dragon of Loowater + Problem: https://onlinejudge.org/external/112/11292.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int n, m; +int dh[20001], kh[20001]; + +int main(){ + while(cin>>n>>m && (n||m)) { + int mindh=2147483647; + for (int i=0; i>dh[i]; + if (dh[i]>kh[i]; + if (kh[i] m) doomed = true; + else { + sort(dh, dh+n); + sort(kh, kh+m); + int j=-1; + for (int i=0; !doomed && i bipartite matching + difficulty: easy + date: 15/Jan/2020 + by: @brpapa +*/ +#include +using namespace std; + +int main() { + int N, M; + while (cin >> N >> M && (N || M)) { + vector diameters(N), heights(M); + for (int &d : diameters) cin >> d; + for (int &h : heights) cin >> h; + + sort(diameters.begin(), diameters.end()); + sort(heights.begin(), heights.end()); + + int ans = 0, n = 0; + for (int m = 0; m < M && n < N; m++) + if (diameters[n] <= heights[m]) + ans += heights[m], n++; + + if (n == N) cout << ans << endl; + else cout << "Loowater is doomed!" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11296/11296-18.cpp b/uva_cpp_clean/11296/11296-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9fa7033bc7d8e11a921e898bbc5b65df2a277990 --- /dev/null +++ b/uva_cpp_clean/11296/11296-18.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() { + int n; + while (scanf("%d", &n) != EOF) { + long long ans = 0; + for (int i = 0; 2*i <= n; i++) ans+=i + 1; + printf("%lld\n", ans); + } + return 0; +} +/* + Un problema bastante sencillo si se observa el patron de la solucion. El truco aqui es implementar por fuerza bruta + la solucion primero y observar si la solucion tiene un patron, efectivamente si lo tiene. El patron es la sumatoria + de los primeros n numeros pero con dos condiciones: + 1) El resultado se repite en parejas, donde un numero de la pareja es un n par y el n+1 es impar + 2) Tiene un desfase de 1 +*/ \ No newline at end of file diff --git a/uva_cpp_clean/11296/11296-20.cpp b/uva_cpp_clean/11296/11296-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c96e69ad9c42fdb413ed55c286b14d59ae2e021c --- /dev/null +++ b/uva_cpp_clean/11296/11296-20.cpp @@ -0,0 +1,14 @@ +#include +#include + +using namespace std; + +int main() +{ + long long n; + + while( cin >> n ) + cout << (n/2+2)*(n/2+1)/2 << endl; + + return 0; +} diff --git a/uva_cpp_clean/11297/11297-21.cpp b/uva_cpp_clean/11297/11297-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce174d10283f5809ccdc59f041f5c448eedefe7d --- /dev/null +++ b/uva_cpp_clean/11297/11297-21.cpp @@ -0,0 +1,155 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11297 + Name: Census + Problem: https://onlinejudge.org/external/112/11297.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) return EOF; + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} +inline char readcm() { + char ch; + while ( (ch=readchar())==' ' || ch=='\n' || ch=='\t' ); + return ch; +} + +// ------------------------------------------------------------------ + +inline int max(int a, int b, int c, int d) { + return a>b && a>c && a>d ? a : + b>c && b>d ? b : + c>d ? c : + d; +} +inline int min(int a, int b, int c, int d) { + return a> 1, + my = (y1 + y2) >> 1, + id1,id2,id3,id4; + + initialize(x1, y1, mx, my, id1=chid(id, 0)); + initialize(mx, y1, x2, my, id2=chid(id, 1)); + initialize(x1, my, mx, y2, id3=chid(id, 2)); + initialize(mx, my, x2, y2, id4=chid(id, 3)); + MN[id] = min( MN[id1] , MN[id2] , MN[id3] , MN[id4] ); + MX[id] = max( MX[id1] , MX[id2] , MX[id3] , MX[id4] ); +} + +void updateValue(int x, int y, int x1=0, int y1=0, int x2=n, int y2=n, int id=1) { + if (x=x2 || y=y2) return; + if (x2-x1==1 && y2-y1==1) + MN[id] = MX[id] = V[x1][y1]; + if (x2-x1<=1 && y2-y1<=1) return; + + int mx = (x1 + x2) >> 1, + my = (y1 + y2) >> 1, + id1,id2,id3,id4; + + updateValue(x, y, x1, y1, mx, my, id1=chid(id, 0)); + updateValue(x, y, mx, y1, x2, my, id2=chid(id, 1)); + updateValue(x, y, x1, my, mx, y2, id3=chid(id, 2)); + updateValue(x, y, mx, my, x2, y2, id4=chid(id, 3)); + MN[id] = min( MN[id1] , MN[id2] , MN[id3] , MN[id4] ); + MX[id] = max( MX[id1] , MX[id2] , MX[id3] , MX[id4] ); +} + +int maxx, minn; +void solve(int qx1, int qy1, int qx2, int qy2, int x1=0, int y1=0, int x2=n, int y2=n, int id=1) { + if (qx1>=x2 || qx2<=x1 || qy1>=y2 || qy2<=y1 || (MN[id]>=minn && MX[id]<=maxx)) + return; + + if (qx1<=x1 && qx2>=x2 && qy1<=y1 && qy2>=y2) { + maxx = max(maxx, MX[id]); + minn = min(minn, MN[id]); + return; + } + + int mx = (x1 + x2) >> 1, + my = (y1 + y2) >> 1; + solve(qx1, qy1, qx2, qy2, x1, y1, mx, my, chid(id, 0)); + solve(qx1, qy1, qx2, qy2, mx, y1, x2, my, chid(id, 1)); + solve(qx1, qy1, qx2, qy2, x1, my, mx, y2, chid(id, 2)); + solve(qx1, qy1, qx2, qy2, mx, my, x2, y2, chid(id, 3)); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + n = readInt(); + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + double n, p, k; + while (cin >> n >> p) { + k = pow(p, 1.0/n); + printf ("%0.lf\n", k); + } + return 0; +} diff --git a/uva_cpp_clean/113/113-18.cpp b/uva_cpp_clean/113/113-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3148da57c570904afa3d320ee3849e4e9b0b7dd1 --- /dev/null +++ b/uva_cpp_clean/113/113-18.cpp @@ -0,0 +1,10 @@ +#include +#include + +int main(){ + double n, p; + while (scanf("%lf%lf", &n, &p) == 2){ + printf("%.0lf\n", pow(p, 1 / n)); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/113/113-21.cpp b/uva_cpp_clean/113/113-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4136fcec4dae2e95437955f6e6a42ffd9e15e7e3 --- /dev/null +++ b/uva_cpp_clean/113/113-21.cpp @@ -0,0 +1,969 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 113 + Name: Power of Cryptography + Problem: https://onlinejudge.org/external/1/113.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +#ifndef min +#define min(x,y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef max +#define max(x,y) ((x) > (y) ? (x) : (y)) +#endif + +class BigInt +{ +private: + char *digits; + int size; // number of used bytes (digits) + int capacity; // size of digits + int sign; // -1, 0 or +1 + +public: + BigInt(int n, int cap); + BigInt(int n); + BigInt(long double d); + BigInt(); + BigInt(string s); + BigInt(const char s[]); + BigInt(const BigInt &n); + const BigInt &operator=(const BigInt &n); + const BigInt &operator=(int n); + ~BigInt(); + void normalize(); + static int sig(int n); + static int sig(long double n); + inline int length() { return size; } + + BigInt operator++(); + BigInt operator++(int); + BigInt operator--(); + BigInt operator--(int); + BigInt operator-(); + BigInt operator+ (int n); + BigInt operator+ (BigInt n); + BigInt&operator+=(int n); + BigInt&operator+=(BigInt n); + BigInt operator- (int n); + BigInt operator- (BigInt n); + BigInt&operator-=(int n); + BigInt&operator-=(BigInt n); + BigInt operator* (int n); + BigInt operator* (BigInt n); + void operator*=(int n); + void operator*=(BigInt n); + BigInt operator/ (int n); + BigInt operator/ (BigInt n); + void operator/=(int n); + void operator/=(BigInt n); + int operator% (int n); + BigInt operator% (BigInt n); + void operator%=(int n); + void operator%=(BigInt n); + int divide(int n); + BigInt divide(BigInt n); + BigInt operator* (long double n); + void operator*=(long double n); + + + BigInt operator<< (int n); + void operator<<=(int n); + BigInt operator >> (int n); + void operator>>=(int n); + BigInt operator,(int n); + BigInt operator,(BigInt n); + + /** Casting **/ + bool operator!(); + operator bool(); + operator string(); + + bool operator<(BigInt n); + bool operator>(BigInt n); + bool operator==(BigInt n); + bool operator<=(BigInt n); + bool operator>=(BigInt n); + bool operator<(int n); + bool operator>(int n); + bool operator==(int n); + bool operator<=(int n); + bool operator>=(int n); + int compare(BigInt n); + + int toInt(); + string toString(); + void print(); + void printWithCommas(ostream &out); + +private: + void grow(); + friend istream &operator >> (istream &in, BigInt &n); + friend ostream &operator<<(ostream &out, BigInt n); + friend long double log2(BigInt x, long double epsilon); + inline friend long double log(BigInt x, long double epsilon); + inline friend long double log10(BigInt x, long double epsilon); + inline friend long double lg(BigInt x, long double epsilon); + inline friend long double ln(BigInt x, long double epsilon); +}; + +BigInt operator+(int m, BigInt &n) +{ + return n + m; +} + +BigInt operator-(int m, BigInt &n) +{ + return -n + m; +} + +BigInt operator*(int m, BigInt &n) +{ + return n * m; +} + +BigInt operator/(int m, BigInt &n) +{ + return BigInt(m) / n; +} + +BigInt operator%(int m, BigInt &n) +{ + return BigInt(m) % n; +} + +inline bool isDigit(int c) +{ + return(c >= (int)'0' && c <= (int)'9'); +} + +istream &operator >> (istream &in, BigInt &n) // FIXME: see inside +{ + n.size = 0; + n.sign = 1; + int sign = 1; + int c; + while ((c = in.peek()) >= 0 && + (c == ' ' || c == '\t' || c == '\r' || c == '\n')) + in.get(); + if (c < 0 || (c != (int)'-' && !isDigit(c))) + { + in >> c; // XXX: force in.fail() + return in; + } + if (c == (int)'-') { sign = -1; in.get(); } + + // FIXME: Extremely inefficient! Use a string. + while ((c = in.peek()) >= 0 && isDigit(c)) + { + in.get(); + n *= 10; + n += (c - (int)'0'); + } + n.sign = sign; + n.normalize(); + return in; +} + +ostream &operator<<(ostream &out, BigInt n) //FIXME: make more efficient +{ + return out << n.toString(); +} + +BigInt::BigInt(int n, int cap) +{ + cap = max(cap, (int)sizeof(n) * 8); + capacity = cap; + sign = sig(n); + n *= sign; + digits = new char[cap]; + memset(digits, 0, cap); + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(int n) +{ + capacity = 1024; + sign = sig(n); + n *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + while (n) + { + digits[size++] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(long double d) +{ + capacity = 1024; + sign = (d < 0 ? -1 : d > 0 ? 1 : 0); + d *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + d = floor(d); + while (d > 0) + { + digits[size++] = min(max(0, (int)((d - floor(d / 10) * 10) + 0.5)), 9); + d = floor(d / 10); + } +} + +BigInt::BigInt() +{ + capacity = 128; + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; +} + +BigInt::BigInt(string s) +{ + capacity = max((int)s.size(), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const char s[]) +{ + capacity = max((int)strlen(s), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const BigInt &n) +{ + capacity = n.capacity; + sign = n.sign; + size = n.size; + digits = new char[capacity]; + memcpy(digits, n.digits, capacity); +} + +const BigInt &BigInt::operator=(const BigInt &n) +{ + if (&n != this) + { + if (capacity < n.size) + { + capacity = n.capacity; + delete[] digits; + digits = new char[capacity]; + } + sign = n.sign; + size = n.size; + memcpy(digits, n.digits, size); + memset(digits + size, 0, capacity - size); + } + return *this; +} + +const BigInt &BigInt::operator=(int n) +{ + sign = sig(n); + n *= sign; + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } + return *this; +} + +BigInt::~BigInt() +{ + delete[] digits; +} + +void BigInt::normalize() +{ + while (size && !digits[size - 1]) size--; + if (!size) sign = 0; +} + +int BigInt::sig(int n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::sig(long double n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::toInt() +{ + int result = 0; + for (int i = size - 1; i >= 0; i--) + { + result *= 10; + result += digits[i]; + if (result < 0) return sign * 0x7FFFFFFF; + } + return sign * result; +} + +string BigInt::toString() +{ + string s = (sign >= 0 ? "" : "-"); + for (int i = size - 1; i >= 0; i--) + s += (digits[i] + '0'); + if (size == 0) s += '0'; + return s; +} + +void BigInt::print() //FIXME: make more efficient +{ + cout << toString(); +} + +void BigInt::printWithCommas(ostream &out) +{ + if (sign < 0) out.put('-'); + for (int i = size - 1; i >= 0; i--) + { + out.put(digits[i] + '0'); + if (!(i % 3) && i) out.put(','); + } + if (size == 0) out.put('0'); +} + +void BigInt::grow() +{ + char *olddigits = digits; + int oldCap = capacity; + capacity *= 2; + digits = new char[capacity]; + memcpy(digits, olddigits, oldCap); + memset(digits + oldCap, 0, oldCap); + delete[] olddigits; +} + +BigInt BigInt::operator++() +{ + operator+=(1); + return *this; +} + +BigInt BigInt::operator++(int) +{ + return operator++(); +} + +BigInt BigInt::operator--() +{ + operator-=(1); + return *this; +} + +BigInt BigInt::operator--(int) +{ + return operator--(); +} + +BigInt BigInt::operator-() +{ + BigInt result(*this); + result.sign *= -1; + return result; +} + +BigInt BigInt::operator+(int n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt BigInt::operator+(BigInt n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt &BigInt::operator+=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = nsign; + if (sign == nsign) + { + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] + dig + carry; + digits[i] = newdig % 10; + carry = newdig / 10; + n /= 10; + } + size = max(i, size); + } + else operator-=(-n); + return *this; +} + +BigInt &BigInt::operator+=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = n.sign; + if (sign == n.sign) + { + int carry = 0; + int i; + for (i = 0; i < maxS - 1 || carry; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig += n.digits[i]; + digits[i] = newdig % 10; + carry = newdig / 10; + } + size = max(i, size); + } + else + { + n.sign *= -1; + operator-=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator-(int n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt BigInt::operator-(BigInt n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt &BigInt::operator-=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = 1; + if (sign == nsign) + { + BigInt bin = n; + if (sign >= 0 && *this < bin || sign < 0 && *this > bin) + { + // Subtracting a bigger number + operator=(toInt() - n); + return *this; + } + + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] - dig + carry; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + n /= 10; + } + normalize(); + } + else operator+=(-n); + return *this; +} + +BigInt &BigInt::operator-=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = 1; + if (sign == n.sign) + { + if (sign >= 0 && *this < n || sign < 0 && *this > n) + { + // Subtracting a bigger number + BigInt tmp = n; + tmp -= *this; + *this = tmp; + sign = -sign; + return *this; + } + + int carry = 0; + int i; + for (i = 0; i < maxS - 1; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig -= n.digits[i]; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + } + if (carry) // Subtracted a bigger number, need to flip sign + { + if (i) digits[0] = 10 - digits[0]; + size = (i ? 1 : 0); + for (int j = 1; j < i; j++) + { + digits[j] = 9 - digits[j]; + if (digits[i]) size = j + 1; + } + sign *= -1; + } + normalize(); + } + else + { + n.sign *= -1; + operator+=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator*(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + int nsign = sig(n); + n *= nsign; + result.sign = sign * nsign; + if (!result.sign) return result; + + int i, j; + for (i = 0; n; i++) + { + int dig = n % 10; + if (dig) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = result.digits[i + j] + (j < size ? dig * digits[j] : 0) + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + n /= 10; + } + result.size = i + j - 1; + return result; +} + +BigInt BigInt::operator*(BigInt n) +{ + BigInt result(0, size + n.size); + + result.sign = sign * n.sign; + if (!result.sign) return result; + + int i, j; + for (i = 0; i < n.size; i++) + { + if (n.digits[i]) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = + result.digits[i + j] + + (j < size ? n.digits[i] * digits[j] : 0) + + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + } + result.size = i + j - 1; + + return result; +} + +void BigInt::operator*=(int n) +{ + operator=(operator*(n)); +} + +void BigInt::operator*=(BigInt n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator/(int n) +{ + if (!n) n /= n; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +BigInt BigInt::operator/(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +void BigInt::operator/=(int n) +{ + divide(n); +} + +void BigInt::operator/=(BigInt n) +{ + divide(n); +} + +int BigInt::operator%(int n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(int n) +{ + operator=(divide(n)); +} + +BigInt BigInt::operator%(BigInt n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(BigInt n) +{ + operator=(divide(n)); +} + +int BigInt::divide(int n) +{ + if (!n) n /= n; //XXX: force a crash + + int nsign = sig(n); + n *= nsign; + if (!sign) return 0; + sign *= nsign; + + int tmp = 0; + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = tmp / n; + tmp -= digits[i] * n; + } + normalize(); + return tmp; +} + +BigInt BigInt::divide(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + if (!sign) return 0; + sign *= n.sign; + + int oldSign = n.sign; + n.sign = 1; + + BigInt tmp(0, size); + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = 0; + while (tmp >= n) { tmp -= n; digits[i]++; } + } + normalize(); + + n.sign = oldSign; + + return tmp; +} + +// This is only exact to the first 15 or so digits, but it is +// never an over-estimate +BigInt BigInt::operator*(long double n) +{ + // the number of digits after the decimal point to use + #define DIGS_AFTER_DOT 15 + + int nsign = sig(n); + n *= nsign; + int ndigs = n >= 1 ? (int)log10(n) + 1 : 0; + BigInt result(0, size + ndigs); + result.sign = sign * nsign; + if (!result.sign) return result; + + if (n >= 1) for (int i = 0; i < ndigs; i++) n /= 10; + result.size = 0; + + char afterDot[DIGS_AFTER_DOT + 1]; + memset(afterDot, 0, sizeof(afterDot)); + + // Keep going until the DIGS_AFTER_DOT'th digit after the decimal point + for (int i = ndigs - 1; i >= -DIGS_AFTER_DOT; i--) + { + n *= 10; + int dig = (int)floor(n); + n -= dig; + if (!dig) continue; + + int carry = 0; + for (int j = 0; j < size || carry; j++) + { + int newdig = + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) + + dig * digits[j] + + carry; + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) = newdig % 10; + if (i + j >= 0 && result.digits[i + j]) result.size = max(result.size, i + j + 1); + carry = newdig / 10; + } + } + if (!result.size) result.sign = 0; + return result; +} + +void BigInt::operator*=(long double n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator<<(int n) +{ + BigInt result(*this); + result <<= n; + return result; +} + +void BigInt::operator<<=(int n) +{ + if (n < 0) operator>>=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator*=(mult); + } +} + +BigInt BigInt::operator >> (int n) +{ + BigInt result(*this); + result >>= n; + return result; +} + +void BigInt::operator>>=(int n) +{ + if (n < 0) operator<<=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator/=(mult); + } +} + +BigInt BigInt::operator,(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + for (result.size = 0; n; result.size++) + { + result.digits[result.size] = n % 10; + n /= 10; + } + memcpy(result.digits + result.size, digits, size * sizeof(digits[0])); + result.size += size; + result.sign = 1; + result.normalize(); + return result; +} + +BigInt BigInt::operator,(BigInt n) +{ + BigInt result(0, size + n.size); + memcpy(result.digits, n.digits, n.size * sizeof(n.digits[0])); + memcpy(result.digits + n.size, digits, size * sizeof(digits[0])); + result.size = size + n.size; + result.sign = 1; + result.normalize(); + return result; +} + +bool BigInt::operator!() +{ + return !size; +} + +BigInt::operator bool() +{ + return size; +} + +BigInt::operator string() +{ + return toString(); +} + +bool BigInt::operator<(BigInt n) +{ + return(compare(n) < 0); +} + +bool BigInt::operator>(BigInt n) +{ + return(compare(n) > 0); +} + +bool BigInt::operator==(BigInt n) +{ + return(compare(n) == 0); +} + +bool BigInt::operator<=(BigInt n) +{ + return(compare(n) <= 0); +} + +bool BigInt::operator>=(BigInt n) +{ + return(compare(n) >= 0); +} + +bool BigInt::operator<(int n) +{ + return(compare(BigInt(n)) < 0); +} + +bool BigInt::operator>(int n) +{ + return(compare(BigInt(n)) > 0); +} + +bool BigInt::operator==(int n) +{ + return(compare(BigInt(n)) == 0); +} + +bool BigInt::operator<=(int n) +{ + return(compare(BigInt(n)) <= 0); +} + +bool BigInt::operator>=(int n) +{ + return(compare(BigInt(n)) >= 0); +} + +int BigInt::compare(BigInt n) +{ + if (sign < n.sign) return -1; + if (sign > n.sign) return 1; + if (size < n.size) return -sign; + if (size > n.size) return sign; + for (int i = size - 1; i >= 0; i--) + { + if (digits[i] < n.digits[i]) return -sign; + else if (digits[i] > n.digits[i]) return sign; + } + return 0; +} + +long double log2(BigInt x, long double epsilon = 0.000000000000001) +{ + static /* const */ long double O = 0.0; + if (x.sign <= 0) return O / O; // Return NaN + + long double y = 0.0, z = 1.0, f = 0.0; + while (x >= 2) + { + if (x.divide(2)) f += 1.0; + f /= 2.0; + y++; + } + f += 1.0; + while (z > epsilon) + { + f *= f; + z /= 2.0; + if (f >= 2.0) + { + y += z; + f /= 2.0; + } + } + return y; +} + + +int main() +{ + int n; + char p[102]; + + while (scanf("%d %s", &n, p) == 2) + cout << (int)(0.5 + pow(2, log2(BigInt(p)) / n)) << endl; +} + diff --git a/uva_cpp_clean/113/113-5.cpp b/uva_cpp_clean/113/113-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7c0bf83f55e7694b9b0b62b373b3c2a66bf88654 --- /dev/null +++ b/uva_cpp_clean/113/113-5.cpp @@ -0,0 +1,14 @@ +#include +#include +int main() +{ +double n,p; +double s; +while(scanf("%lf %lf",&n,&p)==2) +{ +s=pow(p,1/n); +printf("%.0lf\n",s); +} +return 0; +} + diff --git a/uva_cpp_clean/11307/11307-21.cpp b/uva_cpp_clean/11307/11307-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ccf40d2121030f436ae245b562f71b1dd560979 --- /dev/null +++ b/uva_cpp_clean/11307/11307-21.cpp @@ -0,0 +1,99 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11307 + Name: Alternative Arborescence + Problem: https://onlinejudge.org/external/113/11307.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 268435456 +using namespace std; + +inline int readchar(bool flag = true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (flag) p++; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + + while (isdigit(ch=readchar(0))) + r = (r<<3) + (r<<1) + (ch=readchar())-'0'; + + return r; +} + +// ----------------------------------------------------------------- + +vector adj[10043]; +int DP[10043][17], n; +bool inside[10043]; +void dfs(int u) { + inside[u] = 1; + + for (int v: adj[u]) + if (!inside[v]) + dfs(v); + + for (int i=1; i<11; ++i) { + DP[u][i] = i; + for (int v: adj[u]) + if (!inside[v]) { + int mn = INF; + for (int j=1; j<11; ++j) + if (i!=j && DP[v][j] 0) { + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +#define psl pair + +vector>res; + +bool cmp (pair a, pair b) { + if (a.second == b.second ) return a.first < b.first; + + return ( a.second < b.second ); +} + +void sort_map (map &mp, vector &vv) { + for (auto it : mp) { + vv.pb ({it.F, it.S}); + } + + sort (all (vv), cmp); +} + +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + + while (tc--) { + cin.ignore(); + string title; + getline (cin, title); + int m, n, b; + cin >> m >> n >> b; + mapmp; + + for (int i = 0; i < m; i++) { + string str; + ll val; + cin >> str >> val; + mp[str] = val; + } + + //~ cout << title << " :-\n"; + mapreci; + + for (int i = 0; i < n; i++) { + //~ cout << i << " :n\n"; + string rec_name; + cin.ignore(); + getline (cin, rec_name); + int q; + cin >> q; + int sum = 0; + + for (int j = 0; j < q; j++) { + string str; + ll val; + cin >> str >> val; + ll tmp = val * mp[str]; + sum += tmp; + //~ cout << tmp << " :tmp\n"; + } + + if (sum <= b) { + res.pb ({rec_name, sum}); + } + } + + //~ cout << "hahahahahahahahha\n"; + + for (char ch : title) { + //~ if(isa) + cout << (char) toupper (ch); + } + nl; + + if ( (int) res.size() == 0) { + cout << "Too expensive!\n"; + + } else { + sort (all (res), cmp); + + for (int i = 0 ; i < (int) res.size() ; i++ ) { + cout << res[i].first << "\n"; + } + } + + nl; + res.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/11308/11308-21.cpp b/uva_cpp_clean/11308/11308-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5e241a2a1bcc5711d9e9e1c9b21b7ac6700ce41 --- /dev/null +++ b/uva_cpp_clean/11308/11308-21.cpp @@ -0,0 +1,105 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11308 + Name: Bankrupt Baker + Problem: https://onlinejudge.org/external/113/11308.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +struct Node { + int val; + Node *nxt[26]; + Node(): val(-1) { memset(nxt, 0, sizeof(nxt)); } + ~Node() { + for (int i=0; i<26; ++i) + if (nxt[i]) + delete nxt[i]; + } +}; + +Node *root; +void setVal(char str[], int val) { + Node *cur = root; + for (int i=0; str[i]; ++i) { + char ch = tolower(str[i])-'a'; + if (!cur->nxt[ch]) + cur->nxt[ch] = new Node(); + cur = cur->nxt[ch]; + } + cur->val = val; +} + +int getVal(char str[]) { + Node *cur = root; + for (int i=0; str[i]; ++i) + cur = cur->nxt[tolower(str[i])-'a']; + return cur->val; +} + + + + +#define MAXL 10000 +void skipline() { + while (getchar()!='\n'); +} + +typedef pair Pair; +int main(){ + int T, n, m, b, c, t; + scanf("%d", &T); + char rtitle[MAXL], str[MAXL]; + while (T--) { + if (root) delete root; + root = new Node(); + + skipline(); + cin.getline(rtitle, MAXL); + for (int i=0; rtitle[i]; ++i) + putchar(toupper(rtitle[i])); + putchar('\n'); + + scanf("%d%d%d", &n, &m, &b); + + for (int i=0; i vec; + for (int i=0; i + +using namespace std; + +int tc, HH, MM; + +bool is_palindrome(int n){ + int pib = 0, tmp = n; + while(tmp) pib = pib * 10 + tmp % 10, tmp /= 10; + return ( n == pib ); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%2d:%2d",&HH, &MM); + while(true){ + MM++; + if(MM == 60) MM = 0, HH++; + if(HH == 24) HH = 0; + if(is_palindrome(HH * 100 + MM)) + break; + } + printf("%.2d:%.2d\n", HH, MM); + } + return(0); +} diff --git a/uva_cpp_clean/11310/11310-13.cpp b/uva_cpp_clean/11310/11310-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bab67a18e5a5d26b6ae896eaa9939e7a44c20980 --- /dev/null +++ b/uva_cpp_clean/11310/11310-13.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +typedef long long ll; + +ll answer[41] = {0}; + +int main() +{ + answer[0] = 1; + answer[1] = 1; + answer[2] = 5; + + for (int i = 3; i < 41; ++i) + { + answer[i] = answer[i - 1] + 4*answer[i - 2] + 2*answer[i - 3]; + } + + int t; + + cin >> t; + + while (t--) + { + int n; + + cin >> n; + + cout << answer[n] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11310/11310-19.cpp b/uva_cpp_clean/11310/11310-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4a128c51920e00c7069d5856f7dea5c82e90c6e --- /dev/null +++ b/uva_cpp_clean/11310/11310-19.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +long long dp[41]; + +int main() { + dp[0] = 1, dp[1] = 1, dp[2] = 5; + for (int i = 3; i < 41; i++) { + dp[i] = dp[i-1] + 4*dp[i-2] + 2*dp[i-3]; + } + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + printf("%lld\n", dp[n]); + } + return 0; +} diff --git a/uva_cpp_clean/11310/11310-21.cpp b/uva_cpp_clean/11310/11310-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f00a5e09d835f76dafb17eb7c6a62a8273cb5e46 --- /dev/null +++ b/uva_cpp_clean/11310/11310-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11310 + Name: Delivery Debacle + Problem: https://onlinejudge.org/external/113/11310.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long DP[43] = { 1, 1, 5 }; + for (int i=3; i<43; ++i) + DP[i] = DP[i-1] + 4*DP[i-2] + 2*DP[i-3]; + + int T, n; + cin >> T; + while (T-- && cin>>n) + cout << DP[n] << '\n'; +} diff --git a/uva_cpp_clean/11311/11311-9.cpp b/uva_cpp_clean/11311/11311-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74c584a63b806c8bc8e57c695153982a488bf95a --- /dev/null +++ b/uva_cpp_clean/11311/11311-9.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int tc, m, n, r, c; + +int main() { + scanf("%d", &tc); + while (tc--) { + scanf("%d %d %d %d", &m, &n, &r, &c); + puts((r xor c xor (m - r - 1) xor (n - c - 1)) == 0 ? "Hansel" : "Gretel"); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/11313/11313-20.cpp b/uva_cpp_clean/11313/11313-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28397aa68f89223c58cb3b7efcd8c4a5a200a0f0 --- /dev/null +++ b/uva_cpp_clean/11313/11313-20.cpp @@ -0,0 +1,20 @@ +#include +#include + +using namespace std; + +int main() +{ + int tc; + + cin >> tc; + while( tc-- ) + { + int n, m; + cin >> n >> m; + + if( (n-1)%(m-1)!=0 ) cout << "cannot do this" << endl; + else cout << (n-1)/(m-1) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11313/11313-21.cpp b/uva_cpp_clean/11313/11313-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0990e9d40a11b621ccd53a9d4ef2a5ca0a6f8961 --- /dev/null +++ b/uva_cpp_clean/11313/11313-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11313 + Name: Gourmet Games + Problem: https://onlinejudge.org/external/113/11313.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, n, m, cnt; + cin>>T; + while (T--) { + cin >> n >> m; + for (cnt=0; n>=m; ) { + cnt += n/m; + n = (n/m) + (n%m); + } + if (n==1) cout << cnt << endl; + else puts("cannot do this"); + } +} diff --git a/uva_cpp_clean/11313/11313-9.cpp b/uva_cpp_clean/11313/11313-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f149f16f6b36754d48d783001dd92594e8301de --- /dev/null +++ b/uva_cpp_clean/11313/11313-9.cpp @@ -0,0 +1,12 @@ +#include + +using namespace std; + +int main(){ + int c, n, m; + scanf("%d", &c); + while(c--) + scanf("%d %d", &n, &m), + (n - 1)%(m - 1) ? printf("cannot do this\n") : printf("%d\n",(n - 1) / (m - 1)); + return(0); +} diff --git a/uva_cpp_clean/11321/11321-14.cpp b/uva_cpp_clean/11321/11321-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..023f5d4ce81ea4d7227dec726ce95a1047ba507f --- /dev/null +++ b/uva_cpp_clean/11321/11321-14.cpp @@ -0,0 +1,93 @@ +/*************************************************** + * Problem name : 11321 Sort! Sort!! and Sort!!!.cpp + * Problem Link : https://uva.onlinejudge.org/external/113/11321.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-05 + * Problem Type : Adhoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +struct SortAr { + ll num, mod; +}; +bool cmp(SortAr a, SortAr b) { + if (a.mod == b.mod) { + if (a.num % 2 != 0 && b.num % 2 == 0) { + return true; + } else if (a.num % 2 == 0 && b.num % 2 != 0) { + return false; + } else if (a.num % 2 != 0 && b.num % 2 != 0) { + return a.num > b.num; + } else { + return a.num < b.num; + } + } else return a.mod < b.mod; +} +int main () { + ll n, m; + while (scanf("%lld %lld", &n, &m) == 2) { + if (n == 0 && m == 0) { + printf("0 0\n"); + break; + } + SortAr ar[n + 3]; + for (ll i = 0 ; i < n; i++) { + ll x; + scanf("%lld", &x); + ar[i].num = x; + ar[i].mod = x % m; + } + sort(ar, ar + n, cmp); + printf("%lld %lld\n", n, m); + for (ll i = 0; i < n; i++) { + printf("%lld\n", ar[i].num); + } + } + return 0; +} diff --git a/uva_cpp_clean/11321/11321-21.cpp b/uva_cpp_clean/11321/11321-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ce9eb2ce56ba0ac9ce292ac3f7f1eb3c3f88c9e --- /dev/null +++ b/uva_cpp_clean/11321/11321-21.cpp @@ -0,0 +1,82 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11321 + Name: Sort! Sort!! and Sort!!! + Problem: https://onlinejudge.org/external/113/11321.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-'); + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +char buf[50]; +void print(long long x) { + int i=48, neg=0; + buf[i--] = '\n'; + if (!x) buf[i--] = '0'; + else if (x < 0) { + x = -x; + neg = 1; + } + for (; x; x/=10) + buf[i--] = x%10 + '0'; + + if (neg) buf[i--] = '-'; + fwrite_unlocked(buf+i+1, 1, 48-i, stdout); +} + +// ------------------------------------------------------------------- + +int n, m, Y[20000]; +bool comp(int x, int y) { + int md1 = x%m, md2 = y%m; + if (md1 != md2) return md1 < md2; + int eo1 = (2+x%2)&1, eo2 = (2+y%2)&1; + return eo1 != eo2 ? eo1 > eo2 : + eo1 ? x > y : x < y; +} + +int main() { + while ((n=readInt()) && (m=readInt())) { + printf("%d %d\n", n, m); + for (int i=0; i + +using namespace std; + +int n, m, tmp; +vector < pair > v; + +bool cmp( pair x, pair y){ + if(x.first != y.first) return (x.first < y.first); + if((x.second & 1) && !(y.second & 1)) return true; + if(!(x.second & 1) && (y.second & 1)) return false; + if((x.second & 1) && (y.second & 1)) return (x.second > y.second); + if(!(x.second & 1) && !(y.second & 1)) return (x.second < y.second); +} + +int main(){ + while(scanf("%d %d", &n, &m),n | m){ + v.resize(n); + for(int i = 0; i < n; i++) scanf("%d", &tmp), v[i] = make_pair(tmp % m, tmp); + sort(v.begin(), v.end(), cmp); + printf("%d %d\n", n, m); + for(int i = 0; i < n; i++) + printf("%d\n", v[i].second); + } + puts("0 0"); + return(0); +} diff --git a/uva_cpp_clean/11327/11327-9.cpp b/uva_cpp_clean/11327/11327-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..448eb7ff978f0726c48623e350db200a06e3498d --- /dev/null +++ b/uva_cpp_clean/11327/11327-9.cpp @@ -0,0 +1,35 @@ +#include + +#define SIZE 200010 + +using namespace std; + +int EulerPhi[SIZE], num, den; +long long dp[SIZE], k, ct; + +void sieve(){ + for(int it = 2; it < SIZE; it++) EulerPhi[it] = it; + dp[1] = 2; + for(int it1 = 2; it1 < SIZE; it1++){ + if( EulerPhi[it1] == it1 ) + for(int it2 = it1; it2 < SIZE; it2 += it1) + EulerPhi[it2] -= EulerPhi[it2] / it1; + dp[it1] = dp[it1 - 1] + EulerPhi[it1]; + } +} + +int gcd(int a, int b){ + return b == 0 ? a : gcd(b, a % b); +} + +int main(){ + sieve(); + while(scanf("%lld", &k), k){ + den = lower_bound(dp, dp + SIZE, k) - dp; + ct = dp[den -1]; + for(num = 0; num <= den and ct != k; num++) + if( gcd(num, den) == 1 ) ct++; + printf("%d/%d\n", num - 1, den); + } + return(0); +} diff --git a/uva_cpp_clean/11332/11332-19.cpp b/uva_cpp_clean/11332/11332-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06ab53f1db67f6048026a5a0daea34a4ffffda49 --- /dev/null +++ b/uva_cpp_clean/11332/11332-19.cpp @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() { + int n; + while (scanf("%d", &n) && n != 0) { + n %= 9; + printf("%d\n", n == 0? 9: n); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11332/11332-2.cpp b/uva_cpp_clean/11332/11332-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..329f7da36e3667aef67c17fd27fad33adb3fb70f --- /dev/null +++ b/uva_cpp_clean/11332/11332-2.cpp @@ -0,0 +1,59 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +pair su(int n) { + int s = 0, c = 0; + while (n) { + s += n%10; + n/=10; + c++; + } + return {s, c}; +} + +int solve(int n) { + auto x = su(n); + if (x.second == 1) return x.first; + return solve(x.first); +} + + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int n; + while (cin >> n) { + if (n == 0) break; + cout << solve(n) << el; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11332/11332-21.cpp b/uva_cpp_clean/11332/11332-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb78b7bccab6f4e45698f868b0f13132a1801e24 --- /dev/null +++ b/uva_cpp_clean/11332/11332-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11332 + Name: Summing Digits + Problem: https://onlinejudge.org/external/113/11332.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int main() { + int n; + while ((cin >> n) && n) { + while (n > 9) { + int s = 0; + while (n) { + s += n % 10; + n /= 10; + } + n = s; + } + cout << n << endl; + } +} diff --git a/uva_cpp_clean/11332/11332-5.cpp b/uva_cpp_clean/11332/11332-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d580072110b67a25b45920d324f8b0106466f373 --- /dev/null +++ b/uva_cpp_clean/11332/11332-5.cpp @@ -0,0 +1,27 @@ +#include +int main() +{ + long n,sum,s; + while(scanf("%ld",&n)==1) + { + if(n==0) + break; + if (n<10) + printf("%ld\n",n); + else + { + while(n>9) + { + sum=0; + while(n!=0) + { + sum+=(n%10); + n/=10; + } + n=sum; + } + printf("%ld\n",n); + } + } + return 0; +} diff --git a/uva_cpp_clean/11332/11332-9.cpp b/uva_cpp_clean/11332/11332-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..753a57a6cee5b3aec74f210caa4dea3718e0341f --- /dev/null +++ b/uva_cpp_clean/11332/11332-9.cpp @@ -0,0 +1,20 @@ +#include + +int main(){ + int i, sum, tam, n, a; + while(scanf("%d", &n), n){ + if(n / 10 == 0) sum = n; + else + while(n / 10 != 0){ + sum = 0; + a = n; + while(a != 0){ + sum += a % 10; + a /= 10; + } + n = sum; + } + printf("%d\n", sum); + } + return(0); +} diff --git a/uva_cpp_clean/11332/main.cpp b/uva_cpp_clean/11332/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a33cfb93bb8ed8ba81c3ae4b4ed899187191da4 --- /dev/null +++ b/uva_cpp_clean/11332/main.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int sum_g(unsigned int f){ + float copy = f; + int sum = 0; + if(f < 10) return f; + else{ + while(copy > 0){ + sum += f % 10; + f /= 10; + copy /= 10; + } + return sum_g(sum); + } +} + +int main(){ + unsigned int n; + while(cin >> n && n!= 0){ + cout << sum_g(n) << endl; + } +return 0; +} diff --git a/uva_cpp_clean/11335/11335-21.cpp b/uva_cpp_clean/11335/11335-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..521c8c34792edeafd70fd22820c84816a5a4624b --- /dev/null +++ b/uva_cpp_clean/11335/11335-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11335 + Name: Discrete Pursuit + Problem: https://onlinejudge.org/external/113/11335.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int a, u, v; + while (cin >> a >> u >> v) { + int b = 0, c = 0, + t = 0; + + while (c < a || c < b) { + ++t; + c = t * (t + 1) >> 1; + a += u; + b += v; + } + cout << t << endl; + } +} diff --git a/uva_cpp_clean/11338/11338-21.cpp b/uva_cpp_clean/11338/11338-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84ad7ea70bf2c9c6bd603ef2fedcedbea97caa52 --- /dev/null +++ b/uva_cpp_clean/11338/11338-21.cpp @@ -0,0 +1,154 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11338 + Name: Minefield + Problem: https://onlinejudge.org/external/113/11338.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} +inline bool readDouble(double &r) { + char ch; + int sgn=1; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) + return 0; + + if (ch == '-') sgn = -1; + else r = ch-'0'; + + while (isdigit(ch=readchar())) + r = r*10 + ch-'0'; + + if (ch=='.') { + double p = 1; + while (isdigit(ch=readchar())) + r += (ch-'0') * (p *= 0.1); + } + return 1; +} + +// --------------------------------------------------------------------------- + + + +int n, S[10043], cse=1; +double maxd, D[10043], E[10043]; +struct Point { + double x, y; + bool operator < (const Point &p) const { return x < p.x; } +} P[10043]; + +double dist2(int i, int j) { + double xx = P[i].x-P[j].x, + yy = P[i].y-P[j].y; + return xx*xx + yy*yy; +} + +double est(int u) { + if (S[u]!= cse) { + S[u] = cse; + E[u] = sqrt(dist2(u, n+1)); + } + return E[u]; +} + +struct State { + int u; + double d, t; + State(int u, double d):u(u),d(d),t(est(u)) { } + bool operator < (const State &o) const { return d+t < o.d+o.t; } +}; + +inline bool checkPush(priority_queue &q, double d, int u, int v) { + double w = dist2(u, v); + if (w <= 2.25) { + double dd = d + sqrt(w); + if (dd < D[v]) { + State t(v, D[v]=dd); + if (t.d + t.t <= maxd) { + q.push(t); + return 1; + } + } + } + return 0; +} + + +bool dijkstra() { + for (int i=0; i q; + q.push(State(0, D[0]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d > D[s.u]) continue; + + for (int v=s.u-1; v>=0 && P[s.u].x-P[v].x<=1.5; --v) + if (checkPush(q, s.d, s.u, v) && v==n+1) + return 1; + + for (int v=s.u+1; v +/* +Problem: 11340 - Newspaper +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2315 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int T, M, K; + +char ch; +double freq[257], value; + +int main () { + fastio; + + cin >> T; + + while ( T-- ){ + cin >> K; + memset(freq, 0, sizeof(freq)); + + while ( K-- ){ + cin >> ch >> value; + freq[(int)ch] = value; + } + + int total_paid = 0; + + cin >> M; + for (int i = 0; i <= M; ++i){ + string line; + getline(cin, line); + + for (auto ch : line) + total_paid += freq[(int)ch]; + } + + cout << fixed << setprecision(2) << total_paid/100.0 << "$\n"; + } + + return 0; +} +/* +Sample input:- +----------------- +1 +7 +a 3 +W 10 +A 100 +, 10 +k 7 +. 3 +I 13 +7 +ACM International Collegiate Programming Contest (abbreviated +as ACM-ICPC or just ICPC) is an annual multi-tiered competition +among the universities of the world. The ICPC challenges students +to set ever higher standards of excellence for themselves +through competition that rewards team work, problem analysis, +and rapid software development. +From Wikipedia. + +Sample output:- +----------------- +3.74$ + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11340/11340-19.cpp b/uva_cpp_clean/11340/11340-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9af9f55c002caca557eabe1c3d7c668d063b763 --- /dev/null +++ b/uva_cpp_clean/11340/11340-19.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + unordered_map prices; + int N; + cin >> N; + while (N--) { + prices.clear(); + int K; + cin >> K; + while (K--) { + char c; + int price; + cin >> c >> price; + prices.insert(pair(c, price)); + } + int M, cost = 0; + cin >> M; + string s; + getline(cin, s); + while (M--) { + getline(cin, s); + for (int i = 0; i < s.length(); i++) { + if (prices.count(s[i])) cost += prices[s[i]]; + } + } + printf("%d.%02d$\n", cost/100, cost%100); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11340/11340-21.cpp b/uva_cpp_clean/11340/11340-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebc0dc94610f2c56aea4305a753c308f1f2d209b --- /dev/null +++ b/uva_cpp_clean/11340/11340-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11340 + Name: Newspaper + Problem: https://onlinejudge.org/external/113/11340.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +void flush() { + while(getchar()!='\n'); +} + +int main(){ + unsigned char ch; + int T, n, val; + int costs[256]; + cin>>T; + while(T--) { + scanf("%d", &n); flush(); + memset(costs, 0, sizeof(costs)); + for (int i=0; i> val; flush(); + costs[ch] = val; + } + + cin>>n; flush(); + long long int result = 0; + while (n>0) { + ch = getchar(); + result += costs[ch]; + if (ch == '\n') n--; + } + + printf("%.2f$\n", result/100.0); + } +} diff --git a/uva_cpp_clean/11341/11341-21.cpp b/uva_cpp_clean/11341/11341-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d9b3f6084dc44e96a6fe67e299ce4aec3f45fd5 --- /dev/null +++ b/uva_cpp_clean/11341/11341-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11341 + Name: Term Strategy + Problem: https://onlinejudge.org/external/113/11341.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m, S[101]; + cin >> T; + while (T--) { + cin >> n >> m; + + int DP[101] = {}; + for (int i=0; i> S[j]; + + for (int r=m; r>0 && DP[r]>=0; --r) { + int mx = -INF; + for (int j=r-i; j>0 && S[j]>4; --j) + mx = max(mx, DP[r-j] + S[j]); + DP[r] = mx; + } + } + + double result = round(100.0*DP[m] / n)/100.0; + if (result<5) + cout << "Peter, you shouldn't have played billiard that much.\n"; + else + cout << "Maximal possible average mark - " + << fixed << setprecision(2) << result << ".\n"; + } +} diff --git a/uva_cpp_clean/11341/11341-9.cpp b/uva_cpp_clean/11341/11341-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbfeb59128ce140e75d77627d0fdc2f6906fee91 --- /dev/null +++ b/uva_cpp_clean/11341/11341-9.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +const int INF = 1e7, MAX_M = 100 + 10, MAX_N = 10 + 10; + +int n, m, memo[MAX_M][MAX_N]; +vector >> arr; + +int dp (int sum, int id) { + if (sum < 0) return -INF; + if (id == n) return 0; + if (memo[sum][id] != INF) return memo[sum][id]; + int ret = -INF; + for (auto it: arr[id]) { + int val = dp(sum - it.first, id + 1); + if (val >= 0) ret = max(ret, it.second + val); + } + return memo[sum][id] = ret; +} + +void readCase () { + cin >> n >> m; + for (int i = 0; i < n; i++) { + arr.push_back({}); + int prev = -1, curr; + for (int j = 1; j <= m; j++) { + cin >> curr; + if (curr != prev and curr >= 5) arr[i].push_back({j, curr}); + prev = curr; + } + } +} + +void clearCase () { + fill(memo[0], memo[MAX_M], INF); + arr.clear(); +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + clearCase(); + readCase(); + int grade = dp(m, 0); + if (5 * n <= grade) printf("Maximal possible average mark - %.2lf.\n", 1.0 * grade / n + 0.001, 2); + else puts("Peter, you shouldn't have played billiard that much."); + } + return (0); +} diff --git a/uva_cpp_clean/11342/11342-18.cpp b/uva_cpp_clean/11342/11342-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..61327a5f689dba91cc115d59517b13f12c66e3d3 --- /dev/null +++ b/uva_cpp_clean/11342/11342-18.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + + +using namespace std; + +int main(){ + int squares[50010][4] = {0}, n, number; + memset(squares, 0, sizeof(squares)); //Para no usar 3 for-loop para inicializarlo + for(int i = 0; pow(i, 2) <= 50000; i++){ + for(int j = 0; pow(j, 2) <= 50000; j++){ + for(int k = 0; pow(k, 2) <= 50000; k++){ + int tentativeAns = pow(i, 2) + pow(j, 2) + pow(k, 2); + if(tentativeAns > 50000) break; + if(squares[tentativeAns][3] != 0) continue; + squares[tentativeAns][0] = i; + squares[tentativeAns][1] = j; + squares[tentativeAns][2] = k; + squares[tentativeAns][3] = 1; + } + } + } + scanf("%d", &n); + while(n--){ + scanf("%d", &number); + if(squares[number][3]) printf("%d %d %d\n", squares[number][0], squares[number][1], squares[number][2]); + else printf("-1\n"); + } + return 0; +} +/* + La estrategia a usar es "memorizar" las raices, es decir, generar todas las soluciones antes de empezar, eso nos + asegura un algoritmo optimizado en tiempo de ejecucion. Respecto a como se soluciono el problema, fui buscando con + fuerza bruta todas las combinaciones donde una triada me de un numero que no se pase del 50000 (limite estipulado), + en una matriz voy guardando en cada posicion cada numero de la triada hallada. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/11342/11342-21.cpp b/uva_cpp_clean/11342/11342-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91c4ed4dd8120858fc67994b6249dc6101104c98 --- /dev/null +++ b/uva_cpp_clean/11342/11342-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11342 + Name: Three-square + Problem: https://onlinejudge.org/external/113/11342.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MAXN 50005 +bool E[MAXN]; +int X[MAXN], Y[MAXN], Z[MAXN]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i=0, x; (x=i*i)> T; + while (T-- && cin>>n) { + if (!E[n]) cout << "-1\n"; + else cout << X[n] << ' ' << Y[n] << ' ' << Z[n] << '\n'; + } +} diff --git a/uva_cpp_clean/11342/11342-9.cpp b/uva_cpp_clean/11342/11342-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4eff77f5ef22e3ffaec6e5ca36abd039cfde40a4 --- /dev/null +++ b/uva_cpp_clean/11342/11342-9.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +vector > v[200000]; +int tc, K; + +int main(){ + for(int a = 0; a <= 225; a++) + for(int b = a; b <= 225; b++) + for(int c = b; c <= 225; c++) + if(v[a * a + b * b + c * c].size() == 0) + v[a * a + b * b + c * c].push_back(make_tuple(a, b, c)); + scanf("%d", &tc); + while(tc--){ + scanf("%d", &K); + if(v[K].size() == 0) puts("-1"); + else printf("%d %d %d\n", get<0>(v[K][0]), get<1>(v[K][0]), get<2>(v[K][0])); + } + return(0); +} diff --git a/uva_cpp_clean/11343/11343-21.cpp b/uva_cpp_clean/11343/11343-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5d57fff42ec4d25079d81a264a70a4d3583f739 --- /dev/null +++ b/uva_cpp_clean/11343/11343-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11343 + Name: Isolated Segments + Problem: https://onlinejudge.org/external/113/11343.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 1e-8 + +struct Point { + double x, y; + Point():x(0),y(0){} + Point(double x, double y):x(x),y(y){} + bool operator < (const Point &o) const { return x < o.x; } + bool operator == (const Point &o) const { return fabs(x-o.x) EPS) return 0; + + double a21 = angle(p3, p4, p1), a22 = angle(p3, p4, p2);; + if (a21*a22 > EPS) return 0; + + if (a11*a12 < -EPS && a21*a22 < -EPS) return 1; + return (fabs(a11)> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + Point Pu[143], Pv[143]; + while (T-- && cin>>n) { + bool hi[143] = {}; + for (int i=0; i> Pu[i] >> Pv[i]; + for (int j=0; j + +#define SIZE 1010 + +using namespace std; + +bool is_prime[SIZE]; +vector prime; +int tc, Factor[SIZE][SIZE], num, v[SIZE]; +char s[SIZE]; +long long ans; +double aux; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(int it1 = 2; it1 < SIZE; it1++) + if( is_prime[it1] ){ + for(int it2 = it1 * it1; it2 < SIZE; it2 += it2) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +void GetFactors(){ + for(int num = 1; num < SIZE; num++){ + int pib = num; + for(int it = 0; it < prime.size() and pib != 1; it++) + if( pib % prime[it] == 0 ){ + int ct = 0; + while( pib % prime[it] == 0 ) pib /= prime[it], ct++; + Factor[num][it] = ct; + } + } +} + +int main(){ + sieve(); + GetFactors(); + scanf("%d\n", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%s", s); + int k = 0; + for(int it = 0; s[it]; it++) if( s[it] == '!' ) k++; + num = 0; + for(int it = 0; s[it] != '!'; it++) num = num * 10 + ( s[it] - '0' ); + memset(v, 0, sizeof v); + for(int n = num; n > 1; n -= k) + for(int it = 0; it < SIZE; it++) v[it] += Factor[n][it]; + ans = aux = 1; + for(int it = 0; it < SIZE; it++) ans *= v[it] + 1, aux *= v[it] + 1; + if( aux > 1e18 ) printf("Case %d: Infinity\n", t); + else printf("Case %d: %lld\n", t, ans); + } + return(0); +} diff --git a/uva_cpp_clean/11348/11348-19.cpp b/uva_cpp_clean/11348/11348-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc15d03c9038ec6dba0315cd16bc19f4245968a3 --- /dev/null +++ b/uva_cpp_clean/11348/11348-19.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +using namespace std; + +int main() { + int K; + cin >> K; + int grid[52][52]; + unordered_map m; + unordered_set set; + for (int c = 1; c <= K; c++) { + m.clear(); + int N; + cin >> N; + for (int i = 0; i < N; i++) { + cin >> grid[i][0]; + set.clear(); + for (int j = 1; j <= grid[i][0]; j++) { + cin >> grid[i][j]; + if (!set.count(grid[i][j])) m[grid[i][j]]++, set.insert(grid[i][j]); + } + } + printf("Case %d:", c); + int total = 0; + for (int i = 0; i < N; i++) { + int acum = 0; + set.clear(); + for (int j = 1; j <= grid[i][0]; j++) + if (m[grid[i][j]] == 1 && !set.count(grid[i][j])) acum++, set.insert(grid[i][j]); + grid[i][0] = acum*100; + total += acum; + } + for (int i = 0; i < N; i++) printf(" %.6f%%", double(grid[i][0])/total); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11348/11348-21.cpp b/uva_cpp_clean/11348/11348-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0508984fb5c379be6d4eadd1c03c419146c0e48 --- /dev/null +++ b/uva_cpp_clean/11348/11348-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11348 + Name: Exhibition + Problem: https://onlinejudge.org/external/113/11348.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +int main(){ + int T, n, c, x, un[60], seen[10001]; + scanf("%d", &T); + for (int cse=1; cse<=T; ++cse) { + printf("Case %d:", cse); + memset(seen, 0, sizeof(seen)); + + int tn = 0; + scanf("%d", &n); + for (int i=0; i0 && seen[x]!=i+1) { + --un[seen[x] - 1]; --tn; + seen[x] = -1; + } + } + } + + if (!tn) putchar(' '); + else for (int i=0; i +#include + +using namespace std; + +int main(){ + int testCase, n, cases = 0; + string input; + bool isSymmetric; + cin >> testCase; + getline(cin, input); + while(cases < testCase){ + getline(cin, input); + n = stoi(input.substr(3, input.size())); + isSymmetric = true; + long long int matriz[n][n]; + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + cin >> matriz[i][j]; + if(matriz[i][j] < 0) isSymmetric = false; + } + } + if(isSymmetric){ + for(int i = 0; i < n & isSymmetric; i++){ + for(int j = 0; j < n; j++){ + if(matriz[i][j] != matriz[n-1-i][n-1-j]) isSymmetric = false; + } + } + } + if(isSymmetric) printf("Test #%d: Symmetric.\n", cases + 1); + else printf("Test #%d: Non-symmetric.\n", cases + 1); + cases++; + getline(cin, input); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11349/11349-19.cpp b/uva_cpp_clean/11349/11349-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..450bc75d1fedd0e2e90688710d46ea5a247bd80b --- /dev/null +++ b/uva_cpp_clean/11349/11349-19.cpp @@ -0,0 +1,69 @@ +// las12uvas contest PROBLEM C 2015 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector

vdd; +typedef vector vll; +typedef vector vvi; + +int main() { + int cases; + cin >>cases; + long long mat[301][301]; + for (int i = 0; i < cases; i++) { + bool sim = true; + int n; + char trash; + cin >> trash >> trash; + cin >> n; + for(int j = 0; j < n; j++){ + for (int l = 0; l < n; l++) + { + cin >> mat[j][l]; + if (mat[j][l]<0) sim = false; + } + } + for(int j = 0; j < n && sim; j++){ + for (int l = 0; l < n && sim; l++) + { + if(mat[j][l] != mat[n-1-j][n-1-l]) { + sim = false; + } + } + } + printf("Test #%d: ", i+1); + if (sim)printf("Symmetric.\n"); + else printf("Non-symmetric.\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11349/11349-20.cpp b/uva_cpp_clean/11349/11349-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..951aa2ab0dbc50aec16c8122e131d1fc835f84f8 --- /dev/null +++ b/uva_cpp_clean/11349/11349-20.cpp @@ -0,0 +1,49 @@ +#include + +long long matrix[102][102]; +char temp[3]; + +int main() +{ + int tc, nCase; + char dump; + + scanf("%d%c", &tc, &dump); + nCase = 0; + while( tc-- ) + { + int n; + scanf("%s %s %d", temp, temp, &n); + + bool flag = true; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11349 + Name: Symmetric Matrix + Problem: https://onlinejudge.org/external/113/11349.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline long long readInt() { + char ch; + int sgn=1; + long long r = 0; + while (!isdigit(ch=readchar()) && ch!='-'); + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +// ----------------------------------------------------------------- + +long long X[200][200]; +bool isSymmetric(int n) { + for (int i=0; i + +#define SIZE 310 + +using namespace std; + +int tc, n; +long v[SIZE][SIZE]; + +bool symmetric(){ + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) + if(v[i][j] < 0 || v[i][j] != v[n + 1 - i][n + 1 - j]) return false; + return true; +} + +int main(){ + scanf("%d", &tc); + for(int t = 1;t <= tc; t++){ + scanf("\n%*c %*c %d", &n); + for(int i = 1;i <= n; i++) + for(int j = 1; j <= n; j++) + scanf("%ld", &v[i][j]); + if(symmetric()) printf("Test #%d: Symmetric.\n", t); + else printf("Test #%d: Non-symmetric.\n", t); + } + return(0); +} diff --git a/uva_cpp_clean/11350/11350-21.cpp b/uva_cpp_clean/11350/11350-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9a592d2015b3858910e04deaa9559a0b252dd6d --- /dev/null +++ b/uva_cpp_clean/11350/11350-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11350 + Name: Stern-Brocot Tree + Problem: https://onlinejudge.org/external/113/11350.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char str[100]; + int T; + cin >> T; + while (T--) { + cin >> str; + + long long la=0, lb=1, ra=1, rb=0; + for (int i=0; str[i]; ++i) + if (str[i] == 'L') { + ra += la; + rb += lb; + } + else { + la += ra; + lb += rb; + } + cout << (la+ra) << '/' << (lb+rb) << endl; + } +} diff --git a/uva_cpp_clean/11352/11352-21.cpp b/uva_cpp_clean/11352/11352-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..881bb3c839382c1f6151b2e0b42fb3b002eeed44 --- /dev/null +++ b/uva_cpp_clean/11352/11352-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11352 + Name: Crazy King + Problem: https://onlinejudge.org/external/113/11352.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct State { + int i, j, d; + State(int i, int j, int d):i(i),j(j),d(d){} + bool operator == (const State &s) const { return i==s.i && j==s.j; } +}; + + +bool B[120][120]; +int dy[] = {0, 1,-1,1,-1, 2,-2,2,-2}, + dx[] = {0, 2,2,-2,-2, 1,1,-1,-1}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + + char ch; + int T; cin>>T; + for (int cse=1; cse<=T; ++cse) { + int n, m; cin >> n >> m; + + memset(B, -1, sizeof(B)); + for (int i=2; i q; + State dst(0,0,0); + for (int i=2; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 2000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (1500) +#define mod 1000000007 +////=====================================//// + +struct factcount +{ + ll value=0,cnt=0; + +}A[mx6]; + +bool prime[mx6+10]; +ll pd[mx6+10]; +int k=1; + +void seive() +{ + for(int i=4;i<=mx6;i+=2) + prime[i]=1; + + prime[0]=1; + prime[1]=1; + pd[0]=2; + + for(ll i=3;i<=mx6-6;i+=2) + { + if(!prime[i]) + { + pd[k++]=i; + ll mul = i<<1; + ll j=i; + for(j*=i;j<=mx6;j+=mul) + { + prime[j]=1; + } + } + } +// PN(k),PL(pd[k-1]); +} + +void fact(int n) +{ + int p=n; + A[p].value=n; + + int sq = sqrt(n),i=0; + int c=0; + while(n!=1) + { + if(pd[i]>sq || i==k-1) break; + + while(n%pd[i]==0) + { + n/=pd[i]; + c++; + } + i++; + } + if(n!=1) + { + c++; + } + + A[p].cnt=c; +} + +bool cmp(factcount a, factcount b) +{ + if(a.cnt==0 || b.cnt==0) return false; + if(a.cnt==b.cnt) + return a.value +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 50005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +struct Edge { + int u, v, w; + bool operator< (const Edge &p) const { + return w < p.w; + } +}; +vectorEd; +vectoradj[MAX], cost[MAX]; +int par[MAX], level[MAX]; +int table[MAX][17], mx_rd[MAX][17]; + +int find (int r) { + if (par[r] == r) return r ; + else return par[r] = find (par[r]); +} + +void MST (int n) { + sort (all (Ed) ); + + for (int i = 1; i <= n; i++) { + par[i] = i; + } + + int cnt = 0; + + for (int i = 0; i < (int) Ed.size(); i++) { + int u = Ed[i].u; + int v = Ed[i].v; + int w = Ed[i].w; + int uu = find (u); + int vv = find (v); + + if (uu != vv ) { + par[vv] = uu; + cnt++; + adj[u].pb (v); + adj[v].pb (u); + cost[u].pb (w); + cost[v].pb (w); + + if (cnt == n - 1) break; + } + } +} + + + +void dfs (int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + par[v] = u; + level[v] = level[u] + 1; + mx_rd[v][0] = cost[u][i]; + dfs (v, u); + } +} + +void build_lca (int n) { + for (int i = 1; i <= n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 1 ; i <= n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + mx_rd[i][j] = max (mx_rd[i][j - 1], mx_rd[x][j - 1]); + + } else { + table[i][j] = -1; + mx_rd[i][j] = mx_rd[i][j - 1]; + } + } + } +} + +int query (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} + +int get_max (int u, int uu) { + int mx = 0; + + for (int i = log2 (uu); i >= 0; i--) { + if ( (1 << i) <= uu) { + mx = max (mx, mx_rd[u][i]); + u = table[u][i]; + uu -= (1 << i); + } + + if (uu == 0) break; + } + + return mx; +} + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + int n, m; + int t = 1; + + while (cin >> n >> m) { + for (int i = 0; i < m ; i++) { + Edge get; + cin >> get.u >> get.v >> get.w; + Ed.pb (get); + } + + MST (n); + par[1] = -1; + level[1] = 0; + SET (table, -1); + mx_rd[1][0] = 0; + dfs (1, -1); + build_lca (n); + int q; + cin >> q; + + if (t != 1) nl; + + t++; + + while (q--) { + int u, v; + cin >> u >> v; + int r = query (u, v); + int uu = level[u] - level[r]; + int vv = level[v] - level[r]; + cout << max (get_max (u, uu), get_max (v, vv) ) << "\n"; + } + + Ed.clear(); + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + cost[i].clear(); + } + } + + return 0; +} diff --git a/uva_cpp_clean/11354/11354-21.cpp b/uva_cpp_clean/11354/11354-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f464199d721177ff443d1207f246b357f223d04b --- /dev/null +++ b/uva_cpp_clean/11354/11354-21.cpp @@ -0,0 +1,108 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11354 + Name: Bond + Problem: https://onlinejudge.org/external/113/11354.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int gid[55000]; +int find(int u) { return gid[u]<0 ? u : gid[u]=find(gid[u]); } +bool join(int u, int v) { + if ((u=find(u)) != (v=find(v))) { + if (gid[v] < gid[u]) + swap(u, v); + + gid[u] += gid[v]; + gid[v] = u; + return 1; + } + return 0; +} + +struct Edge { + int u, v, w; + bool operator < (const Edge &o) const { return w < o.w; } +} +E[100143]; + +#define ff first +#define ss second +typedef pair Pair; +vector adj[55000]; + + +int par[55000], parw[55000]; +void dfs(int u) { + int upar = par[u]; + for (Pair v: adj[u]) + if (v.ff != upar) { + parw[v.ff] = v.ss; + par[v.ff] = u; + dfs(v.ff); + } +} + +int seen[55000], mxx[55000], cse; +int LCA(int u, int v) { + int mx = mxx[u] = 0; + while (u) { + seen[u] = cse; + mx = mxx[par[u]] = max(mx, parw[u]); + u = par[u]; + } + + mx = 0; + while (v) { + if (seen[v] == cse) + return max(mx, mxx[v]); + + mx = max(mx, parw[v]); + v = par[v]; + } + return 0; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m; + bool frst = 1; + while (cin >> n >> m) { + if (frst) frst = 0; + else cout << '\n'; + + for (int i=0; i<=n; ++i) { + adj[i].clear(); + gid[i] = -1; + } + + for (int i=0; i> E[i].u >> E[i].v >> E[i].w; + sort(E, E+m); + + for (int i=0; i> m; + while (m--) { + int u, v; ++cse; + cin >> u >> v; + cout << LCA(u, v) << '\n'; + } + } +} diff --git a/uva_cpp_clean/11356/11356-20.cpp b/uva_cpp_clean/11356/11356-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25a9ba7a79e35328367dbd2f1907a21e335931f6 --- /dev/null +++ b/uva_cpp_clean/11356/11356-20.cpp @@ -0,0 +1,59 @@ +#include +#include + +const char month[14][12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; +int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + +int tc, nCase; +char dump, mmInAlpha[12]; +int yyyy, dd, K, mm; + +int monthInDigit(char* mmInAlpha) +{ + for(int j=0 ; j<12 ; j++) + if( !strcmp(mmInAlpha, month[j]) ) + return (j+1); +} + +bool isLeap(int year) +{ + if( (year%400 == 0) || ((year%4 == 0) && (year%100 != 0)) ) return true; + return false; +} + +int main() +{ + scanf("%d%c", &tc, &dump); + nCase = 1; + while( tc-- ) + { + scanf("%d-%[^-]-%d", &yyyy, mmInAlpha, &dd); + scanf("%d", &K); + + mm = monthInDigit(mmInAlpha); + + if( isLeap(yyyy) ) days[1] = 29; + else days[1] = 28; + + for(int i=1 ; i<=K ; i++) + { + dd++; + if( dd>days[mm-1] ) + { + dd = 1; + mm++; + if( mm>12 ) + { + mm = 1; + yyyy++; + if( isLeap(yyyy) ) days[1] = 29; + else days[1] = 28; + } + } + } + + printf("Case %d: %0004d-%s-%02d\n", nCase, yyyy, month[mm-1], dd); + ++nCase; + } + return 0; +} diff --git a/uva_cpp_clean/11357/11357-19.cpp b/uva_cpp_clean/11357/11357-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fa8e3c8f481f68b84cd0d66da4ea7f266a2da80 --- /dev/null +++ b/uva_cpp_clean/11357/11357-19.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +using namespace std; + +int var[30]; + +bool proc(string str) { + for (int i = 0; i < 30; i++) var[i] = -1; + for (int i = 0; i < str.length(); i++) { + if (str[i] == '&') continue; + if (str[i] == '~') { + i++; + if (var[str[i]-'a'] == -1) var[str[i]-'a'] = 0; + else if (var[str[i]-'a'] == 1) return false; + } else { + if (var[str[i]-'a'] == -1) var[str[i]-'a'] = 1; + else if (var[str[i]-'a'] == 0) return false; + } + } + return true; +} + +int main() { + int T; + string form; + cin >> T; + while (T--){ + cin >> form; + bool ok = false; + int last; + for (int i = 0; i < form.length() && !ok; i++) { + if (form[i] == '(') last = i; + else if (form[i] == ')') ok = ok || proc(form.substr(last+1, i-last-1)); + } + if (ok) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11360/11360-18.cpp b/uva_cpp_clean/11360/11360-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8ba3f2860c8f56a3d64ac4e7b3b25faef58ce606 --- /dev/null +++ b/uva_cpp_clean/11360/11360-18.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +using namespace std; + +int matriz[11][11]; + +void increase(int n){ + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + matriz[i][j] += 1; + if(matriz[i][j] == 10) matriz[i][j] = 0; + } + } +} + +void decrease(int n){ + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + matriz[i][j] -= 1; + if(matriz[i][j] == -1) matriz[i][j] = 9; + } + } +} + +void changeRow(int a, int b, int n){ + for(int i = 0; i < n; i++) swap(matriz[a][i], matriz[b][i]); +} + +void changeCol(int a, int b, int n){ + for(int i = 0; i < n; i++) swap(matriz[i][a], matriz[i][b]); +} + +void transpose(int n){ + for(int i = 0; i < n; i++){ + for(int j = i + 1; j < n; j++) swap(matriz[i][j], matriz[j][i]); + } +} + +int main(){ + int tc, n, op, cont = 0; + string opS; + cin >> tc; + while(cont < tc){ + cin >> n; + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++) scanf("%1d", &matriz[i][j]); //Para tomar un solo numero + } + cin >> op; + while(op-- > 0){ + cin >> opS; + if(opS == "inc") increase(n); + else if(opS == "dec") decrease(n); + else if(opS == "transpose") transpose(n); + else if(opS == "row"){ + int a, b; + cin >> a >> b; + a--; + b--; + changeRow(a, b, n); + }else if(opS == "col"){ + int a, b; + cin >> a >> b; + a--; + b--; + changeCol(a, b, n); + } + } + printf("Case #%d\n", cont + 1); + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++) printf("%d", matriz[i][j]); + printf("\n"); + } + printf("\n"); + cont++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11360/11360-21.cpp b/uva_cpp_clean/11360/11360-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df3656bfe5ad22d9117610a3d9f7d1e37f99a3c6 --- /dev/null +++ b/uva_cpp_clean/11360/11360-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11360 + Name: Have Fun with Matrices + Problem: https://onlinejudge.org/external/113/11360.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char ch, cm[40]; + int T, n, m, a, b, X[20][20]; + cin>>T; + for (int cse=1; cse<=T; ++cse) { + cin >> n; + for (int i=0; i> ch; + X[i][j] = ch-'0'; + } + + cin >> m; + int diff = 0; + for (int i=0; i> cm; + switch (cm[0]) { + case 'i': ++diff; break; + case 'd': --diff; break; + case 't': + for (int i=0; i> a >> b; + for (int j=0; j> a >> b; + for (int i=0; i + +using namespace std; + +class Matrix +{ +public: + Matrix(int n); + ~Matrix(); + + void Increment(); + void Decrement(); + void Transpose(); + void SwapRow(int a, int b); + void SwapCol(int a, int b); + + friend ostream &operator<<(ostream &output, Matrix &r); + friend istream &operator>>(istream &input, Matrix &m); + +private: + int n; + int **tab; +}; + +Matrix::Matrix(int n) +{ + this->n = n; + tab = new int *[n]; + + for (int i = 0; i < n; ++i) + tab[i] = new int[n]; +} + +Matrix::~Matrix() +{ + for (int i = 0; i < n; ++i) + delete[] tab[i]; + + delete[] tab; +} + +void Matrix::Increment() +{ + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + tab[i][j] = (tab[i][j] + 1) % 10; +} + +void Matrix::Decrement() +{ + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + tab[i][j] = (tab[i][j] - 1) % 10; + if (tab[i][j] < 0) + tab[i][j] += 10; + } +} + +void Matrix::Transpose() +{ + Matrix temp(n); + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + temp.tab[i][j] = tab[j][i]; + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + tab[i][j] = temp.tab[i][j]; +} + +void Matrix::SwapRow(int a, int b) +{ + int temp[n]; + + for (int i = 0; i < n; i++) + temp[i] = tab[a][i]; + + for (int i = 0; i < n; i++) + { + tab[a][i] = tab[b][i]; + tab[b][i] = temp[i]; + } +} + +void Matrix::SwapCol(int a, int b) +{ + int temp[n]; + + for (int i = 0; i < n; i++) + temp[i] = tab[i][a]; + + for (int i = 0; i < n; i++) + { + tab[i][a] = tab[i][b]; + tab[i][b] = temp[i]; + } +} + +ostream &operator<<(ostream &output, Matrix &m) +{ + for (int i = 0; i < m.n; i++) + { + for (int j = 0; j < m.n; j++) + output << m.tab[i][j]; + + cout << endl; + } + + return output; +} + +istream &operator>>(istream &input, Matrix &m) +{ + char c; + for (int i = 0; i < m.n; i++) + for (int j = 0; j < m.n; j++) + { + input >> c; + m.tab[i][j] = c - '0'; + } + + return input; +} + +int main() +{ + string command; + int tests, n, m, casenr = 0; + cin >> tests; + + while (tests--) + { + cin >> n; + Matrix tab(n); + + cin >> tab >> m; + + for (int i = 0; i < m; i++) + { + cin >> command; + + if (command == "inc") + tab.Increment(); + else if (command == "dec") + tab.Decrement(); + else if (command == "row" || command == "col") + { + int a, b; + cin >> a >> b; + + a--; + b--; + + if (command == "row") + tab.SwapRow(a, b); + else + tab.SwapCol(a, b); + } + else if (command == "transpose") + tab.Transpose(); + } + + cout << "Case #" << ++casenr << endl + << tab << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11360/11360-9.cpp b/uva_cpp_clean/11360/11360-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70e9ef11381371835d5652a49a6fe73bd4f7efae --- /dev/null +++ b/uva_cpp_clean/11360/11360-9.cpp @@ -0,0 +1,61 @@ +#include + +#define SIZE 60 + +using namespace std; + +int tc, n, m, v[SIZE][SIZE], x, y; +char s[SIZE], word[SIZE]; + +inline void xor_swap(int &a, int &b){ + a ^= b, b ^= a, a ^= b; +} + +void row(int r1, int r2){ + for(int j = 1; j <= n; j++) xor_swap(v[r1][j], v[r2][j]); +} + +void col(int c1,int c2){ + for(int i = 1; i <= n; i++) xor_swap(v[i][c1], v[i][c2]); +} + +void inc(int ad){ + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) + v[i][j] = (v[i][j] + ad + 10) % 10; +} + +void transpose(){ + for(int i = 1; i <= n; i++) + for(int j = i + 1; j <= n; j++) + xor_swap(v[i][j], v[j][i]); +} + +void printMatrix(int t){ + printf("Case #%d\n", t); + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) + printf( j != n ? "%d" : "%d\n", v[i][j]); + putchar('\n'); +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d", &n); + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) scanf("%1d", &v[i][j]); + scanf("%d\n", &m); + while(m--){ + cin.getline(s, SIZE); + sscanf(s,"%s %d %d", word, &x, &y); + if(strcmp(word, "row") == 0) row(x, y); + else if(strcmp(word, "col") == 0) col(x, y); + else if(strcmp(word, "inc") == 0) inc(1); + else if(strcmp(word, "dec") == 0) inc(-1); + else transpose(); + } + printMatrix(t); + } + return(0); +} diff --git a/uva_cpp_clean/11362/11362-14.cpp b/uva_cpp_clean/11362/11362-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66ff99a2dcf12d98d667b7f67c2cdb5606c761b8 --- /dev/null +++ b/uva_cpp_clean/11362/11362-14.cpp @@ -0,0 +1,91 @@ +/*************************************************** + * Problem name : 11362 Phone List.cpp + * Problem Link : https://uva.onlinejudge.org/external/113/11362.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-27 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 10003 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc, n; + //~ string str[MAX]; + //~ mapmp; + vectorV; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d", &n); + getchar(); + for (int i = 0; i < n; i++) { + string str; + cin >> str; + //~ mp[str] = 1; + V.push_back(str); + } + sort(V.begin(), V.end()); + //~ for(int i = 0 ; i +#include + +using namespace std; + +bool isPrefix(string a, string b) +{ + int lenA = a.size(), lenB = b.size(); + if( lenA>lenB ) return false; + for(int i=0 ; i> tc; + while( tc-- ) + { + int n; + cin >> n; + + string str[10005]; + for(int i=0 ; i> str[i]; + + sort(str, str+n); + + bool flag = true; + for(int i=0 ; i<(n-1) ; i++) + if( isPrefix(str[i], str[i+1]) ) + { + flag = false; + break; + } + + if( flag ) cout << "YES" << endl; + else cout << "NO" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11362/11362-21.cpp b/uva_cpp_clean/11362/11362-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5b5be8d92d63c37c96926b7c2a6dcbd2ffa76d4 --- /dev/null +++ b/uva_cpp_clean/11362/11362-21.cpp @@ -0,0 +1,97 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11362 + Name: Phone List + Problem: https://onlinejudge.org/external/113/11362.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} +inline int readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return EOF; + + int l = 0; + str[l++] = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + str[l++] = ch; + str[l] = 0; + return l; +} + +// ---------------------------------------------------------------------- + +int S[100143][10], F[100143], scnt; +bool insertWord(char num[]) { + int cur = 0; + bool result; + for (int i=0; num[i]; ++i) { + int d = num[i]-'0'; + result = !S[cur][d]; + if (result) { + S[cur][d] = ++scnt; + F[scnt] = false; + memset(S[scnt], 0, 10*sizeof(int)); + } + cur = S[cur][d]; + if (F[cur]) return false; + } + F[cur] = true; + return result; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + while (T--) { + int n = readUInt(); + memset(S[scnt = 0], 0, 10*sizeof(int)); + + char num[17]; + bool consistent = true; + for (int i=0; i +#include +#include + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + int n; + std::cin >> n; + + std::vector numbers(n); + std::unordered_map prefixes; + + for (int i = 0; i < n; i++) + { + std::string number; + std::cin >> number; + + numbers[i] = number; + + for (unsigned int i = 1; i <= number.length() - 1; i++) + { + prefixes[number.substr(0, i)]++; + } + } + bool consistent = true; + + for (int i = 0; i < n; i++) + { + if (prefixes[numbers[i]] != 0) + { + consistent = false; + break; + } + } + + if (consistent) + { + std::cout << "YES" << std::endl; + } + else + { + std::cout << "NO" << std::endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11362/11362-5.cpp b/uva_cpp_clean/11362/11362-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5105cf2cdc26218b818283b86c2e0bc14167c73e --- /dev/null +++ b/uva_cpp_clean/11362/11362-5.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +using namespace std; +bool arry(string a,string b) +{ + if(b.size()>s[i]; + + sort(s,s+n); + + bool r=true; + for(i=0;i + +using namespace std; + +int main () { + int tc; + cin >> tc; + while (tc--) { + int n; + vector s; + cin >> n; + s.resize(n); + for (int i = 0; i < n; i++) cin >> s[i]; + sort(begin(s), end(s)); + bool ok = true; + for (int i = 1; i < n and ok; i++) + if (s[i - 1] == s[i].substr(0, s[i - 1].size())) ok = false; + puts(ok ? "YES" : "NO"); + } + return (0); +} diff --git a/uva_cpp_clean/11364/11364-19.cpp b/uva_cpp_clean/11364/11364-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06ede227af5347496dbb714aa472e7b68714056f --- /dev/null +++ b/uva_cpp_clean/11364/11364-19.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +using namespace std; + +/*int N; THAT'S THE WAY YOU SHOULDN'T SOLVE THIS PROBLEM. +int stores[22]; +bool visited[22]; +int mini; + +void proc(int ini, int acum, int last, bool first) { + if (acum > mini) return; + int end = true; + for (int i = 0; i < N; i++) { + if (first) ini = i, last = stores[i]; + if (!visited[i]) { + visited[i] = true; + proc(ini, acum + (int)fabs(stores[i]-last), stores[i], false); + visited[i] = false; + end = false; + } + } + if (end) mini = min(acum + (int)fabs(stores[ini]-last), mini); +}*/ + +int main() { + int t, N; + cin >> t; + while (t--) { + cin >> N; + int mini = INT_MAX, maxi = -1, val; + for (int i = 0; i < N; i++) { + cin >> val; + mini = min(mini, val), maxi = max(maxi, val); + } + printf("%d\n", 2*(maxi-mini)); + } + return 0; +} diff --git a/uva_cpp_clean/11364/11364-21.cpp b/uva_cpp_clean/11364/11364-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1063678da3ee742eaddd4f0b434fc80821a196b8 --- /dev/null +++ b/uva_cpp_clean/11364/11364-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11364 + Name: Parking + Problem: https://onlinejudge.org/external/113/11364.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int t; + cin>>t; + while(t--) { + int n, x, min=1000, max=-1000; + cin>>n; + while (n--) { + cin>>x; + if (xmax) max = x; + } + + cout << 2*(max-min) < +#include +using namespace std; +int main() +{ + int a[109],t,n,i,s; + scanf("%d",&t); + while(t--) + { + s=0; + scanf("%d",&n); + for(i=0;i + +void burbuja(int v[],int n){ + int i, j, temp; + for(i = 0;i < n; i++) + for(j = n - 1; j > i; j--) + if(v[i] > v[j]){ + temp = v[i]; + v[i] = v[j]; + v[j] = temp; + } +} + +int main(){ + int N, n, a[100], i; + scanf("%d", &N); + while(N--){ + scanf("%d", &n); + for(i = 0;i < n; i++) scanf("%d", &a[i]); + burbuja(a, n); + printf("%d\n",2*(a[n-1]-a[0])); + } + return(0); +} diff --git a/uva_cpp_clean/11364/main.cpp b/uva_cpp_clean/11364/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..553391e0bcfd5a2961bc971e9dfab8ad739cf0bb --- /dev/null +++ b/uva_cpp_clean/11364/main.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main(){ +int ncasos, ntiendas, t; +cin >> ncasos; +while (ncasos > 0) { + cin >> ntiendas; + int tmen = 100, tma = 0; + while (ntiendas > 0){ + cin >> t; + if (t < tmen) tmen = t; + if (t > tma) tma = t; + ntiendas --; + } + cout << 2*(tma-tmen) << endl; + ncasos--; +} +return 0; +} diff --git a/uva_cpp_clean/11367/11367-19.cpp b/uva_cpp_clean/11367/11367-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..692f32daf11229d5e7783592b7ebf0a552e92363 --- /dev/null +++ b/uva_cpp_clean/11367/11367-19.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef pair ii; +typedef pair iii; +typedef vector vii; + +vi prices; +int costs[1000][102]; +vector AdjList; +int c, s, e, N, M; + +int dijkstra() { + for (int i = 0; i < N; i++) + for (int j = 0; j < 102; j++) + costs[i][j] = 100000000; + costs[s][0] = 0; + priority_queue pq; + pq.push(iii(0, ii(0, s))); + while (!pq.empty()) { + iii p = pq.top(); pq.pop(); + int cost = -p.first, u = p.second.second, g = p.second.first; + if (cost > costs[u][g]) continue; + if (u == e) return cost; + for (int j = 0; j < AdjList[u].size(); j++) { + ii v = AdjList[u][j]; + if (g - v.second >= 0 && (cost < costs[v.first][g - v.second])) { + costs[v.first][g - v.second] = cost; + pq.push(iii(-cost, ii(g - v.second, v.first))); + } + } + if (g < c && costs[u][g+1] > cost+prices[u]) { + costs[u][g+1] = cost+prices[u]; + pq.push(iii(-(cost+prices[u]), ii(g+1, u))); + } + } + return -1; +} + +int main() { + while (cin >> N >> M) { + prices.assign(N, 0); + AdjList.assign(N, vii()); + for (int i = 0; i < N; i++) cin >> prices[i]; + for (int i = 0; i < M; i++) { + int u, v, d; + cin >> u >> v >> d; + AdjList[u].push_back(ii(v, d)); + AdjList[v].push_back(ii(u, d)); + } + int q; + cin >> q; + for (int i = 0; i < q; i++) { + cin >> c >> s >> e; + int ret = dijkstra(); + if (ret != -1) printf("%d\n", ret); + else printf("impossible\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/11367/11367-21.cpp b/uva_cpp_clean/11367/11367-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98753d801c5ec533951c056df8acbbca41248e20 --- /dev/null +++ b/uva_cpp_clean/11367/11367-21.cpp @@ -0,0 +1,169 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11367 + Name: Full Tank? + Problem: https://onlinejudge.org/external/113/11367.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------------ + +template +class IndexQueue { + int mind, maxd; + vector V[300000]; + void _update() { + while (mind<=maxd && V[mind].empty()) ++mind; + if (mind>maxd) clear(); + } + + public: + IndexQueue() { mind=INF; maxd=-1; } + bool empty() { _update(); return mind>maxd; } + void clear() { + for (int i=mind; i<=maxd; ++i) + V[i].clear(); + mind=INF; + maxd=-1; + } + + void push(int key, T data) { + V[key].push_back(data); + if (key < mind) mind = key; + if (key > maxd) maxd = key; + } + + T pop() { + _update(); + T r = V[mind].back(); + V[mind].pop_back(); + return r; + } +}; + +// ------------------------------------------------------------------------ + +struct State { + int u, f, d; + State(int u, int f, int d):u(u),f(f),d(d){} +}; + +vector adj[1043]; +int cse, S[1043][1043], + W[1043][1043], + D[1043][100], + E[1043][100], + P[1043]; + +IndexQueue q; +int dijkstra(int n, int mxc, int src, int dst) { + q.clear(); + q.push(0, State(src, 0, D[src][0]=0)); + while (!q.empty()) { + State s = q.pop(); + if (s.u == dst) return s.d; + if (D[s.u][s.f] != s.d) continue; + + if (s.f < mxc) { + int d = s.d + P[s.u]; + if (E[s.u][s.f+1]!=cse || d < D[s.u][s.f+1]) { + E[s.u][s.f+1] =cse; + q.push(d, State(s.u, s.f+1, D[s.u][s.f+1]=d)); + } + } + + for (int v: adj[s.u]) { + int w = W[s.u][v]; + if (s.f < w) break; // adj is sorted, so we can break out on first far city + int r = s.f - w; + if (E[v][r]!=cse || s.d < D[v][r]) { + E[v][r] =cse; + q.push(s.d, State(v, r, D[v][r]=s.d)); + } + } + } + return -1; +} + +int uuu; +bool comp(int v1, int v2) { + return W[uuu][v1] < W[uuu][v2]; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m; + for (cse=1; (n=readUInt())!=EOF && (m=readUInt())!=EOF; ++cse) { + for (int i=0; i shortest path > single-source > weighted graph + difficulty: hard + date: 25/Feb/2020 + hint: find the shortest path on state-space graph, where each vertex represent a city and a level of car fuel + by: @brpapa +*/ +#include +#include +#include +#include +using namespace std; +const int INF = 1 << 30; + +struct Tadj { + int id, w; + Tadj(int id, int w) : id(id), w(w) {} +}; +vector> adjList; +int V; + +vector fp; // fuel prices + +int dijk(int s, int e, int C) { + // source vertex s, goal vertex e, car fuel capacity C + + // sd[v][c] = menor distância do vértice (s, 0) até (v, c) + vector> sd(V, vector(C+1, INF)); + sd[s][0] = 0; + + priority_queue> pq; // distance from (s,0), id, car fuel level c + pq.push(make_tuple(-sd[s][0], s, 0)); + + while (!pq.empty()) { + int u_d = -get<0>(pq.top()), u_id = get<1>(pq.top()), u_c = get<2>(pq.top()); + pq.pop(); + + if (u_d > sd[u_id][u_c]) continue; + + // para cada aresta (u_id, u_c) --v.w--> v.id + for (Tadj v : adjList[u_id]) { + if (u_c-v.w >= 0) { + // (u_id, u_c) --0--> (v.id, u_c-v.w) + int nd = sd[u_id][u_c] + 0; + int &od = sd[v.id][u_c-v.w]; + + if (nd < od) { + od = nd; + pq.push(make_tuple(-nd, v.id, u_c-v.w)); + } + } + } + + if (u_c+1 <= C) { + // (u_id, u_c) --(fp[u])--> (u_id, u_c+1) + int nd = sd[u_id][u_c] + fp[u_id]; + int &od = sd[u_id][u_c+1]; + + if (nd < od) { + od = nd; + pq.push(make_tuple(-nd, u_id, u_c+1)); + } + } + } + return sd[e][0]; +} + +int main() { + int E; cin >> V >> E; + adjList.resize(V); + fp.resize(V); for (int &p : fp) cin >> p; + + while (E--) { + int u, v, w; cin >> u >> v >> w; + adjList[u].push_back(Tadj(v, w)); + adjList[v].push_back(Tadj(u, w)); + } + + int Q; cin >> Q; + while (Q--) { + int C, s, e; cin >> C >> s >> e; + + int ans = dijk(s, e, C); + if (ans == INF) cout << "impossible" << endl; + else cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11368/11368-21.cpp b/uva_cpp_clean/11368/11368-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15db824d6435ddcaa2125db882efb15510658664 --- /dev/null +++ b/uva_cpp_clean/11368/11368-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11368 + Name: Nested Dolls + Problem: https://onlinejudge.org/external/113/11368.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + return r; +} + +// ------------------------------------------------------------------------------ + +struct Point { + int x, y; + bool operator < (const Point &o) const { return x!=o.x ? xo.y; } +} P[20086]; + +int M[20086]; +int LIS(int n) { + int L = 0, mx=0; + for (int i=0; i>1; + if (M[mid] >= y) { + lo = mid + 1; + if (mid==L || M[lo] + +using namespace std; + +int tc, n, lis[20010], LIS, pos, ans; +vector < pair > u(20010); +vector v, w; + +bool cmp(const pair p1, const pair p2){ + return (p1.second > p2.second || (p1.second == p2.second && p1.first < p2.first) ); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + v.clear(), ans = LIS = 0; + scanf("%d", &n); + for(int i = 0; i < n; i++) scanf("%d %d", &u[i].first, &u[i].second); + sort(u.begin(), u.begin() + n, cmp); + for(int i = 0; i < n; i++) v.push_back(u[i].first); + for(int i = 0; i < n; i++){ + pos = lower_bound(lis, lis + LIS, v[i] + 1) - lis; + lis[pos] = v[i]; + if(pos + 1 > LIS) LIS = pos + 1, ans++; + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11369/11369-14.cpp b/uva_cpp_clean/11369/11369-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13ab6be7c5f99a7182f152436cc70867b240325b --- /dev/null +++ b/uva_cpp_clean/11369/11369-14.cpp @@ -0,0 +1,76 @@ +/*************************************************** + * Problem name : 11369 Shopaholic.cpp + * Problem Link : https://uva.onlinejudge.org/external/113/11369.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-27 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 20003 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + int tc, n; + int ar[MAX]; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d", &n); + for (int i = 0; i < n; i++) { + scanf("%d", &ar[i]); + } + sort(ar, ar + n); + int sum = 0; + for (int i = n - 3; i >= 0; i -= 3) { + sum += ar[i]; + } + printf("%d\n", sum); + } + return 0; +} + diff --git a/uva_cpp_clean/11369/11369-21.cpp b/uva_cpp_clean/11369/11369-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d761a0d1b260e270a2167e01ecdcb826c21372b4 --- /dev/null +++ b/uva_cpp_clean/11369/11369-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11369 + Name: Shopaholic + Problem: https://onlinejudge.org/external/113/11369.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int X[20002]; +int main(){ + int T, n; + cin>>T; + while (T--) { + cin>>n; + for (int i=0; i>X[i]; + sort(X, X+n); + + int sum = 0; + for (int i=n%3; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +long long big(int a,int b ) +{ +return a>b; +} +int main() +{ + ll a[50000],t,n; + sc("%lld",&t); + while(t--) + { + sc("%lld",&n); + for(int i=0;i +#include +#include < \ No newline at end of file diff --git a/uva_cpp_clean/11371/11371-19.cpp b/uva_cpp_clean/11371/11371-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22fd36fb71a5724dc3c0321c9aabc00052cf278b --- /dev/null +++ b/uva_cpp_clean/11371/11371-19.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +int main() { + int n; + int digits[10]; + while (cin >> n) { + for (int i = 0; i < 10; i++) { + digits[i] = 0; + } + while (n) { + digits[n%10]++; + n /= 10; + } + long long a = 0; + for (int i = 9; i >= 0; i--) { + for (int j = 0; j < digits[i]; j++) { + a *= 10; + a += i; + } + } + long long b = 0; + for (int i = 1; i < 10; i++) { + if (digits[i]) { + b = i, digits[i]--; + break; + } + } + for (int i = 0; i < 10; i++) { + for (int j = 0; j < digits[i]; j++) { + b *= 10; + b += i; + } + } + printf("%lld - %lld = %lld = 9 * %lld\n", a, b, a - b, (a - b)/9); + } + return 0; +} diff --git a/uva_cpp_clean/11371/11371-20.cpp b/uva_cpp_clean/11371/11371-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80a84aeba2e2ccb071014cb17001ac4ba4cc7cff --- /dev/null +++ b/uva_cpp_clean/11371/11371-20.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +using namespace std; + +bool descend(char a, char b) +{ + return (a>b); +} + +int main() +{ + char a[500]; + + while( cin >> a ) + { + int len = strlen(a); + + sort(a, a+len, descend); + + char b[500]; + strcpy(b, a); + + sort(a, a+len); + + int i; + for(i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11371 + Name: Number Theory for Newbies + Problem: https://onlinejudge.org/external/113/11371.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +long long strtoi(const string &s) { + long long r = 0; + for (char c: s) + r = r*10 + c-'0'; + return r; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string str; + while (cin >> str) { + sort(str.rbegin(), str.rend()); + long long mx = strtoi(str); + + sort(str.begin(), str.end()); + int i; for (i=0; str[i]=='0'; ++i); + swap(str[i], str[0]); + long long mn = strtoi(str); + + cout << mx << " - " << mn << " = " + << (mx - mn) << " = 9 * " + << (mx-mn)/9 << endl; + } +} diff --git a/uva_cpp_clean/11371/11371-5.cpp b/uva_cpp_clean/11371/11371-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9df9f860d56ee2c249016073fa1b492db48b963e --- /dev/null +++ b/uva_cpp_clean/11371/11371-5.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +int main() +{ + string n,a; +long long s,b,c,d,l; +while(cin>>n) +{ + sort(n.begin(),n.end()); + a=n; + if(a[0]=='0') + { + for(int i=1;i +using namespace std; +using VI = vector; +using VVI = vector; +using VD = vector; +using VVD = vector; +using VS = vector; +using VVS = vector; +using VB = vector; +using VVB = vector; +using PII = pair; +using PDD = pair; +using PDI = pair; +constexpr double pi = 3.1415926535897932385; +constexpr double EPS = 1e-12; + +struct Edge +{ + int from, to, weight; + Edge() {}; + Edge(int t, int w) : to(t), weight(w) {}; +}; + +void dijkstra(vector>& adj, int s, vector& dist, vector& p) +{ + dist = vector(adj.size(), INFINITY); // use INFINITY in double if dist might be added + dist[s] = 0; + p = vector(adj.size()); + priority_queue, greater> q; // weight, vertex + q.emplace(0, s); + while (!q.empty()) + { + PII cur = q.top(); + q.pop(); + if (dist[cur.second] != cur.first) + continue; + for (Edge& edge : adj[cur.second]) + if (cur.first + edge.weight < dist[edge.to]) + { + dist[edge.to] = cur.first + edge.weight; + p[edge.to] = cur.second; + q.emplace(dist[edge.to], edge.to); + } + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + bool first = true; + int N, S, E; + while (cin >> N >> S >> E) + { + if (first) + first = false; + else + cout << '\n'; + + vector> adj(N + 1); + int M; + cin >> M; + for (int i = 0; i < M; ++i) + { + int X, Y, Z; + cin >> X >> Y >> Z; + adj[X].emplace_back(Y, Z); + adj[Y].emplace_back(X, Z); + } + int K; + cin >> K; + vector commercial(K); + for (Edge& edge : commercial) + cin >> edge.from >> edge.to >> edge.weight; + + vector dist_from_s; + vector p_from_s; + dijkstra(adj, S, dist_from_s, p_from_s); + vector dist_from_e; + vector p_from_e; + dijkstra(adj, E, dist_from_e, p_from_e); + + double min_dist = dist_from_s[E]; + Edge min_edge(-1, -1); + for (Edge& edge : commercial) + { + double cur_dist = dist_from_s[edge.from] + edge.weight + dist_from_e[edge.to]; + if (cur_dist < min_dist) + { + min_dist = cur_dist; + min_edge.from = edge.from; + min_edge.to = edge.to; + } + swap(edge.from, edge.to); + cur_dist = dist_from_s[edge.from] + edge.weight + dist_from_e[edge.to]; + if (cur_dist < min_dist) + { + min_dist = cur_dist; + min_edge.from = edge.from; + min_edge.to = edge.to; + } + } + + if (min_dist == dist_from_s[E]) + { + int cur = S; + while (cur != E) + { + cout << cur << ' '; + cur = p_from_e[cur]; + } + cout << E << '\n' + << "Ticket Not Used\n" + << dist_from_s[E] << '\n'; + } + else + { + deque path {min_edge.from}; + while (path.front() != S) + path.push_front(p_from_s[path.front()]); + for (int i : path) + cout << i << ' '; + int cur = min_edge.to; + while (cur != E) + { + cout << cur << ' '; + cur = p_from_e[cur]; + } + cout << E << '\n' + << min_edge.from << '\n' + << min_dist << '\n'; + } + } + return 0; +} diff --git a/uva_cpp_clean/11377/11377-21.cpp b/uva_cpp_clean/11377/11377-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e3797aa544c604d0471aafe2ae5d4f0a9998039 --- /dev/null +++ b/uva_cpp_clean/11377/11377-21.cpp @@ -0,0 +1,148 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11377 + Name: Airport Setup + Problem: https://onlinejudge.org/external/113/11377.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// -------------------------------------------------------------- + +template +class IndexQueue { + int mind, maxd; + vector V[300000]; + void _update() { + while (mind<=maxd && V[mind].empty()) ++mind; + if (mind>maxd) clear(); + } + + public: + IndexQueue() { mind=INF; maxd=-1; } + bool empty() { _update(); return mind>maxd; } + void clear() { + for (int i=mind; i<=maxd; ++i) + V[i].clear(); + mind=INF; + maxd=-1; + } + + void push(int key, T data) { + V[key].push_back(data); + if (key < mind) mind = key; + if (key > maxd) maxd = key; + } + + int top() { return mind; } + T pop() { + _update(); + T r = V[mind].back(); + V[mind].pop_back(); + return r; + } +}; + +// --------------------------------------------------------------------- + +vector adj[2043]; +int D[2043], A[2043], S[2043], SC=1; +IndexQueue q; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + for (int cse=1; cse<=T; ++cse) { + cout << "Case " << cse << ":\n"; + int n = readUInt(), + m = readUInt(), + k = readUInt(); + + for (int i=0; i +using namespace std; + +int proc(int N) { + if (N == 1) return 1; + return 1 + proc(N/2); +} + +int main() { + int N; + while (cin >> N) { + printf("%d\n", proc(N)); + } + return 0; +} diff --git a/uva_cpp_clean/11384/11384-5.cpp b/uva_cpp_clean/11384/11384-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bacc21896d88ee38af45469ed447b829b38a3427 --- /dev/null +++ b/uva_cpp_clean/11384/11384-5.cpp @@ -0,0 +1,16 @@ +#include +int main() +{ +long long int n,i; +while(scanf("%lld",&n)==1) +{ +i=0; +while(n>=1) +{ +n/=2; +i++; +} +printf("%lld\n",i); +} +return 0; +} diff --git a/uva_cpp_clean/11385/11385-19.cpp b/uva_cpp_clean/11385/11385-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67ba411fe6d9e6ea57db8ebc79470b7e583fc4b1 --- /dev/null +++ b/uva_cpp_clean/11385/11385-19.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + unordered_map fib; + long long fib0 = 0, fib1 = 1; + int pos = 1; + for (int i = 0; i < 91; i++, pos++) { + long long aux = fib1 + fib0; + fib.insert(pair(aux, pos)); + fib0 = fib1; + fib1 = aux; + } + int T; + queue seq; + cin >> T; + while (T--) { + int N; + long long max = 0; + cin >> N; + for (int i = 0; i < N; i++) { + long long f; + cin >> f; + seq.push(f); + if (f > max) max = f; + } + string cypher; + getline(cin, cypher); + getline(cin, cypher); + string msg(fib[max], ' '); + int i = 0; + while (!seq.empty()) { + for (; i < cypher.length() && !isupper(cypher[i]); i++); + msg[fib[seq.front()]-1] = cypher[i]; + seq.pop(); + i++; + } + cout << msg << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11385/main.cpp b/uva_cpp_clean/11385/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87bda9bf83927d158440e308158759f17b489dde --- /dev/null +++ b/uva_cpp_clean/11385/main.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +using namespace std; + +long fibo[] = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073}; +vector fibonacci(fibo, fibo + sizeof(fibo)/ sizeof(long)); + +long fibo_position(long number){ + for(long i = 0; i < fibonacci.size() ; i++){ + if(number == fibonacci[i]) return i; + } + return -1; +} + +string alphabetics(string tex){ + string res = ""; + for(int i = 0; i < tex.size(); i++){ + if((int)tex[i] >= (int)'A' && (int)tex[i] <= (int)'Z') res += tex[i]; + } + return res; +} + +long sequence_position(long num, vector sequence){ + for(long i = 0; i < sequence.size(); i++){ + if(sequence[i] == num) return i; + } + return -1; +} + +int main(){ + + cout << fibo[1] << endl; + long maxim, decimal, n, cases; + string text, data, res = ""; + vector sequence; + + cin >> cases; + for(;cases>0; cases--){ + maxim = 0; + res = ""; + sequence.clear(); + cin >> n; + cin.ignore(); + getline(cin, data); + istringstream iss (data); + getline(cin, text); + text = alphabetics(text); + // cout << text << endl + for(;n>0; n--){ + long i; + iss >> i; + maxim = max(maxim, i); + sequence.push_back(i); + } + + maxim = fibo_position(maxim); + for(long i = 0; i <= maxim; i++){ + decimal = sequence_position(fibonacci[i], sequence); + if(decimal == -1) res+= " "; + else{ + res += text[decimal]; + } + } + cout << res << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/11387/11387-18.cpp b/uva_cpp_clean/11387/11387-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d36f497b68523ff10db2d9628cfa42e4bf2fabc5 --- /dev/null +++ b/uva_cpp_clean/11387/11387-18.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main(){ + int n; + while(scanf("%d", &n) && n!=0){ + if(n%2==0 && 3<=n-1){ + printf("%d\n", 3*n/2); + for(int i = 1; i < n; i++) printf("%d %d\n", i, i+1); + printf("%d %d\n", n, 1); + for(int i = 1; i <= n/2; i++) printf("%d %d\n", i, n/2+i); + }else printf("Impossible\n"); + } + return 0; +} +/* + Primero que nada, un grafo r-regular existe si y solo si 0<=r<=n-1 donde r y n no deben los dos impar, + recordemos que un grafo existe si y solo si hay un numero par de vertices de grado impar o si estos dos numeros + son par. Para generar el grafo encontre un patron que era que si primero unos los i-esimos nodos con los i+1-esimos, + y al final el n-esimo con el primer nodo, me gastaria n aristas y todos los nodos tendrian grad(G) = 2, entonces, + uno la primera mitad de los nodos con la segunda mitad. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/11388/11388-19.cpp b/uva_cpp_clean/11388/11388-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b6fa0bcd21de9715b660080455602ec65c3e39c --- /dev/null +++ b/uva_cpp_clean/11388/11388-19.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +typedef long long ll; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +int main() { + int T; + cin >> T; + while (T--) { + ll G, L, P; + cin >> G >> L; + P = G * L; + ll a = G, b = L, i = 1; + while (a <= b) { + ll val = gcd(a, b); + if (val == G && (i * b) == L) break; + i++; + a = i * G; + b = P / a; + } + if (a > b) printf("-1\n"); + else printf("%lld %lld\n", a, b); + } + return 0; +} diff --git a/uva_cpp_clean/11388/11388-21.cpp b/uva_cpp_clean/11388/11388-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d45bd46c65bd2882add3a83aa3088a7bbf9bd170 --- /dev/null +++ b/uva_cpp_clean/11388/11388-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11388 + Name: GCD LCM + Problem: https://onlinejudge.org/external/113/11388.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, g, l; + cin>>T; + while(T--) { + cin>>g>>l; + if (l%g) cout << "-1\n"; + else cout << g << " " << l << endl; + } +} diff --git a/uva_cpp_clean/11388/11388-5.cpp b/uva_cpp_clean/11388/11388-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..baf6569ad317e12b4d88120fb03d422349fa1ced --- /dev/null +++ b/uva_cpp_clean/11388/11388-5.cpp @@ -0,0 +1,17 @@ +#include +int main() +{ +int n,m,t; +scanf("%d",&t); +while(t--) +{ + scanf("%d%d",&m,&n); + if(n%m==0) + printf("%d %d\n",m,n); + else + printf("-1\n"); +} +return 0; +} + + diff --git a/uva_cpp_clean/11388/11388-9.cpp b/uva_cpp_clean/11388/11388-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09ef9230987da46867649a132af66f31df698bc2 --- /dev/null +++ b/uva_cpp_clean/11388/11388-9.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int tc, G, L; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &G, &L); + if( L % G != 0 ) puts("-1"); + else printf("%d %d\n", G, L); + } + return(0); +} diff --git a/uva_cpp_clean/11389/11389-13.cpp b/uva_cpp_clean/11389/11389-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cd258417638612e7d5d92ace95745a65eb8b563 --- /dev/null +++ b/uva_cpp_clean/11389/11389-13.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +using namespace std; + +int n, d, r; + +int main() +{ + while (cin >> n >> d >> r, n != 0 || d != 0 || r != 0) + { + int sum = 0; + + int* morning = new int[n]; + int* evening = new int[n]; + + for (int i = 0; i < n; i++) cin >> morning[i]; + for (int i = 0; i < n; i++) cin >> evening[i]; + + sort(morning, morning + n); + sort(evening, evening + n); + + for (int i = 0; i < n; i++) sum += (morning[i] + evening[n - i - 1] > d) ? ((morning[i] + evening[n - i - 1] - d) * r) : 0; + + cout << sum << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11389/11389-19.cpp b/uva_cpp_clean/11389/11389-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b807aef6a222f19c20ca926a44b25e8ecbad7bf --- /dev/null +++ b/uva_cpp_clean/11389/11389-19.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + +int main() { + int n, d, r; + priority_queue mq, aq; + while (scanf("%d %d %d", &n, &d, &r) && !(n == 0 && d == 0 && r == 0)) { + for (int i = 0; i < n; i++) { + int m; + scanf("%d", &m); + mq.push(-m); + } + for (int i = 0; i < n; i++) { + int m; + scanf("%d", &m); + aq.push(m); + } + int sum = 0; + for (int i = 0; i < n; i++) { + int m = aq.top() - mq.top() - d; + if(m > 0) sum += m; + aq.pop(); mq.pop(); + } + printf("%d\n", sum*r); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11389/11389-21.cpp b/uva_cpp_clean/11389/11389-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c078a357dacd625cadf74661b9e450cf1f72fe71 --- /dev/null +++ b/uva_cpp_clean/11389/11389-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11389 + Name: The Bus Driver Problem + Problem: https://onlinejudge.org/external/113/11389.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int n, d, r, A[102], B[102]; bool used[102]; + while (cin>>n>>d>>r && (n||d||r)) { + for (int i=0; i>A[i]; + + for (int i=0; i>B[i]; + + sort(A, A+n, greater()); + sort(B, B+n); + + int sum=0; + memset(used, 0, n); + for (int i=0; i 0) ind--; + if (used[ind]) for(ind=0; used[ind]; ind++); + used[ind] = true; + + if (A[i] + B[ind] > d) + sum += A[i] + B[ind] - d; + } + + cout << sum*r << endl; + } +} diff --git a/uva_cpp_clean/11389/11389-5.cpp b/uva_cpp_clean/11389/11389-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a4976deb9941eb4cb76368a5c6c3863b32bc035d --- /dev/null +++ b/uva_cpp_clean/11389/11389-5.cpp @@ -0,0 +1,187 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +bool cmp(int a,int b) +{ + return a>b; +} + +int main() +{ + int n,d,r; + while(scanf("%d%d%d",&n,&d,&r)==3) + { + if(n==0 && d==0 && r==0) + break; + int morn[110],ev[110]; + + for0(i,n) + { + si(morn[i]); + } + + for0(i,n) + { + si(ev[i]); + } + + sort(morn,morn+n); + + sort(ev,ev+n,cmp); + +// P(ev[0]); + + int res,ot=0; + for0(i,n) + { + res=morn[i]+ev[i]; + if(res > d ) + ot+= (res-d)*r; + } + + P(ot); + + } + + + return 0; +} + diff --git a/uva_cpp_clean/11389/11389-6.cpp b/uva_cpp_clean/11389/11389-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fce785574145d1425d97ee4ed27b2f68e7534af1 --- /dev/null +++ b/uva_cpp_clean/11389/11389-6.cpp @@ -0,0 +1,31 @@ +/* + greedy > loading balance + difficulty: easy + date: 16/Jan/2020 + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + while (true) { + int N, D, R; cin >> N >> D >> R; + if (!N && !D && !R) break; + + vector morning(N), evening(N); + for (int &m : morning) cin >> m; + for (int &e : evening) cin >> e; + sort(morning.begin(), morning.end()); + sort(evening.rbegin(), evening.rend()); + + int ans = 0; + for (int i = 0; i < N; i++) + ans += max(0, (morning[i]+evening[i]-D)*R); + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11389/11389-9.cpp b/uva_cpp_clean/11389/11389-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..429cfaff334d65b4641474f79966a4e83a54b4bf --- /dev/null +++ b/uva_cpp_clean/11389/11389-9.cpp @@ -0,0 +1,22 @@ +#include + +#define SIZE 105 + +using namespace std; + +int n, d, r, morning[SIZE], evening[SIZE], ans; + +int main(){ + while(scanf("%d %d %d", &n, &d, &r), n | d | r){ + for(int i = 0; i < n; i++) scanf("%d", &morning[i]); + for(int i = 0; i < n; i++) scanf("%d", &evening[i]); + sort(morning, morning + n); + sort(evening, evening + n); + ans = 0; + for(int i = 0; i < n; i++) + if(morning[i] + evening[n - i - 1] > d) + ans += (morning[i] + evening[n - i - 1] - d) * r; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11395/11395-9.cpp b/uva_cpp_clean/11395/11395-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be4c1daf81fd7d86bac617b2ebdf833d6fd19d26 --- /dev/null +++ b/uva_cpp_clean/11395/11395-9.cpp @@ -0,0 +1,11 @@ +#include + +using namespace std; + +long long N; + +int main(){ + while(scanf("%lld", &N), N) + printf("%lld\n", N - int( sqrt(N) ) - int( sqrt(N / 2))); + return(0); +} diff --git a/uva_cpp_clean/11396/11396-13.cpp b/uva_cpp_clean/11396/11396-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec2eac7d3453e53f2620d45b8aeea7ce23522925 --- /dev/null +++ b/uva_cpp_clean/11396/11396-13.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +using namespace std; + +int n; +vector color; +vector < vector > graph; + +bool bfs(int start) +{ + int b = 0, w = 0; + + queue q; + + q.push(start); + + color[start] = 1; + + while (!q.empty()) + { + int top = q.front(); + + q.pop(); + + color[top] ? b++ : w++; + + for (int i = 0; i < graph[top].size(); i++) + { + int child = graph[top][i]; + + if (color[child] == -1) + { + color[child] = !color[top]; + + q.push(child); + } + + else if (color[child] == color[top]) + { + return false; + } + } + } + + return (abs(b - w)) ? false : true; +} + +int main() +{ + while (cin >> n, n != 0) + { + bool ans = true; + + graph.clear(); + graph.resize(n); + + color.clear(); + color.resize(n, -1); + + int x, y; + + while (cin >> x >> y, x != 0 || y != 0) + { + x--; + y--; + + graph[x].push_back(y); + graph[y].push_back(x); + } + + if (n % 2 == 1) cout << "NO" << endl; + + else + { + for (int i = 0; i < n; i++) + { + if (color[i] == -1) + { + ans &= bfs(i); + } + } + + printf("%s\n", ans ? "YES" : "NO"); + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11396/11396-21.cpp b/uva_cpp_clean/11396/11396-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9be511671b8264aaf844c082ba5845a42439670d --- /dev/null +++ b/uva_cpp_clean/11396/11396-21.cpp @@ -0,0 +1,54 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11396 + Name: Claw Decomposition + Problem: https://onlinejudge.org/external/113/11396.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +list adj[400]; + +int color[400]; +bool dfs(int u, int c) { + if (color[u] != -1) + return color[u] == c; + color[u] = c; + + for (int v: adj[u]) + if (!dfs(v, 1-c)) + return false; + + return true; +} + + +int main(){ + int n, u, v; + while (cin>>n && n) { + for (int i=0; i>u>>v && (u||v)) { + adj[u-1].push_back(v-1); + adj[v-1].push_back(u-1); + cnt++; + } + + if (cnt%3==0 && dfs(0, 0)) + cout << "YES\n"; + else + cout << "NO\n"; + } +} diff --git a/uva_cpp_clean/11396/11396-9.cpp b/uva_cpp_clean/11396/11396-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c0ffd0f431c3a46b06fdcf2ae86936f7a0ef741 --- /dev/null +++ b/uva_cpp_clean/11396/11396-9.cpp @@ -0,0 +1,60 @@ +#include + +#define WHITE -1 +#define RED 0 +#define BLACK 1 + +using namespace std; + +const int MAX_V = 310; + +int V, a, b, color[MAX_V]; +vector AdjList[MAX_V]; +bool vis[MAX_V]; + +bool isBipartite(int source){ + bool isOK = true; + queue Q; + Q.push(source); + fill(color, color + V + 1, WHITE); + color[source] = RED; + while( not Q.empty() and isOK ){ + int u = Q.front(); Q.pop(); + vis[u] = true; + for(int j = 0; j < AdjList[u].size() and isOK; j++){ + int v = AdjList[u][j]; + if( color[v] == WHITE ) + color[v] = color[u] ^ 1, Q.push(v); + else if( color[v] == color[u] ) + isOK = false; + } + } + return isOK; +} + +void solve(){ + bool ret = true; + for(int u = 1; u <= V; u++) + if( not vis[u] ) ret &= isBipartite(u); + puts( ret ? "YES" : "NO" ); +} + +void read(){ + while(scanf("%d %d", &a, &b), a | b){ + AdjList[a].push_back(b); + AdjList[b].push_back(a); + } +} + +void clean(){ + fill(vis, vis + V + 1, false); + for(int u = 1; u <= V; u++) AdjList[u].clear(); +} + +int main(){ + while(scanf("%d", &V), V){ + read(); + solve(); + clean(); + } +} diff --git a/uva_cpp_clean/114/114-21.cpp b/uva_cpp_clean/114/114-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f38c74aaa51393ac943d89e34ee046bb5222f9ae --- /dev/null +++ b/uva_cpp_clean/114/114-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 114 + Name: Simulation Wizardry + Problem: https://onlinejudge.org/external/1/114.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int G[55][55] = {}, C[55*55], P[55*55], + w, h, wc, m, x, y, d, l; + cin >> w >> h >> C[1] >> m; + + for (int x=1; x<=w; ++x) + G[x][1] = G[x][h] = 1; + for (int y=1; y<=h; ++y) + G[1][y] = G[w][y] = 1; + + for (int i=2; i> x >> y >> P[i] >> C[i]; + G[x][y] = i; + } + + int dx[] = {1, 0, -1, 0}, + dy[] = {0, 1, 0, -1}, + sum = 0; + + while (cin >> x >> y >> d >> l) { + int p = 0, t; + while (--l > 0) { + if ((t = G[x + dx[d]][y + dy[d]])) { + l -= C[t]; + p += P[t]; + d = (d + 3) % 4; + } + else { + x += dx[d]; + y += dy[d]; + } + } + cout << p << endl; + sum += p; + } + cout << sum << endl; +} diff --git a/uva_cpp_clean/11400/11400-7.cpp b/uva_cpp_clean/11400/11400-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2ba303feec574bfd15f960dc6f333213c264988 --- /dev/null +++ b/uva_cpp_clean/11400/11400-7.cpp @@ -0,0 +1,79 @@ +/** + * Created by LonelyEnvoy on 2017-4-3. + * Lighting System Design + * Keywords: dp + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define rep(i, a, n) for (int i = a; i < n; i++) +#define erep(i, a, n) for (int i = a; i <= n; i++) +#define per(i, a, n) for (int i = n - 1; i >= a; i--) + +const int INF = 0x3f3f3f3f; +const double EPS = numeric_limits::epsilon(); +typedef long long ll; +#define MAX 1001 +// end of header + +struct Unit { + int v, k, c, l; + bool operator<(const Unit& u) { + return v < u.v; + } +}; + +Unit units[MAX]; +int sumNumOfBlub[MAX]; +int dp[MAX]; + +int main() { + int n; + while (~scanf("%d", &n) && n) { + + memset(sumNumOfBlub, 0, sizeof(sumNumOfBlub)); + memset(dp, 0, sizeof(dp)); + + erep(i, 1, n) { + scanf("%d%d%d%d", &units[i].v, &units[i].k, &units[i].c, &units[i].l); + } + sort(units + 1, units + n + 1); + + erep(i, 1, n) { + dp[i] = dp[i - 1] + units[i].c * units[i].l + units[i].k; + } + + erep(i, 1, n) { + erep(j, 1, i) { + sumNumOfBlub[i] += units[j].l; + } + } + + erep(i, 1, n) { + erep(j, 0, i) { + dp[i] = min(dp[i], dp[j] + (sumNumOfBlub[i] - sumNumOfBlub[j]) * units[i].c + units[i].k); + } + } + + printf("%d\n", dp[n]); + } + return 0; +} diff --git a/uva_cpp_clean/11401/11401-13.cpp b/uva_cpp_clean/11401/11401-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6120b5b7c30307e8dd14d0a0b47c70f1f4b03ca --- /dev/null +++ b/uva_cpp_clean/11401/11401-13.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 1000001 +typedef long long ll; +typedef vector vll; + +vll dp(MAX); + +void cal() +{ + dp[3] = 0; + + for (int i = 4; i < MAX; ++i) + { + dp[i] = dp[i - 1]; + + ll temp = 0; + + if (i % 2 == 0) + { + temp = (ll)((((i - 4) / 2) + 1) * (((i - 4) / 2) + 1)); + + dp[i] += temp; + } + + else + { + temp = (ll)(((i - 1) * (i - 3)) / 4); + + dp[i] += temp; + } + } +} + +int main() +{ + int n; + + cal(); + + while (cin >> n && n >= 3) + { + cout << dp[n] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11402/11402-21.cpp b/uva_cpp_clean/11402/11402-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c2d79e43ca3ff8dce40da33920ba855714f6326 --- /dev/null +++ b/uva_cpp_clean/11402/11402-21.cpp @@ -0,0 +1,134 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11402 + Name: Ahoy, Pirates! + Problem: https://onlinejudge.org/external/114/11402.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MAXS 4096000 +int n; +char O[MAXS]; +void assignRange(char Y[], int rl, int rr, int val, int rest=-1, int sl=0, int sr=n, int si=0) { + if (rl<=sl && rr>=sr) Y[si] = val; + if (Y[si] == val) return; + if (rl>=sr || rr<=sl) { + if (rest != -1) + Y[si] = rest; + return; + } + + int id1 = (si<<1) + 1, id2 = (si<<1) + 2; + if (Y[si] != -1) + Y[id1] = Y[id2] = Y[si]; + + int cval = rest!=-1 ? rest : Y[si]; + int mid = (sl + sr) >> 1; + assignRange(Y, rl, rr, val, cval, sl, mid, id1); + assignRange(Y, rl, rr, val, cval, mid, sr, id2); + Y[si] = Y[id1]==Y[id2] ? Y[id1] : -1; +} + +void reverseRange(char Y[], int rl, int rr, int sl=0, int sr=n, int si=0) { + if (rl<=sl && rr>=sr && Y[si]!=-1) + { Y[si] ^= 1; return; } + if (rl>=sr || rr<=sl) return; + + if (Y[si] != -1) { + assignRange(Y, rl, rr, Y[si]^1, -1, sl, sr, si); + return; + } + + int id1 = (si<<1) + 1, id2 = (si<<1) + 2; + int mid = (sl + sr) >> 1; + reverseRange(Y, rl, rr, sl, mid, id1); + reverseRange(Y, rl, rr, mid, sr, id2); + Y[si] = Y[id1]==Y[id2] ? Y[id1] : -1; +} + +int getSum(char Y[], int rl, int rr, int val=-1, int sl=0, int sr=n, int si=0) { + if (sl>=sr || rl>=sr || sl>=rr) return 0; + if (val< 0) val = Y[si]; + if (val>=0) + return val ? min(sr, rr) - max(sl, rl) : 0; + + int id1 = (si<<1) + 1, + id2 = (si<<1) + 2; + int mid = (sl + sr) >> 1; + return getSum(Y, rl, rr, val, sl, mid, id1) + + getSum(Y, rl, rr, val, mid, sr, id2); +} + + +int getPoint(char Y[], int ind, int sl=0, int sr=n, int si=0) { + if (Y[si] >= 0) return Y[si]; + int mid = (sl + sr) >> 1; + return ind < mid + ? getPoint(Y, ind, sl, mid, (si<<1)+1) + : getPoint(Y, ind, mid, sr, (si<<1)+2); +} + + +bool M[4096000]; +void initialize(int sl=0, int sr=n, int si=0) { + if (sl == sr-1) O[si] = M[sl]; + if (sl >= sr-1) return; + + int id1 = (si<<1)+1, + id2 = (si<<1)+2, + mid = (sl + sr) >> 1; + initialize(sl, mid, id1); + initialize(mid, sr, id2); + O[si] = O[id1]==O[id2] ? O[id1] : -1; +} + + +void printRange(char Y[], int cc=-1) { + for (int i=0; i'; + cout << getPoint(Y, i) << ' '; + } + cout << endl; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char cm; + int T, m, c; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cout << "Case " << cse << ":\n"; + n = 0; + for (cin>>c; c--; ) { + string str; + cin >> m >> str; + for (int i=0; i> m; + for (int i=0, q=0, s,e; i> cm >> s >> e; + if (cm == 'F') + assignRange(O, s, e+1, 1); + else if (cm == 'E') + assignRange(O, s, e+1, 0); + else if (cm == 'I') + reverseRange(O, s, e+1); + + else + cout << "Q" << (++q) << ": " << getSum(O, s, e+1) << endl; + } + } +} diff --git a/uva_cpp_clean/11402/11402-6.cpp b/uva_cpp_clean/11402/11402-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7fcb14f68417c2967d9c719e69c20f62e38300 --- /dev/null +++ b/uva_cpp_clean/11402/11402-6.cpp @@ -0,0 +1,104 @@ +/* + data structures > segment tree > lazy propagation + difficulty: medium + date: 12/Mar/2020 + hint: build a segment tree for RSQ, but keep in lazy[v] the type of pending operation to be performed in that interval of A + by: @brpapa +*/ +#include +#include +using namespace std; + +#define NONE ' ' +#define SET_1 'F' +#define SET_0 'E' +#define TOGGLE 'I' + +vector A, bin_tree; +vector lazy; + +int le(int v) { return (v << 1) + 1; } +int ri(int v) { return (v << 1) + 2; } + +void build(int v, int l, int r) { + if (l == r) { bin_tree[v] = A[l]; return; } + + int mid = (l + r) / 2; + build(le(v), l, mid + 0); + build(ri(v), mid + 1, r); + + bin_tree[v] = bin_tree[le(v)] + bin_tree[ri(v)]; +} + +char inverse(char c) { + if (c == SET_1) return SET_0; + if (c == SET_0) return SET_1; + if (c == TOGGLE) return NONE; + if (c == NONE) return TOGGLE; +} + +void propagate_lazy(int v, int l, int r) { + if (lazy[v] == NONE) return; + if (lazy[v] == SET_1) bin_tree[v] = (r-l+1) * 1; // A[l..r] = 1 + if (lazy[v] == SET_0) bin_tree[v] = 0; // A[l..r] = 0 + if (lazy[v] == TOGGLE) bin_tree[v] = (r-l+1) - bin_tree[v]; // inverte A[l..r] + + if (l != r) { + if (lazy[v] == TOGGLE) { + lazy[le(v)] = inverse(lazy[le(v)]); + lazy[ri(v)] = inverse(lazy[ri(v)]); + } + else lazy[le(v)] = lazy[ri(v)] = lazy[v]; + } + lazy[v] = NONE; +} + +int range_query(int v, int l, int r, int ql, int qr) { + propagate_lazy(v, l, r); + + if (ql > r || qr < l) return 0; + if (l >= ql && r <= qr) return bin_tree[v]; + + int mid = (l + r) / 2; + return range_query(le(v), l, mid, ql, qr) + range_query(ri(v), mid + 1, r, ql, qr); +} + +void range_update(int v, int l, int r, int ul, int ur, char op) { + propagate_lazy(v, l, r); + + if (ul > r || ur < l) return; + if (l >= ul && r <= ur) { lazy[v] = op; propagate_lazy(v, l, r); return; } + + int mid = (l + r) / 2; + range_update(le(v), l, mid + 0, ul, ur, op); + range_update(ri(v), mid + 1, r, ul, ur, op); + + bin_tree[v] = bin_tree[le(v)] + bin_tree[ri(v)]; +} + +int main() { + int T, t = 0; cin >> T; + while (T--) { + A.clear(); + + int M; cin >> M; + while (M--) { + int F; string s; cin >> F >> s; + while (F--) for (char c : s) A.push_back(c == '1'? 1:0); + } + + bin_tree.assign(3 * A.size(), 0); + lazy.assign(3 * A.size(), NONE); + build(0, 0, A.size()-1); + + printf("Case %d:\n", ++t); + int Q, q = 0; cin >> Q; + while (Q--) { + char c; int l, r; cin >> c >> l >> r; + + if (c =='S') printf("Q%d: %d\n", ++q, range_query(0, 0, A.size()-1, l, r)); + else range_update(0, 0, A.size()-1, l, r, c); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11404/11404-19.cpp b/uva_cpp_clean/11404/11404-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a20c5d8383c238c94b2111a5a0a14457b5182f23 --- /dev/null +++ b/uva_cpp_clean/11404/11404-19.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +using namespace std; + +string s; +int dp[1001][1001]; +bool mask[1001][1001]; +string dp2[1001][1001]; + +void proc() { + for (int i = 0; i < s.length(); i++) dp[i][i] = 1, mask[i][i] = false; + for (int i = 1; i < s.length(); i++) { + for (int j = 0; j < s.length() - i; j++) { + mask[j+i][j] = false; + if (s[j] == s[j+i]) dp[j+i][j] = 2 + dp[j+i-1][j+1]; + else dp[j+i][j] = max(dp[j+i-1][j], dp[j+i][j+1]); + } + } +} + +string proc2(int fst, int snd) { + if (snd < fst) return ""; + if (fst == snd) return s.substr(fst, 1); + if (!mask[snd][fst]) { + mask[snd][fst] = true; + if (s[fst] == s[snd]) dp2[snd][fst] = s[fst] + proc2(fst+1, snd-1) + s[fst]; + else { + if (dp[snd-1][fst] == dp[snd][fst+1]) dp2[snd][fst] = min(proc2(fst+1, snd), proc2(fst, snd-1)); + else if (dp[snd-1][fst] > dp[snd][fst+1]) dp2[snd][fst] = proc2(fst, snd-1); + else dp2[snd][fst] = proc2(fst+1, snd); + } + } + return dp2[snd][fst]; +} + +int main() { + while (cin >> s) { + proc(); + printf("%s\n", proc2(0, s.length()-1).c_str()); + } + return 0; +} diff --git a/uva_cpp_clean/11407/11407-19.cpp b/uva_cpp_clean/11407/11407-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca8792e6767676f0445f007eb309ae13c256009b --- /dev/null +++ b/uva_cpp_clean/11407/11407-19.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +using namespace std; + +unordered_map m; + +int proc(int N) { + if (m[N] == 0 && N != 0) { + int auxi = floor(sqrt(N)); + int mini = INT_MAX; + do { + int val = proc(N - (auxi*auxi)) + 1; + if (val < mini) mini = val; + auxi--; + } while (auxi); + m[N] = mini; + } + return m[N]; +} + +int main() { + int t; + cin >> t; + while (t--) { + int N; + cin >> N; + printf("%d\n", proc(N)); + } + return 0; +} diff --git a/uva_cpp_clean/11407/11407-20.cpp b/uva_cpp_clean/11407/11407-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1033862b60d9f27d728a85238180da6d2a3bd68f --- /dev/null +++ b/uva_cpp_clean/11407/11407-20.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +int n[10005]; + +int main() +{ + n[0] = 0; + for(int i=1 ; i<10005 ; i++) n[i] = 4; + + for(int i=1 ; i*i<10005 ; i++) n[i*i] = 1; + + for(int i=1 ; i<10005 ; i++) + for(int j=1 ; j<=i ; j++) + if( n[j]==1 ) n[i] = min(n[i], n[i-j]+1); + + int tc; + + scanf("%d", &tc); + while( tc-- ) + { + int N; + scanf("%d", &N); + + printf("%d\n", n[N]); + } + return 0; +} diff --git a/uva_cpp_clean/11407/11407-21.cpp b/uva_cpp_clean/11407/11407-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d59e3b5d5e0de5c453453965e0eb140ba3acac91 --- /dev/null +++ b/uva_cpp_clean/11407/11407-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11407 + Name: Squares + Problem: https://onlinejudge.org/external/114/11407.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int DP[10143]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i=0; i<10143; ++i) + DP[i] = i; + + for (int i=2, u; (u=i*i)<10143; ++i) + for (int j=0; j+u<10143; ++j) + DP[j+u] = min(DP[j+u], DP[j]+1); + + int T, n; + cin >> T; + while (T--) { + cin >> n; + cout << DP[n] << "\n"; + } + +} diff --git a/uva_cpp_clean/11407/11407-6.cpp b/uva_cpp_clean/11407/11407-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f7f77c9ba50fbabce20d45c039b67d4e5240907 --- /dev/null +++ b/uva_cpp_clean/11407/11407-6.cpp @@ -0,0 +1,36 @@ +/* + dynamic programming > coin change + difficulty: easy + date: 14/May/2020 + hint: consider the coins as the perfect squares + by: @brpapa +*/ +#include +using namespace std; + +int memo[10010]; +int dp(int n) { + // valor restante n + + if (n == 0) return 0; + + int &ans = memo[n]; + if (ans != -1) return ans; + + ans = 1 << 30; + for (int i = 1; n-i*i >= 0; i++) + ans = min(ans, 1 + dp(n-i*i)); + + return ans; +} + +int main() { + memset(memo, -1, sizeof memo); // o conjunto dos quadrados perfeitos nunca muda + + int T; cin >> T; + while (T--) { + int N; cin >> N; + cout << dp(N) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11407/11407-9.cpp b/uva_cpp_clean/11407/11407-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1182c1ef3f97936e3e1758591dc966e9ebc766c3 --- /dev/null +++ b/uva_cpp_clean/11407/11407-9.cpp @@ -0,0 +1,25 @@ +#include + +#define MAX_N 10010 + +using namespace std; + +int tc,n,memo[MAX_N]; + +int dp(int sum){ + if(sum==0) return 0; + if(memo[sum]) return memo[sum]; + int ans = INT_MAX; + for(int it=1;it*it<=sum;it++) + ans = min(ans,1+dp(sum-it*it)); + return memo[sum] = ans; +} + +int main(){ + scanf("%d",&tc); + while(tc--){ + scanf("%d",&n); + printf("%d\n",dp(n)); + } + return(0); +} diff --git a/uva_cpp_clean/11408/11408-5.cpp b/uva_cpp_clean/11408/11408-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..726555825aa0893f5ecbe358bbaf8047484a4ea1 --- /dev/null +++ b/uva_cpp_clean/11408/11408-5.cpp @@ -0,0 +1,201 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (5000010) +#define mod 1000000007 +////=====================================//// + +bool prime[mx+10]; +int check[mx+10]; +int cs[mx+10]; + +void cal() +{ + for(int i=1;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11412 + Name: Dig the Holes + Problem: https://onlinejudge.org/external/114/11412.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +vector perms; +char colors[] = "RGBYOV", ss[5]; +bool used[6]; int Ci[128]; +void initAll(int idx) { + if (idx == 4) { + perms.push_back(ss); + return; + } + + for (int i=0; i<6; ++i) + if (!used[i]) { + used[i] = 1; + ss[idx] = colors[i]; + initAll(idx + 1); + used[i] = 0; + } +} + +bool check(const string &so, const string &sg, int x, int y) { + int cc[6]={}; + for (int i=0; i<4; ++i) + if (sg[i] == so[i]) + --x; + else ++cc[Ci[so[i]]]; + if (x) return 0; + + for (int i=0; i<4; ++i) + if (sg[i] != so[i]) + if (cc[Ci[sg[i]]]) { + --cc[Ci[sg[i]]]; + --y; + } + return !y; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=0; i<6; ++i) + Ci[colors[i]] = i; + initAll(0); + + int T, x1, x2, y1, y2; + cin >> T; + string s1, s2; + while (T--) { + cin >> s1 >> x1 >> y1 + >> s2 >> x2 >> y2; + + bool flag = false; + for (string s: perms) + if (check(s, s1, x1, y1) && check(s, s2, x2, y2)) { + flag = true; + break; + } + cout << (flag ? "Possible\n" : "Cheat\n"); + } +} diff --git a/uva_cpp_clean/11413/11413-11.cpp b/uva_cpp_clean/11413/11413-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2b36c9fc3f646bb4f3fdc40f0736fdf69da4d8b --- /dev/null +++ b/uva_cpp_clean/11413/11413-11.cpp @@ -0,0 +1,76 @@ +#include +/* +Problem: 11413 - Fill the Containers +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2408 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N, M, vessels[1001]; +bool valid(int capacity); + +int main () { + + while ( scanf("%d %d\n", &N, &M) != EOF ){ + + int max_capacity = 0, sum = 0; + for (int i = 0; i < N; ++i){ + scanf("%d", &vessels[i]); + max_capacity = max(max_capacity, vessels[i]); + sum += vessels[i]; + } + + int capacity = max_capacity; + for (int b = sum; b >= 1; b /= 2) + while (!valid(capacity + b)) capacity += b; + + if (!valid(capacity)) capacity++; + + printf("%d\n", capacity); + } + + return 0; +} + +bool valid(int capacity){ + + int containers = 0, seg_sum = 0; + for (int i = 0; i < N; ++i){ + + if (seg_sum+vessels[i] > capacity) seg_sum = 0; + if (seg_sum == 0) containers++; + + seg_sum += vessels[i]; + } + + return containers <= M; +} +/* +Sample input:- +----------------- +5 3 +1 2 3 4 5 +3 2 +4 78 9 + +Sample output:- +----------------- +6 +82 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11413/11413-14.cpp b/uva_cpp_clean/11413/11413-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c9575d59f98c99fabc0cc8ea18ce6497d0b13ea4 --- /dev/null +++ b/uva_cpp_clean/11413/11413-14.cpp @@ -0,0 +1,63 @@ +/*************************************************** + * Problem name : 11413 - Fill the Containers.cpp + * OJ : Uva + * Result : AC + * Date : 07-04-17 + * Problem Type : Binary Search + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 10005 +using namespace std; +typedef long long ll; +ll ar[MAX],sum,mx,n,m; +ll BinarySearch(){ + ll high = sum; + ll low = mx; + ll ans; + while(high>=low){ + ll mid = (high+low)/2; + int sm = 0,box = 1; + for(int i = 0; imid){ + box++; + sm = ar[i]; + } + } + if(box<=m){ + ans = mid; + high = mid -1; + } + else{ + low = mid + 1; + } + } + return ans; +} +int main () { + while(scanf("%lld %lld", &n, &m) == 2){ + sum = 0, mx = 0; + for(ll i = 0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11413 + Name: Fill the Containers + Problem: https://onlinejudge.org/external/114/11413.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int c[1001], n, m; +bool ispos(int cap) { + int sum = 0, cnt = 1; + for (int i=0; i cap) { + sum = c[i]; + if (++cnt > m) + return false; + } + return true; +} + +int main() { + while (scanf("%d%d", &n, &m)==2) { + int lo=0, hi=0; + for (int i=0; i>1; + if (ispos(mid)) + hi = mid-1; + else + lo = mid+1; + } + printf("%d\n", hi+1); + } +} diff --git a/uva_cpp_clean/11413/11413-9.cpp b/uva_cpp_clean/11413/11413-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3503f1147cb799a48339addbb65d5be04a93508 --- /dev/null +++ b/uva_cpp_clean/11413/11413-9.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int n, m, v[1010], lo, hi, mid, _max; + +bool isValid(int min_c){ + int c = 1, pib = 0; + for(int i = 0; i < n; i++){ + if(pib + v[i] <= min_c) pib += v[i]; + else c++, pib = v[i]; + } + return (c <= m); +} + +int main(){ + while(scanf("%d %d", &n, &m)==2){ + _max = -1; + for(int i = 0; i < n; i++) scanf("%d", &v[i]), _max = max(_max, v[i]); + lo = _max, hi = 1000000000; + while(lo != hi){ + mid = (lo + hi) >> 1; + if(isValid(mid)) hi = mid; + else lo = mid + 1; + } + printf("%d\n", lo); + } + return(0); +} diff --git a/uva_cpp_clean/11414/11414-6.cc b/uva_cpp_clean/11414/11414-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..dd85a0cdab81a47cbff70a2caaebd2b296fdd583 --- /dev/null +++ b/uva_cpp_clean/11414/11414-6.cc @@ -0,0 +1,22 @@ +// https://uva.onlinejudge.org/external/114/11414.pdf +#include +using namespace std; +using vi=vector; +int main(){ + int n,t; + cin>>t; + while(t--){ + cin>>n; + vi a(n); + int o=1; + for(int i=0;i>a[i]; + sort(&a[0],&a[n],greater()); + if(accumulate(&a[0],&a[n],0)%2)o=0; + for(int k=1;k<=n;k++){ + int s=0; + for(int i=k;ik*(k-1)+s)o=0; + } + cout<<(o?"Yes\n":"No\n"); + } +} diff --git a/uva_cpp_clean/11417/11417-18.cpp b/uva_cpp_clean/11417/11417-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..42b4b103447289b928a94074cc094e92303ab97a --- /dev/null +++ b/uva_cpp_clean/11417/11417-18.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main(){ + int n; + while(scanf("%d", &n) != EOF && n != 0){ + int sum = 0; + for(int i = 1; i < n; i++){ + for(int j = i + 1; j <= n; j++) sum+=__gcd(i, j); + } + printf("%d\n", sum); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11417/11417-21.cpp b/uva_cpp_clean/11417/11417-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7a9fea02197000f451023bc053d1cd9439cc59d --- /dev/null +++ b/uva_cpp_clean/11417/11417-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11417 + Name: GCD + Problem: https://onlinejudge.org/external/114/11417.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int GCD[502][502]; +int gcd(int a, int b) { + if (!b) return a; + if (GCD[a][b]) return GCD[a][b]; + return GCD[a][b] = gcd(b, a%b); +} + +int main(){ + int n; + while(cin>>n && n) { + int sum=0; + for (int i=1; i +int gcd( int a, int b) +{ +if(a%b==0) +return b; +else +return gcd(b,a%b); +} +int main() +{ + int i,n,s,j; + while(scanf("%d",&n)==1) + { + if(n==0) + break; + else + { + s=0; + i=1; + for(i=1;i + +using namespace std; + +int N, G; + +int gcd(int a, int b){ + return b == 0 ? a : gcd(b, a % b); +} + +int main(){ + while(scanf("%d", &N), N){ + G = 0; + for(int it1 = 1; it1 < N; it1++) + for(int it2 = it1 + 1; it2 <= N; it2++) + G += gcd(it1, it2); + printf("%d\n", G); + } + return(0); +} diff --git a/uva_cpp_clean/11418/11418-19.cpp b/uva_cpp_clean/11418/11418-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8825291a59d4a2259cff5f220c491733920d1af6 --- /dev/null +++ b/uva_cpp_clean/11418/11418-19.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +int N, M, mf, f, s, t, INF = 2000000; +int res[60][60]; +vi p; + +void augment(int v, int minEdge) { + if (v == s) { + f = minEdge; + return; + } else if (p[v] != -1) { + augment(p[v], min(minEdge, res[p[v]][v])); + res[p[v]][v] -= f; res[v][p[v]] += f; + } +} + +int main() { + int T; + vector v[27]; + cin >> T; + for (int c = 1; c <= T; c++) { + int n; + cin >> n; + for (int i = 0; i < n + 28; i++) { + for (int j = 0; j < n + 28; j++) { + res[i][j] = 0; + } + } + for (int j = 1; j < n + 1; j++) { + res[0][j] = 1; + } + for (int j = 0; j < n; j++) { + res[n + 1 + j][n + 27] = 1; + } + for (int i = 0; i < n; i++) { + int k; + cin >> k; + v[i].assign(k, ""); + for (int j = 0; j < k; j++) { + cin >> v[i][j]; + transform(v[i][j].begin(), v[i][j].end(), v[i][j].begin(), ::tolower); + v[i][j][0] = v[i][j][0] - 'a' + 'A'; + res[i + 1][n + 1 + v[i][j][0] - 'A'] = 1; + } + } + N = n + 28; + mf = 0; + s = 0, t = n + 27; + while (true) { + f = 0; + vi dist(N, INF); dist[s] = 0; + queue q; + q.push(s); + p.assign(N, -1); + while (!q.empty()) { + int u = q.front(); q.pop(); + if (u == t) break; + for (int v = 0; v < N; v++) { + if (res[u][v] > 0 && dist[v] == INF) { + dist[v] = dist[u] + 1, q.push(v), p[v] = u; + } + } + } + augment(t, INF); + if (f == 0) break; + mf += f; + } + printf("Case #%d:\n", c); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (res[n + 1 + i][j + 1]) { + for (int k = 0; k < v[j].size(); k++) { + if (v[j][k][0] - 'A' == i) printf("%s\n", v[j][k].c_str()); + } + break; + } + } + } + } + return 0; +} diff --git a/uva_cpp_clean/11418/11418-21.cpp b/uva_cpp_clean/11418/11418-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39bf47966b2efd764c660a17f8ceefa22315ea21 --- /dev/null +++ b/uva_cpp_clean/11418/11418-21.cpp @@ -0,0 +1,77 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11418 + Name: Clever Naming Patterns + Problem: https://onlinejudge.org/external/114/11418.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +void capitalize(string &s) { + s[0] = toupper(s[0]); + for (int i=1; s[i]; ++i) + s[i] = tolower(s[i]); +} + +char R[43]; +vector adj[43]; +int match[26]; +bool seen[26]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + if (match[v]==-1 || bpm(match[v])) { + match[v] = u; + return 1; + } + } + return 0; +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + vector names[43]; + for (int cse=1; cse<=T; ++cse) { + cin >> n; + for (int i=0, sz; i> sz; + adj[i].clear(); + names[i].resize(sz); + for (int j=0; j> names[i][j]; + capitalize(names[i][j]); + int v = names[i][j][0]-'A'; + if (v < n) + adj[i].push_back(v); + } + } + + memset(match, -1, sizeof(match)); + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11419 + Name: SAM I AM + Problem: https://onlinejudge.org/external/114/11419.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// --------------------------------------------------------------------- + +vector adj[1143]; +int matchL[1143], matchR[1143]; +bool seen[1143]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + if (matchR[v] < 0 || bpm(matchR[v])) { + matchR[v] = u; + matchL[u] = v; + return true; + } + } + return false; +} + +// http://www.immorlica.com/combOpt/lec1.pdf +void konig(int u) { + matchL[u] = -1; + for (int v: adj[u]) + if (!seen[v] && matchR[v]>=0) { + seen[v] = 1; + konig(matchR[v]); + } +} + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + while (1) { + int r = readUInt(), + c = readUInt(), + n = readUInt(); + if (r<=0 && c<=0 && n<=0) + return 0; + + for (int i=0; i<=r; ++i) + adj[i].clear(); + + while (n--) { + int u = readUInt()-1, + v = readUInt()-1; + adj[u].push_back(v); + } + + int cnt = 0; + memset(matchL, -1, r*sizeof(int)); + memset(matchR, -1, c*sizeof(int)); + for (int i=0; i=0) + cout << " r" << i+1; + + for (int i=0; i +#include +using namespace std; + +unordered_map map; + +unsigned long long proc(int n, int s, bool lock) { + if (s > n || s <= -1 || (s == n && !lock)) return 0; + if (n == 1) return s == 1? (lock? 1: 0): (lock? 1: 2); + if (s == n) return 1; + if (!map.count(n*1000 + s*10 + (lock? 1: 0))) { + unsigned long long val = 0; + /*if (s == 0) { + if (lock) val += proc(n-1, s, false); + else { + val += proc(n-1, s, false); + val += proc(n-1, s, true); + } + } else {*/ + val += proc(n-1, s, false); + if (lock) val += proc(n-1, s-1, true); + else val += proc(n-1, s, true); + //} + map[n*1000 + s*10 + (lock? 1: 0)] = val; + } + return map[n*1000 + s*10 + (lock? 1: 0)]; +} + +int main() { + int n, s; + while (cin >> n >> s && !(n < 0 && s < 0)) { + printf("%llu\n", proc(n, s, true)); + } + return 0; +} diff --git a/uva_cpp_clean/11420/11420-21.cpp b/uva_cpp_clean/11420/11420-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47a892aba887bd04547ffd245505fe4a064e6283 --- /dev/null +++ b/uva_cpp_clean/11420/11420-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11420 + Name: Chest of Drawers + Problem: https://onlinejudge.org/external/114/11420.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + unsigned long long DP[66][66]={{1}}; + for (int n=1; n<66; ++n) + for (int s=0; s<66; ++s) { + DP[n][s] = s > 0 ? DP[n-1][s-1] : 1; + for (int k=0; k<=n-2; ++k) + DP[n][s] += DP[k][s]; + } + + for (int n, k; cin>>n>>k && (n>=0 && k>=0); ) + cout << DP[n][k] << "\n"; +} diff --git a/uva_cpp_clean/11420/11420-9.cpp b/uva_cpp_clean/11420/11420-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8031f8705d92087c3b67b16d39c770aeb570ea70 --- /dev/null +++ b/uva_cpp_clean/11420/11420-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +typedef long long ll; + +const int SIZE = 70; + +ll n, s, memo[2][SIZE][SIZE]; + +ll dp (bool secure, int locked, int id) { + if (id == n) return locked == s; + if (~memo[secure][locked][id]) return memo[secure][locked][id]; + ll ret = 0; + ret += dp(true, locked + secure, id + 1); + ret += dp(false, locked, id + 1); + return memo[secure][locked][id] = ret; +} + +int main () { + while (cin >> n >> s) { + if (n < 0 and s < 0) break; + memset(memo, -1, sizeof memo); + if (s > n) cout << 0 << endl; + else cout << dp(true, 0, 0) << endl; + } + return (0); +} diff --git a/uva_cpp_clean/11427/11427-19.cpp b/uva_cpp_clean/11427/11427-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..656a668f2f8e0dcf2ad5e120fed019f01753539c --- /dev/null +++ b/uva_cpp_clean/11427/11427-19.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; + +unordered_map > dp; +int pn, pd, n; + +long double proc(int w, int l) { + if (w + l > n) return 0; + if (w + l <= n && w * pd > pn * (w+l)) return 1; + if (!dp.count(w) || !dp[w].count(l)) { + dp[w][l] = (pn*proc(w+1, l) + (pd-pn)*proc(w, l+1)) / ((long double) pd); + } + return dp[w][l]; +} + +int main() { + int N; + cin >> N; + for (int c = 1; c <= N; c++) { + scanf("%d/%d %d", &pn, &pd, &n); + dp.clear(); + long double pf = proc(0, 0); + if (ceil(1/(1-pf)) - (1/(1-pf)) < 1e-9) printf("Case #%d: %.0Lf\n", c, ceil(1/(1-pf))); + else printf("Case #%d: %.0Lf\n", c, floor(1/(1-pf))); + } + return 0; +} diff --git a/uva_cpp_clean/11428/11428-19.cpp b/uva_cpp_clean/11428/11428-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51131c18e926e813db63a3918a4419d4f2aa6c87 --- /dev/null +++ b/uva_cpp_clean/11428/11428-19.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +typedef long long ll; + +int main() { + ll N; + while (cin >> N && N != 0) { + bool ok = true, end = false; + int solx, soly; + for (ll x = 2; ok && !end; x++) { + ll px = x*x*x; + for (ll y = x - 1; y > 0 && ok && !end; y--) { + ll py = y*y*y; + if (px - py > N) { + if (y == x - 1) ok = false; + break; + } + if (px - py == N) solx = x, soly = y, end = true; + } + } + if (ok) printf("%d %d\n", solx, soly); + else printf("No solution\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11428/11428-21.cpp b/uva_cpp_clean/11428/11428-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc41041136ee743d3c888d9115ad7f7b84a0c47f --- /dev/null +++ b/uva_cpp_clean/11428/11428-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11428 + Name: Cubes + Problem: https://onlinejudge.org/external/114/11428.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int X[10001], Y[10001], n; + +int main(){ + for (int i=60; i>0; i--) + for (int j=i-1; j>0; j--) { + int d = i*i*i - j*j*j; + if (d > 10000) break; + X[d] = i; Y[d] = j; + } + + + + while (cin>>n && n) + if (X[n]) + printf("%d %d\n", X[n], Y[n]); + else + puts("No solution"); +} diff --git a/uva_cpp_clean/11428/11428-5.cpp b/uva_cpp_clean/11428/11428-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fa4c6f36066b8fac1eca3c67de10ce6699947320 --- /dev/null +++ b/uva_cpp_clean/11428/11428-5.cpp @@ -0,0 +1,38 @@ +#include +#include +int main() +{ + int n,i,j,k,x,y,f; + while(scanf("%d",&n)==1) + { + if(n==0) + break; + k=0; + f=0; + x=0; + y=0; + for(j=1;jn) + break; + } + if(x==0&&y==0) + printf("No solution\n"); + else + printf("%d %d\n",x,y); + } + + + + + return 0; +} diff --git a/uva_cpp_clean/11432/11432-21.cpp b/uva_cpp_clean/11432/11432-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e061c6f803a440fb9e565a70c3afdb454d1136f --- /dev/null +++ b/uva_cpp_clean/11432/11432-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11432 + Name: Busy Programmer + Problem: https://onlinejudge.org/external/114/11432.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long DP[34][77][77] = {}; + for (int g=1; g<34; ++g) { + DP[g][0][0] = 1; + for (int i=1; i<=66; ++i) { + for (int j=1; j<=i; ++j) { + DP[g][i][j] = 0; + for (int k=min(i, g); k>0; --k) + DP[g][i][j] += DP[g][i-k][i-j]; + } + } + } + + for (int cse=1, d,g; cin >> d >> g && (d>=0 && g>=0); ++cse) { + long long sum = 0; d <<= 1; + for (int i=1; d-i>=0; i+=g+1) + sum += DP[g][d-i][d>>1]; + + cout << "Case " << cse << ": " << 2*sum << endl; + } +} diff --git a/uva_cpp_clean/11437/11437-20.cpp b/uva_cpp_clean/11437/11437-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90aa9ea4ad1c1360d3932b3c5f2c708d9e79432c --- /dev/null +++ b/uva_cpp_clean/11437/11437-20.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int tc; + + cin >> tc; + while( tc-- ) + { + double Ax, Ay, Bx, By, Cx, Cy; + cin >> Ax >> Ay >> Bx >> By >> Cx >> Cy; + + double area = (Ax*By+Bx*Cy+Cx*Ay-Ay*Bx-By*Cx-Cy*Ax)/(2*7); + + if( area<0) + area *= -1; + + cout << fixed << setprecision(0) << area << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11448/11448-21.cpp b/uva_cpp_clean/11448/11448-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40cd94385797f880b122fabff0bc5255749459ea --- /dev/null +++ b/uva_cpp_clean/11448/11448-21.cpp @@ -0,0 +1,360 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11448 + Name: Who said crisis? + Problem: https://onlinejudge.org/external/114/11448.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + + if (cells) delete [] cells; + cells = ncells; + } + if (!cp) + memset(cells, 0, capacity*CellSize); + else memset(cells+sz, 0, (capacity-sz)*CellSize); + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + // --------------------------------------------------------------------- + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + + BigInt(const char s[]) { + while (s[0]==' ' || s[0]=='\n' || s[0]=='\t') s++; + sign = 1; + if (s[0] == '+') s++; + if (s[0] == '-') { sign=-1; s++; } + while (s[0]=='0') s++; + if (!s[0]) { init(); return; } + + int len = strlen(s); + size = (len+3)/MAX_D; + cells = new CellType[capacity = size+16]; + for (int i=len-1; i>=0; i-=MAX_D) { + int rc = 0; + for (int k=i-MAX_D+1; k<=i; k++) { + if (k<0 || s[k]=='-' || s[k]=='+') continue; + rc = rc*10 + s[k] - '0'; + } + cells[(len-i-1) / MAX_D] = rc; + } + for (int i=size; i= 0) + delete [] cells; + } + + int getSize() { return size; } + int getCell(int i) { return cells[i]; } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { + return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign; + } + bool operator == (const BigInt &o) const { return sign==o.sign && numEq(o); } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + int osz = size; + setSize(max(size, o.size)+1); + for (int i=osz; i= MAX_C) { + cells[i] -= MAX_C; + ++cells[i+1]; + } + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (*this == o) { + sign = size = 0; + return *this; + } + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i=0; --i) { + t = t*MAX_C + cells[i]; + cells[i] = t / den; + t -= cells[i] * den; + } + return autoSize(); + } + BigInt operator/ (int den) const { return *new BigInt(*this) *= den; } + + int operator% (int den) const { + int r = 0; + for (int i = size-1; i>=0; --i) + r = (r * MAX_C + cells[i]) % den; + return r; + } + + // --------------------------------------------------------------------- + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + +// tostring, input from char + +bool powseen[10][10000]; +BigInt pmemo[10][10000]; +BigInt pow(int a, int b) { + if (!b) return BigInt(1); + if (b==1 || a<=1) + return BigInt(a); + + if (a<10 && b<10000 && powseen[a][b]) + return pmemo[a][b]; + + BigInt c = pow(a, b>>1); + c *= c; + if (b&1) c *= a; + + if (a<10 && b<10000) { + powseen[a][b] = 1; + pmemo[a][b] = c; + } + return c; +} + +BigInt pow(int a, BigInt b) { + if (b.getSize() <= 1) + return pow(a, b.getCell(0)); + + bool has2 = b % 2; + b /= 2; + BigInt c = pow(a, b); + c *= c; + if (b%2) c*= a; + + return c; +} + + + +char s1[10017], s2[10017]; +int main() { + int T; + scanf("%d", &T); + while (T--) { + scanf("%s%s", s1, s2); + + BigInt b1(s1), b2(s2); + b1 -= b2; + cout << b1 << endl; + } +} diff --git a/uva_cpp_clean/11450/11450-13.cpp b/uva_cpp_clean/11450/11450-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f12d3961f5988ad405768c348dd4ecbb3ea54050 --- /dev/null +++ b/uva_cpp_clean/11450/11450-13.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include + +using namespace std; + +#define MIN -1000000007 + +bool dp[2][210]; +int m, c, k, price[25][25]; + +int main() +{ + int t, result = 0; + + cin >> t; + + while (t--) + { + cin >> m >> c; + + memset(price, 0, sizeof price); + + for (int i = 0; i < c; i++) + { + cin >> price[i][0]; + + for (int j = 1; j <= price[i][0]; j++) + { + cin >> price[i][j]; + } + } + + memset(dp, false, sizeof dp); + + for (int i = 1; i <= price[0][0]; i++) + { + if (m - price[0][i] >= 0) + { + dp[1][m - price[0][i]] = true; + } + } + + for (int i = 1; i < c; i++) + { + for (int j = 0; j <= m; j++) + { + dp[0][j] = dp[1][j]; + dp[1][j] = false; + } + + for (int j = 0; j <= m; j++) + { + if (dp[0][j]) + { + for (int k = 1; k <= price[i][0]; k++) + { + if (j - price[i][k] >= 0) + { + dp[1][j - price[i][k]] = true; + } + } + } + } + } + + for (result = 0; result <= m && !dp[1][result]; result++); + + if (result == m + 1) + { + cout << "no solution" << endl; + } + } +} diff --git a/uva_cpp_clean/11450/11450-19.cpp b/uva_cpp_clean/11450/11450-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9392b7429d1ef46de1d38cde7f4782e5990a9299 --- /dev/null +++ b/uva_cpp_clean/11450/11450-19.cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; + +int garments[20][21]; +int costs[20][201]; + +int main() { + int N; + cin >> N; + while (N--) { + int M, C; + cin >> M >> C; + for (int i = 0; i < C; i++) { + cin >> garments[i][0]; + for (int j = 1; j <= garments[i][0]; j++) cin >> garments[i][j]; + int * f = &garments[i][1]; + sort(f, f + garments[i][0]); + } + int p = -1, j = 1; + for (int i = 0; i <= M; i++) { + if (j <= garments[0][0] && garments[0][j] <= i) p = garments[0][j], j++; + costs[0][i] = p; + } + for (int j = 1; j < C; j++) { + for (int i = 0; i <= M; i++) { + int val = -1; + for (int k = 1; k <= garments[j][0]; k++) { + if (i >= garments[j][k]) { + if (costs[j-1][i - garments[j][k]] != -1 && costs[j-1][i - garments[j][k]] + garments[j][k] > val) val = costs[j-1][i - garments[j][k]] + garments[j][k]; + } + } + costs[j][i] = val; + } + } + if (costs[C-1][M] == -1) printf("no solution\n"); + else printf("%d\n", costs[C-1][M]); + } + return 0; +} diff --git a/uva_cpp_clean/11450/11450-21.cpp b/uva_cpp_clean/11450/11450-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ededb0093c2e33e92bf3fed862862326f18922f7 --- /dev/null +++ b/uva_cpp_clean/11450/11450-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11450 + Name: Wedding shopping + Problem: https://onlinejudge.org/external/114/11450.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int n; +vector items[30]; +int memo[202][22]; +int ks(int remm, int ind) { + if (ind==n) return 0; + if (remm<=0) return -(1<<30); + if (memo[remm][ind] != -1) + return memo[remm][ind]; + + int mx = -(1<<30); + for (int w: items[ind]) + if (w <= remm) + mx = max(mx, ks(remm-w, ind+1) + w); + + return memo[remm][ind] = mx; +} + + + +int main(){ + int T, m, k, x; + cin>>T; + while (T--) { + cin>>m>>n; + int sm = 0; + for (int i=0; i> k; + int mn = 1<<30; + items[i].clear(); + for (int j=0; j> x; + items[i].push_back(x); + if (x < mn) mn = x; + } + sm += mn; + } + + if (sm > m) puts("no solution"); + else if (sm == m) cout << sm << endl; + else { + memset(memo, -1, sizeof(memo)); + cout << ks(m, 0) << endl; + } + } +} diff --git a/uva_cpp_clean/11450/11450-6.cpp b/uva_cpp_clean/11450/11450-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2aacce9cb5a08c312e2fd89432c2eb727328aab --- /dev/null +++ b/uva_cpp_clean/11450/11450-6.cpp @@ -0,0 +1,50 @@ +/* + dynamic programming + difficulty: easy + date: 16/Jan/2020 + by: @brpapa +*/ +#include +#include +#include +#define INF 0x3f3f3f3f +using namespace std; + +vector prices[22]; // prices[c] = preços dos modelos da vestimenta c +int M, C; // dinheiro disponível, vestimentas para comprar + +int memo[22][210]; +int dp(int c, int m) { + // c: vestimenta atual + // m: dinheiro restante + + if (m < 0) return -INF; // inviável + if (c == C) return M-m; // total gasto + + int &ans = memo[c][m]; + if (ans != -1) return ans; + + for (int price : prices[c]) + ans = max(ans, dp(c+1, m-price)); + + return ans; +} + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int N; cin >> N; + while (N--) { + cin >> M >> C; + memset(memo, -1, sizeof(memo)); + for (int c = 0; c < C; c++) { + int K; cin >> K; + prices[c].resize(K); + for (int &p : prices[c]) cin >> p; + } + int ans = dp(0, M); + if (ans >= 0) cout << ans << endl; + else cout << "no solution" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11450/11450-9.cpp b/uva_cpp_clean/11450/11450-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fc12d3ef081f89c140e98ad68c96efbfb25d8aa --- /dev/null +++ b/uva_cpp_clean/11450/11450-9.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int tc,M,C,price[25][25],score; +int memo[210][25]; + +int shop(int money,int g){ + if(money<0) return -1; + if(g==C) return M-money; + int &ans = memo[money][g]; + if(ans!=-1) return ans; + for(int model=1;model<=price[g][0];model++) + ans = max(ans,shop(money-price[g][model],g+1)); + return ans; +} + +int main(){ + scanf("%d",&tc); + while(tc--){ + scanf("%d %d",&M,&C); + for(int i=0;i +#include +using namespace std; + +int main() { + int N; + string line; + cin >> N; + getline(cin, line); + while (N--) { + getline(cin, line); + int pos = 0, comp1 = 0, comp2 = 0, i = 1; + bool found = false; + while (!found) { + for (; i < line.length() && !found; i++) { + comp2 = i; + for (; i < line.length() && line[i] == line[comp1]; i++, comp1++) { + if (comp1 == comp2 - 1) { + found = true; + break; + } + } + if (!found) i = comp2; + comp1 = 0; + } + for (; i < line.length() && line[i] == line[comp1]; i++, comp1++); + if (i != line.length()) found = false, comp1 = 0, i = comp2 + 1; + } + for (int i = 0; i < 8; i++) cout << line[comp1 + (i%(line.length() - comp1))]; + cout << "..." << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11452/11452-21.cpp b/uva_cpp_clean/11452/11452-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81a78d35a2a7d2d83536c8522e7c897bf93caca6 --- /dev/null +++ b/uva_cpp_clean/11452/11452-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11452 + Name: Dancing the Cheeky-Cheeky + Problem: https://onlinejudge.org/external/114/11452.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + char str[2048]; + while (T-- && cin >> str) { + int l = 1, i; + for (i=1; str[i]; ++i) { + if (str[i] != str[i%l]) { + while (str[i] != str[i%l]) + ++l; + i = l; + } + } + + for (int j=0; j<8; ++j, ++i) + cout << str[i%l]; + cout << "...\n"; + } +} diff --git a/uva_cpp_clean/11455/11455-19.cpp b/uva_cpp_clean/11455/11455-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a341f9a92ecdea03b3b697fb643c4958a8bab44 --- /dev/null +++ b/uva_cpp_clean/11455/11455-19.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main() { + int N, l[4]; + cin >> N; + while (N--) { + cin >> l[0] >> l[1] >> l[2] >> l[3]; + sort(l, l+4); + if (l[0] == l[1] && l[1] == l[2] && l[2] == l[3]) printf("square\n"); + else if (l[0] == l[1] && l[2] == l[3] && l[1] != l[2]) printf("rectangle\n"); + else if (l[3] >= l[0] + l[1] + l[2] || + l[3] < (l[0] < l[1] + l[2]? 0 : l[0] - l[1] - l[2])) printf("banana\n"); + else printf("quadrangle\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11455/11455-21.cpp b/uva_cpp_clean/11455/11455-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d46469cbe444e933da57d2722c21caaee626cfa6 --- /dev/null +++ b/uva_cpp_clean/11455/11455-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11455 + Name: Behold my quadrangle + Problem: https://onlinejudge.org/external/114/11455.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int T, x[4]; + cin>>T; + while(T--) { + cin>>x[0]>>x[1]>>x[2]>>x[3]; + sort(x, x+4); + + if (x[0]==x[1] && x[2]==x[3]) + cout<< (x[1]==x[2] ? "square\n" : "rectangle\n"); + else if (x[3] < x[0]+x[1]+x[2]) + cout << "quadrangle\n"; + else + cout << "banana\n"; + } +} diff --git a/uva_cpp_clean/11455/11455-5.cpp b/uva_cpp_clean/11455/11455-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6244d8bbc107859b28acf29824df27442141385d --- /dev/null +++ b/uva_cpp_clean/11455/11455-5.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + int t,a,b,c,d; + sc("%d",&t); + while(t--) + { + sc("%d%d%d%d",&a,&b,&c,&d); + if(a==b&&a==c&&a==d) + { + pf("square\n"); + } + else if( (a==b && c==d)||(b==c && d==a)||(a==c&& b==d)) + { + pf("rectangle\n"); + } + else if((a<=b+c+d)&&(b<=c+d+a)&&(c<=d+a+b)&&(d<=a+b+c)) + { + pf("quadrangle\n"); + } + else + { + pf("banana\n"); + } + } + + + return 0; +} diff --git a/uva_cpp_clean/11456/11456-10.cpp b/uva_cpp_clean/11456/11456-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c7b0efa98ed07decdb8406c55df40616b3f176e --- /dev/null +++ b/uva_cpp_clean/11456/11456-10.cpp @@ -0,0 +1,72 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + __builtin_popcountll(); +*/ +bool isPrime(int x) { + if (x <= 1) return false; if (x <= 3) return true; + if (x % 2 == 0 || x % 3 == 0) return false; + for (int i = 5; i * i <= x; i = i + 6) if (x % i == 0 || x % (i + 2) == 0) return false; + return true; +} + +const int m=2e3+5; +int n; +int arr[m]; + +int dp[m][m][2]; + +int lis(int i,int prev,bool x) { + + if(i==n) return 0; + + int &ans = dp[i][prev][x]; + if(ans!=-1) return ans; + + ans =lis(i + 1 , prev , x); + if(x == 1) { + if (arr[i] > arr[prev]) { + ans = max(ans, 1+lis(i+1,i,x)); + } + } else { + if (arr[i] < arr[prev]) { + ans = max(ans, 1+lis(i+1,i,x)); + } + } + + return ans; +} + +void solveit() { + + cin>>n; + for (int i = 0; i < n; ++i) { + cin>>arr[i]; + } + + memset(dp,-1,sizeof dp); + + int ans = 0; + for(int i=0;i>t; + while (t--) { + solveit(); + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/11456/11456-13.cpp b/uva_cpp_clean/11456/11456-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10e295dcb638c8a2bbccf0ec22ea08b5eae0900a --- /dev/null +++ b/uva_cpp_clean/11456/11456-13.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 2010 + +int main() +{ + int t, n, m = 0; + + cin >> t; + + while (t--) + { + cin >> n; + + int ARY[MAX]; + int LIS[MAX]; + int LDS[MAX]; + + memset(LIS, 0, sizeof LIS); + memset(LDS, 0, sizeof LDS); + + for (int i = 0; i < n; i++) + { + cin >> ARY[i]; + } + + LIS[0] = LDS[0] = 1; + + reverse(ARY, ARY + n); + + for (int i = 0; i < n; i++) + { + LIS[i] = LDS[i] = 1; + + for (int j = 0; j < i; j++) + { + if (ARY[i] > ARY[j] && LIS[i] < LIS[j] + 1) + { + LIS[i] = LIS[j] + 1; + } + + if (ARY[i] < ARY[j] && LDS[i] < LDS[j] + 1) + { + LDS[i] = LDS[j] + 1; + } + } + } + + m = 0; + + for (int i = 0; i < n; i++) + { + m = max(m, LIS[i] + LDS[i] - 1); + } + + cout << m << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11456/11456-21.cpp b/uva_cpp_clean/11456/11456-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7655515d8b00f55d2a87f4d70903d2b8807d3967 --- /dev/null +++ b/uva_cpp_clean/11456/11456-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11456 + Name: Trainsorting + Problem: https://onlinejudge.org/external/114/11456.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int X[2086], I[2086], D[2086], M[2086], n; +int LIS(bool comp(int, int), int R[]) { + int L = 0; + for (int i=0; i>1; + if (comp(X[M[mid]], X[i])) + lo = mid + 1; + else + hi = mid - 1; + } + + M[R[i] = lo] = i; + L = max(L, lo); + } + return L; +} + +bool isLt(int a, int b) { return a < b; } +bool isGt(int a, int b) { return a > b; } +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + while (T--) { + cin >> n; + for (int i=0; i> X[n-i-1]; + + LIS(isLt, I); + LIS(isGt, D); + + int mx = 0; + for (int i=0; i longest increasing subsequence (LIS) + difficulty: medium + date: 01/Mar/2020 + hint: find the max(lis[i]+lds[i]-1) for all i in [0 .. N-1], being i where the subsequence starts + by: @brpapa +*/ +#include +#include +using namespace std; +#define MAX_N 2002 + +vector lis; // lis[i] = tamanho da maior lis começando em A[i] +vector lds; // lds[i] = tamanho da maior lds começando em A[i] + +int main() { + int T; cin >> T; + while (T--) { + int N; cin >> N; lis.assign(N, 1); lds.assign(N, 1); + vector A(N); for (int &a : A) cin >> a; + + for (int i = N-1; i >= 0; i--) + for (int j = i+1; j < N; j++) + if (A[i] < A[j] && lis[j]+1 > lis[i]) + lis[i] = lis[j]+1; + + for (int i = N-1; i >= 0; i--) + for (int j = i+1; j < N; j++) + if (A[i] > A[j] && lds[j]+1 > lds[i]) + lds[i] = lds[j]+1; + + int ans = 0; + for (int i = 0; i < N; i++) ans = max(ans, lis[i]+lds[i]-1); + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11456/11456-9.cpp b/uva_cpp_clean/11456/11456-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..788707eeba7385dffa43b679d4a96cb8786daa5e --- /dev/null +++ b/uva_cpp_clean/11456/11456-9.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int tc, n, ans; +vector v, lis, lds; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + v.resize(n), lis.resize(n), lds.resize(n); + ans = 0; + for(int i = 0; i < n; i++) scanf("%d", &v[i]), lis[i] = lds[i] = 1; + for(int i = n - 1; i >= 0; i--){ + for(int j = i + 1; j < n; j++){ + if(v[i] > v[j] && lis[i] < lis[j] + 1) + lis[i] = lis[j] + 1; + if(v[i] < v[j] && lds[i] < lds[j] + 1) + lds[i] = lds[j] + 1; + } + ans = max(ans, lis[i] + lds[i] - 1); + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11459/11459-19.cpp b/uva_cpp_clean/11459/11459-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a94845f02f0ff791b894fa612cb14f29438ed2a --- /dev/null +++ b/uva_cpp_clean/11459/11459-19.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +using namespace std; + +int main() { + int cases, a, b, c; + cin >> cases; + while (cases--) { + unordered_map pos; + unordered_map ls; + cin >> a >> b >> c; + for (int i = 0; i < b; i++) { + int f, s; + cin >> f >> s; + ls[f] = s; + } + bool end = false; + for (int i = 0, player = 0; i < c; i++, player = (player + 1) % a) { + int dice; + cin >> dice; + if (!end) { + pos[player] = pos[player] + dice; + if (ls[pos[player] + 1]) pos[player] = ls[pos[player] + 1] - 1; + if (pos[player] >= 99) { + pos[player] = 99; end = true; + } + } + } + for (int i = 0; i < a; i++) { + printf("Position of player %d is %d.\n", i+1, pos[i]+1); + } + } + return 0; +} diff --git a/uva_cpp_clean/11459/11459-21.cpp b/uva_cpp_clean/11459/11459-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..959b5b7460da8bfce48e61551e1b327626b60231 --- /dev/null +++ b/uva_cpp_clean/11459/11459-21.cpp @@ -0,0 +1,94 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11459 + Name: Snakes and Ladders + Problem: https://onlinejudge.org/external/114/11459.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------ + +char BUF[8000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 8000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int P[1000143]; + int T = readUInt(); + while (T--) { + int p = readUInt(), + m = readUInt(), + n = readUInt(); + + for (int i=0; i= 100) { + P[k] = 100; + fin = 1; + } + + if (++k == p) k = 0; + } + + for (int i=0; i + +#define SIZE 110 +#define LIM 1000010 + +using namespace std; + +int tc, a, b, c, x, y, turn, num, v[SIZE], player[LIM]; +bool completed; + +int main(){ + scanf("%d", &tc); + while(tc--){ + memset(v, 0, sizeof v); + scanf("%d %d %d", &a, &b, &c); + while(b--){ + scanf("%d %d", &x, &y); + v[x] = y; + } + turn = 1, completed = false; + fill(player, player + a + 5, 1); + while(c--){ + if( turn == a + 1 ) turn = 1; + scanf("%d", &num); + if( completed ) continue; + player[turn] += num; + if( player[turn] > 100 ) player[turn] = 100; + if( v[player[turn]] ) player[turn] = v[player[turn]]; + if( player[turn] == 100 ) completed = true; + turn++; + } + for(int it = 1; it <= a; it++) + printf("Position of player %d is %d.\n", it, player[it]); + } + return(0); +} diff --git a/uva_cpp_clean/11461/11461-18.cpp b/uva_cpp_clean/11461/11461-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5745e8223804c6a025c17ae0835005ee4a3adf8b --- /dev/null +++ b/uva_cpp_clean/11461/11461-18.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +int main(){ + int a, b, cont; + double square; + while(scanf("%d %d", &a, &b) && a != 0 && b != 0){ + square = a; + cont = 0; + while(square <= b){ + if(sqrt(square) == (int)sqrt(square)) cont++; + square++; + } + printf("%d\n", cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11461/11461-19.cpp b/uva_cpp_clean/11461/11461-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd1903d63128123381fba0bca3efc45282ba0f3f --- /dev/null +++ b/uva_cpp_clean/11461/11461-19.cpp @@ -0,0 +1,13 @@ +#include +#include +using namespace std; + +int main() { + int a, b; + while (scanf("%d %d", &a, &b) && !(a == 0 && b == 0)) { + a = ceil(sqrt(a)); + b = floor(sqrt(b)); + printf("%d\n", b-a+1); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11461/11461-21.cpp b/uva_cpp_clean/11461/11461-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf1d70281d8884847bb4b794fb3cbd6c4bd7bc1b --- /dev/null +++ b/uva_cpp_clean/11461/11461-21.cpp @@ -0,0 +1,26 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11461 + Name: Square Numbers + Problem: https://onlinejudge.org/external/114/11461.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int cum[100001] = { 1, 2 }; +int main() { + for (int i = 1, j = 0, c = 0; j < 100001; i++) + for (c++; j < i*i && j < 100001; j++) + cum[j] = c; + + int a, b; + while ((cin >> a >> b) && a) + cout << cum[b] - cum[a - 1] << endl; +} \ No newline at end of file diff --git a/uva_cpp_clean/11461/11461-5.cpp b/uva_cpp_clean/11461/11461-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ac22565fad5c3cdbe4f8342b789b1345bfa57acb --- /dev/null +++ b/uva_cpp_clean/11461/11461-5.cpp @@ -0,0 +1,20 @@ +#include +#include +int main() +{ + int t,a,b,c,i,d; + while(scanf("%d %d", &a, &b)==2) + { + if(a==0&&b==0) + break; + d=0; + for(i=a;i<=b;i++) + { + c=sqrt(i); + if(c*c==i) + d++; + } + printf("%d\n", d); + } +return 0; +} diff --git a/uva_cpp_clean/11462/11462-11.cpp b/uva_cpp_clean/11462/11462-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..424b3a4833b296d05cea1a528f1ec30ad7101f12 --- /dev/null +++ b/uva_cpp_clean/11462/11462-11.cpp @@ -0,0 +1,70 @@ +#include +/* +Problem: 11462 - Age Sort +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2457 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int N, age, age_freq[2000002]; +set ages; +set ::iterator it; + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + while ( cin >> N && N ){ + int left = N; // numbers left to print + + for (int i = 0; i < N; i++){ + cin >> age; + age_freq[age]++; + ages.insert(age); + } + + it = ages.begin(); + while (it != ages.end()){ + while (age_freq[*it]){ + cout << *it; + cout << ( (left == 1)? "" : " "); + age_freq[*it]--; + left--; + } + it++; + } + + cout << '\n'; + ages.clear(); + } + + return 0; +} +/* +Sample input:- +----------------- +5 +3 4 2 1 5 +5 +2 3 2 3 1 +0 + +Sample output:- +----------------- +1 2 3 4 5 +1 2 2 3 3 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11462/11462-14.cpp b/uva_cpp_clean/11462/11462-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2c076452aa7ae687bc446c578b56ffeb0b7d2f4f --- /dev/null +++ b/uva_cpp_clean/11462/11462-14.cpp @@ -0,0 +1,54 @@ +#include +#include +#define MAX 2000000 +using namespace std; +typedef long long ll; +ll ar[MAX]; +ll total_node; +void max_heapify(int par) { + ll L = 2*par; + ll R = 2*par + 1; + if(R<=total_node) { + if(ar[R]>=ar[L] && ar[R]>ar[par]) { + swap(ar[par],ar[R]); + max_heapify(R); + } + else if(ar[L]>=ar[R] && ar[L]>ar[par]) { + swap(ar[par],ar[L]); + max_heapify(L); + } + } + else if(L<=total_node) { + if(ar[L]>ar[par]) { + swap(ar[par],ar[L]); + max_heapify(L); + } + } +} +void heapSort(void) { + for(ll i = total_node; i>=1; i--) { + swap(ar[1],ar[i]); + total_node--; + max_heapify(1); + } +} +int main() +{ + while(scanf("%lld", &total_node) == 1 && total_node!=0) { + + ll n = total_node; + for(ll i = 1; i<=total_node; i++) { + scanf("%lld", &ar[i]); + } + for(ll i = total_node/2; i>=1; i--) { + max_heapify(i); + } + heapSort(); + for(ll i = 1; i<=n; i++) { + if(i == n) printf("%lld\n",ar[i]); + else printf("%lld ", ar[i]); + } + } + return 0; +} + diff --git a/uva_cpp_clean/11462/11462-17.cpp b/uva_cpp_clean/11462/11462-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01592bb89a771659e033b5a80f9befc49a1f6364 --- /dev/null +++ b/uva_cpp_clean/11462/11462-17.cpp @@ -0,0 +1,32 @@ +/** + * Age Sort + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n; + while ((cin >> n) && n) + { + array ages {0}; + while (n--) + { + int age; + cin >> age; + ++ages[age]; + } + bool first = true; + for (int i = 1; i < 100; ++i) + for (int cnt = 0; cnt < ages[i]; ++cnt) + { + if (first) + first = false; + else + cout << " "; + cout << i; + } + cout << "\n"; + } +} diff --git a/uva_cpp_clean/11462/11462-18.cpp b/uva_cpp_clean/11462/11462-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..87e478c0a4231c23328f8fa6ae66634814140634 --- /dev/null +++ b/uva_cpp_clean/11462/11462-18.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +int main(){ + int n, numero; + while(true){ + cin >> n; + int personas[n]; + if(n == 0) break; + for(int i = 0; i < n; i++){ + cin >>personas[i]; + } + sort(personas, personas+n); + for(int i = 0; i < n; i++){ + if(i != n - 1){ + cout << personas[i] << " "; + } else cout << personas[i]; + } + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11462/11462-19.cpp b/uva_cpp_clean/11462/11462-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00a3a34b63908a3cd95d54bac44aabf66f4e4227 --- /dev/null +++ b/uva_cpp_clean/11462/11462-19.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main() { + int ages[101]; + for (int i = 0; i < 101; i++) ages[i] = 0; + int n; + while (scanf("%d", &n) && n != 0) { + for (int i = 0; i < n; i++) { + int age; + scanf("%d", &age); + ages[age]++; + } + bool first = true; + for (int i = 0; i < 101; i++) + for (; ages[i] > 0; ages[i]--) { + if (!first) printf(" "); + printf("%d", i); + first = false; + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11462/11462-21.cpp b/uva_cpp_clean/11462/11462-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..054a8c3b58a82927a5ff526f323356a0ee4cd742 --- /dev/null +++ b/uva_cpp_clean/11462/11462-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11462 + Name: Age Sort + Problem: https://onlinejudge.org/external/114/11462.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int cnts[101]; +int main(){ + int n; + while(cin>>n && n) { + for (int i=0; i +#include + +using namespace std; +long long a[2000005],i,n; + +int main() +{ + while (scanf("%lld",&n)==1) + { + if (n==0) + break; + + for (i=0;i + +using namespace std; + +const int SIZE = 105; + +int n, mx, x, ct[SIZE]; + +int main(){ + while(scanf("%d", &n), n){ + mx = 0; + for(int it = 0; it < n; it++){ + scanf("%d", &x); + ct[x]++; + if( x > mx ) mx = x; + } + for(int it = 1; it <= mx; it++){ + for(int j = 0; j < ct[it]; j++) + printf( it == mx and j == ct[it] - 1 ? "%d\n" : "%d ", it); + ct[it] = 0; + } + } + return(0); +} diff --git a/uva_cpp_clean/11463/11463-13.cpp b/uva_cpp_clean/11463/11463-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f2cf482b2f170aecb8100e8b1562a26d2fa808e --- /dev/null +++ b/uva_cpp_clean/11463/11463-13.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 105 +#define INF 1E8+7 + +int town[MAX][MAX]; + +int main() +{ + int test, t = 1; + + cin >> test; + + while (test--) + { + int n, r; + + cin >> n >> r; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + town[i][j] = (i == j) ? 0 : INF; + } + } + + for (int i = 0; i < r; i++) + { + int x, y; + + cin >> x >> y; + + town[y][x] = town[x][y] = 1; + } + + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + town[i][j] = min(town[i][j], town[i][k] + town[k][j]); + } + } + } + + int startPoint, endPoint; + + cin >> startPoint >> endPoint; + + int ans = 0; + + for (int i = 0; i < n; i++) + { + if (town[startPoint][i] != INF && town[i][endPoint] != INF) + { + ans = max(town[startPoint][i] + town[i][endPoint], ans); + } + } + + cout << "Case " << t++ << ": " << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11463/11463-14.cpp b/uva_cpp_clean/11463/11463-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..acbc7b9db3beca5f58a2c970ea2724601bde0ce0 --- /dev/null +++ b/uva_cpp_clean/11463/11463-14.cpp @@ -0,0 +1,97 @@ +/*************************************************** + * Problem name : 11463 - Commandos(DFS).cpp + * OJ : Uva + * Result : Accepted + * Date : 17-04-17 + * Problem Type : Graph(DFS) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 105 +using namespace std; +typedef long long ll; +vectoradj[MAX]; +int cost[MAX], flag; +void dfs(int u) { + if (flag == 0) { + cost[u] = 0; + flag = 1; + } + int sz = adj[u].size(); + for (int i = 0; i < sz; i++) { + int v = adj[u][i]; + if (cost[u] + 1 < cost[v]) { + cost[v] = cost[u] + 1; + dfs(v); + } + } + return ; +} +void mem() { + for (int i = 0; i < MAX; i++) { + cost[i] = 1000000; + } +} +int main() { + int n, e, tc, sum; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + sum = 0; + scanf("%d %d", &n, &e); + for (int i = 1; i <= e; i++) { + int x, y; + scanf("%d %d", &x, &y); + adj[x].push_back(y); + adj[y].push_back(x); + } + int s, d; + scanf("%d %d", &s, &d); + flag = 0; + mem(); + dfs(s); + int max = 0, indx; + for (int i = 0; i < n; i++) { + if (max <= cost[i] && i != d) { + max = cost[i]; + indx = i; + } + } + int ar[MAX], cnt = 0; + int a = cost[indx]; + for (int i = 0; i < n; i++) { + if (a == cost[i] && i != d) { + ar[cnt++] = i; + } + } + max = 0; + for (int i = 0 ; i < cnt; i++) { + flag = 0; + mem(); + dfs(ar[i]); + if (max <= cost[d]) { + max = cost[d]; + } + } + sum = a + max; + printf("Case %d: %d\n", t, sum); + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + cost[i] = 0; + } + } + return 0; +} + diff --git a/uva_cpp_clean/11463/11463-19.cpp b/uva_cpp_clean/11463/11463-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5b02a47c6c8061a691323f0360080ad66d5b84b --- /dev/null +++ b/uva_cpp_clean/11463/11463-19.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef pair ii; +typedef vector vii; + +vector AdjList; + +void dijkstra(int s, vi & dist) { + dist[s] = 0; + priority_queue, greater > pq; + pq.push(ii(0,s)); + while(!pq.empty()){ + ii front=pq.top(); pq.pop(); + int d=front.first, u=front.second; + if (d > dist[u]) continue; + for(int j=0; j< (int)AdjList[u].size(); j++){ + ii v = AdjList[u][j]; + if (dist[u] + v.second < dist[v.first]){ + dist[v.first] = dist[u] + v.second; + pq.push(ii(dist[v.first],v.first)); +}}}} + +int main() { + int T; + cin >> T; + for (int i = 1; i <= T; i++) { + int N, R; + cin >> N >> R; + AdjList.assign(N, vii()); + while (R--) { + int u, v; + cin >> u >> v; + AdjList[u].push_back(make_pair(v, 1)); + AdjList[v].push_back(make_pair(u, 1)); + } + int s, d; + cin >> s >> d; + vi d1(N, INT_MAX), d2(N, INT_MAX); + dijkstra(s, d1); dijkstra(d, d2); + int time = 0; + for (int j = 0; j < N; j++) { + if (d1[j] + d2[j] > time) time = d1[j] + d2[j]; + } + printf("Case %d: %d\n", i, time); + } + return 0; +} diff --git a/uva_cpp_clean/11463/11463-21.cpp b/uva_cpp_clean/11463/11463-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c0025c96f520006135b474a6f54e7f829db7afc --- /dev/null +++ b/uva_cpp_clean/11463/11463-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11463 + Name: Commandos + Problem: https://onlinejudge.org/external/114/11463.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + + +int D1[143], D2[143]; +vector adj[143]; +void bfs(int n, int src, int D[]) { + for (int i=0; i q; + q.push(src); D[src] = 0; + while (!q.empty()) { + int u = q.front(); q.pop(); + for (int v: adj[u]) + if (D[v] == INF) { + q.push(v); + D[v] = D[u] + 1; + } + } +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m, u, v; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n >> m; + for (int i=0; i> u >> v; + adj[u].push_back(v); + adj[v].push_back(u); + } + + cin >> u >> v; + bfs(n, u, D1); + bfs(n, v, D2); + + int mxd = 0; + for (int i=0; i + +#define MAX 9999 + +int main() +{ + int tests, caseNr = 0; + std::cin >> tests; + + while (tests--) + { + int n, r; + std::cin >> n >> r; + + int *tab = new int[n * n]; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i != j) + { + tab[i * n + j] = MAX; + } + else + { + tab[i * n + j] = 0; + } + } + } + + for (int i = 0; i < r; i++) + { + int u, v; + std::cin >> u >> v; + + tab[u * n + v] = 1; + tab[v * n + u] = 1; + } + + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + tab[i * n + j] = std::min(tab[i * n + j], tab[i * n + k] + tab[k * n + j]); + } + } + } + + int start, end; + std::cin >> start >> end; + + int time = 0; + for (int i = 0; i < n; i++) + { + time = std::max(time, tab[start * n + i] + tab[i * n + end]); + } + + std::cout << "Case " << ++caseNr << ": " << time << std::endl; + delete[] tab; + } +} diff --git a/uva_cpp_clean/11463/11463-5.cpp b/uva_cpp_clean/11463/11463-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..29e3a0e6f966cae7c73db53b804a38e1c412d7f5 --- /dev/null +++ b/uva_cpp_clean/11463/11463-5.cpp @@ -0,0 +1,214 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +//help http://acm.hust.edu.cn/vjudge/problem/viewSource.action?id=2080594 + +vector g[1000]; +int vis[1000],level[1000]; + +int bfs(int s, int d) +{ + if(s==d) + return 0; + queueq; + + cover(vis,0); + cover(level,0); + + q.push(s); + + vis[s]=1; + level[s]=0; + + while(!q.empty()) + { + int p=q.front(); + q.pop(); + + int len=g[p].size(); + for0(i,len) + { + int node=g[p][i]; + if(vis[node]==0) + { + level[node]=level[p]+1; + vis[node]=1; + + q.push(node); + + if(node==d) + return level[node]; + } + } + } + return 0; +} + +int main() +{ + int t,no=0; + si(t); + while(t--) + { + int n,e; + sii(n,e); + + int res=0; + + for0(i,e) + { + int a,b; + sii(a,b); + g[a].pb(b); + g[b].pb(a); + } + + int s,d; + sii(s,d); + + for0(i,n) + { + int r1=bfs(s,i); + int r2=bfs(i,d); + res=max(res,r1+r2); + } + + Case(no); + cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11464 + Name: Even Parity + Problem: https://onlinejudge.org/external/114/11464.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int X[16][16], M[16][16], n; +void click(int i, int j) { + if (i) M[i-1][j] = !M[i-1][j]; + if (j) M[i][j-1] = !M[i][j-1]; + if (i= best) return; + if (i && j && M[i-1][j-1]) return; + if (j>1 && M[i][j-2] && X[i+1][j-2]) return; + if (j == n) { + if (j && M[i][j-1] && X[i+1][j-1]) return; + ++i, j=0; + } + + if (i < n) { + backtrack(i, j+1, step); + if (!X[i][j]) { + click(i, j); + backtrack(i, j+1, step+1); + click(i, j); + } + return; + } + + best = step; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + for (int cse=1; T-- && cin >> n; ++cse) { + memset(M, 0, sizeof(M)); + for (int i=0; i> X[i][j]; + if (X[i][j]) + click(i, j); + } + X[n][i] = 1; + } + + best = 616; + backtrack(0, 0, 0); + cout << "Case " << cse << ": " << (best==616 ? -1 : best) << '\n'; + } +} diff --git a/uva_cpp_clean/11464/11464-9.cpp b/uva_cpp_clean/11464/11464-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1a871adc6e78d9c6eac14ec74a389d21b8c15ef --- /dev/null +++ b/uva_cpp_clean/11464/11464-9.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +const int MAX_N = 20; + +int tc, n, changes, ans; +bool grid[MAX_N][MAX_N]; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +inline bool isValid(int r, int c) { + return (0 <= r and 0 <= c and r < n and c < n); +} + +bool evenParity(int r, int c) { + int ct = 0; + for (int d = 0; d < 4; d++) { + int nr = r + dr[d], nc = c + dc[d]; + ct += (isValid(nr, nc) and grid[nr][nc]); + } + return not (ct & 1); +} + +void backtrack(int r = 0, int c = 0) { + if (r == n) { + ans = min(ans, changes); + return; + } + if (changes > ans) return; + if (grid[r][c] == false) { + grid[r][c] = true, changes++; + if (not isValid(r - 1, c) or evenParity(r - 1, c)) + backtrack(c + 1 == n ? r + 1 : r, c + 1 == n ? 0 : c + 1); + grid[r][c] = false, changes--; + } + if (not isValid(r - 1, c) or evenParity(r - 1, c)) + backtrack(c + 1 == n ? r + 1 : r, c + 1 == n ? 0 : c + 1); +} + +void init() { + ans = n * n; +} + +void read() { + scanf("%d", &n); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + scanf("%d", &grid[i][j]); +} + +int main() { + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + read(); + init(); + backtrack(); + printf("Case %d: %d\n", t, ans == n * n ? -1 : ans); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/11466/11466-13.cpp b/uva_cpp_clean/11466/11466-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca41556171f60e13876d7d74429e6aa0c8a74029 --- /dev/null +++ b/uva_cpp_clean/11466/11466-13.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 20000001 + +typedef long long ll; +typedef vector vll; +typedef vector vb; + +ll n; +vll primes; +vb isPrime(MAX, true); + +void findPrime() +{ + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += i) + { + isPrime[j] = false; + } + + primes.push_back(i); + } + } +} + +ll findLargestFactor() +{ + int count = 0; + ll largestFactor = n, primes_index = 0, pf = primes[primes_index]; + + while (pf * pf <= n) + { + largestFactor = pf; + + if (n % pf == 0) + { + count++; + + while (n % pf == 0) + { + n /= pf; + } + } + + pf = primes[++primes_index]; + } + + if (n != 1) + { + count++; + largestFactor = n; + } + + return (count == 1) ? (ll)-1 : largestFactor; +} + +int main() +{ + findPrime(); + + while (cin >> n, n != 0) + { + n = abs(n); + + if (n == 1) cout << -1 << endl; + + else cout << findLargestFactor() << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11466/11466-19.cpp b/uva_cpp_clean/11466/11466-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d97c2a681626f55b16cb20ffae11e16bf63fe28 --- /dev/null +++ b/uva_cpp_clean/11466/11466-19.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair llll; +typedef vector vll; + +ll sieve_size; +bitset<100000001> bs; +vi primes; + +inline void sieve() { + sieve_size=100000001; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline llll primeFactors(ll n) { + llll factors; int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.first++, factors.second = p; + p=primes[++idx]; + } + if (n!=1) factors.first++, factors.second = n; + return factors; +} + + +int main() { + sieve(); + long long N; + llll pf; + while (scanf("%lld", &N) && N != 0) { + if (N < 0) N = -N; + if (N != 1) pf = primeFactors(N); + if (pf.first <= 1 || N == 1) printf("-1\n"); + else printf("%lld\n", pf.second); + } + return 0; +} diff --git a/uva_cpp_clean/11466/11466-20.cpp b/uva_cpp_clean/11466/11466-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55f0c812b4347991e7d679d3ec24932ffbe9cefa --- /dev/null +++ b/uva_cpp_clean/11466/11466-20.cpp @@ -0,0 +1,30 @@ +#include +#include + +int main() +{ + long long N; + + while( scanf("%lld", &N)!=EOF && N ) + { + if( N<0 ) N = -N; + + long long LPD = -1, div_counter = 0, temp = sqrt(N); + for(long long i=2 ; i<=temp && N!=1 ; i++) + { + while( N%i==0 ) + { + LPD = i; + N /= i; + } + + if( LPD==i ) div_counter++; + } + + if( N!=1 && LPD!=-1 ) LPD = N; + else if( div_counter==1 ) LPD = -1; + + printf("%lld\n", LPD); + } + return 0; +} diff --git a/uva_cpp_clean/11466/11466-21.cpp b/uva_cpp_clean/11466/11466-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f707b2a6e38c4e6bc209ebd1db3b7eb68feaa1cf --- /dev/null +++ b/uva_cpp_clean/11466/11466-21.cpp @@ -0,0 +1,96 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11466 + Name: Largest Prime Divisor + Problem: https://onlinejudge.org/external/114/11466.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +long long mod_mul(long long a, long long b, long long mod) { + long long x = 0, y = a % mod; + while (b > 0) { + if (b & 1) + x = (x + y) % mod; + y = (y * 2) % mod; + b /= 2; + } + return x % mod; +} +long long mod_pow(long long a, long long exp, long long mod) { + long long ans = 1; + while (exp > 0) { + if (exp & 1) + ans = mod_mul(ans, a, mod); + a = mod_mul(a, a, mod); + exp >>= 1; + } + return ans; +} +const int rounds = 20; +bool witness(long long a, long long n) { + long long u = n - 1; + int t = 0; + while (u % 2 == 0) { + t++; + u >>= 1; + } + long long next = mod_pow(a, u, n); + if (next == 1) return false; + long long last; + for (int i = 0; i < t; ++i) { + last = next; + next = mod_mul(last, last, n); + if (next == 1) { + return last != n - 1; + } + } + return next != 1; +} +bool miller_rabin(long long n, int it = rounds) { + if (n <= 1) return false; + if (n == 2) return true; + if (n % 2 == 0) return false; + for (int i = 0; i < it; ++i) { + long long a = rand() % (n - 1) + 1; + if (witness(a, n)) { + return false; + } + } + return true; +} + +// ---------------------------------------------------------------------- + +bool np[10000143] = {1, 1}; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long n, r; + while (cin >> n && n) { + if (n < 0) n = -n; + int sq = sqrt(n), c=0; + if (!miller_rabin(n)) { + for (int i=2; i<=sq; ++i) + if (n%i == 0) { + while (n%i == 0) + n /= i; + r = i; + ++c; + if (miller_rabin(n)) break; + sq = sqrt(n); + } + + if (n > 1) ++c; + if (n > r) r = n; + } + + cout << (c>1 ? r : -1) << '\n'; + } +} diff --git a/uva_cpp_clean/11466/11466-5.cpp b/uva_cpp_clean/11466/11466-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a3e90179663f5689b4627a80c2d173bd4f98c9c2 --- /dev/null +++ b/uva_cpp_clean/11466/11466-5.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define Max 10001000 +#define st(s) s.size(); +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +using namespace std; + +bool prime[Max]; +ll p[1000000],k=0; +void sieve() +{ + ll i,j; + prime[1]=false; + for(i=2;i<=10001000;i++) + { + if(prime[i]!=false) + { + p[k++]=i; + for(j=i+i;j<=10001000;j+=i) + { + prime[j]=false; + } + } + } +} +int main() +{ + cover(prime,true); + sieve(); + ll n,i,j,a; + int c; + while(scanf("%lld",&n)==1) + { + if(n==0) break; + if(n<0) n*=-1; + for(i=0,c=0;i1&&p[i]<=n;i++) + { + if(n%p[i]==0) + { + //pf("%lld\n",p[i]); + c++; + while(n>1 && n%p[i]==0) + { + n/=p[i]; + } + a=p[i]; + } + if(n==1) break; + } + if(n==1) + { + if(c>1) printf("%lld\n",a); + else printf("-1\n"); + } + else + { + if(c>0) printf("%lld\n",n); + else printf("-1\n"); + } + } +} + +/*input + 1000 + 20 + 32 + 1 + -1 + -10 + 61536575712 + 8172385155 + 90090 + 12 + 199900 + -26356 + -32 + 8748234 + 23462482 + 23457826407 + 234872648001 + 436598 + 345387 + 2347 + 17 + 37 + 0 + +output: + 5 + 5 + -1 + -1 + -1 + 5 + 324889 + 16509869 + 13 + 3 + 1999 + 599 + -1 + 113 + 690073 + 77418569 + 348559 + 521 + 16447 + -1 + -1 + -1 +*/ diff --git a/uva_cpp_clean/11466/11466-9.cpp b/uva_cpp_clean/11466/11466-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ad71c42b1b7fe721d87295be6db4be4788e4bd7 --- /dev/null +++ b/uva_cpp_clean/11466/11466-9.cpp @@ -0,0 +1,42 @@ +#include + +#define SIZE 10000010 + +using namespace std; + +long long n, ans; +int ct; +bool is_prime[SIZE]; +vector prime; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++) + if( is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +long long LargestPrimeFactor(long long N){ + N = abs( N ); + ct = ans = 0; + for(int it = 0; it < prime.size() and prime[it] * prime[it] <= N and N != 1; it++) + if( N % prime[it] == 0 ){ + ans = prime[it]; + ct++; + while ( N % prime[it] == 0 ) N /= prime[it]; + } + if( N != 1) ans = N, ct += 1; + if( ans == 0 or ct == 1) return -1; + return ans; +} + +int main(){ + sieve(); + while(scanf("%lld", &n), n) + printf("%lld\n", LargestPrimeFactor(n)); + return(0); +} diff --git a/uva_cpp_clean/11470/11470-18.cpp b/uva_cpp_clean/11470/11470-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1a3fc276a1634036e5c5e570e42f78b5491ab3be --- /dev/null +++ b/uva_cpp_clean/11470/11470-18.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + +int main(){ + double n, grid[11][11]; + int tc = 0; + while(true){ + cin >> n; + if(n == 0) break; + printf("Case %d: ", tc+1); + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++) cin >> grid[i][j]; + } + int i = 0, j = 0, iF = n, jF = n, sum; + for(int s = 0; s < ceil(n/2); s++){ + sum = 0; + if(i == iF - 1 && j == jF - 1){ + cout << grid[i][j] << endl; + break; + } + for(int k = j; k < jF; k++) sum += grid[i][k]; + for(int k = j; k < jF; k++) sum += grid[iF - 1][k]; + for(int k = i + 1; k < iF - 1; k++) sum += grid[k][j]; + for(int k = i + 1; k < iF - 1; k++) sum += grid[k][jF - 1]; + i++; + iF--; + j++; + jF--; + cout << sum; + if(s != ceil(n/2) - 1) cout << " "; + else cout << endl; + } + tc++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11470/11470-19.cpp b/uva_cpp_clean/11470/11470-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f14c90a40b49fa9a16c46301207238cffd56d668 --- /dev/null +++ b/uva_cpp_clean/11470/11470-19.cpp @@ -0,0 +1,69 @@ +// las12uvas contest PROBLEM D 2015 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector
vdd; +typedef vector vll; +typedef vector vvi; + +bool proc(int mat[11][11], int s, int dim, int & sum) { + if(s > dim/2) return false; + if( dim % 2 == 0 && s == dim/2) return false; + sum = 0; + if(s == dim/2 && dim%2 == 1) {sum = mat[s][s]; return true;} + for(int i = s; i < dim - s; i++) {sum += mat[i][s] + mat[i][dim-s-1];} + for(int i = s + 1; i < dim-s-1; i++) {sum += mat[s][i] + mat[dim-s-1][i];} + return true; +} + +int main() { + int vec[11]; + int mat[11][11]; + int cases = 1; + int n; + while (scanf("%d", &n) && n!=0) { + for (int i = 0; i < 11; i++) vec[i] = 0; + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) scanf("%d", &mat[i][j]); + + printf("Case %d:", cases++); + for (int i = 0; i < n/2 + 1; i++) { + int sum; + bool ok = proc(mat, i, n, sum); + if (ok) printf(" %d", sum); + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11470/11470-21.cpp b/uva_cpp_clean/11470/11470-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a25d4c1931aaaf6ef1a2b72d067150656e8ab73 --- /dev/null +++ b/uva_cpp_clean/11470/11470-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11470 + Name: Square Sums + Problem: https://onlinejudge.org/external/114/11470.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int M[20][20]; + for (int cse=1, n; cin >> n && n; ++cse) { + cout << "Case " << cse << ":"; + + for (int i=0; i> M[i][j]; + + for (int i=0; i<=(n-1)>>1; ++i) { + int sum = 0, e = n-i-1; + + if (i == e) sum = M[i][i]; + else + for (int j=i; j<=e; ++j) + sum += M[i][j] + M[e][j]; + + for (int j=i+1; j + +#define SIZE 15 +#define LIM 110 + +using namespace std; + +int tc, n, lim, grid[SIZE][SIZE], dis[LIM], ans[SIZE]; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < n and C < n ); +} + +void bfs(){ + fill(ans, ans + SIZE, 0); + fill(dis, dis + LIM, -1); + queue < int > Q; + dis[ n / 2 * n + n / 2 ] = 1; + Q.push( n / 2 * n + n / 2 ); + if( n % 2 == 0 ) + for(int d = 2; d < 5; d++){ + int u = ( n / 2 + dr[d] ) * n + n / 2 + dc[d]; + dis[u] = 1; + Q.push(u); + } + while( not Q.empty() ){ + int u = Q.front(); Q.pop(); + int r = u / n, c = u % n; + ans[ dis[u] ] += grid[r][c]; + for(int d = 0; d < 8; d++){ + int row = r + dr[d], col = c + dc[d], v = row * n + col; + if( not is_possible(row, col) ) continue; + if( dis[v] == -1 ) + dis[v] = dis[u] + 1, Q.push(v); + } + } +} + +int main(){ + while(scanf("%d", &n), n){ + for(int row = 0; row < n; row++) + for(int col = 0; col < n; col++) + scanf("%d", &grid[row][col]); + bfs(); + printf("Case %d: ", ++tc); + for(int it = ( n + 1 ) / 2; it >= 1; it--) + printf( it != 1 ? "%d " : "%d\n", ans[it]); + } + return(0); +} diff --git a/uva_cpp_clean/11471/11471-21.cpp b/uva_cpp_clean/11471/11471-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a18640e18ade1d174b5c7b73538f29265ef7cfa4 --- /dev/null +++ b/uva_cpp_clean/11471/11471-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11471 + Name: Arrange the Tiles + Problem: https://onlinejudge.org/external/114/11471.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define TOP 0 +#define RIGHT 1 +#define BOTTOM 2 +#define LEFT 3 + + +string S[12]; +int C[12], sz, M[4][3]; +int brutecount(int i, int j) { + if (j == 3) ++i, j=0; + if (i == 4) return 1; + + int sum = 0; + for (int k=0; k0 && (!i || S[M[i-1][j]][BOTTOM] == S[k][TOP]) && (!j || S[M[i][j-1]][RIGHT] == S[k][LEFT])) { + M[i][j] = k; + sum += (C[k]--) * brutecount(i, j+1); + ++C[k]; + } + return sum; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + for (int i=0; i<12; ++i) + cin >> S[i]; + sort(S, S+12); + + C[0] = 1; sz = 1; + for (int i=1; i<12; ++i) + if (S[i] == S[sz-1]) + C[sz-1]++; + else { + S[sz] = S[i]; + C[sz++] = 1; + } + + cout << "Case " << cse << ": " << brutecount(0,0) << '\n'; + } +} diff --git a/uva_cpp_clean/11471/11471-9.cpp b/uva_cpp_clean/11471/11471-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd7ce1c556ab75d30dccabc5aa187bcd3358a7be --- /dev/null +++ b/uva_cpp_clean/11471/11471-9.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +const int SIZE = 12, MAX_N = 4, MAX_M = 3; + +int tc, t, ans; +string s[SIZE], grid[MAX_N][MAX_M]; +map ct; + +void print() { + printf("Case %d: %d\n", ++t, ans); +} + +inline bool isValid(int r, int c) { + return (0 <= r and 0 <= c and r < MAX_N and c < MAX_M); +} + +bool check(int r, int c, string ss) { + if (isValid(r, c - 1) and grid[r][c - 1][1] != ss[3]) return false; + if (isValid(r - 1, c) and grid[r - 1][c][2] != ss[0]) return false; + return true; +} + +void backtrack(int r = 0, int c = 0, int ret = 1) { + if (r == MAX_N) { + ans += ret; + return; + } + for (map :: iterator it = ct.begin(); it != ct.end(); ++it) + if (it -> second and check(r, c, it -> first)) { + int p = it -> second; + grid[r][c] = it -> first; + ct[it -> first]--; + backtrack(c + 1 == MAX_M ? r + 1 : r, c + 1 == MAX_M ? 0 : c + 1, ret * p); + grid[r][c] = ""; + ct[it -> first]++; + } +} + +void solve() { + for (int i = 0; i < SIZE; i++) ct[s[i]]++; + backtrack(); +} + +void read() { + for (int i = 0; i < SIZE; i++) cin >> s[i]; +} + +void init() { + ans = 0; + ct.clear(); + fill(grid[0], grid[MAX_N], ""); +} + +int main() { + scanf("%d\n", &tc); + while(tc--) { + init(); + read(); + solve(); + print(); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/11472/11472-14.cpp b/uva_cpp_clean/11472/11472-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbd201ed2a1cf0145433f528d2b12a0e7419c388 --- /dev/null +++ b/uva_cpp_clean/11472/11472-14.cpp @@ -0,0 +1,115 @@ +/*************************************************** + * Problem Name : 11472 - Beautiful Numbers.cpp + * Problem Link : https://uva.onlinejudge.org/external/114/11472.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : Digit DP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int n, m; +ll dp[103][11][1030]; +vectorvec; +ll digitDP (int pos, int pre, int mask) { + if (pos == m) { + return 0LL; + } + + ll &ans = dp[pos][pre][mask]; + + if (ans != -1) return ans; + + ans = 0; + int high = n - 1; + + for (int i = 0; i <= high; i++) { + if (abs (pre - i) == 1 || (pos == 0 && i != 0) ) { + int new_mask = mask; + new_mask = new_mask | (1 << i); + int cnt = __builtin_popcount (new_mask); + + if (cnt == n) { + ans = (ans + (1LL + digitDP (pos + 1, i, new_mask) ) ) % MOD; + + } else { + ans = (ans + digitDP (pos + 1, i, new_mask) ) % MOD; + } + } + } + + return ans; +} + +ll solve () { + SET (dp, -1); + ll ans = digitDP (0, 0, 0); + return ans; +} +int main () { + __FastIO; + int tc; + cin >> tc; + + while (tc--) { + cin >> n >> m; + ll ans = solve (); + cout << ans << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/11475/11475-19.cpp b/uva_cpp_clean/11475/11475-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c907fb982282586dd65a2ee698d8d6c1ac38c57 --- /dev/null +++ b/uva_cpp_clean/11475/11475-19.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main() { + string word; + while (cin >> word) { + int j = word.length() - 1, match = 0; + for (int i = 0; i < j; i++) { + if (word[i] == word[j]) j--; + else i = match, j = word.length() - 1, match++; + } + cout << word; + for (int i = match - 1; i >= 0; i--) cout << word[i]; + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11475/11475-21.cpp b/uva_cpp_clean/11475/11475-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b891186504124d2795e8e10c0162ea18739dd2c0 --- /dev/null +++ b/uva_cpp_clean/11475/11475-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11475 + Name: Extend to Palindrome + Problem: https://onlinejudge.org/external/114/11475.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool isPal(char s[], int l) { + for (int i=0, hl=l/2; i<=hl; ++i) + if (s[i] != s[l-i-1]) + return false; + return true; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char inp[286000]; + while (cin >> inp) { + int len = strlen(inp); + for (int i=0; i + +using namespace std; + +int prefixFunction (const string s) { + int n = s.size(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 and s[i] != s[j]) j = pi[j - 1]; + if (s[i] == s[j]) j++; + pi[i] = j; + } + return pi.back(); +} + +int main () { + string s; + while (cin >> s) { + string rs = s; + reverse(begin(rs), end(rs)); + rs += '#'; + rs.append(s); + int p_len = prefixFunction(rs); + cout << s; + for (int i = s.size() - p_len - 1; i >= 0; i--) cout << s[i]; + cout << endl; + } + return (0); +} diff --git a/uva_cpp_clean/11476/11476-9.cpp b/uva_cpp_clean/11476/11476-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1338be55fa07eb1d47c3524ef9eccb6b40a4dea --- /dev/null +++ b/uva_cpp_clean/11476/11476-9.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +typedef unsigned long long ull; + +const long long L1 = 3e5, L2 = 9e10; + +int tc; +ull N, d; +bool is_prime[L1], first; +vector prime; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(ull i = 2; i < L1; i++) + if( is_prime[i] ){ + for(ull j = i * i; j < L1; j += i) + is_prime[j] = false; + prime.push_back(i); + } +} + +ull mulMod(ull a, ull b, ull c){ + ull ret = 0, pib = a % c; + while(b){ + if( b & 1 ) ret += pib; + pib <<= 1; + if( ret >= c ) ret -= c; + if( pib >= c ) pib -= c; + b >>= 1; + } + return ret; +} + +ull powMod(ull a, ull b, ull c){ + ull ret = 1, pib = a; + while(b){ + if( b & 1 ) ret = mulMod(ret, pib, c); + pib = mulMod(pib, pib, c); + b >>= 1; + } + return ret; +} + +bool MillerRobin(ull num, int it = 10){ + if( num < 2 ) return false; + if( num < 4 ) return true; + if( not ( num & 1 ) ) return false; + ull s = num - 1; + while( not ( s & 1 ) ) s >>= 1; + while(it--){ + ull rd = rand() % ( num - 1 ) + 1, pib = s; + ull ret = powMod(rd, pib, num); + if( ret == -1 or ret == 1 ) continue; + while( pib != num - 1 and ret != 1 and ret != num - 1 ){ + ret = mulMod(ret, ret, num); + pib <<= 1; + } + if( ret != num - 1 ) return false; + } + return true; +} + +ull gcd(ull a, ull b){ + return b == 0 ? a : gcd(b, a % b); +} + +ull PollardRho(ull num){ + int i = 0, k = 2; + ull ret = 3, pib = 3, d; + while(++i){ + ret = ( mulMod(ret, ret, num) + num - 1 ) % num; + if( pib - ret >= 0 ) d = gcd(pib - ret, num); + else d = gcd(pib - ret, num); + if( d != 1 and d != num ) return d; + if( i == k ) pib = ret, k <<= 1; + } +} + +void GetFactors(){ + for(int i = 0; i < prime.size() and prime[i] * prime[i] <= N and N != 1; i++) + if( N % prime[i] == 0 ){ + int e = 0; + while( N % prime[i] == 0 ) N /= prime[i], e++; + if( e == 1 ) printf( first ? " %lld" : " * %lld", prime[i]); + else printf( first ? " %llu^%d" : " * %lld^%d", prime[i], e); + first = false; + } +} + +int main(){ + sieve(); + scanf("%d", &tc); + while(tc--){ + first = true; + scanf("%lld", &N); + printf("%lld =", N); + if( N < L1 ){ + if( is_prime[N] ) printf(" %lld", N); + else{ + GetFactors(); + if( N != 1 ) printf( first ? " %lld" : " * %lld", N); + } + } + else if( N < L2 ){ + GetFactors(); + if( N != 1 ) printf( first ? " %lld" : " * %lld", N); + } + else{ + if( MillerRobin(N) ) printf(" %lld", N); + else{ + GetFactors(); + if( N != 1 ){ + if( MillerRobin(N) ) printf(first ? " %lld" : " * %lld", N); + else{ + ull d = PollardRho(N); + if( d > N / d ) d = N / d; + if( d != N / d ) printf( first ? " %lld * %lld" : " * %lld * %lld", d, N / d); + else printf(first ? " %lld^2" : " * %lld^2", d); + } + } + } + } + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/11479/11479-19.cpp b/uva_cpp_clean/11479/11479-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c7147bf6e8e2941084edfdc41489a9eb04e91f2 --- /dev/null +++ b/uva_cpp_clean/11479/11479-19.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main() { + int T; + cin >> T; + for (int i = 1; i <= T; i++) { + double a, b, c; + cin >> a >> b >> c; + printf("Case %d: ", i); + if (a < b + c && b < a + c && c < a + b && a > 0 && b > 0 && c > 0) { + if (a == b && b == c) printf("Equilateral\n"); + else if (a != b && a != c && b != c) printf("Scalene\n"); + else printf("Isosceles\n"); + } else printf("Invalid\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11479/11479-21.cpp b/uva_cpp_clean/11479/11479-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0d48a955dd81b64b230df1ae91a1f84d577b86f --- /dev/null +++ b/uva_cpp_clean/11479/11479-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11479 + Name: Is this the easiest problem? + Problem: https://onlinejudge.org/external/114/11479.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main() { + int T; + cin>>T; + for(int cse=1; cse<=T; cse++) { + long long int a[3]; + cin>>a[0]>>a[1]>>a[2]; + sort(a, a+3); + cout << "Case " << cse << ": "; + if (a[0]+a[1] <= a[2]) + cout << "Invalid\n"; + else if (a[0]==a[1] && a[1]==a[2]) + cout << "Equilateral\n"; + else if (a[0]==a[1] || a[1]==a[2]) + cout << "Isosceles\n"; + else + cout << "Scalene\n"; + } +} diff --git a/uva_cpp_clean/11479/11479-5.cpp b/uva_cpp_clean/11479/11479-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c3409e13ae23922f940fb52f7bf13a26e88d340c --- /dev/null +++ b/uva_cpp_clean/11479/11479-5.cpp @@ -0,0 +1,48 @@ +//#include +//int main() +//{ +//long int t,a,b,c,i; +//while(scanf("%ld",&t)==1) +//{ +//i=1; +//while(i<=t) +// { +// scanf("%ld%ld%ld",&a,&b,&c); +// if((a+b)<=c || (b+c)<=a || (c+a)<=b) +// printf("Case %ld: Invalid\n",i); +// else if(a<=0 || b<=0 || c<=0) +// printf("Case %ld: Invalid\n",i); +// else if(a==b && b==c) +// printf("Case %ld: Equilateral\n",i); +// else if(a==b || b==c || c==a) +// printf("Case %ld: Isosceles\n",i); +// else +// printf("Case %ld: Scalene\n",i); +// i++; +// } +// +//} +//return 0; +//} +#include +#include +#include +#include +using namespace std; + +int main() +{ + int i,j; + scanf("%d", &j); + for(i=1;i<=j;i++) { + long int a, b, c; + scanf("%ld %ld %ld", &a, &b, &c); + if (a>0 && b>0 && c>0 && abs(b-c) < a && a < (b + c)) { + if (a == b == c) printf("Case %d: Equilateral\n", i); + else if (a == b || a == c || b == c) printf("Case %d: Isosceles\n", i); + else printf("Case %d: Scalene\n", i); + } + else printf("Case %d: Invalid\n", i); + } + return 0; + } diff --git a/uva_cpp_clean/1148/1148-21.cpp b/uva_cpp_clean/1148/1148-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a59358ab569a0a1b2cc996f727b2c5cd8bbe145a --- /dev/null +++ b/uva_cpp_clean/1148/1148-21.cpp @@ -0,0 +1,106 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1148 + Name: The mysterious X network + Problem: https://onlinejudge.org/external/11/1148.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------------- + +#define MAXQS 131072 +class FastQueue { + int data[MAXQS], l, r; + public: + FastQueue():l(0),r(0) {} + void clear() { l=r=0; } + bool empty() { return l==r; } + void push(int x) { data[r++] = x; r&=MAXQS-1; } + int pop(){ int result = data[l++]; l&=MAXQS-1; return result; } + int front() { return data[l]; } + int size() { return r>=l ? r-l : MAXQS-l+r; } +} q; + +vector adj[MAXQS]; +int dist[MAXQS]; + +int main() { + int T = readUInt(); + while (T--) { + int n = readUInt(); + for (int i=0; i + +using namespace std; + +int tc, n, d[100010], _source, _target, u, v, nu; +vector G[100010]; + +int bfs(int source, int target){ + queue Q; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + u = Q.front(), Q.pop(); + if(u == target) return d[target] - 1; + for(int i = 0; i < G[u].size(); i++){ + v = G[u][i]; + if(d[v] == -1) + d[v] = d[u] + 1, Q.push(v); + } + } +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + for(int i = 0; i < n; i++) G[i].clear(); + scanf("%d", &n); + for(int i = 0; i < n; i++){ + d[i] = -1; + scanf("%d %d", &u, &nu); + for(int j = 0; j < nu; j++) + scanf("%d", &v), G[u].push_back(v), G[v].push_back(u); + } + scanf("%d %d", &_source, &_target); + printf("%d %d %d\n", _source, _target, bfs(_source, _target)); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/11487/11487-21.cpp b/uva_cpp_clean/11487/11487-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..604df2482d6c204f1ebeb5927d426801c8250d31 --- /dev/null +++ b/uva_cpp_clean/11487/11487-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11487 + Name: Gathering Food + Problem: https://onlinejudge.org/external/114/11487.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct State { + int i, j, d; + State(int i, int j, int d):i(i),j(j),d(d){} +}; + +#define MODUL 20437 +char M[17][17]; +int Pi[26], Pj[26], C[11][11], n, + di[] = {1,-1,0,0}, + dj[] = {0,0,1,-1}; + +int bfs(int sid, int &c) { + memset(C, 0, sizeof(C)); + char sch = 'A' + sid, + dch = 'A' + sid + 1; + + queue q; + C[Pi[sid]][Pj[sid]] = 1; + q.push(State(Pi[sid], Pj[sid], 0)); + while (!q.empty()) { + State s = q.front(); q.pop(); + if (M[s.i][s.j] == dch) { + c = C[s.i][s.j]; + return s.d; + } + + for (int k=0; k<4; ++k) { + State t(s.i+di[k], s.j+dj[k], s.d+1); + if (M[t.i][t.j]=='.' || (M[t.i][t.j]>='A' && M[t.i][t.j]<=dch)) { + if (!C[t.i][t.j]) + q.push(t); + + C[t.i][t.j] += C[s.i][s.j]; + C[t.i][t.j] %= MODUL; + } + } + } + return c = 0; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + memset(M, '#', sizeof(M)); + for (int cse=1; cin >> n && n; ++cse) { + int m = 0; + for (int i=1; i<=n; ++i) { + cin >> (M[i]+1); + M[i][n+1] = '#'; + M[i][n+2] = 0; + + for (int j=1; j<=n; ++j) + if (M[i][j]>='A') { + int id = M[i][j] - 'A'; + m = max(m, id); + Pi[id] = i; + Pj[id] = j; + } + } + memset(M[n+1], '#', 17); + + int sum=0, cnt=1; + for (int i=0, c; cnt && i +#include +#include +#include + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + int n; + std::cin >> n; + std::unordered_map prefixes; + + for (int i = 0; i < n; i++) + { + std::string temp; + std::cin >> temp; + + for (unsigned int j = 1; j <= temp.length(); j++) + { + prefixes[temp.substr(0, j)]++; + } + } + + int max = 0; + + for (auto &pair : prefixes) + { + int value = pair.second * pair.first.length(); + max = std::max(max, value); + } + + std::cout << max << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11489/11489-21.cpp b/uva_cpp_clean/11489/11489-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fad714069f94b9a3d7056ae0e6a1f8e8f7922a0 --- /dev/null +++ b/uva_cpp_clean/11489/11489-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11489 + Name: Integer Game + Problem: https://onlinejudge.org/external/114/11489.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool SWins(char str[]) { + if (!str[1]) return 1; + + int sum=0, cnt=0; + for (int i=0; str[i]; ++i) { + int d = str[i] - '0'; + if (d%3 == 0) ++cnt; + sum += d; + } + + int r = 9 + sum%3; + if (r%3 != 0) { + bool found = 0; + for (int i=0; !found && str[i]; ++i) + if ( (r-(str[i]-'0')) % 3 == 0) + found = 1; + if (found) ++cnt; + else return 0; + } + + return cnt&1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + char str[1024]; + for (int cse=1; T-- && cin >> str; ++cse) { + + + cout << "Case " << cse << ": " << (SWins(str) ? "S\n" : "T\n"); + } +} diff --git a/uva_cpp_clean/11489/11489-9.cpp b/uva_cpp_clean/11489/11489-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4fb5c74ec93cc80c7beb6f95443f24420293531 --- /dev/null +++ b/uva_cpp_clean/11489/11489-9.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +const int SIZE = 1010; + +int tc; +char N[SIZE]; + +bool doesSwin() { + bool S_turn = true; + int sum = 0; + for (int i = 0; N[i]; i++) sum += N[i] - '0'; + while (true) { + bool possible = false; + for (int i = 0; N[i]; i++) { + if (N[i] and (N[i] - '0') % 3 == sum % 3) { + S_turn = !S_turn; + sum -= N[i] - '0'; + N[i] = 0; + possible = true; + } + } + if (not possible) break; + } + return S_turn == false; +} + +int main() { + scanf("%d\n", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%s\n", N); + printf("Case %d: %c\n", t, doesSwin() ? 'S' : 'T'); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/11491/11491-6.cpp b/uva_cpp_clean/11491/11491-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca5d4539afb0bc010a8dfeba1f9411bee89c17c2 --- /dev/null +++ b/uva_cpp_clean/11491/11491-6.cpp @@ -0,0 +1,46 @@ +/* + greedy + difficulty: easy + date: 03/Oct/2020 + problem: remove D digits from the given number, in a such way that the remain number is maximum + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +int main() { + int N, D; + while (cin >> N >> D && (N || D)) { + string s; cin >> s; + string ans; + + int d = D; // remaining to delete + int i = 0; // current index + while (i < N && d > 0) { + // cout << i << " " << d << endl; + auto max = max_element(s.begin()+i, s.begin()+i+d+1); + int i_max = max-s.begin(); + + d -= i_max-i; + ans.push_back(*max); + if (ans.size() == N-D) break; + + i = i_max+1; + } + + while (i < N && ans.size() < N-D) ans.push_back(s[i++]); + cout << ans << endl; + } + return 0; +} + +/* + 18412923, i = 0, d = 3 + + 18412923, i = 2, d = 2 + 8412923 + + 18412923, i = 3, d = 2 + 84 923 +*/ \ No newline at end of file diff --git a/uva_cpp_clean/11492/11492-13.cpp b/uva_cpp_clean/11492/11492-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/uva_cpp_clean/11492/11492-21.cpp b/uva_cpp_clean/11492/11492-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5a0245719b345af654bab542d81fe5aef724e82 --- /dev/null +++ b/uva_cpp_clean/11492/11492-21.cpp @@ -0,0 +1,139 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11492 + Name: Babel + Problem: https://onlinejudge.org/external/114/11492.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline int readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return 0; + + int l = 0; + str[l++] = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + str[l++] = ch; + str[l] = 0; + return l; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------------- + +struct State { + int u, s, d; + State(int u, char s, int d):u(u),s(s),d(d){} + bool operator < (const State &o) const { return d > o.d; } +}; + +int D[4013][27]; +vector adj[4013]; + +int LS, NS[200000][26], SC, SID[200000]; +int readLid() { + char s[60]; readStr(s); + + int cur = 0; + for (int i=0; s[i]; ++i) { + char ch = s[i]-'a'; + if (!NS[cur][ch]) { + NS[cur][ch] = ++LS; + memset(NS[LS], 0, 26*sizeof(int)); + SID[LS] = -1; + } + cur = NS[cur][ch]; + } + + if (SID[cur] < 0) { + adj[SC].clear(); + for (int i=0; i<27; ++i) + D[SC][i] = INF; + SID[cur] = SC++; + } + + return SID[cur]; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char s[60]; + for (int n; (n=readUInt()) > 0; ) { + LS = SC = 0; + memset(NS[0], 0, 26*sizeof(int)); + + int src = readLid(), + dst = readLid(); + + for (int i=0; i q; + q.push(State(src, 0, D[src][0]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.u][s.s]) continue; + if (s.u == dst) { + cout << s.d << endl; + goto fin; + } + + for (State &t: adj[s.u]) if (s.s!=t.s) { + int d = s.d + t.d; + if (d < D[t.u][t.s]) + q.push(State(t.u, t.s, D[t.u][t.s]=d)); + } + } + } + cout << "impossivel\n"; + fin:; + } +} diff --git a/uva_cpp_clean/11494/11494-14.cpp b/uva_cpp_clean/11494/11494-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa7ff02884e14a8d5953681d6c12d7ad6ab45f04 --- /dev/null +++ b/uva_cpp_clean/11494/11494-14.cpp @@ -0,0 +1,69 @@ +/*************************************************** + * Problem name : 11494 Queen.cpp + * Problem Link : https://uva.onlinejudge.org/external/114/11494.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-27 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int x, y, a, b; + while (scanf("%d %d %d %d", &x, &y, &a, &b) == 4 && x != 0 && y != 0 && a != 0 && b != 0) { + int xx = x + a, yy = y + b; + //~ printf("%lld %lld\n", xx, yy); + if (x == a && y == b) printf("0\n"); + else if (( x == y && a == b) || ((a != b) && ((x + y) == (a + b)))) printf("1\n"); + else if ((xx % 2 == 1 && yy % 2 == 1 && (abs(x - a) == abs(y - b))) || (xx % 2 == 0 && yy % 2 == 0 && (abs(x - a) == abs(y - b)))) printf("1\n"); + else if (x == a || y == b) printf("1\n"); + else printf("2\n"); + } + return 0; +} + diff --git a/uva_cpp_clean/11494/11494-21.cpp b/uva_cpp_clean/11494/11494-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..430ac97f5e775b4ba784e9b09e41218ff0c4c72b --- /dev/null +++ b/uva_cpp_clean/11494/11494-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11494 + Name: Queen + Problem: https://onlinejudge.org/external/114/11494.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int x1,y1, x2,y2; + while (cin>>x1>>y1>>x2>>y2 && (x1||y1||x2||y2)) { + if (x1==x2 && y1==y2) puts("0"); + else if (x1==x2 || y1==y2 || x1-x2==y1-y2 || x2-x1==y1-y2) + puts("1"); + else puts("2"); + } +} diff --git a/uva_cpp_clean/11494/11494-9.cpp b/uva_cpp_clean/11494/11494-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a58345902e7b6b1aaa59c3a79bc1dd868245a09 --- /dev/null +++ b/uva_cpp_clean/11494/11494-9.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int X1, Y1, X2, Y2; + +int main(){ + while(scanf("%d %d %d %d", &X1, &Y1, &X2, &Y2), X1 | Y1 | X2 | Y2 ){ + if( X1 == X2 and Y1 == Y2 ) + puts("0"); + else if( X1 == X2 or Y1 == Y2 or abs(X1 - X2) == abs(Y1 - Y2) ) + puts("1"); + else + puts("2"); + } + return(0); +} diff --git a/uva_cpp_clean/11495/11495-21.cpp b/uva_cpp_clean/11495/11495-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..373395b6d5c36a432809a68e1231c4eb14c3c4b3 --- /dev/null +++ b/uva_cpp_clean/11495/11495-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11495 + Name: Bubbles and Buckets + Problem: https://onlinejudge.org/external/114/11495.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +// BIT/Fenwick Tree +int X[100143], n, x; + +void add(int i, int inc) { + if (i == 0) { + X[0] += inc; + return; + } + + for(; i<=n; i += (i & -i)) { + X[i] += inc; + } +} + +int sum(int i) { + int result = X[0]; + for (; i; i -= (i & -i)) { + result += X[i]; + } + return result; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n && n > 0) { + memset(X, 0, n*sizeof(int)); + + int result = 0; + for (int i=0; i> x; + add(x, 1); + result += i-sum(x-1); + } + cout << (result%2 ? "Marcelo" : "Carlos") << endl; + } +} diff --git a/uva_cpp_clean/11495/11495-9.cpp b/uva_cpp_clean/11495/11495-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36a496382df3885840c594b62ea30cab6a840126 --- /dev/null +++ b/uva_cpp_clean/11495/11495-9.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +const int SIZE = 100005; + +int n, v[SIZE]; + +long long Merge(int lo, int mid, int hi){ + long long inv = 0; + vector L, R; + int l1 = mid - lo + 1, l2 = hi - mid; + for(int it = 0; it < l1; it++) L.push_back(v[lo + it]); + for(int it = 0; it < l2; it++) R.push_back(v[mid + 1 + it]); + L.push_back(INT_MAX), R.push_back(INT_MAX); + int i = 0, j = 0; + for(int it = lo; it <= hi; it++) + if( L[i] <= R[j] ) + v[it] = L[i++]; + else + v[it] = R[j++], inv += l1 - i; + return inv; +} + +long long MergeSort(int lo, int hi){ + long long inv = 0; + if( lo < hi ){ + int mid = ( lo + hi ) / 2; + inv += MergeSort(lo, mid); + inv += MergeSort(mid + 1, hi); + inv += Merge(lo, mid, hi); + } + return inv; +} + +int main(){ + while(scanf("%d", &n), n){ + for(int it = 0; it < n; it++) + scanf("%d", v + it); + puts( MergeSort(0, n - 1) & 1 ? "Marcelo" : "Carlos" ); + } + return(0); +} diff --git a/uva_cpp_clean/11496/11496-19.cpp b/uva_cpp_clean/11496/11496-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44fc9f6daff64d65ac853ced1158f335c6b06dd7 --- /dev/null +++ b/uva_cpp_clean/11496/11496-19.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main() { + int n; + while (cin >> n && n != 0) { + int p1, p2, p3, first, second, count = 0; + cin >> p1 >> p2; + first = p1; + p3 = p2; + second = p2; + for (int i = 2; i < n; i++) { + cin >> p3; + if ((p2 > p1 && p2 > p3) || (p2 < p1 && p2 < p3)) count++; + p1 = p2; + p2 = p3; + } + if ((p3 > p1 && p3 > first) || (p3 < p1 && p3 < first)) count++; + if ((first > second && first > p3) || (first < second && first < p3)) count++; + printf("%d\n", count); + } + return 0; +} diff --git a/uva_cpp_clean/11496/11496-21.cpp b/uva_cpp_clean/11496/11496-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e326e40d73de7d1ae4e4f32be7cf0ad9d1d1323c --- /dev/null +++ b/uva_cpp_clean/11496/11496-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11496 + Name: Musical Loop + Problem: https://onlinejudge.org/external/114/11496.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int sgn(int x) { return x<0 ? -1 : !x ? 0 : 1; } +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, x, frst, lst, s; + while (cin>>n && n) { + cin >> frst >> lst; + int dir = sgn(lst - frst), + cnt = 0, + d1 = dir; + for (int i=2; i> x; + s = sgn(x - lst); + if (s*dir < 0) ++cnt; + dir = s; + lst = x; + } + s = sgn(frst - lst); + if (s*dir < 0) ++cnt; + if (s*d1 < 0) ++cnt; + + cout << cnt << endl; + } +} diff --git a/uva_cpp_clean/11498/11498-13.cpp b/uva_cpp_clean/11498/11498-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf0cb8617cc328a905a6821afc01559fff8fd0f8 --- /dev/null +++ b/uva_cpp_clean/11498/11498-13.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int test = 1; + + queue dir; + + int m, n, a, b; + + cin >> test; + + while (test != 0) + { + cin >> m >> n; + + for (int i = 0; i < test; i++) + { + cin >> a >> b; + + a -= m; + + b -= n; + + if (a > 0 && b > 0) + { + dir.push("NE"); + } + + else if (a > 0 && b < 0) + { + dir.push("SE"); + } + + else if (a < 0 && b > 0) + { + dir.push("NO"); + } + + else if (a < 0 && b < 0) + { + dir.push("SO"); + } + + else if (a == 0 || b == 0) + { + dir.push("divisa"); + } + } + + cin >> test; + } + + while (!dir.empty()) + { + cout << dir.front() << endl; + + dir.pop(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11498/11498-14.cpp b/uva_cpp_clean/11498/11498-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3898cdfea5079fb9b8314618dba980e29484a714 --- /dev/null +++ b/uva_cpp_clean/11498/11498-14.cpp @@ -0,0 +1,56 @@ +/*************************************************** + * Problem name : 11498 Division of Nlogonia.cpp + * OJ : Uva + * Result : AC + * Date : 03-05-2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define MAX 10000 +using namespace std; +typedef long long ll; +int main () { + ll n,x,y,dx,dy; + while(scanf("%lld", &n) == 1 && n!=0){ + scanf("%lld %lld", &dx,&dy); + for(int t = 1; t<=n; t++){ + scanf("%lld %lld", &x,&y); + if(x == dx || y == dy){ + printf("divisa\n"); + } + else{ + int xx = x - dx; + int yy = y - dy; + if(xx<0 && yy>0){ + printf("NO\n"); + } + else if( xx > 0 && yy >0){ + printf("NE\n"); + } + else if(xx<0 && yy<0){ + printf("SO\n"); + } + else if(xx>0 && yy<0){ + printf("SE\n"); + } + } + } + } + return 0; +} + diff --git a/uva_cpp_clean/11498/11498-19.cpp b/uva_cpp_clean/11498/11498-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b8ecf9aa8601650682cb7b9334be28021a15101 --- /dev/null +++ b/uva_cpp_clean/11498/11498-19.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() { + int K; + while (scanf("%d", &K) && K != 0) { + int N, M; + scanf("%d %d", &N, &M); + for (int i = 0; i < K; i++) { + int X, Y; + scanf("%d %d", &X, &Y); + if (X > N) { + if (Y > M) printf("NE\n"); + else if (Y < M) printf("SE\n"); + else printf("divisa\n"); + + } else if (X < N) { + if (Y > M) printf("NO\n"); + else if (Y < M) printf("SO\n"); + else printf("divisa\n"); + } + else printf("divisa\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11498/11498-21.cpp b/uva_cpp_clean/11498/11498-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a81c7f41fbb36854408c10434f2d0bd2ced5d2b8 --- /dev/null +++ b/uva_cpp_clean/11498/11498-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11498 + Name: Division of Nlogonia + Problem: https://onlinejudge.org/external/114/11498.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int k; + while ((cin >> k) && k) { + int x, y, a, b; + cin >> x >> y; + while (k--) { + cin >> a >> b; + if (a == x || b == y) + cout << "divisa\n"; + else cout << (b < y ? "S" : "N") << (a < x ? "O" : "E") << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11498/11498-3.cpp b/uva_cpp_clean/11498/11498-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4aeac9d036c8e6283235efbd6285376ee5d7156 --- /dev/null +++ b/uva_cpp_clean/11498/11498-3.cpp @@ -0,0 +1,27 @@ +#include + +int main(){ + // INPUT PROCESS HAS BEEN MODIFIED DUE TO MULTIPLY CASES IN ONE INPUT + // integer K is the number of queries + // integers N, M are the coordinates (x,y) of point A (The division point) + int K, N, M; + + // integers a and b stand for the coordinates of point X whose location we must determine + int a,b; + + while ( scanf ("%d %d %d", &K, &N, &M) != EOF ){ + for ( int l = 0; l < K; l++){ + scanf ( "%d %d", &a, &b); + if ( a == N || b == M) + printf ( "divisa\n" ); + else if ( a < N ) { + if ( b > M ) printf ( "NO\n" ); + else printf ("SO\n"); + } + else { + if ( b > M ) printf ( "NE\n" ); + else printf ( "SE\n" ); + } + } + } +} diff --git a/uva_cpp_clean/11498/11498-9.cpp b/uva_cpp_clean/11498/11498-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3300a79fd2d943969875282449c8762a1de53318 --- /dev/null +++ b/uva_cpp_clean/11498/11498-9.cpp @@ -0,0 +1,19 @@ +#include + +int main(){ + int cond = 1, n, x, y, a, b; + while(cond){ + scanf("%d", &n); + if(n == 0) break; + scanf("%d %d", &x, &y); + while(n--){ + scanf("%d %d", &a, &b); + if(a > x && b > y) printf("NE\n"); + else if(a < x && b > y) printf("NO\n"); + else if(a < x && b < y) printf("SO\n"); + else if(a > x && b < y) printf("SE\n"); + else printf("divisa\n"); + } + } + return(0); +} diff --git a/uva_cpp_clean/11498/main.cpp b/uva_cpp_clean/11498/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0f4a9108c2358d8f46387ccc5cdc6cb811e0196 --- /dev/null +++ b/uva_cpp_clean/11498/main.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main(){ + + int k,n,m,x,y; + cin >> k; + while(k > 0){ + cin >> n >> m; + for(k; k > 0; k--){ + cin >> x >> y; + if(x == n || y == m) cout << "divisa" << endl; + else{ + if(y > m) cout << "N"; + else cout << "S"; + if(x > n) cout << "E" << endl; + else cout << "O" << endl; + } + } + cin >> k; + } + return 0; +} diff --git a/uva_cpp_clean/115/115-21.cpp b/uva_cpp_clean/115/115-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f63e78f37f4d7df0232a22ad10c545efbe75a6a --- /dev/null +++ b/uva_cpp_clean/115/115-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 115 + Name: Climbing Trees + Problem: https://onlinejudge.org/external/1/115.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int seen[313], par[313], ncnt; +map nids; +int nameToId(string name) { + auto r = nids.emplace(name, ncnt); + if (r.second) ++ncnt; + return r.first->second; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + memset(par, -1, sizeof(par)); + + string s1, s2; + while (cin >> s1 >> s2 && s1!="no.child") + par[nameToId(s1)] = nameToId(s2); + + + while (cin >> s1 >> s2) { + int u = nameToId(s1), + v = nameToId(s2); + + memset(seen, 0, sizeof(seen)); + for (int k=u, d=1; k!=-1; k=par[k], ++d) + seen[k] = d; + + int r=-1, d1, d2; + for (int k=v, d=1; k!=-1; k=par[k], ++d) + if (seen[k]) { + d1 = seen[k]-2; + d2 = d-2; + r = k; + break; + } + + int mn = min(d1, d2), + df = abs(d1 -d2); + + if (r < 0) cout << "no relation\n"; + else if (!d1 && !d2) cout << "sibling\n"; + else if (u == r || v == r) { + for (int i=2; i 1) cout << "grand "; + cout << (u==r ? "parent\n" : "child\n"); + } + else { + cout << mn << " cousin"; + if (df) cout << " removed " << df; + cout << '\n'; + } + } +} diff --git a/uva_cpp_clean/11500/11500-19.cpp b/uva_cpp_clean/11500/11500-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84789cb6113e260d126525f306bdabffc34e78a9 --- /dev/null +++ b/uva_cpp_clean/11500/11500-19.cpp @@ -0,0 +1,74 @@ +#include +#include +using namespace std; + +long double ** mat1[7][11], ** mat2; +bool mask[7][11]; + +inline int trunc(int n) { + if (n < 0) return 0; + if (n > 21) return 21; + return n; +} + +void proc(int AT, int D) { + if (!mask[AT][D]) { + mask[AT][D] = true; + long double ** m1 = mat1[AT][D]; + long double ** m2 = mat2; + for (int i = 0; i < 22; i++) { + for (int j = 0; j < 22; j++) { + if (j == 0) m1[i][j] = 1; + else m1[i][j] = 0; + } + } + for (int it = 0; it < 625; it++) { + for (int i = 0; i < 22; i++) { + for (int j = 0; j < 22; j++) { + if (i == 0 || j == 0) m2[i][j] = m1[i][j]; + else m2[i][j] = (AT*m1[trunc(i + D)][trunc(j - D)] + (6-AT)*m1[trunc(i - D)][trunc(j + D)])/6.0; + } + } + long double ** auxi = m1; + m1 = m2; + m2 = auxi; + } + mat1[AT][D] = m1; + mat2 = m2; + } +} + +int main() { + int EV1, EV2, AT, D; + for (int j = 0; j < 7; j++) + for (int k = 0; k < 11; k++) + mat1[j][k] = new long double *[22]; + mat2 = new long double *[22]; + for (int i = 0; i < 22; i++) { + for (int j = 0; j < 7; j++) + for (int k = 0; k < 11; k++) + mat1[j][k][i] = new long double [22]; + mat2[i] = new long double [22]; + } + + for (int j = 0; j < 7; j++) + for (int k = 0; k < 11; k++) + proc(j, k); + + while (scanf("%d %d %d %d", &EV1, &EV2, &AT, &D) && !(EV1 == 0 && EV2 == 0 && AT == 0 && D == 0)) { + long double sol = mat1[AT][D][EV1][EV2]; + printf("%.1Lf\n", sol*100); + } + + for (int i = 0; i < 22; i++) { + for (int j = 0; j < 7; j++) + for (int k = 0; k < 11; k++) + delete [] mat1[j][k][i]; + delete [] mat2[i]; + } + for (int j = 0; j < 7; j++) + for (int k = 0; k < 11; k++) + delete [] mat1[j][k]; + delete [] mat2; + return 0; +} diff --git a/uva_cpp_clean/11503/11503-10.cpp b/uva_cpp_clean/11503/11503-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..531a81ae8c1a23e23d3995fd086ece5b16dab2c1 --- /dev/null +++ b/uva_cpp_clean/11503/11503-10.cpp @@ -0,0 +1,112 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)2e5+9; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; + +int minimumSpanningTree(vector> &edges) { + + + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + int ans = 0,a,b,weight; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + } else { + ans +=weight; + } + } + return ans; +} + + +int main() { + FAST + + int t; cin>>t; + + while (t--) { + int f;cin>>f; + DSU dsu; + dsu.init(f*2+5); + + string a,b; + int c=0; + map mp; + + while (f--) { + cin>>a>>b; + + if(mp.count(a)==0) { + mp[a]=c; + c++; + } + + if(mp.count(b) ==0) { + mp[b]=c; + c++; + } + dsu.merge(mp[a],mp[b]); + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 1000005 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +////////////////////////////////////////////////////////////////////////////////////////////////// +ll par[MAX], flag,gm[MAX]; +mapmp; +ll findRepresentative(ll r) { + if (par[r] == r) { + return r; + } else { + par[r] = findRepresentative(par[r]); + } + + return par[r]; +} +ll setUnion(ll a, ll b) { + ll u = findRepresentative(a); + ll v = findRepresentative(b); + ll r = gm[u]; + ll rr = gm[v]; + ll ans; + if (u != v) { + ans = r + rr; + par[u] = v; + ll x = findRepresentative(u); + return gm[x] = ans; + } + return rr; +} +int main () { + ll tc, F; + scanf("%lld", &tc); + for (ll t = 1; t <= tc; t++) { + scanf("%lld", &F); + ll j = 1; + for (ll i = 1; i <= F; i++) { + string a, b; + cin >> a >> b; + if (mp[a] == 0) { + mp[a] = j++; + par[mp[a]] = mp[a]; + gm[mp[a]] = 1; + } + if (mp[b] == 0) { + mp[b] = j++; + par[mp[b]] = mp[b]; + gm[mp[b]] = 1;; + } + ll cnt = setUnion(mp[a], mp[b]); + printf("%lld\n", cnt); + } + mp.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/11503/11503-19.cpp b/uva_cpp_clean/11503/11503-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89d80270729de3d7044699ccf7519b08a5446408 --- /dev/null +++ b/uva_cpp_clean/11503/11503-19.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vi; +typedef vector vii; + +struct ufds { + vii p; vi r; int numSets; + ufds(int N) { + p.assign(N, ii(0, 1)); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i].first = i; + } + int subSetSize(int i) { + int x = findSet(i); + return p[x].second; + } + int findSet(int i) { return (p[i].first==i)? i: (p[i].first=findSet(p[i].first)); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y].first = x, p[x].second += p[y].second; + else { + p[x].first = y; + p[y].second += p[x].second; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +int main() { + int N; + cin >> N; + while (N--) { + int F; + unordered_map names; + cin >> F; + ufds friends(2*F); + while (F--) { + string s1, s2; + cin >> s1 >> s2; + if (!names.count(s1)) names.insert(make_pair(s1, names.size())); + if (!names.count(s2)) names.insert(make_pair(s2, names.size())); + friends.unionSet(names[s1], names[s2]); + printf("%d\n", friends.subSetSize(names[s1])); + } + } + return 0; +} diff --git a/uva_cpp_clean/11503/11503-21.cpp b/uva_cpp_clean/11503/11503-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5024876df796544b86db9e67414429ffae1721e --- /dev/null +++ b/uva_cpp_clean/11503/11503-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11503 + Name: Virtual Friends + Problem: https://onlinejudge.org/external/115/11503.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXN 100001 + +int ids[MAXN], sze[MAXN]; +int find(int id) { + return ids[id] == id ? id : (ids[id] = find(ids[id])); +} +int join(int u, int v) { + u = find(u); v = find(v); + if (u != v) { + sze[v] += sze[u]; + ids[u] = v; + } + return sze[v]; +} + +int cnt; +map names; +int find(string name) { + auto it = names.find(name); + if (it != names.end()) + return find(it->second); + + sze[++cnt] = 1; + names[name] = cnt; + return ids[cnt] = cnt; +} + + +int main(){ + string s1, s2; + int T, n; + cin>>T; + while (T--) { + names.clear(); cnt=0; + cin >> n; + for (int i=0; i>s1>>s2; + cout << join(find(s1), find(s2)) << endl; + } + } +} diff --git a/uva_cpp_clean/11503/11503-5.cpp b/uva_cpp_clean/11503/11503-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e69f2b7bbc1e8ef6dc746f066847f8aca6f8ddf0 --- /dev/null +++ b/uva_cpp_clean/11503/11503-5.cpp @@ -0,0 +1,128 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define CP(a) cout<mp; + int f,fno=1; + sii(f); + for(int i=1;i<=2*f;i++) + { + par[i]=i; + sum[i]=1; + } + while(f--) + { + string s1,s2; + cin>>s1>>s2; + if(mp[s1]==0) mp[s1]=fno,fno++; + if(mp[s2]==0) mp[s2]=fno,fno++; + CP(Union(mp[s1],mp[s2])); + } + } + return 0; +} diff --git a/uva_cpp_clean/11503/11503-6.cc b/uva_cpp_clean/11503/11503-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..3b65ac2f4f5ace1feee129927752a14fe3cc34b4 --- /dev/null +++ b/uva_cpp_clean/11503/11503-6.cc @@ -0,0 +1,45 @@ +// https://uva.onlinejudge.org/external/115/11503.pdf +#include +using namespace std; +using ii=tuple; +using vi=vector; +using vii=vector; +using msi=unordered_map; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t,m,u,v; + cin>>t; + while(t--){ + cin>>m; + vii e(m); + msi x; + int n=0; + for(int i=0;i>r>>s; + if(!x.count(r))x[r]=n++; + if(!x.count(s))x[s]=n++; + e[i]={x[r],x[s]}; + } + vi s=vi(n, 1),p=vi(n); + for(int i=0;ifind=[&](int i){ + if(i==p[i])return i; + return p[i]=find(p[i]); + }; + functionunite=[&](int i, int j){ + i=find(i); + j=find(j); + if(i==j)return; + if(s[i] + +using namespace std; + +struct UnionFind{ + int numSets; + vector path, rank, setSize; + + UnionFind(int N_): numSets(N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + setSize.assign(N_, 1); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + numSets--; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x, setSize[x] += setSize[y]; + else path[x] = y, setSize[y] += setSize[x]; + if( rank[x] == rank[y] ) rank[y]++; + } + + int sizeOfSet(int idx){ + return setSize[ findSet(idx) ]; + } +}; + +const int LIM = 25; +int tc, F, ct, idx1, idx2; +char s1[LIM], s2[LIM]; +map < string, int > mp; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d\n", &F); + mp.clear(), ct = 0; + UnionFind UF(F + 1); + for(int it = 0; it < F; it++){ + scanf("%s %s", s1, s2); + idx1 = mp[s1]; + if( mp[s1] == 0 ) mp[s1] = idx1 = ++ct; + idx2 = mp[s2]; + if( mp[s2] == 0 ) mp[s2] = idx2 = ++ct; + UF.unionSet(idx1, idx2); + printf("%d\n", UF.sizeOfSet(idx1)); + } + } + return(0); +} diff --git a/uva_cpp_clean/11504/11504-12.cpp b/uva_cpp_clean/11504/11504-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9e630259ed8148cdc650a12d867f82d4f469647 --- /dev/null +++ b/uva_cpp_clean/11504/11504-12.cpp @@ -0,0 +1,125 @@ +#include +using namespace std; + +#define fastio \ + ios_base::sync_with_stdio(0); \ + cin.tie(NULL); \ + cout.tie(NULL) +#define PI(a, b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long int +#define S second +#define F first +#define FOR(var, len) for (var = 0; var < len; var++) +#define debug1 cout << "debug1" << '\n' +#define debug2 cout << "debug2" << '\n' +#define debug3 cout << "debug3" << '\n' + +// int dr[] = {0,-1,0,1,-1,-1,1,1}; +// int dc[] = {-1,0,1,0,-1,1,1,-1}; + +// Maths Utils +int binExp(int a, int b, int m) +{ + int r = 1; + while (b) + { + if (b % 2 == 1) + r = (r * a) % m; + a = (a * a) % m; + b = b / 2; + } + return r; +} + +int scc, cnt; +int visited[100005], dist[100005], low[100005]; +vector adj[100005]; +stack st; + +void tarjan(int u) +{ + visited[u] = 1; + dist[u] = low[u] = cnt++; + + st.push(u); + + for (auto v : adj[u]) + { + if (!visited[v]) + { + tarjan(v); + low[u] = min(low[u], low[v]); + } + else if (visited[v] == 1) + { + low[u] = min(low[u], dist[v]); + } + } + + if (dist[u] == low[u]) + { + scc++; + int x; + while (1) + { + x = st.top(); + visited[x] = 2; + dist[x] = scc; + st.pop(); + if(x == u) + break; + } + } +} + +int32_t main() +{ + // freopen("input.txt","r",stdin); + // freopen("output.txt","w",stdout); + fastio; + int n, m, t; + cin >> t; + while (t--) + { + cin >> n >> m; + + // pre-process + for (int i = 1; i < 100005; i++) + { + adj[i].clear(); + low[i] = dist[i] = -1; + visited[i] = 0; + } + scc = 0; + cnt = 0; + + int a, b, p; + for (int i = 0; i < m; i++) + { + cin >> a >> b; + adj[a].EB(b); + // adj[b].EB(a); + } + + for (int i = 1; i <= n; i++) + if (!visited[i]) + tarjan(i); + + + vector degree(scc+1,0); + for(int i = 1;i<=n;i++) + for(auto v:adj[i]){ + if(dist[i]!=dist[v]) + degree[dist[v]]++; + } + + int ans = count(degree.begin()+1,degree.end(),0); + cout< +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +ofstream fout("f.out"); + +int n, m, test, dfsCount, scc, ans; + +stack points; +vector visited; +vector dfs_num, dfs_low; +vector < vector > graph; +vector < vector > parent; + +void initialize(int n) +{ + ans = 0; + scc = dfsCount = 0; + + visited.clear(); + visited.resize(n, 0); + + parent.clear(); + parent.resize(n); + + dfs_num.clear(); + dfs_num.resize(n, -1); + + dfs_low.clear(); + dfs_low.resize(n, -1); + + graph.clear(); + graph.resize(n); +} + +void dfs(int start) +{ + visited[start] = true; + + points.push(start); + + dfs_num[start] = dfs_low[start] = ++dfsCount; + + for (int i = 0; i < (int)graph[start].size(); i++) + { + int child = graph[start][i]; + + if (dfs_num[child] == -1) dfs(child); + + if (visited[child]) dfs_low[start] = min(dfs_low[child], dfs_low[start]); + } + + if (dfs_num[start] == dfs_low[start]) + { + bool ok = true; + + while (points.top() != start) + { + visited[points.top()] = false; + + for (int i = 0; i < parent[points.top()].size(); i++) + { + int x = parent[points.top()][i]; + + if (dfs_num[x] == -1) ok = false; + } + + points.pop(); + } + + visited[points.top()] = false; + + for (int i = 0; i < parent[points.top()].size(); i++) + { + int x = parent[points.top()][i]; + + if (dfs_num[x] == -1) ok = false; + } + + points.pop(); + + if (points.empty() && ok) ans++; + } +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n >> m; + + initialize(n + 1); + + for (int i = 0; i < m; i++) + { + int v, w; + + cin >> v >> w; + + graph[v].push_back(w); + + parent[w].push_back(v); + } + + for (int i = 1; i <= n; i++) + { + if (dfs_num[i] == -1) + { + dfs(i); + } + } + + cout << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11504/11504-14.cpp b/uva_cpp_clean/11504/11504-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..824c879e9faf0b5c226c427c23dcee34645fc459 --- /dev/null +++ b/uva_cpp_clean/11504/11504-14.cpp @@ -0,0 +1,174 @@ +/*************************************************** + * Problem Name : 11504 Dominos.cpp + * Problem Link : https://onlinejudge.org/external/115/11504.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-10-31 + * Problem Type : Graph (top_sort) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX], ans; +bool vis[MAX]; + +void dfs (int u) { + vis[u] = true; + + for (int v : adj[u]) { + if (!vis[v]) { + dfs (v); + } + } + + ans.pb (u); +} +void dfs1(int u) { + vis[u] = true; + + for (int v : adj[u]) { + if (!vis[v]) { + dfs1 (v); + } + } +} + +void top_sort (int n) { + SET (vis, false); + ans.clear(); + + for (int i = 1; i <= n; i++) { + if (!vis[i]) { + dfs (i); + } + } + + reverse (all (ans) ); +} + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + int tc, n, m; + cin >> tc; + + while (tc--) { + cin >> n >> m; + + for (int i = 0; i < m; i++) { + int x, y; + cin >> x >> y; + adj[x].pb (y); + } + + top_sort (n); + SET (vis, false); + int cnt = 0; + + for (int i = 0; i < n; i++) { + int u = ans[i]; + + if (vis[u] == false) { + cnt++; + dfs1(u); + } + } + + cout << cnt << "\n"; + ans.clear(); + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + } + } + + return 0; +} diff --git a/uva_cpp_clean/11504/11504-18.cpp b/uva_cpp_clean/11504/11504-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..29ccc913a067b6cfbd9f0fbbcd262d9abb613e96 --- /dev/null +++ b/uva_cpp_clean/11504/11504-18.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +vector graph[150000]; +stack scc; +bool visited[150000]; + +void SCC_DFS(int u){ + visited[u] = true; + for(int i = 0; i < graph[u].size(); i++){ + int v = graph[u][i]; + if(!visited[v]) SCC_DFS(v); + } + scc.push(u); +} + +void DFS(int u){ + visited[u] = true; + for(int i = 0; i < graph[u].size(); i++){ + int v = graph[u][i]; + if(!visited[v]) DFS(v); + } +} + +int main(){ + int tc, n, m, u, v, knock = 0; + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &n, &m); + for(int i = 0; i <= n; i++) graph[i].clear(); + knock = 0; + memset(visited, false, sizeof(visited)); + for(int i = 0; i < m; i++) scanf("%d %d", &u, &v), graph[u].push_back(v); + for(int i = 1; i <= n; i++) if(!visited[i]) SCC_DFS(i); + memset(visited, false, sizeof(visited)); + while(!scc.empty()){ + int u = scc.top(); + scc.pop(); + if(!visited[u]){ + DFS(u); + knock++; + } + } + printf("%d\n", knock); + } + return 0; +} +/* + Problema algo raro al inicio, la primera estrategia fue usar ordenamiento topologico para obtener con que nodo + empezar para simular la situacion, pero, no cai en cuenta que salia mas sencillo al hallar las componentes + fuertemente conexas y contarlas. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/11504/11504-19.cpp b/uva_cpp_clean/11504/11504-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83c97d3f84ea2ac2afb28d8bcf2f7b2a2a363d02 --- /dev/null +++ b/uva_cpp_clean/11504/11504-19.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vii; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + void clear(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +ufds ds; + +vector AdjList; +vi knocked; +int knocks; + +vi dfs_num, dfs_low; +vector visited; +stack s; +int counter, n0; + +void scc(int n, int p) { + visited[n] = true; + s.push(n); + dfs_num[n] = dfs_low[n] = counter++; + for (int i = 0; i < AdjList[n].size(); i++) { + if (dfs_num[AdjList[n][i]] == -1) { + scc(AdjList[n][i], n); + } + if (visited[AdjList[n][i]]) dfs_low[n] = min(dfs_low[n], dfs_low[AdjList[n][i]]); + } + if (dfs_num[n] == dfs_low[n]) { + while (1) { + int v = s.top(); s.pop(); + visited[v] = 0; + ds.unionSet(n, v); + if (n == v) break; + } + } +} + +void proc(int k) { + k = ds.findSet(k); + queue q; + knocks++; + knocked[k] = 2; + for (int i = 0; i < AdjList[k].size(); i++) { + q.push(AdjList[k][i]); + } + while (!q.empty()) { + if (!knocked[q.front()]) { + knocked[q.front()] = 1; + for (int i = 0; i < AdjList[q.front()].size(); i++) { + q.push(AdjList[q.front()][i]); + } + } else if (ds.findSet(q.front()) != k && knocked[ds.findSet(q.front())] == 2) knocks--, knocked[ds.findSet(q.front())] = 1; + q.pop(); + } +} + +int main() { + int T; + cin >> T; + while (T--) { + int n, m; + cin >> n >> m; + AdjList.assign(n, vi()); + for (int i = 0; i < m; i++) { + int s, d; + cin >> s >> d; + AdjList[s-1].push_back(d-1); + } + + n0 = counter = 0; + dfs_num.assign(n, -1); + dfs_low.assign(n, -1); + visited.assign(n, false); + ds.clear(n); + for (int i = 0; i < n; i++) { + if (dfs_num[i] == -1) scc(i, i); + } + + knocked.assign(n, false); + knocks = 0; + for (int i = 0; i < n; i++) { + if (!knocked[i]) proc(i); + } + printf("%d\n", knocks); + } + return 0; +} diff --git a/uva_cpp_clean/11504/11504-21.cpp b/uva_cpp_clean/11504/11504-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..312232ccf8763a37c78c96b295e6f0d4c945e64c --- /dev/null +++ b/uva_cpp_clean/11504/11504-21.cpp @@ -0,0 +1,87 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11504 + Name: Dominos + Problem: https://onlinejudge.org/external/115/11504.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +#define MAXN 100001 + +int idcnt; +map ids; +int readId() { + int x; cin>>x; + auto it = ids.find(x); + if (it == ids.end()) + return ids[x] = idcnt++; + return it->second; +} + + +list adj[MAXN]; +bool seen[MAXN]; +int order[MAXN], ocnt=0; +void topo(int u) { + seen[u] = true; + for (int v: adj[u]) + if (!seen[v]) + topo(v); + + order[ocnt++] = u; +} + +void dfs(int u) { + seen[u] = true; + for (int v: adj[u]) + if (!seen[v]) + dfs(v); +} + +int main(){ + int T, n, m, u, v; + cin>>T; + while (T--) { + cin>>n>>m; + for (int i=0; i=0; i--) { + int u = order[i]; + if (!seen[u]) { + result++; + dfs(u); + } + } + + cout << result << endl; + } +} diff --git a/uva_cpp_clean/11504/11504-5.cpp b/uva_cpp_clean/11504/11504-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..545606d478ef4268b340dd7bf7384405a7aebe70 --- /dev/null +++ b/uva_cpp_clean/11504/11504-5.cpp @@ -0,0 +1,186 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int col[mx]; +vi g[mx]; +bool f; +void dfs(int u) +{ + col[u]=1; + for0(i,sz(g[u])) + { + int v=g[u][i]; + if(!col[v]) + dfs(v); + } + col[u]=2; +} + +int main() +{ + int t,no=0; + si(t); + while(t--) + { + int n,e; + sii(n,e); + while(e--) + { + int a,b; + sii(a,b); + g[a].pb(b); + } + + vi v1; + vi v2; + for1(i,n) + { + if(!col[i]) + { + v1.pb(i); + dfs(i); + } + } + + int len=sz(v1); + cover(col,0); + rfor0(i,len) + { + if(!col[v1[i]]) + { + v2.pb(v1[i]); + dfs(v1[i]); + } + } + + P(min(sz(v1),sz(v2))); + + cover(col,0); + for0(i,mx) + { + g[i].clear(); + } + } + return 0; +} diff --git a/uva_cpp_clean/11504/11504-6.cc b/uva_cpp_clean/11504/11504-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..a98986a3d7be0ce744f220205ce302aa0d6ee5f8 --- /dev/null +++ b/uva_cpp_clean/11504/11504-6.cc @@ -0,0 +1,55 @@ +// https://uva.onlinejudge.org/external/115/11504.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +using qi=queue; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t,n,m,u,v; + cin>>t; + while(t--){ + cin>>n>>m; + vvi g(n),h(n),c; + for(int i=0;i>u>>v; + u--;v--; + g[u].push_back(v); + h[v].push_back(u); + } + vi a,s(n),t(n),r(n); + functiondfs1=[&](int i){ + s[i]=1; + for(int j:g[i]) + if(!s[j])dfs1(j); + a.push_back(i); + }; + for(int i=0;idfs2=[&](int i){ + t[i]=1; + c.back().push_back(i); + for(int j:h[i]) + if(!t[j])dfs2(j); + }; + for(int i=0;idfs3=[&](int i){ + r[i]=1; + for(int j:g[i]) + if(!r[j])dfs3(j); + }; + int k=0; + for(vi &x:c) + if(!r[x[0]]){ + k++; + dfs3(x[0]); + } + cout< traversal > strongly connected components (SCC) + difficulty: medium + date: 07/Feb/2020 + hint: count the number of SCCs without incoming edge from a vertex of another SCC + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +vector> adjList; + +#define UNVISITED -1 +#define VISITING 0 // visitado, mas ainda visitando seus adjacentes +#define VISITED 1 // todos seus adjacentes visitados e já faz parte de um SCC +vector state; + +vector order; +vector low; + +stack s; // vértices atuais que estão sendo VISITING (na ordem de visitação) +int countOrder, countSCC; + +vector scc; // scc[v] = scc em que v faz parte + + +void dfs(int v) { + low[v] = order[v] = countOrder++; + state[v] = VISITING; s.push(v); + + for (int u : adjList[v]) { + if (state[u] == UNVISITED) dfs(u); + if (state[u] == VISITING) low[v] = min(low[v], low[u]); + } + + if (low[v] == order[v]) { + int u; + do { + u = s.top(); s.pop(); + state[u] = VISITED; + scc[u] = countSCC; + } while (u != v); + + countSCC++; + } +} + +int main() { + int T; cin >> T; + while (T--) { + int V, E; cin >> V >> E; + + countOrder = countSCC = 0; + order.resize(V); low.resize(V); state.assign(V, UNVISITED); scc.resize(V); + adjList.assign(V, vector()); + + while (E--) { + int v, u; cin >> v >> u; v--; u--; + adjList[v].push_back(u); + } + + for (int v = 0; v < V; v++) + if (state[v] == UNVISITED) + dfs(v); + + int ans = countSCC; + vector x(countSCC, false); + + for (int v = 0; v < V; v++) + for (int u : adjList[v]) + if (scc[v] != scc[u] && !x[scc[u]]) { + ans--; + x[scc[u]] = true; + } + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11504/11504-9.cpp b/uva_cpp_clean/11504/11504-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da29ecbe8a68535df1e394d3521f4ce7b9278406 --- /dev/null +++ b/uva_cpp_clean/11504/11504-9.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +const int MAX_V = 100010; + +int tc, V, E, CC; +bool vis[MAX_V]; +vector AdjList[MAX_V], ts; + +void dfs(int u, bool ok = true){ + vis[u] = true; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not vis[v] ) dfs(v); + } + if( ok ) ts.push_back(u); +} + +void solve(){ + fill(vis, vis + V + 1, false); + for(int u = 1; u <= V; u++) + if( not vis[u] ) + dfs(u); + reverse(ts.begin(), ts.end()); + fill(vis, vis + V + 1, false); + for(int j = 0; j < ts.size(); j++) + if( not vis[ts[j]] ) + CC++, dfs(ts[j], false); + printf("%d\n", CC); +} + +void read(){ + int u, v; + scanf("%d %d", &V, &E); + while(E--){ + scanf("%d %d", &u, &v); + AdjList[u].push_back(v); + } +} + +void clear(){ + CC = 0; + ts.clear(); + for(int u = 1; u <= V; u++) AdjList[u].clear(); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11505/11505-21.cpp b/uva_cpp_clean/11505/11505-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca56471d275ab8f921b727276198f79c0472e46f --- /dev/null +++ b/uva_cpp_clean/11505/11505-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11505 + Name: Logo + Problem: https://onlinejudge.org/external/115/11505.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +const double DR = acos(-1)/180; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(0); + + string cm; + int T, n, a; + cin >> T; + while (T-- && cin >> n) { + int d=0; + double x=0, y=0; + while (n--) { + int dir = 1; + cin >> cm >> a; + switch (cm[0]) { + case 'l': d += a; break; + case 'r': d -= a; break; + case 'b': dir = -1; + case 'f': + double r = d * DR; + x += dir * a * cos(r); + y += dir * a * sin(r); + } + d = d % 360; + if (d < 360) d += 360; + } + cout << sqrt(x*x + y*y) << '\n'; + } +} diff --git a/uva_cpp_clean/11506/11506-19.cpp b/uva_cpp_clean/11506/11506-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..902dd8212c6e40b9992b821625fba813f2f78f9b --- /dev/null +++ b/uva_cpp_clean/11506/11506-19.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +int M, W, mf, f, s, t, INF = INT_MAX; +int res[102][102]; +vi p; + +void augment(int v, int minEdge) { + if (v == s) { + f = minEdge; + return; + } else if (p[v] != -1) { + augment(p[v], min(minEdge, res[p[v]][v])); + res[p[v]][v] -= f; res[v][p[v]] += f; + } +} + +int main() { + while (cin >> M >> W && !(M == 0 && W == 0)) { + for (int i = 0; i < 2*(M-1); i++) + for (int j = 0; j < 2*(M-1); j++) + res[i][j] = 0; + for (int i = 0; i < M-2; i++) { + int id, c; + cin >> id >> c; + id--; + res[2*id][(2*id)+1] = c; + } + for (int i = 0; i < W; i++) { + int j, k, c; + cin >> j >> k >> c; + j--, k--; + if (j > k) { + int auxi = k; + k = j; + j = auxi; + } + if (j == 0 && k == M-1) res[0][1] = res[1][0] = c; + else if (j == 0) res[0][2*k] = res[(2*k)+1][0] = c; + else if (k == M-1) res[1][2*j] = res[(2*j)+1][1] = c; + else res[(2*j)+1][2*k] = res[(2*k)+1][2*j] = c; + } + s = mf = 0, t = 1; + while (true) { + f = 0; + vi dist(2*(M-1), INF); dist[s] = 0; + queue q; + q.push(s); + p.assign(2*(M-1), -1); + while (!q.empty()) { + int u = q.front(); q.pop(); + if (u == t) break; + for (int v = 0; v < 2*(M-1); v++) { + if (res[u][v] > 0 && dist[v] == INF) { + dist[v] = dist[u] + 1, q.push(v), p[v] = u; + } + } + } + augment(t, INF); + if (f == 0) break; + mf += f; + } + printf("%d\n", mf); + } + return 0; +} diff --git a/uva_cpp_clean/11507/11507-20.cpp b/uva_cpp_clean/11507/11507-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..734b4cc2d330a7b723a0d584ada127c5fac36ff2 --- /dev/null +++ b/uva_cpp_clean/11507/11507-20.cpp @@ -0,0 +1,42 @@ +#include +#include + +using namespace std; + +bool change(char a, char s, char a2) +{ + if(a == 'x' && s == '-') return true; + if(a == 'y' && a2 == 'y' && s == '+') return true; + if(a == 'z' && a2 == 'z' && s == '+') return true; + return false; +} + +int main() +{ + int L; + + while( cin >> L && L ) + { + --L; + + int sign = 1; + char axis = 'x'; + + for(int i=0 ; i> decision; + + if( decision[0]=='N' ) continue; + + if( change(axis, decision[0], decision[1]) ) sign *= -1; + + if(axis == 'x') axis = decision[1]; + else if(axis == 'y' && decision[1] == 'y') axis = 'x'; + else if(axis == 'z' && decision[1] == 'z') axis = 'x'; + } + + cout << (sign==1 ? '+' : '-') << axis << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11507/11507-21.cpp b/uva_cpp_clean/11507/11507-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16005fa56e5e1bb385b8c9a1dfda14b369de27f0 --- /dev/null +++ b/uva_cpp_clean/11507/11507-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11507 + Name: Bender B. Rodríguez Problem + Problem: https://onlinejudge.org/external/115/11507.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char str[7]; + int dx[] = { 1, 0, -1, 0 }, + dw[] = { 0, 1, 0, -1 }, n; + while (cin >> n && n) { + int x=1, y=0, z=0; + for (int i=1; i> str; + if (str[1] != 'o') { + int &w = str[1]=='y' ? y : z, + d = str[0]=='+' ? 1 : 3; + for (int k=0; k<4; ++k) + if (x == dx[k] && w == dw[k]) { + x = dx[(k+d)&3]; + w = dw[(k+d)&3]; + break; + } + } + } + + cout << (x>0 || y>0 || z>0 ? '+' : '-') + << (x ? 'x' : z ? 'z' : 'y') + << endl; + } +} diff --git a/uva_cpp_clean/11507/main.cpp b/uva_cpp_clean/11507/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a49d09eb64530459ee67b2e0b54dd4b913d96298 --- /dev/null +++ b/uva_cpp_clean/11507/main.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; + +string position(string bend, string next_bend){ + string res; + char bend_sign = bend[0]; + char next_bend_sign = next_bend[0]; + char bend_direction = bend[1]; + char next_bend_direction = next_bend[1]; + if(next_bend == "No") return bend; + if(bend == "+x") return next_bend; + if(bend == "-x"){ + if(next_bend_sign == '-') res = "+"; + else res= "-"; + return res+next_bend_direction; + } + if(bend == next_bend) return "-x"; + if(next_bend_direction == bend_direction) return "+x"; + return bend; +} + +int main(){ + string bend, next_bend; + int n; + while(cin >> n && n!= 0){ + cin >> bend; + if(bend == "No") bend = "+x"; + for(n; n>2; n--){ + cin >> next_bend; + bend = position(bend, next_bend); + } + cout << bend << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/11512/11512-19.cpp b/uva_cpp_clean/11512/11512-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88270a87dd9b51783d24d339eeb46e549cc1cbe4 --- /dev/null +++ b/uva_cpp_clean/11512/11512-19.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include +using namespace std; + +int size = 4; +char characters[4] = {'A', 'C', 'G', 'T'}; + +struct node { + string pattern; + bool isword; + bool issuffix; + int nodes; + unordered_map map; + + node() { isword = issuffix = false; pattern = ""; nodes = 0; } + + node(string pattern, bool isword, bool issuffix, unordered_map & map, int nodes) { + this->pattern = pattern; + this->isword = isword; + this->issuffix = issuffix; + this->map = map; + this->nodes = nodes; + } + + void insert(string word, int i, bool isword) { + if (i < word.length()) { + if (nodes == 0) pattern = word.substr(i, word.length()-i); + else { + int j; + for (j = 0; i < word.length() && j < pattern.length() && word[i] == pattern[j]; i++, j++); + if (j == pattern.length()) { + if (i < word.length()) { + if (!map.count(word[i])) map.insert(pair(word[i], new node())); + map[word[i]]->insert(word, i, isword); + } + } else { + unordered_map aux = map; + map = unordered_map (); + map.insert(pair(pattern[j], new node(pattern.substr(j, pattern.length()-j), this->isword, issuffix, aux, nodes))); + pattern = pattern.substr(0, j); + issuffix = false; + this->isword = false; + if (i < word.length()) { + map.insert(pair(word[i], new node())); + map[word[i]]->insert(word, i, isword); + } + } + //if (!map.count(word[i])) map.insert(pair(word[i], new node(, isword, issuffix, map))); + //else map[word[i]]->insert(word, i+1, isword); + } + if (i >= word.length() || nodes == 0) { + issuffix = true; + this->isword |= isword; + } + nodes++; + } + } + + int search(string word, int i) { + int j; + for (j = 0; i < word.length() && j < pattern.length() && word[i] == pattern[j]; i++, j++); + if (j == pattern.length()) { + if (i == word.length()) return isword? 2: issuffix? 1: 0; + else { + if (i >= word.length() || !map.count(word[i])) return 0; + return map[word[i]]->search(word, i); + } + } else return 0; + } + + void print(int i){ + cout << i << "p:" << pattern << endl; + for (auto it = map.begin(); it != map.end(); ++it) it->second->print(i+1); + } + + pair lrs() { + pair res = pair("", 0); + if (map.empty()) return res; + for (int i = 0; i < size; i++) { + if (map.count(characters[i])) { + pair t = map[characters[i]]->lrs(); + if (pattern.length() + t.first.length() > res.first.length()) { + res = t; + res.first = pattern + res.first; + if (res.second == 0) res.second = nodes; + } + } + } + return res; + } +}; + +struct suffixtree { + node* root; + + suffixtree() { root = new node(); } + + ~suffixtree() { delete root; } + + void insert(string word) { + for (int j = word.length() - 1; j >= 0; j--) { + if (j == 0) root->insert(word, j, true); + else root->insert(word, j, false); + } + } + + int search(string word) { return root->search(word, 0); } + + void clear() { delete root; root = new node(); } + + void print() { root->print(0); } + + pair lrs() { + return root->lrs(); + } +}; + +int main() { + suffixtree tree; + string word; + int N; + cin >> N; + while (N--) { + cin >> word; + tree.insert(word); + pair res = tree.lrs(); + if (res.second >= 2) cout << res.first << " " << res.second << '\n'; + else printf("No repetitions found!\n"); + tree.clear(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11512/11512-21.cpp b/uva_cpp_clean/11512/11512-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cf985f5d30962d6085e754b59ae71dbfafb052a --- /dev/null +++ b/uva_cpp_clean/11512/11512-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11512 + Name: GATTACA + Problem: https://onlinejudge.org/external/115/11512.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +const char DNA[] = "ACGT"; +int D[128]; + + +int S[1000143][4], C[1000143], Sc; +bool addWord(const char w[]) { + int cur = 0; + for (int i=0; w[i]; ++i) { + int wi = D[w[i]]; + if (!S[cur][wi]) { + C[Sc] = 0; + memset(S[Sc], 0, 4*sizeof(int)); + S[cur][wi] = Sc++; + } + cur = S[cur][wi]; + C[cur]++; + } + return C[cur] > 1; +} + +int bestl, bestr; +char best[1024], path[1024]; +void findBest(int l, int cur) { + if (C[cur] > 1 && l > bestl) { + path[l] = 0; + strcpy(best, path); + bestl = l; + bestr = C[cur]; + } + + for (int i=0; i<4; ++i) + if (S[cur][i]) { + path[l] = DNA[i]; + findBest(l+1, S[cur][i]); + } +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=0; DNA[i]; ++i) + D[DNA[i]] = i; + + int n; + cin >> n; + cin.ignore(100, '\n'); + while (n--) { + char word[1024]; + cin.getline(word, 1024); + memset(S, C[0]=0, 100*sizeof(int)); + Sc = 1; + + int len = strlen(word); + for (int i=0; word[i]; ++i) + if (addWord(word+i)) + break; + + bestl = bestr = 0; + findBest(0, 0); + if (bestl) + cout << best << ' ' << bestr << endl; + else + cout << "No repetitions found!\n"; + } +} diff --git a/uva_cpp_clean/11513/11513-21.cpp b/uva_cpp_clean/11513/11513-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..923b7a8afdd919b98de282b6e33fdc8053577b5a --- /dev/null +++ b/uva_cpp_clean/11513/11513-21.cpp @@ -0,0 +1,96 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11513 + Name: 9 Puzzle + Problem: https://onlinejudge.org/external/115/11513.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char MovN[6][3] = { + "H1", "H2", "H3", + "V1", "V2", "V3" +}; +int Mov[6][3] = { + { 0, 1, 2 }, + { 3, 4, 5 }, + { 6, 7, 8 }, + { 6, 3, 0 }, + { 7, 4, 1 }, + { 8, 5, 2 } +}; +int X[9]; + + +struct State { + char s[9]; + State(){} + State(const State &t){ memcpy(s, t.s, 9); } + int toInt() { + int r = 0; + for (int i=0; i<9; ++i) + r = r*10 + s[i]; + return r; + } +}; + +unordered_map P; +void reverse_bfs() { + State src; + for (int i=0; i<9; ++i) + src.s[i] = i; + + queue q; + q.push(src); + P[src.toInt()] = -1; + + while (!q.empty()) { + State s = q.front(); q.pop(); + + for (int k=0; k<6; ++k) { + State t(s); + swap(t.s[Mov[k][0]], t.s[Mov[k][2]]); + swap(t.s[Mov[k][0]], t.s[Mov[k][1]]); + int vid = t.toInt(); + if (P.emplace(vid, k).second) + q.push(t); + } + } +} + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + reverse_bfs(); + + State t; + while (cin>>X[0]>>X[1]>>X[2]>>X[3]>>X[4]>>X[5]>>X[6]>>X[7]>>X[8]) { + for (int i=0; i<9; ++i) + t.s[i] = X[i]-1; + + auto it = P.find(t.toInt()); + if (it == P.end()) cout << "Not solvable\n"; + else { + int d = 0; + string result = ""; + for (int k=it->second; k!=-1; ++d) { + swap(t.s[Mov[k][0]], t.s[Mov[k][1]]); + swap(t.s[Mov[k][0]], t.s[Mov[k][2]]); + result += MovN[k]; + k = P[t.toInt()]; + } + + cout << d << ' ' << result << '\n'; + } + } +} diff --git a/uva_cpp_clean/11516/11516-21.cpp b/uva_cpp_clean/11516/11516-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7e7fda82eab0aaf7a5b3bec54207408bbcd6a2a --- /dev/null +++ b/uva_cpp_clean/11516/11516-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11516 + Name: WiFi + Problem: https://onlinejudge.org/external/115/11516.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------------------- + +int X[100143], n, m; +bool check(int len) { + int last = -(int)1e9, + cnt = 0; + for (int i=0; i len) { + last = X[i] + len; + if (++cnt > n) return 0; + } + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(1); + + int T = readUInt(); + while (T--) { + n = readUInt(); + m = readUInt(); + for (int i=0; i>1; + if (check(mid)) + hi = mid - 1; + else + lo = mid + 1; + } + cout << (hi+1)/10.0 << '\n'; + } +} diff --git a/uva_cpp_clean/11517/11517-13.cpp b/uva_cpp_clean/11517/11517-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1235a25a9b5eb921ea1821729cdf5b79a1d9bb65 --- /dev/null +++ b/uva_cpp_clean/11517/11517-13.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 100000 + +int main() +{ + int t, n, V; + + cin >> t; + + while (t--) + { + cin >> V >> n; + + int dp[MAX]; + + int coinValue[105]; + + memset(coinValue, 0, sizeof coinValue); + + for (int i = 1; i <= n; i++) + { + cin >> coinValue[i]; + } + + for (int i = 0; i < MAX; i++) + { + dp[i] = MAX; + } + + dp[0] = 0; + + sort(coinValue + 1, coinValue + n + 1); + + for (int i = 1; i <= n; ++i) + { + for (int j = MAX; j >= 0; j--) + { + if (j + coinValue[i] <= MAX) + { + dp[j + coinValue[i]] = min(dp[j] + 1, dp[j + coinValue[i]]); + } + } + } + + while (dp[V] == MAX) + { + V++; + } + + cout << V << " " << dp[V] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11517/11517-21.cpp b/uva_cpp_clean/11517/11517-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ccdbca6598238a541170144545ac7b6c0d37eb2 --- /dev/null +++ b/uva_cpp_clean/11517/11517-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11517 + Name: Exact Change + Problem: https://onlinejudge.org/external/115/11517.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int C[143], DP[20086]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, p; + cin >> T; + while (T--) { + cin >> p >> n; + for (int i=0; i> C[i]; + + memset(DP, 127, sizeof(DP)); + DP[0] = 0; + for (int i=0; i=0; --j) + DP[j+c] = min(DP[j+c], DP[j]+1); + } + + int j; + for (j=p; DP[j]>100; ++j); + cout << j << ' ' << DP[j] << '\n'; + } +} diff --git a/uva_cpp_clean/11517/11517-9.cpp b/uva_cpp_clean/11517/11517-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6aae338e22060cb4e709a500c8ca9654e566987d --- /dev/null +++ b/uva_cpp_clean/11517/11517-9.cpp @@ -0,0 +1,26 @@ +#include + +#define MAX_N 110 +#define MAX_W 10010 +#define INF 1e5 + +using namespace std; + +int tc,n,price,v[MAX_N],dp[MAX_W+10]; + +int main(){ + scanf("%d",&tc); + while(tc--){ + fill(dp,dp+MAX_W,INF); + scanf("%d %d",&price,&n); + for(int i=0;i=0;j--) + if(j+v[i]<=MAX_W && dp[j+v[i]]>dp[j]+1) + dp[j+v[i]] = dp[j]+1; + while(dp[price]==INF) price++; + printf("%d %d\n",price,dp[price]); + } + return(0); +} diff --git a/uva_cpp_clean/11518/11518-19.cpp b/uva_cpp_clean/11518/11518-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06cd40f2ce35d376834ccf1f293087ac5896eb6e --- /dev/null +++ b/uva_cpp_clean/11518/11518-19.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vii; +typedef vector vi; + +vector AdjList; +vector knocked; + +int proc(int k) { + int tiles = 0; + queue q; + q.push(k); + while (!q.empty()) { + if (!knocked[q.front()]) { + knocked[q.front()] = true; + tiles++; + for (int i = 0; i < AdjList[q.front()].size(); i++) { + q.push(AdjList[q.front()][i]); + } + } + q.pop(); + } + return tiles; +} + +int main() { + int T; + cin >> T; + while (T--) { + int n, m, l; + cin >> n >> m >> l; + AdjList.assign(n, vi()); + knocked.assign(n, false); + for (int i = 0; i < m; i++) { + int s, d; + cin >> s >> d; + AdjList[s-1].push_back(d-1); + } + int tiles = 0; + for (int i = 0; i < l; i++) { + int k; + cin >> k; + tiles += proc(k-1); + } + printf("%d\n", tiles); + } + return 0; +} diff --git a/uva_cpp_clean/11518/11518-21.cpp b/uva_cpp_clean/11518/11518-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e7bc6dda2a1f26fc260ce9524cc2d2e6c312522 --- /dev/null +++ b/uva_cpp_clean/11518/11518-21.cpp @@ -0,0 +1,84 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11518 + Name: Dominos 2 + Problem: https://onlinejudge.org/external/115/11518.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +// ------------------------------------------------------------------- + +#define MAXN 10017 +vector adj[MAXN]; +bool seen[MAXN]; +void dfs(int u) { + seen[u] = 1; + for (int v: adj[u]) + if (!seen[v]) + dfs(v); +} + + +int main() { + int T = readUInt(); + while (T--) { + int n = readUInt(), + m = readUInt(), + l = readUInt(); + + for (int i=0; i +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t,n,m,l,u,v; + cin>>t; + while(t--){ + cin>>n>>m>>l; + vvi g(n); + for(int i=0;i>u>>v; + u--;v--; + g[u].push_back(v); + } + vi s(n); + functiondfs=[&](int u){ + s[u]=1; + for(int v:g[u]) + if(!s[v]) + dfs(v); + }; + for(int i=0;i>u; + u--; + if(!s[u]) + dfs(u); + } + int c=0; + for(int x:s)c+=x; + cout< + +#define SIZE 10010 + +using namespace std; + +int tc, n, m, l, a, b; +vector G[SIZE]; +bool vis[SIZE]; + +void dfs(int u){ + vis[u] = true; + for(int v = 0; v < G[u].size(); v++) + if( not vis[G[u][v]] ) + dfs(G[u][v]); +} + +void clear_(){ + for(int it = 1; it <= n; it++) G[it].clear(); + memset(vis, false, sizeof vis); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d", &n, &m, &l); + for(int it = 0; it < m; it++){ + scanf("%d %d", &a, &b); + G[a].push_back(b); + } + for(int it = 0; it < l; it++){ + scanf("%d", &a); + dfs(a); + } + printf("%d\n", accumulate(vis, vis + n + 1, 0)); + clear_(); + } + return(0); +} diff --git a/uva_cpp_clean/1152/1152-14.cpp b/uva_cpp_clean/1152/1152-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9aaf74597b7b18f9bfc9b5d5948e0676e3c49dff --- /dev/null +++ b/uva_cpp_clean/1152/1152-14.cpp @@ -0,0 +1,130 @@ +/*************************************************** + * Problem name : 1152 4 - Values whose sum is 0.cpp + * OJ : Uva + * Verdict : Accepted + * Date : 31.05.2017 + * Problem Type : Binary Search + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +//~ #define MAX 33554433 +#define MAX 16000001 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} + +int flag ; +int LowerBound(int ar[], int n, int value) { + int high = n - 1; + int low = 0; + int ans = -1; + while (high >= low) { + int mid = (high + low) / 2; + if (ar[mid] == value) { + ans = mid; + high = mid - 1; + } else if (ar[mid] > value) { + high = mid - 1; + } else { + low = mid + 1; + } + } + if (ans == -1) { + flag = 1; + ans = high; + } + return ans + 1; +} +int UpperBound(int ar[], int n, int value) { + int high = n - 1; + int ans, low = 0; + while (high >= low) { + int mid = (high + low) / 2; + if (ar[mid] == value) { + ans = mid; + low = mid + 1; + } else if (ar[mid] > value) { + high = mid - 1; + } else { + low = mid + 1; + } + } + return ans + 1; +} +int main () { + int A[MAX], B[MAX], C[MAX], D[MAX]; + int a[MAX], b[MAX]; + int tc, n; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d", &n); + for (int i = 0; i < n; i++) { + scanf("%d %d %d %d", &A[i], &B[i], &C[i], &D[i]); + } + int k = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + a[k] = A[i] + B[j]; + b[k] = C[i] + D[j]; + k++; + } + } + sort(b, b + k); + int sum = 0; + for (int i = 0; i < k; i++) { + + int x = a[i] * -1; + flag = 0; + int l = LowerBound(b, k, x); + if (flag == 0) { + int u = UpperBound(b, k, x); + int ans = (u - l) + 1; + sum = sum + ans; + } + + } + printf("%d\n", sum); + if(t!=tc) printf("\n"); + } + + return 0; +} + diff --git a/uva_cpp_clean/1152/1152-19.cpp b/uva_cpp_clean/1152/1152-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5425b1db3d03b71637ed7b1072f65469c067231d --- /dev/null +++ b/uva_cpp_clean/1152/1152-19.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +using namespace std; + +int main() { + vector A, B, C, D, sum; + unordered_map m; + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + A.clear(), B.clear(), C.clear(), D.clear(), sum.clear(), m.clear(); + for (int i = 0; i < n; i++) { + int a, b, c, d; + cin >> a >> b >> c >> d; + A.push_back(a), B.push_back(b), C.push_back(c), D.push_back(d); + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + int sum1 = A[i] + B[j], sum2 = C[i] + D[j]; + sum.push_back(sum1); + m[sum2]++; + } + } + int res = 0; + for (int i = 0; i < sum.size(); i++) { + res += m[-sum[i]]; + } + printf("%d\n", res); + if (t) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/1152/1152-4.cpp b/uva_cpp_clean/1152/1152-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1d292717fec6281ba3fcf17f197e47fb54202a1 --- /dev/null +++ b/uva_cpp_clean/1152/1152-4.cpp @@ -0,0 +1,84 @@ +#include +#include + +#define offset 536870912 + +int main() +{ + char partialsum[offset * 2]; + + int tests; + std::cin >> tests; + + while (tests--) + { + int n; + std::cin >> n; + + memset(partialsum, 0, sizeof(partialsum)); + + int *a = new int[n]; + int *b = new int[n]; + int *c = new int[n]; + int *d = new int[n]; + + for (int i = 0; i < n; i++) + { + int ia, ib, ic, id; + std::cin >> ia >> ib >> ic >> id; + + a[i] = ia; + b[i] = ib; + c[i] = ic; + d[i] = id; + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int sum = a[i] + b[j]; + + if (sum < 0) + { + sum = std::abs(sum) + offset; + } + + (*(partialsum + sum))++; + } + } + + int zeros = 0; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int sum = c[i] + d[j]; + + if (sum > 0) + { + sum += offset; + } + else + { + sum = std::abs(sum); + } + + zeros += *(partialsum + sum); + } + } + + delete[] a; + delete[] b; + delete[] c; + delete[] d; + + std::cout << zeros << std::endl; + + if (tests != 0) + { + std::cout << std::endl; + } + } +} diff --git a/uva_cpp_clean/11520/11520-21.cpp b/uva_cpp_clean/11520/11520-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9f2da380efe4435c787f8d59a77a74e70543c00 --- /dev/null +++ b/uva_cpp_clean/11520/11520-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11520 + Name: Fill the Square + Problem: https://onlinejudge.org/external/115/11520.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n; + char M[12][12] = {}; + for (int i=1; i<=n; ++i) + cin >> (M[i]+1); + + cout << "Case " << cse << ":\n"; + for (int i=1; i<=n; ++i) { + for (int j=1; j<=n; ++j) + if (M[i][j] == '.') { + char ch = 'A'; + while (M[i-1][j]==ch || M[i][j-1]==ch || M[i+1][j]==ch || M[i][j+1]==ch) + ++ch; + M[i][j] = ch; + } + cout << (M[i]+1) << '\n'; + } + } +} diff --git a/uva_cpp_clean/11525/11525-19.cpp b/uva_cpp_clean/11525/11525-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6e7407bcd164123f6b7c5c17a6d5c0b739b23a7 --- /dev/null +++ b/uva_cpp_clean/11525/11525-19.cpp @@ -0,0 +1,66 @@ +#include +#include +using namespace std; + +typedef vector vi; + +int LSOne(int n) { + return n & (-n); +} + +void ft_create(vi &ft, int n) { + ft.assign(n + 1, 0); +} + +int ft_rsq(const vi &ft, int b) { + int sum = 0; for (; b; b -= LSOne(b)) sum += ft[b]; + return sum; +} + +int ft_rsq(const vi &ft, int a, int b) { + return ft_rsq(ft, b) - (a == 1 ? 0 : ft_rsq(ft, a - 1)); +} + +void ft_adjust(vi &ft, int k, int v) { + for (; k < (int)ft.size(); k += LSOne(k)) ft[k] += v; +} + +int binsearch(const vi &ft, int a, int b, int val) { + if (a == b) return ft_rsq(ft, a + 1) == val? a: -1; + else if (a > b) return -1; + int mid = (a+b)/2; + int ret = ft_rsq(ft, mid + 1); + if (ret == val) { + int v = binsearch(ft, a, mid - 1, val); + if (v == -1) return mid; + return v; + } + else if (ret > val) { + return binsearch(ft, a, mid - 1, val); + } + else return binsearch(ft, mid + 1, b, val); +} + +int main() { + int T; + cin >> T; + while (T--) { + int K; + cin >> K; + vi ft; + ft_create(ft, K); + for (int i = 1; i < K; i++) ft_adjust(ft, i+1, 1); + bool first = false; + for (int i = 0; i < K; i++) { + int val; + cin >> val; + int p = binsearch(ft, 0, K - 1, val); + if (first) printf(" "); + else first = true; + printf("%d", p+1); + ft_adjust(ft, p+1, -1); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11526/11526-21.cpp b/uva_cpp_clean/11526/11526-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99797bd3645850830de97c833162487668df6dac --- /dev/null +++ b/uva_cpp_clean/11526/11526-21.cpp @@ -0,0 +1,65 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11526 + Name: H(n) + Problem: https://onlinejudge.org/external/115/11526.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +typedef unsigned int uint; +long long F[50] = { 1, 2 }; +uint findNext(uint n, long long i) { + long long j=i+1; + if (n/j != n/i) + return j; + + int fi = 0; + while (n/j == n/i) + j = i + F[++fi]; + + return findNext(n, i + F[fi-1]); +} + +long long H1(int n) { + long long res = 0; + for (uint i=1, j; i<=n; i = j) { + j = n/i==1 ? n+1 : findNext(n, i); + res += (j-i) * (n/i); + } + return res; +} + +// ------------------------------------------ + +long long H2(int n) { + long long res = 0; + for (int j=1, k=n; ;j=n/k) { + int r = n/j, l = n/(j+1); + res += (long long) j * (r-l); + k -= r-l; + if (!k) break; + } + return res; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i=2; i<50; ++i) + F[i] = F[i-1] + F[i-2]; + + int T, n; + cin >> T; + while (T--) { + cin >> n; + cout << (n<1 ? 0 : H2(n)) << endl; + } +} diff --git a/uva_cpp_clean/11526/11526-5.cpp b/uva_cpp_clean/11526/11526-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..dc0ff3a4fdf958bf5ff1a95ccf54f0bfcf77b831 --- /dev/null +++ b/uva_cpp_clean/11526/11526-5.cpp @@ -0,0 +1,23 @@ +#include +#include +int main() +{ + long long n,t,i,r; + int p; + scanf("%lld",&t); + while(t--) + { + scanf("%lld",&n); + r=0; + p=int(sqrt(n))+1; + for(i=1;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11530 + Name: SMS Typing + Problem: https://onlinejudge.org/external/115/11530.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int num[] = { 1,2,3, 1,2,3, 1,2,3, 1,2,3, 1,2,3, 1,2,3,4, 1,2,3, 1,2,3,4 }; + +int main() { + int n; + cin >> n; + char line[200]; + cin.getline(line,200); + for(int cse=1; cse<=n; cse++) { + cin.getline(line, 200); + int score = 0; + for(int i=0; line[i]; i++) + if (line[i]==' ') score++; + else score += num[line[i]-'a']; + + printf("Case #%d: %d\n", cse, score); + } +} diff --git a/uva_cpp_clean/11530/11530-5.cpp b/uva_cpp_clean/11530/11530-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0e071dd1c6013ee4771d383f3a52686b692d05f3 --- /dev/null +++ b/uva_cpp_clean/11530/11530-5.cpp @@ -0,0 +1,30 @@ +#include +#include + +int main() +{ + char a[107]; + int n,s,j,t,i; + scanf("%d",&t); + getchar(); + for(i=1;i<=t;i++) + { + gets(a); + s=0; + n=strlen(a); + for(j=0;j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11532 + Name: Simple Adjacency Maximization + Problem: https://onlinejudge.org/external/115/11532.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, p, q; + cin >> T; + while (T-- && cin >> p >> q) { + unsigned long long r = 0, b = 1; + while (p > 2*q) + r |= b, + b<<= 1, + --p; + + for (int i=1; ; b<<=1, ++i) + if (i%3 != 2) + if (!p) break; + else r |= b, --p; + else + if (!q) break; + else --q; + + cout << r << '\n'; + } +} diff --git a/uva_cpp_clean/11536/11536-11.cpp b/uva_cpp_clean/11536/11536-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3535e6f0bed94c528597dcccfd7724a6dd7ff6e5 --- /dev/null +++ b/uva_cpp_clean/11536/11536-11.cpp @@ -0,0 +1,87 @@ +#include +/* +Problem: 11536 - Smallest Sub-Array +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2531 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int T, M, N, K; +int sequence[1000001]; +int p1, p2, testcase = 1, len, curr_size; + +int main () { + scanf("%d\n", &T); + + // init the sequence + sequence[0] = 1; sequence[1] = 2; sequence[2] = 3; + + while ( T-- ){ + scanf("%d %d %d\n", &N, &M, &K); + + // create the sequence + int last_three = 6; + for (int i = 3; i < N; i++){ + sequence[i] = last_three%M + 1; + last_three -= sequence[i-3]; + last_three += sequence[i]; + } + + // now search for the segment + len = INT_MAX; + p1 = p2 = curr_size = 0; + int freq[1001] = {}; + + while ( p1 <= p2 && p2 < N ){ + if (curr_size < K){ + if (sequence[p2] <= K && !freq[sequence[p2]]) + curr_size++; + + freq[sequence[p2]]++; + p2++; + } + + if (curr_size == K){ + while (sequence[p1] > K || ((sequence[p1] <= K) && (freq[sequence[p1]] > 1))){ + freq[sequence[p1]]--; + p1++; + } + + len = min(len, p2-p1); + freq[sequence[p2]]++; p2++; + } + } + + printf("Case %d: ", testcase++); + + if (len == INT_MAX) printf("sequence nai\n"); + else printf("%d\n", len); + } + + return 0; +} +/* +Sample input:- +----------------- +2 +20 12 4 +20 12 8 + +Sample output:- +----------------- +Case 1: 13 +Case 2: sequence nai + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11538/11538-13.cpp b/uva_cpp_clean/11538/11538-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd30a2e7d0e3c0b1c37d0a1d5d634f6f494e5233 --- /dev/null +++ b/uva_cpp_clean/11538/11538-13.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +typedef long long ll; + +ll n, m; + +int main() +{ + while (cin >> n >> m && (n != 0 || m != 0)) + { + if (n > m) swap(n, m); + + ll d = 0; + ll a = n * (n - 1); + ll b = m * (m - 1); + + ll ans = 0; + + ans += (a * m) + (b * n); + ans += 4 * (2 * (n * (n - 1) * (n - 2) / 6) + (m - n + 1) * n * (n - 1) / 2); + + cout << (ll) ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11541/11541-19.cpp b/uva_cpp_clean/11541/11541-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a18b9d4eb8db2b4bcdcacff2a79fe9bf698a0a4 --- /dev/null +++ b/uva_cpp_clean/11541/11541-19.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + string s; + getline(cin, s); + for (int i = 1; i <= n; i++) { + getline(cin, s); + char c; + int acum = 0; + printf("Case %d: ", i); + for (int j = 0; j < s.length(); j++) { + if (isalpha(s[j])) { + while (acum--) printf("%c", c); + c = s[j]; + acum = 0; + } else acum = 10*acum + (s[j]-'0'); + } + while (acum--) printf("%c", c); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11541/11541-20.cpp b/uva_cpp_clean/11541/11541-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..307156aec414a64338a7710fd054e91380cb42e4 --- /dev/null +++ b/uva_cpp_clean/11541/11541-20.cpp @@ -0,0 +1,32 @@ +#include +#include + +char line[105]; + +int main() +{ + int tc, nCase; + char dump; + + scanf("%d%c", &tc, &dump); + nCase = 0; + while( tc-- ) + { + scanf("%s", line); + + printf("Case %d: ", ++nCase); + int len = strlen(line); + for(int i=0 ; i='A' && line[i]<='Z' ) letter = line[i++]; + + int counter = 0; + while( line[i]>='0' && line[i]<='9' ) counter = (counter*10)+(line[i++]-'0'); + + for(int j=0 ; j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11541 + Name: Decoding + Problem: https://onlinejudge.org/external/115/11541.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T; + cin>>T; + for (int cse = 1; cse<=T; cse++) { + printf("Case %d: ", cse); + + char c, ch = getchar(); + while (ch<'A' || ch>'Z') ch = getchar(); + + while (ch != '\n') { + int cnt = 0; + while ((c=getchar())>='0' && c<='9') + cnt = cnt*10 + c - '0'; + + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +int main() +{ + int t,no=0; + char s[1000],c; + sc("%d",&t); + getchar(); + while(t--) + { + gets(s); + int l=strlen(s); + pf("Case %d: ",++no); + for(int i=0;i='A'&&s[i]<='Z') + c=s[i]; + else if(s[i]>='0'&&s[i]<='9') + { + p+=s[i]-'0'; + i++; + while(s[i]>='0'&&s[i]<='9') + { + p=p*10+s[i]-'0'; + i++; + } + i--; + for(int j=0;j + +using namespace std; + +const int LEN = 210; + +int tc; +char line[LEN]; + +int main(){ + scanf("%d\n", &tc); + for(int t = 1; t <= tc; t++){ + printf("Case %d: ", t); + scanf("%s", line); + for(int it = 0; line[it]; ){ + char ch = line[it]; + int ct = 0; + for(it++; line[it] and isdigit(line[it]); it++) + ct = 10 * ct + ( line[it] - '0' ); + while(ct--) putchar(ch); + } + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/11545/11545-21.cpp b/uva_cpp_clean/11545/11545-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2846550d954806edc60b0b0cfb4660373a7309a --- /dev/null +++ b/uva_cpp_clean/11545/11545-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11545 + Name: Avoiding Jungle in the Dark + Problem: https://onlinejudge.org/external/115/11545.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +struct State { + int i, d; + State(int i, int d):i(i),d(d){} + bool operator < (const State &o) const { return d-i > o.d-o.i; } +}; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, D[1043][24]; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + string M; + cin >> M; + for (int i=0; i<=M.length(); ++i) + for (int j=0; j<24; ++j) + D[i][j] = INF; + + int result = -1; + priority_queue q; + q.push(State(0, D[0][0]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.i][s.d%24]) continue; + if (M[s.i] == 'D') { + result = s.d; + break; + } + + + // rest here one more 8hours + if (s.d+8 < D[s.i][(s.d+8)%24]) + q.push(State(s.i, D[s.i][(s.d+8)%24] = s.d+8)); + + + int bj = -1; + for (int i=1; i<=16 && s.i+i=12) + break; + } + else bj = i; + } + + if (bj != -1) { + int ii = s.i + bj, + dd = s.d + bj + (M[ii]!='D' ? 8 : 0); + + if (dd < D[ii][dd%24]) + q.push(State(ii, D[ii][dd%24] = dd)); + } + } + cout << "Case #" << cse << ": " << result << endl; + } +} diff --git a/uva_cpp_clean/11547/11547-19.cpp b/uva_cpp_clean/11547/11547-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15dd9a12bb927f97772f8f2fda3076f21ad527ba --- /dev/null +++ b/uva_cpp_clean/11547/11547-19.cpp @@ -0,0 +1,15 @@ +#include +#include +using namespace std; + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + int n; + scanf("%d", &n); + n = fabs((((n*315)+36962)%100)/10); + printf("%d\n", n); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11547/11547-21.cpp b/uva_cpp_clean/11547/11547-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..837ab1ed494b0884f295ff0167a9da522c389f5d --- /dev/null +++ b/uva_cpp_clean/11547/11547-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11547 + Name: Automatic Answer + Problem: https://onlinejudge.org/external/115/11547.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int main() { + int n; + cin >> n; + while (n--) { + int x; + cin >> x; + x = (x * 63 + 7492) * 5 - 498; + if (x < 0) x = -x; + cout << (x / 10) % 10 << endl; + } +} diff --git a/uva_cpp_clean/11547/11547-5.cpp b/uva_cpp_clean/11547/11547-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bf4d48a7d2099a3424bed7ac5828d427bf96ee58 --- /dev/null +++ b/uva_cpp_clean/11547/11547-5.cpp @@ -0,0 +1,16 @@ +#include +int main() +{ +long long t,n,a; +scanf("%lld",&t); +while(t--) +{ +scanf("%lld",&n); +a=((((((n*567)/9)+7492)*235)/47)-498); +if(a<0) +a*=-1; +a=(a/10)%10; +printf("%lld\n",a); +} +return 0; +} diff --git a/uva_cpp_clean/11547/11547-9.cpp b/uva_cpp_clean/11547/11547-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..571c21bcc2aa6f318f35ce3efff55118b47f79a3 --- /dev/null +++ b/uva_cpp_clean/11547/11547-9.cpp @@ -0,0 +1,20 @@ +#include + +int main(){ + int n, a; + scanf("%d", &n); + while(n--){ + scanf("%d", &a); + a *= 567; + a /= 9; + a += 7492; + a *= 235; + a /= 47; + a -= 498; + a /= 10; + a = a % 10; + a = ( a >= 0 ) ? a : -a; + printf("%d\n", a); + } + return(0); +} diff --git a/uva_cpp_clean/11547/main.cpp b/uva_cpp_clean/11547/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9f8185f9ce42eed9e1130fe3994b617da452af9 --- /dev/null +++ b/uva_cpp_clean/11547/main.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main(){ + + int t, n; + cin >> t; + for(t; t>0; t--){ + cin >> n; + if(n >= 0){ + n = 315*n + 62; + n = n/10; + n = abs(n%10); + }else{ + n = 315*n + 36962; + n = n/10; + n = abs(n%10); + } + cout << n << endl; + + } + + return 0; +} diff --git a/uva_cpp_clean/11548/11548-21.cpp b/uva_cpp_clean/11548/11548-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d74be7b0e0f1fe05c5c4b62d72e23638b6b293a4 --- /dev/null +++ b/uva_cpp_clean/11548/11548-21.cpp @@ -0,0 +1,74 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11548 + Name: Blackboard Bonanza + Problem: https://onlinejudge.org/external/115/11548.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool isHopefull(const string &s1, const string &s2, int best) { + if (s1.length()<=best || s2.length()<=best) + return 0; + + char cnt[26] = {}; + for (int i=0; i best) + return 1; + } + return 0; +} + +int candies(const string &s1, const string &s2) { + int result = 0; + for (int j=0; j s2.length(); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + string S[77]; + while (T-- && cin >> n) { + for (int i=0; i> S[i]; + sort(S, S+n, comp); + + int best = 0; + for (int d=1; d + +#define SIZE 100 + +using namespace std; + +int tc, n, ans, ct; +char word[SIZE][SIZE]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d\n", &n); + for(int it = 0; it < n; it++) scanf("%s", word[it]); + ans = 0; + for(int it1 = 0; it1 < n; it1++) + for(int it2 = 0; it2 < n; it2++) + if( it1 != it2 ) + for(int pos = 0; word[it1][pos]; pos++){ + ct = 0; + for(int it = 0; word[it1][pos + it] and word[it2][it]; it++) + ct += ( word[it1][pos + it] == word[it2][it] ); + ans = max(ans, ct); + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11549/11549-19.cpp b/uva_cpp_clean/11549/11549-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3945fa5f3454961d21939f20d98c5cd668d48351 --- /dev/null +++ b/uva_cpp_clean/11549/11549-19.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +using namespace std; + +int main() { + int T; + cin >> T; + while (T--) { + long long n, k; + cin >> n >> k; + long long limit = pow(10, n) - 1; + unordered_set s; + long long maxi = k; + long long val = k; + do { + s.insert(val); + if (val > maxi) maxi = val; + val = val*val; + while (val > limit) val /= 10; + } while (!s.count(val)); + printf("%lld\n", maxi); + } + return 0; +} diff --git a/uva_cpp_clean/11549/11549-4.cpp b/uva_cpp_clean/11549/11549-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d55613142c1be67867f1a951606f2e2c4975204 --- /dev/null +++ b/uva_cpp_clean/11549/11549-4.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int tests; + cin >> tests; + + while (tests--) + { + int n, k; + cin >> n >> k; + + int mod = pow(10, n); + + long long result = k; + unordered_map hashtable; + unordered_map::iterator it; + + hashtable[result] = 1; + + while (true) + { + result *= result; + + while (result >= mod) + { + result /= 10; + } + + if (hashtable.find(result) != hashtable.end()) + { + break; + } + + hashtable[result] = 1; + } + + result = 0; + + for (it = hashtable.begin(); it != hashtable.end(); it++) + { + if (it->first > result) + { + result = it->first; + } + } + + cout << result << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11549/11549-9.cpp b/uva_cpp_clean/11549/11549-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b002d184a6d41847beff3d8c6e27ddf6164a38a --- /dev/null +++ b/uva_cpp_clean/11549/11549-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int tc, n, MOD; +long long k, mx; + +long long maximum(){ + mx = k; + set vis; + while( vis.find(k) == vis.end() ){ + vis.insert(k); + k = k * k; + while( k >= MOD ) k /= 10; + mx = max(mx, k); + } + return mx; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %lld", &n, &k); + MOD = exp10(n); + printf("%lld\n", maximum()); + } + return(0); +} diff --git a/uva_cpp_clean/11550/11550-21.cpp b/uva_cpp_clean/11550/11550-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17fa72b5436fc82952f6fbdc3bc4ac66b1ce5af --- /dev/null +++ b/uva_cpp_clean/11550/11550-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11550 + Name: Demanding Dilemma + Problem: https://onlinejudge.org/external/115/11550.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +int mat[30][30], sum[30], vv[30][2]; +int main(){ + int T, n, m, x; + scanf("%d", &T); + for (int cse=1; cse<=T; ++cse) { + int ced = 0; + bool isvalid = 1; + scanf("%d%d", &n, &m); + for (int j=0; j +using namespace std; +using ii=tuple; +using vi=vector; +using vvi=vector; +using sii=unordered_set; +int main(){ + int t,n,m,x; + cin>>t; + while(t--){ + cin>>n>>m; + vvi b(m); + for(int i=0;i>x; + if(x)b[j].push_back(i); + } + int o=1; + vvi a(n,vi(n)); + for(int i=0;i + +using namespace std; + +int a[10][100]; +bool b[10][100]; +vector aux; + +int main(){ + int c,n,m,ind,sum; + scanf("%d",&c); + while(c--){ + ind = 1; + memset(b, false, sizeof b); + scanf("%d%d",&n,&m); + for(int i = 0;i < n; i++) + for(int j = 0; j < m; j++) + scanf("%d", &a[i][j]); + for(int i = 0; i < m; i++){ + sum = 0; + for(int j = 0; j < n; j++) + sum += a[j][i]; + if(sum != 2) ind = 0; + } + for(int j = 0; j < m; j++){ + for(int i = 0; i < n; i++) + if(a[i][j] == 1) + aux.push_back(i); + if(b[aux[0]][aux[1]] == true) ind = 0; + b[aux[0]][aux[1]] = true; + aux.clear(); + } + printf(ind ? "Yes\n" : "No\n"); + } + return(0); +} diff --git a/uva_cpp_clean/11551/11551-8.cpp b/uva_cpp_clean/11551/11551-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..063c1f17a97b7315338d92fa8603090a0848d69f --- /dev/null +++ b/uva_cpp_clean/11551/11551-8.cpp @@ -0,0 +1,50 @@ +// Problem name: Experienced Endeavour +// Problem link: https://vjudge.net/problem/UVA-11551 +// Submission link: https://vjudge.net/solution/18102877 + +#include + +#define MAX ((int)1010) +#define EPS ((double)1e-5) +#define mp make_pair +#define F first +#define S second +#define sqr(x) ((x) * (x)) +#define endl '\n' + +using namespace std; + +typedef pair point; + +double dist(point a, point b){ + return sqrt(sqr(a.F - b.F) + sqr(a.S - b.S)); +} + +int n; +double ans[ MAX ][ MAX ]; +point p[ MAX ]; + +double solve(int left, int right){ + int now = max(left, right) + 1; + if(now == n - 1) + return dist(p[ left ], p[ now ]) + dist(p[ now ], p[ right ]); + if(ans[ left ][ right ] >= EPS) + return ans[ left ][ right ]; + return ans[ left ][ right ] = min(dist(p[ left ], p[ now ]) + solve(now, right), dist(p[ now ], p[ right ]) + solve(left, now)); +} + +int main(){ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + while(cin >> n){ + for(int i = 0 ; i < n ; i++) + cin >> p[ i ].F >> p[ i ].S; + memset(ans, 0, sizeof(ans)); + cout.precision(2); + cout.flags(ios::fixed); + cout << solve(0, 0) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11552/11552-9.cpp b/uva_cpp_clean/11552/11552-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e361c0437d3ca51adc2c19acb170443c1ad153d --- /dev/null +++ b/uva_cpp_clean/11552/11552-9.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +const int MAX_LEN = 1e3 + 10, ALPHABET = 30; + +int k, memo[MAX_LEN][ALPHABET]; +string s; +vector group; + +int dp (int id = 0, char last_ch = 'z' + 1) { + if (id == group.size()) return 0; + if (~memo[id][last_ch]) return memo[id][last_ch]; + int ret = INT_MAX, pib = 0; + if (group[id].find(last_ch) != string::npos) { + pib = -1; + } + if (group[id].size() == 1) { + ret = (int)group[id].size() + pib + dp(id + 1, group[id][0]); + } + else { + for (char ch: group[id]) { + if (ch != last_ch) { + ret = min(ret, (int)group[id].size() + pib + dp(id + 1, ch)); + } + } + } + return memo[id][last_ch] = ret; +} + +void preprocess () { + for (int i = 0; i < s.size(); i += k) { + set st; + for (int j = i; j < i + k; j++) st.insert(s[j]); + group.push_back(string(begin(st), end(st))); + } +} + +void clearCase () { + group.clear(); + memset(memo, -1, sizeof memo); +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + clearCase(); + cin >> k >> s; + preprocess(); + cout << dp() << endl; + } + return (0); +} diff --git a/uva_cpp_clean/11553/11553-11.cpp b/uva_cpp_clean/11553/11553-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec5c4f433d9ffbd60e6fd102f15e29d2e371cca0 --- /dev/null +++ b/uva_cpp_clean/11553/11553-11.cpp @@ -0,0 +1,88 @@ +#include +/* +Problem: 11553 - Grid Game +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2548 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int T, N, grid[8][8]; +int score, best_score; + +int main () { + scanf("%d\n", &T); + + while ( T-- ){ + scanf("%d\n", &N); + best_score = INT_MAX; + + int alice_moves[N]; + + for (int i = 0; i < N; i++) + alice_moves[i] = i; + + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + scanf("%d", &grid[i][j]); + + do { + score = 0; + bool vis[N] = {}; + + for (int i = 0; i < N; i++){ + + int min_idx = 0, min_value = INT_MAX; + for (int j = 0; j < N; j++){ + if ((!vis[j]) && (grid[alice_moves[i]][j] < min_value)){ + min_value = grid[alice_moves[i]][j]; + min_idx = j; + } + } + + score += min_value; + vis[min_idx] = 1; + } + + best_score = min(best_score, score); + + } while ( next_permutation(alice_moves, alice_moves+N) ); + + printf("%d\n", best_score); + } + + return 0; +} +/* +Sample input:- +----------------- +3 +2 +10 10 +-5 -5 +2 +10 -5 +10 -5 +2 +10 -5 +-5 10 + +Sample output:- +----------------- +5 +5 +-10 + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11553/11553-13.cpp b/uva_cpp_clean/11553/11553-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ecc22b21e4dda9baedd435a57b0d8ada2dddf7f --- /dev/null +++ b/uva_cpp_clean/11553/11553-13.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +using namespace std; + +int TC, n, ans, minimum; +int col[10], table[10][10]; + +bool place(int r, int c) +{ + for (int pre = 0; pre < r; pre++) + { + if (col[pre] == c) + { + return false; + } + } + + return true; +} + +void backTrack(int r) +{ + if (r == n) + { + ans = 0; + + for (int i = 0; i < n; i++) + { + ans += table[i][col[i]]; + } + + minimum = min(minimum, ans); + } + + for (int c = 0; c < n; c++) + { + if (place(r, c)) + { + col[r] = c; + + backTrack(r + 1); + } + } +} + +int main() +{ + cin >> TC; + + while (TC--) + { + cin >> n; + + minimum = 100000000; + ans = 0; + + memset(table, 0, sizeof table); + + memset(col, 0, sizeof col); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> table[i][j]; + } + } + + backTrack(0); + + cout << minimum << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11553/11553-21.cpp b/uva_cpp_clean/11553/11553-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b80354ae6a7701b7f4faf5a9c5f3be4b6a08400f --- /dev/null +++ b/uva_cpp_clean/11553/11553-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11553 + Name: Grid Game + Problem: https://onlinejudge.org/external/115/11553.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + + +int X[17][17], n; +bool used[17]; +int bt(int r) { + if (r == n) return 0; + + int mx = INF; + for (int c=0; c> T; + while (T-- && cin>>n) { + for (int i=0; i> X[i][j]; + + cout << bt(0) << '\n'; + } +} diff --git a/uva_cpp_clean/11554/11554-14.cpp b/uva_cpp_clean/11554/11554-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee5e40255219efdc64137314772967d7707783bb --- /dev/null +++ b/uva_cpp_clean/11554/11554-14.cpp @@ -0,0 +1,177 @@ +/*************************************************** + * Problem Name : 11554 - Hapless Hedonism.cpp + * Problem Link : https://onlinejudge.org/external/115/11554.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-08-17 + * Problem Type : adHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 1000005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ + +vectorev, od; + +ll fun (ll n) { + if (n <= 0) return 0; + + return (n * (n + 1LL) ) / 2LL; +} + +void pre_cal() { + ll sum = 0 ; + + for (int i = 0; i < MAX; i += 2) { + sum += fun (i); + ev.pb (sum); + } + + sum = 0; + od.pb (0); + + for (int i = 1; i < MAX; i += 2) { + sum += fun (i); + od.pb (sum); + } +} + + +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + pre_cal(); + int tc; + cin >> tc; + + while (tc--) { + int n; + cin >> n; + ll sum = 0; + + if (n <= 3) { + sum = 0; + + } else if (n & 1) { + sum = ev[n / 2 - 1]; + + } else { + sum = od[n / 2 - 1]; + } + + //~ for (int i = 1; i <= n; i++) { + //~ ll x = (n - i - i - 1); + //~ cout << x << "\n"; + //~ sum += fun (x); + //~ } + cout << sum << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/11559/11559-13.cpp b/uva_cpp_clean/11559/11559-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aef26256a81e3426a26f0cd99677642b340cbfae --- /dev/null +++ b/uva_cpp_clean/11559/11559-13.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +bool ok; +int n, h, b, w, total; + +class comparator +{ +public: + + bool operator()(pair &x, pair &y) + { + return x.second > y.second; + } +}; + +struct hotel +{ + int price; + + vector beds; +}; + +int main() +{ + while (cin >> n >> b >> h >> w) + { + ok = false; + + total = 0; + + priority_queue, vector>, comparator> pq; + + vector hotels; + + for (int i = 0; i < h; i++) + { + hotel temp; + + cin >> temp.price; + + pq.push(make_pair(i, temp.price)); + + for (int j = 0; j < w; j++) + { + int empty; + + cin >> empty; + + temp.beds.push_back(empty); + } + + hotels.push_back(temp); + } + + while (!pq.empty() && !ok) + { + int ind = pq.top().first; + + int person = pq.top().second; + + pq.pop(); + + for (int i = 0; i < w; i++) + { + if (hotels[ind].beds[i] >= n && b >= person * n) + { + ok = true; + + total = person * n; + + break; + } + } + } + + if (ok) cout << total << endl; + + else cout << "stay home" << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11559/11559-19.cpp b/uva_cpp_clean/11559/11559-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7d5fb44e12f7e43a0f2fb27b5483675bf1a6475 --- /dev/null +++ b/uva_cpp_clean/11559/11559-19.cpp @@ -0,0 +1,22 @@ +#include +#include +using namespace std; + +int main() { + int N, B, H, W; + while (scanf("%d %d %d %d", &N, &B, &H, &W) != EOF) { + int cost = B + 1; + for (int i = 0; i < H; i++) { + int p; + scanf("%d", &p); + for (int j = 0; j < W; j++) { + int rooms; + scanf("%d", &rooms); + if (rooms >= N) cost = min(cost, p*N); + } + } + if (cost < B) printf("%d\n", cost); + else printf("stay home\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11559/11559-20.cpp b/uva_cpp_clean/11559/11559-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..542d400632addb672ce8b6ac30e3e0c3ce376b3f --- /dev/null +++ b/uva_cpp_clean/11559/11559-20.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +int main() +{ + long N, B, H, W; + + while( cin >> N >> B >> H >> W ) + { + long cost = 0; + for(int i=0 ; i> p; + + for(int j=1 ; j<=W ; j++) + { + long a; + cin >> a; + + if( a>=N ) + if( cost==0 || p*NB ) cout << "stay home" << endl; + else cout << cost << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11559/11559-21.cpp b/uva_cpp_clean/11559/11559-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..628d46ea413b9a03f791c786ae4a11aa39a75cd2 --- /dev/null +++ b/uva_cpp_clean/11559/11559-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11559 + Name: Event Planning + Problem: https://onlinejudge.org/external/115/11559.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n, b, h, w, p, beds; + while(cin>>n>>b>>h>>w) { + int bestp = 2147483647; + for (int i=0; i> p; + for (int j=0; j>beds; + if (beds>=n) f = true; + } + + if (f && p +int main() +{ + long frnd,bud,hotel,week,amount,cost=15000000,b,p,visit,i,j,room; + while(scanf("%ld%ld%ld%ld",&frnd,&bud,&hotel,&week)==4) + { + for(i=0;i=frnd) + { + p=amount*frnd; + if(cost>p) + cost=p; + } + } + } + if(cost>bud) + printf("stay home\n"); + else + printf("%ld\n",cost); + cost=15000000; + + + } + + return 0; +} +//khub valo laglo problem ta kore........ diff --git a/uva_cpp_clean/11559/11559-9.cpp b/uva_cpp_clean/11559/11559-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82b523ce7e78f4bb94728cf9f76bcfb5f161b9ce --- /dev/null +++ b/uva_cpp_clean/11559/11559-9.cpp @@ -0,0 +1,22 @@ +#include +#define max(a,b) ( a >= b ? a : b ) +#define min(a,b) ( a <= b ? a : b ) + +int main(){ + int N, B, H, W, p, ans, cap, i, j, x; + while(scanf("%d %d %d %d", &N, &B, &H, &W) == 4){ + ans = B + 1; + for(i = 0; i < H; i++){ + scanf("%d", &p); + cap = 0; + for(j = 0, x; j < W; j++){ + scanf("%d",&x); + cap = max(cap,x); + } + if(cap >= N) ans = min(ans, N * p); + } + if(ans == B + 1) printf("stay home\n"); + else printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11559/main.cpp b/uva_cpp_clean/11559/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e79592179c3fc07305bb52a95fda6c94568dae69 --- /dev/null +++ b/uva_cpp_clean/11559/main.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +int main(){ + long personas, presupuesto, hoteles, opciones, camas, precio, mejor_precio = INT_MAX , mas_camas = 0; + while(cin >> personas >> presupuesto >> hoteles >> opciones){ + bool res = false; + mejor_precio = INT_MAX; + for(int i = 0; i < hoteles; i++){ + cin >> precio; + mas_camas = 0; + for(int c = 0; c < opciones; c++){ + cin >> camas; + mas_camas = max(mas_camas, camas); + + if(precio*personas <= presupuesto && mas_camas >= personas){ + res = true; + mejor_precio = min(precio, mejor_precio); + } + } + } + if(res) cout << mejor_precio * personas << endl; + else cout << "stay home" << endl; + } +return 0; +} diff --git a/uva_cpp_clean/11561/11561-21.cpp b/uva_cpp_clean/11561/11561-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddc2b6e01e7f2cf6bb9d722e02a31fe09ccc367c --- /dev/null +++ b/uva_cpp_clean/11561/11561-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11561 + Name: Getting Gold + Problem: https://onlinejudge.org/external/115/11561.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char M[55][55]; +int dy[]={0,1,0,-1}, + dx[]={1,0,-1,0}; +int dfs(int y, int x) { + if (M[y][x] == '#') return 0; + int r = M[y][x] == 'G'; + M[y][x] = '#'; + + for (int k=0; k<4; ++k) + if (M[y+dy[k]][x+dx[k]] == 'T') + return r; + + for (int k=0; k<4; ++k) + r += dfs(y+dy[k], x+dx[k]); + + return r; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m; + while (cin>>m>>n) { + for (int i=0; i> M[i]; + + for (int i=0; i + +#define SIZE 60 + +using namespace std; + +int n, m; +char grid[SIZE][SIZE]; +bool vis[SIZE][SIZE]; +pair P; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +inline bool is_possible(int R, int C){ + return ( 1 <= R and 1 <= C and R < n and C < m and grid[R][C] != '#' and grid[R][C] != 'T' ); +} + +bool is_safe(int R, int C){ + for(int d = 0; d < 4; d++) + if( grid[R + dr[d]][C + dc[d]] == 'T' ) return false; + return true; +} + +int floodfill(int r, int c){ + grid[r][c] = '.'; + vis[r][c] = true; + if( not is_safe(r, c) ) return 0; + int ct = 0; + for(int d = 0; d < 4; d++){ + int row = r + dr[d], col = c + dc[d]; + if( is_possible(row, col) and not vis[row][col] ){ + if( grid[row][col] == 'G' ) ct++; + ct += floodfill(row, col); + } + } + return ct; +} + +pair findInitialPosition(){ + for(int row = 1; row < n; row++) + for(int col = 1; col < m; col++) + if( grid[row][col] == 'P' ) + return make_pair(row, col); +} + +int main(){ + while(~scanf("%d %d\n", &m, &n)){ + memset(vis, false, sizeof vis); + for(int row = 0; row < n; row++) scanf("%s", grid[row]); + n--, m--; + P = findInitialPosition(); + printf("%d\n", floodfill(P.first, P.second)); + } + return(0); +} diff --git a/uva_cpp_clean/11565/11565-11.cpp b/uva_cpp_clean/11565/11565-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a898be9b211d7b0a7b924f8c385d6e51d0ed34b8 --- /dev/null +++ b/uva_cpp_clean/11565/11565-11.cpp @@ -0,0 +1,66 @@ +#include +/* +Problem: 11565 - Simple Equations +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2612 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int T, A, B, C; + +int main () { + + scanf("%d\n", &T); + + while ( T-- ){ + scanf("%d %d %d\n", &A, &B, &C); + int ans[3] = {}; + bool found = false; + + for (int x = -100; x <= 100 && !found; x++){ + for (int y = -100; y <= 100 && !found; y++){ + if (x == y) continue; + + for (int z = -100; z <= 100 && !found; z++){ + if ( (x != z && y != z) && ((x+y+z) == A) && ((x*y*z) == B) && ((x*x + y*y + z*z) == C)){ + ans[0] = x; ans[1] = y; ans[2] = z; + sort(ans, ans+3); + + found = true; + } + } + } + } + + if (found) printf("%d %d %d\n", ans[0], ans[1], ans[2]); + else printf("No solution.\n"); + } + + return 0; +} +/* +Sample input:- +----------------- +2 +1 2 3 +6 6 14 + +Sample output:- +----------------- +No solution. +1 2 3 + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11565/11565-13.cpp b/uva_cpp_clean/11565/11565-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f80b0bd3c777434273fd989ebc7f953246e35d65 --- /dev/null +++ b/uva_cpp_clean/11565/11565-13.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int t; + + cin >> t; + + while (t--) + { + bool found = false; + int a, b, c; + + cin >> a >> b >> c; + + for (int x = -100; x <= 100; x++) + { + if ((x * x) <= c && !found) + { + for (int y = -100; y <= 100; y++) + { + if ((x * x) + (y * y) <= c && !found && y != x) + { + for (int z = -100; z <= 100; z++) + { + if (z != x && y != z && !found && (x + y + z) == a && (x * y * z) == b && ((x * x) + (y * y) + (z * z)) == c) + { + cout << x << " " << y << " " << z << endl; + + found = true; + } + } + } + } + } + } + + if (!found) + { + cout << "No solution." << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11565/11565-19.cpp b/uva_cpp_clean/11565/11565-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..698d71dbada274686c5c25af055b2deda28660f0 --- /dev/null +++ b/uva_cpp_clean/11565/11565-19.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main() { + int N; + cin >> N; + while (N--) { + int A, B, C; + cin >> A >> B >> C; + bool ok = false; + for (int x = -100; x <= 100 && !ok; x++) { + for (int y = -100; y <= 100 && x*x <= C && !ok; y++) { + for (int z = -100; z <= 100 && x != y && x*x + y*y <= C && !ok; z++) { + if (x + y + z == A && x*y*z == B && x*x + y*y + z*z == C && x != z && y != z) { + printf("%d %d %d\n", x, y, z); + ok = true; + } + } + } + } + if (!ok) printf("No solution.\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11565/11565-21.cpp b/uva_cpp_clean/11565/11565-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a47d194c7ab644c708a6be9fe416f79d60a6f85 --- /dev/null +++ b/uva_cpp_clean/11565/11565-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11565 + Name: Simple Equations + Problem: https://onlinejudge.org/external/115/11565.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, a, b, c; + cin >> T; + while (T-- && cin>>a>>b>>c) { + int mx = sqrt(c); + for (int x=-mx; x<=mx; ++x) + if (x && b % x == 0) { + int mxy = x<0 ? 0 : mx; + for (int y=x+1; y <= mxy; ++y) + if (y && b % y == 0) { + int z = a - x - y; + if (z>y && x*y*z == b && x*x+y*y+z*z==c) { + cout << x << ' ' << y << ' ' << z << '\n'; + goto fin; + } + } + } + cout << "No solution.\n"; + fin:; + } +} diff --git a/uva_cpp_clean/11565/11565-3.cpp b/uva_cpp_clean/11565/11565-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf2628278d838073afb5f8ab5cfe06ac56c0bb2f --- /dev/null +++ b/uva_cpp_clean/11565/11565-3.cpp @@ -0,0 +1,31 @@ +#include + + +int main () +{ + int cases,A,B,C,x,y,z; + bool answer; + scanf("%d",&cases); + + while ( cases-- ) + { + scanf("%d %d %d", &A,&B,&C); + answer = true; + for ( x = -100; x < 101 && answer; x++ ) if (x*x<=C) + for ( y = -100; y < 101 && answer; y++ ) + if ( x != y && (x*x+y*y + +int main(){ + int tc; + scanf("%d", &tc); + while(tc--){ + int A, B, C; + scanf("%d %d %d",&A,&B,&C); + int sqrtC = sqrt(C), sx, sy, sz; + bool found = false; + int x,y; + for(x = -sqrtC; x <= sqrtC; x++){ + for(y = -sqrtC; y <= sqrtC; y++){ + int z = A - x - y; + if(x == y || x == z || y == z) continue; + if(x * y * z == B && x * x + y * y + z * z == C && found == false){ + sx = x; sy = y; sz =z; + found = true; + break; + } + } + if(found) break; + } + if(found) printf("%d %d %d\n", sx, sy, sz); + else printf("No solution.\n"); + } + return(0); +} diff --git a/uva_cpp_clean/11566/11566-21.cpp b/uva_cpp_clean/11566/11566-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..196f1e79125d6d60286302579d4f6743984d4401 --- /dev/null +++ b/uva_cpp_clean/11566/11566-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11566 + Name: Let's Yum Cha! + Problem: https://onlinejudge.org/external/115/11566.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 268435456 +using namespace std; + + +int P[107], V[107], DP[107][23][1005], M[107][23][1005], cse, a1, a2; +int rec(int id, int c, int r) { + int aa = a2 - ceil(1.1L * (r + a1)); + if (c<0 || aa<0) return -INF; + if (!c || !aa || id<0) return 0; + if (M[id][c][r] == cse) return DP[id][c][r]; + + DP[id][c][r] = max( + rec(id-1, c, r), + max( + rec(id-1, c-1, r+P[id]) + V[id], + rec(id-1, c-2, r+2*P[id]) + 2*V[id] + ) + ); + M[id][c][r] = cse; + return DP[id][c][r]; +} + +int main() { + int n, x, t, k; + for (cse=1; scanf("%d%d%d%d", &n, &x, &t, &k)==4 && (n++||x||t||k); ++cse) { + a1 = t*n; a2 = x*n; + + for (int i=0; i + +using namespace std; + +const int MAX_K = 100 + 10, INF = 1e6, MAX_N = 10 + 15, MAX_COST = 20000 + 10; + +int n, x, t, k, price[MAX_K], favour[MAX_K], memo[MAX_K][MAX_COST][MAX_N]; + +int dp (int id, int cost, int dishes) { + if (dishes < 0) return -INF; + if (id == k) { + int total_cost = cost + t * (n + 1); + total_cost = total_cost + ceil(0.10 * total_cost); + if (total_cost <= x * (n + 1)) return 0; + return -INF; + } + if (memo[id][cost][dishes] != INF) return memo[id][cost][dishes]; + int ret = -INF; + for (int it = 0; it < 3; it++) { + ret = max(ret, favour[id] * it + dp(id + 1, cost + it * price[id], dishes - it)); + } + return memo[id][cost][dishes] = ret; +} + +void readCase () { + for (int i = 0; i < k; i++) { + cin >> price[i]; + favour[i] = 0; + for (int j = 0, val; j <= n; j++) { + cin >> val; + favour[i] += val; + } + } +} + +void clearCase () { + for (int i = 0; i <= k; i++) + for (int j = 0; j < MAX_COST; j++) + for (int k = 0; k <= 2 * (n + 1); k++) + memo[i][j][k] = INF; +} + +int main () { + while (cin >> n >> x >> t >> k, n + x + t + k) { + readCase(); + clearCase(); + int mxFavour = dp(0, 0, 2 * (n + 1)); + printf("%.2lf\n", 1.0 * mxFavour / (n + 1)); + } + return (0); +} diff --git a/uva_cpp_clean/11567/11567-13.cpp b/uva_cpp_clean/11567/11567-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cec44e21d4744ca85894b34bcb4c13de3a1f908 --- /dev/null +++ b/uva_cpp_clean/11567/11567-13.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + long long s, how = 0; + + while (cin >> s) + { + how = 0; + + while (s != 0) + { + if (s % 2 == 0) s /= 2; + + else + { + if (s % 4 == 3) + { + if (s == 3) s--; + + else s++; + } + + else s--; + } + + how++; + } + + cout << how << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11567/11567-21.cpp b/uva_cpp_clean/11567/11567-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..131afd592d26966cb11f07d4b81bb29e7fa99984 --- /dev/null +++ b/uva_cpp_clean/11567/11567-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11567 + Name: Moliu Number Generator + Problem: https://onlinejudge.org/external/115/11567.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + unsigned int n; + while (cin >> n) { + int cnt = 0; + for (; n; ++cnt) + if (n&1) + if (n>3 && n&2) ++n; + else --n; + else n >>= 1; + cout << cnt << endl; + } +} diff --git a/uva_cpp_clean/11571/11571-9.cpp b/uva_cpp_clean/11571/11571-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc7cec91130e18021606a2f98f6b3a39ace0bdba --- /dev/null +++ b/uva_cpp_clean/11571/11571-9.cpp @@ -0,0 +1,39 @@ +/** + * > Author : TISparta + * > Date : 22-03-18 + * > Tags : Math + * > Difficulty : 5 / 10 + */ + +#include + +using namespace std; + +int tc; +long double A, B, C, a0, a1, a2, Q, R, theta; +long long x, y, z; + +bool isValid(long long x, long long y, long long z){ + return ( x < y and y < z and x + y + z == A and x * y * z == B and x * x + y * y + z * z == C ); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%Lf %Lf %Lf", &A, &B, &C); + a0 = -B, a1 = (A * A - C) / 2.0L, a2 = -A; + Q = ( a2 * a2 - 3.0 * a1 ) / 9.0L; + R = ( 9.0L * a1 * a2 - 27.0L * a0 - 2.0L * a2 * a2 * a2 ) / 54.0L; + theta = acosl(R / sqrtl(Q * Q * Q)); + vector Z; + for(int k = 0; k < 3; k++) + Z.push_back( (long long)round( 2.0L * sqrtl(Q) * cosl( ( theta + 2.0L * M_PI * k ) / 3.0L) - a2 / 3.0L ) ); + sort(Z.begin(), Z.end()); + x = Z[0], y = Z[1], z = Z[2]; + if( isValid(x, y, z) ) + printf("%lld %lld %lld\n", x, y, z); + else + puts("No solution."); + } + return(0); +} diff --git a/uva_cpp_clean/11572/11572-14.cpp b/uva_cpp_clean/11572/11572-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aed2b55f4520b63bbb2e3399c107a18687095602 --- /dev/null +++ b/uva_cpp_clean/11572/11572-14.cpp @@ -0,0 +1,147 @@ +/*************************************************** + * Problem Name : 11572 - Unique Snowflakes.cpp + * Problem Link : + * OJ : + * Verdict : AC + * Date : 2020-03-05 + * Problem Type : + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + int n; + cin >> n; + vectorvec (n + 1); + + for (int i = 1; i <= n; i++) { + cin >> vec[i]; + } + + int mx = 0, cnt = 0; + int i = 1, j = 1; + mapmp; + + while (j <= n) { + int x = vec[j]; + + if (mp[x] == 0) { + cnt++; + mp[x]++; + j++; + + } else { + while (vec[i] != vec[j]) { + int xx = vec[i]; + mp[xx]--; + + if (mp[xx] == 0) cnt--; + + i++; + } + + i++; + j++; + } + + mx = max (mx, cnt); + } + + cout << mx << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/11572/11572-21.cpp b/uva_cpp_clean/11572/11572-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..513896d112f978c1252eeea43df19d53415ae371 --- /dev/null +++ b/uva_cpp_clean/11572/11572-21.cpp @@ -0,0 +1,66 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11572 + Name: Unique Snowflakes + Problem: https://onlinejudge.org/external/115/11572.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct Node { + bool seen; + Node *nxt[10]; + Node(): seen(0) { memset(nxt, 0, sizeof(nxt)); } + ~Node() { + for (int i=0; i<10; ++i) + if (nxt[i]) + delete nxt[i]; + } +}; +Node *root; +bool setval(int ind, bool nval) { + Node *cur = root; + for (; ind; ind /= 10) { + int d = ind % 10; + if (!cur->nxt[d]) + cur->nxt[d] = new Node(); + cur = cur->nxt[d]; + } + bool result = cur->seen; + cur->seen = nval; + return result; +} + + +int main(){ + int T, n, X[1000001]; + root = new Node(); + + scanf("%d", &T); + while (T--) { + scanf("%d", &n); + + int mx=0, i; + for (int j=i=0; j +#include //max +#include + +using namespace std; + +int main() +{ + int cases, + inputInt, + leftIndex, + packageSize, + tempSize, + sequenceLength; + + map inputMap; + + scanf("%d",&cases); + + while( cases-- ) + { + inputMap.clear(); + scanf("%d",&sequenceLength); + + leftIndex = packageSize = tempSize = 0; + + // Iterating through the input and moving the leftIndex (let's say, temporary sequence's + // starting point to the last occurence of a character + for ( int l = 0; l < sequenceLength; l++ ) + { + scanf("%d", &inputInt); + if( inputMap[inputInt] > 0 ) + { + // The leftIndex will either be 0 (the start of the array) or at the last + // occurence of a duplicate character (cause no duplicates should exist + // within a package) + leftIndex = max( leftIndex, inputMap[inputInt] ); + // Temporary sequence's size is equal to: + // ( currentIndex - starting(leftIndex ) + tempSize = l - leftIndex; + } + + tempSize++; + // Last occurence of "inputInt" **Must be greater than 0** + inputMap[inputInt] = l + 1; + packageSize = max( tempSize, packageSize ); + } + printf("%d\n",packageSize); + } + return 0; +} + diff --git a/uva_cpp_clean/11572/11572-4.cpp b/uva_cpp_clean/11572/11572-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf7850c73f89ac11e11896096f1f237716afde9e --- /dev/null +++ b/uva_cpp_clean/11572/11572-4.cpp @@ -0,0 +1,46 @@ +#include +#include + +using namespace std; + +int main() +{ + int tests; + cin >> tests; + + while (tests--) + { + int n, i; + cin >> n; + + map lastSeen; + int begin = 0, max = 0; + + for (i = 1; i <= n; i++) + { + int temp; + cin >> temp; + + if (lastSeen[temp] > begin) + { + if (i - begin - 1 > max) + { + max = i - 1 - begin; + } + + begin = lastSeen[temp]; + } + + lastSeen[temp] = i; + } + + if (i - begin - 1 > max) + { + max = i - 1 - begin; + } + + cout << max << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11576/11576-9.cpp b/uva_cpp_clean/11576/11576-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..206adc92bc6b1d0eb3364de63755925b62a6a603 --- /dev/null +++ b/uva_cpp_clean/11576/11576-9.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int prefixFunction (const string s) { + int n = s.size(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 and s[i] != s[j]) j = pi[j - 1]; + if (s[i] == s[j]) j++; + pi[i] = j; + } + return pi.back(); +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + int n, k, ans = 0; + cin >> k >> n; + string prev, curr; + cin >> prev; + ans = prev.size(); + for (int i = 1; i < n; i++) { + cin >> curr; + ans += k - prefixFunction(curr + '#' + prev); + prev = curr; + } + cout << ans << endl; + } + return (0); +} diff --git a/uva_cpp_clean/11577/11577-14.cpp b/uva_cpp_clean/11577/11577-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf11d355358b707d948bdfbea7551466685072a7 --- /dev/null +++ b/uva_cpp_clean/11577/11577-14.cpp @@ -0,0 +1,99 @@ +/*************************************************** + * Problem name : 11577 Letter Frequency.cpp + * Problem Link : https://uva.onlinejudge.org/external/115/11577.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-21 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 1000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int mp[MAX]; +//~ mapmp; +int main () { + int tc; + scanf("%d", &tc); + getchar(); + for (int t = 1; t <= tc; t++) { + string str; + getline(cin, str); + int sz = str.size(); + for (int i = 0; i < sz; i++) { + char ch = str[i]; + if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) { + if (ch >= 'A' && ch <= 'Z') { + ch = ch + 32; + } + mp[ch]++; + } + } + //~ map::iterator it; + int mx = 0; + //~ for (it = mp.begin(); it != mp.end(); it++) { + //~ int x = it->second; + //~ mx = max(x, mx); + //~ } + //~ for (it = mp.begin(); it != mp.end(); it++) { + //~ int x = it->second; + //~ if (x == mx) printf("%c", it->first); + //~ } + //~ nl; + for(int i = 97; i<=123; i++){ + mx = max(mx, mp[i]); + } + for(int i = 97; i<=123; i++){ + if(mp[i] == mx) + printf("%c", i); + } + nl; + SET(mp); + //~ mp.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/11577/11577-18.cpp b/uva_cpp_clean/11577/11577-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..becda398b75606d3f00b54cdfe8a4dce46063de6 --- /dev/null +++ b/uva_cpp_clean/11577/11577-18.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +using namespace std; + +int main(){ + int n, max; + string input; + scanf("%d", &n); + cin.get(); + while(n--){ + map freq; + getline(cin, input); + for(int i = 0; input[i]; i++){ + if(input[i] >= 'a' && input[i] <= 'z') freq[input[i]]+=1; + else if(input[i] >= 'A' && input[i] <= 'Z') freq[input[i] + 32]+=1; + } + max = -1; + for(auto iterator = freq.begin(); iterator != freq.end(); iterator++){ + if(iterator->second > max) max = iterator->second; + } + for(auto iterator = freq.begin(); iterator != freq.end(); iterator++){ + if(iterator->second == max) printf("%c", iterator->first); + } + printf("\n"); + } + return 0; +} +// Otro problema del tipo de guardar la frecuencia en que aparece una letra... diff --git a/uva_cpp_clean/11577/11577-21.cpp b/uva_cpp_clean/11577/11577-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f764efd8bdcd9a307e3fbba64062a78fbb1ed46 --- /dev/null +++ b/uva_cpp_clean/11577/11577-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11577 + Name: Letter Frequency + Problem: https://onlinejudge.org/external/115/11577.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + char line[400]; + int freq[26]; + int T; + cin>>T; + cin.getline(line, 400); + while (T--) { + memset(freq, 0, sizeof(freq)); + cin.getline(line, 400); + + int mx=0; + for (int i=0; line[i]; i++) { + char ch = tolower(line[i]); + if (ch>='a' && ch<='z') { + int t = ++freq[ch-'a']; + if (t > mx) mx = t; + } + } + + for (int i=0; i<26; i++) + if (freq[i] == mx) + putchar('a' + i); + putchar('\n'); + } +} diff --git a/uva_cpp_clean/11577/11577-5.cpp b/uva_cpp_clean/11577/11577-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d0338cb21c101a1df8048694e5cac159efd3f521 --- /dev/null +++ b/uva_cpp_clean/11577/11577-5.cpp @@ -0,0 +1,75 @@ +/*-----------------------------------------------*/ +//Problem Setter: Sumudu Fernando +//Problem Name : Letter Frequency +//Uva Problem No: 11577 +//Type : Ad hoc. +//Author : Shipu Ahamed +//University : BUBT +//E-mail : shipuahamed01@gmail.com +/*-----------------------------------------------*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + string s,p; + int t; + cin>>t; + getchar(); + while(t--) + { + getline(cin,s); + + int l=s.size(); + mapmp; + mapmp1; + priority_queuepq; + + for(int i=0;i='A'&&s[i]<='Z') + { + s[i]=s[i]+32; + } + mp[s[i]]++; + } + for(int i=0;i +#include +#include +using namespace std; + +const double eps=1e-9; + +int main() { + int T; + double sides[10002]; + cin >> T; + while (T--) { + int N; + cin >> N; + for (int i = 0; i < N; i++) cin >> sides[i]; + sort(sides, sides+N); + double best = 0, v; + for (int i = 2; i < N; i++) { + if (sides[i-2] + sides[i-1] <= sides[i]) continue; + v = (sides[i] + sides[i-1] + sides[i-2])*(-sides[i] + sides[i-1] + sides[i-2])*(sides[i] - sides[i-1] + sides[i-2])*(sides[i] + sides[i-1] - sides[i-2]); + v /= 16; + if (v > best) best = v; + } + printf("%.2lf\n", (sqrt(best))); + } + return 0; +} diff --git a/uva_cpp_clean/11579/11579-20.cpp b/uva_cpp_clean/11579/11579-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e05b2ade72dc338233d49b33c0f23b0f23aa65e8 --- /dev/null +++ b/uva_cpp_clean/11579/11579-20.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include + +using namespace std; + +double s[100005]; + +double triangle_area(double a, double b, double c) +{ + if( a+b> tc; + while( tc-- ) + { + int N; + cin >> N; + + for(int i=0 ; i> s[i]; + + sort(s, s+N); + + double area = 0.0; + for(int i=N-1 ; i>1 ; i--) + area = max(area, triangle_area(s[i], s[i-1], s[i-2])); + + cout << fixed << setprecision(2) << area << endl; + } + return 0; +} diff --git a/uva_cpp_clean/1158/1158-19.cpp b/uva_cpp_clean/1158/1158-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17e737ca0d4e863551b9a2795eed1189e70b5cec --- /dev/null +++ b/uva_cpp_clean/1158/1158-19.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +using namespace std; + +unordered_map map; +unordered_map pyramids; +unordered_map cubes; + +int proc(int N) { + if (N == 0) return 0; + if (!map.count(N)) { + int mini = 100000000; + for (int i = floor(cbrt(N)); i >= 1; i--) { + int val = proc(N - i*i*i) + 1; + if (val < mini) mini = val; + } + for (int i = 106; i >= 1; i--) { + if (pyramids[i] > N) continue; + int val = proc(N - pyramids[i]) + 1; + if (val < mini) mini = val; + } + map[N] = mini; + } + return map[N]; +} + +int main() { + int val1 = 0; + for (int i = 0; val1 <= 400000; i++) { + val1 += i*i; + pyramids[i] = val1; + } + int N; + while (cin >> N && N != -1) { + printf("%d\n", proc(N)); + } + return 0; +} diff --git a/uva_cpp_clean/11581/11581-11.cpp b/uva_cpp_clean/11581/11581-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96f9ab59775729e46bcec69dce4fed3171832c21 --- /dev/null +++ b/uva_cpp_clean/11581/11581-11.cpp @@ -0,0 +1,113 @@ +#include +/* +Problem: 11581 - Grid Successors +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2628 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int T, N; +char grid[3][3]; + +int dx[] = {0 , 1, 0, -1}; +int dy[] = {-1, 0, 1, 0}; + +bool valid(int i, int j){ + return ((i>=0) && (j>=0) && (i<3) && (j<3)); +} + +void f(char g[][3]){ + + char tmp[3][3]; + + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + tmp[i][j] = g[i][j]-'0'; + + + for (int i = 0; i < 3; ++i){ + for (int j = 0; j < 3; ++j){ + + g[i][j] = 0; + + for (int k = 0; k < 4; ++k) + if (valid(i+dx[k], j+dy[k])) + g[i][j] += tmp[i+dx[k]][j+dy[k]]; + + + g[i][j] %= 2; + g[i][j] += '0'; + } + } + +} + +int main () { + fastio; + + cin >> T; + while ( T-- ){ + + int s = 0; + + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + cin >> grid[i][j], s+=grid[i][j]-'0'; + + int ans = -1; + + while (s != 0){ + ans++; + f(grid); + + s = 0; + + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + s += grid[i][j]-'0'; + + } + + cout << ans << '\n'; + } + + return 0; +} +/* +Sample input:- +----------------- +3 +111 +100 +001 +101 +000 +101 +000 +000 +000 + +Sample output:- +----------------- +3 +0 +-1 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11581/11581-21.cpp b/uva_cpp_clean/11581/11581-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32c06adeb83c116de85aa4119fccab3d229f376f --- /dev/null +++ b/uva_cpp_clean/11581/11581-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11581 + Name: Grid Successors + Problem: https://onlinejudge.org/external/115/11581.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int f(int g) { + return + ( (g>>1) & 0x0DB ) ^ + ( (g<<1) & 0x1B6 ) ^ + ( (g>>3) & 0x03F ) ^ + ( (g<<3) & 0x1F8 ); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, seen[512]; + cin >> T; + while (T--) { + int g = 0; + for (int i=0; i<9; ++i) { + char ch; + cin >> ch; + g <<= 1; + if (ch == '1') + g |= 1; + } + + memset(seen, -1, sizeof(seen)); + + int fi = g, i; + for (i=0; seen[fi]<0; ++i) { + seen[fi] = i; + fi = f(fi); + } + cout << seen[fi]-1 << endl; + } +} diff --git a/uva_cpp_clean/11581/11581-9.cpp b/uva_cpp_clean/11581/11581-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2061a86489464a18e978a74f00e4bdfb64bd964 --- /dev/null +++ b/uva_cpp_clean/11581/11581-9.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int tc, ans, v[5][5], u[5][5]; +int x[] = {-1, 0, 0, 1}; +int y[] = {0, 1, -1, 0}; + +bool check(){ + for(int i = 1; i <= 3; i++) + for(int j = 1; j <= 3; j++) if(v[i][j]) return true; + return false; +} + +void transform(){ + for(int i = 1; i <= 3; i++) + for(int j = 1; j <= 3; j++){ + u[i][j] = 0; + for(int it = 0; it < 4; it++) + u[i][j] ^= v[i + x[it]][j + y[it]]; + } + for(int i = 1; i <= 3; i++) + for(int j = 1; j <= 3; j++) v[i][j] = u[i][j]; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + ans = -1; + for(int i = 1; i <= 3; i++) + for(int j = 1; j <= 3; j++) scanf("%1d", &v[i][j]); + while(check()){ + transform(); + ans++; + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11582/11582-19.cpp b/uva_cpp_clean/11582/11582-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f379c4c62cb379f1a270c4ca03c6f5f5098edba9 --- /dev/null +++ b/uva_cpp_clean/11582/11582-19.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +using namespace std; + +#define MAGIC_NUMBER 3003 + +typedef unsigned long long llu; + +int proc(llu a, llu b, int mod) { + if (b == 0) return 1; + int pos = proc(a, b / 2, mod); + pos *= pos; + pos %= mod; + if (b % 2) pos *= a, pos %= mod; + return pos; +} + +int main() { + int T, fib[MAGIC_NUMBER]; + cin >> T; + while (T--) { + llu a, b; + int n; + cin >> a >> b >> n; + if (n == 1) printf("0\n"); + else { + fib[0] = 0, fib[1] = 1; + int i = 2; + for (; i < MAGIC_NUMBER; i++) { + fib[i] = (fib[i-1] + fib[i-2]) % n; + if (fib[i] == fib[1] && fib[i-1] == fib[0]) break; + } + a %= i-1; + int pos = proc(a, b, i - 1); + printf("%d\n", fib[pos]); + } + } + return 0; +} diff --git a/uva_cpp_clean/11584/11584-19.cpp b/uva_cpp_clean/11584/11584-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bcba6a8d61f2c4cb07cedf7186618ca218d160c6 --- /dev/null +++ b/uva_cpp_clean/11584/11584-19.cpp @@ -0,0 +1,70 @@ +#include +#include +using namespace std; + +/*typedef unordered_map uii; + +unordered_map dp; +string s; + +int proc(int i, int j) { + if (i > j) return 0; + if (i == j) return 1; + if (!dp[i].count(j)) { + bool ok = false; + if (s[i] == s[j]) { + if (proc(i + 1, j - 1) <= 1) dp[i][j] = 1, ok = true; + } + if (!ok) { + int mini = 10000; + for (int k = i; k < j; k++) { + int sum = proc(i, k) + proc(k + 1, j); + if (sum < mini) mini = sum; + } + dp[i][j] = mini; + } + } + return dp[i][j]; +}*/ + +string s; +int dp[1010][1010]; +int v[1010]; + +int proc(int i) { + if (i >= s.length()) return 0; + if (v[i] == -1) { + int mini = 10000, l = dp[s.length()][i]; + while (l > 1) { + int val = 1 + proc(i + l); + if (val < mini) mini = val; + l = dp[l-1][i]; + } + int val = 1 + proc(i + 1); + if (val < mini) mini = val; + v[i] = mini; + } + return v[i]; +} + +int main() { + int n; + cin >> n; + while (n--) { + //dp.clear(); + cin >> s; + for (int i = 0; i < s.length(); i++) dp[1][i] = 1, v[i] = -1; + for (int l = 2; l <= s.length(); l++) { + for (int i = 0; i < s.length(); i++) { + if (i + l - 1 >= s.length()) dp[l][i] = dp[l-1][i]; + else { + if (s[i] == s[i+l-1] && dp[l-2][i+1] == l-2) dp[l][i] = l; //Palindrome + else dp[l][i] = dp[l-1][i]; + } + } + } + printf("%d\n", proc(0)); + //printf("%d\n", proc(0, s.length() - 1)); + } + return 0; +} diff --git a/uva_cpp_clean/11584/11584-7.cpp b/uva_cpp_clean/11584/11584-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea142d81e817ab451331fa5265f238961a7e223b --- /dev/null +++ b/uva_cpp_clean/11584/11584-7.cpp @@ -0,0 +1,96 @@ +/** + * Created by LonelyEnvoy on 2017-4-4. + * Partitioning by Palindromes + * Keywords: dp + */ + +// 状态定义:第 i 个字母前(包括此字母)共有 dp[i] 个回文串 +// 状态转移方程:dp[i] = min { dp[j] + 1 | j <= i-1, str[j+1...i] 是回文串 } + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define rep(i, a, n) for (int i = a; i < n; ++i) +#define erep(i, a, n) for (int i = a; i <= n; ++i) +#define per(i, a, n) for (int i = n - 1; i >= a; --i) + +const int INF = 0x3f3f3f3f; +const double EPS = numeric_limits::epsilon(); +typedef long long ll; +// end of header + +#define MAX 1001 + +char str[MAX]; +bool palin[MAX][MAX]; // palin[i][j] 记录第 i 个字母到第 j 个字母组成的子串是否为回文串 +int dp[MAX]; + +bool isPalin(int a, int b) { // 判断第 i 个字母到第 j 个字母组成的子串是否为回文串 + if (a == b) { + return true; + } + int mid = (a + b) / 2; + if ((b - a) % 2) { // len is even + erep(i, 0, mid - a) { + if (str[mid-i] != str[mid+i+1]) { + return false; + } + } + } else { // len is odd + erep(i, 1, mid - a) { + if (str[mid-i] != str[mid+i]) { + return false; + } + } + } + return true; +} + +int main() { + int T; + scanf("%d", &T); + while (T--) { + memset(palin, false, sizeof(palin)); + memset(dp, 0, sizeof(dp)); + + scanf("%s", str + 1); + int len = 0; + while (str[len + 1] != '\0') len++; + + erep(i, 1, len) { + erep(j, 1, i) { + palin[j][i] = isPalin(j, i); + } + } + + int mindp; + erep(i, 1, len) { + mindp = INF; + erep(j, 0, i-1) { // 注意!必须从0开始,因为 dp[1] = dp[0] + 1。如果从1开始,会导致dp[1] = 0(第1个字母自身不是回文串,错误) + if (palin[j+1][i] && dp[j] + 1 < mindp) { + mindp = dp[j] + 1; + } + } + dp[i] = mindp == INF ? 0 : mindp; + } + + printf("%d\n", dp[len]); + } +} diff --git a/uva_cpp_clean/11586/11586-2.cpp b/uva_cpp_clean/11586/11586-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f0a756650d7460b87bd00ebb32bc295e5b037da --- /dev/null +++ b/uva_cpp_clean/11586/11586-2.cpp @@ -0,0 +1,74 @@ + + +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +vector strip(string& s) { + vector ret; + string to_ret = ""; + for (auto c: s) { + if (c != ' ') to_ret += c; + if ((int) to_ret.size() == 2) { + ret.push_back(to_ret); + to_ret = ""; + } + } + return ret; +} + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + + string t; + getline(cin, t); + int tc = stoi(t); + while (tc--) { + string s; + getline(cin, s); + vector vs = strip(s); + map mp; + REP(i, (int) vs.size()) { + REP(j, 2) { + mp[vs[i][j]]++; + } + } + + if (mp['M'] > 1 && mp['F'] == mp['M']) cout << "LOOP\n"; + else cout << "NO LOOP\n"; + } + + return 0; +} + + diff --git a/uva_cpp_clean/11586/11586-21.cpp b/uva_cpp_clean/11586/11586-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93df468b357e0253628344edce964edb6340185b --- /dev/null +++ b/uva_cpp_clean/11586/11586-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11586 + Name: Train Tracks + Problem: https://onlinejudge.org/external/115/11586.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + int T; + cin >> T; + cin.ignore(100, '\n'); + while (T--) { + string str; + getline(cin, str); + + int f=0, m=0; + for (int i=0; str[i]; ++i) + if (str[i]=='F') ++f; + else if (str[i]=='M') ++m; + + cout << (f>1 && f==m ? "LOOP\n" : "NO LOOP\n"); + } +} diff --git a/uva_cpp_clean/11586/main.cpp b/uva_cpp_clean/11586/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..483e0dba9c1b9585305536c408cacab79c163eac --- /dev/null +++ b/uva_cpp_clean/11586/main.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int main(){ + int cases, f_counter, m_counter; + string line; + cin >> cases; + cin.ignore(); + for(;cases > 0; cases--){ + f_counter = 0; + m_counter = 0; + getline(cin, line); + for(int i = 0; i< line.size(); i++){ + if(line[i] == 'M') m_counter++; + else if(line[i] == 'F') f_counter++; + // cout << line[i] << ' ' << m_counter << ' ' << f_counter << endl; + } + if(f_counter == m_counter && f_counter > 1) cout << "LOOP" << endl; + else cout << "NO LOOP" << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/11588/11588-14.cpp b/uva_cpp_clean/11588/11588-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..48c1244906dc054a7fa4fbd8710fb6319c7cd021 --- /dev/null +++ b/uva_cpp_clean/11588/11588-14.cpp @@ -0,0 +1,101 @@ +/*************************************************** + * Problem name : 11588 Image Coding.cpp + * Problem Link : https://uva.onlinejudge.org/external/115/11588.pdf + * OJ : Uva + * Verdict : AC + * Date : 13.06.2017 + * Problem Type : ADHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 25 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +mapmp; +mapchk; +vectorans; +int main () { + int tc, R, C, M, N; + char str[MAX][MAX]; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d %d %d %d\n", &R, &C, &M, &N); + for (int i = 0; i < R; i++) { + for (int j = 0; j < C; j++) { + cin >> str[i][j]; + if (mp[str[i][j]] == 0) mp[str[i][j]] = 1; + else mp[str[i][j]]++; + } + } + int mx = 0; + char ch; + for (int i = 0; i < R; i++) { + for (int j = 0; j < C; j++) { + int x = mp[str[i][j]]; + if (mx <= x) { + mx = x; + } + } + } + for (int i = 0; i < R; i++) { + for (int j = 0; j < C; j++) { + int x = mp[str[i][j]]; + if (mx == x && chk[str[i][j]] == 0) { + ch = str[i][j]; + ans.push_back(ch); + chk[str[i][j]] = 1; + } + } + } + int sz = ans.size(); + int MX = sz * mx * M; + int MN = ((C * R) - (sz * mx)) * N; + cout <<"Case "< +#include + +int tc, nCase; +int R, C, M, N; +char pixelGrid[25]; +int counter[28], MAX, ans; + +int main() +{ + scanf("%d", &tc); + nCase = 1; + while( tc-- ) + { + scanf("%d %d %d %d", &R, &C, &M, &N); + + memset(counter, 0, sizeof(counter)); + for(int i=1 ; i<=R ; i++) + { + scanf("%s", pixelGrid); + for(int j=0 ; jMAX ) MAX = counter[i]; + + ans = 0; + for(int i=0 ; i<26 ; i++) + if( counter[i]==MAX ) ans += counter[i]*M; + else ans += counter[i]*N; + + printf("Case %d: %d\n", nCase, ans); + ++nCase; + } + return 0; +} diff --git a/uva_cpp_clean/11588/11588-21.cpp b/uva_cpp_clean/11588/11588-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e1a904d31f8ef69530fe23e596924ef72b7e6b6 --- /dev/null +++ b/uva_cpp_clean/11588/11588-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11588 + Name: Image Coding + Problem: https://onlinejudge.org/external/115/11588.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +void skipline() { while(getchar()!='\n'); } +char A[500]; +int main() { + int T, cse=1, n, m, x, y; + scanf("%d", &T); + while (T--) { + scanf("%d%d%d%d", &n, &m, &x, &y); + int freq[26]={}, mx=0; + for (int i=0; i mx) + mx = freq[ch]; + } + } + + int sum = 0; + for (int i=0; i + +#define SIZE 410 + +using namespace std; + +int tc, R, C, M, N, abc[30], _max, sz; +char s[SIZE], ss[SIZE]; + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + s[0] = '\0'; + memset(abc, 0, sizeof abc); + _max = -1, sz = 0; + scanf("%d %d %d %d\n", &R, &C, &M, &N); + for(int i = 0; i < R; i++) scanf("%s", ss), strcat(s, ss); + for(int i = 0; s[i]; i++) abc[s[i] - 'A']++; + _max = -1; + for(int i = 0; i < 26; i++) + if(abc[i] > _max) _max = abc[i]; + for(int i = 0; s[i]; i++) + if(abc[s[i] - 'A'] == _max) sz += M; + else sz += N; + printf("Case %d: %d\n", t, sz); + } + return(0); +} diff --git a/uva_cpp_clean/11597/11597-13.cpp b/uva_cpp_clean/11597/11597-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2631909ad759048cbecb5af6095ea6d0047b1b7 --- /dev/null +++ b/uva_cpp_clean/11597/11597-13.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; +int main() +{ + int cou = 1, t; + while (cin >> t && t != 0) + cout << "Case " << cou++ << ": " << t / 2 << endl; +} \ No newline at end of file diff --git a/uva_cpp_clean/11597/11597-19.cpp b/uva_cpp_clean/11597/11597-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5d4ff470032b20c41e35ae4cd093a2740bc4887 --- /dev/null +++ b/uva_cpp_clean/11597/11597-19.cpp @@ -0,0 +1,10 @@ +#include +using namespace std; + +int main() { + int n, c = 1; + while (cin >> n && n != 0) { + printf("Case %d: %d\n", c++, n/2); + } + return 0; +} diff --git a/uva_cpp_clean/11597/11597-20.cpp b/uva_cpp_clean/11597/11597-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38d04fcbbc292092bad9ce83703ef07f599420e4 --- /dev/null +++ b/uva_cpp_clean/11597/11597-20.cpp @@ -0,0 +1,14 @@ +#include +#include + +using namespace std; + +int main() +{ + int n, ncase = 0; + + while( cin >> n && n ) + cout << "Case " << ++ncase << ": " << n/2 << endl; + + return 0; +} diff --git a/uva_cpp_clean/11597/11597-5.cpp b/uva_cpp_clean/11597/11597-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1ce9c38955c8033d3e84bf05d7e175f888f69b2d --- /dev/null +++ b/uva_cpp_clean/11597/11597-5.cpp @@ -0,0 +1,11 @@ +#include +int main() +{ + int a,i=1; + while(scanf("%d",&a)==1 && a>1) + { + printf("Case %d: %d\n",i,a/2); + i++; + } + return 0; +} diff --git a/uva_cpp_clean/116/116-14.cpp b/uva_cpp_clean/116/116-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5918c5e790bef98b891f07bb16f722e00b98561b --- /dev/null +++ b/uva_cpp_clean/116/116-14.cpp @@ -0,0 +1,243 @@ +/*************************************************** + * Problem Name : 116 - Unidirectional TSP.cpp + * Problem Link : https://uva.onlinejudge.org/external/1/116.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-02-06 + * Problem Type : dp + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("/home/saikat/Desktop/logN/input.txt", "r", stdin); +#define __FileWrite freopen ("/home/saikat/Desktop/logN/output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 105 +#define INF 1000000009 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[MAX][MAX]; +int dp[MAX][MAX]; +int n, m; +vectorvec; +int fun (int i, int j) { + if (i < 1) { + i = n; + } + + if (i > n) { + i = 1; + } + + if (j == m) return dp[i][j] = ar[i][j]; + + if (dp[i][j] != -1) return dp[i][j]; + + int a = INF, b = INF, c = INF; + a = ar[i][j] + fun (i - 1, j + 1); + b = ar[i][j] + fun (i, j + 1); + c = ar[i][j] + fun (i + 1, j + 1); + return dp[i][j] = Min3 (a, b, c); +} +void print (int i, int j) { + if (j == m) { + return ; + } + + if (i == 1) { + int a = dp[n][j + 1]; + int b = dp[i][j + 1]; + int c = dp[i + 1][j + 1]; + + if (i + 1 > n) c = INF; + + if (a <= b && a <= c) { + if (a == b) { + vec.pb (i); + print (i, j + 1); + + } else if (a == c) { + vec.pb (i + 1); + print (i + 1, j + 1); + + } else { + vec.pb (n); + print (n, j + 1); + } + + } else if (c <= a && c <= b) { + if (c == b) { + vec.pb (i); + print (i, j + 1); + + } else { + vec.pb (i + 1); + print (i + 1, j + 1); + } + + } else { + vec.pb (i); + print (i, j + 1); + } + + } else if (i == n) { + int a = dp[i - 1][j + 1]; + int b = dp[i][j + 1]; + int c = dp[1][j + 1]; + + if (i - 1 < 1) a = INF; + + if (b <= a && b <= c) { + if (b == c) { + vec.pb (1); + print (1, j + 1); + + } else if (b == a) { + vec.pb (i - 1); + print (i - 1, j + 1); + + } else { + vec.pb (i); + print (i, j + 1); + } + + } else if (a <= b && a <= c) { + if (a == c) { + vec.pb (1); + print (1, j + 1); + + } else { + vec.pb (i - 1); + print (i - 1, j + 1); + } + + } else { + vec.pb (1); + print (1, j + 1); + } + + } else { + int a = dp[i - 1][j + 1]; + int b = dp[i][j + 1]; + int c = dp[i + 1][j + 1]; + + if (i - 1 < 1) a = INF; + + if (i + 1 > n) c = INF; + + if (c <= a && c <= b) { + if (c == a) { + vec.pb (i - 1); + print (i - 1, j + 1); + + } else if (c == b) { + vec.pb (i); + print (i, j + 1); + + } else { + vec.pb (i + 1); + print (i + 1, j + 1); + } + + } else if (b <= a && b <= c) { + if (b == a) { + vec.pb (i - 1); + print (i - 1, j + 1); + + } else { + vec.pb (i); + print (i, j + 1); + } + + } else { + vec.pb (i - 1); + print (i - 1, j + 1); + } + } +} +int main () { + __FastIO; + + while (cin >> n >> m) { + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + cin >> ar[i][j]; + } + } + + int mn = INF; + int id; + + for (int i = 1; i <= n; i++) { + SET (dp, -1); + int ans = fun (i, 1); + + if (mn > ans) { + id = i; + mn = ans; + } + } + + SET (dp, -1); + int an = fun (id, 1); + vec.pb (id); + print (id, 1); + + for (int i = 0; i < (int) vec.size(); i++) { + if (i != 0) cout << " "; + + cout << vec[i]; + } + + nl; + cout << an << "\n"; + vec.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/116/116-19.cpp b/uva_cpp_clean/116/116-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..865e4da821f5a348fc284e1e8dadfce217094b60 --- /dev/null +++ b/uva_cpp_clean/116/116-19.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +using namespace std; + +typedef pair ii; +int grid[11][101]; +ii path[11][101]; +int limx, limy; + +void proc(){ + for(int i = 0; i < limx; i++){ + path[i][limy-1].first = grid[i][limy-1]; + path[i][limy-1].second = -1; + } + for(int j = limy-2; j >= 0; j--){ + for(int i = 0; i < limx; i++){ + int low = min(min((limx+i-1)%limx, (i+1)%limx), i); + int high = max(max((limx+i-1)%limx, (i+1)%limx), i); + int middle = (limx+i-1)%limx + (i+1)%limx + i - low - high; + int cost = min(min(path[low][j+1].first, path[middle][j+1].first),path[high][j+1].first); + path[i][j].first = cost + grid[i][j]; + path[i][j].second = cost == path[low][j+1].first? low: cost == path[middle][j+1].first? middle: high; + } + } +} + +int main(){ + while(scanf("%d %d", &limx, &limy) != EOF){ + for(int i = 0; i < limx; i++){ + for(int j = 0; j < limy; j++){ + scanf("%d", &grid[i][j]); + } + } + proc(); + int mincost = path[0][0].first, pos = 0; + for(int i = 1; i < limx; i++){ + if(mincost > path[i][0].first) { + mincost = path[i][0].first; + pos = i; + } + } + for(int j = 0; j < limy; j++){ + printf("%d", pos+1); + pos = path[pos][j].second; + if(j < limy -1) printf(" "); + } + printf("\n%d\n", mincost); + } + return 0; +} diff --git a/uva_cpp_clean/116/116-21.cpp b/uva_cpp_clean/116/116-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f655b1ba42f957fa1b26f86e6e42fd52a118bc3a --- /dev/null +++ b/uva_cpp_clean/116/116-21.cpp @@ -0,0 +1,74 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 116 + Name: Unidirectional TSP + Problem: https://onlinejudge.org/external/1/116.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int Mat[12][102],m,n; +int DP[12][102]; +int P[12][102]; +int y[3]; +int mod(int x,int y){ + return x%y<0?x%y+y:x%y; +} +bool les(int y,int x,int z){ + return DP[y][z]<=DP[x][z]; +} + +int main(){ + int i,j,my,ans; + while(scanf("%d%d",&m,&n)==2) + { + for(i=0;i>Mat[i][j]; + + for(i=0;i=0;j--) + for(i=0;i traveling salesman problem (TSP) + difficulty: medium + date: 14/Nov/2019 + by: @brpapa +*/ +#include +#include +#include +#define INF 2147483647 +#define min(a, b) ((a)<(b) ? (a):(b)) +#define min3(a, b, c) ((a)<(b) ? min(a, c):min(b, c)) +#define adj(n, sup) ((n)<(0)? (sup-1):((n)%(sup))) //reescreve n, caso tenha extrapolado os limites 0 e sup-1 +using namespace std; + +int m[10][100], tab[10][100]; +int ans[100]; //ans[i] = linha do melhor caminho na coluna i +int qteLIN, qteCOL; + +void dpBU(int qteLIN, int qteCOL) { + //caso base + for (int i = 0; i < qteLIN; i++) + tab[i][qteCOL-1] = m[i][qteCOL-1]; + + //preenche tab, da última para a primeira coluna, de cima para baixo + for (int j = qteCOL-2; j >= 0; j--) + for (int i = 0; i < qteLIN; i++) + tab[i][j] = m[i][j] + min3( + tab[adj(i-1, qteLIN)][j+1], + tab[i][j+1], + tab[adj(i+1, qteLIN)][j+1] + ); +} + +//! seria mais simples rodar dp de novo +void recover() { + int minAux = INF; + + //coluna 0 + for (int i = 0; i < qteLIN; i++) + if (tab[i][0] < minAux) { + minAux = tab[i][0]; + ans[0] = i; + } + //próximas colunas + for (int j = 1; j < qteCOL; j++) { + minAux = INF; + + priority_queue path; //guarda linhas válidas + for (int k = -1; k <= 1; k++) + path.push(adj(ans[j-1]+k, qteLIN)); + + while (!path.empty()) { + int i = path.top(); + path.pop(); + + if (tab[i][j] <= minAux) { //para minAux iguais prioriza o menor i + minAux = tab[i][j]; + ans[j] = i; + } + } + } +} + +int main() { + while (scanf("%d %d", &qteLIN, &qteCOL) != EOF) { + for (int i = 0; i < qteLIN; i++) + for (int j = 0; j < qteCOL; j++) + scanf("%d", &m[i][j]); + + dpBU(qteLIN, qteCOL); + recover(); //gera ans + + for (int j = 0; j < qteCOL-1; j++) + printf("%d ", ans[j]+1); + printf("%d\n%d\n", ans[qteCOL-1]+1, tab[ans[0]][0]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/116/116-7.cpp b/uva_cpp_clean/116/116-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ce0120269ebf71a6adbf1b90074bf68bf344c92 --- /dev/null +++ b/uva_cpp_clean/116/116-7.cpp @@ -0,0 +1,166 @@ +/** + * Created by LonelyEnvoy on 2017-4-2. + * Unidirectional TSP + * Keywords: dp + */ + + /* + 总结: + 1. dp 完毕遍历输出的时候,循着最小路径依次输出,注意对比的是 dp 数组而不是源输入数组,容易写错。 + 2. 由于 INF 值可能涉及整数加减,故不能使用numeric_limits::max(),否则可能溢出。建议使用0x3f3f3f3f,一般题设数据不会超出此范围。 + 3. 在长时间调试依然 WA 时,为了不占用队友的上机时间,可以将输出流重定向到文件,打印出来再仔细比对。方法: + FILE* fp = freopen("out.txt", "w", stdout); + // ... + // printf ... + // ... + fclose(fp); // 不可缺少,否则文件不会保存 + 4. 不要使用过多的宏定义简写,容易出错,影响效率和调试体验。 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define rep(i, a, n) for (int i = a; i < n; i++) +#define erep(i, a, n) for (int i = a; i <= n; i++) +#define per(i, a, n) for (int i = n - 1; i >= a; i--) + +const int INF = 0x3f3f3f3f; +const double EPS = numeric_limits::epsilon(); +typedef long long ll; +#define MAX 1000 +// end of header + +int m; // x +int n; // y + +int matrix[MAX][MAX]; +int dp[MAX][MAX]; +int minWeight; + +// calculate total min weight and select the best route +int solve(int x, int y) { + if (dp[x][y] != -1) return dp[x][y]; + dp[x][y] = matrix[x][y]; + if (x >= 0 && x <= m-1 && y >= 0 && y <= n-2) { // stops when hit right bound + int minNext = INF; + int targetX; + erep(i, -1, 1) { + targetX = x + i; + // 越界处理 + if (targetX == -1) { + targetX = m - 1; + } else if (targetX == m) { + targetX = 0; + } + minNext = min(minNext, solve(targetX, y + 1) + matrix[x][y]); + } + dp[x][y] = minNext; + } + return dp[x][y]; +} + +// print the solution +void printPath() { + // find the starting point + int x; // starting point (x,0) + int minW = INF; + rep(i, 0, m) { + if (dp[i][0] < minW) { + minW = dp[i][0]; + x = i; + } + } + + vector path; + path.push_back(x); + int targetX; + int minTarget; + int minX; + int lastX = x; + + int traverseOrder[3]; // the order of searching + + rep(j, 1, n) { + minTarget = INF; + if (lastX == 0) { // make sure it's lexicographically ordered + traverseOrder[0] = 0; + traverseOrder[1] = 1; + traverseOrder[2] = -1; + } else if (lastX == m - 1) { + traverseOrder[0] = 1; + traverseOrder[1] = -1; + traverseOrder[2] = 0; + } else { + traverseOrder[0] = -1; + traverseOrder[1] = 0; + traverseOrder[2] = 1; + } + rep(i, 0, 3) { + targetX = lastX + traverseOrder[i]; + // 越界处理 + if (targetX == -1) { + targetX = m - 1; + } else if (targetX == m) { + targetX = 0; + } + // log the min path + if (dp[targetX][j] < minTarget) { + minTarget = dp[targetX][j]; + minX = targetX; + } + } + path.push_back(minX); + lastX = minX; + } + + // output + bool first = true; + rep(i, 0, n) { + if (!first) { + printf(" "); + } else { + first = false; + } + printf("%d", path[i] + 1); + } + printf("\n"); +} + +int main() { + while (~scanf("%d%d", &m, &n)) { + // init + memset(dp, -1, sizeof(dp)); + minWeight = INF; + // read + rep(i, 0, m) { + rep(j, 0, n) { + scanf("%d", &matrix[i][j]); + } + } + // parse + rep(i, 0, m) { + minWeight = min(minWeight, solve(i, 0)); + } + // output + printPath(); + printf("%d\n", minWeight); + } + return 0; +} diff --git a/uva_cpp_clean/116/116-9.cpp b/uva_cpp_clean/116/116-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54aa17e59108a64a8b12b7c4d1e66767b9303336 --- /dev/null +++ b/uva_cpp_clean/116/116-9.cpp @@ -0,0 +1,38 @@ +#include + +#define MAX_M 15 +#define MAX_N 110 +#define INF (int)1e9 + +using namespace std; + +int m,n,v[MAX_M][MAX_N],memo[MAX_M][MAX_N],take[MAX_M][MAX_N],ans,it; + +int dp(int x,int y){ + if(y==n) return 0; + if(memo[x][y]!=INF) return memo[x][y]; + int aux = INF; + for(int row=-1;row<=1;row++){ + int r = (x+row+m)%m; + int tmp = v[x][y]+dp(r,y+1); + if(tmp +using namespace std; + +struct Uf { + Uf(int n) : p(n), size(n, 1) { + iota(p.begin(), p.end(), 0); + } + int find(int x) { + if (x != p[x]) + p[x] = find(p[x]); + return p[x]; + } + int merge(int x, int y) { + int xp = find(x), yp = find(y); + if (xp == yp) + return; + if (size[xp] > size[yp]) + swap(xp, yp); + p[xp] = yp; + size[yp] += xp; + } + vector p, size; +}; + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + Uf uf(100001); + int a, b; + while (cin >> a) { + iota(uf.p.begin(), uf.p.end(), 0); + int refusal = 0; + while (a != -1) { + cin >> b; + int pa = uf.find(a), pb = uf.find(b); + if (pa == pb) + ++refusal; + else + uf.p[pa] = pb; + cin >> a; + } + cout << refusal << '\n'; + } + return 0; +} diff --git a/uva_cpp_clean/1160/1160-21.cpp b/uva_cpp_clean/1160/1160-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c856f74614533ca2fa6910ab02330bd163b44e0 --- /dev/null +++ b/uva_cpp_clean/1160/1160-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1160 + Name: X-Plosives + Problem: https://onlinejudge.org/external/11/1160.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int frn[100017], par[100017], cse; +int find(int u) { return par[u]<0 ? u : par[u]=find(par[u]); } +bool join(int u, int v) { + if ((u=find(u)) != (v=find(v))) { + if (par[v] < par[u]) + swap(u, v); + + par[u] += par[v]; + par[v] = u; + return 1; + } + return 0; +} + +void init(int u) { + if (frn[u] != cse) { + frn[u] = cse; + par[u] = -1; + } +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int cnt = 0, u, v; + for (cse=1; cin>>u; ) + if (u < 0) { + cout << cnt << endl; + cnt = 0; + ++cse; + } + else { + cin >> v; + init(u); init(v); + if (!join(u, v)) + ++cnt; + } +} diff --git a/uva_cpp_clean/1160/1160-9.cpp b/uva_cpp_clean/1160/1160-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08ce79a923387bd4e70b2743cce6decbc2e6d1fe --- /dev/null +++ b/uva_cpp_clean/1160/1160-9.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 100010; + +int a, b, ans; + +int main(){ + while(~scanf("%d %d", &a, &b)){ + UnionFind UF(SIZE); + ans = 0; + if( not UF.isSameSet(a, b) ) + UF.unionSet(a, b); + else + ans++; + while(scanf("%d", &a), a != -1){ + scanf("%d", &b); + if( not UF.isSameSet(a, b) ) + UF.unionSet(a, b); + else + ans++; + } + printf("%d\n", ans); + } +} diff --git a/uva_cpp_clean/11608/11608-21.cpp b/uva_cpp_clean/11608/11608-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b58ee8e0c8712d2ca1b3afdead85cd394784262 --- /dev/null +++ b/uva_cpp_clean/11608/11608-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11608 + Name: No Problem + Problem: https://onlinejudge.org/external/116/11608.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int cse=1, x, s, m[12]; + while (cin>>s && s>=0) { + for (int i=0; i<12; i++) + cin >> m[i]; + + printf("Case %d:\n", cse++); + for (int i=0; i<12; i++) { + cin >> x; + if (s >= x) { + puts("No problem! :D"); + s -= x; + } + else puts("No problem. :("); + s += m[i]; + } + } +} diff --git a/uva_cpp_clean/11608/11608-5.cpp b/uva_cpp_clean/11608/11608-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..af8bd25d4c979729a73b816be4eacdce37f437a8 --- /dev/null +++ b/uva_cpp_clean/11608/11608-5.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int a[20],b[20],no=0; + while(sc("%d",&a[0])) + { + if(a[0]<0) + break; + for(int i=1;i<=12;i++) + sc("%d",&a[i]); + for(int i=0;i<12;i++) + sc("%d",&b[i]); + pf("Case %d:\n",++no); + for(int i=0;i<12;i++) + { + if(a[i]-b[i]>=0) + { + a[i+1]=a[i+1]+a[i]-b[i]; + pf("No problem! :D\n"); + } + else + { + a[i+1]+=a[i]; + pf("No problem. :(\n"); + } + } + } + + + return 0; +} diff --git a/uva_cpp_clean/11609/11609-5.cpp b/uva_cpp_clean/11609/11609-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2475173def04a9e42394382de67573b67d686e99 --- /dev/null +++ b/uva_cpp_clean/11609/11609-5.cpp @@ -0,0 +1,60 @@ +/*-----------------------------------------------*/ +//Problem Setter: Towhidul Islam Talukdar +//Problem Name : Teams +//Uva Problem No: 11609 +//Type : Math,Bigmod,Summations. +//Author : Shipu Ahamed +//University : BUBT +//E-mail : shipuahamed01@gmail.com +/*-----------------------------------------------*/ +//Hints :http://www.outsbook.com/uva/?page=latest_post&category=-1&id=11609 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +#define mod 1000000007 +using namespace std; +ll bigmod (ll b, ll p) +{ + if (p==0) + return 1; + if (p==1) + return b; + if (p % 2==0 ) { + ll r = bigmod(b, p / 2) % mod; + return (r*r) % mod; + } + else + return (bigmod (b, p - 1) % mod) * (b % mod); +} +int main() +{ + int t,no=0; + cin>>t; + while(t--) + { + int n; + cin>>n; + pf("Case #%d: ",++no); + cout<<((n%mod)*bigmod(2,n-1)%mod)%mod< +/* +Problem: 11614 - Etruscan Warriors Never Play Chess +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2661 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int T; +ull N; + +int main () { + + scanf("%d\n", &T); + + while ( T-- ){ + scanf("%llu\n", &N); + // K(K+1)/2 = N + // K^2 + K - 2N = 0 + // a = 1, b = 1, c = -2N + // X = (-b + sqrt(b^2 - 4*a* c)) / 2a + // K = (-1 + sqrt( 1 - 4*1* -2*N)) / 2*1 + // K = (-1 + sqrt( 1 + 8*N)) / 2 + + printf("%d\n", (int)((-1+ pow(1 + 8.0*N, .5))/2)); + } + + return 0; +} +/* +Sample input:- +----------------- +6 +3 +6 +7 +8 +9 +10 + +Sample output:- +----------------- +2 +3 +3 +3 +3 +4 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11614/11614-14.cpp b/uva_cpp_clean/11614/11614-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68555269790c73545dbee0f28e04a8873ac43da4 --- /dev/null +++ b/uva_cpp_clean/11614/11614-14.cpp @@ -0,0 +1,84 @@ +/*************************************************** + * Problem name : 11614 Etruscan Warriors Never Play Chess.cpp + * Problem Link : https://uva.onlinejudge.org/external/116/11614.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-27 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +ll binarySearch(ull n) { + if(n == 0) return 0; + ll low = 1, high = 1500000000, ans, mid; + while (high >= low) { + mid = (high + low) / 2; + ull chk = (mid * (mid + 1) / 2); + if (chk == n) { + ans = mid; + break; + } else if (chk < n) { + ans = mid; + low = mid + 1; + } else high = mid - 1; + } + return ans; +} +int main () { + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + ll tc, n; + scanf("%lld", &tc); + for (ll t = 1; t <= tc; t++) { + scanf("%lld", &n); + printf("%lld\n", binarySearch(n)); + } + + return 0; +} + diff --git a/uva_cpp_clean/11614/11614-19.cpp b/uva_cpp_clean/11614/11614-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93fbb4a2d285e860a64c51a9b1fe967f2fa72813 --- /dev/null +++ b/uva_cpp_clean/11614/11614-19.cpp @@ -0,0 +1,15 @@ +#include +#include +using namespace std; + +int main() { + int m; + scanf("%d", &m); + while (m--) { + unsigned long long n; + scanf("%llu", &n); + unsigned long long r = (sqrt(1+8*n) - 1)/2; + printf("%llu\n", r); + } + return 0; +} diff --git a/uva_cpp_clean/11614/11614-20.cpp b/uva_cpp_clean/11614/11614-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6958f2e8f42854c58a11e3567f7dc2bba0b1d45d --- /dev/null +++ b/uva_cpp_clean/11614/11614-20.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int tc; + + cin >> tc; + while( tc-- ) + { + long long n; + cin >> n; + + long long row = (sqrt(1+8*n)-1)/2; + + cout << row << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11614/11614-21.cpp b/uva_cpp_clean/11614/11614-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d15a223dd099a5cf94512a9a65cd91477cb45cd9 --- /dev/null +++ b/uva_cpp_clean/11614/11614-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11614 + Name: Etruscan Warriors Never Play Chess + Problem: https://onlinejudge.org/external/116/11614.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + long long T, n; + cin>>T; + while (T--) { + cin>>n; + cout << (int) floor((1 + sqrt(1 + (long double)8*n))/2)-1 << endl; + } +} diff --git a/uva_cpp_clean/11614/11614-5.cpp b/uva_cpp_clean/11614/11614-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e1de3f1061595cdb02885b23fce1053eccd04246 --- /dev/null +++ b/uva_cpp_clean/11614/11614-5.cpp @@ -0,0 +1,97 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define CP(a) cout< + +using namespace std; + +int main(){ + int c; + long long n; + scanf("%d", &c); + while(c--){ + scanf("%lld", &n); + printf("%lld\n", (long long)(sqrt(1 + 8 * n) - 1) / 2); + } + return(0); +} diff --git a/uva_cpp_clean/11615/11615-21.cpp b/uva_cpp_clean/11615/11615-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c26dd310a623f035a2f4a34fe5abc7cd39edabe6 --- /dev/null +++ b/uva_cpp_clean/11615/11615-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11615 + Name: Family Tree + Problem: https://onlinejudge.org/external/116/11615.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int lg2(int x) { + int r = 0; + for (; x; x>>=1) + ++r; + return r-1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, a, b; + cin >> T; + while (T-- && cin >> n >> a >> b) { + int al = (1< +#include +#include +#include + +using namespace std; + +string arabicToRoman(int arabic) { + static int romanValues[13] = { 1000, 900, 500, 400, 100, 90, + 50, 40, 10, 9, 5, 4, 1 }; + static string romanStrings[13] = { "M", "CM", "D", "CD", "C", + "XC", "L", "XL", "X", "IX", + "V", "IV", "I" }; + + string result; + for(int i = 0; i < 13; ++i) { + if (arabic == 0) + break; + + int times = arabic / romanValues[i]; + arabic -= times * romanValues[i]; + + for(int j = 0; j < times; ++j) + result += romanStrings[i]; + } + + return result; +} + +int romanToArabic(const string& roman) { + map romanValues; + romanValues['M'] = 1000; + romanValues['D'] = 500; + romanValues['C'] = 100; + romanValues['L'] = 50; + romanValues['X'] = 10; + romanValues['V'] = 5; + romanValues['I'] = 1; + + int result = 0; + for(int i = 0; i < roman.size(); ++i) { + int value = romanValues[roman[i]]; + if (i < roman.size() - 1 && + value < romanValues[roman[i + 1]]) + result -= value; + else + result += value; + } + + return result; +} + +bool isArabic(const string& number) { + return number[0] >= '0' && number[0] <= '9'; +} + +int stringToInt(const string& number) { + istringstream input(number); + + int result; + input >> result; + + return result; +} + +int main() { + string number; + while(cin >> number) { + if (isArabic(number)) + cout << arabicToRoman(stringToInt(number)) << '\n'; + else + cout << romanToArabic(number) << '\n'; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11616/11616-19.cpp b/uva_cpp_clean/11616/11616-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21d0cd3b9fbb82e520774252a74e446577f0716e --- /dev/null +++ b/uva_cpp_clean/11616/11616-19.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +using namespace std; + +int value(char c) { + switch(c) { + case 'M': return 1000; + case 'D': return 500; + case 'C': return 100; + case 'L': return 50; + case 'X': return 10; + case 'V': return 5; + case 'I': return 1; + } + return -1; +} + +int toArabic(string number) { + int n = 0; + int level = 2000; + for (int i = 0; i < number.length(); i++) { + if (value(number[i]) > level) { + int val = value(number[i]) - 2*level; + level = value(number[i]); + n += val; + } + else { + level = value(number[i]); + n += value(number[i]); + } + } + return n; +} + +string value(char c, int pos) { + switch(c) { + case '9': return pos == 0? "IX": pos == 1? "XC": pos == 2? "CM": ""; + case '8': return pos == 0? "VIII": pos == 1? "LXXX": pos == 2? "DCCC": ""; + case '7': return pos == 0? "VII": pos == 1? "LXX": pos == 2? "DCC": ""; + case '6': return pos == 0? "VI": pos == 1? "LX": pos == 2? "DC": ""; + case '5': return pos == 0? "V": pos == 1? "L": pos == 2? "D": ""; + case '4': return pos == 0? "IV": pos == 1? "XL": pos == 2? "CD": ""; + case '3': return pos == 0? "III": pos == 1? "XXX": pos == 2? "CCC": "MMM"; + case '2': return pos == 0? "II": pos == 1? "XX": pos == 2? "CC": "MM"; + case '1': return pos == 0? "I": pos == 1? "X": pos == 2? "C": "M"; + } + return ""; +} + +string toRoman(string number) { + string n = ""; + for (int i = 0; i < number.length(); i++) { + n += value(number[i], number.length()-i-1); + } + return n; +} + +int main() { + string number; + while (cin >> number) { + if (isdigit(number[0])) cout << toRoman(number) << endl; + else cout << toArabic(number) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11616/11616-9.cpp b/uva_cpp_clean/11616/11616-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b827dba34d4a09019cb417fd65772229020a837 --- /dev/null +++ b/uva_cpp_clean/11616/11616-9.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +const int LEN = 50; + +char line[LEN]; + +void DecimalToRoman(int num){ + map < int, string > cvt; + cvt[1] = "I", cvt[10] = "X", cvt[100] = "C", cvt[1000] = "M"; + cvt[9] = "IX", cvt[90] = "XC", cvt[900] = "CM"; + cvt[5] = "V", cvt[50] = "L", cvt[500] = "D"; + cvt[4] = "IV", cvt[40] = "XL", cvt[400] = "CD"; + for(map :: reverse_iterator it = cvt.rbegin(); it != cvt.rend(); ++it) + while( num >= it -> first ){ + printf("%s", ( it -> second ).c_str()); + num -= it -> first; + } + putchar('\n'); +} + +void RomanToDecimal(char R[]){ + map < char, int > cvt; + cvt['I'] = 1, cvt['V'] = 5, cvt['X'] = 10, cvt['L'] = 50; + cvt['C'] = 100, cvt['D'] = 500, cvt['M'] = 1000; + int num = 0; + for(int it = 0; R[it]; it++) + if( R[it + 1] and cvt[R[it]] < cvt[R[it + 1]] ) + num += cvt[R[it + 1]] - cvt[R[it]], it++; + else + num += cvt[R[it]]; + printf("%d\n", num); +} + +int main(){ + while(~scanf("%s", line)){ + if( isdigit(line[0]) ) DecimalToRoman( atoi(line) ); + else RomanToDecimal(line); + } + return(0); +} diff --git a/uva_cpp_clean/11621/11621-21.cpp b/uva_cpp_clean/11621/11621-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b68f5398dabbe17eec1639c0b178747851775f23 --- /dev/null +++ b/uva_cpp_clean/11621/11621-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11621 + Name: Small Factors + Problem: https://onlinejudge.org/external/116/11621.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +typedef unsigned int uint; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector v; + uint maxi = 2147483648; + for (uint i=0, x=1; i<32; ++i, x*=2) + for (uint j=0, y=1; (long long)x*y<=maxi; ++j, y*=3) + v.push_back(x*y); + sort(v.begin(), v.end()); + + int n; + while (cin >> n && n) + cout << (*lower_bound(v.begin(), v.end(), n)) << "\n"; +} diff --git a/uva_cpp_clean/11621/11621-9.cpp b/uva_cpp_clean/11621/11621-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..430fd9aaea460943f7c8baa3717b2a45d7c25035 --- /dev/null +++ b/uva_cpp_clean/11621/11621-9.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +#define LIM (1L<<31) + +long num, aux; +vector seq; +int n, p; + +int main(){ + num = aux = 1; + while(true){ + if( num << 1 > LIM ) + break; + while(true){ + if( aux > LIM ) + break; + seq.push_back(aux); + aux *= 3; + } + num <<= 1; + aux = num; + } + sort(seq.begin(), seq.end()); + while(scanf("%d" ,&n), n){ + p = upper_bound(seq.begin(), seq.end(), n - 1) - seq.begin(); + printf("%d\n", seq[p]); + } + return(0); +} diff --git a/uva_cpp_clean/11624/11624-14.cpp b/uva_cpp_clean/11624/11624-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..049f9b7142f82860a01669382f76342976881037 --- /dev/null +++ b/uva_cpp_clean/11624/11624-14.cpp @@ -0,0 +1,227 @@ +/*************************************************** + * Problem Name : 11624 - Fire!.cpp + * Problem Link : https://uva.onlinejudge.org/external/116/11624.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-08-29 + * Problem Type : Graph(bfs) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 1005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int fx[] = {1, -1, 0, 0}; +int fy[] = {0, 0, 1, -1}; + + +vectorvec; +char str[MAX][MAX]; +bool vis[MAX][MAX]; +int dis[MAX][MAX]; +int dis1[MAX][MAX]; +int n, m; + +bool isValid (int x, int y) { + if (x >= 0 && x < n && y >= 0 && y < m && str[x][y] == '.' && !vis[x][y]) { + return true; + + } else { + return false; + } +} + + +void bfs1 () { + SET (vis, false); + SET (dis1, 0); + queueq; + + for (int i = 0; i < (int) vec.size(); i++) { + q.push ({vec[i].first, vec[i].second}); + dis1[vec[i].first][vec[i].second] = 0; + vis[vec[i].first][vec[i].second] = true; + } + + while (!q.empty() ) { + pii top = q.front(); + q.pop(); + + for (int i = 0; i < 4; i++) { + int tx = top.first + fx[i]; + int ty = top.second + fy[i]; + + if (isValid (tx, ty) ) { + dis1[tx][ty] = dis1[top.first][top.second] + 1; + vis[tx][ty] = true; + q.push ({tx, ty}); + } + } + } +} +int bfs (int sx, int sy) { + SET (vis, false); + SET (dis, 0); + queueq; + q.push ({sx, sy}); + vis[sx][sy] = true; + dis[sx][sy] = 0; + + while (!q.empty() ) { + pii top = q.front(); + q.pop(); + + if (top.first == 0 || top.first == n - 1 || top.second == 0 + || top.second == m - 1) { + return dis[top.first][top.second] + 1; + } + + for (int i = 0; i < 4; i++) { + int tx = top.first + fx[i]; + int ty = top.second + fy[i]; + + if (isValid (tx, ty) ) { + dis[tx][ty] = dis[top.first][top.second] + 1; + vis[tx][ty] = true; + + if (dis[tx][ty] < dis1[tx][ty] || dis1[tx][ty] == 0) { + if (tx == 0 || tx == n - 1 || ty == 0 || ty == m - 1) { + return dis[tx][ty] + 1; + } + + q.push ({tx, ty}); + } + } + } + } + + return -1; +} + + +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + int tc; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> n >> m; + int sx, sy; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> str[i][j]; + + if (str[i][j] == 'J') { + sx = i, sy = j; + + } else if (str[i][j] == 'F') { + vec.pb ({i, j}); + } + } + } + + if (sx == 0 || sx == n - 1 || sy == 0 || sy == m - 1) { + cout << 1 << "\n"; + + } else { + bfs1(); + int ans = bfs (sx, sy); + + if (ans == -1) { + cout << "IMPOSSIBLE\n"; + + } else { + cout << ans << "\n"; + } + } + + vec.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/11624/11624-19.cpp b/uva_cpp_clean/11624/11624-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7857aac2182292fc84d50df8787fe4442ae5c9f5 --- /dev/null +++ b/uva_cpp_clean/11624/11624-19.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iii; + +int mat[1001][1001]; +bool mask[1001][1001]; +bool mask2[1001][1001]; +int R, C; +int jx, jy; + +inline bool limits(int x, int y) { + return x < R && x >= 0 && y < C && y >= 0; +} + +inline bool border(int x, int y) { + return x == R-1 || x == 0 || y == C-1 || y == 0; +} + +queue fire; + +void flood() { + while (!fire.empty()) { + iii sq = fire.front(); + fire.pop(); + if (limits(sq.first.first, sq.first.second) && !mask2[sq.first.first][sq.first.second]) { + mask2[sq.first.first][sq.first.second] = true; + mat[sq.first.first][sq.first.second] = sq.second; + fire.push(iii(ii(sq.first.first + 1, sq.first.second), sq.second + 1)); + fire.push(iii(ii(sq.first.first - 1, sq.first.second), sq.second + 1)); + fire.push(iii(ii(sq.first.first, sq.first.second + 1), sq.second + 1)); + fire.push(iii(ii(sq.first.first, sq.first.second - 1), sq.second + 1)); + } + } +} + +int main() { + int T; + cin >> T; + while (T--) { + cin >> R >> C; + bool ok = false; + for (int i = 0; i < R; i++) { + for (int j = 0; j < C; j++) { + char c; + cin >> c; + if (c == '#') { + mat[i][j] = -1; + mask[i][j] = mask2[i][j] = true; + } + else { + mat[i][j] = 1000000; + if (c == 'J') jx = i, jy = j; + if (c == 'F') fire.push(iii(ii(i, j), 0)); + mask[i][j] = mask2[i][j] = false; + } + } + + } + flood(); + queue q; + q.push(iii(ii(jx, jy), 0)); + ok = false; + int sol; + while (!q.empty() && !ok) { + iii sq = q.front(); + q.pop(); + if (limits(sq.first.first, sq.first.second) && sq.second < mat[sq.first.first][sq.first.second]) { + if (border(sq.first.first, sq.first.second)) ok = true, sol = sq.second; + else if (!mask[sq.first.first][sq.first.second]) { + mask[sq.first.first][sq.first.second] = true; + q.push(iii(ii(sq.first.first + 1, sq.first.second), sq.second + 1)); + q.push(iii(ii(sq.first.first - 1, sq.first.second), sq.second + 1)); + q.push(iii(ii(sq.first.first, sq.first.second + 1), sq.second + 1)); + q.push(iii(ii(sq.first.first, sq.first.second - 1), sq.second + 1)); + } + } + } + if (ok) printf("%d\n", sol + 1); + else printf("IMPOSSIBLE\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11624/11624-21.cpp b/uva_cpp_clean/11624/11624-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1b531f31fe00d4759b1958a7e6b9d0d50a92fa0 --- /dev/null +++ b/uva_cpp_clean/11624/11624-21.cpp @@ -0,0 +1,141 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11624 + Name: Fire! + Problem: https://onlinejudge.org/external/116/11624.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +inline bool readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return 0; + + *str++ = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + *str++ = ch; + *str = 0; + return 1; +} + +// ------------------------------------------------------------------- + + + +struct State { + int i, j, d; + State(int i, int j, int d):i(i),j(j),d(d){} +}; + +int dy[] = {1,-1,0,0}, + dx[] = {0,0,1,-1}; +bool S[1017][1017], B[1017][1017]; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char line[1017]; + int T = readUInt(); + while (T--) { + int n = readUInt(), + m = readUInt(); + + queue q1, q2; + for (int i=0; i d) break; + q1.pop(); + if (B[s.i][s.j]) continue; + + for (int k=0; k<4; ++k) { + State t(s.i+dy[k], s.j+dx[k], s.d+1); + if (t.i<0 || t.j<0 || t.i>=n || t.j>=m) { + cout << t.d << endl; + goto fin; + } + if (!S[t.i][t.j]) { + q1.push(t); + S[t.i][t.j] = 1; + } + } + } + + while (!q2.empty()) { + State s = q2.front(); + if (s.d > d) break; + q2.pop(); + + for (int k=0; k<4; ++k) { + State t(s.i+dy[k], s.j+dx[k], s.d+1); + if (t.i<0 || t.j<0 || t.i>=n || t.j>=m) + continue; + + if (!B[t.i][t.j]) { + q2.push(t); + B[t.i][t.j] = + S[t.i][t.j] = 1; + } + } + } + } + cout << "IMPOSSIBLE\n"; + fin:; + } +} diff --git a/uva_cpp_clean/11624/11624-5.cpp b/uva_cpp_clean/11624/11624-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ba30e9694199421c41dde256f90eaf79f7bd48b4 --- /dev/null +++ b/uva_cpp_clean/11624/11624-5.cpp @@ -0,0 +1,257 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() +#define coff ios_base::sync_with_stdio(0); + +#define ff first +#define se second +#define pb push_back +#define sz(a) ((int)a.size()) +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one. in long long use __builtin_popcountll(i) +#define parity(i) __builtin_parity(i) //evenparity 0 and odd parity 1 +#define btz(a) __builtin_ctz(a) //count binary trailling zero +#define un(v) ST(v), (v).erase(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +typedef long long ll; +typedef unsigned long long ull; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Debug +#define dbg(x) cout<<#x<<'='<>= 1; } return res; } +ll modInverse(ll a, ll m){return bigmod(a,m-2,m);} + +////============ CONSTANT ===============//// +#define inf INT_MAX //infinity value +#define eps 1e-9 +#define mx 100010 +#define mod 1000000007 +////=====================================//// + + +int row,col; + +struct info +{ + int x,y; + info(int xx,int yy) + { + x=xx; + y=yy; + } + info(){} +}; + +int dx[]={0,0,1,-1}; +int dy[]={1,-1,0,0}; + +int in_grid(int i,int j) +{ + if((i >= 0 && i < row) && (j >= 0 && j < col)) + { + return 1; + } + else + return 0; +} + + +int jan[1010][1010],f[1010][1010]; +bool vis[1010][1010]; +char grid[1010][1010]; + +int bfs(info n) +{ + cover(vis,0); + + queueq; + q.push(n); + + jan[n.x][n.y]=0; + + while(!q.empty()) + { + info p=q.front(); + q.pop(); + + for(int i=0;i<4;i++) + { + int x=p.x+dx[i]; + int y=p.y+dy[i]; + + if(!in_grid(x,y)) + continue; + + if(!vis[x][y] && grid[x][y]=='.') + { + if(jan[p.x][p.y]==inf) + jan[p.x][p.y]=0; + jan[x][y]=jan[p.x][p.y]+1; + vis[x][y]=1; + q.push(info(x,y)); + } + } + } + + cover(vis,0); + + vectorfire; + for(int i=0;i +using namespace std; + +typedef long long ll; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +int main() { + int n, m; + int c[10001]; + while (cin >> n >> m && !(n == 0 && m == 0)) { + int sum = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) + cin >> c[i]; + sum += c[i]; + } + for (int i = 0; i < n; i++) { + int g = gcd(c[i], sum); + printf("%d / %d\n", c[i] / g, sum / g); + } + } + return 0; +} diff --git a/uva_cpp_clean/11629/11629-14.cpp b/uva_cpp_clean/11629/11629-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86e6326c357ca572be41871c1778412a26d46bd1 --- /dev/null +++ b/uva_cpp_clean/11629/11629-14.cpp @@ -0,0 +1,189 @@ +/*************************************************** + * Problem Name : 11629 - Ballot evaluation.cpp + * Problem Link : https://onlinejudge.org/external/116/11629.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-11-15 + * Problem Type : + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +vector purse (string str) { + stringstream ss; + ss << str; + vectorvec; + string num; + + while (ss >> num) { + vec.pb (num); + } + + return vec; +} +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int p, g; + cin >> p >> g; + mapmp; + + for (int i = 0; i < p; i++) { + string str; + double val; + cin >> str >> val; + mp[str] = val * 100; + } + + cin.ignore(); + int t = 1; + + while (g--) { + string str; + getline (cin, str); + vectorvec = purse (str); + int sz = vec.size(); + int val = toInt (vec[sz - 1]); + string sign = vec[sz - 2]; + double sum = 0; + + for (int i = 0; i < sz - 2; i++) { + sum += mp[vec[i]]; + } + + bool f = 0; + val = val * 100; + + if (sign == ">") { + if (sum > val) f = 1; + + } else if (sign == "<") { + if (sum < val) f = 1; + + } else if (sign == ">=") { + if (sum >= val) f = 1; + + } else if (sign == "<=") { + if (sum <= val) f = 1; + + } else { + if (sum == val) f = 1; + } + + if (!f) { + cout << "Guess #" << t++ << " was incorrect.\n"; + + } else { + cout << "Guess #" << t++ << " was correct.\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/11629/11629-21.cpp b/uva_cpp_clean/11629/11629-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..233aa2ca7c91bd3aef6960f4a1cff4d75d3f94eb --- /dev/null +++ b/uva_cpp_clean/11629/11629-21.cpp @@ -0,0 +1,90 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11629 + Name: Ballot evaluation + Problem: https://onlinejudge.org/external/116/11629.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define EPS 1e-9 +struct Node { + int val; + Node *nxt[127]; + Node() { + val = -1; + memset(nxt, 0, sizeof(nxt)); + } + ~Node() { + for (int i=0; i<127; ++i) + if (nxt[i]) + delete nxt[i]; + } +}; + +Node *root; +void assignVal(char str[], int val) { + Node *cur = root; + for (int i=0; str[i]; ++i) { + if (!cur->nxt[str[i]]) + cur->nxt[str[i]] = new Node(); + + cur = cur->nxt[str[i]]; + } + cur->val = val; +} + +int readVal(char str[]) { + Node *cur = root; + for (int i=0; str[i]; ++i) + cur = cur->nxt[str[i]]; + return cur->val; +} + +bool check(int x, char ch[]) { + if (ch[0]=='<') + return ch[1]=='=' ? x<=0 : x<0; + + if (ch[0]=='>') + return ch[1]=='=' ? x>=0 : x>0; + + return !x; +} + +int main() { + int n, m; + double x; + char str[50]; + while (scanf("%d%d", &n, &m)==2) { + if (root) delete root; + root = new Node(); + + for (int i=0; i +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)2e5+5; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; + +int minimumSpanningTree(vector> &edges) { + + + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + int ans = 0,a,b,weight; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + } else { + ans +=weight; + } + } + return ans; +} + + +int main() { + FAST + + int n,m,a,b,w; + + while (cin>>n>>m) { + if(n==0 && m==0) break; + vector> arr; + + for(int i=0;i>a>>b>>w; + arr.push_back({w,a,b}); + } + + int res = minimumSpanningTree(arr); + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 200010 + +int n, m, sum = 0; +int parent[MAX], sz[MAX]; + +ofstream fout("f.out"); + +class comparator +{ +public: + + bool operator()(pair, int> &child1, pair, int> &child2) + { + return child1.second > child2.second; + } +}; + +priority_queue< pair< pair< int, int >, int >, vector< pair< pair< int, int >, int > >, comparator> pq; + +int findParent(int x) +{ + if (x == parent[x]) return parent[x]; + + else return parent[x] = findParent(parent[x]); +} + +bool merge(pair edge) +{ + int x = edge.first; + + int y = edge.second; + + if (findParent(x) == findParent(y)) return true; + + else + { + x = findParent(x); + + y = findParent(y); + + if (sz[x] < sz[y]) swap(x, y); + + parent[y] = x; + + sz[x] += sz[y]; + + return false; + } +} + +void initialize(int n) +{ + sum = 0; + + memset(sz, 1, sizeof sz); + + for (int i = 0; i < n; i++) + { + parent[i] = i; + } + + while (!pq.empty()) + { + pq.pop(); + } +} + +int mst() +{ + int ans = 0, how = 0; + + while (how != n - 1) + { + pair < pair , int > temp = pq.top(); + + if (!merge(temp.first)) + { + ans += temp.second; + + how++; + } + + pq.pop(); + } + + return ans; +} + +int main() +{ + while (cin >> n >> m, n != 0 || m != 0) + { + initialize(n); + + for (int i = 0; i < m; i++) + { + int x, y, w; + + cin >> x >> y >> w; + + sum += w; + + pq.push(make_pair(make_pair(x, y), w)); + } + + cout << sum - mst() << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11631/11631-19.cpp b/uva_cpp_clean/11631/11631-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5c974ad66c669998206f06b5a1196668662ce07 --- /dev/null +++ b/uva_cpp_clean/11631/11631-19.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iii; +typedef vector vi; + +struct ufds { + vi p, r; + int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { + return (p[i] == i) ? i : (p[i] = findSet(p[i])); + } + bool isSameSet(int i, int j) { + return findSet(i) == findSet(j); + } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } + numSets--; + } + } +}; + +int main() { + int m, n; + priority_queue q; + while (scanf("%d %d", &m, &n) && !(m == 0 && n == 0)) { + ufds u(m+1); + int sum = 0; + for (int i = 0; i < n; i++) { + int x, y, z; + scanf("%d %d %d", &x, &y, &z); + q.push(iii(-z, ii(x, y))); + sum += z; + } + while (!q.empty()) { + if(!u.isSameSet(q.top().second.first, q.top().second.second)) { + sum += q.top().first; + u.unionSet(q.top().second.first, q.top().second.second); + } + q.pop(); + } + printf("%d\n", sum); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11631/11631-20.cpp b/uva_cpp_clean/11631/11631-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2edf8c979b5351f9cb9046a1e8afc481a7d9561 --- /dev/null +++ b/uva_cpp_clean/11631/11631-20.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +const int MAXM = 200000; +int p[MAXM+2]; + +struct data { + int x, y, cost; + bool operator < ( const data& t ) const { + return ( cost < t.cost ); + } +} ; + +vector < data > edges; + +int findParent( int node ) { + if( p[node] == node ) return (node); + return ( p[node] = findParent( p[node] ) ); +} + +int mst( int nNode ) { + sort( edges.begin(), edges.end() ); + for( int i = 0 ; i < nNode ; ++i ) { + p[i] = i; + } + + int nodeCounter = 0, cost = 0; + for( int i = 0 ; i < edges.size() ; ++i ) { + int x = findParent( edges[i].x ); + int y = findParent( edges[i].y ); + if( x != y ) { + p[x] = y; + ++nodeCounter; + cost += edges[i].cost; + if( nodeCounter == (nNode - 1) ) { + break; + } + } + } + return (cost); +} + +int main( int argc, char ** argv ) { + int m, n; + while( scanf( "%d %d", &m, &n ) == 2 && (m && n) ) { + int totalCost = 0; + for( int i = 1 ; i <= n ; ++i ) { + int x, y, z; + scanf( "%d %d %d", &x, &y, &z ); + data temp; + temp.x = x, temp.y = y, temp.cost = z; + edges.push_back( temp ); + totalCost += z; + } + + int minCost = mst( m ); + + printf( "%d\n", (totalCost - minCost) ); + + edges.clear(); + } + return 0; +} diff --git a/uva_cpp_clean/11631/11631-21.cpp b/uva_cpp_clean/11631/11631-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f07a7c776108b42fe102b8ffb59ecfb5f230489 --- /dev/null +++ b/uva_cpp_clean/11631/11631-21.cpp @@ -0,0 +1,92 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11631 + Name: Dark roads + Problem: https://onlinejudge.org/external/116/11631.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +// --------------------------------------------------------------------- + +#define MAXN 200002 + +int par[MAXN]; +int find(int u) { return par[u]<0 ? u : par[u]=find(par[u]); } +void join(int u, int v) { + if ((u=find(u)) == (v=find(v))) return; + if (par[v] < par[u]) + swap(u, v); + par[u] += par[v]; + par[v] = u; +} + + +struct Edge { + int u, v, d; + bool operator < (const Edge &e) const { return d < e.d; } +} q[MAXN]; + + + +int main(){ + int n; + while ((n = readUInt())) { + int m = readUInt(); + memset(par, -1, n*sizeof(int)); + + int sum = 0; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int par[200010]; + +struct info{ + int u,v,cost; + info(int a,int b,int c) + { + u=a; + v=b; + cost=c; + } +}; + +int Find(int n) +{ + if(par[n]==n) + return n; + + return par[n]=Find(par[n]); +} + +void Union(int a,int b) +{ + int u=Find(a); + int v=Find(b); + if(u!=v) + { + par[u]=v; + } +} + +bool cmp(info a,info b) +{ + return a.costv; + + int total=0; + while(m--) + { + int a,b,c; + sii(a,b),si(c); + v.pb(info(a,b,c)); + total+=c; + } + + sort(all(v),cmp); + int len=v.size(); + + int sum=0; + for0(i,len) + { + int x = Find(v[i].u); + int y = Find(v[i].v); + if(x!=y) + { + sum+=v[i].cost; + Union(x,y); + } + } + + P(total-sum); + + } + + + return 0; +} diff --git a/uva_cpp_clean/11631/11631-9.cpp b/uva_cpp_clean/11631/11631-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6b3aa06621a934bf266a419088085afd516f6b5 --- /dev/null +++ b/uva_cpp_clean/11631/11631-9.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +int n, m, x, y, w, total_cost; +vector < pair < int, ii > > EdgeList; + +int Kruskal(){ + sort(EdgeList.begin(), EdgeList.end()); + UnionFind UF(n); + int min_cost = 0, setSize = 1; + for(int it = 0; it < m; it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + min_cost += front.first, setSize++; + UF.unionSet(front.second.first, front.second.second); + if( setSize == n ) break; + } + return min_cost; +} + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + EdgeList.clear(); + total_cost = 0; + for(int it = 0; it < m; it++){ + scanf("%d %d %d", &x, &y, &w); + EdgeList.push_back( make_pair( w, ii(x, y) )); + total_cost += w; + } + printf("%d\n", total_cost - Kruskal()); + } + return(0); +} diff --git a/uva_cpp_clean/11634/11634-9.cpp b/uva_cpp_clean/11634/11634-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e0016f75f2bd7bb009bed9f8279da563ab86880 --- /dev/null +++ b/uva_cpp_clean/11634/11634-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +const int SIZE = 10005; + +int n, ct; +bool vis[SIZE]; + +int numValues(){ + ct = 0; + memset(vis, false, sizeof vis); + while( not vis[n] ){ + vis[n] = true; + n = ( n * n / 100 ) % 10000; + ct++; + } + return ct; +} + +int main(){ + while( scanf("%d", &n), n) + printf("%d\n", numValues()); + return(0); +} diff --git a/uva_cpp_clean/11636/11636-19.cpp b/uva_cpp_clean/11636/11636-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45fd530d4b154f9ab9817160d4b37ba5a656cc57 --- /dev/null +++ b/uva_cpp_clean/11636/11636-19.cpp @@ -0,0 +1,11 @@ +#include +#include +using namespace std; + +int main() { + int N, X = 1; + while (scanf("%d", &N) && N > 0) { + printf("Case %d: %d\n", X++, (int)ceil(log2(N))); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11636/11636-21.cpp b/uva_cpp_clean/11636/11636-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c00f90c39131af8cf353435c3398d8da356c24e8 --- /dev/null +++ b/uva_cpp_clean/11636/11636-21.cpp @@ -0,0 +1,23 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11636 + Name: Hello World! + Problem: https://onlinejudge.org/external/116/11636.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int n; + for(int cse=1; cin>>n && n>=0; cse++) + printf("Case %d: %d\n", cse, !n ? 0 : int(ceil(log2(n)))); +} + diff --git a/uva_cpp_clean/11636/11636-5.cpp b/uva_cpp_clean/11636/11636-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b3bf354529df5a91a65fc09a024141a49f0a3149 --- /dev/null +++ b/uva_cpp_clean/11636/11636-5.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int n,no=0,c; + while(sc("%d",&n)==1) + { + if(n<0) + break; + c=0; + for(int i=1;i +using namespace std; + +int main() { + int T; + cin >> T; + while (T--) { + int h, m; + char c; + cin >> h >> c >> m; + h = 12 - h; + if (h == 0) h = 12; + m = 60 - m; + if (m == 60) m = 0; + if (m != 0) h--; + if (h == 0) h = 12; + printf("%02d:%02d\n", h, m); + } + return 0; +} diff --git a/uva_cpp_clean/11650/11650-20.cpp b/uva_cpp_clean/11650/11650-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7434121a656e37be95abe4b3cd6e118bab197ee1 --- /dev/null +++ b/uva_cpp_clean/11650/11650-20.cpp @@ -0,0 +1,21 @@ +#include + +int main() +{ + int tc; + + scanf("%d", &tc); + while( tc-- ) + { + int HH, MM; + scanf("%d:%d", &HH, &MM); + + HH = 11-HH; + if( !MM ) HH++; + if( HH<=0 ) HH += 12; + if( MM ) MM = 60-MM; + + printf("%02d:%02d\n", HH, MM); + } + return 0; +} diff --git a/uva_cpp_clean/11650/11650-21.cpp b/uva_cpp_clean/11650/11650-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..71838072ac480f49dd414dfb451d5733cda6813d --- /dev/null +++ b/uva_cpp_clean/11650/11650-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11650 + Name: Mirror Clock + Problem: https://onlinejudge.org/external/116/11650.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, h, m; + cin>>T; + while (T--) { + scanf("%d:%d", &h, &m); + + int t = 720 - (h%12)*60 - m; + h = t/60; m = t%60; + + printf("%02d:%02d\n", h?h:12, m); + } +} diff --git a/uva_cpp_clean/11650/11650-5.cpp b/uva_cpp_clean/11650/11650-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5487f5b7801c95e6866e3c4cb0dff9555ad3516a --- /dev/null +++ b/uva_cpp_clean/11650/11650-5.cpp @@ -0,0 +1,20 @@ +#include +int main() + { + int t,a,b; + scanf("%d", &t); + while(t--) + { + scanf("%d:%d", &a, &b); + a=11-a+(b==0); + if(a<=0) + a=a+12; + if(a!=0) + b=60-b; + if(b==60) + b=0; + + printf("%02d:%02d\n",a,b); + } + return 0; +} diff --git a/uva_cpp_clean/11655/11655-21.cpp b/uva_cpp_clean/11655/11655-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddbd7fd5c32eea633c424742bcd06886ec22d3ad --- /dev/null +++ b/uva_cpp_clean/11655/11655-21.cpp @@ -0,0 +1,112 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11655 + Name: Waterland + Problem: https://onlinejudge.org/external/116/11655.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------------- + +#define MODUL 100000 +int C1[5555], C2[5555], n; +vector adj[5555], adjr[5555]; +bool seen[5555], isin[5555]; +int ord[5555], oi; +void dfs(int u) { + seen[u] = true; + + if (!(isin[u] = u==n-1)) + for (int v: adj[u]) { + if (!seen[v]) + dfs(v); + isin[u] |= isin[v]; + } + + if (isin[u]) + ord[--oi] = u; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + for (int cse=1; cse<=T; ++cse) { + n = readUInt(); + for (int i=0; i=oi; --i) { + int u = ord[i]; + if (u) + s = (s + (long long)C1[u] * C2[u]) % MODUL; + for (int v: adjr[u]) + C2[v] = (C2[v] + C2[u]) % MODUL; + } + + cout << "Case " << cse << ": " + << s << ' ' << C1[n-1] << "\n"; + } +} diff --git a/uva_cpp_clean/11658/11658-21.cpp b/uva_cpp_clean/11658/11658-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be9f85c4d9973fb21fa113ca2670156cfa260334 --- /dev/null +++ b/uva_cpp_clean/11658/11658-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11658 + Name: Best Coalitions + Problem: https://onlinejudge.org/external/116/11658.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[143], n, x, bs; + while (cin >> n >> x && (n||x)) { + int sz = 0; + for (int i=1, d, d1, d2; i<=n; ++i) { + cin >> d1; + cin.ignore(1,'.'); + cin >> d2; + d = d1 * 100 + d2; + if (i == x) + bs = d; + else X[sz++] = d; + } + + int r = 0; + if (bs < 5000) { + bool dp[10000] = {-1}; + int mx=10000-bs; + for (int i=0; i=0; --j) + dp[j] |= dp[j-X[i]]; + + for (int i=5001-bs; i<=mx; ++i) + if (dp[i]) { + r = i; + break; + } + } + + cout << fixed << setprecision(2) + << round(10000.0 * bs / (bs + r))/100.0 << endl; + } +} diff --git a/uva_cpp_clean/11658/11658-6.cpp b/uva_cpp_clean/11658/11658-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..442eb61125dc0b651c308507a670d69defc388e2 --- /dev/null +++ b/uva_cpp_clean/11658/11658-6.cpp @@ -0,0 +1,49 @@ +/* + dynamic programming > subset sum + difficulty: medium + date: 15/May/2020 + problem: find the smallest sum s of a subset of A, s >= S + hint: scan as %d.%d + by: @brpapa +*/ +#include +using namespace std; +const int INF = 1 << 30; + +vector A; +int S; + +// retorna a menor soma entre elementos de A, mas que seja >= S +int memo[101][5001]; +int dp(int i, int s) { + // i-ésimo número de A, soma s do subconjunto atual + + if (s >= S) return s; + if (i == A.size()) return INF; // inválido + + int &ans = memo[i][s]; + if (ans != -1) return ans; + + return ans = min(dp(i+1, s+A[i]), dp(i+1, s)); +} + +int main() { + int N, x; + while (cin >> N >> x && (N || x)) { + A.clear(); x--; + int me; + for (int n = 0; n < N; n++) { + int a, b; scanf("%d.%d", &a, &b); int c = a*100 + b; + if (n == x) me = c; + else A.push_back(c); + } + + S = 5001 - me; // faltante até alcançar 50.01% + memset(memo, -1, sizeof memo); + + int num = me*100; + int den = me + dp(0, 0); + printf("%.2f\n", (double)num/den); + } + return 0; +} diff --git a/uva_cpp_clean/11658/11658-9.cpp b/uva_cpp_clean/11658/11658-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..395a5c4a3af880e91b9c55e8131be2cb5c4b744c --- /dev/null +++ b/uva_cpp_clean/11658/11658-9.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +const int MAX_N = 100 + 5, MAX_SUM = 5000 + 5, INF = 1e7; + +int n, x, arr[MAX_N], memo[MAX_N][MAX_SUM]; + +int dp (int id, int sum) { + if (5000 < sum) return sum; + if (id == n + 1) return INF; + if (~memo[id][sum]) return memo[id][sum]; + if (id == x) return dp(id + 1, sum); + return memo[id][sum] = min(dp(id + 1, sum), dp(id + 1, sum + arr[id])); +} + +void read () { + for (int i = 1; i <= n; i++) { + int a, b; + scanf("%d.%d", &a, &b); + arr[i] = a * 100 + b; + } +} + +void clear () { + memset(memo, -1, sizeof memo); +} + +int main () { + while (scanf("%d %d", &n, &x), n or x) { + clear(); + read(); + printf("%.2lf\n", 100.0 * arr[x] / dp(1, arr[x])); + } + return (0); +} diff --git a/uva_cpp_clean/11661/11661-18.cpp b/uva_cpp_clean/11661/11661-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1f17bad9d0bcf26a0cfb57674e0a423200a3e7db --- /dev/null +++ b/uva_cpp_clean/11661/11661-18.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main(){ + string highway; + int l; + while(scanf("%d", &l) && l != 0){ + cin >> highway; + int minDistance = l, lastR = -1, lastD = -1; + bool zFound = false; + for(int i = 0; i < l; i++){ + if(highway[i] == 'Z'){ + printf("0\n"); + zFound = true; + break; + } + if(highway[i] == 'R') lastR = i; + if(highway[i] == 'D') lastD = i; + if(lastR >= 0 && lastD >= 0) minDistance = min(minDistance, abs(lastD - lastR)); + } + if(!zFound) printf("%d\n", minDistance); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11661/11661-19.cpp b/uva_cpp_clean/11661/11661-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c42d9d9684b6e6cbbb33f00cab81738eeb434e7b --- /dev/null +++ b/uva_cpp_clean/11661/11661-19.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +using namespace std; + +int main() { + int L; + string s; + while (cin >> L && L != 0) { + cin >> s; + int d = -1, r = -1, dist = L + 1; + for (int i = 0; i < L; i++) { + if (s[i] == 'R') r = i; + else if (s[i] == 'D') d = i; + else if (s[i] == 'Z') { + d = r = i, dist = 0; + break; + } else continue; + if (d >= 0 && r >= 0) dist = min(dist, abs(d - r)); + } + printf("%d\n", dist); + } + return 0; +} diff --git a/uva_cpp_clean/11661/11661-20.cpp b/uva_cpp_clean/11661/11661-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9dd1cde3f43e5f620b70c4020bb4c272a4b025bc --- /dev/null +++ b/uva_cpp_clean/11661/11661-20.cpp @@ -0,0 +1,39 @@ +#include + +char S[2000005]; + +int main() +{ + int L; + char dump; + + while( scanf("%d%c", &L, &dump)==2 && L ) + { + gets(S); + + int R = -1, D = -1, min_distance = 2000001; + for(int I=0 ; I-1 ) + if( I-D-1 ) + if( I-R>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11661 + Name: Burger Time? + Problem: https://onlinejudge.org/external/116/11661.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define INF 3000000 + +int main(){ + int l; + while (cin>>l && l) { + while (getchar()!='\n'); + + int mn=INF; + int lastD=-INF, lastR=-INF; + for (int i=0; i +#include +char a[2000010]; +int main() +{ + int n,i,d,min,f; + while(scanf("%d",&n)==1) + { + if(n==0) + break; + scanf("%s",a); + f=0; + d=1; + min=2000010; + for(i=0;id) + min=d; + i--; + d=1; + break; + + } + else if(a[i]=='Z') + { + min=0; + f=1; + break; + } + + } + } + else if(a[i]=='D') + { + i++; + for(i=i;id) + min=d; + i--; + d=1; + break; + } + else if(a[i]=='Z') + { + min=0; + f=1; + break; + } + + } + + } + else + continue; + } + printf("%d\n",min); + } + return 0; +} diff --git a/uva_cpp_clean/11661/main.cpp b/uva_cpp_clean/11661/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7cf0379c8710df28954b32aedf38c5d53c36a58 --- /dev/null +++ b/uva_cpp_clean/11661/main.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +using namespace std; + +int main(){ + string highway; + int lenght, better, memory_D, memory_R; + while(cin >> lenght && lenght != 0){ + cin.ignore(); + better = INT_MAX; + memory_D = -1; + memory_R = -1; + getline(cin, highway); + for(int i = 0; i < lenght; i++){ + if(highway[i] == 'Z') better = 0; + if(highway[i] == 'R') memory_R = i; + if(highway[i] == 'D') memory_D = i; + if(memory_D != -1 && memory_R!=-1) better = min(better, max(memory_D,memory_R)-min(memory_D,memory_R)); + } + cout << better << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/11664/11664-21.cpp b/uva_cpp_clean/11664/11664-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43ebd6a9f0d417d8b140b9f8d3f68b68672abbdd --- /dev/null +++ b/uva_cpp_clean/11664/11664-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11664 + Name: Langton's Ant + Problem: https://onlinejudge.org/external/116/11664.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +int l, r; +char c[100]; +bool isMod2() { + return c[r-1]&1; +} +void shr() { + int t = 0; + for (int i=l; i>1; + t -= c[i]<<1; + } + while (l0; --i) { + M[i][n+1] = M[n+1][i] = 3; + for (int j=n; j>0; --j) { + M[i][j] = isMod2(); + shr(); + } + } + + M[n][n] = 2; + for (char d = 0; ;) { + if (M[y][x]&2) break; + if (M[y][x]) --d; + else ++d; + d = (d+4)%4; + M[y][x] = !M[y][x]; + y += dy[d]; + x += dx[d]; + } + puts( M[y][x] == 2 ? "Yes" : "Kaputt!"); + } +} diff --git a/uva_cpp_clean/11677/11677-19.cpp b/uva_cpp_clean/11677/11677-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff3857a1435746a8a9352433f0249023a5c9fde5 --- /dev/null +++ b/uva_cpp_clean/11677/11677-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int h1, m1, h2, m2; + while (cin >> h1 >> m1 >> h2 >> m2 && !(h1 == 0 && m1 == 0 && h2 == 0 && m2 == 0)) { + m1 += h1*60, m2 += h2*60; + int r = m2 - m1; + if (r < 0) r += 1440; + printf("%d\n", r); + } + return 0; +} diff --git a/uva_cpp_clean/11677/11677-21.cpp b/uva_cpp_clean/11677/11677-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4862152e4699dd3e3f49cdb1b22754285136bd87 --- /dev/null +++ b/uva_cpp_clean/11677/11677-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11677 + Name: Alarm Clock + Problem: https://onlinejudge.org/external/116/11677.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int h1, m1, h2, m2; + while (cin>>h1>>m1>>h2>>m2 && (h1||m1||h2||m2)) + cout << ( (h2*60+m2 - h1*60-m1 + 24*60) % (24*60) ) << endl; +} diff --git a/uva_cpp_clean/11677/11677-5.cpp b/uva_cpp_clean/11677/11677-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..30acb75c14d4235e3e0402fdb3c58f3d4801d3ba --- /dev/null +++ b/uva_cpp_clean/11677/11677-5.cpp @@ -0,0 +1,27 @@ +#include +int main() +{ + int a,b,c,d; + while(scanf("%d%d%d%d",&a,&b,&c,&d)==4 && (a<24) && (b<60) && (c<24) && (d<60)) + { + if(a==0&&b==0&&c==0&&d==0) + break; + else + { + a=c-a; + b=d-b; + if(b<0) + { + b=b+60; + a=a-1; + } + if(a<0) + a=a+24; + + a=a*60+b; + } + printf("%d\n",a); + } + return 0 ; + +} diff --git a/uva_cpp_clean/11679/11679-2.cpp b/uva_cpp_clean/11679/11679-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54d7ca64514948b376905546350d52bf807dde50 --- /dev/null +++ b/uva_cpp_clean/11679/11679-2.cpp @@ -0,0 +1,54 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int b, n; + while (cin >> b >> n) { + if (!b && !n) break; + vector ve(b); + for (int i = 0; i < b; ++i) cin >> ve[i]; + for (int i = 0; i < n; ++i) { + int d, c, v; cin >> d >> c >> v; + ve[d-1] -= v; + ve[c-1] += v; + } + bool f = 1; + for (int i = 0; i < b; ++i) + if (ve[i] < 0) f = false; + if (f) cout << "S" << el; + else cout << "N" << el; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11679/11679-21.cpp b/uva_cpp_clean/11679/11679-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..903df83f9c03db869308a4d80b2b9450eb569cfe --- /dev/null +++ b/uva_cpp_clean/11679/11679-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11679 + Name: Sub-prime + Problem: https://onlinejudge.org/external/116/11679.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int R[23], n, m; + while (cin >> n >> m && (n||m)) { + for (int i=1; i<=n; ++i) + cin >> R[i]; + + for (int i=0, u,v,d; i> u >> v >> d; + R[u] -= d; + R[v] += d; + } + + bool possible = 1; + for (int i=1; possible && i<=n; ++i) + possible &= R[i] >= 0; + + cout << (possible ? "S\n" : "N\n"); + } +} diff --git a/uva_cpp_clean/11679/11679-6.cc b/uva_cpp_clean/11679/11679-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..e46b431a8a9129c8c7b6603bf6de19d37e9a1c18 --- /dev/null +++ b/uva_cpp_clean/11679/11679-6.cc @@ -0,0 +1,28 @@ +// https://uva.onlinejudge.org/external/116/11679.pdf +#include +#include + +using namespace std; +typedef long long ll; +typedef vector vl; + +int main() { + while (true) { + int b, n; + cin >> b >> n; + if (!n && !b) break; + vl r(b); + for (int i = 0; i < b; i++) cin >> r[i]; + for (int i = 0; i < n; i++) { + int d, c, v; + cin >> d >> c >> v; + d--; + c--; + r[d] -= v; + r[c] += v; + } + bool ok = true; + for (int i = 0; i < b; i++) if (r[i] < 0) ok = false; + cout << (ok ? "S" : "N") << endl; + } +} diff --git a/uva_cpp_clean/11679/11679-9.cpp b/uva_cpp_clean/11679/11679-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e864d92bcd08522745076f3969d2d4d696e1c0a4 --- /dev/null +++ b/uva_cpp_clean/11679/11679-9.cpp @@ -0,0 +1,19 @@ +#include + +int main(){ + int ban[20], N, B, c, b, a, i, ind; + while(scanf("%d%d", &B, &N),B || N){ + ind = 1; + for(i = 0; i < B; i++) scanf("%d", &ban[i]); + for(i = 0; i < N; i++){ + scanf("%d %d %d", &a, &b, &c); + ban[b - 1] +=c; + ban[a - 1] -=c; + } + for(i = 0; i < B; i++) + if(ban[i] < 0) ind = 0; + if(ind) printf("S\n"); + else printf("N\n"); + } + return(0); +} diff --git a/uva_cpp_clean/11679/main.cpp b/uva_cpp_clean/11679/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad81597b4899f21cdfeb0e77a85e2c687f897df0 --- /dev/null +++ b/uva_cpp_clean/11679/main.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +int main(){ + int n_banks, deventures, operation , operB , number; + vector banks; + while(cin >> n_banks >> deventures && n_banks != 0 && deventures != 0){ + bool res = true; + banks.clear(); + for(int i = 0; i < n_banks; i++){ + cin >> operation; + banks.push_back(operation); + } + for(int i = 0; i < deventures; i++){ + cin >> operation >> operB >> number; + banks[operation-1] -= number; + banks[operB-1] += number; + // cout << operation << ' ' << banks[operation-1] << ' ' << operB << ' ' << banks[operB-1] << endl; + } + for(int i = 0; i < n_banks; i++){ + if(banks[i] < 0) res = false; + } + if(res) cout << "S" << endl; + else cout << "N" << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/11683/11683-2.cpp b/uva_cpp_clean/11683/11683-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b87624b966fff81e2014793a2c09e6a0b78458e --- /dev/null +++ b/uva_cpp_clean/11683/11683-2.cpp @@ -0,0 +1,56 @@ +#include "bits/stdc++.h" + + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + + int height, length; + while(cin >> height && height) { + cin >> length; + int lastHeight = height; + int currentHeight; + int cuts = 0; + while(length--) { + cin >> currentHeight; + if (lastHeight > currentHeight) + cuts += lastHeight - currentHeight; + lastHeight = currentHeight; + } + cout << cuts << '\n'; + } + + return 0; +} + diff --git a/uva_cpp_clean/11683/11683-5.cpp b/uva_cpp_clean/11683/11683-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0cd66b2be92a80491213d6b859d8b7a265c6d79e --- /dev/null +++ b/uva_cpp_clean/11683/11683-5.cpp @@ -0,0 +1,133 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include + +using namespace std; + +#define ll long long +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define sff(t) scanf("%f",&t) +#define sdb(t) scanf("%lf",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define ssff(a,b) scanf("%f%f",&a,&b) +#define ssdb(a,b) scanf("%lf%lf",&a,&b) + +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PFa) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a + +#define CP(a) cout< " << (x) << endl; + +#define ff first +#define se second +#define pb push_back +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define parity(i) __builtin_parity(i) //evenparity 0 and odd parity 1 +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +////============ CONSTANT ===============//// +#define MAXLL 9223372036854775807 +#define MINLL 9223372036854775808 +#define MAXL 2147483647 +#define MINL 2147483648 +#define max7 10000007 +#define max6 1000006 +#define mx (1000010) +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mod 10007 +////=====================================//// + +template string NumberToString( T Number ){stringstream st;st << Number;return st.str();} + +int stringconvert(string& s){int p; istringstream st(s); st>>p ; return p;} +int SOD(int n){int sum=0;for(int i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +int main() +{ + int a,n,p; + while(sii(a)) + { + if(a==0) break; + ssii(n,p); + int x,res=0; + for(int i=1;i +using namespace std; + +int main(){ + int hight, weight, n, next_n, res; + while(cin >> hight >> weight && hight != 0){ + cin >> n; + res = hight - n; + for(;weight > 1; weight--){ + cin >> next_n; + if(n > next_n) res += n - next_n; + n = next_n; + } + cout << res << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/11686/11686-10.cpp b/uva_cpp_clean/11686/11686-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f5d674de5861245157f408ae1ee8473b689e2dd --- /dev/null +++ b/uva_cpp_clean/11686/11686-10.cpp @@ -0,0 +1,83 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +using namespace std; +typedef long long ll; +void fast() { + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); +} +const int N=(int)1e6+5; + +int n,m; +vector adj[N]; +vector adjj[N]; +int vis[N]; +vector ans; + +bool mychild(int parent, int cch) { + for(int ccch:adj[parent]) { + if(ccch==cch)return true; + } + return false; +} + + +bool canmove(int ch) { + for(int cch:adjj[ch]) { + if(!vis[cch] && !mychild(ch,cch)) return false; + } + return true; +} +void dfs(int node) { + vis[node]=1; + ans.push_back(node); + + for(auto ch:adj[node]) { + if(!vis[ch] && canmove(ch)) { + + dfs(ch); + } + } +} + +int main() { + fast(); + + int a,b; + while (cin>>n>>m){ + if(n==0&&m==0)break; + + memset(adj,0,sizeof adj); + memset(adjj,0,sizeof adjj); + memset(vis,0,sizeof vis); + ans.clear(); + + vector roots(n+5,1); + for(int i=0;i>a>>b; + adj[a].push_back(b); + adjj[a].push_back(b); + adjj[b].push_back(a); + roots[b]=0; + } + + for(int i=1;i<=n;i++) { + if(roots[i] && !vis[i]) { + dfs(i); + } + } + + if(ans.size()==n) { + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 1000000 +using namespace std; +typedef long long ll; +vectoradj[MAX]; +vectorV; +int color[MAX], c; +void dfs(ll u) { + if (c == 1) return; + color[u] = 1; + ll sz = adj[u].size(); + for (ll i = 0; i < sz; i++) { + ll v = adj[u][i]; + if (color[v] == 1) { + c = 1; + return; + } + if (color[v] == 0) { + dfs(v); + } + } + color[u] = 2; + V.push_back(u); + return ; +} +void mem() { + V.clear(); + for (ll i = 0; i < MAX; i++) { + adj[i].clear(); + color[i] = 0; + } +} +int main () { + ll n, m; + while (scanf("%lld %lld", &n, &m) == 2 && n != 0 && m != 0) { + for (ll i = 1; i <= m; i++) { + ll u, v; + scanf("%lld %lld", &u, &v); + adj[u].push_back(v); + } + c = 0; + for (int i = 1; i <= n; i++) { + if (color[i] == 0) { + dfs(i); + } + } + if (c == 1) { + printf("IMPOSSIBLE\n"); + } else { + for (int i = n - 1; i >= 0; i--) { + printf("%lld\n", V[i]); + } + } + mem(); + } + return 0; +} diff --git a/uva_cpp_clean/11686/11686-19.cpp b/uva_cpp_clean/11686/11686-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76a218ee4aa2eebf9f45280fde7b1255e95ac829 --- /dev/null +++ b/uva_cpp_clean/11686/11686-19.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +vector AdjList; +int visited[1000000]; +stack sol; +bool ok; + +void toposort(int j) { + visited[j] = 1; + for (int i = 0; i < AdjList[j].size() && ok; i++) { + if (visited[AdjList[j][i]] == 0) { + visited[AdjList[j][i]] = true; + toposort(AdjList[j][i]); + } else if (visited[AdjList[j][i]] == 1) { + ok = false; + } + } + visited[j] = 2; + sol.push(j); +} + +int main() { + int n, m; + while (cin >> n >> m && !(n == 0 && m == 0)) { + AdjList.assign(n, vi()); + while (m--) { + int a, b; + cin >> a >> b; + AdjList[a-1].push_back(b-1); + } + for (int i = 0; i < n; i++) visited[i] = 0; + ok = true; + for (int i = 0; i < n && ok; i++) { + if (visited[i] == 0) { + visited[i] = true; + toposort(i); + } + } + if (!ok) { + printf("IMPOSSIBLE\n"); + while (!sol.empty()) sol.pop(); + } + else { + while (!sol.empty()) { + printf("%d\n", sol.top() + 1); + sol.pop(); + } + } + } + return 0; +} diff --git a/uva_cpp_clean/11686/11686-21.cpp b/uva_cpp_clean/11686/11686-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e86f8169aa0a8539d76ca7a6288738c498dd6f4 --- /dev/null +++ b/uva_cpp_clean/11686/11686-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11686 + Name: Pick up sticks + Problem: https://onlinejudge.org/external/116/11686.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 1000001 +list adj[MAXN]; +bool seen[MAXN], inside[MAXN]; +int order[MAXN], ocnt; +bool topo(int u) { + seen[u] = + inside[u] = true; + + for (int v: adj[u]) + if (!seen[v]) { + + if (!topo(v)) return 0; + } + else if (inside[v]) + return 0; + + inside[u] = false; + order[--ocnt] = u; + return 1; +} + + +int main(){ + int n, m, u, v; + while (cin>>n>>m && (n||m)) { + for (int i=0; i>u>>v; + adj[u-1].push_back(v-1); + } + + ocnt=n; + bool f = true; + for (int i=0; f && i + +#define WHITE -1 +#define GRAY 0 +#define BLACK 1 + +using namespace std; + +const int MAX_V = 1000010; + +int E, V, a, b, vis[MAX_V]; +vector AdjList[MAX_V], ts; + +bool dfs(int u){ + vis[u] = GRAY; + bool ret = true; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( vis[v] == WHITE ) + ret &= dfs(v); + else if( vis[v] == GRAY ) + return false; + } + vis[u] = BLACK; + ts.push_back(u); + return ret; +} + +bool TopologicalSort(){ + bool ret = true; + fill(vis, vis + V + 1, WHITE); + for(int u = 1; u <= V; u++) + if( vis[u] == WHITE ) + ret &= dfs(u); + return ret; +} + +void read(){ + for(int i = 0; i < E; i++){ + scanf("%d %d", &a, &b); + AdjList[a].push_back(b); + } +} + +void solve(){ + bool ret = TopologicalSort(); + if( not ret ) puts("IMPOSSIBLE"); + else for(int i = ts.size() - 1; i >= 0; i--) printf("%d\n", ts[i]); +} + +void clear_(){ + ts.clear(); + for(int i = 1; i <= V; i++) AdjList[i].clear(); +} + +int main(){ + while(scanf("%d %d", &V, &E), E | V){ + read(); + solve(); + clear_(); + } + return(0); +} diff --git a/uva_cpp_clean/11687/11687-2.cpp b/uva_cpp_clean/11687/11687-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2205eb34bc6e11e0a4a1660f88886711e35a258d --- /dev/null +++ b/uva_cpp_clean/11687/11687-2.cpp @@ -0,0 +1,60 @@ + + +#include "bits/stdc++.h" +#include + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + + +int solve(int siz, int c) { + if (siz == 1) return c + 1; + int ne_sz = 0; + do { + siz/=10; + ++ne_sz; + } while (siz); + return solve(ne_sz, c+1); +} + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + string s; + while (cin >> s) { + if (s == "END") break; + int sz = (int) s.size(); + if (sz == 1 && s[0] == '1') cout << 1 << el; + else cout << solve(sz, 1) << el; + } + return 0; +} + diff --git a/uva_cpp_clean/11687/11687-20.cpp b/uva_cpp_clean/11687/11687-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39fd4ffd41918011548eefb22c9ca1a9f52ecce2 --- /dev/null +++ b/uva_cpp_clean/11687/11687-20.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + char num[1000002]; + + while( cin >> num && strcmp(num, "END") ) + { + if( strcmp(num, "1")==0 ) printf("1\n"); + else + { + int i = 0, pre = 0, len = strlen(num); + while( len!=pre ) + { + i++; + pre = len; + + int temp = 0; + while( len>0 ) + { + len /= 10; + temp++; + } + len = temp; + } + cout << i+1 << endl; + } + } + return 0; +} diff --git a/uva_cpp_clean/11687/11687-21.cpp b/uva_cpp_clean/11687/11687-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9938b893ec7aae869f46ee3b83248aeb8ee803b4 --- /dev/null +++ b/uva_cpp_clean/11687/11687-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11687 + Name: Digits + Problem: https://onlinejudge.org/external/116/11687.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int digits(int x) { + int c = 1; + while (x > 9) + x /= 10, + c++; + return c; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string inp; + while (cin >> inp && inp!="END") { + if (inp == "1") cout << "1\n"; + else { + int i = 2, x = inp.length(); + while (x != 1) + x = digits(x), + i++; + cout << i << endl; + } + } +} diff --git a/uva_cpp_clean/11687/main.cpp b/uva_cpp_clean/11687/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2c69baca5b444d902aca48d024abc99f1b8f959 --- /dev/null +++ b/uva_cpp_clean/11687/main.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main(){ + string number; + while(getline(cin, number) && number != "END"){ + int i = 0; + while(number != "1"){ + number = to_string(number.size()); + i++; + } + cout << i+1 << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/11689/11689-21.cpp b/uva_cpp_clean/11689/11689-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3f87a174cc4bec9601052166241b5bc6a909840 --- /dev/null +++ b/uva_cpp_clean/11689/11689-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11689 + Name: Soda Surpler + Problem: https://onlinejudge.org/external/116/11689.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, e, f, c; + cin >> T; + while(T--) { + cin >> e >> f >> c; + e += f; + f = 0; + + while (e >= c) { + f += e/c; + e = e/c + e%c; + } + + cout << f << endl; + } +} diff --git a/uva_cpp_clean/11689/11689-5.cpp b/uva_cpp_clean/11689/11689-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d09355c7c2428f9d636603e87d8efe61f3a6383a --- /dev/null +++ b/uva_cpp_clean/11689/11689-5.cpp @@ -0,0 +1,22 @@ +#include +int main() +{ + int n,e,f,c,a,b,d,i; + while (scanf("%d",&n)==1) + { + for (i=0;i=c) + { + a=d/c; + b=b+a; + d=a+(d%c); + } + printf("%d\n",b); + } + } + return 0; +} diff --git a/uva_cpp_clean/11689/11689-9.cpp b/uva_cpp_clean/11689/11689-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..237ca74450d9c30263d62e7e7706ed79c7a11041 --- /dev/null +++ b/uva_cpp_clean/11689/11689-9.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int main(){ + int c, x, y, z, ans; + scanf("%d", &c); + while(c--){ + scanf("%d %d %d", &x, &y, &z); + x += y; + ans = 0; + while(x >= z) + ans += x / z, x = x / z + x % z; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11690/11690-19.cpp b/uva_cpp_clean/11690/11690-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbd8a8f0443510083c4d283f1986342f9937bab3 --- /dev/null +++ b/uva_cpp_clean/11690/11690-19.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + void clear(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +ufds ds; + +int main() { + int N; + cin >> N; + int o[10000]; + unordered_map map; + while (N--) { + int n, m; + cin >> n >> m; + for (int i = 0; i < n; i++) cin >> o[i]; + int x, y; + ds.clear(n); + for (int i = 0; i < m; i++) { + cin >> x >> y; + ds.unionSet(x, y); + } + map.clear(); + for (int i = 0; i < n; i++) map[ds.findSet(i)] += o[i]; + bool ok = true; + for (auto it = map.begin(); it != map.end() && ok; ++it) { + ok = ok && (it->second == 0); + } + if (ok) printf("POSSIBLE\n"); + else printf("IMPOSSIBLE\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11690/11690-21.cpp b/uva_cpp_clean/11690/11690-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e957d88f32e9fdc89afbe1e441300c5a76750dc3 --- /dev/null +++ b/uva_cpp_clean/11690/11690-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11690 + Name: Money Matters + Problem: https://onlinejudge.org/external/116/11690.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int gid[100001], owes[100001]; +int find(int u) { + return u==gid[u] ? u : gid[u]=find(gid[u]); +} + +void join(int u, int v) { + u=find(u); v=find(v); + if (u != v) { + owes[v] += owes[u]; + gid[u] = v; + } +} + + +int main(){ + int T, n, m, u, v; + scanf("%d", &T); + while (T--) { + scanf("%d%d", &n, &m); + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int par[mx5],sum[mx5],a[mx5]; + +int Find(int n) +{ + if(par[n]==n) + return n; + return par[n]=Find(par[n]); +} + +int Union(int a,int b) +{ + int u= Find(a); + int v= Find(b); + if(u!=v) + { + par[u]=v; + } + +} + +int main() +{ + int t; + si(t); + while(t--) + { + int n,m; + sii(n,m); + + cover(sum,0); + + for0(i,n) + { + si(a[i]),par[i]=i; + } + + int x,y; + while(m--) + { + sii(x,y); + Union(x,y); + } + + for0(i,n) + { + sum[ Find(i) ] += a[i]; + } + + bool f=true; + for0(i,n) + { + if(sum[i] !=0) + { + f=false; + break; + } + } + + if(f) + puts("POSSIBLE"); + else + puts("IMPOSSIBLE"); + + + } + + return 0; +} diff --git a/uva_cpp_clean/11690/11690-6.cc b/uva_cpp_clean/11690/11690-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..260d8ff4c264ce20bc401ca4ae707baa7c88b04a --- /dev/null +++ b/uva_cpp_clean/11690/11690-6.cc @@ -0,0 +1,40 @@ +// https://uva.onlinejudge.org/external/116/11690.pdf +#include +using namespace std; +using ll=long long; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + ll t,n,m,u,v; + cin>>t; + while(t--){ + cin>>n>>m;; + vi a(n); + vvi g(n); + for(ll i=0;i>a[i]; + for(ll i=0;i>u>>v; + g[u].push_back(v); + g[v].push_back(u); + } + vi s(n); + functiondfs=[&](ll u,ll k){ + s[u]=k; + for(ll v:g[u]) + if(!s[v]) + dfs(v,k); + }; + ll k=1; + for(ll i=0;i + +using namespace std; + +struct UnionFind{ + int numSets; + vector path, rank, setSize, w; + + UnionFind(int N_): numSets(N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + setSize.assign(N_, 1); + w.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + numSets--; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x, w[x] += w[y], w[y] = 0; + else path[x] = y, w[y] += w[x], w[x] = 0; + if( rank[x] == rank[y] ) rank[y]++; + } + + int sizeOfSet(int idx){ + return setSize[ findSet(idx) ]; + } +}; + +int tc, n, m, a, b; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &n, &m); + UnionFind UF(n); + for(int it = 0; it < n; it++){ + scanf("%d", &a); + UF.w[it] = a; + } + while(m--){ + scanf("%d %d", &a, &b); + UF.unionSet(a, b); + } + puts( all_of(UF.w.begin(), UF.w.end(), [](int it){ return ( it == 0 ); } ) ? "POSSIBLE" : "IMPOSSIBLE" ); + } + return(0); +} diff --git a/uva_cpp_clean/11692/11692-9.cpp b/uva_cpp_clean/11692/11692-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..279c308658afc09cbf1aa0544e769a221a855490 --- /dev/null +++ b/uva_cpp_clean/11692/11692-9.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int tc; +double L, K, T1, T2, H, A, B, C, D, r, ans1, ans2; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%lf %lf %lf %lf %lf", &L, &K, &T1, &T2, &H); + if( H < L ){ + printf("%.6lf %.6lf\n", H, H); + continue; + } + A = T1, B = - K * ( T1 + T2 ) - H, C = K * L; + D = sqrt( B * B - 4 * A * C ); + r = 2.0 * C / ( -B - D ); + ans2 = max(H, T1 * r); + ans1 = ( H == L ) ? H : ans2; + printf("%.6lf %.6lf\n", ans1, ans2); + } + return(0); +} diff --git a/uva_cpp_clean/11695/11695-21.cpp b/uva_cpp_clean/11695/11695-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2b03fcc82fab78d8452873e5b57bb6114d65168 --- /dev/null +++ b/uva_cpp_clean/11695/11695-21.cpp @@ -0,0 +1,138 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11695 + Name: Flight Planning + Problem: https://onlinejudge.org/external/116/11695.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +void remove(vector &v, int x) { + int sz = 0; + for (int i=0; i adj[2543]; + +bool inside[2543]; +Edge dfsDia(int u) { + inside[u] = true; + Edge r(u, 0); + for (int v: adj[u]) + if (!inside[v]) { + Edge rr = dfsDia(v); + if (++rr.d > r.d) + r = rr; + } + inside[u] = false; + return r; +} + + +int P[2543], D[2543]; +int dfsPath(int u, int dst, int d=0) { + P[d] = u; + if (u == dst) return d; + inside[u] = true; + for (int v: adj[u]) + if (!inside[v]) { + int r = dfsPath(v, dst, d+1); + if (r != -1) { + inside[u] = false; + return r; + } + } + + inside[u] = false; + return -1; +} + +int findDiameter(int t) { + int u = dfsDia(t).u, + v = dfsDia(u).u; + + //if (u > v) swap(u, v); + return dfsPath(u, v) + 1; +} + + +struct Result { + int i1, j1, i2, j2, l; + Result(int i1, int j1, int i2, int j2, int l):i1(i1),j1(j1),i2(i2),j2(j2),l(l){} + bool operator < (const Result& o) const { return l < o.l; } +}; + +Result calc(int root) { + int i1 = D[root+0], + j1 = D[root+1]; + + remove(adj[i1], j1); + remove(adj[j1], i1); + + int i2 = P[findDiameter(i1)>>1], + j2 = P[findDiameter(j1)>>1]; + + adj[i2].push_back(j2); + adj[j2].push_back(i2); + + int l = findDiameter(0)-1; + + adj[i2].pop_back(); + adj[j2].pop_back(); + adj[i1].push_back(j1); + adj[j1].push_back(i1); + + return Result(i1, j1, i2, j2, l); +} + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + while (T--) { + cin >> n; + for (int i=0; i> u >> v; + adj[u-1].push_back(v-1); + adj[v-1].push_back(u-1); + } + + int l = findDiameter(0); + memcpy(D, P, sizeof(D)); + Result r(0,0,0,0,10000); + for (int i=-2; i<2; ++i) { + int root = (l>>1)+i; + if (root>=0 && root+1>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 117 + Name: The Postal Worker Rings Once + Problem: https://onlinejudge.org/external/1/117.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +#define INF 1073741824 + +struct VD { + int id, d; + VD (int id, int d): id(id), d(d) {} + bool operator < (const VD& o) const { + return d > o.d; + } +}; + + +list adj[26]; +int dist[26][26]; +bool seen[26]; + +int main(){ + int u, v; + char word[100]; + while (cin >> word) { + for (int i=0; i<26; i++) + adj[i].clear(); + + int sum = 0; + while (strcmp(word, "deadend")) { + int len = strlen(word); + sum += len; + + u = word[0]-'a'; + v = word[len-1]-'a'; + adj[u].push_back(v); + adj[v].push_back(u); + dist[u][v] = dist[v][u] = len; + + cin >> word; + } + + int s=-1, t=-1; + for (int i=0; i<26 && t==-1; i++) + if (adj[i].size()%2) + if (s==-1) s = i; + else t = i; + + if (t != -1) { + memset(seen, 0, sizeof(seen)); + priority_queue q; + q.push(VD(s, 0)); + + while (!q.empty()) { + VD u = q.top(); q.pop(); + if (u.id == t) { sum += u.d; break; } + if (seen[u.id]) continue; + seen[u.id] = true; + for (int v: adj[u.id]) + if (!seen[v]) + q.push(VD(v, u.d + dist[u.id][v])); + } + } + + cout << sum << endl; + } +} diff --git a/uva_cpp_clean/11701/11701-21.cpp b/uva_cpp_clean/11701/11701-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77a2a27fa3f3d5312a0c4bfcea35242aeee54731 --- /dev/null +++ b/uva_cpp_clean/11701/11701-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11701 + Name: Cantor + Problem: https://onlinejudge.org/external/117/11701.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define EPS 1e-12 +using namespace std; + + +bool isCantor(double x, double l=0, double r=1) { + double d = r - l; + if (d < EPS) return 1; + + double m = l + d/3; + if (x <= m) + return isCantor(x, l, m); + + m += d/3; + if (x >= m) + return isCantor(x, m, r); + + return 0; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + double x; + while (cin >> x) + cout << (isCantor(x) ? "MEMBER\n" : "NON-MEMBER\n"); +} diff --git a/uva_cpp_clean/11701/11701-9.cpp b/uva_cpp_clean/11701/11701-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8702fe8dbd40d64b7e7b6812f00b3cbfca40dd99 --- /dev/null +++ b/uva_cpp_clean/11701/11701-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +char s[50]; +double n, l, r, ll, rr; +bool ok; + +int main(){ + while(scanf("%s", s)){ + if(sscanf(s, "%lf", &n) == 0) break; + ok = true; + l = 0.0 , r = 1.0; + for(int i = 0; i < 80; i++){ + ll = (2.0 * l + r) / 3.0, rr = (l + 2.0 * r) / 3.0; + if(fabs(n - ll) < 1e-15) break; + if(fabs(n - rr) < 1e-15) break; + if(n < ll) r = ll; + else if(n > rr) l = rr; + else{ + ok = false; + break; + } + } + puts( ok ? "MEMBER" : "NON-MEMBER" ); + } + return(0); +} diff --git a/uva_cpp_clean/11703/11703-19.cpp b/uva_cpp_clean/11703/11703-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69ddaa68cf587e2e718240cb5ba7cdcd1a4e306d --- /dev/null +++ b/uva_cpp_clean/11703/11703-19.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include +using namespace std; + +unordered_map m; + +int hell(int i) { + if (i == 0) return 1; + if (m[i] == 0) { + double s = sin(i); + m[i] = (hell(floor(i-sqrt(i))) + hell(floor(log(i))) + hell(floor(i*s*s)))%1000000; + } + return m[i]; +} + +int main() { + int n; + while (cin >> n && n != -1) { + printf("%d\n", hell(n)); + } + return 0; +} diff --git a/uva_cpp_clean/11703/11703-21.cpp b/uva_cpp_clean/11703/11703-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2150dda418311e75c67fbeb502c2ec21c154af3c --- /dev/null +++ b/uva_cpp_clean/11703/11703-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11703 + Name: sqrt log sin + Problem: https://onlinejudge.org/external/117/11703.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +int DP[1000001]={1}; +int main() { + for (int i=1; i<1000001; i++) { + double s = sin(i); + DP[i] = (DP[int(i-sqrt(i))] + DP[int(log(i))] + DP[int(i*s*s)]) % 1000000; + } + + for (int n; scanf("%d", &n)==1 && n>=0; ) + printf("%d\n", DP[n]); +} diff --git a/uva_cpp_clean/11703/11703-9.cpp b/uva_cpp_clean/11703/11703-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00d233867b8a4e6c3c7408cf7ed57081f452419e --- /dev/null +++ b/uva_cpp_clean/11703/11703-9.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int n,memo[1000010]; +const int MOD = 1000000; + +int dp(int idx){ + if(!idx) return 1; + if(memo[idx]) return memo[idx]; + return memo[idx] = ( dp(floor(idx-sqrt(idx))) + dp(floor(log(idx))) + dp(floor(idx*sin(idx)*sin(idx))) ) % MOD; +} + +int main(){ + while(scanf("%d",&n),n!=-1) printf("%d\n",dp(n)); + return(0); +} diff --git a/uva_cpp_clean/11709/11709-21.cpp b/uva_cpp_clean/11709/11709-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13fb07158a9f9a2144d5569f490d6cc45fc7788e --- /dev/null +++ b/uva_cpp_clean/11709/11709-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11709 + Name: Trust groups + Problem: https://onlinejudge.org/external/117/11709.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +#define MAXN 1001 +map ids; +list adj[MAXN]; + + +int pre[MAXN], low[MAXN], pcnt, result; +bool onstack[MAXN]; +stack S; + +void scc(int u) { + low[u]=pre[u]=pcnt++; + onstack[u] = 1; + S.push(u); + + for (int v: adj[u]) + if (pre[v] < 0) { + scc(v); + low[u] = min(low[u], low[v]); + } + else if (onstack[v]) + low[u] = min(low[u], pre[v]); + + if (low[u] == pre[u]) { + result++; + onstack[u] = 0; + while (S.top() != u) { + onstack[S.top()] = 0; + S.pop(); + } + S.pop(); + } +} + + + +int main(){ + int n, m; + char w1[50], w2[50]; + while (cin>>n>>m && (n||m)) { + while (getchar()!='\n'); + for (int i=0; i +using namespace std; +using vi=vector; +using vvi=vector; +using msi=unordered_map; +int main(){ + for(;;){ + int n,m,u,v; + string r; + cin>>n>>m; + if(!n)break; + getline(cin,r); + msi x; + int k=0; + for(int i=0;i dfs1=[&](int i){ + s[i]=1; + for(int j:g[i]) + if(!s[j])dfs1(j); + a.push_back(i); + }; + for(int i=0;i dfs2=[&](int i){ + t[i]=1; + for(int j:h[i]) + if(!t[j])dfs2(j); + }; + k=0; + for(int i=0;i + +using namespace std; + +const int MAX_V = 1010, LEN = 50; + +int V, E, a, b, dfsCounter, numSCC, dfs_num[MAX_V], dfs_low[MAX_V]; +bool vis[MAX_V]; +char line[LEN]; +vector AdjList[MAX_V], path; +map mp; + +void dfs(int u){ + dfs_low[u] = dfs_num[u] = ++dfsCounter; + vis[u] = true; + path.push_back(u); + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not dfs_num[v] ) dfs(v); + if( vis[v] ) dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + if( dfs_low[u] == dfs_num[u] ){ + numSCC++; + while(true){ + int v = path.back(); path.pop_back(); + vis[v] = false; + if( u == v ) break; + } + } +} + +void TarjanSCC(){ + fill(dfs_num, dfs_num + V, 0); + fill(vis, vis + V, false); + dfsCounter = numSCC = 0; + for(int u = 0; u < V; u++) + if( not dfs_num[u] ) dfs(u); +} + +void solve(){ + TarjanSCC(); + printf("%d\n", numSCC); +} + +void read(){ + for(int i = 0; i < V; i++){ + cin.getline(line, LEN); + mp[line] = i; + } + for(int i = 0; i < E; i++){ + cin.getline(line, LEN); + a = mp[line]; + cin.getline(line, LEN); + b = mp[line]; + AdjList[a].push_back(b); + } +} + +void clear(){ + mp.clear(); + for(int u = 0; u < V; u++) AdjList[u].clear(); +} + +int main(){ + while(scanf("%d %d\n", &V, &E), V | E){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11710/11710-13.cpp b/uva_cpp_clean/11710/11710-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af8c9c4b31b1f9a7a7cea325de10e7c82b45d544 --- /dev/null +++ b/uva_cpp_clean/11710/11710-13.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 500 + +int n, m, counter = 0; + +bool visited[MAX]; +map names; +int parent[MAX], sz[MAX]; +vector > > edges; + +int find(int x) +{ + if (x == parent[x]) return x; + + else return parent[x] = find(parent[x]); +} + +bool merge(int x, int y) +{ + int px = find(x); + int py = find(y); + + if (px == py) return false; + + else + { + if (sz[px] < sz[py]) swap(px, py); + + sz[px] += sz[py]; + + parent[py] = px; + + return true; + } +} + +int kruskal() +{ + int ans = 0; + int how = 0; + + for (int i = 0; i < edges.size() && how < n; i++) + { + int w = edges[i].first; + + if (merge(edges[i].second.first, edges[i].second.second)) + { + visited[edges[i].second.first] = true; + visited[edges[i].second.second] = true; + + ans += w; + + how++; + } + } + + return how == n - 1 ? ans : -1; +} + +void initilize() +{ + counter = 0; + + names.clear(); + + edges.clear(); + + for (int i = 0; i < MAX; i++) + { + sz[i] = 1; + + visited[i] = false; + + parent[i] = i; + } +} + +int main() +{ + while (cin >> n >> m , n != 0 || m != 0) + { + initilize(); + + for (int i = 0; i < n; i++) + { + string station; + + cin >> station; + + names[station] = counter++; + } + + for (int i = 0; i < m; i++) + { + int w; + + string v, u; + + cin >> v >> u >> w; + + edges.push_back({ w, { names[v], names[u] } }); + } + + string nop; + + cin >> nop; + + sort(edges.begin(), edges.end()); + + int ans = kruskal(); + + if (ans == -1) cout << "Impossible" << endl; + + else cout << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11710/11710-21.cpp b/uva_cpp_clean/11710/11710-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cab111a6f1afc6630bfedb87c1c8a8696f745243 --- /dev/null +++ b/uva_cpp_clean/11710/11710-21.cpp @@ -0,0 +1,112 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11710 + Name: Expensive subway + Problem: https://onlinejudge.org/external/117/11710.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +bool readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return 0; + + *str++ = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + *str++ = ch; + *str = 0; + return 1; +} + +// ------------------------------------------------------------ + + +#define MAXN 417 +int par[MAXN]; +int find(int u) { return par[u]<0 ? u : par[u]=find(par[u]); } +void join(int u, int v) { + if ((u=find(u)) == (v=find(v))) return; + if (par[v] < par[u]) + swap(u, v); + par[u] += par[v]; + par[v] = u; +} + + +struct Edge { + int u, v, w; + bool operator < (const Edge &e) const { return w < e.w; } +} q[79801]; + +int main(){ + int n, m, w, c; + char s1[100], s2[100]; + while ((n=readUInt())) { + int m = readUInt(); + unordered_map ids; + for (int i=0; i minimum spanning tree (MST) + difficulty: easy + date: 03/Jun/2020 + by: @brpapa +*/ +#include +using namespace std; +struct UFDS { + vector p; // p[i] = pai do item i + + UFDS(int N) { + p.resize(N); + for (int i = 0; i < N; i++) p[i] = i; + } + + int findSet(int i) { + if (p[i] == i) return i; + return p[i] = findSet(p[i]); + } + + bool isSameSet(int i, int j) { + return findSet(i) == findSet(j); + } + + void unionSet(int i, int j) { + int setI = findSet(i); + int setJ = findSet(j); + if (!isSameSet(setI, setJ)) p[setI] = setJ; + } +}; + +map station2id; + +struct Tedge { + int u, v, w; + Tedge() {} + Tedge(int u, int v, int w) : u(u), v(v), w(w) {} + bool operator<(const Tedge& p) const { return w < p.w; } +}; + +int main() { + int V, E; + while (cin >> V >> E && (V || E)) { + for (int v = 0; v < V; v++) { + string station; cin >> station; station2id[station] = v; + } + + vector edgeList(E); + for (Tedge &e : edgeList) { + string u, v; cin >> u >> v; int w; cin >> w; + e = Tedge(station2id[u], station2id[v], w); + } + string station; cin >> station; + int s = station2id[station]; + + UFDS uf(V); + sort(edgeList.begin(), edgeList.end()); + + int ans = 0; + for (Tedge e: edgeList) + if (!uf.isSameSet(e.u, e.v)) { + uf.unionSet(e.u, e.v); + ans += e.w; + } + + bool allConnected = true; + for (int v = 0; v < V; v++) if (!uf.isSameSet(s, v)) allConnected = false; + + if (!allConnected) cout << "Impossible" << endl; + else cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11710/11710-9.cpp b/uva_cpp_clean/11710/11710-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a13638d21f3c038239e6c74b342848e10d74286c --- /dev/null +++ b/uva_cpp_clean/11710/11710-9.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +const int LIM = 15; +int s, c, ct, w, cost; +char word1[LIM], word2[LIM]; +vector < pair > EdgeList; +map < string, int > mp; + +int Kruskal(){ + if( s == 1 ) return 0; + UnionFind UF(s); + sort(EdgeList.begin(), EdgeList.end()); + int min_cost = 0, setSize = 1; + for(int it = 0; it < c; it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + min_cost += front.first, + UF.unionSet(front.second.first, front.second.second), setSize++; + if( setSize == s ) return min_cost; + } + return -1; +} + +int main(){ + while(scanf("%d %d\n", &s, &c), s | c){ + EdgeList.clear(); + mp.clear(); + ct = 0; + for(int it = 0; it < s; it++){ + scanf("%s", word1); + mp[word1] = ct++; + } + for(int it = 0; it < c; it++){ + scanf("%s %s %d\n", word1, word2, &w); + EdgeList.push_back( make_pair(w, ii(mp[word1], mp[word2]) )); + } + scanf("%s", word1); + cost = Kruskal(); + ( cost == -1 ) ? puts("Impossible") : printf("%d\n", cost); + } + return(0); +} diff --git a/uva_cpp_clean/11711/11711-4.cpp b/uva_cpp_clean/11711/11711-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..671c81b017674e0164b314e145b643c62d9c5d0f --- /dev/null +++ b/uva_cpp_clean/11711/11711-4.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include + +enum class status +{ + AC, + WA, + TLE, + MLE +}; + +class turing_machine +{ +private: + class rule + { + public: + int state; + int letter; + int nextState; + int nextLetter; + int head_move; + + rule(int _state = 0, int _letter = 0, int _nextState = 0, int _nextLetter = 0, int _move = 0) + { + state = _state; + letter = _letter; + nextState = _nextState; + nextLetter = _nextLetter; + head_move = _move; + } + + ~rule() {} + }; + + int *tape; + int tapeSize, head; + std::vector rules; + + rule *find_rule(int state, int letter) + { + for (auto &rule : rules) + { + if (rule.state == state && rule.letter == letter) + { + return &rule; + } + } + + return NULL; + } + + bool validate_output(int expected_output) + { + int sum = 0; + + for (int i = 0; i < tapeSize; i++) + { + if (tape[i] == 0) + { + break; + } + + sum += tape[i]; + } + + return sum == expected_output; + } + +public: + turing_machine(int _tapeSize) + { + tape = new int[_tapeSize]; + tapeSize = _tapeSize; + head = 0; + } + + ~turing_machine() + { + delete[] tape; + } + + void add_rule(int state, int letter, int nextState, int nextLetter, char head_move) + { + rules.push_back(rule(state, letter, nextState, nextLetter, head_move == 'R' ? 1 : -1)); + } + + status run(int input, int output) + { + head = 0; + memset(tape, 0, tapeSize * sizeof(int)); + + for (int i = 0; i < input; i++) + { + tape[i] = 1; + } + + rule *current = find_rule(0, tape[head]); + + for (int i = 0; i < 10000; i++) + { + if (current == NULL) + { + return validate_output(output) ? status::AC : status::WA; + } + + tape[head] = current->nextLetter; + head += current->head_move; + + if (head < 0 || head >= tapeSize) + { + return status::MLE; + } + + current = find_rule(current->nextState, tape[head]); + } + + return status::TLE; + } +}; + +int main() +{ + int rules, tests; + + while (std::cin >> rules >> tests && rules != 0 && tests != 0) + { + turing_machine machine(1000); + + for (int i = 0; i < rules; i++) + { + char head_move; + int state, letter, nextState, nextLetter; + std::cin >> state >> letter >> nextState >> nextLetter >> head_move; + + machine.add_rule(state, letter, nextState, nextLetter, head_move); + } + + for (int i = 0; i < tests; i++) + { + int input, output; + std::cin >> input >> output; + + switch (machine.run(input, output)) + { + case status::AC: + std::cout << "AC" << std::endl; + break; + case status::WA: + std::cout << "WA" << std::endl; + break; + case status::TLE: + std::cout << "TLE" << std::endl; + break; + case status::MLE: + std::cout << "MLE" << std::endl; + break; + default: + break; + } + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11713/11713-14.cpp b/uva_cpp_clean/11713/11713-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4401bd001b67d54db245066decad9bcb231ebb0f --- /dev/null +++ b/uva_cpp_clean/11713/11713-14.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +using namespace std; +int isVowel(char ch) { + if(ch == 'a' ||ch == 'u' ||ch == 'e' ||ch == 'i' ||ch == 'o') { + return 1; + } + else { + return 0; + } +} +int main() +{ + string a,b; + int tc; + cin>>tc; + for(int i = 1; i<=tc; i++) { + cin>>a>>b; + bool flag = false; + int al = a.size(); + int bl = b.size(); + if(al == bl) { + for(int i = 0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector
vdd; +typedef vector vll; +typedef vector vvi; + +bool isvoc(char c) { + return c == 'a' || c == 'e' || c == 'i' || c == 'o'|| c == 'u'; +} + +int main() { + string str1, str2; + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + cin >> str1 >> str2; + bool ok = true; + if (str1.length() != str2.length()) ok = false; + str1.erase(remove_if(str1.begin(), str1.end(), isvoc), str1.end()); + str2.erase(remove_if(str2.begin(), str2.end(), isvoc), str2.end()); + if(str1 != str2) ok = false; + if(ok) cout << "Yes" << endl; + else cout << "No" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11713/11713-20.cpp b/uva_cpp_clean/11713/11713-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f99092e69710c5693a26afc0eef11876504d5a28 --- /dev/null +++ b/uva_cpp_clean/11713/11713-20.cpp @@ -0,0 +1,36 @@ +#include +#include + +char name_1[25], name_2[25]; + +int main() +{ + int tc; + char dump; + + scanf("%d%c", &tc, &dump); + while( tc-- ) + { + gets(name_1); + gets(name_2); + + if( strlen(name_1)==strlen(name_2) ) + { + bool flag; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11713 + Name: Abstract Names + Problem: https://onlinejudge.org/external/117/11713.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +bool isvowel(char ch) { + return ch=='a' || ch=='e' || ch=='o' || ch=='u' || ch=='i'; +} + +bool check(char *w1, char *w2) { + int i; + for (i=0; w1[i]; i++) + if (!w2[i]) return 0; + else { + bool v1 = isvowel(w1[i]), + v2 = isvowel(w2[i]); + + if ((v1^v2) || (!v1 && w1[i]!=w2[i])) + return false; + } + + return !w2[i]; +} + +int main(){ + int n; + cin>>n; + while(n--) { + char w1[30], w2[30]; + cin>>w1>>w2; + puts(check(w1, w2) ? "Yes" : "No"); + } +} diff --git a/uva_cpp_clean/11713/11713-5.cpp b/uva_cpp_clean/11713/11713-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9e8e49cbd762116cb5556d979cb816b62be01044 --- /dev/null +++ b/uva_cpp_clean/11713/11713-5.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +int main() +{ + int t; + char a[50],b[50],c[50],d[50]; + sc("%d",&t); + while(t--) + { + sc("%s",a); + sc("%s",b); + int l1=strlen(a); + int l2=strlen(b); + if(l1!=l2) + { + pf("No\n"); + continue; + } + int j=0; + int k=0; + for(int i=0;i + +using namespace std; + +const int LEN = 30; + +int tc; +char s1[LEN], s2[LEN]; + +inline bool isvowel(char ch){ + return ( ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' ); +} + +bool check(){ + if( strlen(s1) != strlen(s2) ) return false; + for(int it = 0; s1[it]; it++){ + int ct = isvowel(s1[it]) + isvowel(s2[it]); + if( ct == 1 ) return false; + if( ct == 0 and s1[it] != s2[it] ) return false; + } + return true; +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + scanf("%s", s1); + scanf("%s", s2); + puts( check() ? "Yes" : "No" ); + } + return(0); +} diff --git a/uva_cpp_clean/11714/11714-20.cpp b/uva_cpp_clean/11714/11714-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65821f6f671166127499e13f56588ca17e2ea537 --- /dev/null +++ b/uva_cpp_clean/11714/11714-20.cpp @@ -0,0 +1,14 @@ +#include +#include + +int n, comparison; + +int main() +{ + while( scanf("%d", &n)!=EOF ) + { + comparison = (n-1)+log2(n-1); + printf("%d\n", comparison); + } + return 0; +} diff --git a/uva_cpp_clean/11715/11715-19.cpp b/uva_cpp_clean/11715/11715-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..501704d935e55baf7d349e7db63423dd9fe6d6a1 --- /dev/null +++ b/uva_cpp_clean/11715/11715-19.cpp @@ -0,0 +1,34 @@ +#include +#include +using namespace std; + +int main() { + int t, cases = 1; + while (cin >> t && t != 0) { + cout << "Case "<< cases << ": "; + double v1, v2, v3; + cin >> v1 >> v2 >> v3; + if (t == 1) { + double a = (v2 - v1)/v3; + double s = v1*v3 + (a*v3*v3)/2; + printf("%.3f %.3f\n", s, a); + } + else if (t == 2) { + double ti = (v2-v1)/v3; + double s = v1*ti + (v3*ti*ti)/2; + printf("%.3f %.3f\n", s, ti); + } + else if (t == 3) { + double ti = (-v1 + sqrt(v1*v1 + 2*v3*v2))/v2; + double v = v1 + v2*ti; + printf("%.3f %.3f\n", v, ti); + } + else if (t == 4) { + double ti = (-v1 + sqrt(v1*v1 - 2*v3*v2))/(-v2); + double u = v1 - v2*ti; + printf("%.3f %.3f\n", u, ti); + } + cases++; + } + return 0; +} diff --git a/uva_cpp_clean/11715/11715-21.cpp b/uva_cpp_clean/11715/11715-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75ac06c244dfb2d413e01c0211bf4c4b0a36bd3c --- /dev/null +++ b/uva_cpp_clean/11715/11715-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11715 + Name: Car + Problem: https://onlinejudge.org/external/117/11715.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int tp, cse=1; + long double u, v, t, a, s; + while (cin>>tp && tp) { + printf("Case %d: ", cse++); + + if (tp==1) cin>>u>>v>>t; + else if (tp==2) cin>>u>>v>>a; + else if (tp==3) cin>>u>>a>>s; + else cin>>v>>a>>s; + + if (tp==1) a=(v-u)/t; + else if (tp==2) t=(v-u)/a; + if (tp<=2) { + s=0.5*a*t*t + u*t; + printf("%.3Lf %.3Lf\n", s, tp==1 ? a : t); + } + else { + if (tp == 3) t = (sqrt(u*u + 2*a*s) - u) / a; + else t = (-sqrt(v*v - 2*a*s) + v) / a; + printf("%.3Lf %.3Lf\n", tp==3 ? a*t+u : v-a*t, t); + } + } +} diff --git a/uva_cpp_clean/11715/11715-5.cpp b/uva_cpp_clean/11715/11715-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..96e2dc5af7bcc5f45b35af470e4a10a354f34cf2 --- /dev/null +++ b/uva_cpp_clean/11715/11715-5.cpp @@ -0,0 +1,40 @@ +#include +#include +int main() +{ +double u,v,s,t,a; +int te=1,n; +while(scanf("%d",&n)==1 && n!=0) +{ + if(n==1) + { + scanf("%lf%lf%lf",&u,&v,&t); + s=(u+v)*t/2; + a=(v-u)/t; + printf("Case %d: %.3lf %.3lf\n",te++,s,a); + } + else if(n==2) + { + scanf("%lf%lf%lf",&u,&v,&a); + t=(v-u)/a; + s=(u+v)*t/2; + printf("Case %d: %.3lf %.3lf\n",te++,s,t); + } + else if(n==3) + { + scanf("%lf%lf%lf",&u,&a,&s); + v=sqrt(u*u+2*a*s); + t=(v-u)/a; + printf("Case %d: %.3lf %.3lf\n",te++,v,t); + } + else if(n==4) + { + scanf("%lf%lf%lf",&v,&a,&s); + u=sqrt(v*v-2*a*s); + t=(v-u)/a; + printf("Case %d: %.3lf %.3lf\n",te++,u,t); + } + +} +return 0; +} diff --git a/uva_cpp_clean/11716/11716-19.cpp b/uva_cpp_clean/11716/11716-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee2e272809860064ecf487463093ad2682eca71f --- /dev/null +++ b/uva_cpp_clean/11716/11716-19.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + string s; + getline(cin, s); + while (n--) { + getline(cin, s); + int r = floor(sqrt(s.length())); + if (r*r == s.length()) { + for (int i = 0; i < r; i++) + for (int j = 0; j < r; j++) + printf("%c", s[j*r + i]); + printf("\n"); + } else printf("INVALID\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11716/11716-21.cpp b/uva_cpp_clean/11716/11716-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a02199170f03b4c6215fac6ef7df8298bc75d0b --- /dev/null +++ b/uva_cpp_clean/11716/11716-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11716 + Name: Digital Fortress + Problem: https://onlinejudge.org/external/117/11716.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXL 10010 +char line[10010]; + +int main(){ + int T, len; + cin>>T; cin.getline(line, MAXL); + while (T--) { + cin.getline(line, MAXL); + len = cin.gcount() - 1; + int sq = sqrt((double)len); + if (sq * sq != len) puts("INVALID"); + else { + for (int i=0; i + +using namespace std; + +const int LEN = 10010; + +int tc, m, n; +char line[LEN]; + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + cin.getline(line, LEN); + m = strlen(line); + n = sqrt(m); + if( n * n != m ){ + puts("INVALID"); + continue; + } + for(int col = 0; col < n; col++) + for(int row = 0; row < n; row++) + putchar( line[row * n + col] ); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/11718/11718-19.cpp b/uva_cpp_clean/11718/11718-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0942f17d9888d4cffd1e2f92c15536d3d0bf62a7 --- /dev/null +++ b/uva_cpp_clean/11718/11718-19.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +long long power(long long b, long long e, long long mod) { + if (e == 0) return 1; + if (e == 1) return b%mod; + long long aux = power(b, e/2, mod); + aux = (aux*aux)%mod; + if (e%2) aux = (aux*(b%mod))%mod; + return aux; +} + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 1; i <= cases; i++) { + long long n, k, mod, res = 0; + scanf("%lld %lld %lld", &n, &k, &mod); + for (int j = 0; j < n; j++) { + int val; + scanf("%d", &val); + res += val; + res %= mod; + } + res *= (k%mod) * power(n, k-1, mod) ; + res %= mod; + printf("Case %d: %lld\n", i, res); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11718/11718-6.cpp b/uva_cpp_clean/11718/11718-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e6f5931953cae5fa6e167ba1140666ecfae1360 --- /dev/null +++ b/uva_cpp_clean/11718/11718-6.cpp @@ -0,0 +1,32 @@ +/* + math > ad-hoc > finding pattern + difficulty: hard + date: 16/Feb/2020 + hint: compute K * N^(K-1) * sumA using fast power mod + by: @brpapa +*/ +#include +#define ll long long +using namespace std; + +ll fastPowerMod(ll a, ll n, int mod) { + if (n == 0) return 1; + if (n == 1) return a % mod; + if (n % 2 == 0) return fastPowerMod((a * a) % mod, n / 2, mod); + return ((a % mod) * fastPowerMod((a * a) % mod, (n - 1) / 2, mod)) % mod; +} + +int main() { + int T; cin >> T; + for (int t = 1; t <= T; t++) { + ll N, K, MOD; cin >> N >> K >> MOD; + + ll sumA = 0, a; + for (int n = 0; n < N; n++) cin >> a, sumA += a; + + ll ans = (K * fastPowerMod(N, K-1, MOD)) % MOD; + ans = (ans * sumA) % MOD; + printf("Case %d: %lld\n", t, ans); + } + return 0; +} diff --git a/uva_cpp_clean/1172/1172-19.cpp b/uva_cpp_clean/1172/1172-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51015a0bd3763b6f81388fa0ffa1826813d474dd --- /dev/null +++ b/uva_cpp_clean/1172/1172-19.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair si; +typedef unordered_map uiii; + +vector v1, v2; +unordered_map dp; + +ii proc(int c1, int c2) { + if (c1 >= v1.size() || c2 >= v2.size()) return ii(0, 0); + if (!dp[c1].count(c2)) { + ii sol = ii(0, 0); + if (v1[c1].first == v2[c2].first) { + ii prev = proc(c1+1, c2+1); + prev.first += v1[c1].second + v2[c2].second; + prev.second--; + sol = prev; + } + sol = max(sol, proc(c1+1, c2)); + sol = max(sol, proc(c1, c2+1)); + dp[c1][c2] = sol; + } + return dp[c1][c2]; +} + +int main() { + int T; + cin >> T; + while (T--) { + int N; + cin >> N; + v1.assign(N, si()); + dp.clear(); + for (int i = 0; i < N; i++) { + string name, os; + int val; + cin >> name >> os >> val; + v1[i] = si(os, val); + } + cin >> N; + v2.assign(N, si()); + for (int i = 0; i < N; i++) { + string name, os; + int val; + cin >> name >> os >> val; + v2[i] = si(os, val); + } + ii sol = proc(0, 0); + printf("%d %d\n", sol.first, -sol.second); + } + return 0; +} diff --git a/uva_cpp_clean/1172/1172-21.cpp b/uva_cpp_clean/1172/1172-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44d0f5d465bc809a0eb9102d0df31cd9374ee71d --- /dev/null +++ b/uva_cpp_clean/1172/1172-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1172 + Name: The Bridges of Kolsberg + Problem: https://onlinejudge.org/external/11/1172.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int DPs[1024][1024], DPc[1024][1024], + A[1024], B[1024], X[1024], Y[1024], n, m; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + string str, id; + while (T--) { + int icnt = 0; + map ids; + + for (int i=0; i<2; ++i) { + int *Z = i ? Y : X, + *C = i ? B : A, + &sz = i ? m : n; + cin >> sz; + for (int j=1; j<=sz; ++j) { + cin >> str >> id >> C[j]; + auto it = ids.emplace(id, icnt); + if (it.second) ++icnt; + Z[j] = it.first->second; + } + } + + for (int i=1; i<=n; ++i) + for (int j=1; j<=m; ++j) { + int &ds = DPs[i][j], + &dc = DPc[i][j]; + + if (X[i] == Y[j]) { + int dps = DPs[i-1][j-1] + A[i] + B[j]; + if (dps > DPs[i-1][j] && dps > DPs[i][j-1]) { + ds = dps, + dc = DPc[i-1][j-1] + 1; + continue; + } + } + + if (DPs[i-1][j] > DPs[i][j-1]) + ds = DPs[i-1][j], + dc = DPc[i-1][j]; + + else + ds = DPs[i][j-1], + dc = DPc[i][j-1]; + } + + cout << DPs[n][m] << ' ' << DPc[n][m] << '\n'; + } +} diff --git a/uva_cpp_clean/11723/11723-11.cpp b/uva_cpp_clean/11723/11723-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d70b72aa25f5934c674f22ad9300297e06ad7622 --- /dev/null +++ b/uva_cpp_clean/11723/11723-11.cpp @@ -0,0 +1,54 @@ +#include +/* +Problem: 11723 - Numbering Roads +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2823 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N, R, testcase = 0; +double K; + +int main () { + + while ( scanf("%d%d", &R, &N) && N ){ + printf("Case %d: ", ++testcase); + // N + N*(K) >= R + // so, k = ceil(R/N - 1) + // 0 <= K <= 26 + K = ceil((double)R/N - 1); + + if (K <= 26) printf("%d\n", (int)K); + else printf("impossible\n"); + } + + return 0; +} +/* +Sample input:- +----------------- +8 5 +100 2 +0 0 + +Sample output:- +----------------- +1 +impossible + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11723/11723-19.cpp b/uva_cpp_clean/11723/11723-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3b97ca43d7abe92101682474a41e3975ce33281 --- /dev/null +++ b/uva_cpp_clean/11723/11723-19.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +using namespace std; + +int main() { + int i = 1; + int R, N; + while (scanf("%d %d", &R, &N) != EOF && !(R == 0 && N == 0)) { + int suffixes = (R-N)/N; + if ((R-N)%N) suffixes++; + if (R <= N) suffixes = 0; + printf("Case %d: ", i); + if (suffixes <= 26) printf("%d\n", suffixes); + else printf("impossible\n"); + i++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11723/11723-20.cpp b/uva_cpp_clean/11723/11723-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8baeaae007405d2faf27fbf0d62a00f0c0f08258 --- /dev/null +++ b/uva_cpp_clean/11723/11723-20.cpp @@ -0,0 +1,17 @@ +#include + +int main() +{ + int R, N, nCase; + + nCase = 1; + while( scanf("%d %d", &R, &N)!=EOF && (R || N) ) + { + int D = (R-1)/N; + + if( D<=26 ) printf("Case %d: %d\n", nCase, D); + else printf("Case %d: impossible\n", nCase); + ++nCase; + } + return 0; +} diff --git a/uva_cpp_clean/11723/11723-21.cpp b/uva_cpp_clean/11723/11723-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3371adc4e94fe2b902138460f5699f6da248e936 --- /dev/null +++ b/uva_cpp_clean/11723/11723-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11723 + Name: Numbering Roads + Problem: https://onlinejudge.org/external/117/11723.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int cse=1, n, r; + while (cin>>n>>r && (n||r)) { + printf("Case %d: ", cse++); + + int x = ceil((double)n/r) - 1; + if (x>26) puts("impossible"); + else cout< ad-hoc + difficulty: easy + date: 14/Feb/2020 + by: @brpapa +*/ +#include +#include +using namespace std; + +int main() { + int R, N, D, T = 1; + while (true) { + cin >> R >> N; if (!R && !N) break; + + // (D+1)*N >= R + D = (int)ceil((double)R/N - 1); + + cout << "Case " << (T++) << ": "; + if (D > 26) cout << "impossible" << endl; + else cout << D << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11723/11723-9.cpp b/uva_cpp_clean/11723/11723-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32905bc2fa54b24efb0dc7cd71ab8d190c9726c6 --- /dev/null +++ b/uva_cpp_clean/11723/11723-9.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main(){ + int a, b, ans, t = 0; + while(scanf("%d %d", &a, &b), a | b){ + if(a / b > 26) + printf("Case %d: impossible\n", ++t); + else{ + ans = a / b; + if(!(a % b)) + ans--; + printf("Case %d: %d\n", ++t, ans); + } + } + return(0); +} diff --git a/uva_cpp_clean/11727/11727-13.cpp b/uva_cpp_clean/11727/11727-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd42ea6c1631ea6805891b987c869ef36d63e108 --- /dev/null +++ b/uva_cpp_clean/11727/11727-13.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + queue q; + + int ar[3]; + + int t, a, b, c; + + cin >> t; + + for (int i = 0; i < t; i++) + { + cin >> ar[0] >> ar[1] >> ar[2]; + + sort(ar, ar + 3); + + q.push(ar[1]); + } + + int counter = 1; + + while (!q.empty()) + { + cout << "Case " < +using namespace std; + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 1; i <= cases; i++) { + int n1, n2, n3; + scanf("%d %d %d", &n1, &n2, &n3); + printf("Case %d: %d\n", i, n1 + n2 + n3 - min(n1, min(n2, n3)) - max(n1, max(n2, n3))); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11727/11727-21.cpp b/uva_cpp_clean/11727/11727-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2d6f5607ebb94d98423a361ffa83c9c5ed4a972 --- /dev/null +++ b/uva_cpp_clean/11727/11727-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11727 + Name: Cost Cutting + Problem: https://onlinejudge.org/external/117/11727.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int max(int a, int b) { return a > b ? a : b; } +int min(int a, int b) { return a < b ? a : b; } + +int main() { + int C; + cin >> C; + for (int c = 1; c <= C; c++) { + int x, y, z, r; + cin >> x >> y >> z; + + printf("Case %d: %d\n", c, (x >= min(y, z) && x <= max(y, z)) ? x : (y >= min(x, z) && y <= max(x, z)) ? y : z); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11727/11727-3.cpp b/uva_cpp_clean/11727/11727-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1400d17cedfda6a830dd89111ae6b8fdd6730d57 --- /dev/null +++ b/uva_cpp_clean/11727/11727-3.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +int main(){ + + // Input specifications: first line contains integer N, number of test cases + // Next N lines contain 3 numbers that depict the salary of the employees + // Task : to print the salary with the medium value for each test case + + int N; // Number of test cases + int a,b,c; // Salaries + int result; // what i am gonna print + scanf ("%d",&N); // passing the value to N + + // The problem should be repeated for each test case + for ( int l = 1; l <= N; l++) { + scanf ("%d %d %d", &a, &b, &c); + result = a + b + c - max(a, max (b,c)) - min (a, min (b,c)); + printf ("Case %d: %d\n", l, result); + } + return 0; + +} + diff --git a/uva_cpp_clean/11727/11727-5.cpp b/uva_cpp_clean/11727/11727-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..21ca0e9a0febb448cef882f39e0f975386482bea --- /dev/null +++ b/uva_cpp_clean/11727/11727-5.cpp @@ -0,0 +1,29 @@ +#include +int main() +{ + int a[5],t,i,j,c,k; + while(scanf("%d",&t)==1) + { + for(k=1;k<=t;k++) + { + for(i=0;i<3;i++) + { + scanf("%d",&a[i]); + } + for(i=0;i<3;i++) + { + for(j=0;j<3-i-1;j++) + { + if(a[j]>a[j+1]) + { + c=a[j]; + a[j]=a[j+1]; + a[j+1]=c; + } + } + } + printf("Case %d: %d\n",k,a[1]); + } + } + return 0; +} diff --git a/uva_cpp_clean/11727/11727-9.cpp b/uva_cpp_clean/11727/11727-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6c66e7190b1f6ec5c86e57872c8aeeaa3fb4d40 --- /dev/null +++ b/uva_cpp_clean/11727/11727-9.cpp @@ -0,0 +1,24 @@ +#include + +void burbuja(int v[],int n){ + int i, j, temp; + for(i = 0; i < n; i++) + for(j = n - 1 ; j > i; j--) + if(v[i] > v[j]){ + temp = v[i]; + v[i] = v[j]; + v[j] = temp; + } +} + +int main(){ + int n, a[3], may, men,i = 1; + scanf("%d",&n); + while(n--){ + scanf("%d %d %d", &a[0], &a[1], &a[2]); + burbuja(a, 3); + printf("Case %d: %d\n", i, a[1]); + i++; + } + return(0); +} diff --git a/uva_cpp_clean/11727/main.cpp b/uva_cpp_clean/11727/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..617f07a07fcd1d9e44cdf9b8172e2be407ace900 --- /dev/null +++ b/uva_cpp_clean/11727/main.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int mediano(int s1, int s2, int s3){ + + if(s1 > s2 && s2 > s3 || (s3 > s2 && s2 > s1)) return s2; + if(s2 > s1 && s1 > s3 || (s3 > s1 && s1 > s2)) return s1; + if(s1 > s3 && s3 > s2 || (s2 > s3 && s3 > s1)) return s3; + +} + +int main(){ + + int a, s1, s2, s3, cont; + cin >> a; + for(cont = 1; cont <= a ; cont++){ + + cin >> s1 >> s2 >> s3; + int s = mediano(s1,s2,s3); + + cout << "Case " << cont << ": " << s << endl; + + + } + + return 0; +} diff --git a/uva_cpp_clean/11728/11728-13.cpp b/uva_cpp_clean/11728/11728-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44178ea16fb7e436b048e873e3c7153839d30f46 --- /dev/null +++ b/uva_cpp_clean/11728/11728-13.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 10000010 + +typedef long long ll; +typedef vector vll; +typedef vector vb; + +ll s; +int c = 1; +vb isPrime(MAX, true); +vll primes; + +void findPrimes() +{ + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; i++) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += i) + { + isPrime[j] = false; + } + + primes.push_back(i); + } + } +} + +ll sumDiv(ll n) +{ + ll ans = 1; + + ll p_inx = 0, p = primes[p_inx]; + + while (p * p <= n) + { + if (n % p == 0) + { + ll how = 0; + + while (n % p == 0) + { + n /= p; + + how++; + } + + ans *= ((ll)(pow((double)p, how + 1.0) - 1) / (p - 1)); + } + + p = primes[++p_inx]; + } + + if (n != 1) + { + ans *= ((ll)(pow((double)n, 2.0) - 1) / (n - 1)); + } + + return ans; +} + +int main() +{ + findPrimes(); + + while (cin >> s && s != 0) + { + bool ok = false; + + ll num = -1; + + for (ll i = s; i > 0 && !ok; i--) + { + if (sumDiv(i) == s) + { + ok = true; + num = i; + } + } + + cout << "Case " << c++ << ": " << num << endl; + } +} diff --git a/uva_cpp_clean/11729/11729-17.cpp b/uva_cpp_clean/11729/11729-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ad52992e3b0575eeeae771cd2f76c2d9af02938 --- /dev/null +++ b/uva_cpp_clean/11729/11729-17.cpp @@ -0,0 +1,23 @@ +/** + * Commando War + */ +#include +using namespace std; +int main() +{ + int n, T = 0; + while ((cin >> n) && n) + { + vector> jbs(n); + for (auto &jb : jbs) + cin >> jb.second >> jb.first; + sort(jbs.rbegin(), jbs.rend()); + int b = 0, ans = 0; + for (auto jb : jbs) + { + b += jb.second; + ans = max(ans, b + jb.first); + } + cout << "Case " << ++T << ": " << ans << "\n"; + } +} diff --git a/uva_cpp_clean/11729/11729-21.cpp b/uva_cpp_clean/11729/11729-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c84275644d16e0f0f00aaabd5acf3d9cd723b7d --- /dev/null +++ b/uva_cpp_clean/11729/11729-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11729 + Name: Commando War + Problem: https://onlinejudge.org/external/117/11729.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int B[1001], J[1001], ord[1001]; +bool comp(int i, int j) { + return J[i] != J[j] ? J[i] > J[j] : B[i] < B[j]; +} + +int main(){ + int cse=1, n; + while (cin>>n && n) { + for (int i=0; i> B[i] >> J[i]; + ord[i] = i; + } + sort(ord, ord+n, comp); + + int s=0, mx=0; + for (int i=0; i +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e4+9; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; +int n,m,cost; +pair minimumSpanningTree(vector> &edges) { + + + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + int ans = 0,plans=0,a,b,weight; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + if(weight>= cost) { + plans++; + } else { + ans+=weight; + } + } + } + + ///cout< st; + for(int i=1;i<=n;i++) { + st.insert(dsu.find(i)); + } + + plans+=st.size(); + + return {ans+(plans*cost),plans}; +} + + +int main() { + FAST + + int t;cin>>t; + int a,b,w; + int c=1; + + while(t--) { + cin >> n >> m >> cost; + + vector> arr; + + for (int i = 0; i < m; i++) { + cin >> a >> b >> w; + + arr.push_back({w, a, b}); + } + + auto res = minimumSpanningTree(arr); + + + cout<<"Case #"< +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iii; +typedef vector vi; + +struct UFDS { + vi p; + void init(int N) { + p.assign(N, 0); + for (int i = 0; i < N; i++) p[i]=i; + } + int find(int i) { + return p[i]==i? i: (p[i] = find(p[i])); + } + void unionn(int i, int j) { + int x = find(i), y = find(j); + if (x != y) p[y] = x; + } +}; + +priority_queue pq; +UFDS ufds; + +int N, M, A, cost, aero; + +void proc() { + int cair = aero, croad = 0; + while (!pq.empty()) { + iii road = pq.top(); + pq.pop(); + if (ufds.find(road.second.first) != ufds.find(road.second.second)){ + ufds.unionn(road.second.first, road.second.second); + cair--; + croad -= road.first; + int ncost = cair*A + croad; + if (ncost>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11733 + Name: Airports + Problem: https://onlinejudge.org/external/117/11733.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) + return EOF; + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +// ----------------------------------------------------------------- + +#define MAXN 10017 +int par[MAXN]; +int find(int u) { return par[u]<0 ? u : par[u]=find(par[u]); } +void join(int u, int v) { + if ((u=find(u)) == (v=find(v))) return; + if (par[v] < par[u]) + swap(u, v); + par[u] += par[v]; + par[v] = u; +} + +// -------- + +struct Pair { + int u, v; + Pair(){} + Pair(int u, int v):u(u),v(v){} +}; +vector adj[MAXN]; +int mindeg, maxdeg; +void qPush(int u, int v, int c) { + while (c < mindeg) + adj[--mindeg].clear(); + adj[c].push_back(Pair(u, v)); +} + +inline bool qEmpty() { return mindeg == maxdeg; } +int qPop(int &u, int &v) { + int c = mindeg; + Pair &p = adj[c].back(); + u = p.u; v = p.v; + + adj[c].pop_back(); + while (!qEmpty() && adj[mindeg].empty()) + ++mindeg; + + return c; +} + +// -------- + +int main() { + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +int tc, N, M, A, a, b, w; +vector < pair < int, ii > > EdgeList; +ii ans; + +ii Kruskal(){ + UnionFind UF(N); + sort(EdgeList.begin(), EdgeList.end()); + int CC = N, min_cost = 0; + for(int it = 0; it < EdgeList.size(); it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + min_cost += front.first, CC--, + UF.unionSet(front.second.first, front.second.second); + } + min_cost += CC * A; + return ii(min_cost, CC); +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + EdgeList.clear(); + scanf("%d %d %d", &N, &M, &A); + for(int it = 0; it < M; it++){ + scanf("%d %d %d", &a, &b, &w); + if( w < A ) + EdgeList.push_back( make_pair( w, ii(a - 1, b - 1) ) ); + } + ans = Kruskal(); + printf("Case #%d: %d %d\n", t, ans.first, ans.second); + } + return(0); +} diff --git a/uva_cpp_clean/11734/11734-20.cpp b/uva_cpp_clean/11734/11734-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a69bdb18b5fc5dfd024fd0cd2a5260fec85b59fb --- /dev/null +++ b/uva_cpp_clean/11734/11734-20.cpp @@ -0,0 +1,40 @@ +#include +#include + +char teamOutput[25], judgeOutput[25], temp[25]; + +int main() +{ + int tc, nCase; + char dump; + + scanf("%d%c", &tc, &dump); + nCase = 1; + while( tc-- ) + { + gets(teamOutput); + gets(judgeOutput); + + if( !strcmp(teamOutput, judgeOutput) ) printf("Case %d: Yes\n", nCase); + else + { + int len = strlen(teamOutput), pos = 0; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11734 + Name: Big Number of Teams will Solve This + Problem: https://onlinejudge.org/external/117/11734.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +char ww[3][30] = { "Wrong Answer", "Output Format Error", "Yes" }, + jo[100], to[100]; +int check() { + int f=2, i,j; + for (i=j=0; to[i] || jo[j]; i++) + if (to[i]==' ' || to[i]=='\t') f = 1; + else if (to[i] == jo[j]) j++; + else return 0; + + return to[i] == jo[j] ? f : 0; +} + +int main() { + int T; + cin>>T; + cin.getline(jo, 100); + for (int cse=1; cse<=T; cse++) { + cin.getline(to, 100); + cin.getline(jo, 100); + printf("Case %d: %s\n", cse, ww[check()]); + } +} diff --git a/uva_cpp_clean/11734/11734-5.cpp b/uva_cpp_clean/11734/11734-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4d2ac63c63dd102d36dbe45aeabf43de80cb5d0a --- /dev/null +++ b/uva_cpp_clean/11734/11734-5.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +int main() +{ + int n,i,g,f,j,k; + char te[30],ju[30],t[30]; + scanf("%d",&n); + getchar(); + for(i=1;i<=n;i++) + { + gets(te); + gets(ju); + if(strcmp(te,ju)==0) + printf("Case %d: Yes\n",i); + + else + { + g=strlen(te); + f=strlen(ju); + if(g==f) + printf("Case %d: Wrong Answer\n",i); + else + { + for(j=0,k=0;j +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 2005 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +struct edge { + //~ string u,v; + ll u, v, w; + bool operator<(const edge &p)const { + return w < p.w; + } +}; +mapmp; +vectoradj[MAX]; +ll par[MAX], color[MAX]; +vectorE; +ll find(ll r) { + if (par[r] == r) return r ; + else return par[r] = find(par[r]); +} +ll MST(ll n) { + sort(E.begin(), E.end()); + for (int i = 0; i < n; i++) { + par[i] = i; + } + ll cnt = 0, mx = 0, sz = E.size(); + for (int i = 0; i < sz; i++) { + ll u = find(E[i].u); + ll v = find(E[i].v); + if (u != v) { + par[u] = v; + cnt++; + ll x = E[i].u; + ll y = E[i].v; + ll ww = E[i].w; + adj[x].push_back(y); + adj[y].push_back(x); + mx += ww; + //~ printf("%d %d %d\n\n", x,y,ww); + if (cnt == n - 1) break; + } + } + return mx; +} +void dfs(ll u) { + color[u] = 1; + ll sz = adj[u].size(); + for (int i = 0; i < sz; i++) { + ll v = adj[u][i]; + if (color[v] == 0) { + dfs(v); + } + } +} +void mem() { + for (int i = 0; i < MAX; i++) { + color[i] = 0; + } +} +void Clear() { + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + par[i] = 0; + } + E.clear(), mp.clear(); +} +int main () { + //~ __FastIO; + ll n, m, tc; + cin >> tc; + for (int t = 1; t <= tc; t++) { + cin >> n >> m; + if (m != 0 ) { + ll k = 1; + //~ getchar(); + for (int i = 1; i <= m; i++) { + getchar(); + //~ cin.ignore(); + string u, v; + ll w; + cin >> u >> v; + cin >> w; + if (mp[u] == 0) { + mp[u] = k++; + //~ cout << u << " " << mp[u]; + //~ nl; + } + if (mp[v] == 0) { + mp[v] = k++; + //~ cout << u << " " << mp[u]; + //~ nl; + } + //~ scanf("%lld %lld %lld", &u, &v, &w); + edge get; + get.u = mp[u]; + get.v = mp[v]; + get.w = w; + E.push_back(get); + } + ll mx = MST(n); + mem(); + dfs(1); + int flag = 0; + for (int i = 1; i <= n; i++) { + if (color[i] == 0) { + flag = 1; + break; + } + } + if (flag) cout << "Impossible\n"; + else cout << mx << "\n"; + Clear(); + } else { + int flag = 0; + for (int i = 0; i < n; i++) { + if (color[i] == 0) { + flag = 1; + break; + } + } + if (flag) cout << "Impossible\n"; + else cout << "0\n"; + } + if(t != tc) nl; + } + return 0; +} + diff --git a/uva_cpp_clean/1174/1174-19.cpp b/uva_cpp_clean/1174/1174-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24bc00168d6450ca3ad3a7eb18f284194f5e4682 --- /dev/null +++ b/uva_cpp_clean/1174/1174-19.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair link; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +priority_queue pq; +unordered_map map; + +int main() { + int cases; + cin >> cases; + while (cases--) { + map.clear(); + int m, n; + cin >> m >> n; + for (int i = 0; i < n; i++) { + string k1, k2; + int cost; + cin >> k1 >> k2 >> cost; + if (!map.count(k1)) map.insert(make_pair(k1, map.size())); + if (!map.count(k2)) map.insert(make_pair(k2, map.size())); + link l; l.second.first = map[k1]; l.second.second = map[k2]; + l.first = -cost; + pq.push(l); + } + ufds u(m); + int c = 0; + while (!pq.empty()) { + if (!u.isSameSet(pq.top().second.first, pq.top().second.second)) { + c -= pq.top().first; + u.unionSet(pq.top().second.first, pq.top().second.second); + } + pq.pop(); + } + printf("%d\n", c); + if (cases) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/1174/1174-21.cpp b/uva_cpp_clean/1174/1174-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd9bd4711220d1c84942dd986bee157343e18bff --- /dev/null +++ b/uva_cpp_clean/1174/1174-21.cpp @@ -0,0 +1,77 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1174 + Name: IP-TV + Problem: https://onlinejudge.org/external/11/1174.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +short gid[2017]; +short find(short u) { + return gid[u]==u ? u : gid[u]=find(gid[u]); +} +void join(short u, short v) { + gid[find(u)] = find(v); +} + +short idcnt; +unordered_map ids; +short getId(const char s[]) { + auto it = ids.find(s); + if (it != ids.end()) + return it->second; + return ids[s] = idcnt++; +} + +struct Edge { + int w; + short u, v; + Edge(short u, short v, int w):u(u),v(v),w(w){} + bool operator < (const Edge &e) const { + return w > e.w; + } +}; + + +int main(){ + int T, n, m, w; + scanf("%d", &T); + char s1[100], s2[100]; + while (T--) { + scanf("%d%d", &n, &m); + + ids.clear(); idcnt = 0; + for (int i=0; i q; + for (int i=0; i +#include +#include +#include +#include + +class edge +{ +public: + int to; + int cost; + + edge() + { + to = -1; + cost = -1; + } + + edge(int _to, int _cost) + { + to = _to; + cost = _cost; + } + + bool operator<(const edge &x) const + { + return (cost > x.cost); + } +}; + +class node +{ +public: + int index; + bool connected; + std::vector edges; + + node() + { + index = -1; + connected = false; + } + + node(int _index) + { + index = _index; + connected = false; + } +}; + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + int m, n, cityIndex = 0; + ; + std::cin >> m >> n; + + std::vector nodes; + std::unordered_map cities; + + for (int i = 0; i < n; i++) + { + int cost; + std::string first, second; + std::cin >> first >> second >> cost; + + if (cities.find(first) == cities.end()) + { + nodes.push_back(node(cityIndex)); + cities[first] = cityIndex++; + } + + if (cities.find(second) == cities.end()) + { + nodes.push_back(node(cityIndex)); + cities[second] = cityIndex++; + } + + nodes[cities[first]].edges.push_back(edge(cities[second], cost)); + nodes[cities[second]].edges.push_back(edge(cities[first], cost)); + } + + int connected = 0, cost = 0; + std::priority_queue edgesQueue; + + nodes[0].connected = true; + connected++; + + for (unsigned int i = 0; i < nodes[0].edges.size(); i++) + { + edgesQueue.push(nodes[0].edges[i]); + } + + while (connected != m) + { + edge cheapest = edgesQueue.top(); + edgesQueue.pop(); + + if (nodes[cheapest.to].connected) + { + continue; + } + + nodes[cheapest.to].connected = true; + cost += cheapest.cost; + connected++; + + for (unsigned int i = 0; i < nodes[cheapest.to].edges.size(); i++) + { + if (!nodes[nodes[cheapest.to].edges[i].to].connected) + { + edgesQueue.push(nodes[cheapest.to].edges[i]); + } + } + } + + std::cout << cost << std::endl; + + if (tests != 0) + { + std::cout << std::endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1174/1174-6.cpp b/uva_cpp_clean/1174/1174-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6368118fa4d1a40a72bc5d0d80d40d60a648afd1 --- /dev/null +++ b/uva_cpp_clean/1174/1174-6.cpp @@ -0,0 +1,70 @@ +/* + graphs > minimum spanning tree (MST) + difficulty: easy + date: 03/Jun/2020 + by: @brpapa +*/ +#include +using namespace std; +struct UFDS { + vector p; // p[i] = pai do item i + + UFDS(int N) { + p.resize(N); + for (int i = 0; i < N; i++) p[i] = i; + } + + int findSet(int i) { + if (p[i] == i) return i; + return p[i] = findSet(p[i]); + } + + bool isSameSet(int i, int j) { + return findSet(i) == findSet(j); + } + + void unionSet(int i, int j) { + int setI = findSet(i); + int setJ = findSet(j); + if (!isSameSet(setI, setJ)) p[setI] = setJ; + } +}; + +map city2id; + +struct Tedge { + int u, v, w; + Tedge() {} + Tedge(int u, int v, int w) : u(u), v(v), w(w) {} + bool operator<(const Tedge& p) const { return w < p.w; } +}; + +int main() { + int t = 0, T; cin >> T; + while (T--) { + if (t++ > 0) cout << endl; + + int V, E; cin >> V >> E; + vector edgeList(E); city2id.clear(); + V = 0; + for (Tedge &e : edgeList) { + string u, v; cin >> u >> v; int w; cin >> w; + + if (!city2id.count(u)) city2id[u] = V++; + if (!city2id.count(v)) city2id[v] = V++; + e = Tedge(city2id[u], city2id[v], w); + } + + UFDS uf(V); + sort(edgeList.begin(), edgeList.end()); + + int ans = 0; + for (Tedge e: edgeList) + if (!uf.isSameSet(e.u, e.v)) { + uf.unionSet(e.u, e.v); + ans += e.w; + } + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/1174/1174-9.cpp b/uva_cpp_clean/1174/1174-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4965c4c77ee084520daf12f9776634d51d6afe69 --- /dev/null +++ b/uva_cpp_clean/1174/1174-9.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +const int LIM = 35; +int tc, N, M, w, ct; +char word1[LIM], word2[LIM]; +vector < pair < int, ii > > EdgeList; +map < string, int > mp; + +int Kruskal(){ + UnionFind UF(N); + sort(EdgeList.begin(), EdgeList.end()); + int min_cost = 0, setSize = 1; + for(int it = 0; it < M; it++){ + pair < int, ii > front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + min_cost += front.first, setSize++, + UF.unionSet(front.second.first, front.second.second); + if( setSize == N ) break; + } + return min_cost; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + EdgeList.clear(); + mp.clear(); + ct = 0; + scanf("%d", &N); + scanf("%d\n", &M); + for(int it = 0; it < M; it++){ + scanf("%s %s %d\n", word1, word2, &w); + if( mp.find(word1) == mp.end() ) mp[word1] = ct++; + if( mp.find(word2) == mp.end() ) mp[word2] = ct++; + EdgeList.push_back( make_pair( w, ii(mp[word1], mp[word2]) ) ); + } + printf("%d\n", Kruskal()); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/11742/11742-11.cpp b/uva_cpp_clean/11742/11742-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ba9b360c509e454dc00a69615ecdf855acccba0 --- /dev/null +++ b/uva_cpp_clean/11742/11742-11.cpp @@ -0,0 +1,75 @@ +#include +/* +Problem: 11742 - Social Constraints +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2842 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N, M; + +bool satisfied; +int a[21], b[21], c[21], teens[8]; + +int main () { + + while ( scanf("%d %d\n", &N, &M) && N ){ + + int seats = 0; + + for (int i = 0; i < M; i++) + scanf("%d %d %d\n", &a[i], &b[i], &c[i]); + + for (int i = 0; i < N; i++) + teens[i] = i; + + do { + satisfied = true; + + for (int i = 0; i < M; i++){ + if (c[i] > 0 && abs(teens[a[i]]-teens[b[i]]) <= c[i]) continue; + if (c[i] < 0 && abs(teens[a[i]]-teens[b[i]]) >= -c[i]) continue; + + satisfied = false; + break; + } + + if (satisfied) seats++; + + } while (next_permutation(teens, teens+N)); + + + printf("%d\n", seats); + } + + return 0; +} +/* +Sample input:- +----------------- +3 1 +0 1 -2 +3 0 +0 0 + +Sample output:- +----------------- +2 +6 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11742/11742-21.cpp b/uva_cpp_clean/11742/11742-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa7e401e30a09e31f6ac89580f6a2c1d5f0ef641 --- /dev/null +++ b/uva_cpp_clean/11742/11742-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11742 + Name: Social Constraints + Problem: https://onlinejudge.org/external/117/11742.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +typedef pair Pair; +#define ss second +#define ff first + + +vector adj[17]; +int P[17], n; +bool used[17]; +bool check(int u, int pu) { + for (Pair uv: adj[u]) { + if (uv.ss<0 && abs(pu-P[uv.ff])<-uv.ss) + return 0; + if (uv.ss>0 && abs(pu-P[uv.ff])>uv.ss) + return 0; + } + return 1; +} + + +int bt(int pi) { + if (pi == n) return 1; + + int sum = 0; + for (int i=0; i> n >> m && (n||m)) { + for (int i=0; i> u >> v >> w; + if (u < v) swap(u, v); + adj[u].push_back(Pair(v, w)); + } + + cout << bt(0) << '\n'; + } +} diff --git a/uva_cpp_clean/11742/11742-3.cpp b/uva_cpp_clean/11742/11742-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5be16a8e12740743fb512f8e08fd229c3f6374bc --- /dev/null +++ b/uva_cpp_clean/11742/11742-3.cpp @@ -0,0 +1,63 @@ +#include // next_permutation +#include + +using namespace std; + +struct constraint +{ + int personA; + int personB; + int distance; +}; + +// Declarations: +int sizeOfGroup, numberOfConstraints, counter, + party[8] = {0,1,2,3,4,5,6,7}, tempArray[8]; +constraint allConstraints[20]; + +bool isEverythingFine() +{ + //tempArray is used to store the locations of the elements in party array + for ( int l = 0; l < sizeOfGroup; l++ ) + tempArray[party[l]] = l; + + for ( int l = 0; l < numberOfConstraints; l++ ) + { + if ( allConstraints[l].distance < 0 ) + { + if ( abs(allConstraints[l].distance) > + abs( tempArray[allConstraints[l].personA] - tempArray[allConstraints[l].personB])) + return false; + } + else + if ( allConstraints[l].distance < + abs( tempArray[allConstraints[l].personA] - tempArray[allConstraints[l].personB])) + return false; + } + return true; +} +int main() +{ + + // Input part: + while ( scanf("%d %d", &sizeOfGroup, &numberOfConstraints), sizeOfGroup) + { + // printf("%d %d\n", sizeOfGroup, numberOfConstraints); + for ( int i = 0; i < numberOfConstraints; i++ ) + scanf("%d %d %d", &allConstraints[i].personA, + &allConstraints[i].personB, + &allConstraints[i].distance); + + // Input part finished: Main part of the function + counter = 0; + do + { + if ( isEverythingFine() ) + counter++; + } + while ( next_permutation( party, party + sizeOfGroup ) ); + + // Output: + printf("%d\n", counter); + } +} diff --git a/uva_cpp_clean/11743/11743-14.cpp b/uva_cpp_clean/11743/11743-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7f49864a1adbf9dd69f6f0b4122226e5d6124ae --- /dev/null +++ b/uva_cpp_clean/11743/11743-14.cpp @@ -0,0 +1,106 @@ +/*************************************************** + * Problem Name : 11743 Credit Check.cpp + * Problem Link : https://uva.onlinejudge.org/external/117/11743.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-10-19 + * Problem Type : Implementation + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int digitSum (int n) { + int sum = 0; + + while (n > 0) { + sum += (n % 10); + n /= 10; + } + + return sum; +} +int main () { + __FastIO; + int tc; + cin >> tc; + + while (tc--) { + string a, b, c, d; + cin >> a >> b >> c >> d; + int sum = 0; + sum += (digitSum ( (a[0] - '0') * 2) ); + sum += (digitSum ( (a[2] - '0') * 2) ); + sum += (digitSum ( (b[0] - '0') * 2) ); + sum += (digitSum ( (b[2] - '0') * 2) ); + sum += (digitSum ( (c[0] - '0') * 2) ); + sum += (digitSum ( (c[2] - '0') * 2) ); + sum += (digitSum ( (d[0] - '0') * 2) ); + sum += (digitSum ( (d[2] - '0') * 2) ); + //~ cout << sum << " -\n"; + //~ single digit sum + sum += (digitSum (a[1] - '0') ); + sum += (digitSum (a[3] - '0') ); + sum += (digitSum (b[1] - '0') ); + sum += (digitSum (b[3] - '0') ); + sum += (digitSum (c[1] - '0') ); + sum += (digitSum (c[3] - '0') ); + sum += (digitSum (d[1] - '0') ); + sum += (digitSum (d[3] - '0') ); + + //~ cout << sum << " --\n"; + if (sum % 10 == 0) cout << "Valid\n"; + else cout << "Invalid\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/11743/11743-19.cpp b/uva_cpp_clean/11743/11743-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fec8010bbebf3ca6bcbd28f7b57acb665654dad2 --- /dev/null +++ b/uva_cpp_clean/11743/11743-19.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + int sum = 0; + for (int j = 0; j < 16; j++){ + int n; + scanf("%1d", &n); + if (j % 2 == 0) sum += n == 9? n: (n*2)%9; + else sum += n; + } + if (sum % 10 != 0) printf("Invalid\n"); + else printf("Valid\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11743/11743-21.cpp b/uva_cpp_clean/11743/11743-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abbee843c3b9c6b3c06039e8f4696431ea6cbae5 --- /dev/null +++ b/uva_cpp_clean/11743/11743-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11743 + Name: Credit Check + Problem: https://onlinejudge.org/external/117/11743.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int dsum(int x) { + return x<10 ? x : x/10 + x%10; +} + +int main(){ + int T; + cin>>T; + char line[100]; + cin.getline(line, 100); + while (T--) { + cin.getline(line, 100); + + int res=0; + int cnt=0; + for (int i=0; line[i]; i++) + if (line[i] != ' ') { + int x = line[i]-'0'; + res += cnt&1 ? x : dsum(x<<1); + cnt++; + } + + puts(res%10 ? "Invalid" : "Valid"); + } +} diff --git a/uva_cpp_clean/11747/11747-13.cpp b/uva_cpp_clean/11747/11747-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10b0df2acabfd32325e89783094e9e29ee50112e --- /dev/null +++ b/uva_cpp_clean/11747/11747-13.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 200010 + +ofstream fout("f.out"); + +int n, m; +int parent[MAX], sz[MAX]; +vector ans; + +class comparator +{ +public: + + bool operator()(pair, int> &child1, pair, int> &child2) + { + return child1.second > child2.second; + } +}; + +priority_queue< pair< pair< int, int >, int >, vector< pair< pair< int, int >, int > >, comparator> pq; + +int findParent(int x) +{ + if (x == parent[x]) return parent[x]; + + else return parent[x] = findParent(parent[x]); +} + +bool merge(pair edge) +{ + int x = edge.first; + + int y = edge.second; + + if (findParent(x) == findParent(y)) return true; + + else + { + x = findParent(x); + + y = findParent(y); + + if (sz[x] < sz[y]) swap(x, y); + + parent[y] = x; + + sz[x] += sz[y]; + + return false; + } +} + +void initialize(int n) +{ + ans.clear(); + + memset(sz, 1, sizeof sz); + + for (int i = 0; i < n; i++) + { + parent[i] = i; + } +} + +void mst() +{ + while (!pq.empty()) + { + pair < pair , int > temp = pq.top(); + + if (merge(temp.first)) + { + ans.push_back(temp.second); + } + + pq.pop(); + } +} + +int main() +{ + while (cin >> n >> m, n != 0 || m != 0) + { + initialize(n); + + for (int i = 0; i < m; i++) + { + int x, y, w; + + cin >> x >> y >> w; + + pq.push(make_pair(make_pair(x, y), w)); + } + + mst(); + + sort(ans.begin(), ans.end()); + + if (ans.size() == 0) cout << "forest" << endl; + + else + { + cout << ans[0]; + + for (int i = 1; i < ans.size(); i++) + { + cout << " " << ans[i]; + } + + cout << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11747/11747-19.cpp b/uva_cpp_clean/11747/11747-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..572d265c0be4f6d7a2400068f6febfc53be9d5f3 --- /dev/null +++ b/uva_cpp_clean/11747/11747-19.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iii; +typedef vector vi; + +struct ufds { + vi p, r; + int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { + return (p[i] == i) ? i : (p[i] = findSet(p[i])); + } + bool isSameSet(int i, int j) { + return findSet(i) == findSet(j); + } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } + numSets--; + } + } +}; + +int main() { + int n, m; + priority_queue q; + queue output; + while (scanf("%d %d", &n, &m) && !(m == 0 && n == 0)) { + ufds u(n+1); + for (int i = 0; i < m; i++) { + int x, y, z; + scanf("%d %d %d", &x, &y, &z); + q.push(iii(-z, ii(x, y))); + } + while (!q.empty()) { + if(!u.isSameSet(q.top().second.first, q.top().second.second)) { + u.unionSet(q.top().second.first, q.top().second.second); + } else { + output.push(-q.top().first); + } + q.pop(); + } + if (output.empty()) printf("forest\n"); + else { + bool first = false; + while (!output.empty()) { + if (first) printf(" "); + else first = true; + printf("%d", output.front()); + output.pop(); + } + printf("\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11747/11747-21.cpp b/uva_cpp_clean/11747/11747-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13174c40217207c62fe15df4af59b64eee7edcea --- /dev/null +++ b/uva_cpp_clean/11747/11747-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11747 + Name: Heavy Cycle Edges + Problem: https://onlinejudge.org/external/117/11747.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int ids[1001]; +int find(int u) { + return u==ids[u] ? u : ids[u] = find(ids[u]); +} +void join(int u, int v) { + ids[find(u)] = find(v); +} + +struct Edge { + int u, v, w; + + Edge (int u, int v, int w) : u(u), v(v), w(w){} + bool operator < (const Edge &e) const { return w > e.w; } +}; + +int main(){ + int n, m, u, v, w; + while (scanf("%d%d", &n, &m)==2 && (n||m)) { + for (int i=0; i q; + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +int N, M, a, b, w; +vector < pair > EdgeList; + +void Kruskal(){ + UnionFind UF(N); + sort(EdgeList.begin(), EdgeList.end()); + vector ans; + for(int it = 0; it < M; it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + UF.unionSet(front.second.first, front.second.second); + else + ans.push_back(front.first); + } + if( ans.empty() ) puts("forest"); + else + for(int it = 0; it < ans.size(); it++) + printf( it == ans.size() - 1 ? "%d\n" : "%d ", ans[it]); +} + +int main(){ + while(scanf("%d %d", &N, &M), N | M ){ + EdgeList.clear(); + for(int it = 0; it < M; it++){ + scanf("%d %d %d", &a, &b, &w); + EdgeList.push_back( make_pair( w, ii(a, b) ) ); + } + Kruskal(); + } + return(0); +} diff --git a/uva_cpp_clean/11749/11749-21.cpp b/uva_cpp_clean/11749/11749-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a29fd37b2e9d6fd5335cc5e0b37e8a330cdbe57 --- /dev/null +++ b/uva_cpp_clean/11749/11749-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11749 + Name: Poor Trade Advisor + Problem: https://onlinejudge.org/external/117/11749.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +int readInt() { + int sgn=1; + int r=0; + char ch; + while (!isdigit(ch=getchar()) && ch!='-'); + if (ch == '-') sgn = -1; + else r = ch-'0'; + + while (isdigit(ch=getchar())) + r = r*10 + ch-'0'; + return sgn*r; +} + + +#define MAXE 1000001 +int U[MAXE], V[MAXE], gid[600], gcn[600]; +int find(int u) { return gid[u]==u ? u : gid[u]=find(gid[u]); } + + +int main(){ + int n, m, u, v, d; + while (scanf("%d%d", &n, &m)==2 && (n||m)) { + for (int i=0; i mx) { + mx = d; + j = 0; + } + if (d == mx) { + U[j] = u - 1; + V[j] = v - 1; + ++j; + } + } + + int bcnt = 0; + for (int k=0; k bcnt) { + bcnt = gcn[vg]; + if (bcnt == n) break; + } + } + } + + printf("%d\n", bcnt); + } +} diff --git a/uva_cpp_clean/11749/11749-6.cc b/uva_cpp_clean/11749/11749-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..a1d273c68c4d2d426cb850e28e276cfba592dd3f --- /dev/null +++ b/uva_cpp_clean/11749/11749-6.cc @@ -0,0 +1,45 @@ +// https://uva.onlinejudge.org/external/117/11749.pdf +#include +using namespace std; +using iii=tuple; +using vi=vector; +using vvi=vector; +using viii=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + int n,m,u,v,w,W=-2147483648,M=0; + cin>>n>>m; + if(!n)break; + viii e(m); + for(int i=0;i>u>>v>>w; + u--;v--; + W=max(W,w); + e[i]=make_tuple(u,v,w); + } + vvi g(n); + for (iii &x:e){ + tie(u,v,w)=x; + if(w==W){ + g[u].push_back(v); + g[v].push_back(u); + } + } + vi s(n); + functiondfs=[&](int u){ + s[u]=1; + for(int v:g[u]) + if(!s[v]){ + dfs(v); + s[u]+=s[v]; + } + }; + for(int i=0;i + +#define SIZE 510 + +using namespace std; + +int n, m, a, b, c, w, ans; +vector < pair > G[SIZE]; +bool vis[SIZE]; + +int floodfill(int u){ + vis[u] = true; + int ct = 1; + for(int v = 0; v < G[u].size(); v++) + if( not vis[G[u][v].first] and G[u][v].second == w ) + ct += floodfill(G[u][v].first); + return ct; +} + +void clear_(){ + w = ans = INT_MIN; + for(int it = 1; it <= n; it++) G[it].clear(); + memset(vis, false, sizeof vis); +} + +bool check(int u){ + for(int v = 0; v < G[u].size(); v++) + if( G[u][v].second == w ) return true; + return false; +} + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + clear_(); + for(int it = 0; it < m; it++){ + scanf("%d %d %d", &a, &b, &c); + G[a].push_back( make_pair(b, c) ); + G[b].push_back( make_pair(a, c) ); + w = max(w, c); + } + for(int it = 1; it <= n; it++) + if( check(it) ) + ans = max(ans, floodfill(it)); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11752/11752-9.cpp b/uva_cpp_clean/11752/11752-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..477cf79d99c0c4f0db0df8d62fb625d976fbc1f7 --- /dev/null +++ b/uva_cpp_clean/11752/11752-9.cpp @@ -0,0 +1,21 @@ +import sys + +def solution(): + + not_prime = lambda num : any(num%it==0 for it in range(2,num)) + + comp = [it for it in range(4,70) if not_prime(it)] + + ans, LIM = set([1]), 1<<64 + + for a in range(2,(1<<16) + 1): + for b in comp: + if a**b < LIM: + ans.add(a**b) + else: + break + for it in sorted(ans): + sys.stdout.write(str(it) + '\n') + +if __name__=="__main__": + solution() diff --git a/uva_cpp_clean/11753/11753-19.cpp b/uva_cpp_clean/11753/11753-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..230265c5c5ba45aa780b9dcc5d3fc6ad9029d60c --- /dev/null +++ b/uva_cpp_clean/11753/11753-19.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + +vector v; +int N, K, inc; + +int proc(int i, int j) { + if (inc > K) return K + 10; + if (i == j) return 0; + if (i == j - 1) return v[i] == v[j]? 0: 1; + int sol; + if (v[i] == v[j]) sol = proc(i+1, j-1); + else { + inc++; + sol = min(proc(i+1, j), proc(i, j-1)) + 1; + inc--; + } + return sol; +} + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + v.clear(); + cin >> N >> K; + inc = 0; + for (int i = 0; i < N; i++) { + int val; + cin >> val; + v.push_back(val); + } + int sol = proc(0, v.size() - 1); + if (sol == 0) printf("Case %d: Too easy\n", c); + else if (sol <= K) printf("Case %d: %d\n", c, sol); + else printf("Case %d: Too difficult\n", c); + + } + return 0; +} diff --git a/uva_cpp_clean/11760/11760-21.cpp b/uva_cpp_clean/11760/11760-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c3d0fb8c97314bc8953b47a9e40a0dd46bbf2e9 --- /dev/null +++ b/uva_cpp_clean/11760/11760-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11760 + Name: Brother Arif, Please feed us! + Problem: https://onlinejudge.org/external/117/11760.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +char msg[2][200] = { + "Escaped again! More 2D grid problems!", + "Party time! Let's find a restaurant!" +}; + +bool rs[10001], cs[10001]; +int main() { + int n, m, k, x, y, cse=1; + while (scanf("%d%d%d", &n, &m, &k)==3 && (n||m||k)) { + memset(rs, 0, n); + memset(cs, 0, m); + for (int i=0; i + +#define SIZE 10010 + +using namespace std; + +int tc, R, C, N, r, c, x, y; +bitset row, column; +int dx[] = {-1, 0, 0, 0, 1}; +int dy[] = {0, 1, 0, -1, 0}; +bool ok; + +inline bool isValid(int _x, int _y){ + return (_x >= 0 && _y >= 0 && _x < R && _y < C); +} + +int main(){ + while(scanf("%d %d %d", &R, &C, &N),R | C | N){ + row.reset(), column.reset(); + for(int i = 0; i < N; i++) scanf("%d %d", &r, &c), row.set(r, 1), column.set(c, 1); + scanf("%d %d",&x, &y); + ok = true; + for(int i = 0; i < 5; i++){ + int X = x + dx[i]; + int Y = y + dy[i]; + if(isValid(X, Y) && (!row.test(X) && !column.test(Y))) + ok = false; + } + printf("Case %d: ", ++tc); + if(ok) puts("Party time! Let's find a restaurant!"); + else puts("Escaped again! More 2D grid problems!"); + } + return(0); +} diff --git a/uva_cpp_clean/11762/11762-19.cpp b/uva_cpp_clean/11762/11762-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a686bddaa2a9813fe8ad5a6235b5567ba63657f --- /dev/null +++ b/uva_cpp_clean/11762/11762-19.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<100000001> bs; +vi primes; + +inline void sieve(ll upperbound = 1000000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline void primeFactors(ll n, vii & factors) { + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +long double dp[1000010]; + +long double proc(int N) { + if (N == 1) return 0; + if (dp[N] == 0) { + vii factors; + primeFactors(N, factors); + long double divisors = factors.size(); + int primnum = upper_bound(primes.begin(), primes.end(), N) - primes.begin(); + long double E = 0; + for (int i = 0; i < factors.size(); i++) { + E += proc(N/factors[i].first)/primnum; + } + dp[N] = (E+1)*primnum/divisors; + } + return dp[N]; +} + +int main() { + sieve(); + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + int N; + cin >> N; + printf("Case %d: %.10Lf\n", c, proc(N)); + } + return 0; +} diff --git a/uva_cpp_clean/11764/11764-19.cpp b/uva_cpp_clean/11764/11764-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77abb84e2b2e82dd1f5d8f69da52dbd518e6cafb --- /dev/null +++ b/uva_cpp_clean/11764/11764-19.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +int main() { + int T; + scanf("%d", &T); + for (int i = 1; i <= T; i++) { + int up = 0, down = 0, h; + int blocks; + scanf("%d", &blocks); scanf("%d", &h); + for (int j = 1; j < blocks; j++) { + int next; + scanf("%d", &next); + if (next > h) up++; + else if (next < h) down++; + h = next; + } + printf("Case %d: %d %d\n", i, up, down); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11764/11764-2.cpp b/uva_cpp_clean/11764/11764-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd90babdad8bbe825e3a5613913d315aa2d0b402 --- /dev/null +++ b/uva_cpp_clean/11764/11764-2.cpp @@ -0,0 +1,51 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; cin >> tc; + int t = 1; + while (tc--) { + int up = 0, down = 0; + int n; cin >> n; + vi v(n); + for (int i = 0; i < n; ++i) cin >> v[i]; + for (int i = 1; i < n; ++i) { + if (v[i-1] > v[i]) down++; + else if (v[i-1] < v[i]) up++; + } + printf("Case %d: %d %d\n", t++, up, down); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11764/11764-21.cpp b/uva_cpp_clean/11764/11764-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32560cc41b6432d074693b12f81b053a795bae3c --- /dev/null +++ b/uva_cpp_clean/11764/11764-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11764 + Name: Jumping Mario + Problem: https://onlinejudge.org/external/117/11764.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int C; + cin >> C; + for (int c = 1; c <= C; c++) { + int n, x, y, h = 0, l = 0; + cin >> n >> x; + while (--n) { + cin >> y; + if (y > x) h++; + if (y < x) l++; + x = y; + } + + printf("Case %d: %d %d\n", c, h, l); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11764/11764-4.cpp b/uva_cpp_clean/11764/11764-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3abe5e3dcc790effd3960322ac3907ccf6501268 --- /dev/null +++ b/uva_cpp_clean/11764/11764-4.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int main() +{ + int tests, casenr = 0; + cin >> tests; + + while (tests--) + { + int walls; + cin >> walls; + + int prev = -1, low = 0, high = 0, temp; + + for (int i = 0; i < walls; i++) + { + cin >> temp; + + if (prev == -1) + prev = temp; + else + { + if (temp > prev) + high++; + else if (temp < prev) + low++; + prev = temp; + } + } + + cout << "Case " << ++casenr << ": " << high << " " << low << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11764/11764-5.cpp b/uva_cpp_clean/11764/11764-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2a1413246b38f20e15d80b67eee71b8cac9e9636 --- /dev/null +++ b/uva_cpp_clean/11764/11764-5.cpp @@ -0,0 +1,28 @@ +#include +int main() +{ + int t,n,a[100],i,up,low,j; + while(scanf("%d",&t)==1) + { + for(i=1;i<=t;i++) + { + scanf("%d",&n); + for(j=0;ja[j+1]) + low++; + } + printf("Case %d: %d %d\n",i,up,low); + } + } + + + + return 0; +} diff --git a/uva_cpp_clean/11764/11764-9.cpp b/uva_cpp_clean/11764/11764-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a55dd886a4afa3172c3deec6eba85093a15103a7 --- /dev/null +++ b/uva_cpp_clean/11764/11764-9.cpp @@ -0,0 +1,17 @@ +#include + +int main(){ + int n, a[50], i, t = 1, taller, shorter, N; + scanf("%d", &N); + while(N--){ + taller = shorter = 0; + scanf("%d", &n); + for(i = 0; i < n; i++) scanf("%d", &a[i]); + for(i = 0; i < n - 1; i++) + if(a[i] < a[i+1]) taller++; + else if (a[i] > a[i+1]) shorter++; + printf("Case %d: %d %d\n", t, taller, shorter); + t++; + } + return(0); +} diff --git a/uva_cpp_clean/11764/main.cpp b/uva_cpp_clean/11764/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d903f193ae4eb2679ee7337383aa2c141d0ec7d --- /dev/null +++ b/uva_cpp_clean/11764/main.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main(){ + short high, low, cases, walls ,actual_h, next_h; + cin >> cases; + for(int i=1; i <= cases; i++){ + cin >> walls; + high = 0; + low = 0; + cin >> actual_h; + for(int c=1; c < walls; c++){ + cin >> next_h; + if(actual_h - next_h > 0) low++; + else if(actual_h - next_h < 0) high++; + actual_h = next_h; + } + cout << "Case " << i << ": " << high << ' ' << low << endl;; + } + + + +return 0; +} diff --git a/uva_cpp_clean/11770/11770-21.cpp b/uva_cpp_clean/11770/11770-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89b81d6212c194fc79596a56b293465c0988ad72 --- /dev/null +++ b/uva_cpp_clean/11770/11770-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11770 + Name: Lighting Away + Problem: https://onlinejudge.org/external/117/11770.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXN 10001 + +list adj[MAXN]; + +bool seen[MAXN]; +int order[MAXN], ocnt; +void topo(int u) { + seen[u]=1; + for (int v: adj[u]) + if (!seen[v]) + topo(v); + + if (ocnt>0) + order[--ocnt] = u; +} + + +int main(){ + int T, n, m, u, v; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin>>n>>m; + for (int i=0; i>u>>v; + adj[u-1].push_back(v-1); + } + + ocnt = n; + for (int i=0; i +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (10010) +#define mod 1000000007 +////=====================================//// + +int col[mx]; +vi g[mx]; +bool f; +void dfs(int u) +{ + col[u]=1; + for0(i,sz(g[u])) + { + int v=g[u][i]; + if(!col[v]) + dfs(v); + } + col[u]=2; +} + +int main() +{ + int t,no=0; + si(t); + while(t--) + { + int n,e; + sii(n,e); + while(e--) + { + int a,b; + sii(a,b); + g[a].pb(b); + } + + vi v1; + vi v2; + for1(i,n) + { + if(!col[i]) + { + v1.pb(i); + dfs(i); + } + } + + int len=sz(v1); + cover(col,0); + rfor0(i,len) + { + if(!col[v1[i]]) + { + v2.pb(v1[i]); + dfs(v1[i]); + } + } + + Case(no); + P(min(sz(v1),sz(v2))); + + cover(col,0); + for0(i,mx) + { + g[i].clear(); + } + } + return 0; +} diff --git a/uva_cpp_clean/11770/11770-6.cc b/uva_cpp_clean/11770/11770-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..9d05657f06cfdc0852e0f89e0de2e761a69b7691 --- /dev/null +++ b/uva_cpp_clean/11770/11770-6.cc @@ -0,0 +1,55 @@ +// https://uva.onlinejudge.org/external/117/11770.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +using qi=queue; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t,n,m,u,v; + cin>>t; + for(int T=1;T<=t;T++){ + cin>>n>>m; + vvi g(n),h(n),c; + for(int i=0;i>u>>v; + u--;v--; + g[u].push_back(v); + h[v].push_back(u); + } + vi a,s(n),t(n),r(n); + functiondfs1=[&](int i){ + s[i]=1; + for(int j:g[i]) + if(!s[j])dfs1(j); + a.push_back(i); + }; + for(int i=0;idfs2=[&](int i){ + t[i]=1; + c.back().push_back(i); + for(int j:h[i]) + if(!t[j])dfs2(j); + }; + for(int i=0;idfs3=[&](int i){ + r[i]=1; + for(int j:g[i]) + if(!r[j])dfs3(j); + }; + int k=0; + for(vi &x:c) + if(!r[x[0]]){ + k++; + dfs3(x[0]); + } + cout<<"Case "< + +using namespace std; + +const int MAX_V = 10010; + +int tc, V, E, CC; +bool vis[MAX_V]; +vector AdjList[MAX_V], ts; + +void dfs(int u, bool ok = true){ + vis[u] = true; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not vis[v] ) dfs(v); + } + if( ok ) ts.push_back(u); +} + +void solve(int t){ + fill(vis, vis + V + 1, false); + for(int u = 1; u <= V; u++) + if( not vis[u] ) + dfs(u); + reverse(ts.begin(), ts.end()); + fill(vis, vis + V + 1, false); + for(int j = 0; j < ts.size(); j++) + if( not vis[ts[j]] ) + CC++, dfs(ts[j], false); + printf("Case %d: %d\n", t, CC); +} + +void read(){ + int u, v; + scanf("%d %d", &V, &E); + while(E--){ + scanf("%d %d", &u, &v); + AdjList[u].push_back(v); + } +} + +void clear(){ + CC = 0; + ts.clear(); + for(int u = 1; u <= V; u++) AdjList[u].clear(); +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + read(); + solve(t); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11774/11774-14.cpp b/uva_cpp_clean/11774/11774-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ffe0f40edf8211ace0bd71e1afa2469d278fad45 --- /dev/null +++ b/uva_cpp_clean/11774/11774-14.cpp @@ -0,0 +1,69 @@ +/*************************************************** + * Problem name : 11774 Doom’s Day.cpp + * Problem Link : https://uva.onlinejudge.org/external/117/11774.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-23 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc; + ull m, n; + + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%llu %llu", &m, &n); + ull g = gcd(m, n); + ull ans = m / g + n / g; + printf("Case %d: %llu\n", t, ans); + } + return 0; +} + diff --git a/uva_cpp_clean/11774/11774-20.cpp b/uva_cpp_clean/11774/11774-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e33fbc6f3a1d054cdde5c586db84d02d65d61af0 --- /dev/null +++ b/uva_cpp_clean/11774/11774-20.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; + +int GCD(int a, int b) +{ + if( b==0 ) return a; + + return GCD(b, a%b); +} + +int main() +{ + int tc, ncase; + + cin >> tc; + ncase = 0; + while( tc-- ) + { + int m, n; + cin >> m >> n; + + if( m==n ) cout << "Case " << ++ncase << ": 2" << endl; + else cout << "Case " << ++ncase << ": " << (m/GCD(m, n)+n/GCD(m, n)) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11774/11774-9.cpp b/uva_cpp_clean/11774/11774-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87ffb3d986b494110a1a7a78fbe5dd391941c611 --- /dev/null +++ b/uva_cpp_clean/11774/11774-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int tc; +unsigned long long m, n; + +unsigned long long gcd(unsigned long long a, unsigned long long b){ + return b == 0 ? a : gcd(b, a % b); +} + + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%llu %llu", &m, &n); + printf("Case %d: %llu\n", t, m / gcd(m, n) + n / gcd(m, n)); + } + return(0); +} diff --git a/uva_cpp_clean/11776/11776-19.cpp b/uva_cpp_clean/11776/11776-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d9641d97eced2a5bdc2c099cb944af5216f7d10 --- /dev/null +++ b/uva_cpp_clean/11776/11776-19.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +using namespace std; + +typedef pair ii; + +int main() { + int N, c = 1; + while (cin >> N && N != -1) { + priority_queue open, close; + for (int i = 0; i < N; i++) { + int S, E; + cin >> S >> E; + open.push(make_pair(-S, -E)); + } + int o = 0, max = 0; + close.push(make_pair(-10000000000, -10000000000)); + while (!open.empty()) { + if (open.top().first < close.top().first) { + close.pop(); + o--; + } else { + ii u = open.top(); + open.pop(); + o++; + if (o > max) max = o; + close.push(make_pair(u.second, u.first)); + } + } + printf("Case %d: %d\n", c, max); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/11777/11777-19.cpp b/uva_cpp_clean/11777/11777-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..704ae7c2d57b47e65a3a715045b3ed748eb863ca --- /dev/null +++ b/uva_cpp_clean/11777/11777-19.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +char grade(int count) { + if (count >= 90) return 'A'; + if (count >= 80) return 'B'; + if (count >= 70) return 'C'; + if (count >= 60) return 'D'; + return 'F'; +} + +int main() { + int n; + cin >> n; + for (int i = 1; i <= n; i++) { + int count = 0, t1, t2, final, a, c1, c2, c3; + cin >> t1 >> t2 >> final >> a >> c1 >> c2 >> c3; + count = t1 + t2 + final + a; + int mini = min(c1, min(c2, c3)); + count += (c1+c2+c3-mini)/2; + printf("Case %d: %c\n", i, grade(count)); + } + return 0; +} diff --git a/uva_cpp_clean/11777/11777-21.cpp b/uva_cpp_clean/11777/11777-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19fc78cb77724091a8594fa49e456ffc0778ef13 --- /dev/null +++ b/uva_cpp_clean/11777/11777-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11777 + Name: Automate the Grades + Problem: https://onlinejudge.org/external/117/11777.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int T, g[10]; + cin>>T; + for (int cse=1; cse<=T; cse++) { + for (int i=0; i<7; i++) + cin>>g[i]; + sort(g+4, g+7); + double r = g[0]+g[1]+g[2]+g[3]+(g[5]+g[6])/2.0; + printf("Case %d: ", cse); + if (r>=90) cout << "A\n"; + else if (r>=80) cout << "B\n"; + else if (r>=70) cout << "C\n"; + else if (r>=60) cout << "D\n"; + else cout << "F\n"; + } +} diff --git a/uva_cpp_clean/11777/11777-5.cpp b/uva_cpp_clean/11777/11777-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d1cabe19aa795a6df3911d8b7a33ab240379aa5b --- /dev/null +++ b/uva_cpp_clean/11777/11777-5.cpp @@ -0,0 +1,30 @@ +#include +int main() +{ + int t1,t2,f,at,ct1,ct2,ct3,c,d,n,i; + while(scanf("%d",&n)==1) + { + for(i=1;i<=n;i++) + { + scanf("%d%d%d%d%d%d%d",&t1,&t2,&f,&at,&ct1,&ct2,&ct3); + if(ct1<=ct2 && ct1<=ct3) + c=(ct2+ct3)/2; + else if(ct2<=ct1 && ct2<=ct3) + c=(ct1+ct3)/2; + else if(ct3<=ct1 && ct3<=ct2) + c=(ct1+ct2)/2; + d=t1+t2+f+at+c; + if(d>=90) + printf("Case %d: A\n",i); + else if(d>=80) + printf("Case %d: B\n",i); + else if(d>=70) + printf("Case %d: C\n",i); + else if(d>=60) + printf("Case %d: D\n",i); + else if(d<60) + printf("Case %d: F\n",i); + } + } + return 0; +} diff --git a/uva_cpp_clean/11777/11777-9.cpp b/uva_cpp_clean/11777/11777-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8022e8793b1497f2184e54a72964400d9cbe7a2a --- /dev/null +++ b/uva_cpp_clean/11777/11777-9.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int tc, t1, t2, f, a, c[5], total; + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d %d %d %d %d %d %d", &t1, &t2, &f, &a, &c[0], &c[1], &c[2]); + sort(c, c + 3); + total = t1 + t2 + f + a + (c[1] + c[2]) / 2; + printf("Case %d: ", t); + if(total >= 90) puts("A"); + else if(total >= 80) puts("B"); + else if(total >= 70) puts("C"); + else if(total >= 60) puts("D"); + else puts("F"); + } + return(0); +} diff --git a/uva_cpp_clean/11780/11780-9.cpp b/uva_cpp_clean/11780/11780-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81e5c086f6b3db0cb8bb71f93e964c31b4ba1e8b --- /dev/null +++ b/uva_cpp_clean/11780/11780-9.cpp @@ -0,0 +1,34 @@ +#include + +#define LIM 17 + +using namespace std; + +int n, tmp; +vector fibo; +float km, ans; + +int main(){ + fibo.push_back(0); + fibo.push_back(1); + for(int i = 2; i < LIM; i++) + fibo.push_back(fibo[i - 1] + fibo[i - 2]); + while(scanf("%d", &n), n){ + ans = 0; + km = 1.6 * n; + while(n){ + tmp = fibo[lower_bound(fibo.begin(), fibo.end(), n + 1) - fibo.begin() - 1]; + ans += ceil(1.6 * tmp); + n -= tmp; + } + ans = abs(km - ans); + while(ans > 0.5) + ans -= 1; + if(ans == 0.60) + ans = 1 - ans; + else if(ans == 0.80) + ans = 1 - ans; + printf("%.2f\n", abs(ans)); + } + return(0); +} diff --git a/uva_cpp_clean/11782/11782-21.cpp b/uva_cpp_clean/11782/11782-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c457bb313dedc1da14fd11338c63de262eb3b9ab --- /dev/null +++ b/uva_cpp_clean/11782/11782-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11782 + Name: Optimal Cut + Problem: https://onlinejudge.org/external/117/11782.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) return EOF; + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +// ---------------------------------------------------------------- + +int DP[2100043][23], h, n; +int dfs(int id, int depth) { + int val = readInt(); + if (depth < h) + dfs(2*id+1, depth+1), + dfs(2*id+2, depth+1); + + for (int i=1; i<=n; ++i) { + DP[id][i] = val; + if (depth < h) + for (int j=1; j= 0) { + n = readInt(); + cout << dfs(0, 0) << endl; + } +} diff --git a/uva_cpp_clean/11787/11787-14.cpp b/uva_cpp_clean/11787/11787-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36a3120f386199384a299af1dbbe27ce6cc0a143 --- /dev/null +++ b/uva_cpp_clean/11787/11787-14.cpp @@ -0,0 +1,106 @@ +/*************************************************** + * Problem name : 11787 Numeral Hieroglyphs.cpp + * Problem Link : https://uva.onlinejudge.org/external/117/11787.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-26 + * Problem Type : ADHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +map mp, chk; +void pre() { + mp['B'] = 1 , mp['U'] = 10, mp['S'] = 100, mp['P'] = 1000, mp['F'] = 10000, mp['T'] = 100000, mp['M'] = 1000000; +} +int main () { + pre(); + int n; + string str; + scanf("%d", &n); + for (int q = 1; q <= n; q++) { + cin >> str; + ll ans = 0; + int sz = str.size(), flag = 0, ck = 0; + ll a = mp[str[0]]; + for (int i = 1; i < sz; i++) { + if (mp[str[i]] > a && ck == 0) { + ck = 1; + } else if (mp[str[i]] < a && ck == 0) { + ck = 2; + } + if (ck == 1) { + if (mp[str[i]] < a) { + flag = 1; + break; + } else a = mp[str[i]]; + } else if (ck == 2) { + if (mp[str[i]] > a) { + flag = 1; + break; + } else a = mp[str[i]]; + } + } + if (flag) printf("error\n"); + else { + flag = 0; + for (int i = 0; i < sz; i++) { + ans += mp[str[i]]; + chk[str[i]]++; + if (chk[str[i]] > 9) { + flag = 1; + break; + } + } + if (flag) printf("error\n"); + else printf("%lld\n", ans); + } + chk.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/11787/11787-9.cpp b/uva_cpp_clean/11787/11787-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..817128b6b4bccb8c32c03abe4d85adeae7c1984d --- /dev/null +++ b/uva_cpp_clean/11787/11787-9.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +const int LEN = 510; + +int tc, ans, pre, cur; +char line[LEN]; +map mp, ct; +string s = "MTFPSUB"; +bool flag, pos, neg; + +int main(){ + mp['B'] = 1, mp['U'] = 2, mp['S'] = 3, mp['P'] = 4; + mp['F'] = 5, mp['T'] = 6, mp['M'] = 7; + scanf("%d\n", &tc); + while(tc--){ + flag = pos = neg = false; + ct.clear(); + scanf("%s", line); + for(int it = 0; line[it] and not flag; it++){ + ct[line[it]]++; + if( ct[line[it]] == 10 ){ + puts("error"); + flag = true; + } + } + if( flag ) continue; + pre = mp[line[0]]; + for(int it = 1; line[it]; it++){ + cur = mp[line[it]]; + if( cur - pre > 0 ) pos = true; + if( cur - pre < 0 ) neg = true; + pre = cur; + } + if( pos and neg ){ + puts("error"); + continue; + } + ans = 0; + for(int it = 0; it < s.size(); it++) + ans = 10 * ans + ct[s[it]]; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11790/11790-13.cpp b/uva_cpp_clean/11790/11790-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05844ea12c1d0a296cbc67482da42ff5c09d45fe --- /dev/null +++ b/uva_cpp_clean/11790/11790-13.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 100000 + +void show(int c, int inc, int dec) +{ + if (inc >= dec) + { + cout << "Case " << c << ". Increasing (" << inc << "). Decreasing (" << dec << ")." << endl; + } + + else + { + cout << "Case " << c << ". Decreasing (" << dec << "). Increasing (" << inc << ")." << endl; + } +} + +int main() +{ + int t, n, c = 1; + + cin >> t; + + while (t--) + { + cin >> n; + + int a[2][MAX]; + + int LIS[MAX]; + int LDS[MAX]; + + memset(LIS, 0, sizeof LIS); + memset(LDS, 0, sizeof LDS); + memset(a, 0, sizeof a); + + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < n; j++) + { + cin >> a[i][j]; + } + } + + for (int i = 0; i < n; i++) + { + LIS[i] = LDS[i] = a[1][i]; + + for (int j = 0; j < i; j++) + { + if (a[0][i] > a[0][j]) + { + LIS[i] = max(LIS[j] + a[1][i], LIS[i]); + } + + if (a[0][i] < a[0][j]) + { + LDS[i] = max(LDS[j] + a[1][i], LDS[i]); + } + } + } + + int inc = 0, dec = 0; + + for (int i = 0; i < n; i++) + { + inc = max(inc, LIS[i]); + dec = max(dec, LDS[i]); + } + + show(c++, inc, dec); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11790/11790-21.cpp b/uva_cpp_clean/11790/11790-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..442137ba601a31c01ddc96c99ef68ea52162a458 --- /dev/null +++ b/uva_cpp_clean/11790/11790-21.cpp @@ -0,0 +1,101 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11790 + Name: Murcia's Skyline + Problem: https://onlinejudge.org/external/117/11790.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ----------------------------------------------------------------------- + +int M[1000043], H[10043], W[10043], n; +int LIS(bool comp(int, int)) { + int L = 0; + for (int i=0; i> 1; + if (comp(H[M[mid]], H[i])) + lo = mid + 1; + else + hi = mid - 1; + } + + for (int j=0; j b; } + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt();; + for (int cse=1; cse<=T; ++cse) { + n = readUInt(); + for (int i=0; i= decr) { + cout << " Increasing (" << incr << ")."; + cout << " Decreasing (" << decr << ")."; + } + else { + cout << " Decreasing (" << decr << ")."; + cout << " Increasing (" << incr << ")."; + } + cout << "\n"; + } +} diff --git a/uva_cpp_clean/11790/11790-5.cpp b/uva_cpp_clean/11790/11790-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..22886826befc38b15eed0f94de46be3197a7deaf --- /dev/null +++ b/uva_cpp_clean/11790/11790-5.cpp @@ -0,0 +1,139 @@ +/****************************************************************** +*** Problewm : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d. ",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define CP(a) cout<=m2) + { + Case(no); + cout<<"Increasing ("< + +#define rs resize + +using namespace std; + +int tc, n, ans_lis, ans_lds, ans_wlis, ans_wlds; +vector v, w, wlis, wlds; + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d", &n); + v.rs(n), w.rs(n), wlis.rs(n), wlds.rs(n); + for(int i = 0; i < n; i++) scanf("%d", &v[i]); + for(int i = 0; i < n; i++) scanf("%d", &w[i]); + ans_wlis = ans_wlds = 0; + for(int i = 0; i < n; i++){ + wlis[i] = wlds[i] = w[i]; + for(int j = 0; j < i; j++){ + if(v[i] > v[j] && wlis[i] < wlis[j] + w[i]) + wlis[i] = wlis[j] + w[i]; + if(v[i] < v[j] && wlds[i] < wlds[j] + w[i]) + wlds[i] = wlds[j] + w[i]; + } + if(wlis[i] > ans_wlis) ans_wlis = wlis[i]; + if(wlds[i] > ans_wlds) ans_wlds = wlds[i]; + } + if(ans_wlis >= ans_wlds) + printf("Case %d. Increasing (%d). Decreasing (%d).\n", t, ans_wlis, ans_wlds); + else + printf("Case %d. Decreasing (%d). Increasing (%d).\n", t, ans_wlds, ans_wlis); + } + return(0); +} diff --git a/uva_cpp_clean/11792/11792-21.cpp b/uva_cpp_clean/11792/11792-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bbcbe883d819075d72926118eabf61ee4c056c9 --- /dev/null +++ b/uva_cpp_clean/11792/11792-21.cpp @@ -0,0 +1,115 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11792 + Name: Krochanska is Here! + Problem: https://onlinejudge.org/external/117/11792.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define INF 1073741824 + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ----------------------------------------------------------- +char D[10017], ID[10017]; +unsigned int UID[117], M[117][117]; +int main() { + vector X[101]; + for (int i=0; i<101; ++i) + X[i].reserve(1000); + + int T = readUInt(); + while (T--) { + int n = readUInt(), + r = readUInt(); + + int m = 0; + memset(D, 0, n+1); + for (int i=0; i 1) { + int v = ID[j]; + if (l!=-1 && k-l +#include +#include + +using namespace std; + +int main() +{ + int test, counter = 1; + + cin >> test; + + while (test--) + { + int how, m = 0; + + cin >> how; + + int * a = new int[how]; + + for (int i = 0; i < how; i++) + { + cin >> a[i]; + + m = max(m, a[i]); + } + + cout << "Case " << counter << ": " << m << endl; + + counter++; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11799/11799-19.cpp b/uva_cpp_clean/11799/11799-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57c9c7091527eab2c8b663a32e2e0ce3e34aed1f --- /dev/null +++ b/uva_cpp_clean/11799/11799-19.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 1; i <= cases; i++) { + int N, max = 0, s; + scanf("%d", &N); + for (int j = 0; j < N; j++) { + scanf("%d", &s); + if (s > max) max = s; + } + printf("Case %d: %d\n", i, max); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11799/11799-21.cpp b/uva_cpp_clean/11799/11799-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac8025f2727034cfbd9c7c01068cfac56db06519 --- /dev/null +++ b/uva_cpp_clean/11799/11799-21.cpp @@ -0,0 +1,30 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11799 + Name: Horror Dash + Problem: https://onlinejudge.org/external/117/11799.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int C; + cin >> C; + for (int c = 1; c <= C; c++) { + int n, x; cin >> n; + int max = 0; + while (n--) { + cin >> x; + if (x > max) max = x; + } + + printf("Case %d: %d\n", c, max); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11799/11799-3.cpp b/uva_cpp_clean/11799/11799-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0870d34bd9f826ae6cd033e3ca3204dada2ffe3 --- /dev/null +++ b/uva_cpp_clean/11799/11799-3.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +int main(){ + // Input : Line 1 : N (number of cases) + // N lines : L (number of children taking part) + // speed (childs speed) + int N, L, speed; + // However, we set best as the fastest kid among the children, cause his speed the clown must maintain + int best; + + scanf("%d",&N); + for ( int l = 0; l < N; l++){ + scanf ("%d", &L); + best = 0; + for ( int i = 0; i < L; i++){ + scanf("%d", &speed); + best = max (best,speed); + } + printf ("Case %d: %d\n", l+1, best); + } +} diff --git a/uva_cpp_clean/11799/11799-5.cpp b/uva_cpp_clean/11799/11799-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..10f2cfef2e4e76644068395c4dd80f4c57279769 --- /dev/null +++ b/uva_cpp_clean/11799/11799-5.cpp @@ -0,0 +1,21 @@ +#include +int main() +{ + int t,n,c,k,i,max; + while (scanf("%d",&t)==1) + { + for(i=1;i<=t;i++) + { + max=0; + scanf("%d",&n); + for (k=0;kmax) + max=c; + } + printf("Case %d: %ld\n",i,max); + } + } +return 0; +} diff --git a/uva_cpp_clean/11799/11799-6.cpp b/uva_cpp_clean/11799/11799-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ab46cbc2c64ed576ca697091107a8b14c7dab8e --- /dev/null +++ b/uva_cpp_clean/11799/11799-6.cpp @@ -0,0 +1,23 @@ +/* + ad-hoc + difficulty: easy + date: 17/Feb/2020 + by: @brpapa +*/ +#include +#define INF (int)0x7f7f7f7f +using namespace std; + +int main() { + int T; cin >> T; + for (int t = 1; t <= T; t++) { + int N; cin >> N; + int ans = -INF; + while (N--) { + int c; cin >> c; + ans = max(ans, c); + } + printf("Case %d: %d\n", t, ans); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11799/11799-9.cpp b/uva_cpp_clean/11799/11799-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c040e516169ac657ac2d8a5b6a41954478c49adc --- /dev/null +++ b/uva_cpp_clean/11799/11799-9.cpp @@ -0,0 +1,16 @@ +#include + +int main(){ + int N, i, a[10000], n, max, t = 1; + scanf("%d", &N); + while(N--){ + scanf("%d", &n); + for(i = 0; i < n; i++) scanf("%d",&a[i]); + max = a[0]; + for(i = 1; i < n; i++) + if(a[i] > max) max = a[i]; + printf("Case %d: %d\n", t, max); + t++; + } + return(0); +} diff --git a/uva_cpp_clean/11799/main.cpp b/uva_cpp_clean/11799/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0211f9f6b15a183d61fac058d96633f1b472e0f8 --- /dev/null +++ b/uva_cpp_clean/11799/main.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +int main(){ + short casos, n, siguiente, mayor; + cin >> casos; + for(short i = 1; i <= casos; i++){ + cin >> n; + cin >> mayor; + for(short c=1; c < n; c++){ + cin >> siguiente; + mayor = max(mayor, siguiente); + } + cout << "Case " << i << ": " << mayor << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/118/118-15.cpp b/uva_cpp_clean/118/118-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0655353b66b6ef1d3d9b069a3df3af9ec58593d --- /dev/null +++ b/uva_cpp_clean/118/118-15.cpp @@ -0,0 +1,62 @@ +/* + 模擬題,照做就好 +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + // 紀錄曾經掉下去的地方 + int maps[55][55] = {}; + // 四個方向 + int position[4][2] = {0, 1, + 1, 0, + 0, -1, + -1, 0}; + + int i, n, m, x, x_next, y, y_next, towards[128]; + char start, steps[101], face[5] = "NESW"; + + towards['N'] = 0; + towards['E'] = 1; + towards['S'] = 2; + towards['W'] = 3; + + cin >> n >> m; + while (cin >> x >> y >> start) { + cin >> steps; + int face_now = towards[start], flag = 0; + // 模擬移動 + for (i = 0; steps[i]; i++) { + // 左轉 + if (steps[i] == 'L') + face_now = (face_now + 3) % 4; + // 右轉 + else if (steps[i] == 'R') + face_now = (face_now + 1) % 4; + // 往前走 + else { + x_next = x + position[face_now][0]; + y_next = y + position[face_now][1]; + // 判斷這裡之前是否有機器人掉下去過了 + if (x_next < 0 || x_next > n || y_next < 0 || y_next > m) { + if (maps[x][y]) + continue; + maps[x][y] = 1; + flag = 1; + break; + } + x = x_next; + y = y_next; + } + } + + cout << x << ' ' << y << ' ' << face[face_now]; + if (flag) + cout << " LOST"; + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/118/118-19.cpp b/uva_cpp_clean/118/118-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54e53449bba180b46e076cb68b0fc2265a5e5089 --- /dev/null +++ b/uva_cpp_clean/118/118-19.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + int x, y; //Current position; + char dir; + int dircode; // N 0 E 1 S 2 W 3 + bool grid[52][52]; + int maxx, maxy; //Included + scanf("%d %d\n", &maxx, &maxy); + for (int i = 0; i <= maxx; i++) + for (int j = 0; j <= maxy; j++) + grid[i][j] = false; + while (scanf("%d %d %c\n", &x, &y, &dir) != EOF) { + dircode = dir == 'N'? 0: dir == 'E'? 1: dir == 'S'? 2: 3; + char str[102]; + bool lost = false; + scanf("%s", str); + int i = 0; + while (true) { + if (str[i] == 'R') dircode = (dircode+1)%4; + else if (str[i] == 'L') dircode = (dircode+4-1)%4; + else if (str[i] == 'F'){ + if (!(x == maxx && dircode == 1) && !(y == maxy && dircode == 0) && !(x == 0 && dircode == 3) && !(y == 0 && dircode == 2)) { + switch(dircode){ + case 0: y+=1; break; + case 1: x+=1; break; + case 2: y-=1; break; + case 3: x-=1; break; + } + } + else if (!grid[x][y]) { + lost = true; + grid[x][y] = true; + } + } + else break; + if (lost) break; + i++; + } + printf("%d %d %c", x, y, (dircode == 0? 'N': dircode == 1? 'E': dircode == 2? 'S': 'W')); + if (lost) printf(" LOST"); + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/118/118-21.cpp b/uva_cpp_clean/118/118-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88873e3a4826131cbcf812ae2c52ff5e4c02c25d --- /dev/null +++ b/uva_cpp_clean/118/118-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 118 + Name: Mutant Flatworld Explorers + Problem: https://onlinejudge.org/external/1/118.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +bool scent[1000][1000]; +char dirs[] = "NESW", L[90], R[90]; + +int main(){ + for (int i=0; i<4; i++) { + L[dirs[i]] = dirs[(4+i-1)%4]; + R[dirs[i]] = dirs[(i+1)%4]; + } + + + int maxx, maxy; + cin >> maxx >> maxy; + + int x, y; + char insts[300], cmd, d; + while (cin >> x >> y >> d) { + if (d>='a') d-=32; + bool fall = false; + cin >> insts; + for (int i=0; !fall && (cmd = insts[i]); i++) + if (cmd=='L' || cmd=='l') d=L[d]; + else if (cmd=='R' || cmd=='r') d=R[d]; + else if (d=='N') + if (y0) y--; + else { if(!scent[x][y]) fall=true; } + else if (d=='W') + if (x>0) x--; + else { if(!scent[x][y]) fall=true; } + + printf("%d %d %c", x, y, d); + if (fall) { + cout << " LOST\n"; + scent[x][y] = true; + } + else cout << endl; + } +} diff --git a/uva_cpp_clean/118/118-6.cc b/uva_cpp_clean/118/118-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..477f8343b03469033e8d5132786cd76aacc67b25 --- /dev/null +++ b/uva_cpp_clean/118/118-6.cc @@ -0,0 +1,61 @@ +// https://uva.onlinejudge.org/external/1/118.pdf +#include +using namespace std; +using vb=vector; +using vvb=vector; +int main(){ + int X,Y,x,y; + char o; + string s; + cin>>X>>Y; + vvb a(X+1, vb(Y+1)); + while(cin>>x>>y>>o>>s){ + bool l=0; + for(auto c:s){ + if(c=='R'){ + if(o=='N')o='E'; + else if(o=='E')o='S'; + else if(o=='S')o='W'; + else o='N'; + }else if(c=='L'){ + if(o=='N')o='W'; + else if(o=='E')o='N'; + else if(o=='S')o='E'; + else o='S'; + }else{ + if(o=='N'){ + if(y==Y){ + if(!a[x][y]){ + a[x][y]=1; + l=1; + break; + } + }else y++; + }else if(o=='E'){ + if(x==X){ + if(!a[x][y]){ + a[x][y]=1; + l=1; + break; + } + }else x++; + }else if(o=='S'){ + if(y==0){ + if(!a[x][y]){ + l=1; + break; + } + }else y--; + }else{ + if(x==0){ + if(!a[x][y]){ + l=1; + break; + } + }else x--; + } + } + } + printf("%d %d %c%s\n",x,y,o,l?" LOST":""); + } +} diff --git a/uva_cpp_clean/1180/1180-20.cpp b/uva_cpp_clean/1180/1180-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..020920bb23e7aa429504ba0c9b6149ac3a693c69 --- /dev/null +++ b/uva_cpp_clean/1180/1180-20.cpp @@ -0,0 +1,47 @@ +#include +#include + +using namespace std; + +int prime[] = {2, 3, 5, 7, 13, 17}; + +int bin_search(int num) +{ + for(int i=0 ; i<6 ; i++) + { + if( prime[i]==num ) + return 1; + } + return 0; +} + +int main() +{ + int num_P; + cin >> num_P; + + int flag[num_P]; + for(int i=0 ; i> P; + + if( bin_search(P)==1 ) + flag[i] = 1; + + getchar(); + } + + for(int i=0 ; i +#include + +int main() +{ + int n; + std::cin >> n; + + while (n--) + { + int p; + char t; + + std::cin >> p >> t; + + if (p == 2 || p == 3 || p == 5 || p == 7 || p == 13 || p == 17) + { + std::cout << "Yes" << std::endl; + } + else + { + std::cout << "No" << std::endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11804/11804-19.cpp b/uva_cpp_clean/11804/11804-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ead6a5b3f32cc8c00a7cb28ff73c5a8c0b192887 --- /dev/null +++ b/uva_cpp_clean/11804/11804-19.cpp @@ -0,0 +1,81 @@ +// las12uvas contest PROBLEM F 2015 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair iis; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector
vdd; +typedef vector vll; +typedef vector vvi; + +int main() { + vector vec; + vector v1, v2; + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + vec.clear(); + v1.clear(); + v2.clear(); + for(int j = 0; j < 10; j++){ + string name; + int at, df; + cin >> name >> at >> df; + at = -at; + vec.push_back(iis(ii(at, df), name)); + } + printf("Case %d:\n", i + 1); + sort(vec.begin(), vec.end()); + for (int j = 0; j < 5; j++) { + v1.push_back(vec[j].second); + } + for (int j = 0; j < 5; j++) { + v2.push_back(vec[j+5].second); + } + sort(v1.begin(), v1.end()); + sort(v2.begin(), v2.end()); + int j = 0; + cout << "(" << v1[j]; + j++; + for (; j < 5; j++) { + cout << ", " << v1[j]; + } + j = 0; + cout << ")\n(" << v2[j]; + j++; + for (; j < 5; j++) { + cout << ", " << v2[j]; + } + cout<< ")" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11804/11804-21.cpp b/uva_cpp_clean/11804/11804-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e3dc148fd44e97e308948ccbe090aa8d2819bfe --- /dev/null +++ b/uva_cpp_clean/11804/11804-21.cpp @@ -0,0 +1,78 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11804 + Name: Argentina + Problem: https://onlinejudge.org/external/118/11804.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct Person { + int a, d; + string name; + bool operator < (const Person &o) const { return name < o.name; } +} +P[10]; + + +int bsa, bsd; +bool chosen[10], bchosen[10]; +void bt(int idx, int ca, int sa, int sd) { + if (ca > 5 || idx-ca > 5) return; + if (idx == 10) { + if (sa > bsa || (sa == bsa && sd > bsd)) { + bsa = sa; + bsd = sd; + memcpy(bchosen, chosen, sizeof(chosen)); + } + return; + } + + chosen[idx] = 1; + bt(idx+1, ca+1, sa + P[idx].a, sd); + chosen[idx] = 0; + bt(idx+1, ca+0, sa, sd + P[idx].d); +} + + +void printNames(int ids[]) { + cout << '(' << P[ids[0]].name; + for (int i=1; i<5; ++i) + cout << ", " << P[ids[i]].name; + cout << ")\n"; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cout << "Case " << cse << ":\n"; + + for (int i=0; i<10; ++i) + cin >> P[i].name >> P[i].a >> P[i].d; + + sort(P, P+10); + bsa = bsd = -1; + bt(0, 0, 0, 0); + + int att[5], def[5], sum=0; + for (int i=0, a=0, d=0; i<10; ++i) + if (bchosen[i]) + att[a++] = i, + sum += P[i].a; + else + def[d++] = i; + + printNames(att); + printNames(def); + } +} diff --git a/uva_cpp_clean/11804/11804-9.cpp b/uva_cpp_clean/11804/11804-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6cb739c61d907bdd2e924f20c17747b2c850166 --- /dev/null +++ b/uva_cpp_clean/11804/11804-9.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +int tc, a, b, at, df, _max_at, _max_df; +string s; +vector < tuple > v(10); +vector u(5), w; +bool used[10]; + +void proof(int i, int j, int k, int l, int m){ + memset(used, false, sizeof used), df = 0; + at = get<1>(v[i]) + get<1>(v[j]) + get<1>(v[k]) + get<1>(v[l]) + get<1>(v[m]); + used[i] = used[j] = used[k] = used[l] = used[m] = true; + for(int i = 0; i < 10; i++) + if(!used[i]) df += get<2>(v[i]); + if(at > _max_at){ + _max_at = at, _max_df = df; + u[0] = i, u[1] = j, u[2] = k, u[3] = l, u[4] = m; + w.clear(); + for(int i = 0; i < 10; i++) + if(!used[i]) w.push_back(i); + } + else if(at == _max_at && df > _max_df){ + _max_df = df; + u[0] = i, u[1] = j, u[2] = k, u[3] = l, u[4] = m; + w.clear(); + for(int i = 0; i < 10; i++) + if(!used[i]) w.push_back(i); + } +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + getchar(), _max_at = _max_df = -1; + for(int i = 0; i < 10; i++) cin >> s >> a >> b, v[i] = make_tuple(s, a, b); + sort(v.begin(), v.end()); + for(int a = 0; a < 6; a++) + for(int b = a + 1; b < 7; b++) + for(int c = b + 1; c < 8; c++) + for(int d = c + 1; d < 9; d++) + for(int e = d + 1; e < 10; e++) + proof(a, b, c, d, e); + printf("Case %d:\n",t); + putchar('('); + for(int i = 0; i < 5; i++) + cout<< get<0>(v[u[i]]), printf(i!=4 ? ", " : ")\n"); + putchar('('); + for(int i = 0; i < 5; i++) + cout<< get<0>(v[w[i]]), printf(i!=4 ? ", " : ")\n"); + } + return(0); +} diff --git a/uva_cpp_clean/11805/11805-11.cpp b/uva_cpp_clean/11805/11805-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc909075eae8857859a85db8ff0485d0aa1e3736 --- /dev/null +++ b/uva_cpp_clean/11805/11805-11.cpp @@ -0,0 +1,55 @@ +#include +/* +Problem: 11805 - Bafana Bafana +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2905 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int T, N, K, P, ans; +int testcase = 0; + +int main () { + + scanf("%d", &T); + + while ( T-- ){ + scanf("%d%d%d", &N, &K, &P); + + ans = (K+P)%N; + + printf("Case %d: %d\n", ++testcase, (ans)? ans : N); + } + + return 0; +} +/* +Sample input:- +----------------- +3 +5 2 5 +6 3 5 +4 1 3 + +Sample output:- +----------------- +Case 1: 2 +Case 2: 2 +Case 3: 4 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11805/11805-19.cpp b/uva_cpp_clean/11805/11805-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19cb27e27ed4d0ea2054a6510a47175093bc7d16 --- /dev/null +++ b/uva_cpp_clean/11805/11805-19.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 1; i <= cases; i++) { + int N, K, P; + scanf("%d %d %d", &N, &K, &P); + int r = (K+P)%N; + printf("Case %d: %d\n", i, r == 0? N: r); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11805/11805-21.cpp b/uva_cpp_clean/11805/11805-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df452f42230b1a4cc0b7794d51ea029b83e44cea --- /dev/null +++ b/uva_cpp_clean/11805/11805-21.cpp @@ -0,0 +1,26 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11805 + Name: Bafana Bafana + Problem: https://onlinejudge.org/external/118/11805.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int t; + cin>>t; + for(int cse=1; cse<=t; cse++) { + int n,k,p,r; + cin>>n>>k>>p; + r=(k+p)%n; + printf("Case %d: %d\n", cse, r ? r : n); + } +} diff --git a/uva_cpp_clean/11805/11805-5.cpp b/uva_cpp_clean/11805/11805-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b8306df0b9c1b1f5f68eb96de73c9dc0f77849bc --- /dev/null +++ b/uva_cpp_clean/11805/11805-5.cpp @@ -0,0 +1,21 @@ +#include +int main() +{ + int n,k,p,a,s,i; + while(scanf("%d",&a)==1) + { + for(i=1;i<=a;i++) + { + scanf("%d%d%d",&n,&k,&p); + s=k+p; + while(s>n) + { + s=s-n; + } + printf("Case %d: %d\n",i,s); + } + } + return 0; +} + + diff --git a/uva_cpp_clean/11805/11805-9.cpp b/uva_cpp_clean/11805/11805-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..275d4c192be070875cb2bb4b5936c9cd11534f94 --- /dev/null +++ b/uva_cpp_clean/11805/11805-9.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main(){ + int c, n, k, p, ans, t = 0; + scanf("%d", &c); + while(c--){ + scanf("%d %d %d", &n, &k, &p); + ans = k + p % n; + if(ans > n) + ans %= n; + printf("Case %d: %d\n", ++t, ans); + } + return(0); +} diff --git a/uva_cpp_clean/11824/11824-21.cpp b/uva_cpp_clean/11824/11824-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f928984c1ad85dcc3ce662f7297720cb5a3c893 --- /dev/null +++ b/uva_cpp_clean/11824/11824-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11824 + Name: A Minimum Land Price + Problem: https://onlinejudge.org/external/118/11824.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + int T, n; + cin >> T; + while (T--) { + priority_queue q; + while (cin>>n && n) + q.push(n); + + int sum = 0; + for (int i=1; !q.empty(); ++i) { + sum += 2 * pow(q.top(), i); + if (sum > 5000000) break; + q.pop(); + } + + if (q.empty()) printf("%d\n", sum); + else puts("Too expensive"); + } +} diff --git a/uva_cpp_clean/11824/11824-9.cpp b/uva_cpp_clean/11824/11824-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6375b8b3e44d00b12761d8ef34dbb7531245fb3a --- /dev/null +++ b/uva_cpp_clean/11824/11824-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int tc, n, tmp, price; +vector v; +bool ok; + +int main(){ + scanf("%d", &tc); + while(tc--){ + v.clear(), ok = true; + while(scanf("%d",&tmp), tmp) v.push_back(tmp); + sort(v.rbegin(), v.rend()); + price = 0; + for(int i = 0; i < v.size(); i++){ + price += 2 * pow(v[i], i + 1); + if(price >= 5000000) ok = false, i = v.size(); + } + if(ok) printf("%d\n", price); + else puts("Too expensive"); + } + + return(0); +} diff --git a/uva_cpp_clean/11827/11827-13.cpp b/uva_cpp_clean/11827/11827-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbd4ac02bd4d69d4671c602c6c8792a59b668054 --- /dev/null +++ b/uva_cpp_clean/11827/11827-13.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int gcd(int a, int b) +{ + if (b == 0) return a; + + else return gcd(b, a % b); +} + +int main() +{ + string s; + + int t, n, a[99], ans = 0; + + getline(cin, s); + istringstream is(s); + + is >> t; + + while (t--) + { + getline(cin, s); + istringstream is(s); + + ans = n = 0; + + while (is >> a[n]) ++n; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i != j) + { + ans = max(ans, gcd(max(a[i], a[j]), min(a[i], a[j]))); + } + } + } + + cout << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11827/11827-19.cpp b/uva_cpp_clean/11827/11827-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9425d95c8a0eb4d01d3c44820aae37727370eba1 --- /dev/null +++ b/uva_cpp_clean/11827/11827-19.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +int main() { + int N, val[101]; + cin >> N; + string s; + getline(cin, s); + while (N--) { + getline(cin, s); + stringstream ss(s); + int n = 0, maxi = 1; + while (ss >> val[n]) n++; + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + maxi = max(ll(maxi), gcd(val[i], val[j])); + } + } + printf("%d\n", maxi); + } + return 0; +} diff --git a/uva_cpp_clean/11827/11827-20.cpp b/uva_cpp_clean/11827/11827-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..480823aae08c77dad9968375f2a5c7423a0d3137 --- /dev/null +++ b/uva_cpp_clean/11827/11827-20.cpp @@ -0,0 +1,45 @@ +#include + +int Int[102]; +char dump, M[12]; + +int GCD(int a, int b) +{ + while( b ) b ^= a ^= b ^= a %= b; + return (a); +} + +int main() +{ + int tc; + scanf("%d%c", &tc, &dump); + while( tc-- ) + { + gets(M); + + for(int i=0 ; i<102 ; i++) Int[i] = 0; + + int pos = 0, len = 0; + while( M[pos] ) + { + while( M[pos] && M[pos]!=' ' ) + { + Int[len] = (Int[len]*10) + (M[pos]-'0'); + pos++; + } + while( M[pos]==' ' ) pos++; + len++; + } + + int maxGCD = 1; + for(int i=0 ; imaxGCD ) maxGCD = temp; + } + + printf("%d\n", maxGCD); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11827/11827-21.cpp b/uva_cpp_clean/11827/11827-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d85e463077cb843383132683fd97ada7b358f35f --- /dev/null +++ b/uva_cpp_clean/11827/11827-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11827 + Name: Maximum GCD + Problem: https://onlinejudge.org/external/118/11827.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXL 100000 + + +int gcd(int a, int b) { + if (b==0) return a; + return gcd(b, a%b); +} + +char line[MAXL]; +int x[101]; + +int main(){ + int T; + cin >> T; + cin.getline(line, 100000); + while (T--) { + cin.getline(line, 100000); + stringstream sin(line); + + int mx=1; + for (int i=0; sin>>x[i]; i++) + for (int j=0; j mx) mx = g; + } + + cout << mx << endl; + } +} diff --git a/uva_cpp_clean/11827/11827-9.cpp b/uva_cpp_clean/11827/11827-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1f237606f009fbb7c4c84a72ca68e21ab1d94e0 --- /dev/null +++ b/uva_cpp_clean/11827/11827-9.cpp @@ -0,0 +1,33 @@ +#include + +#define SIZE 110 +#define LIM 1010 + +using namespace std; + + +int tc, v[SIZE], ct, ans; +char line[LIM], *p; + +int gcd(int a,int b){ + return b == 0 ? a : gcd(b, a % b); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + ct = 0; + cin.getline(line, LIM); + p = strtok(line, " "); + while(p){ + v[ct++] = atoi(p); + p = strtok(NULL, " "); + } + ans = -1; + for(int it1 = 0; it1 < ct; it1++) + for(int it2 = it1 + 1; it2 < ct; it2++) + ans = max(ans, gcd(v[it1], v[it2])); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11830/11830-19.cpp b/uva_cpp_clean/11830/11830-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c12dccb33b6c649c12764bfc279a4a07623a381 --- /dev/null +++ b/uva_cpp_clean/11830/11830-19.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +int main() { + char D; + string N; + queue q; + while (cin >> D >> N && !(D == '0' && N == "0")) { + bool leading = true; + for (int i = 0; i < N.length(); i++) { + if (N[i] != D) { + if (!(leading && N[i] == '0')) { + leading = false; + q.push(N[i]); + } + } + } + if (q.empty()) printf("0"); + while (!q.empty()) { + printf("%c", q.front()); + q.pop(); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11830/11830-21.cpp b/uva_cpp_clean/11830/11830-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e61af915d16f0e9baeafc5051ac3f5f16641b931 --- /dev/null +++ b/uva_cpp_clean/11830/11830-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11830 + Name: Contract Revision + Problem: https://onlinejudge.org/external/118/11830.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +int main() { + char s[1000], d; + while (scanf(" %c %s", &d, s)==2 && d!='0') { + int j=0; + for (int i=0; s[i]; i++) + if (s[i] != d) + s[j++] = s[i]; + s[j] = 0; + + int i=0; + while (s[i] == '0') ++i; + if (i < j) puts(s+i); + else puts("0"); + } +} diff --git a/uva_cpp_clean/11831/11831-13.cpp b/uva_cpp_clean/11831/11831-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5dd560676744c70329a2d5eed641909d29716d3 --- /dev/null +++ b/uva_cpp_clean/11831/11831-13.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +ofstream fout("f.out"); + +int main() +{ + int n, m, intro; + + while (cin >> n >> m >> intro, n != 0 && m != 0 && intro != 0) + { + int x = 0, y = 0; + bool found = false; + + int orientation, how = 0; + + string s, go; + + vector land; + + for (int i = 0; i < n; i++) + { + cin >> s; + + land.push_back(s); + } + + cin >> go; + + for (int i = 0; i < n && !found; i++) + { + for (int j = 0; j < m && !found; j++) + { + switch (land[i][j]) + { + case 'N': + orientation = 0; + x = i; + y = j; + found = true; + break; + + case 'S': + orientation = 2; + x = i; + y = j; + found = true; + break; + + case 'L': + orientation = 1; + x = i; + y = j; + found = true; + break; + + case 'O': + orientation = 3; + x = i; + y = j; + found = true; + break; + + default : + break; + } + } + } + + for (int i = 0; i < intro; i++) + { + switch (go[i]) + { + case 'D': + orientation = (orientation + 1) % 4; + break; + + case 'E': + orientation = ((orientation - 1) < 0) ? 3 : orientation - 1; + break; + + case 'F': + + switch (orientation) + { + case 0: + if (x - 1 >= 0 && land[x - 1][y] != '#') x--; + break; + + case 1: + if (y + 1 < m && land[x][y + 1] != '#') y++; + break; + + case 2: + if (x + 1 < n && land[x + 1][y] != '#') x++; + break; + + case 3: + if (y - 1 >= 0 && land[x][y - 1] != '#') y--; + break; + } + + if (land[x][y] == '*') + { + how++; + + land[x][y] = '.'; + } + break; + } + } + + cout << how << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11831/11831-21.cpp b/uva_cpp_clean/11831/11831-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2433515fbe5f8617710c079b3944788ee1d9693 --- /dev/null +++ b/uva_cpp_clean/11831/11831-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11831 + Name: Sticker Collector Robot + Problem: https://onlinejudge.org/external/118/11831.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +char mat[110][110]; +int DD[127], EE[127], n,m,s,cx,cy,ncx,ncy; +int main(){ + char dirs[] = "NLSO", dir; + for (int i=0; i<4; i++) { + DD[dirs[i]] = dirs[(i+1)%4]; + EE[dirs[i]] = dirs[(i+3)%4]; + } + + while (cin>>n>>m>>s && (n||m||s)) { + while (getchar() != '\n'); + + for (int i=0; i=0 && ncx=0 && ncy traversal + difficulty: easy + date: 27/Feb/2020 + hint: consider grid as an implicit graph and walk through it, or just rotate the robot, for each received instruction + by: @brpapa +*/ +#include +using namespace std; + +int R, C, I; +char grid[110][110]; +char instructions[5*10010]; + +char orientations[4] = {'N', 'L', 'S', 'O'}; // -> right +int dr[4] = {-1, 0, 1, 0}; +int dc[4] = { 0, 1, 0,-1}; + +int walk(int i, int rr, int rc, int ro) { + // instructions[i] = received instruction + // grid[rr][rc] = robot position + // orientations[ro] = robot orientation + + if (i == I) return 0; + + if (instructions[i] == 'D') return walk(i+1, rr, rc, (ro+1) % 4); + if (instructions[i] == 'E') return walk(i+1, rr, rc, ro == 0? 3: ro-1); + + // robo se movimenta para onde está orientado + int nrr = rr+dr[ro], nrc = rc+dc[ro]; + + // new robot row or ner robot column is invalid + if (nrr < 0 || nrr == R || nrc < 0 || nrc == C || grid[nrr][nrc] == '#') + return walk(i+1, rr, rc, ro); + + if (grid[nrr][nrc] == '*') { + grid[nrr][nrc] = '.'; // evita nova contagem no futuro + return 1 + walk(i+1, nrr, nrc, ro); + } + + return walk(i+1, nrr, nrc, ro); +} + +int main() { + while (cin >> R >> C >> I && (R || C || I)) { + int rr, rc, ro = -1; + + for (int r = 0; r < R; r++) + for (int c = 0; c < C; c++) { + cin >> grid[r][c]; + + for (int o = 0; o < 4 && ro == -1; o++) + if (grid[r][c] == orientations[o]) { + rr = r; rc = c; ro = o; + grid[r][c] = '.'; + } + } + + for (int i = 0; i < I; i++) cin >> instructions[i]; + + cout << walk(0, rr, rc, ro) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11832/11832-21.cpp b/uva_cpp_clean/11832/11832-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f63d0b6948971a4646c11b3fb37ae4ef7a3d2bed --- /dev/null +++ b/uva_cpp_clean/11832/11832-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11832 + Name: Account Book + Problem: https://onlinejudge.org/external/118/11832.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char chh[] = "*+-?"; +bool dp[44][88000]; +int X[44], R[44], C[44], seen[44][88000], cse, n; +bool dfs(int idx, int f) { + if (C[idx] < abs(f)) return 0; + if (idx == n) return !f; + if (seen[idx][f+44000] == cse) + return dp[idx][f+44000]; + + bool r1 = dfs(idx+1, f - X[idx]), + r2 = dfs(idx+1, f + X[idx]); + + R[idx] |= r2<<1 | r1; + + seen[idx][f+44000] = cse; + return dp[idx][f+44000] = r1 || r2; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int f; + for (cse=1; cin>>n>>f && (n||f); ++cse) { + for (int i=0; i> X[i]; + + C[n] = 0; + for (int i=n-1; i>=0; --i) + C[i] = C[i+1] + X[i]; + + memset(R, 0, sizeof(R)); + bool r = dfs(0, f); + if (!r) cout << "*\n"; + else { + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11833 + Name: Route Change + Problem: https://onlinejudge.org/external/118/11833.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +struct State { + int u, d; + State(int u, int d):u(u),d(d){} + bool operator < (const State& e) const { return d>e.d; } +}; + +int W[343][343], D[343]; +vector adj[343]; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m, c, k; + for (int cse=1; cin>>n>>m>>c>>k && (n||m||c||k); ++cse) { + for (int i=0; i> u >> v >> d; + W[u][v] = W[v][u] = d; + adj[u].push_back(v); + adj[v].push_back(u); + } + D[c-1] = 0; + for (int i=c-2; i>=0; --i) + D[i] = D[i+1] + W[i][i+1]; + D[c-1] = INF; + + priority_queue q; + q.push(State(k, D[k]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.u]) continue; + if (s.u == c-1) break; + + for (int v: adj[s.u]) { + int dd = s.d + W[s.u][v]; + if (v < c - 1) { + dd += D[v]; + v = c - 1; + } + if (dd < D[v]) + q.push(State(v, D[v]=dd)); + } + } + cout << D[c-1] << endl; + } +} diff --git a/uva_cpp_clean/11833/11833-6.cpp b/uva_cpp_clean/11833/11833-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4777758dc2e758eb1f68c70691a9c0406da6aa9 --- /dev/null +++ b/uva_cpp_clean/11833/11833-6.cpp @@ -0,0 +1,96 @@ +/* + graphs > shortest path > single-source > weighted graph + difficulty: medium + date: 22/May/2019 + hint: build the graph already with the given constraints + by: @brpapa +*/ +#include +#include +#include +using namespace std; +#define MAX_V 250 +#define INF (1 << 30) + +struct Tadj { + int id, w; + Tadj(int id, int w) : id(id), w(w) {} +}; +vector adjList[MAX_V]; + +struct Theap { + int id, c; + + Theap(int id, int c) : id(id), c(c) {} + bool operator<(const Theap &p) const { + return this->c < p.c; + } +}; +int c[MAX_V]; +int R; // cidades da rota: 0 a R-1 + +void insertEdge(int a, int b, int w) { + Tadj v(a, w), u(b, w); + + // a é da rota + if (a < R) { + if (b == a + 1 && b < R) // b é o seguinte imediato e da rota + adjList[a].push_back(u); + else if (b == a - 1 || b >= R) // b é o anterior imediato da rota ou não é da rota + adjList[b].push_back(v); + } + // a não é da rota e b é da rota + else if (a >= R && b < R) + adjList[a].push_back(u); + // ambos não pertencem à rota + else if (a >= R && b >= R) { + adjList[a].push_back(u); + adjList[b].push_back(v); + } +} + +void dijkstra(int init_v) { + priority_queue heap; + + c[init_v] = 0; + heap.push(Theap(init_v, c[init_v])); + + while (!heap.empty()) { + int v = heap.top().id; // vértice de menor custo + heap.pop(); // fecha vértice v + + for (Tadj u : adjList[v]) { + int c_new = c[v] + u.w; + + if (c_new < c[u.id]) { + c[u.id] = c_new; + heap.push(Theap(u.id, c[u.id])); + } + } + } +} + +int main() { + int V, E; // cidades, estradas + int x; // cidade onde veiculo foi consertado + + while (true) { + scanf("%d %d %d %d", &V, &E, &R, &x); + if (V == 0 && E == 0 && R == 0 && x == 0) break; + + // init + for (int v = 0; v < MAX_V; v++) { + adjList[v].clear(); + c[v] = INF; + } + + int v1, v2, w; + while (E--) { + scanf("%d %d %d", &v1, &v2, &w); + insertEdge(v1, v2, w); + } + dijkstra(x); + printf("%d\n", c[R-1]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11834/11834-19.cpp b/uva_cpp_clean/11834/11834-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a46a94d3ce796e6c75364db9b5ad141acf60f1ad --- /dev/null +++ b/uva_cpp_clean/11834/11834-19.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main() { + int W, L, R1, R2; + while (cin >> W >> L >> R1 >> R2 && !(W == 0 && L == 0 && R1 == 0 && R2 == 0)) { + int x = W - R2 - R1, y = L - R2 - R1; + bool ok = false; + if (2*R1 <= W && 2*R1 <= L && 2*R2 <= W && 2*R2 <= L && x >= 0 && y >= 0) { + ok = (x * x + y * y >= (R2 + R1) * (R2 + R1)); + } + if (ok) printf("S\n"); + else printf("N\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11835/11835-21.cpp b/uva_cpp_clean/11835/11835-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e352aac614fbd0bd087b799a5550e39997f4bed2 --- /dev/null +++ b/uva_cpp_clean/11835/11835-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11835 + Name: Formula 1 + Problem: https://onlinejudge.org/external/118/11835.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int X[143][143], ord[143][143], scores[143], a; +bool comp(int i, int j) { return X[a][i] < X[a][j]; } + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m, s; + while (cin >> n >> m && (n||m)) { + for (a=0; a> X[a][j]; + ord[a][j] = j; + } + sort(ord[a], ord[a]+m, comp); + } + + cin >> s; + for (int i=0, c; i> c; + for (int j=0, score; j> score; + for (int k=0; k + +#define SIZE 110 + +using namespace std; + +int G, P, S, K, result[SIZE][SIZE], score[SIZE], final_result[SIZE], _max; + +int main(){ + while(scanf("%d %d",&G,&P),G|P){ + for(int i = 1; i <= G; i++) + for(int j = 1; j <= P; j++) scanf("%d", &result[i][j]); + scanf("%d", &S); + while(S--){ + _max = -1; + scanf("%d", &K); + for(int i = 1; i <= K; i++) scanf("%d", &score[i]); + for(int j = 1; j <= P; j++){ + final_result[j] = 0; + for(int i = 1;i <= G; i++) + if(result[i][j] <= K) + final_result[j] += score[result[i][j]]; + _max = max(_max, final_result[j]); + } + vector ans; + for(int i = 1; i <= P; i++) + if(final_result[i] == _max) + ans.push_back(i); + for(int i = 0; i < ans.size(); i++) + printf(i != ans.size() - 1 ? "%d " : "%d\n", ans[i]); + } + } + return(0); +} diff --git a/uva_cpp_clean/11838/11838-12.cpp b/uva_cpp_clean/11838/11838-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a94a96ea95de2a5d7ffbc5cc9e26e28a0458c973 --- /dev/null +++ b/uva_cpp_clean/11838/11838-12.cpp @@ -0,0 +1,101 @@ +#include +using namespace std; + +#define fastio \ + ios_base::sync_with_stdio(0); \ + cin.tie(NULL); \ + cout.tie(NULL) +#define PI(a, b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long int +#define S second +#define F first +#define FOR(var, len) for (var = 0; var < len; var++) +#define debug1 cout << "debug1" << '\n' +#define debug2 cout << "debug2" << '\n' +#define debug3 cout << "debug3" << '\n' + + +// int dr[] = {0,-1,0,1,-1,-1,1,1}; +// int dc[] = {-1,0,1,0,-1,1,1,-1}; + + +// Maths Utils +int binExp(int a, int b, int m){int r = 1;while (b){if (b % 2 == 1)r = (r * a) % m;a = (a * a) % m;b = b / 2;}return r;} + +int ans,cnt; +int visited[2005],dist[2005],low[2005]; +vector adj[2005]; +stack st; + +void tarjan(int u){ + visited[u] = 1; + dist[u] = low[u] = cnt++; + + st.push(u); + + for(auto v:adj[u]){ + if(!visited[v]){ + tarjan(v); + low[u] = min(low[u],low[v]); + + }else if(visited[v] == 1){ + low[u] = min(low[u],dist[v]); + } + } + + if(dist[u] == low[u]){ + ans++; + int x; + while(st.top()!=u){ + x = st.top(); + visited[x] = 2; + low[x] = dist[u]; + st.pop(); + } + st.pop(); + visited[u] = 2; + } +} + + +int32_t main() +{ + // freopen("input.txt","r",stdin); + // freopen("output.txt","w",stdout); + fastio; + int n,m; + while (1) + { + cin>>n>>m; + + if(n == 0)break; + + // pre-process + for(int i = 1;i<2005;i++){ + adj[i].clear(); + low[i] = dist[i] = -1; + visited[i] = 0; + } + ans = 0; + cnt = 0; + + int a,b,p; + for(int i = 0;i>a>>b>>p; + + adj[a].EB(b); + if(p == 2)adj[b].EB(a); + } + + for(int i = 1;i<=n;i++) + if(!visited[i]) + tarjan(i); + // cout< +#include +#include +#include +#include +#include + +using namespace std; + +ofstream fout("f.out"); + +int n, m, dfsCount, ans; + +stack points; +vector visited; +vector < vector > town; +vector dfs_num, dfs_low; + +void initialize(int n) +{ + ans = dfsCount = 0; + + visited.clear(); + visited.resize(n, 0); + + town.clear(); + town.resize(n); + + dfs_num.clear(); + dfs_num.resize(n, -1); + + dfs_low.clear(); + dfs_low.resize(n, -1); +} + +void dfs(int start) +{ + points.push(start); + + visited[start] = true; + + dfs_num[start] = dfs_low[start] = ++dfsCount; + + for (int i = 0; i < (int)town[start].size(); i++) + { + int child = town[start][i]; + + if (dfs_num[child] == -1) dfs(child); + + if (visited[child]) dfs_low[start] = min(dfs_low[start], dfs_low[child]); + } + + if (dfs_low[start] == dfs_num[start]) + { + ans++; + + while (points.top() != start) + { + visited[points.top()] = false; + + points.pop(); + } + + visited[points.top()] = false; + + points.pop(); + } +} + +int main() +{ + while (cin >> n >> m, n != 0 || m != 0) + { + initialize(n); + + for (int i = 0; i < m; i++) + { + int v, w, p; + + cin >> v >> w >> p; + + v--; + w--; + + town[v].push_back(w); + + if (p == 2) town[w].push_back(v); + } + + for (int i = 0; i < n; i++) if (dfs_num[i] == -1) dfs(i); + + if (ans == 1) cout << 1 << endl; + + else cout << 0 << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11838/11838-14.cpp b/uva_cpp_clean/11838/11838-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b25a8f23a1456044637ed74d58a72564edb185ad --- /dev/null +++ b/uva_cpp_clean/11838/11838-14.cpp @@ -0,0 +1,119 @@ +/*************************************************** + * Problem Name : 11838 - Come and Go.cpp + * Problem Link : https://uva.onlinejudge.org/external/118/11838.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-10-25 + * Problem Type : Graph (dfs) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 2005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +bool vis[MAX]; +int cnt; +void dfs (int u) { + vis[u] = true; + + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (!vis[v]) { + cnt++; + dfs (v); + } + } +} +int main () { + __FastIO; + int n, m; + + while (cin >> n >> m) { + if (n == 0 && m == 0) break; + + for (int i = 0; i < m; i++) { + int u, v, p; + cin >> u >> v >> p; + + if (p == 1) { + adj[u].pb (v); + + } else { + adj[u].pb (v); + adj[v].pb (u); + } + } + + bool flag = false; + + for (int i = 1; i <= n; i++) { + SET (vis, false); + cnt = 1; + dfs (i); + + if (cnt != n) { + flag = true; + break; + } + } + + if (flag) cout << "0\n"; + else cout << "1\n"; + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + } + } + + return 0; +} diff --git a/uva_cpp_clean/11838/11838-19.cpp b/uva_cpp_clean/11838/11838-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f460a2ac8c5a94ceb7c19cef9c2a3e6176bdcb4c --- /dev/null +++ b/uva_cpp_clean/11838/11838-19.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vii; +typedef vector vi; + +vector AdjList; + +vi dfs_num, dfs_low; +vector visited; +stack s; +int counter, n0; + +void scc(int n, int p) { + visited[n] = true; + s.push(n); + dfs_num[n] = dfs_low[n] = counter++; + for (int i = 0; i < AdjList[n].size(); i++) { + if (dfs_num[AdjList[n][i]] == -1) { + scc(AdjList[n][i], n); + } + if (visited[AdjList[n][i]]) dfs_low[n] = min(dfs_low[n], dfs_low[AdjList[n][i]]); + } + if (dfs_num[n] == dfs_low[n]) { + n0++; + while (1) { + int v = s.top(); s.pop(); + visited[v] = 0; + if (n == v) break; + } + } +} + +int main() { + int n, m; + while (cin >> n >> m && !(n == 0 && m == 0)) { + AdjList.assign(n, vi()); + for (int i = 0; i < m; i++) { + int s, d, w; + cin >> s >> d >> w; + AdjList[s-1].push_back(d-1); + if (w == 2) AdjList[d-1].push_back(s-1); + } + + n0 = counter = 0; + dfs_num.assign(n, -1); + dfs_low.assign(n, -1); + visited.assign(n, false); + for (int i = 0; i < n; i++) { + if (dfs_num[i] == -1) scc(i, i); + } + printf("%d\n", n0 > 1? 0: 1); + } + return 0; +} diff --git a/uva_cpp_clean/11838/11838-21.cpp b/uva_cpp_clean/11838/11838-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..845fce7349e4f97bca25f93ef2cdf420d02b26b8 --- /dev/null +++ b/uva_cpp_clean/11838/11838-21.cpp @@ -0,0 +1,66 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11838 + Name: Come and Go + Problem: https://onlinejudge.org/external/118/11838.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 2001 + +list adj[MAXN]; +int pre[MAXN], low[MAXN], pcnt=0; +bool onstack[MAXN]; + +bool isc(int u) { + pre[u] = low[u] = pcnt++; + onstack[u]=1; + + for (int v: adj[u]) + if (pre[v] < 0) { + if (!isc(v)) return 0; + low[u] = min(low[u], low[v]); + } + else if (onstack[v]) + low[u] = min(low[u], pre[v]); + + return pre[u]!=low[u] || u==0; +} + + +int main(){ + int n, m, u, v, p; + while (cin>>n>>m && (n||m)) { + for (int i=0; i>u>>v>>p; + adj[u-1].push_back(v-1); + if (p==2) + adj[v-1].push_back(u-1); + } + + bool result = true; + for (int i=0; result && i 0) + result = false; + else result = isc(i); + } + + puts(result ? "1" : "0"); + } +} diff --git a/uva_cpp_clean/11838/11838-6.cc b/uva_cpp_clean/11838/11838-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..dfa57f78760d0deadc6a8d8fa3131fcf216df92b --- /dev/null +++ b/uva_cpp_clean/11838/11838-6.cc @@ -0,0 +1,49 @@ +// https://uva.onlinejudge.org/external/118/11838.pdf +#include +using namespace std; +using vi=vector; +using si=unordered_set; +using vsi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + while(1){ + int n,m,u,v,p; + cin>>n>>m; + if(!n)break; + vsi g(n),h(n); + for(int i=0;i>u>>v>>p; + u--;v--; + g[u].insert(v); + h[v].insert(u); + if(p==2){ + g[v].insert(u); + h[u].insert(v); + } + } + vi s(n),t(n); + int k; + function dfs1=[&](int i){ + s[i]=1; + for(int j:g[i]) + if(!s[j])dfs1(j); + k=i; + }; + for(int i=0;i dfs2=[&](int i){ + t[i]=1; + for(int j:h[i]) + if(!t[j])dfs2(j); + }; + dfs2(k); + int c=1; + for(int i=0;i + +using namespace std; + +const int MAX_V = 2010; + +int V, E, a, b, p, calls, dfsCounter, numSCC, dfs_low[MAX_V], dfs_num[MAX_V]; +bool vis[MAX_V]; +vector AdjList[MAX_V]; + +void dfs(int u){ + dfs_low[u] = dfs_num[u] = ++dfsCounter; + vis[u] = true; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not dfs_num[v] ) dfs(v); + if( vis[v] ) dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + if( dfs_low[u] == dfs_num[u] ) numSCC++; +} + +void TarjanSCC(){ + fill(dfs_num, dfs_num + V + 1, 0); + fill(vis, vis + V + 1, false); + dfsCounter = numSCC = calls = 0; + for(int u = 1; u <= V; u++) + if( not dfs_num[u] ) calls++, dfs(u); +} + +void solve(){ + TarjanSCC(); + printf("%d\n", numSCC == 1 and calls == 1 ); +} + +void read(){ + for(int i = 0; i < E; i++){ + scanf("%d %d %d", &a, &b, &p); + AdjList[a].push_back(b); + if( p == 2 ) AdjList[b].push_back(a); + } +} + +void clear(){ + for(int u = 1; u <= V; u++) AdjList[u].clear(); +} + +int main(){ + while(scanf("%d %d", &V, &E), V | E){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11839/11839-14.cpp b/uva_cpp_clean/11839/11839-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1a49011a8b54f528baff5e4bef4850f7b776103 --- /dev/null +++ b/uva_cpp_clean/11839/11839-14.cpp @@ -0,0 +1,77 @@ +/*************************************************** + * Problem name : 11839 Optical Reader.cpp + * Problem Link : https://uva.onlinejudge.org/external/118/11839.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-23 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 6 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int n; + char ar[] = {'A', 'B', 'C', 'D', 'E'}; + while (scanf("%d", &n) == 1 && n != 0 ) { + for (int k = 1; k <= n; k++) { + int cnt = 0; + char ch; + for (int i = 0; i < 5; i++) { + int x; + scanf("%d", &x); + if (x <= 127) { + cnt++; + ch = ar[i]; + } + } + if (cnt != 1) printf("*\n"); + else printf("%c\n", ch); + } + } + return 0; +} + diff --git a/uva_cpp_clean/11847/11847-19.cpp b/uva_cpp_clean/11847/11847-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e93d145c7be9265fb6c759bbdebeeb15cb3e0401 --- /dev/null +++ b/uva_cpp_clean/11847/11847-19.cpp @@ -0,0 +1,11 @@ +#include +#include +using namespace std; + +int main() { + int n; + while (scanf("%d", &n) && n != 0) { + printf("%d\n", int(floor(log2(n)))); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11849/11849-11.cpp b/uva_cpp_clean/11849/11849-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7e2875ecc4373a88dd84a1c0d414e502935424a --- /dev/null +++ b/uva_cpp_clean/11849/11849-11.cpp @@ -0,0 +1,70 @@ +#include +/* +Problem: 11849 - CD +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2949 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int N, M, number; +set Jack_cds, Jill_cds, intersection; + +int main () { + + while ( scanf("%d %d\n", &N, &M) && N && M ){ + + while ( N-- ){ + scanf("%d\n", &number); + Jack_cds.insert(number); + } + + while ( M-- ){ + scanf("%d\n", &number); + Jill_cds.insert(number); + } + + set_intersection(Jack_cds.begin(), Jack_cds.end(), + Jill_cds.begin(), Jill_cds.end(), + inserter(intersection, intersection.begin())); + + printf("%zu\n", intersection.size()); + + Jack_cds.clear(); + Jill_cds.clear(); + intersection.clear(); + } + + return 0; +} + +/* +Sample input:- +----------------- +3 3 +1 +2 +3 +1 +2 +4 +0 0 + +Sample output:- +----------------- +2 + +Resources:- +------------- +https://stackoverflow.com/questions/13448064/how-to-find-the-intersection-of-two-stdset-in-c +https://www.geeksforgeeks.org/std-set_intersection-in-cpp/ +http://www.cplusplus.com/reference/algorithm/set_intersection/ +http://www.cplusplus.com/reference/set/set/ +https://stackoverflow.com/questions/2524611/how-can-one-print-a-size-t-variable-portably-using-the-printf-family +https://stackoverflow.com/questions/502856/whats-the-difference-between-size-t-and-int-in-c +*/ diff --git a/uva_cpp_clean/11849/11849-14.cpp b/uva_cpp_clean/11849/11849-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed958dd5c4e602bf60891e9b1f57657c42b4cba3 --- /dev/null +++ b/uva_cpp_clean/11849/11849-14.cpp @@ -0,0 +1,74 @@ +/*************************************************** + * Problem name : 11849 CD.cpp + * Problem Link : https://uva.onlinejudge.org/external/118/11849.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-27 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ll N, M, x; + mapmp; + while (scanf("%lld %lld", &N, &M) == 2 && N != 0 && M != 0) { + for (int i = 0; i < N; i++) { + scanf("%lld",&x); + mp[x] = 1; + } + int cnt = 0; + for (int i = 0; i < M; i++) { + scanf("%lld", &x); + if(mp[x] == 1) cnt++; + } + printf("%d\n", cnt); + mp.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/11849/11849-19.cpp b/uva_cpp_clean/11849/11849-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd79c8198e0411071939d7375d38e3a23ab4097 --- /dev/null +++ b/uva_cpp_clean/11849/11849-19.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +int main() { + int N, M; + unordered_set cds; + while (scanf("%d %d", &N, &M) && !(N == 0 && M == 0)) { + cds.clear(); + while (N--) { + int cd; + scanf("%d", &cd); + cds.insert(cd); + } + int count = 0; + while (M--) { + int cd; + scanf("%d", &cd); + if (cds.count(cd)) count ++; + } + printf("%d\n", count); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11849/11849-20.cpp b/uva_cpp_clean/11849/11849-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae6d85310814c7086efddb857a6b1c7b68818fd5 --- /dev/null +++ b/uva_cpp_clean/11849/11849-20.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +long long jack[10000005]; + +int main() +{ + long long N, M; + + while( cin >> N >> M && N+M!=0 ) + { + for(long long i=0 ; i<10000005 ; i++) + jack[i] = 0; + + long long temp; + for(long long i=0 ; i> temp; + jack[temp] = 1; + } + + long long jill, same = 0; + for(long long i=0 ; i> jill; + if( jack[jill]==1 ) + ++same; + } + + cout << same << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11849/11849-21.cpp b/uva_cpp_clean/11849/11849-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e70270904d565a681510550d9addd2c8a49eee4 --- /dev/null +++ b/uva_cpp_clean/11849/11849-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11849 + Name: CD + Problem: https://onlinejudge.org/external/118/11849.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MAXN 1000001 +int A[MAXN], B[MAXN]; + +int main(){ + int n, m; + while (cin>>n>>m && (n || m)) { + for (int i=0; i>A[i]; + + for (int j=0; j>B[j]; + + int cnt = 0; + for (int i=0, j=0; i +#include + +using namespace std; + +set cds; + +int main() +{ + // N: number Of Cds Owned By Jack + // M: number of Cds owned by Jill + int N,M; + int total, temp; + + while ( scanf("%d %d",&N,&M), N && M ) + { + cds.clear(); + total = 0; + + while ( N-- ) + { + scanf("%d",&temp); + cds.insert(temp); + } + while ( M-- ) + { + scanf("%d",&temp); + if ( cds.find(temp) != cds.end() ) + total++; + } + printf("%d\n",total); + } +} diff --git a/uva_cpp_clean/11849/11849-5.cpp b/uva_cpp_clean/11849/11849-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ea11632bcb86c551743bb599653a1cad2d8e67c3 --- /dev/null +++ b/uva_cpp_clean/11849/11849-5.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; +int main() +{ + int n,m,a,i,c; + while(scanf("%d%d",&n,&m)==2) + { + if(n==0&&m==0) break; + c=0; + mapmp; + for(i=0;i + +using namespace std; + +int n, m, num, ans; + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + set v; + ans = 0; + for(int i = 0; i < n; i++) scanf("%d", &num), v.insert(num); + for(int i = 0; i < m; i++){ + scanf("%d", &num); + if(v.find(num) != v.end()) ans++; + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/1185/1185-21.cpp b/uva_cpp_clean/1185/1185-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0ae2b98aaf1bf5c622955435181c836b7b3b3f8 --- /dev/null +++ b/uva_cpp_clean/1185/1185-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1185 + Name: Big Number + Problem: https://onlinejudge.org/external/11/1185.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int R[10000001] = {1,1,1,1}; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + double l = 0; + for (int i=2; i<10000001; ++i) { + l += log10(i); + R[i] = ceil(l); + } + + int T, n; cin >> T; + while (T--) { + cin >> n; + cout << R[n] << endl; + } +} diff --git a/uva_cpp_clean/1185/1185-5.cpp b/uva_cpp_clean/1185/1185-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bc686b0d90698a4dd472d6c45aedce202389b39b --- /dev/null +++ b/uva_cpp_clean/1185/1185-5.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +int main() +{ + long long n,i,t; + int d; + while(scanf("%lld",&t)==1) + { + while(t--) + { + scanf("%lld",&n); + d=0; + for(i=1;i<=n;i++){ + d=d+log10(i); + } + printf("%d\n",d); + } + } + + + +return 0; +} diff --git a/uva_cpp_clean/11850/11850-19.cpp b/uva_cpp_clean/11850/11850-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1a1f15472b0cd73560ac0df11ff96eaa9cb128e --- /dev/null +++ b/uva_cpp_clean/11850/11850-19.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +using namespace std; + +int main() { + int n; + while (cin >> n && n != 0) { + vector v; + for (int i = 0; i < n; i++) { + int s; + cin >> s; + v.push_back(s); + } + sort(v.begin(), v.end()); + bool ok = true; + for (int i = 1; i < n && ok; i++) { + if (v[i]-v[i-1] > 200) ok = false; + } + if (ok && 1422 - v[n-1] <= 100) printf("POSSIBLE\n"); + else printf("IMPOSSIBLE\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11850/11850-21.cpp b/uva_cpp_clean/11850/11850-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fb3d3b7e0d7c325ea8c7bdfcf3da4d9422c6dd0 --- /dev/null +++ b/uva_cpp_clean/11850/11850-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11850 + Name: Alaska + Problem: https://onlinejudge.org/external/118/11850.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int X[1500], n; +bool check() { + make_heap(X, X+n); + int lst = X[0]; pop_heap(X, X+n); + if (1422-lst > 100) return 0; + for (int i=1; i 200) + return 0; + + lst = X[0]; + pop_heap(X, X+n-i); + } + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin>>n && n) { + for (int i=0; i> X[i]; + + puts(check() ? "POSSIBLE" : "IMPOSSIBLE"); + } +} diff --git a/uva_cpp_clean/11850/11850-5.cpp b/uva_cpp_clean/11850/11850-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3d630c9b284494f1d41281c33699dbebb9dc0dc5 --- /dev/null +++ b/uva_cpp_clean/11850/11850-5.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; +int main() +{ + int n,a[1480],p,i; + while(scanf("%d",&n)==1) + { + if(n==0) + break; + for(i=0;i200) + { + printf("IMPOSSIBLE\n"); + continue; + } + else + { + for(i=0;i200) + { + int c=0; + break; + } + + if(c==1) + printf("IMPOSSIBLE\n"); + else + printf("POSSIBLE\n"); + + } + +} + +return 0; +} diff --git a/uva_cpp_clean/11853/11853-17.cpp b/uva_cpp_clean/11853/11853-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b60ffeae3982e1f1700b145212a6abee4ff389f5 --- /dev/null +++ b/uva_cpp_clean/11853/11853-17.cpp @@ -0,0 +1,58 @@ +/** + * AOAPC II Example 6-21 Paintball + */ +#include +using namespace std; +struct Circle +{ + double x, y, r; + bool vis = false; + Circle(double x, double y, double r) : x(x), y(y), r(r) { } +}; +vector cs; +double start, finish; +bool dfs(Circle &cur) +{ + if (cur.vis) + return false; + cur.vis = true; + if (cur.y - cur.r <= 0) + return true; + for (Circle &c : cs) + if ((cur.x - c.x) * (cur.x - c.x) + (cur.y - c.y) * (cur.y - c.y) <= (cur.r + c.r) * (cur.r + c.r) && dfs(c)) + return true; + if (cur.x - cur.r <= 0) + start = min(start, cur.y - sqrt(cur.r * cur.r - cur.x * cur.x)); + if (cur.x + cur.r >= 1000) + finish = min(finish, cur.y - sqrt(cur.r * cur.r - (1000 - cur.x) * (1000 - cur.x))); + return false; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + cout << fixed << setprecision(2); + int n; + while (cin >> n) + { + cs.clear(); + start = finish = 1000; + while (n--) + { + double x, y, r; + cin >> x >> y >> r; + cs.push_back(Circle(x, y, r)); + } + bool connected = false; + for (Circle &c : cs) + if (c.y + c.r >= 1000 && dfs(c)) + { + connected = true; + break; + } + if (!connected) + cout << "0.00 " << start << " 1000.00 " << finish << "\n"; + else + cout << "IMPOSSIBLE\n"; + } +} diff --git a/uva_cpp_clean/11854/11854-21.cpp b/uva_cpp_clean/11854/11854-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b0b8ee2656ef6a1f0799b4cc482798a5e68579c --- /dev/null +++ b/uva_cpp_clean/11854/11854-21.cpp @@ -0,0 +1,26 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11854 + Name: Egypt + Problem: https://onlinejudge.org/external/118/11854.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main() { + long long int sizes[3]; + while(cin>>sizes[0]>>sizes[1]>>sizes[2] && (sizes[0]||sizes[1]||sizes[2])) { + sort(sizes, sizes+3); + + cout << ((sizes[0]*sizes[0] + sizes[1]*sizes[1] == sizes[2]*sizes[2]) ? "right\n" : "wrong\n"); + } +} diff --git a/uva_cpp_clean/11854/11854-5.cpp b/uva_cpp_clean/11854/11854-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2a3b4deb22ba63741107e7f53a1cadfa0f8f3f8d --- /dev/null +++ b/uva_cpp_clean/11854/11854-5.cpp @@ -0,0 +1,23 @@ +#include +int main() +{ +long a,b,c,d,e,f; +while(scanf("%ld %ld %ld",&a,&b,&c)==3) +{ +d=a*a; +e=b*b; +f=c*c; +if(a!=0 || b!=0 || c!=0) + { + if(f==d+e) + printf("right\n"); + else if(d==e+f) + printf("right\n"); + else if(e==d+f) + printf("right\n"); + else + printf("wrong\n"); + } +} +return 0; +} diff --git a/uva_cpp_clean/11854/main.cpp b/uva_cpp_clean/11854/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb7b1be201f54ec0eae20c8880551cd1a76bdeda --- /dev/null +++ b/uva_cpp_clean/11854/main.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main(){ + int a,b,c; + int h,c1,c2; + while(cin >> a >> b >> c && (a || b || c)){ + if(a>=b && a>=c){ + h = a; + c1 = b; + c2 = c; + }else if(b>=c){ + h = b; + c1 = a; + c2 = c; + }else{ + h = c; + c1 = b; + c2 = a; + } + if((c1*c1 + c2*c2) == (h*h)) cout << "right" << endl; + else cout << "wrong" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11857/11857-10.cpp b/uva_cpp_clean/11857/11857-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..573f4656c1a8700779bd6a37338c5c931b8cdcc4 --- /dev/null +++ b/uva_cpp_clean/11857/11857-10.cpp @@ -0,0 +1,103 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +const int N= (int)1e5+5; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + int count(int u) { + return sz[find(u)]; + } +}; + +// {weight, a, b} +pair minimumSpanningTree(vector> &edges) { + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + int ans = 0,a,b,weight,l=0; + for (auto e: edges) { + + tie(weight,a,b) = e; + + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + ans=max(ans , weight); + l++; + } + } + return {ans,l}; +} + + +int main() { + FAST + + int n,m,w,a,b; + + while (cin>>n>>m) { + if(n==0 && m==0) break; + + vector> arr; + + for(int i=0;i>a>>b>>w; + arr.push_back({w,a,b}); + } + + auto ans = minimumSpanningTree(arr); + + if(ans.second == (n-1)) cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11857 + Name: Driving Range + Problem: https://onlinejudge.org/external/118/11857.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +// ------------------------------------------------------------ + +#define MAXN 1000017 +int par[MAXN]; +int find(int u) { return par[u]<0 ? u : par[u]=find(par[u]); } +void join(int u, int v) { + if ((u=find(u)) == (v=find(v))) return; + if (par[v] < par[u]) + swap(u, v); + par[u] += par[v]; + par[v] = u; +} + + +struct Edge { + int u, v, d; + bool operator < (const Edge &e) const { return d < e.d; } +} q[MAXN]; + + +int main() { + int n; + while ((n=readUInt())) { + memset(par, -1, n*sizeof(int)); + + int m=readUInt(); + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +int N, M, a, b, w; +vector < pair < int, ii > > EdgeList; + +void Kruskal(){ + UnionFind UF(N); + sort(EdgeList.begin(), EdgeList.end()); + int ans = 0, CC = N; + for(int it = 0; it < M; it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + ans = front.first, CC--; + UF.unionSet(front.second.first, front.second.second); + } + if( CC != 1 ) puts("IMPOSSIBLE"); + else printf("%d\n", ans); +} + +int main(){ + while(scanf("%d %d", &N, &M), N | M){ + EdgeList.clear(); + for(int it = 0; it < M; it++){ + scanf("%d %d %d", &a, &b, &w); + EdgeList.push_back( make_pair( w, ii(a, b) ) ); + } + Kruskal(); + } + return(0); +} diff --git a/uva_cpp_clean/11858/11858-9.cpp b/uva_cpp_clean/11858/11858-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1bbddd7387b841d9d058ff3ff873158ec3722c2 --- /dev/null +++ b/uva_cpp_clean/11858/11858-9.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +const int SIZE = 1000005; + +int n, v[SIZE]; + +long long Merge(int lo, int mid, int hi){ + long long inv = 0; + vector L, R; + int l1 = mid - lo + 1, l2 = hi - mid; + for(int it = 0; it < l1; it++) L.push_back(v[lo + it]); + for(int it = 0; it < l2; it++) R.push_back(v[mid + 1 + it]); + L.push_back(INT_MAX), R.push_back(INT_MAX); + int i = 0, j = 0; + for(int it = lo; it <= hi; it++) + if( L[i] <= R[j] ) + v[it] = L[i++]; + else + v[it] = R[j++], inv += l1 - i; + return inv; +} + +long long MergeSort(int lo, int hi){ + long long inv = 0; + if( lo < hi ){ + int mid = ( lo + hi ) / 2; + inv += MergeSort(lo, mid); + inv += MergeSort(mid + 1, hi); + inv += Merge(lo, mid, hi); + } + return inv; +} + +int main(){ + while(~scanf("%d", &n)){ + for(int it = 0; it < n; it++) + scanf("%d", v + it); + printf("%lld\n", MergeSort(0, n - 1)); + } + return(0); +} diff --git a/uva_cpp_clean/11860/11860-19.cpp b/uva_cpp_clean/11860/11860-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51e33b628326e98f1ffe52168462d54a8e811697 --- /dev/null +++ b/uva_cpp_clean/11860/11860-19.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair si; + +int main() { + int T; + cin >> T; + string line; + getline(cin, line); + for (int c = 1; c <= T; c++) { + vector text; + unordered_set words; + unordered_map match; + while (getline(cin, line) && line != "END") { + int p = 0, l = 0; + for (int i = 0; i < line.length(); i++) { + if (!isalpha(line[i])) { + if (l != 0) text.push_back(line.substr(p, l)), words.insert(line.substr(p, l)); + p = i+1; + l = 0; + } else { + l++; + } + } + if (l != 0) text.push_back(line.substr(p, l)), words.insert(line.substr(p, l)); + } + int ini = 0, end = 999999, count = 0, cini = 0; + queue q; + for (int i = 0; i < text.size(); i++) { + if (match[text[i]] == 0) count++; + match[text[i]]++; + q.push(make_pair(text[i], i)); + while (match[q.front().first] > 1) match[q.front().first]--, q.pop(), cini = q.front().second; + if (count == words.size()) { + if (i-cini < end-ini) ini = cini, end = i; + } + } + printf("Document %d: %d %d\n", c, ini + 1, end + 1); + } + return 0; +} diff --git a/uva_cpp_clean/11860/11860-21.cpp b/uva_cpp_clean/11860/11860-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..241d66f9a49d64e9b197ac5711d53e2333c28c3b --- /dev/null +++ b/uva_cpp_clean/11860/11860-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11860 + Name: Document Analyzer + Problem: https://onlinejudge.org/external/118/11860.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXN 100001 +int seen[MAXN], X[MAXN]; + +int main(){ + int T; + scanf("%d", &T); + for (int cse=1; cse<=T; ++cse) { + int un=0, tn=0; + + unordered_map ids; + char word[200]={0}, len=0; + while (word[0] != 'E') { + if (len) { + auto it = ids.find(word); + int id; + if (it == ids.end()) { + id = ids[word] = un++; + seen[id] = 0; + } + else id = it->second; + + X[tn++] = id; + } + + while (!isalpha(word[0]=getchar())); + for(len=1; isalpha(word[len]=getchar()); ++len); + word[len] = 0; + } + + int bi=-1, bj=MAXN+1, run=0; + for (int i=0,j=0; j1) + --seen[X[i++]]; + + if (run==un && j-i +/* +Problem: 11875 - Brick Game +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2986 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int T, N, age, captin, testcase = 0; + +int main () { + + scanf("%d\n", &T); + + while ( T-- ){ + scanf("%d\n", &N); + + for (int i = 0; i < N; ++i){ + scanf("%d", &age); + if (i == (N-1)/2) captin = age; + } + + printf("Case %d: %d\n", ++testcase, captin); + } + + return 0; +} +/* +Sample input:- +----------------- +2 +5 19 17 16 14 12 +5 12 14 16 17 18 + +Sample output:- +----------------- +Case 1: 16 +Case 2: 16 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11875/11875-19.cpp b/uva_cpp_clean/11875/11875-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0239b10493e11c8f798630ffed436662935cb03c --- /dev/null +++ b/uva_cpp_clean/11875/11875-19.cpp @@ -0,0 +1,17 @@ +#include +#include +using namespace std; + +int main() { + int T; + int players[11]; + scanf("%d", &T); + for (int i = 1; i <= T; i++) { + int N; + scanf("%d", &N); + for (int j = 0; j < N; j++) scanf("%d", &players[j]); + sort(players, players+N); + printf("Case %d: %d\n", i, players[N/2]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11875/11875-21.cpp b/uva_cpp_clean/11875/11875-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14576011e63e436f250188b9e569177cf6d76361 --- /dev/null +++ b/uva_cpp_clean/11875/11875-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11875 + Name: Brick Game + Problem: https://onlinejudge.org/external/118/11875.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int t; + cin>>t; + for(int cse=1; cse<=t; cse++) { + int n, x; + cin>>n; + for (int i=0; i<=n/2; i++) + cin>>x; + printf("Case %d: %d\n", cse, x); + for (int i=0; i>x; + } +} diff --git a/uva_cpp_clean/11875/11875-5.cpp b/uva_cpp_clean/11875/11875-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3e4f2ab6e98c1e5d2eeac52e30d498384349d279 --- /dev/null +++ b/uva_cpp_clean/11875/11875-5.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +using namespace std; +int main() +{ + int n,i,a[12],t,p,mem; + while(scanf("%d",&n)==1) + { + t=1; + while(n--) + { + scanf("%d",&mem); + for(i=0;i ad-hoc + difficulty: easy + date: 14/Feb/2020 + by: @brpapa +*/ +#include +using namespace std; + +int main() { + int T; cin >> T; + for (int t = 1; t <= T; t++) { + int N; cin >> N; + for (int n = 0; n < N; n++) { + int a; cin >> a; + if (n == (N-1)/2) cout << "Case " << t << ": " << a << endl; + } + } + return 0; +} diff --git a/uva_cpp_clean/11875/11875-9.cpp b/uva_cpp_clean/11875/11875-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58b7dbcccde801a71e23ba1a1db00e55fe18256d --- /dev/null +++ b/uva_cpp_clean/11875/11875-9.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main(){ + int c, n, tmp, t = 0; + vector a; + scanf("%d", &c); + while(c--){ + scanf("%d", &n); + for(int i = 0 ; i < n; i++) + scanf("%d", &tmp), a.push_back(tmp); + sort(a.begin(), a.end()); + printf("Case %d: %d\n", ++t, a[a.size() / 2]); + a.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11876/11876-14.cpp b/uva_cpp_clean/11876/11876-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef712b22751a4e04a588724b04adc4ae5d4f84c0 --- /dev/null +++ b/uva_cpp_clean/11876/11876-14.cpp @@ -0,0 +1,121 @@ +/*************************************************** + * Problem name : 11876 - N + NOD (N).cpp + * Problem Link : https://uva.onlinejudge.org/external/118/11876.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-29 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 1000003 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +ll prime[MAX], arr[MAX], cnt, len, sum[MAX]; +mapmp; +void sieve() { + ll i, j; + cnt = 0; + len = sqrt(MAX); + for (i = 2; i <= len; i++) { + if (arr[i] == 0) { + for (j = i + i; j < MAX; j += i) { + arr[j] = 1; + } + } + } + for (i = 2; i < MAX; i++) { + if (arr[i] == 0) { + prime[cnt++] = i; + } + } +} +ll divisor(ll n) { + if (n == 0) return 0; + ll i, divN = 1, c; + for (i = 0; i < cnt && prime[i] <= len; i++) { + if (n % prime[i] == 0) { + c = 0; + while (n % prime[i] == 0) { + c++; + n /= prime[i]; + } + divN *= (c + 1); + } + } + if (n > 1) divN *= 2; + + return divN; +} +void PreCal() { + ll k = 1, x = 1, cnt; + mp[x] = true; + while (x <= MAX) { + x = k + divisor(k); + mp[x] = true; + k = x; + } + sum[0] = 0, cnt = 0; + for (int i = 1; i <= MAX; i++) { + if (mp[i] == true) cnt++; + + sum[i] = cnt; + } +} +int main () { + ll tc, a, b; + sieve(); + PreCal(); + scanf("%lld", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%lld %lld", &a, &b); + ll ans; + if (mp[a] == true) ans = (sum[b] - sum[a]) + 1; + else ans = sum[b] - sum[a]; + printf("Case %d: %lld\n", t, ans); + } + return 0; +} + diff --git a/uva_cpp_clean/11876/11876-19.cpp b/uva_cpp_clean/11876/11876-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7498b83c1aa4f8a7370cdd1725652a27fefc5dcd --- /dev/null +++ b/uva_cpp_clean/11876/11876-19.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +inline void sieve(ll upperbound = 1000010) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +vii factors; + +inline void primeFactors(ll n) { + factors.clear(); + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +int N[1000000], s = 0; + +int divisors(int n) { + int ret = 1; + primeFactors(n); + for (int i = 0; i < factors.size(); i++) { + ret *= factors[i].second + 1; + } + return ret; +} + +int main() { + sieve(); + N[0] = 1; + for (int i = 1; true; i++) { + N[i] = N[i - 1] + divisors(N[i - 1]); + s++; + if (N[i] > 1000001) break; + } + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + int A, B; + cin >> A >> B; + printf("Case %d: %ld\n", c, upper_bound(N, N + s, B) - lower_bound(N, N + s, A)); + } + return 0; +} diff --git a/uva_cpp_clean/11876/11876-21.cpp b/uva_cpp_clean/11876/11876-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cc84ee7a5d34fd55942dbbce1020406082993fa --- /dev/null +++ b/uva_cpp_clean/11876/11876-21.cpp @@ -0,0 +1,109 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11876 + Name: N + NOD (N) + Problem: https://onlinejudge.org/external/118/11876.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// --------------------------------------------------------------- + +#define MAXP 1000012 +int primes[80000], isnp[MAXP], pnt; +void sieve() { + isnp[1] = 1; + for (int i=2; i 1) result *= 2; + return result; +} + +bool E[MAXP]; +int N[87000], I[MAXP], nn; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + sieve(); + E[N[0] = 1] = 1; + for (nn=1; N[nn-1]<1000000; ++nn) + E[N[nn] = N[nn-1] + divisors(N[nn-1])] = 1; + + I[0] = 0; + for (int i=1, k=0; i= N[k]) { + ++I[i]; + ++k; + } + } + + + int T = readUInt(); + for (int cse=1; cse<=T; ++cse) { + int l = readUInt(), + r = readUInt(), + rs = I[r] - I[l]; + if (E[l]) ++rs; + + cout << "Case " << cse << ": " << rs << "\n"; + } +} diff --git a/uva_cpp_clean/11876/11876-5.cpp b/uva_cpp_clean/11876/11876-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7e82eba50e489311d0c15b33809d7d56b267256e --- /dev/null +++ b/uva_cpp_clean/11876/11876-5.cpp @@ -0,0 +1,195 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +ll d[mx6],ans[mx6],p[mx6]; + +void sieve() +{ + for1(i,mx6-6) + { + for(int j=i;j<=mx6-6;j+=i) + { + d[j]++; + } + } +} + +void cal() +{ + sieve(); + p[0]=1; + for1(i,mx6-6) + { + p[i]= p[i-1]+d[p[i-1]]; + } + + int j=0; + for1(i,mx6-6) + { + ans[i]=ans[i-1]; + if(i==p[j]) + { + j++; + ans[i]++; + } + } + +} + +int main() +{ + cal(); + int t,no=0; + si(t); + while(t--) + { + int a,b; + sii(a,b); + Case(no); + P(ans[b]-ans[a-1]); + } + + + return 0; +} + + + diff --git a/uva_cpp_clean/11876/11876-9.cpp b/uva_cpp_clean/11876/11876-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e8ebcdb3034141a4ba78ce9f2a2d7b6bb1b6cd5 --- /dev/null +++ b/uva_cpp_clean/11876/11876-9.cpp @@ -0,0 +1,50 @@ +#include + +#define LIM 1000000 + +using namespace std; + +bitset prime; +vector p; + +void sieve(){ + prime[0] = prime[1] = true; + p.push_back(2); + for(int i = 3; i seq; + seq.push_back(1); + for(int i=1; seq[i - 1] < 1000000; i++) + seq.push_back(seq[i - 1] + NOD(seq[i - 1])); + scanf("%d", &c); + while(c--){ + scanf("%d %d", &n, &m); + printf("Case %d: ", ++t); + ans = upper_bound(seq.begin(), seq.end(), m) - seq.begin() - (upper_bound(seq.begin(), seq.end(), n - 1) - seq.begin()); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11877/11877-21.cpp b/uva_cpp_clean/11877/11877-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc04bc70fc01ba15b0a678facb306491c86d442a --- /dev/null +++ b/uva_cpp_clean/11877/11877-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11877 + Name: The Coco-Cola Store + Problem: https://onlinejudge.org/external/118/11877.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int DP[110] = {0,0, 1,1}; +int main(){ + for (int i=4; i<110; i++) + DP[i] = i/3 + DP[i/3+i%3]; + + int n; + while (cin>>n && n) + cout << DP[n] << endl; +} diff --git a/uva_cpp_clean/11877/11877-5.cpp b/uva_cpp_clean/11877/11877-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..031336006033cc761a3cabd70f951622698b828e --- /dev/null +++ b/uva_cpp_clean/11877/11877-5.cpp @@ -0,0 +1,24 @@ +#include +int main(){ + int n; + while(scanf("%d",&n)==1) + { + if(n==0) + break; + int c=0; + while(n>=3) + { + n=n-3; + c++; + n=n+1; + } + if(n==2) + { + c++; + printf("%d\n",c); + } + else + printf("%d\n",c); + } + return 0; +} diff --git a/uva_cpp_clean/11877/11877-9.cpp b/uva_cpp_clean/11877/11877-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6281f5bae64ba347e154cc8ad797414375445bb1 --- /dev/null +++ b/uva_cpp_clean/11877/11877-9.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main(){ + int n, ans; + while(scanf("%d", &n), n){ + ans = 0; + while(n >= 3) + ans += n / 3, n = n / 3 + n % 3; + if(n == 2) + ans++; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11878/11878-14.cpp b/uva_cpp_clean/11878/11878-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a2d6a52f57dcc30e33ce2cd7a450c8be3500100 --- /dev/null +++ b/uva_cpp_clean/11878/11878-14.cpp @@ -0,0 +1,92 @@ +/*************************************************** + * Problem name : 11878 Homework Checker.cpp + * Problem Link : https://uva.onlinejudge.org/external/118/11878.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-25 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + string str; + int cnt = 0; + while (cin >> str) { + int sz = str.size(), a = 0, b = 0, c = 0, i = 0, flag = 0; + char ch; + while (str[i] != '+' && str[i] != '-') { + a = (a * 10 ) + (str[i] - '0'); + i++; + } + ch = str[i]; + i++; + while (str[i] != '=') { + b = (b * 10) + (str[i] - '0'); + i++; + } + i++; + if (str[i] != '?') { + while (i < sz) { + c = (c * 10) + (str[i] - '0'); + i++; + } + } else { + flag = 1; + } + int ans; + if (ch == '+') ans = a + b; + else if (ch == '-') ans = a - b; + if (!flag && ans == c) cnt++; + //~ printf("a = %d , b = %d , c = %d, ch = %c\n", a, b, c, ch); + } + printf("%d\n", cnt); + return 0; +} + diff --git a/uva_cpp_clean/11878/11878-19.cpp b/uva_cpp_clean/11878/11878-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..378fdee11154fd08c7649474d3b56c5816111fe5 --- /dev/null +++ b/uva_cpp_clean/11878/11878-19.cpp @@ -0,0 +1,33 @@ +#include +#include +using namespace std; + +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + string eq; + int correct = 0; + while (cin >> eq) { + int a = 0, b = 0, c = 0, i = 0; + bool op, na = false; // +/!- + for (; i < eq.length(); i++) { + if(eq[i] != '+' && eq[i] != '-') a = 10*a + (eq[i]-'0'); + else { op = eq[i] == '+'; break; } + } + i++; + for (; i < eq.length(); i++) { + if(eq[i] != '=') b = 10*b + (eq[i]-'0'); + else break; + } + i++; + for (; i < eq.length(); i++) { + if(eq[i] != '?') c = 10*c + (eq[i]-'0'); + else { na = true; break; } + } + if (!na) { + if ((op && a + b == c) || (!op && a - b == c)) correct++; + } + } + printf("%d\n", correct); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11878/11878-21.cpp b/uva_cpp_clean/11878/11878-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8433c2bf11ff1b73dbacc7c5b1f4a718f0b8e18 --- /dev/null +++ b/uva_cpp_clean/11878/11878-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11878 + Name: Homework Checker + Problem: https://onlinejudge.org/external/118/11878.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool solve(const string &str) { + if (str[str.length()-1] == '?') + return 0; + + int x, y, z; + if (sscanf(&str[0], "%d+%d=%d", &x, &y, &z)==3) + return x+y == z; + else + if (sscanf(&str[0], "%d-%d=%d", &x, &y, &z)==3) + return x-y == z; + return 0; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int cnt = 0; + string str; + while (cin >> str) + if (solve(str)) + ++cnt; + cout << cnt << endl; +} diff --git a/uva_cpp_clean/11878/11878-5.cpp b/uva_cpp_clean/11878/11878-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4446a6abb33d9f91834e9c1697d75d23c0b37656 --- /dev/null +++ b/uva_cpp_clean/11878/11878-5.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int a,b,count=0; + char sine,c[1000],no; + while(sc("%d%c%d=%s",&a,&sine,&b,c)==4) + { + if(sine=='+') + { + if(a+b==atoi(c)) + count++; + } + else if(sine=='-') + { + if(a-b==atoi(c)) + count++; + } + } + pf("%d\n",count); + return 0; +} diff --git a/uva_cpp_clean/11879/11879-19.cpp b/uva_cpp_clean/11879/11879-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3f9b6c872533ea0887d1fde6b4d9a1db611f8b1 --- /dev/null +++ b/uva_cpp_clean/11879/11879-19.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main() { + string n; + while (cin >> n && n != "0") { + int l = n.length(), i = 0; + while (i < l - 1 && n[i] == '0') i++; + while (l - i > 6) { + int val = (n[l-1]-'0')*5; + if (n[l-2]-'0' < val%10) val += 10; + n[l-2] = (n[l-2]-'0'+10-val%10)%10 + '0'; + val /= 10; + bool carry = n[l-3]-'0' < val%10; + n[l-3] = (n[l-3]-'0'+10-val%10)%10 + '0'; + int pos = l - 4; + while (carry) { + carry = n[pos] == '0'; + n[pos] = (n[pos]-'0'+9)%10 + '0'; + pos--; + } + l--; + } + int check = atoi(n.substr(i, l - i).c_str()); + if (check % 17 == 0) printf("1\n"); + else printf("0\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11879/11879-21.cpp b/uva_cpp_clean/11879/11879-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cd3782de6bb604520eb3d9bc7dd13a4c8a766a4 --- /dev/null +++ b/uva_cpp_clean/11879/11879-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11879 + Name: Multiple of 17 + Problem: https://onlinejudge.org/external/118/11879.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char ch; + for (;;) { + int n = 0, i=0; + while ((ch=getchar()) != '\n') + n = (n*10 + ch - '0') % 17, i++; + + if (i==1 && !n) break; + puts(n?"0":"1"); + } +} + diff --git a/uva_cpp_clean/11879/11879-5.cpp b/uva_cpp_clean/11879/11879-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c4b4c66b90204639507ccd948739a28286a333db --- /dev/null +++ b/uva_cpp_clean/11879/11879-5.cpp @@ -0,0 +1,24 @@ +#include +#include +int main() +{ + int i,sum,r,l; + char s[1020]; + while(gets(s)) + { + r=0; + l=strlen(s); + if(l==1 && s[0]=='0') + break; + for(i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11881 + Name: Internal Rate of Return + Problem: https://onlinejudge.org/external/118/11881.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int CF[17], n; +double npv(double irr) { + double result = CF[0]; + double den = 1; + for (int i=1; i> n && n++) { + for (int i=0; i> CF[i]; + + if (npv(0) == npv(1)) + cout << "Too many\n"; + + else { + double lo = -1, hi = 1e10; + while (hi-lo > 1e-9) { + double mid = (lo + hi) / 2; + if (npv(mid) < 0) + hi = mid; + else + lo = mid; + } + + cout << fixed << setprecision(2) << lo << endl; + } + } +} diff --git a/uva_cpp_clean/11881/11881-9.cpp b/uva_cpp_clean/11881/11881-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d25e2cca6eb526b8e3d824712bdbb8940c4b3de6 --- /dev/null +++ b/uva_cpp_clean/11881/11881-9.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int T, v[15]; +double lo, hi, mid; + +double solve(double IRR){ + double ans = v[0]; + for(int i = 1; i <= T; i++) ans += v[i] / pow(1 + IRR, i); + return ans; +} + +int main(){ + while(scanf("%d", &T), T){ + for(int i = 0; i <= T; i++) scanf("%d", &v[i]); + lo = -0.99, hi = 10000.00; + for(int it = 0; it < 80; it++){ + mid = (lo + hi) / 2.0; + if(solve(mid) * solve(lo) > 0) lo = mid; + else hi = mid; + } + printf("%.2lf\n", (lo + hi) / 2.0); + } + return(0); +} diff --git a/uva_cpp_clean/11888/11888-9.cpp b/uva_cpp_clean/11888/11888-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9665eb77e165e79b3761f03cadce6ca5db663e76 --- /dev/null +++ b/uva_cpp_clean/11888/11888-9.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +typedef long long ll; + +const int P = 31, MAX_S = 2e5 + 10; + +int n; +bool pal[MAX_S], rpal[MAX_S]; +string s; + +void solve () { + for (int i = 0; i + 1 < n; i++) { + if (pal[i] and rpal[i + 1]) { + puts("alindrome"); + return; + } + } + puts(pal[n - 1] ? "palindrome" : "simple"); +} + +void calculate1 () { + ll hash1 = 0LL, hash2 = 0LL, power = 1; + for (int i = 0; i < n; i++) { + hash1 = hash1 + power * (s[i] - 'a' + 1); + hash2 = hash2 * P + (s[i] - 'a' + 1); + power *= P; + pal[i] = (hash1 == hash2); + } +} + +void calculate2 () { + ll hash1 = 0LL, hash2 = 0LL, power = 1; + for (int i = 0; i < n; i++) { + hash1 = hash1 + power * (s[n - 1 - i] - 'a' + 1); + hash2 = hash2 * P + (s[n - 1 - i] - 'a' + 1); + power *= P; + rpal[n - 1 - i] = (hash1 == hash2); + } +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + cin >> s; + n = s.size(); + calculate1(); + calculate2(); + solve(); + } + return (0); +} diff --git a/uva_cpp_clean/11889/11889-13.cpp b/uva_cpp_clean/11889/11889-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc2303437110d5ba83b5d782f3ab164faae0aa6f --- /dev/null +++ b/uva_cpp_clean/11889/11889-13.cpp @@ -0,0 +1,53 @@ +#include +#include + +using namespace std; + +typedef long long ll; + +ll gcd(ll a, ll b) +{ + if (b == 0) return a; + + else return gcd(b, a % b); +} + +ll lcm(ll a, ll b) +{ + return b * (a / gcd(a, b)); +} + +int main() +{ + ll x, y, t; + + cin >> t; + + while (t--) + { + cin >> x >> y; + + if (y % x) cout << "NO SOLUTION" << endl; + + else + { + ll ans; + ll temp = ans = y / x; + + if (y == lcm(max(ans, x), min(ans, x))) + { + cout << ans << endl; + } + + else + { + while (y != lcm(max(ans, x), min(ans, x))) + { + ans += temp; + } + + cout << ans << endl; + } + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11889/11889-14.cpp b/uva_cpp_clean/11889/11889-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e12577628412ae100d03f233f32725f52bc991a2 --- /dev/null +++ b/uva_cpp_clean/11889/11889-14.cpp @@ -0,0 +1,101 @@ +/*************************************************** + * Problem name : 11889 - Benefit.cpp + * Problem Link : https://uva.onlinejudge.org/external/118/11889.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-29 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +vectordivs; +//~ ll gcd(ll a, ll b) { +//~ if (a % b == 0) +//~ return b; +//~ return gcd(b, a % b); +//~ } +//~ ll lcm(ll a, ll b) { +//~ return a * b / gcd(a, b); +//~ } +void Divisors(ll n) { + ll len = sqrt(n); + for (ll i = 1; i <= len; i++) { + if (n % i == 0) { + divs.push_back(i); + if (n % (n / i) == 0) { + divs.push_back(n / i); + } + } + + } +} +int main () { + ll tc, A, C; + scanf("%lld", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%lld %lld", &A, &C); + if (C % A != 0) { + printf("NO SOLUTION\n"); + continue; + } + Divisors(C); + sort(divs.begin(), divs.end()); + int sz = divs.size(); + for (int i = 0; i < sz; i++) { + //~ printf("%lld ", divs[i]); + int lc = lcm(A, divs[i]); + if (lc == C) { + printf("%lld\n", divs[i]); + break; + } + } + divs.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/11889/11889-9.cpp b/uva_cpp_clean/11889/11889-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58063730e665f579fe59a4d84533611763166d15 --- /dev/null +++ b/uva_cpp_clean/11889/11889-9.cpp @@ -0,0 +1,43 @@ +#include + +#define SIZE 3200 + +using namespace std; + +bool is_prime[SIZE]; +vector prime; +int tc, A, C, ans; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(int it1 = 2; it1 < SIZE; it1++) + if( is_prime[it1] ){ + for(int it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +int main(){ + sieve(); + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &A, &C); + if( C % A != 0 ){ + puts("NO SOLUTION"); + continue; + } + ans = 1; + for(int it = 0; it < prime.size() and prime[it] * prime[it] <= C and C != 1; it++) + if( C % prime[it] == 0 ){ + int ct1 = 0, ct2 = 0, p = 1; + while( A % prime[it] == 0 ) A /= prime[it], ct1++; + while( C % prime[it] == 0 ) C /= prime[it], p *= prime[it], ct2++; + if( ct2 > ct1 ) ans *= p; + } + if( C != 1 and A == 1) ans *= C; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11892/11892-19.cpp b/uva_cpp_clean/11892/11892-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d9fc6b6474f04fd10c85343b0de640af1d04067 --- /dev/null +++ b/uva_cpp_clean/11892/11892-19.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +int dp[20000][20000]; + +int proc(int eq1, int neq1, bool forced) { + if (eq1 < 0 || neq1 < 0) return 0; + if (eq1 + neq1 <= 1) return 1; + if (forced) return proc(eq1 - 1, neq1, false) == 1? -1: 1; + if (dp[eq1][neq1] == 0) { + if (proc(eq1 - 1, neq1, false) == -1 || + proc(eq1 + 1, neq1 - 1, true) == -1 || + proc(eq1, neq1 - 1, false) == -1) + dp[eq1][neq1] = 1; + else dp[eq1][neq1] = -1; + } + return dp[eq1][neq1]; +} + +int main() { + int T; + cin >> T; + while (T--) { + int N; + cin >> N; + int eq1 = 0, neq1 = 0; + for (int i = 0; i < N; i++) { + int val; + cin >> val; + if (val == 1) eq1++; + else neq1++; + } + if (proc(eq1, neq1, false) == 1) printf("poopi\n"); + else printf("piloop\n"); + } + return 0; +} diff --git a/uva_cpp_clean/119/119-14.cpp b/uva_cpp_clean/119/119-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06e9578e83ef8d4e87d3f4be3f90f7f825db1775 --- /dev/null +++ b/uva_cpp_clean/119/119-14.cpp @@ -0,0 +1,89 @@ +/*************************************************** + * Problem name : 119 - Greedy Gift Givers.cpp + * Problem Link : https://uva.onlinejudge.org/external/1/119.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-23 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 11 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +mapmp; +int main () { + int n,t = 0; + string str[MAX]; + while (scanf("%d", &n) == 1) { + getchar(); + for (int i = 0; i < n; i++) { + cin >> str[i]; + } + for (int i = 1; i <= n; i++) { + string s, ss; + int t_gift, gift, fnd; + cin >> s >> t_gift >> fnd; + if (fnd != 0) { + gift = t_gift / fnd; + mp[s] -= t_gift; + getchar(); + for (int j = 1; j <= fnd; j++) { + cin >> ss; + mp[ss] += gift; + } + mp[s] += (t_gift - (gift * fnd)); + } + } + if(t!=0) nl; + for (int i = 0; i < n; i++) { + cout << str[i] << " " << mp[str[i]] << "\n"; + } + t++; + mp.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/119/119-2.cpp b/uva_cpp_clean/119/119-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3b777a2afb5911ae171ecb4e074040c59b1aae6 --- /dev/null +++ b/uva_cpp_clean/119/119-2.cpp @@ -0,0 +1,67 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +//const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + int N; + int T = 1; + while (cin >> N) { + map mp; + vector names; + + REP(i, N) { + string s; cin >> s; + mp.insert({s, 0}); + names.push_back(s); + } + + REP(i, N) { + string s; int val, n; cin >> s >> val >> n; + if (!n) continue; + int new_val = val / n; + mp[s] -= new_val * n; + REP(j, n) { + string ss; cin >> ss; + mp[ss] += new_val; + } + } + + + if (T++ > 1) cout << el; + + REP(i, N) { + cout << names[i] << ' ' << mp[names[i]] << el; + } + } + return 0; +} diff --git a/uva_cpp_clean/119/119-5.cpp b/uva_cpp_clean/119/119-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..61e21c7e884acfa7ec47e974e43a920cac2deb53 --- /dev/null +++ b/uva_cpp_clean/119/119-5.cpp @@ -0,0 +1,87 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main() +{ + + string name; + int n,f=0; + while(cin>>n) + { + mapmp; + vectorv; + for(int i=0;i>name; + mp[name]=0; + v.pb(name); + } + int amt,man; + for(int i=0;i>name; + cin>>amt>>man; + if(amt==0 && man==0||man==0) continue; + + mp[name]+=(amt%man)-amt; + amt=amt/man; + for(int j=0;j>name; + mp[name]=mp[name]+amt; + } + } + if(f) + cout< +#include +#include +#include + +using namespace std; + +int main(){ + int cases, n; + int rest, operation; + map values; + string name, destination; + queue names; + long i = 0; + while(cin >> cases){ + if(i!=0) cout << endl; + values.clear(); + while(!names.empty()) names.pop(); + for(short i=0; i < cases; i++){ + cin >> name; + names.push(name); + values.insert(pair (name, 0)); + } + for(short i=0; i < cases; i++){ + cin >> name >> operation >> n; + if(n > 0){ + values[name] -= operation; + rest = operation%n; + values[name] += rest; + operation /= n; + } + for(short c=0; c < n; c++){ + cin >> name; + values[name] += operation; + // if(c < rest) values[name] += 1; + } + } + for(short i=0; i < cases; i++){ + cout << names.front() << ' ' << values[names.front()] << endl; + names.pop(); + } + i++; + } + + +return 0; +} diff --git a/uva_cpp_clean/11900/11900-14.cpp b/uva_cpp_clean/11900/11900-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165aabffe7a395d7ab28a794a407cd3d2e4c6adf --- /dev/null +++ b/uva_cpp_clean/11900/11900-14.cpp @@ -0,0 +1,77 @@ +/*************************************************** + * Problem name : 11900 - Boiled Eggs.cpp + * Problem Link : https://uva.onlinejudge.org/external/119/11900.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-29 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc,n,P,Q; + int ar[MAX]; + scanf("%d", &tc); + for(int t = 1; t<=tc; t++){ + scanf("%d %d %d", &n, &P, &Q); + for(int i = 0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11900 + Name: Boiled Eggs + Problem: https://onlinejudge.org/external/119/11900.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int n, p, q, w[32]; +int memo[32][32]; +int ks(int remw, int ind) { + if (remw < 0) return -1; + if (ind==n || !remw) return 0; + if (memo[remw][ind] != -1) + return memo[remw][ind]; + + return memo[remw][ind] = max(ks(remw-w[ind], ind+1)+1, ks(remw, ind+1)); +} + +int main(){ + int T; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin >> n >> p >> q; + for (int i=0; i> w[i]; + + memset(memo, -1, sizeof(memo)); + printf("Case %d: %d\n", cse, min(p, ks(q, 0))); + } +} diff --git a/uva_cpp_clean/11900/11900-5.cpp b/uva_cpp_clean/11900/11900-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..49b0f4204c31088c519aee8f2633683a9cbc4e4d --- /dev/null +++ b/uva_cpp_clean/11900/11900-5.cpp @@ -0,0 +1,28 @@ +#include +int main() +{ + int n,i,k,a,b,c,d[1000],t=1; + scanf("%d",&n); + while(t<=n) + { + int s=0; + scanf("%d %d %d",&a,&b,&c); + i=1; + while(i<=a) + { + scanf("%d",&d[i]); + i++; + } + k=0; + for(i=1;i<=a && i<=b;i++) + { + s+=d[i]; + if(s>c) + break; + k++; + } + printf("Case %d: %d\n",t,k); + t++; + } + return 0; +} diff --git a/uva_cpp_clean/11902/11902-12.cpp b/uva_cpp_clean/11902/11902-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..592cefca24fbf0fd03a0ff0d79b0516eaa81ab3d --- /dev/null +++ b/uva_cpp_clean/11902/11902-12.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + +#define fastio \ + ios_base::sync_with_stdio(0); \ + cin.tie(NULL); \ + cout.tie(NULL) +#define PI(a, b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long int +#define S second +#define F first +#define FOR(var, len) for (var = 0; var < len; var++) +#define debug1 cout << "debug1" << '\n' +#define debug2 cout << "debug2" << '\n' +#define debug3 cout << "debug3" << '\n' + +// Maths Utils +int binExp(int a, int b, int m) +{ + int r = 1; + while (b) + { + if (b % 2 == 1) + r = (r * a) % m; + + a = (a * a) % m; + b = b / 2; + } + return r; +} + +int adj[104][104]; +vector vis1, vis2; + +void dfs(int s, int x, int n, int p) +{ + vis1[s] = 1; + for (int i = 0; i < n; i++) + { + if (i != p and adj[s][i] == 1 and !vis1[i]) + { + if (i != x) + dfs(i, x, n, s); + } + } +} + +void print_line(int n) +{ + cout << '+'; + for (int i = 1; i <= 2 * n - 1; i++) + cout << '-'; + cout << "+\n"; +} + +int32_t main() +{ + fastio; + int t = 1, tt = 0; + cin >> t; + + while (t--) + { + tt++; + int n; + cin >> n; + char ans[n][n]; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + cin >> adj[i][j]; + + vis1.assign(n, 0); + dfs(0, -1, n, -1); + + for (int i = 0; i < n; i++) + { + if (vis1[i]) + ans[0][i] = 'Y'; + else + ans[0][i] = 'N'; + } + + vis2 = vis1; + + vis1.assign(n, 0); + for (int i = 1; i < n; i++) + { + dfs(0, i, n, -1); + for (int j = 0; j < n; j++) + { + if (vis2[j] and !vis1[j]) + ans[i][j] = 'Y'; + else + ans[i][j] = 'N'; + vis1[j] = 0; + } + } + + // printing + cout << "Case " << tt << ":\n"; + print_line(n); + for (int i = 0; i < n; i++) + { + cout << '|'; + for (int j = 0; j < n; j++) + { + cout << ans[i][j] << "|"; + } + cout << '\n'; + print_line(n); + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11902/11902-13.cpp b/uva_cpp_clean/11902/11902-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78584ddfa2a9f3703aadd08aa6df1ea55e12b469 --- /dev/null +++ b/uva_cpp_clean/11902/11902-13.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include + +using namespace std; + +bool ok = false; +int test, t = 1, n; +bool visited[105]; +char ans[105][105]; +bool graph[105][105]; + +void summarize() +{ + cout << "+"; + + for (int i = 0; i < 2 * n - 1; i++) cout << "-"; + + cout << "+" << endl; +} + +void show() +{ + printf("Case %d:\n", t++); + + for (int i = 0; i < n; i++) + { + summarize(); + + for (int j = 0; j < n; j++) + { + cout << "|"; + + if (ans[i][j] == 'Y') cout << 'Y'; + + else cout << 'N'; + } + + cout << "|" << endl; + } + + summarize(); +} + +void dfs(int source) +{ + visited[source] = true; + + for (int i = 0; i < n; i++) + { + if (graph[source][i] && !visited[i]) + { + dfs(i); + } + } +} + +void dfsZero(int source) +{ + ans[source][source] = ans[0][source] = 'Y'; + + visited[source] = true; + + for (int i = 0; i < n; i++) + { + if (graph[source][i] && !visited[i]) + { + dfsZero(i); + } + } +} + +int main() +{ + cin >> test; + + while (test--) + { + ok = false; + + memset(ans, 0, sizeof ans); + memset(graph, 0, sizeof graph); + memset(visited, 0, sizeof visited); + + cin >> n; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> graph[i][j]; + + ans[i][j] = 'N'; + } + } + + dfsZero(0); + + for (int i = 1; i < n; i++) + { + for (int j = 0; j < n; j++) + { + memset(visited, 0, sizeof visited); + + visited[i] = true; + + dfs(0); + + if (!visited[j] && ans[j][j] == 'Y') + { + ans[i][j] = 'Y'; + } + } + } + + show(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11902/11902-18.cpp b/uva_cpp_clean/11902/11902-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..43a292938deff5c6de5d25ec7356f87a7cbb219c --- /dev/null +++ b/uva_cpp_clean/11902/11902-18.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +bool visited[110], dom[110][110]; +int adjM[110][110], n; + +void DFS(int u){ + visited[u] = true; + for(int i = 0; i < n; i++){ + if(adjM[u][i] == 1 && !visited[i]) DFS(i); + } +} + +int main(){ + int colT, rowT, tc, cont = 0; + scanf("%d", &tc); + while(cont < tc){ + printf("Case %d:\n", ++cont); + memset(visited, false, sizeof(visited)); + memset(dom, false, sizeof(dom)); + scanf("%d", &n); + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++) scanf("%d", &adjM[i][j]); + } + dom[0][0] = true; + for(int i = 1; i < n; i++){ //Simular que el nodo i no existe para ver que nodos no son accesibles (Caso main) + dom[0][i] = true; + memset(visited, false, sizeof(visited)); + visited[i] = true; + DFS(0); + for(int j = 0; j < n; j++) if(!visited[j]) dom[i][j] = true; + dom[i][i] = true; + } + memset(visited, false, sizeof(visited)); + DFS(0); //Obtener que nodos son accesibles desde el nodo cero (Caso secundario) + for(int i = 0; i < n; i++){ + printf("+"); + for(int k = 0; k < 2*n-1; k++) printf("-"); + printf("+\n"); + for(int j = 0; j < n; j++){ + if(dom[i][j] && visited[j]) printf("|Y"); + else printf("|N"); + } + printf("|\n"); + } + printf("+"); + for(int k = 0; k < 2*n-1; k++) printf("-"); + printf("+\n"); + } + return 0; +} +/* + Como son casos pequeños, la estrategia es simular que un nodo i se remueve del grafo y tirar DFS, revisar que nodos + no fueron accesibles si se quita el nodo i, entonces, el nodo i vendria siendo el dominador de los que no fueron + accesibles +*/ \ No newline at end of file diff --git a/uva_cpp_clean/11902/11902-21.cpp b/uva_cpp_clean/11902/11902-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db5b69bfc238ab75930596b2876fc55a0b99ad3f --- /dev/null +++ b/uva_cpp_clean/11902/11902-21.cpp @@ -0,0 +1,86 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11902 + Name: Dominator + Problem: https://onlinejudge.org/external/119/11902.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +int n; +list adj[101]; +bitset<101> D[101]; +bool inside[101], + seen[101]; + +void dfs(int u) { + bool first = !seen[u]; + inside[u]=1; + seen[u]=1; + D[u].set(u); + for (int v: adj[u]) + if (!inside[v]) { + if (!seen[v]) { + D[v] = D[u]; + dfs(v); + } + else { + bitset<101> bb = D[v] & D[u]; + bb.set(v); + if (bb != D[v]) { + D[v] = bb; + dfs(v); + } + } + } + inside[u]=0; +} + + +void printDash() { + putchar('+'); + for (int i=0; i<2*n-1; i++) + putchar('-'); + puts("+"); +} + +int main(){ + int T, x; + cin >> T; + for (int cse=1; cse<=T; cse++) { + cin >> n; + for (int i=0; i>x; + if (x) adj[i].push_back(j); + } + } + + dfs(0); + + printf("Case %d:\n", cse); + printDash(); + for (int i=0; i +#include +#include +#include +#include +#include + +using namespace std; + +ofstream fout("f.out"); + +short grid[105][105]; + +int test, t = 1, r, c, m, n, w, even = 0, odd = 0; + +void dfs(int i, int j) +{ + int temp = 0; + + grid[i][j] = 1; + + map , bool> ok; + + if (i + m < r && j + n < c && grid[i + m][j + n] != -1) + { + if (grid[i + m][j + n] == 0) + { + dfs(i + m, j + n); + } + + if (grid[i + m][j + n] != -1 && ok[make_pair(i + m , j + n)] == 0) + { + temp++; + + ok[make_pair(i + m, j + n)] = 1; + } + } + + if (i + m < r && j - n >= 0 && grid[i + m][j - n] != -1) + { + if (grid[i + m][j - n] == 0) + { + dfs(i + m, j - n); + } + + if (grid[i + m][j - n] != -1 && ok[make_pair(i + m, j - n)] == 0) + { + temp++; + + ok[make_pair(i + m, j - n)] = 1; + } + } + + if (i - m >= 0 && j + n < c && grid[i - m][j + n] != -1) + { + if (grid[i - m][j + n] == 0) + { + dfs(i - m, j + n); + } + + if (grid[i - m][j + n] != -1 && ok[make_pair(i - m, j + n)] == 0) + { + temp++; + + ok[make_pair(i - m, j + n)] = 1; + } + } + + if (i - m >= 0 && j - n >= 0 && grid[i - m][j - n] != -1) + { + if (grid[i - m][j - n] == 0) + { + dfs(i - m, j - n); + } + + if (grid[i - m][j - n] != -1 && ok[make_pair(i - m, j - n)] == 0) + { + temp++; + + ok[make_pair(i - m, j - n)] = 1; + } + } + + if (i + n < r && j + m < c && grid[i + n][j + m] != -1) + { + if (grid[i + n][j + m] == 0) + { + dfs(i + n, j + m); + } + + if (grid[i + n][j + m] != -1 && ok[make_pair(i + n, j + m)] == 0) + { + temp++; + + ok[make_pair(i + n, j + m)] = 1; + } + } + + if (i + n < r && j - m >= 0 && grid[i + n][j - m] != -1) + { + if (grid[i + n][j - m] == 0) + { + dfs(i + n, j - m); + } + + if (grid[i + n][j - m] != -1 && ok[make_pair(i + n, j - m)] == 0) + { + temp++; + + ok[make_pair(i + n, j - m)] = 1; + } + } + + if (i - n >= 0 && j + m < c && grid[i - n][j + m] != -1) + { + if (grid[i - n][j + m] == 0) + { + dfs(i - n, j + m); + } + + if (grid[i - n][j + m] != -1 && ok[make_pair(i - n, j + m)] == 0) + { + temp++; + + ok[make_pair(i - n, j + m)] = 1; + } + } + + if (i - n >= 0 && j - m >= 0 && grid[i - n][j - m] != -1) + { + if (grid[i - n][j - m] == 0) + { + dfs(i - n, j - m); + } + + if (grid[i - n][j - m] != -1 && ok[make_pair(i - n, j - m)] == 0) + { + temp++; + + ok[make_pair(i - n, j - m)] = 1; + } + } + + (temp % 2) ? odd++ : even++; + + ok.clear(); +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> r >> c >> m >> n; + + memset(grid, 0, sizeof grid); + + cin >> w; + + even = odd = 0; + + for (int i = 0; i < w; i++) + { + int x, y; + + cin >> x >> y; + + grid[x][y] = -1; + } + + dfs(0, 0); + + cout << "Case " << t++ << ":"; + + cout << " " << even << " " << odd << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11906/11906-21.cpp b/uva_cpp_clean/11906/11906-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8cfcb9c582ccf6c74d96497b24dafa459071a6c0 --- /dev/null +++ b/uva_cpp_clean/11906/11906-21.cpp @@ -0,0 +1,72 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11906 + Name: Knight in a War Grid + Problem: https://onlinejudge.org/external/119/11906.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +bool s[110][110], g[110][110]; +int r, c, m, n, eve, odd; + +int dfs(int x, int y) { + if (x<0 || x>=r || y<0 || y>=c || g[x][y]) return 0; + if (!s[x][y]) { + s[x][y]=1; + + int r = 0; + r += dfs(x+m, y+n); + if (m) + r += dfs(x-m, y+n); + if (n) { + r += dfs(x+m, y-n); + if (m) + r += dfs(x-m, y-n); + } + + if (m != n) { + r += dfs(x+n, y+m); + if (m) + r += dfs(x+n, y-m); + if (n) { + r += dfs(x-n, y+m); + if (m) + r += dfs(x-n, y-m); + } + } + + if (r&1) + odd++; + else eve++; + } + return 1; +} + +int main(){ + int T, u, v, w; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin>>r>>c>>m>>n>>w; + for (int i=0; i>u>>v; + g[u][v]=1; + } + + odd=eve=0; + dfs(0, 0); + + printf("Case %d: %d %d\n", cse, eve, odd); + } +} diff --git a/uva_cpp_clean/11906/11906-6.cpp b/uva_cpp_clean/11906/11906-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3212f6d2b0e7f601ccf2ab88e9bc920d9aa93986 --- /dev/null +++ b/uva_cpp_clean/11906/11906-6.cpp @@ -0,0 +1,72 @@ +/* + graphs > traversal + difficulty: medium + date: 28/Feb/2020 + hint: consider grid as an implicit graph and walk through it avoiding redundant positions (nr, nc) + by: @brpapa +*/ +#include +#include +#include +using namespace std; +#define mp make_pair +#define UNVISITED 0 +#define VISITING 1 +#define WATER 2 +#define EVEN 3 +#define ODD 4 + +int R, C, M, N; +int grid[110][110]; + +set> d; // dr, dc +void walk(int r, int c) { + if (grid[r][c] != UNVISITED) return; + + grid[r][c] = VISITING; + int countAdjReachable = 0; + + // para cada distinto adjacente de grid[r][c] + for (pair n : d) { + int nr = r+n.first, nc = c+n.second; + if (nr >= 0 && nr < R && nc >= 0 && nc < C && grid[nr][nc] != WATER) { + countAdjReachable++; + walk(nr, nc); + } + } + + if (countAdjReachable%2) grid[r][c] = ODD; + else grid[r][c] = EVEN; +} + +int main() { + int T; cin >> T; + for (int t = 1; t <= T; t++) { + memset(grid, UNVISITED, sizeof grid); + + cin >> R >> C >> M >> N; + int W; cin >> W; + while (W--) { + int r, c; cin >> r >> c; + grid[r][c] = WATER; + } + + d.clear(); + // descarta possíveis redundâncias + d.insert({ + mp(M, N), mp(-M, N), mp(M, -N), mp(-M, -N), + mp(N, M), mp(N, -M), mp(-N, M), mp(-N, -M) + }); + + walk(0, 0); + + int countEven = 0, countOdd = 0; + for (int r = 0; r < R; r++) + for (int c = 0; c < C; c++) { + if (grid[r][c] == EVEN) countEven++; + if (grid[r][c] == ODD) countOdd++; + } + cout << "Case " << t << ": " << countEven << " " << countOdd << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11909/11909-19.cpp b/uva_cpp_clean/11909/11909-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ceabfa4e0555919a3463ccedd7a7b93e328dfaa --- /dev/null +++ b/uva_cpp_clean/11909/11909-19.cpp @@ -0,0 +1,22 @@ +#include +#include +using namespace std; + +double PI = 2*acos(0); + +int main() { + double l, w, h, th; + while (cin >> l >> w >> h >> th) { + double d, v, H = sqrt((l*l) + (h*h)); + if (((PI*th)/180) <= asin(h/H)) { + th = 90 - th; + d = l/tan((PI*th)/180); + v = (h*l - ((d*l)/2))*w; + } else { + d = h/tan((PI*th)/180); + v = ((d*h)/2)*w; + } + printf("%.3lf mL\n", v); + } + return 0; +} diff --git a/uva_cpp_clean/11909/11909-20.cpp b/uva_cpp_clean/11909/11909-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abf6db8c7f26765f7b5b026e41f19c368df3846e --- /dev/null +++ b/uva_cpp_clean/11909/11909-20.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +#define Pi acos(-1) + +using namespace std; + +int main() +{ + double l, w, h, theta; + + while( cin >> l >> w >> h >> theta ) + { + double d = l*tan((theta*Pi)/180.0); + + double volume; + if( d>h ) volume = (0.5*h*h*l*w)/d; + else volume = l*w*(h-(d*0.5)); + + cout << fixed << setprecision(3) << volume << " mL" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11917/11917-14.cpp b/uva_cpp_clean/11917/11917-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0810c4f8e880eb079be1eeb61bedc5dc97bc2eb1 --- /dev/null +++ b/uva_cpp_clean/11917/11917-14.cpp @@ -0,0 +1,155 @@ +/*************************************************** + * Problem Name : 11917 - Do Your Own Homework.cpp + * Problem Link : https://onlinejudge.org/external/119/11917.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-11-15 + * Problem Type : + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + int n; + cin >> n; + mapmp; + + for (int i = 0; i < n; i++) { + string str; + int val; + cin >> str >> val; + mp[str] = val; + } + + int d; + cin >> d; + string str; + cin >> str; + cout << "Case " << t << ": "; + + if (mp[str] == 0 || mp[str] > d + 5) { + cout << "Do your own homework!\n"; + + } else if (mp[str] <= d) { + cout << "Yesss\n"; + + } else { + cout << "Late\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/11917/11917-21.cpp b/uva_cpp_clean/11917/11917-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b77e5151ad638417e852dde9a1da8b7c2f0de25 --- /dev/null +++ b/uva_cpp_clean/11917/11917-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11917 + Name: Do Your Own Homework + Problem: https://onlinejudge.org/external/119/11917.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + int T, n, x; + char str[100]; + scanf("%d", &T); + for (int cse=1; cse<=T; ++cse) { + printf("Case %d: ", cse); + + scanf("%d", &n); + map mp; + for (int i=0; isecond > x+5) + puts("Do your own homework!"); + + else if (it->second <= x) puts("Yesss"); + + else puts("Late"); + } +} diff --git a/uva_cpp_clean/11926/11926-11.cpp b/uva_cpp_clean/11926/11926-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..813372c9ddb35276cd4bd5371f3f2ee4e0c87dcc --- /dev/null +++ b/uva_cpp_clean/11926/11926-11.cpp @@ -0,0 +1,77 @@ +#include +/* +Problem: 11926 - Multitasking +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3077 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int N, M; +bitset<1000002> calendar; + +bool testRange(int start, int end){ + for (int i = start+1; i <= end; ++i){ + if (calendar.test(i)) return true; + else calendar.set(i); + } + + return false; +} + +int main () { + fastio; + + while ( cin >> N >> M && (N || M)){ + bool conflict = false; + int start, end, interval; + calendar.reset(); + + while ((N--)){ + cin >> start >> end; + conflict |= testRange(start, end); + } + + while ((M--)){ + cin >> start >> end >> interval; + + while ((start < 1000000)){ + conflict |= testRange(start, end); + + start += interval; + end = min(end+interval, 1000000); + } + } + + cout << (conflict? "CONFLICT" : "NO CONFLICT" ) << '\n'; + } + + return 0; +} +/* +Sample input:- +----------------- + + +Sample output:- +----------------- + + +Resources:- +------------- + + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11926/11926-19.cpp b/uva_cpp_clean/11926/11926-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c83063ce8d00183a0d3efe6fdfedd1a1587eacdc --- /dev/null +++ b/uva_cpp_clean/11926/11926-19.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +int main() { + bitset<1000010> bs; + int n, m; + while (scanf("%d %d", &n, &m) && !(n == 0 && m == 0)) { + bool ok = true; + bs.reset(); + for (int i = 0; i < n; i++) { + int start, end; + scanf("%d %d", &start, &end); + if (ok) { + for (int i = start; i < end && ok; i++) { + ok = !(bs[i]); + bs.set(i); + } + } + } + for (int i = 0; i < m; i++) { + int start, end, inter, pos; + scanf("%d %d %d", &start, &end, &inter); + if (ok) { + pos = start; + while (pos <= 1000000 && ok) { + for (int i = 0; i < end - start && i+pos <= 1000000 && ok; i++) { + ok = !(bs[i+pos]); + bs.set(i+pos); + } + pos += inter; + } + } + } + if (ok) printf("NO "); + printf("CONFLICT\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11926/11926-21.cpp b/uva_cpp_clean/11926/11926-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43e941bcebf0d37494ca7fdc183173d0fced09a0 --- /dev/null +++ b/uva_cpp_clean/11926/11926-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11926 + Name: Multitasking + Problem: https://onlinejudge.org/external/119/11926.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +int main() { + int n, m, s, e, r; + while (scanf("%d%d", &n, &m)==2 && (n||m)) { + bool bits[1000017]={}, conf=0; + for (int i=0; i +#include + +using namespace std; + +#define MIL 1000000 + +bitset timeTable; + +int main(void) { + int normal, + repetitive, + a, b, + rep, s, e; + + while (cin >> normal >> repetitive) { + if (normal == 0 && repetitive == 0) + break; + + bool allesGut = true; + timeTable &= 0; + + while (normal--) { + cin >> a >> b; + + for ( int l = a; l < b; l++ ) { + if (timeTable[l] == 1) + allesGut = false; + + timeTable[l] = 1; + } + } + + while (repetitive--) { + cin >> a >> b >> rep; + + for (int i = 0; b + i * rep < MIL; i++) { + s = a + i * rep; + e = b + i * rep; + + for ( int l = s; l < e; l++ ) { + if (timeTable[l] == true) + allesGut = false; + + timeTable[l] = true; + } + } + } + + if (allesGut) + cout << "NO CONFLICT" << endl; + else + cout << "CONFLICT" << endl; + } + + return 0; + +} diff --git a/uva_cpp_clean/11926/11926-9.cpp b/uva_cpp_clean/11926/11926-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e47bea336d6ef6e54efdefdf964aa045a0c1b26 --- /dev/null +++ b/uva_cpp_clean/11926/11926-9.cpp @@ -0,0 +1,35 @@ +#include + +#define LIM (int)1e6 + +using namespace std; + +int n, m, a, b, c; +bitset <1000010> bs; + +bool ok; + +int main(){ + while(scanf("%d %d",&n, &m),n | m){ + ok = true, bs.reset(); + for(int i = 0; i < n; i++){ + scanf("%d %d", &a, &b); + for(int it = a; it < b; it++){ + if(bs.test(it)) ok = false; + bs.set(it, 1); + } + } + for(int i = 0; i < m; i++){ + scanf("%d %d %d", &a, &b, &c); + int it = a; + while(it <= LIM){ + if(bs.test(it)) ok = false; + bs.set(it, 1); + it++; + if(it == b) a += c, b += c, it = a; + } + } + puts( ok ? "NO CONFLICT" : "CONFLICT" ); + } + return(0); +} diff --git a/uva_cpp_clean/1193/1193-19.cpp b/uva_cpp_clean/1193/1193-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b558251f845f7ba774b8742df564c4958b03d926 --- /dev/null +++ b/uva_cpp_clean/1193/1193-19.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; + +ii islands[1002]; + +int main() { + int n, d; + int cases = 1; + scanf("%d %d", &n, &d); + d *= d; + while (n != 0 && d != 0) { + int cont = 0; + bool ok = true; + for (int i = 0; i < n; i++) { + int x, y; + scanf("%d %d", &x, &y); + int sq = d - y*y; + if (sq < 0) ok = false; + if (ok) { + double r = sqrt(sq); + islands[i].first = x - r; + islands[i].second = x + r; + } + } + if (ok) { + sort(islands, islands+n); + for (int i = 0; i < n; i++) { + cont ++; + int j; + for (j = i + 1; j < n; j++) { + if (islands[i].second >= islands[j].first) { + if(islands[i].second >= islands[j].second) i = j; + } + else { + i = j - 1; + break; + } + } + if(j == n) break; + } + } + else cont = -1; + printf("Case %d: %d\n", cases, cont); + scanf("%d %d", &n, &d); + d *= d; + cases++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1193/1193-21.cpp b/uva_cpp_clean/1193/1193-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd4bc71949a34490c4a73a46da2252b9f37a514a --- /dev/null +++ b/uva_cpp_clean/1193/1193-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1193 + Name: Radar Installation + Problem: https://onlinejudge.org/external/11/1193.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +struct Range { + long double s, e; + Range(long double s, long double e): s(s),e(e) {} + bool operator < (const Range &r) const { + return s < r.s; + } +}; + + +int countmin(const vector &vec) { + int cnt = 0; + for (int i=0; i>n>>r && (n||r)) { + bool possible = true; + vector vec; + long long r2 = r*r; + for (int i=0; i>x>>y; + if (possible && y <= r) { + long double dif = sqrt(r2 - y*y); + vec.push_back(Range(x-dif, x+dif)); + } + else possible = false; + } + sort(vec.begin(), vec.end()); + + if (!possible) printf("Case %d: -1\n", cse++); + else printf("Case %d: %d\n", cse++, countmin(vec)); + } +} diff --git a/uva_cpp_clean/1193/1193-9.cpp b/uva_cpp_clean/1193/1193-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d26a8c72de4eaae5f07cd5b1ab58efe2979da980 --- /dev/null +++ b/uva_cpp_clean/1193/1193-9.cpp @@ -0,0 +1,31 @@ +#include + +#define SIZE 1010 + +using namespace std; + +int n, d, ans, tc, j, x, y; +pair v[SIZE]; +bool check; +double dx; + +int main(){ + while(scanf("%d %d", &n, &d), n | d){ + check = true; + for(int i = 0; i < n; i++){ + scanf("%d %d", &x, &y); + if(y > d) check = false; + dx = sqrt(d * d - y * y); + v[i] = make_pair(x + dx, x - dx); + } + sort(v ,v + n); + ans = 0; + for(int i = 0; check && i < n; ans++){ + for(j = i; j = v[j].second; j++) ; + i = j; + } + ans = check ? ans : -1; + printf("Case %d: %d\n", ++tc, ans); + } + return(0); +} diff --git a/uva_cpp_clean/11930/11930-8.cpp b/uva_cpp_clean/11930/11930-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2bd78c0831e1811072bcadbfb9e6c9e7998d7d71 --- /dev/null +++ b/uva_cpp_clean/11930/11930-8.cpp @@ -0,0 +1,132 @@ +// Problem name: Rectangles +// Problem link: https://vjudge.net/problem/UVA-11930 +// Submission link: https://vjudge.net/solution/16352874 + +#include + +#define EPS ((double)1e-12) +#define pb push_back +#define VAR(x) ((x) << 1) +#define NOT(x) ((x) ^ 1) +#define endl '\n' + +using namespace std; + +struct two_satisfability{ + int n, time; + vector dt, low, scc; + vector> G; + stack s; + + two_satisfability(int n) : n(n), time(0), dt(n << 1, 0), low(n << 1, 0), scc(n << 1), G(n << 1){ } + + void add_set(int u){ add_or(u, u); } + void add_not(int u){ add_or(NOT(u), NOT(u)); } + void add_or(int u, int v){ G[ NOT(u) ].pb(v), G[ NOT(v) ].pb(u); } + void add_nor(int u, int v){ add_or(NOT(u), NOT(u)); add_or(NOT(v), NOT(v)); } + void add_and(int u, int v){ add_or(u, u); add_or(v, v); } + void add_nand(int u, int v){ add_or(NOT(u), NOT(v)); } + void add_xor(int u, int v){ add_or(u, v); add_or(NOT(u), NOT(v)); } + void add_implication(int u, int v){ add_or(NOT(u), v); } + void add_double_implication(int u, int v){ add_or(NOT(u), v); add_or(u, NOT(v)); } + + void dfs(int v){ + dt[ v ] = low[ v ] = ++time; + s.push(v); + for(int u : G[ v ]) + low[ v ] = min(low[ v ], !dt[ u ] ? dfs(u), low[ u ] : (!scc[ u ] ? dt[ u ] : low[ v ])); + if(dt[ v ] == low[ v ]){ + do{ + scc[ s.top() ] = time, s.pop(); + }while(!scc[ v ]); + } + } + + bool solve(){ + for(int i = 0 ; i < n ; ++i){ + int x = VAR(i); + if(!dt[ x ]) dfs(x); + if(!dt[ NOT(x) ]) dfs(NOT(x)); + if(scc[ x ] == scc[ NOT(x) ]) return 0; + } + return 1; + } +}; + +struct point{ + double x, y; + point() : x(0), y(0){ } + point(double x, double y) : x(x), y(y){ } + point(const point &p) : x(p.x), y(p.y){ } + point operator -(const point &p) const{ return point(x - p.x, y - p.y); } + friend istream &operator >>(istream &in, point &p){ in >> p.x >> p.y; return in; } +}; + +double dot(point a, point b){ return a.x * b.x + a.y * b.y; } +double dist2(point a, point b){ return dot(a - b, a - b); } +double cross(point a, point b){ return a.x * b.y - a.y * b.x; } + +bool lines_parallel(point a, point b, point c, point d){ + return fabs(cross(b - a, c - d)) < EPS; +} + +bool lines_collinear(point a, point b, point c, point d){ + return lines_parallel(a, b, c, d) && fabs(cross(a - b, a - c)) < EPS && fabs(cross(c - d, c - a)) < EPS; +} + +bool segments_intersect(point a, point b, point c, point d){ + if(lines_collinear(a, b, c, d)){ + if(dist2(a, c) < EPS || dist2(a, d) < EPS || dist2(b, c) < EPS || dist2(b, d) < EPS) + return true; + if(dot(c - a, c - b) > 0 && dot(d - a, d - b) > 0 && dot(c-b, d-b) > 0) + return false; + return true; + } + if(cross(d - a, b - a) * cross(c - a, b - a) > 0) + return false; + if(cross(a - c, d - c) * cross(b - c, d - c) > 0) + return false; +return true; +} + +struct rectangle{ + point p1, p2, p3, p4; + + rectangle(point _p1, point _p2, point _p3, point _p4) : p1(_p1), p2(_p2), p3(_p3), p4(_p4){ + if(dist2(p1, p2) > dist2(p1, p3)) + swap(p2, p3); + if(dist2(p1, p3) < dist2(p1, p4)) + swap(p3, p4); + } +}; + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0); + +#ifdef JoseA132 + freopen("01.in", "r", stdin); +#endif + + int n; + while(cin >> n && n){ + two_satisfability ts(n); + vector r; + for(int i = 0 ; i < n ; ++i){ + point p1, p2, p3, p4; + cin >> p1 >> p2 >> p3 >> p4; + r.pb(rectangle(p1, p2, p3, p4)); + for(int j = 0 ; j < i ; ++j){ + if(segments_intersect(r[ i ].p1, r[ i ].p3, r[ j ].p1, r[ j ].p3)) + ts.add_nand(VAR(i), VAR(j)); + if(segments_intersect(r[ i ].p1, r[ i ].p3, r[ j ].p2, r[ j ].p4)) + ts.add_nand(VAR(i), NOT(VAR(j))); + if(segments_intersect(r[ i ].p2, r[ i ].p4, r[ j ].p1, r[ j ].p3)) + ts.add_nand(NOT(VAR(i)), VAR(j)); + if(segments_intersect(r[ i ].p2, r[ i ].p4, r[ j ].p2, r[ j ].p4)) + ts.add_nand(NOT(VAR(i)), NOT(VAR(j))); + } + } + cout << (ts.solve() ? "YES" : "NO") << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11933/11933-11.cpp b/uva_cpp_clean/11933/11933-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23a17dbde85c69177dee12adf3513d2f2c706d91 --- /dev/null +++ b/uva_cpp_clean/11933/11933-11.cpp @@ -0,0 +1,62 @@ +#include +/* +Problem: 11933 - Splitting Numbers +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=3084 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +long long int N, n1, n2; + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + + while ( cin >> N && N ){ + bool even = 1; // the first bit index is 0 and 0 is an even number + n1 = 0, n2 = 0; + + for (int i = 0; i < 32; i++){ + if ( !(N & (1< +using namespace std; + +int main() { + unsigned int n, a, b; + while (scanf("%d", &n) && n != 0) { + a = b = 0; + int bits = 1; + for (int i = 0; i < 32; i++) { + int val = n & (1 << i); + if (val) { + if (bits % 2) a |= (1 << i); + else b |= (1 << i); + bits++; + } + } + printf("%d %d\n", a, b); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11933/11933-21.cpp b/uva_cpp_clean/11933/11933-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1dcafb01ce55504951f47ebf3adf5f7b7029aaf --- /dev/null +++ b/uva_cpp_clean/11933/11933-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11933 + Name: Splitting Numbers + Problem: https://onlinejudge.org/external/119/11933.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +int main() { + int n, a, b, t; + while (scanf("%d", &n)==1 && n) { + a = b = 0; + while (n) { + t = n & -n; + a |= t; + n -= t; + t = n & -n; + b |= t; + n -= t; + } + printf("%d %d\n", a, b); + } +} diff --git a/uva_cpp_clean/11933/11933-3.cpp b/uva_cpp_clean/11933/11933-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..302b1bf5bca81f0af211f444af64c62eaaad5c3a --- /dev/null +++ b/uva_cpp_clean/11933/11933-3.cpp @@ -0,0 +1,50 @@ +// Problem UVa ID: 11933 +#include + +#define isOn(S, j) (S & ( 1 << j )) + +int leastSB ( int toCheck ){ + int result = 0; + bool notFound = true ; + + while (notFound){ + if ( isOn (toCheck, result) ){ + notFound = false; + break; + } + result++; + } + return result; +} + +int main (){ + // Problem description: Given an integer N split it + int N; + // Assign value passed by the user + scanf("%d",&N); + + // Those are the two (derivative) integers from N. Values will be assigned using + int oddBits,evenBits; + + // The input will contain more than one test case: + while ( N != 0 ){ + // Resetting the output vars' value + oddBits = 0; + evenBits = 0; + + // Purely useless var to check whether d is odd or even + int count = 0; + + for (int d = leastSB(N); d < 32; d++){ + if ( isOn(N,d) ){ + if ( count % 2 == 0 ) + oddBits |= ( 1 << d ); + else + evenBits |= ( 1 << d ); + count ++; + } + } + printf ("%d %d\n", oddBits, evenBits); + scanf ("%d", &N); + } +} diff --git a/uva_cpp_clean/11933/11933-9.cpp b/uva_cpp_clean/11933/11933-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f084b96c5d80ff7c511f6ef3e8d6b6027fe30f44 --- /dev/null +++ b/uva_cpp_clean/11933/11933-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int i; +long long n, a, b; +bool ind; + +int main(){ + while(scanf("%lld", &n), n){ + ind = true; + a = b = i = 0; + while(n){ + if( (n>>0) & 1 ){ + if(ind) a |= 1<>= 1; + } + cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11934 + Name: Magic Formula + Problem: https://onlinejudge.org/external/119/11934.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int a,b,c,d,l; + while (cin>>a>>b>>c>>d>>l && (a||b||c||d||l)) { + a%=d; + b%=d; + c%=d; + int cnt=0; + for (int i=0; i<=l; i++) + if ( ((((a*i)%d)*i)%d + (b*i)%d + c)%d ==0) + cnt++; + + cout << cnt << endl; + } +} diff --git a/uva_cpp_clean/11934/11934-5.cpp b/uva_cpp_clean/11934/11934-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4770cf5d40f70ed0cfadfcb4fb6f031f2fffba3b --- /dev/null +++ b/uva_cpp_clean/11934/11934-5.cpp @@ -0,0 +1,24 @@ +#include + +int main() +{ + + long long f,i,k,a ,b ,c ,d ,L; + + while(scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&L)==5) + { + if(a==0&&b==0&&c==0&&d==0&&L==0) + break; + k=0; + for(i=0;i<=L;i++) + { + f=(i*i*a)+(i*b)+c; + if(f%d==0) + k++; + } + printf("%lld\n",k); + } + + + return 0; +} diff --git a/uva_cpp_clean/11934/11934-9.cpp b/uva_cpp_clean/11934/11934-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc0201dfc05be7246d0b5b64b4be0222880f4be6 --- /dev/null +++ b/uva_cpp_clean/11934/11934-9.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main(){ + int a, b, c, L, d, ans, i; + while(scanf("%d %d %d %d %d", &a, &b, &c, &d, &L), a || b || c || L || d){ + ans=0; + for(i = 0; i <= L; i++) + if(!(((a * i * i) % d + (b * i) % d + c % d) % d)) + ans++; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11935/11935-21.cpp b/uva_cpp_clean/11935/11935-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d058122002f7859676ee207d7e356646de02102a --- /dev/null +++ b/uva_cpp_clean/11935/11935-21.cpp @@ -0,0 +1,72 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11935 + Name: Through the Desert + Problem: https://onlinejudge.org/external/119/11935.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int D[55], E[55], F[55], n; +bool isEnough(double cap) { + int ld = 0, lc = 0, ll = 0; + double fuel = cap; + for (int i=0; i<=n; ++i) { + int d = D[i] - ld; + fuel -= lc * d / 100.0; + fuel -= ll * d; + ld = D[i]; + if (fuel < 0) return 0; + + switch (E[i]) { + case 1: lc = F[i]; break; + case 2: ++ll; break; + case 3: fuel = cap; break; + case 4: ll = 0; break; + } + } + return 1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (1) { + string cm, tmp; + for (n=0; cin >> D[n] >> cm; ++n) { + if (cm == "Fuel" ) { + cin >> tmp >> F[n]; + if (!n && !D[n] && !F[n]) return 0; + E[n] = 1; + } + else if (cm == "Leak") { + E[n] = 2; + } + else if (cm == "Gas") { + cin >> tmp; + E[n] = 3; + } + else if (cm == "Mechanic") { + E[n] = 4; + } + else break; + } + + double lo = 0, hi = 1e10; + while (hi-lo > 1e-9) { + double mid = (lo + hi) / 2; + if (isEnough(mid)) + hi = mid; + else + lo = mid; + } + + cout << fixed << setprecision(3) << lo << "\n"; + } +} diff --git a/uva_cpp_clean/11935/11935-9.cpp b/uva_cpp_clean/11935/11935-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e56556ddeab94ff05712bf5a99ca0a3fdad2b615 --- /dev/null +++ b/uva_cpp_clean/11935/11935-9.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main(){ + char s[100], c1[100], c2[100]; + float k, ans, ln, cn, l, c, lpos, cpos; + while(cin.getline(s, 100)){ + l = lpos = ln = ans = c = 0; + k = sscanf(s, "%f %s %s %f", &cpos, c1, c2, &ln); + if(cpos == 0 && ln == 0) + break; + lpos = cpos; + cn = ln; + while(true){ + c += l * (cpos - lpos) + ln * (cpos - lpos) / 100; + ans = max(ans, c); + lpos = cpos; + if(k == 4) + ln = cn; + if(c1[0] == 'L') + l++; + else if(c1[1] == 'a') + c = 0; + else if(c1[0] == 'M') + l = 0; + else if(c1[1] == 'o') + break; + cin.getline(s, 100); + k = sscanf(s, "%f %s %s %f", &cpos, c1, c2, &cn); + } + printf("%.3f\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/11936/11936-19.cpp b/uva_cpp_clean/11936/11936-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9bf0b4952f72227b27cec87326c86943eafdd390 --- /dev/null +++ b/uva_cpp_clean/11936/11936-19.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +int main() { + int T; + cin >> T; + while (T--) { + int a, b, c; + cin >> a >> b >> c; + if (a < b + c && b < a + c && c < a + b && a > 0 && b > 0 && c > 0) printf("OK\n"); + else printf("Wrong!!\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11936/11936-21.cpp b/uva_cpp_clean/11936/11936-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dba6548f0e9a58e931efe037f4d7d9993108c797 --- /dev/null +++ b/uva_cpp_clean/11936/11936-21.cpp @@ -0,0 +1,26 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11936 + Name: The Lazy Lumberjacks + Problem: https://onlinejudge.org/external/119/11936.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int n, a[3]; + cin>>n; + while(n--) { + cin>>a[0]>>a[1]>>a[2]; + sort(a,a+3); + cout << (a[0]+a[1]<=a[2] ? "Wrong!!\n" : "OK\n"); + } +} diff --git a/uva_cpp_clean/11936/11936-5.cpp b/uva_cpp_clean/11936/11936-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e693039bb1abaf28858460aabddd01a4ffe32197 --- /dev/null +++ b/uva_cpp_clean/11936/11936-5.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int t; + sc("%d",&t); + while(t--) + { + ll a,b,c; + sc("%lld%lld%lld",&a,&b,&c); + if(a+b>c && b+c>a && a+c>b) + pf("OK\n"); + else + pf("Wrong!!\n"); + } + return 0; +} + diff --git a/uva_cpp_clean/1194/1194-21.cpp b/uva_cpp_clean/1194/1194-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b838ce6e8a89574bd50810c687259fc123d0631 --- /dev/null +++ b/uva_cpp_clean/1194/1194-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1194 + Name: Machine Schedule + Problem: https://onlinejudge.org/external/11/1194.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +vector adj[143]; +int matchL[143], matchR[143]; +bool seen[143]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + if (matchR[v]==-1 || bpm(matchR[v])) { + matchR[v] = u; + matchL[u] = v; + return true; + } + } + return false; +} + +// http://www.immorlica.com/combOpt/lec1.pdf +void konig(int u) { + matchL[u] = -1; + for (int v: adj[u]) + if (!seen[v] && matchR[v]>=0) { + seen[v] = 1; + konig(matchR[v]); + } +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int l, r, n; + while (cin >> l >> r >> n) { + for (int i=0; i<=l; ++i) + adj[i].clear(); + + adj[0].push_back(0); + for (int i=0, t,u,v; i> t >> u >> v; + if (u || v) + adj[u].push_back(v); + } + + int cnt = 0; + memset(matchL, -1, l*sizeof(int)); + memset(matchR, -1, r*sizeof(int)); + for (int i=0; i=0) --cnt; + if (seen[0]) --cnt; + cout << cnt << '\n'; + } +} diff --git a/uva_cpp_clean/11942/11942-14.cpp b/uva_cpp_clean/11942/11942-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78fb2f9f22a7bb5c9150cb7c4ec6ffbdc3bc535c --- /dev/null +++ b/uva_cpp_clean/11942/11942-14.cpp @@ -0,0 +1,79 @@ +/*************************************************** + * Problem name : 11942 Lumberjack Sequencing.cpp + * Problem Link : https://uva.onlinejudge.org/external/119/11942.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-27 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc, n = 10; + int ar[11]; + scanf("%d", &tc); + printf("Lumberjacks:\n"); + for (int t = 1; t <= tc; t++) { + for (int i = 0; i < n; i++) { + scanf("%d", &ar[i]); + } + int cnt = 0; + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (ar[i] > ar[j]) { + cnt++; + } + } + } + if(cnt == 0 || cnt == 45) printf("Ordered\n"); + else printf("Unordered\n"); + //~ printf("%d\n", cnt); + } + return 0; +} + diff --git a/uva_cpp_clean/11942/11942-18.cpp b/uva_cpp_clean/11942/11942-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..34839c386c01f39e62387b01ec6092dd4b9428c2 --- /dev/null +++ b/uva_cpp_clean/11942/11942-18.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int main(){ + bool asc, desc; + int n, lj[21]; + scanf("%d", &n); + printf("Lumberjacks:\n"); + while(n-- > 0){ + asc = desc = false; + for(int i = 0; i < 10; i++) scanf("%d", &lj[i]); + for(int i = 0; i < 9; i++){ + if(lj[i] > lj[i + 1]) desc = true; + else asc = true; + } + if(asc && desc) printf("Unordered\n"); + else printf("Ordered\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11942/11942-19.cpp b/uva_cpp_clean/11942/11942-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9410847f059283c05c5f34f095d61149528a133 --- /dev/null +++ b/uva_cpp_clean/11942/11942-19.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +int main() { + int N; + cin >> N; + printf("Lumberjacks:\n"); + while (N--) { + bool or1 = true, or2 = true; + int prev, act; + cin >> prev; + for (int i = 1; i < 10; i++) { + cin >> act; + or1 &= (act >= prev); + or2 &= (act <= prev); + prev = act; + } + if (or1 || or2) printf("Ordered\n"); + else printf("Unordered\n"); + } + return 0; +} diff --git a/uva_cpp_clean/11942/11942-2.cpp b/uva_cpp_clean/11942/11942-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..934a6f98c841071c4a94962b805b6f383c83229d --- /dev/null +++ b/uva_cpp_clean/11942/11942-2.cpp @@ -0,0 +1,61 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; cin >> tc; + printf("Lumberjacks:\n"); + while (tc--) { + vi v(10); + for (int i = 0; i < 10; ++i) cin >> v[i]; + bool as = 0, f = 1; + if (v[0] > v[1]) as = 1; + for (int i = 1; i < 10; ++i) { + if (as) { + if (v[i-1] < v[i]) { + f = 0; + break; + } + } else { + if (v[i-1] > v[i]) { + f = 0; + break; + } + } + } + if (!f) printf("Unordered\n"); + else printf("Ordered\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11942/11942-20.cpp b/uva_cpp_clean/11942/11942-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ec3a6657cafddec54b0bb32ff1731bc5dadccf7 --- /dev/null +++ b/uva_cpp_clean/11942/11942-20.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +int N[12]; + +int main() +{ + int tc; + + cin >> tc; + cout << "Lumberjacks:" << endl; + while( tc-- ) + { + cin >> N[0]; + + bool ascend = false, descend = false; + for(int i=1 ; i<10 ; i++) + { + cin >> N[i]; + + if( N[i-1]>N[i] ) descend = true; + else ascend = true; + } + + if( ascend && descend ) cout << "Unordered" << endl; + else cout << "Ordered" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/11942/11942-21.cpp b/uva_cpp_clean/11942/11942-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09f8b44aa357a2a4ba919c616585e1892408f8b5 --- /dev/null +++ b/uva_cpp_clean/11942/11942-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11942 + Name: Lumberjack Sequencing + Problem: https://onlinejudge.org/external/119/11942.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int nums[20]; +bool check() { + bool f = nums[0] < nums[1]; + for (int i=1; i<10; i++) + if (f ^ (nums[i-1]>n; + cout<<"Lumberjacks:\n"; + while (n--) { + for (int i=0; i<10; i++) + cin>>nums[i]; + + cout << (check() ? "Ordered\n" : "Unordered\n"); + } +} diff --git a/uva_cpp_clean/11942/11942-5.cpp b/uva_cpp_clean/11942/11942-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c0ae6cff89cc0ee80fc3875db6f967a0dc5318ae --- /dev/null +++ b/uva_cpp_clean/11942/11942-5.cpp @@ -0,0 +1,48 @@ +#include +int main() +{ + int n,c,i,a[15],up,down; + printf("Lumberjacks:\n"); + while(scanf("%d",&n)==1) + { + while(n--) + { + for(i=0;i<10;i++) + { + scanf("%d",&a[i]); + } + up=a[0]; + down=a[1]; + + if(up>down) + { + c=0; + for(i=0;i<9;i++) + { + if(a[i]>a[i+1]) + { + c++; + } + } + } + else if(up + +int main(){ + int n, min, max, temp, ind1, ind2, i; + scanf("%d", &n); + printf("Lumberjacks:\n"); + while(n--){ + scanf("%d",&temp); + min = max = temp; + ind1 = ind2 = 0; + for(i = 0; i < 9; i++){ + scanf("%d", &temp); + if(temp > min){ + min = temp; + ind1++; + } + else if(temp < max){ + max = temp; + ind2++; + } + else{ + ind1++; + ind2++; + } + } + if(ind1 == 9 || ind2 == 9) printf("Ordered\n"); + else printf("Unordered\n"); + } + return(0); +} diff --git a/uva_cpp_clean/11942/main.cpp b/uva_cpp_clean/11942/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e84c23e4d55ede83ef43af94ad74419b60684f4f --- /dev/null +++ b/uva_cpp_clean/11942/main.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int main(){ + cout << "Lumberjacks:\n"; + short n, actual, next; + cin >> n; + for(short i = 0; i < n; i++){ + bool res = true; + cin >> actual >> next; + if(actual < next){ + for(short c = 0; c < 8; c++){ + actual = next; + cin >> next; + if(actual > next) res = false; + } + }else{ + for(short c = 0; c < 8; c++){ + actual = next; + cin >> next; + if(actual < next) res = false; + } + } + if(res) cout << "Ordered\n"; + else cout << "Unordered\n"; + } + + +return 0; +} diff --git a/uva_cpp_clean/11945/11945-14.cpp b/uva_cpp_clean/11945/11945-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ea07248528ef49e32ced40421815247d1e170d33 --- /dev/null +++ b/uva_cpp_clean/11945/11945-14.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#define MAX 10000 +using namespace std; +int main() +{ + double ar[MAX],sum,multi; + int rem; + int n; + scanf("%d", &n); + for(int t = 1; t<=n; t++) { + sum = 0; + for(int i = 0; i<12; i++) { + scanf("%lf", &ar[i]); + sum+=ar[i]; + } + double avg = sum/12.0; + std::cout << std::fixed; + std::cout << std::setprecision(2); + if(avg>=1000) { + rem = avg/1000; + multi = avg - rem*1000; + std::cout << std::fixed; + std::cout << std::setprecision(2); + if(multi<10.0) { + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +char code(char ch) { + if (ch == '4') return 'A'; + else if (ch == '8') return 'B'; + else if (ch == '3') return 'E'; + else if (ch == '6') return 'G'; + else if (ch == '1') return 'I'; + else if (ch == '0') return 'O'; + else if (ch == '9') return 'P'; + else if (ch == '5') return 'S'; + else if (ch == '7') return 'T'; + else if (ch == '2') return 'Z'; + else return ch; +} +int main () { + int tc; + string str; + scanf("%d", &tc); + getchar(); + for (int t = 1; t <= tc; t++) { + while (getline(cin, str)) { + int l = str.size(); + if (l == 0) break; + for (int i = 0; i < l; i++) { + cout << code(str[i]); + } + nl; + } + if(t!=tc) nl; + } + return 0; +} + diff --git a/uva_cpp_clean/11946/11946-19.cpp b/uva_cpp_clean/11946/11946-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d282a88700f09dc4816d0b6f20b15b4a353df2aa --- /dev/null +++ b/uva_cpp_clean/11946/11946-19.cpp @@ -0,0 +1,68 @@ +// las12uvas contest PROBLEM G 2015 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector
vdd; +typedef vector vll; +typedef vector vvi; + +char tonom(char c) { + switch(c) { + case '0': return 'O'; + case '1': return 'I'; + case '2': return 'Z'; + case '3': return 'E'; + case '4': return 'A'; + case '5': return 'S'; + case '6': return 'G'; + case '7': return 'T'; + case '8': return 'B'; + case '9': return 'P'; + default: return c; + } +} + +int main() { + int cases; + scanf("%d", &cases); + string str; + getline(cin, str); + for (int i = 0; i < cases; i++) { + while(getline(cin, str) && str != "") { + transform(str.begin(), str.end(), str.begin(), tonom); + cout << str << endl; + } + if (i != cases -1)cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11946/11946-21.cpp b/uva_cpp_clean/11946/11946-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0865764ca32fe9d8119083f83f5df182bf206229 --- /dev/null +++ b/uva_cpp_clean/11946/11946-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11946 + Name: Code Number + Problem: https://onlinejudge.org/external/119/11946.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + int T; + cin >> T; + cin.ignore(100, '\n'); + + char ch; + char D[] = "OIZEASGTBP"; + while ( (ch=getchar()) != EOF ) + putchar(ch>='0' && ch<='9' ? D[ch-'0'] : ch); +} diff --git a/uva_cpp_clean/11946/11946-5.cpp b/uva_cpp_clean/11946/11946-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4fdd5bb252048ddfd6e8f75e672f1be36d82f3b5 --- /dev/null +++ b/uva_cpp_clean/11946/11946-5.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + int t,k; + string a; + sc("%d",&t); + getchar(); + for(k=0;k + +using namespace std; + +const int LEN = 110; + +int tc; +map mp; +char line[LEN]; +bool first = true; + +int main(){ + mp[0] = 'O', mp[1] = 'I', mp[2] = 'Z', mp[3] = 'E', mp[4] = 'A'; + mp[5] = 'S', mp[6] = 'G', mp[7] = 'T', mp[8] = 'B', mp[9] = 'P'; + scanf("%d\n", &tc); + while(tc--){ + if(not first) putchar('\n'); + first = false; + while(cin.getline(line, LEN) and line[0] != '\0'){ + for(int it = 0; line[it]; it++) + putchar( isdigit(line[it]) ? mp[line[it] - '0'] : line[it]); + putchar('\n'); + } + } + return(0); +} diff --git a/uva_cpp_clean/11947/11947-19.cpp b/uva_cpp_clean/11947/11947-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5d618e50ed24219d0990a20b0e1e77790f6d728 --- /dev/null +++ b/uva_cpp_clean/11947/11947-19.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +string sign(int day, int mon) { + switch (mon) { + case 0: return (day <= 20? "capricorn": "aquarius"); + case 1: return (day <= 19? "aquarius": "pisces"); + case 2: return (day <= 20? "pisces": "aries"); + case 3: return (day <= 20? "aries": "taurus"); + case 4: return (day <= 21? "taurus": "gemini"); + case 5: return (day <= 21? "gemini": "cancer"); + case 6: return (day <= 22? "cancer": "leo"); + case 7: return (day <= 21? "leo": "virgo"); + case 8: return (day <= 23? "virgo": "libra"); + case 9: return (day <= 23? "libra": "scorpio"); + case 10: return (day <= 22? "scorpio": "sagittarius"); + case 11: return (day <= 22? "sagittarius": "capricorn"); + } + return ""; +} + +int main() { + int n; + cin >> n; + for (int i = 1; i <= n; i++) { + int date; + struct tm t = {0}; + cin >> date; + t.tm_year = date%10000 - 1900; + t.tm_mday = (date/10000)%100; + t.tm_mon = (date/1000000) - 1; + mktime(&t); + t.tm_mday += 280; + mktime(&t); + printf("%d %.2d/%.2d/%.2d %s\n", i, t.tm_mon + 1, t.tm_mday, t.tm_year + 1900, sign(t.tm_mday, t.tm_mon).c_str()); + } + return 0; +} diff --git a/uva_cpp_clean/11947/main.cpp b/uva_cpp_clean/11947/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cdb9c7943e303e51e0f0577eb74e7756fdaa883 --- /dev/null +++ b/uva_cpp_clean/11947/main.cpp @@ -0,0 +1,109 @@ +#include +#include +using namespace std; + +int main(){ + int cases, idate, iday, imonth, iyear, fday, fmonth, fyear; + cin >> cases; + for(int i = 1; i <= cases; i++){ + cin >> idate; + imonth = idate/1000000; + idate %= 1000000; + iday = idate/10000; + iyear = idate%10000; + fday = iday; + fmonth = imonth; + fyear = iyear; + int remaining = 280; + int passed = 0; + while(remaining > 0){ + switch (fmonth) { + case 1: + passed = 32 - fday; + break; + case 3: + passed = 32 - fday; + break; + case 5: + passed = 32 - fday; + break; + case 7: + passed = 32 - fday; + break; + case 8: + passed = 32 - fday; + break; + case 10: + passed = 32 - fday; + break; + case 12: + passed = 32 - fday; + break; + case 2: + if(fyear%400 == 0 || (fyear % 4 == 0 && fyear % 100 != 0)) passed = 30 - fday; + else passed = 29 - fday; + break; + default: passed = 31 - fday; + } + + if(passed <= remaining){ + fday = 1; + fmonth += 1; + remaining -= passed; + }else{ + fday += remaining; + remaining = 0; + } + if(fmonth == 13){ + fmonth = 1; + fyear += 1; + } + } + int total = fday * fmonth; + string sign = ""; + + if(fmonth == 1){ + if(fday < 21) sign = "capricorn"; + else sign = "aquarius"; + }else if(fmonth == 2){ + if(fday <= 19) sign = "aquarius"; + else sign = "pisces"; + }else if(fmonth == 3){ + if(fday <= 20) sign = "pisces"; + else sign = "aries"; + }else if(fmonth == 4){ + if(fday <= 20) sign = "aries"; + else sign = "taurus"; + }else if(fmonth == 5){ + if(fday <= 21) sign = "taurus"; + else sign = "gemini"; + }else if(fmonth == 6){ + if(fday <= 21) sign = "gemini"; + else sign = "cancer"; + }else if(fmonth == 7){ + if(fday <= 22) sign = "cancer"; + else sign = "leo"; + }else if(fmonth == 8){ + if(fday <= 21) sign = "leo"; + else sign = "virgo"; + }else if(fmonth == 9){ + if(fday <= 23) sign = "virgo"; + else sign = "libra"; + }else if(fmonth == 10){ + if(fday <= 23) sign = "libra"; + else sign = "scorpio"; + }else if(fmonth == 11){ + if(fday <= 22) sign = "scorpio"; + else sign = "sagittarius"; + }else if(fmonth == 12){ + if(fday <= 22) sign = "sagittarius"; + else sign = "capricorn"; + } + + + printf("%d %02d/%02d/%d %s\n",i, fmonth, fday, fyear, sign.c_str()); + } + + +return 0; +} diff --git a/uva_cpp_clean/1195/1195-21.cpp b/uva_cpp_clean/1195/1195-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1baf0b645c74ea4821fbe4f117a13fd03c888bbe --- /dev/null +++ b/uva_cpp_clean/1195/1195-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1195 + Name: Calling Extraterrestrial Intelligence Again + Problem: https://onlinejudge.org/external/11/1195.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +bool isnp[100100]; +int prv[100100], prs[100100]={0,0,2}, primes[9600]={2}, pnt=1; +inline bool isprime(int n) { + return n==2 || ((n&1) && !isnp[n]); +} + + +int main() { + int lastp = 2; + for (int i=3; i<100099; i+=2) { + if (!isnp[i]) { + prv[i] = pnt; + lastp = primes[pnt++] = i; + for (int k=2*i; k<100100; k+=i) + isnp[k] = 1; + } + prs[i]=prs[i+1]=lastp; + } + + int m, a, b; + while (scanf("%d%d%d", &m, &a, &b)==3 && (m||a||b)) { + int bp=0, bq=0, nq=0; + + int p = prs[(int)sqrt(m)]; + for (int pi=prv[p]; pi>=0; pi--) { + p=primes[pi]; + int q = prs[m/p]; + if (!nq && p*b < a*q) { + nq = 1; + while (p*b < a*q) + q = primes[ prv[q]-1 ]; + } + + if (p*b < a*q) break; + if (p*q > bp*bq) { + bp = p; + bq = q; + if (p*q == m) break; + } + } + + printf("%d %d\n", bp, bq); + } +} diff --git a/uva_cpp_clean/11951/11951-13.cpp b/uva_cpp_clean/11951/11951-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46601664ed06d3e0cc5aeb4295e706696f768cb8 --- /dev/null +++ b/uva_cpp_clean/11951/11951-13.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 120 + +int main() +{ + ios::sync_with_stdio(false); + + int t, n, m, k, counter = 1; + + cin >> t; + + while (t--) + { + long long area = 0, result = 0; + + cin >> n >> m >> k; + + long long a[MAX][MAX]; + + memset(a, 0, sizeof a); + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + cin >> a[i][j]; + + a[i][j] += a[i][j - 1]; + a[i][j] += a[i - 1][j]; + + a[i][j] -= a[i - 1][j - 1]; + } + } + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + for (int x = i; x <= n; x++) + { + for (int y = j; y <= m; y++) + { + long long tmp = 0; + long long sum = a[x][y]; + + sum -= a[x][j - 1]; + sum -= a[i - 1][y]; + + sum += a[i - 1][j - 1]; + + tmp = (x - i + 1) * (y - j + 1); + + if (sum <= k) + { + if (tmp == area) + { + result = min(result, sum); + } + + else if (tmp > area) + { + result = sum; + area = tmp; + } + } + } + } + } + } + + cout << "Case #" << counter << ": " << area << " " << result << endl; + counter++; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11951/11951-19.cpp b/uva_cpp_clean/11951/11951-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3dee4aeb1f20355f46b4b42be187f2330911d66a --- /dev/null +++ b/uva_cpp_clean/11951/11951-19.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; + +int main() { + long long grid[100][100], K; + int N, M, cases; + scanf("%d", &cases); + for (int c = 1; c <= cases; c++) { + scanf("%d %d %lld", &N, &M, &K); + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + scanf("%lld", &grid[i][j]); + grid[i][j] += i-1 >= 0? grid[i-1][j]: 0; + grid[i][j] += j-1 >= 0? grid[i][j-1]: 0; + grid[i][j] -= i-1 >= 0 && j-1 >= 0? grid[i-1][j-1]: 0; + } + } + long long area = 0, cost = 0; + for (int i0 = 0; i0 < N; i0++) { + for (int i1 = i0; i1 < N; i1++) { + for (int j0 = 0; j0 < M; j0++) { + for (int j1 = j0; j1 < M; j1++) { + long long p = grid[i1][j1]; + if (i0 > 0) p -= grid[i0-1][j1]; + if (j0 > 0) p -= grid[i1][j0-1]; + if (i0 > 0 && j0 > 0) p += grid[i0-1][j0-1]; + long long a = (i1-i0+1)*(j1-j0+1); + if ((a > area && p <= K) || (a == area && p < cost)) area = a, cost = p; + } + } + } + } + printf("Case #%d: %lld %lld\n", c, area, cost); + } + return 0; +} diff --git a/uva_cpp_clean/11951/11951-21.cpp b/uva_cpp_clean/11951/11951-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d850c39f81e27cd49ce1160c5e64824e99fef60 --- /dev/null +++ b/uva_cpp_clean/11951/11951-21.cpp @@ -0,0 +1,93 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11951 + Name: Area + Problem: https://onlinejudge.org/external/119/11951.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n' && ch!='\t') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------------- + +int C[143][143]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + for (int cse=1; cse <= T; ++cse) { + cout << "Case #" << cse << ": "; + int n = readUInt(), + m = readUInt(), + k = readUInt(), + bp = INF, ba = 1; + + for (int i=1; i<=n; ++i) + for (int j=0, x; j k) + cout << "0 0\n"; + + else { + for (int i1=0; i1 k) + sum -= C[i2][lj] - C[i1][lj], + ++lj; + + int ar = h * (j - lj + 1); + if (ar > ba || (ar == ba && sum < bp)) { + bp = sum; + ba = ar; + } + } + } + cout << ba << ' ' << bp << '\n'; + } + } +} diff --git a/uva_cpp_clean/11951/11951-9.cpp b/uva_cpp_clean/11951/11951-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a60b045018f7bd50df04ad43faf2734cccf77955 --- /dev/null +++ b/uva_cpp_clean/11951/11951-9.cpp @@ -0,0 +1,79 @@ +#include + +#define INF 1L<<60 + +using namespace std; + +int tc, n, m, k, t; +long long cum_sum[100][100], max_area, min_price, current_price, sum; +vector v[100]; + +long long sub_matrix_complete(int pi, int pj, int x, int y){ + sum = 0; + if(pj == 0) + for(int i = pi; i < pi + y; i++){ + sum += cum_sum[i][pj + x - 1]; + if(sum > k) + return INF; + } + else + for(int i = pi; i < pi + y; i++){ + sum += cum_sum[i][pj + x - 1] - cum_sum[i][pj - 1]; + if(sum > k) + return INF; + } + return sum; +} + + +long long sub_matrix(int pi, int pj, int x, int y){ + sum = 0; + if(pj == 0) + for(int i = pi; i < pi + y; i++){ + sum += cum_sum[i][pj + x - 1]; + if(sum > min_price || sum > k) + return INF; + } + else + for(int i = pi; i < pi + y; i++){ + sum += cum_sum[i][pj + x - 1] - cum_sum[i][pj - 1]; + if(sum > min_price || sum > k) + return INF; + } + return sum; +} + +int main(){ + for(int i = 0; i < 100; i++) + v[i].resize(100); + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d", &n, &m, &k); + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + scanf("%d", &v[i][j]); + memset(cum_sum, 0, sizeof cum_sum); + for(int i = 0; i < n; i++){ + cum_sum[i][0] = v[i][0]; + for(int j = 1; j < m; j++) + cum_sum[i][j] = cum_sum[i][j - 1] + v[i][j]; + } + min_price = max_area = 0; + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + for(int x = 1; x <= m - j; x++) + for(int y = 1; y <= n - i; y++) + if(x * y > max_area){ + current_price = sub_matrix_complete(i, j, x, y); + if(current_price <= k) + min_price = current_price, max_area = x * y; + } + else if(x * y == max_area){ + current_price = sub_matrix(i, j, x, y); + if(current_price < min_price) + min_price = current_price, max_area = x * y; + } + printf("Case #%d: %lld %lld\n", ++t, max_area, min_price); + } + return(0); +} diff --git a/uva_cpp_clean/11953/11953-10.cpp b/uva_cpp_clean/11953/11953-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0f1e8cb00b6c1dc13ba4b4ed3f77e17b84f72c7 --- /dev/null +++ b/uva_cpp_clean/11953/11953-10.cpp @@ -0,0 +1,67 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +using namespace std; +typedef long long ll; +void fast() { + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); +} + +int dx[] ={-1,1,0,0,-1,1,-1,1}; +int dy[] ={0,0,-1,1,-1,1,1,-1}; + +const int N=100+5; + +char grid[N][N]; +bool vis[N][N]; +int n; + +bool isvalid(int i,int j) { + + return (i>=0 && i < n && j >=0 && j>t; + int c=1; + while (t--) { + cin>>n; + memset(grid,0,sizeof grid); + memset(vis,0, sizeof vis); + for(int i=0;i>grid[i][j]; + } + } + int ans=0; + for(int i=0;i +/* +Problem: 11953 - Battleships +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3104 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int T, N; +char grid[101][101]; + +int battleships = 0, x, test_case = 0; +bool visited[101][101]; + +void dfs(int i, int j){ + if (i >= N || j >= N || j < 0 || i < 0) return; + if (visited[i][j] || grid[i][j] == '.') return; + + if (grid[i][j] == 'x') x++; + visited[i][j] = 1; + + dfs(i+1, j); + dfs(i, j+1); +} + +int main () { + scanf("%d\n", &T); + + while ( T-- ){ + scanf("%d\n", &N); + + battleships = 0; + test_case++; + + for (int i = 0; i < N; i++){ + for (int j = 0; j < N; j++){ + scanf("%c", &grid[i][j]); + visited[i][j] = 0; + } + scanf("\n"); + } + + for (int i = 0; i < N; i++){ + for (int j = 0; j < N; j++){ + if (visited[i][j] || grid[i][j] == '.') continue; + x = 0; + dfs(i, j); + if (x) battleships++; + } + } + + printf("Case %d: %d\n", test_case, battleships); + } + + return 0; +} + +/* +Sample input:- +----------------- +2 +4 +x... +..x. +@.@. +.... +2 +.. +x. + +Sample output:- +----------------- +Case 1: 2 +Case 1: 3 + +Resources:- +------------- +Video Solution (Arabic) [Eng Aya Elymany]: https://www.youtube.com/watch?v=nvPucDrmErI + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/11953/11953-12.cpp b/uva_cpp_clean/11953/11953-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..763d6a7030e7c20701bcb5931c0520c939550e4b --- /dev/null +++ b/uva_cpp_clean/11953/11953-12.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + +#define fastio \ + ios_base::sync_with_stdio(0); \ + cin.tie(NULL); \ + cout.tie(NULL) +#define PI(a, b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long int +#define S second +#define F first +#define FOR(var, len) for (var = 0; var < len; var++) +#define debug1 cout << "debug1" << '\n' +#define debug2 cout << "debug2" << '\n' +#define debug3 cout << "debug3" << '\n' + +// Maths Utils +int binExp(int a, int b, int m) +{ + int r = 1; + while (b) + { + if (b % 2 == 1) + r = (r * a) % m; + + a = (a * a) % m; + b = b / 2; + } + return r; +} + +int dr[] = {0, -1, 0, 1, -1, 1, -1, 1}; +int dc[] = {-1, 0, 1, 0, -1, 1, 1, -1}; + +char grid[102][102]; +int visited[102][102]; + +void dfs(int i, int j, int n) +{ + visited[i][j] = 1; + + int x, y; + for (int a = 0; a < 8; a++) + { + x = i + dr[a]; + y = j + dc[a]; + + if (x >= 0 and y >= 0 and x < n and y < n and !visited[x][y]) + { + if (grid[x][y] == 'x' or grid[x][y] == '@') + dfs(x, y, n); + } + } +} + +int32_t main() +{ + fastio; + int tt; + cin >> tt; + for (int t = 1; t <= tt; t++) + { + int n; + cin >> n; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + cin >> grid[i][j]; + + memset(visited, 0, sizeof(visited)); + int ans = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + if (grid[i][j] == 'x' and !visited[i][j]) + { + ans++; + dfs(i,j,n); + } + } + + printf("Case %d: %d\n", t, ans); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11953/11953-13.cpp b/uva_cpp_clean/11953/11953-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9923507d21300d741f7d9710b4640b512818f29 --- /dev/null +++ b/uva_cpp_clean/11953/11953-13.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int test, t = 1, n, ans = 0; + +vector grid; + +int directionX[4] = { 1, 0, -1, 0 }; +int directionY[4] = { 0, 1, 0, -1 }; + +bool ok(int i, int j, int num) +{ + if (i + directionX[num] < n && i + directionX[num] >= 0 && j + directionY[num] < n && j + directionY[num] >= 0) return true; + + else return false; +} + +void dfs(int i, int j) +{ + grid[i][j] = '.'; + + for (int k = 0; k < 4; k++) + { + if (ok(i, j, k) && grid[i + directionX[k]][j + directionY[k]] != '.') + { + dfs(i + directionX[k], j + directionY[k]); + } + } +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n; + + grid.clear(); + + ans = 0; + + for (int i = 0; i < n; i++) + { + string s; + + cin >> s; + + grid.push_back(s); + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (grid[i][j] == 'x') + { + ans++; + + dfs(i, j); + } + } + } + + cout << "Case " << t++ << ": " << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/11953/11953-18.cpp b/uva_cpp_clean/11953/11953-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..da92f83d325ac71e596b6de9aa0a863fd44c37cf --- /dev/null +++ b/uva_cpp_clean/11953/11953-18.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +bool vist[101][101]; +int nI[] = {-1, 1, 0, 0}, nJ[] = {0, 0, 1, -1}; +int n; +vector g; + +void DFS(int i, int j){ + vist[i][j] = true; + g[i][j] = '.'; + for(int k = 0; k < 4; k++){ + int ik = i + nI[k]; + int jk = j + nJ[k]; + if(ik < n && ik >= 0 && jk < n && jk >= 0 && (g[ik][jk] == 'x' || g[ik][jk] == '@')) DFS(ik, jk); + } +} + +int main(){ + int tc, cont = 0, ans; + string inp; + scanf("%d", &tc); + while(cont < tc){ + scanf("%d", &n); + ans = 0; + g.clear(); + memset(vist, false, sizeof(vist)); + for(int i = 0; i < n; i++) cin >> inp, g.push_back(inp); + for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(g[i][j] == 'x') DFS(i, j), ans++; + printf("Case %d: %d\n", ++cont, ans); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11953/11953-19.cpp b/uva_cpp_clean/11953/11953-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b6bc5b0bd4e8811b54970dbe32f6dfa4aa70b55 --- /dev/null +++ b/uva_cpp_clean/11953/11953-19.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +string grid[100]; +bool mask[100][100]; +int N; + +int proc() { + int ships = 0; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (!mask[i][j]) { + if (grid[i][j] == 'x') { + ships++; + mask[i][j] = true; + for (int k = j+1; k < N && grid[i][k] != '.'; k++) mask[i][k] = true; + for (int k = j-1; k >= 0 && grid[i][k] != '.'; k--) mask[i][k] = true; + for (int k = i+1; k < N && grid[k][j] != '.'; k++) mask[k][j] = true; + for (int k = i-1; k >= 0 && grid[k][j] != '.'; k--) mask[k][j] = true; + } + } + } + } + return ships; +} + +int main() { + int T; + cin >> T; + for (int i = 1; i <= T; i++) { + cin >> N; + for (int i = 0; i < N; i++) { + cin >> grid[i]; + for (int j = 0; j < N; j++) mask[i][j] = false; + } + printf("Case %d: %d\n", i, proc()); + } + return 0; +} diff --git a/uva_cpp_clean/11953/11953-21.cpp b/uva_cpp_clean/11953/11953-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45b8da91549848c4db0a7026530c39007c4112b3 --- /dev/null +++ b/uva_cpp_clean/11953/11953-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11953 + Name: Battleships + Problem: https://onlinejudge.org/external/119/11953.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char X[200][200]; +int dx[] = {0,1,0,-1}, + dy[] = {1,0,-1,0}, n; +void dfs(int y, int x) { + if (y<0 || y>=n || x<0 || x>=n || X[y][x]=='.') + return; + + X[y][x] = '.'; + for (int i=0; i<4; ++i) + dfs(y+dy[i], x+dx[i]); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int cse=1, T; cin >> T; + while (T--) { + cin >> n; + for (int i=0; i> X[i]; + + int cnt = 0; + for (int i=0; i + +#define SIZE 110 + +using namespace std; + +int tc, N, ans; +char grid[SIZE][SIZE]; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < N ); +} + +void floodfill(int r, int c){ + if( not is_possible(r, c) ) return; + if( grid[r][c] == '.' ) return; + grid[r][c] = '.'; + for(int d = 0; d < 4; d++) + floodfill(r + dr[d], c + dc[d]); +} + +int solve(){ + ans = 0; + for(int row = 0; row < N; row++) + for(int col = 0; col < N; col++) + if( grid[row][col] == 'x' ) + floodfill(row, col), ans++; + return ans; +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d\n", &N); + for(int row = 0; row < N; row++) scanf("%s", grid[row]); + printf("Case %d: %d\n", t, solve()); + } + return(0); +} diff --git a/uva_cpp_clean/11955/11955-13.cpp b/uva_cpp_clean/11955/11955-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1434b6c78df77a0c95abe43bcb79b8575b7b7be4 --- /dev/null +++ b/uva_cpp_clean/11955/11955-13.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 51 + +int cot = 1; +typedef long long ll; +typedef vector vll; + +ll dp[MAX][MAX]; + +void cal() +{ + dp[0][0] = 0; + dp[1][0] = dp[1][1] = 1; + + for (int i = 2; i < MAX; ++i) + { + dp[i][0] = dp[i][i] = 1; + + for (int j = 1; j < i; ++j) + { + dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; + } + } +} + +void print_ans(int n, string a, string b) +{ + cout << "Case " << cot++ << ": "; + + if (n == 1) cout << a << "+" << b << endl; + + else + { + cout << a << "^" << n << "+"; + + for (int i = 1; i < n; ++i) + { + cout << dp[n][i] << "*" << a; + + if (n - i != 1) cout << "^" << n - i; + + cout << "*" << b; + + if (i != 1) cout << "^" << i; + + cout << "+"; + } + + cout << b << "^" << n << endl; + } +} + +int main() +{ + int t; + + cin >> t; + + cal(); + + while(t--) + { + string s; + + cin >> s; + + string a = "", b = ""; + + int n = 0; + unsigned long inx = s.find('+'); + unsigned long iny = s.find('^'); + + for (int i = 1; i < s.size(); ++i) + { + if (i < inx) + a += s[i]; + + else if (i > inx && i < iny && s[i] != ')') + b += s[i]; + + else if (i > iny) + { + n = (n * 10) + (int)((s[i] - '0')); + } + } + + print_ans(n, a, b); + } +} diff --git a/uva_cpp_clean/11955/11955-14.cpp b/uva_cpp_clean/11955/11955-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..221359235b7916fc7c693b0a32a65ba93ab3e1be --- /dev/null +++ b/uva_cpp_clean/11955/11955-14.cpp @@ -0,0 +1,176 @@ +/*************************************************** + * Problem Name : 11955 Binomial Theorem.cpp + * Problem Link : + * OJ : Uva + * Verdict : AC + * Date : 2020-04-18 + * Problem Type : Implementation + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +ll c_pascal (ll n, ll r) { + ll v = 1; + + for (int i = 1; i <= r; i++) { + v = v * (n + 1 - i) / i; + } + + return v; +} +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + string str; + cin >> str; + string a, b; + int k = 0; + int pos = 1; + + for (; str[pos] != '+'; pos++) { + a += str[pos]; + } + + for (pos = pos + 1; str[pos] != ')'; pos++) { + b += str[pos]; + } + + string s_num = ""; + + for (pos = pos + 2; pos < (int) str.size(); pos++) { + s_num += str[pos]; + } + + k = toInt (s_num); + string ans = ""; + + for (int i = k, j = 0; i >= 0; i--, j++) { + ll co = c_pascal (k, j); + + if (co != 1) { + ans += toString (co); + ans += "*"; + } + + bool f = false; + + if (i > 0) { + ans += a; + f = true; + + if (i > 1) { + ans += "^"; + ans += toString (i); + } + } + + if (j > 0) { + if (f) ans += "*"; + + ans += b; + + if (j > 1) { + ans += "^"; + ans += toString (j); + } + } + + if (i != 0) ans += "+"; + } + + cout << "Case " << t << ": " << ans << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/11955/11955-9.cpp b/uva_cpp_clean/11955/11955-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95309d7a4c77b8021150eb354ac0d57070c06cce --- /dev/null +++ b/uva_cpp_clean/11955/11955-9.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +const int SIZE = 55, LEN = 100; + +int tc, d, n, pos1, pos2, z1, z2; +long long C[SIZE][SIZE]; +char line[LEN], x[LEN], y[LEN]; + +void BinomialCoef(){ + for(int i = 0; i < SIZE; i++) C[i][0] = C[i][i] = 1; + for(int i = 2; i < SIZE; i++) + for(int k = 1; k < i; k++) + C[i][k] = C[i - 1][k] + C[i - 1][k - 1]; +} + +void read(){ + cin.getline(line, LEN); + for(int i = 0; line[i]; i++) + if( line[i] == '+' ) pos1 = i; + else if( line[i] == '^' ) pos2 = i; + d = n = 0; + for(int i = 1; i < pos1; i++) x[d++] = line[i]; + x[d] = '\0', d = 0; + for(int i = pos1 + 1; i + 1 < pos2; i++) y[d++] = line[i]; + y[d] = '\0'; + for(int i = pos2 + 1; line[i]; i++) n = 10 * n + ( line[i] - '0' ); +} + +void print(int t){ + printf("Case %d: ", t); + printf("%s", x); + if( n != 1 ) printf("^%d", n); + for(int k = 1; k < n; k++){ + z1 = n - k, z2 = k; + printf("+%lld*%s", C[n][k], x); + if( z1 != 1 ) printf("^%d", z1); + printf("*%s", y); + if( z2 != 1 ) printf("^%d", z2); + } + printf("+%s", y); + if( n != 1 ) printf("^%d", n); + putchar('\n'); +} + +int main(){ + BinomialCoef(); + scanf("%d\n", &tc); + for(int t = 1; t <= tc; t++){ + read(); + print(t); + } + return(0); +} diff --git a/uva_cpp_clean/11956/11956-2.cpp b/uva_cpp_clean/11956/11956-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..330c438fc105f5ed8b09c09ca38372c8cf34743e --- /dev/null +++ b/uva_cpp_clean/11956/11956-2.cpp @@ -0,0 +1,61 @@ + + +#include "bits/stdc++.h" +#include + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + int tc; cin >> tc; + int t = 1; + char command[100005]; + getchar(); + while (tc--) { + gets(command); + unsigned char mem[100] = {}; + int id = 0; + for(int i = 0; command[i]; ++i) { + if(command[i] == '>') id++; + else if (command[i] == '<') id--; + else if (command[i] == '+') mem[id]++; + else if (command[i] == '-') mem[id]--; + if (id >= 100) id = 0; + if (id < 0 ) id = 99; + } + printf("Case %d:", t++); + REP(i, 100) printf(" %02X", mem[i]); + puts(""); + } + return 0; +} diff --git a/uva_cpp_clean/11956/main.cpp b/uva_cpp_clean/11956/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7c903562eda6ee92478ecfd752c4cc40048d20c --- /dev/null +++ b/uva_cpp_clean/11956/main.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace std; + +int main(){ + short cases; + string line; + char command; + + cin >> cases; + cin.ignore(); + for(int i=1; i <= cases; i++){ + array memory; + for(int c=0; c < 100; c++){ + memory[c] = 0; + } + int position = 0; + getline(cin,line); + // cout << "line size = " << line.size() << endl; + for(int c = 0; c < line.size(); c++){ + command = line.at(c); + // cout << "comando: " <': + if(position == 99) position = 0; + else position++; + break; + case '<': + if(position == 0) position = 99; + else position--; + break; + case '+': + if(memory[position] == 255) memory[position] = 0; + else memory[position] ++; + break; + case '-': + if(memory[position] == 0) memory[position] = 255; + else memory[position]--; + break; + case '.': + cases = cases; + } + } + // cout << "memoria " << memory[0] << endl; + printf("%s %d: ", "Case",i); + for(short c = 0; c < 99; c++){ + printf("%02X ", memory[c]); + } + printf("%02X", memory[99]); + cout << endl; + } + +return 0; +} diff --git a/uva_cpp_clean/11957/11957-21.cpp b/uva_cpp_clean/11957/11957-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7bdfa1c9dcc39bd19839fbdf1c6793f1e5ffc245 --- /dev/null +++ b/uva_cpp_clean/11957/11957-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11957 + Name: Checkers + Problem: https://onlinejudge.org/external/119/11957.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MODUL 1000007 +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char s[143]; + bool B[143][143]; + int T, C[143][143], n; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n; + for (int i=n-1; i>=0; --i) { + memset(C[i], 0, n*sizeof(int)); + cin >> s; + for (int j=0; j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11959 + Name: Dice + Problem: https://onlinejudge.org/external/119/11959.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +string P[64]; +string rotate(string s, int a, int b, int c, int d) { + char sa = s[a]; + s[a] = s[b]; + s[b] = s[c]; + s[c] = s[d]; + s[d] = sa; + return s; +} + +bool check(const string &s1, const string &s2) { + for (int i=0; i<64; ++i) { + bool f = 1; + for (int j=0; f && j<6; ++j) + f &= s1[j] == s2[P[i][j]-'0']; + if (f) return 1; + } + return 0; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string s1 = "012345", s2; + for (int i=0, sz=0; i<4; ++i) { + string s2 = s1 = rotate(s1, 2, 3, 4, 5); + for (int j=0; j<4; ++j) { + string s3 = s2 = rotate(s2, 0, 3, 1, 5); + for (int k=0; k<4; ++k) + P[sz++] = s3 = rotate(s3, 2, 0, 4, 1); + } + } + + int T; + cin >> T; + while (T-- && cin>>s1>>s2) + cout << (check(s1, s2) ? "Equal\n" : "Not Equal\n"); +} diff --git a/uva_cpp_clean/11959/11959-9.cpp b/uva_cpp_clean/11959/11959-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50d9b4fb7ed9bdfda89214a36b4ba98e653715a7 --- /dev/null +++ b/uva_cpp_clean/11959/11959-9.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +bool ok; +int tc,u[10],v[10]; +int w[24][6] = { +{1,2,3,4,5,6},{4,6,3,2,5,1}, +{1,2,4,5,6,3},{4,6,2,5,1,3}, +{1,2,5,6,3,4},{4,6,5,1,3,2}, +{1,2,6,3,4,5},{4,6,1,3,2,5}, +{2,1,6,5,4,3},{6,4,3,1,5,2}, +{2,1,5,4,3,6},{6,4,1,5,2,3}, +{2,1,4,3,6,5},{6,4,5,2,3,1}, +{2,1,3,6,5,4},{6,4,2,3,1,5}, +{3,5,6,2,4,1}, +{3,5,2,4,1,6}, +{3,5,4,1,6,2}, +{3,5,1,6,2,4}, +{5,3,6,1,4,2}, +{5,3,1,4,2,6}, +{5,3,4,2,6,1}, +{5,3,2,6,1,4}, + }; + +int main(){ + scanf("%d",&tc); + while(tc--){ + ok = false; + for(int i = 1;i <= 6; i++) scanf("%1d", &u[i]); + for(int i = 1; i <= 6; i++) scanf("%1d", &v[i]); + for(int i = 0; i < 24; i++){ + if(u[w[i][0]] == v[1] && u[w[i][1]] == v[2] && + u[w[i][2]] == v[3] && u[w[i][3]] == v[4] && + u[w[i][4]] == v[5] && u[w[i][5]] == v[6]){ + ok = true; + break; + } + } + puts(ok ? "Equal" : "Not Equal"); + } + return(0); +} diff --git a/uva_cpp_clean/1196/1196-21.cpp b/uva_cpp_clean/1196/1196-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af05c4f3bc4387c322893b87a56d535fecd280a6 --- /dev/null +++ b/uva_cpp_clean/1196/1196-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1196 + Name: Tiling Up Blocks + Problem: https://onlinejudge.org/external/11/1196.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAX 10005 +int X[MAX], Y[MAX]; +bool comp(int i, int j) { + return X[i] != X[j] + ? X[i] < X[j] + : Y[i] < Y[j]; +} + +int main() { + int n, order[MAX], M[MAX]; + + while (scanf("%d", &n)==1 && n) { + for (int i=0; i>1; + if (Y[M[mid]] <= Y[u]) + lo = mid + 1; + else hi = mid - 1; + } + M[lo] = u; + len = max(len, lo); + } + printf("%d\n", len); + } + puts("*"); +} diff --git a/uva_cpp_clean/1196/1196-9.cpp b/uva_cpp_clean/1196/1196-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c2d279d68ead8aa5843a62bdd94627dda118899 --- /dev/null +++ b/uva_cpp_clean/1196/1196-9.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int n, lis[10010], LIS, pos; +vector < pair > u(10010); +vector v(10010); + +int main(){ + while(scanf("%d",&n), n){ + LIS = 0; + for(int i = 0; i < n; i++) scanf("%d %d", &u[i].first, &u[i].second); + sort(u.begin(), u.begin() + n); + for(int i = 0; i < n; i++) v[i] = u[i].second; + for(int i = 0; i < n; i++){ + pos = lower_bound(lis, lis + LIS, v[i] + 1) - lis; + lis[pos] = v[i]; + if(pos + 1 > LIS) LIS = pos + 1; + } + printf("%d\n", LIS); + } + puts("*"); + return(0); +} diff --git a/uva_cpp_clean/11960/11960-5.cpp b/uva_cpp_clean/11960/11960-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6ce76eb8c62bbc3581e7635a266650a275662391 --- /dev/null +++ b/uva_cpp_clean/11960/11960-5.cpp @@ -0,0 +1,195 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +ll d[mx6],ans[mx6]; + +struct data{ + ll val=0,divs=0; +}; + +void sieve() +{ + for1(i,mx6-6) + { + for(int j=i;j<=mx6-6;j+=i) + { + d[j]++; + } + } +} + +void cal() +{ + data p; + for1(i,mx6-6) + { + if(d[i]>=p.divs) + { + p.divs=d[i]; + p.val=i; + ans[i]=p.val; + } + else + { + ans[i]=p.val; + } +// PPL(p.divs,p.val); + } +} + +int main() +{ + sieve(); + cal(); + int t; + si(t); + while(t--) + { + int n; + si(n); + PL(ans[n]); + } + + + return 0; +} + + diff --git a/uva_cpp_clean/11961/11961-19.cpp b/uva_cpp_clean/11961/11961-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e65dd89d348e5f75a3d561f7d54d67b5ac1dfd37 --- /dev/null +++ b/uva_cpp_clean/11961/11961-19.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +using namespace std; + +char str[12]; +vector sol; +int N, K; +string s; + +void proc(int pos, int K) { + if (pos == N) { + str[pos] = 0; + sol.push_back(string(str)); + return; + } + if (K == 0) { + str[pos] = s[pos]; + proc(pos+1, K); + return; + } + str[pos] = 'A'; + proc(pos+1, str[pos] == s[pos]? K: K-1); + str[pos] = 'C'; + proc(pos+1, str[pos] == s[pos]? K: K-1); + str[pos] = 'G'; + proc(pos+1, str[pos] == s[pos]? K: K-1); + str[pos] = 'T'; + proc(pos+1, str[pos] == s[pos]? K: K-1); +} + +int main() { + int T; + cin >> T; + while (T--) { + cin >> N >> K >> s; + sol.clear(); + proc(0, K); + printf("%ld\n", sol.size()); + for (int i = 0; i < sol.size(); i++) { + printf("%s\n", sol[i].c_str()); + } + } + return 0; +} diff --git a/uva_cpp_clean/11961/11961-21.cpp b/uva_cpp_clean/11961/11961-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d5b89f905c5146ba65fef7b9228df0bac9c3be6 --- /dev/null +++ b/uva_cpp_clean/11961/11961-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11961 + Name: DNA + Problem: https://onlinejudge.org/external/119/11961.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +vector res; +char E[] = "ACGT", str[17]; +void dfs(int idx, int cnt) { + if (!str[idx] || !cnt) { + res.push_back(str); + return; + } + + char o = str[idx]; + for (int i=0; i<4; ++i) { + str[idx] = E[i]; + dfs(idx+1, cnt - (E[i] != o)); + } + str[idx] = o; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, c; + cin >> T; + while (T-- && cin >> n >> c >> str) { + res.clear(); + dfs(0, c); + cout << res.size() << '\n'; + for (const string &s: res) + cout << s << '\n'; + } +} diff --git a/uva_cpp_clean/11962/11962-19.cpp b/uva_cpp_clean/11962/11962-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ef2882eb61604fd97e05a3a336c89cf1c4d3aec --- /dev/null +++ b/uva_cpp_clean/11962/11962-19.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +typedef unsigned long long ll; + +ll pot[31]; +string s; + +ll proc(int pos) { + if (pos == 0) return 0; + ll res = 0; + switch (s[s.length() - pos]) { + case 'A': res = 0 * pot[pos-1]; break; + case 'C': res = 1 * pot[pos-1]; break; + case 'G': res = 2 * pot[pos-1]; break; + case 'T': res = 3 * pot[pos-1]; break; + } + res += proc(pos - 1); + return res; +} + +int main() { + pot[0] = 1; + for (int i = 1; i < 31; i++) pot[i] = 4*pot[i - 1]; + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + cin >> s; + ll res = proc(s.length()); + printf("Case %d: (%lu:%llu)\n", c, s.length(), res); + } + return 0; +} diff --git a/uva_cpp_clean/11965/11965-18.cpp b/uva_cpp_clean/11965/11965-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a9d1fb085db29fc3b71898f6890ed9ffe19673ca --- /dev/null +++ b/uva_cpp_clean/11965/11965-18.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +using namespace std; + +int main(){ + string input; + int t, n, cont = 0; + scanf("%d", &t); + while(cont < t){ + printf("Case %d:\n", cont + 1); + scanf("%d", &n); + cin.get(); + while(n--){ + getline(cin, input); + if(input.size() > 0) printf("%c", input[0]); + for(int i = 1; input[i]; i++){ + if(input[i] != ' '){ + printf("%c", input[i]); + }else{ + if(input[i] != input[i - 1])printf("%c", input[i]); + } + } + printf("\n"); + } + if(cont < t - 1) printf("\n"); + cont++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11966/11966-19.cpp b/uva_cpp_clean/11966/11966-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..801a9bc4b8ba217c36e0bca118625647eb5c769d --- /dev/null +++ b/uva_cpp_clean/11966/11966-19.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair dd; +typedef pair street; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + int N; + double D; + cin >> N >> D; + D *= D; + ufds u(N); + vector
v; + for (int i = 0; i < N; i++) { + double x, y; + cin >> x >> y; + for (int j = 0; j < i; j++) { + if ((v[j].first-x)*(v[j].first-x) + (v[j].second-y)*(v[j].second-y) <= D) { + u.unionSet(i, j); + } + } + v.push_back(make_pair(x, y)); + } + printf("Case %d: %d\n", c, u.numSets); + } + return 0; +} diff --git a/uva_cpp_clean/11966/11966-6.cc b/uva_cpp_clean/11966/11966-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..0970c9da22295fcc8789b6986e255a76b5b376e4 --- /dev/null +++ b/uva_cpp_clean/11966/11966-6.cc @@ -0,0 +1,43 @@ +// https://uva.onlinejudge.org/external/119/11966.pdf +#include +using namespace std; +using p=complex; +using vp=vector

; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t,n; + double d,x,y; + cin>>t; + for(int T=1;T<=t;T++){ + cin>>n>>d; + vp a(n); + for(int i=0;i>x>>y; + a[i]={x,y}; + } + vvi g(n); + for(int i=0;idfs=[&](int u){ + s[u]=1; + for(int v:g[u]) + if(!s[v]) + dfs(v); + }; + int k=0; + for(int i=0;i union-find disjoint sets (UFDS) + difficulty: easy + date: 20/Jul/2020 + by: @brpapa +*/ +#include +using namespace std; + +class UFDS { + private: + vector p; // p[n] = pai do elemento n + vector size; // size[n] = tamanho do conjunto identificado por n + int qtyDisjointSets; + + public: + UFDS() {} + + // elementos n (0 <= n < N) + UFDS(int N) { + p.resize(N); + size.assign(N, 1); + qtyDisjointSets = N; + + // inicialmente, há N conjuntos disjuntos + for (int n = 0; n < N; n++) p[n] = n; + } + + // retorna o elemento identificador do conjunto de n + int findSet(int n) { + if (p[n] == n) return n; + + // path compression durante a busca + return p[n] = findSet(p[n]); + } + + // os conjuntos de n e de m são os mesmos? + bool isSameSet(int n, int m) { + return findSet(n) == findSet(m); + } + + // conecta os conjuntos de n e de m + void unionSet(int n, int m) { + int nId = findSet(n); + int mId = findSet(m); + + // union by size: conecta a menor árvore (em profundidade) à maior árvore + if (!isSameSet(nId, mId)) { + if (size[nId] > size[mId]) + swap(nId, mId); + + p[nId] = mId; + size[mId] += size[nId]; + + qtyDisjointSets--; + } + } + + // retorna o tamanho do conjunto de n + int sizeOfSet(int n) { + return size[findSet(n)]; + } + + // retorna a quantidade de conjuntos disjuntos + int count() { + return qtyDisjointSets; + } +}; + +double dist(pair p, pair q) { + double dx = p.first-q.first; + double dy = p.second-q.second; + return sqrt(dx*dx + dy*dy); +} + +int main() { + int t = 1, T; cin >> T; + while (T--) { + int N; double d; cin >> N >> d; + + UFDS uf(N); + + vector> stars(N); + for (auto &star : stars) + cin >> star.first >> star.second; + + for (int i = 0; i < N; i++) + for (int j = i+1; j < N; j++) { + if (dist(stars[i], stars[j]) <= d) + uf.unionSet(i, j); + } + + printf("Case %d: %d\n", t++, uf.count()); + } + return 0; +} diff --git a/uva_cpp_clean/1197/1197-21.cpp b/uva_cpp_clean/1197/1197-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f28a4452742d6f27f309c5abc14fbb7fbdd3090 --- /dev/null +++ b/uva_cpp_clean/1197/1197-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1197 + Name: The Suspects + Problem: https://onlinejudge.org/external/11/1197.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MAXN 30017 + +short gid[MAXN], gcn[MAXN], tmp[MAXN]; +short find(short u) { + if (gid[u]==u) return u; + + int i=0; + for (; u!=gid[u]; u=gid[u]) + tmp[i++] = u; + + for (int j=0; j 0) { + scanf("%d", &x); + for (int j=1; j +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + while(1){ + int n,m,k,u,v; + cin>>n>>m; + if(!n)break; + vi s=vi(n, 1),p=vi(n); + for(int i=0;ifind=[&](int i){ + if(i==p[i])return i; + return p[i]=find(p[i]); + }; + functionunite=[&](int i, int j){ + i=find(i); + j=find(j); + if(i==j)return; + if(s[i]>k>>u; + for(int j=1;j>v; + unite(u,v); + } + } + k=0; + for(int i=0;i + +using namespace std; + +struct UnionFind{ + int numSets; + vector path, rank, setSize; + + UnionFind(int N_): numSets(N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + setSize.assign(N_, 1); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + numSets--; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x, setSize[x] += setSize[y]; + else path[x] = y, setSize[y] += setSize[x]; + if( rank[x] == rank[y] ) rank[y]++; + } + + int sizeOfSet(int idx){ + return setSize[ findSet(idx) ]; + } +}; + +int n, m, k, a, b; + +int main(){ + while(scanf("%d %d", &n, &m), n | m){ + UnionFind UF(n); + while(m--){ + scanf("%d", &k); + if(k) scanf("%d", &a); + for(int it = 1; it < k; it++) + scanf("%d", &b), UF.unionSet(a, b); + } + printf("%d\n", UF.sizeOfSet(0)); + } +} diff --git a/uva_cpp_clean/11975/11975-9.cpp b/uva_cpp_clean/11975/11975-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d70e8e2f0fc58b070b72b78ccca9f24c7f043b4 --- /dev/null +++ b/uva_cpp_clean/11975/11975-9.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +int tc, N, L, corner, mid, diagonal, table, v[100]; +int m[1000][5][5], money[1000]; +bool x[1000][5][5], ok_corner[1000], ok_mid[1000], ok_diagonal[1000], ok_table[1000]; + +void reset(){ + for(int p = 0; p < L; p++) + for(int i = 0; i < 5; i++) + for(int j = 0; j < 5; j++) + x[p][i][j] = false; + for(int p = 0; p < L; p++) + money[p] = ok_corner[p] = ok_mid[p] = ok_diagonal[p] = ok_table[p] = false; +} + +void check(int ball, int p){ + if(ball <= 35 && !ok_corner[p]){ + if(x[p][0][0] && x[p][4][0] && x[p][0][4] && x[p][4][4]) + money[p] += corner, ok_corner[p] = true; + } + if(ball <= 40 && !ok_mid[p]){ + if(x[p][2][0] && x[p][2][1] && x[p][2][2] && x[p][2][3] && x[p][2][4]) + money[p] += mid, ok_mid[p] = true; + } + if(ball <= 45 && !ok_diagonal[p]){ + if(x[p][0][0] && x[p][1][1] && x[p][2][2] && x[p][3][3] && x[p][4][4] && + x[p][0][4] && x[p][1][3] && x[p][3][1] && x[p][4][0]) + money[p] += diagonal, ok_diagonal[p] = true; + } + if(ball >= 25 && !ok_table[p]){ + for(int i = 0; i < 5; i++) + for(int j = 0; j < 5; j++) + if(!x[p][i][j]) return; + money[p] += table, ok_table[p] = true; + } +} + +void fillNumber(int num, int p){ + for(int i = 0; i < 5; i++) + for(int j = 0; j < 5; j++) + if(m[p][i][j] == num){ + x[p][i][j] = true; + return; + } +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d %d", &N, &L); + for(int i = 0; i < N; i++) scanf("%d", &v[i]); + scanf("%d %d %d %d", &corner, &mid, &diagonal, &table); + for(int p = 0; p < L; p++) + for(int i = 0; i < 5; i++) + for(int j = 0; j < 5; j++) + scanf("%d", &m[p][i][j]); + for(int i = 0; i < N; i++) + for(int j = 0; j < L; j++) + fillNumber(v[i], j), check(i + 1, j); + printf("Case %d:\n", t); + for(int i = 0; i < L; i++) + printf("%d\n",money[i]); + if(t != tc) putchar('\n'); + reset(); + } + return(0); +} diff --git a/uva_cpp_clean/1198/1198-21.cpp b/uva_cpp_clean/1198/1198-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ec2cfff591816007c2dd9a5cb5b6ae42e31a431 --- /dev/null +++ b/uva_cpp_clean/1198/1198-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1198 + Name: The Geodetic Set Problem + Problem: https://onlinejudge.org/external/11/1198.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + + +long long P[43][43]; +int D[43][43]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string line; + int n, m; + cin >> n; cin.ignore(100, '\n'); + + for (int u=0; u>v; ) + if (D[u][--v]==INF) + D[u][v] = 1; + } + + for (int k=0; k> m; cin.ignore(100, '\n'); + while (m--) { + getline(cin, line); + stringstream sin(line); + vector s; + for (int v; sin>>v;) + s.push_back(v-1); + + long long bs = 0; + for (int i=0; i +using namespace std; + +int main() { + int T; + cin >> T; + for (int i = 1; i <= T; i++) { + double C, d; + cin >> C >> d; + C += (d*5)/9; + printf("Case %d: %.2f\n", i, C); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11984/11984-21.cpp b/uva_cpp_clean/11984/11984-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3e128a1390413babb308eeb5c9755ca14e2cb20 --- /dev/null +++ b/uva_cpp_clean/11984/11984-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11984 + Name: A Change in Thermal Unit + Problem: https://onlinejudge.org/external/119/11984.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T; + cin >> T; + for (int cse=1; cse<=T; cse++) { + int c, d; + cin >> c >> d; + printf("Case %d: %.2f\n", cse, c + 5*d/9.0); + } +} diff --git a/uva_cpp_clean/11984/11984-5.cpp b/uva_cpp_clean/11984/11984-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fa02bf140cfadb33b6e2ea50c8729ce60bcc922d --- /dev/null +++ b/uva_cpp_clean/11984/11984-5.cpp @@ -0,0 +1,15 @@ +#include +int main() +{ + int t,no=0; + double f,a,c,d; + scanf("%d",&t); + while(t--) + { + scanf("%lf%lf",&c,&d); + f = 9*c/5+d; + a = f*5/9; + printf("Case %d: %.2lf\n",++no,a); + } + return 0; +} diff --git a/uva_cpp_clean/11986/11986-14.cpp b/uva_cpp_clean/11986/11986-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd106e3784a383799b82531b0e2c8a04f44cc134 --- /dev/null +++ b/uva_cpp_clean/11986/11986-14.cpp @@ -0,0 +1,81 @@ +/*************************************************** + * Problem Name : 11986 - Save from Radiation.cpp + * Problem Link : https://uva.onlinejudge.org/external/119/11986.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-09-21 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + int tc; + ull n; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> n; + n++; + ull i; + + for (i = 0; ; i++) { + if ( (ull) (1ull << i) >= n) break; + } + + cout << "Case " << t << ": " << i << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/11987/11987-21.cpp b/uva_cpp_clean/11987/11987-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5be1725691e3841558afb9a5f4876e52d81a9325 --- /dev/null +++ b/uva_cpp_clean/11987/11987-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11987 + Name: Almost Union-Find + Problem: https://onlinejudge.org/external/119/11987.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int P[200143], C[200143], S[200143], n, m; + +int Find(int p) { + return P[p]==p ? p : P[p] = Find(P[p]); +} + +void Union(int p, int q) { + if (Find(p) == Find(q)) return; + C[Find(q)] += C[Find(p)]; + S[Find(q)] += S[Find(p)]; + P[Find(p)] = Find(q); +} + +void Move(int p, int q) { + if (Find(p) == Find(q)) return; + C[Find(p)]--; + S[Find(p)]-=p; + C[Find(q)]++; + S[Find(q)]+=p; + P[p] = Find(q); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n >> m) { + for (int i=1; i<=n; ++i) { + P[i] = n+i; + C[n+i] = 1; + S[n+i] = i; + } + for (int i=n+1; i<=2*n; ++i) { + P[i] = i; + } + + for (int i=0; i> t; + switch(t) { + case 1: + cin >> p >> q; + Union(p, q); + break; + + case 2: + cin >> p >> q; + Move(p, q); // move p to q + break; + + case 3: + cin >> p; + cout << C[Find(p)] << ' ' << S[Find(p)] << endl; + } + } + } +} diff --git a/uva_cpp_clean/11988/11988-11.cpp b/uva_cpp_clean/11988/11988-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0a3b5e974f00c43e407d0fbbbe36d8ead6265cc --- /dev/null +++ b/uva_cpp_clean/11988/11988-11.cpp @@ -0,0 +1,62 @@ +#include +/* +Problem: 11988 - Broken Keyboard (a.k.a. Beiju Text) +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=3139 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +string text; +deque beiju_text; +deque ::iterator it = beiju_text.begin(); + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + + while ( cin >> text && !cin.eof() ){ + it = beiju_text.begin(); + + for (int i = 0; i < text.size(); i++){ + if (text[i] == '[') {it = beiju_text.begin(); continue;} + else if (text[i] == ']') {it = beiju_text.end(); continue;} + + it = beiju_text.emplace(it, text[i]); + it = (it == beiju_text.end())? it : it+1; + } + + it = beiju_text.begin(); + while (it != beiju_text.end()){ + cout << *it++; + } + + cout << '\n'; + beiju_text.clear(); + } + + return 0; +} + +/* +input:- +----------- +This_is_a_[Beiju]_text +[[]][][]Happy_Birthday_to_Tsinghua_University + +output:- +----------- +BeijuThis_is_a__text +Happy_Birthday_to_Tsinghua_University + +Resources:- +------------- +http://www.cplusplus.com/reference/deque/deque/emplace/ +http://www.cplusplus.com/reference/deque/deque/ +https://www.geeksforgeeks.org/deque-cpp-stl/ +*/ diff --git a/uva_cpp_clean/11988/11988-14.cpp b/uva_cpp_clean/11988/11988-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42a4e08ea408dafd7fe5ce4e11fce3a6d2f37915 --- /dev/null +++ b/uva_cpp_clean/11988/11988-14.cpp @@ -0,0 +1,88 @@ +/*************************************************** + * Problem name : 11988 Broken Keyboard (a.k.a. Beiju Text).cpp + * Problem Link : https://uva.onlinejudge.org/external/119/11988.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-05 + * Problem Type : String(STL) + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + string str; + while (getline(cin, str)) { + string braket = "", ans = "", khali = ""; + int f = 0; + str.pb(']'); + vectorfaul; + for (int i = 0; i < (int)str.size(); i++) { + if (str[i] != '[' && str[i] != ']') { + khali += str[i]; + } else { + if (f == 0) { + for (int j = 0; j < (int)khali.size(); j++) { + ans.pb(khali[j]); + } + khali.clear(); + } else { + faul.push_back(khali); + khali.clear(); + } + + if (str[i] == '[') f = 1; + else f = 0; + } + } + reverse(faul.begin(), faul.end()); + for (int i = 0; i < (int)faul.size(); i++) { + braket += faul[i]; + } + cout << braket + ans; nl; + } + return 0; +} diff --git a/uva_cpp_clean/11988/11988-17.cpp b/uva_cpp_clean/11988/11988-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fad2ac8425fbb0479becdf0ad28b46ffb9b1db3 --- /dev/null +++ b/uva_cpp_clean/11988/11988-17.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + string line; + while (cin >> line) + { + list ans; + bool back = true; + auto pos = ans.begin(); + for (char c : line) + if (c == '[') + { + back = false; + pos = ans.begin(); + } + else if (c == ']') + back = true; + else if (back) + ans.push_back(c); + else + pos = ++ans.insert(pos, c); + for (char c : ans) + cout << c; + cout << "\n"; + } +} diff --git a/uva_cpp_clean/11988/11988-21.cpp b/uva_cpp_clean/11988/11988-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebdaf08458f5995cd3b2b97d91d3de4a26ee62aa --- /dev/null +++ b/uva_cpp_clean/11988/11988-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11988 + Name: Broken Keyboard (a.k.a. Beiju Text) + Problem: https://onlinejudge.org/external/119/11988.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + + +bool term[127]; +char line[100017], out[100017]; +int nxt[100017]; +int main() { + term['[']=term[']']=term['\n']=1; + + int st=0, ed=-1, ind=0; + char last = ']', ch; + for (int i=0; (ch = line[i] = readchar())!=EOF; ++i) { + nxt[i] = i + 1; + if (term[ch]) { + if (i > ind) { + if (last == ']') { + if (ed >= 0) + nxt[ed] = ind; + ed = i-1; + } + else { + nxt[i-1] = st; + st = ind; + if (ed == -1) + ed = i-1; + } + } + + if (ch == '\n') { + int k = 0; + if (i) { + nxt[ed]=-1; + for (int j=st; j>=0; j=nxt[j]) + out[k++] = line[j]; + } + out[k] = '\n'; + fwrite_unlocked(out, 1, k+1, stdout); + + last = ']'; + st = ind = 0; + ed = i = -1; + } + else { + ind = i--; + last = ch; + } + } + } +} diff --git a/uva_cpp_clean/11988/11988-3.cpp b/uva_cpp_clean/11988/11988-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..920a5a24d9e3573dc37cfd2d742d71f62b1cff49 --- /dev/null +++ b/uva_cpp_clean/11988/11988-3.cpp @@ -0,0 +1,38 @@ +// Problem : U-Va ID 11988 - Broken Keyboard (a.k.a. Beiju Text) + +#include +#include +#include + +using namespace std; + +int main (){ + list beijuLine; + list :: iterator ex; + + char inputLine[100001]; + + // while there is still input + while ( scanf("%s", inputLine) != EOF ){ + ex = beijuLine.begin(); + int len = strlen(inputLine); + for ( int l = 0; l < len; l++){ + // if input character is [ (which means home key pressed) + if ( inputLine[l] == '[' ) + ex = beijuLine.begin(); + // if input character is ] (which means end key pressed) + else if ( inputLine[l] == ']' ) + ex = beijuLine.end(); + else + beijuLine.insert( ex, inputLine[l] ); + } + + // print every character in the list + for ( ex = beijuLine.begin(); ex != beijuLine.end(); ex++ ) + printf ("%c", *ex ); + // print a new beijuLine character and then erase the contents of line + printf ("\n"); + beijuLine.clear(); + + } +} diff --git a/uva_cpp_clean/11988/11988-9.cpp b/uva_cpp_clean/11988/11988-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25a8b74f8bd24591588a468523dba20fa3f632ff --- /dev/null +++ b/uva_cpp_clean/11988/11988-9.cpp @@ -0,0 +1,24 @@ +#include + +#define SIZE 100010 + +using namespace std; + +char s[SIZE]; +list v; +bool _home; +list ::iterator it; + +int main(){ + while(scanf("%s", s) != EOF){ + v.clear(), _home = false; + for(int i = 0; s[i]; i++) + if(s[i] == '[') _home = true, it = v.begin(); + else if(s[i] == ']') _home = false; + else if(_home) v.insert(it, s[i]); + else v.push_back(s[i]); + for(it = v.begin(); it != v.end(); ++it) printf("%c", *it); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/11991/11991-18.cpp b/uva_cpp_clean/11991/11991-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cbfdf977b73f4d7195503600d0ac7bb86b37780e --- /dev/null +++ b/uva_cpp_clean/11991/11991-18.cpp @@ -0,0 +1,40 @@ +#include +#define pb push_back +#define pf push_front +#define pob pop_back +#define fp first +#define sp second +#define mp make_pair +#define ins insert +#define uEdge(u, v) g[u].pb(v), g[v].pb(u) +#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w}) +#define dEdge(u, v) g[u].pb(v) +#define dwEdge(u, v, w) g[u].pb({v, w}) +#define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define minHeap int, vector, greater + +using namespace std; +typedef long long int lli; +typedef pair pii; +vector g[0]; + +lli gcd(lli a, lli b){ + if(b == 0) return a; + a%=b; + return gcd(b, a); +} + +int main(){ + int n, m, k, v, ni; + vector> arr; + while(scanf("%d %d", &n, &m) != EOF){ + arr.assign(1000000, vector()); + for(int i = 1; i <= n; i++) scanf("%d", &ni), arr[ni].pb(i); + for(int i = 0; i < m; i++){ + scanf("%d %d", &k, &v); + if(k - 1 < arr[v].size()) printf("%d\n", arr[v][k - 1]); + else printf("0\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11991/11991-19.cpp b/uva_cpp_clean/11991/11991-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..008d13d1e2e66368d81d1ec20648fbc7aca35fd3 --- /dev/null +++ b/uva_cpp_clean/11991/11991-19.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +typedef pair ii; + +int main() { + ii vec[1000010]; + int n, m; + while (cin >> n >> m) { + for (int i = 0; i < n; i++) { + int val; + cin >> val; + vec[i] = ii(val, i); + } + sort(vec, vec + n); + for (int i = 0; i < m; i++) { + int k, v; + cin >> k >> v; + int p1 = lower_bound(vec, vec + n, ii(v, -1)) - vec; + int p2 = lower_bound(vec, vec + n, ii(v, n + 1)) - vec; + if (k > p2 - p1) printf("0\n"); + else printf("%d\n", vec[p1+k-1].second + 1); + } + } + return 0; +} diff --git a/uva_cpp_clean/11991/11991-21.cpp b/uva_cpp_clean/11991/11991-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b07abf554e58cb6bf3731fb039f7425b48a40a3 --- /dev/null +++ b/uva_cpp_clean/11991/11991-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11991 + Name: Easy Problem from Rujia Liu? + Problem: https://onlinejudge.org/external/119/11991.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + + +int main(){ + int n, m, x, k; + while (cin>>n>>m) { + map > pos; + for (int i=1; i<=n; i++) { + cin >> x; + pos[x].push_back(i); + } + + for (int i=0; i> k >> x; + + int r = 0; + auto it = pos.find(x); + if (it != pos.end()) { + vector &vv = it->second; + if (vv.size() >= k) + r = vv[k-1]; + } + cout << r << endl; + } + } +} diff --git a/uva_cpp_clean/11991/11991-6.cc b/uva_cpp_clean/11991/11991-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..82b191b412965b49ddb9bae058465b71472e085f --- /dev/null +++ b/uva_cpp_clean/11991/11991-6.cc @@ -0,0 +1,19 @@ +// https://uva.onlinejudge.org/external/119/11991.pdf +#include +using namespace std; +using vi=vector; +using mvi=unordered_map; +int main(){ + int n,m,k,v; + while(cin>>n>>m){ + mvi a; + for(int i=1;i<=n;i++){ + cin>>v; + a[v].push_back(i); + } + for(int i=0;i>k>>v; + cout<<(a[v].size()>=k?a[v][k-1]:0)<<"\n"; + } + } +} diff --git a/uva_cpp_clean/11991/11991-9.cpp b/uva_cpp_clean/11991/11991-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee69d1253026cddf2cb34af0117c4aa487fb7755 --- /dev/null +++ b/uva_cpp_clean/11991/11991-9.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +vector a[1000005]; + +int main(){ + int n, m, tmp, x, y; + while(scanf("%d %d", &n, &m) != EOF){ + for(int i = 1; i <= n; i++) + scanf("%d", &tmp), + a[tmp].push_back(i); + for(int i = 0; i < m; i++){ + scanf("%d %d", &x, &y); + if(a[y].size() < x) + printf("0\n"); + else + printf("%d\n",a[y][x - 1]); + } + for(int i = 0; i < 1000005; i++) + a[i].clear(); + } + return(0); +} diff --git a/uva_cpp_clean/11995/11995-11.cpp b/uva_cpp_clean/11995/11995-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5735eb808924b5d156370005b1a3edae98246d04 --- /dev/null +++ b/uva_cpp_clean/11995/11995-11.cpp @@ -0,0 +1,124 @@ +#include +/* +Problem: 11995 - I Can Guess the Data Structure! +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=3146 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int N, op, x; + +int main () { + + while ( scanf("%d\n", &N) != EOF ){ + stack stack_bag; + queue queue_bag; + priority_queue priority_queue_bag; + + bool is_it_a_stack = 1, is_it_a_queue = 1, is_it_a_pqueue = 1; + + while ( N-- ){ + scanf("%d %d\n", &op, &x); + if (op == 1) { + stack_bag.push(x); + queue_bag.push(x); + priority_queue_bag.push(x); + } + + else{ + + if (stack_bag.size() > 0){ + if (stack_bag.top() != x) is_it_a_stack = false; + stack_bag.pop(); + } else is_it_a_stack = false; + + + if(queue_bag.size() > 0){ + if (queue_bag.front() != x) is_it_a_queue = false; + queue_bag.pop(); + } else is_it_a_queue = false; + + if(priority_queue_bag.size() > 0){ + if (priority_queue_bag.top() != x) is_it_a_pqueue = false; + priority_queue_bag.pop(); + } else is_it_a_pqueue = false; + + } + + } + if(is_it_a_stack+is_it_a_queue+is_it_a_pqueue > 1) + printf("not sure\n"); + + else if(is_it_a_stack+is_it_a_queue+is_it_a_pqueue == 0) + printf("impossible\n"); + + else{ + if (is_it_a_stack) printf("stack\n"); + else if (is_it_a_queue) printf("queue\n"); + else printf("priority queue\n"); + } + } + + return 0; +} +/* +Sample input:- +----------------- +6 +1 2 +1 1 +1 3 +2 2 +2 1 +2 3 +6 +1 2 +1 1 +1 3 +2 3 +2 1 +2 2 +6 +1 2 +1 1 +1 3 +2 3 +2 2 +2 1 +6 +1 2 +1 1 +1 3 +2 1 +2 2 +2 3 +6 +1 1 +1 2 +1 3 +2 3 +2 2 +2 1 + +Sample output:- +----------------- +queue +stack +priority queue +impossible +not sure + + +Resources:- +------------- +http://www.cplusplus.com/reference/stack/stack/ +http://www.cplusplus.com/reference/queue/queue/ +http://www.cplusplus.com/reference/queue/priority_queue/ + +*/ diff --git a/uva_cpp_clean/11995/11995-14.cpp b/uva_cpp_clean/11995/11995-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eef26af0502b82ff2f5878e16c052bbcee37f1d --- /dev/null +++ b/uva_cpp_clean/11995/11995-14.cpp @@ -0,0 +1,216 @@ +/*************************************************** + * Problem Name : 11995 I Can Guess the Data Structure!.cpp + * Problem Link : https://onlinejudge.org/external/119/11995.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-10-12 + * Problem Type : adHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int n; + + while (cin >> n) { + int P = 0, S = 0, Q = 0; + queueq; + stackst; + priority_queuepq; + + for (int i = 0; i < n; i++) { + int x, c; + cin >> c >> x; + + if (c == 1) { + q.push (x); + st.push (x); + pq.push (x); + + } else { + // queue + if (!q.empty() ) { + int tmp = q.front(); + + if (tmp != x) { + Q = 1; + } + + q.pop(); + + } else { + Q = 1; + } + + // stack + if (!st.empty() ) { + int tmp = st.top(); + + if (tmp != x) { + S = 1; + } + + st.pop(); + + } else { + S = 1; + } + + // priority-queue + if (!pq.empty() ) { + int tmp = pq.top(); + + if (tmp != x) { + P = 1; + } + + pq.pop(); + + } else { + P = 1; + } + } + } + + //~ cout << Q << " " << S << " " << P << " -d\n"; + + if (S == 1 && Q == 1 && P == 1) { + cout << "impossible\n"; + + } else if (S == 0) { + if (Q == 0 || P == 0) { + cout << "not sure\n"; + + } else { + cout << "stack\n"; + } + + } else if (Q == 0) { + if (S == 0 || P == 0) { + cout << "not sure\n"; + + } else { + cout << "queue\n"; + } + + } else if (P == 0) { + if (S == 0 || Q == 0) { + cout << "not sure\n"; + + } else { + cout << "priority queue\n"; + } + } + } + + return 0; +} diff --git a/uva_cpp_clean/11995/11995-19.cpp b/uva_cpp_clean/11995/11995-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da8f7ec796dbd5cc9ab244f09ec4bddb204cd4b5 --- /dev/null +++ b/uva_cpp_clean/11995/11995-19.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +using namespace std; + +int main() { + int n; + stack stack; + queue queue; + priority_queue pqueue; + while (scanf("%d", &n) != EOF) { + int s, q, pq; + s = q = pq = 1; + while (!stack.empty()) stack.pop(); + while (!queue.empty()) queue.pop(); + while (!pqueue.empty()) pqueue.pop(); + while (n--) { + int op, elem; + scanf("%d %d", &op, &elem); + if (op == 1) { + if (s) stack.push(elem); + if (q) queue.push(elem); + if (pq) pqueue.push(elem); + } else { + if (stack.empty() || queue.empty() || pqueue.empty()) s = q = pq = 0; + if (s) s = stack.top() == elem, stack.pop(); + if (q) q = queue.front() == elem, queue.pop(); + if (pq) pq = pqueue.top() == elem, pqueue.pop(); + } + } + if (s+q+pq > 1) printf("not sure\n"); + else if (s+q+pq == 1) { + if (s) printf("stack\n"); + else if (q) printf("queue\n"); + else printf("priority queue\n"); + } else { + printf("impossible\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/11995/11995-21.cpp b/uva_cpp_clean/11995/11995-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a2676dbed583c43b3b1df36f41fa213a889e3fb --- /dev/null +++ b/uva_cpp_clean/11995/11995-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 11995 + Name: I Can Guess the Data Structure! + Problem: https://onlinejudge.org/external/119/11995.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + int n, sum, id, x; + while (cin>>n) { + bool ip=1, iq=1, is=1; + priority_queue p; + queue q; + stack s; + sum=3; + + for (int i=0; i> id >> x; + + if (sum) { + if (id == 1) { + if (ip) p.push(x); + if (iq) q.push(x); + if (is) s.push(x); + } + else { + if (ip) { + if (p.empty()) ip=0; + else { + if (x != p.top()) ip=0; + p.pop(); + } + } + if (iq) { + if (q.empty()) iq=0; + else { + if (x != q.front()) iq=0; + q.pop(); + } + } + if (is) { + if (s.empty()) is=0; + else { + if (x != s.top()) is=0; + s.pop(); + } + } + } + sum = ip+iq+is; + } + } + + if (!sum) puts("impossible"); + else if (sum>1) puts("not sure"); + else if (ip) puts("priority queue"); + else if (iq) puts("queue"); + else puts("stack"); + } +} diff --git a/uva_cpp_clean/11995/11995-5.cpp b/uva_cpp_clean/11995/11995-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..693580c29acfb9de22cca265e4eeac7d67f1e9be --- /dev/null +++ b/uva_cpp_clean/11995/11995-5.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +#define x first +#define y second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) SORT(v), (v).earse(unique(v.begin(),v.end()),v.end()) +#define mymem(a,d) memset(a,d,sizeof(a)) +using namespace std; +int main() +{ + int i,j,k,l,n,cmd,p,temp; + while(sc("%d",&n) == 1) + { + stackst; + queueq; + priority_queuepq; + int s=1; + int qu=1; + int pri=1; + while(n--) + { + sc("%d %d",&cmd,&p); + + if(cmd==1) { + if(s) + st.push(p); + if(qu) + q.push(p); + if(pri) + pq.push(p); + } + else if(cmd==2) { + if(s) { + if(!st.empty() && st.top() == p) { + st.pop(); + } + else + s=0; + } + if(qu) { + if(!q.empty() && q.front() == p) { + q.pop(); + } + else + qu=0; + } + if(pri) { + if(!pq.empty() && pq.top() == p) { + pq.pop(); + } + else + pri= false; + } + } + } + + if(s && !pri && !qu) + printf("stack\n"); + else if(!s && !pri && qu) + printf("queue\n"); + else if(!s && pri && !qu) + printf("priority queue\n"); + else if(pri==1 || qu==1 || s==1) + printf("not sure\n"); + else + printf("impossible\n"); + + } + + + return 0; +} diff --git a/uva_cpp_clean/11995/11995-9.cpp b/uva_cpp_clean/11995/11995-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c003de10cec252ee08dc633a5cfab46775ec079 --- /dev/null +++ b/uva_cpp_clean/11995/11995-9.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int n, tmp1, tmp2; +bool bs, bq, bpq; + +int main(){ + while(scanf("%d", &n) == 1){ + bs = bq = bpq = true; + queue q; + stack s; + priority_queue pq; + for(int i = 0; i < n; i++){ + scanf("%d %d", &tmp1, &tmp2); + if(tmp1 == 1){ + if(bs) s.push(tmp2); + if(bq) q.push(tmp2); + if(bpq) pq.push(tmp2); + } + else{ + if(bs){ + if(s.empty() || s.top() != tmp2) bs = false; + else s.pop(); + } + if(bq){ + if(q.empty() || q.front() != tmp2) bq = false; + else q.pop(); + } + if(bpq){ + if(pq.empty() || pq.top() != tmp2) bpq = false; + else pq.pop(); + } + } + } + if(bs && !bq && !bpq) puts("stack"); + else if(!bs && bq && !bpq) puts("queue"); + else if(!bs && !bq && bpq) puts("priority queue"); + else if(bs + bq + bpq == 0) puts("impossible"); + else puts("not sure"); + } + return(0); +} diff --git a/uva_cpp_clean/120/120-18.cpp b/uva_cpp_clean/120/120-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3139ef1f70366a7afa175c2ec93c2dae2a5aca03 --- /dev/null +++ b/uva_cpp_clean/120/120-18.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int vector[30]; + int cont, number; + string line; + stringstream ss; + while(getline(cin, line)){ + cont = 0; + ss.clear(); + ss.str(line); + while(ss >> number){ + vector[cont] = number; + cont++; + printf("%d ", number); + } + printf("\n"); + for(int i = cont - 1; i >= 0; i--){ + int maxN = -1; + int index; + for(int j = 0; j <= i; j++){ + if(vector[j] > maxN){ + index = j; + maxN = vector[j]; + } + } + if(i != index){ + if(index != 0){ + printf("%d ",cont - index); + for(int k = 0; k <= index/2; k++) swap(vector[k], vector[index - k]); + } + printf("%d ", cont - i); + for(int k = 0; k <= i/2; k++) swap(vector[k], vector[i - k]); + } + } + printf("%d\n", 0); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/120/120-19.cpp b/uva_cpp_clean/120/120-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2459df9639a2b48e438557845257b97308737fc7 --- /dev/null +++ b/uva_cpp_clean/120/120-19.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; + +int main() { + int pancakes[30]; + while (cin >> pancakes[0]) { + int size = 1; + while (cin.peek() != '\n') { + cin >> pancakes[size]; + size++; + } + bool first = false; + for (int k = 0; k < size; k++) { + if (!first) first = true; + else cout << " "; + cout << pancakes[k]; + } cout << endl; + for (int i = size - 1; i >= 0; i--) { + int max = pancakes[i], index = i; + for (int j = 0; j < i; j++) { + if (pancakes[j] > max) max = pancakes[j], index = j; + } + if (index != i) { + if (index != 0) cout << size - index << " "; + cout << size - i << " "; + int p1 = 0, p2 = index; + while (p1 < p2) { + int aux = pancakes[p1]; + pancakes[p1] = pancakes[p2]; + pancakes[p2] = aux; + p1++, p2--; + } + p1 = 0, p2 = i; + while (p1 < p2) { + int aux = pancakes[p1]; + pancakes[p1] = pancakes[p2]; + pancakes[p2] = aux; + p1++, p2--; + } + } + } + cout << 0 << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/120/120-21.cpp b/uva_cpp_clean/120/120-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6fbb660609de500ad46057e9724896d6dc742b06 --- /dev/null +++ b/uva_cpp_clean/120/120-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 120 + Name: Stacks of Flapjacks + Problem: https://onlinejudge.org/external/1/120.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int a[31]; +void flip(int l) { + for (int i = l/2; i>=0; i--) { + int t = a[i]; + a[i] = a[l-i]; + a[l-i] = t; + } +} + +int main() { + char line[300]; + while (cin.getline(line, 300)) { + int n = sscanf(line, "%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d",a,a+1,a+2,a+3,a+4,a+5,a+6,a+7,a+8,a+9,a+10,a+11,a+12,a+13,a+14,a+15,a+16,a+17,a+18,a+19,a+20,a+21,a+22,a+23,a+24,a+25,a+26,a+27,a+28,a+29,a+30); + + cout << a[0]; + for(int i=1; ia[mxi]) + mxi=j; + + if (mxi != n-i-1) { + if (mxi) { + cout << n-mxi << " "; + flip(mxi); + } + + cout << i+1 << " "; + flip(n-i-1); + } + } + cout<<"0\n"; + } +} + diff --git a/uva_cpp_clean/120/120-9.cpp b/uva_cpp_clean/120/120-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55f31b29f10642f1aa45944b7d90b6eeb8a57b8f --- /dev/null +++ b/uva_cpp_clean/120/120-9.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +const int LEN = 300, SIZE = 35; + +int n, v[SIZE], u[SIZE]; +char line[LEN], *p; + +int main(){ + while(cin.getline(line, LEN)){ + p = strtok(line, " "); + n = 0; + while(p) v[n] = u[n] = atoi(p), n++, p = strtok(NULL, " "); + sort(u, u + n); + for(int it = 0; it < n; it++) + printf( it != n - 1 ? "%d " : "%d\n", v[it] ); + for(int it = n - 1; it >= 0; it--) + if( v[it] != u[it] ){ + int id; + for(id = it - 1; id >= 0; id--) + if( v[id] == u[it] ) break; + printf("%d ", n - id); + for(int j = 0; j < id; j++) + swap( v[j], v[id--]); + printf("%d ", n - it); + id = it; + for(int j = 0; j < id; j++) + swap( v[j], v[id--]); + } + printf("0\n"); + } + return(0); +} diff --git a/uva_cpp_clean/12003/12003-14.cpp b/uva_cpp_clean/12003/12003-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..248878325683a6abdfdcfc513c1688de05af8f78 --- /dev/null +++ b/uva_cpp_clean/12003/12003-14.cpp @@ -0,0 +1,119 @@ +/*************************************************** + * Problem Name : 12003 - Array Transformer.cpp + * Problem Link : https://uva.onlinejudge.org/external/120/12003.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-25 + * Problem Type : Data Structure(srt root decomposition) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 300005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorblock[550]; +int ar[MAX]; +int blockSz; + +void update(int i, int pre, int value) { + int id = i/blockSz; + int x = (int)(lower_bound(all(block[id]), pre) - block[id].begin()); + block[id][x] = value; + ar[i] = value; + sort(all(block[id])); +} + +int query(int L, int R, int value) { + int l = L/blockSz; + int r = R/blockSz; + if (l == r) { + int sum = 0; + for (int i = L; i <= R; i++) { + if(ar[i] >= value) sum++; + } + return sum; + } + int sum = 0; + for (int i = L; i < (l + 1)*blockSz; i++) { + if(ar[i] < value) sum++; + } + for (int i = l + 1; i < r; i++) { + int x = (int)(lower_bound(all(block[i]), value) - block[i].begin()); + sum+=x; + } + for (int i = r * blockSz; i <= R; i++) { + if(ar[i] < value) sum++; + } + return sum; +} +int main () { + __FastIO; + int n, m, u; + cin >> n >> m >> u; + blockSz = (int)sqrt(n); + for(int i = 0; i> ar[i]; + int id = i/blockSz; + block[id].pb(ar[i]); + } + + for(int i = 0; i<=blockSz; i++) { + sort(all(block[i])); + } + for(int q = 1; q<=m; q++) { + int l, r, v, p; + cin >> l >> r >> v >> p; + int ans = query(l-1, r-1, v); + int id = p-1; + int value = ((ll)u *(ll)ans)/(r- l + 1); + int pre = ar[id]; + update(id, pre, value); + } + for(int i = 0; i Author : TISparta + * > Date : 03-05-18 + * > Tags : Sqrt decomposition, Binary search + * > Difficulty : 5 / 10 + */ + +#include + +using namespace std; + +const int MAX_N = 3e5, OFFSET = 2; + +int N, Q, BLOCK_SIZE; +long long u, arr[MAX_N + OFFSET]; +vector > chunks; + +inline void printArray() { + for (int i = 0; i < N; i++) cout << arr[i] << endl; +} + +void processQuery() { + int L, R, v, p; + cin >> L >> R >> v >> p; + L--, R--, p--; + int den = R - L + 1, id, cp; + long long k = 0; + while (L <= R and L % BLOCK_SIZE != 0) { + k += arr[L] < v; + L++; + } + if (L % BLOCK_SIZE == 0) { + id = L / BLOCK_SIZE; + while (L + BLOCK_SIZE <= R) { + k += upper_bound(chunks[id].begin(), chunks[id].end(), v - 1) - chunks[id].begin(); + L += BLOCK_SIZE; + id++; + } + } + while (L <= R) { + k += arr[L] < v; + L++; + } + long long new_val = k * u / den; + id = p / BLOCK_SIZE; + cp = find(chunks[id].begin(), chunks[id].end(), arr[p]) - chunks[id].begin(); + chunks[id][cp] = new_val; + arr[p] = new_val; + sort(chunks[id].begin(), chunks[id].end()); +} + +void readArray() { + cin >> N >> Q >> u; + BLOCK_SIZE = sqrt(N); + for (int i = 0, id = 0; i < N; id++) { + chunks.push_back({}); + for (int j = i; j < N and j < i + BLOCK_SIZE; j++) { + cin >> arr[j]; + chunks[id].push_back(arr[j]); + } + sort(chunks[id].begin(), chunks[id].end()); + i += BLOCK_SIZE; + } +} + +inline void fastIO() { + ios::sync_with_stdio(false); + cin.tie(NULL); cout.tie(NULL); +} + +int main() { + fastIO(); + readArray(); + while (Q--) processQuery(); + printArray(); + return (0); +} diff --git a/uva_cpp_clean/12004/12004-9.cpp b/uva_cpp_clean/12004/12004-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83d6911b56083f74aea539e6020a0e1bc120a7cb --- /dev/null +++ b/uva_cpp_clean/12004/12004-9.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int tc, n; + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d", &n); + long long p = (long long)n * (n - 1) / 2; + if(p % 2 == 0) printf("Case %d: %lld\n", t, p / 2); + else printf("Case %d: %lld/2\n", t, p); + } + return(0); +} diff --git a/uva_cpp_clean/12015/12015-2.cpp b/uva_cpp_clean/12015/12015-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..499acf0cf985b957ce5b4922cf46a2abbd930fb3 --- /dev/null +++ b/uva_cpp_clean/12015/12015-2.cpp @@ -0,0 +1,55 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +char names[10][110]; +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; cin >> tc; + int p,count=1; + + priority_queue > pq; + while (tc--) { + printf("Case #%d:\n",count++); + for (int i = 0; i < 10; ++i) { + cin >> names[i] >> p; + pq.push(pair(p,-i)); + } + p = pq.top().first; + while(pq.top().first == p){ + printf("%s\n",names[-pq.top().second]); + pq.pop(); + } + while(!pq.empty()) pq.pop(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12015/12015-21.cpp b/uva_cpp_clean/12015/12015-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59d7ff91a2225ee08a10d0cdd59a3701d652e8dc --- /dev/null +++ b/uva_cpp_clean/12015/12015-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12015 + Name: Google is Feeling Lucky + Problem: https://onlinejudge.org/external/120/12015.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct S { + char url[105]; + int rel, ind; + bool operator < (const S& o) const { + return rel == o.rel ? ind > o.ind : rel < o.rel; + } +}; + +int main(){ + int T; + cin>>T; + S sr; + for (int cse=1; cse<=T; cse++) { + priority_queue p; + for (int i=0; i<10; i++) { + cin >> sr.url >> sr.rel; + sr.ind = i; + p.push(sr); + } + + printf("Case #%d:\n", cse); + int toprel = p.top().rel; + while (p.top().rel == toprel) { + cout << p.top().url << endl; + p.pop(); + } + } +} diff --git a/uva_cpp_clean/12015/12015-5.cpp b/uva_cpp_clean/12015/12015-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5e574673d280f1db47161f2506bb078a3b7bef4d --- /dev/null +++ b/uva_cpp_clean/12015/12015-5.cpp @@ -0,0 +1,58 @@ +#include +#include +int main() +{ + char a[1000],b[1000],c[1000],d[1000],e[1000],f[1000],g[1000],h[1000],i[1000],j[1000]; + int n,k[1000],max,p,t=1; + while(scanf("%d",&n)==1) + { + while(n--) + { + scanf("%s%d",a,&k[0]); + scanf("%s%d",b,&k[1]); + scanf("%s%d",c,&k[2]); + scanf("%s%d",d,&k[3]); + scanf("%s%d",e,&k[4]); + scanf("%s%d",f,&k[5]); + scanf("%s%d",g,&k[6]); + scanf("%s%d",h,&k[7]); + scanf("%s%d",i,&k[8]); + scanf("%s%d",j,&k[9]); + max=0; + for(p=0;p<10;p++) + { + if(k[p]>max) + max=k[p]; + + } + printf("Case #%d:\n",t++); + for(p=0;p<10;p++) + { + if(k[p]==max) + { + if(p==0) + puts(a); + if(p==1) + puts(b); + if(p==2) + puts(c); + if(p==3) + puts(d); + if(p==4) + puts(e); + if(p==5) + puts(f); + if(p==6) + puts(g); + if(p==7) + puts(h); + if(p==8) + puts(i); + if(p==9) + puts(j); + } + } + } + } + return 0; +} diff --git a/uva_cpp_clean/12015/12015-6.cpp b/uva_cpp_clean/12015/12015-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b6cba14d04401f369f8ddfa0d80573d4a293767 --- /dev/null +++ b/uva_cpp_clean/12015/12015-6.cpp @@ -0,0 +1,34 @@ +/* + ad-hoc + difficulty: easy + date: 17/Feb/2020 + by: @brpapa +*/ +#include +#include +#define INF (int)0x7f7f7f7f +using namespace std; + +int main() { + int T; cin >> T; + for (int t = 1; t <= T; t++) { + string page; + vector ans; + + int r, rMax = -INF; + for (int l = 0; l < 10; l++) { + cin >> page >> r; + if (r > rMax) { + rMax = r; + ans.clear(); ans.push_back(page); + } + else if (r == rMax) { + ans.push_back(page); + } + } + + printf("Case #%d:\n", t); + for (string a : ans) cout << a << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12015/12015-9.cpp b/uva_cpp_clean/12015/12015-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2fef03e2811b16806d6a8181bd207d57edd8338 --- /dev/null +++ b/uva_cpp_clean/12015/12015-9.cpp @@ -0,0 +1,22 @@ +#include + +int main(){ + int a[10], t = 1, i, max, n; + char b[10][100]; + scanf("%d", &n); + while(n--){ + for(i = 0; i < 10; i++){ + scanf("%s", b[i]); + scanf("%d", &a[i]); + } + max = a[0]; + for(i = 1; i < 10; i++) + if(a[i] > max) max = a[i]; + printf("Case #%d:\n", t); + t++; + for(i = 0; i < 10; i++) + if(a[i] == max) + printf("%s\n", b[i]); + } + return(0); +} diff --git a/uva_cpp_clean/12015/main.cpp b/uva_cpp_clean/12015/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e33b01506bffe71ada3b024208c7850a6191a34a --- /dev/null +++ b/uva_cpp_clean/12015/main.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; + +int main(){ + short cases, maxim, actual; + string web; + queue> data; + + cin >> cases; + for(short i=1; i <= cases; i++){ + maxim = 0; + cout << "Case #" << i << ":\n"; + for(short c=0; c < 10; c++){ + cin >> web >> actual; + maxim = max(maxim, actual); + data.push(make_pair(web, actual)); + } + for(short j=0; j < 10; j++){ + pair ele = data.front(); + data.pop(); + if(ele.second == maxim) cout << ele.first << endl; + } + + } + +return 0; +} diff --git a/uva_cpp_clean/12019/12019-19.cpp b/uva_cpp_clean/12019/12019-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86613c974cd9db9ab87e6455e10b1835e1616378 --- /dev/null +++ b/uva_cpp_clean/12019/12019-19.cpp @@ -0,0 +1,59 @@ +// las12uvas contest PROBLEM H 2015 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector

vdd; +typedef vector vll; +typedef vector vvi; + +string dayOfWeek[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; + +int dateToInt (int m, int d, int y){ + return + 1461*(y+4800+(m-14)/12)/4+ + 367*(m -2-(m-14)/12*12)/12- + 3*((y+4900+(m-14)/12)/100)/4+ + d-32075; +} +string intToDay (int jd) { return dayOfWeek[jd%7]; } + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + int m, d; + cin >> m >> d; + cout << intToDay (dateToInt (m, d, 2011)) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12019/12019-21.cpp b/uva_cpp_clean/12019/12019-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..491ebe8a40114dd1a6a81738d49c29631c4ccdd9 --- /dev/null +++ b/uva_cpp_clean/12019/12019-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12019 + Name: Doom's Day Algorithm + Problem: https://onlinejudge.org/external/120/12019.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char wd[7][20] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; +int dc[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, m, d; + cin >> T; + while (T--) { + cin >> m >> d; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1202 + Name: Finding Nemo + Problem: https://onlinejudge.org/external/12/1202.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + + +struct State { + int x, y, d; + State(int x, int y, int d):y(y),x(x),d(d){} + bool operator < (const State &o) const { return d > o.d; } +}; + +int D[201][201]; +char L[201][201], B[201][201]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int m, n, x, y, d, t; + while (cin >> m >> n && m>-1) { + int w = 0, h = 0; + while (m--) { + cin >> x >> y >> d >> t; + + for (int mxw=x+(d?0:t); w<=mxw; ++w) + for (int i=0; i> x >> y >> d; + if (d) L[x][y] = 2; + else B[x][y] = 2; + } + + + double f1, f2; + cin >> f1 >> f2; + int dx = f1, dy = f2; + if (dx>=w || dy>=h) { + cout << "0\n"; + continue; + } + + for (int i=0; i q; + q.push(State(0, 0, D[0][0]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.x][s.y]) continue; + if (s.x == dx && s.y == dy) { + cout << s.d << endl; + goto fin; + } + + if (s.x && L[s.x][s.y]!=1) { + int d = s.d + (L[s.x][s.y]==2); + if (d < D[s.x-1][s.y]) + q.push(State(s.x-1, s.y, D[s.x-1][s.y]=d)); + } + + if (s.y && B[s.x][s.y]!=1) { + int d = s.d + (B[s.x][s.y]==2); + if (d < D[s.x][s.y-1]) + q.push(State(s.x, s.y-1, D[s.x][s.y-1]=d)); + } + + if (s.x < w-1 && L[s.x+1][s.y]!=1) { + int d = s.d + (L[s.x+1][s.y]==2); + if (d < D[s.x+1][s.y]) + q.push(State(s.x+1, s.y, D[s.x+1][s.y]=d)); + } + + if (s.y < h-1 && B[s.x][s.y+1]!=1) { + int d = s.d + (B[s.x][s.y+1]==2); + if (d < D[s.x][s.y+1]) + q.push(State(s.x, s.y+1, D[s.x][s.y+1]=d)); + } + } + cout << "-1\n"; + fin:; + } +} diff --git a/uva_cpp_clean/12027/12027-9.cpp b/uva_cpp_clean/12027/12027-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54d5ecf540a1e7e47f63bbbc281aaa06dc8a0cf3 --- /dev/null +++ b/uva_cpp_clean/12027/12027-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main(){ + string a; + int d, lim; + while(true){ + cin >> a; + if(a.size() == 1 && a[0]=='0') + break; + if(a.size() & 1) + d = sqrt(a[0] - '0'), lim = (a.size() - 1) / 2; + else + d = sqrt((a[0] - '0') * 10 + a[1] - '0'), lim = (a.size() - 2) / 2; + printf("%d", d); + for(int i = 0; i < lim; i++) + putchar('0'); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/1203/1203-14.cpp b/uva_cpp_clean/1203/1203-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..daa30c51aa9a12fd46dfeae0c4d5c1cf145fe175 --- /dev/null +++ b/uva_cpp_clean/1203/1203-14.cpp @@ -0,0 +1,132 @@ +/*************************************************** + * Problem Name : 1203 - Argus.cpp + * Problem Link : https://onlinejudge.org/external/12/1203.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-03-28 + * Problem Type : STL + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +bool cmp (pair >a, pair >b) { + if (a.first == b.first) { + return a.second.second < b.second.second; + + } else { + return a.first < b.first; + } +} + +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + string str; + int id, p; + vector > >vec; + + while (cin >> str) { + if (str == "#") break; + + cin >> id >> p; + vec.pb ({p, {p, id}}); + } + + int k; + cin >> k; + + while (k--) { + sort (all (vec), cmp); + cout << vec[0].second.second << "\n"; + vec[0].first = vec[0].first + vec[0].second.first; + } + + return 0; +} diff --git a/uva_cpp_clean/1203/1203-19.cpp b/uva_cpp_clean/1203/1203-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c438cab9bb74a90d5bd34add19958d278171bf78 --- /dev/null +++ b/uva_cpp_clean/1203/1203-19.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iii; + +int main() { + string trash; + priority_queue pq; + int k; + while (cin >> trash && trash != "#") { + int q, p; + cin >> q >> p; + pq.push(iii(-p, ii(-q, -p))); + } + cin >> k; + while (k--) { + iii ins = pq.top(); + pq.pop(); + cout << -ins.second.first << endl; + ins.first += ins.second.second; + pq.push(ins); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1203/1203-21.cpp b/uva_cpp_clean/1203/1203-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..862415133fb41023bf7cc9bb5d7edb4631f09890 --- /dev/null +++ b/uva_cpp_clean/1203/1203-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1203 + Name: Argus + Problem: https://onlinejudge.org/external/12/1203.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct Event { + int qid, tm; + Event(int q, int t):qid(q),tm(t){} + bool operator < (const Event &e) const { + return tm==e.tm ? qid>e.qid : tm>e.tm; + } +}; + +int qs[10000]; + +int main(){ + int qid, t, n; + priority_queue q; + while (scanf("Register %d %d\n", &qid, &t)==2) { + q.push(Event(qid, t)); + qs[qid] = t; + } + + scanf("%*s%d", &n); + for (int i=0; i +#include + +using namespace std; +using ii = std :: pair; +using vii = vector; + +auto comparisonOnly = [](const pair& a, const pair& b) +{ + if ( a.first == b.first ) + return a.second > b.second; + else + return a.first > b.first; +}; + +priority_queue registerQueue(comparisonOnly); +int periodArray[3001]; + +int main (){ + char registerTest[8]; + int queryID; + + while ( scanf("%s",registerTest) && registerTest[0] != '#' ) + { + scanf("%d", &queryID); + scanf("%d", &periodArray[queryID] ); + registerQueue.push( make_pair( periodArray[queryID], queryID)); + } + + int k, addingValue; + scanf("%d", &k); + + while ( k -- ) + { + queryID = registerQueue.top().second; + printf("%d\n", queryID); + registerQueue.push + (make_pair(registerQueue.top().first + periodArray[queryID], queryID)); + registerQueue.pop(); + } +} diff --git a/uva_cpp_clean/1203/1203-9.cpp b/uva_cpp_clean/1203/1203-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fec1c118886bf54cb95170edb7400a966bfc6d42 --- /dev/null +++ b/uva_cpp_clean/1203/1203-9.cpp @@ -0,0 +1,23 @@ +#include + +#define SIZE 30 + +using namespace std; + +int Q_num, Period, k; +char s[SIZE]; +priority_queue < pair > v; + +int main(){ + while(cin.getline(s, SIZE)){ + if(sscanf(s,"%*s %d %d", &Q_num, &Period) == -1) break; + for(int i = 0, it = Period; i < 10000; i++, it += Period) + v.push(make_pair( -it, -Q_num)); + } + scanf("%d",&k); + while(k--){ + printf("%d\n", -v.top().second); + v.pop(); + } + return(0); +} diff --git a/uva_cpp_clean/12032/12032-11.cpp b/uva_cpp_clean/12032/12032-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74874cab65cd3973958c6c7ddda73d89123784ff --- /dev/null +++ b/uva_cpp_clean/12032/12032-11.cpp @@ -0,0 +1,77 @@ +#include +/* +Problem: 12032 - The Monkey and the Oiled Bamboo +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3183 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +const int MAX_HEIGHT = 10000000; + +bool valid(int k); +int T, N, K, heights[100002], testcase = 0; + +int main () { + + heights[0] = 0; + scanf("%d\n", &T); + while ( T-- ){ + scanf("%d\n", &N); + + for (int i = 0; i < N; i++) + scanf("%d", &heights[i+1]); + + K = 0; + for (int b = MAX_HEIGHT; b >= 1; b /= 2){ + while (!valid(K+b)) K +=b; + } + + printf("Case %d: %d\n", ++testcase, K+1); + } + + return 0; +} + +bool valid(int k){ + + for (int i = 0; i < N; ++i){ + if (heights[i]+k < heights[i+1]){ + return false; + } + + if (heights[i+1]-heights[i] == k) k--; + } + + return true; +} + +/* +Sample input:- +----------------- +2 +5 +1 6 7 11 13 +4 +3 9 10 14 + +Sample output:- +----------------- +Case 1: 5 +Case 2: 6 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/12032/12032-13.cpp b/uva_cpp_clean/12032/12032-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ff738a477809a8c74348ae2ed30a271fad3a6a8 --- /dev/null +++ b/uva_cpp_clean/12032/12032-13.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +using namespace std; + +int binarySearch(int myLow, int a[], int n) +{ + int ans = 0; + + int l = myLow, h = 10000005; + + while (l < h) + { + int mid = (l + h) / 2; + + int temp = mid; + + bool ok = true; + + for (int i = 1; i <= n; i++) + { + if (a[i] - a[i - 1] > temp) + { + ok = false; + + break; + } + + if (a[i] - a[i - 1] == temp) temp--; + } + + if (ok) + { + h = mid; + + ans = mid; + } + + else + { + l = mid; + + if (mid + 1 == h) break; + } + } + + return ans; +} + +int main() +{ + int test, t = 1; + + cin >> test; + + while (test--) + { + int n, myLowerBound = 0; + + cin >> n; + + int* a = new int[n + 1]; + + a[0] = 0; + + for (int i = 1; i <= n; i++) + { + cin >> a[i]; + + myLowerBound = max(myLowerBound, a[i] - a[i - 1]); + } + + cout << "Case " << t++ << ": " << binarySearch(myLowerBound, a, n) << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/12032/12032-19.cpp b/uva_cpp_clean/12032/12032-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bdec7e8fd41a4f2e9a3b528cf92ca3c855cf8bf --- /dev/null +++ b/uva_cpp_clean/12032/12032-19.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +int main() { + int cases; + cin >> cases; + for (int c = 1; c <= cases; c++) { + int N, prev = 0, k = 0, max = 0; + cin >> N; + for (int i = 0; i < N; i++) { + int step; + cin >> step; + if (step - prev > max) { + if (step - prev <= k) k++, max = k; + else k = step - prev, max = k-1; + } if (step - prev == max) max--; + prev = step; + } + printf("Case %d: %d\n", c, k); + } + return 0; +} diff --git a/uva_cpp_clean/12032/12032-21.cpp b/uva_cpp_clean/12032/12032-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5312899777548a14b9831648de9cc460d54d7cd --- /dev/null +++ b/uva_cpp_clean/12032/12032-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12032 + Name: The Monkey and the Oiled Bamboo + Problem: https://onlinejudge.org/external/120/12032.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define INF 1073741824 +int x[100002], n; +bool ispos(int k) { + for (int i=1; i<=n; i++) { + int diff = x[i]-x[i-1]; + if (diff > k) return 0; + if (diff == k) k--; + } + return 1; +} + +int main() { + int T; + scanf("%d", &T); + for (int cse=1; cse<=T; cse++) { + scanf("%d", &n); + int lo=0, hi=0; + for (int i=1; i<=n; i++) { + scanf("%d", x+i); + int dif = x[i] - x[i-1]; + if (dif > lo) + hi = lo = dif; + else if (dif == lo) + hi++; + } + + while (lo <= hi) { + int mid = (lo+hi)>>1; + if (ispos(mid)) + hi = mid-1; + else + lo = mid+1; + } + printf("Case %d: %d\n", cse, hi+1); + } +} diff --git a/uva_cpp_clean/12032/12032-9.cpp b/uva_cpp_clean/12032/12032-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5ea9e2e60b17524d0a8d5f5675432b360361133 --- /dev/null +++ b/uva_cpp_clean/12032/12032-9.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int tc, n, v[100010], l0, hi, mid, pib; + +bool isValid(int strength){ + pib = v[0]; + for(int i = 1; i < n + 1; i++){ + if(strength < pib) return false; + else if(strength == pib) strength--; + pib = v[i] - v[i - 1]; + } + return true; +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d", &n); + for(int i = 0; i < n; i++) scanf("%d", &v[i]); + l0 = 1, hi = 10000000; + while(l0 != hi){ + mid = (l0 + hi) >> 1; + if(isValid(mid)) hi = mid; + else l0 = mid + 1; + } + printf("Case %d: %d\n", t, l0); + } + return(0); +} diff --git a/uva_cpp_clean/12043/12043-14.cpp b/uva_cpp_clean/12043/12043-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6c236a72d633e1bb258099b4fbbda99c87daa8a --- /dev/null +++ b/uva_cpp_clean/12043/12043-14.cpp @@ -0,0 +1,232 @@ +/*************************************************** + * Problem Name : 12043 - Divisors.cpp + * Problem Link : https://onlinejudge.org/external/120/12043.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-08-17 + * Problem Type : NT + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +vectorprime; +char mark[MAX]; + +void sieve (int n) { + int sq = (int) sqrt (n) + 1; + mark[0] = mark[1] = 1; + + for (int i = 4; i < n; i += 2) { + mark[i] = 1; + } + + for (int i = 0; i <= sq; i += 2) { + if (mark[i] == 0) { + for (int j = i * i; j < n; j += (2 * i) ) { + mark[j] = 1; + } + } + } + + for (int i = 1; i < n; i++) { + if (mark[i] == 0) prime.pb (i); + } +} + +int nod (int n) { + int cnt = 1; + int sq = (int) sqrt (n); + + for (int i = 0; i < (int) prime.size() && prime[i] <= sq; i++) { + int c = 0; + + while (n % prime[i] == 0) { + c++; + n /= prime[i]; + } + + sq = (int) sqrt (n); + c++; + cnt *= c; + } + + if (n != 1) { + cnt *= 2; + } + + return cnt; +} + + +int sod (int n) { + int sum = 1; + int sq = (int) sqrt (n); + + for (int i = 0; i < (int) prime.size() && prime[i] <= sq; i++) { + int c = 1; + int res = 1; + + while (n % prime[i] == 0) { + n /= prime[i]; + c *= prime[i]; + res += c; + } + + sq = (int) sqrt (n); + sum *= res; + } + + if (n != 1) { + sum *= (n + 1); + } + + return sum; +} + +int d[MAX], s[MAX]; + + +void pre_cal() { + for (int i = 1; i < MAX; i++) { + d[i] = nod (i); + s[i] = sod (i); + } +} + + +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + sieve (MAX - 2); + pre_cal(); + int tc; + cin >> tc; + + while (tc--) { + int a, b, k; + cin >> a >> b >> k; + + if (a > b) swap (a, b); + + ll sum_d = 0; + ll sum_s = 0; + + for (int i = a; i <= b; i++) { + if (i % k == 0) { + sum_d += (ll)d[i]; + sum_s += (ll)s[i]; + } + } + + cout << sum_d << " " << sum_s << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/12043/12043-18.cpp b/uva_cpp_clean/12043/12043-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2fd2305e9d60483c85d0ef0f56bb9fe55e374aca --- /dev/null +++ b/uva_cpp_clean/12043/12043-18.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +int d[100100], sigma[100100]; + +void divisors(){ + for(int i = 1; i <= 100000; i++){ + for(int j = i; j <= 100000; j+=i) d[j]++, sigma[j]+=i; + } +} + +int main(){ + int tc; + long long int a, b, c, g, h; + divisors(); + scanf("%d", &tc); + while(tc--){ + scanf("%lld %lld %lld", &a, &b, &c); + g = h = 0; + for(int i = a; i <= b; i++) if(i % c == 0) g+=d[i], h+=sigma[i]; + printf("%lld %lld\n", g, h); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12043/12043-9.cpp b/uva_cpp_clean/12043/12043-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c59963dd9082a64f67d9aeb92ae74366d8670df2 --- /dev/null +++ b/uva_cpp_clean/12043/12043-9.cpp @@ -0,0 +1,32 @@ +#include + +#define SIZE 100010 + +using namespace std; + +int numDiv[SIZE], sumDiv[SIZE], tc, a, b, k, ans1; +long long ans2; + +void sieve(){ + for(int it1 = 1; it1 < SIZE; it1++) + for(int it2 = it1; it2 < SIZE; it2 += it1) + numDiv[it2]++, sumDiv[it2] += it1; +} + +void solve(){ + ans1 = 0, ans2 = 0; + int ac = ( a % k == 0 ) ? 0 : (k - a % k); + for(int it = a + ac; it <= b; it += k) + ans1 += numDiv[it], ans2 += sumDiv[it]; +} + +int main(){ + sieve(); + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d", &a, &b, &k); + solve(); + printf("%d %lld\n", ans1, ans2); + } + return(0); +} diff --git a/uva_cpp_clean/12047/12047-13.cpp b/uva_cpp_clean/12047/12047-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9232a3d74e745a3d6431be8918133341cf1b71e9 --- /dev/null +++ b/uva_cpp_clean/12047/12047-13.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 10005 +int test, n, m, s, t, p, ans; + +ofstream fout("f.out"); + +int dist[2][MAX]; +vector > > >graph; +vector , int> > edges; + +void dijkstar(int pos, int start) +{ + dist[pos][start] = 0; + + queue pq; + + pq.push(start); + + while (!pq.empty()) + { + int head = pq.front(); + + pq.pop(); + + for (int i = 0; i < graph[pos][head].size(); i++) + { + int child = graph[pos][head][i].first; + + int weight = graph[pos][head][i].second; + + if (dist[pos][child] > weight + dist[pos][head]) + { + dist[pos][child] = weight + dist[pos][head]; + + pq.push(child); + } + } + } +} + +void initialize() +{ + ans = -1; + + graph.clear(); + graph.resize(2); + + graph[0].resize(n + 1); + graph[1].resize(n + 1); + + edges.clear(); + + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < MAX; j++) + { + dist[i][j] = 100000000; + } + } +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n >> m >> s >> t >> p; + + initialize(); + + for (int i = 0; i < m; i++) + { + int x, y, w; + + cin >> x >> y >> w; + + edges.push_back(make_pair(make_pair(x, y), w)); + + graph[0][x].push_back(make_pair(y, w)); + graph[1][y].push_back(make_pair(x, w)); + } + + dijkstar(0, s); + dijkstar(1, t); + + for (int i = 0; i < edges.size(); i++) + { + pair < pair , int > head = edges[i]; + + int x, y, w; + + x = head.first.first; + y = head.first.second; + w = head.second; + + if (dist[0][x] + w + dist[1][y] <= p) ans = max(ans, w); + } + + cout << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/12047/12047-21.cpp b/uva_cpp_clean/12047/12047-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..708d5a62ddb31ea6f2761eac49d77915e9a4e9d6 --- /dev/null +++ b/uva_cpp_clean/12047/12047-21.cpp @@ -0,0 +1,159 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12047 + Name: Highest Paid Toll + Problem: https://onlinejudge.org/external/120/12047.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 134217728 +using namespace std; +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------------- + +template +class IndexQueue { + int mind, maxd; + vector V[300000]; + void _update() { + while (mind<=maxd && V[mind].empty()) ++mind; + if (mind>maxd) clear(); + } + + public: + IndexQueue() { mind=INF; maxd=-1; } + bool empty() { _update(); return mind>maxd; } + void clear() { + for (int i=mind; i<=maxd; ++i) + V[i].clear(); + mind=INF; + maxd=-1; + } + + void push(int key, T data) { + V[key].push_back(data); + if (key < mind) mind = key; + if (key > maxd) maxd = key; + } + + T pop() { + _update(); + T r = V[mind].back(); + V[mind].pop_back(); + return r; + } +}; + +// ---------------------------------------------------------------------- + +struct UD { + int u, d; + UD(int u, int d):u(u),d(d){} +}; + +IndexQueue q; +void dijkstra(int n, int src, int maxd, int D[], const vector adj[]) { + for (int i=0; i adj[10043], adr[10043]; +int solve(int n, int src, int dst, int maxd) { + dijkstra(n, src, maxd, DS, adj); + if (DS[dst] > maxd) return -1; + + dijkstra(n, dst, maxd, DT, adr); + if (DT[src] > maxd) return -1; + + int mxr = -1; + for (int u=0; u mxr) { + int c = DS[u] + vd.d + DT[vd.u]; + if (c <= maxd) + mxr = vd.d; + } + return mxr; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + while (T--) { + int n = readUInt(), + m = readUInt(), + src = readUInt()-1, + dst = readUInt()-1, + maxd = readUInt(); + + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + + while (tc--) { + int n, m; + cin >> n >> m; + vectorvec (n); + + for (int i = 0 ; i < n; i++) { + cin >> vec[i]; + } + + vectorqr (m); + + for (int i = 0; i < m; i++) { + cin >> qr[i]; + } + + mapmp; + + for (auto v : vec) { + mp[v]++; + } + + for (auto v : qr) { + mp[v]--; + } + + int cnt = 0; + + for (auto it : mp) { + cnt += abs (it.S); + } + + cout << cnt << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/12049/12049-19.cpp b/uva_cpp_clean/12049/12049-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a62b8eafd9ee1672135fc76da33cd2695cc04cfe --- /dev/null +++ b/uva_cpp_clean/12049/12049-19.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; + +int main() { + int T; + cin >> T; + while (T--) { + int N, M, auxi; + unordered_map l1, l2; + cin >> N >> M; + for (int i = 0; i < N; i++) { + cin >> auxi; + l1[auxi]++; + } + for (int i = 0; i < M; i++) { + cin >> auxi; + l2[auxi]++; + } + int count = 0; + for (auto it = l1.begin(); it != l1.end(); ++it) { + count += abs(l2[it->first] - it->second); + it->second = l2[it->first] = min(l2[it->first], it->second); + } + for (auto it = l2.begin(); it != l2.end(); ++it) { + count += abs(l1[it->first] - it->second); + it->second = l1[it->first] = min(l1[it->first], it->second); + } + printf("%d\n", count); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12049/12049-21.cpp b/uva_cpp_clean/12049/12049-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f647b92b9287389d2585eeff60e63f08fa600e0 --- /dev/null +++ b/uva_cpp_clean/12049/12049-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12049 + Name: Just Prune The List + Problem: https://onlinejudge.org/external/120/12049.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + srand(rand()); + int rnd = rand(); + + int T, n, m, x; + scanf("%d", &T); + while (T--) { + unordered_map dif(20000); + scanf("%d%d", &n, &m); + int cnt = n + m; + + for (int i=0; isecond) { + --it->second; + cnt -= 2; + } + } + + printf("%d\n", cnt); + } +} diff --git a/uva_cpp_clean/12049/12049-9.cpp b/uva_cpp_clean/12049/12049-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..041c67bbc7d6c3a1d3b860f1811e3d794bd33448 --- /dev/null +++ b/uva_cpp_clean/12049/12049-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int tc, n, m, num, a, b, ans; + +int main(){ + scanf("%d", &tc); + while(tc--){ + multiset u, v; + set visited; + ans = 0; + scanf("%d %d", &n, &m); + for(int i = 0; i < n; i++) scanf("%d", &num), u.insert(num); + for(int i = 0; i < m; i++) scanf("%d", &num), v.insert(num); + for(multiset ::iterator it = u.begin(); it != u.end(); ++it){ + if(visited.find(*it) == visited.end()){ + a = u.count(*it), b = 0; + if(v.find(*it) != v.end()) b = v.count(*it), v.erase(*it); + visited.insert(*it); + ans += abs(a - b); + } + } + ans += v.size(); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/12050/12050-5.cpp b/uva_cpp_clean/12050/12050-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..148822efd083cd1bd6ecdaeeedc578e68b49bb6e --- /dev/null +++ b/uva_cpp_clean/12050/12050-5.cpp @@ -0,0 +1,99 @@ +/****************************************************************** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_HIDDEN *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +using namespace std; + +typedef vector vi; + +#define ll long long +#define pb push_back + +////============ CONSTANT ===============//// +#define mx 30 +////====================================//// + +ll pal[mx]; + +void palindrome() +{ + pal[1]=9; + pal[2]=9; + ll range=18; + for(int len=3;len2000000000) + break; + } +} + +int main() +{ + palindrome(); + ll n; + while(scanf("%lld",&n)) + { + if(n==0) break; + vi v; + for(int i=1;i<=mx;i++) + { + if(n<=pal[i]) + { + int m; + if(i%2) + m=(i+1)/2; + else + m=i/2; + + for(int j=0;j=0;l--) + v.pb(v[l]); + + break; + } + else + n=n-pal[i]; + } + int s=v.size(); + + for(int i=0;i +#include +#include +using namespace std; + +typedef long long ll; +typedef pair ii; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +int main() { + int n, c = 1; + while (cin >> n && n != 0) { + int acum = 0; + for (int i = 0; i < n; i++) { + int val; + cin >> val; + acum += val; + } + bool sign = acum < 0; + if (sign) acum = - acum; + int g = gcd(acum, n); + n /= g; + acum /= g; + int a = acum/n, b = acum%n; + int la = ceil(log10(a + 0.5)), lb = ceil(log10(b + 0.5)), ln = ceil(log10(n + 0.5)); + printf("Case %d:\n", c++); + if (n == 1) { + if (sign) printf("- "); + printf("%d\n", a); + } else { + if (sign) printf(" "); + for (int i = 0; i < la + ln - lb; i++) printf(" "); + printf("%d\n", b); + if (sign) printf("- "); + if (a > 0) printf("%d", a); + for (int i = 0; i < ln; i++) printf("-"); + printf("\n"); + if (sign) printf(" "); + for (int i = 0; i < la; i++) printf(" "); + printf("%d\n", n); + } + } + return 0; +} diff --git a/uva_cpp_clean/12068/12068-19.cpp b/uva_cpp_clean/12068/12068-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abd50d6cd56e7135a5da310a9631bc96f04f7b8a --- /dev/null +++ b/uva_cpp_clean/12068/12068-19.cpp @@ -0,0 +1,45 @@ +#include +#include +using namespace std; + +typedef pair frac; + +long long gcd(long long n, long long m) { + long long auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m > 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +void add(frac & q, frac r) { + q.first = q.first*r.second + r.first*q.second; + q.second = q.second*r.second; + long long d = gcd(q.first, q.second); + q.first /= d, q.second /= d; +} + +int main() { + int T, N, list[10]; + cin >> T; + for (int c = 1; c <= T; c++) { + cin >> N; + frac q(0, 1); + for (int i = 0; i < N; i++) { + cin >> list[i]; + frac r(1, list[i]); + add(q, r); + } + q.second *= N; + long long auxi = q.first; + q.first = q.second, q.second = auxi; + add(q, frac(0, 1)); + printf("Case %d: %lld/%lld\n", c, q.first, q.second); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12068/12068-9.cpp b/uva_cpp_clean/12068/12068-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..56078315927f4dfd80ecd3cc58d0dda2dcbce672 --- /dev/null +++ b/uva_cpp_clean/12068/12068-9.cpp @@ -0,0 +1,31 @@ +#include + +#define SIZE 20 + +using namespace std; + +int tc, n; +long long v[SIZE], num, den; + +long long gcd(long long a, long long b){ + return b == 0 ? a : gcd(b, a % b); +} + +long long lcm(long long a, long long b){ + return a * ( b / gcd(a, b)); +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d", &n); + for(int it = 0; it < n; it++) scanf("%lld", &v[it]); + num = v[0]; + for(int it = 1; it < n; it++) num = lcm(num, v[it]); + den = 0; + for(int it = 0; it < n; it++) den += num / v[it]; + num *= n; + printf("Case %d: %lld/%lld\n", t, num / gcd(num, den), den / gcd(num, den)); + } + return(0); +} diff --git a/uva_cpp_clean/1207/1207-14.cpp b/uva_cpp_clean/1207/1207-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b871f5d03a2f89f1b8b58140244e6d78b2583e1b --- /dev/null +++ b/uva_cpp_clean/1207/1207-14.cpp @@ -0,0 +1,85 @@ +/*************************************************** + * Problem name : 1207 AGTC.cpp + * Problem Link : https://uva.onlinejudge.org/external/12/1207.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-26 + * Problem Type : DP(LCS) + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 2000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int dp[MAX][MAX]; +string A, B; +int Edit_Dist(int a, int b) { + if (a == 0) return b; + if(b == 0) return a; + if(dp[a][b] != -1) return dp[a][b]; + if(A[a-1] == B[b -1]){ + return Edit_Dist(a - 1, b - 1); + } + int in = Edit_Dist(a, b -1) + 1; + int rm = Edit_Dist(a-1, b) + 1; + int rp = Edit_Dist(a - 1, b -1) + 1; + + return dp[a][b] = min(in, min(rm, rp)); +} +int main () { + int a, b; + while (scanf("%d", &a) == 1) { + getchar(); + cin >> A; + cin >> b; + getchar(); + cin >> B; + SET(dp); + printf("%d\n", Edit_Dist(a, b)); + } + + return 0; +} + diff --git a/uva_cpp_clean/1207/1207-19.cpp b/uva_cpp_clean/1207/1207-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..510c0d05e2d6d045b7beeba82852508675dbbf64 --- /dev/null +++ b/uva_cpp_clean/1207/1207-19.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int N, M; +string x, y; +unordered_map > dp; + +int proc(int i, int j) { + if (i >= N) return M - j; + if (j >= M) return N - i; + if (!dp[i].count(j)) { + if (x[i] == y[j]) dp[i][j] = proc(i+1, j+1); + else dp[i][j] = 1 + min(proc(i+1, j+1), min(proc(i+1, j), proc(i, j+1))); + } + return dp[i][j]; +} + +int main() { + while (cin >> N >> x >> M >> y) { + dp.clear(); + printf("%d\n", proc(0, 0)); + } + return 0; +} diff --git a/uva_cpp_clean/1207/1207-9.cpp b/uva_cpp_clean/1207/1207-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d591d30501b50f64e5b6c2d67c0d91ff6d8903b8 --- /dev/null +++ b/uva_cpp_clean/1207/1207-9.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +const int W_DEL = 1, W_INS = 1, W_SUB = 1; + +const int MAX_LEN = 1000 + 10; + +string s1, s2; +int dp[MAX_LEN][MAX_LEN]; + +void stringAlignment () { + int m = s1.size(), n = s2.size(); + for (int i = 0; i <= n; i++) { + for (int j = 0; j <= m; j++) { + if (i == 0) dp[0][j] = j * W_INS; + else if (j == 0) dp[i][j] = i * W_DEL; + else if (s1[j - 1] == s2[i - 1]) dp[i][j] = dp[i - 1][j - 1]; + else dp[i][j] = min(dp[i - 1][j - 1] + W_SUB, min(dp[i - 1][j] + W_DEL, dp[i][j - 1] + W_INS)); + } + } + cout << dp[n][m] << endl; +} + +int main () { + int x, y; + while (cin >> x >> s1 >> y >> s2) stringAlignment(); + return (0); +} diff --git a/uva_cpp_clean/1208/1208-10.cpp b/uva_cpp_clean/1208/1208-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b331eb87578119aba8c94eb971ad4ddfff06b78 --- /dev/null +++ b/uva_cpp_clean/1208/1208-10.cpp @@ -0,0 +1,125 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e2+9; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; + +vector> minimumSpanningTree(vector> &edges) { + + + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + + vector> ans; + + int weight; + int a,b; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + ans.push_back({a,b,weight}); + } + } + + return ans; + +} + +void test() { + + int n; + cin >> n; + + int x; + char c; + + vector> arr; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + cin >> x; + + if (j < n - 1) cin >> c; + + if (x > 0 && j >= i) { + arr.push_back({x, i, j}); + } + } + } + + auto res = minimumSpanningTree(arr); + + for (auto ans: res) { + cout << char(get<0>(ans) + 'A') << "-" << char(get<1>(ans) + 'A') << " " << get<2>(ans) << "\n"; + } + +} + +int main() { + FAST + + int t; cin>>t; + + int c=1; + while (t--) { + cout<<"Case "< +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair tunnel; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +priority_queue pq; + +int main() { + char trash; + int cases; + cin >> cases; + for (int c = 1; c <= cases; c++) { + int n; + cin >> n; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + int cost; + cin >> cost; + if (j != n - 1) cin >> trash; + if (cost != 0) { + tunnel l; l.second.first = -i; l.second.second = -j; + l.first = -cost; + pq.push(l); + } + } + } + ufds u(n); + printf("Case %d:\n", c); + while (!pq.empty()) { + if (!u.isSameSet(-pq.top().second.first, -pq.top().second.second)) { + printf("%c-%c %d\n", -pq.top().second.first + 'A', -pq.top().second.second + 'A', -pq.top().first); + u.unionSet(-pq.top().second.first, -pq.top().second.second); + } + pq.pop(); + } + } + return 0; +} diff --git a/uva_cpp_clean/1208/1208-21.cpp b/uva_cpp_clean/1208/1208-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d69be658732ace9cfe259922859786bbd3cc4be --- /dev/null +++ b/uva_cpp_clean/1208/1208-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1208 + Name: Oreon + Problem: https://onlinejudge.org/external/12/1208.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char gid[30]; +char find(char u) { + return gid[u]==u?u: gid[u] = find(gid[u]); +} +void join(char u, char v) { + gid[find(u)]=find(v); +} + +struct Edge { + int w; + char u, v; + Edge(char u, char v, char w):u(u),v(v),w(w){} + bool operator < (const Edge &e) const { + return w > e.w; + } +}; + + +int main(){ + int T, n, w; + scanf("%d", &T); + for (int cse=1; cse<=T; ++cse) { + scanf("%d", &n); + priority_queue q; + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +int tc, N, M, w; +vector < pair > EdgeList; + +void Kruskal(){ + UnionFind UF(N); + sort(EdgeList.begin(), EdgeList.end()); + int setSize = 1; + for(int it = 0; it < EdgeList.size(); it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + printf("%c-%c %d\n", front.second.first + 'A', front.second.second + 'A', front.first), setSize++; + UF.unionSet(front.second.first, front.second.second); + if( setSize == N ) return; + } +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + EdgeList.clear(); + scanf("%d", &N); + for(int row = 0; row < N; row++) + for(int col = 0; col < N; col++){ + scanf("%d, ", &w); + if( col > row and w ) + EdgeList.push_back( make_pair( w, ii(row, col) ) ); + } + printf("Case %d:\n", t); + Kruskal(); + } + return(0); +} diff --git a/uva_cpp_clean/12083/12083-21.cpp b/uva_cpp_clean/12083/12083-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29263f0941d9469e7cf77d81e11cf4ff77716e8f --- /dev/null +++ b/uva_cpp_clean/12083/12083-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12083 + Name: Guardian of Decency + Problem: https://onlinejudge.org/external/120/12083.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +vector adj[555]; +bool seen[555]; +int match[555]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + if (match[v]==-1 || bpm(match[v])) { + match[v] = u; + return true; + } + } + return false; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string M[555], S[555], g; + int H[555], T, n; + cin >> T; + while (T-- && cin >> n) { + vector L, R; + for (int i=0; i> H[i] >> g >> M[i] >> S[i]; + if (g[0] == 'M') + L.push_back(i); + else + R.push_back(i); + } + + for (int i=0; i +#include +#include + +std::string range(int begin, int end) +{ + std::string beginStr = std::to_string(begin); + std::string endStr = std::to_string(end); + std::string result = ""; + bool changed = false; + + for (unsigned int i = 0; i < beginStr.length(); i++) + { + if (beginStr[i] != endStr[i]) + { + changed = true; + } + + if (changed) + { + result += endStr[i]; + } + } + + return result; +} + +int main() +{ + int n, caseNr = 0; + while (std::cin >> n && n != 0) + { + std::vector numbers; + + for (int i = 0; i < n; i++) + { + std::string number; + std::cin >> number; + numbers.push_back(std::stoll(number)); + } + + long long start = 0; + numbers.push_back(0); + + std::cout << "Case " << ++caseNr << ":" << std::endl; + + for (unsigned int i = 0; i < numbers.size() - 1; i++) + { + if (numbers[i] + 1 == numbers[i + 1]) + { + if (start == 0) + { + start = numbers[i]; + } + } + else + { + if (start != 0) + { + std::cout << 0 << start << "-" << range(start, numbers[i]) << std::endl; + start = 0; + } + else + { + std::cout << 0 << numbers[i] << std::endl; + } + } + } + + std::cout << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/12086/12086-14.cpp b/uva_cpp_clean/12086/12086-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..871d372a7be963593e7ea38b0280c5d03a834292 --- /dev/null +++ b/uva_cpp_clean/12086/12086-14.cpp @@ -0,0 +1,133 @@ +/*************************************************** + * Problem Name : 12086 Potentiometers.cpp + * Problem Link : https://uva.onlinejudge.org/external/120/12086.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-10-10 + * Problem Type : Data Structure (BIT) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 200005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +ll tree[MAX]; +int getNext (int id) { + return id + (id & -id); +} +int getPar (int id) { + return id - (id & -id); +} +void update (int id, int n, ll val) { + id++; + + while (id <= n) { + tree[id] += val; + id = getNext (id); + } +} +ll query (int id) { + id++; + ll sum = 0; + + while (id > 0) { + sum += tree[id]; + id = getPar (id); + } + + return sum; +} +int main () { + __FastIO; + int n, t = 1; + + while (cin >> n) { + if (n == 0) break; + + ll ar[MAX]; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + SET (tree, 0); + + for (int i = 0; i < n; i++) { + update (i, n, ar[i]); + } + + string str; + + if (t != 1) nl; + + cout << "Case " << t++ << ":\n"; + + while (cin >> str) { + if (str == "END") break; + + if (str[0] == 'M') { + int l, r; + cin >> l >> r; + l--, r--; + cout << (query (r) - query (l) ) + ar[l] << "\n"; + + } else { + int id, val; + cin >> id >> val; + id--; + update (id, n, val - ar[id]); + ar[id] = val; + } + } + } + + return 0; +} diff --git a/uva_cpp_clean/12086/12086-18.cpp b/uva_cpp_clean/12086/12086-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2bb1070ba70630758959d48d928a93f16957ed01 --- /dev/null +++ b/uva_cpp_clean/12086/12086-18.cpp @@ -0,0 +1,74 @@ +#include +#include + +using namespace std; + +int pots[900000]; +//Segmend tree implemetation +void insert_update(int idx, int st, int ed, int pos, int value) +{ + if(st == pos && pos == ed) + { + pots[idx] = value; + return; + } + + int l = idx * 2; + int r = l + 1; + int mid = (st + ed) / 2; + + if(pos <= mid) insert_update(l, st, mid, pos, value); + else insert_update(r, mid+1, ed, pos, value); + + pots[idx] = pots[l] + pots[r]; +} + + +int quer(int idx, int st, int ed, int i, int j) +{ + if(i==st && j==ed) return pots[idx]; + + int l = idx * 2; + int r = l + 1; + int mid = (st + ed) / 2; + + + if(j<=mid) + { + return quer(l, st, mid, i, j); + } + else if(i > mid) + { + return quer(r, mid+1, ed, i, j); + } + else return quer(l, st, mid, i, mid) + quer(r, mid+1, ed, mid+1, j); + + +} + +int main() +{ + int n, tc = 0, x, y, num; + char type[4]; + while (scanf("%d", &n), n) + { + for (int i = 1; i <= n; i++){ + scanf("%d", &num); + insert_update(1, 1, n, i, num); + } + if(tc) printf("\n"); + printf("Case %d:\n", ++tc); + while (scanf("%s", type), strcmp(type, "END") !=0) + { + scanf("%d %d", &x, &y); + if (type[0] == 'S') + { + insert_update(1, 1, n, x, y); + } else if (type[0] == 'M'){ + int sum = quer(1, 1, n, x, y); + printf("%d\n", sum); + } + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12086/12086-19.cpp b/uva_cpp_clean/12086/12086-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d303ba07020f8dffa16151b3d8055b149c11080b --- /dev/null +++ b/uva_cpp_clean/12086/12086-19.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +inline int LSOne(int i) { + return (i & (-i)); +} + +struct Fenwick { + vi ft, values; + + Fenwick(int n) { + ft.assign(n + 1, 0); + values.assign(n, 0); + } + + int rsq(int b) { + b++; + int sum = 0; + for (; b; b -= LSOne(b)) sum += ft[b]; + return sum; + } + + int rsq(int a, int b) { + return rsq(b) - (a == 0 ? 0 : rsq(a - 1)); + } + + void insert(int pos, int val) { + int diff = val - values[pos], k = pos + 1; + values[pos] = val; + for (; k < (int)ft.size(); k += LSOne(k)) ft[k] += diff; + } + + void insertDiff(int val, int diff) { + insert(val - 1, values[val-1] + diff); + } +}; + +int main() { + int N, c = 1; + while (cin >> N && N != 0) { + if (c != 1) printf("\n"); + Fenwick fen(N); + for (int i = 0; i < N; i++) { + int aux; + cin >> aux; + fen.insert(i, aux); + } + string s; + printf("Case %d:\n", c); + while (cin >> s && s != "END") { + int a, b; + cin >> a >> b; + if (s == "S") { + fen.insert(a - 1, b); + } else { + printf("%d\n", fen.rsq(a - 1, b - 1)); + } + } + c++; + } + return 0; +} diff --git a/uva_cpp_clean/12086/12086-21.cpp b/uva_cpp_clean/12086/12086-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f221e6630b1514b20901482c82a6a7b4d5b66aa --- /dev/null +++ b/uva_cpp_clean/12086/12086-21.cpp @@ -0,0 +1,122 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12086 + Name: Potentiometers + Problem: https://onlinejudge.org/external/120/12086.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +inline char readCh() { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + return ch; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +char buf[50]; +void print(long long x) { + int i=48, neg=0; + buf[i--] = '\n'; + if (!x) buf[i--] = '0'; + else if (x < 0) { + x = -x; + neg = 1; + } + for (; x; x/=10) + buf[i--] = x%10 + '0'; + + if (neg) buf[i--] = '-'; + fwrite_unlocked(buf+i+1, 1, 48-i, stdout); +} + +// ------------------------------------------------------------------------ + +#define LSB(i) ((i) & -(i)) + +void init(int BIT[], int size) { + for (int i = 0; i < size; ++i) { + int j = i + LSB(i+1); + + if (j < size) + BIT[j] += BIT[i]; + } +} + +void update(int BIT[], int size, int i, int delta) { + while (i < size) { + BIT[i] += delta; + i += LSB(i+1); + } +} + +// [i .. j-1] +int getRange(int BIT[], int i, int j) { + int sum = 0; + while (j > i) { + sum += BIT[j-1]; + j -= LSB(j); + } + while (i > j) { + sum -= BIT[i-1]; + i -= LSB(i); + } + return sum; +} + +// ------------------------------------------------------------------------ + +int X[200017], B[200017]; +int main() { + bool frst = 1; + int n, cse = 1; + while ((n=readUInt())) { + if (frst) frst = 0; + else putchar('\n'); + printf("Case %d:\n", cse++); + + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1209 + Name: Wordfish + Problem: https://onlinejudge.org/external/12/1209.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int mdist(string str) { + int d = 1000; + for (int i=1; i>s1) { + string s2 = s1, bs = s1; + int d = mdist(s1); + for (int i=0; i<10 && prev_permutation(s1.begin(), s1.end()); ++i) { + int t = mdist(s1); + if (t >= d) { + bs = s1; + d = t; + } + } + + for (int i=0; i<10 && next_permutation(s2.begin(), s2.end()); ++i) { + int t = mdist(s2); + if (t > d) { + bs = s2; + d = t; + } + } + + cout << bs << d << endl; + } +} diff --git a/uva_cpp_clean/1209/1209-9.cpp b/uva_cpp_clean/1209/1209-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18ca3d25ed438cf8de2069164e0e87634007057e --- /dev/null +++ b/uva_cpp_clean/1209/1209-9.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int _max; +string s, ss, ans; +vector v; + +int distance(string x){ + int a, b, aux = INT_MAX; + for(int i = 1; i < x.size(); i++){ + a = x[i] - 'A', b = x[i-1] - 'A'; + aux = min(aux, abs(a - b)); + } + return aux; +} + +int main(){ + while(cin >> s){ + ans = "", v.clear(), ss = s, _max = -1; + for(int i = 0; i < 10; i++){ + prev_permutation(ss.begin(), ss.end()); + v.push_back(ss); + } + v.push_back(s); + for(int i = 0; i < 10; i++){ + next_permutation(s.begin(), s.end()); + v.push_back(s); + } + sort(v.begin(), v.end()); + for(int i = 0; i < v.size(); i++){ + int p = distance(v[i]); + if(p > _max) _max = p, ans = v[i] + to_string(p); + } + cout << ans << endl; + } + return(0); +} diff --git a/uva_cpp_clean/12096/12096-17.cpp b/uva_cpp_clean/12096/12096-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e436590b6b34d9256f87dfe785cb691a0e19ade --- /dev/null +++ b/uva_cpp_clean/12096/12096-17.cpp @@ -0,0 +1,53 @@ +/** + * AOAPC II Example 5-5 The SetStack Computer + * ICPC NWERC 2006 + */ +#include +using namespace std; +map, int> IDs; +vector> Sets; +int ID(set st) { + if (IDs.find(st) != IDs.end()) + return IDs[st]; + Sets.push_back(st); + return IDs[st] = Sets.size() - 1; +} +int main() { + int t; + cin >> t; + while (t--) { + IDs.clear(); + Sets.clear(); + stack stk; + int n; + cin >> n; + string op; + while (n--) { + cin >> op; + if (op[0] == 'P') + stk.push(ID(set())); + else if (op[0] == 'D') + stk.push(stk.top()); + else { + auto x1 = Sets[stk.top()]; + stk.pop(); + set x2 = Sets[stk.top()], x; + stk.pop(); + switch (op[0]) { + case 'U': + set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin())); + break; + case 'I': + set_intersection(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin())); + break; + case 'A': + x = x2; + x.insert(ID(x1)); + } + stk.push(ID(x)); + } + cout << Sets[stk.top()].size() << "\n"; + } + cout << "***\n"; + } +} diff --git a/uva_cpp_clean/12097/12097-6.cpp b/uva_cpp_clean/12097/12097-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6045e24c63baf71e28b2e72671a0cacda0f25882 --- /dev/null +++ b/uva_cpp_clean/12097/12097-6.cpp @@ -0,0 +1,69 @@ +/* + miscellaneous > binary search > on answer + difficulty: medium + date: 29/Dec/2019 + by: @brpapa +*/ +#include +#include +#include +#define EPS 1e-5 +using namespace std; + +vector piesVol; + +// 1 barriga só pode comer 1 pedaço de 1 torta +bool can(double v, int B) { + // v: volume do pedaço + // B: qte de barrigas famintas + + int b = 0; + for (double pieVol : piesVol) { + double leftPieVol = pieVol; + + while (leftPieVol - v >= 0) { + leftPieVol -= v; + b++; + } + + if (b >= B) + return true; + } + return false; +} + +int main() { + int T; + cin >> T; + + while (T--) { + int N, B; + cin >> N >> B; + B++; + + piesVol.assign(N, 0); + int radii; + double sumPiesVol = 0; + for (double &pieVol : piesVol) { + cin >> radii; + pieVol = M_PI * radii * radii; + sumPiesVol += pieVol; + } + + double low = 0, high = sumPiesVol / B; + double ans = 0; // volume do pedaço + + while (high - low > EPS) { + double mid = (low + high) / 2; + + if (can(mid, B)) { + ans = mid; + low = mid; + } else { + high = mid; + } + } + printf("%.4lf\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/121/121-21.cpp b/uva_cpp_clean/121/121-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f7ee580fe24037da6d23f49d7efc19cf83ee3c0 --- /dev/null +++ b/uva_cpp_clean/121/121-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 121 + Name: Pipe Fitters + Problem: https://onlinejudge.org/external/1/121.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +const double cc = 1.1547005383792515; +int skew(double w, double h) { + int hn = cc*(h-1) + 1, + hn1 = hn>>1, + hn2 = hn-hn1, + wn2 = w, + wn1 = w-wn2<0.5 ? wn2-1 : wn2; + + return hn1*wn1 + hn2*wn2; +} + +int main(){ + double a, b; + while (cin>>a>>b) { + int g = floor(a)*floor(b), + s = max(skew(a,b), skew(b,a)); + + if (g >= s) + printf("%d grid\n", g); + else printf("%d skew\n", s); + } +} diff --git a/uva_cpp_clean/1210/1210-14.cpp b/uva_cpp_clean/1210/1210-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd0f62587995d5a4318d49af89c5c33b081bffa2 --- /dev/null +++ b/uva_cpp_clean/1210/1210-14.cpp @@ -0,0 +1,161 @@ +/*************************************************** + * Problem Name : 1210 - Sum of Consecutive Prime Numbers.cpp + * Problem Link : https://onlinejudge.org/external/12/1210.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-08-17 + * Problem Type : NT + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 10005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +vectorprime; +bool isPrime (int n) { + for (int i = 2; i <= sqrt (n); i++) { + if (n % i == 0) return 0; + } + + return 1; +} + +void pre_cal() { + for (int i = 2; i < MAX; i++) { + if (isPrime (i) ) { + prime.pb (i); + } + } +} + +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + pre_cal(); + int n; + + while (cin >> n) { + if (n == 0) break; + + int cnt = 0; + + for (int i = 0; i < (int) prime.size(); i++) { + int sum = 0; + + for (int j = i; j < (int) prime.size(); j++) { + sum += prime[j]; + + if (sum == n) cnt++; + } + } + + cout << cnt << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/1210/1210-19.cpp b/uva_cpp_clean/1210/1210-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a4a3c63008ff3dd8cda242ae2bd00f06f920826 --- /dev/null +++ b/uva_cpp_clean/1210/1210-19.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; +typedef unordered_map uii; +typedef unordered_map uiuii; + +ll sieve_size; +bitset<100000001> bs; +vi primes; +uiuii dp; + +inline void sieve(ll upperbound = 10002) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +int proc(int n, int next) { + if (n == 0) return 1; + if (next >= primes.size()) return 0; + if (!(dp.count(n) && dp[n].count(next))) { + int val = 0; + if (n - primes[next] >= 0) val = proc(n - primes[next], next + 1); + dp[n][next] = val; + } + return dp[n][next]; +} + +int main() { + sieve(); + int n; + while (cin >> n && n != 0) { + int val = 0; + for (int i = 0; i < primes.size(); i++) { + if (n - primes[i] < 0) break; + val += dp[n][i] = proc(n - primes[i], i + 1); + } + printf("%d\n", val); + } + return 0; +} diff --git a/uva_cpp_clean/1210/1210-20.cpp b/uva_cpp_clean/1210/1210-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9eb359d432d85c5d0c594b5f053359babaf0a29 --- /dev/null +++ b/uva_cpp_clean/1210/1210-20.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace std; + +#define N 10002 +#define Check(N,pos) ((bool) ((N)&(1<<(pos)))) +#define Set(N,pos) ((N)=((N)|(1<<(pos)))) + +unsigned status[(N>>5)+1], counter[N]; +vector < unsigned > primes; + +void bitSieve() +{ + unsigned i, j, sqrtN = unsigned (sqrt(N)); + for(i=3 ; i<=sqrtN ; i+=2) + if( !Check(status[i>>5], i&31) ) + for(j=i*i ; j<=N ; j+=(i<<1)) status[j>>5] = Set(status[j>>5], j&31); + + primes.push_back(2); + for(i=3 ; i<=N ; i+=2) + if( !Check(status[i>>5], i&31) ) primes.push_back(i); +} + +void primeConsecutiveSum() +{ + for(unsigned i=0 ; i(N-2) ) break; + counter[sum]++; + } + } +} + +int main() +{ + bitSieve(); + primeConsecutiveSum(); + + unsigned n; + while( scanf("%u", &n)==1 && n ) + printf("%u\n", counter[n]); + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1210/1210-21.cpp b/uva_cpp_clean/1210/1210-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4b3b1db3b2749866c972545fe69a7190021bbf3 --- /dev/null +++ b/uva_cpp_clean/1210/1210-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1210 + Name: Sum of Consecutive Prime Numbers + Problem: https://onlinejudge.org/external/12/1210.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXP 10001 +bool nisp[MAXP] = {1, 1}; +int cump[MAXP]={2}, pcnt=1; +void sieve() { + for (int i=3; i=0 && cump[i]-cump[j] + +using namespace std; + +const int MAX_N = 10010; + +int N, ways[MAX_N]; +bool is_prime[MAX_N]; +vector prime; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(int i = 2; i < MAX_N; i++) + if( is_prime[i] ){ + for(int j = i * i; j < MAX_N; j += i) + is_prime[j] = false; + prime.push_back(i); + } +} + +void precomputation(){ + sieve(); + for(int i = 0; i < prime.size(); i++) + for(int j = i, sum = 0; j < prime.size() and sum + prime[j] < MAX_N; j++) + sum += prime[j], ways[sum]++; +} + +int main(){ + precomputation(); + while(scanf("%d", &N), N) printf("%d\n", ways[N]); + return(0); +} diff --git a/uva_cpp_clean/12100/12100-14.cpp b/uva_cpp_clean/12100/12100-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92a38cc7a13a7cfafd61d95c74933c5316c3b36b --- /dev/null +++ b/uva_cpp_clean/12100/12100-14.cpp @@ -0,0 +1,106 @@ +/*************************************************** + * Problem Name : 12100 - Printer Queue.cpp + * Problem Link : https://uva.onlinejudge.org/external/121/12100.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-07-17 + * Problem Type : STL + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +bool cmp (pii a, pii b) { + if (a.second == b.second ) return a.first < b.first; + else return a.second > b.second ; +} +int main () { + __FastIO; + int tc, n, m; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> n >> m; + //~ int ar[n + 3]; + queueq; + vectorv; + + for (int i = 0; i < n; i++) { + int x; + cin >> x; + q.push (pii (i, x) ); + v.pb (pii (i, x) ); + } + + sort (all (v), cmp); + //~ for (int i = 0; i < n; i++) { + //~ cout << v[i].second << " " << v[i].first << "\n"; + //~ } + int i = 0, ans; + + while (!q.empty() ) { + pii top = q.front(); + q.pop(); + + if (top.second == v[i].second) { + i++; + + if (top.first == m) { + ans = i; + break; + } + + } else { + q.push (top); + } + } + + cout << ans << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/12100/12100-17.cpp b/uva_cpp_clean/12100/12100-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9d603f37aa2f6c9a0274baef5d501c9cf6a1a7d --- /dev/null +++ b/uva_cpp_clean/12100/12100-17.cpp @@ -0,0 +1,39 @@ +/** + * AOAPC II Exercise 5-7 Printer Queue + * 25 AC + */ +#include +using namespace std; +int main() +{ + int T; + cin >> T; + while (T--) + { + int n, m; + cin >> n >> m; + vector jobs(n); + vector nums(10, 0); + for (int &i : jobs) + { + cin >> i; + ++nums[i]; + } + int priority = 9, i = 0, t = 0; + while (true) + { + while (nums[priority] == 0) + --priority; + if (jobs[i] == priority) + { + jobs[i] = 0; + ++t; + --nums[priority]; + if (i == m) + break; + } + i = (i + 1) % n; + } + cout << t << "\n"; + } +} diff --git a/uva_cpp_clean/12100/12100-18.cpp b/uva_cpp_clean/12100/12100-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f21d9d14dc97bacc52fca5c42d091e17d1a59a9d --- /dev/null +++ b/uva_cpp_clean/12100/12100-18.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +using namespace std; + +bool cmp(const int &i, const int &j){ + if(i > j) return true; + return false; +} + +int main(){ + int tc, n, m, time, priority, indexPP; + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &n, &m); + queue> cola; + vector priorityPrint; + for(int i = 0; i < n; i++){ + scanf("%d", &priority); + if(i == m) cola.push(make_pair(priority, true)); + else cola.push(make_pair(priority, false)); + priorityPrint.push_back(priority); + } + time = 0; + indexPP = 0; + sort(priorityPrint.begin(), priorityPrint.end(), cmp); + while(!cola.empty()){ + int prior = cola.front().first; + bool myJob = cola.front().second; + cola.pop(); + if(prior == priorityPrint[indexPP]){ + time++; + if(myJob) break; + indexPP++; + }else cola.push(make_pair(prior, myJob)); + } + printf("%d\n", time); + } + return 0; +} +/* + La estrategia es usar un vector y una queue, el vector me tiene organizado las prioridades y en la queue guardo + las prioridades tambien, esta a su vez la inicializo con pair para guardar un booleano a cada prioridad y asi + identificar "my job". El vector lo organizo de la mayor prioridad a la menor. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/12100/12100-19.cpp b/uva_cpp_clean/12100/12100-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7897838611b9bd0794e375bfa834ed22a1e555d4 --- /dev/null +++ b/uva_cpp_clean/12100/12100-19.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +int main() { + int jobs[9]; + int T; + cin >> T; + while (T--) { + for (int i = 0; i < 9; i++) jobs[i] = 0; + int n, m; + cin >> n >> m; + queue q; + for (int i = 0; i < n; i++) { + int v; + cin >> v; + jobs[v-1]++; + if (i == m) v += 10; + q.push(v-1); + } + int cp = 8, min = 0; + while (jobs[cp] == 0 && cp >= 0) cp--; + bool stop = false; + while (!stop) { + if (cp > (q.front()%10)) q.push(q.front()); + else { + min++; + jobs[cp]--; + while (jobs[cp] == 0 && cp >= 0) cp--; + if (q.front() >= 10) stop = true; + } + q.pop(); + } + printf("%d\n", min); + } + return 0; +} diff --git a/uva_cpp_clean/12100/12100-21.cpp b/uva_cpp_clean/12100/12100-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dec303082c6c76bf371680818754cb96810926af --- /dev/null +++ b/uva_cpp_clean/12100/12100-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12100 + Name: Printer Queue + Problem: https://onlinejudge.org/external/121/12100.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + int T, n, m, x; + cin>>T; + while (T--) { + int qs[10] = {}; + cin >> n >> m; + queue q; + for (int i=0; i> x; + q.push(i==m ? -x : x); + for (int y=1; y +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef vector vi; +typedef vector vii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; +vi codes; +unordered_map m; + +void sieve(ll upperbound = 10001) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + if (i > 1000 && i < 10000) m[i] = codes.size(), codes.push_back(i); + } +} + +#define N 1061 +int AdjMat[N][N]; + +void floyd() { + for (int k = 0; k < N; k++) + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + if (AdjMat[i][k] + AdjMat[k][j] < AdjMat[i][j]) { + AdjMat[i][j] = AdjMat[i][k] + AdjMat[k][j]; + } +} + +int main() { + sieve(); + for (int i = 0; i < N; i++) { + AdjMat[i][i] = 0; + for (int j = i + 1; j < N; j++) { + int c1 = codes[i], c2 = codes[j], match = 0; + while (c1) { + if (c1%10 == c2%10) match++; + c1 /= 10, c2 /= 10; + } + if (match == 3) AdjMat[i][j] = AdjMat[j][i] = 1; + else AdjMat[i][j] = AdjMat[j][i] = 2000; + } + } + floyd(); + int T; + scanf("%d", &T); + while (T--) { + int s, d; + scanf("%d %d", &s, &d); + s = m[s], d = m[d]; + if (AdjMat[s][d] >= 2000) printf("Impossible\n"); + else printf("%d\n", AdjMat[s][d]); + } + return 0; +} diff --git a/uva_cpp_clean/1211/1211-21.cpp b/uva_cpp_clean/1211/1211-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7495aea22ff8f295bd08458a1ee827cc19009035 --- /dev/null +++ b/uva_cpp_clean/1211/1211-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1211 + Name: Atomic Car Race + Problem: https://onlinejudge.org/external/12/1211.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(4); + + int A[143], n, r; + double C[10143], DP[143], b, v, e, f; + A[0] = C[0] = DP[0] = 0; + + while (cin >> n && n) { + for (int i=1; i<=n; ++i) + cin >> A[i]; + cin >> b >> r >> v >> e >> f; + + for (int x=0; x<=A[n]; ++x) + C[x] = (x?C[x-1]:0) + (x>=r ? 1/(v-e*(x-r)) : 1/(v-f*(r-x))); + + for (int i=1; i<=n; ++i) { + DP[i] = 1e100; + for (int j=0; j +using namespace std; + +int main() { + int B, S, c = 1; + while (cin >> B >> S && !(B == 0 && S == 0)) { + printf("Case %d: ", c); + if (B == 1) printf(":-\\\n"); + else if (S >= B) printf(":-|\n"); + else printf(":-(\n"); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/12114/12114-5.cpp b/uva_cpp_clean/12114/12114-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e6467456ac73b2d705fb3401efdd4c44fa18dfca --- /dev/null +++ b/uva_cpp_clean/12114/12114-5.cpp @@ -0,0 +1,19 @@ +#include +int main() +{ + long long int B,S; + int t=1; + while(scanf("%lld%lld",&B,&S)==2) + { + if(B==0&&S==0) + break; + if(B<=1) + printf("Case %d: :-\\\n",t++); + else if(B<=S) + printf("Case %d: :-|\n",t++); + else + printf("Case %d: :-(\n",t++); + + } + return 0; +} diff --git a/uva_cpp_clean/12124/12124-17.cpp b/uva_cpp_clean/12124/12124-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57c5e8d3aedd9b8e78d27caa29ed41b52736201f --- /dev/null +++ b/uva_cpp_clean/12124/12124-17.cpp @@ -0,0 +1,67 @@ +/** + * Assemble + * 2007 NWERC + * LA 3971 + */ +#include +using namespace std; +struct Comp +{ + int p, q; + Comp(int p, int q) : p(p), q(q) { } +}; +map> types; +int b; +bool ok(int q) +{ + int s = 0; + for (const auto &type : types) + { + int minP = 2e6; + for (auto &comp : type.second) + if (comp.q >= q) + minP = min(minP, comp.p); + if (minP == 2e6 || (s += minP) > b) + return false; + } + return true; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int t; + cin >>t; + while (t--) + { + int n; + cin >> n >> b; + types.clear(); + // vector qs; + int maxQ = 0; + while (n--) + { + string type, name; + int p, q; + cin >> type >> name >> p >> q; + types[type].push_back({p, q}); + // qs.push_back(q); + maxQ = max(maxQ, q); + } + // sort(qs.begin(), qs.end()); + // qs.erase(unique(qs.begin(), qs.end()), qs.end()); + // int l = 0, r = qs.size() - 1; + int l = 0, r = maxQ; + while (l < r) + { + int m = l + (r - l + 1) / 2; // ?? (l + r) / 2 + // if (ok(qs[m])) + if (ok(m)) + l = m; + else + r = m - 1; + } + // cout << qs[l] << "\n"; + cout << l << "\n"; + } +} diff --git a/uva_cpp_clean/1213/1213-12.cpp b/uva_cpp_clean/1213/1213-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a93595a5c769fee60a6e3bc3a4c2342282edbb52 --- /dev/null +++ b/uva_cpp_clean/1213/1213-12.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) +#define PI(a,b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long +#define S second +#define F first +#define endl '\n' +#define FOR(var,len) for(var = 0;var prime ; +bool is_prime[1121]; + +int dp[1121][15]; + + +void sieve(){ + int i,j; + memset(is_prime,true,sizeof(is_prime)); + for(i = 2;i*i<=1120;++i) + if(is_prime[i]){ + for(j = i*i;j<=1120;j+=i) + is_prime[j] = false; + } + // prime.EB(0); + for(i = 2;i<=1120;++i) + if(is_prime[i])prime.EB(i); +} + + +void solve () { + sieve(); + // cout<=prime[id];n--) + for(k = 1;k<=14;k++) + dp[n][k]+=dp[n-prime[id]][k-1]; + while(cin>>N>>K && N){ + cout<>tt; + // for( t = 0 ;t>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1213 + Name: Sum of Different Primes + Problem: https://onlinejudge.org/external/12/1213.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int primes[200], pcn; +bool isnp[1143]; +void sieve() { + isnp[0] = isnp[1] = 1; + for (int i=2; i<=1143; ++i) + if (!isnp[i]) { + primes[pcn++] = i; + for (int j=i+i; j<=1143; j+=i) + isnp[j] = 1; + } +} + +int DP[16][200][1143]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + sieve(); + + for (int pi=0; pi=0; --pi) + for (int x=primes[pi]; x<1143; ++x) + DP[k][pi][x] = DP[k-1][pi+1][x-primes[pi]] + DP[k][pi+1][x]; + + int n, k; + while (cin >> n >> k && (n||k)) + cout << DP[k][0][n] << endl; +} diff --git a/uva_cpp_clean/1213/1213-9.cpp b/uva_cpp_clean/1213/1213-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f470e0567324efd23f6f281e5be5ba6c86f696c --- /dev/null +++ b/uva_cpp_clean/1213/1213-9.cpp @@ -0,0 +1,39 @@ +#include + +#define MAX_N 250 +#define MAX_SUM 1500 +#define MAX_K 20 + +using namespace std; + +int sum,k,primes[MAX_N],n; +int dp[MAX_SUM][MAX_N][MAX_K]; + +bool p[MAX_SUM]; + +void sieve(){ + primes[n++] = 2; + for(int i=3;i=n || ct==0) return 0; + if(dp[s][it][ct]!=-1) return dp[s][it][ct]; + return dp[s][it][ct] = numberOfWays(s-primes[it],it+1,ct-1)+numberOfWays(s,it+1,ct); +} + +int main(){ + sieve(); + memset(dp,-1,sizeof dp); + for(int it=0;it +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc; + scanf("%d", &tc); + getchar(); + for (int t = 1; t <= tc; t++) { + string ws, we, ms, me; + cin >> ws >> we; + cin >> ms >> me; + //~ int Wsh,Wsm Weh,Wem, Msh, Msm, Meh, Mem; + int flag = 0; + int Wsh = (ws[0]-'0')*10+(ws[1]-'0')*1, Wsm = (ws[3]-'0')*10+(ws[4]-'0')*1; + int Weh = (we[0]-'0')*10+(we[1]-'0')*1, Wem = (we[3]-'0')*10+(we[4]-'0')*1; + + int Msh = (ms[0]- '0')*10+(ms[1]-'0')*1, Msm = (ms[3]- '0')*10+(ms[4]- '0')*1; + int Meh = (me[0]- '0')*10+(me[1]- '0')*1, Mem = (me[3]- '0')*10+(me[4]- '0')*1; + + double Ws = Wsh + (Wsm/100.0); + double We = Weh + (Wem/100.0); + double Ms = Msh + (Msm/100.0); + double Me = Meh + (Mem/100.0); + //~ printf("%lf %lf %lf %lf\n",Ws, We, Ms, Me); + + if(Ms>=Ws && Ms<=We){ + flag = 1; + } + else if(Me>=Ws && Me <= We){ + flag = 1; + } + else if(MsWe){ + flag = 1; + } + + if(!flag) printf("Case %d: Hits Meeting\n", t); + else printf("Case %d: Mrs Meeting\n", t); + } + return 0; +} + diff --git a/uva_cpp_clean/12136/12136-5.cpp b/uva_cpp_clean/12136/12136-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..83890b2ff8f64671e5541ad75ae8b6c7e914ece0 --- /dev/null +++ b/uva_cpp_clean/12136/12136-5.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pi 2*acos(0.0) +using namespace std; + +int main() +{ + int test,t=0,h1,h2,h3,h4,m1,m2,m3,m4,no=0; + char c; + scanf("%d",&test); + while(test--) + { + int a=0,b=0,c=0,d=0; + scanf("%d%:%d%d:%d",&h1,&m1,&h2,&m2); + scanf("%d:%d%d:%d",&h3,&m3,&h4,&m4); + a=(h1*60)+m1; + b=(h2*60)+m2; + c=(h3*60)+m3; + d=(h4*60)+m4; + if(h1a&&d>a) + { + if(c>b) + printf("Case %d: Hits Meeting\n",++no); + else + printf("Case %d: Mrs Meeting\n",++no); + } + else + printf("Case %d: Mrs Meeting\n",++no); + + } + return 0; +} diff --git a/uva_cpp_clean/12144/12144-21.cpp b/uva_cpp_clean/12144/12144-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e36e62b67d6c11cc4b2fa7a88f37aa0256a3681 --- /dev/null +++ b/uva_cpp_clean/12144/12144-21.cpp @@ -0,0 +1,172 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12144 + Name: Almost Shortest Path + Problem: https://onlinejudge.org/external/121/12144.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------------ + +template +class IndexQueue { + int mind, maxd; + vector V[600000]; + void _update() { + while (mind<=maxd && V[mind].empty()) ++mind; + if (mind>maxd) clear(); + } + + public: + IndexQueue() { mind=INF; maxd=-1; } + bool empty() { _update(); return mind>maxd; } + void clear() { + for (int i=mind; i<=maxd; ++i) + V[i].clear(); + mind=INF; + maxd=-1; + } + + void push(int key, T data) { + V[key].push_back(data); + if (key < mind) mind = key; + if (key > maxd) maxd = key; + } + + int top() { return mind; } + T pop() { + _update(); + T r = V[mind].back(); + V[mind].pop_back(); + return r; + } +}; + +// ------------------------------------------------------------------------ + +int cse=1, seen[543]; +int W[543][543], M[543][543], + D[543]; +vector adj[543], pre[543]; + +void InfD(int n) { for (int i=0; i q; +bool dijkstra1(int n, int src, int dst) { + InfD(n); + q.clear(); + q.push(D[src]=0, src); + while (!q.empty()) { + int d = q.top(), u = q.pop(); + if (d != D[u]) continue; + if (u == dst) { + seen[dst] = cse; + mark(dst); + return 1; + } + + for (int v: adj[u]) { + int dd = d + W[u][v]; + if (dd < D[v]) { + q.push(D[v]=dd, v); + pre[v].clear(); + pre[v].push_back(u); + } + else if (dd == D[v]) + pre[v].push_back(u); + } + } + return 0; +} + +int dijkstra2(int n, int src, int dst) { + InfD(n); + q.clear(); + q.push(D[src]=0, src); + while (!q.empty()) { + int d = q.top(), u = q.pop(); + if (d != D[u]) continue; + if (u == dst) return d; + + for (int v: adj[u]) if (M[u][v]!=cse) { + int dd = d + W[u][v]; + if (dd < D[v]) + q.push(D[v]=dd, v); + } + } + return -1; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int n,m; (n=readUInt()) && (m=readUInt()); ++cse) { + int src = readUInt(), + dst = readUInt(); + + for (int i=0; i shortest path > single-source > weighted graph + difficulty: medium + date: 08/Sep/2020 + problem: almost shortest path + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; +const int INF = 1 << 30; + +vector>> adj_list; // adj_list[u] = {{v, w}, ...} +int V; + +vector> p; + +/* O((V+E)*log(V)) */ +ll dijkstra(int s, int t, bool first_run) { + vector sd(V, INF); + p.assign(V, {-1,-1}); + + priority_queue> pq; // { distância negativa de s até v, v } + pq.push({-0, s}); + sd[s] = 0; + + vector> pt; + + while (!pq.empty()) { + ll dist = -pq.top().first; int u = pq.top().second; pq.pop(); + if (dist > sd[u]) continue; + + if (u == t && !first_run) return dist; + + // para cada aresta u --w--> v + for (int i = 0; i < adj_list[u].size(); i++) { + int v; ll w; tie(v,w) = adj_list[u][i]; + + ll &od = sd[v]; // old distance de v: s -> ... -> v + ll nd = sd[u] + w; // new distance de v: s -> ... -> u -> v + + // tenta relaxar sd[v] + if (nd < od) { + od = nd; + pq.push({-nd, v}); + p[v] = {u,i}; + + if (v == t) pt = {p[v]}; + } + else if (v == t && nd == od) + pt.push_back({u,i}); + } + } + + // invalida os melhores caminhos + if (first_run) { + for (auto parent : pt) { + int v, i; tie(v, i) = parent; + + adj_list[v][i].second = INF; + + for (int u; p[v].first != -1; v = u) { + tie(u, i) = p[v]; + adj_list[u][i].second = INF; + } + } + } + + return INF; +} + +int main() { + int E; + while (cin >> V >> E && (V || E)) { + adj_list.assign(V, vector>()); + int s, t; cin >> s >> t; + while (E--) { + int u, v; ll w; cin >> u >> v >> w; + adj_list[u].push_back({v,w}); + } + + dijkstra(s, t, true); + ll ans = dijkstra(s, t, false); + ans = ans != INF? ans: -1; + cout << ans << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12148/12148-18.cpp b/uva_cpp_clean/12148/12148-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fc862fa69f99cadce77e1d9640f7cd6740467660 --- /dev/null +++ b/uva_cpp_clean/12148/12148-18.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +void day(int &dd, int &mm, int &yy){ + int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int leap = (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0); + if(leap) month[2]++; + dd++; + if(dd > month[mm]) mm++, dd = 1; + if(mm == 13) yy++, mm = dd = 1; +} + +int main(){ + int n, days, sum; + while(scanf("%d", &n) && n){ + int mm[1500], yy[1500], dd[1500], cc[1500]; + days = sum = 0; + for(int i = 0; i < n; i++) scanf("%d %d %d %d", &dd[i], &mm[i], &yy[i], &cc[i]); + for(int i = 0; i < n - 1; i++){ + day(dd[i], mm[i], yy[i]); + if(dd[i] == dd[i + 1] && mm[i] == mm[i + 1] && yy[i] == yy[i + 1]) days++, sum+=(cc[i + 1] - cc[i]); + } + printf("%d %d\n", days, sum); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12149/12149-11.cpp b/uva_cpp_clean/12149/12149-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3372541210028ce43f831763f23a44ddc0a275f --- /dev/null +++ b/uva_cpp_clean/12149/12149-11.cpp @@ -0,0 +1,50 @@ +#include +/* +Problem: 12149 - Feynman +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3301 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N; + +int main () { + + while ( scanf("%d\n", &N) && N ){ + + printf("%d\n", (N*(N+1)*(2*N + 1))/6 ); + + } + + return 0; +} +/* +Sample input:- +----------------- +2 +1 +8 +0 + +Sample output:- +----------------- +5 +1 +204 + +Resources:- +------------- + +Explanation: + +--------------- +*/ diff --git a/uva_cpp_clean/12149/12149-19.cpp b/uva_cpp_clean/12149/12149-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50dce028446d0855d65049defa21d01cfd192dbc --- /dev/null +++ b/uva_cpp_clean/12149/12149-19.cpp @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() { + int squares[101]; + squares[0] = 0; + for (int i = 1; i < 101; i++) squares[i] = i*i + squares[i-1]; + int N; + while (scanf("%d", &N) && N != 0) printf("%d\n", squares[N]); + return 0; +} diff --git a/uva_cpp_clean/12149/12149-21.cpp b/uva_cpp_clean/12149/12149-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a21234a307d1b6f84d5d0b42b21586a4756c1f0 --- /dev/null +++ b/uva_cpp_clean/12149/12149-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12149 + Name: Feynman + Problem: https://onlinejudge.org/external/121/12149.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n; + while(cin>>n && n) + cout << (n*(n+1)*(2*n+1))/6 << endl; +} diff --git a/uva_cpp_clean/12149/12149-5.cpp b/uva_cpp_clean/12149/12149-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0ab7d962ec98541d6e4bf1a805f75ebc1039fd5d --- /dev/null +++ b/uva_cpp_clean/12149/12149-5.cpp @@ -0,0 +1,21 @@ +#include +int main() +{ + long n,i,m,s; + while(scanf("%ld",&n)==1) + { + if(n==0) + break; + i=1; + s=0; + m=0; + while(i<=n) + { + m=i*i; + s+=m; + i++; + } + printf("%ld\n",s); + } + return 0; +} diff --git a/uva_cpp_clean/12149/12149-9.cpp b/uva_cpp_clean/12149/12149-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..808be897970006017f1a7c9c6418375f647258c4 --- /dev/null +++ b/uva_cpp_clean/12149/12149-9.cpp @@ -0,0 +1,9 @@ +#include + +int main(){ + int n; + while(scanf("%d", &n), n){ + printf("%d\n", n * (n + 1) * (2 * n + 1) / 6); + } + return(0); +} diff --git a/uva_cpp_clean/12150/12150-21.cpp b/uva_cpp_clean/12150/12150-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b17d6d368f4f73fe76800043b99c02f7bf0416ac --- /dev/null +++ b/uva_cpp_clean/12150/12150-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12150 + Name: Pole Position + Problem: https://onlinejudge.org/external/121/12150.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, id, y, X[2000]; + while (cin>>n && n) { + memset(X, 0, sizeof(int)*n); + bool def = 0; + for (int i=0; i> id >> y; + if (i+y>=0 && i+y //printf scanf +#define rep(i,b,c) for ( int i = b; i < c; i++ ) +using namespace std; + + +int main () +{ + // Integer N: Number of cars taking place + // Integer C: Car Number + // Integer P: Car Position ( when compared to starting position + // Integer position: secondary variable to help us easily deal with the problem + int myArray[1000]; + int N,C,P,position; + bool possible; + + while ( scanf("%d", &N) != EOF && N) + { + rep(l,0,N) + myArray[l] = -11; + possible = true; + + rep(l,0,N) + { + scanf("%d %d",&C,&P); + position = l + P; + if ( position < 0 || position >= N || myArray[position] != -11 ) + { + possible = false; + break; + } + myArray[position] = C; + } + + if ( possible ) + { + rep(l,0,N) + { + if ( l > 0 ) printf(" "); + printf("%d", myArray[l]); + } + puts(""); + } + else + puts ("-1"); + } +} + + + + diff --git a/uva_cpp_clean/12155/12155-21.cpp b/uva_cpp_clean/12155/12155-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6543b62ffb94002794ffa16fe422f316b029e37e --- /dev/null +++ b/uva_cpp_clean/12155/12155-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12155 + Name: ASCII Diamondi + Problem: https://onlinejudge.org/external/121/12155.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +inline int abs(int x){ return x<0 ? -x : x; } +int main() { + int n, r1, c1, r2, c2; + for (int cse=1; scanf("%d%d%d%d%d", &n, &r1, &c1, &r2, &c2)==5 && n>0; cse++) { + int sz = 2*n - 1, + h = r2 - r1 + 1, + w = c2 - c1 + 1; + + printf("Case %d:\n", cse); + for (int i=r1; i<=r2; i++) { + int d1 = abs(i - (sz*(i/sz)+n-1)); + for (int j=c1; j<=c2; j++) { + int d = d1 + abs(j - (sz*(j/sz)+n-1)); + putchar(d +using namespace std; + +int main() { + int T, N; + cin >> T; + for (int c = 1; c <= T; c++) { + cin >> N; + int mile = 0, juice = 0; + for (int i = 0; i < N; i++) { + int call; + cin >> call; + mile += (call/30 + 1)*10; + juice += (call/60 + 1)*15; + } + printf("Case %d: ", c); + int mini = min(mile, juice); + if (mile == mini) printf("Mile "); + if (juice == mini) printf("Juice "); + printf("%d\n", mini); + } + return 0; +} diff --git a/uva_cpp_clean/12157/12157-2.cpp b/uva_cpp_clean/12157/12157-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eda627b26d9d38af6d501f37460d4aa3879c9612 --- /dev/null +++ b/uva_cpp_clean/12157/12157-2.cpp @@ -0,0 +1,51 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int t; cin >> t; + for(int cs = 1 ; cs <= t ; cs ++ ){ + int n ; scanf("%d",&n); + int mile = 0 , juice = 0 ; + while(n-- ){ + int x ; scanf("%d",&x); + mile += ( ( x / 30 ) + 1 ) * 10 ; + juice += ( ( x / 60 ) + 1 ) * 15 ; + } + printf("Case %d: ",cs); + if(mile < juice )printf("Mile %d\n",mile); + else if( mile > juice )printf("Juice %d\n",juice); + else printf("Mile Juice %d\n",mile); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12157/12157-21.cpp b/uva_cpp_clean/12157/12157-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22652a4280dcbfb87b1f44671789b4dee9752a4e --- /dev/null +++ b/uva_cpp_clean/12157/12157-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12157 + Name: Tariff Plan + Problem: https://onlinejudge.org/external/121/12157.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, n, x; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin>>n; + + int m=0, j=0; + for (int i=0; i>x; + m += ((x/30) + 1) * 10; + j += ((x/60) + 1) * 15; + } + + printf("Case %d: ", cse); + if (m<=j) cout << "Mile "; + if (j<=m) cout << "Juice "; + cout << (m +int main() +{ + int t, n,m,j,a,i; + scanf("%d",&t); + for(i=1;i<=t;i++) + { + scanf("%d",&n); + m=j=0; + while(n--) + { + scanf("%d",&a); + m+=(a+30)/30; + j+=(a+60)/60; + } + m*=10; + j*=15; + if(m +#define min(a,b) ( a <= b ? a : b ); + +int main(){ + int N, n, a, sum1, sum2, i, sum, t = 1; + scanf("%d", &N); + while(N--){ + sum1 = sum2 = sum = 0; + scanf("%d",&n); + for(i = 0; i < n; i++){ + scanf("%d", &a); + sum1 += (a / 30) * 10 + 10; + sum2 += (a / 60) * 15 + 15; + } + sum = min(sum1, sum2); + if(sum == sum1 && sum == sum2) printf("Case %d: Mike Juice %d\n", t, sum); + else if(sum == 1 && sum != sum2) printf("Case %d: Mile %d\n", t, sum); + else printf("Case %d: Juice %d\n", t, sum); + t++; + } + return(0); +} diff --git a/uva_cpp_clean/12157/main.cpp b/uva_cpp_clean/12157/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7ef317e8b774af2ed7385fdde0175916a4f54b5 --- /dev/null +++ b/uva_cpp_clean/12157/main.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main(){ + short cases, calls, duration, J, M; + cin >> cases; + for(short i = 1; i <= cases; i++){ + J = 0; + M = 0; + cin >> calls; + for(short c = 0; c < calls; c++){ + cin >> duration; + M += (duration/30) * 10 + 10; + J += (duration/60) * 15 + 15; + } + if(M < J) cout << "Case " << i << ": Mile " << M << endl; + else if(M > J) cout << "Case " << i << ": Juice " << J << endl; + else cout << "Case " << i << ": Mile Juice " << M << endl; + } + + +return 0; +} diff --git a/uva_cpp_clean/1216/1216-10.cpp b/uva_cpp_clean/1216/1216-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ef271d2e8030141806bc54c164c9580ec6f6f9d --- /dev/null +++ b/uva_cpp_clean/1216/1216-10.cpp @@ -0,0 +1,128 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)2e6+9; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; + +ll r; + +ll minimumSpanningTree(vector> &edges) { + + + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + + ll weight; + vector ans; + int a,b; + for (auto e: edges) { + + tie(weight,a,b) =e; + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + ans.push_back(weight); + ///cout<>t; + + ///ll ma=-1; + while (t--) { + cin >> r; + vector> arr; + + while (cin >> a) { + if (a == -1) break; + + cin >> b; + + arr.push_back({a, b}); + ///ma=max(ma,max(a,b)); + } + + + ll n = arr.size(); + + vector> edges; + for (ll i = 0; i < n; i++) { + for (ll j = i + 1; j < n; j++) { + ll l1 = (arr[i].first - arr[j].first); + ll l2 = (arr[i].second - arr[j].second); + + ll len = sqrt((l1 * l1) + (l2 * l2)); + edges.push_back({len, i, j}); + } + } + + auto res = minimumSpanningTree(edges); + + cout << res << "\n"; + } +} + diff --git a/uva_cpp_clean/1216/1216-21.cpp b/uva_cpp_clean/1216/1216-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee0053b2f687e0063fbcb0074df07f05a30323a5 --- /dev/null +++ b/uva_cpp_clean/1216/1216-21.cpp @@ -0,0 +1,109 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1216 + Name: The Bug Sensor Problem + Problem: https://onlinejudge.org/external/12/1216.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int par[1000000]; +int find(int u) { return par[u]<0 ? u : par[u]=find(par[u]); } +bool join(int u, int v) { + if ((u=find(u)) != (v=find(v))) { + if (par[v] < par[u]) + swap(u, v); + + par[u] += par[v]; + par[v] = u; + return 1; + } + return 0; +} + +// ----------------------------------------------------- + +#define INF 9223372036854775807 +typedef long long Int; + +int np; +struct Point { + int x, y; + bool operator < (const Point &p) const { return x!=p.x ? x E; + +Int cp(int s, int e) { + int sz = e-s; + if (sz <= 1) return INF; + if (sz == 2) { + Edge e(s, s+1); + E.push_back(e); + return e.d; + } + + int mid = (s+e)>>1; + Int del = min(cp(s, mid), cp(mid, e)), + x1 = P[mid-1].x, + x2 = P[mid].x; + + //TODO: understand the relation between delta and needed distanct points + // 42 is the smallest number giving AC + for (int i=mid-1; i>=s && x1-P[i].x <= 43*del; --i) + for (int j=mid; j> T; + while (T--) { + int k; cin>>k; + E.clear(); + + for (np=0; cin>>P[np].x && P[np].x!=-1; ++np) + cin >> P[np].y; + + sort(P, P+np); + cp(0, np); + + Int md; + sort(E.begin(), E.end()); + memset(par, -1, np*sizeof(int)); + for (int i=0, cnt=k; cnt + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 500010; + +struct Edge_{ + int u, v; + long long w; + Edge_() {}; + Edge_(int u_, int v_, long long w_): + u(u_), v(v_), w(w_) {}; + bool operator < (const Edge_& other){ + return ( w < other.w ); + } +}E[SIZE]; + +struct Point_{ + int x, y; + Point_() {}; + Point_(int x_, int y_): x(x_), y(y_) {}; +}P[SIZE]; + +int tc, N, M, CC, a, b; + +inline long long pow2(long long P_){ + return P_ * P_; +} + +inline long long dis(int i, int j){ + return pow2(P[i].x - P[j].x) + pow2(P[i].y - P[j].y); +} + +int Kruskal(){ + UnionFind UF(N); + double ans = 0; + int setSize = N; + for(int i = 0; i < N; i++) + for(int j = i + 1; j < N; j++) + E[M++] = Edge_(i, j, dis(i, j)); + sort(E, E + M); + for(int it = 0; it < M and setSize != CC; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ) + ans = sqrt(E[it].w), setSize--, + UF.unionSet(E[it].u, E[it].v); + return ceil(ans); +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + N = M = 0; + scanf("%d", &CC); + while(scanf("%d", &a), ~a){ + scanf("%d", &b); + P[N++] = Point_(a, b); + } + printf("%d\n", Kruskal()); + } + return(0); +} diff --git a/uva_cpp_clean/12160/12160-13.cpp b/uva_cpp_clean/12160/12160-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed4da8b1587e6ab5f27f7d719f1269794dcfb1df --- /dev/null +++ b/uva_cpp_clean/12160/12160-13.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 10000 + +int l, u, r, t = 1; + +vector dis, buttons; +vector > graph; + +int bfs() +{ + dis[l] = 0; + + queue pq; + + pq.push(l); + + while (!pq.empty()) + { + int head = pq.front(); + + pq.pop(); + + int childVer; + + for (int i = 0; i < r; i++) + { + childVer = graph[head][i]; + + if (1 + dis[head] < dis[childVer]) + { + dis[childVer] = 1 + dis[head]; + + pq.push(childVer); + } + } + } + + return dis[u]; +} + +void initialize(int r) +{ + graph.clear(); + graph.resize(MAX); + + dis.clear(); + dis.resize(MAX, INT_MAX); + + buttons.clear(); + buttons.resize(r); +} + +void makeGraph() +{ + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < r; j++) + { + int child = (i + buttons[j]) % MAX; + + graph[i].push_back(child); + } + } +} + +int main() +{ + while (cin >> l >> u >> r, l != 0 || u != 0 || r != 0) + { + initialize(r); + + for (int i = 0; i < r; i++) + { + cin >> buttons[i]; + } + + makeGraph(); + + int ans = bfs(); + + cout << "Case " << t++ << ": "; + + if (ans != INT_MAX) cout << ans << endl; + + else cout << "Permanently Locked" << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/12160/12160-21.cpp b/uva_cpp_clean/12160/12160-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..228991f721fa2a13bb61ed2a5a4d80a840fb81e3 --- /dev/null +++ b/uva_cpp_clean/12160/12160-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12160 + Name: Unlock the Lock + Problem: https://onlinejudge.org/external/121/12160.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + + vector X; + int src, dst, n; + for (int cse=1; cin >> src >> dst >> n && (src||dst||n); ++cse) { + cout << "Case " << cse << ": "; + + X.resize(n); + for (int i=0; i> X[i]; + + if (src == dst) + cout << 0 << endl; + else { + int d[10000]={}; + queue q; + q.push(src); + d[src]=1; + while (!q.empty()) { + int c = q.front(); q.pop(); + for (int x: X) { + int nc = (c + x)%10000; + if (!d[nc]) { + if (nc == dst) { + cout << d[c] << endl; + goto fin; + } + d[nc] = d[c] + 1; + q.push(nc); + } + } + } + cout << "Permanently Locked\n"; + } + fin:; + } +} diff --git a/uva_cpp_clean/12166/12166-17.cpp b/uva_cpp_clean/12166/12166-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa71cd60e1c380b2a599d847b7cf78c81a1ca1a6 --- /dev/null +++ b/uva_cpp_clean/12166/12166-17.cpp @@ -0,0 +1,56 @@ +/** + * AOAPC II Exercise 6-6 Equilibrium Mobile + * ICPC NWERC 2008 + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int T; + cin >> T; + while (T--) + { + string line; + cin >> line; + int dep = 0, cnt = 0; + long long cur = 0; + map tot_weights; + for (char c : line) + switch (c) + { + case '[': + ++dep; + break; + case ']': + if (cur) + { + ++tot_weights[cur << dep]; + ++cnt; + cur = 0; + } + --dep; + break; + case ',': + if (cur) + { + ++tot_weights[cur << dep]; + ++cnt; + cur = 0; + } + break; + default: + cur = cur * 10 + c - '0'; + } + if (cur) + cout << "0\n"; + else + { + int ans = -1; + for (auto &w : tot_weights) + ans = max(ans, w.second); + cout << cnt - ans << "\n"; + } + } +} diff --git a/uva_cpp_clean/12166/12166-19.cpp b/uva_cpp_clean/12166/12166-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e2fadbc8dc924ea9dc283cf03a15fffca2d01e8 --- /dev/null +++ b/uva_cpp_clean/12166/12166-19.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + int N; + unordered_map m; + cin >> N; + string s; + getline(cin, s); + while (N--) { + m.clear(); + getline(cin, s); + istringstream iss(s); + char c; + long long val; + int open = 0, max = -1, id = -1, nodes = 0; + while (iss) { + c = iss.peek(); + if (isdigit(c)) { + nodes++; + iss >> val; + m[val*(1< max) max = m[val*(1<> c; + } + printf("%d\n", nodes-max); + } + return 0; +} diff --git a/uva_cpp_clean/12168/12168-21.cpp b/uva_cpp_clean/12168/12168-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d9f4562a227304a72494a7f899e2ba8c884580a --- /dev/null +++ b/uva_cpp_clean/12168/12168-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12168 + Name: Cat vs. Dog + Problem: https://onlinejudge.org/external/121/12168.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +vector adj[555]; +int match[555]; +bool seen[555]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + if (match[v]==-1 || bpm(match[v])) { + match[v] = u; + return true; + } + } + return false; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string X[555], Y[555]; + int T, c, d, n; + cin >> T; + while (T-- && cin >> c >> d >> n) { + vector L, R; + for (int i=0; i> X[i] >> Y[i]; + if (X[i][0] == 'C') + L.push_back(i); + else + R.push_back(i); + } + + for (int i=0; i +using namespace std; +struct Cell +{ + int x, y, z; + Cell(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) { } +}; +void elim(vector &vec) +{ + vec.push_back(0); + vec.push_back(1100); + sort(vec.begin(), vec.end()); + vec.erase(unique(vec.begin(), vec.end()), vec.end()); +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); // 4 0 + int T; // |/ + cin >> T; // 3 -- --> 1 + const array dx { 0, 1, 0, -1, 0, 0 }, // /| + dy { 0, 0, 0, 0, -1, 1 }, // L v + dz { -1, 0, 1, 0, 0, 0 }; // 2 5 + while (T--) + { + int n; + cin >> n; + vector xs, ys, zs, x0s(n), x1s(n), y0s(n), y1s(n), z0s(n), z1s(n); + for (int i = 0; i < n; ++i) + { + cin >> x0s[i] >> y0s[i] >> z0s[i] >> x1s[i] >> y1s[i] >> z1s[i]; + x1s[i] += x0s[i]; + y1s[i] += y0s[i]; + z1s[i] += z0s[i]; + xs.push_back(x0s[i]); + xs.push_back(x1s[i]); + ys.push_back(y0s[i]); + ys.push_back(y1s[i]); + zs.push_back(z0s[i]); + zs.push_back(z1s[i]); + } + elim(xs); + elim(ys); + elim(zs); + int nx = xs.size(), ny = ys.size(), nz = zs.size(); + auto boxes = vector>>(nx, vector>(ny, vector(nz, false))); + auto vis = vector>>(nx, vector>(ny, vector(nz, false))); + for (int i = 0; i < n; ++i) + { + int x0_pos = lower_bound(xs.begin(), xs.end(), x0s[i]) - xs.begin(), + x1_pos = lower_bound(xs.begin(), xs.end(), x1s[i]) - xs.begin(), + y0_pos = lower_bound(ys.begin(), ys.end(), y0s[i]) - ys.begin(), + y1_pos = lower_bound(ys.begin(), ys.end(), y1s[i]) - ys.begin(), + z0_pos = lower_bound(zs.begin(), zs.end(), z0s[i]) - zs.begin(), + z1_pos = lower_bound(zs.begin(), zs.end(), z1s[i]) - zs.begin(); + for (int x = x0_pos; x < x1_pos; ++x) + for (int y = y0_pos; y < y1_pos; ++y) + for (int z = z0_pos; z < z1_pos; ++z) + boxes[x][y][z] = true; + } + int volume = 1100 * 1100 * 1100, area = 0; + queue q; + q.push(Cell()); + vis[0][0][0] = true; + while (!q.empty()) + { + Cell cur = q.front(); + q.pop(); + int lx = (xs[cur.x + 1] - xs[cur.x]), ly = (ys[cur.y + 1] - ys[cur.y]), lz = (zs[cur.z + 1] - zs[cur.z]); + volume -= lx * ly * lz; + for (int i = 0; i < 6; ++i) + { + Cell next(cur.x + dx[i], cur.y + dy[i], cur.z + dz[i]); + if (0 <= next.x && next.x < nx - 1 && 0 <= next.y && next.y < ny - 1 && 0 <= next.z && next.z < nz - 1) + if (boxes[next.x][next.y][next.z]) + if (dx[i]) + area += ly * lz; + else if (dy[i]) + area += lx * lz; + else + area += lx * ly; + else if (!vis[next.x][next.y][next.z]) + { + q.push(next); + vis[next.x][next.y][next.z] = true; + } + } + } + cout << area << " " << volume << "\n"; + } +} diff --git a/uva_cpp_clean/12176/12176-14.cpp b/uva_cpp_clean/12176/12176-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3ffe8e44ddfe95c28dc622a79c4f7394d2a8c56 --- /dev/null +++ b/uva_cpp_clean/12176/12176-14.cpp @@ -0,0 +1,246 @@ +/*************************************************** + * Problem Name : 12176 Bring Your Own Horse.cpp + * Problem Link : https://uva.onlinejudge.org/external/121/12176.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-09-14 + * Problem Type : Graph (MST) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 3005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +vectorcost[MAX]; +int par[MAX], level[MAX]; +int mx_rd[MAX][15], table[MAX][15]; +struct Edge { + int u, v, w; +}; +bool cmp (Edge a, Edge b) { + return a.w < b.w; +} +vectorE; + +int find (int r) { + if (par[r] == r) return r; + else return par[r] = find (par[r]); +} +void MST (int n) { + sort (all (E), cmp); + + for (int i = 1; i <= n; i++) { + par[i] = i; + } + + int cnt = 0; + + for (int i = 0; i < (int) E.size(); i++) { + int u = find (E[i].u); + int v = find (E[i].v); + + if (u != v) { + par[u] = v; + cnt++; + adj[E[i].u].pb (E[i].v); + adj[E[i].v].pb (E[i].u); + cost[E[i].u].pb (E[i].w); + cost[E[i].v].pb (E[i].w); + + if (cnt == n) break; + } + } +} +//~ bool vis[MAX]; +//~ int bfs (int s, int d) { +//~ SET (vis, false); +//~ queueQ; +//~ vis[s] = true; +//~ mx[s] = 0; +//~ Q.push (s); + +//~ while (!Q.empty() ) { +//~ int u = Q.front(); +//~ Q.pop(); + +//~ for (int i = 0; i < (int) adj[u].size(); i++) { +//~ int v = adj[u][i]; + +//~ if (!vis[v]) { +//~ vis[v] = true; +//~ mx[v] = max (mx[u], cost[u][i]); + +//~ if (v == d) return mx[d]; + +//~ Q.push (v); +//~ } +//~ } +//~ } + +//~ return mx[d]; +//~ } + +void dfs (int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + level[v] = level[u] + 1; + par[v] = u; + mx_rd[v][0] = cost[u][i]; + dfs (v, u); + } +} +void LCA (int n) { + SET (table, -1); + + for (int i = 1; i <= n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 1; i <= n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + mx_rd[i][j] = max (mx_rd[i][j - 1], mx_rd[x][j - 1]); + + } else { + mx_rd[i][j] = mx_rd[i][j - 1]; + } + } + } +} + +int LCA_Quary (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} +int getMAX (int u, int uu) { + int mx = 0; + for (int i = log2 (uu); i >= 0; i--) { + if ( (1 << i) <= uu) { + mx = max (mx, mx_rd[u][i]); + u = table[u][i]; + uu -= (1 << i); + } + + if (uu == 0) break; + } + return mx; +} +int main () { + __FastIO; + int tc, N, M; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> N >> M; + + for (int i = 1; i <= M; i++) { + Edge get; + cin >> get.u >> get.v >> get.w; + E.pb (get); + } + + MST (N); + par[1] = -1; + level[1] = 0; + mx_rd[1][0] = 0; + dfs (1, -1); + LCA (N); + int Q; + cin >> Q; + cout << "Case " << t << "\n"; + + for (int q = 1; q <= Q; q++) { + int u, v; + cin >> u >> v; + int x = LCA_Quary (u, v); + int uu = level[u] - level[x]; + int vv = level[v] - level[x]; + cout << max(getMAX(u, uu), getMAX(v, vv))<<"\n"; + //~ mx[u] = 0; + //~ dfs (u, -1, v); + //~ cout << bfs (u, v) << "\n"; + } + + nl; + E.clear(); + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + cost[i].clear(); + } + } + + return 0; +} diff --git a/uva_cpp_clean/12182/12182-21.cpp b/uva_cpp_clean/12182/12182-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dfcb96274c40cb7c3ef1a9e78593436cc2ca1cd --- /dev/null +++ b/uva_cpp_clean/12182/12182-21.cpp @@ -0,0 +1,66 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12182 + Name: Toll Road + Problem: https://onlinejudge.org/external/121/12182.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + + +struct Road { + int id, v; + Road(int id, int v):id(id),v(v) {} +}; + +list adj[200001]; +int cse, cidv[200001], prot[200001]; +void checkId(int u) { + if (cidv[u] != cse) { + cidv[u] = cse; + adj[u].clear(); + } +} + +int best; +bool inside[200001]; +int dfs(int u) { + inside[u]=1; + + int sum = 0; + for (Road r: adj[u]) if (!inside[r.v]) { + int pr = prot[r.id] + dfs(r.v); + if (pr > 0) sum += pr; + } + if (sum > best) + best = sum; + + inside[u]=0; + return sum; +} + + +int main() { + int n, u, v; + while (scanf("%d", &n)==1 && n) { + cse++; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12186 + Name: Another Crisis + Problem: https://onlinejudge.org/external/121/12186.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------------- + +int n, p, D[100143]; +vector adj[100143]; +bool comp(int u, int v) { return D[u] < D[v]; } + +int dfs(int u) { + if (adj[u].empty()) return 1; + + for (int v: adj[u]) + D[v] = dfs(v); + + int mid = ceil(adj[u].size() * (p/100.0)); + nth_element(adj[u].begin(), adj[u].begin()+mid, adj[u].end(), comp); + + int sum = 0; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define rep(i, a, n) for (int i = a; i < n; ++i) +#define erep(i, a, n) for (int i = a; i <= n; ++i) +#define per(i, a, n) for (int i = n - 1; i >= a; --i) + +const int INF = 0x3f3f3f3f; +const double EPS = numeric_limits::epsilon(); +typedef long long ll; +// end of header + +#define MAX 100001 + +vector childs[MAX]; +int n; +int t; + +int minWorker(int u) { + int size = childs[u].size(); + if (size == 0) { + return 1; + } + + vector workers; // 如需要创建不确定大小的数组,应使用vector代替,提高效率,减少代码量 + rep(i, 0, size) { + workers.push_back(minWorker(childs[u][i])); + } + sort(workers.begin(), workers.end()); + int needed = ceil((double)size * t / 100); + int sum = 0; + rep(i, 0, needed) { + sum += workers[i]; + } + return sum; +} + +int main() { + while (~scanf("%d%d", &n, &t) && n && t) { + + rep(i, 0, MAX) { // 注意:必须全部清空,不能只清除到n! + childs[i].clear(); + } + + int parent; + erep(i, 1, n) { + scanf("%d", &parent); + childs[parent].push_back(i); + } + + printf("%d\n", minWorker(0)); + } +} diff --git a/uva_cpp_clean/12187/12187-19.cpp b/uva_cpp_clean/12187/12187-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d906f625466db5f49e9b76a8197b175f3f220c7 --- /dev/null +++ b/uva_cpp_clean/12187/12187-19.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +int main() { + int * kingdom1 = new int[10000], * kingdom2 = new int[10000]; + int N, R, C, K; + while (cin >> N >> R >> C >> K && !(N == 0 && R == 0 && C == 0 && K == 0)) { + for (int i = 0; i < R; i++) + for (int j = 0; j < C; j++) + cin >> kingdom1[i*100 + j]; + + for (int b = 0; b < K; b++) { + int * k1 = (b & 1 ? kingdom2: kingdom1); + int * k2 = (b & 1 ? kingdom1: kingdom2); + for (int i = 0; i < R; i++) + for (int j = 0; j < C; j++) { + int attacked = 0, auxi = (k1[i*100 + j] + N - 1) % N; + if (i > 0) attacked |= k1[(i-1)*100 + j] == auxi; + if (j > 0) attacked |= k1[i*100 + j-1] == auxi; + if (i < R - 1) attacked |= k1[(i+1)*100 + j] == auxi; + if (j < C - 1) attacked |= k1[i*100 + j+1] == auxi; + if (attacked) k2[i*100 + j] = auxi; + else k2[i*100 + j] = k1[i*100 + j]; + } + } + int * k = (K & 1 ? kingdom2: kingdom1); + for (int i = 0; i < R; i++) { + bool first = false; + for (int j = 0; j < C; j++) { + if (first) printf(" "); + else first = true; + printf("%d", k[i*100 + j]); + } + printf("\n"); + } + } + delete [] kingdom1; + delete [] kingdom2; + return 0; +} diff --git a/uva_cpp_clean/12187/12187-21.cpp b/uva_cpp_clean/12187/12187-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6802098e34724cdff27a65eee88701ad27aa8ec --- /dev/null +++ b/uva_cpp_clean/12187/12187-21.cpp @@ -0,0 +1,97 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12187 + Name: Brothers + Problem: https://onlinejudge.org/external/121/12187.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------ + +int di[] = {0,0,1,-1}, + dj[] = {1,-1,0,0}; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[128][128], Y[128][128], n, r, c, k; + memset(X, -1, sizeof(X)); + memset(Y, -1, sizeof(Y)); + while ( (n=readUInt())!=EOF ) { + r = readUInt(); + c = readUInt(); + k = readUInt(); + if (!(n||r||c||k)) break; + + memset(X[r+1], -1, 512); + memset(Y[r+1], -1, 512); + for (int i=1; i<=r; ++i) { + X[i][c+1] = Y[i][c+1] = -1; + for (int j=1; j<=c; ++j) + X[i][j] = readUInt(); + } + + bool some = 1; + while (k-- && some) { + some = 0; + for (int i=1; i<=r; ++i) + for (int j=1; j<=c; ++j) { + int en = (n+X[i][j]-1) % n; + Y[i][j] = X[i][j]; + for (int k=0; k<4; ++k) + if (X[i+di[k]][j+dj[k]] == en) { + Y[i][j] = en; + some = 1; + break; + } + } + swap(X, Y); + } + + for (int i=1; i<=r; ++i) { + cout << X[i][1]; + for (int j=2; j<=c; ++j) + cout << ' ' << X[i][j]; + cout << '\n'; + } + } +} diff --git a/uva_cpp_clean/12187/12187-9.cpp b/uva_cpp_clean/12187/12187-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b73f445aaf50f1fa39f7d951bfb8c1a645502cc --- /dev/null +++ b/uva_cpp_clean/12187/12187-9.cpp @@ -0,0 +1,45 @@ +#include + +#define SIZE 110 + +using namespace std; + +int n, r, c, k, v[SIZE][SIZE], u[SIZE][SIZE]; +bool visited[SIZE][SIZE]; +int dx[] = {-1, 0, 0, 1}; +int dy[] = {0, 1, -1, 0}; + +void simulate(){ + memset(visited, false, sizeof visited); + memset(u, -1, sizeof u); + for(int i = 1; i <= r; i++) + for(int j = 1; j <= c; j++) + for(int it = 0; it < 4; it++){ + int x = i + dx[it], y = j + dy[it]; + if(!visited[x][y] && v[x][y] == (v[i][j] + 1 + n) % n) + u[x][y] = v[i][j], visited[x][y] = true; + } + for(int i = 1; i <= r; i++) + for(int j = 1; j <= c; j++) + if(u[i][j] == -1) u[i][j] = v[i][j]; + for(int i = 1; i <= r; i++) + for(int j = 1; j <= c; j++) + v[i][j] = u[i][j]; +} + +void printMatrix(){ + for(int i = 1; i <= r; i++) + for(int j = 1; j <= c; j++) + printf( j != c ? "%d " : "%d\n", u[i][j]); +} + +int main(){ + while(scanf("%d %d %d %d", &n, &r, &c, &k),n | r | c | k){ + memset(v, -1, sizeof v); + for(int i = 1; i <= r; i++) + for(int j = 1; j <= c; j++) scanf("%d", &v[i][j]); + while(k--) simulate(); + printMatrix(); + } + return(0); +} diff --git a/uva_cpp_clean/12190/12190-21.cpp b/uva_cpp_clean/12190/12190-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..532b66dec350b1082adf2c6c6eafda5a33e09743 --- /dev/null +++ b/uva_cpp_clean/12190/12190-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12190 + Name: Electric Bill + Problem: https://onlinejudge.org/external/121/12190.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +long long MH[] = {0, 100, 10000, 1000000, 1LL<<62}, + HP[] = {0, 2, 3, 5, 7}; +long long price(long long cwh) { + long long result = 0; + for (int i=1; cwh>0; ++i) { + long long h = min(MH[i]-MH[i-1], cwh); + result += h * HP[i]; + cwh -= h; + } + return result; +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long a, b, t; + while (cin >> a >> b && (a||b)) { + long long lo = 0, hi = a+1; + while (lo <= hi) { + long long mid = (lo + hi) >> 1; + if (price(mid) <= a) + lo = mid + 1; + else + hi = mid - 1; + } + + + t = lo - 1; lo = 0; hi = t + 1; + while (lo <= hi) { + long long mid = (lo + hi) >> 1; + if (price(t-mid)-price(mid) >= b) + lo = mid + 1; + else + hi = mid - 1; + } + + cout << price(lo-1) << endl; + } +} diff --git a/uva_cpp_clean/12190/12190-9.cpp b/uva_cpp_clean/12190/12190-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e7f7b13b3d02826f56670d9cf293cb2ebdc38a6 --- /dev/null +++ b/uva_cpp_clean/12190/12190-9.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +long long A, B, lo, hi, mid, bee; + +long long price(long long num){ + long long aux; + aux = num <= 100 ? num * 2 : 200; + if(num > 100){ + if(num <= 10000) aux += (num - 100) * 3; + else aux += 29700; + } + if(num > 10000){ + if(num <= 1000000) aux += (num - 10000) * 5; + else aux += 4950000; + } + if(num > 1000000) aux += (num - 1000000) * 7; + return aux; +} + +long long invPrice(long long num){ + long long aux; + aux = num <= 200 ? num / 2 : 100; + if(num > 200){ + if(num <= 29900) aux += (num - 200) / 3; + else aux += 9900; + } + if(num > 29900){ + if(num <= 4979900) aux += (num - 29900) / 5; + else aux += 990000; + } + if(num > 4979900) aux += (num - 4979900) / 7; + return aux; +} + +int main(){ + while(scanf("%lld %lld", &A, &B), A | B){ + bee = invPrice(A); + lo = 0, hi = bee; + while(lo != hi){ + mid = (lo + hi) >> 1; + if(price(bee - mid) - price(mid) > B) lo = mid + 1; + else hi = mid; + } + printf("%lld\n", price(lo)); + } + return(0); +} diff --git a/uva_cpp_clean/12192/12192-11.cpp b/uva_cpp_clean/12192/12192-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..697f3c4dc264a8d9c9fe2e5920e852aaed4b4b1c --- /dev/null +++ b/uva_cpp_clean/12192/12192-11.cpp @@ -0,0 +1,107 @@ +#include +/* +Problem: 12192 - Grapevine +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3344 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int M, N, Q, L, R; +int grid[501][501]; + +int main () { + fastio; + + while ( cin >> N >> M && N && M ){ + + for (int i = 0; i < N; ++i){ + for (int j = 0; j < M; ++j){ + cin >> grid[i][j]; + } + } + + cin >> Q; + + while (Q--){ + cin >> L >> R; + + int ans = 0; + + for (int i = 0; i < N; ++i){ + int pos = lower_bound(grid[i], grid[i]+M, L) - grid[i]; + if (pos == M) continue; // not found + + int tmp = 0; + int x = i, y = pos; + while (x < N && y < M){ + if (grid[x][y] <= R) tmp++; + x++, y++; + } + + ans = max(ans, tmp); + } + + cout << ans << '\n'; + } + + cout << "-\n"; + } + + return 0; +} +/* +Sample input:- +----------------- +4 5 +13 21 25 33 34 +16 21 33 35 35 +16 33 33 45 50 +23 51 66 83 93 +3 +22 90 +33 35 +20 100 +4 4 +1 7 9 11 +5 8 10 12 +7 10 15 17 +11 19 30 41 +4 +6 20 +7 9 +10 10 +13 14 +0 0 + + +Sample output:- +----------------- +3 +2 +4 +- +3 +1 +1 +0 +- + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/12192/12192-19.cpp b/uva_cpp_clean/12192/12192-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65c5cf17911628f29dccd16a546d0670cf89f0f0 --- /dev/null +++ b/uva_cpp_clean/12192/12192-19.cpp @@ -0,0 +1,31 @@ +#include +#include +using namespace std; + +int main() { + int N, M, H[501][501]; + while (cin >> N >> M && !(N == 0 && M == 0)) { + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + cin >> H[i][j]; + } + } + int Q, L, U; + cin >> Q; + for (int q = 0; q < Q; q++) { + cin >> L >> U; + int maxi = 0; + for (int i = 0; i < N; i++) { + int j = lower_bound(H[i], H[i] + M, L) - H[i]; + int val = 0; + for (int k = 0; k < N - i && k < M - j; k++) { + if (H[i+k][j+k] <= U) val++; + if (val > maxi) maxi = val; + } + } + printf("%d\n", maxi); + } + printf("-\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12192/12192-21.cpp b/uva_cpp_clean/12192/12192-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..960f793b93ad8d1f6564847967e77780dd195ec3 --- /dev/null +++ b/uva_cpp_clean/12192/12192-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12192 + Name: Grapevine + Problem: https://onlinejudge.org/external/121/12192.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXNN 300000 +int H[501][501], n, m, l, r; +int ispos(int sz) { + for (int i=0, j=m; i<=n-sz; i++) { + j = lower_bound(H[i], H[i]+min(m, j+1), l) - H[i]; + if (j<=m-sz && H[i+sz-1][j+sz-1] <= r) + return true; + } + return false; +} + + +int main(){ + int q; + while (scanf("%d%d", &n, &m)==2 && (n||m)) { + for (int i=0; i>1; + if (ispos(mid)) + lo = mid + 1; + else + hi = mid - 1; + } + printf("%d\n", lo-1); + } + puts("-"); + } +} diff --git a/uva_cpp_clean/12192/12192-9.cpp b/uva_cpp_clean/12192/12192-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b6216df7aed16eb8bfbe16b876f229de297b9ce --- /dev/null +++ b/uva_cpp_clean/12192/12192-9.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int n, m, Q, L, U, l, x, ans; +vector v[500]; + +int main(){ + for(int i = 0; i <500; i++) + v[i].resize(500); + while(scanf("%d %d", &n, &m), n | m){ + for(int i = 0; i < n; i++){ + for(int j = 0; j < m; j++) + scanf("%d", &v[i][j]); + } + scanf("%d", &Q); + while(Q--){ + scanf("%d %d", &L, &U); + ans = 0; + for(int i = 0; i < n; i++){ + l = lower_bound(v[i].begin(), v[i].begin() + m, L) - v[i].begin(); + x = ans; + while(true){ + if(i + x >= n || l + x >= m || v[i + x][l + x] > U) + break; + ans = max(ans, x + 1); + x++; + } + } + printf("%d\n", ans); + } + putchar('-'); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/12195/12195-19.cpp b/uva_cpp_clean/12195/12195-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..960287e2cc5eec22779218ab9f1cae611769b5e9 --- /dev/null +++ b/uva_cpp_clean/12195/12195-19.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main() { + string s; + while (cin >> s && s != "*") { + int m = 0, d = 0; + for (int i = 0; i < s.length(); i++) { + if (s[i] == '/') { + if (d == 64) m++; + d = 0; + } else if (s[i] == 'W') d += 64; + else if (s[i] == 'H') d += 32; + else if (s[i] == 'Q') d += 16; + else if (s[i] == 'E') d += 8; + else if (s[i] == 'S') d += 4; + else if (s[i] == 'T') d += 2; + else if (s[i] == 'X') d += 1; + } + printf("%d\n", m); + } + return 0; +} diff --git a/uva_cpp_clean/122/122-15.cpp b/uva_cpp_clean/122/122-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6120f4ba35faa4432656df1ce5e3b8965451ed8c --- /dev/null +++ b/uva_cpp_clean/122/122-15.cpp @@ -0,0 +1,83 @@ +/* + 二元樹層次遍歷問題 + 時間複雜度: O(NlogN) (排序) +*/ +#include +#include +#include +#include +#include + +using namespace std; + +struct Node { + int number; + string route; +}; + +// 定義兩個Node的大小,讓他由上到下、由左而右 +bool cmp(Node a, Node b) { + if (a.route.size() < b.route.size()) + return true; + else if (a.route.size() == b.route.size()) + return a.route < b.route; + else + return false; +} + +vector tree_nodes; +unordered_map seen; + +bool read_data() { + seen.clear(); + tree_nodes.clear(); + bool is_end = false; + char c; + int x; + string input, route; + + while (cin >> input && input != "()") { + is_end = true; + stringstream string_data(input); + string_data >> c >> x >> c >> route; // (11,LL) => (x,route + route.pop_back(); // 刪掉右括號 + tree_nodes.push_back(Node{x, route}); + } + return is_end; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + while (read_data()) { + sort(tree_nodes.begin(), tree_nodes.end(), cmp); + // 如果第一個不是root(有路徑),就一定沒完成 + if (tree_nodes[0].route != "") + cout << "not complete\n"; + else { + bool is_complete = true; + int i, total_nodes; + string parent; + seen[""] = 1; + total_nodes = tree_nodes.size(); + // 按照順序迭代所有節點,如果上面沒有節點或是已經出現過一次了,也是沒完成 + for (i = 1; i < total_nodes; i++) { + parent = tree_nodes[i].route.substr(0, tree_nodes[i].route.size() - 1); // RRL 上方的節點必定是 RR,所以去掉最後一個字 + if (seen[parent] == 0 || seen[tree_nodes[i].route]) { + is_complete = false; + break; + } + seen[tree_nodes[i].route]++; + } + + if (is_complete) { + cout << tree_nodes.front().number; + for (i = 1; i < total_nodes; i++) + cout << ' ' << tree_nodes[i].number; + cout << '\n'; + } else + cout << "not complete\n"; + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/122/122-17.cpp b/uva_cpp_clean/122/122-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a939406799f063fd865781a7d87511a6ce1e171 --- /dev/null +++ b/uva_cpp_clean/122/122-17.cpp @@ -0,0 +1,78 @@ +/** + * AOAPC II Example 6-7 Trees on the Level + * Array implementation of binary tree. + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + string nd; + while (cin >> nd) + { + bool error = false; + int root = 1, cnt = 1; + vector left { 0, 0 }, right { 0, 0 }, val { 0, 0 }; + while (nd != "()") + { + auto sep = nd.find(','); + int v = stoi(nd.substr(1, sep - 1)); + int u = root; + for (char c : nd.substr(sep + 1)) + if (c == 'L') + { + if (left[u] == 0) + { + left[u] = ++cnt; + left.push_back(0); right.push_back(0); val.push_back(0); + } + u = left[u]; + } + else if (c == 'R') + { + if (right[u] == 0) + { + right[u] = ++cnt; + left.push_back(0); right.push_back(0); val.push_back(0); + } + u = right[u]; + } + if (val[u]) + error = true; + val[u] = v; + cin >> nd; + } + if (error) + { + cout << "not complete\n"; + continue; + } + vector ans; + queue q; + q.push(root); + while (!q.empty()) + { + auto u = q.front(); + q.pop(); + if (val[u] == 0) + { + error = true; + break; + } + ans.push_back(val[u]); + if (left[u]) + q.push(left[u]); + if (right[u]) + q.push(right[u]); + } + if (error) + cout << "not complete\n"; + else + { + for (int i = 0; i < ans.size() - 1; ++i) + cout << ans[i] << " "; + cout << ans.back() << "\n"; + } + } +} diff --git a/uva_cpp_clean/122/122-21.cpp b/uva_cpp_clean/122/122-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a76b9a30eed04a4bea0cdaf0a84b16b6c8f6af5 --- /dev/null +++ b/uva_cpp_clean/122/122-21.cpp @@ -0,0 +1,84 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 122 + Name: Trees on the level + Problem: https://onlinejudge.org/external/1/122.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct Node { + int val; + Node *left, *right; + Node(): val(-1), left(0), right(0) {} + ~Node() { + if (left) delete left; + if (right) delete right; + } +}; + + +int main(){ + char w[10000]; + int x, n; + for (;;) { + int free = 1; + bool dup = 0; + Node *root = new Node(); + for (n=0; cin>>w && w[1]!=')'; n++) if (!dup) { + sscanf(w+1, "%d", &x); + int i=2; + while (w[i]!=',') i++; + + Node *c = root; + for (i++; w[i]!=')'; i++) { + if (w[i] == 'L') { + if (!c->left) { + c->left = new Node(); + free++; + } + c = c->left; + } + else { + if (!c->right) { + c->right = new Node(); + free++; + } + c = c->right; + } + } + + if (c->val == -1) { + c->val = x; + free--; + } + else dup=1; + } + if (!n) break; + + if (dup || free) puts("not complete"); + else { + queue q; + q.push(root); + while (!q.empty()) { + Node *c = q.front(); q.pop(); + if (c!=root) putchar(' '); + cout << c->val; + + if (c->left) q.push(c->left); + if (c->right) q.push(c->right); + } + cout << endl; + } + + delete root; + } +} diff --git a/uva_cpp_clean/12207/12207-21.cpp b/uva_cpp_clean/12207/12207-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4df2136dcf9000bcd50b7efbd7115741c727b6c2 --- /dev/null +++ b/uva_cpp_clean/12207/12207-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12207 + Name: That is Your Queue + Problem: https://onlinejudge.org/external/122/12207.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, p, x; + for (int cse=1; cin>>p>>n && (n||p); ++cse) { + list q; + for (int i=min(n,p); i>0; --i) + q.push_front(i); + + cout << "Case " << cse << ":\n"; + for (int i=0; i> c; + if (c == 'N') { + cout << q.front() << endl; + q.push_back(q.front()); + q.pop_front(); + } + else { + cin >> x; + for (auto it=q.begin(); it!=q.end(); ++it) + if (*it == x) { + q.erase(it); + break; + } + q.push_front(x); + } + } + } +} diff --git a/uva_cpp_clean/12207/12207-4.cpp b/uva_cpp_clean/12207/12207-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aeb97ea45bb59b17baa9c709385f9accdc3c5b84 --- /dev/null +++ b/uva_cpp_clean/12207/12207-4.cpp @@ -0,0 +1,66 @@ +#include +#include + +using namespace std; + +class EmergencyQueue +{ +private: + list pepole; + +public: + EmergencyQueue(int population) + { + for (int i = 1; i <= population; i++) + { + pepole.push_back(i); + } + } + + int Next() + { + int person = pepole.front(); + pepole.pop_front(); + pepole.push_back(person); + + return person; + } + + void Emegency(int person) + { + pepole.remove(person); + pepole.push_front(person); + } +}; + +int main() +{ + int population, commnands, caseN = 0; + + while (true) + { + cin >> population >> commnands; + if (population == 0 && commnands == 0) + break; + + cout << "Case " << ++caseN << ":" << endl; + + EmergencyQueue em = EmergencyQueue(min(commnands, population)); + char command; + + for (int i = 0; i < commnands; i++) + { + cin >> command; + if (command == 'E') + { + int inx; + cin >> inx; + em.Emegency(inx); + } + else + cout << em.Next() << endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1221/1221-21.cpp b/uva_cpp_clean/1221/1221-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..486c248a886786f2e8e135cf689b7478375de786 --- /dev/null +++ b/uva_cpp_clean/1221/1221-21.cpp @@ -0,0 +1,101 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1221 + Name: Against Mammoths + Problem: https://onlinejudge.org/external/12/1221.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + + +int Ix[255], Iy[255], Rx[255], Ry[255], D[255][255], n, m; +vector adj[255]; +int match[255]; +bool seen[255]; +bool dfs(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + if (match[v]==-1 || dfs(match[v])) { + match[v] = u; + return 1; + } + } + return 0; +} + +bool bpm(int maxt) { + if (n < m) return 0; + + for (int i=0; i=cnt; ++i) { + memset(seen, 0, m); + if (dfs(i) && !--cnt) + return 1; + } + return 0; +} + + +int divu(int a, int b) { return (a+b-1) / b; } +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n >> m && (n||m)) { + for (int i=0; i> Ix[i] >> Rx[i]; + + for (int i=0; i> Iy[i] >> Ry[i]; + + vector V; + for (int i=0; i> tt; + if (Rx[i] <= Ry[j]) + D[i][j] = Ix[i] >= Iy[j]+tt*Ry[j] ? tt : INF; + else + D[i][j] = tt + divu( Iy[j]-Ix[i]+Ry[j]*tt , Rx[i]-Ry[j] ); + + if (D[i][j] != INF) + V.push_back(D[i][j]); + } + + if (!bpm(INF-1)) + cout << "IMPOSSIBLE\n"; + else if (V.empty()) + cout << "0\n"; + else { + int lo=0, hi=1; + sort(V.begin(), V.end()); + for (int i=1; i> 1; + if (bpm(V[mid])) + hi = mid - 1; + else + lo = mid + 1; + } + + cout << V[hi+1] << endl; + } + } +} diff --git a/uva_cpp_clean/12210/12210-13.cpp b/uva_cpp_clean/12210/12210-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a97b3814a984de9e6a6810221ee07a56ef4789ec --- /dev/null +++ b/uva_cpp_clean/12210/12210-13.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +int b, s, how, ans, t = 1; + +int main() +{ + while (cin >> b >> s, b != 0 || s != 0) + { + int* bachelor = new int[b]; + int* spinster = new int[s]; + + for (int i = 0; i < b; i++) cin >> bachelor[i]; + for (int i = 0; i < s; i++) cin >> spinster[i]; + + sort(bachelor, bachelor + b); + + cout << "Case " << t++ << ": "; + + if (b > s) cout << b - s << " " << bachelor[0] << endl; + + else cout << 0 << endl; + } +} diff --git a/uva_cpp_clean/12210/12210-19.cpp b/uva_cpp_clean/12210/12210-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b53e67fa967b7183add51b4b8c185166f0313867 --- /dev/null +++ b/uva_cpp_clean/12210/12210-19.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + int B, S, cases = 1; + priority_queue bach, spin; + stack pending; + while (scanf("%d %d", &B, &S) && !(B == 0 && S == 0)) { + while(!pending.empty()) pending.pop(); + while(!bach.empty()) bach.pop(); + while(!spin.empty()) spin.pop(); + for (int i = 0; i < B; i++) { + int age; + scanf("%d", &age); + bach.push(age); + } + for (int i = 0; i < S; i++) { + int age; + scanf("%d", &age); + spin.push(age); + } + bool singles = false; + int bage, count; + while (!bach.empty()) { + bage = bach.top(); + bach.pop(); + if (!singles) { + if (pending.empty() && !spin.empty()) { + pending.push(spin.top()); + spin.pop(); + } + while (!spin.empty() && abs(bage - spin.top()) < abs(bage - pending.top())) { + pending.push(spin.top()); + spin.pop(); + } + if (!pending.empty()) pending.pop(); + else singles = true, count = bach.size() + 1; + } + } + printf("Case %d: ", cases); + if (singles) printf("%d %d\n", count, bage); + else printf("0\n"); + cases ++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12210/12210-21.cpp b/uva_cpp_clean/12210/12210-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5119a783f4629282fa47b2f447c08b635155e714 --- /dev/null +++ b/uva_cpp_clean/12210/12210-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12210 + Name: A Match Making Problem + Problem: https://onlinejudge.org/external/122/12210.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m, x; + for (int cse=1; cin >> n >> m && (n||m); ++cse) { + int dif = n - m, mn = 100; + while (n--) { + cin >> x; + mn = min(mn, x); + } + + while (m--) cin >> x; + + cout << "Case " << cse << ": "; + if (dif <= 0) cout << "0\n"; + else cout << dif << ' ' << mn << "\n"; + } +} diff --git a/uva_cpp_clean/12238/12238-14.cpp b/uva_cpp_clean/12238/12238-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34296272683b98d65f0921e57290aeb60af9c131 --- /dev/null +++ b/uva_cpp_clean/12238/12238-14.cpp @@ -0,0 +1,162 @@ +/*************************************************** + * Problem Name : 12238 - Ants Colony.cpp + * Problem Link : https://uva.onlinejudge.org/external/122/12238.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-09-19 + * Problem Type : LCA + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX], cost[MAX]; +int level[MAX], par[MAX], table[MAX][27]; +ll dis[MAX]; +void dfs (int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + level[v] = level[u] + 1; + par[v] = u; + dis[v] = dis[u] + (ll) cost[u][i]; + dfs (v, u); + } +} + +void LCA (int n) { + SET (table, -1); + + for (int i = 0; i < n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 0; i < n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + } + } + } +} + +int LCA_Quary (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} +int main () { + __FastIO; + int n; + + while (cin >> n) { + if (n == 0) break; + + for (int i = 1; i < n ; i++) { + int v, w; + cin >> v >> w; + //~ adj[i].pb(v); + adj[v].pb (i); + //~ cost[i].pb(w); + cost[v].pb (w); + } + + SET (level, 0); + SET (par, 0); + SET (dis, 0); + par[0] = -1; + dfs (0, -1); + LCA (n); + int Q; + cin >> Q; + + for (int q = 1; q <= Q ; q++) { + int u, v; + cin >> u >> v; + int x = LCA_Quary (u, v); + ll ans = (dis[u] + dis[v] ) - 2 * dis[x]; + + if (q != 1) cout << " "; + + cout << ans; + } + + nl; + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + cost[i].clear(); + } + } + + return 0; +} diff --git a/uva_cpp_clean/12238/12238-6.cpp b/uva_cpp_clean/12238/12238-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c0599143d7dcbc9e1bf50bb6ab0affc10241e11 --- /dev/null +++ b/uva_cpp_clean/12238/12238-6.cpp @@ -0,0 +1,140 @@ +/* + graphs > specials > tree > lowest common ancestor (LCA) + difficulty: medium + date: 10/Oct/2020 + problem: given a weighted tree, find the distance between 2 query vertices + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +vector>> adj_list; + +class sparse_table { + private: + vector A; int N; + vector> table; + + int range_combination(int i, int j) { + return A[i] < A[j]? i : j; + } + + void build() { + int P = (int)floor(log2(N)); + table.assign(P+1, vector(N)); + + for (int n = 0; n < N; n++) table[0][n] = n; + + for (int p = 1; p <= P; p++) + for (int n = 0; n + (1< const &A) { + this->A = A; + N = (int)A.size(); + build(); + } + + int range_query(int l, int r) { + int size = r-l+1; + int p = (int)floor(log2(size)); + return range_combination(table[p][l], table[p][r - (1<>> adj_list; int V; + + int e; + vector tour_depth; + vector tour_vertex; + + vector depth; + vector last_e; + vector seen; + + sparse_table st; + + void process(int u, ll u_depth) { + tour_vertex[e] = u; + tour_depth[e] = u_depth; + last_e[u] = e++; + } + + void dfs(int u, ll u_depth = 0) { + seen[u] = true; + depth[u] = u_depth; + process(u, u_depth); + + for (auto adj : this->adj_list[u]) { + int v; ll w; tie(v, w) = adj; + if (!seen[v]) { + dfs(v, u_depth+w); + process(u, u_depth); + } + } + } + + void build(int root) { + last_e.resize(V); + depth.resize(V); + + e = 0; + tour_depth.resize(2*V-1); + tour_vertex.resize(2*V-1); + + seen.assign(V, false); + dfs(root); + + st = sparse_table(tour_depth); + } + + public: + lca() {} + lca(vector>> const &adj_list, int root = 0) { + this->adj_list = adj_list; + V = adj_list.size(); + build(root); + } + + int query_lca(int u, int v) { + int l = min(last_e[u], last_e[v]); + int r = max(last_e[u], last_e[v]); + return tour_vertex[st.range_query(l, r)]; + } + + ll query_dist(int u, int v) { + return depth[u] + depth[v] - 2 * depth[query_lca(u,v)]; + } +}; + +int main() { + int V; + while (cin >> V && V != 0) { + adj_list.assign(V, vector>()); + for (int u = 1; u <= V-1; u++) { + int v; ll w; cin >> v >> w; + adj_list[u].push_back({v,w}); + adj_list[v].push_back({u,w}); + } + + lca l(adj_list); + + int Q; cin >> Q; + while (Q--) { + int s, t; cin >> s >> t; + cout << l.query_dist(s, t); if (Q > 0) cout << " "; + } + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12238/12238-9.cpp b/uva_cpp_clean/12238/12238-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02178fd5ed0d3534be7183c8754b351b71aff05e --- /dev/null +++ b/uva_cpp_clean/12238/12238-9.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +const int MAX_V = 1e5 + 10, LG = 18; + +int V, H[MAX_V], jump[LG][MAX_V]; +long long add[MAX_V]; +vector G[MAX_V], W[MAX_V]; + +void dfs (int u, int p = -1) { + for (int i = 0; i < G[u].size(); i++) { + int v = G[u][i], w = W[u][i]; + if (v != p) { + H[v] = H[u] + 1; + jump[0][v] = u; + add[v] = w + add[u]; + dfs(v, u); + } + } +} + +void buildLCA () { + for (int k = 1; k < LG; k++) { + for (int v = 0; v < V; v++) { + int u = jump[k - 1][v]; + if (~u) jump[k][v] = jump[k - 1][u]; + } + } +} + +void walk (int& u, int d) { + for (int k = LG - 1; k >= 0; k--) if ((d >> k) bitand 1) u = jump[k][u]; +} + +int getLCA (int u, int v) { + if (H[u] < H[v]) swap(u, v); + walk(u, H[u] - H[v]); + if (u == v) return u; + for (int k = LG - 1; k >= 0; k--) { + if (jump[k][u] != jump[k][v]) { + u = jump[k][u]; + v = jump[k][v]; + } + } + return jump[0][u]; +} + +void processQueries () { + int q; + scanf("%d", &q); + while (q--) { + int u, v; + scanf("%d %d", &u, &v); + printf("%lld", add[u] + add[v] - 2 * add[getLCA(u, v)]); + if (q) putchar(' '); + } + putchar('\n'); +} + +bool readTree () { + scanf("%d", &V); + if (V == 0) return false; + for (int u = 1, v, w; u < V; u++) { + scanf("%d %d", &v, &w); + G[u].push_back(v); W[u].push_back(w); + G[v].push_back(u); W[v].push_back(w); + } + return true; +} + +void clear () { + fill(add, add + V + 10, 0); + for (int i = 0; i < LG; i++) fill(jump[i], jump[i] + V + 10, -1); + for (int i = 0; i < V; i++) G[i].clear(), W[i].clear(); +} + +int main () { + memset(jump, -1, sizeof jump); + while (readTree()) { + dfs(0); + buildLCA(); + processQueries(); + clear(); + } + return (0); +} diff --git a/uva_cpp_clean/12239/12239-9.cpp b/uva_cpp_clean/12239/12239-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ae33861eed4c8f1f2f0f9fe667bf52efe46eb26 --- /dev/null +++ b/uva_cpp_clean/12239/12239-9.cpp @@ -0,0 +1,24 @@ +#include + +#define SIZE 100 + +using namespace std; + +int N, B, b[SIZE]; +bool vis[SIZE], ok; + +int main(){ + while(scanf("%d %d", &N, &B), N | B){ + memset(vis, false, sizeof vis); + for(int it = 0; it < B; it++) scanf("%d", &b[it]); + for(int it1 = 0; it1 < B; it1++) + for(int it2 = it1; it2 < B; it2++) + vis[ abs(b[it1] - b[it2]) ] = true; + ok = true; + for(int it = 0; it <= N; it++) + if( not vis[it] ) ok = false; + if(ok) puts("Y"); + else puts("N"); + } + return(0); +} diff --git a/uva_cpp_clean/12243/12243-14.cpp b/uva_cpp_clean/12243/12243-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f18e1962301b5f0e0bd697dee33e26d7d3fe8089 --- /dev/null +++ b/uva_cpp_clean/12243/12243-14.cpp @@ -0,0 +1,122 @@ +/*************************************************** + * Problem Name : 12243 Flowers Flourish from France.cpp + * Problem Link : https://onlinejudge.org/external/122/12243.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-10-28 + * Problem Type : Easy + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10); + //~ cout << fixed; + string line; + while(getline(cin, line)){ + if(line[0] == '*') break; + bool flag = false; + char c = '.'; + string str; + stringstream ss; + ss << line; + while(ss>>str){ + if(c!= '.'){ + if( c!= tolower(str[0])) flag = true; + }else{ + c = tolower(str[0]); + } + } + if(!flag) cout << "Y\n"; + else cout << "N\n"; + } + return 0; +} diff --git a/uva_cpp_clean/12243/12243-19.cpp b/uva_cpp_clean/12243/12243-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b2db3727437aff3ffe87430aa32018df41ca85d --- /dev/null +++ b/uva_cpp_clean/12243/12243-19.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + string line; + while (getline(cin, line) && line != "*") { + stringstream st(line); + string s; + bool ok = true; + st >> s; + char ref = s[0]; + if (isupper(ref)) ref = tolower(ref); + while (st >> s) { + char c = s[0]; + if (isupper(c)) c = tolower(c); + ok = ok && (c == ref); + } + if (ok) printf("Y\n"); + else printf("N\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12243/12243-20.cpp b/uva_cpp_clean/12243/12243-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d499003e9f12261a82457ba34a07852c1bff165 --- /dev/null +++ b/uva_cpp_clean/12243/12243-20.cpp @@ -0,0 +1,30 @@ +#include +#include + +char sentence[1055]; + +int main() +{ + while( gets(sentence) && strcmp(sentence, "*") ) + { + if( sentence[0]>='A' && sentence[0]<='Z' ) sentence[0] = 'a'+sentence[0]-'A'; + + bool flag = true; + int counter = 0; + for(int i=1 ; i='A' && sentence[i+1]<='Z' ) sentence[i+1] = 'a'+sentence[i+1]-'A'; + + if( sentence[0]==sentence[i+1] ) flag = true; + else if( sentence[0]!=sentence[i+1] ) flag = false; + } + if( !flag ) break; + } + + printf("%s\n", (flag || !counter) ? "Y" : "N"); + } + return 0; +} diff --git a/uva_cpp_clean/12243/12243-21.cpp b/uva_cpp_clean/12243/12243-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9d1c8c59db0ae69e68ab9c177fe2712831d3086 --- /dev/null +++ b/uva_cpp_clean/12243/12243-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12243 + Name: Flowers Flourish from France + Problem: https://onlinejudge.org/external/122/12243.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string line, word; + while (getline(cin, line)) { + stringstream sin(line); + sin >> word; + if (word == "*") break; + char ch=toupper(word[0]); + bool t=1; + while (t && sin >> word) + t = t && toupper(word[0]) == ch; + + cout << (t ? "Y\n" : "N\n"); + } +} diff --git a/uva_cpp_clean/12243/12243-5.cpp b/uva_cpp_clean/12243/12243-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..258b36c78fdbecd3a860e5eba37b79151e0e98c5 --- /dev/null +++ b/uva_cpp_clean/12243/12243-5.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +int main() +{ + char s[2000]; + char c; + while(gets(s)) + { + if(!strcmp(s,"*")) + break; + int p=0,k=0; + for(int i=0;s[i]!='\0';i++) + { + if(s[i]>='A'&&s[i]<='Z') + s[i]=s[i]+32; + if(s[i]==' ') + p++; + } + c=s[0]; + int f=0; + for(int i=0;s[i]!='\0';i++) + { + if(s[i]==' ') + { + if(c==s[i+1]) + k++; + } + + } + if(p==k){ + printf("Y\n"); + continue; + } + else{ + printf("N\n"); + continue; + } + } + return 0; +} diff --git a/uva_cpp_clean/12247/12247-19.cpp b/uva_cpp_clean/12247/12247-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20708cf7f71eab5db7131bb09fc14abfda90b736 --- /dev/null +++ b/uva_cpp_clean/12247/12247-19.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + +int main() { + int A, B, C, X, Y; + bool deck[53]; + for (int i = 0; i < 53; i++) deck[i] = false; + while (cin >> A >> B >> C >> X >> Y && !(A == 0 && B == 0 && C == 0 && X == 0 && Y == 0)) { + int P1[3], P2[3]; + P1[0] = A; P1[1] = B; P1[2] = C; + sort(P1, P1 + 3); + deck[A] = deck[B] = deck[C] = deck[X] = deck[Y] = true; + int sol = -1; + int match[3]; + for (int i = 1; i <= 52 && sol == -1; i++) { + if (deck[i]) continue; + match[0] = match[1] = match[2] = false; + P2[0] = X; P2[1] = Y; P2[2] = i; + sort(P2, P2 + 3); + int count = 0; + for (int k = 0; k <= 2; k++) { + int auxi = -1; + for (int j = 2; j >= 0; j--) { + if (!match[j]) { + if (auxi == -1) auxi = j; + if (P2[j] < P1[k]) { + auxi = j, count++; + break; + } + } + } + match[auxi] = true; + } + if (count < 2) sol = i; + } + printf("%d\n", sol); + deck[A] = deck[B] = deck[C] = deck[X] = deck[Y] = false; + } + return 0; +} diff --git a/uva_cpp_clean/12247/main.cpp b/uva_cpp_clean/12247/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be14a867fc61095f16ec599eef3d58047f0aa96b --- /dev/null +++ b/uva_cpp_clean/12247/main.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; + +int main(){ + + int a, b, c, x, y, option; + while(cin >> a >> b >> c >> x >> y && a){ + if(max(x, max(c, max(a,b))) == x || max(y, max(c, max(a,b))) == y){ + int grande = max(x,y); + int peque = min(x,y); + int cont = 0; + if(peque > a) cont++; + if(peque > b) cont++; + if(peque > c) cont++; + if(cont <= 1){ + int sol = max(a, max(b,c))+1; + if(sol != x && sol != y && sol != a && sol != b && sol != c && sol <= 52) cout << sol << endl; + else if(sol+1 != x && sol+1 != y && sol+1 != a && sol+1 != b && sol+1 != c && sol+1 <= 52) cout << sol+1 << endl; + else if(sol+2 != x && sol+2 != y && sol+2 != a && sol+2 != b && sol+2 != c && sol+2 <= 52) cout << sol+2 << endl; + else if(sol+3 != x && sol+3 != y && sol+3 != a && sol+3 != b && sol+3 != c && sol+3 <= 52) cout << sol+3 << endl; + else if(sol + 4 <= 52) cout << sol +4 << endl; + else cout << -1 << endl; + } + else if(cont == 2){ + //cout << "here" << endl; + int big = max(a, max(b,c)); + int little = min(a, min(b,c)); + int sol = a+b+c-big-little+1; + if(sol != x && sol != y && sol != a && sol != b && sol != c && sol <= 52) cout << sol << endl; + else if(sol+1 != x && sol+1 != y && sol+1 != a && sol+1 != b && sol+1 != c && sol+1 <= 52) cout << sol+1 << endl; + else if(sol+2 != x && sol+2 != y && sol+2 != a && sol+2 != b && sol+2 != c && sol+2 <= 52) cout << sol+2 << endl; + else if(sol+3 != x && sol+3 != y && sol+3 != a && sol+3 != b && sol+3 != c && sol+3 <= 52) cout << sol+3 << endl; + else if(sol + 4 <= 52) cout << sol +4 << endl; + else cout << "-1\n"; + + }else if(a != 1 && b != 1 && c != 1 && x != 1 && y != 1) cout << "1\n"; + else if(a != 2 && b != 2 && c != 2 && x != 2 && y != 2) cout << "2\n"; + else if(a != 3 && b != 3 && c != 3 && x != 3 && y != 3) cout << "3\n"; + else if(a != 4 && b != 4 && c != 4 && x != 4 && y !=4) cout << "4\n"; + else if(a != 5 && b != 5 && c != 5 && x != 5 && y !=5) cout << "5\n"; + else cout << "6\n"; + + }else{ + int big = max(a, max(b,c)); + int little = min(a, min(b,c)); + int middle = a+b+c-big-little; + int contX = 0, contY = 0; + if(x > a) contX ++; + if(x > b) contX ++; + if(x > c) contX ++; + if(y > a) contY ++; + if(y > b) contY ++; + if(y > c) contY ++; + + if(contY == 2 && contX == 2){ + int sol = middle+1; + if(sol != x && sol != y && sol != a && sol != b && sol != c) cout << sol << endl; + else if(sol+1 != x && sol+1 != y && sol+1 != a && sol+1 != b && sol+1 != c && sol +1 <= 52) cout << sol+1 << endl; + else if(sol+2 != x && sol+2 != y && sol+2 != a && sol+2 != b && sol+2 != c && sol +2 <= 52) cout << sol+2 << endl; + else if(sol+3 != x && sol+3 != y && sol+3 != a && sol+3 != b && sol+3 != c && sol +3 <= 52) cout << sol+3 << endl; + else if(sol+4 != x && sol+4 != y && sol+4 != a && sol+4 != b && sol+4 != c && sol +4 <= 52) cout << sol+4 << endl; + else if(sol +5 <= 52) cout << sol+5 << endl; + } + else cout << -1 << endl; + + } + } + + +return 0; +} diff --git a/uva_cpp_clean/12249/12249-21.cpp b/uva_cpp_clean/12249/12249-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e38f16f6205b768b76e2adb010b01cc8d4ef6d3 --- /dev/null +++ b/uva_cpp_clean/12249/12249-21.cpp @@ -0,0 +1,58 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12249 + Name: Overlapping Scenes + Problem: https://onlinejudge.org/external/122/12249.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +string merge(const string &s1, const string &s2) { + int l1 = s1.length(), + l2 = s2.length(); + for (int i=min(l1, l2); i>0; --i) + if (s1.substr(l1-i, i) == s2.substr(0, i)) + return s1 + s2.substr(i, l2-i); + return s1 + s2; +} + +int n, best; +string S[7]; +bool used[7]; +void bt(int cnt, const string bb) { + if (bb.length() >= best) return; + if (cnt == n) { + best = bb.length(); + return; + } + + for (int i=0; i> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n; + for (int i=0; i> S[i]; + + best = (int)1e9; + bt(0, ""); + cout << "Case " << cse << ": " << best << '\n'; + } +} diff --git a/uva_cpp_clean/1225/1225-11.cpp b/uva_cpp_clean/1225/1225-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4bc83c1ab4eaa1c982bbbe275cd93d7137db22fc --- /dev/null +++ b/uva_cpp_clean/1225/1225-11.cpp @@ -0,0 +1,61 @@ +#include +/* +Problem: 1225 - Digit Counting +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3666 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int T, N; + +int main () { + + scanf("%d", &T); + while ( T-- ){ + scanf("%d\n", &N); + + int f[10] = {}; // frequency array for each digit + + for (int i = 1; i <= N; ++i){ + int n = i; + + while ( n ){ + f[n%10]++; + n /= 10; + } + } + + printf("%d %d %d %d %d %d %d %d %d %d\n", f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]); + } + + return 0; +} +/* +Sample input:- +----------------- +2 +3 +13 + +Sample output:- +----------------- +0 1 1 1 0 0 0 0 0 0 +1 6 2 2 1 1 1 1 1 1 + + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/1225/1225-14.cpp b/uva_cpp_clean/1225/1225-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fd1d199570f1f61c25bc8cb68c89ab5a83eac67 --- /dev/null +++ b/uva_cpp_clean/1225/1225-14.cpp @@ -0,0 +1,79 @@ +/*************************************************** + * Problem name : 1225 Digit Counting.cpp + * Problem Link : https://uva.onlinejudge.org/external/12/1225.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-25 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,0,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 11 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc, N, ar[MAX]; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d", &N); + string str = ""; + for (int i = 1; i <= N; i++) { + str += NumberToString(i); + } + SET(ar); + int sz = str.size(); + for (int i = 0; i < sz; i++) { + int num = (str[i] - '0'); + ar[num]++; + } + for (int i = 0; i <= 9; i++) { + if (i != 0) printf(" "); + printf("%d", ar[i]); + } + nl; + + } + return 0; +} diff --git a/uva_cpp_clean/1225/1225-15.cpp b/uva_cpp_clean/1225/1225-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e777f37bfd46a85a17b8119750f186252c76bb9a --- /dev/null +++ b/uva_cpp_clean/1225/1225-15.cpp @@ -0,0 +1,39 @@ +/* + 建表 + 時間複雜度: O(1) +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + vector> table; + table.reserve(10000); + table.push_back(vector(10, 0)); // 沒有 0 所以把它拿來初始化 + int n; + cin >> n; + + for (int i = 1; i < 10000; i++) { + auto temp = table.back(); // 每一項的值是以上一項為基礎 + int j = i; + while (j) { + temp[j % 10]++; + j /= 10; + } + table.push_back(temp); + } + + while (n--) { + int m; + cin >> m; + for (int i = 0; i < 9; i++) + cout << table[m][i] << " "; + cout << table[m][9] << "\n"; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1225/1225-17.cpp b/uva_cpp_clean/1225/1225-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d37959051fba96968e2de0befd96d2af2c6780ab --- /dev/null +++ b/uva_cpp_clean/1225/1225-17.cpp @@ -0,0 +1,31 @@ +/** + * AOAPC II 3-3 Digit Counting + * Table lookup + * 10 AC + */ +#include +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + const int maxN = 10005; + vector> t(maxN, vector(maxN)); + for (int i = 1; i < maxN; ++i) { + copy(t[i - 1].begin(), t[i - 1].end(), t[i].begin()); + int x = i; + while (x > 0) { + ++t[i][ x % 10 ]; + x /= 10; + } + } + int N; + cin >> N; + while (N--) { + int d; + cin >> d; + cout << t[d][0]; + for (int i = 1; i < 10; ++i) + cout << " " << t[d][i]; + cout << '\n'; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1225/1225-19.cpp b/uva_cpp_clean/1225/1225-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e929bf4357e4ec8b9af5ffd107780c13a49a494 --- /dev/null +++ b/uva_cpp_clean/1225/1225-19.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() { + int grid[10002][10]; + for (int i = 0; i < 10; i++) { + grid[0][i] = 0; + } + for (int i = 1; i < 10002; i++) { + for (int j = 0; j < 10; j++) { + grid[i][j] = grid[i-1][j] + (i%10 == j? 1: 0) + ((i/10)%10 == j && i >= 10? 1: 0) + ((i/100)%10 == j && i >= 100? 1: 0) + ((i/1000)%10 == j && i >= 1000? 1: 0) + ((i/10000)%10 == j && i >= 10000? 1: 0); + } + } + int T; + scanf("%d", &T); + while (T--) { + int n; + scanf("%d", &n); + for (int j = 0; j < 10; j++) { + printf("%d", grid[n][j]); + if (j != 9) printf(" "); + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1225/1225-4.cpp b/uva_cpp_clean/1225/1225-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3908bdf24712380f594a49cd922f2c801df56acd --- /dev/null +++ b/uva_cpp_clean/1225/1225-4.cpp @@ -0,0 +1,32 @@ +#include + +int main() +{ + int n; + std::cin >> n; + + while (n--) + { + int number; + std::cin >> number; + + int tab[10] = {0}; + + for (int i = 1; i <= number; i++) + { + int temp = i; + while (temp != 0) + { + tab[temp % 10]++; + temp /= 10; + } + } + + std::cout << tab[0]; + for (int i = 1; i < 10; i++) + { + std::cout << " " << tab[i]; + } + std::cout << std::endl; + } +} diff --git a/uva_cpp_clean/1225/1225-5.cpp b/uva_cpp_clean/1225/1225-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..aeb37daa02aef8cc5cac0c38e9e8471ad5dae555 --- /dev/null +++ b/uva_cpp_clean/1225/1225-5.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int t; + sc("%d",&t); + while(t--) + { + mapmp; + int n; + sc("%d",&n); + for(int i=1;i<=n;i++) + { + int tp=i; + while(tp>0) + { + mp[tp%10+'0']++; + tp=tp/10; + } + } + pf("%d %d %d %d %d %d %d %d %d %d\n",mp['0'],mp['1'],mp['2'], + mp['3'],mp['4'],mp['5'],mp['6'],mp['7'],mp['8'],mp['9']); + } + + + return 0; +} diff --git a/uva_cpp_clean/1225/1225-9.cpp b/uva_cpp_clean/1225/1225-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24201fe158ecf723fce6e9a6d142055949c3d240 --- /dev/null +++ b/uva_cpp_clean/1225/1225-9.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int a[10001][10]; + +int main(){ + int c, n; + scanf("%d", &c); + for(int i = 1; i < 10001; i++){ + n = i; + while(n) + a[i][n % 10]++, n /= 10; + for(int j = 0; j < 10; j++) + a[i][j] += a[i - 1][j]; + } + while(c--){ + scanf("%d", &n); + for(int j = 0; j < 10; j++){ + printf("%d", a[n][j]); + if(j != 9) + putchar(' '); + } + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/12250/12250-19.cpp b/uva_cpp_clean/12250/12250-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c9b4de11a709f0250cac613222228343dc7515a --- /dev/null +++ b/uva_cpp_clean/12250/12250-19.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +int main() { + string word; + int n = 1; + while (getline(cin, word) && word != "#") { + cout << "Case " << n++ << ": "; + if (word == "HELLO") cout << "ENGLISH"; + else if (word == "HOLA") cout << "SPANISH"; + else if (word == "HALLO") cout << "GERMAN"; + else if (word == "BONJOUR") cout << "FRENCH"; + else if (word == "CIAO") cout << "ITALIAN"; + else if (word == "ZDRAVSTVUJTE") cout << "RUSSIAN"; + else cout << "UNKNOWN"; + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12250/12250-21.cpp b/uva_cpp_clean/12250/12250-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4724bc056c21b0df2de46d0532e5ff13daae5425 --- /dev/null +++ b/uva_cpp_clean/12250/12250-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12250 + Name: Language Detection + Problem: https://onlinejudge.org/external/122/12250.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + map langs; + langs[string("HELLO")] = "ENGLISH"; + langs[string("HOLA")] = "SPANISH"; + langs[string("HALLO")] = "GERMAN"; + langs[string("BONJOUR")] = "FRENCH"; + langs[string("CIAO")] = "ITALIAN"; + langs[string("ZDRAVSTVUJTE")] = "RUSSIAN"; + + int cse=1; + char word[20]; + while(cin>>word && (word[1] || word[0]!='#')) { + char *res = langs[string(word)]; + if (!res || !res[0]) res = "UNKNOWN"; + + printf("Case %d: %s\n", cse++, res); + } +} diff --git a/uva_cpp_clean/12250/12250-5.cpp b/uva_cpp_clean/12250/12250-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bf8db6332c017f48ec3b36029f82f205bddafda0 --- /dev/null +++ b/uva_cpp_clean/12250/12250-5.cpp @@ -0,0 +1,60 @@ +/*# include +# include +int main() +{ + char a[20]; + int i; + for(i=1;scanf("%s",a);i++) + { + if(a[0]=='#' && a[1]=='\0') + return 0; + if(strcmp(a,"HELLO")==0) + printf("Case %d: ENGLISH\n",i); + else if(strcmp(a,"HOLA")==0) + printf("Case %d: SPANISH\n",i); + else if(strcmp(a,"HALLO")==0) + printf("Case %d: GERMAN\n",i); + else if(strcmp(a,"BONJOUR")==0) + printf("Case %d: FRENCH\n",i); + else if(strcmp(a,"CIAO")==0) + printf("Case %d: ITALIAN\n",i); + else if(strcmp(a,"ZDRAVSTVUJTE")==0) + printf("Case %d: RUSSIAN\n",i); + else + printf("Case %d: UNKNOWN\n",i); + } + return 0; +}*/ + + +# include +# include +int main() +{ + char a[20]; + int i; + for(i = 1 ; ; i++) + { + while(scanf ("%s",&a)==1) +{ + if(a[0]=='#' && a[1]=='\0') + return 0; + if(strcmp(a,"HELLO")==0) + printf("Case %d: ENGLISH\n",i); + else if(strcmp(a,"HOLA")==0) + printf("Case %d: SPANISH\n",i); + else if(strcmp(a,"HALLO")==0) + printf("Case %d: GERMAN\n",i); + else if(strcmp(a,"BONJOUR")==0) + printf("Case %d: FRENCH\n",i); + else if(strcmp(a,"CIAO")==0) + printf("Case %d: ITALIAN\n",i); + else if(strcmp(a,"ZDRAVSTVUJTE")==0) + printf("Case %d: RUSSIAN\n",i); + else + printf("Case %d: UNKNOWN\n",i); + break; + } +} + return 0; +} diff --git a/uva_cpp_clean/12250/12250-9.cpp b/uva_cpp_clean/12250/12250-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5467796ad504ea8db1c564af9c8776423cf653eb --- /dev/null +++ b/uva_cpp_clean/12250/12250-9.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +int main() { + char word[100]; + int i = 1; + + while (scanf("%s", &word) != EOF) { + if (strcmp(word, "#") == 0) { + break; + } + else if (strcmp(word, "HOLA") == 0) { + printf("Case %d: SPANISH\n", i); + } + else if (strcmp(word, "HELLO") == 0) { + printf("Case %d: ENGLISH\n", i); + } + else if (strcmp(word, "HALLO") == 0) { + printf("Case %d: GERMAN\n", i); + } + else if (strcmp(word, "BONJOUR") == 0) { + printf("Case %d: FRENCH\n", i); + } + else if (strcmp(word, "CIAO") == 0) { + printf("Case %d: ITALIAN\n", i); + } + else if (strcmp(word, "ZDRAVSTVUJTE") == 0) { + printf("Case %d: RUSSIAN\n", i); + } + else { + printf("Case %d: UNKNOWN\n", i); + } + ++i; + } + return(0); +} diff --git a/uva_cpp_clean/12250/main.cpp b/uva_cpp_clean/12250/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4bdde0e1219facb9120b9b4d17eb478b6adcf646 --- /dev/null +++ b/uva_cpp_clean/12250/main.cpp @@ -0,0 +1,39 @@ +// 12250 - Language Detection + +#include +#include + +using namespace std; + +int main(){ + + string S; + int c = 1; + while(cin >> S){ + if(S == "#") break; + if(S == "HELLO"){ + cout << "Case " << c << ": " << "ENGLISH" << endl; + c++; + }else if(S == "HOLA"){ + cout << "Case " << c << ": " << "SPANISH" << endl; + c++; + }else if (S == "HALLO"){ + cout << "Case " << c << ": " << "GERMAN" << endl; + c++; + }else if (S == "BONJOUR"){ + cout << "Case " << c << ": " << "FRENCH" << endl; + c++; + }else if (S =="CIAO"){ + cout << "Case " << c << ": " << "ITALIAN" << endl; + c++; + }else if (S =="ZDRAVSTVUJTE"){ + cout << "Case " << c << ": " << "RUSSIAN" << endl; + c++; + }else{ + cout << "Case " << c << ": " << "UNKNOWN" << endl; + c++; + } + } + + return 0; +} diff --git a/uva_cpp_clean/1226/1226-21.cpp b/uva_cpp_clean/1226/1226-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f47f23697d0f5ae5526373bf4b854535211c4be3 --- /dev/null +++ b/uva_cpp_clean/1226/1226-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1226 + Name: Numerical surprises + Problem: https://onlinejudge.org/external/12/1226.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, n; + char s[3000]; + scanf("%d", &T); + while (T--) { + scanf("%d%s", &n, s); + int r = 0; + for (int i=0; s[i]; i++) + r = (r*10 + s[i] - '0') % n; + + printf("%d\n", r); + } +} diff --git a/uva_cpp_clean/12279/12279-21.cpp b/uva_cpp_clean/12279/12279-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6caefc11dfb646f6bce613ad39ecc80b0ce81460 --- /dev/null +++ b/uva_cpp_clean/12279/12279-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12279 + Name: Emoogle Balance + Problem: https://onlinejudge.org/external/122/12279.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n, x, cse=1; + while(cin>>n && n) { + int c = 0; + for (int i=0; i>x; + if (x==0) c++; + } + + printf("Case %d: %d\n", cse++, n - 2*c); + } +} diff --git a/uva_cpp_clean/12279/12279-9.cpp b/uva_cpp_clean/12279/12279-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be9e4d677c4a44fe937f57e8f8b140175473762e --- /dev/null +++ b/uva_cpp_clean/12279/12279-9.cpp @@ -0,0 +1,16 @@ +#include + +int main(){ + int n, a, b, i, x, j = 1; + while(scanf("%d", &n), n){ + a = b = 0; + for(i = 0; i < n; i++){ + scanf("%d", &x); + if(x) a++; + else b++; + } + printf("Case %d: %d\n", j, a - b); + j++; + } + return(0); +} diff --git a/uva_cpp_clean/12279/main.cpp b/uva_cpp_clean/12279/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6120ab9212d1e5219b3c71b172f4777586fb4278 --- /dev/null +++ b/uva_cpp_clean/12279/main.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +int main(){ + + int n, x, res, c = 1; + cin >> n; + while(n!=0){ + res = 0; + for(n; n>0 ; n--){ + cin >> x; + if(x == 0) res--; + else res++; + } + cout << "Case " << c << ": " << res << endl; + c++; + cin >> n; + } + cout << endl; + + return 0; +} diff --git a/uva_cpp_clean/12283/12283-19.cpp b/uva_cpp_clean/12283/12283-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe15c1d5bf5940e93ac42f700009f48878de15bd --- /dev/null +++ b/uva_cpp_clean/12283/12283-19.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +int N, M; +int dp[101][101], costumes[100]; + +int proc(int i, int j) { + if (j < i) return 0; + if (dp[i][j] == -1) { + int val = 1 + proc(i, j-1); + for (int k = i; k < j; k++) { + if (costumes[k] == costumes[j]) val = min(val, proc(i, k) + proc(k+1, j-1)); + } + dp[i][j] = val; + } + return dp[i][j]; +} + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + cin >> N >> M; + for (int i = 0; i < N; i++) cin >> costumes[i]; + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + dp[i][j] = -1; + printf("Case %d: %d\n", c, proc(0, N-1)); + } + return 0; +} diff --git a/uva_cpp_clean/12289/12289-14.cpp b/uva_cpp_clean/12289/12289-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d52e753bb84316665076c6720ac7628f61439578 --- /dev/null +++ b/uva_cpp_clean/12289/12289-14.cpp @@ -0,0 +1,51 @@ +/*************************************************** + * Problem name : 12289 - One-Two-Three.cpp + * OJ : Uva + * Verdict : AC + * Date : 10.05.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define MAX 100000 +using namespace std; +typedef long long ll; +int main () { + string s; + int tc; + scanf("%d", &tc); + for(int i = 1; i<=tc; i++){ + cin >> s; + if(s.size() == 5){ + printf("3\n"); + } + else{ + int t = 0; + if(s[0] == 'o') t++; + if(s[1] == 'n') t++; + if(s[2] == 'e') t++; + if(t>=2){ + printf("1\n"); + } + else{ + printf("2\n"); + } + } + } + return 0; +} + diff --git a/uva_cpp_clean/12289/12289-18.cpp b/uva_cpp_clean/12289/12289-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a3a7af254764335bbb5685869a8772b8296f6034 --- /dev/null +++ b/uva_cpp_clean/12289/12289-18.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int n, cont; + scanf("%d", &n); + cin.get(); + while(n--){ + char input[200]; + scanf("%s", &input); + if(strlen(input) == 3){ + cont = 0; + if(input[0] == 'o') cont++; + if(input[1] == 'n') cont++; + if(input[2] == 'e') cont++; + if(cont >= 2) printf("1\n"); + else printf("2\n"); + }else printf("3\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12289/12289-21.cpp b/uva_cpp_clean/12289/12289-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f0ee6afa7cd0ee6cdd612913e0a6e9de111ec8e --- /dev/null +++ b/uva_cpp_clean/12289/12289-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12289 + Name: One-Two-Three + Problem: https://onlinejudge.org/external/122/12289.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +int diff(char *w1, char *w2) { + int r=0; + for(int i=0; i<3; i++) + if (w1[i] != w2[i]) + r++; + return r; +} + +int main() { + int n; + cin>>n; + while(n--) { + char word[10]; + cin>>word; + if (strlen(word)==5) + cout<<"3\n"; + else if (diff(word, "one") < diff(word, "two")) + cout<<"1\n"; + else + cout<<"2\n"; + } +} diff --git a/uva_cpp_clean/12289/12289-5.cpp b/uva_cpp_clean/12289/12289-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..28252476e8b830b478f12e7502c131124e093e18 --- /dev/null +++ b/uva_cpp_clean/12289/12289-5.cpp @@ -0,0 +1,28 @@ +# include +# include +# include + +int main() +{ + int n; + char a[7],chr; + scanf("%d%c",&n,&chr); +while(n--) +{ + + gets(a); + if(strlen(a)==5) + printf("3\n"); + else if((a[0]=='o' && a[1]=='n')|| + (a[0]=='o' && a[1]=='e')|| + (a[1]=='n' && a[2]=='e')|| + (a[0]=='o' && a[2]=='e')|| + (a[0]=='o' && a[2]=='n')) + printf("1\n"); + else + printf("2\n"); + +} + + return 0; +} diff --git a/uva_cpp_clean/12289/12289-9.cpp b/uva_cpp_clean/12289/12289-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f27c0aa1cb118ad1c56827aceb5c2975c042a9c --- /dev/null +++ b/uva_cpp_clean/12289/12289-9.cpp @@ -0,0 +1,22 @@ +#include +#include + +int main(){ + int n, i, cont1, cont2, tam; + char a[6]; + scanf("%d", &n); + while(n--){ + scanf("%s", a); + tam = strlen(a); + if(tam == 3){ + cont1 = cont2 = 0; + if(a[0] == 'o') cont1++; + if(a[1] == 'n') cont1++; + if(a[2] == 'e') cont1++; + if(cont1 >= 2) printf("1\n"); + else printf("2\n"); + } + else if(tam == 5) printf("3\n"); + } + return(0); +} diff --git a/uva_cpp_clean/12289/main.cpp b/uva_cpp_clean/12289/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..671030bbcf9301ea70923cb0bedd16be2c8a0233 --- /dev/null +++ b/uva_cpp_clean/12289/main.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +using namespace std; + +int main(){ + + int num; + cin >> num; + for(num; num > 0 ; num--){ + int cont1 = 0; + int cont2 = 0; + vector nums = {"one","two","three"}; + string palabra; + cin >> palabra; + int l = palabra.length(); + if(l == 5) cout << 3 << endl; + else{ + if(palabra[0] == 'o') cont1++; + else cont2++; + if(palabra[1] == 'w') cont2++; + else cont1 ++; + if(palabra[2] == 'e') cont1++; + else cont2++; + if(cont1 > cont2) cout << 1 << endl; + else cout << 2 << endl; + } + } + + return 0; +} diff --git a/uva_cpp_clean/1229/1229-9.cpp b/uva_cpp_clean/1229/1229-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4facee397014823cfdc1ac1b7c4f15789f4eb2f7 --- /dev/null +++ b/uva_cpp_clean/1229/1229-9.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +const int MAX_V = 3010; + +int V, ct, dfsCounter, numSCC, dfs_low[MAX_V], dfs_num[MAX_V]; +bool in_stack[MAX_V], vis[MAX_V]; +string line, word_u, word_v; +stringstream ss; +vector AdjList[MAX_V], SCC[MAX_V], path; +vector ans; +map mp; +map inv_mp; + +void dfs1(int u){ + dfs_num[u] = dfs_low[u] = ++dfsCounter; + in_stack[u] = true; + path.push_back(u); + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not dfs_num[v] ) dfs1(v); + if( in_stack[v] ) dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + if( dfs_low[u] == dfs_num[u] ){ + int v; + do{ + v = path.back(); path.pop_back(); + in_stack[v] = false; + SCC[numSCC].push_back(v); + }while( u != v ); + numSCC++; + } +} + +void TarjanSCC(){ + V = ct; + fill(dfs_num, dfs_num + V, 0); + fill(in_stack, in_stack + V, false); + dfsCounter = numSCC = 0; + for(int u = 0; u < V; u++) + if( not dfs_num[u] ) dfs1(u); +} + +void dfs2(int u){ + if( vis[u] ) return; + vis[u] = true; + ans.push_back( inv_mp[u] ); + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not vis[v] ) dfs2(v); + } +} + +void solve(){ + TarjanSCC(); + fill(vis, vis + V, false); + for(int u = 0; u < numSCC; u++) + if( SCC[u].size() > 1 ) dfs2(SCC[u].front()); + sort(ans.begin(), ans.end()); + printf("%d\n", (int)ans.size()); + for(int i = 0, sz = ans.size(); i < sz; i++) + printf( i == sz - 1 ? "%s\n" : "%s ", ans[i].c_str()); +} + +int counter(string& x){ + if( mp.find(x) != mp.end() ) return mp[x]; + inv_mp[ct] = x; + return mp[x] = ct++; +} + +void read(){ + for(int i = 0; i < V; i++){ + getline(cin, line); + ss.clear(); + ss << line; + ss >> word_u; + int u = counter(word_u); + while(ss >> word_v) AdjList[u].push_back(counter(word_v)); + } +} + +void clear(){ + ct = 0; + mp.clear(); + ans.clear(); + for(int u = 0; u < V; u++) AdjList[u].clear(); + for(int u = 0; u < numSCC; u++) SCC[u].clear(); +} + +int main(){ + while(scanf("%d\n", &V), V){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/12290/12290-9.cpp b/uva_cpp_clean/12290/12290-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0ff20fdf7ec8c5f5708ccf79b3f992ea48543d7 --- /dev/null +++ b/uva_cpp_clean/12290/12290-9.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int n, m, k, ct, t, s, pib; +bool v[500000]; + +bool seven(int num){ + while(num){ + if(num % 10 == 7) + return true; + num /= 10; + } + return false; +} + +int main(){ + for(int i = 0; i < 500000; i++) + if(!(i % 7) || seven(i)) + v[i] = true; + while(scanf("%d %d %d", &n, &m, &k), n | m | k){ + ct = t = 1, s = 0; + while(true){ + if(ct == m && v[t]) + s++; + if(s == k){ + printf("%d\n", t); + break; + } + if(ct == n) + pib = -1; + else if(ct == 1) + pib = 1; + t++; + ct += pib; + } + } + return(0); +} diff --git a/uva_cpp_clean/12291/12291-21.cpp b/uva_cpp_clean/12291/12291-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a19d330765afc70a09fe98f3f367e9f1edb54818 --- /dev/null +++ b/uva_cpp_clean/12291/12291-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12291 + Name: Polyomino Composer + Problem: https://onlinejudge.org/external/122/12291.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool findStart(char M[20][20], int n, int &i, int &j) { + for (i=0; i=n || j<0 || j>=n ? '.' : L[i][j]; +} + + +bool check() { + int i1, j1, i2, j2; + bool bs = findStart(S, m, i1, j1); + for (int k=0; k<2; ++k) + if (findStart(L, n, i2, j2) != bs) + return 0; + + else { + int di = i2-i1, dj = j2-j1; + for (int i=0; i> n >> m && (n || m)) { + for (int i=0; i> L[i]; + for (int i=0; i> S[i]; + + cout << (check() ? "1\n" : "0\n"); + } +} diff --git a/uva_cpp_clean/12291/12291-9.cpp b/uva_cpp_clean/12291/12291-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42e5901e1ee1c53d2e0d52f83beb24b0e43cdf71 --- /dev/null +++ b/uva_cpp_clean/12291/12291-9.cpp @@ -0,0 +1,79 @@ +#include + +#define SIZE 15 + +using namespace std; + +int n, m, lx, ly, rx, ry, row, column, r, c; +bool ans; +char u[SIZE][SIZE], v[SIZE][SIZE], w[SIZE][SIZE], x[SIZE][SIZE], y[SIZE][SIZE]; + +bool simulate2(int a,int b){ + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + y[i][j] = x[i][j]; + for(int i = 0; i <= row; i++) + for(int j = 0; j <= column; j++) + if(w[i][j] == '*'){ + if(y[a + i][b + j] == '.') return false; + y[a + i][b + j] = '.'; + } + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + if(y[i][j] == '*') return false; + return true; +} + +bool check(){ + for(int i = 0; i < r; i++) + for(int j = 0; j < c; j++) + if(simulate2(i, j)) return true; + return false; +} + +bool simulate1(int a, int b){ + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + x[i][j] = u[i][j]; + for(int i = 0; i <= row; i++) + for(int j = 0; j <= column; j++) + if(w[i][j] == '*'){ + if(x[a + i][b + j] == '.') return false; + x[a + i][b + j] = '.'; + } + return check(); +} + +void findLimits(){ + for(int j = 0; j < m; j++) + for(int i = 0; i < m; i++) + if(v[i][j] == '*') lx = j, i = j = m; + for(int i = 0; i < m; i++) + for(int j = 0; j < m; j++) + if(v[i][j] == '*') ly = i, i = j = m; + for(int j= m - 1; j >= 0; j--) + for(int i= m - 1; i >= 0; i--) + if(v[i][j] == '*') rx = j, i = j = -1; + for(int i= m - 1; i >= 0; i--) + for(int j= m - 1; j >= 0; j--) + if(v[i][j] == '*') ry = i, i = j = -1; +} + +int main(){ + while(scanf("%d %d\n", &n, &m), n | m){ + ans = false; + for(int i = 0; i < n; i++) scanf("%s", u[i]); + for(int i = 0; i < m; i++) scanf("%s", v[i]); + findLimits(); + for(int i = ly; i <= ry; i++) + for(int j = lx; j <= rx; j++) + w[i - ly][j - lx] = v[i][j]; + row = ry - ly, column = rx - lx; + r = n - row, c = n - column; + for(int i = 0; i < r; i++) + for(int j = 0; j < c; j++) + if(simulate1(i, j)) ans = true, i = j = n; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/12292/12292-21.cpp b/uva_cpp_clean/12292/12292-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e43447296259caf98839e9c8fa91abded8838083 --- /dev/null +++ b/uva_cpp_clean/12292/12292-21.cpp @@ -0,0 +1,123 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12292 + Name: Polyomino Decomposer + Problem: https://onlinejudge.org/external/122/12292.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int Px[23], Py[23], sz, cnt, n; +char M[17][17]; + +// check & fill +bool check() { + char ch = 'A'; + int oy = Py[0], + ox = Px[0]; + + for (int i=0; i=0 && ii=0 && jj= 'A') + M[i][j] = '*'; + return 0; +} + + +// check if pattern is connected +bool used[23][23]; +int dfs(int i, int j) { + if (!used[i][j]) return 0; + used[i][j] = 0; + + int r = 1; + if (i) r += dfs(i-1, j); + if (j) r += dfs(i, j-1); + if (i= sz; + + do { + int k = 0; + memset(used, 0, sizeof(used)); + for (int j=0; j> n && n) { + int snt = 0; + for (int i=0; i> M[i]; + for (int j=0; j +int main() +{ + long long n; + while(scanf("%lld",&n)==1) + { + if(n==0) + break; + else + { + int p=1; + while(p +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#define MP make_pair +#define PB push_back +#define foreach(e,x) for(__typeof(x.begin()) e=x.begin(); e!=x.end(); ++ e) +using namespace std; + +typedef pair PII; +typedef long long LL; +typedef unsigned long long ULL; +typedef long double LD; + +int N; +char buf[20]; + +void solve() +{ + scanf("%d%s", &N, buf); + if (buf[0] == 'A') { + -- N; + if (N % 3 == 0) + puts("Bob"); + else + puts("Alice"); + } else { + if (N % 3 == 0) + puts("Alice"); + else + puts("Bob"); + } +} + +int main() +{ + int T; scanf("%d", &T); + for(int i = 1; i <= T; ++ i) { + printf("Case %d: ", i); + solve(); + } + return 0; +} diff --git a/uva_cpp_clean/12293/12293-9.cpp b/uva_cpp_clean/12293/12293-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9aad26b64d15bf184892df436e4dab12b7bf79ab --- /dev/null +++ b/uva_cpp_clean/12293/12293-9.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +namespace Pattern { + int minVal(int, int); + int maxVal(int, int); + int minimax(int); + void getAnswers(int); +} + +namespace Pattern { + + const int WIN = 1; + const int FAIL = -1; + + int minVal(int _max, int _min) { + if (_max == 1 and _min == 1) return WIN; + int ret = WIN; + for (int it = 1; 2 * it <= _max and ret != FAIL; it++) + ret = min(ret, maxVal(max(it, _max - it), min(it, _max - it))); + return ret; + } + + int maxVal(int _max, int _min) { + if (_max == 1 and _min == 1) return FAIL; + int ret = FAIL; + for (int it = 1; 2 * it <= _max and ret != WIN; it++) + ret = max(ret, minVal(max(it, _max - it), min(it, _max - it))); + return ret; + } + + int minimax(int k) { + return maxVal(k, 1) == WIN; + } + + void getAnswers(int limit) { + for (int k = 2; k <= limit; k++) + printf("%3d : %s\n", k, minimax(k) ? "Alice" : "Bob"); + } + +} + +const int Alice = 0; +const int Bob = 1; + +int n; +map winner; + +void fillBobPositions() { + for (long long k = 3; k <= 1e9; k = k * 2 + 1) winner[k] = Bob; +} + + +int main() { + //Pattern::getAnswers(35); + fillBobPositions(); + while (scanf("%d", &n), n) puts(winner[n] == Alice ? "Alice" : "Bob"); + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/123/123-18.cpp b/uva_cpp_clean/123/123-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2edbb1cd3d08936b198dbc03db06c7547b3cc476 --- /dev/null +++ b/uva_cpp_clean/123/123-18.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +using namespace std; + +vector ignored; + +bool isntIgnored(string word){ + for (int i = 0; i < ignored.size(); i++) { + if (ignored.at(i).c_str() == word) return false; + } + return true; +} + +string toUpper(string word){ + for (int i = 0; i < word.size(); i++) word[i] = toupper(word[i]); + return word; +} + +string toLower(string word){ + for (int i = 0; i < word.size(); i++) + if (word[i] >= 'A' && word[i] <= 'Z') word[i] += 32; + return word; +} + +int main(){ + multimap KWIC; + string line, word; + while (true) { + cin >> line; + if (line == "::") break; + ignored.push_back(line); + } + getline(cin, line); + while (getline(cin, line)){ + line = toLower(line); + for (int i = 0; i < line.size(); i++){ + if (isalpha(line[i])){ + string word = ""; + while (isalpha(line[i])){ + word += line[i]; + i++; + } + if (isntIgnored(word)){ + string auxLine = line; + word = toUpper(word); + auxLine = auxLine.replace(i - word.size(), word.size(), word); + KWIC.insert(make_pair(word, auxLine)); + } + } + } + } + for (auto iterator = KWIC.begin(); iterator != KWIC.end(); iterator++){ + cout << iterator->second << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/123/123-21.cpp b/uva_cpp_clean/123/123-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d673ce86cdf0c71ed4bab4e8f31636de70ebb5f4 --- /dev/null +++ b/uva_cpp_clean/123/123-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 123 + Name: Searching Quickly + Problem: https://onlinejudge.org/external/1/123.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +map ig; +map > id; + +int main(){ + char ln[500], ln2[500], ln3[500]; + while (cin.getline(ln, 500) && ln[0]!=':') + ig[ln] = 1; + + while (cin.getline(ln, 500)) { + for (int i=0; !i || ln[i-1]; i++) + ln[i] = ln2[i] = tolower(ln[i]); + + for (char *w=strtok(ln, " "); w; w=strtok(0, " ")) + if (!ig[w]) { + strcpy(ln3, ln2); + for (int i=w-ln; ln[i]; i++) + ln3[i] = toupper(ln3[i]); + + id[w].push_back(ln3); + } + } + + for (auto &it: id) + for (string ln: it.second) + cout << ln << endl; +} diff --git a/uva_cpp_clean/123/123-9.cpp b/uva_cpp_clean/123/123-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eccbd05e630097ac860be7a3cf5886206ddad09f --- /dev/null +++ b/uva_cpp_clean/123/123-9.cpp @@ -0,0 +1,77 @@ +#include + +#define MAX_WORD 60 +#define MAX_TITLE 210 +#define SIZE 10010 + +using namespace std; + +struct KWIC{ + char key[SIZE], line[SIZE]; + int idx_n, idx_m; +}aux; + +int n, m, it; +char word[MAX_WORD][SIZE], titles[MAX_TITLE][SIZE], s[SIZE], part[SIZE][SIZE], *p; +vector v; + +void toLower(){ + for(int i = 0; i < m; i++) + for(int j = 0; titles[i][j]; j++) titles[i][j] = tolower(titles[i][j]); +} + +void cpy(char* x, char* y){ + int pos; + for(pos = 0; y[pos]; pos++) x[pos] = tolower(y[pos]); + x[pos] = '\0'; +} + +bool isNotIgnored(char* x){ + for(int i = 0; i < n; i++) if(strcmp(word[i], x) == 0) return false; + return true; +} + +void toUpper_nth_word(int k){ + int pos = 0; + for(int i = 0; s[i]; i++){ + if(s[i] != ' '){ + pos++; + if(pos == k){ + while(s[i] && s[i] != ' ') s[i] = toupper(s[i]), i++; + break; + } + while(s[i] && s[i] != ' ') i++; + } + } +} + +bool cmp(KWIC x, KWIC y){ + if(strcmp(x.key, y.key)!=0) return (strcmp(x.key, y.key) < 0); + if(x.idx_m != y.idx_m) return (x.idx_m < y.idx_m); + return (x.idx_n < y.idx_n); +} + +int main(){ + while(scanf("%s", word[n]), strcmp(word[n], "::") != 0) n++; + while(cin.getline(titles[m], SIZE)) m++; + toLower(); + for(int i = 0; i < m; i++){ + strcpy(s, titles[i]); + p = strtok(s, " "), it = 0; + while(p) cpy(part[it++], p), p = strtok(NULL, " "); + for(int j = 0; j < it; j++) + if(isNotIgnored(part[j])){ + strcpy(s, titles[i]); + toUpper_nth_word(j + 1); + strcpy(aux.key, part[j]); + strcpy(aux.line, s); + aux.idx_n = j; + aux.idx_m = i; + v.push_back(aux); + } + } + sort(v.begin(), v.end(), cmp); + for(int i = 0; i < v.size(); i++) + printf("%s\n", v[i].line); + return(0); +} diff --git a/uva_cpp_clean/1230/1230-19.cpp b/uva_cpp_clean/1230/1230-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f9f8d6ec672ddf204fa1672229033051934ec78 --- /dev/null +++ b/uva_cpp_clean/1230/1230-19.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +long long power(long long b, long long e, long long mod) { + if (e == 0) return 1%mod; + if (e == 1) return b%mod; + long long aux = power(b, e/2, mod); + aux = (aux*aux)%mod; + if (e%2) aux = (aux*(b%mod))%mod; + return aux; +} + +int main() { + int m; + scanf("%d", &m); + while (m--) { + unsigned int x, y, n; + scanf("%d %d %d", &x, &y, &n); + printf("%lld\n", power(x, y, n)); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1230/1230-21.cpp b/uva_cpp_clean/1230/1230-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..309f7177802a34752658f92b26155c496c4a9343 --- /dev/null +++ b/uva_cpp_clean/1230/1230-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1230 + Name: MODEX + Problem: https://onlinejudge.org/external/12/1230.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int powmod(int a, int b, int m) { + if (!b) return 1; + if (b==1) return a%m; + + long long r = powmod(a, b>>1, m); + r = (r * r) % m; + if (b&1) r = (r * a) % m; + return r; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, x, y, n; + cin >> T; + while (T-- && cin >> x >> y >> n) + cout << powmod(x, y, n) << '\n'; +} diff --git a/uva_cpp_clean/1230/1230-5.cpp b/uva_cpp_clean/1230/1230-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..97ab0b2c6213e9d1f5bc0b143cd9945b8d8f5311 --- /dev/null +++ b/uva_cpp_clean/1230/1230-5.cpp @@ -0,0 +1,21 @@ +#include +int main() +{ + long long t,x,y,m,r; + scanf("%lld",&t); + while(t--) + { + scanf("%lld %lld %lld",&x,&y,&m); + r=1; + while(y) + { + if(y%2!=0) + r=(r*x)%m; + x=(x*x)%m; + y/=2; + } + printf("%lld\n",r); + } + return 0; +} + diff --git a/uva_cpp_clean/1231/1231-21.cpp b/uva_cpp_clean/1231/1231-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6141b8a7d42ed6e53b378ece322daa3a587972df --- /dev/null +++ b/uva_cpp_clean/1231/1231-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1231 + Name: ACORN + Problem: https://onlinejudge.org/external/12/1231.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MAXTH 2143 +int M[MAXTH][MAXTH], DP[MAXTH][MAXTH], DPP[MAXTH], t, f; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int cse; + cin >> cse; + for (int i=0; i> t >> h >> f; + for (int i=0; i> sz; + int nj = 0, j; + for (int k=0; k> j; + M[i][j]++; + } + } + + DPP[0] = 0; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12319 + Name: Edgetown's Traffic Jams + Problem: https://onlinejudge.org/external/123/12319.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +inline int readchar(bool peek=0) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int r = *p; + if (!peek) ++p; + return r; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar(1))) { + r = (r<<3) + (r<<1) + ch-'0'; + readchar(); + } + + while (readchar(1) == ' ') + readchar(); + + return r; +} + +// ------------------------------------------------------------------- + + +#define MAXQS 128 +class FastQueue { + int data[MAXQS], l, r; + public: + FastQueue():l(0),r(0) {} + void clear() { l=r=0; } + bool empty() { return l==r; } + void push(int x) { data[r++] = x; r&=MAXQS-1; } + int pop(){ int result = data[l++]; l&=MAXQS-1; return result; } + int front() { return data[l]; } + int size() { return r>=l ? r-l : MAXQS-l+r; } +} q; + +void bfs(int n, int src, const vector adj[], int D[]) { + for (int i=0; i adj[2][143]; +int D[2][143][143]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1, n; (n=readUInt()); ++cse) { + for (int i=0; i a*D[0][i][j]+b) { + cout << "No\n"; + goto fin; + } + cout << "Yes\n"; + fin:; + } +} diff --git a/uva_cpp_clean/1232/1232-21.cpp b/uva_cpp_clean/1232/1232-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2bb0aea88b0993f99ff20dfbf31a8bcd6d61355d --- /dev/null +++ b/uva_cpp_clean/1232/1232-21.cpp @@ -0,0 +1,115 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1232 + Name: SKYLINE + Problem: https://onlinejudge.org/external/12/1232.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// -------------------------------------------------------------------------------------- + +int Y[1000043], MX[1000043], MN[1000043], mxn; +int assignRange(int rl, int rr, int val, int rest=-1, int sl=0, int sr=mxn, int si=1) { + if (val < MN[si]) return 0; + if (rl<=sl && rr>=sr && val >= MX[si]) + Y[si] = MX[si] = MN[si] = val; + if (Y[si] == val) + return max(min(rr, sr)-max(rl, sl), 0); + + if (rest == -1) rest = Y[si]; + if (rl>=sr || rr<=sl) { + if (rest != -1) + Y[si] = MX[si] = MN[si] = rest; + return 0; + } + + int id1 = si<<1, id2 = si<<1|1; + if (Y[si] != -1) + Y[id1] = Y[id2] = + MX[id1] = MX[id2] = + MN[id1] = MN[id2] = Y[si]; + + int mid = (sl + sr) >> 1; + int result = + assignRange(rl, rr, val, rest, sl, mid, id1) + + assignRange(rl, rr, val, rest, mid, sr, id2); + + if (Y[id1] == Y[id2] && Y[id1]!=-1) + Y[si] = MX[si] = MN[si] = Y[id1]; + + else { + Y[si] = -1; + MX[si] = max(MX[id1], MX[id2]); + MN[si] = min(MN[id1], MN[id2]); + } + return result; +} + +int getPoint(int ind, int sl=0, int sr=mxn, int si=1) { + if (Y[si]>=0) return Y[si]; + int mid = (sl + sr) >> 1; + return ind < mid + ? getPoint(ind, sl, mid, si<<1) + : getPoint(ind, mid, sr, si<<1|1); +} + + + +int L[100043], R[100043], H[100043]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + while (T--) { + int n = readUInt(); + int sum = mxn = Y[1] = MX[1] = MN[1] = 0; + for (int i=0; i +#include +#include +using namespace std; + +int main() { + int L, G; + priority_queue > q; + while (scanf("%d %d", &L, &G) && !(L == 0 && G == 0)) { + for (int i = 0; i < G; i++) { + int x, r; + scanf("%d %d", &x, &r); + pair g(-(x-r), -(x+r)); + q.push(g); + } + int cont = 0, x = 0; + bool stop = false; + while (x < L && !stop) { + if (q.empty() || x < -q.top().first) stop = true; + else { + cont++; + int ext = -q.top().second; + while (!q.empty() && x >= -q.top().first) { + if (-q.top().second > ext) ext = -q.top().second; + q.pop(); + } + x = ext; + } + } + while (!q.empty()) q.pop(); + if (stop) printf("-1\n"); + else printf("%d\n", G-cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12321/12321-21.cpp b/uva_cpp_clean/12321/12321-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2fdaeb9f0d96dfb64a3f8adc78647eabdc8efa7 --- /dev/null +++ b/uva_cpp_clean/12321/12321-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12321 + Name: Gas Stations + Problem: https://onlinejudge.org/external/123/12321.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +struct Range { + int s, e; + Range(int s, int e): s(s),e(e) {} + bool operator < (const Range& r) const { + return s < r.s; + } +}; + +int minchoose(const vector &vec, int len) { + int cnt=0, right=0; + for (int i=0; rightvec[bj].e) + bj = j; + + if (bj < 0) return -1; + right = vec[bj].e; + i = j; + } + return right < len ? -1 : cnt; +} + +int main(){ + int l, g, xi, ri; + while (cin>>l>>g && (l||g)) { + vector vec; + for (int i=0; i> xi >> ri; + vec.push_back(Range(xi-ri, xi+ri)); + } + sort(vec.begin(), vec.end()); + + int m = minchoose(vec, l); + if (m >= 0) m = g-m; + cout << m << endl; + } +} diff --git a/uva_cpp_clean/12321/12321-9.cpp b/uva_cpp_clean/12321/12321-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c13dd1925203bb03a5bce9add9e20edc0fd2107 --- /dev/null +++ b/uva_cpp_clean/12321/12321-9.cpp @@ -0,0 +1,28 @@ +#include + +#define SIZE 10010 + +using namespace std; + +int L, G, x, r, lo, ans, _max, j; +pair v[SIZE]; + +int main(){ + while(scanf("%d %d", &L, &G), L | G){ + for(int i = 0; i < G; i++) + scanf("%d %d", &x, &r), v[i] = make_pair(x - r, x + r); + sort(v, v + G); + lo = ans = 0; + for(int i = 0; i < G && lo < L; ++ans){ + _max = lo; + for(j = i; j < G && v[j].first <= lo; j++) + _max = max(_max, v[j].second); + lo = _max; + if(j == i) break; + i = j; + } + ans = (lo < L) ? -1 : G - ans; + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/12324/12324-21.cpp b/uva_cpp_clean/12324/12324-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f490b65762b427994c41649ed2a2c17b9fa463e8 --- /dev/null +++ b/uva_cpp_clean/12324/12324-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12324 + Name: Philip J. Fry Problem + Problem: https://onlinejudge.org/external/123/12324.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int X[143], Y[143], DP[143][143], n; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n && n) { + int mxr = 0; + for (int i=0; i> X[i] >> Y[i]; + mxr += Y[i]; + } + + memset(DP[n], 0, (n+1)*sizeof(int)); + for (int i=n-1; i>=0; --i) { + int nr = n-i-1; + DP[i][0] = DP[i+1][min(Y[i], nr)] + X[i]; + for (int r=min(mxr, n-i); r>0; --r) + DP[i][r] = min(DP[i+1][min(r+Y[i], nr)] + X[i], DP[i+1][min(r+Y[i]-1, nr)] + X[i]/2); + } + + cout << DP[0][0] << '\n'; + } +} diff --git a/uva_cpp_clean/1233/1233-21.cpp b/uva_cpp_clean/1233/1233-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..231f503b40cd287c855343598041a82dad2f3340 --- /dev/null +++ b/uva_cpp_clean/1233/1233-21.cpp @@ -0,0 +1,108 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1233 + Name: USHER + Problem: https://onlinejudge.org/external/12/1233.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------------- + +struct State { + int u, d; + State(int u, int d):u(u),d(d){} + bool operator < (const State &o) const { return d > o.d; } +}; + +vector adj[555]; +int D[555], W[555][555], S[555][555]; + +int main() { + ios_base::sync_with_stdio(0); + + int T = readUInt(); + for (int cse=1; cse<=T; ++cse) { + int c = readUInt(), + n = readUInt() + 1, + m = readUInt(); + + priority_queue q; + for (int i=0; i +#include +#include +using namespace std; + +typedef pair ic; + +int main() { + unsigned long long fact[21], k; + fact[0] = 1; + for (int i = 1; i < 21; i++) fact[i] = fact[i-1]*i; + ic p[21]; + bool mask[21]; + int T; + string s; + cin >> T; + for (int c = 1; c <= T; c++) { + cin >> s >> k; + for (int i = 0; i < s.length(); i++) { + p[i].second = s[i]; + mask[i] = false; + } + k--; + for (int i = 0; i < s.length(); i++) { + int j, f, last; + for (j = 0, f = 1, last = 0; j < s.length(); j++) { + if (!mask[j]) { + last = j; + if (fact[s.length()-(i+1)]*f > k) break; + f++; + } + } + mask[last] = true; + p[i].first = last+1; + k -= fact[s.length()-(i+1)]*(f-1); + } + sort(p, p + s.length()); + printf("Case %d: ", c); + for (int i = 0; i < s.length(); i++) printf("%c", p[i].second); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/1234/1234-10.cpp b/uva_cpp_clean/1234/1234-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de56b949a544337178526c23e554c7cd30646210 --- /dev/null +++ b/uva_cpp_clean/1234/1234-10.cpp @@ -0,0 +1,115 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e4+9; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; + +int maximumSpanningTree(vector> &edges) { + + + sort(edges.rbegin(), edges.rend()); + DSU dsu; + dsu.init(N); + + int ans=0; + + int weight; + int a,b; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + ans+=weight; + } + } + + return ans; + +} + +void test() { + + int n,m,a,b,w,sum=0; + cin >> n>>m; + + vector> arr; + + for(int i=0;i>a>>b>>w; + arr.push_back({w,a,b}); + sum+=w; + } + + + auto res = maximumSpanningTree(arr); + + cout<>t; + + while (t--) { + test(); + } + + cin>>t; + +} + diff --git a/uva_cpp_clean/1234/1234-21.cpp b/uva_cpp_clean/1234/1234-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..647e0f70b563053726884e4eefae4e456154617d --- /dev/null +++ b/uva_cpp_clean/1234/1234-21.cpp @@ -0,0 +1,131 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1234 + Name: RACING + Problem: https://onlinejudge.org/external/12/1234.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +inline char readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) + return EOF; + + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + // skip trailing precision/etc + while (ch!=' ' && ch!='\n') + ch = readchar(); + + return r; +} + +// ----------------------------------------------------------------- + +int par[10017]; +int find(int u) { return par[u]<0 ? u : par[u]=find(par[u]); } +bool join(int u, int v) { + if ((u=find(u)) != (v=find(v))) { + if (par[v] < par[u]) + swap(u, v); + + par[u] += par[v]; + par[v] = u; + return 1; + } + return 0; +} + +// --------------------------------------- + +#define MAXD 1007 +struct Pair { + int u, v; + Pair(int u, int v):u(u),v(v){} +}; + +int maxd, mind; +vector adj[MAXD]; +void qClear() { maxd = 0; mind = MAXD; } +bool qEmpty() { return maxd=MAXD || d<0) while(1); + + adj[d].push_back(Pair(u, v)); + if (d < mind) mind = d; + if (d > maxd) maxd = d; +} +int qPop(int &u, int &v) { + Pair &p = adj[maxd].back(); + u = p.u; v = p.v; + int d = maxd; + + adj[maxd].pop_back(); + while (!qEmpty() && adj[maxd].empty()) + --maxd; + + return d; +} + +// --------------------------------------- + +int main() { + int T = readInt(); + while (T--) { + int n = readInt(), + m = readInt(); + + qClear(); + memset(par, -1, n*sizeof(int)); + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 100010; + +struct Edge_{ + int u, v, w; + bool operator < (const Edge_& other){ + return ( w > other.w ); + } +}E[SIZE]; + +int tc, N, M, sum; + +int Kruskal(){ + UnionFind UF(N + 1); + int max_cost = 0, setSize = 1; + sort(E, E + M); + for(int it = 0; it < M and setSize != N; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ) + max_cost += E[it].w, setSize++, + UF.unionSet(E[it].u, E[it].v); + return max_cost; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + sum = 0; + scanf("%d %d", &N, &M); + for(int it = 0; it < M; it++){ + scanf("%d %d %d", &E[it].u, &E[it].v, &E[it].w); + sum += E[it].w; + } + printf("%d\n", sum - Kruskal()); + } + return(0); +} diff --git a/uva_cpp_clean/12342/12342-19.cpp b/uva_cpp_clean/12342/12342-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8ee1842d3c466bee0225ff0fbd869d4df391aab --- /dev/null +++ b/uva_cpp_clean/12342/12342-19.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; + +int tax(double k) { + double t = 0; + k -= 180000; + if (k > 0) t += (k <= 300000? k: 300000)*0.1, k -= 300000; + if (k > 0) t += (k <= 400000? k: 400000)*0.15, k -= 400000; + if (k > 0) t += (k <= 300000? k: 300000)*0.20, k -= 300000; + if (k > 0) t += k*0.25; + return ceil(t); +} + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + double k; + cin >> k; + int t = tax(k); + if (t != 0 && t < 2000) t = 2000; + printf("Case %d: %d\n", c, t); + } + return 0; +} diff --git a/uva_cpp_clean/12342/12342-21.cpp b/uva_cpp_clean/12342/12342-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7f310dd4854647048b4e97061d0d7a05323bdd9 --- /dev/null +++ b/uva_cpp_clean/12342/12342-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12342 + Name: Tax Calculator + Problem: https://onlinejudge.org/external/123/12342.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int X[] = { 180000, 300000, 400000, 300000 }, + Y[] = { 0, 10, 15, 20, 25 }; + +int main() { + long long T, n, t; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n; + + t = 0; + for (int i=0; i<4 && n>0; ++i) { + t += (n > X[i] ? X[i] : n) * Y[i]; + n -= X[i]; + } + + if (n > 0) + t += n * Y[4]; + + if (t > 0) { + t = ceil(t / 100.0); + if (t > 0 && t < 2000) + t = 2000; + } + + cout << "Case " << cse << ": " << t << endl; + } +} diff --git a/uva_cpp_clean/12342/12342-5.cpp b/uva_cpp_clean/12342/12342-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5c606749da5c828199b78feec36c4f143869ecc7 --- /dev/null +++ b/uva_cpp_clean/12342/12342-5.cpp @@ -0,0 +1,75 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define sort(v) sort(v.begin(),v.end()) +#define un(v) sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; + +int main() +{ + double k,res; + int t,no=0; + sc("%d",&t); + while(t--) + { + scanf("%lf",&k); + if(k<=180000) + { + printf("Case %d: 0\n",++no); + continue; + } + if(k<=480000) + res=((k-180000)*0.1); + else if(k<=880000) + res=30000+((k-480000)*0.15); + else if(k<=1180000) + res=90000+((k-880000)*0.20); + else + res=150000+((k-1180000)*0.25); + res=ceil(res); + if(res<2000) + printf("Case %d: 2000\n",++no); + else + printf("Case %d: %.lf\n",++no,res); + } + return 0; +} diff --git a/uva_cpp_clean/12345/12345-21.cpp b/uva_cpp_clean/12345/12345-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19d10cd4077bf92829be51c4c766a88207ba3886 --- /dev/null +++ b/uva_cpp_clean/12345/12345-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12345 + Name: Dynamic len(set(a[L:R])) + Problem: https://onlinejudge.org/external/123/12345.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int ids[1000001], icnt=1; +int getId(int x) { + if (!ids[x]) + ids[x] = icnt++; + return ids[x]; +} + +bool seen[100000]; +int X[50001]; +int main() { + char mq; + int n, m, l, r; + scanf("%d%d", &n, &m); + for (int i=0; i +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + __builtin_popcountll(); +*/ + +int n,m; +int fi[22],ci[22]; +ll v,t; + +map,ll> dp; + +ll solve(int i,ll cost,ll water) { + if(i== n) { + if(water*t >= v) return cost; + return 1e9; + } + + if(dp.count({i,water})) { + return dp[{i,water}]; + } + + ll &ans=dp[{i,water}]; + ans =1e9; + ans = min(ans,solve(i+1,cost+ci[i],water+fi[i])); + ans = min(ans, solve(i+1,cost , water)); + + return ans; +} + + +void solveit() { + + int i; + cin>>n; + + for(i=0;i>fi[i]>>ci[i]; + } + cin>>m; + for(i=1;i<=m;i++) { + dp.clear(); + cin>>v>>t; + ll ans = solve(0,0,0); + cout<<"Case "<>t; + while (t--) { + solveit(); + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/12346/12346-21.cpp b/uva_cpp_clean/12346/12346-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15214af63039d300da137211e916e1f85b7af930 --- /dev/null +++ b/uva_cpp_clean/12346/12346-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12346 + Name: Water Gate Management + Problem: https://onlinejudge.org/external/123/12346.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + + +typedef long long int Int; +Int F[23], C[23], S[23], v, t, n, best; +void bt(int idx, Int cost, Int sum) { + if (cost >= best || (sum+S[idx])*t < v) return; + if (sum*t >= v) { best = cost; return; } + if (idx == n) return; + + bt(idx+1, cost + C[idx], sum + F[idx]); + bt(idx+1, cost, sum); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + cin >> n; + for (int i=0; i> F[i] >> C[i]; + + for (int i=n-1; i>=0; --i) + S[i] = S[i+1] + F[i]; + + int T; cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> v >> t; + best = INF; + bt(0, 0, 0); + + cout << "Case " << cse << ": "; + if (best == INF) cout << "IMPOSSIBLE\n"; + else cout << best << '\n'; + } +} diff --git a/uva_cpp_clean/12347/12347-21.cpp b/uva_cpp_clean/12347/12347-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a4565d68810331e59f72d8cb96aea019ec42ca1 --- /dev/null +++ b/uva_cpp_clean/12347/12347-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12347 + Name: Binary Search Tree + Problem: https://onlinejudge.org/external/123/12347.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[10143], P[1000143], n; + for (n=0; cin >> X[n]; ++n) + P[X[n]] = n; + + sort(X, X + n); + + stack st; + for (int i=0; i P[X[i]]) { + cout << st.top() << endl; + st.pop(); + } + + if (i < n-1 && P[X[i]] > P[X[i+1]]) + cout << X[i] << endl; + else + st.push(X[i]); + } + + while (!st.empty()) { + cout << st.top() << endl; + st.pop(); + } +} diff --git a/uva_cpp_clean/12347/12347.cpp b/uva_cpp_clean/12347/12347.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93797a73b3858675f59092945dfd67ef61134d68 --- /dev/null +++ b/uva_cpp_clean/12347/12347.cpp @@ -0,0 +1,49 @@ +// 02nd SEP 2016 +// uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3769 +#include +#include +#include +#include + +using namespace std; + +map> t; + +void print(int n) { + if (n == -1) return; + print(t[n][0]); + print(t[n][1]); + cout << n << '\n'; + +} + +void build(int n, int p, int i) { + if(n == -1) { + t[p][i>=p] = i; + return; + } + if (i >= n) + build(t[n][1], n, i); + else + build(t[n][0], n, i); + +} + +int main() { + vector a; + long input; + while (scanf("%d", &input) != EOF) + a.push_back(input); + + for (int i : a) { + t[i].push_back(-1); + t[i].push_back(-1); + } + + for (int i = 1; i < a.size(); ++i) + build(a[0], 0, a[i]); + + print(a[0]); + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12348/12348-21.cpp b/uva_cpp_clean/12348/12348-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5daa78121cc71512fdf59d5415281e02cf846ec9 --- /dev/null +++ b/uva_cpp_clean/12348/12348-21.cpp @@ -0,0 +1,78 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12348 + Name: Fun Coloring + Problem: https://onlinejudge.org/external/123/12348.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +// UVa has issue with this solution, but it looks right based on uDebug +// https://www.udebug.com/UVa/12348 + +int bitcount(int i) { + i = i - ((i >> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >> 2) & 0x33333333); + i = (i + (i >> 4)) & 0x0F0F0F0F; + return (i * 0x01010101) >> 24; +} + +int n, m; +set sets; +bool check_permutation(int perm) { + for (int s: sets) { + int reds = s & perm; + // no reds, or all reds + if (!reds || reds == s) { + return false; + } + } + return true; +} + +bool permutation_exists() { + for (int perm=(1<=0; --perm) { + if (check_permutation(perm)) { + return true; + } + } + return false; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + while(T--) { + cin >> n >> m; + string ln; + getline(cin, ln); + + sets.clear(); + for (int i=0; i> x; ) { + v |= 1 << (x-1); + } + + // more than one constraint + if (bitcount(v) > 1) { + sets.insert(v); + } + } + + cout << (permutation_exists() ? 'Y' : 'N'); + } + cout.flush(); +} diff --git a/uva_cpp_clean/1235/1235-10.cpp b/uva_cpp_clean/1235/1235-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6057b88e1d31b6c4803d5769361d7984f14334c --- /dev/null +++ b/uva_cpp_clean/1235/1235-10.cpp @@ -0,0 +1,145 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +const int N= (int)1e6+5; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + int count(int u) { + return sz[find(u)]; + } +}; + +// {weight, a, b} +int minimumSpanningTree(vector> &edges) { + + bool onezero=1; + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + int ans = 0,a,b,weight; + //cout<<"my path here \n"; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if(a==0 || b==0) { + if(onezero) onezero=0; + else continue; + } + +// cout<>n; + + vector arr; + arr.push_back("0000"); + + string s; + for(int i=0;i>s; + arr.push_back(s); + } + + ///{w,i,j} + vector> grap; + + for(int i=0;i<=n;i++) { + for(int j=i+1;j<=n;j++) { + grap.push_back({minLen(arr[i],arr[j]),i,j}); + } + } + +// cout<<"my grap\n"; +// for(auto xx:grap) { +// cout<(xx)<<" "<(xx)<<" "<(xx)<<"\n"; +// } + +// cout<<"\n"; + + auto res = minimumSpanningTree(grap); + cout<>t; + + while (t--) { + test(); + } + +} + diff --git a/uva_cpp_clean/1235/1235-19.cpp b/uva_cpp_clean/1235/1235-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c78fb62e86d08e99119bf47d27ce8615f1a7099 --- /dev/null +++ b/uva_cpp_clean/1235/1235-19.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair roll; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +int dist(int k1, int k2) { + int d = 0; + for (int i = 0; i < 4; i++) { + int mini = min(k1%10, k2%10), maxi = max(k1%10, k2%10); + d += min(maxi-mini, 10 + mini - maxi); + k1 /= 10; k2 /= 10; + } + return d; +} + +priority_queue pq; +vi v; + +int main() { + int cases; + cin >> cases; + while (cases--) { + v.clear(); + int n; + cin >> n; + for (int i = 0; i < n; i++) { + int k; + cin >> k; + v.push_back(k); + for (int j = 0; j < v.size() - 1; j++) { + roll l; l.second.first = j; l.second.second = i; + l.first = -dist(k, v[j]); + pq.push(l); + } + } + ufds u(n + 1); + int d = 0; + while (!pq.empty()) { + if (!u.isSameSet(pq.top().second.first, pq.top().second.second)) { + d -= pq.top().first; + u.unionSet(pq.top().second.first, pq.top().second.second); + } + pq.pop(); + } + int m = 50; + for (int i = 0; i < v.size(); i++) m = min(m, dist(0, v[i])); + d += m; + printf("%d\n", d); + } + return 0; +} diff --git a/uva_cpp_clean/1235/1235-21.cpp b/uva_cpp_clean/1235/1235-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32d02a6e7ec4db35265aa391df5e6e3464017203 --- /dev/null +++ b/uva_cpp_clean/1235/1235-21.cpp @@ -0,0 +1,128 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1235 + Name: Anti Brute Force Lock + Problem: https://onlinejudge.org/external/12/1235.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +char buf[50]; +void print(long long x) { + int i=48, neg=0; + buf[i--] = '\n'; + if (!x) buf[i--] = '0'; + for (; x; x/=10) + buf[i--] = x%10 + '0'; + + fwrite_unlocked(buf+i+1, 1, 48-i, stdout); +} + +// --------------------------------------------------------------------------- +#define MAXN 501 +int C[MAXN], ids[10000], gid[MAXN]; +int abs(int x) { return x<0 ? -x : x; } +int dist(int i, int j) { + int x = C[i], y = C[j], + d = 0; + for (int k=0; k<4; ++k) { + int t = abs(x%10 - y%10); + d += min(t, 10-t); + x /= 10; + y /= 10; + } + return d; +} + +int find(int u) { return u == gid[u] ? u : gid[u] = find(gid[u]); } +void join(int u, int v) { gid[find(u)] = find(v); } + +// ---- + +int deg[40], mindeg; +struct Pair { int i, j; } adj[40][MAXN*MAXN]; +void qPush(int i, int j) { + int d = dist(i, j); + adj[d][deg[d]].i = i; + adj[d][deg[d]].j = j; + ++deg[d]; + if (d < mindeg) mindeg = d; +} + +bool qEmpty() { return mindeg==40; } + +int qPop(int &u, int &v) { + int d = mindeg; + Pair &p = adj[d][--deg[d]]; + while (mindeg<40 && !deg[mindeg]) ++mindeg; + + u = p.i; + v = p.j; + return d; +} + +// ---- + +int main() { + int T = readUInt(); + while (T--) { + mindeg = 40; + memset(deg, 0, sizeof(deg)); + + int n = readUInt(); + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +const int SIZE = 510; +int tc, N, V[SIZE]; +vector < pair > EdgeList; + +int rolls(int num1, int num2){ + int moves = 0; + for(int it = 0; it < 4; it++){ + int x = num1 % 10, y = num2 % 10; + num1 /= 10, num2 /= 10; + moves += min( abs(x - y), 10 - abs(x - y) ); + } + return moves; +} + +int Kruskal(){ + UnionFind UF(N); + sort(EdgeList.begin(), EdgeList.end()); + int min_cost = INT_MAX, setSize = 1; + for(int it = 0; it < N; it++) min_cost = min(min_cost, rolls(0, V[it])); + for(int it = 0; it < EdgeList.size(); it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + min_cost += front.first, setSize++, + UF.unionSet(front.second.first, front.second.second); + if( setSize == N ) break; + } + return min_cost; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + EdgeList.clear(); + scanf("%d", &N); + for(int it = 0; it < N; it++) + scanf("%d", V + it); + for(int i = 0; i < N; i++) + for(int j = i + 1; j < N; j++) + EdgeList.push_back( make_pair( rolls(V[i], V[j]), ii(i, j) ) ); + printf("%d\n", Kruskal()); + } + return(0); +} diff --git a/uva_cpp_clean/12356/12356-11.cpp b/uva_cpp_clean/12356/12356-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9cb36c4fe3a8ffa8ebbedf1ca41b51fd6b8d69c --- /dev/null +++ b/uva_cpp_clean/12356/12356-11.cpp @@ -0,0 +1,92 @@ +#include +/* +Problem: 12356 - Army Buddies +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3778 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +int S, B; + +int main () { + fastio; + + while ( cin >> S >> B && (B|S)){ + int left[100002], right[100002]; + int L, R, lBuddy, rBuddy; + + + for (int i = 1 ;i <= S; ++i) + left[i] = i-1, right[i] = i+1; + + left[1] = -1, right[S] = -1; + + while (B--){ + cin >> L >> R; + + left[right[R]] = left[L]; + right[left[L]] = right[R]; + + lBuddy = left[L]; + rBuddy = right[R]; + + if (lBuddy != -1) cout << lBuddy; + else cout << "*"; + + cout << " "; + + if (rBuddy != -1) cout << rBuddy; + else cout << "*"; + + cout << '\n'; + } + + cout << "-\n"; + } + + return 0; +} +/* +Sample input:- +----------------- +1 1 +1 1 +10 4 +2 5 +6 9 +1 1 +10 10 +5 1 +1 1 +0 0 + +Sample output:- +----------------- +* * +- +1 6 +1 10 +* 10 +* * +- +* 2 +- + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/12356/12356-14.cpp b/uva_cpp_clean/12356/12356-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6157c307e6b5b55f9eba95a3bcfdc4d8a78ad1e4 --- /dev/null +++ b/uva_cpp_clean/12356/12356-14.cpp @@ -0,0 +1,162 @@ +/*************************************************** + * Problem Name : 12356 Army buddies.cpp + * Problem Link : https://onlinejudge.org/external/123/12356.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-10-19 + * Problem Type : + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int l[MAX], r[MAX]; +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int s, b; + + while (cin >> s >> b) { + if (s == 0 && b == 0) break; + + for (int i = 1; i <= s; i++) { + l[i] = i - 1; + r[i] = i + 1; + } + + r[s] = -1; + l[1] = -1; + + for (int i = 1; i <= b; i++) { + int x, y; + cin >> x >> y; + l[r[y]] = l[x]; + + if (l[x] == -1) { + cout << "* "; + + } else { + cout << l[x] << " "; + } + + r[l[x]] = r[y]; + + if (r[y] == -1) { + cout << "*\n"; + + } else { + cout << r[y] << "\n"; + } + } + + cout << "-\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/12356/12356-21.cpp b/uva_cpp_clean/12356/12356-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f00504f3d7b411af268da94b0b34d654660d469 --- /dev/null +++ b/uva_cpp_clean/12356/12356-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12356 + Name: Army Buddies + Problem: https://onlinejudge.org/external/123/12356.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +int L[100016], R[100016], id[100016], cse=0; +void fix(int u) { + if (id[u] != cse) { + id[u] = cse; + L[u] = u-1; + R[u] = u+1; + } +} + +int main() { + int s, n, a, b; + while (scanf("%d%d", &s, &n)==2 && (s||n)) { + cse++; + fix(1); fix(s); + L[1] = R[s] = 0; + for (int i=0; i traversal > bridges or articulation points + difficulty: hard + date: 27/May/2020 + problem: given an undirected graph, check if there is a unique path between 2 query vertices + hint: there is a unique path between s and t if the path between them is formed only by bridge edges; for optimize, keep sets for the vertices that are on the same connected component in the pruned graph (with only bridge edges) + by: @brpapa +*/ +#include +using namespace std; +struct UFDS { + vector p; + UFDS(int I) { + p.assign(I, 0); + for (int i = 0; i < I; i++) p[i] = i; + } + int findSet(int i) { + if (p[i] == i) return i; + return p[i] = findSet(p[i]); + } + bool isSameSet(int i, int j) { + return findSet(i) == findSet(j); + } + void unionSet(int i, int j) { + int setI = findSet(i); + int setJ = findSet(j); + if (!isSameSet(setI, setJ)) p[setI] = setJ; + } +}; + +#define UNVISITED -1 +#define VISITING 0 + +vector> adjList; +vector> adjListBridge; // grafo com apenas as arestas q são bridge +vector order, low, parent, state; +int countOrder; + +void dfs(int u) { + state[u] = VISITING; + low[u] = order[u] = countOrder++; + + for (int v : adjList[u]) { + if (state[v] == UNVISITED) { + parent[v] = u; + dfs(v); + low[u] = min(low[u], low[v]); + if (order[u] < low[v]) { // u -> v é bridge + adjListBridge[u].push_back(v); + adjListBridge[v].push_back(u); + } + } + else if (v != parent[u]) + low[u] = min(low[u], low[v]); + } +} + +void reach(int u, UFDS &set) { + state[u] = VISITING; + + for (int v : adjListBridge[u]) + if (state[v] == UNVISITED) { + set.unionSet(u, v); + reach(v, set); + } +} + +int main() { + int V, E, Q; + while (cin >> V >> E >> Q && (V || E || Q)) { + adjList.assign(V, vector()); + while (E--) { + int u, v; cin >> u >> v; u--; v--; + adjList[u].push_back(v); + adjList[v].push_back(u); + } + + adjListBridge.assign(V, vector()); + order.resize(V); low.resize(V); parent.resize(V); + + countOrder = 0; state.assign(V, UNVISITED); + for (int v = 0; v < V; v++) if (state[v] == UNVISITED) dfs(v); + + + UFDS sets = UFDS(V); + state.assign(V, UNVISITED); + for (int v = 0; v < V; v++) if (state[v] == UNVISITED) reach(v, sets); + + while (Q--) { + int s, t; cin >> s >> t; s--; t--; + cout << (sets.isSameSet(s, t)? "Y":"N") << endl; + } + cout << "-" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12364/12364-4.cpp b/uva_cpp_clean/12364/12364-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0ed81b8ce2e34ff37075c9593b86d9403227004 --- /dev/null +++ b/uva_cpp_clean/12364/12364-4.cpp @@ -0,0 +1,174 @@ +#include + +void init_screen(char *screen, int height, int width) +{ + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + if (j + 1 != width) + { + screen[i * width + j] = '.'; + } + else + { + screen[i * width + j] = '\0'; + } + } + } + + for (int i = 0; i < height; i++) + { + for (int j = 2; j < width; j += 3) + { + if (screen[i * width + j] != '\0') + { + screen[i * width + j] = ' '; + } + } + } +} + +void n2b(char digit, char *top, char *mid, char *bot) +{ + switch (digit) + { + case '1': + top[0] = '*'; + break; + case '2': + top[0] = mid[0] = '*'; + break; + case '3': + top[0] = top[1] = '*'; + break; + case '4': + top[0] = top[1] = mid[1] = '*'; + break; + case '5': + top[0] = mid[1] = '*'; + break; + case '6': + top[0] = top[1] = mid[0] = '*'; + break; + case '7': + top[0] = top[1] = mid[0] = mid[1] = '*'; + break; + case '8': + top[0] = mid[0] = mid[1] = '*'; + break; + case '9': + top[1] = mid[0] = '*'; + break; + case '0': + top[1] = mid[0] = mid[1] = '*'; + break; + default: + break; + } +} + +void n2b(int d) +{ + std::string number; + std::cin >> number; + + const int height = 3, width = 3 * d; + + char screen[height * width]; + init_screen(screen, height, width); + + size_t index = 0; + for (int i = 0; i < d; i++) + { + n2b(number[i], &screen[0 * width + index], &screen[1 * width + index], &screen[2 * width + index]); + index += 3; + } + std::cout << &screen[0 * width] << std::endl + << &screen[1 * width] << std::endl + << &screen[2 * width] << std::endl; +} + +char b2n(char *top, char *mid, char *bot) +{ + if (top[0] == '*' && top[1] == '.' && mid[0] == '.' && mid[1] == '.') + { + return '1'; + } + else if (top[0] == '*' && top[1] == '.' && mid[0] == '*' && mid[1] == '.') + { + return '2'; + } + else if (top[0] == '*' && top[1] == '*' && mid[0] == '.' && mid[1] == '.') + { + return '3'; + } + else if (top[0] == '*' && top[1] == '*' && mid[0] == '.' && mid[1] == '*') + { + return '4'; + } + else if (top[0] == '*' && top[1] == '.' && mid[0] == '.' && mid[1] == '*') + { + return '5'; + } + else if (top[0] == '*' && top[1] == '*' && mid[0] == '*' && mid[1] == '.') + { + return '6'; + } + else if (top[0] == '*' && top[1] == '*' && mid[0] == '*' && mid[1] == '*') + { + return '7'; + } + else if (top[0] == '*' && top[1] == '.' && mid[0] == '*' && mid[1] == '*') + { + return '8'; + } + else if (top[0] == '.' && top[1] == '*' && mid[0] == '*' && mid[1] == '.') + { + return '9'; + } + else if (top[0] == '.' && top[1] == '*' && mid[0] == '*' && mid[1] == '*') + { + return '0'; + } + + return '?'; +} + +void b2n(int d) +{ + std::string top, mid, bot, output = ""; + std::getline(std::cin, top); + std::getline(std::cin, top); + std::getline(std::cin, mid); + std::getline(std::cin, bot); + + int index = 0; + for (int i = 0; i < d; i++) + { + output += b2n(&top[index], &mid[index], &bot[index]); + index += 3; + } + + std::cout << output << std::endl; +} + +int main() +{ + int d; + char mode; + + while (std::cin >> d >> mode && d) + { + if (mode == 'S') + { + n2b(d); + } + else + { + b2n(d); + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1237/1237-11.cpp b/uva_cpp_clean/1237/1237-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dca497cb93e50baf18737a0b5379f97ac69fa4d3 --- /dev/null +++ b/uva_cpp_clean/1237/1237-11.cpp @@ -0,0 +1,88 @@ +#include +/* +Problem: 1237 - Expert Enough? +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3678 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int T, D, L, H, Q, P; + +int found, idx; +string makers[10001], maker; +int Ls[10001], Hs[10001]; + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> T; + + while ( T-- ){ + + cin >> D; + for (int i = 0; i < D; i++){ + cin >> maker >> L >> H; + Ls[i] = L; + Hs[i] = H; + makers[i] = maker; + } + + cin >> Q; + while ( Q-- ){ + cin >> P; + found = 0; + for (int i = 0; i < D; i++){ + if ( (Ls[i] <= P) and (P <= Hs[i]) ){ + idx = i; + found++; + } + } + + if (found != 1) cout << "UNDETERMINED\n"; + else cout << makers[idx] << '\n'; + } + + if (T) cout << '\n'; + } + + return 0; +} +/* +Sample input:- +----------------- +1 +4 +HONDA 10000 45000 +PEUGEOT 12000 44000 +BMW 30000 75900 +CHEVROLET 7000 37000 +4 +60000 +7500 +5000 +10000 + +Sample output:- +----------------- +BMW +CHEVROLET +UNDETERMINED +UNDETERMINED + + +Resources:- +------------- + + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/1237/1237-13.cpp b/uva_cpp_clean/1237/1237-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be2196095080df43ac021cb01f35273af5bc25c1 --- /dev/null +++ b/uva_cpp_clean/1237/1237-13.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int t; + + cin >> t; + + bool first = true; + + while (t--) + { + if (!first) + + cout << '\n'; + else + + first = false; + + int d; + + cin >> d; + + map < pair < int, int >, string > marker; + map < pair < int, int >, string >::iterator it; + + for (int i = 0; i < d; i++) + { + string name; + + int low, high; + + cin >> name >> low >> high; + + marker[make_pair(low, high)] = name; + } + + int q; + + cin >> q; + + while (q--) + { + int p; + + cin >> p; + + string ans = "UNDETERMINED"; + + for (it = marker.begin(); it != marker.end(); it++) + { + if (p >= it->first.first && p <= it->first.second) + { + if (ans != "UNDETERMINED") + { + ans = "UNDETERMINED"; + + break; + } + + else + { + ans = it->second; + } + } + } + + cout << ans << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1237/1237-19.cpp b/uva_cpp_clean/1237/1237-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66235eb263338f5ac0964a05fefeb15e1cbe0810 --- /dev/null +++ b/uva_cpp_clean/1237/1237-19.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair sii; + +int main() { + int T; + vector v; + cin >> T; + while (T--) { + v.clear(); + int D; + cin >> D; + while (D--) { + sii tm("", ii(0,0)); + cin >> tm.first >> tm.second.first >> tm.second.second; + v.push_back(tm); + } + cin >> D; + while (D--) { + int price; + string tm; + cin >> price; + int count = 0; + for (int i = 0; i < v.size() && count < 2; i++) { + if (v[i].second.first <= price && price <= v[i].second.second) { + count++; + tm = v[i].first; + } + } + if (count == 1) cout << tm << endl; + else cout << "UNDETERMINED" << endl; + } + if (T != 0) cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1237/1237-2.cpp b/uva_cpp_clean/1237/1237-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..342eb05c707c4cb9f5cf12b61bf4c88467fc4fbf --- /dev/null +++ b/uva_cpp_clean/1237/1237-2.cpp @@ -0,0 +1,91 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +#define in cin +#define out cout +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +// search +string search_for_car(int value, + vector>>& v) { + int count = 0; + string name; + for (auto ele: v) { + int l = ele.second.first, h = ele.second.second; + string n = ele.first; + if (value >= l && value <= h) { + // out << l << ' ' << h << ' ' << value << ' ' << name << el; + count++; + name = n; + } + } + if (count == 1) return name; + else return "UNDETERMINED"; +} + +int main(){ + ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; in >> tc; + while (tc--) { + int d; in >> d; + vector>> vt; + while (d--) { + string s; in >> s; int l, h; in >> l >> h; + vt.push_back({s, {l, h}}); + } + // sort(all(vt), sort2); + const int size = (int) vt.size(); + int q; in >> q; + while(q--) { + int t; in >> t; + out << search_for_car(t, vt) << el; + } + if (tc == 0) break; + out << el; + } + return 0; +} + + +/* +CHEVROLET 7000 37000 +HONDA 10000 45000 +PEUGEOT 12000 44000 +BMW 30000 75900 + + + + + + + + + +*/ diff --git a/uva_cpp_clean/1237/1237-21.cpp b/uva_cpp_clean/1237/1237-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c170a34a4b18e73e7dab81bea4817b3a4a00c424 --- /dev/null +++ b/uva_cpp_clean/1237/1237-21.cpp @@ -0,0 +1,83 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1237 + Name: Expert Enough? + Problem: https://onlinejudge.org/external/12/1237.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 10017 +char names[MAXN][200]; +int L[MAXN], R[MAXN], ord[MAXN]; +bool comp(int i, int j) { return L[i]!=L[j] ? L[i]b && a>c ? a : b > c ? b : c; } + +struct Node { + int ind, maxx; + Node *left, *right; + Node(int i, int mx, Node *l, Node *r):ind(i), maxx(mx), left(l),right(r) {} + ~Node() { + if (left) delete left; + if (right) delete right; + } +}; + +Node* MakeTree(int lo, int hi) { + if (lo >= hi) return 0; + int mid = (lo+hi)>>1; + Node *l = MakeTree(lo, mid), + *r = MakeTree(mid+1, hi); + return new Node(ord[mid], max(R[ord[mid]], l ? l->maxx : 0, r ? r->maxx : 0), l, r); +} + + +int rind; +int findNode(Node* r, int p) { + if (!r || r->maxx < p) return 0; + + int ind = r->ind, + cnt = L[ind] <= p && p <= R[ind] ? 1 : 0; + if (cnt) rind = ind; + + if (p >= L[ind]) + cnt += findNode(r->right, p); + + if (cnt < 2) + cnt += findNode(r->left, p); + + return cnt; +} + +int main(){ + int T, n, q, p; + scanf("%d", &T); + while (T--) { + scanf("%d", &n); + for (int i=0; i1) puts("UNDETERMINED"); + else puts(names[rind]); + } + + if (T) putchar('\n'); + delete root; + } +} diff --git a/uva_cpp_clean/1237/1237-3.cpp b/uva_cpp_clean/1237/1237-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f1df621430df0dbfed351f943d8b8a85e7dcd2e --- /dev/null +++ b/uva_cpp_clean/1237/1237-3.cpp @@ -0,0 +1,56 @@ +// Problem: U-Va 1237 - Expert Enough? + +#include +#include +#include + +using namespace std; + +int main () { + int cases, + dataSize, + queries, + compBrands, + brandNo, + price; + + vector lows, highs; + vector names; + + cin >> cases; + bool newLine = false; + while ( cases-- ){ + if ( newLine ) + cout << endl; + cin >> dataSize; + + lows.resize(dataSize); + highs.resize(dataSize); + names.resize(dataSize); + + for ( int l = 0; l < dataSize; l++ ) + cin >> names[l] >> lows[l] >> highs[l]; + + cin >> queries; + while ( queries-- ){ + compBrands = 0; + brandNo = 0; + + cin >> price; + for ( int l = 0; l < dataSize; l++ ) + if ( price >= lows[l] && price <= highs[l] ){ + compBrands ++; + if ( compBrands == 1 ) + brandNo = l; + else + break; + } + if ( compBrands != 1 ) + cout << "UNDETERMINED" << endl; + else + cout << names[brandNo] << endl; + } + newLine = true; + } +} + diff --git a/uva_cpp_clean/1237/1237-9.cpp b/uva_cpp_clean/1237/1237-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5505059e09067cd10ebb8f4a96de74fe992e391a --- /dev/null +++ b/uva_cpp_clean/1237/1237-9.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int c, n, m, lab, ct, tmp; +string s[10000]; +vector < pair > a(10000); + +int main(){ + scanf("%d",&c); + while(c--){ + scanf("%d", &n); + for(int i = 0; i < n; i++) + cin >> s[i] >> a[i].first >> a[i].second; + scanf("%d", &m); + for(int i = 0; i < m; i++){ + scanf("%d", &tmp); + ct = 0; + for(int j = 0; j < n; j++) + if(a[j].first <= tmp && tmp <= a[j].second) + ct++, lab = j; + if(ct == 1) + cout << s[lab] << endl; + else + cout << "UNDETERMINED" << endl; + } + if(c) + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/12372/12372-19.cpp b/uva_cpp_clean/12372/12372-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d052076860997a1d1f51a6ab2dcf05c6b8cb4b1 --- /dev/null +++ b/uva_cpp_clean/12372/12372-19.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int main() { + int T; + cin >> T; + for (int i = 1; i <= T; i++) { + int L, W, H; + cin >> L >> W >> H; + printf("Case %d: ", i); + if (L <= 20 && W <= 20 && H <= 20) printf("good\n"); + else printf("bad\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12372/12372-21.cpp b/uva_cpp_clean/12372/12372-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3186962db4217782149efdbfab2f2ec0ce15c915 --- /dev/null +++ b/uva_cpp_clean/12372/12372-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12372 + Name: Packing for Holiday + Problem: https://onlinejudge.org/external/123/12372.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int n; + cin>>n; + for(int i=1; i<=n; i++) { + int a,b,c; + cin>>a>>b>>c; + printf("Case %d: %s\n", i, (a<=20 && b<=20 && c<=20) ? "good" : "bad"); + } +} diff --git a/uva_cpp_clean/12372/12372-5.cpp b/uva_cpp_clean/12372/12372-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2aa394c4a8309257887db4120a5f4ab1565fb73d --- /dev/null +++ b/uva_cpp_clean/12372/12372-5.cpp @@ -0,0 +1,17 @@ +#include +int main() +{ + int a,b,c,t,i; + while(scanf("%d",&t)==1) + { + for(i=1;i<=t;i++) + { + scanf("%d%d%d",&a,&b,&c); + if(a<=20 && b<=20 && c<=20) + printf("Case %d: good\n",i); + else + printf("Case %d: bad\n",i); + } + } + return 0; +} diff --git a/uva_cpp_clean/12372/12372-9.cpp b/uva_cpp_clean/12372/12372-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..56b23759f5ff479c27e94684090b47dcdb80be7d --- /dev/null +++ b/uva_cpp_clean/12372/12372-9.cpp @@ -0,0 +1,18 @@ +#include + +int main(){ + int n, a, b, c, i = 1; + scanf("%d", &n); + while(n--){ + scanf("%d %d %d", &a, &b, &c); + if(a > 20 || b > 20 || c > 20){ + printf("Case %d: bad\n", i); + i++; + } + else{ + printf("Case %d: good\n", i); + i++; + } + } + return(0); +} diff --git a/uva_cpp_clean/12372/main.cpp b/uva_cpp_clean/12372/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2dad67cf792543e2cebbd9697cf853f52bc7010 --- /dev/null +++ b/uva_cpp_clean/12372/main.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +int main(){ +int t; +cin >> t; +int l,w,h; +for(int c=1; c<=t; c++){ + string output = "good"; + cin >> l >> w >> h; + if(l > 20 || w > 20 || h > 20) output = "bad"; + cout << "Case " << c << ": " << output << '\n'; +} +std::cout << '\n'; + + +return 0; +} diff --git a/uva_cpp_clean/12376/12376-21.cpp b/uva_cpp_clean/12376/12376-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81d3ad775c451bf731d9ee854192a66049c60cfd --- /dev/null +++ b/uva_cpp_clean/12376/12376-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12376 + Name: As Long as I Learn, I Live + Problem: https://onlinejudge.org/external/123/12376.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int val[102]; +list adj[102]; + +int main(){ + int T, n, m; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin>>n>>m; + for (int i=0; i>val[i]; + } + + for (int i=0; i>u>>v; + adj[u].push_back(v); + } + + int tv=0, u=0; + while (!adj[u].empty()) { + int bv=0; + for (int v: adj[u]) + if (val[v] >= val[bv]) + bv = v; + tv += val[u = bv]; + } + + printf("Case %d: %d %d\n", cse, tv, u); + } +} diff --git a/uva_cpp_clean/1239/1239-19.cpp b/uva_cpp_clean/1239/1239-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9e487a616102af3443e15bf0cfc9860ce0b67a3 --- /dev/null +++ b/uva_cpp_clean/1239/1239-19.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +string s; +int k, DP[1002][1002]; + +int main() { + int T; + cin >> T; + while (T--) { + cin >> s >> k; + int l = 1; + for (int i = 0; i < s.length(); i++) DP[i][i] = 0; + for (int i = 0; i < s.length() - 1; i++) { + DP[i][i+1] = s[i] == s[i+1]? 0: 1; + if (DP[i][i+1] <= k) l = 2; + } + for (int j = 2; j < s.length(); j++) { + for (int i = 0; i + j < s.length(); i++) { + DP[i][i+j] = DP[i+1][i+j-1] + (s[i] == s[i+j]? 0: 1); + if (DP[i][i+j] <= k) l = max(l, j+1); + } + } + printf("%d\n", l); + } + return 0; +} diff --git a/uva_cpp_clean/12397/12397-19.cpp b/uva_cpp_clean/12397/12397-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f774d9872f52d99cacccbeebe3ef4a7b07a0d2e1 --- /dev/null +++ b/uva_cpp_clean/12397/12397-19.cpp @@ -0,0 +1,34 @@ +#include +#include +using namespace std; + +int chartomatches(char c, int pos) { + switch(c) { + case '9': return pos == 0? 3: pos == 1? 4: pos == 2? 6: 0; + case '8': return pos == 0? 5: pos == 1? 8: pos == 2? 9: 0; + case '7': return pos == 0? 4: pos == 1? 6: pos == 2? 7: 0; + case '6': return pos == 0? 3: pos == 1? 4: pos == 2? 5: 0; + case '5': return pos == 0? 2: pos == 1? 2: pos == 2? 3: 0; + case '4': return pos == 0? 3: pos == 1? 4: pos == 2? 5: 0; + case '3': return pos == 0? 3: pos == 1? 6: pos == 2? 6: 12; + case '2': return pos == 0? 2: pos == 1? 4: pos == 2? 4: 8; + case '1': return pos == 0? 1: pos == 1? 2: pos == 2? 2: 4; + } + return 0; +} + +int matches(string number) { + int n = 0; + for (int i = 0; i < number.length(); i++) { + n += chartomatches(number[i], number.length()-i-1); + } + return n; +} + +int main() { + string number; + while (cin >> number) { + cout << matches(number) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12397/12397-21.cpp b/uva_cpp_clean/12397/12397-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8abf93add1eab1c8cdd857f14c6b336c0d6f5142 --- /dev/null +++ b/uva_cpp_clean/12397/12397-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12397 + Name: Roman Numerals + Problem: https://onlinejudge.org/external/123/12397.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int vals[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000}, + cnts[] = {1, 3, 2, 3, 2, 4, 2, 4, 2, 5, 3, 6, 4}; + +int main(){ + int n; + while (scanf("%d", &n)==1) { + int c = 0; + for (int i=12; i>=0; i--) { + int x = n/vals[i]; + n -= x * vals[i]; + c += cnts[i] * x; + } + + printf("%d\n", c); + } +} diff --git a/uva_cpp_clean/12397/12397-9.cpp b/uva_cpp_clean/12397/12397-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdc8a03c94ee52d1d296e3e209c02da45b69fd0e --- /dev/null +++ b/uva_cpp_clean/12397/12397-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int n; + +int numberOfMatches(int num){ + map < int, int > cvt; + cvt[1] = 1, cvt[10] = 2, cvt[100] = 2, cvt[1000] = 4; + cvt[9] = 3, cvt[90] = 4, cvt[900] = 6; + cvt[5] = 2, cvt[50] = 2, cvt[500] = 3; + cvt[4] = 3, cvt[40] = 4, cvt[400] = 5; + int matches = 0; + for(map :: reverse_iterator it = cvt.rbegin(); it != cvt.rend(); ++it) + while( num >= it -> first ){ + matches += it -> second; + num -= it -> first; + } + return matches; +} + +int main(){ + while(~scanf("%d", &n)) printf("%d\n", numberOfMatches(n)); + return(0); +} diff --git a/uva_cpp_clean/12398/12398-19.cpp b/uva_cpp_clean/12398/12398-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01e89e207aa7ee0e412c193cd322a755a8888491 --- /dev/null +++ b/uva_cpp_clean/12398/12398-19.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +inline bool limits(int i, int j) { + return i >= 0 && i < 3 && j >= 0 && j < 3; +} + +int main() { + int mat[3][3], c = 1; + string s; + while (getline(cin, s)) { + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + mat[i][j] = 0; + for (int k = 0; k < s.length(); k++) { + int val = s[k] - 'a'; + int i = val/3, j = val%3; + mat[i][j]++, mat[i][j]%=10; + if (limits(i-1, j)) mat[i-1][j]++, mat[i-1][j]%=10; + if (limits(i+1, j)) mat[i+1][j]++, mat[i+1][j]%=10; + if (limits(i, j-1)) mat[i][j-1]++, mat[i][j-1]%=10; + if (limits(i, j+1)) mat[i][j+1]++, mat[i][j+1]%=10; + } + printf("Case #%d:\n", c++); + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (j != 0) printf(" "); + printf("%d", mat[i][j]); + } + printf("\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/12398/12398-21.cpp b/uva_cpp_clean/12398/12398-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76020097e93ab731fa1d0b11f209084f2b8b4362 --- /dev/null +++ b/uva_cpp_clean/12398/12398-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12398 + Name: NumPuzz I + Problem: https://onlinejudge.org/external/123/12398.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int dx[] = {0, 1, -1, 0}, + dy[] = {1, 0, 0, -1}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char line[300]; + for (int cse=1; cin.getline(line, 300);) { + int M[3][3] = {}; + for (int i=0; line[i]; ++i) { + int k = line[i]-'a', + u = k/3, v = k % 3; + + M[u][v] = (M[u][v]+1)%10; + for (int j=0; j<4; ++j) { + int y = u + dy[j], + x = v + dx[j]; + + if (x>=0 && x<3 && y>=0 && y<3) + M[y][x] = (M[y][x]+1)%10; + } + } + + cout << "Case #" << (cse++) << ":\n"; + for (int i=0; i<3; ++i) + cout << M[i][0] << ' ' << M[i][1] << ' ' << M[i][2] << endl; + } +} diff --git a/uva_cpp_clean/12398/12398-9.cpp b/uva_cpp_clean/12398/12398-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faed6dce69bf7964cb3c1f47d5223df656371bf2 --- /dev/null +++ b/uva_cpp_clean/12398/12398-9.cpp @@ -0,0 +1,32 @@ +#include + +#define SIZE 210 + +using namespace std; + +char s[SIZE]; +int tc, v[5][5], x, y; +int dx[] = {-1, 0, 0, 1, 0}; +int dy[] = {0, 1, -1, 0, 0}; + +void inc(int pos){ + x = pos / 3 + 1, y = pos % 3 + 1; + for(int it = 0; it < 5; it++) + v[x + dx[it]][y + dy[it]] = (v[x + dx[it]][y + dy[it]] + 1) % 10; +} + +void printMatrix(){ + printf("Case #%d:\n", ++tc); + for(int i = 1; i <= 3; i++) + for(int j = 1; j <= 3; j++) + printf( j != 3 ? "%d " : "%d\n", v[i][j]); +} + +int main(){ + while(cin.getline(s, SIZE)){ + memset(v, 0, sizeof v); + for(int i = 0, j = strlen(s); i < j; i++) inc(s[i] - 'a'); + printMatrix(); + } + return(0); +} diff --git a/uva_cpp_clean/124/124-21.cpp b/uva_cpp_clean/124/124-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2345fc95db0d6a854c93447b1e78312f7f82367c --- /dev/null +++ b/uva_cpp_clean/124/124-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 124 + Name: Following Orders + Problem: https://onlinejudge.org/external/1/124.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int n; +char sels[30], chars[30]; +int ids[127], seen[30]; +vector results; +int adj[30][30]; + +void dfs(int u, int ind) { + if (ind == n-1) { + results.push_back(sels); + return; + } + + seen[u] = 1; + for (int v=1; v>c1; n++) { + chars[n] = c1; + ids[c1] = n; + } + + sels[n-1] = 0; + stringstream sin2(l2); + while (sin2>>c1>>c2) + adj[ids[c1]][ids[c2]] = 1; + + results.clear(); + dfs(0, 0); + sort(results.begin(), results.end()); + + if (frst) frst=0; + else putchar('\n'); + for (string s: results) + cout << s << endl; + } +} diff --git a/uva_cpp_clean/124/124-4.cpp b/uva_cpp_clean/124/124-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..710c837c0f9cfc535d2b935ca2fdef1b9c77e489 --- /dev/null +++ b/uva_cpp_clean/124/124-4.cpp @@ -0,0 +1,190 @@ +#include +#include +#include +#include + +using namespace std; + +class Node +{ +public: + bool used = false; + char value = 0; + vector parents; + vector children; + + static bool Compare(const Node *n1, const Node *n2); +}; + +bool Node::Compare(const Node *n1, const Node *n2) +{ + return n1->value < n2->value; +} + +class Graph +{ +public: + vector nodes; + + void AddEdge(char a, char b); + void Solve(); + +private: + vector parentless; + + Graph Clone(); + int FindInNodes(char x); + int FindInVector(vector &n, char x); + bool IsParentless(vector &parents); + void SolveRecursively(string order); + void ReloadParentless(); +}; + +Graph Graph::Clone() +{ + Graph g; + + for (unsigned int i = 0; i < nodes.size(); i++) + { + Node n; + n.used = nodes[i].used; + n.value = nodes[i].value; + g.nodes.push_back(n); + } + + for (unsigned int i = 0; i < nodes.size(); i++) + { + for (unsigned int j = 0; j < nodes[i].children.size(); j++) + g.nodes[i].children.push_back(&g.nodes[FindInNodes(nodes[i].children[j]->value)]); + + for (unsigned int j = 0; j < nodes[i].parents.size(); j++) + g.nodes[i].parents.push_back(&g.nodes[FindInNodes(nodes[i].parents[j]->value)]); + } + + g.ReloadParentless(); + + return g; +} + +void Graph::SolveRecursively(string order) +{ + if (parentless.size() == 0) + { + cout << order << endl; + } + else if (parentless.size() == 1) + { + char value = parentless[0]->value; + parentless[0]->used = true; + + ReloadParentless(); + SolveRecursively(order + value); + } + else + { + for (unsigned int j = 0; j < parentless.size(); j++) + { + char value = parentless[j]->value; + Graph g = Clone(); + g.parentless[j]->used = true; + + g.ReloadParentless(); + g.SolveRecursively(order + value); + } + } +} + +int Graph::FindInNodes(char x) +{ + for (unsigned int i = 0; i < nodes.size(); i++) + if (nodes[i].value == x) + return i; + + return -1; +} + +int Graph::FindInVector(vector &n, char x) +{ + for (unsigned int i = 0; i < n.size(); i++) + if (n[i]->value == x) + return i; + + return -1; +} + +void Graph::AddEdge(char a, char b) +{ + int first = FindInNodes(a), second = FindInNodes(b); + Node *nfirst = &nodes[first], *nsecond = &nodes[second]; + + if (FindInVector(nfirst->children, nsecond->value) == -1) + nfirst->children.push_back(nsecond); + if (FindInVector(nsecond->parents, nfirst->value) == -1) + nsecond->parents.push_back(nfirst); +} + +bool Graph::IsParentless(vector &parents) +{ + for (unsigned int i = 0; i < parents.size(); i++) + if (parents[i]->used == false) + return false; + + return true; +} + +void Graph::ReloadParentless() +{ + parentless.clear(); + for (unsigned int i = 0; i < nodes.size(); i++) + if (nodes[i].used == false && IsParentless(nodes[i].parents)) + parentless.push_back(&nodes[i]); + + sort(parentless.begin(), parentless.end(), Node::Compare); +} + +void Graph::Solve() +{ + ReloadParentless(); + SolveRecursively(""); +} + +int main() +{ + bool firstinput = true; + string line; + while (getline(cin, line)) + { + stringstream ss; + ss << line; + + char temp; + vector nodes; + + while (ss >> temp) + { + Node n; + n.value = temp; + nodes.push_back(n); + } + + getline(cin, line); + ss.clear(); + ss << line; + + Graph graph; + graph.nodes = nodes; + + char first, second; + while (ss >> first >> second) + graph.AddEdge(first, second); + + if (firstinput) + firstinput = false; + else + cout << endl; + + graph.Solve(); + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/124/124-6.cc b/uva_cpp_clean/124/124-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..808b1aec86f5443009e041a0b11081432a44b5fa --- /dev/null +++ b/uva_cpp_clean/124/124-6.cc @@ -0,0 +1,53 @@ +// https://uva.onlinejudge.org/external/1/124.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +using qi=queue; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(int T=0;;T++){ + string s; + getline(cin,s); + if(s.empty())break; + vi m(128); + stringstream in(s); + char x,y; + int n=0; + vi c; + while(in>>x){ + m[x]=n++; + c.push_back(x); + } + sort(c.begin(),c.end()); + for(int i=0;i>x>>y){ + int u=m[x],v=m[y]; + g[u].push_back(v); + a[v]++; + } + string t; + vi r(n); + functionf=[&](int u){ + r[u]=1; + t.push_back(c[u]); + if(t.size()==n)cout< + +using namespace std; + +const int MAX_V = 50; + +string line, x, y; +stringstream ss; +vector v; +vector ans; +map mp; +bool pos[MAX_V][MAX_V], vis[MAX_V], first = true; + +bool check(int id){ + for(int i = 0; i < ans.size(); i++) + if( not pos[ans[i]][id] ) return false; + return true; +} + +void backtrack(){ + if( ans.size() == v.size() ){ + for(int i = 0; i < ans.size(); i++) cout << v[ans[i]]; cout << endl; + return; + } + for(int i = 0; i < v.size(); i++) + if( not vis[i] and check(i) ){ + vis[i] = true; + ans.push_back(i); + backtrack(); + vis[i] = false; + ans.pop_back(); + } +} + +void solve(){ + if( not first ) puts(""); + first = false; + backtrack(); +} + +void read(){ + memset(pos, true, sizeof pos); + ss.clear(); + ss << line; + while( ss >> x ) v.push_back(x); + sort(v.begin(), v.end()); + for(int i = 0; i < v.size(); i++) mp[v[i]] = i; + getline(cin, line); + ss.clear(); + ss << line; + while( ss >> x >> y){ + int u = mp[x], v = mp[y]; + pos[v][u] = false; + } +} + +void clear(){ + v.clear(); + mp.clear(); +} + +int main(){ + while(getline(cin, line)){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/12403/12403-21.cpp b/uva_cpp_clean/12403/12403-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5215baa22d2b4804056a172a9d2462e78fe85b06 --- /dev/null +++ b/uva_cpp_clean/12403/12403-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12403 + Name: Save Setu + Problem: https://onlinejudge.org/external/124/12403.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char op[20]; + long long int n, x, s=0; + cin>>n; + while(n--) { + cin>>op; + if (op[0]=='r') cout<>x; + s+=x; + } + } +} diff --git a/uva_cpp_clean/12403/12403-5.cpp b/uva_cpp_clean/12403/12403-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0f3446a54c6d24b900a0d280c9c0b57ac46aa6a1 --- /dev/null +++ b/uva_cpp_clean/12403/12403-5.cpp @@ -0,0 +1,23 @@ +#include +#include + +int main () +{ + int t,s=0,d=0; + char a [100]; + scanf ("%d", &t); + while ( t-- ) + { + scanf ("%s", a); + if (strcmp (a, "donate")==0) + { + scanf ("%d", &d); + s += d; + } + else + printf ("%d\n", s); + } + + return 0; + +} diff --git a/uva_cpp_clean/12403/12403-9.cpp b/uva_cpp_clean/12403/12403-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c912355080bbac3f7959529576f3477c283d6498 --- /dev/null +++ b/uva_cpp_clean/12403/12403-9.cpp @@ -0,0 +1,21 @@ +#include +#include + +int main(){ + int n, ans = 0, monto; + char aviso[10]; + scanf("%d", &n); + while(n){ + scanf("%s", aviso); + if(strcmp(aviso,"donate") == 0){ + scanf("%d", &monto); + ans += monto; + n--; + } + if(strcmp(aviso,"report") == 0){ + printf("%d\n",ans); + n--; + } + } + return(0); +} diff --git a/uva_cpp_clean/12403/main.cpp b/uva_cpp_clean/12403/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e437bdc5064b50eb36ba327401111ec145a4562 --- /dev/null +++ b/uva_cpp_clean/12403/main.cpp @@ -0,0 +1,21 @@ +#include +#include + +using namespace std; + +int main(){ +int account = 0, new_money = 0, cases; +string command; +cin >> cases; +for(int i=0; i> command; + if(command == "donate"){ + cin >> new_money; + account += new_money; + }else{ + cout << account << endl; + } +} + +return 0; +} diff --git a/uva_cpp_clean/12405/12405-13.cpp b/uva_cpp_clean/12405/12405-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6c51c502f5ba56609d7aa667442d036bf238b05 --- /dev/null +++ b/uva_cpp_clean/12405/12405-13.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + +string s; +int test, n, t = 1, how = 0; + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n; + + cin >> s; + + how = 0; + + for (int i = 0; i < n; i++) + { + if (s[i] == '.') + { + how++; + + i += 2; + } + } + + printf("Case %d: %d\n",t++ , how); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/12405/12405-20.cpp b/uva_cpp_clean/12405/12405-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..855599928cc2082d4ce41678fb60efe6b863974b --- /dev/null +++ b/uva_cpp_clean/12405/12405-20.cpp @@ -0,0 +1,25 @@ +#include + +int tc, nCase; +int N, scarecrow, flag; +char field[105]; + +int main() +{ + scanf("%d ", &tc); + nCase = 1; + while( tc-- ) + { + scanf("%d", &N); + scanf("%s", field); + + scarecrow = 0; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12405 + Name: Scarecrow + Problem: https://onlinejudge.org/external/124/12405.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char line[200]; + int T, n; + cin>>T; + for (int cse=1; cse<=T; cse++) { + cin >> n >> line; + + int rs=0, res=0; + for (int i=0; i0 && i>0 && i0) res++; + printf("Case %d: %d\n", cse, res); + } +} diff --git a/uva_cpp_clean/12405/12405-5.cpp b/uva_cpp_clean/12405/12405-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b5b7f1bd627e5ea7913457a7687999d7b18dd802 --- /dev/null +++ b/uva_cpp_clean/12405/12405-5.cpp @@ -0,0 +1,29 @@ +#include +int main() +{ + int t,n,j,c,i; + char a[110]; + while(scanf("%d",&t)==1) + { + for(j=1;j<=t;j++) + { + scanf("%d %s",&n,a); + c = 0; + for(i=0;i + +using namespace std; + +int tc, n, ans; +char s[105]; + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d", &n); + scanf("%s", s); + ans = 0; + for(int i = 0; i < n; i++){ + if(s[i] == '#') continue; + ans++; + s[i] = s[i + 1] = s[i + 2] = '#'; + } + printf("Case %d: %d\n", t, ans); + } + return(0); +} diff --git a/uva_cpp_clean/12406/12406-21.cpp b/uva_cpp_clean/12406/12406-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd1653d12d43d9f9bb8d0749d588ef2b4e9577ed --- /dev/null +++ b/uva_cpp_clean/12406/12406-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12406 + Name: Help Dexter + Problem: https://onlinejudge.org/external/124/12406.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1152921504606846976LL +using namespace std; + + +long long int mn[23][23], mx[23][23]; +void dfs(long long int num, int d) { + if (d > 17) return; + for (int i=0; i<18 && num % (1<> T; + for (int cse=1; cse<=T; ++cse) { + cin >> p >> q; + + cout << "Case " << cse << ": "; + if (mn[p][q] == INF) cout << "impossible\n"; + else { + cout << mn[p][q]; + if (mn[p][q] != mx[p][q]) + cout << ' ' << mx[p][q]; + cout << '\n'; + } + } +} diff --git a/uva_cpp_clean/1241/1241-19.cpp b/uva_cpp_clean/1241/1241-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..518cfc2f2dcef566d1a53f4556c701622a9010b6 --- /dev/null +++ b/uva_cpp_clean/1241/1241-19.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +using namespace std; + +int cont; + +void func(unordered_set & wd, int N) { + if (N == 0) return; + unordered_set wdn; + for (int i = 0; i < (1<<(N-1)); i++) { + if (wd.count((i << 1)) && wd.count((i << 1)+1)) { + wdn.insert(i); + } else if (wd.count((i << 1)) || wd.count((i << 1)+1)) { + cont++; + } + } + if (wdn.size() != 0) func(wdn, N-1); +} + +int main() { + int T; + cin >> T; + while (T--) { + int N, M; + cin >> N >> M; + unordered_set wd; + while (M--) { + int p; + cin >> p; + wd.insert(p-1); + } + cont = 0; + func(wd, N); + printf("%d\n", cont); + } + return 0; +} diff --git a/uva_cpp_clean/1241/1241-21.cpp b/uva_cpp_clean/1241/1241-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbf6ebf28244a3a1393c80c95227d6e1b0d663bf --- /dev/null +++ b/uva_cpp_clean/1241/1241-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1241 + Name: Jollybee Tournament + Problem: https://onlinejudge.org/external/12/1241.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +bool wd[2048]; +int cum[2048]; +int countWo(int s, int e) { + if (s+1 >= e) return 0; + int m = (e+s)>>1; + return countWo(s, m) + countWo(m, e) + ((cum[e]-cum[m]==0)^(cum[m]-cum[s]==0)); +} + +int main() { + int T, n, m, sz, x; + scanf("%d", &T); + while (T--) { + scanf("%d%d", &n, &m); + memset(wd, 0, sz=1< + +using namespace std; + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + int t; + for(cin >> t ; t-- ; ){ + int n, m, p = 0; + cin >> n >> m; + + vector player(1 << n, 1); + for(int x ; m-- ; ) + cin >> x, player[ x - 1 ] = 0; + + int ans = 0; + for(int gap = 1 << n ; gap > 1 ; gap >>= 1){ + for(int i = 0 ; i < gap ; i += 2){ + ans += player[ i ] ^ player[ i + 1 ]; + player[ i >> 1 ] = player[ i ] | player[ i + 1 ]; + } + } + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1241/1241-9.cpp b/uva_cpp_clean/1241/1241-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c2253e1f9a926b0c2b53647ec322fc93ddf329a --- /dev/null +++ b/uva_cpp_clean/1241/1241-9.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int a[1<<11]; + +int main(){ + int c, tmp, lim, ans, n, m; + scanf("%d", &c); + while(c--){ + ans = 0; + scanf("%d %d", &n, &m); + lim = 1< +using namespace std; +struct Student { + string sid, name; + int cid, tot = 0, psd = 0; + vector scr; + + bool read() { + cin >> sid; + if (sid != "0") { + cin >> cid >> name; + scr = vector(4); + for (int &i : scr) { + cin >> i; + tot += i; + if (i >= 60) + ++psd; + } + return true; + } + return false; + } +}; +void query(list &data, list::iterator iter) { + int rank = 1; + for (Student stu : data) + if (stu.tot > iter->tot) + ++rank; + cout << rank << " " << iter->sid << " " << iter->cid << " " << iter->name << " "; + for (int i : iter->scr) + cout << i << " "; + cout << iter->tot << " " << iter->tot / 4.0 + 5e-5 << '\n'; +} +void stats(list &data) { + const static vector classes { "Chinese", "Mathematics", "English", "Programming" }; + cout << "Please enter class ID, 0 for the whole statistics.\n"; + int choice, cnt = 0; + vector score(4, 0), sub_passed(4, 0), all_passed(5, 0); + cin >> choice; + for (Student stu : data) + if (choice == 0 || stu.cid == choice) { + ++cnt; + transform(stu.scr.begin(), stu.scr.end(), score.begin(), score.begin(), plus()); + transform(stu.scr.begin(), stu.scr.end(), sub_passed.begin(), sub_passed.begin(), [](int scor, int su){ + if (scor >= 60) + return su + 1; + return su; + }); + ++all_passed[stu.psd]; + } + if (!cnt) + return; + for (int id = 0; id < 4; ++id) + cout << classes[id] << '\n' + << "Average Score: " << 1.0 * score[id] / cnt + 5e-5 << '\n' + << "Number of passed students: " << sub_passed[id] << '\n' + << "Number of failed students: " << cnt - sub_passed[id] << "\n\n"; + int cur_sum = 0; + cout << "Overall:\n" + << "Number of students who passed all subjects: " << (cur_sum += all_passed[4]) << '\n' + << "Number of students who passed 3 or more subjects: " << (cur_sum += all_passed[3]) << '\n' + << "Number of students who passed 2 or more subjects: " << (cur_sum += all_passed[2]) << '\n' + << "Number of students who passed 1 or more subjects: " << (cur_sum += all_passed[1]) << '\n' + << "Number of students who failed all subjects: " << all_passed[0] << "\n\n"; +} +int main() { + ios::sync_with_stdio(false); + cout << fixed << setprecision(2); + list data; + bool cont = true; + while (cont) { + cout << "Welcome to Student Performance Management System (SPMS).\n\n" + << "1 - Add\n" + << "2 - Remove\n" + << "3 - Query\n" + << "4 - Show ranking\n" + << "5 - Show Statistics\n" + << "0 - Exit\n\n"; + int choice; + cin >> choice; + switch (choice) { + case 1: + while (true) { + cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish.\n"; + Student cur; + if (cur.read()) { + bool ok = true; + for (Student stu : data) + if (cur.sid == stu.sid) { + ok = false; + break; + } + if (ok) + data.push_back(cur); + else + cout << "Duplicated SID.\n"; + } + else + break; + } + break; + case 2: + while (true) { + string entry; + int cnt = 0; + cout << "Please enter SID or name. Enter 0 to finish.\n"; + cin >> entry; + if (!isalpha(entry[0])) { + if (entry == "0") + break; + auto iter = data.begin(); + while (iter != data.end()) { + if (iter->sid == entry) { + iter = data.erase(iter); + ++cnt; + break; + } + else + ++iter; + } + } + else { + auto iter = data.begin(); + while (iter != data.end()) { + if (iter->name == entry) { + iter = data.erase(iter); + ++cnt; + } + else + ++iter; + } + } + cout << cnt << " student(s) removed.\n"; + } + break; + case 3: + while (true) { + string entry; + cout << "Please enter SID or name. Enter 0 to finish.\n"; + cin >> entry; + if (!isalpha(entry[0])) { + if (entry == "0") + break; + for (auto iter = data.begin(); iter != data.end(); ++iter) + if (iter->sid == entry) { + query(data, iter); + break; + } + } + else + for (auto iter = data.begin(); iter != data.end(); ++iter) + if (iter->name == entry) { + query(data, iter); + } + } + break; + case 4: + cout << "Showing the ranklist hurts students' self-esteem. Don't do that.\n"; + break; + case 5: + stats(data); + break; + case 0: + cont = false; + } + } +} diff --git a/uva_cpp_clean/12416/12416-20.cpp b/uva_cpp_clean/12416/12416-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98d6d65a5faef86c1eb0deca1dc080fc69ad28df --- /dev/null +++ b/uva_cpp_clean/12416/12416-20.cpp @@ -0,0 +1,32 @@ +#include +#include + +const int S = 100000005; +char line[S]; +int len, counter, MAX, step; + +int main() +{ + while( gets(line) ) + { + len = strlen(line), counter = 0, MAX = 0; + for(int i=0 ; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; +int main() +{ + string s; + while(getline(cin,s)) + { + int l=s.size(); + int c=0,Max=0; + for(int i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12428 + Name: Enemy at the Gates + Problem: https://onlinejudge.org/external/124/12428.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool check(long long n, long long m) { + return m>=n-1 && m<=(n*(n-1)>>1); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + while (T--) { + long long n, m; + cin >> n >> m; + int lo=0, hi=n-1; + while (lo <= hi) { + int mid = (lo + hi) >> 1; + if (check(n-mid, m-mid)) + lo = mid + 1; + else + hi = mid - 1; + } + cout << lo-1 << '\n'; + } +} diff --git a/uva_cpp_clean/12439/12439-15.cpp b/uva_cpp_clean/12439/12439-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0894ecb8d464ca1b106eb04d3cb5251ff753d2df --- /dev/null +++ b/uva_cpp_clean/12439/12439-15.cpp @@ -0,0 +1,29 @@ +/* + 閏年、數學 +*/ +#include +#include +#include + +using namespace std; + +inline int day_count(int year, string month, int day) { + int ans = (year / 4) - (year / 100) + (year / 400); + if ((year % 4 == 0) and (year % 100 != 0 or year % 400 == 0)) + if (month == "January" or (month == "February" and day != 29)) + --ans; + return ans; +} + +int main() { + int n, day, year, leap_days; + char no_use; + string month; + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> month >> day >> no_use >> year; + leap_days = day_count(year, month, day - 1); // 半開區間 + cin >> month >> day >> no_use >> year; + cout << "Case " << i << ": " << abs(leap_days -= day_count(year, month, day)) << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/12442/12442-13.cpp b/uva_cpp_clean/12442/12442-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..157f6f9c5db8dcb52a6c71f0e5e4d6e337ebab21 --- /dev/null +++ b/uva_cpp_clean/12442/12442-13.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 50010 + +int test, t = 1, n; + +int result[MAX]; +bool visited[MAX]; + +vector > graph(n); + +int dfs(int start) +{ + if (visited[start]) return 0; + + visited[start] = true; + + int temp = dfs(graph[start][0]); + + visited[start] = false; + + return result[start] = temp + 1; +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n; + + graph.clear(); + + graph.resize(n); + + memset(visited, 0, sizeof visited); + + memset(result, -1, sizeof result); + + for (int i = 0; i < n; i++) + { + int x, y; + + cin >> x >> y; + + x--; + + y--; + + graph[x].push_back(y); + } + + int ans = 999999, tmp = 0; + + for (int i = 0; i < n; i++) + { + if (result[i] == -1) dfs(i); + + if (result[i] > tmp) + { + tmp = result[i]; + + ans = i + 1; + } + + else if (result[i] == tmp) ans = min(i + 1, ans); + } + + printf("Case %d: %d\n", t++, ans); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/12442/12442-21.cpp b/uva_cpp_clean/12442/12442-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e9765f3ed9ab45111241841b80c8dc98066d6ba --- /dev/null +++ b/uva_cpp_clean/12442/12442-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12442 + Name: Forwarding Emails + Problem: https://onlinejudge.org/external/124/12442.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 50001 +int nxt[MAXN], prv[MAXN], sze[MAXN], onstack[MAXN]; + +int main(){ + int cse=1, T, n, u, v; + cin >> T; + while (T--) { + cin>>n; + for (int i=0; i>u>>v; + nxt[u-1] = v-1; + sze[i] = onstack[i] = 0; + } + + int maxsz=0; + for (int s=0; s specials + difficulty: medium + date: 29/Feb/2020 + problem: given a graph with all vertices with out-degree 1, find the vertice that reaches the most vertices + by: @brpapa +*/ +#include +#include +using namespace std; + +vector> adjList; // embora cada vértice só tenha 1 adjacente +vector reach; // reach[v] = qtd de outros vértices que v alcança + +#define UNVISITED -1 +#define VISITING 0 // vértices do CC atual que estão sendo visitados +#define VISITED 1 +vector state; + +// calcula reach[u] +int dfs(int u) { + state[u] = VISITING; + + reach[u] = 0; + for (int v : adjList[u]) + if (state[v] != VISITING) + reach[u] += 1 + dfs(v); // mesmo se v já foi visitado, ainda é preciso recalcular, pois v pode estar no meio de um ciclo e, portanto, não ter recebido reach[v] correto + + state[u] = VISITED; + return reach[u]; +} + +int main() { + int T; cin >> T; + for (int t = 1; t <= T; t++) { + int V; cin >> V; adjList.assign(V, vector()); + + for (int e = 0; e < V; e++) { + int u, v; cin >> u >> v; u--; v--; + adjList[u].push_back(v); + } + + state.assign(V, UNVISITED); reach.assign(V, -1); + int maxReach = 0, vMaxReach; + + for (int v = 0; v < V; v++) { + if (state[v] == UNVISITED) dfs(v); + if (reach[v] > maxReach) { maxReach = reach[v]; vMaxReach = v; } + } + + cout << "Case " << t << ": " << vMaxReach+1 << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12455/12455-11.cpp b/uva_cpp_clean/12455/12455-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24c966bcccc1078dff786fb29e85b8da14da621c --- /dev/null +++ b/uva_cpp_clean/12455/12455-11.cpp @@ -0,0 +1,72 @@ +#include +/* +Problem: 12455 - Bars +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3886 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +bool possible; +int T, N, P, Ps[21]; + +int main () { + scanf("%d\n", &T); + + while ( T-- ){ + scanf("%d\n%d\n", &N, &P); + possible = false; + + for (int i = 0; i < P; i++) + scanf("%d", &Ps[i]); + + for (int mask = 0; mask < (1< +#include +#include + +using namespace std; + +int TC, n, p; +bool canMake[1010]; + +int main() +{ + cin >> TC; + + while (TC--) + { + cin >> n; + + cin >> p; + + int* value = new int[p]; + + memset(canMake, false, sizeof canMake); + + for (int i = 0; i < p; i++) + { + cin >> value[i]; + } + + canMake[0] = true; + + sort(value, value + p); + + for (int i = 0; i < p; i++) + { + for (int j = 1001; j >= 0; j--) + { + if (j - value[i] >= 0 && canMake[j - value[i]]) canMake[j] = true; + } + } + + if (canMake[n]) cout << "YES" << endl; + + else cout << "NO" << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/12455/12455-14.cpp b/uva_cpp_clean/12455/12455-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..71e85f1ebed3e96d4a58ead38c1ac0387ccee3ff --- /dev/null +++ b/uva_cpp_clean/12455/12455-14.cpp @@ -0,0 +1,113 @@ +/*************************************************** + * Problem Name : 12455 Bars.cpp + * Problem Link : https://uva.onlinejudge.org/external/124/12455.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : Basic Recursion(I use DP) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int W, n, ar[23]; +int dp[23][1003]; +int fun (int i, int sum) { + if (i >= n) { + if (sum == W) return 1; + else return 0; + } + + if (dp[i][sum] != -1) return dp[i][sum]; + + int res1 = 0, res2 = 0; + + if (sum + ar[i] <= W) { + res1 = fun (i + 1, sum + ar[i]); + } + + res2 = fun (i + 1, sum); + return dp[i][sum] = res1 | res2; +} +int main () { + __FastIO; + int tc; + cin >> tc; + + while (tc--) { + cin >> W >> n; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + if (W == 0) { + cout << "YES\n"; + continue; + } + + SET (dp, -1); + int ans = fun (0, 0); + + if (ans == 1) { + cout << "YES\n"; + + } else { + cout << "NO\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/12455/12455-19.cpp b/uva_cpp_clean/12455/12455-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b9ca79c692fc30560d1d5a29d0f5d17a0d534eb --- /dev/null +++ b/uva_cpp_clean/12455/12455-19.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +using namespace std; + +int n, p; +int longs[20]; + +bool proc(int pos, int sum) { + if (sum == n) return true; + if (sum < n && pos < p && sum + (p-pos)*longs[pos] >= n) { + return proc(pos+1, sum + longs[pos]) || proc(pos+1, sum); + } + return false; +} + +int main() { + int cases; + scanf("%d", &cases); + while (cases--) { + scanf("%d", &n); + scanf("%d", &p); + for (int i = 0; i < p; i++) { + scanf("%d", &longs[i]); + } + sort(longs, longs + p, greater()); + if (proc(0, 0)) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12455/12455-2.cpp b/uva_cpp_clean/12455/12455-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c031cb1cc1808dc4f940a5dd8617b77cdfcec26 --- /dev/null +++ b/uva_cpp_clean/12455/12455-2.cpp @@ -0,0 +1,49 @@ +#include "bits/stdc++.h" + +#define el "\n" +#define all(v) ((v).begin()),((v).end()) + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +const int OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + ios::sync_with_stdio(false); + cin.tie(0); + + int t; cin >> t; + + while(t--) { + int n; cin >> n; + int p; cin >> p; + vi v(p); + for (int i = 0; i < p; ++i) cin >> v[i]; + + int mask, sum = 0; + for (mask = 0; mask < (1 << p); ++mask) { + sum = 0; + for (int j = 0; j < p; ++j) + if (mask & (1< + +int tc, n, p, pi; +bool status[1005]; + +int main() +{ + scanf("%d", &tc); + while( tc-- ) + { + status[0] = true; + for(int i=1 ; i<1002 ; i++) status[i] = false; + + scanf("%d", &n); + scanf("%d", &p); + for(int i=0 ; i

=0 ; j--) + if( !status[j+pi] && status[j] ) status[j+pi] = true; + } + + if( status[n] ) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12455/12455-21.cpp b/uva_cpp_clean/12455/12455-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a273817dbd528748669a3543a3b50d04b266a1e6 --- /dev/null +++ b/uva_cpp_clean/12455/12455-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12455 + Name: Bars + Problem: https://onlinejudge.org/external/124/12455.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[23], T, n, l; + cin >> T; + while (T-- && cin>>l>>n) { + int sz = 0; + for (int i=0; i> X[sz]; + if (X[sz] <= l) ++sz; + } + + bool DP[1001] = { 1 }; + for (int i=0; i=0; --j) + DP[j + X[i]] |= DP[j]; + + cout << (DP[l] ? "YES\n" : "NO\n"); + } +} diff --git a/uva_cpp_clean/12455/12455-3.cpp b/uva_cpp_clean/12455/12455-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7247a4c09c9123f481a35cae34aa30d32100baa8 --- /dev/null +++ b/uva_cpp_clean/12455/12455-3.cpp @@ -0,0 +1,36 @@ +#include + +int main () +{ + bool no; + int cases, target, bars, sum, bar[20]; + long permCounter; + + scanf("%d",&cases); + while ( cases-- ) + { + scanf("%d %d", &target, &bars); + no = true; + + for ( int l = 0; l < bars; l++ ) + scanf("%d", &bar[l]); + + for ( permCounter = 0; permCounter < (1< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d",a) +#define PLN(a) printf("%lld",a) + + +#define ff first +#define se second +#define pb push_back +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +////============ CONSTANT ===============//// +#define mx (2000010) +#define inf (1<<30) //infinity value +#define eps 1e-9 +#define mod 1000000007 +////====================================//// + +int main() +{ + int t; + sii(t); + while(t--) + { + int n,p; + bool make[10000]; + cover(make,false); + int bar[10000]; + ssii(n,p); + int len=0; + while(p--) + { + sii(bar[len++]); + } + make[0]=true; + for(int i=0;i=bar[i];j--) + { + if(make[j-bar[i]]) + make[j]=1; + // P(j-bar[i]); + } + } + if(make[n]) + puts("YES"); + else + puts("NO"); + + } + return 0; +} diff --git a/uva_cpp_clean/12455/12455-6.cpp b/uva_cpp_clean/12455/12455-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00912f0c223f72b11daf29fedb8d30803af5e452 --- /dev/null +++ b/uva_cpp_clean/12455/12455-6.cpp @@ -0,0 +1,34 @@ +/* + brute force > iterative > all subsets + difficulty: easy + date: 11/Jan/2020 + hint: use bitmask + by: @brpapa +*/ +#include +#include +#define bitIsOn(S, i) (S & (1 << i)) +using namespace std; + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int T; cin >> T; + while (T--) { + int K; cin >> K; + int N; cin >> N; + vector lenBars(N); for (int &l : lenBars) cin >> l; + + bool ok = false; + for (int bm = 0; bm < (1 << N) && !ok; bm++) { + int k = 0; + for (int j = 0; j < N; j++) + if (bitIsOn(bm, j)) + k += lenBars[j]; + + if (k == K) ok = true; + } + cout << (ok? "YES":"NO") << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12455/12455-8.cpp b/uva_cpp_clean/12455/12455-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e000e72926485f2cdb61ff3de304df7ca04d45ed --- /dev/null +++ b/uva_cpp_clean/12455/12455-8.cpp @@ -0,0 +1,38 @@ +// Problem name: Bars +// Problem link: https://vjudge.net/problem/UVA-12455 +// Submission link: https://vjudge.net/solution/32987738 + +#include + +using namespace std; + +template +T bitmask_solve(int n, const function &f, T initial_value, const function &update = [](T a, T b){ return max(a, b); }){ + T ans = initial_value; + for(int mask = 1 << n ; --mask >= 0 ; ans = update(ans, f(mask))); + return ans; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + int t; + for(cin >> t ; t-- ; ){ + int n, p; + cin >> n >> p; + vector v(p); + for(int &x : v) + cin >> x; + + auto f = [n, p, &v](int mask){ + int sum = 0; + for(int i = 0 ; i < p ; ++i) + if(mask & (1 << i)) + sum += v[ i ]; + return n == sum; + }; + + cout << (bitmask_solve(p, f, false, [](bool a, bool b){ return a || b; }) ? "YES" : "NO") << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12459/12459-19.cpp b/uva_cpp_clean/12459/12459-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34caffa28ecfa07e4216eda640c5406823428692 --- /dev/null +++ b/uva_cpp_clean/12459/12459-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + long long fib[81]; + fib[0] = 1, fib[1] = 1; + for (int i = 2; i < 81; i++) fib[i] = fib[i-1] + fib[i-2]; + int n; + while (cin >> n && n) { + printf("%lld\n", fib[n]); + } + return 0; +} diff --git a/uva_cpp_clean/12459/12459-20.cpp b/uva_cpp_clean/12459/12459-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d8bed776d90def7de4c007634b30e5dbf3be0ff --- /dev/null +++ b/uva_cpp_clean/12459/12459-20.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +long ancestor[82]; + +int main() +{ + ancestor[0] = ancestor[1] = 1; + for(int i=2 ; i<82 ; i++) ancestor[i] = ancestor[i-2]+ancestor[i-1]; + + int generation; + + while( cin >> generation && generation ) + cout << ancestor[generation] << endl; + + return 0; +} diff --git a/uva_cpp_clean/12459/12459-21.cpp b/uva_cpp_clean/12459/12459-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e95ef2c80060ca4be0accdc6ed7fa2361f40159 --- /dev/null +++ b/uva_cpp_clean/12459/12459-21.cpp @@ -0,0 +1,24 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12459 + Name: Bees' ancestors + Problem: https://onlinejudge.org/external/124/12459.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +int main(){ + long long DP[81]; + DP[0] = DP[1] = 1; + for (int i=2; i<81; i++) + DP[i] = DP[i-1] + DP[i-2]; + + int n; + while (scanf("%d", &n)==1 && n) + printf("%lld\n", DP[n]); +} diff --git a/uva_cpp_clean/12459/12459-5.cpp b/uva_cpp_clean/12459/12459-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..16275aba022124d823907d838dabc2e1a86c8aaf --- /dev/null +++ b/uva_cpp_clean/12459/12459-5.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + ll n; + while(sc("%lld",&n)==1) + { + ll a=2; + ll b=1; + ll sum=0; + if(n==0) + break; + else if(n==1){ + pf("1\n"); + continue; + } + else if(n==2){ + pf("2\n"); + continue; + } + + while(n>=3) + { + sum=a+b; + b=a; + a=sum; + n--; + } + pf("%lld\n",sum); + } + return 0; +} diff --git a/uva_cpp_clean/1246/1246-20.cpp b/uva_cpp_clean/1246/1246-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f02aa0e26484e7ff0bdbcb529df6169b3615729 --- /dev/null +++ b/uva_cpp_clean/1246/1246-20.cpp @@ -0,0 +1,53 @@ +#include +#include + +const int SZ = 1000005; +bool prime[SZ]; +int temp, tc, L, H; +int ans[SZ], len; + +bool divCheck(int num) +{ + int counter = 0; + for(int j=1 ; j*j<=num ; j++) + if( num%j==0 ) + { + counter++; + if( j!=(num/j) ) counter++; + } + return prime[counter]; +} + +int main() +{ + prime[0] = prime[1] = false; + for(int i=2 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12461 + Name: Airplane + Problem: https://onlinejudge.org/external/124/12461.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while (cin >> n && (n)) { + cout << "1/2" << endl; + } +} diff --git a/uva_cpp_clean/12461/12461-5.cpp b/uva_cpp_clean/12461/12461-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ee6d2876eca93f1ecbd6f9ae1c69fcbdd9f849a1 --- /dev/null +++ b/uva_cpp_clean/12461/12461-5.cpp @@ -0,0 +1,13 @@ +#include +int main() +{ + int a; + while(scanf("%d",&a)==1) + { + if(a==0) + break; + else + printf("1/2\n"); + } +return 0; +} diff --git a/uva_cpp_clean/12463/12463-19.cpp b/uva_cpp_clean/12463/12463-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..901267ecda174a2fb693498f8eb513f6bf98c22e --- /dev/null +++ b/uva_cpp_clean/12463/12463-19.cpp @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() { + long long a, b, c, d, e; + while (cin >> a >> b >> c >> d >> e && + !(a == 0 && b == 0 && c == 0 && d == 0 && e == 0)) { + printf("%lld\n", a*b*c*d*d*e*e); + } + return 0; +} diff --git a/uva_cpp_clean/12463/12463-20.cpp b/uva_cpp_clean/12463/12463-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad9f1268e4dc4301ead6b2b53b99881992bc1897 --- /dev/null +++ b/uva_cpp_clean/12463/12463-20.cpp @@ -0,0 +1,14 @@ +#include +#include + +using namespace std; + +int main() +{ + int a, b, c, d, e; + + while( cin >> a >> b >> c >> d >> e && a+b+c+d+e!=0 ) + cout << a*b*c*d*d*e*e << endl; + + return 0; +} diff --git a/uva_cpp_clean/12464/12464-9.cpp b/uva_cpp_clean/12464/12464-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..912d3c4515d1f1d76d5df92b4468117862f868d5 --- /dev/null +++ b/uva_cpp_clean/12464/12464-9.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +const double EPS = 1e-9; + +int pos, mu, lambda; +long long alfa, beta, n; +double next_; +vector dp; + +bool check(){ + for(int i = 1; i < dp.size(); i++) + if( fabs(dp[i] - next_) < EPS and fabs(dp[i - 1] - dp.back()) < EPS ){ + mu = i - 1; + lambda = pos - mu - 1; + return true; + } + return false; +} + +int main(){ + while(scanf("%lld %lld %lld", &alfa, &beta, &n), alfa | beta | n){ + dp.clear(); + dp.push_back(alfa); + dp.push_back(beta); + pos = 2; + while( true ){ + next_ = ( 1.0 + dp[pos - 1] ) / dp[ pos - 2]; + if( check() ) break; + dp.push_back(next_); + pos++; + } + if( n < mu ) pos = n; + else pos = mu + ( n - mu ) % lambda; + printf("%lld\n", (long long)round(dp[pos])); + } + return(0); +} diff --git a/uva_cpp_clean/12467/12467-9.cpp b/uva_cpp_clean/12467/12467-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c2f5d6cb9dafdded932fd72296e5a9eb5c8871f --- /dev/null +++ b/uva_cpp_clean/12467/12467-9.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +vector prefixFunction (const string s) { + int n = s.size(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 and s[i] != s[j]) j = pi[j - 1]; + if (s[i] == s[j]) j++; + pi[i] = j; + } + return pi; +} + +int main () { + int tc; + string s; + cin >> tc; + while (tc--) { + cin >> s; + string rs = s; + reverse(begin(rs), end(rs)); + vector pi = prefixFunction(s + '#' + rs); + int key_len = *max_element(begin(pi) + s.size(), end(pi)); + string key = s.substr(0, key_len); + reverse(begin(key), end(key)); + cout << key << endl; + } + return (0); +} diff --git a/uva_cpp_clean/12468/12468-19.cpp b/uva_cpp_clean/12468/12468-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41e5d697f328aa15928547d531a8b5e474b86e5a --- /dev/null +++ b/uva_cpp_clean/12468/12468-19.cpp @@ -0,0 +1,12 @@ +#include +#include +using namespace std; + +int main() { + int a, b; + while (cin >> a >> b && !(a == -1 && b == -1)) { + int c1 = max(a, b) - min(a, b), c2 = min(a, b) + 100 - max(a, b); + printf("%d\n", min(c1, c2)); + } + return 0; +} diff --git a/uva_cpp_clean/12468/12468-2.cpp b/uva_cpp_clean/12468/12468-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..789858d026a9729324b03b493dcf59019b85acac --- /dev/null +++ b/uva_cpp_clean/12468/12468-2.cpp @@ -0,0 +1,42 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int a, b; + while(cin >> a >> b) { + if(a < 0) break; + printf("%d\n", min(abs(a-b), min(100-a+b, 100-b+a))); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12468/12468-21.cpp b/uva_cpp_clean/12468/12468-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbe993cbc36f9f9e47ab0f3283895cbaa8d391f8 --- /dev/null +++ b/uva_cpp_clean/12468/12468-21.cpp @@ -0,0 +1,23 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12468 + Name: Zapping + Problem: https://onlinejudge.org/external/124/12468.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int a, b; + while(cin>>a>>b && (a!=-1 || b!=-1)) { + int diff = a>b ? a-b : b-a; + cout << min(diff, 100-diff) << endl; + } +} diff --git a/uva_cpp_clean/12468/12468-5.cpp b/uva_cpp_clean/12468/12468-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a97db1538b17e5d922bbdd0eb5b8376e438ac85a --- /dev/null +++ b/uva_cpp_clean/12468/12468-5.cpp @@ -0,0 +1,15 @@ +#include +#include +int main() +{ + int a,b,i,c; + while(scanf("%d%d",&a,&b)==2&& a>=0&&b>=0) + { + c=abs(a-b); + if(c>=50) + c=100-c; + + printf("%d\n",c); + } + return 0; +} diff --git a/uva_cpp_clean/12468/12468-9.cpp b/uva_cpp_clean/12468/12468-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2c218fae5044c2f1641be8cfd2cdc53064f7bde --- /dev/null +++ b/uva_cpp_clean/12468/12468-9.cpp @@ -0,0 +1,14 @@ +#include +#define swap(a, b, temp) ( temp = a, a = b, b = temp ) + +int main(){ + int a, b, ans1, ans2, temp; + while(scanf("%d %d", &a, &b), a != -1, b != -1){ + if(a > b) swap(a,b,temp); + ans1 = b - a; + ans2 = 100 - b + a; + if(ans1 < ans2) printf("%d\n", ans1); + else printf("%d\n", ans2); + } + return(0); +} diff --git a/uva_cpp_clean/12468/main.cpp b/uva_cpp_clean/12468/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7981d33eda3b599d015ca0d7b8b62b8c99e602c --- /dev/null +++ b/uva_cpp_clean/12468/main.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +int main(){ + short a, b, rute1, rute2, maxim, minim; + while(cin >> a >> b && a != -1){ + maxim = max(a,b); + minim = min(a,b); + rute1 = maxim - minim; + rute2 = (99-maxim) + minim + 1; + cout << min(rute1 , rute2) << endl; + } +return 0; +} diff --git a/uva_cpp_clean/12469/12469-19.cpp b/uva_cpp_clean/12469/12469-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4219566f089b27116f8e7a19ff9ecf4c8979a94a --- /dev/null +++ b/uva_cpp_clean/12469/12469-19.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +using namespace std; + +unordered_map map; +unordered_map mini; + +int proc(int player, int n, int k) { + if (n <= k) return player; + if (map[n] == 0) map[n] = 2000000000; + if (map[n] <= k) return player; + else if (k <= mini[n]) return 1 - player; + int i = min(n/3, k); + mini[n] = max(i, mini[n]); + while (i > 0) { + if (proc(1-player, n-i, 2*i) == player) { + map[n] = i; + mini[n] = 0; + return player; + } + i--; + } + return 1 - player; +} + +int main() { + int n; + while (cin >> n && n != 0) { + if (proc(0, n, n-1)) printf("Roberto\n"); + else printf("Alicia\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12469/12469-9.cpp b/uva_cpp_clean/12469/12469-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdd2d7e515ff0f540a170dff87a0fd9b5aa68237 --- /dev/null +++ b/uva_cpp_clean/12469/12469-9.cpp @@ -0,0 +1,43 @@ +#include + +#define WIN 1 +#define FAIL -1 +#define UNVISITED -2 + +using namespace std; + +const int MAX_N = 1010; + +int8_t memo[MAX_N][MAX_N][2]; + +int minVal(int, int); +int maxVal(int, int); + +int minVal(int limit, int n) { + if (n == 0) return WIN; + if (memo[limit][n][0] != UNVISITED) return memo[limit][n][0]; + int ret = WIN; + for (int k = 1; k <= limit and ret != FAIL; k++) + ret = min(ret, maxVal(min(2 * k, n - k), n - k)); + return memo[limit][n][0] = ret; +} + +int maxVal(int limit, int n) { + if (n == 0) return FAIL; + if (memo[limit][n][1] != UNVISITED) return memo[limit][n][1]; + int ret = FAIL; + for (int k = 1; k <= limit and ret != WIN; k++) + ret = max(ret, minVal(min(2 * k, n - k), n - k)); + return memo[limit][n][1] = ret; +} + +bool minimax(int n) { + return maxVal(n - 1, n) == WIN; +} + +int main() { + int n; + memset(memo, UNVISITED, sizeof memo); + while (scanf("%d", &n), n) puts(minimax(n) ? "Alicia" : "Roberto"); + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/1247/1247-21.cpp b/uva_cpp_clean/1247/1247-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e01630d4802b6c04b7e3f31ebda8a22bb05f2d8 --- /dev/null +++ b/uva_cpp_clean/1247/1247-21.cpp @@ -0,0 +1,72 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1247 + Name: Interstar Transport + Problem: https://onlinejudge.org/external/12/1247.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +int D[26][26]={}, L[26][26], P[26][26]; +void printPath(int u, int v) { + if (P[u][v] == u) { + cout << char(u+'A') << ' '; + return; + } + printPath(u, P[u][v]); + printPath(P[u][v], v); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char c1, c2; + for (int n, m; cin >> n >> m; ) { + for (int i=0; i<26; ++i) { + for (int j=0; j<26; ++j) { + P[i][j] = i; + D[i][j] = L[i][j] = INF; + } + D[i][i] = 0; + } + + for (int i=0; i> c1 >> c2 >> d; + c1 -= 'A'; c2 -= 'A'; + D[c1][c2] = D[c2][c1] = d; + L[c1][c2] = L[c2][c1] = 1; + P[c1][c2] = c1; + P[c2][c1] = c2; + } + + for (int k=0; k<26; ++k) + for (int i=0; i<26; ++i) + if (i!=k && D[i][k]!=INF) + for (int j=0; j<26; ++j) + if (j!=k && D[j][k]!=INF) { + int d = D[i][k] + D[k][j], + l = L[i][k] + L[k][j]; + + if (d < D[i][j] || (d==D[i][j] && l < L[i][j])) { + D[i][j] = d; + L[i][j] = l; + P[i][j] = k; + } + } + + cin >> m; + while (m--) { + cin >> c1 >> c2; + printPath(c1-'A', c2-'A'); + cout << c2 << endl; + } + } +} diff --git a/uva_cpp_clean/12470/12470-19.cpp b/uva_cpp_clean/12470/12470-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..478f49a7dd48948c463f4298d226e6459d1e3f7c --- /dev/null +++ b/uva_cpp_clean/12470/12470-19.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; + +int mod = 1000000009; + +struct lll { + long long first; + long long second; + long long third; + lll (long long f, long long s, long long t) { + first = f; + second = s; + third = t; + } + lll () {} +}; +struct mat { + lll first; + lll second; + lll third; + mat () {} + mat (lll f, lll s, lll t) { + first = f; + second = s; + third = t; + } +}; + +mat prod(const mat & mn0, const mat & mn1) { + mat mn2(lll( + (mn0.first.first*mn1.first.first + mn0.first.second*mn1.second.first + mn0.first.third*mn1.third.first)%mod, + (mn0.first.first*mn1.first.second + mn0.first.second*mn1.second.second + mn0.first.third*mn1.third.second)%mod, + (mn0.first.first*mn1.first.third + mn0.first.second*mn1.second.third + mn0.first.third*mn1.third.third)%mod), + lll( + (mn0.second.first*mn1.first.first + mn0.second.second*mn1.second.first + mn0.second.third*mn1.third.first)%mod, + (mn0.second.first*mn1.first.second + mn0.second.second*mn1.second.second+mn0.second.third*mn1.third.second)%mod, + (mn0.second.first*mn1.first.third + mn0.second.second*mn1.second.third + mn0.second.third*mn1.third.third)%mod), + lll( + (mn0.third.first*mn1.first.first + mn0.third.second*mn1.second.first + mn0.third.third*mn1.third.first)%mod, + (mn0.third.first*mn1.first.second + mn0.third.second*mn1.second.second + mn0.third.third*mn1.third.second)%mod, + (mn0.third.first*mn1.first.third + mn0.third.second*mn1.second.third + mn0.third.third*mn1.third.third)%mod) + ); + return mn2; +} + +unordered_map dp; +mat mat1 = mat(lll(1, 1, 0), lll(1, 0, 1), lll(1, 0, 0)); + +mat pow(long long n) { + if (n == 0) return mat(lll(1, 0, 0), lll(0, 1, 0), lll(0, 0, 1)); + if (n == 1) return mat1; + if (!dp.count(n)) { + mat mn1 = pow(n/2); + mat mn2 = prod(mn1, mn1); + if (n%2) { + mn2 = prod(mn2, mat1); + } + dp[n] = mn2; + } + return dp[n]; +} + +int main() { + long long n; + while (cin >> n && n) { + long long val = pow(n).second.second%mod; + printf("%lld\n", val); + } + return 0; +} diff --git a/uva_cpp_clean/12470/12470-5.cpp b/uva_cpp_clean/12470/12470-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..77ae28bca1c103e4e910dcfda1bd9d74e24c9147 --- /dev/null +++ b/uva_cpp_clean/12470/12470-5.cpp @@ -0,0 +1,134 @@ +/****************************************************************** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_HIDDEN *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d",a) +#define PLN(a) printf("%lld",a) + + +#define ff first +#define se second +#define pb push_back +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +////============ CONSTANT ===============//// +#define mx (1000000) +#define inf (1<<30) //infinity value +#define eps 1e-9 +#define mod 1000000009 +////====================================//// + +struct Matrix +{ + ll mat[5][5]; +}; + +Matrix Mul(Matrix aa, Matrix bb) +{ + Matrix ret; + cover(ret.mat,0); + for(int i = 0; i < 3; i ++) + for(int j = 0; j < 3; j ++) + { + for(int k = 0; k < 3; k ++) + { + ret.mat[i][j] += (aa.mat[i][k]*bb.mat[k][j]); + ret.mat[i][j] %= mod; + } + } + return ret; +} + +Matrix exp(Matrix a, ll b) +{ + if(b == 1) return a; + if(b&1) + { + return Mul(a, exp(a,b-1)); + } + else + { + Matrix tmp = exp(a,b/2); + return Mul(tmp,tmp); + } +} + +int main() +{ + ll n; + Matrix base; + while(sll(n) && n) + { + base.mat[0][0]=0; base.mat[0][1]=0; base.mat[0][2]=1; + base.mat[1][0]=1; base.mat[1][1]=0; base.mat[1][2]=1; + base.mat[2][0]=0; base.mat[2][1]=1; base.mat[2][2]=1; + + Matrix res = exp(base, n); +// for(int i = 0; i < 3; i ++){ +// for(int j = 0; j < 3; j ++) +// PLN(res.mat[i][j]),pf(" "); +// puts(""); +// } + PL(res.mat[1][1]); + } + return 0; +} diff --git a/uva_cpp_clean/12478/12478-21.cpp b/uva_cpp_clean/12478/12478-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c29d5ad5748f90650e94e25542c4c5267c999d0 --- /dev/null +++ b/uva_cpp_clean/12478/12478-21.cpp @@ -0,0 +1,79 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12478 + Name: Hardest Problem Ever (Easy) + Problem: https://onlinejudge.org/external/124/12478.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char G[10][10] = { + "OBIDAIBKR", + "RKAULHISP", + "SADIYANNO", + "HEISAWHIA", + "IRAKIBULS", + "MFBINTRNO", + "UTOYZIFAH", + "LEBSYNUNE", + "EMOTIONAL" +}, +names[10][10] = { + "RAKIBUL", + "ANINDYA", + "MOSHIUR", + "SHIPLU", + "OBAIDA", + "KABIR", + "SUNNY", + "WASI" +}; + +bool match(int ii, int jj, int F[], int l) { + if (9-ii >= l) { + int C[100] = {}; + for (int i=0; i= l) { + int C[100] = {}; + for (int j=0; j + +// The hardest problem ever created: +int main (){ + printf ("%s\n","KABIR"); +} diff --git a/uva_cpp_clean/12478/12478-5.cpp b/uva_cpp_clean/12478/12478-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..166d3f609001ccbd2dc412ac1cb74bb3b17e12e4 --- /dev/null +++ b/uva_cpp_clean/12478/12478-5.cpp @@ -0,0 +1,6 @@ +#include +int main () +{ +printf("KABIR\n"); +return 0; +} diff --git a/uva_cpp_clean/12478/main.cpp b/uva_cpp_clean/12478/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..877d44a12ab10b36e7a3600a73df7ae8924e3646 --- /dev/null +++ b/uva_cpp_clean/12478/main.cpp @@ -0,0 +1,9 @@ +#include +using namespace std; + +int main(){ + + cout << "KABIR" << endl; + +return 0; +} diff --git a/uva_cpp_clean/12482/12482-21.cpp b/uva_cpp_clean/12482/12482-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..472fe8b9d2a4e9c638d269978579f29620453c2f --- /dev/null +++ b/uva_cpp_clean/12482/12482-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12482 + Name: Short Story Competition + Problem: https://onlinejudge.org/external/124/12482.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, p, l; + string word; + while (cin >> n >> p >> l) { + cin >> word; + int cl = 1, cc = word.length(); + for (int i=1; i> word; + cc += 1 + word.length(); + if (cc > l) { + cc = word.length(); + ++cl; + } + } + + cout << (cl+p-1)/p << '\n'; + } +} diff --git a/uva_cpp_clean/12482/12482-5.cpp b/uva_cpp_clean/12482/12482-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..343105a65798dda3d72c2af9a3d709ced5705493 --- /dev/null +++ b/uva_cpp_clean/12482/12482-5.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + int n,l,c; + string s; + while(cin>>n>>l>>c) + { + int len,line=1,page=0; + cin>>s; + len=s.size(); + for(int i=0;i>s; + if((len+s.size()+1)<=c) + { + len+=s.size()+1; + } + else + { + line++; + len=s.size(); + } + } + page=line/l; + if(line%l) + page++; + cout< game theory > minimax + difficulty: medium + date: 28/Mar/2020 + problem: alberto and wanderley take one of two cards at the edges of the cards sequence, alberto want maximize it + hint: fill memo table in row-major order + by: @brpapa +*/ +#include +using namespace std; + +typedef long long ll; +const int pALB = 0; +const int pWAN = 1; + +int A[10010]; + +ll memo[10010][10010]; +ll minimax(bool p, int l, int r) { + // cartas atuais A[l:r], jogador atual p + + if (l > r) return 0ll; + + ll &ans = memo[r][l]; // r >= l + if (ans != -1) return ans; + + if (p == pALB) { + return ans = max( + (ll)A[l] + minimax(!p, l+1, r), + (ll)A[r] + minimax(!p, l, r-1) + ); + } + else { + // pWAN escolhe estado em que pALB recebe menos + return ans = min( + minimax(!p, l+1, r), + minimax(!p, l, r-1) + ); + } +} + +int main() { + int N; + while (cin >> N) { + for (int i = 0; i < N; i++) { + cin >> A[i]; + for (int j = 0; j <= i; j++) memo[i][j] = -1; + } + cout << minimax(pALB, 0, N-1) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12485/12485-19.cpp b/uva_cpp_clean/12485/12485-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80b3f2a8665cec3d1ae0a3cf33033f930a45fe14 --- /dev/null +++ b/uva_cpp_clean/12485/12485-19.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; + +int main() { + int v[10002]; + int N; + while (scanf("%d", &N) != EOF) { + int mean = 0; + for (int i = 0; i < N; i++) { + int note; + scanf("%d", ¬e); + v[i] = note; + mean += note; + } + if (mean % N != 0) printf("-1\n"); + else { + mean /= N; + int moves = 1; + for (int i = 0; v[i] < mean; i++) { + moves += mean - v[i]; + } + printf("%d\n", moves); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12485/12485-21.cpp b/uva_cpp_clean/12485/12485-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..545f6c0320d3a14499e4f585dfa60796acf38617 --- /dev/null +++ b/uva_cpp_clean/12485/12485-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12485 + Name: Perfect Choir + Problem: https://onlinejudge.org/external/124/12485.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[10143], n; + while (cin >> n) { + int sum = 0; + for (int i=0; i> X[i]; + sum += X[i]; + } + + if (sum % n) cout << "-1\n"; + else { + int avg = sum / n; + sum = 0; + for (int i=0; i>1)+1 << "\n"; + } + } +} diff --git a/uva_cpp_clean/12486/12486-14.cpp b/uva_cpp_clean/12486/12486-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..385571e81eb6e39a114850923ec4cdf424c219bc --- /dev/null +++ b/uva_cpp_clean/12486/12486-14.cpp @@ -0,0 +1,132 @@ +/*************************************************** + * Problem Name : 12486 - Space Elevator.cpp + * Problem Link : https://uva.onlinejudge.org/external/124/12486.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : Digit DP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +ll c; +ll dp[30][11][2]; +ll digitDP (int pos, int pre, int isSuru) { + if (pos == 20) { + if (isSuru == 1) return 1LL; + else return 0LL; + } + + ll &ans = dp[pos][pre][isSuru]; + + if (ans != -1) return ans; + + ans = 0; + int low = 0, high = 9; + + for (int i = low; i <= high; i++) { + if (i == 4) continue; + + if (pre == 1 && i == 3) continue; + + ans += digitDP (pos + 1, i, isSuru | (i > 0) ); + } + + return ans; +} + +void print (int pos, int pre, int isSuru) { + if (pos == 20) { + return ; + } + + int low = 0, high = 9; + + for (int i = low; i <= high; i++) { + if (i == 4) continue; + + if (pre == 1 && i == 3) continue; + + ll cnt = digitDP (pos + 1, i, isSuru | (i > 0) ); + + if (cnt >= c) { + if (isSuru == 1 || (i > 0) ) { + cout << i; + } + + print (pos + 1, i, isSuru | (i > 0) ); + return; + + } else { + c -= cnt; + } + } +} + +int main () { + __FastIO; + ll n; + SET (dp, -1); + digitDP (0, 0, 0); + + while (cin >> n) { + c = n; + print (0, 0, 0); + nl; + } + + return 0; +} diff --git a/uva_cpp_clean/12488/12488-21.cpp b/uva_cpp_clean/12488/12488-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f635f1991278dbcb3f8fdc0f08ff1fc0c270d325 --- /dev/null +++ b/uva_cpp_clean/12488/12488-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12488 + Name: Start Grid + Problem: https://onlinejudge.org/external/124/12488.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int X[43], Y[43], n; + while (cin >> n) { + for (int i=0; i> X[i]; + for (int j=0, y; j> y; + Y[y] = j; + } + + int cnt = 0; + for (int i=0; i Y[X[j]]) + swap(X[j-1], X[j]), + ++cnt; + cout << cnt << '\n'; + } +} diff --git a/uva_cpp_clean/12488/12488-5.cpp b/uva_cpp_clean/12488/12488-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c9132e6b953ae5d8dedff2a2fa9375587539ee13 --- /dev/null +++ b/uva_cpp_clean/12488/12488-5.cpp @@ -0,0 +1,212 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +struct track +{ + int value,pos; + +}; + + +int main() +{ + + int n; + while(si(n)==1) + { + track a[100],b[100]; + + for0(i,n) + { + int p; + si(p); + a[i].pos=i+1; + a[i].value=p; + } + for0(i,n) + { + int p; + si(p); + b[i].pos=i+1; + b[i].value=p; + } + +// for0(i,n) +// { +// PP(b[i].value,b[i].pos); +// } + + int res=0; + for0(i,n) + { + for0(j,n) + { + if(a[i].value == b[j].value ) + { +// PN(a[i].value),PP(a[i].pos,b[i].pos); + res+=abs(a[i].pos-b[j].pos); +// P(res); + b[j].value=-1; + break; + } + else + { + b[j].pos++; + } + + } + + } + + P(res); + + } + + return 0; +} diff --git a/uva_cpp_clean/12488/12488-9.cpp b/uva_cpp_clean/12488/12488-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a9c40d04dd0ae78c04b530a7ec74d363f427ef9 --- /dev/null +++ b/uva_cpp_clean/12488/12488-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int n, s[25], f[25], p, ans; + +inline int position(int num){ + for(int i = 0; i < n; i++) + if(f[i] == num) return i; +} + +inline void minMov(int i){ + p = position(s[i]); + ans += p - i; + for(int ct = p; ct > i; ct--) + swap(f[ct], f[ct - 1]); +} + +int main(){ + while(scanf("%d", &n) == 1){ + ans = 0; + for(int i = 0; i < n; i++) scanf("%d", &s[i]); + for(int i = 0; i < n; i++) scanf("%d", &f[i]); + for(int i = 0; i < n; i++) minMov(i); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/12498/12498-21.cpp b/uva_cpp_clean/12498/12498-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c0ae9262a0ab5520a1bc8d1fb269b515fd68c3a --- /dev/null +++ b/uva_cpp_clean/12498/12498-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12498 + Name: Ant's Shopping Mall + Problem: https://onlinejudge.org/external/124/12498.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char M[55][55]; + int T, n, m, D[55][55]; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n >> m; + for (int i=0; i> M[i]; + + for (int i=0; i=0; --j) { + if (M[i][j] == '0') + d = 0; + else ++d; + D[i][j] = min(D[i][j], d); + } + } + + int mxs = INF; + for (int j=0; j +#include + +using namespace std; + +char shoppingMall[50][50]; +int column,row,t, + leftBound, rightBound, + result, tmp; +bool ok; + +int main() +{ + scanf("%d",&t); + for ( int cs = 0; cs < t; cs++ ) + { + scanf("%d %d",&row, &column); + for ( int r = 0; r < row; r++ ) + scanf("%s",shoppingMall[r]); + + result = -1; + for ( int c = 0; c < column; c++ ) + { + tmp = 0; + ok = true; + for ( int r = 0; r < row; r++ ) + { + if ( shoppingMall[r][c] == '0' ) continue; + + leftBound = rightBound = -1; + + if ( c > 0 ) + for ( int k = c; k >= 0; k-- ) + if ( shoppingMall[r][k] == '0' ) + { + leftBound = c - k ; + break; + } + + if ( c + 1 < column ) + for ( int k = c; k < column; k++ ) + if ( shoppingMall[r][k] == '0' ) + { + rightBound = k - c; + break; + } + if ( leftBound == -1 && rightBound == -1 ) + ok = false; + else if ( leftBound == -1 ) + tmp += rightBound; + else if ( rightBound == -1 ) + tmp += leftBound; + else + tmp += min ( rightBound, leftBound ); + } + + if ( ok && ( ( result == -1 ) || ( tmp < result ) ) ) result = tmp; + if (!ok) continue; + } + + printf("Case %d: %d\n", cs+1, result); + } +} diff --git a/uva_cpp_clean/12498/12498-9.cpp b/uva_cpp_clean/12498/12498-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e57102460f7f9edbfab260326b81398e719ad611 --- /dev/null +++ b/uva_cpp_clean/12498/12498-9.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int tc, R, C, m, ans, tmp, aux; +vector v[55]; + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + ans = -1; + scanf("%d %d", &R, &C); + for(int i = 0; i < R; i++) + for(int j = 0; j < C; j++){ + scanf("%1d", &m); + if(!m) v[i].push_back(j); + } + for(int i = 0; i < R; i++){ + if(!v[i].size()){ + ans = -1; + break; + } + for(int j = 0; j < v[i].size(); j++){ + aux = 0; + for(int p = 0; p < R; p++){ + tmp = 100000; + for(int q = 0; q < v[p].size(); q++) + tmp = min(tmp, abs(v[p][q] - v[i][j])); + aux += tmp; + } + if(aux>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 125 + Name: Numbering Paths + Problem: https://onlinejudge.org/external/1/125.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int M[143][143]; +int main() { + int m, u, v; + for (int cse=0; cin >> m; ++cse) { + int n = 0; + for (int i=0; i> u >> v; + int nn = max(u, v) + 1; + for (; n < nn; ++n) + for (int j=0; j<=n; ++j) + M[n][j]=M[j][n]=0; + M[u][v] = 1; + } + + for (int k=0; k +using namespace std; + +int main() { + int T; + scanf("%d", &T); + while (T--) { + double x, y, z; + scanf("%lf %lf %lf", &x, &y, &z); + printf("%d\n", int(z*(2*x - y)/(x+y))); + } + return 0; +} diff --git a/uva_cpp_clean/12502/12502-21.cpp b/uva_cpp_clean/12502/12502-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a246a5140930fa84794fce19573ae541dcb83d6 --- /dev/null +++ b/uva_cpp_clean/12502/12502-21.cpp @@ -0,0 +1,24 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12502 + Name: Three Families + Problem: https://onlinejudge.org/external/125/12502.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T, a, b, m; + cin>>T; + while(T--) { + cin>>a>>b>>m; + cout << (3*a*m)/(a+b)-m << endl; + } +} diff --git a/uva_cpp_clean/12502/12502-5.cpp b/uva_cpp_clean/12502/12502-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5a41b6074c456f743656bf0982374d3a8c89508b --- /dev/null +++ b/uva_cpp_clean/12502/12502-5.cpp @@ -0,0 +1,17 @@ +#include +#include +int main() +{ + int a,b,c,n,e,d; + scanf("%d",&n); + while(n--) + { + scanf("%d%d%d",&a,&b,&c); + e=c*(a+(a-b))/(a+b); + if(e<=0) + printf("0\n"); + else + printf("%d\n",e); + } + return 0; +} diff --git a/uva_cpp_clean/12502/12502-9.cpp b/uva_cpp_clean/12502/12502-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..330beb68c3c188d76540085361000005373ef1d0 --- /dev/null +++ b/uva_cpp_clean/12502/12502-9.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main(){ + int c, a, b, m; + scanf("%d", &c); + while(c--){ + scanf("%d %d %d", &a, &b, &m); + printf("%d\n", (a + a - b) * m / (a + b)); + } + return(0); +} diff --git a/uva_cpp_clean/12503/12503-14.cpp b/uva_cpp_clean/12503/12503-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54c7000dfdc4e81954fc48006827d5fbd35cbf12 --- /dev/null +++ b/uva_cpp_clean/12503/12503-14.cpp @@ -0,0 +1,91 @@ +/*************************************************** + * Problem name : 12503 Robot Instructions.cpp + * Problem Link : https://uva.onlinejudge.org/external/125/12503.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-05 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ __FastIO; + int tc, n; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d", &n); + mapmp; + int pos = 0; + for (int i = 1; i <= n; i++) { + string str; + cin >> str; + if (str == "LEFT") { + mp[i] = "LEFT"; + pos = pos - 1; + + } else if (str == "RIGHT") { + mp[i] = "RIGHT"; + pos = pos + 1; + } else { + int x; + cin >> str >> x; + string s = mp[x]; + if (s == "LEFT") { + pos = pos - 1; + mp[i] = "LEFT"; + } else { + mp[i] = "RIGHT"; + pos = pos + 1; + } + } + } + printf("%d\n", pos); + } + + return 0; +} diff --git a/uva_cpp_clean/12503/12503-2.cpp b/uva_cpp_clean/12503/12503-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86785ef8688bb372eb9f9bd43cb94b02294254b9 --- /dev/null +++ b/uva_cpp_clean/12503/12503-2.cpp @@ -0,0 +1,60 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; cin >> tc; + while (tc--) { + int n; cin >> n; + vector v; + int position = 0; + while ( n-- ) { + string inst; + cin >> inst; + if (inst == "LEFT") { + --position; + v.push_back(-1); + } else if (inst == "RIGHT") { + ++position; + v.push_back(1); + } else { + int i; + cin >> inst >> i; + position += v[i - 1]; + v.push_back(v[i - 1]); + } + } + cout << position << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12503/12503-21.cpp b/uva_cpp_clean/12503/12503-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e26d27730cd3a786b3c07760abe9109f3e9cef3 --- /dev/null +++ b/uva_cpp_clean/12503/12503-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12503 + Name: Robot Instructions + Problem: https://onlinejudge.org/external/125/12503.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int insts[110]; + +int main(){ + int T, n, x; + char cm[20]; + cin>>T; + while (T--) { + cin>>n; + int p = 0; + for (int i=0; i>cm; + if (cm[0]=='R' || cm[0]=='r') + p += (insts[i] = 1); + else if (cm[0]=='L' || cm[0]=='l') + p += (insts[i] = -1); + else { + cin >> cm >> x; + p += (insts[i] = insts[x - 1]); + } + } + cout << p << endl; + } +} diff --git a/uva_cpp_clean/12503/12503-5.cpp b/uva_cpp_clean/12503/12503-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..dc336bb084c9a3bfe13c9bba8599be1704abd314 --- /dev/null +++ b/uva_cpp_clean/12503/12503-5.cpp @@ -0,0 +1,37 @@ +#include +#include +int main() +{ + char a[30]; + int t,n,s,e,i,b[110]; + while(scanf("%d",&t)==1) + { + while(t--) + { + scanf("%d",&n); + s=0; + for(i=1;i<=n;i++) + { + scanf("%s",a); + if(strcmp(a,"LEFT")==0) + { + s-=1; + b[i]=-1; + } + else if(strcmp(a,"RIGHT")==0) + { + s+=1; + b[i]=1; + } + else + { + scanf("%s%d",a,&e); + s+=b[e]; + b[i]=b[e]; + } + } + printf("%d\n",s); + } + } +return 0; +} diff --git a/uva_cpp_clean/12503/12503-9.cpp b/uva_cpp_clean/12503/12503-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8e31f34f08ad6e8ea8cdb8f8a127123ac6da840 --- /dev/null +++ b/uva_cpp_clean/12503/12503-9.cpp @@ -0,0 +1,25 @@ +#include +#include + +int main(){ + int n, N, i, j; + char p, a[100][20], aux[20]; + scanf("%d", &N); + while(N--){ + p = 0; + scanf("%d",&n); + for(i = 0; i < n; i++){ + scanf("%s", a[i]); + if(!strcmp(a[i], "SAME")){ + scanf("%s", aux); + scanf("%d", &j); + strcpy(a[i], a[j-1]); + } + } + for(i = 0; i < n; i++) + if(!strcmp(a[i], "LEFT")) p--; + else p++; + printf("%d\n", p); + } + return(0); +} diff --git a/uva_cpp_clean/12503/main.cpp b/uva_cpp_clean/12503/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ffd4275371c0d8959da8e051fa11fbf205128bf --- /dev/null +++ b/uva_cpp_clean/12503/main.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +using namespace std; + +int main(){ + short cases, n, p, traduction; + string instruction; + vector secuence; + + cin >> cases; + for(short i=0; i < cases; i++){ + p = 0; + secuence.clear(); + cin >> n; + for(short c=0; c < n; c++){ + cin >> instruction; + if(instruction == "LEFT") secuence.push_back(-1); + else if(instruction == "RIGHT") secuence.push_back(1); + else{ + cin >> instruction >> traduction; + secuence.push_back(secuence[traduction-1]); + } + } + for(short c=0; c < n; c++){ + p += secuence[c]; + } + cout << p << endl; + } +return 0; +} diff --git a/uva_cpp_clean/12504/12504-11.cpp b/uva_cpp_clean/12504/12504-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bbbeb245eb9ffe596430357aef209939fa30a48 --- /dev/null +++ b/uva_cpp_clean/12504/12504-11.cpp @@ -0,0 +1,151 @@ +#include +/* +Problem: 12504 - Updating a Dictionary +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=3948 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +#define endl '\n' + +using namespace std; + +int T = 0; + +void input_dict(map & dict); +void out_set(set & keys, char ch); + +int main () { + scanf("%d\n", &T); + + while ( T-- ){ + map ::iterator it; + map ::iterator old_it; + + map old_dict, new_dict; + set updated_keys, new_keys, removed_keys; + + input_dict(old_dict); + input_dict(new_dict); + + it = new_dict.begin(); + while (it != new_dict.end()){ + it++; + } + + //check for new, updated keys first + it = new_dict.begin(); + while (it != new_dict.end()){ + old_it = old_dict.find(it->first); + + if (old_it == old_dict.end()){ + new_keys.insert(it->first); + } + + else if (old_it->first == it->first){ + if (old_it->second != it->second) + updated_keys.insert(it->first); + } + + it++; + } + + //check for removed now + it = old_dict.begin(); + while ( it != old_dict.end() ){ + if (new_dict.find(it->first) == new_dict.end()) removed_keys.insert(it->first); + it++; + } + + if ( (new_keys.size() == 0 && updated_keys.size() == 0 && removed_keys.size() == 0) ) + cout << "No changes" << endl; + + else { + // now print +new, -removed, *updated, respectively + + out_set(new_keys, '+'); + out_set(removed_keys, '-'); + out_set(updated_keys, '*'); + } + + cout << endl; + } + + return 0; +} + +void out_set(set & keys, char ch){ + set ::iterator it; + + if (keys.size() != 0){ + cout << ch; + + for (it=keys.begin(); it != keys.end(); ++it){ + cout << *it; + it++; if (it != keys.end()) cout << ","; it--; + + }cout << endl; + } +} + +void input_dict(map & dict){ + // Take a dictionary reference and update it's keys & values from stdin + // Returns the keys as std::set + + string key = "", value = "", line = ""; + getline(cin, line); + if (line == "{}") return; + + int idx = 0; + while (line[idx] != '}'){ + idx++; + + key = "", value = ""; + while (line[idx] != ':'){ + key += line[idx]; + idx++; + } + + idx++; + while (line[idx] != ',' && line[idx] != '}'){ + value += line[idx]; + idx++; + } + + dict[key] = value; + } +} + +/* +Sample input:- +----------------- +3 +{a:3,b:4,c:10,f:6} +{a:3,c:5,d:10,ee:4} +{x:1,xyz:123456789123456789123456789} +{xyz:123456789123456789123456789,x:1} +{first:1,second:2,third:3} +{third:3,second:2} + +Sample output:- +----------------- ++d,ee +-b,f +*c + +No changes + +-first + +Resources:- +------------- +https://stackoverflow.com/questions/26281979/c-loop-through-map +https://stackoverflow.com/questions/10038985/remove-a-key-from-a-c-map +https://stackoverflow.com/questions/110157/how-to-retrieve-all-keys-or-values-from-a-stdmap-and-put-them-into-a-vector +https://stackoverflow.com/questions/7763397/c-pass-a-map-by-reference-into-function +http://www.cplusplus.com/reference/map/map/find/ +*/ diff --git a/uva_cpp_clean/12504/12504-14.cpp b/uva_cpp_clean/12504/12504-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1556964675644837c4195122a89013e60180094f --- /dev/null +++ b/uva_cpp_clean/12504/12504-14.cpp @@ -0,0 +1,244 @@ +/*************************************************** + * Problem Name : 12504 - Updating a Dictionary.cpp + * Problem Link : https://onlinejudge.org/external/125/12504.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-11-15 + * Problem Type : + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int tc; + cin >> tc; + cin.ignore(); + + while (tc--) { + mapold_d, new_d; + string str, nstr; + //~ cin >> str; + //~ cin >> nstr; + getline (cin, str); + getline (cin, nstr); + string key, val; + + for (int i = 1; i < (int) str.size(); i++) { + if (isalpha (str[i]) ) { + key += str[i]; + + } else if (isdigit (str[i]) ) { + val += str[i]; + + } else if (str[i] == ',') { + old_d[key] = val; + key = "", val = ""; + } + } + + if (!key.empty() ) old_d[key] = val; + + key = "", val = ""; + + for (int i = 1; i < (int) nstr.size(); i++) { + if (isalpha (nstr[i]) ) { + key += nstr[i]; + + } else if (isdigit (nstr[i]) ) { + val += nstr[i]; + + } else if (nstr[i] == ',') { + new_d[key] = val; + key = "", val = ""; + } + } + + if (!key.empty() ) new_d[key] = val; + + vectoradd, min, cng; + + for (auto it : old_d) { + //~ cout << it.F << " : " << it.S << "\n"; + if (new_d.find (it.F) == new_d.end() ) { + min.pb (it.F); + } + } + + //~ debug; + + for (auto it : new_d) { + if (old_d.find (it.F) == old_d.end() ) { + add.pb (it.F); + + } else if (it.S != old_d[it.F]) { + cng.pb (it.F); + } + + //~ cout << it.F << " : " << it.S << "\n"; + } + + bool f = 0; + + if ( (int) add.size() >= 1) { + f = 1; + cout << "+"; + + for (int i = 0; i < (int) add.size(); i++) { + cout << add[i]; + + if (i != (int) add.size() - 1) { + cout << ","; + } + } + + nl; + } + + if ( (int) min.size() >= 1) { + f = 1; + cout << "-"; + + for (int i = 0; i < (int) min.size(); i++) { + cout << min[i]; + + if (i != (int) min.size() - 1) { + cout << ","; + } + } + + nl; + } + + if ( (int) cng.size() >= 1) { + f = 1; + cout << "*"; + + for (int i = 0; i < (int) cng.size(); i++) { + cout << cng[i]; + + if (i != (int) cng.size() - 1) { + cout << ","; + } + } + + nl; + } + + if (!f) { + cout << "No changes\n"; + } + + nl; + } + + return 0; +} diff --git a/uva_cpp_clean/12504/12504-21.cpp b/uva_cpp_clean/12504/12504-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d43ec4148f953462250a9c643dccbf4c98b986b --- /dev/null +++ b/uva_cpp_clean/12504/12504-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12504 + Name: Updating a Dictionary + Problem: https://onlinejudge.org/external/125/12504.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +int main(){ + int T; + scanf("%d", &T); + while (T--) { + map mp; + char key[200], val[200]; + + scanf(" %*c"); + char ch = ','; + while (ch != '}') { + if (scanf("%[a-z]:%[0-9]%c", key, val, &ch) != 3) { getchar(); break; } + mp[key] = val; + } + + scanf(" %*c"); + ch = ','; + while (ch!='}') { + if (scanf("%[a-z]:%[0-9]%c", key, val, &ch) != 3) { getchar(); break; } + auto it = mp.find(key); + if (it == mp.end()) + mp[key] = "+"; + + else if (it->second != val) + it->second = "*"; + + else mp.erase(it); + } + + + vector ins, rem, chg; + for (auto p: mp) + if (p.second == "+") + ins.push_back(p.first); + + else if (p.second=="*") + chg.push_back(p.first); + + else rem.push_back(p.first); + + if (ins.empty() && rem.empty() && chg.empty()) + puts("No changes"); + + else { + if (ins.size()) { + cout << '+' << ins[0]; + for (int i=1; i +#include +#include + +int resolve(std::vector &names, int index) +{ + if (names.size() == 1) + { + if (index == 0) + { + return 1; + } + + return index; + } + + std::map> letters; + + for (unsigned int i = 0; i < names.size(); i++) + { + letters[names[i]->at(index)].push_back(names.at(i)); + } + + int sum = 0; + + for (auto &pair : letters) + { + sum += resolve(pair.second, index + 1); + } + + return sum; +} + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + int n; + std::cin >> n; + + std::string names[n]; + std::vector names_pointers; + + for (int i = 0; i < n; i++) + { + std::string name; + std::cin >> names[i]; + names_pointers.push_back(names + i); + } + + std::cout << resolve(names_pointers, 0) << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12515/12515-21.cpp b/uva_cpp_clean/12515/12515-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d19394586f08d56ebfcdfa929ed48cd9247d415a --- /dev/null +++ b/uva_cpp_clean/12515/12515-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12515 + Name: Movie Police + Problem: https://onlinejudge.org/external/125/12515.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +string MM[1143], C; +int diff(string &M, int bd) { + int len = M.length(), + clen = C.length(); + if (len < clen) + return (int)1e9; + + int rd = bd, + mi = len-clen; + for (int i=0; i<=mi && rd; ++i) { + int d = 0; + for (int j=0; j= rd) + break; + rd = min(rd, d); + } + return rd; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, q; + while (cin >> n >> q) { + for (int i=0; i> MM[i]; + + while (q--) { + cin >> C; + int result = -1; + int bd = (int)1e9; + for (int i=0; i + +using namespace std; + +string sig, movies[1000]; +int movie, diffs, res, + M, Q, repB; + +int main() +{ + cin >> M >> Q; + cin >> ws; + for ( int l = 0; l < M; l++ ) + getline ( cin, movies[l]); + + while ( Q-- ) + { + getline(cin, sig); + res = -1; + for ( int d = 0; d < M; d++ ) + for ( int k = 0, repB = movies[d].size() - sig.size(); k <= repB; k++ ) + { + diffs = 0; + + for ( int s = k; s < sig.size() + k; s++ ) + if ( movies[d][s] != sig[s-k] ) diffs++; + + if ( ( res == -1 ) || ( diffs < res ) ) + { + res = diffs; + movie = d+1; + } + } + cout << movie << endl; + } +} diff --git a/uva_cpp_clean/12515/12515-9.cpp b/uva_cpp_clean/12515/12515-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..959562afa69f670bb0932645aee8c8cb4167c619 --- /dev/null +++ b/uva_cpp_clean/12515/12515-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int M, Q, ct, _min, ans; +char s[1010][510], v[510]; + +int main(){ + scanf("%d %d", &M, &Q); + getchar(); + for(int i = 0; i < M; i++) + scanf("%s", s[i]); + while(Q--){ + scanf("%s", v), _min = INT_MAX; + for(int i = 0; i < M; i++) + for(int pib = 0; pib < (int)strlen(s[i]) - (int)strlen(v) + 1; pib++){ + ct = 0; + for(int j= pib; j < pib + strlen(v);j++) + if(s[i][j] != v[j - pib]) ct++; + if(ct < _min) _min = ct, ans = i + 1; + } + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/12517/12517-19.cpp b/uva_cpp_clean/12517/12517-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45305f09eece720b009ebd5aed2c37b347795779 --- /dev/null +++ b/uva_cpp_clean/12517/12517-19.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + +long long sum(long long n) { + long long ret = 0; + while (n) { + ret += n % 10; + n /= 10; + } + return ret; +} + +long long proc(long long n) { + if (n <= 0) return 0; + long long ret = 0; + ret += 45 * (n / 10) + ((n % 10 + 1) * (n % 10) / 2); + ret += 10 * proc(n / 10 - 1) + (n % 10 + 1) * sum(n / 10); + return ret; +} + +int main() { + long long M, N; + while (cin >> M >> N && !(N == 0 && M == 0)) { + printf("%lld\n", proc(N) - proc(M - 1)); + } + return 0; +} diff --git a/uva_cpp_clean/12526/12526-4.cpp b/uva_cpp_clean/12526/12526-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fc08233440a9cb1db57dd8e803bdbf332983754 --- /dev/null +++ b/uva_cpp_clean/12526/12526-4.cpp @@ -0,0 +1,114 @@ +#include +#include +#include + +using namespace std; + +class Node +{ +public: + char value = 0; + vector children; + int FindChild(char value); +}; + +int Node::FindChild(char value) +{ + for (unsigned int i = 0; i < children.size(); i++) + if (children[i].value == value) + return i; + + return -1; +} + +class Trie +{ +public: + void Add(string word); + int Solve(); + +private: + Node root; + void AddRecursively(Node &node, string word, unsigned int index); + int SolveRecursively(Node &node, int count); +}; + +void Trie::Add(string word) +{ + AddRecursively(root, word, 0); +} + +int Trie::Solve() +{ + if (root.children.size() < 2) + return SolveRecursively(root, 1); + else + return SolveRecursively(root, 0); +} + +void Trie::AddRecursively(Node &node, string word, unsigned int index) +{ + if (index > word.size()) + return; + + char value; + if (index == word.size()) + value = '\0'; + else + value = word[index]; + + int next = node.FindChild(value); + + if (next != -1) + { + AddRecursively(node.children[next], word, index + 1); + return; + } + + Node n; + n.value = value; + node.children.push_back(n); + + AddRecursively(node.children[node.children.size() - 1], word, index + 1); +} + +int Trie::SolveRecursively(Node &node, int count) +{ + if (node.children.size() == 0) + return count; + else if (node.children.size() == 1) + return SolveRecursively(node.children[0], count); + else + { + int partialsum = 0; + for (unsigned int i = 0; i < node.children.size(); i++) + { + if (node.children[i].value != '\0') + partialsum += SolveRecursively(node.children[i], count + 1); + else + partialsum += SolveRecursively(node.children[i], count); + } + + return partialsum; + } +} + +int main() +{ + int count; + while (cin >> count) + { + Trie trie; + + string temp; + for (int i = 0; i < count; i++) + { + cin >> temp; + trie.Add(temp); + } + + cout << setprecision(2) << fixed << trie.Solve() / (double)count << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12527/12527-19.cpp b/uva_cpp_clean/12527/12527-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..143eac1dc4f166b58e34fae98911eeb25a4b2d9e --- /dev/null +++ b/uva_cpp_clean/12527/12527-19.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +using namespace std; + +int main() { + vector v; + int mask[10]; + for (int i = 1; i <= 5001; i++) { + int ch = i; + for (int j = 0; j < 10; j++) mask[j] = false; + bool ok = true; + while (ch && ok) { + int val = ch%10; + ch /= 10; + ok = !mask[val]; + mask[val] = true; + } + if (ok) v.push_back(i); + } + int N, M; + while (cin >> N >> M) { + printf("%ld\n", upper_bound(v.begin(), v.end(), M) - lower_bound(v.begin(), v.end(), N)); + } + return 0; +} diff --git a/uva_cpp_clean/12527/12527-20.cpp b/uva_cpp_clean/12527/12527-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5576df9d437e8282f0256cd621edc5cf201cd703 --- /dev/null +++ b/uva_cpp_clean/12527/12527-20.cpp @@ -0,0 +1,34 @@ +#include +#include + +using namespace std; + +bool digit_check(int num) +{ + int digit[10]; + for(int i=0 ; i<10 ; i++) digit[i] = 0; + + while( num!=0 ) + { + if( digit[num%10]!=0 ) return true; + digit[num%10] = 1; + num /= 10; + } + return false; +} + +int main() +{ + int N, M; + + while( cin >> N >> M ) + { + int counter = 0; + for(int i=N ; i<=M ; i++) + if( digit_check(i)==false ) + counter++; + + cout << counter << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12527/12527-21.cpp b/uva_cpp_clean/12527/12527-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af5bc9f9251fd0dce00b5908066ff468801e158a --- /dev/null +++ b/uva_cpp_clean/12527/12527-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12527 + Name: Different Digits + Problem: https://onlinejudge.org/external/125/12527.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int isLucky(int n) { + int dis[4], dc=0; + for (; n; n/=10) { + int d = n%10; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int f; +void digit(int n) +{ + int j=0,a[100]; + while(n) + { + a[j++]=n%10; + n=n/10; + } + f=1; + for(int i=0;i + +using namespace std; + +bool rep_digit(int n){ + vector x; + while(n) + x.push_back(n % 10), n /= 10; + for(int i = 0; i < x.size(); i++) + for(int j= i + 1;j < x.size(); j++) + if(x[i] == x[j]) + return false; + return true; +} + +int main(){ + int n, m, ans; + while(scanf("%d %d", &n, &m) == 2){ + ans = 0; + for(int i = n; i <= m; i++) + ans += rep_digit(i); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/12531/12531-21.cpp b/uva_cpp_clean/12531/12531-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96923dcda822ceafcb5a692ad5da748a64cc4294 --- /dev/null +++ b/uva_cpp_clean/12531/12531-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12531 + Name: Hours and Minutes + Problem: https://onlinejudge.org/external/125/12531.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + bool seen[181] = {}; + int h=0, m=0; + for (int i=1; i<=720; ++i) { + if (i%12==0) + h = (h + 1) % 60; + m = (m + 1) % 60; + + int d = fabs(m-h); + if (d > 30) d -= 30; + seen[d * 6] = true; + } + + while (cin >> m) + cout << (seen[m] ? "Y\n" : "N\n"); +} diff --git a/uva_cpp_clean/12531/12531-5.cpp b/uva_cpp_clean/12531/12531-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ec738cf4689bea8f9d3ab11c0876e19a328a835c --- /dev/null +++ b/uva_cpp_clean/12531/12531-5.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int t; + while(sc("%d",&t)==1) + { + if(t%6==0) + pf("Y\n"); + else + pf("N\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12532/12532-14.cpp b/uva_cpp_clean/12532/12532-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1119e591b9aa0cbd1a47a6076bc77951eced91f2 --- /dev/null +++ b/uva_cpp_clean/12532/12532-14.cpp @@ -0,0 +1,170 @@ +/*************************************************** + * Problem name : 12532-Interval Product.cpp + * Problem Link : https://uva.onlinejudge.org/external/125/12532.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-01 + * Problem Type : Sqrt Root Decomposition + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[MAX]; +int blockZ[MAX]; +int blockN[MAX]; +int blockSz; +void init(int sz) { + for (int i = 0; i <= sz; i++) { + blockZ[i] = 0; + blockN[i] = 0; + } +} +void update(int i, int value) { + int id = i / blockSz; + if (value < 0) { + if (ar[i] == 0) { + blockZ[id] -= 1; + blockN[id] += 1; + } else if (ar[i] > 0) { + blockN[id] += 1; + } + } else if (value > 0) { + if (ar[i] == 0) { + blockZ[id] -= 1; + } else if (ar[i] < 0) { + blockN[id] -= 1; + } + } else { + if (ar[i] < 0) { + blockZ[id] += 1; + blockN[id] -= 1; + } else if (ar[i] > 0) { + blockZ[id] += 1; + } + } + ar[i] = value; +} +int query(int L, int R) { + int l = L / blockSz; + int r = R / blockSz; + int n = 0, z = 0; + if (l == r) { + for (int i = L; i <= R; i++) { + if (ar[i] == 0) z++; + else if (ar[i] < 0) n++; + } + int P; + if ( z > 0 ) P = 0; + else if (n % 2 == 1) P = -1; + else P = 1; + return P; + } + for (int i = L; i < (l + 1)*blockSz; i++) { + if (ar[i] == 0) z++; + else if (ar[i] < 0) n++; + } + for (int i = l + 1; i < r; i++) { + z += blockZ[i]; + n += blockN[i]; + + } + for (int i = r * blockSz; i <= R; i++) { + if (ar[i] == 0) z++; + else if (ar[i] < 0) n++; + } + int P; + if ( z > 0 ) P = 0; + else if (n % 2 == 1) P = -1; + else P = 1; + return P; +} +int main () { + int N, Q; + while (scanf("%d %d", &N, &Q) == 2) { + blockSz = sqrt(N); + init(blockSz); + for (int i = 0; i < N; i++) { + int x; + scanf("%d", &x); + if (x > 0) ar[i] = 1; + else if (x == 0) ar[i] = 0; + else ar[i] = -1; + if(ar[i] == 0){ + blockZ[i/blockSz]+=1; + } + else if(ar[i] < 0){ + blockN[i/blockSz]+=1; + } + } + for (int q = 1; q <= Q; q++) { + char ch; + cin >> ch; + if (ch == 'C') { + int i; + int value; + scanf("%d %d", &i, &value); + if (value > 0) value = 1; + else if (value == 0) value = 0; + else value = -1; + update(i - 1, value ); + } else { + int l, r; + scanf("%d %d", &l, &r); + //~ printf("%lld %lld\n", l, r); + int ans = query(l - 1, r - 1); + //~ printf("%lld\n", ans); + if (ans > 0) printf("+"); + else if (ans == 0)printf("0"); + else printf("-"); + } + + } + nl; + } + + return 0; +} diff --git a/uva_cpp_clean/12532/12532-21.cpp b/uva_cpp_clean/12532/12532-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e9e0a2f399dd929221ba4ebc8b24e145a4cef5d --- /dev/null +++ b/uva_cpp_clean/12532/12532-21.cpp @@ -0,0 +1,122 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12532 + Name: Interval Product + Problem: https://onlinejudge.org/external/125/12532.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +inline char readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +inline char readCh() { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + return ch; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) + return 0; + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +// ------------------------------------------------------------------- + +#define LSB(i) ((i) & -(i)) + +void init(int BIT[], int size) { + for (int i = 0; i < size; ++i) { + int j = i + LSB(i+1); + + if (j < size) + BIT[j] += BIT[i]; + } +} + +bool update(int BIT[], int size, int i, int delta) { + while (i < size) { + BIT[i] += delta; + i += LSB(i+1); + } return 1; +} + +int getRange(int BIT[], int i, int j) { + int sum = 0; + while (j > i) { + sum += BIT[j-1]; + j -= LSB(j); + } + while (i > j) { + sum -= BIT[i-1]; + i -= LSB(i); + } + return sum; +} + +// ------------------------------------------------------------------- + +#define MAXN 100017 +int n, k, BN[MAXN], BZ[MAXN], X[MAXN]; + +char result[MAXN]; +int main() { + while ((n=readInt()) && (k=readInt())) { + for (int i=0; i=0 && update(BN, n, x, -1); + else y<0 && update(BN, n, x, 1); + + if (X[x]) + !y && update(BZ, n, x, 1); + else y && update(BZ, n, x, -1); + + X[x] = y; + } + else result[ri++] = + getRange(BZ, x, y) ? '0' : + getRange(BN, x, y)&1 ? '-' : '+'; + } + + result[ri++] = '\n'; + fwrite_unlocked(result, 1, ri, stdout); + } +} diff --git a/uva_cpp_clean/12541/12541-18.cpp b/uva_cpp_clean/12541/12541-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b0d3e44c8db3687f98aaad9f2a43f8562ac191b3 --- /dev/null +++ b/uva_cpp_clean/12541/12541-18.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +using namespace std; + +bool cmp(const tuple& t1, const tuple& t2){ + if(get<3>(t1) > get<3>(t2)) return true; + else if(get<3>(t1)==get<3>(t2) && get<2>(t1) > get<2>(t2)) return true; + else if(get<3>(t1)==get<3>(t2) && get<2>(t1)==get<2>(t2) && get<1>(t1) > get<1>(t2)) return true; + return false; +} + +int main(){ + int n, day, month, year; + string name; + char junk[20]; + vector> bornDate; + scanf("%d", &n); + while(n--){ + scanf("%s %d %d %d", &junk, &day, &month, &year); + name = junk; + bornDate.push_back(tuple(name, day, month, year)); + } + sort(bornDate.begin(), bornDate.end(), cmp); + cout << get<0>(bornDate[0]) << endl; + cout << get<0>(bornDate[bornDate.size() - 1]) << endl; + return 0; +} +/* + La estrategia a usar es un tuple y crear un metodo custom para ordenar el vector donde tengo las fechas de + nacimiento. Aprendizaje: Gracias a este problema pude aprender algo nuevo de C++ que son los tuple. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/12541/12541-20.cpp b/uva_cpp_clean/12541/12541-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72693ac0373686959d9a69d1a7969d0087c2ecfd --- /dev/null +++ b/uva_cpp_clean/12541/12541-20.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +struct person +{ + string name; + int dd, mm, yy; +} p[105]; + +bool compare(person a, person b) +{ + if(a.yy != b.yy) return a.yy < b.yy; + if(a.mm != b.mm) return a.mm < b.mm; + if(a.dd != b.dd) return a.dd < b.dd; +} + +int main() +{ + int n; + + while( cin >> n ) + { + for(int i=0; i> p[i].name >> p[i].dd >> p[i].mm >> p[i].yy; + + sort(p, p+n, compare); + + cout << p[n-1].name << "\n" << p[0].name << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12541/12541-21.cpp b/uva_cpp_clean/12541/12541-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ce84a87ac159a5b94ddd428089352fbe6e96daa --- /dev/null +++ b/uva_cpp_clean/12541/12541-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12541 + Name: Birthdates + Problem: https://onlinejudge.org/external/125/12541.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +int main() { + char mns[30], mxs[30], name[30]; + int n, mn=10000000, mx=0, y, m, d; + + scanf("%d", &n); + for (int i=0; i mx) { + strcpy(mxs, name); + mx = t; + } + } + puts(mxs); puts(mns); +} diff --git a/uva_cpp_clean/12541/12541-5.cpp b/uva_cpp_clean/12541/12541-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9c9bc2d49273ec844eb830083f46e3597a9fe6f3 --- /dev/null +++ b/uva_cpp_clean/12541/12541-5.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + int n,j=0,d,y,m,a; + string name; + mapmp; + priority_queue< int >pq1; + priority_queue< int >pq2; + sc("%d",&n); + while(n--) + { + cin>>name; + sc("%d %d %d",&d,&m,&y); + a=d+m*31+y*12*31; + mp[a]=name; + pq1.push(a); + pq2.push(-a); + } + cout<<""< + +#define SIZE 20 + +using namespace std; + +int n, dd, mm, yy; +char s[SIZE]; +vector < tuple > v; + +int main(){ + scanf("%d\n", &n); + for(int i = 0; i < n; i++){ + scanf("%s %d %d %d", s, &dd, &mm, &yy); + v.push_back( make_tuple(yy, mm, dd, string(s)) ); + } + sort(v.begin(), v.end()); + printf("%s\n", (get<3>(v.back())).c_str()); + printf("%s\n", (get<3>(v[0])).c_str()); + return(0); +} diff --git a/uva_cpp_clean/12542/12542-5.cpp b/uva_cpp_clean/12542/12542-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..82c26524f5ef269b5fa8002c58ccd824efe01782 --- /dev/null +++ b/uva_cpp_clean/12542/12542-5.cpp @@ -0,0 +1,98 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define st(s) s.size(); +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +using namespace std; + +bool prime[10000010]; +void is_prime() +{ + prime[1]=false; + int n=sqrt(10000000); + for(int i=2;i<=n;i++) + { + if(prime[i]!=false) + { + for(int j=2*i;j<=10000000;j=j+i) + { + prime[j]=false; + } + } + } +} +int main() +{ + + cover(prime,true); + is_prime(); + string s; + while(getline(cin,s)) + { + if(s.compare("0")==0) + break; + + int l=st(s); + ll p; + ll Max=0; + for(int i=0;i=i;j--) + { + istringstream buf(s.substr(i,j)); //string to integer; + buf >> p; + if(p > 100000) + continue; + if(prime[p]) + { + if(Max < p) + Max=p; + } + } + } + cout< +#include +#include +#include +using namespace std; + +int main() { + string s, m = ""; + int size = 0; + while (cin >> s && s != "E-N-D") { + int c = 0; + string proc = ""; + for (int i = 0; i < s.length(); i++) { + if (s[i] == '-' || isalpha(s[i])) c++, proc += s[i]; + } + if (c > size) m = proc, size = c; + } + transform(m.begin(), m.end(), m.begin(), ::tolower); + cout << m << endl; + return 0; +} diff --git a/uva_cpp_clean/12543/12543-20.cpp b/uva_cpp_clean/12543/12543-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df2ba534d5e3b98f3a00a2b1ab579d93c39dd00f --- /dev/null +++ b/uva_cpp_clean/12543/12543-20.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +char text[10005], str[105]; +int MAX = -1, len, character; + +int main() +{ + while( scanf("%s", text) && strcmp("E-N-D" , text) ) + { + len = strlen(text), character = 0; + for(int i=0 ; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +int main() +{ + char s[1000005],a[1000005]; + int max=1; + while(sc("%s",s)) + { + if(!strcmp(s,"E-N-D")) + break; + int l=strlen(s); + int c=0; + for(int i=0;i='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z'||s[i]=='-') + continue; + else + c++; + } + l=l-c; + if(max='A'&&a[i]<='Z') + pf("%c",a[i]+32); + else if(a[i]>='a'&&a[i]<='z'||a[i]=='-') + pf("%c",a[i]); + } + pf("\n"); + return 0; +} diff --git a/uva_cpp_clean/12545/12545-19.cpp b/uva_cpp_clean/12545/12545-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce1010e9a7405dedf8a17e7a81fe35b7b786a59c --- /dev/null +++ b/uva_cpp_clean/12545/12545-19.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + +int main() { + int T; + cin >> T; + bool mask[101]; + for (int c = 1; c <= T; c++) { + string S, T; + cin >> S >> T; + int c0s = 0, c1s = 0, c0t = 0, c1t = 0; + for (int i = 0; i < S.length(); i++) { + if (S[i] == '1') c1s++; + else if (S[i] == '0') c0s++; + if (T[i] == '1') c1t++; + else if (T[i] == '0') c0t++; + mask[i] = (S[i] != T[i]); + } + int res = 0; + for (int i = 0; i < S.length(); i++) { + if (S[i] == '?') { + if (T[i] == '1') { + if (c1s < c1t) { + c1s++; + mask[i] = false; + } else c0s++; + } + else { + if (c0s < c0t) { + c0s++; + mask[i] = false; + } else c1s++; + } + res++; + } + } + if (c1s < c1t) { + for (int i = 0; i < S.length() && c1s < c1t; i++) { + if (S[i] == '0' && T[i] == '1') { + res++; + c1s++; + c0s--; + mask[i] = false; + } + } + } + printf("Case %d: ", c); + if (c0s == c0t && c1s == c1t) { + int auxi = 0; + for (int i = 0; i < S.length(); i++) + if (mask[i]) auxi++; + printf("%d\n", res + auxi/2); + } else printf("-1\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12545/12545-5.cpp b/uva_cpp_clean/12545/12545-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..99ee07b4b05ee10ad16ad6263319c372acbd0979 --- /dev/null +++ b/uva_cpp_clean/12545/12545-5.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int n,no=0; + char s[10000],t[10000],temp; + sc("%d",&n); + while(n--) + { + sc("%s%s",s,t); + int l1=strlen(s); + int l2=strlen(t); + if(l1>l2||l1ot) + { + pf("Case %d: -1\n",++no); + } + else + { + pf("Case %d: %d\n",++no,zs+os-min(zs,os)+ws); + } + } + return 0; +} diff --git a/uva_cpp_clean/12554/12554-2.cpp b/uva_cpp_clean/12554/12554-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b94bfa1acacacb1f925b5dbcab2c41116c23b46d --- /dev/null +++ b/uva_cpp_clean/12554/12554-2.cpp @@ -0,0 +1,59 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; cin >> tc; + string song[16] = {"Happy", "birthday", "to", "you", + "Happy", "birthday", "to", "you", + "Happy", "birthday", "to", "Rujia", + "Happy", "birthday", "to", "you"}; + vector names(tc); + for (int i = 0; i < tc; ++i) cin >> names[i]; + int p = 0; + bool f = 0; + bool allGone = false; + + for (int p = 0, cWord = 0; cWord < 16 || !allGone; ++p, ++cWord) { + cWord %= 16; + cout << names[p] << ": " << song[cWord] << '\n'; + + if (p + 1 == tc) + { + allGone = true; + p = -1; + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12554/12554-20.cpp b/uva_cpp_clean/12554/12554-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18d69e9445de6c94e385ced185ae779c675c71c6 --- /dev/null +++ b/uva_cpp_clean/12554/12554-20.cpp @@ -0,0 +1,29 @@ +#include + +const char song[16][10] = {"Happy", "birthday", "to", "you", "Happy", "birthday", "to", "you", "Happy", "birthday", "to", "Rujia", "Happy", "birthday", "to", "you"}; +char name[100][105]; + +int main() +{ + int n; + + scanf("%d", &n); + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12554 + Name: A Special "Happy Birthday" Song!!! + Problem: https://onlinejudge.org/external/125/12554.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char msg[16][30] = { + "Happy", "birthday", "to", "you", + "Happy", "birthday", "to", "you", + "Happy", "birthday", "to", "Rujia", + "Happy", "birthday", "to", "you", +}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + cin >> n; + string names[200]; + for (int i=0; i> names[i]; + + int sz = ceil(n/16.0) * 16; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int n,p; + char s[100][100]; + char c[16][100]={"Happy","birthday","to","you","Happy","birthday", + "to","you","Happy","birthday","to","Rujia","Happy","birthday","to","you"}; + while(sc("%d",&n)==1) + { + int k=0,p=1; + for(int i=0;i16) + p=ceil(n/16)+1; + for(int j=0,i=0;j<16*p;j++,i++,k++) + { + if(i==n) + i=0; + if(j==16) + k=0; + pf("%s: %s\n",s[i],c[k]); + } + } + return 0; +} diff --git a/uva_cpp_clean/12554/main.cpp b/uva_cpp_clean/12554/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e329458f26c8c4009915efb5b98cc7b0f7ccbc5 --- /dev/null +++ b/uva_cpp_clean/12554/main.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +using namespace std; + +int main(){ + + short n; + string name; + queue names; + queue reserve; + vector letter; + cin >> n; + for(short i=0; i < n; i++){ + cin >> name; + names.push(name); + } + letter.push_back("Happy"); + letter.push_back("birthday"); + letter.push_back("to"); + letter.push_back("you"); + letter.push_back("Happy"); + letter.push_back("birthday"); + letter.push_back("to"); + letter.push_back("you"); + letter.push_back("Happy"); + letter.push_back("birthday"); + letter.push_back("to"); + letter.push_back("Rujia"); + letter.push_back("Happy"); + letter.push_back("birthday"); + letter.push_back("to"); + letter.push_back("you"); + short indice = 0; + while(!names.empty()){ + cout << names.front() << ": " << letter[indice] << endl; + reserve.push(names.front()); + names.pop(); + indice++; + if(indice == 16) indice = 0; + } + while(indice < 16){ + cout << reserve.front() << ": " << letter[indice] << endl; + reserve.push(reserve.front()); + reserve.pop(); + indice++; + } + + +return 0; +} diff --git a/uva_cpp_clean/12555/12555-20.cpp b/uva_cpp_clean/12555/12555-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b393fb4558b5a506e33eb4d695c9814147fbe1d --- /dev/null +++ b/uva_cpp_clean/12555/12555-20.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int tc, counter; + + cin >> tc; + counter = 1; + while( tc-- ) + { + int a; + string s; + cin >> a >> s; + + int b = s.length()>3 ? (int) s[3]-'0' : 0; + + cout << "Case " << counter++ << ": " << (a*0.5)+(b*0.05) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12555/12555-5.cpp b/uva_cpp_clean/12555/12555-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a645f91e247d7aceb72696f61d116e74b0362d4c --- /dev/null +++ b/uva_cpp_clean/12555/12555-5.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main () +{ + + int t; + int no = 0; + scanf ("%d", &t); + while ( t-- ) { + char s [20]; + scanf ("%s", s); + int l = strlen(s); + int a = 0,b=0; + char s1 [10]; + int sum= 0; + printf ("Case %d: ", ++no); + sscanf(s, "%d%s", &a, s1); + int f = 1; + for ( int i = 0; i < l; i++ ) { + if ( isdigit(s [i]) && f ) + continue; + f = 0; + if ( isdigit(s [i]) ) { + f = 1; + b = s [i] - '0'; + break; + } + } + + sum= (a * 50)+(b * 5); + printf ("%d", sum/ 100); + sum%= 100; + if (sum) { + if ( sum% 10 == 0 ) + printf (".%d", sum/ 10); + else + printf (".%d", sum); + } + printf ("\n"); + } + + return 0; +} + + diff --git a/uva_cpp_clean/12563/12563-14.cpp b/uva_cpp_clean/12563/12563-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..659eb7e3e7d9c0acba07cbcb29768cad3be8dfef --- /dev/null +++ b/uva_cpp_clean/12563/12563-14.cpp @@ -0,0 +1,120 @@ +/*************************************************** + * Problem Name : 12563 - Jin Ge Jin Qu hao.cpp + * Problem Link : https://uva.onlinejudge.org/external/125/12563.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-07-17 + * Problem Type : DP (KnapSack) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 55 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int N; +int cap; +int ar[MAX]; +int dp[MAX][10000]; +int dir[MAX][10000]; +int knapSack (int i, int w) { + if (i == N) return 0; + + if (dp[i][w] != -1) return dp[i][w]; + + int pro1 = 0, pro2 = 0; + + if (w - ar[i] >= 0) { + pro1 = 1 + knapSack (i + 1, w - ar[i]); + } + + pro2 = knapSack (i + 1, w); + return dp[i][w] = max (pro1, pro2); +} +int print (int i, int w) { + if (i == N) return 0; + + if (dir[i][w] != -1) return dir[i][w] ; + + int pro1 = -1, pro2 = -1; + int pro = knapSack (i, w); + + if (w + ar[i] >= 0) { + pro1 = 1 + knapSack (i + 1, w - ar[i]); + } + + pro2 = knapSack (i + 1, w); + int sum1 = 0, sum2 = 0; + + if (pro == pro1) sum1 = ar[i] + print (i + 1, w - ar[i]); + + if (pro == pro2) sum2 = print (i + 1, w); + + return dir[i][w] = max (sum1, sum2); +} +int main () { + __FastIO; + int tc; + int T; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> N >> T; + cap = T - 1; + + for (int i = 0; i < N; i++) { + cin >> ar[i]; + } + + SET (dp, -1); + SET (dir, -1); + sort (ar, ar + N); + int cnt = knapSack (0, cap) + 1; + int ans = print (0, cap) + 678; + cout << "Case " << t << ": " << cnt << " " << ans << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/12563/12563-7.cpp b/uva_cpp_clean/12563/12563-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ffff902f75d04266034b7c5c14dd9f3b8c054078 --- /dev/null +++ b/uva_cpp_clean/12563/12563-7.cpp @@ -0,0 +1,63 @@ +/** + * Created by LonelyEnvoy on 2017-8-18. + * Jin Ge Jin Qu hao + * Keywords: dp + * Optimized solution (20ms -> 10ms) + */ + +#include +#include +#include +#include +using namespace std; + +#define rep(i,a,n) for(int i=a;i=a;--i) + +#define MAXN 51 +#define MAXT 9679 + +struct Node { + int count; + int time; +}; + +int v[MAXN]; +Node dp[MAXN][MAXT]; +int n, t; + +int ansCount; +int ansTime; + +void solve() { + erep(i,1,n) { + erep(j,0,t) { + dp[i][j] = dp[i-1][j]; + if (j > v[i] + && (dp[i][j].count < dp[i-1][j-v[i]].count+1 + || (dp[i][j].count == dp[i-1][j-v[i]].count+1 + && dp[i][j].time < dp[i-1][j-v[i]].time+v[i]))) { + dp[i][j].time = dp[i-1][j-v[i]].time+v[i]; + dp[i][j].count = dp[i-1][j-v[i]].count+1; + } + } + } + + ansCount = dp[n][t].count + 1; + ansTime = dp[n][t].time + 678; +} + +int main() { + int kase; + scanf("%d", &kase); + rep(k,0,kase) { + scanf("%d%d", &n, &t); + erep(i,1,n) { + scanf("%d", v+i); + } + solve(); + printf("Case %d: %d %d\n", k+1, ansCount, ansTime); + } +} + diff --git a/uva_cpp_clean/12571/12571-21.cpp b/uva_cpp_clean/12571/12571-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9934c770be8605227e98104c2d4997a3684c3b2c --- /dev/null +++ b/uva_cpp_clean/12571/12571-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12571 + Name: Brother & Sisters! + Problem: https://onlinejudge.org/external/125/12571.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m; cin >> T; + while (T--) { + cin >> n >> m; + + int mx = 0, x; + bool S[256]; + memset(S, 0, sizeof(S)); + for (int i=0; i> x; + S[x & 255] = true; + } + + vector X; + for (int i=0; i<256; ++i) + if (S[i]) + X.push_back(i); + + for (int i=0; i> x; + mx = 0; + for (int y: X) { + mx = max(mx, x&y); + } + cout << mx << endl; + } + } +} diff --git a/uva_cpp_clean/12571/12571-5.cpp b/uva_cpp_clean/12571/12571-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f0bd041165af2ef13f54900b43f8e01eb9618828 --- /dev/null +++ b/uva_cpp_clean/12571/12571-5.cpp @@ -0,0 +1,74 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; +int main() +{ + int t; + cin>>t; + while(t--) + { + int n,k,a[200000],b,i=0; + cin>>n>>k; + int p=n; + for(int i=0;i=0;j--) + { + res=a[j]&b; + if(res!=0) + break; + } + pf("%d\n",res); + } + } + return 0; +} diff --git a/uva_cpp_clean/12573/12573-21.cpp b/uva_cpp_clean/12573/12573-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..944e3cd6ba2e3d9d1ce0bcc548754e7c536bcca4 --- /dev/null +++ b/uva_cpp_clean/12573/12573-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12573 + Name: Sohel Sir's Assignment + Problem: https://onlinejudge.org/external/125/12573.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; +typedef long long Int; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + Int T, x, y; + cin >> T; + while (T-- && cin >> x >> y) { + Int n = x-y+2, + sq = sqrt(n), + mn = Int(1e14); + + if (y < 2) mn = 1; + else if (n<=0) mn = x+1; + else + for (Int i=1; i<=sq; ++i) + if (n % i == 0) { + if (x%i == y-2) + mn = min(mn, i); + + if (x % (n/i) == y-2) + mn = min(mn, n/i); + + if (i > mn) break; + } + + if (mn < Int(1e14)) + cout << mn << '\n'; + else cout << "Impossible\n"; + } +} diff --git a/uva_cpp_clean/12575/12575-19.cpp b/uva_cpp_clean/12575/12575-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e77d7d4fb02c25b120da8afd8d7599d96eff2cea --- /dev/null +++ b/uva_cpp_clean/12575/12575-19.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +double pi = acos(-1); + +int main() { + int T; + cin >> T; + while (T--) { + double A, B; + double l1, l2, x; + cin >> A >> B; + if (A >= 0 && B >= 0) l1 = 0, l2 = pi/2; + else if (A >= 0 && B < 0) l1 = pi/2, l2 = pi; + else if (A < 0 && B < 0) l1 = pi, l2 = 3*pi/2; + else l1 = 3*pi/2, l2 = 2*pi; + if (B != 0) x = atan(A/B); + else x = pi/2; + if (x < 0) x += pi; + double sol[4]; + sol[0] = A*sin(l1) + B*cos(l1); + sol[1] = A*sin(x) + B*cos(x); + sol[2] = A*sin(l2) + B*cos(l2); + sol[3] = A*sin(x+pi) + B*cos(x+pi); + if (sol[0] >= sol[1] && sol[0] >= sol[2] && sol[0] >= sol[3]) printf("%.2lf %.2lf\n", l1, sol[0]); + else if (sol[1] >= sol[0] && sol[1] >= sol[2] && sol[1] >= sol[3]) printf("%.2lf %.2lf\n", x, sol[1]); + else if (sol[2] >= sol[0] && sol[2] >= sol[1] && sol[2] >= sol[3]) printf("%.2lf %.2lf\n", l2, sol[2]); + else printf("%.2lf %.2lf\n", x+pi, sol[3]); + } + return 0; +} diff --git a/uva_cpp_clean/12577/12577-21.cpp b/uva_cpp_clean/12577/12577-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..932b7ce413ec9614d122ecf45e0452ab9fb9fcf5 --- /dev/null +++ b/uva_cpp_clean/12577/12577-21.cpp @@ -0,0 +1,22 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12577 + Name: Hajj-e-Akbar + Problem: https://onlinejudge.org/external/125/12577.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + char str[50]; + int cse = 0; + while (cin.getline(str, 50) && str[0] != '*') + printf("Case %d: Hajj-e-%s\n", ++cse, str[0] == 'H' ? "Akbar" : "Asghar"); +} diff --git a/uva_cpp_clean/12577/12577-5.cpp b/uva_cpp_clean/12577/12577-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3cbce3294864607f489fc8f7d22fb314165b86ff --- /dev/null +++ b/uva_cpp_clean/12577/12577-5.cpp @@ -0,0 +1,17 @@ +#include +#include +int main() +{ + char s[6]; + int no=0; + while(scanf("%s",s)) + { + if(!strcmp(s,"*")) + break; + else if(!strcmp(s,"Hajj")) + printf("Case %d: Hajj-e-Akbar\n",++no); + else + printf("Case %d: Hajj-e-Asghar\n",++no); + } + return 0; +} diff --git a/uva_cpp_clean/12577/12577-9.cpp b/uva_cpp_clean/12577/12577-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..729f9b10ef48fbc4c7358e7d128c96fe6cde7012 --- /dev/null +++ b/uva_cpp_clean/12577/12577-9.cpp @@ -0,0 +1,19 @@ +#include +#include + +int main(){ + char a[10]; + int i = 1; + while(scanf("%s",a) != EOF){ + if(strcmp(a, "*") == 0) break; + else if(strcmp(a, "Hajj") == 0){ + printf("Case %d: Hajj-e-Akbar\n", i); + i++; + } + else{ + printf("Case %d: Hajj-e-Asghar\n", i); + i++; + } + } + return(0); +} diff --git a/uva_cpp_clean/12577/main.cpp b/uva_cpp_clean/12577/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..572ff07a89e65fe9d99a2f25096ea6189a21670a --- /dev/null +++ b/uva_cpp_clean/12577/main.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int main(){ +int counter = 1; +string s; +bool flag = true; +while(flag){ + cin >> s; + if(s != "*"){ + if (s == "Hajj"){ + cout <<"Case "<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12578 + Name: 10:6:2 + Problem: https://onlinejudge.org/external/125/12578.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int T, n; + cin>>T; + while (T--) { + cin>>n;n*=n; + printf("%.2f %.2f\n", 0.125663706144*n, 0.474336293856*n); + } +} diff --git a/uva_cpp_clean/12578/12578-5.cpp b/uva_cpp_clean/12578/12578-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6c0600bec2e8983db6a2e2ae05b0bc982e31ec33 --- /dev/null +++ b/uva_cpp_clean/12578/12578-5.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#define Pi 2*acos(0.0) +int main() +{ + int t; + double b,c,r,h,g,n; + scanf("%d",&t); + while(t--) + { + r=0; + scanf("%lf",&n); + r=n/5; + h=(n*6)/10; + r=Pi*r*r; + g=(n*h)-r; + printf("%.2lf %.2lf\n",r,g); + } + return 0; +} diff --git a/uva_cpp_clean/12578/12578-9.cpp b/uva_cpp_clean/12578/12578-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6650c608a91e1c41f6ba49ac1fa485a0ae74b54 --- /dev/null +++ b/uva_cpp_clean/12578/12578-9.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +const double PI = acos(-1.0); + +int tc; +double L, W, r, red, green; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%lf", &L); + W = L * 0.6; + r = L * 0.2; + red = PI * r * r; + green = W * L - red; + printf("%.2lf %.2lf\n", red, green); + } + return(0); +} diff --git a/uva_cpp_clean/1258/1258-21.cpp b/uva_cpp_clean/1258/1258-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7d044ae35ca612f916f2deb81e4de2da2a94e81 --- /dev/null +++ b/uva_cpp_clean/1258/1258-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1258 + Name: Nowhere Money + Problem: https://onlinejudge.org/external/12/1258.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +typedef unsigned long long int Long; + +Long Fib[92] = {1, 1}; +int main(){ + for (int i=2; i<92; i++) + Fib[i] = Fib[i-1] + Fib[i-2]; + + Long n; + while (scanf("%llu", &n)==1) { + printf("%llu\n", n); + vector r; + int lastI = 92; + for (; n; n -= Fib[lastI]) { + int ind = upper_bound(Fib, Fib+lastI, n) - Fib; + if (Fib[ind] > n) --ind; + printf("%d ", ind); + r.push_back(ind); + lastI = ind; + } + + putchar('\n'); + for (int i=0; i + +#define LIM 91 + +using namespace std; + +int main(){ + vector fib; + long long n; + vector < pair > x; + fib.push_back(1), fib.push_back(1); + for(int i = 2; i < LIM; i++) + fib.push_back(fib[i - 1] + fib[i - 2]); + while(scanf("%lld", &n) == 1){ + printf("%lld\n", n); + for(int i = LIM - 1; n != 0; i--){ + if(fib[i] <= n) + x.push_back( make_pair(i, fib[i])), + n -= fib[i]; + } + for(int i = 0; i < x.size(); i++) + printf("%d ", x[i].first); + putchar('\n'); + for(int i = 0; i < x.size(); i++) + printf("%lld ", x[i].second); + putchar('\n'); + putchar('\n'); + x.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/12582/12582-19.cpp b/uva_cpp_clean/12582/12582-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0fac16c01c121a93638ed6313624bd5f9e03229 --- /dev/null +++ b/uva_cpp_clean/12582/12582-19.cpp @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +int views[30]; + +void proc(int & pos, string & cad){ + char node = cad[pos]; + //cout<> cases; + for(int i = 0; i> cad; + //memset(views, 0, 30); + for(int l = 0; l<30; l++) views[l]=0; + int pos = 0; + proc(pos, cad); + cout << "Case "< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + mapmp; + stackstk; + int t,no=0; + string s; + cin>>t; + while(t--) + { + cin>>s; + stk.push(s[0]); + for(int i=1;i +#include +#include +#include +using namespace std; + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + int n, k; + queue q; + unordered_map rec; + cin >> n >> k; + int count = 0; + for (int i = 0; i < n; i++) { + if (q.size() > k) { + rec[q.front()]--; + q.pop(); + } + char p; + cin >> p; + if (rec[p]) count++; + q.push(p); + rec[p]++; + } + printf("Case %d: %d\n", c, count); + } + return 0; +} diff --git a/uva_cpp_clean/12583/12583-21.cpp b/uva_cpp_clean/12583/12583-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d5cdd433076f9f284a2346333850e5b7a0fe05d --- /dev/null +++ b/uva_cpp_clean/12583/12583-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12583 + Name: Memory Overflow + Problem: https://onlinejudge.org/external/125/12583.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, k, L[26]; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + string str; + cin >> n >> k >> str; + + int cnt=0; + for (int i=0; i<26; ++i) L[i] = -k - 1; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + char a[1000]; + int n,k,t,no=0; + scanf("%d",&t); + while(t--) + { + scanf("%d%d%s",&n,&k,a); + int count=0; + for(int i=1;i + +using namespace std; + +int tc, n, k, ans; +vector ct[26]; +char s[510]; + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + scanf("%d %d %s", &n, &k, s); + ans = 0; + for(int i = 0; i < 26; i++) ct[i].clear(); + for(int i = 0; i < n; i++) + ct[s[i] - 'A'].push_back(i); + for(int i = 0; i < 26; i++) + for(int j = 1; j < ct[i].size(); j++) + if(ct[i][j] - ct[i][j - 1] <= k) ans++; + printf("Case %d: %d\n", t, ans); + } + return(0); +} diff --git a/uva_cpp_clean/12592/12592-14.cpp b/uva_cpp_clean/12592/12592-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87287b186aa4714388f8fd250c8f0fdd24dbf596 --- /dev/null +++ b/uva_cpp_clean/12592/12592-14.cpp @@ -0,0 +1,145 @@ +/*************************************************** + * Problem Name : 12592 - Slogan Learning of Princess.cpp + * Problem Link : https://onlinejudge.org/external/125/12592.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-11-15 + * Problem Type : + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + int n; + cin >> n; + cin.ignore(); + mapmp; + + for (int i = 0; i < n; i++) { + string a, b; + getline (cin, a); + getline (cin, b); + mp[a] = b; + } + + int q; + cin >> q; + cin.ignore(); + + while (q--) { + string str; + getline (cin, str); + cout << mp[str] << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/12592/12592-21.cpp b/uva_cpp_clean/12592/12592-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92e7cb6d8f0462d37d9aa4af418a5d4affaf44dd --- /dev/null +++ b/uva_cpp_clean/12592/12592-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12592 + Name: Slogan Learning of Princess + Problem: https://onlinejudge.org/external/125/12592.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + int n; + while (scanf("%d", &n)==1) { + string line, l2; + getline(cin, line); + map mp; + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + string s[200],p[100],r; + int n,k,q; + while(cin>>n) + { + getchar(); + for(int i=0;i>q; + getchar(); + while(q--) + { + getline(cin,r); + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + int t,n; + string s; + cin>>t; + while(t--) + { + cin>>s; + n=s.size(); + vectorv; + int f=0; + if(s[0]=='W'||s[n-1]=='B') + { + cout<<"IMPOSSIBLE"<3) + break; + } + if(c==1) + { + cout<<"1"< + +using namespace std; + +const int LEN = 100; + +struct Term{ + int c, ex, ey; + Term(int c_, int ex_, int ey_) : c(c_), ex(ex_), ey(ey_) {}; + bool operator < (const Term& other){ + if( ex != other.ex ) return ex > other.ex; + return ey < other.ey; + } + bool operator == (const Term& other){ + return ( ex == other.ex and ey == other.ey ); + } + Term operator * (const Term& other){ + return Term(c * other.c, ex + other.ex, ey + other.ey); + } +}; + +char s[LEN], ch1, ch2, output[3][LEN]; +int coef, e1, e2, it, len, len1, len2; +vector < Term > p, p1, p2; +string x; + +void reading(vector < Term > &p){ + p.clear(), x = ""; + it = ( s[0] == '-' or s[0] == '+' ) ? 1 : 0; + if( s[0] == '-' ) x += "-"; + else x += "+"; + while(s[it]){ + if( s[it] == '-' or s[it] == '+' ) break; + if( x.back() == 'x' and s[it] == 'y' ) x += "1"; + if( x.back() == 'y' and s[it] == 'x' ) x += "1"; + x += s[it++]; + } + if( not isdigit(x[1]) ) x.insert(1, "1"); + if( not isdigit(x.back()) ) x += "1"; + if( x.find('y') == string::npos ) x += "y0"; + if( x.find('x') == string::npos ) x += "x0"; + sscanf(x.c_str(), "%d%c%d%c%d", &coef, &ch1, &e1, &ch2, &e2); + if( ch1 == 'x' ) + p.push_back( Term(coef, e1, e2) ); + else + p.push_back( Term(coef, e2, e1) ); + while(s[it]){ + x.clear(); + x += s[it++]; + while(s[it]){ + if( s[it] == '-' or s[it] == '+' ) break; + if( x.back() == 'x' and s[it] == 'y' ) x += "1"; + if( x.back() == 'y' and s[it] == 'x' ) x += "1"; + x += s[it++]; + } + if( not isdigit(x[1]) ) x.insert(1, "1"); + if( not isdigit(x.back()) ) x += "1"; + if( x.find('y') == string::npos ) x += "y0"; + if( x.find('x') == string::npos ) x += "x0"; + for(int k = 1; k < x.size(); k++) + if( ( x[k] == 'x' and x[k - 1] == 'y' ) or ( x[k] == 'y' and x[k - 1] == 'x' ) ) + x.insert(k, "1"); + sscanf(x.c_str(), "%d%c%d%c%d", &coef, &ch1, &e1, &ch2, &e2); + if( ch1 == 'x' ) + p.push_back( Term(coef, e1, e2) ); + else + p.push_back( Term(coef, e2, e1) ); + } +} + +void print(vector &p){ + memset(output, ' ', sizeof output); + it = 0; + for(int i = 0; i < p.size(); i++){ + if( p[i].c < 0 ) output[1][it++] = '-'; + if( i and p[i].c > 0 ) output[1][it++] = '+'; + if( i ) it++; + if( abs(p[i].c) != 1 or ( abs(p[i].c) == 1 and p[i].ex == 0 and p[i].ey == 0) ){ + x = to_string(p[i].c); + if( x[0] == '-' ) x.erase(x.begin()); + for(int k = 0; k < x.size(); k++) + output[1][it++] = x[k]; + } + if( p[i].ex ){ + output[1][it++] = 'x'; + if( p[i].ex > 1 ){ + x = to_string(p[i].ex); + for(int k = 0; k < x.size(); k++) + output[0][it++] = x[k]; + } + } + if( p[i].ey ){ + output[1][it++] = 'y'; + if( p[i].ey > 1 ){ + x = to_string(p[i].ey); + for(int k = 0; k < x.size(); k++) + output[0][it++] = x[k]; + } + } + it++; + } + if( output[1][0] == ' ' ) output[1][0] = '0'; + len = 2; + for(int i = LEN - 1; i >= 0 and output[0][i] == ' '; i--) len1 = i; + for(int i = LEN - 1; i >= 0 and output[1][i] == ' '; i--) len2 = i; + len = max(len1, len2); + output[0][len] = output[1][len] = '\0'; + if( len1 == 0 and output[1][0] == '0' ) output[0][0] = '\0'; + printf("%s\n", output[0]); + printf("%s\n", output[1]); +} + +int main(){ + while(scanf("%s", s)){ + if( s[0] == '#' ) break; + reading(p1); + scanf("%s", s); + reading(p2); + p.clear(); + for(int i = 0; i < p1.size(); i++) + for(int j = 0; j < p2.size(); j++){ + Term ret = p1[i] * p2[j]; + bool found = false; + for(int k = 0; k < p.size() and not found; k++) + if( p[k] == ret ) p[k].c += ret.c, found = true; + if( not found ) p.push_back(ret); + } + sort(p.begin(), p.end()); + for(int it = 0; it < p.size(); it++) + if( p[it].c == 0 ) p.erase(p.begin() + it), it--; + print(p); + } + return(0); +} diff --git a/uva_cpp_clean/1260/1260-11.cpp b/uva_cpp_clean/1260/1260-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb9e8b37948e941d5acf2aaca2a865c25d77f3e0 --- /dev/null +++ b/uva_cpp_clean/1260/1260-11.cpp @@ -0,0 +1,57 @@ +#include +/* +Problem: 1260 - Sales +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3701 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int T, N; +int sales[1001]; + +int main () { + scanf("%d\n", &T); + + while ( T-- ){ + int b_sum = 0; + scanf("%d\n", &N); + + for (int i = 0; i < N; i++) + scanf("%d", &sales[i]); + + for (int i = 1; i < N; i++) + for (int j = 0; j < i; j++) + if (sales[j] <= sales[i]) b_sum++; + + printf("%d\n", b_sum); + } + + return 0; +} +/* +Sample input:- +----------------- +2 +5 +38 111 102 111 177 +8 +276 284 103 439 452 276 452 398 + +Sample output:- +----------------- +9 +20 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/1260/1260-13.cpp b/uva_cpp_clean/1260/1260-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b59e98cf3fe8152589f77f0faed17f7bfb70f1da --- /dev/null +++ b/uva_cpp_clean/1260/1260-13.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int t; + + cin >> t; + + while (t--) + { + int size, b = 0; + + cin >> size; + + int *a = new int[size]; + + for (int i = 0; i < size; i++) + { + cin >> a[i]; + } + + for (int i = 1; i < size; i++) + { + int temp = 0; + + for (int j = 0; j < i; j++) + { + if (a[j] <= a[i]) + { + temp++; + } + } + + b += temp; + } + + cout << b << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1260/1260-2.cpp b/uva_cpp_clean/1260/1260-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b09c26e00d221438f6a253e57f2a0fff1fa233a --- /dev/null +++ b/uva_cpp_clean/1260/1260-2.cpp @@ -0,0 +1,56 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + + int tc; scanf("%d",&tc); + while (tc--) { + int n; scanf("%d",&n); + int ans = 0; + int j = 0; + vector v(n); + for (int i = 0; i < n; ++i) { + int temp; scanf("%d",&temp); + for (j = i-1; j >= 0; --j) { + if (temp < v[j]) v[j+1] = v[j]; + else break; + } + v[j+1] = temp; + ans += j+1; + } + printf("%d\n", ans); + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1260/1260-21.cpp b/uva_cpp_clean/1260/1260-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd4da3069ea3fd43fdab0738363382fb5f918e11 --- /dev/null +++ b/uva_cpp_clean/1260/1260-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1260 + Name: Sales + Problem: https://onlinejudge.org/external/12/1260.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +unsigned short BIT[5001]; + +int getSum(int i) { + int sum = 0; i++; + while (i) { + sum += BIT[i]; + i -= i & -i; + } + return sum; +} + +int getRange(int i, int j) { + i++; j++; + int sum = 0; + while (j > i) { + sum += BIT[j]; + j -= j & -j; + } + while (i > j) { + sum -= BIT[i]; + i -= i & -i; + } + return sum; +} + +void update(int i, int val) { + i++; + while (i <= 5000) { + BIT[i] += val; + i += i & -i; + } +} + + +int main(){ + int T, n, x; + scanf("%d", &T); + while (T--) { + memset(BIT, 0, sizeof(BIT)); + int result = 0; + scanf("%d", &n); + for (int i=0; i // printf scanf + +int main () +{ + // Integer T: number of test cases + // Integer N: size of array (input) for every test case + // Integer result: each test cases result + int T, n, result; + + scanf ("%d", &T); + while ( T-- ) + { + + scanf ("%d", &n); + + // Integer array a: input + int a[n]; + result = 0; // simply resetting the variable + + for ( int l = 0; l < n; l++ ) + { + scanf ("%d", &a[l]); + for ( int k = 0; k < l; k++ ) + if ( a[l] >= a[k] ) + result++; + } + printf ("%d\n", result ); + } +} diff --git a/uva_cpp_clean/1260/1260-9.cpp b/uva_cpp_clean/1260/1260-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..723f83ffd4c0a1dc4042decb5c666c4b3c596fff --- /dev/null +++ b/uva_cpp_clean/1260/1260-9.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int tc, ct, n; +vector v(1005); + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); ct = 0; + for(int i = 0; i < n; i++) scanf("%d", &v[i]); + for(int i = 1; i < n; i++) + for(int j = 0; j < i; j++) + if(v[j] <= v[i]) ct++; + printf("%d\n", ct); + } + return(0); +} diff --git a/uva_cpp_clean/12602/12602-19.cpp b/uva_cpp_clean/12602/12602-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60faa8cee75383c96df91b3c4b7b5233edf8edc1 --- /dev/null +++ b/uva_cpp_clean/12602/12602-19.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main() { + int N; + scanf("%d", &N); + while (N--) { + char c1, c2, c3, c4; + int val; + scanf("%c%c%c%c%c%d", &c4, &c1, &c2, &c3, &c4, &val); + val = ((c1-'A')*676 + (c2-'A')*26 + (c3-'A')) - val; + if (val <= 100 && val >= -100) printf("nice\n"); + else printf("not nice\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12602/12602-21.cpp b/uva_cpp_clean/12602/12602-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf01462fa00bcbd184ea3bc68b161abf36152a6c --- /dev/null +++ b/uva_cpp_clean/12602/12602-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12602 + Name: Nice Licence Plates + Problem: https://onlinejudge.org/external/126/12602.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + string str; + while (T-- && cin >> str) { + int s1=0, s2=0; + for (int i=0; i<3; ++i) + s1 = s1*26 + str[i]-'A'; + for (int i=4; i<8; ++i) + s2 = s2*10 + str[i]-'0'; + + cout << (abs(s1-s2)<=100 ? "nice\n" : "not nice\n"); + } +} diff --git a/uva_cpp_clean/12602/12602-5.cpp b/uva_cpp_clean/12602/12602-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cee29f0627449ea8f6f5ad431577427d5c7bf20a --- /dev/null +++ b/uva_cpp_clean/12602/12602-5.cpp @@ -0,0 +1,69 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main() +{ + char s[100],p[100]; + int t,sum,res; + cin>>t; + while(t--) + { + sc("%s",s); + int j=0; + for(int i=4;i<8;i++) + p[j++]=s[i]; + p[j]='\0'; + sum=atoi(p); + res=((toascii(s[0])-65)*(26*26))+((toascii(s[1])-65)*26)+(toascii(s[2])-65); + if(abs(res-sum)<=100) + pf("nice\n"); + else + pf("not nice\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12606/12606-21.cpp b/uva_cpp_clean/12606/12606-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a525944c22da28d7589d02f6b0c35b86e69c9bb --- /dev/null +++ b/uva_cpp_clean/12606/12606-21.cpp @@ -0,0 +1,118 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12606 + Name: Evaluating Logic Expressions + Problem: https://onlinejudge.org/external/126/12606.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool readBool() { + string str; + cin >> str; + return str=="true"; +} + +// ---------------------------------------------------------- + +char valNames[3][10] = { + "unknown", + "false", + "true" +}; + +int un, bn, pos, vars[128]; +bool TBL[202][2][2], isal[128], isup[128]; +map ids; +vector tokens; + +int readTbl(int oid, int r1, int r2) { + #define T TBL[oid] + if (r1 < 0 && r2 < 0) + return T[0][0]==T[0][1] && T[1][0]==T[1][1] && T[0][0]==T[1][0] ? T[0][0] : -1; + + if (r1 < 0) + return T[0][r2]==T[1][r2] ? T[0][r2] : -1; + + if (r2 < 0) + return T[r1][0]==T[r1][1] ? T[r1][0] : -1; + + return T[r1][r2]; +} + +int evalExpression() { + string &s = tokens[pos++]; + if (s[0] != '(') + return vars[s[0]]; + + int r1, r2, oid; + if (isup[tokens[pos][0]]) { + oid = ids[tokens[pos++]]; + r2 = evalExpression(); + r1 = 0; + } + else { + r1 = evalExpression(); + oid = ids[tokens[pos++]]; + r2 = evalExpression(); + } + + if (tokens[pos++][0] != ')') + throw "Syntax Error"; + + return readTbl(oid, r1, r2); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i=0; i<26; ++i) + isal[i+'a'] = isal[i+'A'] = isup[i+'A'] = 1; + + string str; + for (int cse=1; cin >> un >> bn && (un>=0 && bn>=0); ++cse) { + ids.clear(); + for (int i=0; i> str; + ids[str] = i; + TBL[i][0][0] = readBool(); + TBL[i][0][1] = readBool(); + } + for (int k=0; k> str; + ids[str] = un+k; + for (int i=0; i<2; ++i) { + for (int j=0; j<2; ++j) + TBL[un+k][i][j] = readBool(); + } + } + + cin.ignore(100, '\n'); + getline(cin, str); + tokens.clear(); + for (int i=0, j=0; i<=str.length(); ++i) + if (!isal[str[i]]) { + if (j < i) + tokens.push_back(str.substr(j, i-j)); + + if (str[i] == '(' || str[i] == ')') + tokens.push_back(str.substr(i, 1)); + + j = i+1; + } + + memset(vars, -1, sizeof(vars)); + while (cin >> str && str!="*") + vars[str[0]] = readBool(); + + pos = 0; + cout << "Case " << cse << ": " << valNames[evalExpression()+1] << '\n'; + } +} diff --git a/uva_cpp_clean/12608/12608-20.cpp b/uva_cpp_clean/12608/12608-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a9ecb4f5ed580f5e4f9a80083b6a0a74e314b6c --- /dev/null +++ b/uva_cpp_clean/12608/12608-20.cpp @@ -0,0 +1,28 @@ +#include + +const int S = 1005; +int tc, i, temp; +int W, N, x[S], w[S], dis; + +int main() +{ + scanf("%d", &tc); + while( tc-- ) + { + scanf("%d %d", &W, &N); + for(i=0 ; iW ) dis += x[i]+x[i], temp = w[i]; + else if( temp+w[i]>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1261 + Name: String Popping + Problem: https://onlinejudge.org/external/12/1261.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +unordered_map DP; +bool dfs(string s) { + int len = s.length(); + if (!len) return 1; + + auto it = DP.find(s); + if (it != DP.end()) + return it->second; + + for (int i=0; i> T; + while (T--) { + string str, ss; + cin >> str; + for (int i=1, k=1; i<=str.length(); ++i) + if (str[i] != str[i-1]) { + ss += k>1 ? "1" : "0"; + k = 1; + } + else ++k; + + cout << dfs(ss) << endl; + } +} diff --git a/uva_cpp_clean/1261/1261-9.cpp b/uva_cpp_clean/1261/1261-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6df2305e109419c1a9953e9b940387c3e18cceb --- /dev/null +++ b/uva_cpp_clean/1261/1261-9.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int tc; +setvisited; +mapmemo; +string line; + +bool dp(string s){ + if(s.empty()) return true; + if(visited.find(s)!=visited.end()) return memo[s]; + int l,r,j; + for(int i=0;i>line; + printf("%d\n",dp(line)); + } + return(0); +} diff --git a/uva_cpp_clean/12611/12611-21.cpp b/uva_cpp_clean/12611/12611-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21b8318bd219a9cf4a80026b835c2a88dd0ef3f9 --- /dev/null +++ b/uva_cpp_clean/12611/12611-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12611 + Name: Beautiful Flag + Problem: https://onlinejudge.org/external/126/12611.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, r; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> r; + int l = r * 5, + h = round(l*0.3), + w1= round(l*0.45), + w2= l - w1; + printf("Case %d:\n%d %d\n%d %d\n%d %d\n%d %d\n", cse, + -w1, h, w2, h, w2,-h,-w1,-h); + } +} diff --git a/uva_cpp_clean/12611/12611-5.cpp b/uva_cpp_clean/12611/12611-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c35fbfcac7a5945ffde8755f5e926f72194bc0b3 --- /dev/null +++ b/uva_cpp_clean/12611/12611-5.cpp @@ -0,0 +1,106 @@ +/****************************************************************** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** My Blog : shipuahamed.blogspot.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_HIDDEN *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t); +#define ssii(a,b) scanf("%d%d",&a,&b); +#define ssll(a,b) scanf("%lld%lld",&a,&b); + + +#define ff first +#define se second +#define eps 1e-9 +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +templateinline void extended_euclid(T a,T b,T &x,T &y){if(a%b==0)x=0,y=1;else{extended_euclid(b,a%b,x,y);T temp=x;x=y;y=-y*(a/b)+temp;}} +templateinline T bigmod(T b,T p,T m){if(!p)return 1;else if(!(p%2)){T x=bigmod(b,p/2,m);return x*x;}else return ((b%m)*bigmod(b,p-1,m))%m;} + +void ASCII_Chart(){int i,j,k;pf("ASCII Chart:(30-129)\n");for(int i=30;i<50;i++){for(int j=0;j<5;j++){k=i+j*20;pf("%3d---> '%c' ",k,k);}pf("\n");}} + +int stringconvert(string s){int p;istringstream buf(s);buf>>p;return p;} +int SOD(int n){int sum=0;for(int i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + + +//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction +//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction +//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction + + + +int main() +{ + int t,r,no=0; + sii(t); + while(t--) + { + sii(r); + int l=5*r; + int w=(60*l)/100; + int left=(45*l)/100; + int right=l-left; + int upper=w/2; + pf("Case %d:\n",++no); + pf("-%d %d\n",left,upper); + pf("%d %d\n",right,upper); + pf("%d -%d\n",right,upper); + pf("-%d -%d\n",left,upper); + } + return 0; +} + diff --git a/uva_cpp_clean/12614/12614-5.cpp b/uva_cpp_clean/12614/12614-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7a6965576d4882e178cf398c8e1e712ea0822ee1 --- /dev/null +++ b/uva_cpp_clean/12614/12614-5.cpp @@ -0,0 +1,105 @@ +/****************************************************************** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** My Blog : shipuahamed.blogspot.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_HIDDEN *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t); +#define ssii(a,b) scanf("%d%d",&a,&b); +#define ssll(a,b) scanf("%lld%lld",&a,&b); + + +#define ff first +#define se second +#define eps 1e-9 +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +templateinline void extended_euclid(T a,T b,T &x,T &y){if(a%b==0)x=0,y=1;else{extended_euclid(b,a%b,x,y);T temp=x;x=y;y=-y*(a/b)+temp;}} +templateinline T bigmod(T b,T p,T m){if(!p)return 1;else if(!(p%2)){T x=bigmod(b,p/2,m);return x*x;}else return ((b%m)*bigmod(b,p-1,m))%m;} + +void ASCII_Chart(){int i,j,k;pf("ASCII Chart:(30-129)\n");for(int i=30;i<50;i++){for(int j=0;j<5;j++){k=i+j*20;pf("%3d---> '%c' ",k,k);}pf("\n");}} + +int stringconvert(string s){int p;istringstream buf(s);buf>>p;return p;} +int SOD(int n){int sum=0;for(int i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + + +//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction +//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction +//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction + + + +int main() +{ + int t,no=0; + sii(t); + while(t--) + { + ll n,a; + sll(n); + vectorv; + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d:",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) + + +#define ff first +#define se second +#define pb push_back +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +////============ CONSTANT ===============//// +#define mx (1000000) +#define inf (1<<30) //infinity value +#define eps 1e-9 +#define mod 1000000007 +////=====================================//// + +bool v[mx+10]; +ll pd[mx+10]; +ll fact[mx+10]; +ll ans; +void sieve(){ + for (ll i = 2; i <=mx; i+= 2) + pd[i] = 2; + + for (ll i = 3; i <=mx; i += 2){ + if (!v[i]){ + pd[i] = i; + for (ll j=i; (j*i) <=mx; j += 2) + { + v[j*i] = true; + pd[j*i] = i; + } + } + } +} + +ll pow(ll a, ll b,ll Mod) +{ + if(b==1) return a; + ll x=pow(a,b/2,Mod); + x=(x*x)%Mod; + if(b%2==1) x=(x*a)%Mod; + return x; +} +ll modInverse(ll a, ll m) { + return pow(a,m-2,m); +} + +ll multi(ll n) +{ + + while (n != 1) + { + ll cnt = 0; + ll s = pd[n]; + while ((n%s) == 0) + n /= s, cnt++; + ll p=modInverse((fact[s]+1),mod); + ans=(ans*p)%mod; + fact[s]+=cnt; + ans=(ans*(fact[s]+1))%mod; + } + return ans; +} + +ll division(ll n) +{ + while (n != 1) + { + ll cnt = 0; + ll s = pd[n]; + while ((n%s) == 0) + n /= s, cnt++; + ll p=modInverse((fact[s]+1),mod); + ans=(ans*p)%mod; + fact[s]-=cnt; + ans=(ans*(fact[s]+1))%mod; + } + return ans; +} +int main() +{ + sieve(); + int t,no=0; + sii(t); + while(t--) + { + cover(fact,0); + ll sum=0; + ll d; + sll(d); + ans=1; + while(d--) + { + ll n; + sll(n); + if(n<0) + { + n=(n*(-1)); + sum=(sum+division(n))%mod; + } + else + { + sum=(sum+multi(n))%mod; + } + } + pf("Case %d: %lld\n",++no,sum); + } + return 0; +} diff --git a/uva_cpp_clean/1262/1262-14.cpp b/uva_cpp_clean/1262/1262-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af9928db14ee212940976ea26f5c68cebe0db9df --- /dev/null +++ b/uva_cpp_clean/1262/1262-14.cpp @@ -0,0 +1,140 @@ +/*************************************************** + * Problem Name : 1262 - Password.cpp + * Problem Link : https://uva.onlinejudge.org/external/12/1262.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-07-17 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 7 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +//~ } +int main () { + __FastIO; + int tc, k; + cin >> tc; + + for (int t = 1; t <= tc; t++) { + cin >> k; + string A[MAX], B[MAX]; + + for (int i = 0; i < 6; i++) { + cin >> A[i]; + } + + for (int i = 0; i < 6; i++) { + cin >> B[i]; + } + + vectorV[MAX], VV[MAX]; + vectorAA, ans; + + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 6; j++) { + char ch = A[j][i]; + V[i].pb (ch); + ch = B[j][i]; + VV[i].pb (ch); + } + } + + for (int i = 0; i < 5; i++) { + sort (all (V[i]) ); + sort (all (VV[i]) ); + } + + for (int i = 0; i < 5; i++) { + string str = ""; + mapmp; + + for (int j = 0; j < 6; j++) { + char ch = V[i][j]; + + if (mp[ch] == 0) { + mp[ch] = 1; + + for (int k = 0; k < 6; k++) { + if (ch == VV[i][k]) { + str += ch; + break; + } + } + } + } + + AA.pb (str); + } + + for (int i = 0; i < (int) AA[0].size(); i++) { + for (int j = 0; j < (int) AA[1].size(); j++) { + for (int k = 0; k < (int) AA[2].size(); k++) { + for (int l = 0; l < (int) AA[3].size(); l++) { + for (int m = 0; m < (int) AA[4].size(); m++) { + string S = ""; + S += AA[0][i]; + S += AA[1][j]; + S += AA[2][k]; + S += AA[3][l]; + S += AA[4][m]; + ans.pb (S); + } + } + } + } + } + + if (k <= (int) ans.size() ) { + cout << ans[k - 1] << "\n"; + + } else cout << "NO\n"; + } + + return 0; +} + diff --git a/uva_cpp_clean/1262/1262-19.cpp b/uva_cpp_clean/1262/1262-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a855ed44dda52cb42fbcf6d412481257d4a12dd6 --- /dev/null +++ b/uva_cpp_clean/1262/1262-19.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +char grid1[6][5], grid2[6][5]; +int mask[5][30], counter[5], val[5]; + +int main() { + int T, K; + cin >> T; + while (T--) { + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 30; j++) + mask[i][j] = 0; + counter[i] = 0; + } + cin >> K; + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 5; j++) { + cin >> grid1[i][j]; + mask[j][grid1[i][j] - 'A'] = 1; + } + } + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 5; j++) { + cin >> grid2[i][j]; + if (mask[j][grid2[i][j] - 'A'] == 1) mask[j][grid2[i][j] - 'A'] = 2, counter[j]++; + } + } + int mult = 1; + for (int i = 0; i < 5; i++) mult *= counter[i]; + if (K > mult) printf("NO\n"); + else { + int acum = 0; + for (int i = 0; i < 5; i++) { + mult /= counter[i]; + int auxi = acum; + for (int j = 0; j < counter[i]; j++) { + auxi += mult; + if (auxi >= K) { + acum = auxi - mult; + auxi = 0; + for (int k = 0; k < 30; k++) { + if (mask[i][k] == 2) { + if (auxi == j) { + val[i] = k; + break; + } + auxi++; + } + } + break; + } + } + } + for (int i = 0; i < 5; i++) printf("%c", val[i] + 'A'); + printf("\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/1262/1262-21.cpp b/uva_cpp_clean/1262/1262-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6625e6592ba2b8e2f7c5ec3d202914c1f2e478e --- /dev/null +++ b/uva_cpp_clean/1262/1262-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1262 + Name: Password + Problem: https://onlinejudge.org/external/12/1262.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +bool see[6][26]; +char row[20][20], res[6]; +int sz[5]; + +int main(){ + int T, k; + scanf("%d", &T); + while (T--) { + scanf("%d", &k); + for (int i=0; i<12; i++) + scanf("%s", row[i]); + + memset(see, 0, sizeof(see)); + for (int i=0; i<6; i++) + for (int j=0; j<5; j++) + see[j][row[i][j]-'A'] = 1; + + vector ves[5]; + for (int j=4; j>=0; j--) { + for (int i=6; i<12; i++) + if (see[j][row[i][j]-'A']) { + ves[j].push_back(row[i][j]); + see[j][row[i][j]-'A'] = 0; + } + sort(ves[j].begin(), ves[j].end()); + sz[j] = j<4 ? sz[j+1]*ves[j+1].size() : 1; + } + + if (sz[0]*ves[0].size() < k--) puts("NO"); + else { + for (int j=0; j<5; j++) { + int i = 0; + while (sz[j]*(i+1) <= k) i++; + res[j] = ves[j][i]; + k -= sz[j]*i; + } + puts(res); + } + } +} diff --git a/uva_cpp_clean/12620/12620-19.cpp b/uva_cpp_clean/12620/12620-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f0d475812888ccb08cb53f04190865c64b5758b --- /dev/null +++ b/uva_cpp_clean/12620/12620-19.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +int main() { + int fibs[300]; + unsigned long long sum[300]; + sum[0] = 0; + sum[1] = 1; + fibs[0] = 0; + fibs[1] = 1; + for (int i = 2; i < 300; i++) fibs[i] = (fibs[i-1] + fibs[i-2])%100, sum[i] = sum[i-1] + fibs[i]; + int T; + cin >> T; + while (T--) { + unsigned long long N, M; + cin >> N >> M; + if (N % 300 == 0) N++; + printf("%llu\n", ((M/300)-(N/300))*sum[299] + sum[M%300] - sum[(N+299)%300]); + } + return 0; +} diff --git a/uva_cpp_clean/12621/12621-15.cpp b/uva_cpp_clean/12621/12621-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03252c314a6db1096a64712fbe6606ae5e7dd77d --- /dev/null +++ b/uva_cpp_clean/12621/12621-15.cpp @@ -0,0 +1,42 @@ +/* + 01背包問題 +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int calories, n_testcase, course, x; + cin >> n_testcase; + +is_printed: + while (n_testcase--) { + bitset<500> dp; + dp[0] = true; + + cin >> calories >> course; + calories /= 10; + + for (int i = 0; i < course; i++) { + cin >> x; + x /= 10; + for (int j = 250; j > -1; j--) + if (dp[j]) + dp[j + x] = 1; + } + + for (int i = calories; i < 500; i++) + if (dp[i]) { + cout << i * 10 << "\n"; + goto is_printed; + } + + cout << "NO SOLUTION\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12626/12626-15.cpp b/uva_cpp_clean/12626/12626-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f573ccf8cd3ba04461e2e067e1d14ebaa0f2e3d7 --- /dev/null +++ b/uva_cpp_clean/12626/12626-15.cpp @@ -0,0 +1,32 @@ +/* + 陣列運用 +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_testcase; + char c; + cin >> n_testcase; + cin.ignore(1024, '\n'); + + while (n_testcase--) { + int count[26] = {}; + while ((c = cin.get()) != '\n') + ++count[c - 65]; + + int ans = min(0x7fffffff, count[12]); + ans = min(ans, count[0] / 3); + ans = min(ans, count[17] / 2); + ans = min(ans, count[6]); + ans = min(ans, count[19]); + ans = min(ans, count[8]); + cout << ans << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/12626/12626-20.cpp b/uva_cpp_clean/12626/12626-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea8de3e65152ed00943deb96545754dd2d045f73 --- /dev/null +++ b/uva_cpp_clean/12626/12626-20.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int tc; + + cin >> tc; + while( tc-- ) + { + char line[605]; + cin >> line; + + int M = 0, A = 0, R = 0, G = 0, I = 0, T = 0; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12626 + Name: I ❤ Pizza + Problem: https://onlinejudge.org/external/126/12626.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char SS[] = "MARGIT"; +int CC[] = {1,3,2,1,1,1}; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + string str; + while (T-- && cin >> str) { + int C[91] = {}; + for (int i=0; str[i]; ++i) + if (str[i]<91) + ++C[str[i]]; + + int mx = (int)1e9; + for (int i=0; SS[i]; ++i) + mx = min(mx, C[SS[i]]/CC[i]); + cout << mx << '\n'; + } +} diff --git a/uva_cpp_clean/12626/12626-5.cpp b/uva_cpp_clean/12626/12626-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5ef6966b560ba800d7a1ce5ea44cb335b61b978b --- /dev/null +++ b/uva_cpp_clean/12626/12626-5.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main() +{ + string nm; + int cnt[10],tst,min; + cin>>tst; + getchar(); + while(tst--) + { + min=100000; + memset(cnt,0,sizeof(cnt)); + getline(cin,nm); + int len=nm.size(); + for(int k=0;kcnt[m]) min=cnt[m]; + } + cout< +using namespace std; + +long long pow[32]; + +long long proc(int K, int A, int B) { + if (B < A) return 0; + if (A == 1 && B == (1<> T; + for (int c = 1; c <= T; c++) { + int K, A, B; + cin >> K >> A >> B; + printf("Case %d: %lld\n", c, proc(K, A, B)); + } + return 0; +} diff --git a/uva_cpp_clean/12640/12640-14.cpp b/uva_cpp_clean/12640/12640-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0395837520b2bfb4bec48d9d1602bf44262a715 --- /dev/null +++ b/uva_cpp_clean/12640/12640-14.cpp @@ -0,0 +1,91 @@ +/*************************************************** + * Problem name : 12640 Largest Sum Game.cpp + * Problem Link : https://uva.onlinejudge.org/external/126/12640.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-27 + * Problem Type : Maximum Sum(kadane) + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int kadane(vectorar, int n) { + int mx, sum; + mx = INT_MIN, sum = 0; + for (int i = 0; i < n; i++) { + sum = sum + ar[i]; + if (sum > mx) { + mx = sum; + } + if (sum < 0) { + sum = 0; + } + } + return mx; +} +int main () { + int x; + char ch; + vectorar; + while (scanf("%d%c", &x, &ch) == 2) { + int flag = 0; + if(x>0) flag = 1; + ar.push_back(x); + if (ch != '\n') { + while (scanf("%d%c", &x, &ch) == 2) { + ar.push_back(x); + if(x>0) flag = 1; + if(ch == '\n') break; + } + } + if(!flag) printf("0\n"); + else printf("%d\n", kadane(ar, ar.size())); + ar.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/12646/12646-19.cpp b/uva_cpp_clean/12646/12646-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1541d696fe388154c289753bde50f400bc5e00e --- /dev/null +++ b/uva_cpp_clean/12646/12646-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int a, b, c; + while (cin >> a >> b >> c) { + if (a != b && b == c) printf("A\n"); + else if (b != a && a == c) printf("B\n"); + else if (c != b && a == b) printf("C\n"); + else printf("*\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12646/12646-21.cpp b/uva_cpp_clean/12646/12646-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3949db01751e14765438023025115d6769517cd --- /dev/null +++ b/uva_cpp_clean/12646/12646-21.cpp @@ -0,0 +1,26 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12646 + Name: Zero or One + Problem: https://onlinejudge.org/external/126/12646.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int a, b, c; + while (cin>>a>>b>>c) { + int t = (a<<2)+(b<<1)+c; + if (t==7 || t==0) puts("*"); + else if (t==6 || t==1) puts("C"); + else if (t==5 || t==2) puts("B"); + else puts("A"); + } +} diff --git a/uva_cpp_clean/12650/12650-14.cpp b/uva_cpp_clean/12650/12650-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e588b5cf88953e180c6b6e2b4c0cce286140fb88 --- /dev/null +++ b/uva_cpp_clean/12650/12650-14.cpp @@ -0,0 +1,89 @@ +/*************************************************** + * Problem name : 12650 Dangerous Dive.cpp + * OJ : Uva + * Verdict : AC + * Date : 01.07.2017 + * Problem Type : AhHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 10003 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int BinarySearch(int ar[], int n, int value) { + int low = 0, high = n - 1; + while (high >= low) { + int mid = (high + low) / 2; + if (ar[mid] == value) return 1; + else if (ar[mid] > value) high = mid - 1; + else low = mid + 1; + } + return 0; +} +int main () { + int N, R; + int ar[MAX]; + vectorans; + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + while (scanf("%d %d", &N, &R) == 2) { + for (int i = 0; i < R ; i++) scanf("%d", &ar[i]); + if (N == R) { + printf("*\n"); + continue; + } + sort(ar, ar + R); + for (int i = 1; i <= N; i++) { + int chk = BinarySearch(ar, R, i); + if (chk == 0) ans.push_back(i); + } + int sz = ans.size(); + for (int i = 0; i < sz; i++) { + printf("%d ", ans[i]); + } + nl; + ans.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/12650/12650-20.cpp b/uva_cpp_clean/12650/12650-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa6f436cab2e80388241eacdeeeb6e4c60430869 --- /dev/null +++ b/uva_cpp_clean/12650/12650-20.cpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +bool volunteer[10005]; + +int main() +{ + int N, R; + + while( cin >> N >> R ) + { + for(int i=0 ; i<10005 ; i++) + volunteer[i] = false; + + int temp; + for(int i=0 ; i> temp; + volunteer[temp] = true; + } + + bool flag = true; + for(int i=1 ; i<=N ; i++) + if( volunteer[i]==false ) + { + cout << i << " "; + flag = false; + } + + if( flag ) cout << "*"; + cout << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12655/12655-14.cpp b/uva_cpp_clean/12655/12655-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ccdcbffe1b664aefb7e767f8667d81e867c65335 --- /dev/null +++ b/uva_cpp_clean/12655/12655-14.cpp @@ -0,0 +1,219 @@ +/*************************************************** + * Problem Name : 12655 Trucks.cpp + * Problem Link : https://uva.onlinejudge.org/external/126/12655.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-09-08 + * Problem Type : LCA + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 20005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX], cost[MAX]; +int level[MAX], par[MAX]; +int table[MAX][17], mn_rd[MAX][17]; + + +struct Edge { + int u, v, w; + bool operator< (const Edge &p) const { + return w > p.w; + } +}; +vectore; +int find (int r) { + if (par[r] == r) return r ; + else return par[r] = find (par[r]); +} +void MST (int n) { + sort (e.begin(), e.end() ); + + for (int i = 1; i <= n; i++) { + par[i] = i; + } + + int cnt = 0, sz = (int) e.size(); + + for (int i = 0; i < sz; i++) { + int u = find (e[i].u); + int v = find (e[i].v); + + if (u != v) { + par[u] = v; + cnt++; + adj[e[i].u].pb (e[i].v); + adj[e[i].v].pb (e[i].u); + cost[e[i].u].pb (e[i].w); + cost[e[i].v].pb (e[i].w); + + if (cnt == n) break; + } + } +} + + +void dfs (int u, int p) { + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + level[v] = level[u] + 1; + par[v] = u; + mn_rd[v][0] = cost[u][i]; + dfs (v, u); + } +} + +void LCA (int n) { + //~ SET (table, -1); + for (int i = 1; i <= n; i++) { + table[i][0] = par[i]; + } + + for (int j = 1; (1 << j) < n; j++) { + for (int i = 1; i <= n; i++) { + if (table[i][j - 1] != -1) { + int x = table[i][j - 1]; + table[i][j] = table[x][j - 1]; + mn_rd[i][j] = min (mn_rd[i][j - 1], mn_rd[x][j - 1]); + + } else { + mn_rd[i][j] = mn_rd[i][j - 1]; + table[i][j] = -1; + } + } + } +} + +int LCA_Quary (int u, int v) { + if (level[u] < level[v]) swap (u, v); + + int lim = log2 (level[u]); + + for (int i = lim; i >= 0; i--) { + if (level[u] - (1 << i) >= level[v]) { + u = table[u][i]; + } + } + + if (u == v) return u; + + for (int i = lim; i >= 0; i--) { + if (table[u][i] != -1 && table[u][i] != table[v][i]) { + u = table[u][i]; + v = table[v][i]; + } + } + + return par[u]; +} +int main () { + __FastIO; + int n, m, s; + + while (cin >> n >> m >> s) { + for (int i = 1; i <= m ; i++) { + Edge get; + cin >> get.u >> get.v >> get.w; + e.pb (get); + } + + MST (n); + par[1] = -1; + level[1] = 0; + mn_rd[1][0] = 100005; + dfs (1, -1); + LCA (n); + + for (int q = 1; q <= s; q++) { + int u, v; + cin >> u >> v; + int x = LCA_Quary (u, v); + int uu = level[u] - level[x]; + int vv = level[v] - level[x]; + int mn = 100005; + + for (int i = log2 (uu); i >= 0; i--) { + if ( (1 << i) <= uu) { + mn = min (mn, mn_rd[u][i]); + u = table[u][i]; + uu -= (1 << i); + } + + if (uu == 0) break; + } + + for (int i = log2 (vv); i >= 0; i--) { + if ( (1 << i) <= vv) { + mn = min (mn, mn_rd[v][i]); + v = table[v][i]; + vv -= (1 << i); + } + + if (vv == 0) break; + } + + cout << mn << "\n"; + } + + e.clear(); + + for (int i = 0; i <= n; i++) { + adj[i].clear(); + cost[i].clear(); + } + } + + return 0; +} diff --git a/uva_cpp_clean/12657/12657-17.cpp b/uva_cpp_clean/12657/12657-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46eb362eb950080d90e0f42cb3ab8262b8b0da8a --- /dev/null +++ b/uva_cpp_clean/12657/12657-17.cpp @@ -0,0 +1,84 @@ +/** + * AOAPC II Example 6-5 Boxes in a Lines + */ +#include +using namespace std; +vector L, R; +void link(int l, int r) +{ + R[l] = r; + L[r] = l; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n, m, T = 1; + while (cin >> n >> m) + { + L.clear(); + R.clear(); + for (int i = 0; i <= n; ++i) + { + L.push_back(i - 1); + R.push_back(i + 1); + } + L[0] = n; + R[n] = 0; + bool inv = false; + while (m--) + { + int op; + cin >> op; + if (op == 4) + { + inv = !inv; + continue; + } + int x, y; + cin >> x >> y; + if (op == 3 && R[y] == x) + swap(x, y); + int lx = L[x], rx = R[x], ly = L[y], ry = R[y]; + if (op == 3) + { + if (rx == y) + { + link(lx, y); + link(y, x); + link(x, ry); + } + else + { + link(lx, y); + link(y, rx); + link(ly, x); + link(x, ry); + } + continue; + } + if (inv) + op = 3 - op; + if (op == 1 && ly == x || op == 2 && ry == x) + continue; + link(lx, rx); + if (op == 1) + { + link(ly, x); + link(x, y); + } + else + { + link(y, x); + link(x, ry); + } + } + long long ans = 0; + for (int i = 1, cur = R[0]; i <= n; ++i, cur = R[cur]) + if (i % 2) + ans += cur; + if (inv && n % 2 == 0) + ans = (long long) n * (n + 1) / 2 - ans; + cout << "Case " << T++ << ": " << ans << "\n"; + } +} diff --git a/uva_cpp_clean/1266/1266-20.cpp b/uva_cpp_clean/1266/1266-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c41583fb92bfef49217806cea966ba1241b40577 --- /dev/null +++ b/uva_cpp_clean/1266/1266-20.cpp @@ -0,0 +1,47 @@ +#include +#include + +int num[17][17]; + +int main() +{ + int N; + bool newLine = false; + + while( scanf("%d", &N)==1 ) + { + memset(num, 0, sizeof(num)); + + int x = 0, y = N/2, temp = N*N; + for(int i=1 ; i<=temp ; i++) + { + if( num[x][y] ) + { + x += 2, y--; + if( x>=N ) x -= N; + if( y<0 ) y += N; + num[x][y] = i; + } + else num[x][y] = i; + + x--, y++; + if( x<0 ) x += N; + if( y>=N ) y -= N; + } + + int len = 1; + while( temp ) + { + temp /= 10; + len++; + } + + if( newLine ) printf("\n"); + newLine = true; + + printf("n=%d, sum=%d\n", N, N*((N*N)+1)/2); + for(int i=0 ; i + +using namespace std; + +const int SIZE = 18; + +int tc, N, r, c, it, grid[SIZE][SIZE], len; +int dr[] = {-1, 1}; +int dc[] = {1, 0}; + +int main(){ + while(~scanf("%d", &N)){ + if(tc++) putchar('\n'); + memset(grid, 0, sizeof grid); + r = 0, c = N / 2, it = 0; + grid[r][c] = ++it; + while( it < N * N ){ + int nr = ( r + dr[0] + N ) % N, nc = ( c + dc[0] ) % N; + if( grid[nr][nc] ) + nr = ( r + dr[1] ) % N, nc = ( c + dc[1] ) % N; + grid[nr][nc] = ++it; + r = nr, c = nc; + } + len = ceil( log10(N * N) ); + printf("n=%d, sum=%d\n", N, N * ( N * N + 1 ) / 2 ); + for(int row = 0; row < N; row++){ + for(int col = 0; col < N; col++) + printf(" %*d", len, grid[row][col]); + putchar('\n'); + } + } + return(0); +} diff --git a/uva_cpp_clean/12667/12667-19.cpp b/uva_cpp_clean/12667/12667-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2eb15e96791cc1fbadd9ef5278fb092ace5907f7 --- /dev/null +++ b/uva_cpp_clean/12667/12667-19.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vi; + +int main() { + int n, t, m; + cin >> n >> t >> m; + vector lb; + for (int i = 0; i < n; i++) lb.push_back(ii(-1, -1)); + vector sub; + for (int i = 0; i < n; i++) sub.push_back(vi(t, 0)); + for (int i = 0; i < m; i++) { + int time, id; + char p; + string ver; + cin >> time >> id >> p >> ver; + if (ver == "Yes") { + if (sub[p-'A'][id-1] == 0) { + sub[p-'A'][id-1] = 1; + lb[p-'A'].first = id; + lb[p-'A'].second = time; + } + } + } + for (int i = 0; i < n; i++) { + printf("%c ", i+'A'); + if (lb[i].first == -1) printf("- -\n"); + else { + printf("%d %d\n", lb[i].second, lb[i].first); + } + } + return 0; +} diff --git a/uva_cpp_clean/1267/1267-17.cpp b/uva_cpp_clean/1267/1267-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a437feefbb421c6cfa18e27881877bbb05757d7 --- /dev/null +++ b/uva_cpp_clean/1267/1267-17.cpp @@ -0,0 +1,64 @@ +/** + * Network + * 2007 Seoul + * LA 3902 + */ +#include +using namespace std; +vector p; +vector> dep, adj; +vector cov; +int k; +void calc(int cur, int pa, int d) +{ + p[cur] = pa; + if (adj[cur].size() == 1) + dep[d].push_back(cur); + ++d; + for (int u : adj[cur]) + if (u != pa) + calc(u, cur, d); +} +void cover(int ser, int pa, int d) +{ + cov[ser] = true; + if (++d > k) + return; + for (int u : adj[ser]) + if (u != pa) + cover(u, ser, d); +} +int main() +{ + int t; + cin >> t; + while (t--) + { + int n, s; + cin >> n >> s >> k; + adj = vector>(n + 1); + for (int i = 0; i < n - 1; ++i) + { + int u, v; + cin >> u >> v; + adj[u].push_back(v); + adj[v].push_back(u); + } + p = vector(n + 1); + dep = vector>(n + 1); + cov = vector(n + 1, false); + calc(s, -1, 0); + int ans = 0; + for (int d = n - 1; d > k; --d) + for (int u : dep[d]) + if (!cov[u]) + { + ++ans; + int ser = u; + for (int i = 0; i < k; ++i) + ser = p[ser]; + cover(ser, -1, 0); + } + cout << ans << "\n"; + } +} diff --git a/uva_cpp_clean/12670/12670-19.cpp b/uva_cpp_clean/12670/12670-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b40270666489ba18819e6b15d6b1f1f7d8a013f9 --- /dev/null +++ b/uva_cpp_clean/12670/12670-19.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +inline long long proc1(long long n) { + if (n == 0) return 0; + long long ret = 0; + while (n > 0) { + int l = floor(log2(n)); + ret += l*((long long) 1<<(l-1)); + long long p = (long long) 1<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12694 + Name: Meeting Room Arrangement + Problem: https://onlinejudge.org/external/126/12694.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int n; +pair E[23]; + +int memo[13][23]; +int dfs(int s, int i) { + if (i == n) return 0; + if (E[i].first < s) { + return dfs(s, i+1); + } + + if (memo[s][i] != -1) { + return memo[s][i]; + } + + return memo[s][i] = max( + dfs(E[i].second, i+1)+1, + dfs(s, i+1) + ); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + while (T--) { + int s, f; + for (n=0; cin >> s >> f && (s || f); n++) { + E[n].first = s; + E[n].second = f; + } + sort(E, E+n); + memset(memo, -1, sizeof(memo)); + cout << dfs(0, 0) << endl; + } +} diff --git a/uva_cpp_clean/127/127-21.cpp b/uva_cpp_clean/127/127-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..604ab42c3e47b57a30400cad0b8090d71c52f353 --- /dev/null +++ b/uva_cpp_clean/127/127-21.cpp @@ -0,0 +1,101 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 127 + Name: "Accordian" Patience + Problem: https://onlinejudge.org/external/1/127.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +class FastStack { + int data[60], sz; + public: + FastStack():sz(0) {} + void clear() { sz=0; } + bool empty() { return !sz; } + void push(int x) { data[sz++] = x; } + int pop() { return data[--sz]; } + int top() { return data[sz-1]; } + int size() { return sz; } +}; + +void swap(FastStack* &a, FastStack* &b) { + FastStack *t = a; a = b; b = t; +} + +// ---------------------------------------------------------------------- + +bool match[52][52]; +FastStack *stacks[54]; +int meld(int ind) { + FastStack *cs = stacks[ind]; + int cur = cs->top(), + bind = ind; + + while (1) { + while (ind>2 && match[stacks[ind-3]->top()][cur]) { + stacks[ind-3]->push(cs->pop()); + int c = 0; + for (int k = 2; k>=0; --k) { + c += meld(ind-k-c-1); + if (c) swap(stacks[ind-k-c], stacks[ind-k]); + } + ind -= c; + if (cs->empty()) + return bind-ind+1; + cur = cs->top(); + } + + if (ind>0 && match[stacks[ind-1]->top()][cur]) { + stacks[ind-1]->push(cs->pop()); + int c = meld(ind-1); + if (c) { + swap(stacks[ind-c], stacks[ind]); + ind -= c; + } + if (cs->empty()) + return bind-ind+1; + cur = cs->top(); + } + else break; + } + return bind-ind; +} + + + +int vals[200]; +char sord[] = "CDHS", ford[] = "A23456789TJQK"; +int main() { + for (int i=0; i<4; ++i) + vals[sord[i]] = 13*i; + for (int i=0; i<13; ++i) + vals[ford[i]] = i; + + for (int i=0; i<52; ++i) + for (int j=0; j<=i; ++j) + if (i%13==j%13 || i/13==j/13) + match[i][j]=match[j][i]=1; + + for (int i=0; i<54; ++i) + stacks[i] = new FastStack(); + + char line[156]; + while (fread(line, 1, 156, stdin) == 156) { + int sz = 0; + for (int i=0; i<52; ++i) { + stacks[sz++]->clear(); + stacks[sz-1]->push(vals[line[3*i]] + vals[line[3*i+1]]); + sz -= meld(sz-1); + } + printf("%d pile%s remaining:", sz, sz==1?"":"s"); + for (int i=0; isize()); + putchar('\n'); + } +} diff --git a/uva_cpp_clean/12700/12700-5.cpp b/uva_cpp_clean/12700/12700-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c58b153776ad44b7cc299e208a201a490b428206 --- /dev/null +++ b/uva_cpp_clean/12700/12700-5.cpp @@ -0,0 +1,190 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int main() +{ + int t,no=0; + si(t); + while(t--) + { + int n; + si(n); + string s; + cin>>s; + + mci mp; + for0(i,n) + { + mp[s[i]]++; + } + Case(no); + if(mp['A']==n) + { + PS("ABANDONED"); + } + else if(mp['B']==n || (mp['B']+mp['A']==n)) + { + PS("BANGLAWASH"); + } + else if(mp['W']==n || (mp['W']+mp['A']==n)) + { + PS("WHITEWASH"); + } + else if(mp['A']==n) + { + PS("ABANDONED"); + } + else if(mp['W']>mp['B']) + { + printf("WWW %d - %d\n",mp['W'],mp['B']); + } + else if(mp['W'] +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (10010) +#define mod 1000000007 +////=====================================//// + +bool prime[mx]; +int pd[mx]; + + +struct factorize +{ + int prime,pow=0; +}; + + +void seive() +{ + prime[0]=1; + prime[0]=1; + for(int i=2;i<=mx;i+=2) + pd[i]=2,prime[i]=1; + + prime[2]=0; + + for(ll i=3;i<=mx;i+=2) + { + if(!prime[i]) + { + pd[i]=i; + ll mul=i<<1; + ll j=i; + for(j*=i;j<=mx;j+=mul) + { + pd[j]=i; + prime[j]=1; + } + } + } +} + +vectorv[50]; +void fact(int n,int pos) +{ + + while(n!=1) + { + int s=pd[n],cnt=0; + while(n%s==0) + { + n/=s,cnt++; + } + factorize res; + res.pow=cnt; + res.prime=s; + v[pos].pb(res); + } + +} + + +int main() +{ + seive(); + int t; + si(t); + while(t--) + { + int n,a,b; + sii(n,a),si(b); + + fact(a,0); + fact(b,1); + + forab(i,2,n) + { + vectoradd; + mii mp; + for0(j,sz(v[i-1])) + { + mp[v[i-1][j].prime]+= v[i-1][j].pow; + } + for0(j,sz(v[i-2])) + { + mp[v[i-2][j].prime]+= v[i-2][j].pow; + } + forstl(it,mp) + { + factorize res; + res.prime= it->ff; + res.pow= it->se; + add.pb(res); + } + v[i]=add; + } + + int s=sz(v[n]); + for0(i,s) + { + PP(v[n][i].prime,v[n][i].pow); + } + nl; + for0(i,n+1) + { + v[i].clear(); + } + + } + return 0; +} + + diff --git a/uva_cpp_clean/12704/12704-19.cpp b/uva_cpp_clean/12704/12704-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cab75fd2dbdf2b9a78089559d2d73fafb9d9e216 --- /dev/null +++ b/uva_cpp_clean/12704/12704-19.cpp @@ -0,0 +1,49 @@ +// las12uvas contest PROBLEM I 2015 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector

vdd; +typedef vector vll; +typedef vector vvi; + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + float x, y, r; + scanf("%f %f %f", &x, &y, &r); + float d = sqrt(x*x + y*y); + printf("%.2f %.2f\n",r-d, r+d); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12708/12708-14.cpp b/uva_cpp_clean/12708/12708-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72cd37148fc80f5823f7af064282dad975cb1485 --- /dev/null +++ b/uva_cpp_clean/12708/12708-14.cpp @@ -0,0 +1,65 @@ +/*************************************************** + * Problem name : 12708 GCD The Largest.cpp + * Problem Link : https://uva.onlinejudge.org/external/127/12708.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-04-29 + * Problem Type : Number Theory (GCD) + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc; + ll n; + scanf("%d", &tc); + for(int t = 1; t<=tc; t++){ + scanf("%lld", &n); + if(n%2 == 0){ + printf("%lld\n", n/2); + } + else printf("%lld\n", (n-1)/2); + } + return 0; +} diff --git a/uva_cpp_clean/12708/12708-19.cpp b/uva_cpp_clean/12708/12708-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b8e8db45e24b7afa8949fa3c7ed57770760c136 --- /dev/null +++ b/uva_cpp_clean/12708/12708-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int T; + scanf("%d", &T); + while (T--) { + long long N; + scanf("%lld", &N); + printf("%lld\n", (N >> 1)); + } + return 0; +} diff --git a/uva_cpp_clean/12708/12708-21.cpp b/uva_cpp_clean/12708/12708-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf5d94a129393816a3d128fc9d0ca039e2b98a5e --- /dev/null +++ b/uva_cpp_clean/12708/12708-21.cpp @@ -0,0 +1,24 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12708 + Name: GCD The Largest + Problem: https://onlinejudge.org/external/127/12708.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + long long n; + while (T-- && cin >> n) + cout << (n >> 1) << '\n'; +} diff --git a/uva_cpp_clean/12708/12708-5.cpp b/uva_cpp_clean/12708/12708-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..114db240903b61d7162587a308f628eea85ecd31 --- /dev/null +++ b/uva_cpp_clean/12708/12708-5.cpp @@ -0,0 +1,27 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include + +int main() +{ + long long n; + int t; + scanf("%d",&t); + while(t--) + { + scanf("%lld",&n); + + printf("%lld\n",n>>1); + } + + return 0; +} + diff --git a/uva_cpp_clean/12709/12709-21.cpp b/uva_cpp_clean/12709/12709-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a5a4488550676904da4ecdf4cd9307f67f5e953 --- /dev/null +++ b/uva_cpp_clean/12709/12709-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12709 + Name: Falling Ants + Problem: https://onlinejudge.org/external/127/12709.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, l, w, h; + while (cin >> n && n) { + int mxh=0, mxv=0; + for (int i=0; i> l >> w >> h; + int v = l*w*h; + if (h > mxh || (h == mxh && v > mxv)) { + mxh = h; + mxv = v; + } + } + cout << mxv << endl; + } +} diff --git a/uva_cpp_clean/12709/12709-5.cpp b/uva_cpp_clean/12709/12709-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d05bceab1a5c8872976eb4f568b29720b0051745 --- /dev/null +++ b/uva_cpp_clean/12709/12709-5.cpp @@ -0,0 +1,170 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int main() +{ + int t; + while(si(t)==1) + { + if(t==0) break; + + int res=0,ans=0; + while(t--) + { + int l,w,h; + sii(l,w),si(h); + if(res>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12716 + Name: GCD XOR + Problem: https://onlinejudge.org/external/127/12716.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int D[30000005] = {}; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, X[10001], mx=0; + cin >> T; + for (int i=0; i> X[i]; + mx = max(mx, X[i]); + } + + for (int c=mx/3; c>0; --c) { + int tc = c+c; + for (int a = c+tc; a<=mx; a+=tc) + if ( (a & c) == c ) + ++D[a]; + } + + for (int i=1; i<=mx; ++i) + D[i] += D[i-1]; + + for (int i=1; i<=T; ++i) + cout << "Case " << i << ": " << D[X[i-1]] << "\n"; +} diff --git a/uva_cpp_clean/12720/12720-21.cpp b/uva_cpp_clean/12720/12720-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bac856faf41f080c7023c9701906008b9660969 --- /dev/null +++ b/uva_cpp_clean/12720/12720-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12720 + Name: Algorithm of Phil + Problem: https://onlinejudge.org/external/127/12720.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + for (int cse=1; cse<=T; ++cse) { + string a; cin >> a; + int l=a.length(), i=(l-1)>>1; + int s = l%2 ? a[i--] == '1' : 0; + for (; i>=0; --i) { + bool ii=a[i]=='1', jj=a[l-i-1]=='1'; + int t = ii&&jj ? 3 : ii||jj ? 2 : 0; + s = (((long long)s << 2) | t) % 1000000007; + } + + printf("Case #%d: %d\n", cse, s); + } +} diff --git a/uva_cpp_clean/12747/12747-6.cpp b/uva_cpp_clean/12747/12747-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce08a154b98bcbdb461d08f315c032067427374c --- /dev/null +++ b/uva_cpp_clean/12747/12747-6.cpp @@ -0,0 +1,56 @@ +/* + dynamic programming > LCS reduced to LIS + difficulty: medium + date: 06/Nov/2020 + problem: edit distance with only inserting and deleting between two permutations from 1 to N + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; +const int INF = 1 << 30; + +int lis(vector a) { + int N = a.size(); + + vector size(N); + vector last(N+1, INF); + last[0] = -INF; + + int biggest_s = 0; + + for (int j = 0; j < N; j++) { + int s = lower_bound(last.begin(), last.end(), a[j]) - last.begin(); + + if (a[j] < last[s]) + last[s] = a[j]; + + size[j] = s; + biggest_s = max(biggest_s, s); + } + + return biggest_s; +} + +int lcs(vector a, vector b) { + vector rev_a(b.size()+1, -1); + vector c(b.size()); + for (int i = 0; i < a.size(); i++) rev_a[a[i]] = i; + for (int i = 0; i < c.size(); i++) c[i] = rev_a[b[i]]; + return lis(c); +} + +int main() { + int T, t = 1; cin >> T; + while (T--) { + int N; cin >> N; + vector A(N), B(N); + for (int &a : A) cin >> a; + for (int &b : B) cin >> b; + + int ans = (N-lcs(A, B)) * 2; + cout << "Case " << (t++) << ": " << ans << endl; + } + return 0; +} + diff --git a/uva_cpp_clean/12748/12748-20.cpp b/uva_cpp_clean/12748/12748-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..120035a134366f7d3ce8dd64a62d05a94f09672c --- /dev/null +++ b/uva_cpp_clean/12748/12748-20.cpp @@ -0,0 +1,37 @@ +#include + +#define sqr(n) ((n)*(n)) + +int rx[1005], ry[1005], r[1005]; + +int main() +{ + int tc, nCase = 1; + scanf("%d", &tc); + while( tc-- ) + { + int n, y; + scanf("%d %d", &n, &y); + + for(int i=0 ; i=sqr(rx[i]-xx) + sqr(ry[i]-yy) ) flag = true; + if( flag ) break; + } + + if( flag ) printf("Yes\n"); + else printf("No\n"); + } + nCase++; + } + return 0; +} diff --git a/uva_cpp_clean/12750/12750-14.cpp b/uva_cpp_clean/12750/12750-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd5a363cd5d516d8cb6a201922fc442a5cce396f --- /dev/null +++ b/uva_cpp_clean/12750/12750-14.cpp @@ -0,0 +1,87 @@ +/*************************************************** + * Problem name : 12750 Keep Rafa at Chelsea!.cpp + * Problem Link : https://uva.onlinejudge.org/external/127/12750.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-05 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ __FastIO; + int tc, n; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d", &n); + int cnt = 0, ans = 503; + for (int i = 1; i <= n; i++) { + string str; + cin >> str; + if (str == "L") { + cnt++; + if (cnt >= 3) { + ans = min(ans, i); + } + } else if (str == "D") { + cnt++; + if (cnt >= 3) { + ans = min(ans, i); + } + } else { + cnt = 0; + } + } + if (ans == 503) { + printf("Case %d: Yay! Mighty Rafa persists!\n", t); + } else { + printf("Case %d: %d\n", t, ans); + } + } + return 0; +} diff --git a/uva_cpp_clean/12750/12750-20.cpp b/uva_cpp_clean/12750/12750-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..783b5db1dc3e45e163df431e7637357736a5db9c --- /dev/null +++ b/uva_cpp_clean/12750/12750-20.cpp @@ -0,0 +1,32 @@ +#include + +char input[3]; + +int main() +{ + int tc, nCase = 1; + scanf("%d", &tc); + while( tc-- ) + { + int n; + scanf("%d", &n); + + bool flag = false; + int match = 0, counter = 0; + for(int i=1 ; i<=n ; i++) + { + scanf("%s", input); + + if( input[0]=='W' ) match = 0; + else match++; + + if( !flag ) counter++; + if( match>=3 ) flag = true; + } + + if( n==counter && !flag ) printf("Case %d: Yay! Mighty Rafa persists!\n", nCase); + else printf("Case %d: %d\n", nCase, counter); + nCase++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12751/12751-21.cpp b/uva_cpp_clean/12751/12751-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce855beeb2bd6beb71063c14423b6c491af6b906 --- /dev/null +++ b/uva_cpp_clean/12751/12751-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12751 + Name: An Interesting Game + Problem: https://onlinejudge.org/external/127/12751.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + int T, n, k, x; + cin >> T; + for (int cse=1; cse<=T && cin >> n >> k >> x; ++cse) + cout << "Case " << cse << ": " << ( ((n*(n+1))>>1) - ((k*(2*x+k-1))>>1) ) << '\n'; +} diff --git a/uva_cpp_clean/12791/12791-6.cpp b/uva_cpp_clean/12791/12791-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..123c9ee2105ddd91e4e70085ab8a3611d76cbee6 --- /dev/null +++ b/uva_cpp_clean/12791/12791-6.cpp @@ -0,0 +1,18 @@ +/* + math > ad-hoc + difficulty: easy + date: 07/May/2020 + by: @brpapa +*/ +#include +using namespace std; + +int main() { + double x, y; + while (cin >> x >> y) { + double t = x; + while (t/x - t/y < 1) t += x; + cout << t/x << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12792/12792-6.cpp b/uva_cpp_clean/12792/12792-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf5d2066eb38a85efa1d9531a9fbb8abcccf0f3b --- /dev/null +++ b/uva_cpp_clean/12792/12792-6.cpp @@ -0,0 +1,24 @@ +/* + brute force > iterative + difficulty: medium + date: 08/May/2020 + problem: simulation + hint: note that if you 'watch' a unique card, the full deck will become sorted as soon as this card reaches its original position + by: @brpapa +*/ +#include +using namespace std; + +int main() { + int N; + while (cin >> N) { + int ans = 1, p1 = 2; // atual posição do número 1 + while (p1 != 1) { + if (p1 <= N/2) p1 *= 2; + else p1 = p1*2-N-1; + ans++; + } + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12797/12797-6.cpp b/uva_cpp_clean/12797/12797-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..227bf2ff58d4e2d5265dbc43bd65a746a093e890 --- /dev/null +++ b/uva_cpp_clean/12797/12797-6.cpp @@ -0,0 +1,68 @@ +/* + graphs > shortest path > single-source > unweighted graph + difficulty: medium + date: 08/May/2020 + hint: apply a BFS for each subset of letters possible (only 2^10) using bitmask + by: @brpapa +*/ +#include +#define INF (1 << 30) +#define UNVISITED 0 +#define VISITED 1 +using namespace std; +typedef pair ii; + +int N; +char grid[101][101]; +int state[101][101]; +int lvl[101][101]; + +int mask; // 0100 = 3a lowercase está habilitada, e 3a uppercase está desabilitada +bool check(char ch) { + if (islower(ch)) return (mask & (1 << (ch-'a'))); + return !(mask & (1 << (ch-'A'))); +} + +int dr[4] = {1, -1, 0, 0}; +int dc[4] = {0, 0, 1, -1}; +int bfs(int sr, int sc) { + for (int r = 0; r < N; r++) for (int c = 0; c < N; c++) state[r][c] = UNVISITED, lvl[r][c] = INF; + queue q; if (check(grid[sr][sc])) q.push(ii(sr, sc)); + lvl[sr][sc] = 1; + + while (!q.empty()) { + int r = q.front().first; + int c = q.front().second; + q.pop(); + + state[r][c] = VISITED; + + // (r, c) -> (nr, nc) + for (int d = 0; d < 4; d++) { + int nr = r + dr[d]; + int nc = c + dc[d]; + if (nr < 0 || nr == N || nc < 0 || nc == N) continue; + if (state[nr][nc] == VISITED) continue; + + if (check(grid[nr][nc])) { + lvl[nr][nc] = lvl[r][c] + 1; + q.push(ii(nr, nc)); + } + } + } + return lvl[N-1][N-1]; +} + +int main() { + while (cin >> N) { + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + cin >> grid[i][j]; + + int ans = INF; + for (mask = 0; mask < (1 << 10); mask++) // O(2^10) + ans = min(ans, bfs(0, 0)); + cout << (ans == INF? -1 : ans) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12798/12798-19.cpp b/uva_cpp_clean/12798/12798-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..955420b5dd90be9841c0ab50d2a50fecc16bc304 --- /dev/null +++ b/uva_cpp_clean/12798/12798-19.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main() { + int N, M; + while (scanf("%d %d", &N, &M) != EOF) { + int cont = 0; + for (int i = 0; i < N; i++) { + bool add = true; + for (int j = 0; j < M; j++) { + int score; + scanf("%d", &score); + if (score == 0) add = false; + } + if (add) cont++; + } + printf("%d\n", cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12798/12798-21.cpp b/uva_cpp_clean/12798/12798-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..031c0591643b86049f1f6cdc50bc7e13bfe731c4 --- /dev/null +++ b/uva_cpp_clean/12798/12798-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12798 + Name: Handball + Problem: https://onlinejudge.org/external/127/12798.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m; + while (cin >> n >> m) { + int cnt = 0; + for (int i=0; i> x; + f &= x > 0; + } + cnt += f; + } + cout << cnt << '\n'; + } +} diff --git a/uva_cpp_clean/12798/12798-6.cpp b/uva_cpp_clean/12798/12798-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c63e5a088918be08ccc38851195e42b89d0410bc --- /dev/null +++ b/uva_cpp_clean/12798/12798-6.cpp @@ -0,0 +1,24 @@ +/* + ad-hoc + difficulty: easy + date: 08/May/2020 + by: @brpapa +*/ +#include +using namespace std; + +int main() { + int N, M; + while (cin >> N >> M) { + int ans = 0, g; + while (N--) { + int mn = 101; + for (int m = 0; m < M; m++) + cin >> g, mn = min(mn, g); + + ans += mn > 0; + } + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/128/128-21.cpp b/uva_cpp_clean/128/128-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..797414531ff9d9b1ae9368dfe3aa04136a667cae --- /dev/null +++ b/uva_cpp_clean/128/128-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 128 + Name: Software CRC + Problem: https://onlinejudge.org/external/1/128.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char line[2000]; + while (cin.getline(line, 2000) && (line[0]!='#' || line[1])) { + unsigned int rem = 0; + for (int i=0; line[i]; i++) + rem = ((rem<<8) + line[i]) % 34943; + rem = (rem<<16) % 34943; + + if (rem > 0) rem = 34943 - rem; + printf("%02X %02X\n", rem>>8, rem&0xFF); + } +} diff --git a/uva_cpp_clean/12801/12801-6.cpp b/uva_cpp_clean/12801/12801-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cf427bdb3db25624d893039b3a00f8bd8fdc86b --- /dev/null +++ b/uva_cpp_clean/12801/12801-6.cpp @@ -0,0 +1,34 @@ +/* + brute force > iterative + difficulty: easy + date: 09/May/2020 + by: @brpapa +*/ +#include +using namespace std; + +int main() { + int C, N; + while (cin >> C >> N) { + vector olives(2*C, 0); + for (int n = 0; n < N; n++) { + int c; cin >> c; olives[c] = olives[c+C] = 1; + } + + vector acc(2*C); acc[0] = olives[0]; + for (int i = 1; i < 2*C; i++) acc[i] = olives[i] + acc[i-1]; + + char ans = 'N'; + for (int b = 0; b < C/N && ans == 'N'; b++) { + bool flag = true; + for (int l = b, r; l <= C; l = r) { + r = l + (C/N); + int rangeSum = acc[r] - acc[l]; // soma em [l+1 .. r] + if (rangeSum != 1) flag = false; + } + if (flag) ans = 'S'; + } + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12802/12802-14.cpp b/uva_cpp_clean/12802/12802-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..109fe07e76f41a65b43e562077ca3aae216b25c9 --- /dev/null +++ b/uva_cpp_clean/12802/12802-14.cpp @@ -0,0 +1,83 @@ +/*************************************************** + * Problem name : 12802 Gift From the Gods.cpp + * OJ : Uva + * Verdict : AC + * Date : 29.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int prime(ll n) { + int i; + if (n < 2)return 0; + for (i = 2; i <= n / 2; i++) { + if (n % i == 0)return 0; + } + return 1; + +} +int rev(ll n) { + int revv = 0; + while (n != 0) { + revv = revv * 10; + revv = revv + n % 10; + n = n / 10; + } + return revv; +} + +int main() { + ll n; + while (1) { + scanf("%lld", &n); + printf("%lld\n", 2 * n); + if (rev(n) == n) { + if (prime(rev(n))) + break; + } + } +} + diff --git a/uva_cpp_clean/12804/12804-19.cpp b/uva_cpp_clean/12804/12804-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df0ad0d10cedd38cef27946ff168c8187aee5c55 --- /dev/null +++ b/uva_cpp_clean/12804/12804-19.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ci; +typedef pair ii; +typedef vector vi; + +vector vect; + +vi dfs_num, dfs_low, S, visited; +int dfsNumberCounter, numSCC, L, acum, warn; + +void tarjanSCC(int u) { + if (dfs_num[u] == -1) acum++; + dfs_low[u] = dfs_num[u] = dfsNumberCounter++; + S.push_back(u); + visited[u] = 1; + int next[2] = {-1, -1}; + if (u < L) { + if (vect[u].first == 'A') next[0] = u + 1; + else if (vect[u].first == 'J') next[0] = vect[u].second; + else if (vect[u].first == 'C') next[0] = u + 1, next[1] = vect[u].second; + } + for (int j = 0; j < 2; j++) { + int v = next[j]; + if (v == -1) continue; + if (v == u) warn = 1; + if (dfs_num[v] == -1) + tarjanSCC(v); + if (visited[v]) + dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + if (dfs_low[u] == dfs_num[u]) { + numSCC++; + while (true) { + int v = S.back(); S.pop_back(); visited[v] = 0; + if (u == v) break; + } + } +} + +int main() { + int T; + cin >> T; + while (T--) { + cin >> L; + vect.assign(L, ci()); + for (int i = 0; i < L; i++) { + cin >> vect[i].first; + if (vect[i].first != 'A') cin >> vect[i].second, vect[i].second--; + } + dfs_num.assign(L+1, -1); dfs_low.assign(L+1, 0); visited.assign(L+1, 0); + dfsNumberCounter = numSCC = acum = warn = 0; + tarjanSCC(0); + if (dfs_num[L] == -1) printf("NEVER\n"); + else if (numSCC != acum || warn) printf("SOMETIMES\n"); + else printf("ALWAYS\n"); + } + return 0; +} diff --git a/uva_cpp_clean/12822/12822-19.cpp b/uva_cpp_clean/12822/12822-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c0f0f9610d9c908a0fa01f18dc86e62a183cbf8 --- /dev/null +++ b/uva_cpp_clean/12822/12822-19.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +int ptime(string t) { + char * ts = &t[0]; + ts[2] = ts[5] = ts[8] = 0; + return atoi(ts)*3600 + atoi(ts + 3)*60 + atoi(ts + 6); +} + +int penergy(int score) { + if (score == 0) return 6; + int res = 0; + while (score) { + switch (score%10) { + case 0: res += 6; break; + case 1: res += 2; break; + case 2: res += 5; break; + case 3: res += 5; break; + case 4: res += 4; break; + case 5: res += 5; break; + case 6: res += 6; break; + case 7: res += 3; break; + case 8: res += 7; break; + case 9: res += 6; break; + } + score /= 10; + } + return res; +} + +int main() { + string s, t, team; + int scoreH, scoreV, score, ct, energy, c = 0; + while (cin >> s >> t) { + if (s == "START") { + c++; + scoreH = scoreV = energy = 0; + ct = ptime(t); + } else { + int at = ptime(t); + energy += (penergy(scoreH) + penergy(scoreV))*(at-ct); + ct = at; + if (s == "SCORE") { + cin >> team >> score; + if (team == "home") scoreH += score; + else scoreV += score; + } else printf("Case %d: %d\n", c, energy); + } + } + return 0; +} diff --git a/uva_cpp_clean/12832/12832-19.cpp b/uva_cpp_clean/12832/12832-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dca1bf3ddfd479a5b3e7e2e91bdf54645683b49b --- /dev/null +++ b/uva_cpp_clean/12832/12832-19.cpp @@ -0,0 +1,58 @@ +#include +#include +using namespace std; + +typedef long long ll; +typedef pair ii; + +int n[10010], k[100010]; +int m; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +ii simp(ll n, ll m) { + if (n == 0) return ii(0, 1); + ll g = gcd(n, m); + return ii(n/g, m/g); +} + +ii sum(ii a, ii b) { + return simp(a.first*b.second + b.first*a.second, a.second*b.second); +} + +ii prob(int i) { + return ii(k[i], n[i]); +} + +ii proc(int i) { + if (i >= m) return ii(0,1); + else { + ii p = prob(i); + return sum(p, proc(i+1)); + } +} + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + cin >> m; + for (int i = 0; i < m; i++) { + cin >> n[i] >> k[i]; + } + ii res = proc(0); + printf("Case %d: %lld/%lld\n", c, res.first, res.second); + } + return 0; +} diff --git a/uva_cpp_clean/12840/12840-21.cpp b/uva_cpp_clean/12840/12840-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cae1a68a4988b4a7890071125bb933297bbf3adf --- /dev/null +++ b/uva_cpp_clean/12840/12840-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12840 + Name: The Archery Puzzle + Problem: https://onlinejudge.org/external/128/12840.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + for (int cse=1; cse<=T; ++cse) { + int X[73], n, s; + cin >> n >> s; + for (int i=0; i> X[i]; + } + + sort(X, X+n, greater()); + cout << "Case " << cse << ": "; + + bool done = false; + queue > > st; + st.push(make_pair(0, vector())); + while (!st.empty()) { + auto p = st.front(); st.pop(); + if (p.first == s) { + cout << "[" << p.second.size() << "]"; + for (int i: p.second) { + cout << ' ' << X[i]; + } + cout << endl; + done = true; + break; + } + + int last = p.second.size() ? p.second.back() : 0; + for (int i=last; i sss(p.second); + sss.push_back(i); + st.push(make_pair(p.first+X[i], sss)); + } + } + } + + if (!done) { + cout << "impossible" << endl; + } + } +} diff --git a/uva_cpp_clean/12843/12843-21.cpp b/uva_cpp_clean/12843/12843-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea7a1c782516e1c07301c9e4e6a732a8bfcbc58f --- /dev/null +++ b/uva_cpp_clean/12843/12843-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12843 + Name: Disputed Claims + Problem: https://onlinejudge.org/external/128/12843.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int Ri[10000000], Rk[10000000]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long n, tn, sn, j, k2, k; + int T; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + cin >> n; + tn = n<<1; + sn = sqrt(tn); + + int sz = 0; + for (int i=1; i<=sn; ++i) + if (tn % i == 0) { + j = tn / i; + k = sqrt(k2 = i*i + j*j); + if (k*k == k2) + Ri[sz] = i, + Rk[sz] = k, + ++sz; + } + cout << "Case " << cse << ": " << sz << "\n"; + for (int i=0; i +#include + +#define PI (acos(-1)) + +int main() +{ + int tc, nCase = 1; + + scanf("%d", &tc); + while( tc-- ) + { + double D, V; + scanf("%lf %lf", &D, &V); + + V *= 282.0; + double A = 8.0*PI*D; + A /= 3.0; + double B = PI*D; + B /= 3.0; + A -= B; + A = V/A; + double R = sqrt(A); + R *= 4.0; + + printf("Case %d: %.3lf\n", nCase, R); + nCase++; + } + return 0; +} diff --git a/uva_cpp_clean/12853/12853-20.cpp b/uva_cpp_clean/12853/12853-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bcb7aae2c4a097f3d390c0ea105a1eaf9bbf457d --- /dev/null +++ b/uva_cpp_clean/12853/12853-20.cpp @@ -0,0 +1,23 @@ +#include +#include + +#define PI (acos(-1)) + +int main() +{ + int tc, nCase = 1; + + scanf("%d", &tc); + while( tc-- ) + { + double D, N; + scanf("%lf %lf", &D, &N); + + double A = D/(N-1); + double C = 2.0*PI*(A+D); + + printf("Case %d: %.3lf\n", nCase, C); + nCase++; + } + return 0; +} diff --git a/uva_cpp_clean/12854/12854-21.cpp b/uva_cpp_clean/12854/12854-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9853fa873a8c634ed385b32e5c98f838bb042e63 --- /dev/null +++ b/uva_cpp_clean/12854/12854-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12854 + Name: Automated Checking Machine + Problem: https://onlinejudge.org/external/128/12854.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + bool X[10]; + while (cin>>X[0]) { + for (int i=1; i<10; ++i) + cin >> X[i]; + + for (int i=0; i<5; ++i) + if (X[i] != !X[5+i]) { + cout << "N\n"; + goto fin; + } + cout << "Y\n"; + fin:; + } +} diff --git a/uva_cpp_clean/12855/12855-21.cpp b/uva_cpp_clean/12855/12855-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77962df9a7184cd81ece0c98554597adbaa11c15 --- /dev/null +++ b/uva_cpp_clean/12855/12855-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12855 + Name: Black and white stones + Problem: https://onlinejudge.org/external/128/12855.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +long long a, b; +long long calc(string s, int cs, int cl) { + int rr = s.length()-1, ll = 0; + for (int i=0; i> a >> b >> s) { + b = a-b; + + int cb = 0; + for (int i=0; s[i]; ++i) + if (s[i] == 'B') + ++cb; + + int cs = 0; + for (int i=0; i> 1; + long long + pr1 = calc(s, cs, mid), + pr2 = calc(s, cs, mid+1); + + price = min(price, min(pr1, pr2)); + if (pr1 < pr2) + hi = mid - 1; + else + lo = mid + 1; + } + cout << price << '\n'; + } +} diff --git a/uva_cpp_clean/12856/12856-14.cpp b/uva_cpp_clean/12856/12856-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3095fd266f59c4b772629de20aca15ac7f8b4d09 --- /dev/null +++ b/uva_cpp_clean/12856/12856-14.cpp @@ -0,0 +1,103 @@ +/*************************************************** + * Problem name : 12856 Counting substhreengs.cpp + * Problem Link : https://uva.onlinejudge.org/external/128/12856.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-05 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +string str; + +ll slide(int st, int end) { + ll C[5]; + ll sum = 0, mod; + SET(C, 0); + ll cnt = 0; + for (int i = st; i <= end; i++) { + ll x = str[i] - '0'; + sum += x; + mod = sum % 3; + if (mod == 0) cnt++; + ll xx = C[mod]; + cnt += xx; + C[mod]++; + } + return cnt; +} + +int main () { + __FastIO; + + while (cin >> str) { + ll cnt = 0; + for (int i = 0; i < (int) str.size(); i++) { + if (isdigit(str[i])) { + for (int j = i; j < (int) str.size(); j++) { + if (!isdigit(str[j])) { + ll tmp = slide(i, j - 1); + cnt += tmp; + i = j; + break; + } else if (isdigit(str[j]) and j == (int) str.size() - 1) { + ll tmp = slide(i, j); + cnt += tmp; + i = j; + break; + } + } + + } + } + cout << cnt << '\n'; + str.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/12858/12858-14.cpp b/uva_cpp_clean/12858/12858-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..141c294078a3901174d5427244f4c4315841f21d --- /dev/null +++ b/uva_cpp_clean/12858/12858-14.cpp @@ -0,0 +1,109 @@ +/*************************************************** + * Problem name : 12858 Even distribution.cpp + * Problem Link : https://uva.onlinejudge.org/external/128/12858.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-05 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +int P[MAX]; +mapvis; +setst; +void bfs(queueQ) { + while (!Q.empty()) { + pii top = Q.front(); + int u = top.first; + int gc = top.second; + Q.pop(); + for (int i = 0; i < (int)adj[u].size(); i++) { + int v = adj[u][i]; + int gcc = gcd(gc, P[v]); + if (vis[make_pair(v,gcc)] == false) { + vis[make_pair(v,gcc)] = true; + //~ gc = gcd(gc, P[v]); + Q.push(make_pair(v,gcc)); + st.insert(gcc); + } + } + } +} + +int main () { + int n, m; + while (scanf("%d %d", &n, &m) == 2) { + queueQ; + for (int i = 1; i <= n; i++) { + scanf("%d", &P[i]); + Q.push(make_pair(i, P[i])); + vis[make_pair(i, P[i])] == true; + st.insert(P[i]); + } + for (int i = 1; i <= m; i++) { + int u, v; + scanf("%d %d", &u, &v); + adj[u].pb(v); + adj[v].pb(u); + } + bfs(Q); + printf("%d\n", (int)st.size()); + vis.clear(); + st.clear(); + for(int i = 0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + int n; + while (cin >> n) { + vector ara(n); + for (int i = 0; i < n; i++) { + cin >> ara[i]; + } + sort(ara.begin(), ara.end()); + int m = INT_MAX; + for (int i = 0; i < n; i++) { + int sum = 0; + bool flag = false; + for (int j = i, k = i + 1;; j += 2, k += 2) { + if (j >= n) j %= n; + if (k >= n) k %= n; + if (flag and j == i) break; + int tmp1 = abs(ara[k] - ara[j]); + int tmp2 = 24 - tmp1; + sum += min(tmp1, tmp2); + flag = true; + } + m = min(m, sum); + } + cout << m << endl; + } + return 0; +} diff --git a/uva_cpp_clean/12869/12869-19.cpp b/uva_cpp_clean/12869/12869-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c59a84dbc3e62684348faefd10256521f2e4fe5f --- /dev/null +++ b/uva_cpp_clean/12869/12869-19.cpp @@ -0,0 +1,10 @@ +#include +using namespace std; + +int main() { + long long l, h; + while (cin >> l >> h && !(l == 0 && h == 0)) { + printf("%lld\n", (h/5) - (l/5) + 1); + } + return 0; +} diff --git a/uva_cpp_clean/12873/12873-19.cpp b/uva_cpp_clean/12873/12873-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee373e27254692ba25d50ba738d01b6dddceea97 --- /dev/null +++ b/uva_cpp_clean/12873/12873-19.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +int P, S, C, M, mf, f, s, t, INF = 2000000; +int res[522][522]; +vi p; + +void augment(int v, int minEdge) { + if (v == s) { + f = minEdge; + return; + } else if (p[v] != -1) { + augment(p[v], min(minEdge, res[p[v]][v])); + res[p[v]][v] -= f; res[v][p[v]] += f; + } +} + +int main() { + int N; + cin >> N; + while (N--) { + cin >> P >> S >> C >> M; + for (int i = 0; i < P+S+2; i++) + for (int j = 0; j < P+S+2; j++) + res[i][j] = 0; + + for (int i = 0; i < M; i++) { + int c, p; + cin >> c >> p; + res[c][p+P] = 1; + } + for (int i = 1; i <= P; i++) res[0][i] = 1; + for (int i = 1; i <= S; i++) res[i+P][P+S+1] = C; + s = 0, t = P+S+1; + + mf = 0; + while (true) { + f = 0; + vi dist(P+S+2, INF); dist[s] = 0; + queue q; + q.push(s); + p.assign(P+S+2, -1); + while (!q.empty()) { + int u = q.front(); q.pop(); + if (u == t) break; + for (int v = 0; v < P+S+2; v++) { + if (res[u][v] > 0 && dist[v] == INF) { + dist[v] = dist[u] + 1, q.push(v), p[v] = u; + } + } + } + augment(t, INF); + if (f == 0) break; + mf += f; + } + printf("%d\n", mf); + } + return 0; +} diff --git a/uva_cpp_clean/12873/12873-21.cpp b/uva_cpp_clean/12873/12873-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fcbe0a7b395f8ce92fe8eac95f43c92fdd17062 --- /dev/null +++ b/uva_cpp_clean/12873/12873-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12873 + Name: The Programmers + Problem: https://onlinejudge.org/external/128/12873.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int P[501], C[23], match[501], S[501], sse; +bool PS[501][23]; +vector adjP[501], adjS[23]; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, p, s, c, m; + cin >> T; + while (T--) { + cin >> p >> s >> c >> m; + memset(match, -1, p*sizeof(int)); + for (int i=0; i> pi >> si; + adjP[pi-1].push_back(si-1); + adjS[si-1].push_back(pi-1); + PS[pi-1][si-1] = 1; + } + + int result = 0; + for (int pi=0; pi q; + q.push(pi); + while (!q.empty()) { + int pi = q.front(); q.pop(); + for (int si: adjP[pi]) + if (PS[pi][si] && !S[si]) { + S[si] = 1; + if (C[si] < c) { + for (int pp=pi, ss=si; ;pp=P[pp]) { + int s0 = match[pp]; + match[pp] = ss; + PS[pp][ss] = 0; + if (s0 == -1) break; + PS[pp][ss=s0] = 1; + } + ++C[si]; + ++result; + goto fin; + } + else { + for (int pi2: adjS[si]) + if (!PS[pi2][si]) { + P[pi2] = pi; + q.push(pi2); + } + } + } + } + fin:; + } + cout << result << "\n"; + } +} diff --git a/uva_cpp_clean/12893/12893-21.cpp b/uva_cpp_clean/12893/12893-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4c619cdba8106d0f16db6d953fd2924c8c365e4 --- /dev/null +++ b/uva_cpp_clean/12893/12893-21.cpp @@ -0,0 +1,30 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12893 + Name: Count It + Problem: https://onlinejudge.org/external/128/12893.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int solve(unsigned long long n) { + if (n<=0) return 0; + return solve(n / 2) + (n % 2); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + while (T--) { + unsigned long long n; + cin >> n; + cout << solve(n) << endl; + } +} diff --git a/uva_cpp_clean/12894/12894-19.cpp b/uva_cpp_clean/12894/12894-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f9f6e70b572ae300bf17ffbde3efc9bf2cdc3be --- /dev/null +++ b/uva_cpp_clean/12894/12894-19.cpp @@ -0,0 +1,59 @@ +// las12uvas contest PROBLEM J 2015 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector
vdd; +typedef vector vll; +typedef vector vvi; + +float pi = acos(-1); + +int main() { + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) { + bool ok = true; + float x0, y0, x1, y1, cx, cy, r; + + cin >> x0 >> y0 >> x1 >> y1 >> cx >> cy >> r; + + if((x1-x0)/(y1-y0)!= (10.0f/6.0f))ok = false; + if((x1-x0)/r != 5.0f) ok = false; + if((x1-x0)/(cx-x0) != (10.0f/4.5f)) ok = false; + if((y1-y0)/(cy-y0) != 2.0f) ok = false; + + if (ok) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12894/12894-21.cpp b/uva_cpp_clean/12894/12894-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d6ac95f2e954b0393a05b1735a9cf0cd9de87d0 --- /dev/null +++ b/uva_cpp_clean/12894/12894-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12894 + Name: Perfect Flag + Problem: https://onlinejudge.org/external/128/12894.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, x0, x1, y0, y1, cx, cy, r; + cin >> T; + while (T--) { + cin>> x0 >> y0 >> x1 >> y1 >> cx >> cy >> r; + int l = x1 - x0, + w = y1 - y0; + + if (10*w == 6*l && l == 5*r && + cy == (y0+y1)/2.0 && + cx == x0 + l * 9 / 20.0) + cout << "YES\n"; + else cout << "NO\n"; + } +} diff --git a/uva_cpp_clean/12895/12895-14.cpp b/uva_cpp_clean/12895/12895-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fec091c8caa07be01cdb6e6ec942662c493c55e8 --- /dev/null +++ b/uva_cpp_clean/12895/12895-14.cpp @@ -0,0 +1,83 @@ +/*************************************************** + * Problem name : 12895 Armstrong Number.cpp + * Problem Link : https://uva.onlinejudge.org/external/128/12895.pdf + * OJ : Uva + * Verdict : AC + * Date : 14.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +ll Pow(ll num, ll n){ + ll sum = 1; + for(int i = 1; i<=n; i++){ + sum = sum * num; + } + + return sum; +} +int main () { + ll tc,sum; + string num; + scanf("%lld", &tc); + for (ll t = 1; t <= tc; t++) { + cin >> num; + int n = num.size(); + sum = 0; + //~ cout << Pow(5,3); + for(int i = 0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12895 + Name: Armstrong Number + Problem: https://onlinejudge.org/external/128/12895.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, x; + cin >> T; + while (T-- && cin >> n) { + int d = log10(n) + 1, + s = 0; + + for (x=n; x; x/=10) + s += pow(x%10, d); + + cout << (s == n ? "Armstrong\n" : "Not Armstrong\n"); + } +} diff --git a/uva_cpp_clean/12896/12896-21.cpp b/uva_cpp_clean/12896/12896-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fffffccf796077e5ba286c6281f16f0a1af3c91a --- /dev/null +++ b/uva_cpp_clean/12896/12896-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12896 + Name: Mobile SMS + Problem: https://onlinejudge.org/external/128/12896.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int klen[10]; +char keys[10][7] = { + " ", + ".,?\"", "abc", "def", + "ghi", "jkl", "mno", + "pqrs", "tuv", "wxyz" +}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=0; i<10; ++i) + klen[i] = strlen(keys[i]); + + int X[143], T, n; + cin >> T; + while (T--) { + cin >> n; + for (int i=0; i> X[i]; + + for (int i=0, c; i> c; --c; + cout << char(keys[X[i]][ c % klen[X[i]] ]); + } + cout << '\n'; + } +} diff --git a/uva_cpp_clean/12896/12896-4.cpp b/uva_cpp_clean/12896/12896-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11a4b02f70b4336724b14c097fbbaa8d28b9e15a --- /dev/null +++ b/uva_cpp_clean/12896/12896-4.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +string tab[] = {" ", ".,?\"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + +int main() +{ + int tests; + cin >> tests; + + while (tests--) + { + int length; + cin >> length; + + int keys[length]; + + for (int i = 0; i < length; i++) + { + cin >> keys[i]; + } + + for (int i = 0; i < length; i++) + { + int count; + cin >> count; + + cout << tab[keys[i]][count - 1]; + } + + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/12898/12898-19.cpp b/uva_cpp_clean/12898/12898-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0a6046d5247441aa83e533b5981ba58be64dac9 --- /dev/null +++ b/uva_cpp_clean/12898/12898-19.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main() { + int T; + cin >> T; + for (int c = 1; c <= T; c++) { + long long A, B; + cin >> A >> B; + long long aux = (~0ll)-(~(A^B)); + int count = 0; + while (aux) { + aux >>= 1; + count++; + } + long long mask = (1ll << count) - 1; + printf("Case %d: %lld %lld\n", c, A | B | mask, A & B & (~mask)); + } + return 0; +} diff --git a/uva_cpp_clean/129/129-17.cpp b/uva_cpp_clean/129/129-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ad4a9cba7c1660796bd389a48bb0243f1b3557b --- /dev/null +++ b/uva_cpp_clean/129/129-17.cpp @@ -0,0 +1,47 @@ +/** + * Krypton Factor + */ +#include +using namespace std; +int n, L, cnt; +bool dfs(string ans) +{ + if (cnt++ == n) + { + for (int i = 0; i < ans.size(); ++i) + { + if (i && i % 64 == 0) + cout << "\n"; + else if (i && i % 4 == 0) + cout << " "; + cout << ans[i]; + } + cout << "\n" + << ans.size() << "\n"; + return true; + } + ans.append(" "); + for (char c = 'A'; c < 'A' + L; ++c) + { + ans.back() = c; + bool ok = true; + for (int len = 1; 2 * len <= ans.size(); ++len) + if (equal(ans.end() - len, ans.end(), ans.end() - 2 * len)) + { + ok = false; + break; + } + if (ok && dfs(ans)) + return true; + } + return false; +} +int main() +{ + while ((cin >> n >> L) && n) + { + string ans = ""; + cnt = 0; + dfs(ans); + } +} diff --git a/uva_cpp_clean/129/129-21.cpp b/uva_cpp_clean/129/129-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5944e143293b678ebf2ad8d030d670eefc73ea1a --- /dev/null +++ b/uva_cpp_clean/129/129-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 129 + Name: Krypton Factor + Problem: https://onlinejudge.org/external/1/129.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char str[88]; +bool check(int len) { + for (int l=len>>1; l>0; --l) { + for (int i=len-l, j=len-2*l; i vec[26]; +bool dfs(int idx=0) { + if (idx >= 80) return 0; + + for (char ch=0; ch<=maxch; ++ch) { + str[idx] = 'A' + ch; + if (check(idx+1)) { + str[idx+1] = 0; + vec[maxch].push_back(str); + if (!dfs(idx+1)) return 0; + } + } + return 1; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (maxch=0; maxch<26; ++maxch) dfs(0); + + int n, l; + while (cin >> n >> l && (n||l)) { + string s = vec[l-1][n-1]; + cout << s[0]; + for (int i=1; s[i]; ++i) { + if (i%64 == 0) + cout << '\n'; + else if (i%4 == 0) + cout << ' '; + cout << s[i]; + } + cout << '\n' << s.length() << '\n'; + } +} diff --git a/uva_cpp_clean/12908/12908-19.cpp b/uva_cpp_clean/12908/12908-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52918acbef687f3582ad13b0c246a7ef9ee2f41f --- /dev/null +++ b/uva_cpp_clean/12908/12908-19.cpp @@ -0,0 +1,13 @@ +#include +#include +using namespace std; + +int main() { + int S; + while (cin >> S && S != 0) { + int n = floor((-1+sqrt(1+8*S))/2); + while (n*(n+1)/2 - S <= 0) n++; + printf("%d %d\n", n*(n+1)/2 - S, n); + } + return 0; +} diff --git a/uva_cpp_clean/12908/12908-21.cpp b/uva_cpp_clean/12908/12908-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f832a64dcdf42b09ac8a50ca1559315743d06ce --- /dev/null +++ b/uva_cpp_clean/12908/12908-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12908 + Name: The book thief + Problem: https://onlinejudge.org/external/129/12908.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + for (int n; cin>>n && n; ) { + int m = floor((sqrt(1 + 8*n)-1) / 2) + 1; + cout << (m*(m+1)/2 - n) << ' ' << m << '\n'; + } +} diff --git a/uva_cpp_clean/12916/12916-21.cpp b/uva_cpp_clean/12916/12916-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..046657c608c27b84f8bc4a650f85cb661185f1d8 --- /dev/null +++ b/uva_cpp_clean/12916/12916-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12916 + Name: Perfect Cyclic String + Problem: https://onlinejudge.org/external/129/12916.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + string str; + while (T-- && cin >> str) { + int l = 1; + for (int i=1; str[i]; ++i) + if (str[i] != str[i%l]) + l = i+1; + if (str.length()%l) + l = str.length(); + cout << l << '\n'; + } +} diff --git a/uva_cpp_clean/12917/12917-14.cpp b/uva_cpp_clean/12917/12917-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fec63f40e4a18cfd92ccbebbd8d672c8cca15d86 --- /dev/null +++ b/uva_cpp_clean/12917/12917-14.cpp @@ -0,0 +1,63 @@ +/*************************************************** + * Problem name : 12917 Prop hunt!.cpp + * Problem Link : https://uva.onlinejudge.org/external/129/12917.pdf + * OJ : Uva + * Verdict : AC + * Date : 29.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int P, H, O; + while (scanf("%d %d %d", &P, &H, &O) == 3) { + if(P + H <= O) printf("Props win!\n"); + else printf("Hunters win!\n"); + + } + return 0; +} + diff --git a/uva_cpp_clean/12918/12918-19.cpp b/uva_cpp_clean/12918/12918-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..123be832784359c1587b8138f93e23aba224ed1c --- /dev/null +++ b/uva_cpp_clean/12918/12918-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int T; + cin >> T; + while (T--) { + long long n, m; + cin >> n >> m; + printf("%lld\n", ((m-n)*n) + n*(n-1)/2); + } + return 0; +} diff --git a/uva_cpp_clean/12930/12930-19.cpp b/uva_cpp_clean/12930/12930-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d76df0cb384045b0539f73e8fdeb985386591d29 --- /dev/null +++ b/uva_cpp_clean/12930/12930-19.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main() { + string n1, n2; + int c = 1; + while (cin >> n1 >> n2) { + int i = 0; + int comp = 0; + for (; i < n1.length() && i < n2.length(); i++) { + if (n1[i] == '.' && n1[i] != n2[i]) { comp = -1; break; } + if (n2[i] == '.' && n1[i] != n2[i]) { comp = 1; break; } + if (n1[i] < n2[i] && !comp) comp = -1; + else if (n1[i] > n2[i] && !comp) comp = 1; + } + for (; i < n1.length() && !comp; i++) { + if (n1[i] != '0') comp = 1; + } + for (; i < n2.length() && !comp; i++) { + if (n2[i] != '0') comp = -1; + } + printf("Case %d: ", c); + if (comp == -1) printf("Smaller\n"); + else if (comp == 0) printf("Same\n"); + else printf("Bigger\n"); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/12946/12946-19.cpp b/uva_cpp_clean/12946/12946-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..676f06fbc31f9c0cad399e58ff453feb7c52bd4d --- /dev/null +++ b/uva_cpp_clean/12946/12946-19.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +typedef pair ii; + +int main() { + ii gauss[30]; + gauss[0].first = 1; + gauss[0].second = 0; + gauss[1].first = -1; + gauss[1].second = 1; + for (int i = 2; i < 30; i++) { + gauss[i].first = -gauss[i-1].first - gauss[i-1].second; + gauss[i].second = gauss[i-1].first - gauss[i-1].second; + } + int p; + while (cin >> p) { + ii res = ii(0, 0); + int i = 0; + while (p) { + int b = p&1; + p >>= 1; + if (b) res.first += gauss[i].first, res.second += gauss[i].second; + i++; + } + printf("%d %d\n", res.first, res.second); + } + return 0; +} diff --git a/uva_cpp_clean/12952/12952-19.cpp b/uva_cpp_clean/12952/12952-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2b98e9a6f20f746fa14c6dfcd90b8d5dee0eb71 --- /dev/null +++ b/uva_cpp_clean/12952/12952-19.cpp @@ -0,0 +1,9 @@ +#include +#include +using namespace std; + +int main() { + int c1, c2; + while (cin >> c1 >> c2) printf("%d\n", max(c1, c2)); + return 0; +} diff --git a/uva_cpp_clean/12952/12952-21.cpp b/uva_cpp_clean/12952/12952-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29f15d0169fdd2b9556cc90589182950462886e2 --- /dev/null +++ b/uva_cpp_clean/12952/12952-21.cpp @@ -0,0 +1,22 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12952 + Name: Tri-du + Problem: https://onlinejudge.org/external/129/12952.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int x, y; + while (cin >> x >> y) + cout << max(x, y) << '\n'; +} diff --git a/uva_cpp_clean/12954/12954-19.cpp b/uva_cpp_clean/12954/12954-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c457af2cccf87b483059ec5ccbe9b6e256ad570 --- /dev/null +++ b/uva_cpp_clean/12954/12954-19.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +int main() { + long long N, B; + while (cin >> N >> B) { + long long d = ceil(sqrt(N*N - B + 1)); + if ((d & 1) != (N & 1)) d++; + long long sp1 = (N - d)/2 + 1, sp2; + sp2 = sp1; + B -= N*N - d*d + 1; + if (B >= d - 1) { + sp1 += d - 1; + B -= d - 1; + if (B >= d - 1) { + sp2 += d - 1; + B -= d - 1; + if (B >= d - 1) { + sp1 -= d - 1; + B -= d - 1; + sp2 -= B; + } else sp1 -= B; + } else sp2 += B; + } else sp1 += B; + printf("%lld %lld\n", sp2, sp1); + } + return 0; +} diff --git a/uva_cpp_clean/12959/12959-14.cpp b/uva_cpp_clean/12959/12959-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..363ad15b0ccc8da392f25144083b874a44c8b14c --- /dev/null +++ b/uva_cpp_clean/12959/12959-14.cpp @@ -0,0 +1,75 @@ +/*************************************************** + * Problem name : 12959 Strategy Game.cpp + * OJ : Uva + * Verdict : AC + * Date : 29.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 250005 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int J, R; + int ar[MAX], P[503]; + while (scanf("%d %d", &J, &R) == 2) { + for (int i = 1; i <= J * R; i++) { + scanf("%d", &ar[i]); + } + int mx = 0, ans; + for (int i = 1; i <= J; i++) { + P[i] = 0; + for (int j = i; j <= J * R; j += J) { + P[i] = P[i] + ar[j]; + } + if (mx <= P[i]) { + mx = P[i]; + ans = i; + } + } + printf("%d\n", ans); + } + return 0; +} + diff --git a/uva_cpp_clean/12960/12960-19.cpp b/uva_cpp_clean/12960/12960-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..667e4b798b1e30ebe0f69e8f5048affd3e41c9e8 --- /dev/null +++ b/uva_cpp_clean/12960/12960-19.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +using namespace std; + +typedef pair ii; + +ii dp[2001][2001]; +unordered_set sp; +string s; + +int main() { + while (cin >> s) { + sp.clear(); + int val, auxi; + cin >> val; + for (int i = 0; i < val; i++) { + cin >> auxi; + sp.insert(auxi-1); + } + for (int i = 0; i < s.length(); i++) { + dp[i][i].first = sp.count(i)? 1: 0; + dp[i][i].second = 1; + } + for (int i = 1; i < s.length(); i++) { + for (int j = 0; j + i < s.length(); j++) { + dp[j][j+i] = max(dp[j+1][j+i], dp[j][j+i-1]); + if (s[j] == s[j+i]) { + ii auxi; + auxi.first = (sp.count(j)? 1: 0) + (sp.count(j+i)? 1: 0) + dp[j+1][j+i-1].first; + auxi.second = 2 + dp[j+1][j+i-1].second; + dp[j][j+i] = max(dp[j][j+i], auxi); + } + } + } + printf("%d\n", dp[0][s.length()-1].second); + } + return 0; +} diff --git a/uva_cpp_clean/12965/12965-21.cpp b/uva_cpp_clean/12965/12965-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16032739926506bcb85ead5cef6eeb71858efeb1 --- /dev/null +++ b/uva_cpp_clean/12965/12965-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12965 + Name: Angry Bids + Problem: https://onlinejudge.org/external/129/12965.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int P, C, Pp[10143], Cp[10143]; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + while (T--) { + cin >> P >> C; P++; + for (int i=1; i> Pp[i]; + } + for (int i=0; i> Cp[i]; + } + + sort(Pp, Pp+P); + sort(Cp, Cp+C); + + int best = 0; + int besta = 1e9; + if (P == 0) { + besta = 0; + } else { + for (int i=0; i +using namespace std; + +unsigned long long GCD(unsigned long long a, unsigned long long b) { + if (b == 0) return a; + else return GCD(b, a%b); +} + +int main() { + unsigned long long v1, d1, v2, d2; + int c = 1; + while (cin >> v1 >> d1 >> v2 >> d2 && !(v1 == 0 && d1 == 0 && v2 == 0 && d2 == 0)) { + printf("Case #%d: ", c); + if ((v1*d2) > (v2*d1)) printf("You owe me a beer!\n"); + else printf("No beer for the captain.\n"); + unsigned long long num = (v1*d2) + (v2*d1), den = 2*v1*v2; + unsigned long long gcd = GCD(max(num, den), min(num, den)); + num /= gcd, den /= gcd; + printf("Avg. arrival time: %llu", num); + if (den == 1) printf("\n"); + else printf("/%llu\n", den); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/12992/12992-14.cpp b/uva_cpp_clean/12992/12992-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..316aabd8d39f110e1a13759ccdbd5b9940ba6e47 --- /dev/null +++ b/uva_cpp_clean/12992/12992-14.cpp @@ -0,0 +1,62 @@ +/*************************************************** + * Problem name : 12992 Huatuo’s Medicine.cpp + * OJ : Uva + * Verdict : AC + * Date : 29.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int tc, n; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d", &n); + printf("Case #%d: %d\n", t, 2 * n - 1); + } + return 0; +} + diff --git a/uva_cpp_clean/12992/12992-19.cpp b/uva_cpp_clean/12992/12992-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..968b39c878fe390dbaf36b30cf12817379d4fe2a --- /dev/null +++ b/uva_cpp_clean/12992/12992-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int T; + cin >> T; + for (int i = 1; i <= T; i++) { + int N; + cin >> N; + printf("Case #%d: %d\n", i, 2*(N-1)+1); + } + return 0; +} diff --git a/uva_cpp_clean/12992/12992-21.cpp b/uva_cpp_clean/12992/12992-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..029bebe76fe7d3c87fcabf97ca1a6567a79af3f8 --- /dev/null +++ b/uva_cpp_clean/12992/12992-21.cpp @@ -0,0 +1,21 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 12992 + Name: Huatuo's Medicine + Problem: https://onlinejudge.org/external/129/12992.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + int T, n; + cin >> T; + for (int cse=1; T-- && cin >> n; ++cse) + cout << "Case #" << cse << ": " << 2*n-1 << '\n'; +} diff --git a/uva_cpp_clean/12996/12996-19.cpp b/uva_cpp_clean/12996/12996-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdaff466808d1ec53dd61a3088261c9c8e175b2e --- /dev/null +++ b/uva_cpp_clean/12996/12996-19.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() { + int T; + int plate[10]; + cin >> T; + for (int c = 1; c <= T; c++) { + int N, L; + cin >> N >> L; + for (int i = 0; i < N; i++) cin >> plate[i]; + bool ok = true; + int sum = 0; + for (int i = 0; i < N; i++) { + int auxi; + cin >> auxi; + ok &= (auxi >= plate[i]); + sum += plate[i]; + } + ok &= (sum <= L); + printf("Case %d: ", c); + if (ok) printf("Yes\n"); + else printf("No\n"); + } + return 0; +} diff --git a/uva_cpp_clean/130/130-21.cpp b/uva_cpp_clean/130/130-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d86505f69bfb3c0f0db125228cac75a53aaed4e7 --- /dev/null +++ b/uva_cpp_clean/130/130-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 130 + Name: Roman Roulette + Problem: https://onlinejudge.org/external/1/130.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define Iter list::iterator + +list q; +void advance(Iter &it) { + it++; + if (it == q.end()) + it = q.begin(); +} + + +int main(){ + int n, k; + while (cin>>n>>k && (n||k)) { + if (n==1) puts("1"); + else { + q.clear(); + for (int i=0; i1) { + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector
vdd; +typedef vector vll; +typedef vector vvi; + +int main() { + int t; + while (cin >> t) { + int cont = 0; + for (int j = 0; j < 5; j++) { + int l; + cin >> l; + if(l == t) cont++; + } + printf("%d\n", cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/13012/13012-21.cpp b/uva_cpp_clean/13012/13012-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b6b2007dad32ef84fba161c3a61780f46eafb14 --- /dev/null +++ b/uva_cpp_clean/13012/13012-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13012 + Name: Identifying tea + Problem: https://onlinejudge.org/external/130/13012.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int t; + while (cin >> t) { + int r = 0; + for (int i=0, x; i<5; ++i) { + cin >> x; + if (x == t) ++r; + } + cout << r << '\n'; + } +} diff --git a/uva_cpp_clean/13012/13012-4.cpp b/uva_cpp_clean/13012/13012-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42eaa6cba8aa2f152739282cabfce672dfa857c7 --- /dev/null +++ b/uva_cpp_clean/13012/13012-4.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + +int main() +{ + int type; + + while (cin >> type) + { + int correct = 0; + + for (int i = 0; i < 5; i++) + { + int guess; + cin >> guess; + + if (guess == type) + { + correct++; + } + } + + cout << correct << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/13018/13018-19.cpp b/uva_cpp_clean/13018/13018-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c1b9ffab2ec65f37bbfa3152ea61b0c868cc35e --- /dev/null +++ b/uva_cpp_clean/13018/13018-19.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main() { + int N, M; + int counts[41]; + bool first = false; + while (cin >> N >> M) { + if (first) printf("\n"); + else first = true; + int maxi = 0; + for (int i = 0; i < 41; i++) counts[i] = 0; + for (int i = 1; i <= N; i++) { + for (int j = 1; j <= M; j++) { + counts[i+j]++; + if (counts[i+j] > maxi) maxi = counts[i+j]; + } + } + for (int i = 0; i < 41; i++) + if (counts[i] == maxi) printf("%d\n", i); + } + return 0; +} diff --git a/uva_cpp_clean/13025/13025-21.cpp b/uva_cpp_clean/13025/13025-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ff6d884ab90b19f7dd885c56f73bc84f20f90d6 --- /dev/null +++ b/uva_cpp_clean/13025/13025-21.cpp @@ -0,0 +1,18 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13025 + Name: Back to the Past + Problem: https://onlinejudge.org/external/130/13025.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + cout << "May 29, 2013 Wednesday\n"; +} diff --git a/uva_cpp_clean/13034/13034-21.cpp b/uva_cpp_clean/13034/13034-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc9466a0725c23c36c5a99e3009ddc7c9c27cfcb --- /dev/null +++ b/uva_cpp_clean/13034/13034-21.cpp @@ -0,0 +1,27 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13034 + Name: Solve Everything :-) + Problem: https://onlinejudge.org/external/130/13034.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + int T; + cin >> T; + for (int cse=1; cse<=T; ++cse) { + bool f = 1; + for (int i=0, x; i<13; ++i) { + cin >> x; + f &= x > 0; + } + cout << "Set #" << cse << ": " << (f ? "Yes\n" : "No\n"); + } +} diff --git a/uva_cpp_clean/13055/13055-21.cpp b/uva_cpp_clean/13055/13055-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0906245e73a5f5babf3642682564cb888aa798a3 --- /dev/null +++ b/uva_cpp_clean/13055/13055-21.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while (cin >> n) { + stack D; + for (int i=0; i> cm; + switch(cm[0]) { + case 'S': + cin >> name; + D.push(name); + break; + + case 'K': + if (!D.empty()) + D.pop(); + break; + + case 'T': + cout << (D.empty() ? "Not in a dream" : D.top()) << endl; + break; + } + } + } +} diff --git a/uva_cpp_clean/13059/13059-21.cpp b/uva_cpp_clean/13059/13059-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..253bdb1aef6063e226f2eafabcfb738addd6daf9 --- /dev/null +++ b/uva_cpp_clean/13059/13059-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13059 + Name: Tennis Championship + Problem: https://onlinejudge.org/external/130/13059.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +typedef unsigned long long Int; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline bool readUInt(Int &r) { + char ch; + r = 0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return 0; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return 1; +} + + +int main() { + for (Int n; readUInt(n); ) { + Int cnt = n>>1; + for (; n>1; cnt += n>>1) + n = (n+1) >> 1; + cout << cnt << endl; + } +} diff --git a/uva_cpp_clean/13072/13072-19.cpp b/uva_cpp_clean/13072/13072-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9f1e13b69b7b1453acd6a9e6d4d273522d3d5ae --- /dev/null +++ b/uva_cpp_clean/13072/13072-19.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +using namespace std; + +int grid[1004][1004]; + +int main() { + string s; + while (cin >> s) { + int n = s.length(); + for (int i = 0; i < n; i++) grid[0][i] = 1; + for (int i = 0; i < n - 1; i++) grid[1][i+1] = s[i] == s[i+1]? 2: 1; + for (int i = 2; i < n; i++) { + for (int j = i; j < n; j++) { + if (s[j] == s[j-i]) grid[i][j] = 2 + grid[i-2][j-1]; + else { + grid[i][j] = max(grid[i-1][j-1], grid[i-1][j]); + } + } + } + int l = n-1, p = n-1; + stack st; + while (l >= 0) { + if (s[p] == s[p-l]) { + printf("%c", s[p]); + if (l != 0) st.push(s[p]); + l -= 2, p--; + } else { + if (grid[l-1][p-1] > grid[l-1][p]) p--; + l--; + } + } + while (!st.empty()) printf("%c", st.top()), st.pop(); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/13075/13075-19.cpp b/uva_cpp_clean/13075/13075-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0624bf5676edcc8aa1790c910cb42e2391ff0c6 --- /dev/null +++ b/uva_cpp_clean/13075/13075-19.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +using namespace std; + +set dp[15][15]; + +void proc(int chocolate, int vanilla) { + if (dp[chocolate][vanilla].empty()) { + if (chocolate == 0) dp[chocolate][vanilla].insert(string(vanilla, 'V')); + else if (vanilla == 0) dp[chocolate][vanilla].insert(string(chocolate, 'C')); + else { + proc(chocolate-1, vanilla); proc(chocolate, vanilla-1); + for (auto it = dp[chocolate-1][vanilla].begin(); it != dp[chocolate-1][vanilla].end(); ++it) { + dp[chocolate][vanilla].insert("C"+(*it)); + } + for (auto it = dp[chocolate][vanilla-1].begin(); it != dp[chocolate][vanilla-1].end(); ++it) { + dp[chocolate][vanilla].insert("V"+(*it)); + } + } + } +} + +int main() { + int N; + cin >> N; + while (N--) { + int chocolate, vanilla; + cin >> chocolate >> vanilla; + proc(chocolate, vanilla); + bool first = false; + for (auto it = dp[chocolate][vanilla].begin(); it != dp[chocolate][vanilla].end(); ++it) { + if (first) printf(" "); + else first = true; + printf("%s", (*it).c_str()); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/13093/13093-14.cpp b/uva_cpp_clean/13093/13093-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e609227725713c430a495b0262be51fd726e7b12 --- /dev/null +++ b/uva_cpp_clean/13093/13093-14.cpp @@ -0,0 +1,77 @@ +/*************************************************** + * Problem name : 13093 Acronyms.cpp + * Problem Link : https://uva.onlinejudge.org/external/130/13093.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-10-31 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + string a, b,tmp; + while(getline(cin, a)){ + getline(cin, b); + mapmp; + string str, str1; + stringstream ss; + ss << a; + while(ss>>tmp){ + str.push_back(tmp[0]); + } + ss.clear(); + ss << b; + while(ss>>tmp){ + str1.push_back(tmp[0]); + } + if(str == str1) printf("yes\n"); + else printf("no\n"); + } + return 0; +} + diff --git a/uva_cpp_clean/13099/13099-19.cpp b/uva_cpp_clean/13099/13099-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e5ccf6a0c42a6aa9ca753a59b8432dd47655a60 --- /dev/null +++ b/uva_cpp_clean/13099/13099-19.cpp @@ -0,0 +1,10 @@ +#include +using namespace std; + +int main() { + double xl, yl, xr, yr; + while (cin >> xl >> yl >> xr >> yr) { + printf("%.6lf\n", ((xr-xl)*(xr-xl) + (yr-yl)*(yr-yl))/6); + } + return 0; +} diff --git a/uva_cpp_clean/131/131-9.cpp b/uva_cpp_clean/131/131-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8076afcce2ece7c3207acb01a595bff4ffb5bee3 --- /dev/null +++ b/uva_cpp_clean/131/131-9.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +const int N_CARDS = 5, LEN = 3; + +int ans; +char card[LEN]; +vector < pair > hand (N_CARDS), deck(N_CARDS), bestHand(N_CARDS); + +void print() { + printf("Best hand: "); + if (ans == 0) puts("highest-card"); + if (ans == 1) puts("one-pair"); + if (ans == 2) puts("two-pairs"); + if (ans == 3) puts("three-of-a-kind"); + if (ans == 4) puts("straight"); + if (ans == 5) puts("flush"); + if (ans == 6) puts("full-house"); + if (ans == 7) puts("four-of-a-kind"); + if (ans == 8) puts("straight-flush"); +} + +bool straightFlush() { + int sc = bestHand[0].second; + for (int i = 1; i < N_CARDS; i++) + if (bestHand[i].second != sc) return false; + vector tmp(N_CARDS); + for (int i = 0; i < N_CARDS; i++) tmp[i] = bestHand[i].first; + sort(tmp.begin(), tmp.end()); + if (tmp == vector {1, 10, 11, 12, 13}) return true; + for (int i = 1; i < N_CARDS; i++) + if (tmp[i] - tmp[i - 1] != 1) return false; + return true; +} + +bool fourOfaKind() { + int fs = bestHand[0].first, ct = 1; + for (int i = 1; i < N_CARDS; i++) + if (bestHand[i].first == fs) ct++; + if (ct == 4) return true; + fs = bestHand[1].first, ct = 0; + for (int i = 0; i < N_CARDS; i++) + if (bestHand[i].first == fs) ct++; + return (ct == 4); +} + +bool fullHouse() { + int val1 = -1, val2 = -1; + val1 = bestHand[0].first; + for (int i = 1; i < N_CARDS; i++) + if (bestHand[i].first != val1) + val2 = bestHand[i].first; + if (val2 == -1) return false; + int ct1 = 0, ct2 = 0; + for (int i = 0; i < N_CARDS; i++) { + if (bestHand[i].first == val1) ct1++; + if (bestHand[i].first == val2) ct2++; + } + return ((ct1 == 3 and ct2 == 2) or (ct1 == 2 and ct2 == 3)); +} + +bool flush() { + int sc = bestHand[0].second; + for (int i = 1; i < N_CARDS; i++) + if (bestHand[i].second != sc) return false; + return (not straightFlush()); +} + +bool straight() { + bool ok = true; + vector tmp(N_CARDS); + for (int i = 0; i < N_CARDS; i++) tmp[i] = bestHand[i].first; + sort(tmp.begin(), tmp.end()); + for (int i = 1; i < N_CARDS; i++) + if (tmp[i] - tmp[i - 1] != 1) ok = false; + if (tmp == vector {1, 10, 11, 12, 13}) ok = true; + return (ok and not straightFlush()); +} + +bool threeOfAKind() { + for (int i = 0, ct; i < N_CARDS; i++) { + ct = 0; + for (int j = 0; j < N_CARDS; j++) + if (bestHand[i].first == bestHand[j].first) ct++; + if (ct == 3 and not fullHouse()) return true; + } + return false; +} + +bool twoPairs() { + int val1 = -1, val2 = -1, val3 = -1; + val1 = bestHand[0].first; + for (int i = 1; i < N_CARDS; i++) + if (bestHand[i].first != val1) + val2 = bestHand[i].first; + for (int i = 2; i < N_CARDS; i++) + if (bestHand[i].first != val1 and bestHand[i].first != val2) + val3 = bestHand[i].first; + vector ct(3, 0); + for (int i = 0; i < N_CARDS; i++) + if (bestHand[i].first == val1) ct[0]++; + else if (bestHand[i].first == val2) ct[1]++; + else if (bestHand[i].first == val3) ct[2]++; + sort(ct.begin(), ct.end()); + return ct == vector {1, 2, 2}; +} + +bool onePair() { + vector tmp(N_CARDS); + for (int i = 0; i < N_CARDS; i++) tmp[i] = bestHand[i].first; + return (set (tmp.begin(), tmp.end())).size() == 4; +} + +bool highestCard() { + return true; +} + +void getBestHand(int mask) { + for (int i = 0, id = 0; i < N_CARDS; i++) { + if ((mask >> i) & 1) bestHand[i] = hand[i]; + else bestHand[i] = deck[id++]; + } +} + +void solve() { + for (int mask = 0; mask < (1 << N_CARDS); mask++) { + do { + getBestHand(mask); + if (highestCard()) ans = max(ans, 0); + if (onePair()) ans = max(ans, 1); + if (twoPairs()) ans = max(ans, 2); + if (threeOfAKind()) ans = max(ans, 3); + if (straight()) ans = max(ans, 4); + if (flush()) ans = max(ans, 5); + if (fullHouse()) ans = max(ans, 6); + if (fourOfaKind()) ans = max(ans, 7); + if (straightFlush()) ans = max(ans, 8); + } while (next_permutation(hand.begin(), hand.end())); + } +} + +void init() { + ans = -1; +} + +pair makeCard() { + if (isdigit(card[0])) return {card[0] - '0', card[1]}; + if (card[0] == 'A') return {1, card[1]}; + if (card[0] == 'T') return {10, card[1]}; + if (card[0] == 'J') return {11, card[1]}; + if (card[0] == 'Q') return {12, card[1]}; + if (card[0] == 'K') return {13, card[1]}; +} + +bool read() { + for (int i = 0; i < N_CARDS; i++) { + if (scanf("%s", card) != 1) return false; + if (i == 0) printf("Hand: "); + hand[i] = makeCard(); + printf("%s ", card); + } + printf("Deck: "); + for (int i = 0; i < N_CARDS; i++) { + scanf("%s", card); + deck[i] = makeCard(); + printf("%s ", card); + } + return true; +} + +int main() { + while(read()) { + init(); + solve(); + print(); + } + return (0); +} diff --git a/uva_cpp_clean/13106/13106-21.cpp b/uva_cpp_clean/13106/13106-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..053231b71b5b1f74c6f5f5dc2f2b9f4ac5f378d4 --- /dev/null +++ b/uva_cpp_clean/13106/13106-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13106 + Name: Vogons + Problem: https://onlinejudge.org/external/131/13106.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int G[400][400], G2[400]; +int dp(int g[], int n) { + if (n <= 1) return g[0]; + int d2 = g[0], d1 = max(d2, g[1]); + for (int i=2; i +using namespace std; + +int main() { + string s; + int code[30]; + while (cin >> s) { + int c = 1; + for (int i = 0; i < 30; i++) code[i] = 0; + for (int i = 0; i < s.length(); i++) { + if (code[s[i] - 'a'] == 0) { + code[s[i] - 'a'] = c, c++; + if (code[s[i] - 'a'] == 2) code[s[i] - 'a'] = 5; + else if (code[s[i] - 'a'] == 5) code[s[i] - 'a'] = 2; + else if (code[s[i] - 'a'] == 6) code[s[i] - 'a'] = 9; + else if (code[s[i] - 'a'] == 9) code[s[i] - 'a'] = 6; + else if (code[s[i] - 'a'] == 12) code[s[i] - 'a'] = 15; + else if (code[s[i] - 'a'] == 15) code[s[i] - 'a'] = 12; + else if (code[s[i] - 'a'] == 16) code[s[i] - 'a'] = 19; + else if (code[s[i] - 'a'] == 19) code[s[i] - 'a'] = 16; + else if (code[s[i] - 'a'] == 22) code[s[i] - 'a'] = 55; + else if (code[s[i] - 'a'] == 25) code[s[i] - 'a'] = 52; + else if (code[s[i] - 'a'] == 26) code[s[i] - 'a'] = 59; + else if (code[s[i] - 'a'] == 29) code[s[i] - 'a'] = 56; + else if (code[s[i] - 'a'] >= 20) code[s[i] - 'a'] += 30; + } + printf("%d", code[s[i] - 'a']); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/13109/13109-19.cpp b/uva_cpp_clean/13109/13109-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cbd4c1491ae3bb4d9f2208511c05e1de2f4c5f4 --- /dev/null +++ b/uva_cpp_clean/13109/13109-19.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; + +int main() { + int T; + cin >> T; + while (T--) { + int M, W; + cin >> M >> W; + vector v; + for (int i = 0; i < M; i++){ + int w; + cin >> w; + v.push_back(w); + } + sort(v.begin(), v.end()); + int w = 0, cont = 0; + for (int i = 0; i < M; i++) { + if (v[i] + w <= W) { + cont++; + w += v[i]; + } else break; + } + printf("%d\n", cont); + } + return 0; +} diff --git a/uva_cpp_clean/13109/13109-21.cpp b/uva_cpp_clean/13109/13109-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..caad38ce25d12fda83f2f0a3e8e3740568497ba8 --- /dev/null +++ b/uva_cpp_clean/13109/13109-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13109 + Name: Elephants + Problem: https://onlinejudge.org/external/131/13109.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + int T, n, m, w, c; + scanf("%d", &T); + while (T--) { + priority_queue, greater > q; + scanf("%d%d", &n, &m); + + for (int i=0; i= 0) c++; + else break; + } + + printf("%d\n", c); + } +} diff --git a/uva_cpp_clean/13121/13121-21.cpp b/uva_cpp_clean/13121/13121-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..642dd2d732d2953f91e51783c553131326795edd --- /dev/null +++ b/uva_cpp_clean/13121/13121-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13121 + Name: Eclipsing Gianik Star + Problem: https://onlinejudge.org/external/131/13121.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 1073741824 +int abs(int x) { return x<0 ? -x : x; } + +struct Triple { + int d, x, y; + Triple(int q, int w, int e):d(q),x(w),y(e){} +}; + +Triple egcd(int a, int b) { + if (!b) return Triple(a, 1, 0); + Triple q = egcd(b, a%b); + return Triple(q.d, q.y, q.x - a/b * q.y); +} + + +int a[400], b[400]; +int nexte(int i, int j) { + if (b[i] == b[j]) return a[i]==a[j] ? 0 : INF; + if (b[i] < b[j]) swap(i, j); + + int bij = b[i]-b[j], + aji = a[j]-a[i]; + if (aji < 0) aji += 360*ceil(-aji / 360.0); + + + Triple t = egcd(bij, 360); + if (aji % t.d) return INF; + int x = (aji/t.d * t.x) % 360; + if (x < 0) x += 360; + int mn = INF; + for (int i=0; i=0 && tm +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vi; +typedef vector vii; + +vector AdjList; + +int main() { + int N, M, B, P; + while (cin >> N >> M >> B >> P) { + AdjList.assign(N + 2, vii()); + for (int i = 0; i < M; i++) { + int U, V, T; + cin >> U >> V >> T; + AdjList[U].push_back(make_pair(V, T)); + AdjList[V].push_back(make_pair(U, T)); + } + vi banks, polices; + for (int i = 0; i < B; i++) { + int b; + cin >> b; + banks.push_back(b); + } + for (int i = 0; i < P; i++) { + int p; + cin >> p; + AdjList[N].push_back(make_pair(p, 0)); + AdjList[p].push_back(make_pair(N, 0)); + polices.push_back(p); + } + vi dist(N+2, 1000000000); dist[N] = 0; + priority_queue pq; + pq.push(make_pair(0, N)); + while (!pq.empty()) { + ii front = pq.top(); pq.pop(); + int d = -front.first, u = front.second; + if (d > dist[u]) continue; + for (int j = 0; j < AdjList[u].size(); j++) { + ii v = AdjList[u][j]; + if (dist[u] + v.second < dist[v.first]) { + dist[v.first] = dist[u] + v.second; + pq.push(make_pair(-dist[v.first], v.first)); + } + } + } + int maxi = 0; + for (int i = 0; i < B; i++) { + if (dist[banks[i]] > maxi) maxi = dist[banks[i]]; + } + int E = maxi, S = 0; + sort(banks.begin(), banks.end()); + queue q; + for (int i = 0; i < B; i++) { + if (dist[banks[i]] == E) q.push(banks[i]), S++; + } + printf("%d ", S); + if (E == 1000000000) { + printf("*\n"); + } else printf("%d\n", E); + bool first = false; + while (!q.empty()) { + if (first) printf(" "); + else first = true; + printf("%d", q.front()); + q.pop(); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/13130/13130-21.cpp b/uva_cpp_clean/13130/13130-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41231ffb5392f597a1a74db8b5b9e4d22b148c0e --- /dev/null +++ b/uva_cpp_clean/13130/13130-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13130 + Name: Cacho + Problem: https://onlinejudge.org/external/131/13130.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int v[5]; +bool check() { + for (int i=1; i<5; ++i) + if (v[i] != v[i-1]+1) + return 0; + return 1; +} + +int main(){ + int T; + scanf("%d", &T); + while (T--) { + scanf("%d%d%d%d%d", v,v+1,v+2,v+3,v+4); + sort(v, v+5); + puts(check() ? "Y" : "N"); + } +} diff --git a/uva_cpp_clean/13131/13131-19.cpp b/uva_cpp_clean/13131/13131-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f5f47c7d63e06ad27c9f51a371541103dd2a916 --- /dev/null +++ b/uva_cpp_clean/13131/13131-19.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +void sieve(ll upperbound) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +void primeFactors(ll n, vii & factors) { + int c; + factors.clear(); + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +int main() { + sieve(50000); + int T; + cin >> T; + int pot[20]; + vii v; + while (T--) { + int N, K; + cin >> N >> K; + primeFactors(N, v); + for (int i = 0; i < v.size(); i++) pot[i] = 0; + int ret = 0; + while (true) { + int val = 1; + for (int i = 0; i < v.size(); i++) { + val *= pow(v[i].first, pot[i]); + } + if (val % K != 0) ret += val; + if (val == N) break; + int j = 0; + bool ok = true; + do { + ok = true; + pot[j]++; + if (pot[j] > v[j].second) pot[j] = 0, ok = false; + j++; + } while (!ok && j < v.size()); + } + printf("%d\n", ret); + } + return 0; +} diff --git a/uva_cpp_clean/13140/13140-19.cpp b/uva_cpp_clean/13140/13140-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d28bb112aa31f186c24187cb02908971cf1297c --- /dev/null +++ b/uva_cpp_clean/13140/13140-19.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; + +int main() { + long long val = 9; + int cont = 0; + while (cont != 7) { + val++; + long long sq = val*val; + int sum = 0; + while (sq) { + sum += sq%10; + sq /= 10; + } + int auxi = floor(sqrt(sum)); + auxi *= auxi; + if (auxi == sum) cont++; + else cont = 0; + } + for (long long i = val - 6; i <= val; i++) printf("%lld %lld\n", i, i*i); + return 0; +} diff --git a/uva_cpp_clean/13141/13141-19.cpp b/uva_cpp_clean/13141/13141-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11687f37a06792af99fe8ea79e0bb98289b3811f --- /dev/null +++ b/uva_cpp_clean/13141/13141-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + long long fib[86]; + fib[1] = fib[2] = 1; + for (int i = 3; i < 86; i++) fib[i] = fib[i-1] + fib[i-2]; + int n; + while (cin >> n && n != 0) { + printf("%lld\n", fib[n]); + } + return 0; +} diff --git a/uva_cpp_clean/13142/13142-19.cpp b/uva_cpp_clean/13142/13142-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b72b21bccf0c005c9636888184e043da4a26299 --- /dev/null +++ b/uva_cpp_clean/13142/13142-19.cpp @@ -0,0 +1,17 @@ +#include +#include +using namespace std; + +int main() { + int N; + cin >> N; + while (N--) { + double T, S, D; + cin >> T >> S >> D; + T *= 54; D *= 625; + double val = (D/T); + if (int(floor(val)) > 0) printf("Remove %d tons\n", int(floor(val))); + else printf("Add %d tons\n", int(floor(fabs(val)))); + } + return 0; +} diff --git a/uva_cpp_clean/13142/13142-21.cpp b/uva_cpp_clean/13142/13142-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..baa7a6e9f72d0a8f00548999404fe7403b7892b6 --- /dev/null +++ b/uva_cpp_clean/13142/13142-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13142 + Name: Destroy the Moon to Save the Earth + Problem: https://onlinejudge.org/external/131/13142.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int C; + cin >> C; + while (C--) { + double C, T, S, D; + cin >> T >> S >> D; + double t = T * 24 * 60 * 60; + double d = D * 1e6; + int res = d / t; + if (res > 0) { + cout << "Remove " << res << " tons" << endl; + } else { + cout << "Add " << (-res) << " tons" << endl; + } + } +} diff --git a/uva_cpp_clean/13146/13146-19.cpp b/uva_cpp_clean/13146/13146-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f43bed1db2fbd3892daa7bcacc260d95ad65b354 --- /dev/null +++ b/uva_cpp_clean/13146/13146-19.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +string s1, s2; +int dp[102][102]; + +int main() { + int N; + cin >> N; + getline(cin, s1); + while (N--) { + getline(cin, s1); + getline(cin, s2); + for (int i = s1.length(); i >= 0; i--) dp[i][s2.length()] = s1.length() - i; + for (int j = s2.length(); j >= 0; j--) dp[s1.length()][j] = s2.length() - j; + for (int i = s1.length()-1; i >= 0; i--) { + for (int j = s2.length()-1; j >= 0; j--) { + dp[i][j] = min(dp[i+1][j+1] + (s1[i] == s2[j]? 0: 1), min(dp[i+1][j] + 1, dp[i][j+1] + 1)); + } + } + printf("%d\n", dp[0][0]); + } + return 0; +} diff --git a/uva_cpp_clean/13148/13148-14.cpp b/uva_cpp_clean/13148/13148-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..106fb82d080f2b8de04ab1ec67add0c0f91db576 --- /dev/null +++ b/uva_cpp_clean/13148/13148-14.cpp @@ -0,0 +1,74 @@ +/*************************************************** + * Problem name : 13148 - A Giveaway.cpp + * Problem Link : https://uva.onlinejudge.org/external/131/13148.pdf + * OJ : Uva + * Verdict : AC + * Date : 15.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +ll ar[21] = {1, 64, 729, 4096, 15625, 46656, 117649, 262144, 531441, 1000000, 1771561, 2985984, 4826809, 7529536, 11390625, 16777216, 24137569, 34012224, 47045881, 64000000, 85766121}; +mapmp; +void PreCal() { + for (int i = 0; i < 21; i++) { + mp[ar[i]] = true; + } +} +int main () { + PreCal(); + ll num; + while(scanf("%lld", &num) == 1 && num != 0){ + if(mp[num] == true){ + printf("Special\n"); + } + else{ + printf("Ordinary\n"); + } + } + return 0; +} + diff --git a/uva_cpp_clean/13151/13151-4.cpp b/uva_cpp_clean/13151/13151-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93bf08f133dda1af569582934ab47d192f94f20b --- /dev/null +++ b/uva_cpp_clean/13151/13151-4.cpp @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +int main() +{ + int n, m; + string s; + + while (true) + { + cin >> s >> m; + + if (s == "0" && m == 0) + break; + + if (s[0] == '0' && s[1] == 'x') + sscanf(s.c_str(), "%X", &n); + else if (s[0] == '0') + sscanf(s.c_str(), "%o", &n); + else + sscanf(s.c_str(), "%d", &n); + + int ret = 0, val; + for (int i = 0; i < m; i++) + { + string cmd; + cin >> cmd >> val; + + if (!strcmp(cmd.c_str(), "i")) + { + if (val != n) + n = val; + else + ret++; + } + else if (!strcmp(cmd.c_str(), "i++")) + { + if (val != n) + n = val; + else + ret++; + + n++; + } + else if (!strcmp(cmd.c_str(), "i--")) + { + if (val != n) + n = val; + else + ret++; + + n--; + } + else if (!strcmp(cmd.c_str(), "++i")) + { + ++n; + + if (val != n) + n = val; + else + ret++; + } + else if (!strcmp(cmd.c_str(), "--i")) + { + --n; + + if (val != n) + n = val; + else + ret++; + } + } + + cout << ret << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/13171/13171-19.cpp b/uva_cpp_clean/13171/13171-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..022fce9c0c7b3c0794f4d2cb03f02a9e14c7decb --- /dev/null +++ b/uva_cpp_clean/13171/13171-19.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main(){ + int mag, yel, cyan; + int T; + cin >> T; + while (T--) { + cin >> mag >> yel >> cyan; + string s; + cin >> s; + bool ok = true; + for (int i = 0; i < s.length() && ok; i++) { + if (s[i] == 'M') mag --; + else if (s[i] == 'Y') yel --; + else if (s[i] == 'C') cyan --; + else if (s[i] == 'R') mag --, yel--; + else if (s[i] == 'B') mag--, yel--, cyan--; + else if (s[i] == 'G') yel--, cyan--; + else if (s[i] == 'V') cyan--, mag--; + else if (s[i] == 'W'); + ok = (mag >= 0 && yel >= 0 && cyan >= 0); + } + if (ok){ + printf("YES %d %d %d\n", mag, yel, cyan); + } else printf("NO\n"); + } + return 0; +} diff --git a/uva_cpp_clean/13171/main.cpp b/uva_cpp_clean/13171/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10d320b37ec5a93746ab7ed5ad960f2a870ee3f8 --- /dev/null +++ b/uva_cpp_clean/13171/main.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +int main(){ +int n; +cin >> n; +for(int i = 0; i < n; i++){ + int m, y, c; + string pixels; + cin >> m >> y >> c; + cin >> pixels; + for(int j = 0; j < pixels.size(); j++){ + char p = pixels[j]; + if(p == 'M') m--; + else if(p == 'Y') y--; + else if(p == 'C') c--; + else if(p == 'R'){ + m--; + y--; + } + else if(p == 'B'){ + m--; + y--; + c--; + } + else if(p == 'G'){ + y--; + c--; + }else if(p == 'V'){ + c--; + m--; + } + } + if(c >= 0 && m >= 0 && y>= 0){ + cout << "YES " << m << ' ' << y << ' ' << c << endl; + }else cout << "NO"<< endl; +} + + +return 0; +} diff --git a/uva_cpp_clean/13177/13177-19.cpp b/uva_cpp_clean/13177/13177-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d88c78555a4a76dc397aeda190d53571f2ddb041 --- /dev/null +++ b/uva_cpp_clean/13177/13177-19.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; + +int main() { + int p, n; + while (cin >> p >> n) { + priority_queue pq; + vector v(n, 0), origin(n, 0); + for (int i = 0; i < n; i++) { + int val; + cin >> val; + pq.push(ii(val, i)); + origin[i] = val; + v[i]++; + p--; + } + while (!pq.empty() && p > 0) { + ii val = pq.top(); + if (val.first <= 1) break; + pq.pop(); + v[val.second]++; + pq.push(ii(origin[val.second] / v[val.second] + (origin[val.second] % v[val.second] != 0? 1: 0), val.second)); + p--; + } + if (pq.empty()) printf("1\n"); + else printf("%d\n", pq.top().first); + } + return 0; +} diff --git a/uva_cpp_clean/13178/13178-19.cpp b/uva_cpp_clean/13178/13178-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..187ed74d6413160d541af2116504a447910f0aea --- /dev/null +++ b/uva_cpp_clean/13178/13178-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int T, N; + cin >> T; + while (T--) { + cin >> N; + if (N % 3 != 1) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} diff --git a/uva_cpp_clean/13180/13180-4.cpp b/uva_cpp_clean/13180/13180-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83e9ef3cb7ed983633d3c33afc12291d97005c6e --- /dev/null +++ b/uva_cpp_clean/13180/13180-4.cpp @@ -0,0 +1,84 @@ +#include +#include + +bool input(std::map &pearls) +{ + pearls.clear(); + int pearl; + + while (std::cin >> pearl && pearl != 0) + { + pearls[pearl]++; + } + + return pearls.size() != 0; +} + +bool can_form(std::map &pearls) +{ + auto largest = --pearls.end(); + + if (largest->second != 1) + { + return false; + } + + for (auto &keyval : pearls) + { + if (keyval == *largest) + { + return true; + } + + if (keyval.second != 2) + { + return false; + } + } + + return true; +} + +void display(std::map &pearls) +{ + for (auto &keyval : pearls) + { + std::cout << keyval.first; + + if (pearls.size() != 1) + { + std::cout << " "; + } + } + + for (auto it = ++pearls.rbegin(); it != pearls.rend(); it++) + { + std::cout << it->first; + + if (it != --pearls.rend()) + { + std::cout << " "; + } + } + + std::cout << std::endl; +} + +int main() +{ + std::map pearls; + + while (input(pearls)) + { + if (can_form(pearls)) + { + display(pearls); + } + else + { + std::cout << "NO" << std::endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/13185/13185-19.cpp b/uva_cpp_clean/13185/13185-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c64e63b820d50e6d611088f7c14393918bbb43fa --- /dev/null +++ b/uva_cpp_clean/13185/13185-19.cpp @@ -0,0 +1,68 @@ +// You can also see UVA13194.cpp +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +void sieve(ll upperbound) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +void primeFactors(ll n, vii & factors) { + int c; + factors.clear(); + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +vii v; +long long dp[20]; + +long long proc(int i) { + if (i >= v.size()) return 1; + if (dp[i] == -1) { + long long ret = 0; + for (int j = 0; j <= v[i].second; j++) + ret += pow(v[i].first, j)*proc(i+1); + dp[i] = ret; + } + return dp[i]; +} + +int main() { + sieve(100); + int T; + cin >> T; + int pot[20]; + while (T--) { + long long N; + cin >> N; + primeFactors(N, v); + for (int i = 0; i < v.size(); i++) pot[i] = 0, dp[i] = -1; + long long ret = proc(0) - N; + if (ret == N) printf("perfect\n"); + else if (ret > N) printf("abundant\n"); + else printf("deficient\n"); + } + return 0; +} diff --git a/uva_cpp_clean/13187/13187-19.cpp b/uva_cpp_clean/13187/13187-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f66dbc54bd9544e9d4e71a30b68ccba9f4736f9 --- /dev/null +++ b/uva_cpp_clean/13187/13187-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int t; + scanf("%d", &t); + while (t--) { + long long S; + scanf("%lld", &S); + printf("%lld\n", ((S + 1)*(S + 1) - 1)); + } + return 0; +} diff --git a/uva_cpp_clean/13190/13190-15.cpp b/uva_cpp_clean/13190/13190-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec3962c46535028c25f681c3a598c3b4df296f22 --- /dev/null +++ b/uva_cpp_clean/13190/13190-15.cpp @@ -0,0 +1,64 @@ +/* + 優先佇列 + 時間複雜度: O(NlogN) +*/ +#include +#include +#include + +using namespace std; + +class Medicine { + private: + static int count; + string name; + int order, freqency, now; + + public: + Medicine(const string& name, const int& freqency, const int& now) { + this->order = Medicine::count++; + this->freqency = freqency; + this->name = name; + this->now = now; + } + + bool operator>(const Medicine& other) const { + if (this->now == other.now) + return this->order > other.order; + return this->now > other.now; + } + + Medicine take() const { + cout << now << " " << name << "\n"; + Medicine&& temp = Medicine(*this); + temp.now += freqency; + return temp; + } +}; + +int Medicine::count = 0; + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + + int n_testcase; + cin >> n_testcase; + + while (n_testcase--) { + priority_queue, greater> schedule; + string name; + int n_medicine, n_times, frequency; + cin >> n_medicine >> n_times; + + while (n_medicine--) { + cin >> name >> frequency; + schedule.emplace(name, frequency, frequency); + } + + while (n_times--) { + schedule.emplace(schedule.top().take()); + schedule.pop(); + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/13191/13191-9.cpp b/uva_cpp_clean/13191/13191-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e1aa088c2df6f91512ccd2f685847e456988925 --- /dev/null +++ b/uva_cpp_clean/13191/13191-9.cpp @@ -0,0 +1,138 @@ +/** + * > Author : TISparta + * > Date : 22-03-18 + * > Tags : Number theory, Miller-Robin algorithm, Rho algorithm + * > Difficulty : 5 / 10 + */ + +#include + +using namespace std; + +typedef unsigned long long ll; + +const int L1 = 3e6; +const long long L2 = 9e12; + +int tc; +ll n; +bool is_prime[L1], first; +vector prime; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(ll i = 2; i < L1; i++) + if( is_prime[i] ){ + for(ll j = i * i; j < L1; j += i) + is_prime[j] = false; + prime.push_back(i); + } +} + +ll mulMod(ll a, ll b, ll c){ + ll ret = 0, pib = a % c; + while(b > 0){ + if( b & 1 ) ret += pib; + pib <<= 1; + if( ret >= c ) ret -= c; + if( pib >= c ) pib -= c; + b >>= 1; + } + return ret; +} + +ll powMod(ll a, ll b, ll c){ + ll ret = 1, pib = a; + while(b > 0){ + if( b & 1 ) ret = mulMod(ret, pib, c); + pib = mulMod(pib, pib, c); + b >>= 1; + } + return ret; +} + +bool MillerRobin(ll num, int it = 30){ + if( num < 2 ) return false; + if( num < 4 ) return true; + if( not ( num & 1 ) ) return false; + ll s = num - 1; + while( not ( s & 1 ) ) s >>= 1; + while(it--){ + ll rd = rand() % ( num - 1 ) + 1, pib = s; + ll ret = powMod(rd, pib, num); + if( ret == -1 or ret == 1 ) continue; + while( pib != num - 1 and ret != 1 and ret != num - 1 ){ + ret = mulMod(ret, ret, num); + pib <<= 1; + } + if( ret != num - 1 ) return false; + } + return true; +} + +ll gcd(ll a, ll b){ + return b == 0 ? a : gcd(b, a % b); +} + +ll PollardRho(ll num){ + int i = 0, k = 2; + ll ret = 3, pib = 3, d; + while(++i){ + ret = mulMod(ret, ret, num) + num - 1; + if( ret >= num ) ret -= num; + if( pib - ret >= 0 ) d = gcd(pib - ret, num); + else d = gcd(ret - pib, num); + if( d != 1 and d != num ) return d; + if( i == k ) pib = ret, k <<= 1; + } +} + +void GetFactors(){ + for(int i = 0; prime[i] * prime[i] <= n and n != 1 and i < prime.size(); i++) + while( n % prime[i] == 0 ){ + n /= prime[i]; + printf( first ? "%lld" : " %lld", prime[i]); + first = false; + } +} + +void solve(){ + first = true; + if( n < L1 ){ + if( is_prime[n] ) printf("%lld", n); + else{ + GetFactors(); + if( n != 1 ) printf(first ? "%lld" : " %lld", n); + } + } + else if( n < L2 ){ + GetFactors(); + if( n != 1 ) printf(first ? "%lld" : " %lld", n); + } + else{ + if( MillerRobin(n) ) printf("%lld", n); + else{ + GetFactors(); + if( n != 1 ){ + if( MillerRobin(n) ) printf(first ? "%lld" : " %lld", n); + else{ + ll d = PollardRho(n); + if( d > n / d ) d = n / d; + printf(first ? "%lld %lld" : " %lld %lld", d, n / d); + } + } + } + } + putchar('\n'); +} + +int main(){ + sieve(); + scanf("%d", &tc); + while(tc--){ + scanf("%lld", &n); + solve(); + } + return(0); +} diff --git a/uva_cpp_clean/13193/13193-19.cpp b/uva_cpp_clean/13193/13193-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea0a7c44f13f67fbce59e787c366fe7b43aaa2de --- /dev/null +++ b/uva_cpp_clean/13193/13193-19.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +int main() { + int t; + int mask[32]; + cin >> t; + while (t--) { + long long n, auxi; + cin >> n; + auxi = n; + for (int i = 0; i < 32; i++) mask[i] = 0; + for (int i = 0; i < 32; i++) { + long long bit = auxi & 1; + int val = auxi & 31; + mask[val]++; + auxi >>= 1; + auxi += bit << 31; + } + bool ok = true; + for (int i = 0; i < 32 && ok; i++) { + ok = (mask[i] > 0); + } + if (ok) printf("yes\n"); + else printf("no\n"); + } + return 0; +} diff --git a/uva_cpp_clean/13194/13194-19.cpp b/uva_cpp_clean/13194/13194-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c125231db6fdd122e06088ef7c9f8f5c51ae324 --- /dev/null +++ b/uva_cpp_clean/13194/13194-19.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +void sieve(ll upperbound) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +void primeFactors(ll n, vii & factors) { + int c; + factors.clear(); + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +vii v; +long long dp[20]; + +long long proc(int i) { + if (i >= v.size()) return 1; + if (dp[i] == -1) { + long long ret = 0; + for (int j = 0; j <= v[i].second; j++) + ret += pow(v[i].first, j)*proc(i+1); + dp[i] = ret; + } + return dp[i]; +} + +int main() { + sieve(1000001); + int T; + cin >> T; + int pot[20]; + while (T--) { + long long N; + cin >> N; + primeFactors(N, v); + for (int i = 0; i < v.size(); i++) pot[i] = 0, dp[i] = -1; + long long ret = proc(0) - N; + if (ret == N) printf("perfect\n"); + else if (ret > N) printf("abundant\n"); + else printf("deficient\n"); + } + return 0; +} diff --git a/uva_cpp_clean/13204/13204-21.cpp b/uva_cpp_clean/13204/13204-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82a229eb0b1a3d88f77bc12afa57e92b87843758 --- /dev/null +++ b/uva_cpp_clean/13204/13204-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13204 + Name: Count these Permutations + Problem: https://onlinejudge.org/external/132/13204.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool seen[100]; +int bf(int i, int s, int n) { + if (i>n) return s == (n*n)>>1; + + int r = 0; + for (int j=1; j<=n; ++j) + if (!seen[j]) { + seen[j] = 1; + r += bf(i+1, s+fabs(i-j), n); + seen[j] = 0; + } + return r; +} +// bf(1, 0, n) + + +#define MODUL 1000000007 +int F[1000000] = { 1 }; +int main() { + for (long long i=1; i<1000000; ++i) + F[i] = (F[i-1] * i) % MODUL; + + int n; + while (cin >> n) { + long long r = F[n>>1]; + r = (r * r) % MODUL; + if (n&1) r = (r * n) % MODUL; + cout << r << endl; + } +} diff --git a/uva_cpp_clean/13206/13206-21.cpp b/uva_cpp_clean/13206/13206-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cecae1b4dff62ed69f71f07a5a8c1c12050e4f9 --- /dev/null +++ b/uva_cpp_clean/13206/13206-21.cpp @@ -0,0 +1,34 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13206 + Name: Beautiful Triad + Problem: https://onlinejudge.org/external/132/13206.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +long long calc(long long l) { + return 3*(l-1)*(l-1) + 3*(l-1) + 1; +} + +int main() { + int T, n, k; + cin >> T; + while (T-- && cin >> n >> k) { + long long cnt = 0; + if (n >= k) + cnt += (n-k+1) * calc(k+1); + + for (int i=1; i<=k; ++i) + cnt += calc(i); + + cout << cnt << endl; + } +} diff --git a/uva_cpp_clean/13207/13207-21.cpp b/uva_cpp_clean/13207/13207-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03e4740b6d837b3d8acc21c11519a6d8137653b2 --- /dev/null +++ b/uva_cpp_clean/13207/13207-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13207 + Name: AirCraft: Monster + Problem: https://onlinejudge.org/external/132/13207.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int X[43], Y[43], Z[43], + Cx[43], Cy[43], Cz[43], n; + +bool bt(int i, int x, int y, int z) { + if (!x && !y && !z) return 1; + if (x<0 || x-Cx[i]>0 || + y<0 || y-Cy[i]>0 || + z<0 || z-Cz[i]>0 || i>=n) return 0; + + return bt(i+1, x, y, z) || bt(i+1, x-X[i], y-Y[i], z-Z[i]); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, x, y, z; + cin >> T; + while (T-- && cin >> x >> y >> z >> n) { + string str; + for (int i=0; i> str >> X[i] >> Y[i] >> Z[i]; + + Cx[n] = Cy[n] = Cz[n] = 0; + for (int i=n-1; i>=0; --i) + Cx[i] = Cx[i+1] + X[i], + Cy[i] = Cy[i+1] + Y[i], + Cz[i] = Cz[i+1] + Z[i]; + + cout << (bt(0, x, y, z) ? "POSSIBLE\n" : "IMPOSSIBLE\n"); + } +} diff --git a/uva_cpp_clean/13208/13208-21.cpp b/uva_cpp_clean/13208/13208-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a1a279f2f326a3e6f7f2ad3fe6f02e542b09c66 --- /dev/null +++ b/uva_cpp_clean/13208/13208-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13208 + Name: Drought In Nlogonia + Problem: https://onlinejudge.org/external/132/13208.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ----------------------------------------------------------------------------- + + +long long C[10143]; +int H[10143], Hl[10143], Hr[10143], X[10143]; +int main() { + int T = readUInt(); + while (T--) { + int a = readUInt(), + n = readUInt(); + + for (int i=1; i<=n; ++i) + C[i] = C[i-1] + (X[i] = readUInt()); + + int mxi = 0; + for (int j=0; j<=n; ++j) { + H[j] = readUInt(); + Hl[j] = mxi = H[mxi] > H[j] ? mxi : j; + } + + mxi = n; + for (int j=n; j>=0; --j) + Hr[j] = mxi = H[mxi] > H[j] ? mxi : j; + + long long sum = 0; + for (int i=mxi; i0; ) { + int ni = Hl[i-1]; + sum += (C[i]-C[ni]) * min(H[i], H[ni]) * a; + i = ni; + } + + cout << sum << '\n'; + } +} diff --git a/uva_cpp_clean/13209/13209-21.cpp b/uva_cpp_clean/13209/13209-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b716a1f5552caa647f7f958be857fdad6d85d57 --- /dev/null +++ b/uva_cpp_clean/13209/13209-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13209 + Name: My Password is a Palindromic Prime Number + Problem: https://onlinejudge.org/external/132/13209.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + while (T-- && cin >> n) { + cout << "0."; + int num = 10; + bool seen[1000143] = {}; + while (!seen[num]) { + seen[num] = 1; + if (num < n) cout << '0'; + else { + int t = num / n; + num -= t * n; + cout << t; + } + num *= 10; + } + cout << endl; + } +} diff --git a/uva_cpp_clean/13212/13212-18.cpp b/uva_cpp_clean/13212/13212-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2a7bbe939196b8856baa7cd0bf2d79ca43c6d13 --- /dev/null +++ b/uva_cpp_clean/13212/13212-18.cpp @@ -0,0 +1,71 @@ +#include +#define pb push_back +#define pf push_front +#define pob pop_back +#define fp first +#define sp second +#define mp make_pair +#define ins insert +#define uEdge(u, v) g[u].pb(v), g[v].pb(u) +#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w}) +#define dEdge(u, v) g[u].pb(v) +#define dwEdge(u, v, w) g[u].pb({v, w}) +#define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define minHeap int, vector, greater + +using namespace std; +typedef long long int lli; +typedef pair pii; +vector g[0]; +lli ans; + +lli gcd(lli a, lli b){ + if(b == 0) return a; + a%=b; + return gcd(b, a); +} + +lli lcm(lli a, lli b){ + return (a*b)/gcd(a, b); +} + +int binsearch(int n, lli arr[], lli sum){ + int l = 0, r = n-1, mid; + while(l<=r){ + mid = (l+r)/2; + if(arr[mid]==sum) return mid; + if(arr[mid]= r) return; + int m = (l+r)/2; + mergesort(a, l, m); + mergesort(a, m+1, r); + merge(a, l, m, r); +} + +int main(){ + int a[1000100], n; + while(scanf("%d", &n) && n){ + for(int i = 0; i < n; i++) scanf("%d", &a[i]); + ans = 0; + mergesort(a, 0, n-1); + printf("%lld\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/13212/13212-21.cpp b/uva_cpp_clean/13212/13212-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b181072c694beeb144d86c7ca180b991b9b2d6a --- /dev/null +++ b/uva_cpp_clean/13212/13212-21.cpp @@ -0,0 +1,94 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13212 + Name: How many inversions? + Problem: https://onlinejudge.org/external/132/13212.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------ + +long long cnt; +int X[1000143], L[1000143], R[1000143], n; +void merge(int l, int m, int r) { + int n1 = m - l, + n2 = r - m; + + for (int i=0; i= r) return; + int m = (l + r) >> 1; + mergesort(l, m); + mergesort(m, r); + merge(l, m, r); +} + +int main() { + while ((n = readUInt())>0) { + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13214 + Name: The Robot's Grid + Problem: https://onlinejudge.org/external/132/13214.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long DP[55][55] = {}; + for (int sum=2; sum<=50; ++sum) { + DP[sum-1][1] = 1; + for (int c=2; c> T; + while (T-- && cin >> r >> c) + cout << DP[r][c] << endl; +} diff --git a/uva_cpp_clean/13215/13215-21.cpp b/uva_cpp_clean/13215/13215-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0633632c24e792489f4daadc32b525b24e874f62 --- /dev/null +++ b/uva_cpp_clean/13215/13215-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13215 + Name: Polygonal Park + Problem: https://onlinejudge.org/external/132/13215.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +const double a = sqrt(3)/4.0; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(4); + + int T, n; + cin >> T; + while (T-- && cin>>n) { + char ch; + double sl[4]={}, l, sum = 0; + for (int i=0, s=3; i> ch >> l; + sum += l*l*(ch == 'T' ? a : 1); + sl[s] += l; + if (ch == 'C') { + if (++s == 4) s = 0; + sl[s] += l; + } + } + cout << max(sl[0], sl[2])*max(sl[1], sl[3])-sum << endl; + } +} diff --git a/uva_cpp_clean/13216/13216-21.cpp b/uva_cpp_clean/13216/13216-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ccf6731dccdd18112e82b669571fd1fa443acb0 --- /dev/null +++ b/uva_cpp_clean/13216/13216-21.cpp @@ -0,0 +1,29 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 13216 + Name: Problem with a ridiculously long name but with a ridiculously short description + Problem: https://onlinejudge.org/external/132/13216.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int X[] = { 56, 96, 36, 76, 16 }; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + string S; + while (T-- && cin >> S) { + if (S == "0") cout << "1\n"; + else if (S == "1") cout << "66\n"; + else cout << X[(S[S.length()-1]-'0'+8)%5] << endl; + } +} diff --git a/uva_cpp_clean/13251/13251-18.cpp b/uva_cpp_clean/13251/13251-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a62b512a2b4a4024f3692dd61e108b5b95d05e9c --- /dev/null +++ b/uva_cpp_clean/13251/13251-18.cpp @@ -0,0 +1,47 @@ +#include //Para aprenderme la liberia + +using namespace std; + +char input[200000]; +set term; //Para aprender a usarlo + +void DFS(int index, int end){ + term.insert(input + index); + int indexSub = -1; + for(int i = index; i <= end; i++){ + if(!isalpha(input[i])){ + indexSub = i + 1; + break; + } + } + if(indexSub == -1) return; + for(int i = indexSub, par = 0; i <= end; i++){ + if(input[i] == '(') par++; + else if(input[i] == ')') par--; + if(input[i] == ',' || i == end){ + if(par == 0 || i == end){ + input[i] = '\0'; + DFS(indexSub, i - 1); + indexSub = i + 1; + } + } + } +} + +int main(){ + while(scanf("%s", input) == 1){ + term.clear(); + DFS(0, strlen(input) - 1); + printf("%d\n", term.size()); + } + return 0; +} + +/* + La estrategia es guardar todas las funciones en el set de string, aprovechando de que esta estructura de datos + guarda solo datos unicos. Lo dificil que encontré en este problema es como sacar cada función, pensé en usar una + pila para hacerlo iterativo pero me dí cuenta de que debia usar recursividad puesto que si se presenta el caso + de que dentro de los argumentos de una función exista otra y esta ultima tambien la debo de tratar como si fuera + una función nueva. Aprendí a realizar substring con arreglos char, tenia que usarlos ya que obteniendo substring + con variables string no me estaba dando las que necesitaba para guardar. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/13252/13252-18.cpp b/uva_cpp_clean/13252/13252-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4f488822f1c5ac197771e945a4453a1c41a782fc --- /dev/null +++ b/uva_cpp_clean/13252/13252-18.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +using namespace std; + +vector sequence; + +void calSequence(int k, int n, int t, int p, int a[]){ + if(t > n){ + if(n % p == 0){ + for(int i = 1; i < p + 1; i++) sequence.push_back(a[i]); + } + }else{ + a[t] = a[t - p]; + calSequence(k, n, t + 1, p, a); + for(int i = a[t - p] + 1; i < k; i++){ + a[t] = i; + calSequence(k, n, t + 1, t, a); + } + } +} + +void deBruijn_Sequence(int k, int n){ + int a[n * k] = {0}; + sequence.clear(); + if(k > 1){ + calSequence(k, n, 1, 1, a); + for(int i = 0; i < sequence.size(); i++) printf("%c", sequence[i]+'A'); + }else for(int i = 0; i < n; i++) printf("%c",'A'); + printf("\n"); +} + +int main() { + int k, n; + while(scanf("%d %d", &k, &n) != EOF) deBruijn_Sequence(k, n); +} +/* + Es un problema interesante y pude aprender mucho aqui. La estrategia es identificar + de que piden la secuencia de de Bruijn, lamentablemente no pude idear una solucion optima + que no sea el crear un grafo asi que implementé la solución dada por Wikipedia +*/ \ No newline at end of file diff --git a/uva_cpp_clean/13257/13257-18.cpp b/uva_cpp_clean/13257/13257-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8b1146c778566944fa618b566a394e1458ab9755 --- /dev/null +++ b/uva_cpp_clean/13257/13257-18.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +int main(){ + int n, len; + char input[100050]; + scanf("%d", &n); + while(n--){ + scanf("%s", input); + len = strlen(input); + int post[len+1][26]; + for(int i = 0; i < 26; i++) post[len][i] = -1; + for(int i = len - 1; i >= 0; i--){ + for(int j = 0; j < 26; j++) post[i][j] = post[i + 1][j]; + post[i][input[i] - 'A'] = i; + } + int result = 0; + for(int i = 0; i < 26; i++){ + if(post[0][i] == -1) continue; + int one = post[0][i]; + for(int j = 0; j < 26; j++){ + if(post[one + 1][j] == -1) continue; + int two = post[one + 1][j]; + for(int k = 0; k < 26; k++){ + if(post[two + 1][k] == -1) continue; + result++; + } + } + } + printf("%d\n", result); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1329/1329-4.cpp b/uva_cpp_clean/1329/1329-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01c8d02a6598014fecdf17cf3c025b26b3611eab --- /dev/null +++ b/uva_cpp_clean/1329/1329-4.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +int findset(int *parent, int *value, int x) +{ + if (x == parent[x]) + { + return x; + } + + int root = findset(parent, value, parent[x]); + value[x] += value[parent[x]]; + parent[x] = root; + + return parent[x]; +} + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + int n; + std::cin >> n; + + int *parent = new int[n + 1]; + int *value = new int[n + 1]; + + for (int i = 1; i < n + 1; i++) + { + parent[i] = i; + value[i] = 0; + } + + while (true) + { + char mode; + std::cin >> mode; + + if (mode == 'O') + { + break; + } + else if (mode == 'E') + { + int i; + std::cin >> i; + + findset(parent, value, i); + std::cout << value[i] << std::endl; + } + else if (mode == 'I') + { + int i, j; + std::cin >> i >> j; + + parent[i] = j; + value[i] = std::abs(i - j) % 1000; + } + } + + delete[] parent; + delete[] value; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/13298/13298-18.cpp b/uva_cpp_clean/13298/13298-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ba6cf7ba504f968bf11abcbfc0dc3d097744757 --- /dev/null +++ b/uva_cpp_clean/13298/13298-18.cpp @@ -0,0 +1,104 @@ +#include +#define pb push_back +#define pf push_front +#define pob pop_back +#define fp first +#define sp second +#define mp make_pair +#define ins insert +#define uEdge(u, v) g[u].pb(v), g[v].pb(u) +#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w}) +#define dEdge(u, v) g[u].pb(v) +#define dwEdge(u, v, w) g[u].pb({v, w}) +#define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define minHeap int, vector, greater +#define MOD 1000000009 + +using namespace std; +typedef long long int lli; +typedef pair pii; +vector g[0]; +lli bc[110]; + +struct matrix{ + int row, col; + lli m[110][110]; +}; + +lli gcd(lli a, lli b){ + if(b == 0) return a; + a%=b; + return gcd(b, a); +} + +lli lcm(lli a, lli b){ + return (a*b)/gcd(a, b); +} + +int binsearch(int n, lli arr[], lli sum){ + int l = 0, r = n-1, mid; + while(l<=r){ + mid = (l+r)/2; + if(arr[mid]==sum) return mid; + if(arr[mid]>1); + return mul(ans, ans); +} + +void setBC(int k){ + bc[0] = 1; + for(int i = 1; i < 110; i++){ + bc[i] = 0; + int aux = k; + for(int j = i-1; j >= 0 && aux; j--, aux--) bc[i]=(bc[i]+bc[j])%MOD; + } +} + +int main(){ + int k; + lli sum, n; + while(scanf("%d %lld", &k, &n) == 2){ + if(!k && !n) break; + setBC(k); + matrix trans; + trans.row = trans.col = k; + for(int i = 0; i < k; i++){ + for(int j = 0; j < k; j++){ + trans.m[i][j] = 0; + if(i==0 || i==j+1) trans.m[i][j] = 1; + } + } + if(n<=k) printf("%lld\n", bc[n]); + else{ + trans = mPow(trans, n-k+1); + sum = 0; + for(int i = 0; i < k; i++) sum+=(trans.m[0][i]*bc[k-i-1])%MOD, sum%=MOD; + printf("%lld\n", sum); + } + } + return 0; +} diff --git a/uva_cpp_clean/133/133-17.cpp b/uva_cpp_clean/133/133-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68ad5e2e3fa61a702eb689687130db143370ec48 --- /dev/null +++ b/uva_cpp_clean/133/133-17.cpp @@ -0,0 +1,38 @@ +/** + * AOAPC II Example 4-3 The Dole Queue + * simulation + */ +#include +using namespace std; +int main() { + int n, k, m; + while ((cin >> n >> k >> m) && n) { + vector dole(n); + iota(dole.begin(), dole.end(), 1); + int left = n, kpos = 0, mpos = n - 1; + while (left) { + int curK = k, knum, curM = m, mnum; + while (curK) { + if (knum = dole[kpos]) + --curK; + kpos = (kpos + 1) % n; + } + while (curM) { + if (mnum = dole[mpos]) + --curM; + mpos = (mpos - 1 + n) % n; + } + dole[(kpos - 1 + n) % n] = 0; + --left; + cout << setw(3) << knum; + if (mnum != knum) { + cout << setw(3) << mnum; + dole[(mnum - 1 + n) % n] = 0; + --left; + } + if (left) + cout << ","; + } + cout << '\n'; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/13301/13301-18.cpp b/uva_cpp_clean/13301/13301-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f63c019d0f2ed6ceb343140d9305cd27b30c531f --- /dev/null +++ b/uva_cpp_clean/13301/13301-18.cpp @@ -0,0 +1,74 @@ +#include +#define pb push_back +#define pf push_front +#define pob pop_back +#define fp first +#define sp second +#define mp make_pair +#define ins insert +#define uEdge(u, v) g[u].pb(v), g[v].pb(u) +#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w}) +#define dEdge(u, v) g[u].pb(v) +#define dwEdge(u, v, w) g[u].pb({v, w}) +#define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define minHeap int, vector, greater + +using namespace std; +typedef long long int lli; +typedef pair pii; +vector g[100000], invG[100000]; +bool vist[100000]; +stack s; +int cnt, n; + +void DFS1(int u){ + vist[u] = true; + for(auto v : g[u]) if(!vist[v]) DFS1(v); + s.push(u); +} + +void DFS2(int u){ + vist[u] = true; + for(auto v : invG[u]) if(!vist[v]) DFS2(v); +} + +void SCC(){ + cnt = 0; + memset(vist, false, sizeof(vist)); + for(int u = 1; u <= n; u++) if(!vist[u]) DFS1(u); //Realizar DFS con el grafo para obtener nodos con el mayor tiempo de llegada + memset(vist, false, sizeof(vist)); + while(!s.empty()){ + int v = s.top(); + s.pop(); + if(!vist[v]){ //Imprimir SCC + DFS2(v); + cnt++; + } + } +} + +int main(){ + int m, u, v, k; + while(scanf("%d %d", &n, &m) != EOF){ + for(int i = 1; i <= n; i++) g[i].clear(), invG[i].clear(); + for(int j = 0; j < m; j++){ + scanf("%d %d %d", &k, &u, &v); + if(k == 1){ + dEdge(u, v); + invG[v].pb(u); + }else{ + uEdge(u, v); + invG[u].pb(v); + invG[v].pb(u); + for(int i = 2; i < k; i++){ + scanf("%d", &u); + uEdge(v, u), invG[u].pb(v), invG[v].pb(u), v = u; + } + } + } + SCC(); + if(cnt == 1) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1339/1339-17.cpp b/uva_cpp_clean/1339/1339-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69ce5b93e70b153c97cfe0bf6c9f219f5ce0ba2f --- /dev/null +++ b/uva_cpp_clean/1339/1339-17.cpp @@ -0,0 +1,22 @@ +/** + * AOAPC II Example 4-1 Ancient Cypher + * stats + */ +#include +using namespace std; +int main() { + string a, b; + while (cin >> a >> b) { + vector freqA(26), freqB(26); + for (char c : a) + ++freqA[c - 'A']; + for (char c : b) + ++freqB[c - 'A']; + sort(freqA.begin(), freqA.end()); + sort(freqB.begin(), freqB.end()); + if (equal(freqA.begin(), freqA.end(), freqB.begin())) + cout << "YES\n"; + else + cout << "NO\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1339/1339-20.cpp b/uva_cpp_clean/1339/1339-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38ccbb949f19a78d89ef93718e1ce61f5ff1f491 --- /dev/null +++ b/uva_cpp_clean/1339/1339-20.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + char line_1[105], line_2[105]; + + while( cin >> line_1 >> line_2 ) + { + int letter_1[26], letter_2[26]; + for(int i=0 ; i<26 ; i++) letter_1[i] = letter_2[i] = 0; + + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1339 + Name: Ancient Cipher + Problem: https://onlinejudge.org/external/13/1339.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string s1, s2; + while (getline(cin, s1) && getline(cin, s2)) { + char c1[26]={}, c2[26]={}; + for (int i=0; s1[i]; ++i) { + ++c1[s1[i]-'A']; + ++c2[s2[i]-'A']; + } + sort(c1, c1+26); + sort(c2, c2+26); + + bool p = 1; + for (int i=0; p && i<26; ++i) + if (c1[i] != c2[i]) + p = 0; + + cout << (p ? "YES\n" : "NO\n"); + } +} diff --git a/uva_cpp_clean/134/134-21.cpp b/uva_cpp_clean/134/134-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43d59ff2e1db2cd7e1b1c7e22290b7fa72a29a68 --- /dev/null +++ b/uva_cpp_clean/134/134-21.cpp @@ -0,0 +1,155 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 134 + Name: Loglan-A Logical Language + Problem: https://onlinejudge.org/external/1/134.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +static inline std::string &rtrim(std::string &s) { + s.erase(std::find_if(s.rbegin(), s.rend(), + std::not1(std::ptr_fun(std::isspace))).base(), s.end()); + return s; +} + +// ------------------------------------------------------ + + + + +int pos, len; +vector tokens; + +bool V[128]; +bool A() { + if (pos >= len) return 0; + string &s = tokens[pos]; + if (s.length()==1 && V[s[0]]) + return ++pos; + return 0; +} + +bool TL(char ch) { + if (pos >= len) return 0; + string &s = tokens[pos]; + if (s.length()==2 && V[s[1]] && s[0]==ch) + return ++pos; + return 0; +} +bool MOD() { return TL('g'); } +bool BA() { return TL('b'); } +bool DA() { return TL('d'); } +bool LA() { return TL('l'); } + +bool NAM() { + if (pos >= len) return 0; + string &s = tokens[pos]; + if (!V[s[s.length()-1]]) + return ++pos; + return 0; +} +bool PREDA() { + if (pos >= len) return 0; + string &s = tokens[pos]; + if (s.length() == 5 && !V[s[0]] && !V[s[3]] && V[s[4]] && V[s[1]]!=V[s[2]]) + return ++pos; + return 0; +} +bool predstring() { + if (pos >= len || !PREDA()) + return 0; + while (PREDA()); + return 1; +} +bool verbpred() { + int b = pos; + if (MOD() && predstring()) + return 1; + pos = b; + return 0; +} +bool predname() { + int b = pos; + if (NAM() || (LA() && predstring())) + return 1; + pos = b; + return 0; +} +bool statement() { + int b = pos; + if (predname() && verbpred()) { + predname(); + return 1; + } + pos = b; + return 0; +} +bool preds() { + int b = pos; + if (predstring()) { + int bb; + do { bb = pos; } + while (A() && predstring()); + pos = bb; + return 1; + } + pos = b; + return 0; +} +bool predclaim() { + int b = pos; + if (DA() && preds()) + return 1; + + pos = b; + if (predname() && BA() && preds()) + return 1; + + pos = b; + return 0; +} +bool sentence() { + int b = pos; + if (statement()) return 1; + + pos = b; + if (predclaim()) return 1; + + pos = b; + return 0; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + V['a']=V['e']=V['i']=V['o']=V['u']=1; + + string line, exline; + while (getline(cin, line) && line[0]!='#') { + line = rtrim(line); + while (line[line.length()-1] != '.') { + getline(cin, exline); + line += " " + exline; + line = rtrim(line); + } + + //cout << line << endl; + tokens.clear(); + for (int i=0, j=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1343 + Name: The Rotation Game + Problem: https://onlinejudge.org/external/13/1343.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int moves[8][7] = { + { 0, 2, 6, 11, 15, 20, 22 }, + { 1, 3, 8, 12, 17, 21, 23 }, + { 10, 9, 8, 7, 6, 5, 4 }, + { 19, 18, 17, 16, 15, 14, 13 }, + { 23, 21, 17, 12, 8, 3, 1 }, + { 22, 20, 15, 11, 6, 2, 0 }, + { 13, 14, 15, 16, 17, 18, 19 }, + { 4, 5, 6, 7, 8, 9, 10} +}; +int center[] = {6, 7, 8, 11, 12, 15, 16, 17}; + +void moveForward(char XX[], int k) { + char t = XX[moves[k][0]]; + for (int j=0; j<6; ++j) + XX[moves[k][j]] = XX[moves[k][j+1]]; + XX[moves[k][6]] = t; +} + +int H(char X[]) { + int cnt[3] = {}; + for (int i=0; i<8; ++i) + ++cnt[X[center[i]]]; + return 8 - max(cnt[0], max(cnt[1], cnt[2])); +} + + +int path[20], maxd; +bool IDAS (char X[], int d) { + int hv = H(X); // minimum remaining moves + if (!hv) { + if (!d) cout << "No moves needed\n"; + else { + for (int i=0; i maxd) return 0; + + char Y[24]; + for (int k=0; k<8; ++k) { + path[d] = k; + memcpy(Y, X, 24); + moveForward(Y, k); + if (IDAS(Y, d+1)) + return 1; + } + return 0; +} + + +int main() { + int xx[24]; + char X[24]; + for (int cse=1; cin>>xx[0] && xx[0]; ++cse) { + for (int i=1; i<24; ++i) + cin >> xx[i]; + + for (int i=0; i<24; ++i) + X[i] = xx[i] - 1; + + bool solved = 0; + for (maxd=1; !solved; ++maxd) + solved |= IDAS(X, 0); + } +} diff --git a/uva_cpp_clean/1347/1347-8.cpp b/uva_cpp_clean/1347/1347-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d4f0edc4349be6bfb55afdf1b6f1e5868b60a07 --- /dev/null +++ b/uva_cpp_clean/1347/1347-8.cpp @@ -0,0 +1,50 @@ +// Problem name: Tour +// Problem link: https://vjudge.net/problem/UVA-1347 +// Submission link: https://vjudge.net/solution/12786049 + +#include + +#define MAX ((int)1010) +#define EPS ((double)1e-5) +#define mp make_pair +#define F first +#define S second +#define sqr(x) ((x) * (x)) +#define endl '\n' + +using namespace std; + +typedef pair point; + +double dist(point a, point b){ + return sqrt(sqr(a.F - b.F) + sqr(a.S - b.S)); +} + +int n; +double ans[ MAX ][ MAX ]; +point p[ MAX ]; + +double solve(int left, int right){ + int now = max(left, right) + 1; + if(now == n - 1) + return dist(p[ left ], p[ now ]) + dist(p[ now ], p[ right ]); + if(ans[ left ][ right ] >= EPS) + return ans[ left ][ right ]; + return ans[ left ][ right ] = min(dist(p[ left ], p[ now ]) + solve(now, right), dist(p[ now ], p[ right ]) + solve(left, now)); +} + +int main(){ + ios_base::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + while(cin >> n){ + for(int i = 0 ; i < n ; i++) + cin >> p[ i ].F >> p[ i ].S; + memset(ans, 0, sizeof(ans)); + cout.precision(2); + cout.flags(ios::fixed); + cout << solve(0, 0) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/136/136-15.cpp b/uva_cpp_clean/136/136-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e4e0c74976ada5aeec0c8cef1fe25a59eef6be9 --- /dev/null +++ b/uva_cpp_clean/136/136-15.cpp @@ -0,0 +1,45 @@ +/* + 醜數 + 時間複雜度: O(1) +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + int i; + long long ith_ugly_number; + // 紀錄是否已經放入priority_queue + unordered_map seen; + priority_queue, greater> ugly_numbers; + + ugly_numbers.push(1); + seen[1] = 1; + + for (i = 1; i < 1501; i++) { + ith_ugly_number = ugly_numbers.top(); + ugly_numbers.pop(); + // 把2、3、5的倍數放進ugly_numbers + if (seen[ith_ugly_number * 2] == 0) { + ugly_numbers.push(ith_ugly_number * 2); + seen[ith_ugly_number * 2] = 1; + } + if (seen[ith_ugly_number * 3] == 0) { + ugly_numbers.push(ith_ugly_number * 3); + seen[ith_ugly_number * 3] = 1; + } + if (seen[ith_ugly_number * 5] == 0) { + ugly_numbers.push(ith_ugly_number * 5); + seen[ith_ugly_number * 5] = 1; + } + } + cout << "The 1500'th ugly number is " << ith_ugly_number << ".\n"; + return 0; +} + +/* + cout << "The 1500'th ugly number is 859963392.\n"; +*/ \ No newline at end of file diff --git a/uva_cpp_clean/136/136-17.cpp b/uva_cpp_clean/136/136-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..197a299f666fea3ef59c5d889ef9d33dac49cc98 --- /dev/null +++ b/uva_cpp_clean/136/136-17.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; +int main() { + const vector pf { 2, 3, 5 }; + set uglys { 1 }; + for (int cnt = 1; cnt < 1500; ++cnt) { + long long cur = *uglys.begin(); + uglys.erase(cur); + for (int f : pf) + uglys.insert(cur * f); + } + cout << "The 1500'th ugly number is " << *uglys.begin() << ".\n"; +} diff --git a/uva_cpp_clean/136/136-19.cpp b/uva_cpp_clean/136/136-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..124cc5a613b98bf616c0926b69a5fcefe6bc896a --- /dev/null +++ b/uva_cpp_clean/136/136-19.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main() { + /*int cont = 0, n = 0, aux; + while (cont != 1500) { + n++; + aux = n; + while (aux % 2 == 0) aux/=2; + while (aux % 3 == 0) aux/=3; + while (aux % 5 == 0) aux/=5; + if (aux == 1) cont ++; + } + printf("%d\n", n);*/ + printf("The 1500'th ugly number is 859963392.\n"); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/136/136-20.cpp b/uva_cpp_clean/136/136-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c5e8f91100cf5f6133f1b3e2b2f5c8ae0a6a371 --- /dev/null +++ b/uva_cpp_clean/136/136-20.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; + +int ugly[1502]; +int p2, p3, p5; + +int main() +{ + ugly[1] = p2 = p3 = p5 = 1; + for(int i=2 ; i<=1500 ; i++) + { + ugly[i] = min(min(2*ugly[p2], 3*ugly[p3]), 5*ugly[p5]); + if( ugly[i]==2*ugly[p2] ) p2++; + if( ugly[i]==3*ugly[p3] ) p3++; + if( ugly[i]==5*ugly[p5] ) p5++; + } + + printf("The 1500'th ugly number is %d.\n", ugly[1500]); + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/136/136-21.cpp b/uva_cpp_clean/136/136-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..097150dbc05209e56fbebf26ef82c5f46493c593 --- /dev/null +++ b/uva_cpp_clean/136/136-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 136 + Name: Ugly Numbers + Problem: https://onlinejudge.org/external/1/136.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +bool isUgly(int n) { + while(n && (n&1)==0) n>>=1; + while(n && n%3==0) n/=3; + while(n && n%5==0) n/=5; + return n==1; +} + +int main(){ + /* + int th=0; + long long int i; + for (i=0; th!=1500; i++) + if (isUgly(i)) + th++; + + cout << i-1 << endl; + */ + printf("The 1500'th ugly number is 859963392.\n"); +} + diff --git a/uva_cpp_clean/136/136-5.cpp b/uva_cpp_clean/136/136-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e55debfa5b4ed5c9313edd98dcd1b5a364b5f3cd --- /dev/null +++ b/uva_cpp_clean/136/136-5.cpp @@ -0,0 +1,6 @@ +#include +int main() +{ + printf("The 1500'th ugly number is 859963392.\n"); + return 0; +} diff --git a/uva_cpp_clean/1365/1365-21.cpp b/uva_cpp_clean/1365/1365-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c57ec8c2b98926a33dc600752ec36ea5f346b7f --- /dev/null +++ b/uva_cpp_clean/1365/1365-21.cpp @@ -0,0 +1,75 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1365 + Name: Wild West + Problem: https://onlinejudge.org/external/13/1365.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +typedef unsigned long long Long; + +#define MAXN 100017 +int x[MAXN], y[MAXN], z[MAXN], ord[MAXN]; +bool lessz (int i, int j) { return z[i] > z[j]; } +bool lessxy(int i, int j) { return x[i] != x[j] ? x[i] < x[j] : y[i] > y[j]; } + +set rarea(lessxy); +Long ainsert(int p) { + int a=x[p], b=y[p]; + Long sum = a*b; + if (rarea.size()) { + int my = 0; + auto it = rarea.lower_bound(p); + if (it == rarea.end()) --it; + else if (x[*it]==a && y[*it]==b) return 0; + + int j = *it; + for (; it!=rarea.begin() && y[j] <= b; j=*it) { + sum -= (y[j] - my) * min(x[j], a); + my = y[j]; + + if (x[j] <= a) { + auto it2 = it; --it; + rarea.erase(it2); + } + else --it; + } + sum -= (min(b, y[j]) - my) * min(x[j], a); + if (x[j] <= a && y[j] <= b) + rarea.erase(it); + } + + if (sum > 0) + rarea.insert(p); + return sum; +} + +int main() { + int n, m; + while (scanf("%d%d", &n, &m)==2 && (n||m)) { + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1367 + Name: Nuclear Plants + Problem: https://onlinejudge.org/external/13/1367.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 3e-5 + +const double nsr=0.58, nlr=1.31; +struct Circle; +struct Rect { + double x1, y1, x2, y2; + Rect(){} + Rect(double x1, double y1, double x2, double y2):x1(x1),y1(y1),x2(x2),y2(y2){} + bool has(double px, double py) const { + return px>=x1 && px<=x2 && py>=y1 && py<=y2; + } + double area() const { return (x2-x1)*(y2-y1); } +}; + +struct Circle { + double x, y, r, r2; + bool has(double px, double py) const { + double xx = px-x, yy=py-y; + return xx*xx+yy*yy <= r2; + } +} p[300]; + +bool includes(const Rect &r, const Circle &c) { + return r.has(c.x, c.y) || r.has(c.x-c.r, c.y) || r.has(c.x+c.r, c.y) || r.has(c.x, c.y-c.r) || r.has(c.x, c.y+c.r); +} + +int includes(const Circle &c, const Rect &r) { + return c.has(r.x1, r.y1) + c.has(r.x2, r.y1) + c.has(r.x1, r.y2) + c.has(r.x2, r.y2); +} + +bool intersects(Circle c, Rect r) { + return includes(r, c) || includes(c, r); +} + +int pi[1000000]; +double solve(Rect &a, int pi[], int len) { + double area = a.area(); + if (!len) return area; + if (area a.y2-a.y1) { + double mx = (a.x1+a.x2)/2; + ar[0].x2 = mx; + ar[0].y2 = a.y2; + ar[1].x1 = mx; + ar[1].y1 = a.y1; + } + else { + double my = (a.y1+a.y2)/2; + ar[0].x2 = a.x2; + ar[0].y2 = my; + ar[1].x1 = a.x1; + ar[1].y1 = my; + } + + double sum = 0; + for (int i=0; i<2; ++i) { + int l2 = 0; + for (int j=0; j> n >> m >> k1 >> k2 && (n||m||k1||k2)) { + Rect a(0, 0, n, m); + + int k = 0; + for (int i=0; i> p[i].x >> p[i].y; + p[i].r2 = nsr*nsr; + p[i].r = nsr; + + if (intersects(p[i], a)) + pi[k++] = i; + } + + for (int i=0; i> p[k1+i].x >> p[k1+i].y; + p[k1+i].r2 = nlr*nlr; + p[k1+i].r = nlr; + + if (intersects(p[k1+i], a)) + pi[k++] = k1+i; + } + + printf("%.2f\n", solve(a, pi, k)); + } +} diff --git a/uva_cpp_clean/1368/1368-17.cpp b/uva_cpp_clean/1368/1368-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..997f0b2a13b7795493ae0e2e9df29ffbb80bc4d0 --- /dev/null +++ b/uva_cpp_clean/1368/1368-17.cpp @@ -0,0 +1,36 @@ +/** + * AOAPC 3.7 DNA Consensus String + * ICPC Seoul 2006 + * stats + * 67 AC + */ +#include +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + const map pMap { {'A', 0}, {'C', 1}, {'G', 2}, {'T', 3} }; + const string nMap = "ACGT"; + int T; + cin >> T; + while (T--) { + int m, n; + cin >> m >> n; + vector> freq(n, vector(4, 0)); + for (int i = 0; i < m; ++i) { + string cur; + cin >> cur; + for (int c = 0; c < n; ++c) + ++freq[c][pMap.at(cur[c])]; + } + string ans = ""; + int ce = 0; + for (auto i : freq) { + auto pos = max_element(i.begin(), i.end()); + ans += nMap[pos - i.begin()]; + ce += m - *pos; + } + cout << ans << '\n'; + cout << ce << '\n'; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1368/1368-19.cpp b/uva_cpp_clean/1368/1368-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb993534b3c12ec75fd75bfb26cecf3aa5a2c11b --- /dev/null +++ b/uva_cpp_clean/1368/1368-19.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + int cases; + vector v; + cin >> cases; + while (cases--) { + v.clear(); + int n, m; + cin >> m >> n; + string s; + for (int i = 0; i < m; i++) { + cin >> s; + v.push_back(s); + } + s = string(n, ' '); + int d = 0; + for (int i = 0; i < n; i++) { + int A = 0, C = 0, G = 0, T = 0; + for (int j = 0; j < m; j++) { + if (v[j][i] == 'A') A++; + if (v[j][i] == 'C') C++; + if (v[j][i] == 'G') G++; + if (v[j][i] == 'T') T++; + } + int maxi = max(max(A, C), max(G, T)); + if (A == maxi) s[i] = 'A'; + else if (C == maxi) s[i] = 'C'; + else if (G == maxi) s[i] = 'G'; + else if (T == maxi) s[i] = 'T'; + d += A + C + G + T - maxi; + } + cout << s << endl << d << endl; + } + return 0; +} diff --git a/uva_cpp_clean/1368/1368-21.cpp b/uva_cpp_clean/1368/1368-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dab0a28734e6e7b2b9534096627178285d5d2658 --- /dev/null +++ b/uva_cpp_clean/1368/1368-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1368 + Name: DNA Consensus String + Problem: https://onlinejudge.org/external/13/1368.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char DNA[] = "ACGT", D[55][1143]; +int ids[91]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=0; DNA[i]; ++i) + ids[DNA[i]] = i; + + int T, n, m; + cin >> T; + while (T-- && cin>>m>>n) { + for (int i=0; i> D[i]; + + int sum = 0; + for (int j=0; j C[mxi]) + mxi = i; + cout << DNA[mxi]; + for (int i=0; i<4; ++i) + if (mxi!=i) + sum += C[i]; + } + cout << '\n' << sum << '\n'; + } +} diff --git a/uva_cpp_clean/1368/1368-4.cpp b/uva_cpp_clean/1368/1368-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdd8abb5e179c796d174be672c7c06cb0a663ebc --- /dev/null +++ b/uva_cpp_clean/1368/1368-4.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +std::pair findnearest(std::vector &input, int index) +{ + char c; + int max = 0; + std::map codes; + + for (size_t i = 0; i < input.size(); i++) + { + codes[input[i][index]]++; + } + + for (const auto &it : codes) + { + if (it.second > max) + { + c = it.first; + max = it.second; + } + } + + return std::make_pair(c, input.size() - max); +} + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + int n, s; + std::cin >> n >> s; + std::vector tab; + + for (int j = 0; j < n; j++) + { + std::string str; + std::cin >> str; + tab.push_back(str); + } + + std::string dna = ""; + int error = 0; + + for (int i = 0; i < s; i++) + { + std::pair n = findnearest(tab, i); + + dna += n.first; + error += n.second; + } + + std::cout << dna << std::endl + << error << std::endl; + } +} diff --git a/uva_cpp_clean/138/138-19.cpp b/uva_cpp_clean/138/138-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1526b553f95aa5f93d18af60a3ca9f0e858776a5 --- /dev/null +++ b/uva_cpp_clean/138/138-19.cpp @@ -0,0 +1,17 @@ +#include +#include +using namespace std; + +int main() { + printf(" 6 8\n 35 49\n 204 288\n 1189 1681\n 6930 9800\n 40391 57121\n 235416 332928\n 1372105 1940449\n 7997214 11309768\n 46611179 65918161\n"); + return 0; +} + +/* +import math; +for i in range(2, 1000000000): + s1 = ((i - 1) * i) /2; + val = math.floor((-1 + math.sqrt(1 + 8*(2*s1 + i)))/2); + if (s1 == ((val*(val+1)/2) - s1 - i)): + print (i, val); +*/ diff --git a/uva_cpp_clean/138/138-20.cpp b/uva_cpp_clean/138/138-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9ce0c4ff3afc9733179a9f37a0014f2331173da --- /dev/null +++ b/uva_cpp_clean/138/138-20.cpp @@ -0,0 +1,22 @@ +#include +#include + +long long tc, n, x; +double temp; + +int main() +{ + tc = 1, x = 8; + while( tc<=10 ) + { + temp = sqrt( ((x*x)+x)/2 ); + n = temp; + if( n==temp ) + { + printf("%10lld%10lld\n", n, x); + tc++; + } + x++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/138/138-21.cpp b/uva_cpp_clean/138/138-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..715af5a181c8590ef827df3a37c5e6f634cec2a5 --- /dev/null +++ b/uva_cpp_clean/138/138-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 138 + Name: Street Numbers + Problem: https://onlinejudge.org/external/1/138.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + puts(" 6 8\n\ + 35 49\n\ + 204 288\n\ + 1189 1681\n\ + 6930 9800\n\ + 40391 57121\n\ + 235416 332928\n\ + 1372105 1940449\n\ + 7997214 11309768\n\ + 46611179 65918161"); +/* + for (long long int y=4; ; y++) { + long long int x2 = (y&1) ? ((y+1)>>1)*y : (y>>1)*(y+1), + x = sqrt(x2); + + if (x*x == x2) + printf("%10lld%10lld\n", x, y); + }*/ +} diff --git a/uva_cpp_clean/138/138-5.cpp b/uva_cpp_clean/138/138-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..af33f80ead9d6bc0d506c8528bd5bc828e959271 --- /dev/null +++ b/uva_cpp_clean/138/138-5.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int s[20] = { +6,8, +35,49, +204,288, +1189,1681, +6930,9800, +40391,57121, +235416,332928, +1372105,1940449, +7997214,11309768, +46611179,65918161 +}; +int main() +{ + for(int i=0;i<20;i+=2) + printf("%10d%10d\n",s[i],s[i+1]); + return 0; +} diff --git a/uva_cpp_clean/1388/1388-19.cpp b/uva_cpp_clean/1388/1388-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb1c392f05c42b045f1c8a5eca00cc5abc2729ca --- /dev/null +++ b/uva_cpp_clean/1388/1388-19.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +int main() { + int n, m; + while (cin >> n >> m) { + double res = 0; + for (int i = 1; i < n; i++) { + double d1 = 10000.0/n, d2 = 10000.0/(n+m); + double cur = i*d1; + double f = floor(cur/d2)*d2, c = ceil(cur/d2)*d2; + if (cur - f < c - cur) res += cur - f; + else res += c - cur; + } + printf("%.4lf\n", res); + } + return 0; +} diff --git a/uva_cpp_clean/1388/1388-9.cpp b/uva_cpp_clean/1388/1388-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1360c59bc72fa9f934b1c4f636917e9163817ef --- /dev/null +++ b/uva_cpp_clean/1388/1388-9.cpp @@ -0,0 +1,44 @@ +#include + +const int EPS = 1e-9; + +using namespace std; + +int N, M; +vector arr_1, arr_2; + +double solve () { + double theta = 0.0; + for (double alfa_1: arr_1) { + double min_ = 360.0; + for (double alfa_2: arr_2) { + min_ = min(min_, fabs(alfa_2 - alfa_1)); + } + theta += min_; + } + return theta * 10000.0 / 360.0; +} + +void build (int n, vector & arr) { + arr.reserve(n); + double angle = 360.0 / n; + arr.emplace_back(0.0); + for (int i = 1; i < n; i++) { + arr.emplace_back(arr.back() + angle); + } +} + +inline void clear () { + arr_1.clear(); + arr_2.clear(); +} + +int main () { + while (~scanf("%d %d", &N, &M)) { + clear(); + build(N, arr_1); + build(N + M, arr_2); + printf("%.6lf\n", solve()); + } + return (0); +} diff --git a/uva_cpp_clean/1395/1395-10.cpp b/uva_cpp_clean/1395/1395-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aebf67d24308c442ef65730e07832a6ad7f95483 --- /dev/null +++ b/uva_cpp_clean/1395/1395-10.cpp @@ -0,0 +1,127 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e2+9; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; + +int n,m; + +vector> edges; + +int minimumSpanningTree(int i) { + + + ///sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + + int mi=1e9,ma=-1e9,ans=0; + + int weight; + int a,b; + for (int j=i;j>n>>m) { + if(n==0 && m==0) break; + + ///if(flag) cout<<"\n"; + + edges.clear(); + + for(int i=0;i>a>>b>>w; + + edges.push_back({w,a,b}); + } + + sort(edges.begin(), edges.end()); + + int res= minimumSpanningTree(0); + + if(res ==-1) cout<<-1<<"\n"; + else { + int ans = res; + for(int i=1;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 140 + Name: Bandwidth + Problem: https://onlinejudge.org/external/1/140.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +set nodes; +bool mat[200][200]; + +int bandwidth(char *s,int len){ + int l,i; + for(l=len-1;l>=1;l--) + for(i=0;i "< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() +#define coff ios_base::sync_with_stdio(0); + +#define ff first +#define se second +#define pb push_back +#define sz(a) ((int)a.size()) +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one. in long long use __builtin_popcountll(i) +#define parity(i) __builtin_parity(i) //evenparity 0 and odd parity 1 +#define btz(a) __builtin_ctz(a) //count binary trailling zero +#define un(v) ST(v), (v).erase(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +typedef long long ll; +typedef unsigned long long ull; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Debug +#define dbg(x) cout<<#x<<'='<>= 1; } return res; } +ll modInverse(ll a, ll m){return bigmod(a,m-2,m);} + +////============ CONSTANT ===============//// +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx 100010 +#define mod 1000000007 +////=====================================//// + +int main() +{ + coff; + + //input; + //output; + + string s; + while(cin>>s) + { + if(s[0]=='#') break; + int len=sz(s); + + int f=1,u,v,g[30][30],visit[30]; + string ord=""; + + cover(g,0); + cover(visit,0); + for(int i=0;irsek) + { + res=rsek; + result=ord; + } + } + while(next_permutation(ord.begin(),ord.end())); + + for(int i=0;i "<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1401 + Name: Remember the Word + Problem: https://onlinejudge.org/external/14/1401.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool H[400414]; +int DP[300313], D[400414][26], n; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string str, s; + for (int cse=1; cin >> str >> n; ++cse) { + int scnt = 0; + memset(D[scnt], 0, 26*sizeof(int)); + for (int i=0; i> s; + int st = 0; + for (int i=0; s[i]; ++i) { + int u = s[i]-'a'; + if (!D[st][u]) { + D[st][u] = ++scnt; + memset(D[scnt], 0, 26*sizeof(int)); + H[scnt] = 0; + } + st = D[st][u]; + } + H[st] = 1; + } + + DP[str.length()] = 1; + for (int i=str.length()-1; i>=0; --i) { + int st = DP[i] = 0; + for (int j=i; str[j]; ++j) { + st = D[st][str[j]-'a']; + if (!st) break; + if (H[st]) { + DP[i] += DP[j+1]; + if (DP[i] >= 20071027) + DP[i] %= 20071027; + } + } + } + + cout << "Case " << cse << ": " << DP[0] << '\n'; + } +} diff --git a/uva_cpp_clean/1407/1407-18.cpp b/uva_cpp_clean/1407/1407-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b610be1d8847e6e052b90f509da03cd6473bfbe6 --- /dev/null +++ b/uva_cpp_clean/1407/1407-18.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +using namespace std; //Programacion dinamica + +int main(){ + int DP[10007], t, N; + for(int i = 0; i <= 10000; i++) DP[i] = i; //Inicializar DP[] con los valores de sus indices + for(int i = 1; i <= 10000; i++){ //Buscar los numeros que elevados al cuadrado y se sumen den el numero deseado + for(int j = 1; j <= sqrt(i); j++){ //Por eso el limite que deben tener es la raiz de numero deseado + DP[i] = min(DP[i], DP[i - j*j] + 1); //Hallando la cantidad minima de los numeros a sumar + } + } + cin >> N; + while(N-- > 0){ + cin >> t; + cout << DP[t] << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/141/141-15.cpp b/uva_cpp_clean/141/141-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..351be541d949216369797075e102bad09a2eb1e2 --- /dev/null +++ b/uva_cpp_clean/141/141-15.cpp @@ -0,0 +1,63 @@ +/* + 模擬 + 時間複雜度: O(N^3) (2N筆輸入,每筆花費4N^2旋轉) +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, k, t, m, n, x, y, move, winner_id; + bitset<2500> temp, board; + char operator_; + + while (cin >> n && n) { + unordered_map, bool> seen; + m = 2 * n; + winner_id = 0; // 一開始沒人贏 + + for (i = 0; i < m; i++) { + cin >> x >> y >> operator_; + x--; + y--; + // 題目定義的操作 + if (operator_ == '+') + board[x * n + y] = 1; + else + board[x * n + y] = 0; + // 盤面出現過了,結束 + if (seen[board]) { + i++; + winner_id = (i & 1) + 1; // 誰贏 + move = i; // 在第幾局贏 + break; + } + // 四個方向都要記錄,最後要轉回原本的樣子 + for (t = 0; t < 4; t++) { + seen[board] = 1; + for (j = 0; j < n; j++) + for (k = 0; k < n; k++) + temp[(k + 1) * n - j - 1] = board[j * n + k]; + board = temp; + } + } + // 分出勝負之後把剩下資料讀完 + for (; i < m; i++) { + cin >> x >> y >> operator_; + } + + if (winner_id) + cout << "Player " << winner_id << " wins on move " << move << '\n'; + else + cout << "Draw\n"; + + // 清空盤面 + board.reset(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/141/141-21.cpp b/uva_cpp_clean/141/141-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a75908ee5e26d3771f5a2f9a449f917f73652f8c --- /dev/null +++ b/uva_cpp_clean/141/141-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 141 + Name: The Spot Game + Problem: https://onlinejudge.org/external/1/141.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +void rot(bool X[55][55], int n) { + for (int k=0; k> n && n) { + bool done = false; + for (int i=1; i<=2*n; ++i) { + cin >> r >> c >> ch; --r; --c; + if (done) continue; + + memcpy(S[i], S[i-1], sizeof(S[i])); + S[i][r][c] = ch=='+'; + // Optimization: count black cells, later compare only if different blacks + + for (int k=0; k<4 && !done; ++k) { + rot(S[i], n); + for (int j=1; j +using namespace std; + +int main() { + char grid[100][100]; + int tops[30]; + int x, y; + while (scanf("%d %d", &x, &y) && !(x == 0 && y == 0)) { + for (int i = 0; i < 30; i++) tops[i] = 0; + for (int i = 0; i < x; i++) scanf("%s", grid[i]); + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { + if (grid[i][j] != '.' && tops[grid[i][j] - 'A'] == 0) { + char c = grid[i][j]; + if (i + 1 < x && grid[i+1][j] == c){ + tops[c - 'A'] = 1; + int l = j; + for (; l < y && grid[i][l] == c; l++); + if (grid[i+1][l-1] == c && l > j + 1) { + int k = i + 1; + bool stop = false; + for (; k < x && grid[k][j] == c && !stop; k++) { + for (int m = j + 1; m < l; m++) { + if (grid[k][m] != c && grid[k][m] != '.') { + stop = true; + break; + } + } + } + if (stop || grid[k - 1][j + 1] != c) tops[c - 'A'] = 2; + } else tops[c - 'A'] = 2; + } else tops[c - 'A'] = 2; + } + } + } + for (int i = 0; i < 30; i++) if (tops[i] == 1) printf("%c", i + 'A'); + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1423/1423-17.cpp b/uva_cpp_clean/1423/1423-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da831ba1f73acfeb2bbd7791a033aebc9f17af3e --- /dev/null +++ b/uva_cpp_clean/1423/1423-17.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; +using VI = vector; +using VVI = vector; +using VD = vector; +using VVD = vector; +using VS = vector; +using VVS = vector; +using VB = vector; +using VVB = vector; +using PII = pair; +using PDD = pair; +using PDI = pair; +constexpr double pi = 3.1415926535897932385; +constexpr double EPS = 1e-12; + +struct Edge +{ + int to, weight; + Edge() {}; + Edge(int t, int w) : to(t), weight(w) {}; +}; + +bool bellman_ford(vector>& adj, double test) +{ + int n = adj.size(); + vector dist(n, 0); + vector in_queue(n, true); + vector bit(n, 1); + + queue q; + for (int i = 0; i < n; ++i) + q.push(i); + while (!q.empty()) + { + int cur = q.front(); + q.pop(); + in_queue[cur] = false; + for (Edge& edge : adj[cur]) + if (dist[cur] + edge.weight - test < dist[edge.to]) + { + dist[edge.to] = dist[cur] + edge.weight - test; + if (!in_queue[edge.to]) + { + if (++bit[edge.to] > n) + return true; + q.push(edge.to); + in_queue[edge.to] = true; + } + } + } + return false; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int T; + cin >> T; + while (T--) + { + int n; + cin >> n; + } + return 0; +} diff --git a/uva_cpp_clean/1428/1428-17.cpp b/uva_cpp_clean/1428/1428-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94a2173fee133bd1f057cccae1a7a814d1be7c4c --- /dev/null +++ b/uva_cpp_clean/1428/1428-17.cpp @@ -0,0 +1,95 @@ +/** + * @file 1428.cpp + * @author R-penguins + * @brief Online Judge 1428 Ping Pong + * @date 2021-04-04 + * + * Live Archive 4329 + * ICPC Beijing 2008 + * + * Solution with Binary Indexed Tree. O(n log n) + */ +#include +using namespace std; +using VI = vector; +using VVI = vector; +using VD = vector; +using VVD = vector; +using VS = vector; +using VVS = vector; +using VB = vector; +using VVB = vector; +using PII = pair; +using PDD = pair; +using PDI = pair; +constexpr double pi = 3.1415926535897932385; +constexpr double EPS = 1e-12; + +struct Bit +{ + Bit(int size) : tree(size + 1) { } + Bit(vector& arr) : tree(arr) + { + for (int i = 1; i < tree.size(); ++i) + { + int p = i + (i & -i); + if (p < tree.size()) + tree[p] += tree[i]; + } + } + int sum(int ind) + { + int ans = 0; + while (ind) + { + ans += tree[ind]; + ind -= (ind & -ind); + } + return ans; + } + void add(int ind, int val) + { + while (ind < tree.size()) + { + tree[ind] += val; + ind += (ind & -ind); + } + } + vector tree; +}; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int T; + cin >> T; + while (T--) + { + int N; + cin >> N; + VI a(N + 1); + for (int i = 1; i < N + 1; ++i) + cin >> a[i]; + vector a_index(N); + for (int i = 1; i < N + 1; ++i) + a_index[i - 1] = {a[i], i}; + sort(a_index.rbegin(), a_index.rend()); + Bit bit(N); + long long ans = 0; + int tot = 0; + for (PII& p : a_index) + { + int ind = p.second; + long long left_greater = bit.sum(ind - 1); + long long right_greater = tot - left_greater; + ans += (ind - 1 - left_greater) * right_greater; + ans += left_greater * ((N - ind) - right_greater); + bit.add(ind, 1); + ++tot; + } + cout << ans << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/1428/1428-21.cpp b/uva_cpp_clean/1428/1428-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6e4c20483ddc7cefa4bdc3bbdcd8df46b249c82 --- /dev/null +++ b/uva_cpp_clean/1428/1428-21.cpp @@ -0,0 +1,110 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1428 + Name: Ping pong + Problem: https://onlinejudge.org/external/14/1428.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 268435456 +#define LSB(i) ((i) & -(i)) +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ------------------------------------------------------------------------------ + + +int X[20143], Bl[400143], Br[400143]; +void init(int BIT[], int size) { + for (int i = 0; i < size; ++i) { + int j = i + LSB(i+1); + + if (j < size) + BIT[j] += BIT[i]; + } +} + +void update(int BIT[], int size, int i, int delta) { + while (i < size) { + BIT[i] += delta; + i += LSB(i+1); + } +} + +// [i .. j-1] +int getRange(int BIT[], int i, int j) { + int sum = 0; + while (j > i) { + sum += BIT[j-1]; + j -= LSB(j); + } + while (i > j) { + sum -= BIT[i-1]; + i -= LSB(i); + } + return sum; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + while (T--) { + int n = readUInt(), mx=0; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 143 + Name: Orchard Trees + Problem: https://onlinejudge.org/external/1/143.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define EPS 1e-9 +#define INF 1073741824 + +bool lineSegIntersect(double p0[], double p1[], double p2[], double p3[]) { + double ucrossv1 = ( p1[0] - p0[0] ) * ( p2[1] - p0[1] ) - ( p1[1] - p0[1] ) * ( p2[0] - p0[0] ); + double ucrossv2 = ( p1[0] - p0[0] ) * ( p3[1] - p0[1] ) - ( p1[1] - p0[1] ) * ( p3[0] - p0[0] ); + if (ucrossv1 * ucrossv2 > 0) return false; + + double vcrossu1 = ( p3[0] - p2[0] ) * ( p0[1] - p2[1] ) - ( p3[1] - p2[1] ) * ( p0[0] - p2[0] ); + double vcrossu2 = ( p3[0] - p2[0] ) * ( p1[1] - p2[1] ) - ( p3[1] - p2[1] ) * ( p1[0] - p2[0] ); + + return vcrossu1 * vcrossu2 <= 0; +} + +bool lineIntersect(double p0[], double p1[], double p2[], double p3[], double r[] ) { + double n[2]; n[0] = p3[1] - p2[1]; n[1] = p2[0] - p3[0]; + double denom = n[0] * ( p1[0] - p0[0] ) + n[1] * ( p1[1] - p0[1] ); + if (fabs( denom ) < EPS) return false; + + double num = n[0] * ( p0[0] - p2[0] ) + n[1] * ( p0[1] - p2[1] ); + double t = -num / denom; + r[0] = p0[0] + t * ( p1[0] - p0[0] ); + r[1] = p0[1] + t * ( p1[1] - p0[1] ); + return true; +} + +double p[3][2]; +int findLR(int y) { + double mnx = INF, mxx = -INF; + double ly0[] = {-1000, y}, + ly1[] = { 1000, y}, + r[2]; + + for (int i=0; i<3; i++) + if (lineSegIntersect(p[i], p[(i+1)%3], ly0, ly1) && lineIntersect(p[i], p[(i+1)%3], ly0, ly1, r)) { + if (r[0] < mnx) + mnx = r[0]; + + if (r[0] > mxx) + mxx = r[0]; + } + + return min(99.0, floor(mxx+EPS)) - max(1.0, ceil(mnx-EPS)) + 1; +} + +int main(){ + while (cin>>p[0][0]>>p[0][1]>>p[1][0]>>p[1][1]>>p[2][0]>>p[2][1]) { + bool nonz=0; + for (int i=0; !nonz && i<3; i++) + if (p[i][0] || p[i][1]) + nonz=1; + if (!nonz) break; + + int mny = max(1.0, ceil(min( min(p[0][1], p[1][1]), p[2][1]))), + mxy = min(99.0, floor(max( max(p[0][1], p[1][1]), p[2][1]))), + result = 0; + + for (int y=mny; y<=mxy; y++) + result += findLR(y); + + printf("%4d\n", result); + } +} diff --git a/uva_cpp_clean/144/144-21.cpp b/uva_cpp_clean/144/144-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1dfef29d77955b0bb57942e5fd111beaeb2d9e17 --- /dev/null +++ b/uva_cpp_clean/144/144-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 144 + Name: Student Grants + Problem: https://onlinejudge.org/external/1/144.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + int n, k; + while (cin >> n >> k && (n||k)) { + queue q; + for (int i=1; i<=n; ++i) + q.push(i); + + int X[43] = {}; + for (int s=0, i=1; !q.empty(); ) { + if (!s) { + s = i++; + if (i > k) + i = 1; + } + + int j = q.front(); q.pop(); + X[j] += s; + if (X[j] >= 40) { + s = X[j] - 40; + printf("%3d", j); + } + else { + q.push(j); + s = 0; + } + } + cout << endl; + } +} diff --git a/uva_cpp_clean/1449/1449-21.cpp b/uva_cpp_clean/1449/1449-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0042413a1cc028ecd42a4fa21c0bfe521918eb78 --- /dev/null +++ b/uva_cpp_clean/1449/1449-21.cpp @@ -0,0 +1,116 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1449 + Name: Dominating Patterns + Problem: https://onlinejudge.org/external/14/1449.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} +inline int readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return EOF; + + int l = 0; + str[l++] = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + str[l++] = ch; + str[l] = 0; + return l; +} + +// -------------------------------------------------------------------------- + +int lps[143]; +int kmp(const char str[], const char pat[]) { + int pl = 1; + for (int l=0; pat[pl]; ) + if (pat[pl] == pat[l]) + lps[pl++] = ++l; + else if (l > 0) + l = lps[l-1]; + else lps[pl++] = 0; + + int cnt = 0; + for (int i=0, j=0; str[i]; ) { + if (str[i] == pat[j]) { + ++i; ++j; + if (j == pl) { + ++cnt; + j = lps[j-1]; + } + } + else if (j > 0) + j = lps[j-1]; + else ++i; + } + return cnt; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int doms[155], n; + char pat[155][143], str[1000143]; + while ((n=readUInt())>0) { + for (int i=0; i a) { + a = x; + sz = 1; + doms[0] = i; + } + else if (x == a) + doms[sz++] = i; + } + + cout << a << '\n'; + for (int i=0; i +#include +#include +#include + +using namespace std; + +int cost[5][3] = { + 2, 10, 6, + 5, 25, 15, + 13, 53, 33, + 17, 87, 47, + 30, 144, 80}; + +int charge_time[5] = {0, 480, 1080, 1320, 1440}; // 因為有跨日所以切成4時段 + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + bitset<1440> used; + char charging_step; + string phone_number; + int a, b, c, d, i, j, time_begin, time_end; + + while (cin >> charging_step && charging_step != '#') { + int charge_categories[5] = {}; + + cin >> phone_number >> a >> b >> c >> d; + time_begin = a * 60 + b; + time_end = c * 60 + d; + + if (time_begin >= time_end) { // 跨日情形(0小時其實是24小時) + // 今天 + for (i = time_begin; i < 1440; i++) + used[i] = 1; + // 明天 + for (i = 0; i < time_end; i++) + used[i] = 1; + + } else + for (i = time_begin; i < time_end; i++) + used[i] = 1; + // 簡單算一下費用 + for (i = 0; i < 4; i++) { + for (j = charge_time[i]; j < charge_time[i + 1]; j++) + charge_categories[i] += used[j]; + charge_categories[4] += charge_categories[i] * cost[charging_step - 'A'][i % 3]; + } + + cout << setw(10) << phone_number + << setw(6) << charge_categories[1] + << setw(6) << charge_categories[2] + << setw(6) << charge_categories[3] + charge_categories[0] + << setw(3) << charging_step + << setw(8) << fixed << setprecision(2) << double(charge_categories[4]) / 100 + << '\n'; + + used.reset(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/145/145-21.cpp b/uva_cpp_clean/145/145-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9e026b3a0c6b6df2f29c6c457fb330548e64829 --- /dev/null +++ b/uva_cpp_clean/145/145-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 145 + Name: Gondwanaland Telecom + Problem: https://onlinejudge.org/external/1/145.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +double rates[3][5] = { + { 0.10, 0.25, 0.53, 0.87, 1.44 }, // 10 + { 0.06, 0.15, 0.33, 0.47, 0.80 }, // 4 + { 0.02, 0.05, 0.13, 0.17, 0.30 } // 10 +}; + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + char step, id[50]; + int h1, m1, h2, m2; + while (cin >> step >> id >> h1 >> m1 >> h2 >> m2) { + h1 = (h1 + 16) % 24; + h2 = (h2 + 16) % 24; + + if (h1*60+m1 >= h2*60+m2) + h2 += 24; + + int t1=0, t2=0, t3=0; + for (int t=h1*60+m1; t +/* +Problem: 146 - ID Codes +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=82 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); + +string code; + +int main () { + fastio; + + while ( cin >> code && (code != "#") ){ + + string first = code; + + next_permutation(code.begin(), code.end()); + sort(first.begin(), first.end()); + + cout << ((code == first)? "No Successor" : code) << '\n'; + + } + + return 0; +} +/* +Sample input:- +----------------- +abaacb +cbbaa +# + +Sample output:- +----------------- +ababac +No Successor + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/146/146-14.cpp b/uva_cpp_clean/146/146-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fbb341f5a35cb2b7fa8777c20f6d621052179992 --- /dev/null +++ b/uva_cpp_clean/146/146-14.cpp @@ -0,0 +1,40 @@ +/*************************************************** + * Problem name : 146 - ID Codes.cpp + * OJ : Uva + * Verdict : AC + * Date : 10.05.2017 + * Problem Type : Stirng(AdHoc) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define MAX 100000 +using namespace std; +typedef long long ll; +int main () { + string str; + while(cin >> str){ + if(str == "#") break; + if(next_permutation(str.begin(),str.end())){ + cout << str << endl; + } + else{ + cout << "No Successor" << endl; + } + } + return 0; +} + diff --git a/uva_cpp_clean/146/146-16.cpp b/uva_cpp_clean/146/146-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eff74b8648561a5765171fe86092e70a4a0fd39e --- /dev/null +++ b/uva_cpp_clean/146/146-16.cpp @@ -0,0 +1,24 @@ +//============================================================================ +// Name : 146 - ID Codes.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 146 ID Codes in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +#include +#include +#include +#include +using namespace std; +int main() { + char text[100]; + while (gets(text) && (text[0] != '#')) { + if (next_permutation(text, text + strlen(text))) { + cout << text << endl; + } else { + cout << "No Successor" << endl; + } + } + return 0; +} diff --git a/uva_cpp_clean/146/146-18.cpp b/uva_cpp_clean/146/146-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5a0f3099234ffd6c16c62d821bc0629da7b23cb0 --- /dev/null +++ b/uva_cpp_clean/146/146-18.cpp @@ -0,0 +1,14 @@ +#include +#include +#include + +using namespace std; + +int main(){ + string input; + while(getline(cin, input) && input != "#"){ + if(next_permutation(input.begin(), input.end())) cout << input << "\n"; + else printf("No Successor\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/146/146-19.cpp b/uva_cpp_clean/146/146-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75655cd0b1b56e468e270c582b40f493b710bd65 --- /dev/null +++ b/uva_cpp_clean/146/146-19.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; + +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + string str; + while (cin >> str && str != "#") { + int pos = -1, last = str.length() - 1; + for (int i = str.length() - 2; i >= 0; i--) { + if(str[i] < str[last]) { + pos = i; + break; + } + else if (str[i] > str[last]) last = i; + } + if (pos == -1) cout << "No Successor" << endl; + else { + int end = str.length() - 1; + for (; str[pos] >= str[end]; end--); + char c = str[end]; + str[end] = str[pos]; + str[pos] = c; + auto it = str.begin(); + for(int i = 0; i <= pos; i++, ++it); + sort(it, str.end()); + cout << str << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/146/146-20.cpp b/uva_cpp_clean/146/146-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c91657b9f69cefb7107bb18d6a14e3e90d1fef0 --- /dev/null +++ b/uva_cpp_clean/146/146-20.cpp @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +using namespace std; + +char code[55]; + +int main() +{ + while( gets(code) && strcmp(code, "#") ) + cout << (next_permutation(code, code+strlen(code)) ? code : "No Successor") << endl; + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/146/146-3.cpp b/uva_cpp_clean/146/146-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..727385fbdb657dc6b7e47b887fa1ed78dfcaa3d2 --- /dev/null +++ b/uva_cpp_clean/146/146-3.cpp @@ -0,0 +1,20 @@ +#include // next_permutation +#include // will be better due to string mod +#include // string + +using namespace std; + +int main () { + string sInput; + + while (true){ + getline ( cin, sInput ); + if ( sInput[0] == '#' ) return 0; + + if ( next_permutation(sInput.begin(), sInput.end())) + cout << sInput << endl; + else + cout << "No Successor" << endl; + } +} + diff --git a/uva_cpp_clean/146/146-5.cpp b/uva_cpp_clean/146/146-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d6947cf3c25bcc0f23f565c26dbe54b3047fd061 --- /dev/null +++ b/uva_cpp_clean/146/146-5.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include +using namespace std; +int main() +{ + char s[100]; + while(gets(s)) + { + if(s[0]=='#') + break; + int l=strlen(s); + if(next_permutation(s,s+l)) + puts(s); + else + printf("No Successor\n"); + + } + return 0; +} diff --git a/uva_cpp_clean/146/146-9.cpp b/uva_cpp_clean/146/146-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15ce3dcd8aff515d9416a4e0e0deafd464c07857 --- /dev/null +++ b/uva_cpp_clean/146/146-9.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +string s; + +int main(){ + while(cin >> s, s!="#"){ + if(next_permutation(s.begin(), s.end())) cout << s << endl; + else cout << "No Successor" << endl; + } + return(0); +} diff --git a/uva_cpp_clean/147/147-12.cpp b/uva_cpp_clean/147/147-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ff3b19416f3455c0d8e80dd28a594646026726c --- /dev/null +++ b/uva_cpp_clean/147/147-12.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) +#define PI(a,b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long +#define S second +#define F first +#define endl '\n' +#define FOR(var,len) for(var = 0;var=0) ? dp[i-arr[j]][j]:0; + y = (j>=1)?dp[i][j-1]:0; + dp[i][j] = x+y; + } + + while(cin>>tmp){ + if(tmp == 0.00)break; + num = (int)(tmp*100); + printf("%6.2f%17lld\n",tmp,dp[num][10]); + // cout< a; + a.insert_after(); +} \ No newline at end of file diff --git a/uva_cpp_clean/147/147-14.cpp b/uva_cpp_clean/147/147-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79f2ac1c8da1b98cecf109b308864e1156bb406a --- /dev/null +++ b/uva_cpp_clean/147/147-14.cpp @@ -0,0 +1,104 @@ +/*************************************************** + * Problem Name : 147 - Dollars.cpp + * Problem Link : https://uva.onlinejudge.org/external/1/147.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-02-06 + * Problem Type : dp + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("/home/saikat/Desktop/logN/input.txt", "r", stdin); +#define __FileWrite freopen ("/home/saikat/Desktop/logN/output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +ll coin[] = {5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000}; +ll ar[11][300003]; +ll fun (ll n) { + for (int i = 0; i < 11; i++) { + ar[i][0] = 1; + } + + for (int i = 5; i <= n; i++) { + ar[0][i] = 1; + } + + for (int i = 1; i < 11; i++) { + for (int j = 5; j <= n; j += 5) { + if (coin[i] > j) { + ar[i][j] = ar[i - 1][j]; + + } else { + ar[i][j] = (ar[i][j - coin[i]]) + ar[i - 1][j]; + } + } + } + + return ar[10][n]; +} +int main () { + __FastIO; + double num; + + while ( cin >> num ) { + if (num == 0.00) break; + + ll n = (ll) (num * 100.0 + .50); + ll ans = fun (n); + cout << right << setw (6) << setprecision (2) << fixed << num; + cout << right << setw (17) << ans << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/147/147-15.cpp b/uva_cpp_clean/147/147-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73e5b32a5f9acd10620d9e5563eae893eb1f290f --- /dev/null +++ b/uva_cpp_clean/147/147-15.cpp @@ -0,0 +1,27 @@ +/* + 找零錢問題 + 時間複雜度: O(N) +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + long long dp[6001] = {1}; + int i, j, x, y, index, currency[11] = {2000, 1000, 400, 200, 100, 40, 20, 10, 4, 2, 1}; + char point; + // 建好所有組合的表 + for (i = 0; i < 11; ++i) + for (j = currency[i]; j < 6001; ++j) + dp[j] += dp[j - currency[i]]; + + while (cin >> x >> point >> y && (x || y)) { + index = 20 * x + y / 5; // 避免浮點數誤差,最小是0.05所以乘20就好,陣列也只要開6000 + cout << setw(6) << fixed << setprecision(2) << double(index) / 20 << setw(17) << dp[index] << '\n'; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/147/147-19.cpp b/uva_cpp_clean/147/147-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b8ff660af00d06e0997ade26645366c0dc4c0ed --- /dev/null +++ b/uva_cpp_clean/147/147-19.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main() { + long long ways[30003]; + for (int i = 0; i < 30003; i++) ways[i] = 1; + for (int i = 10; i < 30003; i++) ways[i] += ways[i-10]; + for (int i = 20; i < 30003; i++) ways[i] += ways[i-20]; + for (int i = 50; i < 30003; i++) ways[i] += ways[i-50]; + for (int i = 100; i < 30003; i++) ways[i] += ways[i-100]; + for (int i = 200; i < 30003; i++) ways[i] += ways[i-200]; + for (int i = 500; i < 30003; i++) ways[i] += ways[i-500]; + for (int i = 1000; i < 30003; i++) ways[i] += ways[i-1000]; + for (int i = 2000; i < 30003; i++) ways[i] += ways[i-2000]; + for (int i = 5000; i < 30003; i++) ways[i] += ways[i-5000]; + for (int i = 10000; i < 30003; i++) ways[i] += ways[i-10000]; + for (int i = 0; i < 5; i++) ways[i] = 0; + int n, i, f; + while (scanf("%d.%d", &i, &f) != EOF && !(i == 0 && f == 0)) { + n = (i*100)+f; + printf("%3d.%02d%17lld\n", i, f, ways[n]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/147/147-20.cpp b/uva_cpp_clean/147/147-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7c89eda310b40710986c0d2ad16d02e5cd7fa87 --- /dev/null +++ b/uva_cpp_clean/147/147-20.cpp @@ -0,0 +1,24 @@ +#include + +const int SZ = 30005; +long nway[SZ]; +const int coin[] = {10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5}; + +int main() +{ + nway[0] = 1; + for(int i=0 ; i<11 ; i++) + { + int c = coin[i]; + for(int j=c ; j<30005 ; j++) nway[j] += nway[j-c]; + } + + int num1, num2; + + while( scanf("%d.%d", &num1, &num2)==2 && (num1 || num2) ) + { + int n = (num1*100)+num2; + printf("%3d.%.2d%17ld\n", num1, num2, nway[n]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/147/147-21.cpp b/uva_cpp_clean/147/147-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..762a8ae918fc9e6f772c7e8aa9f213c21602317f --- /dev/null +++ b/uva_cpp_clean/147/147-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 147 + Name: Dollars + Problem: https://onlinejudge.org/external/1/147.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +long long int ans[30001][11]; +int main(){ + int coins[11]={10000,5000,2000,1000,500,200,100,50,20,10,5}; + int i,j,k; + for(i=0;i<11;i++) + ans[0][i]=1; + + for(i=1;i<30001;i++) + for(k=0;k<11;k++){ + ans[i][k]=0; + for(j=0;j<=k;j++) + if(coins[j]<=i) + ans[i][k]+=ans[i-coins[j]][j]; + } + + double money; + while(scanf("%lf",&money)==1 && money>0) + printf("%6.2f%17lld\n",money,ans[(int)round(money*100)][10]); +} \ No newline at end of file diff --git a/uva_cpp_clean/147/147-5.cpp b/uva_cpp_clean/147/147-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..488b769fe085e1f976d903cc9ac3e223436c4ba9 --- /dev/null +++ b/uva_cpp_clean/147/147-5.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main () +{ + int coin [] = {5,10,20,50,100,200,500,1000,2000,5000,10000}; + long way [30010]; + memset (way,0,sizeof(way)); + way[0]=1; + for (int i=0;i<11;i++) + { + for (int j=coin[i];j<=30010;j++) + way [j]+=way[j-coin[i]]; + } + long a,b; + while (sc("%ld.%ld",&a,&b)) + { + long n=a*100+b; + if (n==0) + break; + pf("%3ld.%.2ld%17ld\n",a,b,way[n]); + + } + + return 0; +} diff --git a/uva_cpp_clean/147/147-9.cpp b/uva_cpp_clean/147/147-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb241500de87fe049c515288d6e1ecd5b052243a --- /dev/null +++ b/uva_cpp_clean/147/147-9.cpp @@ -0,0 +1,24 @@ +#include + +#define MAX_N 20 +#define MAX_W 300010 + +using namespace std; + +float n; +int v[]={5,10,20,50,100,200,500,1000,2000,5000,10000}; +long long memo[MAX_N][MAX_W]; + +long long ways(int id,int sum){ + if(sum==0) return 1; + if(id==11 || sum<0) return 0; + if(~memo[id][sum]) return memo[id][sum]; + return memo[id][sum] = ways(id+1,sum)+ways(id,sum-v[id]); +} + +int main(){ + memset(memo,-1,sizeof memo); + while(scanf("%f",&n),n!=0.00) + printf("%6.2f%17lld\n",n,ways(0,(int)(n*100+1e-2))); + return(0); +} diff --git a/uva_cpp_clean/148/148-9.cpp b/uva_cpp_clean/148/148-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fd503c9b367721c05de8998f3b505a4cadca12a --- /dev/null +++ b/uva_cpp_clean/148/148-9.cpp @@ -0,0 +1,73 @@ +#include + +#define SIZE 2010 +#define LEN 43 + +using namespace std; + +int ct, abc[LEN], pib[LEN], n, N, aux; +char v[SIZE][LEN], s[LEN], w[SIZE][LEN], vis[SIZE][LEN], ss[LEN], ans[SIZE][LEN], *p; + +int cmp(){ + bool more = false, less = false; + for(int it = 0; it < LEN; it++) + if(pib[it] < abc[it]) less = true; + else if( pib[it] > abc[it]) more = true; + if(more) return 1; + if(less) return -1; + return 0; +} + +bool isValid(int idx){ + for(int it = 0; it < n; it++) + if( strcmp(vis[it], v[idx]) == 0 ) return false; + return true; +} + +void backtrack(int idx){ + int d = cmp(); + if( d == 1 ) return; + if( d == 0 ){ + printf("%s =", s); + for(int it = 0; it < aux; it++) + printf(it != aux - 1 ? " %s" : " %s\n", ans[it]); + return; + } + if( idx == N ) return; + for(int it = idx; it < N; it++){ + for(int i = 0; w[it][i]; i++) + pib[w[it][i] - 'A']++; + strcpy(ans[aux++], w[it]); + backtrack(it + 1); + for(int i = 0; w[it][i] ;i++) + pib[w[it][i] - 'A']--; + aux--; + } +} + +int main(){ + while(scanf("%s", v[ct]), v[ct][0] != '#') ct++; + getchar(); + while(cin.getline(s, LEN), s[0] != '#'){ + strcpy(ss, s), n = 0; + p = strtok(ss, " "); + while(p){ + strcpy(vis[n++], p); + p = strtok(NULL, " "); + } + memset(abc, 0, sizeof abc); + for(int it = 0; s[it]; it++) + if( s[it] != ' ' ) + abc[s[it] - 'A']++; + N = aux = 0; + for(int it = 0; it < ct; it++){ + memset(pib, 0, sizeof pib); + for(int i = 0; v[it][i]; i++) + pib[v[it][i] - 'A']++; + if( cmp() <= 0 and isValid(it) ) strcpy(w[N++], v[it]); + } + memset(pib, 0, sizeof pib); + backtrack(0); + } + return(0); +} diff --git a/uva_cpp_clean/151/151-15.cpp b/uva_cpp_clean/151/151-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f858f2bb6e686ab388d0317d52d2ab862578fdc --- /dev/null +++ b/uva_cpp_clean/151/151-15.cpp @@ -0,0 +1,58 @@ +/* + 約瑟夫問題,加上需要迭代每一個m + 時間複雜度: O(NlogN) +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + int n, m, last_offline; + + while (cin >> n && n != 0) { + // 剛好差1直接有解 + if (n == 13) + cout << "1\n"; + else { + n--; // 總共有n個區域,編號1~n,,第一區一定最先停 + // 所以要求的區域變成2~n,轉換成n-1人的約瑟夫問題 + last_offline = 0; + m = 1; + // 經過轉換回左移2區,所以編號13變成編號11 + while (last_offline != 11) { + m++; + // 約瑟夫問題O(logN)解法,詳細在最下方 + last_offline = n * m - 1; + while (last_offline >= n) { + last_offline += (last_offline - n) / (m - 1) - n; + } + } + cout << m << '\n'; + } + } + return 0; +} + +/* +第n * m - 1次數到的區域就是最後一個斷電的區域,那就追蹤他之前每次被數到的時候來確定他的編號。 +假設n=5, m=3,一共有15次計數(每數m次停電一區,一共有n個區域,所以需要n*m次計數), +每一次數到的區域的編號如下: + +計數: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +區域: 0 1 2 3 4 0 1 3 4 1 3 1 3 3 3 + +設第p次(從0數起)數到的區域是y,令 p = m * a + b (0 <= b < m)。 +經過前p次計數,一共有 floor(p / m) = a 區停電,還剩下 n - a 區。 +如果y本次被數到之後沒有斷電,那麼他下次被數到是在其他 n - a 區都數過之後, +也就是第 q = p + n - a = n + (m - 1) * a + b 次計數。 +反推就能得到解 + +由y本次計數後還存活知 b < m - 1 ,故 a = floor((q - n) / (m - 1)), +p = q - (n - a) = q - n + floor((q - n) / (m - 1))。 + +我們要求最後一個停電的的區域,他是第 n * m - 1 次計數後斷電的, +用計算前驅的方式求出這個區域之前一次被數到是在什麼時候 +不斷地重複這一過程,直到知道他是第 k' (0 <= k' < n) 次被數到的人,那麼k'就是這個區域的編號。 +*/ \ No newline at end of file diff --git a/uva_cpp_clean/151/151-16.cpp b/uva_cpp_clean/151/151-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..488c455627f06b4b47cfc71a3b6f45a4e3b3b87f --- /dev/null +++ b/uva_cpp_clean/151/151-16.cpp @@ -0,0 +1,47 @@ +//============================================================================ +// Name : 151 - Power Crisis.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 151 Power Crisis in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +#include +//#include +//#include +using namespace std; +//#define FRI(i, a, b) for(i = a; i < b; ++i) + +int main() { + int n; + int ans[100] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 18, 10, 11, 7, 17, 11, 15, + 29, 5, 21, 13, 26, 14, 11, 23, 22, 9, + 73, 17, 42, 7, 98, 15, 61, 22, 84, 24, + 30, 9, 38, 15, 54, 27, 9, 61, 38, 22, + 19, 178, 38, 53, 79, 68, 166, 20, 9, 22, + 7, 21, 72, 133, 41, 10, 82, 92, 64, 129, + 86, 73, 67, 19, 66, 115, 52, 24, 22, 176, + 10, 57, 137, 239, 41, 70, 60, 116, 81, 79, + 55, 102, 49, 5, 22, 54, 52, 113, 15}; + //int i, n, m; + //list list; + while (cin >> n && n != 0) { + cout << ans[n] << endl; + /* m = 0; + do { + list.clear(); + FRI (i, 0, n) list.push_back(i); + while (list.size() > 1) { + list.pop_front(); + FRI (i, 0, m) { + list.push_back(list.front()); + list.pop_front(); + } + } + m++; + } while(list.front() != 12); + cout << m << endl;*/ + } + return 0; +} diff --git a/uva_cpp_clean/151/151-19.cpp b/uva_cpp_clean/151/151-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b698347c2a4781cc178f108b73eeccb68198de86 --- /dev/null +++ b/uva_cpp_clean/151/151-19.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main() { + int n; + int mask[101]; + while (scanf("%d", &n) && n != 0) { + for (int i = 0; i < n; i++) mask[i] = 0; + int sol = -1; + for (int l = 1; l < 100 && sol == -1; l++) { + int pos = 0; + for (int i = 0; i < n; i++) { + mask[pos] = l; + if (pos == 12) { + if (i == n - 1) sol = l; + break; + } + for (int j = 0; j < l; j++) { + do { + pos++; + pos%=n; + } while (mask[pos] == l); + } + } + } + printf("%d\n", sol); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/152/152-18.cpp b/uva_cpp_clean/152/152-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..499abb5cf44568b130f1d65f829a78802b30e976 --- /dev/null +++ b/uva_cpp_clean/152/152-18.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int trees[5001][3]; + int ans[10]; + int val = 0, minD; + while(true){ + cin >> trees[val][0] >> trees[val][1] >> trees[val][2]; + if(trees[val][0] == 0 && trees[val][1] == 0 && trees[val][2] == 0) break; + val++; + } + for(int i = 0; i < val; i++){ + minD = numeric_limits::max(); + for(int j = 0; j < val; j++){ + if(i == j) continue; + int dx = trees[i][0] - trees[j][0]; + int dy = trees[i][1] - trees[j][1]; + int dz = trees[i][2] - trees[j][2]; + int distance = sqrt(pow(dx, 2) + pow(dy, 2) + pow(dz, 2)); + if(distance < minD) minD = distance; + } + if(minD <= 9) ans[minD]+=1; + } + for(int i = 0; i < 10; i++) printf("%4d", ans[i]); + printf("\n"); + return 0 ; +} \ No newline at end of file diff --git a/uva_cpp_clean/152/152-21.cpp b/uva_cpp_clean/152/152-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ead3876447621e8c9a50c23483b637ef996d3b0 --- /dev/null +++ b/uva_cpp_clean/152/152-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 152 + Name: Tree's a Crowd + Problem: https://onlinejudge.org/external/1/152.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 5001 + +int n, px[MAXN], py[MAXN], pz[MAXN], dd[MAXN], rr[10]; +int dist2(int i, int j) { + int xx=px[i]-px[j], + yy=py[i]-py[j], + zz=pz[i]-pz[j]; + + return xx*xx + yy*yy + zz*zz; +} + +int main(){ + for (n=0; cin>>px[n]>>py[n]>>pz[n] && (px[n]||py[n]||pz[n]); n++) { + dd[n] = 1<<30; + for (int i=0; i +#include +#include +using namespace std; + +unordered_map > comb; +long long proc(int n, int m) { + if (m == 0 || m == n) return 1; + if (m < 0 || m > n) return 0; + if (comb[n][m] == 0) comb[n][m] = proc(n-1, m-1) + proc(n-1, m); + return comb[n][m]; +} + +int bucket[26]; + +long long proc(string & s, int p) { + if (p == s.length() - 1) return 1; + long long val = 0; + for (int i = 0; i < s[p] - 'a'; i++) { + if (bucket[i] > 0) { + bucket[i]--; + long long coef = 1, N = s.length() - 1 - p; + for (int j = 0; j < 26; j++) { + coef *= proc(N, bucket[j]); + N -= bucket[j]; + } + val += coef; + bucket[i]++; + } + } + bucket[s[p] - 'a']--; + val += proc(s, p+1); + bucket[s[p] - 'a']++; + return val; +} + +int main() { + string s; + while (cin >> s && s != "#") { + for (int i = 0; i < 26; i++) bucket[i] = 0; + for (int i = 0; i < s.length(); i++) bucket[s[i]-'a']++; + printf("%10lld\n", proc(s, 0)); + } + return 0; +} diff --git a/uva_cpp_clean/153/153-21.cpp b/uva_cpp_clean/153/153-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ba4506cd08a27178caaf659a86465da547bee33 --- /dev/null +++ b/uva_cpp_clean/153/153-21.cpp @@ -0,0 +1,68 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 153 + Name: Permalex + Problem: https://onlinejudge.org/external/1/153.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; +void multFactor(int R[], int x, int sgn) { + for (int i=x; i>1; --i) { + int p = i; + for (int j=0; primes[j] <= p; ++j) + while (p % primes[j] == 0) { + p /= primes[j]; + R[j] += sgn; + } + } +} + +int calc(int C[]) { + int R[10] = {}, sum = 0; + for (char ch='a'; ch<='z'; ++ch) + if (C[ch]) { + sum += C[ch]; + multFactor(R, C[ch], -1); + } + multFactor(R, sum, 1); + + int result = 1; + for (int i=0; i<10; ++i) + result *= pow(primes[i], R[i]); + return result; +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string str; + while (cin >> str && str != "#") { + int C[128] = {}; + for (char ch: str) + C[ch]++; + + int sum = 1; + for (int i=0; str[i]; ++i) { + for (char ch='a'; ch 0) { + C[ch]--; + sum += calc(C); + C[ch]++; + } + C[str[i]]--; + } + + cout << setw(10) << sum << endl; + } +} diff --git a/uva_cpp_clean/154/154-21.cpp b/uva_cpp_clean/154/154-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b803ddcf9da4583c617c3fbc305e3544470ec0c --- /dev/null +++ b/uva_cpp_clean/154/154-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 154 + Name: Recycling + Problem: https://onlinejudge.org/external/1/154.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int code(char c) +{ + switch(c) + { + case 'r':return 0; + case 'o':return 1; + case 'y':return 2; + case 'g':return 3; + case 'b':return 4; + } +} + +char b[101][5]; +int city; + + +int diff(int i, int j) +{ + int cnt=0; + for(int k=0; k<5; k++) + cnt += (b[i][k] != b[j][k]); + + return cnt; +} + +int solve() +{ + int i,j,t=0,tdiff,md; + md=100000; + for(i=0; i + +using namespace std; + +int t, ans, _max, ct[110]; +char s[110][50], a1, a2, a3, a4, a5, b1, b2, b3, b4, b5; +vector < pair > c[110]; + +void reset(){ + for(int i = 0; i < t; i++) + c[i].clear(), ct[i]=0; + t = _max = -1; +} + +void solve(){ + for(int i = 0; i < t; i++) + for(int j = 0; j < t; j++) + for(int k = 0; k < 5; k++) + for(int m = 0; m < 5; m++) + if(c[i][k].first == c[j][m].first && + c[i][k].second == c[j][m].second) + ct[i]++; + for(int i = 0; i < t; i++) + if(ct[i] > _max) _max = ct[i], ans = i + 1; + printf("%d\n", ans); + reset(); +} + +int main(){ + while(cin.getline(s[t], 100)){ + if(s[t][0] == 'e') solve(); + if(s[t][0] == '#') break; + sscanf(s[t], "%c/%c,%c/%c,%c/%c,%c/%c,%c/%c", &a1, &b1, &a2, &b2, &a3, &b3, &a4, &b4, &a5, &b5); + c[t].push_back( make_pair(a1, b1) ); + c[t].push_back( make_pair(a2, b2) ); + c[t].push_back( make_pair(a3, b3) ); + c[t].push_back( make_pair(a4, b4) ); + c[t].push_back( make_pair(a5, b5) ); + t++; + } + return(0); +} diff --git a/uva_cpp_clean/155/155-21.cpp b/uva_cpp_clean/155/155-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c30981e7761852697eb62b8b1190c6f9f84c6ec --- /dev/null +++ b/uva_cpp_clean/155/155-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 155 + Name: All Squares + Problem: https://onlinejudge.org/external/1/155.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int px, py; +int count (int tk, int cx, int cy) { + int k = tk >> 1, + c = (px>=cx-k && px<=cx+k && + py>=cy-k && py<=cy+k) ? 1:0; + if (k <= 1) return c; + + if (px < cx) + if (py < cy) + return c + count(k, cx-k, cy-k); + else return c + count(k, cx-k, cy+k); + else if (px > cx) + if (py < cy) + return c + count(k, cx+k, cy-k); + else return c + count(k, cx+k, cy+k); + return c; +} + +int main(){ + int k; + while (cin>>k>>px>>py && (k||px||py)) + printf("%3d\n", count(k<<1, 1024, 1024)); +} diff --git a/uva_cpp_clean/156/156-16.cpp b/uva_cpp_clean/156/156-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0db21b1de64aabd4d1030f6d56916fce8b6b2441 --- /dev/null +++ b/uva_cpp_clean/156/156-16.cpp @@ -0,0 +1,36 @@ +//============================================================================ +// Name : 156 - Ananagrams.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 156 Ananagrams in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +#include +#include +#include +#include +using namespace std; +#define FRI(i, a, b) for(i = a; i < b; ++i) + +int main() { + int i, j; + bool check[1005] = {0}; + string iter; + vector word, wsort; + set res; + for (i = 0; cin >> iter && iter != "#"; ++i) { + word.push_back(iter); + transform(iter.begin(), iter.end(), iter.begin(), ::tolower); + sort(iter.begin(), iter.end()); + FRI(j, 0, (int)wsort.size()) { + if (iter == wsort[j]) check[i] = check[j] = true; + } + wsort.push_back(iter); + } + FRI(i, 0, (int)word.size()) { + if (!check[i]) res.insert(word[i]); + } + for (__typeof((res).begin()) word=(res).begin();word!=(res).end();word++) cout << *word << endl; + return 0; +} diff --git a/uva_cpp_clean/156/156-17.cpp b/uva_cpp_clean/156/156-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58d4885be4e47bbfc155f2e61d97b3c1a306fce9 --- /dev/null +++ b/uva_cpp_clean/156/156-17.cpp @@ -0,0 +1,25 @@ +/** + * AOAPC II Example 5-4 Ananagrams + */ +#include +using namespace std; +int main() { + map trans; + string s; + while ((cin >> s) && s[0] != '#') { + string t = s; + for (char &c : t) + c = tolower(c); + sort(t.begin(), t.end()); + if (trans.find(t) == trans.end()) + trans[t] = s; + else + trans[t] = ""; + } + set ans; + for (auto p : trans) + if (p.second != "") + ans.insert(p.second); + for (string s : ans) + cout << s << "\n"; +} diff --git a/uva_cpp_clean/156/156-18.cpp b/uva_cpp_clean/156/156-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..dc5ac3853240b80f480f7564b9f84be5ab60f103 --- /dev/null +++ b/uva_cpp_clean/156/156-18.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +using namespace std; + +string toLowerAndSort(string word){ //Aplicar lowercase y organizar la palabra para evitar O(n!) + for(int i = 0; i < word.size(); i++) if(word[i] >= 'A' && word[i] <= 'Z') word[i] += 32; + sort(word.begin(), word.end()); + return word; +} + +int main(){ + map anagrams; + vector dic; + string line; + while(true){ + cin >> line; + if(line == "#") break; + dic.push_back(line); + anagrams[toLowerAndSort(line)] += 1; + } + sort(dic.begin(), dic.end()); + for(int i = 0; i < dic.size(); i++){ //Anagrama relativo == palabra que no tenga permutacion en el dic + if(anagrams[toLowerAndSort(dic[i])] == 1) printf("%s\n", dic[i].c_str()); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/156/156-21.cpp b/uva_cpp_clean/156/156-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d99637e91cbdb9179e4f19121df6a36885cb7c56 --- /dev/null +++ b/uva_cpp_clean/156/156-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 156 + Name: Ananagrams + Problem: https://onlinejudge.org/external/1/156.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +int val(char ch) { + if (ch>='A' && ch<='Z') + return ch-'A'; + + if (ch>='a' && ch<='z') + return ch-'a'; + + return -1; +} + +string encode(char w[]) { + char cnts[27]; + memset(cnts, 'a', sizeof(cnts)); + int last=-1; + for (int i=0; w[i]; i++) { + int v = val(w[i]); + if (v>=0) cnts[v]++; + if (v>last) last = v; + } + + cnts[last+1]=0; + return string(cnts); +} + + + +int main(){ + int n = 0; + map m; + map w; + + char word[30]; + for(;cin>>word && (word[1] || word[0]!='#'); n++) { + string e = encode(word); + m[e] = m[e] + 1; + w[string(word)] = e; + } + + for (auto i = w.begin(); i!=w.end(); i++) + if (m[i->second] == 1) + cout<< (i->first) < +using namespace std; +vector> g; +vector vis; +bool dfs(int i) +{ + vis[i] = -1; + for (int j = 0; j < 52; ++j) + if (g[i][j] && (vis[j] == -1 || !vis[j] && dfs(j))) + return true; + vis[i] = 1; + return false; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n; + while (cin >> n) + { + g = vector>(52, vector(52, false)); + while (n--) + { + string mol; + cin >> mol; + for (int i = 0; i < 8; i += 2) + { + if (mol[i] == '0') + continue; + int posInv = (mol[i] - 'A') * 2 + (mol[i + 1] == '+' ? 0 : 1); + for (int j = 0; j < 8; j += 2) + { + if (mol[j] == '0' || i == j) + continue; + int posJ = (mol[j] - 'A') * 2 + (mol[j + 1] == '+' ? 1 : 0); + g[posInv][posJ] = true; + } + } + } + bool cycle = false; + vis = vector(52, 0); + for (int i = 0; i < 52; ++i) + if (!vis[i] && dfs(i)) + { + cycle = true; + break; + } + cout << (cycle ? "unbounded" : "bounded") << "\n"; + } +} diff --git a/uva_cpp_clean/1583/1583-19.cpp b/uva_cpp_clean/1583/1583-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..480d12549510f5ff76833f6b96be9a68c8c5cdb8 --- /dev/null +++ b/uva_cpp_clean/1583/1583-19.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; + +int main() { + unordered_map m; + for (int i = 1; i <= 100000; i++) { + int val = i, auxi = i; + while (auxi) { + val += auxi%10; + auxi /= 10; + } + if (!m[val]) m[val] = i; + } + int T; + cin >> T; + while (T--) { + int n; + cin >> n; + printf("%d\n", m[n]); + } + return 0; +} diff --git a/uva_cpp_clean/1583/1583-20.cpp b/uva_cpp_clean/1583/1583-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9bc6c6d36d5de2e292cf5f05c00636b416fa3b7 --- /dev/null +++ b/uva_cpp_clean/1583/1583-20.cpp @@ -0,0 +1,34 @@ +#include + +int main() +{ + int tc; + + scanf("%d", &tc); + while( tc-- ) + { + int N; + scanf("%d", &N); + + bool flag = true; + for(int i=N-45 ; i<=N ; i++) + { + int temp = i, sum = 0; + while( temp!=0 ) + { + sum += temp%10; + temp /= 10; + } + + if( sum+i==N ) + { + printf("%d\n", i); + flag = false; + break; + } + } + + if( flag ) printf("0\n"); + } + return 0; +} diff --git a/uva_cpp_clean/1583/1583-21.cpp b/uva_cpp_clean/1583/1583-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa88f6fdee5aa7829597cd43f65a0996284ddf97 --- /dev/null +++ b/uva_cpp_clean/1583/1583-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1583 + Name: Digit Generator + Problem: https://onlinejudge.org/external/15/1583.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int D[100143]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int i=1; i<100143; ++i) { + int s = i; + for (int x=i; x; x/=10) + s += x % 10; + if (s<100143 && !D[s]) D[s] = i; + } + + + int T, n; + cin >> T; + while (T-- && cin>>n) + cout << D[n] << '\n'; +} diff --git a/uva_cpp_clean/1584/1584-21.cpp b/uva_cpp_clean/1584/1584-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cf1353f6390f01efbabeeb714d67c722c438c74 --- /dev/null +++ b/uva_cpp_clean/1584/1584-21.cpp @@ -0,0 +1,30 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1584 + Name: Circular Sequence + Problem: https://onlinejudge.org/external/15/1584.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + string str; + while (T-- && cin>>str) { + string best = str; + for (int i=1; i + +std::string sequence(std::string input, int x) +{ + std::string s = ""; + + for (unsigned int i = 0; i < input.length(); i++) + { + s += input[(x + i) % input.length()]; + } + + return s; +} + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + std::string input; + std::cin >> input; + + std::string min = input; + + for (unsigned int i = 1; i < input.length(); i++) + { + std::string seq = sequence(input, i); + + if (std::lexicographical_compare(seq.begin(), seq.end(), min.begin(), min.end())) + { + min = seq; + } + } + + std::cout << min << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1585/1585-15.cpp b/uva_cpp_clean/1585/1585-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..386f6b4ac032222ee3fb842a19f26a2f12221bdb --- /dev/null +++ b/uva_cpp_clean/1585/1585-15.cpp @@ -0,0 +1,30 @@ +/* + 模擬 +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int score, point_now, n; + char c; + cin >> n; + cin.ignore(1024, '\n'); + + while (n--) { + score = 0; + point_now = 1; + + while ((c = cin.get()) != '\n') + if (c == 'O') + score += point_now++; + else + point_now = 1; + + cout << score << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1585/1585-17.cpp b/uva_cpp_clean/1585/1585-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8750dd5d9519e2f64f21ff5203f505bfee4bd0c --- /dev/null +++ b/uva_cpp_clean/1585/1585-17.cpp @@ -0,0 +1,19 @@ +/** + * AOAPC II 3-1 + * I/O + * 3:17 AC + */ +#include +using namespace std; +int main() { + int T; + cin >> T; + while (T--) { + string s; + cin >> s; + int score = 0, cur = 0; + for (char c : s) + (c == 'O') ? score += ++cur : cur = 0; + cout << score << '\n'; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1585/1585-19.cpp b/uva_cpp_clean/1585/1585-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f04b4b54df9a730877a9317d3eec969c6a6c20b --- /dev/null +++ b/uva_cpp_clean/1585/1585-19.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main() { + int N; + cin >> N; + while (N--) { + string s; + cin >> s; + int sum = 0, acum = 1; + for (int i = 0; i < s.length(); i++) { + if (s[i] == 'O') sum += (acum++); + else acum = 1; + } + printf("%d\n", sum); + } + return 0; +} diff --git a/uva_cpp_clean/1585/1585-20.cpp b/uva_cpp_clean/1585/1585-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f749e1c5ce3504955e7d45b258b0144800e576c --- /dev/null +++ b/uva_cpp_clean/1585/1585-20.cpp @@ -0,0 +1,26 @@ +#include +#include + +char line[85]; + +int main() +{ + int tc; + char dump; + + scanf("%d%c", &tc, &dump); + while( tc-- ) + { + scanf("%s", line); + + int sum = 0, temp = 0; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1585 + Name: Score + Problem: https://onlinejudge.org/external/15/1585.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char word[100]; + int n; + cin>>n; + while(n--) { + cin>>word; + int sum=0, c=0; + for (int i=0; word[i]; i++) + if (word[i] == 'O') + sum += ++c; + else + c = 0; + + cout << sum << endl; + } +} diff --git a/uva_cpp_clean/1585/1585-4.cpp b/uva_cpp_clean/1585/1585-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69665125c2a1eca96929a9b2a45be6b2b32fb047 --- /dev/null +++ b/uva_cpp_clean/1585/1585-4.cpp @@ -0,0 +1,30 @@ +#include + +int main() +{ + int n; + std::cin >> n; + + for (int i = 0; i < n; i++) + { + std::string str; + std::cin >> str; + + int sum = 0, counter = 1; + + for (size_t j = 0; j < str.length(); j++) + { + if (str[j] == 'X') + { + counter = 1; + } + else + { + sum += counter; + counter++; + } + } + + std::cout << sum << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1586/1586-14.cpp b/uva_cpp_clean/1586/1586-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6a250ff467723621caad0f80d97df1349e953915 --- /dev/null +++ b/uva_cpp_clean/1586/1586-14.cpp @@ -0,0 +1,76 @@ +/*************************************************** + * Problem name : 1586 - Molar mass.cpp + * OJ : Uva + * Result : AC + * Date : 05-04-17 + * Problem Type : AD-Hoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 100 +using namespace std; +typedef long long ll; +double value(char ch) { + if (ch == 'O') return 16.000; + else if (ch == 'N') return 14.010; + else if (ch == 'C') return 12.010; + else if (ch == 'H') return 1.008; + +} +int main () { + int tc, sum; + //~ double O = 16.000, N = 14.010, C = 12.010, H = 1.008,M; + double M; + char str[MAX]; + scanf("%d", &tc); + int ar[MAX], cnt; + for (int t = 1; t <= tc; t++) { + cnt = 0; + scanf("%s", str); + sum = 0, M = 0.0; + int len = strlen(str); + for (int i = 0; i < len ; i++) { + if (isalpha(str[i])) { + ar[cnt++] = sum; + sum = 0; + } else { + sum = sum * 10 + str[i] - '0'; + if(i == len-1){ + ar[cnt++] = sum; + } + } + } + for (int i = 0, j = 1; i < len; i++) { + if (isalpha(str[i])) { + if (ar[j] == 0) { + M = M + value(str[i]) * 1.0; + j++; + } else { + M = M + value(str[i]) * ar[j++]; + } + } + } + //~ for(int i = 1; i +using namespace std; +int main() { + int T; + cin >> T; + cout << fixed << setprecision(3); + const map m{ {'C', 12.01}, {'H', 1.008}, {'O', 16}, {'N', 14.01} }; + while (T--) { + string s; + cin >> s; + double ans = 0, atom = 0; + int cur = 0; + bool one = false; + for (char c : s) { + if (isalpha(c)) { + ans += one ? atom : atom * cur; + atom = m.at(c); + cur = 0; + one = true; + } + else { + cur = cur * 10 + c - '0'; // + one = false; + } + } + ans += one ? atom : atom * cur; + cout << ans << '\n'; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1586/1586-21.cpp b/uva_cpp_clean/1586/1586-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fba785ce031c3dda6c628ed5c004160b48294137 --- /dev/null +++ b/uva_cpp_clean/1586/1586-21.cpp @@ -0,0 +1,42 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1586 + Name: Molar mass + Problem: https://onlinejudge.org/external/15/1586.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + string str; + while (T-- && cin>>str) { + double sum = 0; + for (int i=0; str[i]; ) { + double m; + switch (str[i++]) { + case 'C': m=12.01; break; + case 'H': m=1.008; break; + case 'O': m=16.00; break; + case 'N': m=14.01; break; + } + + int q = 0; + while (str[i]>='0' && str[i]<='9') { + q = q*10 + str[i]-'0'; + ++i; + } + sum += (q==0 ? 1 : q) * m; + } + cout << fixed << setprecision(3) << sum << '\n'; + } +} diff --git a/uva_cpp_clean/1586/1586-4.cpp b/uva_cpp_clean/1586/1586-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d547543f8933985d32acf4c4e30363b0619d7e52 --- /dev/null +++ b/uva_cpp_clean/1586/1586-4.cpp @@ -0,0 +1,69 @@ +#include +#include + +bool is_digit(char &x) +{ + return x >= '0' && x <= '9'; +} + +double mass(char &element, std::string &count_str) +{ + int count = 1; + + if (count_str.length() != 0) + { + count = std::stoi(count_str); + } + + switch (element) + { + case 'C': + return 12.01 * count; + case 'H': + return 1.008 * count; + case 'O': + return 16.00 * count; + case 'N': + return 14.01 * count; + default: + return 0; + } +} + +double mass(std::string &formula) +{ + double sum = 0; + char element; + std::string count; + + for (unsigned int i = 0; i < formula.length(); i++) + { + element = formula[i++]; + count = ""; + + while (i != formula.length() && is_digit(formula[i])) + { + count += formula[i]; + i++; + } + + sum += mass(element, count); + i--; + } + + return sum; +} + +int main() +{ + int tests; + std::cin >> tests; + + while (tests--) + { + std::string formula; + std::cin >> formula; + + std::cout << std::setprecision(3) << std::fixed << mass(formula) << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1587/1587-15.cpp b/uva_cpp_clean/1587/1587-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42d11a8587fb97cb833ad32acaa7ae184dedb296 --- /dev/null +++ b/uva_cpp_clean/1587/1587-15.cpp @@ -0,0 +1,56 @@ +/* + 立方體定義 +*/ +#include +#include +#include + +using namespace std; + +struct pallets { + int x, y; + pallets(int n, int m) { + x = min(n, m); + y = max(n, m); + } + bool operator<(const pallets& other) const { + if (x == other.x) + return y < other.y; + return x < other.x; + } +}; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int x, y; + bool possible; + + while (cin >> x >> y) { + vector plane; + plane.reserve(6); + plane.push_back(pallets(x, y)); + possible = true; + + for (int i = 0; i < 5; i++) { + cin >> x >> y; + plane.push_back(pallets(x, y)); + } + + sort(plane.begin(), plane.end()); + + for (int i = 0; i < 6; i += 2) + if (plane[i].x != plane[i + 1].x || plane[i].y != plane[i + 1].y) + possible = false; // 對面相等 + + if (plane[0].x != plane[2].x || plane[0].y != plane[4].x || plane[2].y != plane[4].y) + possible = false; // 臨邊相等 + + if (possible) + cout << "POSSIBLE\n"; + else + cout << "IMPOSSIBLE\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/1587/1587-21.cpp b/uva_cpp_clean/1587/1587-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..531cff924d7e6c248d6629e5a8f5ee830971b0c7 --- /dev/null +++ b/uva_cpp_clean/1587/1587-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1587 + Name: Box + Problem: https://onlinejudge.org/external/15/1587.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define PAIR pair +PAIR X[100]; + +bool eq(int x, PAIR p) { + return x == p.first || x == p.second; +} + +bool check0(PAIR p1, PAIR p2, PAIR p3) { + return (eq(p1.first, p2) && eq(p1.second, p3)) || (eq(p1.first, p3) && eq(p1.second, p2)); +} + +bool check() { + if (X[0] != X[1] || X[2] != X[3] || X[4] != X[5]) { + return false; + } + + return check0(X[0], X[2], X[4]) && check0(X[2], X[0], X[4]) && check0(X[4], X[0], X[2]); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> X[0].first >> X[0].second) { + for (int i=1; i<6; ++i) { + cin >> X[i].first >> X[i].second; + } + + for (int i=0; i<6; ++i) { + if (X[i].first > X[i].second) { + swap(X[i].first, X[i].second); + } + } + + sort(X, X+6); + cout << (check() ? "POSSIBLE" : "IMPOSSIBLE") << endl; + } +} diff --git a/uva_cpp_clean/1588/1588-21.cpp b/uva_cpp_clean/1588/1588-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3939911c935e70f94a163084a97eebf383829cdd --- /dev/null +++ b/uva_cpp_clean/1588/1588-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1588 + Name: Kickdown + Problem: https://onlinejudge.org/external/15/1588.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool isMatch(string &l1, string &l2, int shift) { + int l = min(l1.length(), l2.length()-shift); + for (int i=0; i> l1 >> l2) { + cout << min(bruteforce(l1, l2), bruteforce(l2, l1)) << endl; + } +} diff --git a/uva_cpp_clean/1589/1589-17.cpp b/uva_cpp_clean/1589/1589-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c0ae5d7d3ee272ef8aec089ce2ef11071b29964 --- /dev/null +++ b/uva_cpp_clean/1589/1589-17.cpp @@ -0,0 +1,165 @@ +/** + * AOAPC II Exercise 4-1 Xiangqi + * 87 WA + * 97 WA + * 107 AC + */ +#include +using namespace std; +struct Pos { + int x, y; + Pos(int x, int y) : x(x), y(y) {} +}; +void mark(vector board, Pos &p, vector> &mark) { + switch (board[p.x][p.y]) { + case 'G': + for (int r = p.x - 1; r >= 1; --r) { + if (r <= 3) + mark[r][p.y] = false; + if (board[r][p.y] != ' ') + break; + } + break; + case 'R': + for (int r = p.x - 1; r >= 1; --r) { + mark[r][p.y] = false; + if (board[r][p.y] != ' ') + break; + } + for (int r = p.x + 1; r <= 10; ++r) { + mark[r][p.y] = false; + if (board[r][p.y] != ' ') + break; + } + for (int c = p.y - 1; c >= 1; --c) { + mark[p.x][c] = false; + if (board[p.x][c] != ' ') + break; + } + for (int c = p.y + 1; c <= 9; ++c) { + mark[p.x][c] = false; + if (board[p.x][c] != ' ') + break; + } + break; + case 'H': + if (p.x - 2 >= 1 && board[p.x - 1][p.y] == ' ') { + if (p.y - 1 >= 1) + mark[p.x - 2][p.y - 1] = false; + if (p.y + 1 <= 9) + mark[p.x - 2][p.y + 1] = false; + } + if (p.x + 2 <= 10 && board[p.x + 1][p.y] == ' ') { + if (p.y - 1 >= 1) + mark[p.x + 2][p.y - 1] = false; + if (p.y + 1 <= 9) + mark[p.x + 2][p.y + 1] = false; + } + if (p.y - 2 >= 1 && board[p.x][p.y - 1] == ' ') { + if (p.x - 1 >= 1) + mark[p.x - 1][p.y - 2] = false; + if (p.x + 1 <= 10) + mark[p.x + 1][p.y - 2] = false; + } + if (p.y + 2 <= 9 && board[p.x][p.y + 1] == ' ') { + if (p.x - 1 >= 1) + mark[p.x - 1][p.y + 2] = false; + if (p.x + 1 <= 10) + mark[p.x + 1][p.y + 2] = false; + } + case 'C': + bool start = false; + for (int r = p.x - 1; r >= 1; --r) + if (board[r][p.y] != ' ') + if (!start) + start = true; + else { + mark[r][p.y] = false; + break; + } + else if (start) + mark[r][p.y] = false; + start = false; + for (int r = p.x + 1; r <= 10; ++r) + if (board[r][p.y] != ' ') + if (!start) + start = true; + else { + mark[r][p.y] = false; + break; + } + else if (start) + mark[r][p.y] = false; + start = false; + for (int c = p.y - 1; c >= 1; --c) + if (board[p.x][c] != ' ') + if (!start) + start = true; + else { + mark[p.x][c] = false; + break; + } + else if (start) + mark[p.x][c] = false; + start = false; + for (int c = p.y + 1; c <= 9; ++c) + if (board[p.x][c] != ' ') + if (!start) + start = true; + else { + mark[p.x][c] = false; + break; + } + else if (start) + mark[p.x][c] = false; + break; + } +} +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n, gx, gy; + while ((cin >> n >> gx >> gy) && n) { + vector board(11, string(10, ' ')); + vector pieces; + vector> oks(11, vector(10, true)); + char cc; + int cx, cy; + while (n--) { + cin >> cc >> cx >> cy; + board[cx][cy] = cc; + pieces.push_back(Pos(cx, cy)); + } + for (Pos p : pieces) + mark(board, p, oks); + bool check = false; + for (int r = gx; r <= 10; ++r) + if (board[r][gy] == 'G') { + check = true; + break; + } + else if (board[r][gy] != ' ') + break; + if (check) { + cout << "NO\n"; + continue; + } + if (gx - 1 >= 1 && oks[gx - 1][gy]) { + cout << "NO\n"; + continue; + } + if (gy + 1 <= 6 && oks[gx][gy + 1]) { + cout << "NO\n"; + continue; + } + if (gx + 1 <= 3 && oks[gx + 1][gy]) { + cout << "NO\n"; + continue; + } + if ((gy - 1) >= 4 && oks[gx][gy - 1]) { + cout << "NO\n"; + continue; + } + cout << "YES\n"; + } +} diff --git a/uva_cpp_clean/1590/1590-19.cpp b/uva_cpp_clean/1590/1590-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2a2d5841728f5ffa19de13a57af3b4a4c3ce063 --- /dev/null +++ b/uva_cpp_clean/1590/1590-19.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +long long toInt(long long b0, long long b1, long long b2, long long b3) { + return (b0 << 24) | (b1 << 16) | (b2 << 8) | (b3); +} + +long long MSOne(long long S) { + int p = 0; + while (S) { + S >>= 1; + p++; + } + return ((long long) 1) << p; +} + +long long getMask(long long ip1, long long ip2) { + long long x = ip1 ^ ip2; + long long mask = ~(0); + while (x) { + x >>= 1; + mask <<= 1; + } + return mask; +} + +int main() { + int m; + while (cin >> m) { + long long mask = ~(0), ip; + long long b0, b1, b2, b3; + char c; + m--; + cin >> b0 >> c >> b1 >> c >> b2 >> c >> b3; + ip = toInt(b0, b1, b2, b3); + while (m--) { + cin >> b0 >> c >> b1 >> c >> b2 >> c >> b3; + long long auxIp = toInt(b0, b1, b2, b3); + if (ip == (auxIp & mask)) continue; + mask = getMask(auxIp, ip);//&= ~(MSOne(ip ^ (auxIp & mask)) - 1); + ip &= mask; + } + printf("%lld.%lld.%lld.%lld\n", (ip >> 24)&255, (ip >> 16)&255, (ip >> 8)&255, ip&255); + printf("%lld.%lld.%lld.%lld\n", (mask >> 24)&255, (mask >> 16)&255, (mask >> 8)&255, mask&255); + } + return 0; +} diff --git a/uva_cpp_clean/1592/1592-17.cpp b/uva_cpp_clean/1592/1592-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f97e47b0bfb6ffeae9b8d0500415765afba36c5 --- /dev/null +++ b/uva_cpp_clean/1592/1592-17.cpp @@ -0,0 +1,48 @@ +/** + * AOAPC II Example 5-9 Database + */ +#include +using namespace std; +int cnt, n, m, c1, c2, r1, r2; +vector> db; +map IDs; +int id(string &s) { + if (IDs.find(s) != IDs.end()) + return IDs[s]; + return IDs[s] = cnt++; +} +bool pnf() { + for (c1 = 0; c1 < m; ++c1) + for (c2 = c1 + 1; c2 < m; ++c2) { + map checker; + for (r2 = 0; r2 < n; ++r2) { + long long num = db[r2][c1] * 1e5 + db[r2][c2]; + if (checker.find(num) != checker.end()) { + r1 = checker[num]; + return false; + } + checker[num] = r2; + } + } + return true; +} +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + while (cin >> n >> m) { + db = vector>(n); + cnt = 0; + IDs.clear(); + string cur; + getline(cin, cur); + for (auto &row : db) + for (int c = 0; c < m; ++c) { + (c == m - 1) ? getline(cin, cur) : getline(cin, cur, ','); + row.push_back(id(cur)); + } + if (pnf()) + cout << "YES\n"; + else + cout << "NO\n" << r1 + 1 << " " << r2 + 1 << "\n" << c1 + 1 << " " << c2 + 1 << "\n"; + } +} diff --git a/uva_cpp_clean/1593/1593-17.cpp b/uva_cpp_clean/1593/1593-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b7b848b1eb79c505f27bc2b9a48dff6830be9c2 --- /dev/null +++ b/uva_cpp_clean/1593/1593-17.cpp @@ -0,0 +1,35 @@ +/** + * AOAPC II Exercise 5-1 Alignment of Code + * ICPC NEERC 2010 + * 13 AC + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + vector len(190, 0); + vector> lines; + string line; + while (getline(cin, line)) + { + lines.push_back(vector()); + stringstream ss(line); + string word; + int i = 0; + while (ss >> word) + { + lines.back().push_back(word); + len[i] = max(len[i], (int) word.length()); + ++i; + } + } + cout << left; + for (auto &line : lines) + { + for (int i = 0; i < line.size() - 1; ++i) + cout << setw(len[i] + 1) << line[i]; + cout << line.back() << "\n"; + } +} diff --git a/uva_cpp_clean/1594/1594-17.cpp b/uva_cpp_clean/1594/1594-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e26b3772250fdcb22d9c76f946aececb3d7f1111 --- /dev/null +++ b/uva_cpp_clean/1594/1594-17.cpp @@ -0,0 +1,40 @@ +/** + * AOAPC II Exercise 5-2 Ducci Sequence + * ICPC Seoul 2009 + * 21 WA + * 24 AC + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int T; + cin >> T; + while (T--) + { + int n; + cin >> n; + deque nums(n); + for (int &i : nums) + cin >> i; + bool zeros = false; + for (int i = 0; i < 1005; ++i) + { + if (all_of(nums.begin(), nums.end(), [](int a){ return a == 0; })) + { + zeros = true; + break; + } + int bk = nums.back(); + adjacent_difference(nums.begin(), nums.end(), nums.begin(), [](int a, int b) + { + return abs(a - b); + }); + nums.push_back(abs(bk - nums.front())); + nums.pop_front(); + } + cout << (zeros ? "ZERO" : "LOOP") << "\n"; + } +} diff --git a/uva_cpp_clean/1595/1595-17.cpp b/uva_cpp_clean/1595/1595-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..559614b680b65cd8ad7f6924ab17286f964757bf --- /dev/null +++ b/uva_cpp_clean/1595/1595-17.cpp @@ -0,0 +1,34 @@ +/** + * AOAPC II Exercise 5-6 Symmetry + * ICPC Seoul 2004 + */ +#include +using namespace std; +int main() +{ + int T; + cin >> T; + while (T--) + { + int n; + cin >> n; + set> pts; + double sum = 0; + for (int i = 0; i < n; ++i) + { + int x, y; + cin >> x >> y; + pts.insert({ x, y }); + sum += x; + } + double mid = sum / n; + bool sym = true; + for (auto &pt : pts) + if (pt.first < mid && !pts.count({ mid + mid - pt.first, pt.second })) + { + sym = false; + break; + } + cout << (sym ? "YES" : "NO") << "\n"; + } +} diff --git a/uva_cpp_clean/1599/1599-17.cpp b/uva_cpp_clean/1599/1599-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4186b09b1bcf8834d04488a3179ed83018856f3b --- /dev/null +++ b/uva_cpp_clean/1599/1599-17.cpp @@ -0,0 +1,71 @@ +/** + * AOAPC II Example 6-20 Ideal Path + * ICPC NEERC 2010 + */ +#include +using namespace std; +struct Edge +{ + int u, c; + Edge(int u, int c) : u(u), c(c) { } +}; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n, m; + while (cin >> n >> m) + { + vector>> g(n + 1); + while (m--) + { + int a, b, c; + cin >> a >> b >> c; + g[a].push_back(make_shared(b, c)); + g[b].push_back(make_shared(a, c)); + } + vector deg(n + 1, -1); + deg[n] = 0; + queue q; + q.push(n); + int max_deg = numeric_limits::max(); + while (!q.empty()) + { + int cur = q.front(); + q.pop(); + if (deg[cur] >= max_deg) + break; + for (auto edge : g[cur]) + if (deg[edge->u] < 0) + { + deg[edge->u] = deg[cur] + 1; + if (edge->u == 1) + max_deg = deg[edge->u]; + q.push(edge->u); + } + } + vector ans; + vector cur_lv {1}; + for (int lv = max_deg; lv > 0; --lv) + { + int min_c = numeric_limits::max(); + for (int cur : cur_lv) + for (auto edge : g[cur]) + if (deg[edge->u] == lv - 1) + min_c = min(min_c, edge->c); + ans.push_back(min_c); + vector next_lv; + for (int cur : cur_lv) + for (auto edge : g[cur]) + if (deg[edge->u] == lv - 1 && edge->c == min_c) + { + next_lv.push_back(edge->u); + deg[edge->u] = -1; + } + cur_lv = next_lv; + } + cout << max_deg << "\n"; + for_each(ans.begin(), ans.end() - 1, [](int i){ cout << i << " "; }); + cout << ans.back() << "\n"; + } +} diff --git a/uva_cpp_clean/160/160-19.cpp b/uva_cpp_clean/160/160-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e550260d05fdbe99845ce017f2dbf0fcb729d35d --- /dev/null +++ b/uva_cpp_clean/160/160-19.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; +typedef unordered_map uii; + +ll sieve_size; +bitset<10000010> bs; +vi primes; + +void sieve(ll upperbound) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +uii primeFactors(ll n) { + uii factors; int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; if(c!=0) factors.insert(ii(p,c)); p=primes[++idx]; + } + if (n!=1) factors.insert(ii(n,1)); + return factors; +} + +vector fact; + +int main() { + sieve(101); + for (int i = 2; i <= 100; i++) fact.push_back(primeFactors(i)); + for (int i = 3; i <= 100; i++) { + uii prev = fact[i-3]; + for (auto it = prev.begin(); it != prev.end(); ++it) { + ii f = *it; + if (fact[i-2].count(f.first)) f.second += fact[i-2][f.first]; + fact[i-2][f.first] = f.second; + } + } + int n; + while (scanf("%d", &n) && n != 0) { + printf("%3d! =", n); + for (int i = 0, cont = 0; i < primes.size(); i++) { + if (fact[n-2].count(primes[i])) { + if(cont != 0 && cont % 15 == 0) printf("\n "); + printf(" %2d", fact[n-2][primes[i]]); cont++; + } + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/160/160-5.cpp b/uva_cpp_clean/160/160-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cef35b0fbee4ec71e27721b404f2f6966651a8b8 --- /dev/null +++ b/uva_cpp_clean/160/160-5.cpp @@ -0,0 +1,190 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + + +int pd [] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; + +mii mp; + +void fact(int n) +{ + for1(i,n) + { + int p=i,k=0; + while(p!=1 && k<25) + { + int c=0; + while(p%pd[k]==0) + { + p /= pd[k]; + c++; + } + mp[pd[k]]+=c; + k++; + } + } +} + +int main() +{ + int n; + while(si(n)==1) + { + if(n==0) break; + + if(n==1) + { + cout<se; + c++; + } + nl; + mp.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/160/160-9.cpp b/uva_cpp_clean/160/160-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3b9435936419aca3bbfa23da2bccb658ef2e5e6 --- /dev/null +++ b/uva_cpp_clean/160/160-9.cpp @@ -0,0 +1,42 @@ +#include + +#define SIZE 110 + +using namespace std; + +int N, Factors[SIZE][SIZE]; +bool is_prime[SIZE]; +vector prime; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + for(int it1 = 2; it1 < SIZE; it1++) + if(is_prime[it1]){ + for(int it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +int main(){ + sieve(); + for(int num = 2; num < SIZE; num++){ + int pib = num; + for(int it = 0; it < prime.size(); it++){ + int ct = 0; + while( pib % prime[it] == 0) pib /= prime[it], ct++; + Factors[num][it] = Factors[num - 1][it] + ct; + } + } + while(scanf("%d", &N), N){ + printf("%3d! =", N); + int ct = 0; + for(int it = 0; it < SIZE and Factors[N][it]; it++){ + ct++; + if( ct == 16 ) ct = 1, printf(" "); + printf(ct != 15 ? "%3d" : "%3d\n", Factors[N][it]); + } + if( ct != 15 ) puts(""); + } + return(0); +} diff --git a/uva_cpp_clean/1600/1600-17.cpp b/uva_cpp_clean/1600/1600-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61e8bad804fc8b45af18d207c7276d914c3e1d6a --- /dev/null +++ b/uva_cpp_clean/1600/1600-17.cpp @@ -0,0 +1,70 @@ +/** + * AOAPC II Exercise 6-5 Patrol Robot + * ICPC Hanoi 2006 + * 60 AC; + */ +#include +using namespace std; +struct Pt +{ + int x, y, dep = 0, cnt = 0; + Pt(int x, int y) : x(x), y(y) { } + Pt step(const Pt &dir) + { + Pt next(x + dir.x, y + dir.y); + next.dep = dep + 1; + return next; + } +}; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + const vector DIRS {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; + int kase; + cin >> kase; + while (kase--) + { + int m, n, k; + cin >> m >> n >> k; + vector> obs(m, vector(n)); + for (auto &row : obs) + for (int &pos : row) + cin >> pos; + bool found = false; + int ans = -1; + queue q; + q.push({0, 0}); + while (!q.empty() && ans == -1) + { + Pt cur = q.front(); + q.pop(); + for (const Pt &dir : DIRS) + { + Pt next = cur.step(dir); + if (0 <= next.x && next.x < m && 0 <= next.y && next.y < n) + { + int &status = obs[next.x][next.y]; + if (status == -1) + continue; + else if (status == 0) + { + status = -1; + next.cnt = 0; + } + else + next.cnt = cur.cnt + 1; + if (next.cnt > k) + continue; + if (next.x == m - 1 && next.y == n - 1) + { + ans = next.dep; + break; + } + q.push(next); + } + } + } + cout << ans << "\n"; + } +} diff --git a/uva_cpp_clean/1600/1600-21.cpp b/uva_cpp_clean/1600/1600-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef3ac98611141aca6ce755be6077d558f7779e4c --- /dev/null +++ b/uva_cpp_clean/1600/1600-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1600 + Name: Patrol Robot + Problem: https://onlinejudge.org/external/16/1600.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct State { + int i, j, k, d; + State(int i, int j, int k, int d):i(i),j(j),k(k),d(d){} +}; + +int dx[]={1,0,-1,0}, dy[]={0,1,0,-1}; +bool B[21][21], seen[21][21][21]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin>>T; + while (T--) { + int m, n, k; cin>>m>>n>>k; + for (int i=0; i> B[i][j]; + } + + + int result = -1; + queue q; + q.push(State(0, 0, 0, 0)); + while (!q.empty()) { + State s = q.front(); q.pop(); + if (s.i == m-1 && s.j == n-1) { + result = s.d; + break; + } + + for (int i=0; i<4; ++i) { + int ny=s.i+dy[i], nx=s.j+dx[i]; + if (ny>=0 && ny=0 && nx>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 161 + Name: Traffic Lights + Problem: https://onlinejudge.org/external/1/161.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + int X[143], n; + while (cin >> X[0] && X[0]) { + for (n=1; cin>>X[n] && X[n]; ++n); + + int Y[20000] = {}; + for (int i=0; i +#include +#include +#include + +#define SIZE 18001 + +void convert(int time) +{ + int h = time / 3600, m = (time / 60) % 60, s = time % 60; + + std::cout << std::setw(2) << std::setfill('0') << h << ":" << std::setw(2) << std::setfill('0') << m << ":" << std::setw(2) << std::setfill('0') << s << std::endl; +} + +int main() +{ + while (true) + { + int time; + std::cin >> time; + + if (time == 0) + { + break; + } + + std::vector times; + times.push_back(time); + + while (std::cin >> time && time != 0) + { + times.push_back(time); + } + + int timeline[SIZE], lights = times.size(); + memset(timeline, 0, SIZE * sizeof(int)); + + for (auto &time : times) + { + for (int i = 0; i < SIZE; i += 2 * time) + { + for (int j = i; j < i + time - 5 && j < SIZE; j++) + { + timeline[j]++; + } + } + } + + bool found = false; + + for (int i = 2 * times[0]; i < SIZE; i++) + { + if (timeline[i] == lights) + { + convert(i); + found = true; + break; + } + } + + if (!found) + { + std::cout << "Signals fail to synchronise in 5 hours" << std::endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/161/main.cpp b/uva_cpp_clean/161/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b079a2deaff0b74a59c62f5a2bad530ca795c402 --- /dev/null +++ b/uva_cpp_clean/161/main.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +using namespace std; + +vector intersectionOf(vector a, vector b){ + + vector result; + + for(int i = 0; i < a.size(); i++){ + for(int c = 0; c < b.size(); c++){ + if(a[i] == b[c]){ + result.push_back(a[i]); + break; + } + } + } + return result; +} + +void imp(vector a){ + for(int i = 0; i < a.size(); i++){ + cout << a[i] << ' '; + } + cout << endl; +} + +int main(){ + vector cycles; + int cycle; + vector intersect; + vector green_time; + + while(cin >> cycle){ + if(cycle != 0){ + cycles.push_back(cycle); + int aux = cycle - 5; + int min = 0; + while(min+aux <= 18000+aux){ + for(int i = 0; i < aux; i++){ + green_time.push_back(min); + min++; + } + min+=5+cycle; + } + if(cycles.size() == 1) intersect = green_time; + intersect = intersectionOf(intersect, green_time); + green_time.clear(); + }else{ + if(cycles.empty()){ + cin >> cycle; + break; + } + else{ + cycles.clear(); + + int h,m, s, first, sol = 0; + first = intersect[0]; + for(int i = 1; i < intersect.size(); i++){ + if(intersect[i]-first != 1){ + sol = intersect[i]; + break; + } + first = intersect[i]; + } + if(sol == 0) cout << "Signals fail to synchronise in 5 hours\n"; + else{ + m = sol / 60; + h = m/60; + m %= 60; + s = sol % 60; + printf("%02d:%02d:%02d\n",h, m, s); + } + intersect.clear(); + } + } + } +return 0; +} diff --git a/uva_cpp_clean/1610/1610-21.cpp b/uva_cpp_clean/1610/1610-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64064cdfcb557db4831b14f45e2ae6becbede5a5 --- /dev/null +++ b/uva_cpp_clean/1610/1610-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1610 + Name: Party Games + Problem: https://onlinejudge.org/external/16/1610.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +string makep(string r, int i) { + string s; + do { + int m = i % 27; + i /= 27; + if (m>0) { + s = string(1, 'A' + m - 1) + s; + } + } while(i>0); + return r + s; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, i, j; + string S[1143]; + while (cin >> n && n) { + for (int i=0; i> S[i]; + sort(S, S+n); + + string s1=S[(n>>1)-1], s2=S[n>>1], r; + if (s1 == s2) { + cout << s1 << endl; + continue; + } + + for (i=0; s1[i] && s1[i]==s2[i]; ++i) + r += s1[i]; + + if (s1.length() > s2.length() && i==s2.length()-1 && s1[i]==s2[i]-1) { + while(s1[i+1]=='Z') + r += s1[i++]; + } + + for (i=0; ;++i) { + string ss = makep(r, i); + if (s1<=ss && ss>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1613 + Name: K-Graph Oddity + Problem: https://onlinejudge.org/external/16/1613.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +char buf[50]; +void print(long long x) { + int i=48, neg=0; + buf[i--] = '\n'; + if (!x) buf[i--] = '0'; + else if (x < 0) { + x = -x; + neg = 1; + } + for (; x; x/=10) + buf[i--] = x%10 + '0'; + + if (neg) buf[i--] = '-'; + fwrite_unlocked(buf+i+1, 1, 48-i, stdout); +} + +void printNl() { + buf[0] = '\n'; + fwrite_unlocked(buf, 1, 1, stdout); +} + +// --------------------------------------------------------------------------- + +#define MAXQS 16384 +class FastQueue { + int data[MAXQS], l, r; + public: + FastQueue():l(0),r(0) {} + void clear() { l=r=0; } + bool empty() { return l==r; } + void push(int x) { data[r++] = x; r&=MAXQS-1; } + int pop() { int result = data[l++]; l&=MAXQS-1; return result; } + int front() { return data[l]; } + int size() { return r>=l ? r-l : MAXQS-l+r; } +}; + +int C[MAXQS]; +vector adj[MAXQS]; +int main() { + FastQueue q; + + int n, m; + bool frst = 1; + while ((n=readUInt())!=EOF) { + if (frst) frst = 0; + else printNl(); + + for (int i=0; i k) { + k = sz; + sr = i; + } + } + k |= 1; + + C[sr]=0; + q.clear(); + q.push(sr); + while (!q.empty()) { + int u = q.pop(), + nc = (C[u]+1) % k; + for (int v: adj[u]) + if (C[v]<0) { + C[v] = nc; + q.push(v); + } + else if (C[v] == C[u]) + C[v] = nc; + } + + print(k); + for (int i=0; i= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define all(v) ((v).begin()),((v).end()) +typedef long long ll; +typedef vector vi; +typedef vector vll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int card_to_int(string card) { + vector cards = {"D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DT", "DJ", "DQ", "DK", "DA", + "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CT", "CJ", "CQ", "CK", "CA", + "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HT", "HJ", "HQ", "HK", "HA", + "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "ST", "SJ", "SQ", "SK", "SA"}; + REP(i, (int) cards.size()) if (cards[i] == card) { + //cout << i << ' ' << cards[i] << el; + return i; + } + return -1; +} + +int trans(char c) { + if(c >= '0' && c <= '9') + return c-'0'; + if(c == 'A') return 1; + if(c == 'T') return 10; + if(c == 'J') return 11; + if(c == 'Q') return 12; + if(c == 'K') return 13; +} + +int main() { + char s[50]; + int card[52]; + int i; + while(true) { + for(i = 0; i < 52; i++) { + scanf("%s", s); + if(s[0] == '#') return 0; + card[i] = trans(s[1]); + } + queue A, B; + for(i = 51; i >= 0; i--) { + if(i%2) A.push(card[i]); + else B.push(card[i]); + } + int turn = 1;//0 A, 1 B + int judge = -1; + queue H;//heap + while(true) { + if(turn == 0 && A.empty()) { + judge = 0; + break; + } + if(turn == 1 && B.empty()) { + judge = 1; + break; + } + int CARD; + if(turn == 0) + CARD = A.front(), A.pop(); + else + CARD = B.front(), B.pop(); + //printf("%d - %c %d %d\n", CARD, turn+'A', A.size(), B.size()); + H.push(CARD); + turn = 1-turn; + int ended = 1; + while(CARD >= 11 || CARD == 1) { + ended = 0; + int paid; + if(CARD == 1) paid = 4; + else if(CARD == 11) paid = 1; + else if(CARD == 12) paid = 2; + else if(CARD == 13) paid = 3; + for(i = 0; i < paid; i++) { + if(turn == 0 && A.empty()) { + judge = 0; + break; + } + if(turn == 1 && B.empty()) { + judge = 1; + break; + } + if(turn == 0) + CARD = A.front(), A.pop(); + else + CARD = B.front(), B.pop(); + H.push(CARD); + //printf("%d - %c\n", CARD, turn+'A'); + if(CARD == 1 || CARD >= 11)//change + break; + } + if(judge >= 0) break; + turn = 1-turn; + } + if(judge >= 0) break; + if(ended == 0) { + if(turn == 0) { + while(!H.empty()) { + A.push(H.front()); + H.pop(); + } + } else { + while(!H.empty()) { + B.push(H.front()); + H.pop(); + } + } + } + } + printf("%d%3d\n", 2-judge, judge ? A.size() : B.size()); + } + return 0; +} + +//int main() { + //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + //string card; + //while (cin >> card) { + //if (card == "#") break; + //deque player, dealer, middle; + //bool p = true; + //int cover = 0, tmp = 0; + //player.push_front(card_to_int(card)); + //for (int i = 0; i < 51; i++) { + //cin >> card; + //if (i % 2 == 0) { + //dealer.push_front(card_to_int(card)); + //} else { + //player.push_front(card_to_int(card)); + //} + //} + + //while (true) { + //if (player.empty() && p || dealer.empty() && !p) + //break; + + //if (p) { + //tmp = player.front(); + //player.pop_front(); + //middle.push_front(tmp); + + //if (tmp > 10) { + //p = false; + //cover = tmp - 10; + //continue; + //} + + //if (cover > 0) { + //if (cover == 1) { + //cover = 0; + //while (!middle.empty()) { + //dealer.push_back(middle.back()); + //middle.pop_back(); + //} + //p = false; + //} else { + //cover--; + //} + //} else { + //p = false; + //} + //} else { + //tmp = dealer.front(); + //dealer.pop_front(); + //middle.push_front(tmp); + + //if (tmp > 10) { + //p = true; + //cover = tmp - 10; + //continue; + //} + + //if (cover > 0) { + //if (cover == 1) { + //cover = 0; + //while (!middle.empty()) { + //player.push_back(middle.back()); + //middle.pop_back(); + //} + //p = true; + //} else { + //cover--; + //} + //} else { + //p = true; + //} + //} + //} + + //if (player.size() == 0) { + //printf("%d%3d\n", 1, (int) dealer.size()); + //} else { + //printf("%d%3d\n", 2, (int) player.size()); + //} + //} + //return 0; +//} + diff --git a/uva_cpp_clean/162/162-21.cpp b/uva_cpp_clean/162/162-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b052e936f579a8f681500bbd9575682cad457f9 --- /dev/null +++ b/uva_cpp_clean/162/162-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 162 + Name: Beggar My Neighbour + Problem: https://onlinejudge.org/external/1/162.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include + +using namespace std; + + +int count(char num){ + if(num=='J' || !num)return 1; + if(num=='Q')return 2; + if(num=='K')return 3; + if(num=='A')return 4; + return 0; +} + +int main(){ + int i; + int pcs[2],tc,cp; + bool finish; + list::iterator gi; + char war,ch; + char cardd[5]; + while(1){ + finish=0; + list deck[2],table; + pcs[0]=pcs[1]=26;tc=0; + for(i=1;i<=52;i++) + { + scanf("%s",cardd); + if(cardd[0]=='#')return 0; + deck[i%2].push_back(cardd[1]); + } + war=0;cp=1; + + while(!finish){ + if(war && !count(ch)){ + deck[cp].splice(deck[cp].begin(),table,table.begin(),table.end()); + pcs[cp]+=tc; + tc=0; + war=0; + } + for(i=1;i<=count(war);i++){ + if(!pcs[cp]){ + finish=1; + break; + } + ch=deck[cp].back(); + deck[cp].pop_back(); + table.push_front(ch); + pcs[cp]--; + tc++; + if(count(ch)){ + war=ch; + break; + } + } + cp=(cp+1)%2; + } + printf("%d%3d\n",cp+1,pcs[cp]); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/1636/1636-20.cpp b/uva_cpp_clean/1636/1636-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f56fbdc8dd92eacceb13c3ac8c06d9b9b58ee033 --- /dev/null +++ b/uva_cpp_clean/1636/1636-20.cpp @@ -0,0 +1,25 @@ +#include +#include + +char line[105]; + +int main() +{ + while( gets(line) ) + { + int len = strlen(line), n = 0, counter = 0; + for(int i=0 ; i(n*n) ) printf("SHOOT\n"); + else if( (counter*len)<(n*n) ) printf("ROTATE\n"); + else if( (counter*len)==(n*n) ) printf("EQUAL\n"); + } + return 0; +} diff --git a/uva_cpp_clean/164/164-21.cpp b/uva_cpp_clean/164/164-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aaaeaae0a8fb31782e566b8db00d6432abea63f --- /dev/null +++ b/uva_cpp_clean/164/164-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 164 + Name: String Computer + Problem: https://onlinejudge.org/external/1/164.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +string s1, s2; +int DP[30][30], P[30][30], n, m; +void print(int i, int j) { + if (i==n && j==m) return; + switch (P[i][j]) { + case 'S': + print(i+1, j+1); + break; + + case 'C': + print(i+1, j+1); + cout << 'C' << s2[j] << setfill('0') << setw(2) << i+1; + break; + + case 'I': + print(i, j+1); + cout << 'I' << s2[j] << setfill('0') << setw(2) << i+1; + break; + + case 'D': + print(i+1, j); + cout << 'D' << s1[i] << setfill('0') << setw(2) << i+1; + break; + } +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> s1 >> s2 && s1!="#") { + n = s1.length(); + m = s2.length(); + + for (int i=0; i<=n; ++i) { + DP[i][m] = n-i; + P[i][m] = 'D'; + } + for (int j=0; j<=m; ++j) { + DP[n][j] = m-j; + P[n][j] = 'I'; + } + + for (int i=n-1; i>=0; --i) + for (int j=m-1; j>=0; --j) + if (s1[i] == s2[j]) { + DP[i][j] = DP[i+1][j+1]; + P[i][j] = 'S'; + } + else if (DP[i+1][j] < DP[i][j+1] && DP[i+1][j] < DP[i+1][j+1]) { + DP[i][j] = DP[i+1][j] + 1; + P[i][j] = 'D'; + } + else if (DP[i+1][j+1] < DP[i][j+1]) { + DP[i][j] = DP[i+1][j+1] + 1; + P[i][j] = 'C'; + } + else { + DP[i][j] = DP[i][j+1] + 1; + P[i][j] = 'I'; + } + + print(0, 0); cout << "E\n"; + } +} diff --git a/uva_cpp_clean/164/164-9.cpp b/uva_cpp_clean/164/164-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5dc51cb990d20e897563a36c00c19ad929fbdd18 --- /dev/null +++ b/uva_cpp_clean/164/164-9.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +const int W_DEL = 1, W_INS = 1, W_SUB = 1; + +const int MAX_LEN = 30; + +string s1, s2; +int dp[MAX_LEN][MAX_LEN]; + +struct Move { + char move, ch; + int pos; + Move () {} + Move (char move_, char ch_, int pos_): move(move_), ch(ch_), pos(pos_) {} +}; + +void stringAlignment () { + int m = s1.size(), n = s2.size(); + for (int i = 0; i <= n; i++) { + for (int j = 0; j <= m; j++) { + if (i == 0) dp[0][j] = j * W_INS; + else if (j == 0) dp[i][j] = i * W_DEL; + else if (s1[j - 1] == s2[i - 1]) dp[i][j] = dp[i - 1][j - 1]; + else dp[i][j] = min(dp[i - 1][j - 1] + W_SUB, min(dp[i - 1][j] + W_DEL, dp[i][j - 1] + W_INS)); + } + } + int i = n, j = m; + vector ans; + while (i or j) { + if (i and j and s1[j - 1] == s2[i - 1]) i--, j--; + else if (i and j and dp[i - 1][j - 1] + W_SUB == dp[i][j]) { + ans.push_back(Move('C', s2[i - 1], i)); + i--, j--; + } + else if (i and dp[i - 1][j] + W_DEL == dp[i][j]) { + ans.push_back(Move('I', s2[i - 1], i)); + i--; + } + else if (j and dp[i][j - 1] + W_INS == dp[i][j]) { + ans.push_back(Move('D', s1[j - 1], j)); + j--; + } + } + reverse(begin(ans), end(ans)); + int cnt_i = 0, cnt_d = 0; + for (Move ans_i: ans) { + if (ans_i.move == 'D') ans_i.pos = ans_i.pos + cnt_i - cnt_d; + cout << ans_i.move << ans_i.ch; + if (ans_i.pos < 10) cout << 0; + cout << ans_i.pos; + if (ans_i.move == 'I') cnt_i++; + if (ans_i.move == 'D') cnt_d++; + } + puts("E"); +} + +int main () { + while (cin >> s1 and s1[0] != '#' and cin >> s2) stringAlignment(); + return (0); +} diff --git a/uva_cpp_clean/1641/1641-4.cpp b/uva_cpp_clean/1641/1641-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86aeb421a4bedb5d4ec46183df3624d771d1aa4f --- /dev/null +++ b/uva_cpp_clean/1641/1641-4.cpp @@ -0,0 +1,41 @@ +#include + +int main() +{ + int h, w; + + while (std::cin >> h >> w) + { + int area = 0; + + for (int i = 0; i < h; i++) + { + std::string line; + std::cin >> line; + + bool inside = false; + + for (int i = 0; i < w; i++) + { + if (line[i] == '.' && inside) + { + area++; + } + else if (line[i] != '.') + { + if (inside) + { + inside = false; + area++; + } + else + { + inside = true; + } + } + } + } + + std::cout << area << std::endl; + } +} diff --git a/uva_cpp_clean/1644/1644-14.cpp b/uva_cpp_clean/1644/1644-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b29f89dd04ae7366d271115f911512fa52f70eec --- /dev/null +++ b/uva_cpp_clean/1644/1644-14.cpp @@ -0,0 +1,173 @@ +/*************************************************** + * Problem Name : 1644 Prime Gap.cpp + * Problem Link : https://onlinejudge.org/external/16/1644.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-06-25 + * Problem Type : Number Theory + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 2000005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +//~ vectorprime; +//~ char mark[MAX]; +//~ void sieve (int n) { +//~ int sq = sqrt (n) + 1; +//~ mark[0] = mark[1] = 1; + +//~ for (int i = 4; i < n; i += 2) { +//~ mark[i] = 1; +//~ } + +//~ for (int i = 3; i <= sq; i++) { +//~ for (int j = i * i; j < n; j += (2 * i) ) { +//~ mark[j] = 1; +//~ } +//~ } + +//~ for (int i = 1; i < n; i++) { +//~ if (!mark[i]) prime.pb (i); +//~ } +//~ } +int lp[MAX]; +vectorpr; +void linear_sieve (int N) { + for (int i = 2; i <= N; ++i) { + if (lp[i] == 0) { + lp[i] = i; + pr.pb (i); + } + + for (int j = 0; j < (int) pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; ++j) { + lp[i * pr[j]] = pr[j]; + } + } +} +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + //~ sieve (MAX); + linear_sieve (MAX - 3); + int n; + + while (cin >> n) { + if (n == 0) break; + + if (lp[n] == n) { + cout << 0 << "\n"; + + } else { + int lw = lower_bound (all (pr), n) - pr.begin(); + cout << pr[lw] - pr[lw - 1] << "\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/1644/1644-19.cpp b/uva_cpp_clean/1644/1644-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9227e1244d8e518af20c2145ca17fe0e09db589 --- /dev/null +++ b/uva_cpp_clean/1644/1644-19.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; + +ll sieve_size; +bitset<100000020> bs; +vi primes; + +inline void sieve(ll upperbound = 1300000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +int main() { + sieve(); + int n; + while (cin >> n && n != 0) { + if (bs[n]) printf("0\n"); + else { + int i = lower_bound(primes.begin(), primes.end(), n) - primes.begin(); + printf("%d\n", primes[i] - primes[i - 1]); + } + } + return 0; +} diff --git a/uva_cpp_clean/1644/1644-20.cpp b/uva_cpp_clean/1644/1644-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b566dcf6b7283ba3c563a3cdff71dad69b99a4b --- /dev/null +++ b/uva_cpp_clean/1644/1644-20.cpp @@ -0,0 +1,41 @@ +#include + +bool prime[1299713]; + +int main() +{ + prime[0] = prime[1] = false; + for(int i=2 ; i<1299713 ; i++) prime[i] = true; + + for(int i=2 ; i*i<1299713 ; i++) + if( prime[i] ) + for(int j=i+i ; j<1299713 ; j+=i) prime[j] = false; + + int k; + + while( scanf("%d", &k)==1 && k ) + { + if( prime[k] ) printf("0\n"); + else + { + int n; + for(int i=k-1 ; ; i--) + if( prime[i] ) + { + n = i; + break; + } + + int p; + for(int i=k+1 ; ; i++) + if( prime[i] ) + { + p = i; + break; + } + + printf("%d\n", p-n); + } + } + return 0; +} diff --git a/uva_cpp_clean/1644/1644-21.cpp b/uva_cpp_clean/1644/1644-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b14d5ab70f417d82bef593a4354c0f7cfdc8dc33 --- /dev/null +++ b/uva_cpp_clean/1644/1644-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1644 + Name: Prime Gap + Problem: https://onlinejudge.org/external/16/1644.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool isnp[1299710]; +int primes[100143], ins[1299710], pnt; +void sieve() { + isnp[0] = isnp[1] = 1; + for (int i=2; i<=1299709; ++i) + if (!isnp[i]) { + primes[pnt++] = i; + for (int j=i<<1; j<=1299709; j+=i) + isnp[j] = 1; + } +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + sieve(); + for (int j=2, k=-1; j<=1299709; ++j) { + if (!isnp[j]) ++k; + ins[j] = k; + } + + int n; + while (cin >> n && n) + if (!isnp[n]) cout << "0\n"; + else { + int ind = ins[n]; + cout << primes[ind+1]-primes[ind] << '\n'; + } +} diff --git a/uva_cpp_clean/1644/1644-4.cpp b/uva_cpp_clean/1644/1644-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d68d79d422516642f64f3545b9b17a4a0eccec0f --- /dev/null +++ b/uva_cpp_clean/1644/1644-4.cpp @@ -0,0 +1,57 @@ +#include + +#define SIZE 1299709 + 1 +bool composite[SIZE]; + +void seedPimes() +{ + for (int i = 2; i * i < SIZE; i++) + { + if (composite[i]) + { + continue; + } + + for (int j = i * i; j < SIZE; j += i) + { + composite[j] = true; + } + } +} + +int main() +{ + seedPimes(); + + int number; + while (std::cin >> number && number != 0) + { + if (!composite[number]) + { + std::cout << 0 << std::endl; + continue; + } + + int up = SIZE, down = 0; + + for (int i = number - 1; i >= 0; i--) + { + if (!composite[i]) + { + down = i; + break; + } + } + + for (int i = number + 1; i < SIZE; i++) + { + if (!composite[i]) + { + up = i; + break; + } + } + + std::cout << up - down << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/165/165-19.cpp b/uva_cpp_clean/165/165-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2da72a2a2fc47aae71173e4927b9726ad1600406 --- /dev/null +++ b/uva_cpp_clean/165/165-19.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +int h, k; +int sol[10], curr[10]; +int MAX; + +bool check(int i, int l, int L, int H) { + if (i < 0) return false; + if (i == 0) return true; + if (H < h) { + bool ret = check(i - curr[l], l, L, H + 1); + for (int d = l + 1; d <= L && !ret; d++) + ret = ret || check(i - curr[d], d, L, H + 1); + return ret; + } + return false; +} + +void proc(int maxi, int l) { + if (l == k) { + if (maxi > MAX) { + MAX = maxi; + for (int i = 0; i < k; i++) { + sol[i] = curr[i]; + } + } + } else { + for (int i = curr[l-1] + 1; i < maxi + 2; i++) { + curr[l] = i; + int j = maxi + 1; + for (; check(j, 0, l, 0); j++); + proc(j - 1, l + 1); + } + } +} + +int main() { + while (cin >> h >> k && !(h == 0 && k == 0)) { + curr[0] = 1; + MAX = 0; + proc(h, 1); + for (int i = 0; i < k; i++) { + printf("%3d", sol[i]); + } + printf(" ->%3d\n", MAX); + } + return 0; +} diff --git a/uva_cpp_clean/166/166-15.cpp b/uva_cpp_clean/166/166-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb5ec6c78914d9c3dee6ce0c313d236ea180ef47 --- /dev/null +++ b/uva_cpp_clean/166/166-15.cpp @@ -0,0 +1,60 @@ +/* + 找零錢問題的變種 + 時間複雜度: O(N^2) +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, k, x, backup, ans, count, temp, total, get_dp[101] = {}, currency[6] = {1, 2, 4, 10, 20, 40}, coin[6]; + double n; + // 建好所有組合的表,找錢時有無限多硬幣 + for (i = 1; i < 101; i++) { + backup = i, count = 0; + for (j = 5; j > -1 && backup; j--) + if (backup >= currency[j]) { + temp = backup / currency[j]; + count += temp; + backup -= (currency[j] * temp); + } + get_dp[i] = count; + } + + while (cin >> coin[0] >> coin[1] >> coin[2] >> coin[3] >> coin[4] >> coin[5] >> n) { + // 把除了 give_dp[0] 以外的值都設成最大值 2139062143 + int give_dp[3001] = {}; // 假設每種不超過500個 + memset(give_dp + 1, 127, sizeof(int) * 3000); + + x = (n + 1e-3) * 20; + + total = 0; + // 自己手上的錢總和 + for (i = 0; i < 6; i++) + total += coin[i] * currency[i]; + + // 6種錢幣,每種可以取 0 到 coin[i] 個,所以是01背包問題的一個變種 + // 在原來01背包問題上多一個執行 coin[i] 次的迴圈即可 + + for (i = 0; i < 6; i++) + for (j = 0; j < coin[i]; j++) + for (k = total; k >= currency[i]; k--) + give_dp[k] = min(give_dp[k], give_dp[k - currency[i]] + 1); + + ans = give_dp[x]; // 直接付清 + + // x最大是 5 * 20 = 100 + // 答案是付 i 元找 i - x 元的最小硬幣數 + for (i = x + 1; i < 101; i++) + ans = min(ans, give_dp[i] + get_dp[i - x]); + + cout << setw(3) << ans << '\n'; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/166/166-21.cpp b/uva_cpp_clean/166/166-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd94c0af54e9b0ed193d719e12b3aee97656e437 --- /dev/null +++ b/uva_cpp_clean/166/166-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 166 + Name: Making Change + Problem: https://onlinejudge.org/external/1/166.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int coins[] = {5, 10, 20, 50, 100, 200}, + changes[101], order[101]; +bool comp(int i, int j) { return changes[i]=0; i--) { + result += n / coins[i]; + n = n % coins[i]; + } + + return result; +} + +int naive(int n, int x[]) { + int res = 0; + for (int i=5; n && i>=0; i--) if (x[i]) { + int cc = min(x[i], n/coins[i]); + n -= cc * coins[i]; + res += cc; + } + return n ? 100 : res; +} + + +int main () { + for (int i=0; i<=100; i++) { + changes[i] = naive(5*i); + order[i] = i; + } + stable_sort(order, order+101, comp); + + int x[6]; float mm; + while (scanf("%d%d%d%d%d%d %f",x,x+1,x+2,x+3,x+4,x+5, &mm)==7) { + int m = mm * 100 + 0.5, t = 0; + for (int i=0; i<6; i++) + t += x[i]*coins[i]; + + int best = 100; + for (int i=0; i<=100; i++) { + int exi = order[i]; + if (changes[exi] >= best) break; + if (m + exi*5 <= t) + best = min(best, changes[exi] + naive(m + exi*5, x)); + } + + printf("%3d\n", best); + } +} + diff --git a/uva_cpp_clean/166/166-6.cpp b/uva_cpp_clean/166/166-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed7241f9b80e148aca3acf9f2da2eb2ba7c62d12 --- /dev/null +++ b/uva_cpp_clean/166/166-6.cpp @@ -0,0 +1,48 @@ +/* + dynamic programming > coin change + difficulty: medium + date: 15/May/2020 + problem: coin change with limited amount of coins + greedy coin change with unlimited amount of coins; I don't know why, but it works without memo + by: @brpapa +*/ +#include +using namespace std; +const int INF = 1 << 30; +const int C = 6; + +array coins = {5, 10, 20, 50, 100, 200}; +array qtyCoins; +int V; + +int greedy(int v) { + int cnt = 0; + for (int c = C-1; c >= 0; c--) + cnt += v/coins[c], v %= coins[c]; + return cnt; +} + +int solve(int c, int v, int qty) { + // moeda atual coins[c], soma acumulada v, uma quantidade qty de todas as moedas anteriores já foram usadas + + if (c == C) + return (v < V)? INF : (qty + greedy(v-V)); // + troco do vendedor + + int ans = INF; + for (int i = 0; i <= qtyCoins[c]; i++) + ans = min(ans, solve(c+1, v + i*coins[c], qty+i)); + return ans; +} + +int main() { + while (true) { + bool inv = true; + for (int &qty : qtyCoins) { cin >> qty; if (qty != 0) inv = false; } + if (inv) break; + + int a, b; scanf("%d.%d", &a, &b); + V = a*100 + b; + + printf("%3d\n", solve(0, 0, 0)); + } + return 0; +} diff --git a/uva_cpp_clean/166/166-9.cpp b/uva_cpp_clean/166/166-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c71d6f75aca9dd81d9e7c6490ac68ff5102efc3 --- /dev/null +++ b/uva_cpp_clean/166/166-9.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +const int N_COINS = 6, MAX_SUM = 1000, INF = 1e7; +const vector value = {5, 10, 20, 50, 100, 200}; + +int money, memo1[MAX_SUM][N_COINS], memo2[MAX_SUM][N_COINS]; +vector coin(N_COINS); + +int coinChange (int sum, int id) { + if (sum == 0) return 0; + if (id == N_COINS or sum >= MAX_SUM) return INF; + if (~memo1[sum][id]) return memo1[sum][id]; + int ret = INF; + for (int k = 0; value[id] * k <= sum; k++) { + ret = min(ret, k + coinChange(sum - value[id] * k, id + 1)); + } + return memo1[sum][id] = ret; +} + +int dp (int sum, int id) { + if (sum >= MAX_SUM) return INF; + if (id == N_COINS) { + if (sum < money) return INF; + return coinChange(sum - money, 0); + } + if (~memo2[sum][id]) return memo2[sum][id]; + int ret = INF; + for (int a = 0; a <= coin[id]; a++) { + ret = min(ret, a + dp(sum + value[id] * a, id + 1)); + } + return memo2[sum][id] = ret; +} + +bool read () { + int sum = 0; + for (int i = 0; i < N_COINS; i++) { + cin >> coin[i]; + sum += coin[i]; + } + if (sum == 0) return false; + int a, b; + scanf("%d.%d", &a, &b); + money = 100 * a + b; + return true; +} + +int main () { + memset(memo1, -1, sizeof memo1); + while (read()) { + memset(memo2, -1, sizeof memo2); + cout << setw(3) << dp(0, 0) << endl; + } + return (0); +} diff --git a/uva_cpp_clean/167/167-16.cpp b/uva_cpp_clean/167/167-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a82717ea4807ba0c9d4425ba4478af9f946064c --- /dev/null +++ b/uva_cpp_clean/167/167-16.cpp @@ -0,0 +1,57 @@ +//============================================================================ +// Name : 167 - The Sultan's Successors.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 167 The Sultan's Successors in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include +#include +using namespace std; + +#define SQUARE 8 + +int main() { + int n, sum, ans; + bool next; + int score[SQUARE][SQUARE]; + + cin >> n; + while (n--) { + int permutations[] = {0, 1, 2, 3, 4, 5, 6, 7}; + for (int i = 0; i < SQUARE; ++i) { + for (int j = 0; j < SQUARE; ++j) { + cin >> score[i][j]; + } + } + ans = 0; + do { + next = false; + for (int i = 0; i < SQUARE; ++i) { + for (int j = i + 1; j < SQUARE; ++j) { + int offset = j - i; + if (permutations[i] + offset == permutations[j] || permutations[i] - offset == permutations[j]) { + next = true; + i = j = SQUARE; + } + } + } + + if (next) { + continue; + } + + sum = 0; + for (int i = 0; i < SQUARE; ++i) { + sum += score[i][permutations[i]]; + } + if (sum > ans) { + ans = sum; + } + } while ( next_permutation(permutations, permutations + SQUARE) ); + cout << setw(5) << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/167/167-19.cpp b/uva_cpp_clean/167/167-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1109a1f28f8a43a61264f039f0ded82f3142968 --- /dev/null +++ b/uva_cpp_clean/167/167-19.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; + +int grid[8][8]; +int mask[8][8]; + +int maxi; + +void func(int sum, int row) { + if (row == 8) { + if (sum > maxi) maxi = sum; + return; + } + for (int i = 0; i < 8; i++) { + if (!mask[row][i]) { + for (int j = 0; j < 8; j++) { + mask[row][j]++; + mask[j][i]++; + if (i-row+j < 8 && i-row+j >= 0) mask[j][i-row+j]++; + if (i+row-j < 8 && i+row-j >= 0) mask[j][i+row-j]++; + } + func(sum + grid[row][i], row + 1); + for (int j = 0; j < 8; j++) { + mask[row][j]--; + mask[j][i]--; + if (i-row+j < 8 && i-row+j >= 0) mask[j][i-row+j]--; + if (i+row-j < 8 && i+row-j >= 0) mask[j][i+row-j]--; + } + } + } +} + +int main() { + int boards; + cin >> boards; + while (boards--) { + for (int i = 0; i < 8; i++) + for (int j = 0; j < 8; j++) + cin >> grid[i][j], mask[i][j] = 0; + maxi = 0; + func(0, 0); + printf("%5d\n", maxi); + } + return 0; +} diff --git a/uva_cpp_clean/167/167-3.cpp b/uva_cpp_clean/167/167-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73610a3a5d6e1b7d0c24e8183dae6af2b65b5b3a --- /dev/null +++ b/uva_cpp_clean/167/167-3.cpp @@ -0,0 +1,50 @@ +#include +#include + +using namespace std; + +// Variables: +// bitsets are used as bool flags for rows, left and right diagonals +// maxSum is the final answer, tempSum a temporary (possibly permanent) answer +bitset<15> row, ld, rd; +int maxSum, tempSum, cases, + answerArr[8], inputArr[8][8]; + + + +void backtracking (int c) +{ + if ( c == 8 ) + { + tempSum = 0; + for ( int l = 0; l < 8; l++ ) + tempSum += inputArr[l][answerArr[l]]; + if ( tempSum > maxSum ) + maxSum = tempSum; + } + for ( int r = 0; r < 8; r++ ) + { + if ( row[r] && ld[r-c+7] && rd[r+c] ) + { + row[r] = ld[r-c+7] = rd[r+c] = 0; + answerArr[c] = r; + backtracking(c+1); + row[r] = ld[r-c+7] = rd[r+c] = 1; + } + } +} + +int main() +{ + scanf("%d",&cases); + while ( cases-- ) + { + maxSum = 0; + row |= ld |= rd |= ( 1 << 16 ) - 1; + for ( int l = 0; l < 8; l++ ) + scanf("%d %d %d %d %d %d %d %d", &inputArr[l][0], &inputArr[l][1], &inputArr[l][2], &inputArr[l][3], &inputArr[l][4], &inputArr[l][5], &inputArr[l][6], &inputArr[l][7]); + backtracking(0); + printf("%5d\n", maxSum); + } +} + diff --git a/uva_cpp_clean/167/167-9.cpp b/uva_cpp_clean/167/167-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b93a439a2a55c4f4b6dab92a5401911fb94bc6ff --- /dev/null +++ b/uva_cpp_clean/167/167-9.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int t; +bool ok; +vector x, ans[200]; + +void f(int col){ + if(col == 8){ + ans[t++] = x; + return; + } + for(int i = 0; i < 8; i++){ + ok = true; + for(int j = 0; j < x.size(); j++) + if(x[j] == i || abs(col - j) == abs(x[j] - i)){ + ok = false; + break; + } + if(ok){ + x.push_back(i); + f(col + 1); + x.pop_back(); + } + } +} + +int main(){ + f(0); + int a[8][8]; + int maxi, sum,k; + cin >> k; + while(k--){ + maxi = 0; + for(int i = 0; i < 8; i++) + for(int j = 0; j < 8; j++) + scanf("%d", &a[i][j]); + for(int i = 0; i < t; i++){ + sum = 0; + for(int j = 0; j < 8; j++) + sum += a[j][ans[i][j]]; + maxi = max(sum, maxi); + } + printf("%5d\n", maxi); + } + + return(0); +} diff --git a/uva_cpp_clean/1673/1673-21.cpp b/uva_cpp_clean/1673/1673-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3c3879eed198bdcf5bfafd1d1438499a516c64f --- /dev/null +++ b/uva_cpp_clean/1673/1673-21.cpp @@ -0,0 +1,103 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1673 + Name: str2int + Problem: https://onlinejudge.org/external/16/1673.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +inline int readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return 0; + + int l = 0; + str[l++] = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + str[l++] = ch; + str[l] = 0; + return l; +} + +//--------------------------------------------------------------- + +#define MAXC 1000000043 +int *nxt, SC; +void insertW(char s[], long long cur=0) { + for (int i=0; s[i]; ++i) { + char d = s[i]-'0'; + long long ind = cur*10+d; + if (!nxt[ind]) { + nxt[ind] = ++SC; + if (SC >= MAXC) while(1); + memset(nxt+SC*10, 0, 40); + } + cur = nxt[ind]; + } +} + +int sumW(int x, long long cur=0) { + int sum = x; + for (int i=0; i<10; ++i) + if (nxt[cur*10+i]) + sum = (sum + sumW((x*10+i)%2012, nxt[cur*10+i])) % 2012; + + return sum; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + nxt = new int[MAXC*10]; + + int n; + char s[200043]; + while ((n=readUInt())!=EOF) { + memset(nxt, SC=0, 40); + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 168 + Name: Theseus and the Minotaur + Problem: https://onlinejudge.org/external/1/168.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +bool candles[27]; +int findNext(vector &ad, int t) { + for (int j=0; j adj[27]; + + while(cin.getline(line, 1000) && line[0]!='#') { + int k = 0; + for (int i=0; line[i]; i++) + if (line[i]!=' ' && line[i]!='\t') + line[k++] = line[i]; + line[k]=0; + if (k==0) continue; + if (line[0]=='#')break; + + + for (int i=0; i<27; i++) { + adj[i].clear(); + candles[i] = false; + } + + char m, t; + for(char *token=strtok(line, ";"); token; token=strtok(0, ";.")) + if (token[1]) { + if(token[1]==':') { + int f = token[0]-'A'; + for(int i=2; token[i]; i++) + if (token[i]>='A' && token[i]<='Z') + adj[f].push_back(token[i]-'A'); + } + else sscanf(token, " %c %c %d", &m, &t, &k); + } + m-='A'; + t-='A'; + + + bool done = false; + while(true) { + for (int i=0; i +using namespace std; +using ii=tuple; +using vii=vector; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + string s; + getline(cin,s); + if(s[0]=='#')break; + vii e; + vi m(128); + int n=0,i=0,u,v,w,k; + for(;;){ + char x=s[i],y; + if(!m[x])m[x]=++n; + u=m[x]-1; + i++; + for(;s[i]!=';'&&s[i]!='.';i++){ + if(s[i]==':')continue; + x=s[i]; + if(!m[x])m[x]=++n; + w=m[x]-1; + e.emplace_back(u,w); + } + if(s[i]==';')i++; + else{ + stringstream in(s.substr(i+1)); + in>>x>>y>>k; + u=m[x]-1; + w=m[y]-1; + break; + } + } + vi r(n); + for(int i=0;i<128;i++) + if(m[i]) + r[m[i]-1]=i; + vvi g(n); + for(ii x:e){ + tie(i,v)=x; + g[i].push_back(v); + } + vi t(n); + for(i=0;;i++){ + int z=u; + for(int v:g[u]) + if(v!=w&&!t[v]){ + u=v; + break; + } + w=z; + if(w==u)break; + if(i%k==k-1){ + t[w]=1; + cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 170 + Name: Clock Patience + Problem: https://onlinejudge.org/external/1/170.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char cards[] = "A23456789TJQK"; +int ranks[128]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=0; i<13; ++i) + ranks[cards[i]] = i; + + string X[52]; + while (cin >> X[0] && X[0]!="#") { + for (int i=1; i<52; ++i) + cin >> X[i]; + + stack st[13]; + for (int i=51; i>=0; --i) + st[(51-i) % 13].push(X[i]); + + string last; + int expo = 0, pos = 12; + while (!st[pos].empty()) { + last = st[pos].top(); + st[pos].pop(); + pos = ranks[last[0]]; + ++expo; + } + + printf("%02d,%s\n", expo, last.c_str()); + } +} diff --git a/uva_cpp_clean/1707/1707-21.cpp b/uva_cpp_clean/1707/1707-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac7f36ff4e6828fe79a4702f795ab29f491db7e4 --- /dev/null +++ b/uva_cpp_clean/1707/1707-21.cpp @@ -0,0 +1,104 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1707 + Name: Surveillance + Problem: https://onlinejudge.org/external/17/1707.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// -------------------------------------------------------------------------- + +#define MAXN 2000170 +int n, A[MAXN], B[MAXN], J[23][MAXN]; + +int main() { + while ((n=readUInt()) != EOF) { + int k = readUInt(), + ln = log2(k)+1, + n2 = (n<<1); + memset(J[0], 0, n2*sizeof(int)); + J[0][n2] = n2; + + for (int i=0; i ev; + + int lst = -1; + for (int i=0; i lst) { + ev.push_back(i); + lst = J[0][i]; + } + J[0][i] = lst>i ? lst : 0; + } + + for (int j=1; j<=ln; ++j) + for (int i=0; i<=n2; ++i) + J[j][i] = J[j-1][ J[j-1][i] ]; + + int result = MAXN; + for (int s: ev) if (J[ln][s]-s>=n) { + int pos = s, + cnt = 0; + for (int j=ln; j>=0; --j) + if (J[j][pos]-s < n) { + pos = J[j][pos]; + cnt |= 1<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1709 + Name: Amalgamated Artichokes + Problem: https://onlinejudge.org/external/17/1709.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +unordered_map S, C; +double SIN(int x) { + auto it = S.find(x); + if (it != S.end()) + return it->second; + + return S[x] = sin(x); +} + +double COS(int x) { + auto it = C.find(x); + if (it != C.end()) + return it->second; + + return C[x] = cos(x); +} + + + +int n; +double X[1000143], M[1000143], p, a, b, c, d; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(9); + + while (cin >> p >> a >> b >> c >> d >> n) { + for (int i=1; i<=n; ++i) + X[i] = p*(sin(a*i + b) + cos(c*i + d)); + + M[n] = X[n]; + for (int i=n-1; i>0; --i) + M[i] = min(X[i], M[i+1]); + + double result = 0; + for (int i=1; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1717 + Name: Ship Traffic + Problem: https://onlinejudge.org/external/17/1717.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline char readNonBlankChar() { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return EOF; + return ch; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-') + if (ch == EOF) return EOF; + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +// ---------------------------------------------------------------- + +struct Event { + double time; + bool tp; + Event(double t, bool tp):time(t),tp(tp){}; + bool operator < (const Event &o) const { return time < o.time; } +}; + +int main() { + cout << fixed << setprecision(9); + + int n; + while ((n=readUInt()) >= 0) { + double + w = readUInt(), // lane width + u = readUInt(), // ships speed + v = readUInt(), // ferry speed + t1 = readUInt(), + t2 = readUInt(), + lct = w / v; // lane cross time + + vector vec; + for (int i=0; i= 0) { + double st = -offset + p / u - lct, + ed = -offset + (p+l) / u; + + if ((st>=t1 && st<=t2) || (ed>=t1 && ed<=t2)) { + vec.push_back(Event(st, 1)); + vec.push_back(Event(ed, 0)); + } + } + } + } + + vec.push_back(Event(t2, 1)); + sort(vec.begin(), vec.end()); + double d = vec[0].time - t1; + + int inq = 0; + for (int i=0, sz=vec.size(); i+1>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1726 + Name: Automatic Cheater Detection + Problem: https://onlinejudge.org/external/17/1726.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +inline bool readBool() { + char ch; + while ((ch=readchar())!='c' && ch!='i'); + return ch == 'c'; +} + +// --------------------------------------------------------------- + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + while (T--) { + int Q = readUInt(); + + int D1[10] = {}, D2[10] = {}; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1727 + Name: Counting Weekend Days + Problem: https://onlinejudge.org/external/17/1727.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; +char daysn[7][4] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; +char montn[12][4]= { + "JAN", "FEB", "MAR", "APR", "MAT", "JUN", + "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" +}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + map did, mid; + for (int i=0; i<7; ++i) + did[daysn[i]] = i; + + for (int i=0; i<12; ++i) + mid[montn[i]] = i; + + int T; + cin >> T; + string m, d; + while (T-- && cin>>m>>d) { + int mn = mid[m], dn = did[d], c=0; + for (int i=0; i= 5) + ++c; + cout << c << '\n'; + } +} diff --git a/uva_cpp_clean/1729/1729-21.cpp b/uva_cpp_clean/1729/1729-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8633a751741d99bd8e7159d40208af61043820c9 --- /dev/null +++ b/uva_cpp_clean/1729/1729-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1729 + Name: Owllen + Problem: https://onlinejudge.org/external/17/1729.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + char str[100143]; + for (int cse=1; cse<=T; ++cse) { + cin >> str; + int F[26] = {}; + for (int i=0; str[i]; ++i) + F[str[i]-'a']++; + + int mn = F[0]; + for (int i=1; i<26; ++i) + mn = min(F[i], mn); + + cout << "Case " << cse << ": " << mn << endl; + } +} diff --git a/uva_cpp_clean/1738/1738-21.cpp b/uva_cpp_clean/1738/1738-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3c205ed30ac041a81ef8b14d7af1e4bf49d57d4 --- /dev/null +++ b/uva_cpp_clean/1738/1738-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 1738 + Name: Ceiling Function + Problem: https://onlinejudge.org/external/17/1738.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct Tree { + int value; + Tree *left; + Tree *right; +}; + +Tree *add(Tree *t, int x) { + if (t == NULL) { + t = new Tree(); + t->value = x; + return t; + } + + if (t->value > x) { + t->left = add(t->left, x); + } else { + t->right = add(t->right, x); + } + return t; +} + +void treeType(Tree *t, int &b, int &x) { + ++x; + if (t == NULL) { + b |= 1 << x; + return; + } + + treeType(t->left, b, x); + treeType(t->right, b, x); +} + +int n, k; +Tree *T[43]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n >> k) { + set types; + + for (int i=0; i> x; + T = add(T, x); + } + + int b = 0, x = -1; + treeType(T, b, x); + types.insert(b); + } + + cout << types.size() << endl; + } +} diff --git a/uva_cpp_clean/174/174-21.cpp b/uva_cpp_clean/174/174-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..866503017d3908faca4e897c80fbd098a978ed41 --- /dev/null +++ b/uva_cpp_clean/174/174-21.cpp @@ -0,0 +1,136 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 174 + Name: Strategy + Problem: https://onlinejudge.org/external/1/174.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +string keywords[] = { + "IF", "THEN", "ELSE", + "AND", "OR", "NULL", + "MY", "YOUR", "LAST", "1", "2", + "TRADE", "CHEAT" +}; + +int skips[127]; +int pos; +string tokens; + + +int Mem[2][2], cu; +int evalMemory() { + char c = tokens[pos]; + pos += skips[c] + skips['L']; + char n = tokens[pos++] - '0' - 1; + + return Mem[c=='M' ? cu : 1-cu][n]; +} +bool evalCond() { + int m = evalMemory(); + char ch = tokens[pos++]; + + char tc = tokens[pos]; + int tst = tc == 'N' ? 0 + : tc == 'T' ? 1 + : 2; + pos += skips[tc]; + return ch=='=' ? m == tst : m != tst; +} +bool evalCondition() { + bool result = evalCond(); + while (1) { + char ch = tokens[pos]; + if (ch != 'A' && ch != 'O') + return result; + + pos += skips[ch]; + if (ch == 'A') + result &= evalCondition(); + else result |= evalCondition(); + } +} +int evalStatement() { + char ch = tokens[pos]; + pos += skips[ch]; + switch (ch) { + case 'T': return 1; + case 'C': return 2; + case 'I': + bool cond = evalCondition(); + pos += 4; // THEN + int t1 = evalStatement(); + pos += 4; // ELSE + int t2 = evalStatement(); + return cond ? t1 : t2; + } + throw "Syntax Error"; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=0; i<13; ++i) + skips[keywords[i][0]] = keywords[i].length(); + + + vector programs; + string line, str; + while (cin >> str) { + line += str; + if (line[line.length()-1] == '.') { + programs.push_back(line); + line = ""; + } + } + + int scores[20] = {}; + for (int i=0; i +using namespace std; + +int *curr, *nextc, *auxi; + +int main() { + curr = new int[30]; + nextc = new int[30]; + int m, n; + while (cin >> m >> n) { + for (int i = 0; i < 30; i++) + curr[i] = 1 << i, nextc[i] = 0; + for (int i = 0; i < m; i++) { + char a, b; + cin >> a >> b; + curr[a-'a'] |= 1 << (b-'a'); + } + bool change = true; + while (change) { + change = false; + for (int i = 0; i < 30; i++) { + nextc[i] = curr[i]; + for (int j = 0; j < 30; j++) { + if ((curr[i]>>(j)) & 1) { + nextc[i] |= curr[j]; + } + } + change = change || nextc[i] != curr[i]; + } + auxi = nextc; + nextc = curr; + curr = auxi; + } + for (int i = 0; i < n; i++) { + string s1, s2; + cin >> s1 >> s2; + bool ok = false; + if (s1.length() == s2.length()) { + ok = true; + for (int j = 0; j < s1.length() && ok; j++) { + if (!((curr[s1[j]-'a'] >> (s2[j]-'a')) & 1)) ok = false; + } + } + if (ok) printf("yes\n"); + else printf("no\n"); + } + } + delete[] nextc; + delete[] curr; + return 0; +} diff --git a/uva_cpp_clean/183/183-21.cpp b/uva_cpp_clean/183/183-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b096a94177a1b64b79c7ca711fe32bf3c95fc820 --- /dev/null +++ b/uva_cpp_clean/183/183-21.cpp @@ -0,0 +1,112 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 183 + Name: Bit Maps + Problem: https://onlinejudge.org/external/1/183.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int n, m; +char tp, line[100000], buf[110], X[300][300]; +int enc(char *res, int r1, int c1, int r2, int c2) { + if (r1==r2 || c1==c2) return 0; + if (r2-r1==1 && c2-c1==1) { + res[0] = X[r1][c1]; + return 1; + } + + int mr = (r1+r2+1)>>1, + mc = (c1+c2+1)>>1, + s = 1; + + res[0] = 'D'; + s += enc(res+s, r1, c1, mr, mc); + s += enc(res+s, r1, mc, mr, c2); + s += enc(res+s, mr, c1, r2, mc); + s += enc(res+s, mr, mc, r2, c2); + + if (s<=5) { + for (int i=2; i>1, + mc = (c1+c2+1)>>1; + + int s = 1; + s += dec(ind + s, r1, c1, mr, mc); + s += dec(ind + s, r1, mc, mr, c2); + s += dec(ind + s, mr, c1, r2, mc); + s += dec(ind + s, mr, mc, r2, c2); + return s; +} + + +int main() { + cin.getline(buf, 100); + while (buf[0]!='#' && sscanf(buf, "%c%d%d", &tp, &n, &m)==3) { + if (tp == 'B') { + int k=0; + while (cin.getline(buf, 100)) { + if (buf[1]==' ' && (buf[0]=='B' || buf[0]=='D')) break; + for (int i=0; buf[i]; i++) + line[k++] = buf[i]; + } + line[k] = 0; + + + for (int i=0; i + +using namespace std; + +const int SIZE = 40005; + +int R, C, idx; +char ch, s[SIZE], line[SIZE], ans[SIZE]; + +bool is_uniform(int r, int c, int idr, int idc){ + char ch_ = line[idr * C + idc]; + for(int row = idr; row < idr + r; row++) + for(int col = idc; col < idc + c; col++) + if( line[row * C + col] != ch_ ) return false; + return true; +} + +void printQuarter(int r, int c, int idr, int idc){ + char ch_ = line[idx]; + for(int row = idr; row < idr + r; row++) + for(int col = idc; col < idc + c; col++) + ans[row * C + col] = ch_; +} + +void B_to_D(int r, int c, int idr, int idc){ + if( r == 0 or c == 0 ) return; + if( is_uniform(r, c, idr, idc) ){ + ans[++idx] = line[idr * C + idc]; + return; + } + ans[++idx] = 'D'; + B_to_D( (r + 1) / 2, (c + 1) / 2, idr, idc); + B_to_D( (r + 1) / 2, c / 2, idr, idc + (c + 1)/ 2); + B_to_D( r / 2, (c + 1) / 2, idr + (r + 1) / 2, idc); + B_to_D( r / 2, c / 2, idr + (r + 1) / 2, idc + (c + 1)/ 2); +} + +void D_to_B(int r, int c, int idr, int idc){ + if( idx >= R * C ) return; + if( r == 0 or c == 0 ) return; + if( line[++idx] != 'D'){ + printQuarter(r, c, idr, idc); + return; + } + D_to_B( (r + 1) / 2, (c + 1) / 2, idr, idc); + D_to_B( (r + 1) / 2, c / 2, idr, idc + (c + 1)/ 2); + D_to_B( r / 2, (c + 1) / 2, idr + (r + 1) / 2, idc); + D_to_B( r / 2, c / 2, idr + (r + 1) / 2, idc + (c + 1)/ 2); +} + +void printAnswer(){ + int it; + for(it = 0; ans[it]; it++){ + if( it % 50 == 0 and it ) putchar('\n'); + putchar(ans[it]); + } + it--; + if( it % 50 != 0 or it == 0 ) + putchar('\n'); +} + +int main(){ + cin.getline(s, SIZE); + sscanf(s, "%c %d %d", &ch, &R, &C); + while(true){ + line[0] = '\0'; + while(cin.getline(s, SIZE)){ + if( s[0] == 'B' or ( s[0] == 'D' and s[1] == ' ' ) ) break; + strcat(line, s); + } + if( ch == 'B' ){ + idx = -1; + printf("D%4d%4d\n", R, C); + B_to_D(R, C, 0, 0); + ans[++idx] = '\0'; + printAnswer(); + } + else{ + printf("B%4d%4d\n", R, C); + idx = -1; + D_to_B(R, C, 0, 0); + ans[R * C] = '\0'; + printAnswer(); + } + if( sscanf(s, "%c %d %d", &ch, &R, &C) != 3 ) break; + } + return(0); +} diff --git a/uva_cpp_clean/186/186-21.cpp b/uva_cpp_clean/186/186-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cec5d110ffc6184c6d3f01ddad7bf378e169abb --- /dev/null +++ b/uva_cpp_clean/186/186-21.cpp @@ -0,0 +1,92 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 186 + Name: Trip Routing + Problem: https://onlinejudge.org/external/1/186.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +int n, D[143][143], P[143][143]; +string names[143], rid[143][143]; +unordered_map ids; +int readId(char delim=',') { + if (getline(cin, names[n], delim)) { + auto r = ids.emplace(names[n], n); + if (r.second) ++n; + return r.first->second; + } + return -1; +} + +void printFlight(int i, int j) { + cout << setw(20) << left << names[i] << ' ' + << setw(20) << left << names[j] << ' ' + << setw(10) << left << rid[i][j]<< ' ' + << setw( 5) << right<< D[i][j] << endl; +} + +void printPath(int u, int v) { + if (P[u][v] == u) { + printFlight(u, v); + return; + } + + printPath(u, P[u][v]); + printPath(P[u][v], v); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=0; i<143; ++i) { + for (int j=0; j<143; ++j) { + D[i][j] = INF; + P[i][j] = i; + } + D[i][i] = 0; + } + + while (cin.peek()!='\n') { + int u = readId(), + v = readId(), + d; string sid; + getline(cin, sid, ','); + cin >> d; + if (d < D[u][v]) { + rid[u][v] = rid[v][u] = sid; + D[u][v] = D[v][u] = d; + } + cin.ignore(100, '\n'); + } + cin.ignore(100, '\n'); + + for (int k=0; k>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 188 + Name: Perfect Hash + Problem: https://onlinejudge.org/external/1/188.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +unsigned int w[20], n; +unsigned int nextC(int c) { + for (int d=1; d + +using namespace std; + +long long n, ct, w[1000], pib; +char s[1000]; +bool ok; + +void reset(){ + for(int i = 0; i < n; i++) + w[i] = 0; + n = 0, ct = 1; +} + +int main(){ + while(cin.getline(s, 1000)){ + reset(); + for(int i = strlen(s) - 1; i >= 0; i--){ + if(s[i] == ' '){ + n++, ct = 1; + while(s[i] == ' ') i--; + i++; + continue; + } + w[n] += (s[i] - 'a' + 1) * ct; + ct <<= 5; + } + n++; + for(long long C = 1; ; C++){ + ok = true, pib = 1; + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + if(i != j && (C / w[i]) % n == (C / w[j]) % n) + ok = false, pib = max(pib, min((C / w[i] + 1) * w[i], (C / w[j] + 1) * w[j])); + if(ok){ + printf("%s\n", s); + printf("%lld\n\n", C); + break; + } + C = pib - 1; + } + } + return(0); +} diff --git a/uva_cpp_clean/190/190-21.cpp b/uva_cpp_clean/190/190-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c38853e85f37c8fda158f94cb3273b046c075f70 --- /dev/null +++ b/uva_cpp_clean/190/190-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 190 + Name: Circle Through Three Points + Problem: https://onlinejudge.org/external/1/190.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define EPS 1e-9 + +bool lineIntersect(double x[], double y[], double r[]) { + double n[2]; n[0] = y[3] - y[2]; n[1] = x[2] - x[3]; + double denom = n[0] * (x[1] - x[0]) + n[1] * (y[1] - y[0]); + if (fabs(denom) < EPS) return false; + + double num = n[0] * (x[0] - x[2]) + n[1] * (y[0] - y[2]); + double t = -num / denom; + r[0] = x[0] + t * (x[1] - x[0]); + r[1] = y[0] + t * (y[1] - y[0]); + return true; +} + +double circle3pts(double x[], double y[], double r[]) { + double lix[4], liy[4]; + lix[0] = 0.5 * (x[0] + x[1]); liy[0] = 0.5 * (y[0] + y[1]); + lix[1] = lix[0] + y[1] - y[0]; liy[1] = liy[0] + x[0] - x[1]; + lix[2] = 0.5 * (x[1] + x[2]); liy[2] = 0.5 * (y[1] + y[2]); + lix[3] = lix[2] + y[2] - y[1]; liy[3] = liy[2] + x[1] - x[2]; + if (!lineIntersect(lix, liy, r)) return -1.0; + return sqrt( + (r[0] - x[0]) * (r[0] - x[0]) + + (r[1] - y[0]) * (r[1] - y[0]) ); +} + +char sgn(double x) { + return x<0 ? '-' : '+'; +} + +void printXY2(char xy, double offset) { + double aof = offset < 0 ? -offset : offset; + if (aof < EPS) + cout << xy << "^2"; + else + printf("(%c %c %.3f)^2", xy, sgn(-offset), aof); +} + +void printXY(double a, char xy) { + double aof = a < 0 ? -a : a; + if (aof > EPS) + printf(" %c %.3f%c", sgn(a), aof, xy); +} + +int main(){ + double x[3], y[3], c[3], r, cx, dy, e; + while (scanf("%lf %lf %lf %lf %lf %lf", x,y, x+1,y+1, x+2,y+2)==6) { + r = circle3pts(x, y, c); + + printXY2('x', c[0]); + cout << " + "; + printXY2('y', c[1]); + printf(" = %.3f^2\n", r); + + cx = -2*c[0]; + dy = -2*c[1]; + e = r*r - c[0]*c[0] - c[1]*c[1]; + cout << "x^2 + y^2"; + printXY(cx, 'x'); + printXY(dy, 'y'); + printf(" %c %.3f = 0\n\n", sgn(-e), e<0?-e:e); + } +} diff --git a/uva_cpp_clean/191/191-21.cpp b/uva_cpp_clean/191/191-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..689abeb5d96d9ceac5bcc093f02ee2b6c6b0d3e1 --- /dev/null +++ b/uva_cpp_clean/191/191-21.cpp @@ -0,0 +1,68 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 191 + Name: Intersection + Problem: https://onlinejudge.org/external/1/191.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define EPS 1e-9 +#define EPS2 1e-10 + +double r1[2], r2[2]; +double l1[2], l2[2]; + +bool pointInR(double x, double y) { + return x>=r1[0] && x<=r2[0] && y>=r1[1] && y<=r2[1]; +} + +bool intersec(double p1[2], double p2[2], double p3[2], double p4[2]) { + double ucrossv1 = ( p2[0] - p1[0] ) * ( p3[1] - p1[1] ) - ( p2[1] - p1[1] ) * ( p3[0] - p1[0] ); + double ucrossv2 = ( p2[0] - p1[0] ) * ( p4[1] - p1[1] ) - ( p2[1] - p1[1] ) * ( p4[0] - p1[0] ); + if (ucrossv1 * ucrossv2 > 0) return false; + + double vcrossu1 = ( p4[0] - p3[0] ) * ( p1[1] - p3[1] ) - ( p4[1] - p3[1] ) * ( p1[0] - p3[0] ); + double vcrossu2 = ( p4[0] - p3[0] ) * ( p2[1] - p3[1] ) - ( p4[1] - p3[1] ) * ( p2[0] - p3[0] ); + return vcrossu1 * vcrossu2 <= 0; +} + + +int main(){ + + int T; + cin>>T; + while (T--) { + cin>>l1[0]>>l1[1]>>l2[0]>>l2[1]; + if (l1[0]==l2[0]) l2[0]+=EPS2; + if (l1[1]==l2[1]) l2[1]+=EPS2; + + cin>>r1[0]>>r1[1]>>r2[0]>>r2[1]; + if (r1[0] > r2[0]) + swap(r1[0], r2[0]); + if (r1[1] > r2[1]) + swap(r1[1], r2[1]); + + r1[0]-=EPS2; r1[1]-=EPS2; + r2[0]+=EPS2; r2[1]+=EPS2; + + double r3[] = {r1[0], r2[1]}; + double r4[] = {r2[0], r1[1]}; + + bool result1 = pointInR(l1[0], l1[1]) + || pointInR(l2[0], l2[1]) + || intersec(l1, l2, r1, r3) + || intersec(l1, l2, r2, r3) + || intersec(l1, l2, r2, r4) + || intersec(l1, l2, r1, r4); + + puts(result1 ? "T" : "F"); + } +} diff --git a/uva_cpp_clean/191/191-5.cpp b/uva_cpp_clean/191/191-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2ce235c1b356d644d4b02c5a94adfcdeecc33202 --- /dev/null +++ b/uva_cpp_clean/191/191-5.cpp @@ -0,0 +1,226 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int tarea(int x1, int y1, int x2, int y2, int x3, int y3){ //i don't use struct bcz it's tough for me. + return x1*y2-y1*x2+x2*y3-y2*x3+x3*y1-y3*x1; +} + +bool online(int x1,int y1,int x2,int y2,int x3,int y3) +{ + int minx,miny,maxx,maxy; + minx=min(x1,x2);miny=min(y1,y2); + maxx=max(x1,x2);maxy=max(y1,y2); + + if(minx<=x3 && maxx>=x3 && miny<=y3 && maxy>=y3 ) + return true; + else + return false; +} + +bool inrect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) +{ + int minx,miny,maxx,maxy; + minx=min(x3,x4);miny=min(y3,y4); + maxx=max(x3,x4);maxy=max(y3,y4); + + if(minx<=x1 && maxx>=x1 && miny<=y1 && maxy>=y1 + && minx<=x2 && maxx>=x2 && miny<=y2 && maxy>=y2) + return true; + else + return false; +} + +bool intersection(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) +{ + int a1=tarea(x3,y3,x4,y4,x1,y1); + int a2=tarea(x3,y3,x4,y4,x2,y2); + int a3=tarea(x1,y1,x2,y2,x3,y3); + int a4=tarea(x1,y1,x2,y2,x4,y4); + +// PP(a1,a2),PP(a3,a4); +// P(online(x3,y3,x4,y4,x1,y1)); +// nl; + if(a1*a2<0 && a3*a4<0) return true; + else if(a1==0 && online(x3,y3,x4,y4,x1,y1)) return true; + else if(a2==0 && online(x3,y3,x4,y4,x2,y2)) return true; + else if(a3==0 && online(x1,y1,x2,y2,x3,y3)) return true; + else if(a4==0 && online(x1,y1,x2,y2,x4,y4)) return true; + else + return false; +} + +int main() +{ +// input; +// output; + int t; + si(t); + while(t--) + { + bool f=0; + int xs,ys,xe,ye,xleft,ytop,xright,ybot; + sii(xs,ys),sii(xe,ye),sii(xleft,ytop),sii(xright,ybot); + + if(xleft>xright) swap(xleft,xright); + if(ybot>ytop) swap(ybot,ytop); + + if(intersection(xs,ys,xe,ye,xleft,ybot,xright,ybot)) f=1; + else if(intersection(xs,ys,xe,ye,xright,ybot,xright,ytop)) f=1; + else if(intersection(xs,ys,xe,ye,xright,ytop,xleft,ytop)) f=1; + else if(intersection(xs,ys,xe,ye,xleft,ytop,xleft,ybot)) f=1; + else if(inrect(xs,ys,xe,ye,xleft,ytop,xright,ybot)) f=1; + + if(f) + puts("T"); + else + puts("F"); + +// P(intersection(xs,ys,xe,ye,xright,ytop,xleft,ytop)); + + } + + return 0; +} +//input +//3 +//2 3 2 6 0 4 5 0 +//2 4 2 6 0 4 5 0 +//1 1 2 2 0 4 4 0 diff --git a/uva_cpp_clean/193/193-13.cpp b/uva_cpp_clean/193/193-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ed3b5e5604f074d8d09e9b248b6e5e7901180cf --- /dev/null +++ b/uva_cpp_clean/193/193-13.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include + +using namespace std; + +int test, n, m, total; + +vector ans; +vector color; +vector > graph; + +void creatGraph() +{ + ans.clear(); + + graph.clear(); + + color.clear(); + + graph.resize(n); + + color.resize(n); + + for (int i = 0; i < n; i++) color[i] = 0; + + for (int i = 0; i < m; i++) + { + int x, y; + + cin >> x >> y; + + x--; + y--; + + graph[x].push_back(y); + + graph[y].push_back(x); + } +} + +void coloring(int vertex) +{ + if (vertex == n) + { + int black = 0; + + for (int i = 0; i < n; i++) if (color[i]) black++; + + if (black > total) + { + total = black; + + ans.clear(); + + for (int i = 0; i < n; i++) if (color[i] == 1) ans.push_back(i + 1); + } + } + + else + { + color[vertex] = 1; + + bool beBlack = true; + + for (int i = 0; i < graph[vertex].size(); i++) + { + if (color[graph[vertex][i]]) + { + beBlack = false; + + break; + } + } + + if (beBlack) coloring(vertex + 1); + + color[vertex] = 0; + + coloring(vertex + 1); + } + +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n >> m; + + creatGraph(); + + total = 0; + + coloring(0); + + cout << total << endl; + + for (int i = 0; i < ans.size() - 1; i++) cout << ans[i] << " "; + + cout << ans[ans.size() - 1] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/193/193-21.cpp b/uva_cpp_clean/193/193-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60a16cb3e2d48066c8140e9f6bc9bf15525d4381 --- /dev/null +++ b/uva_cpp_clean/193/193-21.cpp @@ -0,0 +1,92 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 193 + Name: Graph Coloring + Problem: https://onlinejudge.org/external/1/193.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAX_NODE 105 +typedef bitset biti; +int mat[MAX_NODE][MAX_NODE]; +int n; + +int deg(int node,biti set){ + int i,deg=0; + for(i=1;i<=n;i++) + if(set[i] && mat[node][i]) + deg++; + return deg; +} + +int select_node(biti set,int &mnode){ + int i,max=0,t; + for(i=1;i<=n;i++) + if(set[i] && (t=deg(i,set))>max){ + max=t; + mnode=i; + } + return max; +} +biti rec(biti set){ + int mnode,i,d; + d=select_node(set,mnode); + if(d==0)return set; + biti b1,b2; + + set.reset(mnode); + b1=rec(set); + + for(i=1;i<=n;i++) + if(mat[mnode][i]) + set.reset(i); + b2=rec(set); + b2.set(mnode); + + if(b1.count()>b2.count()) + return b1; + else + return b2; +} + + +int main(){ + int cse,k,i,j,a,b,sum; + biti ans; + cin>>cse; + while(cse--){ + scanf("%d%d",&n,&k); + + for(i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 195 + Name: Anagram + Problem: https://onlinejudge.org/external/1/195.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +struct les{ + bool operator()(char s1, char s2)const{ + if(toupper(s1)==toupper(s2)) + return s1 + +using namespace std; + +int tc; +string s; +vector ans; + +bool cmp(const string x, const string y){ + for(int it = 0; it < x.size(); it++){ + if( x[it] == y[it] ) continue; + if( tolower(x[it]) != tolower(y[it]) ) + return (tolower(x[it]) < tolower(y[it])); + else + return (x[it] < y[it]); + } +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + ans.clear(); + cin >> s; + sort(s.begin(), s.end()); + do{ + ans.push_back(s); + }while(next_permutation(s.begin(), s.end())); + sort(ans.begin(), ans.end(), cmp); + for(int it = 0; it < ans.size(); it++) + printf("%s\n", ans[it].c_str()); + } + return(0); +} diff --git a/uva_cpp_clean/196/196-21.cpp b/uva_cpp_clean/196/196-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f37b5e218590895a1e873eb171236ad5daffe2cc --- /dev/null +++ b/uva_cpp_clean/196/196-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 196 + Name: Spreadsheet + Problem: https://onlinejudge.org/external/1/196.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +vector vec; +int R[1000][20000], S[1000][20000], cse; +int getCell(int r, int c) { + if (S[r][c] == cse) + return R[r][c]; + + string &str = vec[R[r][c]]; + + int result=0; + for (int k=0; str[k++]; ) { + int cc=0, rr=0; + while (str[k]>='A') + cc = cc*26 + str[k++]-'A'+1; + + while (str[k] && str[k]!='+') + rr = rr*10 + str[k++]-'0'; + + result += getCell(rr-1, cc-1); + } + + S[r][c] = cse; + return R[r][c]=result; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m; + string str; + cin >> T; + for (cse=1; cse<=T; ++cse) { + cin >> m >> n; + vec.clear(); + for (int i=0; i> str; + if (str[0] != '=') { + int result = 0, sgn = 1, k = 0; + if (str[0]=='-') { sgn=-1; ++k; } + for (; str[k]; ++k) + result = result*10 + str[k]-'0'; + R[i][j] = result*sgn; + S[i][j] = cse; + } + else { + R[i][j] = vec.size(); + vec.push_back(str); + } + } + + for (int i=0; i + +using namespace std; + +const int MAX_N = 1000 + 5, MAX_M = 18278 + 5, INF = 1e7; + +int n, m, table[MAX_N][MAX_M]; +vector > formula[MAX_N][MAX_M]; + +int dp (int r, int c) { + if (table[r][c] != INF) return table[r][c]; + int ret = 0; + for (auto pp: formula[r][c]) { + ret += dp(pp.first, pp.second); + } + return table[r][c] = ret; +} + +void printCase () { + for (int r = 0; r < n; r++) { + for (int c = 0; c < m; c++) { + if (c) cout << ' '; + cout << dp(r, c); + } + cout << endl; + } +} + +void addFormula (int r, int c, string cell) { + formula[r][c].clear(); + for (int i = 1; i < cell.size(); i++) { + int col = 0, row = 0; + while (isalpha(cell[i])) { + col = col * 26 + (cell[i] - 'A' + 1); + i++; + } + while (i < cell.size() and cell[i] != '+') { + row = row * 10 + (cell[i] - '0'); + i++; + } + formula[r][c].push_back({row - 1, col - 1}); + } +} + +void clearCase () { + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + table[i][j] = INF; +} + +void readCase () { + cin >> m >> n; + clearCase(); + cin.ignore(); + for (int r = 0; r < n; r++) { + string line, cell; + getline(cin, line); + istringstream token(line); + for (int c = 0; c < m; c++) { + token >> cell; + if (cell[0] == '=') { + addFormula(r, c, cell); + } + else { + table[r][c] = stoi(cell); + } + } + } +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + readCase(); + printCase(); + } + return (0); +} diff --git a/uva_cpp_clean/200/200-19.cpp b/uva_cpp_clean/200/200-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97068385313ae0e49769d1d1bb793653078fcf1a --- /dev/null +++ b/uva_cpp_clean/200/200-19.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +using namespace std; + +typedef vector vi; +vector AdjList; +bool visited[26]; + +void toposort(int j) { + for (int i = 0; i < AdjList[j].size(); i++) { + if (!visited[AdjList[j][i]]) { + visited[AdjList[j][i]] = true; + toposort(AdjList[j][i]); + } + } + printf("%c", j+'A'); +} + +int main() { + string n; + vector v; + while (cin >> n && n != "#") { + v.push_back(n); + } + + for (int i = 0; i < 26; i++) visited[i] = true; + AdjList.assign(26, vi()); + for (int i = 0; i < v.size()-1; i++) { + for (int j = 0; j < v[i].length() && j < v[i+1].length(); j++) { + if (v[i][j] != v[i+1][j]) { + AdjList[v[i+1][j]-'A'].push_back(v[i][j] - 'A'); + visited[v[i][j]-'A'] = visited[v[i+1][j]-'A'] = false; + break; + } + } + } + + for (int i = 0; i < 26; i++) { + if (!visited[i]) { + visited[i] = true; + toposort(i); + } + } + printf("\n"); + return 0; +} diff --git a/uva_cpp_clean/200/200-21.cpp b/uva_cpp_clean/200/200-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb7b690abe6a980d21c9983062ddcd30dd0ded38 --- /dev/null +++ b/uva_cpp_clean/200/200-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 200 + Name: Rare Order + Problem: https://onlinejudge.org/external/2/200.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +bool ww[26]; +bool lt[26][26]; +void setLt(char *s1, char* s2) { + int i; + for (i=0; s1[i] && s1[i]==s2[i]; i++); + if (s1[i] && s2[i] && s1[i]!=s2[i]) { + lt[s1[i]-'A'][s2[i]-'A'] = true; + ww[s1[i]-'A'] = ww[s2[i]-'A'] = 1; + } +} + + +bool seen[26]; +int order[26], oc; +void dfs(int v, bool root) { + if (!seen[v]) { + seen[v] = true; + for (int i=0; i<26; i++) + if (lt[v][i]) + dfs(i, 0); + + order[oc++] = v; + } +} + + +int main(){ + char word[30], prev[30]; + while (cin>>prev) { + memset(ww, 0, sizeof(ww)); + memset(lt, 0, sizeof(lt)); + memset(seen, 0, sizeof(seen)); + oc = 0; + + while (cin>>word && word[0]!='#') { + setLt(prev, word); + swap(prev, word); + } + + for (int i=0; i<26; i++) + if (ww[i]) + dfs(i, 1); + + + for (int i=oc-1; i>=0; i--) + cout<< (char)(order[i]+'A'); + cout< +using namespace std; +using vi=vector; +using vvi=vector; +using vs=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + string s; + getline(cin,s); + if(s.empty())break; + vs t; + for(;;){ + if(s[0]=='#')break; + t.push_back(s); + getline(cin,s); + } + vi p(128),m(128); + string q; + for(auto &s:t){ + for(auto c:s){ + p[c]++; + } + } + int n=0; + for(int i='A';i<='Z';i++) + if(p[i]){ + m[i]=n++; + q.push_back(i); + } + vvi g(n); + for(int i=0;idfs=[&](int u){ + w[u]=1; + for(int v:g[u]) + if(!w[v]) + dfs(v); + x.push_back(u); + }; + for(int i=0;i + +using namespace std; + +const int MAX_V = 30; + +bool vis[MAX_V]; +string s; +vector AdjList[MAX_V], ts; +vector word; + +void dfs(int u){ + vis[u] = true; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not vis[v] ) dfs(v); + } + ts.push_back(u); +} + +void TopologicalSort(){ + for(int u = 0; u < MAX_V; u++) + if( not vis[u] and not AdjList[u].empty() ) + dfs(u); +} + +void ConstructGraph(){ + for(int i = 0; i < word.size(); i++) + for(int j = i + 1; j < word.size(); j++) + for(int k = 0; k < min(word[i].size(), word[j].size()); k++){ + int u = word[i][k] - 'A', v = word[j][k] - 'A'; + if( u == v ) continue; + AdjList[u].push_back(v); + break; + } +} + +void solve(){ + ConstructGraph(); + TopologicalSort(); + for(int i = ts.size() - 1; i >= 0; i--) putchar(ts[i] + 'A'); + putchar('\n'); +} + +inline void read(){ + while(cin >> s, s[0] != '#') word.push_back(s); +} + +int main(){ + read(); + solve(); + return(0); +} diff --git a/uva_cpp_clean/201/201-17.cpp b/uva_cpp_clean/201/201-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b67ac876284d7d99a229a96811c2543448532f2 --- /dev/null +++ b/uva_cpp_clean/201/201-17.cpp @@ -0,0 +1,49 @@ +/** + * AOAPC II Exercise 4-2 Squares + * Solution 2 (Inspired by CHEN Feng) + * Counts the max length of horizontal and vertical line segments starting + * at each dot. + */ +#include +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n, m, pn = 0; + while (cin >> n >> m) { + vector> conH(n + 1, vector(n + 1, false)), + conV(n + 1, vector(n + 1, false)); + while (m--) { + char ori; + int i, j; + cin >> ori >> i >> j; + ori == 'H' ? conH[i][j] = true : conV[j][i] = true; + } + vector> lenH(n + 2, vector(n + 2, 0)), + lenV(n + 2, vector(n + 2, 0)); + for (int r = n; r >= 1; --r) + for (int c = n; c >= 1; --c) { + if (conH[r][c]) + lenH[r][c] = lenH[r][c + 1] + 1; + if (conV[r][c]) + lenV[r][c] = lenV[r + 1][c] + 1; + } + vector sz(n + 1, 0); + for (int r = 1; r <= n; ++r) + for (int c = 1; c <= n; ++c) + for (int s = 1; s <= min(lenH[r][c], lenV[r][c]); ++s) + if (lenH[r + s][c] >= s && lenV[r][c + s] >= s) + ++sz[s]; + if (pn) + cout << "\n**********************************\n\n"; + cout << "Problem #" << ++pn << "\n\n"; + bool no = true; + for (int size = 1; size <= n; ++size) + if (sz[size]) { + cout << sz[size] << " square (s) of size " << size << "\n"; + no = false; + } + if (no) + cout << "No completed squares can be found.\n"; + } +} diff --git a/uva_cpp_clean/201/201-21.cpp b/uva_cpp_clean/201/201-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89e50758a9c048fdc65b62a3c22d79152862ad76 --- /dev/null +++ b/uva_cpp_clean/201/201-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 201 + Name: Squares + Problem: https://onlinejudge.org/external/2/201.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool lined(bool H[], int sz) { + for (int i=0; i> n >> m; ++cse) { + if (cse > 1) + cout << "\n**********************************\n\n"; + cout << "Problem #" << cse << "\n\n"; + + bool H[9][9] = {}, + V[9][9] = {}; + + for (int i, j; m--; ) { + char ch; + cin >> ch >> i >> j; + (ch == 'H' ? H : V)[i-1][j-1] = true; + } + + int C[10] = {}; + for (int i=0; i +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int a, b; + while (cin >> a >> b) { + map remPos; + string s = ""; + int rem = a % b, n = rem * 10, dig = n / b; + int pos = 0; + while (remPos.find(rem) == remPos.end()) { + s.append(to_string(dig)); + remPos[rem] = pos++; + rem = n % b; + n = rem * 10; + dig = n / b; + } + int beg = remPos[rem]; + cout << a << "/" << b << " = " << a / b << "." << string(s.begin(), s.begin() + beg) << "("; + if (s.size() <= 50) + cout << string(s.begin() + beg, s.end()); + else + cout << string(s.begin() + beg, s.begin() + 50) << "..."; + cout << ")\n"; + cout << " " << s.size() - beg << " = number of digits in repeating cycle\n\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/202/202-19.cpp b/uva_cpp_clean/202/202-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0492d6109cb3682be1e1681517f6510c62b757ec --- /dev/null +++ b/uva_cpp_clean/202/202-19.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +using namespace std; + +int main() { + int n, n1, m; + while (cin >> n >> m && !(n == 0 && m == 0)) { + int c = 1; + n1 = n; + int prev = n/m; + n %= m; + unordered_map mv; + queue rq; + while (!(n == 0 || mv.count(n))) { + mv[n] = c; + c++; + n *= 10; + rq.push(n/m); + n %= m; + } + if (n == 0) { + rq.push(0); + mv[n] = c; + c++; + } + printf("%d/%d = %d.", n1, m, prev); + for (int i = 0; i < 50 && !rq.empty(); i++) { + if (i == mv[n] - 1) printf("("); + printf("%d", rq.front()); + rq.pop(); + } + if (!rq.empty()) printf("..."); + printf(")\n"); + printf(" %d = number of digits in repeating cycle\n\n", c - mv[n]); + } + return 0; +} diff --git a/uva_cpp_clean/202/202-21.cpp b/uva_cpp_clean/202/202-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..407ab4d31db5765ba0826c1913777954f3c782a6 --- /dev/null +++ b/uva_cpp_clean/202/202-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 202 + Name: Repeating Decimals + Problem: https://onlinejudge.org/external/2/202.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n, d, ind; + int seen[3001], p[100]; + while (cin>>n>>d) { + for (int i=0; i 50) + cout << "..."; + cout << ')' << endl; + + printf(" %d = number of digits in repeating cycle\n\n", ind-seen[n]); + } +} diff --git a/uva_cpp_clean/202/202-9.cpp b/uva_cpp_clean/202/202-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..119ff77d283d5abf2d61aa0560f556bbebbced40 --- /dev/null +++ b/uva_cpp_clean/202/202-9.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +const int SIZE = 3000; + +int num, den, pos, mu, lambda, vis[SIZE], f[SIZE]; + +int main(){ + while(~scanf("%d %d", &num, &den)){ + printf("%d/%d = %d.", num, den, num / den); + num %= den; + pos = 0; + fill(vis, vis + den, 0); + while( vis[num] == 0 ){ + f[pos] = 10 * num / den; + vis[num] = pos; + num = ( num * 10 ) % den; + pos++; + } + lambda = pos - vis[num]; + mu = pos - lambda; + if( mu == 1 and f[0] == f[lambda] ) mu = 0; + for(int it = 0; it < mu; it++) printf("%d", f[it]); + putchar('('); + for(int it = mu, ct = mu; it < mu + lambda; ct++, it++){ + if( ct == 50 ) printf("..."), it = pos; + else printf("%d", f[it]); + } + puts(")"); + printf(" %d = number of digits in repeating cycle\n", lambda); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/207/207-17.cpp b/uva_cpp_clean/207/207-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c983dea7423b38dc26bf2832c7f847a3c53f7e1 --- /dev/null +++ b/uva_cpp_clean/207/207-17.cpp @@ -0,0 +1,133 @@ +/** + * AOAPC II Example 5-10 PGA Tour Prize Money + * ICPC WF 1990 + */ +#include +using namespace std; +struct Player { + string name; + vector rd; + int s36, rank, stot; + bool dq = false; + Player(string &name, vector &rd) : name(name), rd(rd) { + s36 = rd[0] + rd[1]; + if (rd.size() < 4) + dq = true; + stot = s36 + accumulate(rd.begin() + 2, rd.end(), 0); + } +}; +int main() { + cin.tie(nullptr); + const int CUT = 70; + cout << fixed << setprecision(2); + int kase; + cin >> kase; + for (int k = 0; k < kase; ++k) { + double purse; + cin >> purse; + vector perc(CUT); + for (double &d : perc) + cin >> d; + vector players; + // Read players + int np; + cin >> np; + while (np--) { + string na = "", junk; + vector src; + getline(cin, junk); + for (int i = 0; i < 20; ++i) + na += getchar(); + while (na.back() == ' ') + na.pop_back(); + bool valid = true; + for (int i = 0; i < 4; ++i) { + string s = ""; + for (int j = 0; j < 3; ++j) + s += getchar(); + if (isalpha(s[0]) || isalpha(s[1])) { + if (i < 2) + valid = false; + break; + } + src.push_back(stoi(s)); + } + if (valid) + players.push_back(Player(na, src)); + } + // First 36 holes + sort(players.begin(), players.end(), [](Player &a, Player &b){ + return a.s36 < b.s36; + }); + int pos = -1, scr = 0; + auto cut = players.begin(); + for (; cut != players.end(); ++cut) { + ++pos; + if (cut->s36 > scr) { + scr = cut->s36; + if (pos >= CUT) + break; + } + } + players.erase(cut, players.end()); + // Final rounds + sort(players.begin(), players.end(), [](Player &a, Player &b){ + if (a.rd.size() != b.rd.size()) + return a.rd.size() > b.rd.size(); + if (a.stot != b.stot) + return a.stot < b.stot; + return a.name < b.name; + }); + // Ranking + map num; + map totScr; + pos = -1, scr = 0; + int cnt = 0, rk = 0, last_prize = 0; + for (Player &p : players) { + if (p.dq) + break; + ++pos; + if (p.stot > scr) { + rk = pos; + scr = p.stot; + } + p.rank = rk; + if (p.name.back() != '*') { + if (cnt < CUT) { + last_prize = p.rank; + totScr[p.rank] += perc[cnt]; + } + ++num[p.rank]; + ++cnt; + } + } + // Output + if (k) + cout << "\n"; + cout << left << setw(21) << "Player Name" << setw(10) << "Place" << setw(5) << "RD1" << setw(5) << "RD2" << setw(5) << "RD3" << setw(5) << "RD4" << setw(10) << "TOTAL" << "Money Won\n"; + cout << string(71, '-') << "\n"; + for (Player &p : players) { + if (!p.dq) { + cout << left << setw(21) << p.name; + cout << setw(10) << ((num[p.rank] > 1 && p.name.back() != '*' && p.rank <= last_prize) ? to_string(p.rank + 1) + "T" : to_string(p.rank + 1)); + for (int i : p.rd) + cout << setw(5) << i; + cout << setw(3) << p.stot; + if (p.name.back() != '*' && p.rank <= last_prize) { + cout << string(7, ' '); + cout << "$" << right << setw(9) << purse * (totScr[p.rank] / 100) / num[p.rank]; + } + } + else { + cout << left << setw(31) << p.name; + for (int i = 0; i < 4; ++i) + if (i < p.rd.size()) + cout << setw(5) << p.rd[i]; + else + cout << string(5, ' '); + cout << "DQ"; + } + cout << "\n"; + } + } +} diff --git a/uva_cpp_clean/208/208-21.cpp b/uva_cpp_clean/208/208-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd39acfd8fd2271073ccecf3c5a0dc6aabfca4c --- /dev/null +++ b/uva_cpp_clean/208/208-21.cpp @@ -0,0 +1,72 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 208 + Name: Firetruck + Problem: https://onlinejudge.org/external/2/208.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int map[21][21], m[21][21] ,d, mx; +int used[21], p[21]; + +int BT(int i) +{ + if(p[i-1]==d-1) + { + for(int k=0; k>d) + { + memset(map,0,sizeof(map)); + memset(m,0,sizeof(m)); + mx=0; + while(cin>>i>>j && (i||j)) + { + map[i-1][j-1]=map[j-1][i-1]=1; + m[i-1][j-1]=m[j-1][i-1]=1; + mx=max(mx, max(i,j)); + } + + for(k=0; k +using namespace std; +int n, ta, to, tb, te, ts, t; +vector progs; +vector line_nums, vars; +deque readyQ; +queue blockedQ; +bool locked; +void run(int id) +{ + int timer = t; + while (timer > 0) + { + string &line = progs[line_nums[id]]; + switch(line[2]) + { + case '=': + vars[line[0] - 'a'] = stoi(line.substr(4)); + timer -= ta; + break; + case 'i': // print + cout << id + 1 << ": " << vars[line[6] - 'a'] << "\n"; + timer -= to; + break; + case 'c': // lock + if (locked) + { + blockedQ.push(id); + return; + } + locked = true; + timer -= tb; + break; + case 'l': // unlock + locked = false; + if (!blockedQ.empty()) + { + readyQ.push_front(blockedQ.front()); + blockedQ.pop(); + } + timer -= te; + break; + case 'd': // stop + return; + } + ++line_nums[id]; + } + readyQ.push_back(id); +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int T; + cin >> T; + while (T--) + { + cin >> n >> ta >> to >> tb >> te >> ts >> t; + progs.clear(); + line_nums = vector(n); + vars = vector(26, 0); + locked = false; + string line; + int line_num = 0; + getline(cin, line); + for (int i = 0; i < n; ++i) + { + line_nums[i] = line_num; + while (true) + { + getline(cin, line); + progs.push_back(line); + ++line_num; + if (line == "end") + break; + } + readyQ.push_back(i); + } + while (!readyQ.empty()) + { + int cur_id = readyQ.front(); + readyQ.pop_front(); + run(cur_id); + } + if (T) + cout << "\n"; + } +} diff --git a/uva_cpp_clean/213/213-17.cpp b/uva_cpp_clean/213/213-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1adda0de3ef4bd582203c06e8384e07db28f8ec --- /dev/null +++ b/uva_cpp_clean/213/213-17.cpp @@ -0,0 +1,41 @@ +/** + * AOAPC II Example 4-4 Message Encoding + */ +#include +using namespace std; +int read_int(int len) { + int ans = 0; + char cur; + while (len--) { + cin >> cur; + ans = ans * 2 + cur - '0'; + } + return ans; +} +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + string header; + while (getline(cin, header)) { + // Make map + vector dec(8, string(1 << 8, ' ')); + int len = 1, pos = 0; + for (char c : header) { + dec[len][pos++] = c; + if (pos >= (1 << len) - 1) { + ++len; + pos = 0; + } + } + // Decode + while (len = read_int(3)) { + int val; + while ((val = read_int(len)) < (1 << len) - 1) { + char c = dec[len][val]; + cout << dec[len][val]; + } + } + cout << '\n'; + getline(cin, header); + } +} diff --git a/uva_cpp_clean/213/213-21.cpp b/uva_cpp_clean/213/213-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ff19bf0d38b4c9c839397d59e987bf801c7959c --- /dev/null +++ b/uva_cpp_clean/213/213-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 213 + Name: Message Decoding + Problem: https://onlinejudge.org/external/2/213.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline char readNonBlankChar() { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return EOF; + return ch; +} + +inline int readLine(char *str) { + int len=0; + char ch; + while ((ch=readchar())!='\n' && ch!=EOF) + str[len++] = ch; + + str[len] = 0; + if (ch == EOF && !len) return EOF; + return len; +} + + +int readBin(int len) { + int r = 0; + while (len--) + r = (r<<1) | (readNonBlankChar() == '1'); + return r; +} + +int main() { + char H[1024]; + int S[] = { 0, 1, 4, 11, 26, 57, 120, 247, 502, 1013 }; + while (readLine(H) != EOF) { + if (!H[0]) continue; + + int l, x; + while ( (l = readBin(3)) > 0 ) { + int fin = (1 << l) - 1; + while ( (x = readBin(l)) != fin && x!=EOF ) + cout << H[S[l-1] + x]; + } + cout << endl; + while (readchar()!='\n'); + } +} diff --git a/uva_cpp_clean/216/216-13.cpp b/uva_cpp_clean/216/216-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bbd6be8dae2758b864e19b063a13d7da1037b05 --- /dev/null +++ b/uva_cpp_clean/216/216-13.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, counter; +string ans; +double minimum; +double dist[10][10]; +vector < pair < int, int>> points; + +void show() +{ + cout << "**********************************************************" << endl; + cout << "Network #" << counter << endl; + + for (int i = 1; i < n; i++) + { + int pre = (int)ans[i - 1] - 48; + int cur = (int)ans[i] - 48; + + pair p1 = points[pre]; + pair p2 = points[cur]; + + double distance = dist[pre][cur]; + + printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.\n", p1.first, p1.second, p2.first, p2.second, distance); + } + + printf("Number of feet of cable required is %.2lf.\n", minimum); + + counter++; +} + +void per(string s, int start, int size) +{ + static int counter = 0; + + if (start == size) + { + counter++; + + double temp = 0.0; + string answer = s; + + for (int i = 1; i < n; i++) + { + int pre = (int)s[i - 1] - 48; + int cur = (int)s[i] - 48; + + temp += dist[pre][cur]; + } + + if (temp < minimum) + { + minimum = temp; + + ans = answer; + } + } + + else + { + for (int i = start; i <= size; i++) + { + char c = s[i]; + + s[i] = s[start]; + + s[start] = c; + + per(s, start + 1, size); + } + } +} + +void initilize() +{ + ans = ""; + + minimum = 99999999.9; + + memset(dist, 0, sizeof dist); + + string s = "0123456789"; + + s = s.substr(0, n); + + points.clear(); + + for (int i = 0; i < n; i++) + { + int x, y; + + cin >> x >> y; + + points.push_back(make_pair(x, y)); + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int x1, x2, y1, y2; + + x1 = points[i].first; + y1 = points[i].second; + + x2 = points[j].first; + y2 = points[j].second; + + dist[i][j] = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) + 16.00; + } + } + + per(s, 0, n - 1); + + show(); +} + +int main() +{ + counter = 1; + + cin >> n; + + while (n != 0) + { + initilize(); + + cin >> n; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/216/216-21.cpp b/uva_cpp_clean/216/216-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6feec5853698cc71c45cc82756fe596f2f77ed5 --- /dev/null +++ b/uva_cpp_clean/216/216-21.cpp @@ -0,0 +1,69 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 216 + Name: Getting in Line + Problem: https://onlinejudge.org/external/2/216.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +double x[8],y[8]; +double dist(int i,int j) +{ + return sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j])); +} + +int used[8],n; +double best; +int ch[8]; +int bch[8]; +void BT(int i, double d) +{ + if(i==n) + { + if(d>n && n) + { + for(i=0; i>x[i]>>y[i]; + + best=10000000; + BT(0, 0); + puts("**********************************************************"); + printf("Network #%d\n",cse++); + for(i=1; i + +#define INF 10000000 + +using namespace std; + +struct point{ + int x,y; +}p[10]; + +int tc,n; +vectorx,ans; +double min_cable,d[10][10],dd; +bool visited[10]; + +double dist(int x,int y,int xx,int yy){ + return sqrt((x-xx)*(x-xx)+(y-yy)*(y-yy))+16.00; +} + +void backtrack(int pos, double req_cable){ + x.push_back(pos); + if(x.size()==n){ + min_cable = req_cable; + ans = x; + return; + } + for(int it=0;it +using namespace std; +struct Pos { + int x, y; + Pos(int x, int y) : x(x), y(y) {} + Pos operator+(const Pos &oth) const { + return Pos(x + oth.x, y + oth.y); + } + bool operator==(const Pos &oth) const { + return x == oth.x && y == oth.y; + } + bool valid() const { + return x >= 0 && x < 8 && y >= 0 && y < 8; + } +}; +const vector moves { Pos(-1, 0), Pos(-1, 1), Pos(0, 1), Pos(1, 1), + Pos(1, 0), Pos(1, -1), Pos(0, -1), Pos(-1, -1) }; +vector board; +char turn; +char oppo(char a) { + return a == 'W' ? 'B' : 'W'; +} +bool can_put(Pos pos) { + if (board[pos.x][pos.y] != '-') + return false; + for (Pos dir : moves) { + Pos next = pos + dir; + if (next.valid() && board[next.x][next.y] != oppo(turn)) + continue; + while (next.valid()) { + if (board[next.x][next.y] == '-') + break; + else if (board[next.x][next.y] == turn) + return true; + next = next + dir; + } + } + return false; +} +void put(Pos pos) { + board[pos.x][pos.y] = turn; + for (Pos dir : moves) { + Pos next = pos + dir; + if (next.valid() && board[next.x][next.y] != oppo(turn)) + continue; + while (next.valid()) { + if (board[next.x][next.y] == '-') + break; + else if (board[next.x][next.y] == turn) { + for (Pos p = pos + dir; !(p == next); p = p + dir) + board[p.x][p.y] = turn; + break; + } + next = next + dir; + } + } +} +void lis() { + bool first = true; + for (int r = 0; r < 8; ++r) + for (int c = 0; c < 8; ++c) + if (can_put(Pos(r, c))) { + if (first) + first = false; + else + cout << " "; + cout << "(" << r + 1 << "," << c + 1<< ")"; + } + if (first) + cout << "No legal move."; + cout << "\n"; +} +void move() { + char ir, ic; + cin >> ir >> ic; + int r = ir - '0' - 1, c = ic - '0' - 1; + if (!can_put(Pos(r, c))) + turn = oppo(turn); + put(Pos(r, c)); + turn = oppo(turn); + int b = 0, w = 0; + for (string &s : board) + for (char c : s) + if (c == 'B') + ++b; + else if (c == 'W') + ++w; + cout << "Black - " << setw(2) << b << " White - " << setw(2) << w << "\n"; +} +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int gn; + cin >> gn; + for (int g = 0; g < gn; ++g) { + if (g) + cout << "\n"; + board = vector(8); + for (string &s : board) + cin >> s; + cin >> turn; + bool cont = true; + while (cont) { + char cmd; + cin >> cmd; + switch (cmd) { + case 'L': + lis(); + break; + case 'M': + move(); + break; + case 'Q': + cont = false; + for (string &s : board) + cout << s << "\n"; + } + } + } +} diff --git a/uva_cpp_clean/221/221-17.cpp b/uva_cpp_clean/221/221-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b51ccac78ad10b53d57da8452580f378932e0f62 --- /dev/null +++ b/uva_cpp_clean/221/221-17.cpp @@ -0,0 +1,71 @@ +/** + * AOAPC II Example 5-12 Urban Elevation + */ +#include +using namespace std; +struct Building +{ + int id; + double x1, y, x2, h; + bool operator<(const Building &rhs) const + { + return x1 < rhs.x1 || (x1 == rhs.x1 && y < rhs.y); + } +}; +vector bulds; +vector pts; +bool vis(Building &buld) +{ + for (double pt : pts) + if (buld.x1 <= pt && pt <= buld.x2) + { + bool ok = true; + for (Building &cur_buld : bulds) + if (cur_buld.x1 <= pt && pt <= cur_buld.x2 && cur_buld.y < buld.y && cur_buld.h >= buld.h) + { + ok = false; + break; + } + if (ok) + return true; + } + return false; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n, kase = 0; + while ((cin >> n) && n) + { + int cur_id = 1; + bulds = vector(n); + set segs; + for (Building &buld : bulds) + { + double dep, wid; + cin >> buld.x1 >> buld.y >> wid >> dep >> buld.h; + buld.id = cur_id++; + buld.x2 = buld.x1 + wid; + segs.insert(buld.x1); + segs.insert(buld.x2); + } + sort(bulds.begin(), bulds.end()); + pts.clear(); + double l = 0; + for (double d : segs) + { + pts.push_back((l + d) / 2); + l = d; + } + if (kase++) + cout << "\n"; + cout << "For map #" << kase << ", the visible buildings are numbered as follows:\n" + << bulds.front().id; + for_each(bulds.begin() + 1, bulds.end(), [](Building &buld){ + if (vis(buld)) + cout << " " << buld.id; + }); + cout << "\n"; + } +} diff --git a/uva_cpp_clean/222/222-21.cpp b/uva_cpp_clean/222/222-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9077db15cd0ba585ffd1c0c194585ff32ad295c --- /dev/null +++ b/uva_cpp_clean/222/222-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 222 + Name: Budget Travel + Problem: https://onlinejudge.org/external/2/222.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int n; +double D[61], P[61], cap, mpg, best; +void bt(int idx, double cost, double gas) { + if (cost >= best) return; + if (idx == n) { best = cost; return; } + + double geed = (D[idx+1]-D[idx])/mpg; + if (geed <= gas) + bt(idx+1, cost, gas - geed); + + if (geed > gas || gas <= cap/2) + bt(idx+1, cost + 200 + round((cap-gas)*P[idx]), cap - geed); +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(2); + + double iprc, dis; + for (int cse=1; cin >> dis >> cap >> mpg >> iprc >> n; ++cse) { + D[n] = dis; + for (int i=0; i> D[i] >> P[i]; + + best = 1e100; + bt(0, iprc*100, cap-D[0]/mpg); + cout << "Data Set #" << cse + << "\nminimum cost = $" << best/100 << '\n'; + } +} diff --git a/uva_cpp_clean/227/227-17.cpp b/uva_cpp_clean/227/227-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d155c5f231155d3bf74b2fd863989777d2d08aa --- /dev/null +++ b/uva_cpp_clean/227/227-17.cpp @@ -0,0 +1,74 @@ +/** + * AOAPC II 3-5 Puzzle + * ICPC WF 1993 + * I/O, simulation + * 60 WA + * +10 AC + */ +#include +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + vector t(5); + int cnt = 0; + while (getline(cin, t[0]) && t[0][0] != 'Z') { + if (cnt > 0) + cout << '\n'; + cout << "Puzzle #" << ++cnt << ":\n"; + int x, y; + for (int row = 0; row < 5; ++row) { + if (row > 0) + getline(cin, t[row]); + if (t[row].size() < 5) + t[row] += " "; + int pos; + if ((pos = t[row].find(' ')) != string::npos) { + x = row; + y = pos; + } + } + string ops, temp; + getline(cin, temp); + ops.append(temp); + while (temp.back() != '0') { + getline(cin, temp); + ops.append(temp); + } + bool ok = true; + for (char op : ops) { + int r = x, c = y; + switch (op) { + case 'A': + --r; + break; + case 'B': + ++r; + break; + case 'L': + --c; + break; + case 'R': + ++c; + break; + } + if (r < 0 || r > 4 || c < 0 || c > 4) { + ok = false; + break; + } + swap(t[x][y], t[r][c]); + x = r; + y = c; + } + if (ok) + for (string row : t) { + auto iter = row.begin(); + cout << *iter++; + for (; iter < row.end(); ++iter) + cout << " " << *iter; + cout << '\n'; + } + else + cout << "This puzzle has no final configuration.\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/227/227-21.cpp b/uva_cpp_clean/227/227-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..348b40b62d337ea88e2622b9969cbed103809538 --- /dev/null +++ b/uva_cpp_clean/227/227-21.cpp @@ -0,0 +1,110 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 227 + Name: Puzzle + Problem: https://onlinejudge.org/external/2/227.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + static bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} + +inline char readSingleChar() { + char ch; + while ((ch=readchar())=='\n'); + return ch; +} + +void skipLine() { + while (readchar(0)!='\n') + readchar(); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char G[5][5], ch; + for (int cse=1; ;++cse) { + int ii, jj; + for (int i=0; i<5; ++i) { + for (int j=0; j<5; ++j) + if ((G[i][j] = readSingleChar()) == EOF) + return 0; + else if (G[i][j] == ' ') + ii = i, jj = j; + skipLine(); + } + + if (cse > 1) cout << '\n'; + cout << "Puzzle #" << cse << ":\n"; + + bool valid = true; + while ((ch=readSingleChar()) != '0') + switch (ch) { + case 'A': + if (!ii) valid = false; + else { + swap(G[ii][jj], G[ii-1][jj]); + --ii; + } + break; + case 'B': + if (ii==4) valid = false; + else { + swap(G[ii][jj], G[ii+1][jj]); + ++ii; + } + break; + case 'L': + if (!jj) valid = false; + else { + swap(G[ii][jj], G[ii][jj-1]); + --jj; + } + break; + case 'R': + if (jj==4) valid = false; + else { + swap(G[ii][jj], G[ii][jj+1]); + ++jj; + } + break; + default: + valid = false; + } + + if (!valid) + cout << "This puzzle has no final configuration.\n"; + + else { + for (int i=0; i<5; ++i) { + cout << G[i][0]; + for (int j=1; j<5; ++j) + cout << ' ' << G[i][j]; + cout << '\n'; + } + } + } +} diff --git a/uva_cpp_clean/230/230-17.cpp b/uva_cpp_clean/230/230-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdf5f2261721f0ad99655667f2bd315bb4082ad5 --- /dev/null +++ b/uva_cpp_clean/230/230-17.cpp @@ -0,0 +1,62 @@ +/** + * AOAPC II Exercise 5-8 Borrowers + * 40 AC + */ +#include +using namespace std; +map auth; +struct Book +{ + string name; + Book(string name) : name(name) { } + bool operator<(const Book &rhs) const + { + if (auth[name] != auth[rhs.name]) + return auth[name] < auth[rhs.name]; + return name < rhs.name; + } +}; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + string line; + set books; + while (getline(cin, line) && line[0] != 'E') + { + auto pos = line.find('"', 1); + string name = line.substr(0, pos + 1), author = line.substr(pos + 5); + auth[name] = author; + books.insert(name); + } + set rets; + string cmd; + while (cin >> cmd && cmd[0] != 'E') + if (cmd[0] == 'S') + { + for (const Book &ret : rets) + { + auto pos = books.insert(ret).first; + cout << "Put " << ret.name << " "; + if (pos == books.begin()) + cout << "first"; + else + { + --pos; + cout << "after " << pos->name; + } + cout << "\n"; + } + cout << "END\n"; + rets.clear(); + } + else + { + getline(cin, line); + string bk = line.substr(line.find('"')); + if (cmd[0] == 'B') + books.erase(bk); + else if (cmd[0] == 'R') + rets.insert(bk); + } +} diff --git a/uva_cpp_clean/230/230-21.cpp b/uva_cpp_clean/230/230-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c980542bde3feb7ea51a29518ef48f1f54c2616 --- /dev/null +++ b/uva_cpp_clean/230/230-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 230 + Name: Borrowers + Problem: https://onlinejudge.org/external/2/230.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int n; +struct Book { + string title, author; + bool operator < (const Book &o) const { + return author != o.author + ? author < o.author + : title < o.title; + } +} books[10043]; +bool inlib[10043], intab[10043]; + +void readTitle(string &s) { + cin.ignore(1, '"'); + getline(cin, s, '"'); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (n=0; cin.peek()=='"'; ++n) { + readTitle(books[n].title); + cin.ignore(10, ' '); cin.ignore(10, ' '); + getline(cin, books[n].author); + inlib[n] = 1; + } + cin.ignore(100, '\n'); + sort(books, books+n); + + unordered_map ids; + for (int i=0; i> cm && cm!="END") { + if (cm == "SHELVE") { + for (int i=0, j; i=0 && !inlib[j]; --j); + if (j < 0) + cout << "\" first\n"; + else + cout << "\" after \"" << books[j].title << "\"\n"; + + intab[i] = 0; + inlib[i] = 1; + } + cout << "END\n"; + } + else { + readTitle(title); + int id = ids[title]; + intab[id] = cm == "RETURN"; + inlib[id] = 0; + } + } +} diff --git a/uva_cpp_clean/230/230-6.cc b/uva_cpp_clean/230/230-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..71da19cb18b2434efe57eac2b6f0c4db16c90f56 --- /dev/null +++ b/uva_cpp_clean/230/230-6.cc @@ -0,0 +1,86 @@ +// https://uva.onlinejudge.org/external/2/230.pdf +#include +#include + +using namespace std; + +struct cmp { + bool operator() (const string& lhs, const string& rhs) const {return lhs>rhs;} +}; + +typedef pair pss; +typedef multimap mmss; +typedef multimap mmssd; +typedef map mss; + +int main() { + string s; + mmssd shelve; + mss titles; + while (true) { + getline(cin, s); + if (s == "END") break; + int c = 0; + int i = 0; + int spos = -1; + while (i < s.size()) { + if (s[i] == '"') { + c++; + if (c == 2) break; + spos = i; + } + i++; + } + int epos = i++; + string title = s.substr(spos + 1, epos - spos - 1); + while (s[i] != 'b') i++; + i++; + while (s[i] != 'y') i++; + i++; + while (isspace(s[i])) i++; + string author = s.substr(i, s.size() - i); + shelve.insert(pss(author, title)); + titles[title] = author; + } + mmss returns; + while (true) { + getline(cin, s); + if (s == "END") break; + if (s[0] == 'B') { + string title = s.substr(8, s.size() - 9); + string author = titles.find(title)->second; + mmssd::iterator it = shelve.lower_bound(author); + while (true) { + if (it->second == title) break; + it++; + } + shelve.erase(it); + } + if (s[0] == 'R') { + string title = s.substr(8, s.size() - 9); + string author = titles.find(title)->second; + returns.insert(pss(author, title)); + continue; + } + if (s[0] == 'S') { + for (mmss::iterator it = returns.begin(); it != returns.end(); it++) { + string author = it->first; + string title = it->second; + mmssd::iterator it2 = shelve.lower_bound(author); + while (it2 != shelve.end()) { + if (it2->first < author) break; + if (it2->second < title) break; + it2++; + } + if (it2 == shelve.end()) { + cout << "Put \"" << title << "\" first\n"; + } else { + cout << "Put \"" << title << "\" after \"" << it2->second << "\"\n"; + } + shelve.insert(pss(author, title)); + } + cout << "END\n"; + returns.clear(); + } + } +} diff --git a/uva_cpp_clean/231/231-14.cpp b/uva_cpp_clean/231/231-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c6da6630cbebdccd51c41ec475185f7e07ac1f39 --- /dev/null +++ b/uva_cpp_clean/231/231-14.cpp @@ -0,0 +1,95 @@ +/*************************************************** + * Problem name : 231 Testing the CATCHER .cpp + * Problem Link : https://uva.onlinejudge.org/external/2/p231.pdf + * OJ : Uva + * Verdict : AC + * Date : 14.06.2017 + * Problem Type : LDS(DP) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int L[MAX]; +int LIS(int n, vectorar) { + for (int i = 0; i < n; i++) { + L[i] = 1; + } + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (ar[j] < ar[i]) { + if (L[j] < L[i] + 1) { + L[j] = L[i] + 1; + } + } + } + } + int mx = 0; + for (int i = 0; i < n; i++) { + if (mx < L[i]) { + mx = L[i]; + } + } + return mx; +} +int main () { + vectorV; + int x , t = 1; + while (scanf("%d", &x) == 1 && x != -1) { + V.push_back(x); + while (scanf("%d", &x) == 1 && x != -1) { + V.push_back(x); + } + int n = V.size(); + + int result = LIS(n, V); + if (t != 1) printf("\n"); + printf("Test #%d:\n", t++); + printf(" maximum possible interceptions: %d\n", result); + //~ if (t != 1) printf("\n"); + V.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/231/231-20.cpp b/uva_cpp_clean/231/231-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f9c509ad07090b55f466c2c469c4aa36e6d93e2 --- /dev/null +++ b/uva_cpp_clean/231/231-20.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +const int MAXN = 32767; +vector sequence; +int dp[MAXN+2]; + +int main( int argc, char ** argv ) { + int nCase = 0, height; + while( cin >> height && height != -1 ) { + sequence.push_back( height ); + while( cin >> height && height != -1 ) sequence.push_back( height ); + + for( int i = 0 ; i < sequence.size() ; ++i ) dp[i] = 1; + + int missile = 0; + for( int i = 0 ; i < sequence.size() ; ++i ) + for( int j = i + 1 ; j < sequence.size() ; ++j ) + if( (sequence[j] < sequence[i]) && (dp[j] < (dp[i] + 1)) ) { + dp[j] = dp[i] + 1; + missile = max( missile, dp[j] ); + } + + if( nCase ) puts( "" ); + printf( "Test #%d:\n", ++nCase ); + printf( " maximum possible interceptions: %d\n", missile ); + + sequence.clear(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/231/231-21.cpp b/uva_cpp_clean/231/231-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d819ee1ea531cb7a006d5b50ea5ca405a7e1555 --- /dev/null +++ b/uva_cpp_clean/231/231-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 231 + Name: Testing the CATCHER + Problem: https://onlinejudge.org/external/2/231.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int M[1000000], X[1000000], cnt; + for (int cse=1;;cse++) { + for (cnt=0; ;cnt++) { + M[cnt] = 0; + cin>>X[cnt]; + if (X[cnt] < 0) break; + } + if (!cnt) break; + if (cse>1) cout << endl; + + int l=0; + for (int i=0; i>1; + if (X[M[mid]] >= X[i]) + lo = mid+1; + else + hi = mid-1; + } + + M[lo] = i; + if (lo > l) l = lo; + } + + printf("Test #%d:\n maximum possible interceptions: %d\n", cse, l); + } +} diff --git a/uva_cpp_clean/231/231-4.cpp b/uva_cpp_clean/231/231-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80080e7c88885de78b3538ddeff50bc51390ab65 --- /dev/null +++ b/uva_cpp_clean/231/231-4.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int main() +{ + int numbers = 0, caseN = 0, input[1000] = {0}; + + int x; + while (cin >> x) + { + if (x < 0) + break; + input[0] = x; + numbers = 1; + + while (cin >> x) + { + input[numbers] = x; + if (x < 0) + break; + numbers++; + } + + int m[1000] = {0}; + for (int i = 0; i < numbers; i++) + { + for (int j = i + 1; j < numbers; j++) + { + if (m[j] < m[i] + 1 && input[j] < input[i]) + m[j] = m[i] + 1; + } + } + + int out = 0; + for (int i = 0; i < numbers; i++) + { + if (out < m[i]) + out = m[i]; + } + out++; + + if (caseN) + cout << endl; + cout << "Test #" << ++caseN << ":" << endl; + cout << " maximum possible interceptions: " << out << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/231/231-9.cpp b/uva_cpp_clean/231/231-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4eb08f337b8708c0b4a9c73c926b2cac256bc399 --- /dev/null +++ b/uva_cpp_clean/231/231-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int tc, n, ans, v[1010], lds[1010]; + +int main(){ + while(scanf("%d", &v[0]), v[0] != -1){ + if(tc) puts(""); + lds[0] = 1, n = ans = 1; + while(scanf("%d", &v[n]), v[n] != -1) lds[n++] = 1; + for(int i = 1; i < n; i++){ + for(int j = 0; j < i; j++) + if(v[i] < v[j] && lds[i] < lds[j] + 1) + lds[i] = lds[j] + 1; + if(lds[i] > ans) ans = lds[i]; + } + printf("Test #%d:\n", ++tc); + printf(" maximum possible interceptions: %d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/232/232-17.cpp b/uva_cpp_clean/232/232-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ecde187e1b83b29ac354e16faef96ad8af16c3e --- /dev/null +++ b/uva_cpp_clean/232/232-17.cpp @@ -0,0 +1,50 @@ +/** + * AOAPC II 3-6 Puzzle + * ICPC WF 1994 + * I/O + * 47 AC + */ +#include +using namespace std; +int main() { + int r, c, ds = 0; + while ((cin >> r) && r) { + cin >> c; + vector puz(r); + for (string &row : puz) + cin >> row; + vector> num(r, vector(c, 0)); + int n = 0; + for (int row = 0; row < r; ++row) + for (int col = 0; col < c; ++col) + if (puz[row][col] != '*' && (!row || !col || puz[row - 1][col] == '*' || puz[row][col - 1] == '*')) + num[row][col] = ++n; + if (ds) + cout << '\n'; + cout << "puzzle #" << ++ds << ":\n"; + cout << "Across\n"; + for (int row = 0; row < r; ++row) + for (int col = 0; col < c; ++col) { + if (puz[row][col] == '*') + continue; + cout << setw(3) << num[row][col] << "." << puz[row][col]; + while (++col < c && puz[row][col] != '*') + cout << puz[row][col]; + cout << '\n'; + } + cout << "Down\n"; + for (int row = 0; row < r; ++row) + for (int col = 0; col < c; ++col) { + if (puz[row][col] == '*') + continue; + cout << setw(3) << num[row][col] << "." << puz[row][col]; + puz[row][col] = '*'; + int rr = row; + while (++rr < r && puz[rr][col] != '*') { + cout << puz[rr][col]; + puz[rr][col] = '*'; + } + cout << '\n'; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/232/232-21.cpp b/uva_cpp_clean/232/232-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f02a4bb1b4d95bba7cb41197f953a4429142f5c8 --- /dev/null +++ b/uva_cpp_clean/232/232-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 232 + Name: Crossword Answers + Problem: https://onlinejudge.org/external/2/232.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + int r, c; + for (int cse=1; cin >> r >> c; ++cse) { + if (cse>1) cout << '\n'; + + char M[12][12] = {}; + for (int i=1; i<=r; ++i) { + cin >> &M[i][1]; + for (int j=1; j<=c; ++j) + if (M[i][j] == '*') + M[i][j] = 0; + } + + int id = 0; + cout << "puzzle #" << cse << ":\nAcross\n"; + for (int i=1; i<=r; ++i) + for (int j=1; j<=c; ++j) if(M[i][j]) { + id += !M[i-1][j] || !M[i][j-1]; + if (!M[i][j-1]) + cout << setw(3) << id << '.' << &M[i][j] << '\n'; + } + + id = 0; + cout << "Down\n"; + for (int i=1; i<=r; ++i) + for (int j=1; j<=c; ++j) if(M[i][j]) { + id += !M[i-1][j] || !M[i][j-1]; + if (!M[i-1][j]) { + cout << setw(3) << id << '.'; + for (int k=i; M[k][j]; ++k) + cout << M[k][j]; + cout << '\n'; + } + } + } +} diff --git a/uva_cpp_clean/234/234-21.cpp b/uva_cpp_clean/234/234-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e86a4c20bdfc077a49c237152b24b36b60266b0 --- /dev/null +++ b/uva_cpp_clean/234/234-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 234 + Name: Switching Channels + Problem: https://onlinejudge.org/external/2/234.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct Alignment { + int p, t; + bool operator < (const Alignment &o) const { return t < o.t; } +} +A[17]; +int X[17], n, m; + + +bool used[17]; +int M[5], P[17], bestM[5], bestP[17]; +void bt(int idx, int sum, int ia) { + int k; + for (k=0; k<5 && M[k]==bestM[k]; ++k); + if (k>=5 || M[k]>=bestM[k]) return; + + if (idx==n) { + memcpy(bestM, M, sizeof(M)); + memcpy(bestP, P, n*sizeof(int)); + return; + } + + for (int i=0; i=A[j].t; ++j) + M[A[j].p-1] += min(sum+X[i]-A[j].t, A[j].t-sum); + + bt(idx+1, sum+X[i], j); + + for (j=ia; j=A[j].t; ++j) + M[A[j].p-1] -= min(sum+X[i]-A[j].t, A[j].t-sum); + + used[i] = 0; + } +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; cin >> n && n; ++cse) { + int sum = 0; + for (int i=0; i> X[i]; + sum += X[i]; + } + + cin >> m; + for (int i=0; i> A[i].p >> A[i].t; + sort(A, A+m); + + memset(bestM, 127, sizeof(bestM)); + memset(M, 0, sizeof(M)); + for (int i=0; i sum) + M[A[i].p-1] += A[i].t - sum; + + bt(0, 0, 0); + + int err = 0; + for (int i=0; i<5; ++i) + err += bestM[i]; + + cout << "Data set " << cse << "\nOrder:"; + for (int i=0; i +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct Node { + set characters; + int weight; + + bool operator<(const Node &other) const { + if (weight != other.weight) + return weight > other.weight; + return *characters.begin() > *other.characters.begin(); + } +}; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, r, n, count = 1, weighted_sum, total_chars, frequency[26]; + const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const char number[] = "0123456789"; + + while (cin >> r && r) { + cin >> n; + weighted_sum = total_chars = 0; + priority_queue build_huffman_tree; + unordered_map code; + + for (i = 0; i < n; i++) { + cin >> frequency[i]; + total_chars += frequency[i]; + build_huffman_tree.push(Node{{alphabet[i]}, frequency[i]}); + } + // 注意 i == build_huffman_tree.size() + // R大於2時,如果字不夠就要加假想字 + if (r > 2) + while ((i % (r - 1)) != 1) { + build_huffman_tree.push(Node{{']'}, 0}); // 加的字要比Z大 + i++; + } + + // 建構霍夫曼編碼 + while (i > 1) { + Node new_combination = {{}, 0}; + // 依次取最接近的 r 個一組 + for (j = 0; j < r; j++) { + Node now = build_huffman_tree.top(); + build_huffman_tree.pop(); + i--; + // C++11 range based,[PYTHON] for x in now.characters: + for (const char &c : now.characters) { + code[c] += number[j]; + new_combination.characters.insert(c); // 新編號加入舊編號,注意: 這裡用 set 維持字典序由小到大 + } + new_combination.weight += now.weight; // 新權重是 r 個權重和 + } + build_huffman_tree.push(new_combination); + i++; + } + + for (i = 0; i < n; i++) + weighted_sum += frequency[i] * code[alphabet[i]].size(); + + cout << "Set " << count++ << "; average length " << setprecision(2) << fixed << double(weighted_sum) / total_chars << "\n"; + + for (i = 0; i < n; i++) { + reverse(code[alphabet[i]].begin(), code[alphabet[i]].end()); + cout << " " << alphabet[i] << ": " << code[alphabet[i]] << "\n"; + } + cout << "\n"; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/242/242-15.cpp b/uva_cpp_clean/242/242-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70ee37dc2cc624b45d7b5b2d6a1c9b358b1df1f1 --- /dev/null +++ b/uva_cpp_clean/242/242-15.cpp @@ -0,0 +1,77 @@ +/* + 找零錢問題 + 時間複雜度: O(N^2logN) (排序) +*/ +#include +#include +#include +#include + +using namespace std; + +struct stamp_data { + int* denominations; + int stamp_types; +}; + +bool cmp(stamp_data a, stamp_data b) { + if (a.stamp_types == b.stamp_types) { + for (int i = a.stamp_types - 1; i > -1; i--) + if (a.denominations[i] > b.denominations[i]) + return false; + return true; + } + return a.stamp_types < b.stamp_types; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + vector answer(10); + int s, n, i, j, m, temp, k, value; + + while (cin >> s && s) { + value = 0; + answer.clear(); + + cin >> n; + + for (i = 0; i < n; i++) { + cin >> m; + int dp[1101] = {}; + int* denominations = new int[10]; + + // 解完全背包問題 + for (j = 0; j < m; j++) { + cin >> temp; + denominations[j] = temp; + dp[temp] = 1; + for (k = 1; k < 1001; k++) + if (dp[k] && dp[k] < s && (dp[k + temp] > dp[k] + 1 || dp[k + temp] == 0)) + dp[k + temp] = dp[k] + 1; + } + // 找第一個組不出來的數字 + j = 1; + while (dp[j]) + j++; + j--; + // j > value時,有更好的解所以清空之前累積的答案再放入當前面額組合 + // j == value時,就放入當前的面額組合 + if (j > value) { + value = j; + answer.clear(); + answer.push_back(stamp_data{denominations, m}); + } else if (j == value) { + answer.push_back(stamp_data{denominations, m}); + } + } + // 照題意排序,最小的就是題目要的 + sort(answer.begin(), answer.end(), cmp); + cout << "max coverage =" << setw(4) << value << " :"; + for (i = 0; i < answer[0].stamp_types; i++) + cout << setw(3) << answer[0].denominations[i]; + cout << '\n'; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/245/245-15.cpp b/uva_cpp_clean/245/245-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c40c09d7dccdb74008eea8c8dc2a0a4f658787e --- /dev/null +++ b/uva_cpp_clean/245/245-15.cpp @@ -0,0 +1,72 @@ +/* + 連結串列暴力解 + 時間複雜度: O(N) +*/ +#include +#include +#include + +using namespace std; + +struct Node { + string word; + Node *next; +}; + +int main() { + Node *head = new Node{"", NULL}; + char c = getchar(); + + while (1) { + if (isdigit(c)) { + if (c == '0') + break; + + int i, num = 0; + Node *now = head, *prev = new Node{"", NULL}; + // 讀取後續數字部分,最後多的那個getchar + // 剛好會回到最外面迴圈 + while (isdigit(c)) { + num = num * 10 + c - '0'; + c = getchar(); + } + // 找到連結串列中的數字 + for (i = 1; i < num; i++) { + prev = now; + now = now->next; + } + // 如果數字不是1就要把那個node移到第一個 + if (now != head) { + prev->next = now->next; + now->next = head; + head = now; + } + // 數字代表的單字會在第一個 + cout << head->word; + } else if (isalpha(c)) { + string word; + // 讀取整個文字 + while (isalpha(c)) { + word += c; + c = getchar(); + } + + cout << word; + // 在連結串列中新增一個節點 + Node *now = new Node{word, head}; + head = now; + + } else { + putchar(c); + c = getchar(); + } + } + // 釋放掉所有節點的記憶體位置 + Node *temp = head, *t; + while (temp->next != NULL) { + t = temp->next; + delete temp; + temp = t; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/245/245-9.cpp b/uva_cpp_clean/245/245-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db3d3abbf6115d3e378548e945c5707c6042e932 --- /dev/null +++ b/uva_cpp_clean/245/245-9.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int id; +string line, word, tmp; +deque words; + +void putWord(int id) { + tmp = words[id]; + printf("%s", tmp.c_str()); + words.erase(words.begin() + id); + words.push_front(tmp); +} + +void uncompress() { + word = ""; + for (int i = 0; i < line.size(); i++) + if ( isdigit(line[i]) ) { + id = line[i] - '0'; + while (i + 1 < line.size() and isdigit(line[i + 1])) id = id * 10 + (line[++i] - '0'); + putWord(--id); + word = ""; + } + else if ( isalpha(line[i]) ) word += line[i]; + else { + printf("%s", word.c_str()); + putchar(line[i]); + if (not word.empty()) words.push_front(word); + word = ""; + } + if (not word.empty()) { + printf("%s", word.c_str()); + words.push_front(word); + } + putchar('\n'); +} + +int main() { + while (getline(cin, line), line[0] != '0') uncompress(); + return(0); +} \ No newline at end of file diff --git a/uva_cpp_clean/247/247-13.cpp b/uva_cpp_clean/247/247-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d27644ded4a2846a9354feaae6e1e0ad2a2f8cd2 --- /dev/null +++ b/uva_cpp_clean/247/247-13.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, m, dfsCount, num, t = 1; + +stack points; +vector visited; +map numbs; +map names; +vector dfs_num, dfs_low; +vector < vector > people; +vector < vector > ans; + +void show() +{ + if (t != 1) cout << endl; + + cout << "Calling circles for data set " << t++ << ":" << endl; + + for (int i = (int)ans.size() - 1; i >= 0; i--) + { + vector temp = ans[i]; + + if (temp.size() != 0) + { + cout << temp[temp.size() - 1]; + + for (int j = temp.size() - 2; j >= 0; j--) + { + cout << ", " << temp[j]; + } + + cout << endl; + } + } +} + +void initialize(int n) +{ + num = dfsCount = 1; + numbs.clear(); + names.clear(); + + visited.clear(); + visited.resize(n , 0); + + dfs_low.clear(); + dfs_low.resize(n, -1); + + dfs_num.clear(); + dfs_num.resize(n, -1); + + people.clear(); + people.resize(n); + + ans.clear(); + ans.resize(n); +} + +void dfs(int start) +{ + visited[start] = true; + + points.push(start); + + dfs_low[start] = dfs_num[start] = dfsCount++; + + for (int i = 0; i < (int)people[start].size(); i++) + { + int child = people[start][i]; + + if (dfs_num[child] == -1) dfs(child); + + if (visited[child]) dfs_low[start] = min(dfs_low[child], dfs_low[start]); + } + + if (dfs_num[start] == dfs_low[start]) + { + vector subAns; + + while (points.top() != start) + { + subAns.push_back(numbs[points.top()]); + + visited[points.top()] = false; + + points.pop(); + } + + subAns.push_back(numbs[points.top()]); + + visited[points.top()] = false; + + points.pop(); + + ans.push_back(subAns); + } +} + +int main() +{ + while (cin >> n >> m, n != 0 || m != 0) + { + initialize(n + 1); + + for (int i = 0; i < m; i++) + { + string v, w; + + cin >> v >> w; + + if (!names[v]) names[v] = num++; + if (!names[w]) names[w] = num++; + + numbs[names[v]] = v; + numbs[names[w]] = w; + + people[names[v]].push_back(names[w]); + } + + for (int i = 1; i <= n; i++) + { + if (dfs_num[i] == -1) dfs(i); + } + + show(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/247/247-14.cpp b/uva_cpp_clean/247/247-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ece9fe7008c76884c288034373ea861e4bbe43b1 --- /dev/null +++ b/uva_cpp_clean/247/247-14.cpp @@ -0,0 +1,151 @@ +/*************************************************** + * Problem Name : 247 Calling Circles.cpp + * Problem Link : https://uva.onlinejudge.org/external/2/247.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-10-26 + * Problem Type : Graph (SCC) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 30 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX], adjR[MAX], order, component; +bool vis[MAX]; +void dfs (int u) { + vis[u] = true; + + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (!vis[v]) dfs (v); + } + + order.pb (u); +} +void dfs1 (int u) { + vis[u] = true; + component.pb (u); + + for (int i = 0; i < (int) adjR[u].size(); i++) { + int v = adjR[u][i]; + + if (!vis[v]) dfs1 (v); + } +} +int main () { + __FastIO; + int n, m, t = 1; + + while (cin >> n >> m) { + if (n == 0 && m == 0) break; + + mapmp; + mapmpS; + int k = 1; + + for (int i = 0; i < m; i++) { + string a, b; + cin >> a >> b; + + if (mp[a] == 0) { + mp[a] = k++; + mpS[mp[a]] = a; + } + + if (mp[b] == 0) { + mp[b] = k++; + mpS[mp[b]] = b; + } + + adj[mp[a]].pb (mp[b]); + adjR[mp[b]].pb (mp[a]); + } + + SET (vis, false); + order.clear(); + + for (int i = 1; i <= n; i++) { + if (!vis[i]) dfs (i); + } + + SET (vis, false); + + if (t != 1) cout << "\n"; + + cout << "Calling circles for data set " << t++ << ":\n"; + + for (int i = 1; i <= n; i++) { + int v = order[n - i]; + + if (!vis[v]) { + dfs1 (v); + + for (int j = 0; j < (int) component.size(); j++) { + cout << mpS[component[j]]; + + if (j != (int) component.size() - 1) cout << ", "; + } + + nl; + component.clear(); + } + } + + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + adjR[i].clear(); + } + } + + return 0; +} diff --git a/uva_cpp_clean/247/247-16.cpp b/uva_cpp_clean/247/247-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7836c6deda80f3a1887209e203137fe6c774ad81 --- /dev/null +++ b/uva_cpp_clean/247/247-16.cpp @@ -0,0 +1,80 @@ +//============================================================================ +// Name : 247 - Calling Circles.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 247 Calling Circles in C++, Ansi-style +// Run Time : 0.020 seconds +//============================================================================ + +#include +#include +#include +using namespace std; + +#define FRI(i, a, b) for(i = a; i < b; ++i) + +int main() { + int i, j, k, a, b; + int n, m, cases = 0; + int index; + map caller; + string call1, call2; + + while (cin >> n >> m, n != 0 || m != 0) { + bool check[n][n]; + bool used[n]; + string name[n]; + + index = 0; + caller.clear(); + memset(check, 0, sizeof(check)); + memset(used, 0, sizeof(used)); + + while (m--) { + cin >> call1 >> call2; + FRI(i, 0, n) { + a = caller[call1]; + b = caller[call2]; + if (!a) { + name[index++] = call1; + a = caller[call1] = index; + } + if (!b) { + name[index++] = call2; + b = caller[call2] = index; + } + + check[i][i] = check[a - 1][b - 1] = true; + } + } + + FRI(k, 0, n) { + FRI(i, 0, n) { + FRI(j, 0, n) { + if(check[i][k] && check[k][j]) { + check[i][j] = true; + } + } + } + } + + if (cases) cout << endl; + cout << "Calling circles for data set " << ++cases << ":" << endl; + + FRI(i, 0, n) { + if(!used[i]) { + bool next = false; + FRI(j, 0, n) { + if((!used[j]) && check[i][j] && check[j][i]) { + if(next) cout << ", "; + cout << name[j]; + next = used[i] = used[j] = true; + } + } + cout << endl; + } + } + } + + return 0; +} diff --git a/uva_cpp_clean/247/247-19.cpp b/uva_cpp_clean/247/247-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e5e3b9a117ad14a7a1c2f8c5968e5178661fa25 --- /dev/null +++ b/uva_cpp_clean/247/247-19.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vii; +typedef vector vi; + +vector AdjList; +vi knocked; +int knocks; + +vi dfs_num, dfs_low; +vector visited; +stack s; +int counter, n0; + +unordered_map nametoid; +vector idtoname; + +void scc(int n, int p) { + visited[n] = true; + s.push(n); + dfs_num[n] = dfs_low[n] = counter++; + for (int i = 0; i < AdjList[n].size(); i++) { + if (dfs_num[AdjList[n][i]] == -1) { + scc(AdjList[n][i], n); + } + if (visited[AdjList[n][i]]) dfs_low[n] = min(dfs_low[n], dfs_low[AdjList[n][i]]); + } + if (dfs_num[n] == dfs_low[n]) { + bool first = false; + while (1) { + if (first) printf(", "); + else first = true; + int v = s.top(); s.pop(); + visited[v] = 0; + printf("%s", idtoname[v].c_str()); + if (n == v) break; + } + printf("\n"); + } +} + +int main() { + int n, m, c = 0; + bool first = false; + while (cin >> n >> m && !(n == 0 && m == 0)) { + if (first) printf("\n"); + else first = true; + c++; + int id = 0; + AdjList.assign(n, vi()); + idtoname.assign(n, ""); + nametoid.clear(); + for (int i = 0; i < m; i++) { + string s, d; + cin >> s >> d; + if (!nametoid.count(s)) nametoid[s] = id, idtoname[id] = s, id++; + if (!nametoid.count(d)) nametoid[d] = id, idtoname[id] = d, id++; + AdjList[nametoid[s]].push_back(nametoid[d]); + } + + n0 = counter = 0; + dfs_num.assign(n, -1); + dfs_low.assign(n, -1); + visited.assign(n, false); + + printf("Calling circles for data set %d:\n", c); + + for (int i = 0; i < n; i++) { + if (dfs_num[i] == -1) scc(i, i); + } + } + return 0; +} diff --git a/uva_cpp_clean/247/247-21.cpp b/uva_cpp_clean/247/247-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92f81161085c5158214d8f54daf7275a3c26152d --- /dev/null +++ b/uva_cpp_clean/247/247-21.cpp @@ -0,0 +1,91 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 247 + Name: Calling Circles + Problem: https://onlinejudge.org/external/2/247.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +#define MAXN 30 +int cnt; +map ids; +string names[MAXN]; + +int readId() { + string s; cin>>s; + + auto it = ids.find(s); + if (it == ids.end()) { + names[cnt] = s; + return ids[s] = cnt++; + } + return it->second; +} + + +list adj[MAXN]; +int low[MAXN], pre[MAXN], pcnt; +bool onstack[MAXN]; +stack S; + +void cc(int u) { + low[u] = pre[u] = pcnt++; + onstack[u] = 1; + S.push(u); + + for (int v: adj[u]) + if (pre[v] < 0) { + cc(v); + low[u] = min(low[u], low[v]); + } + else if (onstack[v]) + low[u] = min(low[u], pre[v]); + + if (low[u] == pre[u]) { + int v; + for (; (v=S.top()) != u; S.pop()) { + cout << names[v] << ", "; + onstack[v] = 0; + } + cout << names[v] << endl; + onstack[v] = 0; + S.pop(); + } +} + +int main(){ + int cse=1, n, m; + while (cin>>n>>m && (n||m)) { + for (int i=0; i 1) putchar('\n'); + printf("Calling circles for data set %d:\n",cse++); + for (int i=0; i + +using namespace std; + +const int MAX_V = 1010, LEN = 50; + +int tc, V, E, ct, a, b, dfsCounter, numSCC, dfs_num[MAX_V], dfs_low[MAX_V]; +bool vis[MAX_V]; +char name[LEN], word[MAX_V][LEN]; +vector AdjList[MAX_V], path; +map mp; + +void dfs(int u){ + dfs_low[u] = dfs_num[u] = ++dfsCounter; + vis[u] = true; + path.push_back(u); + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not dfs_num[v] ) dfs(v); + if( vis[v] ) dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + if( dfs_low[u] == dfs_num[u] ){ + numSCC++; + while(true){ + int v = path.back(); path.pop_back(); + vis[v] = false; + printf("%s", word[v]); + if( u != v ) printf(", "); + if( u == v ) break; + } + putchar('\n'); + } +} + +void TarjanSCC(){ + fill(dfs_num, dfs_num + V, 0); + fill(vis, vis + V, false); + dfsCounter = numSCC = 0; + for(int u = 0; u < V; u++) + if( not dfs_num[u] ) dfs(u); +} + +void solve(){ + if( tc++ ) putchar('\n'); + printf("Calling circles for data set %d:\n", tc); + TarjanSCC(); +} + +void read(){ + for(int i = 0; i < E; i++){ + scanf("%s ", name); + if( mp.find(name) == mp.end() ) mp[name] = ct, strcpy(word[ct++], name); + a = mp[name]; + scanf("%s\n", name); + if( mp.find(name) == mp.end() ) mp[name] = ct, strcpy(word[ct++], name); + b = mp[name]; + AdjList[a].push_back(b); + } +} + +void clear(){ + ct = 0; + mp.clear(); + for(int u = 0; u < V; u++) AdjList[u].clear(); +} + +int main(){ + while(scanf("%d %d\n", &V, &E), V | E){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/251/251-21.cpp b/uva_cpp_clean/251/251-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acc6fb3a8ac00d64a6c1eb38ca7e1bdffa7e338f --- /dev/null +++ b/uva_cpp_clean/251/251-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 251 + Name: Nondeterministic Trellis Automata + Problem: https://onlinejudge.org/external/2/251.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +void cleanup(string &s) { + bool used[26]={}; + int sz = 0; + for (int i=0; i> n >> m && n; ++cse) { + if (cse > 1) cout << "\n"; + cout << "NTA " << cse << "\n"; + + cin.ignore(100, '\n'); + for (int i=0; i=n-m ? "accept " : "reject ") << str << "\n"; + continue; + } + for (int i=0; i=0; --i) + for (int j=0; j<=i; ++j) { + for (int k1=0; k1 +#include +#include + +using namespace std; + +void aroundY(char *cube){ + char temp = cube[1]; + cube[1] = cube[2]; + cube[2] = cube[4]; + cube[4] = cube[3]; + cube[3] = temp; +} + +void aroundX(char *cube){ + char temp = cube[0]; + cube[0] = cube[4]; + cube[4] = cube[5]; + cube[5] = cube[1]; + cube[1] = temp; +} + +void aroundZ(char *cube){ + char temp = cube[0]; + cube[0] = cube[3]; + cube[3] = cube[5]; + cube[5] = cube[2]; + cube[2] = temp; +} + +bool equals(char *cube1, char *cube2){ + for (int i = 0; i < 6; i++){ + if (cube1[i] != cube2[i]) return false; + } + return true; +} + +bool rotate(char *cube1, char *cube2){ + for(int z = 0; z < 4; z++){ + for(int j = 0; j < 4; j++){ + if(equals(cube1, cube2)) return true; + aroundY(cube2); + } + for(int i = 0; i < 4; i++){ + if(equals(cube1, cube2)) return true; + aroundX(cube2); + } + aroundZ(cube2); + } + return false; +} + +int main(){ + string input; + while(getline(cin, input)){ + char cube1[6]; + char cube2[6]; + for(int i = 0; i < 12; i++){ + if(i < 6) cube1[i] = input[i]; + else cube2[i - 6] = input[i]; + } + if(rotate(cube1, cube2)) printf("TRUE\n"); + else printf("FALSE\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/253/253-21.cpp b/uva_cpp_clean/253/253-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4083f7d0d525e7825bf88a5ed5db5f7bf0c2c42 --- /dev/null +++ b/uva_cpp_clean/253/253-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 253 + Name: Cube painting + Problem: https://onlinejudge.org/external/2/253.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int rots[3][4] = { + {1, 2, 4, 3}, + {0, 3, 5, 2}, + {0, 1, 5, 4} +}; + +bool check(char *u, char *v) { + for (int i=0; i<3; i++) + for (int j=i; j<3; j++) { + int *r1 = rots[i], + *r2 = rots[j]; + + if (u[i]==v[j] && u[5-i]==v[5-j]) { + for (int k=0; k<4; k++) + if (u[r1[0]]==v[r2[(k+0)%4]] && + u[r1[1]]==v[r2[(k+1)%4]] && + u[r1[2]]==v[r2[(k+2)%4]] && + u[r1[3]]==v[r2[(k+3)%4]]) + return true; + } + else if (u[i]==v[5-j] && u[5-i]==v[j]) { + for (int k=0; k<4; k++) + if (u[r1[0]]==v[r2[(k+3)%4]] && + u[r1[1]]==v[r2[(k+2)%4]] && + u[r1[2]]==v[r2[(k+1)%4]] && + u[r1[3]]==v[r2[(k+0)%4]]) + return true; + } + } + return false; +} + +int main(){ + char w[20]; + while (cin>>w) + puts(check(w,w+6) ? "TRUE" : "FALSE"); +} diff --git a/uva_cpp_clean/253/253-9.cpp b/uva_cpp_clean/253/253-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4463484c1b21c566756915097a9e839c4bf1685f --- /dev/null +++ b/uva_cpp_clean/253/253-9.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int d, e, f; +char s[15]; +bool number[10], ok; + +bool isValid(int a, int b, int c){ + d = 5 - c, e = 5 - b, f = 5 - a; + memset(number, false, sizeof number); + number[a] = true, number[b] = true; + number[c] = true, number[d] = true; + number[e] = true, number[f] = true; + for(int i = 0; i <= 5; i++) + if(!number[i]) return false; + if(s[6] != s[a] || s[7] != s[b] || + s[8] != s[c] || s[9] != s[d] || + s[10] != s[e] || s[11] != s[f]) + return false; + return true; +} + +int main(){ + while(scanf("%s",s)==1){ + ok = false; + for(int a = 0; a <= 5; a++) + for(int b = 0; b <= 5; b++) + for(int c = 0; c <= 5; c++) + if(isValid(a, b, c)) + ok = true; + puts(ok ? "TRUE" : "FALSE"); + } + return(0); +} diff --git a/uva_cpp_clean/255/255-9.cpp b/uva_cpp_clean/255/255-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..568786780dc4535e031b5177c6aea382038bc583 --- /dev/null +++ b/uva_cpp_clean/255/255-9.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int K, Q, M, r1, c1, r2, c2, r3, c3; +int dx[] = {1, 0, -1, 0}; +int dy[] = {0, 1, 0, -1}; + +int main(){ + while(~scanf("%d %d %d", &K, &Q, &M)){ + if( K == Q ) puts("Illegal state"); + else{ + r1 = K / 8, c1 = K % 8; + r2 = Q / 8, c2 = Q % 8; + r3 = M / 8, c3 = M % 8; + if( not ( ( r2 == r3 and ( r1 != r3 or (r1 == r3 and not( min(c2, c3) < c1 and c1 < max(c2, c3) ) ) ) ) + or ( c2 == c3 and ( c1 != c3 or (c1 == c3 and not( min(r2, r3) < r1 and r1 < max(r2, r3) ) ) ) ) ) + or K == M or Q == M ) + puts("Illegal move"); + else{ + if( abs(r3 - r1) + abs(c3 - c1) == 1 ) + puts("Move not allowed"); + else{ + int ct = 0; + for(int it = 0; it < 4; it++){ + int row = r1 + dy[it], col = c1 + dx[it]; + if( row >= 0 and col >= 0 and row < 8 and col < 8 + and abs(row - r3) + abs(col - c3) != 1 ) + ct++; + } + if( ct ) + puts("Continue"); + else + puts("Stop"); + } + } + } + + } + return(0); +} diff --git a/uva_cpp_clean/256/256-15.cpp b/uva_cpp_clean/256/256-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..030fc963f3eb134b65caabfea52bd8a4519ad821 --- /dev/null +++ b/uva_cpp_clean/256/256-15.cpp @@ -0,0 +1,45 @@ +/* + 建表 + 時間複雜度: O(1) +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n; + + while (cin >> n) { + if (n == 2) { + cout << "00\n"; + cout << "01\n"; + cout << "81\n"; + } else if (n == 4) { + cout << "0000\n"; + cout << "0001\n"; + cout << "2025\n"; + cout << "3025\n"; + cout << "9801\n"; + } else if (n == 6) { + cout << "000000\n"; + cout << "000001\n"; + cout << "088209\n"; + cout << "494209\n"; + cout << "998001\n"; + } else if (n == 8) { + cout << "00000000\n"; + cout << "00000001\n"; + cout << "04941729\n"; + cout << "07441984\n"; + cout << "24502500\n"; + cout << "25502500\n"; + cout << "52881984\n"; + cout << "60481729\n"; + cout << "99980001\n"; + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/256/256-16.cpp b/uva_cpp_clean/256/256-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3126aef34961fa444f3ce5444f56e8442937bda5 --- /dev/null +++ b/uva_cpp_clean/256/256-16.cpp @@ -0,0 +1,31 @@ +//============================================================================ +// Name : 256 - Quirksome Squares.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 256 Quirksome Squares in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +#include +#include +using namespace std; +#define FRI(i, a, b) for(i = a; i < b; ++i) +#define MAX 1<<16 + +int main() { + int i, num; + int digit, lim, table[MAX]; + FRI(i, 0, MAX) table[i] = i * i; + while (cin >> digit) { + lim = (int)pow(10.0, digit/2); + FRI(i, 0, lim) { + num = table[i]/lim + table[i]%lim; + if (table[i] == num * num) { + cout.width(digit); + cout.fill('0'); + cout << table[i] << endl; + } + } + } + return 0; +} diff --git a/uva_cpp_clean/256/256-19.cpp b/uva_cpp_clean/256/256-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b04c2f2eb18e2341618c7a4e4f7101d2c90f8b9 --- /dev/null +++ b/uva_cpp_clean/256/256-19.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +int main() { + /*for (int i = 0; i <= 10000; i++) { + int sqrt = i*i; + if (sqrt < 100) { + int aux = sqrt%10 + sqrt/10; + aux *= aux; + if (aux == sqrt) printf("%d\n", sqrt); + } else if (sqrt < 10000) { + int aux = sqrt%100 + sqrt/100; + aux *= aux; + if (aux == sqrt) printf("%d\n", sqrt); + } else if (sqrt < 1000000) { + int aux = sqrt%1000 + sqrt/1000; + aux *= aux; + if (aux == sqrt) printf("%d\n", sqrt); + } else if (sqrt < 100000000) { + int aux = sqrt%10000 + sqrt/10000; + aux *= aux; + if (aux == sqrt) printf("%d\n", sqrt); + } + }*/ + char* s2 = "00\n01\n81\n"; // 0 and 1 appear in all outputs + char* s4 = "0000\n0001\n2025\n3025\n9801\n"; + char* s6 = "000000\n000001\n088209\n494209\n998001\n"; + char* s8 = "00000000\n00000001\n04941729\n07441984\n24502500\n25502500\n52881984\n60481729\n99980001\n"; + int n; + while (scanf("%d", &n) != EOF) { + if (n == 2) printf("%s", s2); + else if (n == 4) printf("%s", s4); + else if (n == 6) printf("%s", s6); + else if (n == 8) printf("%s", s8); + } + return 0; +} diff --git a/uva_cpp_clean/256/256-2.cpp b/uva_cpp_clean/256/256-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8596c476f0af2f0c3bb7138b5396dadd3bf3c03d --- /dev/null +++ b/uva_cpp_clean/256/256-2.cpp @@ -0,0 +1,46 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define M_PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +int main(){ + ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + vector v2 = {"00", "01", "81"}; + vector v4 = {"0000","0001","2025","3025","9801"}; + vector v6 = {"000000","000001","088209","494209","998001"}; + vector v8 = {"00000000","00000001","04941729","07441984","24502500","25502500","52881984","60481729","99980001"}; + int n; + while (cin >> n) { + if (n == 2) for (auto x: v2) cout << x << el; + else if (n == 4) for (auto x: v4) cout << x << el; + else if (n == 6) for (auto x: v6) cout << x << el; + else if (n == 8) for (auto x: v8) cout << x << el; + } + return 0; +} diff --git a/uva_cpp_clean/256/256-20.cpp b/uva_cpp_clean/256/256-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27e9ab90887890142fc9f6916a4394464cce2b9d --- /dev/null +++ b/uva_cpp_clean/256/256-20.cpp @@ -0,0 +1,18 @@ +#include +#include + +using namespace std; + +int main() +{ + int num; + + while( cin >> num ) + { + if( num==2 ) cout << "00\n01\n81" << endl; + if( num==4 ) cout << "0000\n0001\n2025\n3025\n9801" << endl; + if( num==6 ) cout << "000000\n000001\n088209\n494209\n998001" << endl; + if( num==8 ) cout << "00000000\n00000001\n04941729\n07441984\n24502500\n25502500\n52881984\n60481729\n99980001" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/256/256-9.cpp b/uva_cpp_clean/256/256-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3022db7422edb66f094993d0bb3be9ca1802c60e --- /dev/null +++ b/uva_cpp_clean/256/256-9.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int n,lim; + +int main(){ + while(scanf("%d", &n) == 1){ + lim = pow(10, n / 2); + for(int i = 0; i < lim; i++) + for(int j = 0; j < lim; j++) + if((i + j)*(i + j) == i * lim + j) + printf("%.*d\n",n,i * lim + j); + } + return(0); +} diff --git a/uva_cpp_clean/257/257-19.cpp b/uva_cpp_clean/257/257-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d669748c3ca20b5c48115f758a2d9038a2e318c2 --- /dev/null +++ b/uva_cpp_clean/257/257-19.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +string s; +int dp[260][260]; +bool mask[260][260]; + +void proc() { + for (int i = 0; i < s.length(); i++) dp[i][i] = 1, mask[i][i] = false; + for (int i = 0; i < s.length() - 1; i++) dp[i][i+1] = s[i] == s[i+1]? 2: 0, mask[i][i+1] = false; + for (int l = 2; l < s.length(); l++) { + for (int i = 0; i + l < s.length(); i++) { + dp[i][i + l] = s[i] == s[i+l] && dp[i+1][i + l - 1]? dp[i+1][i + l - 1] + 2: 0; + mask[i][i + l] = false; + } + } +} + +bool cmp(int p1, int p2, int l) { + for (int i = 0; i <= l; i++) { + if (s[p1 + i] != s[p2 + i]) return false; + } + return true; +} + +bool check(int i, int l) { + if (l <= 0) return true; + if (mask[i][i+l] || mask[i][i+l-1] || mask[i+1][i+l-1]) return false; + else return check(i+1, l-2); +} + +bool proc2() { + int pal1 = -1, len = 0; + for (int l = 2; l < s.length(); l++) { + int first = -1; + for (int i = 0; i + l < s.length(); i++) { + if (dp[i][i+l]) { + if (first == -1) { + first = i; + if (pal1 == -1) pal1 = i, len = l; + else if (check(i, l)) return true; + mask[i][i+l] = true; + } + else { + if (!cmp(first, i, l)) return true; + mask[i][i+l] = true; + } + } + } + } + return false; +} + +int main() { + while (cin >> s) { + proc(); + if (proc2()) printf("%s\n", s.c_str()); + } + return 0; +} diff --git a/uva_cpp_clean/257/257-9.cpp b/uva_cpp_clean/257/257-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..589a154b73bf60f2101c5353120f2b262b966810 --- /dev/null +++ b/uva_cpp_clean/257/257-9.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +string s; + +bool isPalindrome (const string s) { + string rs = s; + reverse(begin(rs), end(rs)); + return s == rs; +} + +bool contains (string x, string y) { + if (x.size() > y.size()) swap(x, y); + if (x.size() == 3) { + if (y.size() == 3) return x == y; + return y.substr(0, 3) == x or y.substr(1, 3) == x; + } + return y == x or y.substr(0, 3) == x.substr(0, 3) or + y.substr(0, 3) == x.substr(1, 3) or + y.substr(1, 3) == x.substr(0, 3) or + y.substr(1, 3) == x.substr(1, 3); +} + +bool isPalinword () { + set st; + for (int i = 0; i < s.size(); i++) + for (int j = i + 2; j <= i + 3 and j < s.size(); j++) + if (isPalindrome(s.substr(i, j - i + 1))) + st.insert(s.substr(i, j - i + 1)); + for (string x: st) + for (string y: st) + if (not contains(x, y)) return true; + return false; +} + +int main () { + while (cin >> s) if (isPalinword()) cout << s << endl; + return (0); +} diff --git a/uva_cpp_clean/259/259-21.cpp b/uva_cpp_clean/259/259-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..631ce20fdc2d4e31cc09d83332a8c94d2175e263 --- /dev/null +++ b/uva_cpp_clean/259/259-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 259 + Name: Software Allocation + Problem: https://onlinejudge.org/external/2/259.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +vector adj[26]; +int match[10],X[26]; +bool seen[10]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = true; + if (match[v] == -1 || bpm(match[v])) { + match[v] = u; + return true; + } + } + return false; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string line; + while (1) { + for (int i=0; i<26; ++i) + adj[i].clear(), + X[i] = 0; + + int kk=0; + while (getline(cin, line) && line[0]) { + ++kk; + int u = line[0]-'A'; + X[u] += line[1]-'0'; + for (int i=3; i +#include +#include +#include +#include +using namespace std; + +#define FRI(i, a, b) for(i = a; i < b; ++i) +#define FRD(i, a, b) for(i = a; i > b; --i) + +int main() { + int i, j, k; + int n, game = 0; + stack< pair > path; + pair current; + bool blackWin; + int di[] = {-1, -1, 0, 0, 1, 1}; + int dj[] = {-1, 0, -1, 1, 0, 1}; + + while (cin >> n, n != 0) { + bool check[n][n]; + string row[n]; + + path = stack< pair >(); + memset(check, false, sizeof(check)); + blackWin = false; + + FRI(i, 0, n) { + cin >> row[i]; + } + + FRI(j, 0, n) { + if (row[0][j] == 'b') { + check[0][j] = true; + path.push(make_pair(0, j)); + } + } + + while(!path.empty()) { + current = path.top(); + path.pop(); + + if (current.first == n-1) { + blackWin = true; + break; + } + + FRI (k, 0, 6) { + i = current.first + di[k]; + j = current.second + dj[k]; + + if (i >= 0 && i < n && j >= 0 && j < n && row[i][j] == 'b' && !check[i][j]) { + check[i][j] = true; + path.push(make_pair(i, j)); + } + } + } + + if (blackWin) { + cout << ++game << " B" << endl; + } else { + cout << ++game << " W" << endl; + } + } + return 0; +} diff --git a/uva_cpp_clean/260/260-18.cpp b/uva_cpp_clean/260/260-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f59ba1d1e252d3ea54faa7c54008b055314f0da5 --- /dev/null +++ b/uva_cpp_clean/260/260-18.cpp @@ -0,0 +1,53 @@ +#include +#include + +using namespace std; + +string matriz[201][201]; +string ganador; +const int direcciones[][2] = {{-1,-1},{-1,0},{0,-1},{0,1},{1,0},{1,1}}; + +void DFS(int n, string letraO, int i, int j){ + matriz[i][j] = ""; + if(letraO == "w" && j == n - 1){ + ganador = "W"; + }else if(letraO == "b" && i == n - 1){ + ganador = "B"; + } + for(int k = 0; k < 6; k++){ + int ni = i + direcciones[k][0]; + int nj = j + direcciones[k][1]; + if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue; + if(matriz[ni][nj] == letraO){ + DFS(n, letraO, ni, nj); + } + } +} + +int main(){ + int n, juego = 1; + string cadena; + while(true){ + cin >> n; + if(n == 0) break; + for(int i = 0; i < n; i++){ + cin >> cadena; + for(int j = 0; j < n; j++){ + matriz[i][j] = cadena.substr(j,1); + } + } + for(int i = 0; i < n; i++){ + if(matriz[i][0] == "w"){ + DFS(n, "w", i, 0); + } + } + for(int j = 0; j < n; j++){ + if(matriz[0][j] == "b"){ + DFS(n, "b", 0, j); + } + } + cout << juego << " " << ganador <>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 260 + Name: Il Gioco dell'X + Problem: https://onlinejudge.org/external/2/260.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int id[]={0, 1, -1, 1, -1, 0}; +int jd[]={1, 1, 0, 0, -1, -1}; + +#define MAXN 202 +char lines[MAXN][MAXN]; +bool seen[MAXN][MAXN]; +int n; + +bool dfs(int ii, int jj) { + if (ii<0 || jj<0 || ii>=n || jj>=n || seen[ii][jj] || lines[ii][jj]!='w') + return false; + + if (jj == n-1) + return true; + + seen[ii][jj] = 1; + + for (int k=0; k<6; k++) + if (dfs(ii+id[k], jj+jd[k])) + return true; + + return false; +} + +int main(){ + int cse=1; + while (cin>>n && n) { + cin.getline(lines[0], MAXN); + for (int i=0; i +using namespace std; +using vs=vector; +using vb=vector; +using vvb=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(int t=1;;t++){ + int n; + cin>>n; + if(!n)break; + vs a(n); + for(int i=0;i>a[i]; + vvb s(n,vb(n)); + functiondfs=[&](int y,int x){ + s[y][x]=1; + if(y>0&&x>0&&!s[y-1][x-1]&&a[y-1][x-1]=='w')dfs(y-1,x-1); + if(y>0&&!s[y-1][x]&&a[y-1][x]=='w')dfs(y-1,x); + if(x>0&&!s[y][x-1]&&a[y][x-1]=='w')dfs(y,x-1); + if(x + +#define SIZE 210 + +using namespace std; + +int tc, N; +char grid[SIZE][SIZE]; +bool ok; +int dr[] = {-1, -1, 0, 0, 1, 1}; +int dc[] = {-1, 0, -1, 1, 0, 1}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < N ); +} + +void floodfill(int r, int c, char ch1, char ch2){ + if( not is_possible(r, c) ) return; + if(grid[r][c] == 'w') return; + if( r == N - 1 ) ok = true; + grid[r][c] = 'w'; + for(int d = 0; not ok and d < 6; d++) + floodfill(r + dr[d], c + dc[d], ch1, ch2); +} + +int main(){ + while(scanf("%d\n", &N), N){ + for(int row = 0; row < N; row++) scanf("%s", grid[row]); + ok = false; + for(int col = 0; col < N; col++) + floodfill(0, col, 'b', 'w'); + if( ok ) printf("%d B\n", ++tc); + else printf("%d W\n", ++tc); + } + return(0); +} diff --git a/uva_cpp_clean/263/263-19.cpp b/uva_cpp_clean/263/263-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90c97ec71c858a16f1ccbf80c3039cc0213caa23 --- /dev/null +++ b/uva_cpp_clean/263/263-19.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +using namespace std; + +int main() { + unordered_set s; + int N; + int v[11]; + while (cin >> N && N) { + printf("Original number was %d\n", N); + s.clear(); + int n1, n2, c = 0; + do { + c++; + s.insert(N); + n1 = 0, n2 = 0; + int i = 0; + while (N) { + v[i] = N % 10; + N /= 10; + i++; + } + sort(v, v + i); + for (int j = 0; j < i; j++) { + n1 *= 10, n2 *= 10; + n1 += v[j], n2 += v[i - j -1]; + } + printf("%d - %d = %d\n", n2, n1, n2 - n1); + N = n2 - n1; + } while (!s.count(N)); + printf("Chain length %d\n\n", c); + } + return 0; +} diff --git a/uva_cpp_clean/263/263-21.cpp b/uva_cpp_clean/263/263-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b31cc8b76a1d4aaa15c0e017d15cf53a394c912 --- /dev/null +++ b/uva_cpp_clean/263/263-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 263 + Name: Number Chains + Problem: https://onlinejudge.org/external/2/263.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int T[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; +int nextNumber(int x, int &a, int &b) { + char F[10] = {}; + for (; x; x/=10) + ++F[x%10]; + + a = b = 0; + for (int i=0, j=0; i<10; ++i) + while (F[i]--) { + a = a*10 + i; + b+= T[j++] * i; + } + return b - a; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while (cin >> n && n) { + cout << "Original number was " << n << '\n'; + + unordered_set seen; + int x=n, a, b, len=0; + do { + x = nextNumber(x, a, b); + cout << b << " - " << a << " = " << x << '\n'; + ++len; + } + while (seen.emplace(x).second); + cout << "Chain length " << len << "\n\n"; + } +} diff --git a/uva_cpp_clean/264/264-15.cpp b/uva_cpp_clean/264/264-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5854cccc4a6f5d21e437d2b11f5392ae95f8271 --- /dev/null +++ b/uva_cpp_clean/264/264-15.cpp @@ -0,0 +1,32 @@ +/* + 分析題 + 時間複雜度: O(N) +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, i, line, total, pos; + + while (cin >> n) { + total = 0; + + for (i = 1; total < n; i++) + total += i; + + line = i - 1; // n 在第 i - 1 斜排 + pos = total - n; // n 是第 i - 1 斜排的倒數第 total - n + 1 個 + + cout << "TERM " << n << " IS "; + + if (line & 1) + cout << pos + 1 << '/' << line - pos << '\n'; + else + cout << line - pos << '/' << pos + 1 << '\n'; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/264/264-19.cpp b/uva_cpp_clean/264/264-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b4a19a938fe6cf8467264fd9c42af99ee4ebd86 --- /dev/null +++ b/uva_cpp_clean/264/264-19.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; + +void proc(int lim, int & a, int & b) { + int f = (-1 + floor(sqrt(1+8*lim)))/2; + int g = ((f+1)*f)/2; + if (g == lim) { + if (f%2 == 0) a = f, b = 1; + else b = f, a = 1; + } + else { + int dif = lim - g; + if ((f+1)%2 == 0) a = dif , b = f+2-dif; + else b = dif , a = f+2-dif; + } +} + +int main() { + int n; + while(scanf("%d", &n) != EOF) { + int a, b; + proc(n, a, b); + printf("TERM %d IS %d/%d\n", n, a, b); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/264/264-20.cpp b/uva_cpp_clean/264/264-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4bc20d341403906015c13e3cbe4601b329c1af1 --- /dev/null +++ b/uva_cpp_clean/264/264-20.cpp @@ -0,0 +1,27 @@ +#include + +int main() +{ + int n; + + while( scanf("%d", &n)!=EOF ) + { + int div = 1, term = 1; + while( term ad-hoc > sequences + difficulty: medium + date: 18/Feb/2020 + hint: use binary search on preprocessed f(x)=f(x-1)+x-1 + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +/* +1 2 3 4 5 6 ... x +1 2 4 7 11 16 ... f(x) + +f(x) = f(x-1)+x-1 + +f(10^4-1) > 10^7 +*/ + +int main() { + vector f((int)1e4); + f[0] = 1; + for (int x = 1; x < (int)1e4; x++) f[x] = f[x-1]+x-1; + + int n; + while (cin >> n) { + int x = upper_bound(f.begin(), f.end(), n) - f.begin() - 1; + + int num = 1 + (n-f[x]); + int den = x - (n-f[x]); + + if (x%2) swap(num, den); + printf("TERM %d IS %d/%d\n", n, num, den); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/271/271-20.cpp b/uva_cpp_clean/271/271-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d955041823f269b98630c45afd3d6439deb1995 --- /dev/null +++ b/uva_cpp_clean/271/271-20.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +int len; +char sentence[260]; +bool flag; +stack < char > check; + +int main() +{ + while( scanf("%s", sentence)!=EOF ) + { + while( !check.empty() ) check.pop(); + + len = strlen(sentence)-1, flag = true; + for(int i=len ; i>=0 ; i--) + { + if( sentence[i]>='p' && sentence[i]<='z' ) check.push(sentence[i]); + else if( sentence[i]=='N' && !check.size() ) flag = false; + else if( sentence[i]=='C' || sentence[i]=='D' || sentence[i]=='E' || sentence[i]=='I' ) + if( 2<=check.size() ) check.pop(); + else flag = false; + if( !flag ) break; + } + + if( flag && check.size()==1 ) printf("YES\n"); + else printf("NO\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/271/271-21.cpp b/uva_cpp_clean/271/271-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c10f076f1a9efd3250e03cd0089605c68e09761 --- /dev/null +++ b/uva_cpp_clean/271/271-21.cpp @@ -0,0 +1,36 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 271 + Name: Simply Syntax + Problem: https://onlinejudge.org/external/2/271.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool check(const char s[]) { + int toSee = 1; + for (int i=0; s[i]; ++i) { + if (!toSee) return 0; + + char ch = s[i]; + if (ch>='p' && ch<='z') --toSee; + else if (ch=='C' || ch=='D' || ch=='E' || ch=='I') ++toSee; + else if (ch!='N') return 0; + } + return !toSee; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char str[1024]; + while (cin >> str) + cout << (check(str) ? "YES\n" : "NO\n"); +} diff --git a/uva_cpp_clean/272/272-14.cpp b/uva_cpp_clean/272/272-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c4bc6ec3a3dc5debb5207b3b14e298598efac2c9 --- /dev/null +++ b/uva_cpp_clean/272/272-14.cpp @@ -0,0 +1,73 @@ +/*************************************************** + * Problem name : 272 - TEX Quotes.cpp + * Problem Link : https://uva.onlinejudge.org/external/2/272.pdf + * OJ : Uva + * Verdict : AC + * Date : 13.06.2017 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ll count = 0; + string s; + while (getline(cin, s)) { + ll l = s.size(); + for (ll i = 0; i < l; i++) { + if (s[i] == '"') { + count = count + 1; + if (count % 2 == 1) + printf("``"); + else + printf("''"); + } else + printf("%c", s[i]); + } + nl; + } + return 0; +} + diff --git a/uva_cpp_clean/272/272-15.cpp b/uva_cpp_clean/272/272-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ecc9c304f7c4752c9eac2557f3ded2ef8de53a0e --- /dev/null +++ b/uva_cpp_clean/272/272-15.cpp @@ -0,0 +1,28 @@ +/* + 字元代換 + 時間複雜度: O(N) +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + bool position = 0; // 紀錄是第奇數個還是第偶數個雙引號 + char c; + + while (cin.get(c)) { + if (c == '"') { + position = !position; + if (position) + cout << "``"; + else + cout << "''"; + + } else + cout << c; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/272/272-16.cpp b/uva_cpp_clean/272/272-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36763add31f727ed6135b5bac600377c884bf3d7 --- /dev/null +++ b/uva_cpp_clean/272/272-16.cpp @@ -0,0 +1,33 @@ +//============================================================================ +// Name : 272 - TEX Quotes.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 272 TEX Quotes in C++, Ansi-style +// Run Time : 0.020 seconds +//============================================================================ + +#include +using namespace std; +#define FRI(i, a, b) for(i = a; i < b; ++i) + +int main() { + int iter; + bool isf = true; + string text; + while (getline(cin, text)) { + while (true) { + iter = text.find('"', 0); + if (iter < 0) break; + if (text[iter] == '"') { + if (isf) { + text.replace(iter, 1, "``"); + } else { + text.replace(iter, 1, "''"); + } + isf = !isf; + } + } + cout << text << endl; + } + return 0; +} diff --git a/uva_cpp_clean/272/272-19.cpp b/uva_cpp_clean/272/272-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8a431227703ea3ac01340060803cbf280b81e43 --- /dev/null +++ b/uva_cpp_clean/272/272-19.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int main() { + char c; + bool open = false; + while (scanf("%c", &c) != EOF) { + if (c == '"') { + if (open) printf("''"); + else printf("``"); + open = !open; + } else printf("%c", c); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/272/272-21.cpp b/uva_cpp_clean/272/272-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb6cd0032d9b54cf53081d270fec770b00234297 --- /dev/null +++ b/uva_cpp_clean/272/272-21.cpp @@ -0,0 +1,27 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 272 + Name: TEX Quotes + Problem: https://onlinejudge.org/external/2/272.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + char ch; + bool f = false; + while (scanf("%c", &ch) == 1) { + if (ch == '"') { + cout << (f ? "''" : "``"); + f = !f; + } + else cout << ch; + } +} diff --git a/uva_cpp_clean/272/272-3.cpp b/uva_cpp_clean/272/272-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2f8f6ed7624b209dd83c50282a59fcd81719ca9 --- /dev/null +++ b/uva_cpp_clean/272/272-3.cpp @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +int main() +{ + bool open=false; + string mL; + int index; + + while ( getline(cin,mL) ) + { + index = 0; + while ( true ) + { + index = mL.find('"',index); + if ( index == string::npos ) + break; + else + if ( open ) + { + open = false; + mL.replace(index,1,"''"); + } + else + { + open = true; + mL.replace(index,1,"``"); + } + } + cout << mL << endl; + } +} + + + diff --git a/uva_cpp_clean/272/272-5.cpp b/uva_cpp_clean/272/272-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9d638ab4b8268211c2546aecb894c711090ce605 --- /dev/null +++ b/uva_cpp_clean/272/272-5.cpp @@ -0,0 +1,28 @@ +#include +#include +int main() +{ + char s[100000]; + long i,l,p=0; + while(gets(s)) + { + l=strlen(s); + for(i=0;i +#include + +int main(){ + int c; + bool ind = true; + while((c = getchar()) != EOF){ + if(c == '"' && ind) + printf("``"), ind = false; + else if(c == '"' && !ind) + printf("''"), ind = true; + else + putchar(c); + } + return(0); +} diff --git a/uva_cpp_clean/274/274-21.cpp b/uva_cpp_clean/274/274-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67fd497dfb1c7e953e75c098f2fd235aa5c96f3b --- /dev/null +++ b/uva_cpp_clean/274/274-21.cpp @@ -0,0 +1,82 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 274 + Name: Cat and Mouse + Problem: https://onlinejudge.org/external/2/274.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int n, hc, hm; +void dfs(int u, vector adj[], bool seen[]) { + seen[u] = true; + for (int v: adj[u]) + if (!seen[v]) + dfs(v, adj, seen); +} + +vector adjC[143], adjM[143], adjMr[143]; +bool cseen[143], mseen[143], dseen[143]; + +bool dfsf(int u) { + if (mseen[u] && u!=hm) + return true; + + dseen[u] = true; + for (int v: adjMr[u]) + if (!cseen[v] && !dseen[v] && dfsf(v)) + return true; + + return false; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, u, v; + cin >> T; + while (T--) { + cin >> n >> hc >> hm; --hc; --hm; + for (int i=0; i>u>>v && u>0) + adjC[u-1].push_back(v-1); + + while (cin.ignore(100,'\n') && cin.peek()!='\n') + if (cin >> u >> v) { + adjM[u-1].push_back(v-1); + adjMr[v-1].push_back(u-1); + } + + memset(cseen, 0, n); + dfs(hc, adjC, cseen); + + memset(mseen, 0, n); + dfs(hm, adjM, mseen); + + bool f1 = false; + for (int i=0; !f1 && i +#include +using namespace std; + +int main() { + int n, m; + while (cin >> n >> m && !(n == 0 && m == 0)) { + int c = 1; + unordered_map mv; + printf("."); + while (!(n == 0 || mv.count(n))) { + if (c%50 == 0) printf("\n"); + mv[n] = c; + c++; + n *= 10; + printf("%d", n/m); + n %= m; + } + printf("\n"); + if (n == 0) printf("This expansion terminates.\n\n"); + else printf("The last %d digits repeat forever.\n\n", c - mv[n]); + } + return 0; +} diff --git a/uva_cpp_clean/275/275-9.cpp b/uva_cpp_clean/275/275-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f320be8a28c2bfbcf6c5e4f93f74efdfcfe98374 --- /dev/null +++ b/uva_cpp_clean/275/275-9.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +const int SIZE = 1000; + +int ct, num, den, pos, mu, lambda, vis[SIZE], f[SIZE]; + +int main(){ + while(scanf("%d %d", &num, &den), num | den){ + num %= den; + pos = 0; + fill(vis, vis + den, 0); + while( vis[num] == 0 ){ + f[pos] = 10 * num / den; + vis[num] = pos; + num = ( num * 10 ) % den; + pos++; + } + lambda = pos - vis[num]; + mu = pos - lambda; + if( mu == 1 and f[0] == f[lambda] ) mu = 0; + putchar('.'); + ct = 1; + for(int it = 0; it < mu; ct++, it++){ + if( ct == 50 ) putchar('\n'), ct = 0; + printf("%d", f[it]); + } + if( not ( f[mu] == 0 and lambda == 1 ) ) + for(int it = mu; it < mu + lambda; ct++, it++){ + if( ct == 50 ) putchar('\n'), ct = 0; + printf("%d", f[it]); + } + putchar('\n'); + if( f[mu] == 0 and lambda == 1 ) + puts("This expansion terminates."); + else + printf("The last %d digits repeat forever.\n", lambda); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/278/278-16.cpp b/uva_cpp_clean/278/278-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a7c039da94e8e00b1ea105f97b678ef97e899f0 --- /dev/null +++ b/uva_cpp_clean/278/278-16.cpp @@ -0,0 +1,71 @@ +//============================================================================ +// Name : 278 - Chess.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 278 Chess in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +/* *** King *** + * o o o + * o K o + * o o o + */ + +/* *** Knight *** + * o x o + * o x o + * x x k x x + * o x o + * o x o + */ + +/* *** Rook *** + * o + * o + * o + * o o r o o o o o + * o + * o + * o + * o + */ + +/* *** Queen *** + * o o + * o o o + * o o o + * o o Q o o o o o + * o o o + * o o o + * o o + * o o + */ + +#include +#include +using namespace std; +#define FRI(i, a, b) for(i = a; i < b; ++i) + +int main() { + char ch; + int mc, m, n, ans; + cin >> mc; + while(mc--) { + cin >> ch >> m >> n; + switch (ch) { + case 'r': + case 'Q': + ans = min(m, n); + break; + case 'k': + ans = (m * n + 1) / 2; + break; + case 'K': + ans = ((m + 1) / 2) * ((n + 1) / 2); + break; + } + cout << ans << endl; + } + return 0; +} diff --git a/uva_cpp_clean/278/278-21.cpp b/uva_cpp_clean/278/278-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b0b8895e46201c2d3df205992b4c247e3ba25ea --- /dev/null +++ b/uva_cpp_clean/278/278-21.cpp @@ -0,0 +1,38 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 278 + Name: Chess + Problem: https://onlinejudge.org/external/2/278.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n, a, b; + char c; + cin>>n; + while (n--) { + cin>>c>>a>>b; + if (a>b) swap(a, b); + switch (c) { + case 'r': + case 'Q': + cout << a << endl; + break; + case 'k': + cout << ((a*b+1)>>1) << endl; + break; + case 'K': + if (a&1) a++; + if (b&1) b++; + cout << (a*b>>2) << endl; + } + } +} diff --git a/uva_cpp_clean/278/278-5.cpp b/uva_cpp_clean/278/278-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9d638ab4b8268211c2546aecb894c711090ce605 --- /dev/null +++ b/uva_cpp_clean/278/278-5.cpp @@ -0,0 +1,28 @@ +#include +#include +int main() +{ + char s[100000]; + long i,l,p=0; + while(gets(s)) + { + l=strlen(s); + for(i=0;i + +using namespace std; + +int tc, m, n; +char ch; + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + scanf("%c %d %d\n", &ch, &m, &n); + if( ch == 'r' or ch == 'Q') printf("%d\n", min(m, n)); + else if( ch == 'k' ) printf("%d\n", ( m * n + 1 ) / 2); + else printf("%d\n", ( ( n + 1 ) / 2 ) * (( m + 1 ) / 2 ) ); + } + return(0); +} diff --git a/uva_cpp_clean/278/main.cpp b/uva_cpp_clean/278/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cdceadd9938cf7aa5a6f89bcea0a7882eee800f --- /dev/null +++ b/uva_cpp_clean/278/main.cpp @@ -0,0 +1,21 @@ +#include +#include + +using namespace std; + +int main(){ + + char select; + int cases, x, y; + cin >> cases; + for(;cases>0;cases--){ + cin >> select >> x >> y; + if(select == 'r' || select == 'Q') cout << min(x,y) << endl; + else if(select == 'K') cout << ceil(x/2.0) * ceil(y/2.0) << endl; + else cout << ceil((x*y)/2.0) << endl; + + } + + +return 0; +} diff --git a/uva_cpp_clean/280/280-13.cpp b/uva_cpp_clean/280/280-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2240c8edd2fb709cc80000ae62dcff1fb3b07f0f --- /dev/null +++ b/uva_cpp_clean/280/280-13.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 200 + +int n, v, u, how, s; +bool visited[MAX]; +vector > graph; + +void init() +{ + graph.clear(); + graph.resize(n + 1); +} + +int bfs(int source) +{ + for (int i = 0; i < MAX; i++) + { + visited[i] = false; + } + + queue q; + + q.push(source); + + while (!q.empty()) + { + int x = q.front(); + + q.pop(); + + for (int i = 0; i < graph[x].size(); i++) + { + int y = graph[x][i]; + + if (visited[y] == false) + { + q.push(y); + + visited[y] = true; + } + } + } + + int ans = 0; + + for (int i = 1; i <= n; i++) + { + if (visited[i] == false) ans++; + } + + return ans; +} + +int main() +{ + while (cin >> n, n != 0) + { + init(); + + while (cin >> u, u != 0) + { + while (cin >> v, v != 0) + { + graph[u].push_back(v); + } + } + + cin >> how; + + for (int i = 0; i < how; i++) + { + int s; + + cin >> s; + + cout << bfs(s); + + for (int i = 1; i <= n; i++) + { + if (visited[i] == false) cout << " " << i; + } + + cout << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/280/280-14.cpp b/uva_cpp_clean/280/280-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23a272880ef9a005e90046539145aaf748e10743 --- /dev/null +++ b/uva_cpp_clean/280/280-14.cpp @@ -0,0 +1,104 @@ +/*************************************************** + * Problem name : 280 Vertex.cpp + * Problem Link : https://uva.onlinejudge.org/external/2/280.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-10-19 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,false,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 105 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +bool color[MAX]; +void dfs(int u){ + //color[u] = true; + int sz = adj[u].size(); + for(int i = 0; iV; + for(int j = 1; j<=n; j++){ + if(color[j] == false){ + cnt++; + V.push_back(j); + } + } + printf("%d", cnt); + for(int k = 0; k<(int)V.size(); k++){ + printf(" %d", V[k]); + } + nl; + } + for(int i = 0; i +#include +#include +using namespace std; + +typedef vector vi; +vector AdjList; +bool visited[100]; + +void visit(int v, int & count) { + if (visited[v]) return; + visited[v] = true, count++; + for (int i = 0; i < AdjList[v].size(); i++) { + visit(AdjList[v][i], count); + } +} + +int main() { + int n; + while (cin >> n && n != 0) { + AdjList.assign(n, vi()); + int v; + while (cin >> v && v != 0) { + int d; + while (cin >> d && d != 0) { + AdjList[v-1].push_back(d-1); + } + } + int c; + cin >> c; + while (c--) { + for (int i = 0; i < n; i++) visited[i] = false; + cin >> v; + int count = 0; + for (int i = 0; i < AdjList[v-1].size(); i++) { + visit(AdjList[v-1][i], count); + } + printf("%d", n - count); + for (int i = 0; i < n; i++) if (!visited[i]) printf(" %d", i+1); + printf("\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/280/280-21.cpp b/uva_cpp_clean/280/280-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c67a95d64fdb544bbb3e058409b83ae8e2e3fea9 --- /dev/null +++ b/uva_cpp_clean/280/280-21.cpp @@ -0,0 +1,58 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 280 + Name: Vertex + Problem: https://onlinejudge.org/external/2/280.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +list adj[110]; +int seen[110], cnt; +void dfs(int u) { + if (seen[u]) return; + seen[u] = 1; cnt++; + + for (int v: adj[u]) + dfs(v); +} + + +int main() { + int n, u, v, t; + while (cin>>n && n) { + for (int i=1; i<=n; i++) + adj[i].clear(); + + while (cin>>u && u) + while (cin>>v && v) + adj[u].push_back(v); + + + cin >> t; + for (int i=0; i>u; + cnt = 0; + for (int i=1; i<=n; i++) + seen[i] = 0; + + for (int v: adj[u]) + dfs(v); + + cout << n-cnt; + for (int i=1; i<=n; i++) + if (!seen[i]) + printf(" %d", i); + putchar('\n'); + } + } +} diff --git a/uva_cpp_clean/280/280-6.cc b/uva_cpp_clean/280/280-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..af554d1b1979b306553189de551fe9e006c59f89 --- /dev/null +++ b/uva_cpp_clean/280/280-6.cc @@ -0,0 +1,41 @@ +// https://uva.onlinejudge.org/external/2/280.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + while(1){ + int n,u,v,c; + cin>>n; + if(!n)break; + vvi g(n); + while(1){ + cin>>u; + if(!u)break; + u--; + while(1){ + cin>>v; + if(!v)break; + v--; + g[u].push_back(v); + } + } + cin>>c; + for(int i=0;idfs=[&](int i){ + s[i]=1; + for(int j:g[i]) + if(!s[j])dfs(j); + }; + cin>>u;u--; + for(int j:g[u])dfs(j); + vi a; + for(int i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 290 + Name: Palindroms <---> smordnilaP + Problem: https://onlinejudge.org/external/2/290.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +bool isPalin(const char str[], int l) { + for (int i=l>>1; i>=0; --i) + if (str[i] != str[l-i-1]) + return 0; + return 1; +} + +#define MAXL 10000 +char s1[MAXL], s2[MAXL]; + +int numsteps(int base, const char str[], int len) { + int l = len; + memcpy(s1, str, l); + for (int i=0; ; ++i) { + if (isPalin(s1, l)) return i; + + int carry = 0; + for (int i=0; i=2; --i) + if (i < mnb) printf(" ?"); + else printf(" %d", numsteps(i, str, l)); + putchar('\n'); + } +} diff --git a/uva_cpp_clean/291/291-16.cpp b/uva_cpp_clean/291/291-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c8c834831719e425d519b2c0b9966f3d5ef31b0 --- /dev/null +++ b/uva_cpp_clean/291/291-16.cpp @@ -0,0 +1,39 @@ +//============================================================================ +// Name : 291 - The House Of Santa Claus.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 278 The House Of Santa Claus in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +#include +#include +#include +using namespace std; +#define FRI(i, a, b) for (i = a; i < b; ++i) + +int mem[] = { 1, 2, 3, 1, 5, 3, 4, 5, 2 }; + +int main() { + int i, x, y; + bool ma; + do { + bool direct[5][5] = { {0, 1, 1, 0, 1}, {0, 0, 1, 0, 1}, {0, 0, 0, 1, 1}, {0, 0, 0, 0, 1}, {0, 0, 0, 0, 0} }; + ma = true; + FRI (i, 0, 8) { + x = min(mem[i] - 1, mem[i + 1] - 1); + y = max(mem[i] - 1, mem[i + 1] - 1); + if (!(direct[x][y]) || direct[y][x]) { + ma = false; + break; + } + direct[y][x] = true; + } + if (ma) { + FRI(i, 0, 9) cout << mem[i]; + cout << endl; + } + next_permutation(mem, mem + 9); + } while (mem[0] == 1); + return 0; +} diff --git a/uva_cpp_clean/291/291-19.cpp b/uva_cpp_clean/291/291-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8c0d333d7cac98bbe2593f8edbdafa115c3521b --- /dev/null +++ b/uva_cpp_clean/291/291-19.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +int mat[5][5]; +int sol[9]; + +void proc(int n, int p) { + sol[p] = n + 1; + if (p == 8) { + for (int i = 0; i < 9; i++) printf("%d", sol[i]); + printf("\n"); + } else { + for (int i = 0; i < 5; i++) { + if (mat[n][i]) { + mat[n][i] = mat[i][n] = 0; + proc(i, p+1); + mat[n][i] = mat[i][n] = 1; + } + } + } +} + +int main() { + for (int i = 0; i < 5; i++) + for (int j = 0; j < 5; j++) + mat[i][j] = 0; + mat[0][1] = mat[1][0] = mat[0][2] = mat[2][0] = + mat[0][4] = mat[4][0] = mat[1][2] = mat[2][1] = + mat[1][4] = mat[4][1] = mat[2][3] = mat[3][2] = + mat[2][4] = mat[4][2] = mat[3][4] = mat[4][3] = 1; + proc(0, 0); + return 0; +} diff --git a/uva_cpp_clean/291/291-21.cpp b/uva_cpp_clean/291/291-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e103d73d30fd91c59cf5af82bd162b7f6ebef3f --- /dev/null +++ b/uva_cpp_clean/291/291-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 291 + Name: The House Of Santa Claus + Problem: https://onlinejudge.org/external/2/291.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int mp[5][5], seen[5][5], sel[9]; +void rec(int id) { + if (id==9) { + for(int i=0; i<9; i++) + cout< +#include +#include + +struct node +{ + int value; + std::vector connected; +}; + +void init(node nodes[6]) +{ + for (int i = 0; i < 6; i++) + { + nodes[i].value = i; + } + + nodes[0].connected.push_back(&nodes[1]); + nodes[0].connected.push_back(&nodes[2]); + nodes[0].connected.push_back(&nodes[4]); + + nodes[1].connected.push_back(&nodes[0]); + nodes[1].connected.push_back(&nodes[2]); + nodes[1].connected.push_back(&nodes[4]); + + nodes[2].connected.push_back(&nodes[0]); + nodes[2].connected.push_back(&nodes[1]); + nodes[2].connected.push_back(&nodes[3]); + nodes[2].connected.push_back(&nodes[4]); + + nodes[3].connected.push_back(&nodes[2]); + nodes[3].connected.push_back(&nodes[4]); + + nodes[4].connected.push_back(&nodes[0]); + nodes[4].connected.push_back(&nodes[1]); + nodes[4].connected.push_back(&nodes[2]); + nodes[4].connected.push_back(&nodes[3]); +} + +bool isline(int x, int y, std::vector &nodes) +{ + if (x == y || nodes.size() < 2) + { + return false; + } + + for (int i = 0; i < (int)nodes.size() - 1; i++) + { + if ((nodes[i]->value == x && nodes[i + 1]->value == y) || (nodes[i]->value == y && nodes[i + 1]->value == x)) + { + return true; + } + } + + return false; +} + +int main() +{ + node nodes[6]; + init(nodes); + + std::queue> pending; + pending.push(std::vector{&nodes[0]}); + + while (!pending.empty()) + { + auto current = pending.front(); + pending.pop(); + + for (auto &child : current.back()->connected) + { + if (!isline(child->value, current.back()->value, current)) + { + auto n = current; + n.push_back(child); + pending.push(n); + } + } + + if (current.size() == 9) + { + for (auto &node : current) + { + std::cout << node->value + 1; + } + std::cout << std::endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/291/291-6.cc b/uva_cpp_clean/291/291-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..fc5a05e56d29304b40c06e315f9533c41c8f1992 --- /dev/null +++ b/uva_cpp_clean/291/291-6.cc @@ -0,0 +1,46 @@ +// https://uva.onlinejudge.org/external/2/291.pdf +#include +using namespace std; +using ii=tuple; +using vi=vector; +using vii=vector; +using vvi=vector; +using sii=set; +int main(){ + vvi g(6); + g[0].push_back(1); + g[0].push_back(2); + g[0].push_back(4); + g[1].push_back(0); + g[1].push_back(2); + g[1].push_back(4); + g[2].push_back(0); + g[2].push_back(1); + g[2].push_back(3); + g[2].push_back(4); + g[3].push_back(2); + g[3].push_back(4); + g[4].push_back(0); + g[4].push_back(1); + g[4].push_back(2); + g[4].push_back(3); + sii t; + vi p(1,0); + functions=[&](int u){ + if(p.size()==9){ + for(int x:p)cout< +#include +#include +#include + +using namespace std; + +#define MAX 10000010 + +typedef long long ll; +typedef vector vll; +typedef vector vb; + +ll l, h, t; +vb isPrime(MAX, true); +vll primes; + +void findPrimes() +{ + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; i++) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += i) + { + isPrime[j] =false; + } + + primes.push_back(i); + } + } +} + +ll sumDiv(ll n) +{ + ll ans = 1; + + ll p_index = 0, p = primes[p_index]; + + while (p * p <= n) + { + if (n % p == 0) + { + ll count = 0; + + while (n % p == 0) + { + n /= p; + count++; + } + + ans *= (count + 1); + } + + p = primes[++p_index]; + } + + if (n != 1) + { + ans *= (2); + } + + return ans; +} + +int main() +{ + cin >> t; + + findPrimes(); + + while (t--) + { + cin >> l >> h; + + ll sol = 0, sol_index = l; + + for (ll i = l; i <= h; ++i) + { + ll temp = sumDiv(i); + + if (sol < temp) + { + sol = temp; + sol_index = i; + } + } + + printf("Between %lld and %lld, %lld has a maximum of %lld divisors.\n", l, h, sol_index, sol); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/294/294-14.cpp b/uva_cpp_clean/294/294-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4b6aed913ed289a94097a9a385de46be7ebd919 --- /dev/null +++ b/uva_cpp_clean/294/294-14.cpp @@ -0,0 +1,110 @@ +/*************************************************** + * Problem name : 294 - Divisors.cpp + * Problem Link : https://uva.onlinejudge.org/external/2/294.pdf + * OJ : Uva + * Verdict : Ac + * Date : 2017-07-28 + * Problem Type : Number Theory + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +ll prime[MAX], arr[MAX], cnt, len, sum[MAX]; +void sieve(ll N) { + ll i, j; + cnt = 0; + len = sqrt(N); + for (i = 2; i <= len; i++) { + if (arr[i] == 0) { + for (j = i + i; j <= N; j += i) { + arr[j] = 1; + } + } + } + for (i = 2; i <= N; i++) { + if (arr[i] == 0) { + prime[cnt++] = i; + } + } +} +ll divisor(ll n) { + ll i, divN = 1, c; + len = sqrt(n); + for (i = 0; i < cnt && prime[i] <= len; i++) { + if (n % prime[i] == 0) { + c = 0; + while (n % prime[i] == 0) { + c++; + n /= prime[i]; + } + divN *= (c + 1); + } + } + if (n > 1) divN *= 2; + + return divN; +} +int main () { + ll tc, a, b; + scanf("%lld", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%lld %lld", &a, &b); + ll N = sqrt(b); + sieve(N); + ll mx = 0, ans; + for (ll i = a; i <= b; i++) { + ll NumberOfDivisor = divisor(i); + if (NumberOfDivisor > mx) { + mx = NumberOfDivisor; + ans = i; + } + } + printf("Between %lld and %lld, %lld has a maximum of %lld divisors.\n", a, b, ans, mx); + } + + return 0; +} diff --git a/uva_cpp_clean/294/294-15.cpp b/uva_cpp_clean/294/294-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54f95828e59b1f4b323ad5fc346d2885f1e6eb1f --- /dev/null +++ b/uva_cpp_clean/294/294-15.cpp @@ -0,0 +1,85 @@ +/* + 質因數分解 + 時間複雜度: O(NlogN) +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + int i, t = 2, j, not_prime[65540] = {0, 0, 1}, prime[6545] = {0, 2}; + int x, y, n_case, ans, m, not_found = 1, cnt, num = 1, k, ans_occur; + + // 建質數表 + for (j = 4; j < 65540; j += 2) { + not_prime[j] = 1; + } + for (i = 3; i < 257; i += 2) { + if (!not_prime[i]) { + prime[t++] = i; + for (j = i * i; j < 65540; j += i) { + not_prime[j] = 1; + } + } + } + for (i = 257; i < 65540; i++) { + if (!not_prime[i]) { + prime[t++] = i; + if (t == 6543) { + break; + } + } + } + + cin >> n_case; + while (n_case--) { + ans = 0; + cin >> x >> y; + // 每個值代公式算因數個數 + for (m = x; m <= y; m++) { + k = m; + t = 1; + i = prime[t]; + num = 1; + not_found = 1; + + while (k != 1 && t < 6543) { + cnt = 0; + if (k % i == 0) { + cnt = 0; + not_found = 0; + while (k % i == 0) { + k /= i; + cnt++; + } + num *= (cnt + 1); + } + t++; + i = prime[t]; + } + + if (k != 1) { + if (not_found) { + if (2 > ans) { + ans = 2; + ans_occur = m; + } + } else { + if (num * 2 > ans) { + ans = num * 2; + ans_occur = m; + } + } + } else { + if (num > ans) { + ans = num; + ans_occur = m; + } + } + } + cout << "Between " << x << " and " << y << ", " << ans_occur << " has a maximum of " << ans << " divisors.\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/294/294-20.cpp b/uva_cpp_clean/294/294-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..835e7fa334f979fb96a4697211a6f78eec7bf3b2 --- /dev/null +++ b/uva_cpp_clean/294/294-20.cpp @@ -0,0 +1,37 @@ +#include +#include + +int main() +{ + long tc; + + scanf("%ld", &tc); + while( tc-- ) + { + long L, U; + scanf("%ld %ld", &L, &U); + + long ans, div_num = -1; + for(long i=L ; i<=U ; i++) + { + long temp = sqrt(i), divisor = 0; + for(long j=1 ; j<=temp ; j++) + { + if( i%j==0 ) + { + divisor++; + if( !(i/j==j) ) divisor++; + } + } + + if( div_num>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 294 + Name: Divisors + Problem: https://onlinejudge.org/external/2/294.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#define maxp 32000 +#include +#include +#include +#include +#include + +using namespace std; + + + +list primes; +bool pt[maxp]; +void calcprimes(){ + int i,j; + for(i=2;i::iterator i; + int c=1,t; + unsigned long int y=x; + + for(i=primes.begin();i!=primes.end() && (*i)>n; + while (n--) + { + scanf("%lu%lu",&l,&u); + max=0; + for(i=l;i<=u;i++) + if((t=count(i))>max)max=t,m=i; + + printf("Between %lu and %lu, %lu has a maximum of %d divisors.\n",l,u,m,max); + } + return 0; +} diff --git a/uva_cpp_clean/294/294-5.cpp b/uva_cpp_clean/294/294-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..17658a50244ac251760d2e3c9914ca89913ddf2f --- /dev/null +++ b/uva_cpp_clean/294/294-5.cpp @@ -0,0 +1,85 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main() +{ +// input; +// output; + long long lwr,upp,t,j,i,divisor,Max,num; + cin>>t; + while(t--) + { + sc("%lld %lld",&lwr,&upp); + Max=0; + if(lwr==1&&upp==0) + { + printf("Between 1 and 1, 0 has a maximum of 2 divisors.\n"); + continue; + } + for(i=lwr;i<=upp;i++) + { + divisor=0; + int p=sqrt(i); + for(j=1;j<=p;j++){ + if(i%j==0){ + divisor++; + if(i/j!=j){ + divisor++; + } + } +// printf("%lld has a maximum of %lld divisors.\n",i,divisor); + if(Max>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 296 + Name: Safebreaker + Problem: https://onlinejudge.org/external/2/296.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int sameDigits(int x, int y) { + int cnt = 0; + for (int i=0; i<4; i++) { + if (x%10 == y%10) + cnt++; + x/=10; y/=10; + } + return cnt; +} + +int hasDigits(int x, int y) { + int hass[] = {0,0,0,0,0,0,0,0,0,0}; + + if (hass[0] || hass[1] || hass[2]) + cout << "!"; + + for (int i=0; i<4; i++) { + hass[y%10]++; + y /= 10; + } + + int cnt=0; + for (int j=0; j<4; j++) { + if (--hass[x%10] >= 0) cnt++; + x /= 10; + } + + return cnt; +} + +int poss[10000]; +int main() { + int T, n, x, num, cp, ce, scnt, hcnt; + scanf("%d", &T); + while (T--) { + scanf("%d", &n); + int sz = 10000; + for (int i=0; i<10000; i++) + poss[i] = i; + + for (int i=0; i + +#define SIZE 15 + +using namespace std; + +int tc, G, guess[SIZE][5], a[SIZE], b[SIZE], pos, aux, s[SIZE], ct, ans; +bool vis[SIZE]; + +bool is_correct(int num){ + memset(s, 0, sizeof s); + pos = 3; + while(num) s[pos--] = num % 10, num /= 10; + for(int it = 0; it < G; it++){ + int x = 0, y = 0; + memset(vis, false, sizeof vis); + for(int i = 0; i < 4; i++) + if( s[i] == guess[it][i] ) x++, vis[i] = true; + if( x != a[it] ) return false; + for(int i = 0; i < 4; i++) + if( guess[it][i] != s[i] ) + for(int j = 0; j < 4; j++) + if( not vis[j] and guess[it][i] == s[j] ) + vis[j] = true, y++, j = 4; + if( y != b[it] ) return false; + } + return true; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &G); + memset(guess, 0, sizeof guess); + for(int it = 0; it < G; it++){ + scanf("%d %d/%d", &aux, &a[it], &b[it]); + pos = 3; + while(aux) guess[it][pos--] = aux % 10, aux /= 10; + } + ct = ans = 0; + for(int it = 0; it < 10000; it++) + if( is_correct(it) ) ans = it, ct++; + if( ct == 0 ) puts("impossible"); + else if( ct == 1 ) printf("%.4d\n", ans); + else puts("indeterminate"); + } + return(0); +} diff --git a/uva_cpp_clean/297/297-17.cpp b/uva_cpp_clean/297/297-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eba6db956164e22cbf7a13dfc006cb559f4db875 --- /dev/null +++ b/uva_cpp_clean/297/297-17.cpp @@ -0,0 +1,48 @@ +/** + * AOAPC II Example 6-11 Quadtrees + */ +#include +using namespace std; +string tree; +int cnt; +auto img = vector>(32, vector(32, false)); +void draw(int &pos, int r, int c, int w) +{ + char cur = tree[pos++]; + if (cur == 'p') + { + draw(pos, r, c + w / 2, w / 2); + draw(pos, r, c, w / 2); + draw(pos, r + w / 2, c, w / 2); + draw(pos, r + w / 2, c + w / 2, w / 2); + } + else if (cur == 'f') + for (int i = r; i < r + w; ++i) + for (int j = c; j < c + w; ++j) + if (!img[i][j]) + { + img[i][j] = true; + ++cnt; + } +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int T; + cin >> T; + for (int k = 0; k < T; ++k) + { + if (k) + for (auto &line : img) + fill(line.begin(), line.end(), false); + cnt = 0; + for (int i = 0; i < 2; ++i) + { + cin >> tree; + int pos = 0; + draw(pos, 0, 0, 32); + } + cout << "There are " << cnt << " black pixels.\n"; + } +} diff --git a/uva_cpp_clean/299/299-15.cpp b/uva_cpp_clean/299/299-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b88724bb995bc9deeb37de8a89785af598d5b7b --- /dev/null +++ b/uva_cpp_clean/299/299-15.cpp @@ -0,0 +1,34 @@ +/* + 氣泡排序交換次數 + 時間複雜度: O(N^2) +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, n_testcase, n_car, cars[50], ans; + cin >> n_testcase; + + while (n_testcase--) { + ans = 0; + cin >> n_car; + + for (i = 0; i < n_car; i++) { + cin >> cars[i]; + } + + for (i = 0; i < n_car - 1; i++) + for (j = 0; j < n_car - i - 1; j++) + if (cars[j] > cars[j + 1]) { + swap(cars[j], cars[j + 1]); + ans++; + } + + cout << "Optimal train swapping takes " << ans << " swaps.\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/299/299-16.cpp b/uva_cpp_clean/299/299-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..034e6c664b6d94ef51aed9b36b48787b73edd90d --- /dev/null +++ b/uva_cpp_clean/299/299-16.cpp @@ -0,0 +1,33 @@ +//============================================================================ +// Name : 299 - Train Swapping.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 299 Train Swapping in C++, Ansi-style +// Run Time : 0.012 seconds +//============================================================================ + +#include +using namespace std; +#define FRI(i, a, b) for(i = a; i < b; ++i) +#define FRD(i, a, b) for(i = a; i > b; --i) + +int main() { + int i, j, n, m, ans; + int num[55]; + cin >> n; + while (n--) { + ans = 0; + cin >> m; + FRI(i, 0, m) { + cin >> num[i]; + FRD(j, i, 0) { + if (num[j] < num[j - 1]) { + num[j] ^= num[j - 1] ^= num[j] ^= num[j - 1]; + ans++; + } else break; + } + } + cout << "Optimal train swapping takes " << ans << " swaps." << endl; + } + return 0; +} diff --git a/uva_cpp_clean/299/299-18.cpp b/uva_cpp_clean/299/299-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c29565b41870aa451b33a429198700f8a0090d18 --- /dev/null +++ b/uva_cpp_clean/299/299-18.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main(){ + int n, cont, L; + cin >> n; + while(n-- > 0){ + cin >> L; + int vector[L]; + for(int i = 0; i < L; i++) cin >> vector[i]; + cont = 0; + for(int i = 0; i < L; i++){ + for(int j = i + 1; j < L; j++){ + if(vector[i] > vector[j]){ + swap(vector[i], vector[j]); + cont++; + } + } + } + printf("Optimal train swapping takes %d swaps.\n", cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/299/299-19.cpp b/uva_cpp_clean/299/299-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23b3ac75b6e122bbe003d5b755da5e1f9fdf8890 --- /dev/null +++ b/uva_cpp_clean/299/299-19.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +int train[51], L; + +int sort() { + int ret = 0, auxi; + for (int i = 0; i < L; i++) { + for (int j = i + 1; j < L; j++) { + if (train[i] > train[j]) ret++, auxi = train[j], train[j] = train[i], train[i] = auxi; + } + } + return ret; +} + +int main() { + int N; + cin >> N; + while (N--) { + cin >> L; + for (int i = 0; i < L; i++) cin >> train[i]; + int sol = sort(); + printf("Optimal train swapping takes %d swaps.\n", sol); + } + return 0; +} diff --git a/uva_cpp_clean/299/299-21.cpp b/uva_cpp_clean/299/299-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33651f46a8700fceac5071bf918002b7461e925d --- /dev/null +++ b/uva_cpp_clean/299/299-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 299 + Name: Train Swapping + Problem: https://onlinejudge.org/external/2/299.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int C; + cin >> C; + while (C--) { + int n, a[50], result = 0; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i]; + + for (int i =0; i a[j + 1]) { + result++; + a[j] = a[j] ^ a[j + 1]; + a[j + 1] = a[j] ^ a[j + 1]; + a[j] = a[j] ^ a[j + 1]; + } + + printf("Optimal train swapping takes %d swaps.\n", result); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/299/299-5.cpp b/uva_cpp_clean/299/299-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2871bad5972a83b2ab16e71fc199956be32e2ea4 --- /dev/null +++ b/uva_cpp_clean/299/299-5.cpp @@ -0,0 +1,25 @@ +#include +int main() +{ + int i,j,k,t,n,b,a[100],c; + scanf("%d",&t); + for(k=1;k<=t;k++) + { + c=0; + scanf("%d",&n); + for(i=0;i=i;j--) + if(a[j-1]>a[j]) + { + c++; + b=a[j-1]; + a[j-1]=a[j]; + a[j]=b; + } + printf("Optimal train swapping takes %d swaps.\n",c); + } +return 0; +} diff --git a/uva_cpp_clean/299/299-9.cpp b/uva_cpp_clean/299/299-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..932acc71903fbdd8c086a2890f2ae09b0c0c723d --- /dev/null +++ b/uva_cpp_clean/299/299-9.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +const int SIZE = 55; + +int tc, N, v[SIZE]; + +int BubbleSort(){ + int ans = 0; + for(int i = 0; i < N; i++) + for(int j = 0; j < i; j++) + if( v[j] > v[i] ) swap(v[j], v[i]), ans++; + return ans; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &N); + for(int it = 0; it < N; it++) + scanf("%d", v + it); + printf("Optimal train swapping takes %d swaps.\n", BubbleSort()); + } + return(0); +} diff --git a/uva_cpp_clean/300/300-21.cpp b/uva_cpp_clean/300/300-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2e03d6192d145124ffafb7ac02023322f2b3059 --- /dev/null +++ b/uva_cpp_clean/300/300-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 300 + Name: Maya Calendar + Problem: https://onlinejudge.org/external/3/300.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char tzo[20][20] = { + "imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", + "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau" +}; + +char hmn[19][19] = { + "pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", + "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu", "uayet" +}; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + map mind; + for (int i=0; i<19; ++i) + mind[hmn[i]] = i; + + int n,d,y; + cin >> n; + cout << n << endl; + while (n--) { + char ch, s[100]; + cin >> d >> ch >> s >> y; + int m = mind[s]; + int x = y*365 + m*20 + d; + cout << (x%13)+1 << ' ' << tzo[x%20] << ' ' << x/260 << endl; + } +} diff --git a/uva_cpp_clean/301/301-21.cpp b/uva_cpp_clean/301/301-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e4b79bd9919d6f513ed6b015d6b85ac97655930 --- /dev/null +++ b/uva_cpp_clean/301/301-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 301 + Name: Transportation + Problem: https://onlinejudge.org/external/3/301.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct Order { + int st, ed, cn; + int price() const { return (ed-st)*cn; } + bool operator < (const Order &o) const { + return st!=o.st ? st < o.st : price() > o.price(); + } +} +O[23]; + +int L[17], H[23], best, n, m, p; +void bt(int idx, int sum, int cap, int lst=-1) { + if (sum + H[idx] <= best) return; + if (idx == m) { + best = sum; + return; + } + + int st = O[idx].st; + for (int i=st; i>lst; --i) + cap += L[i]; + + if (O[idx].cn <= cap) { + L[O[idx].ed] += O[idx].cn; + bt(idx+1, sum + O[idx].price(), cap - O[idx].cn, st); + L[O[idx].ed] -= O[idx].cn; + } + bt(idx+1, sum, cap, st); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n >> p >> m && (n||p||m)) { + for (int i=0; i> O[i].st >> O[i].ed >> O[i].cn; + sort (O, O+m); + + H[m] = 0; + for (int i=m-1; i>=0; --i) + H[i] = H[i+1] + O[i].price(); + + best = 0; + bt(0, 0, n); + cout << best << '\n'; + } +} diff --git a/uva_cpp_clean/302/302-15.cpp b/uva_cpp_clean/302/302-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5afc04e61c3edb5024fedcadf516da181c98f3b0 --- /dev/null +++ b/uva_cpp_clean/302/302-15.cpp @@ -0,0 +1,72 @@ +/* + 一筆畫 (尤拉環) + 時間複雜度: O(V+E) +*/ +#include +#include +#include + +using namespace std; + +bool compare(const pair &a, const pair &b) { + return a.second < b.second; +} + +vector>> adjacency_list(2000); +vector path; +vector visited(2000); + +void DFS(int x) { + // 隨便繞圈圈 + for (const pair &edge : adjacency_list[x]) + if (!visited[edge.second]) { + visited[edge.second] = true; + DFS(edge.first); + path.push_back(edge.second); + } +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, x, y, z, odd_edges, start_point; + path.reserve(2000); + while (cin >> x >> y && x) { + // 建立相鄰表 + start_point = min(x, y); + for (auto &&edges : adjacency_list) + edges.clear(); + cin >> z; + adjacency_list[x].push_back(make_pair(y, z)); + adjacency_list[y].push_back(make_pair(x, z)); + while (cin >> x >> y && x) { + cin >> z; + adjacency_list[x].push_back(make_pair(y, z)); + adjacency_list[y].push_back(make_pair(x, z)); + } + + odd_edges = 0; + // 一筆畫不能有頂點有奇數邊(或2個奇數邊) + for (const vector> &edges : adjacency_list) + if (edges.size() & 1) + odd_edges++; + + if (odd_edges != 0) + cout << "Round trip does not exist.\n\n"; + else { + for (auto &&edges : adjacency_list) + sort(edges.begin(), edges.end(), compare); //街道編號要先排序 + + path.clear(); + fill(visited.begin(), visited.end(), false); + + DFS(start_point); + + for (i = path.size() - 1; i > 0; i--) + cout << path[i] << " "; + cout << path[0] << "\n\n"; + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/305/305-15.cpp b/uva_cpp_clean/305/305-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68b0a08c061de002b84c325b2711ca17838c7f0f --- /dev/null +++ b/uva_cpp_clean/305/305-15.cpp @@ -0,0 +1,18 @@ +/* + 約瑟夫問題、建表、離線計算 + 時間複雜度: O(1) +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int k, ans[14] = {0, 2, 7, 5, 30, 169, 441, 1872, 7632, 1740, 93313, 459901, 1358657, 2504881}; + + while (cin >> k && k) + cout << ans[k] << "\n"; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/305/305-19.cpp b/uva_cpp_clean/305/305-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d12f944a9d00d8244c9cc4a5c2a719aee2be2e6 --- /dev/null +++ b/uva_cpp_clean/305/305-19.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main() { + /*for (int k = 1; k < 14; k++) { + bool sol = false; + int m = k; + for (; !sol; m++) { + int deaths = 0, next = 0; + while (!sol) { + next = (2*k - deaths + next + m - 1) % (2*k - deaths); + deaths++; + if (next < k) break; + if (deaths == k) sol = true; + } + } + printf("%d %d\n", k, m-1); + }*/ + int k; + int sol[14] = { 0, 2, 7, 5, 30, 169, 441, 1872, 7632, 1740, 93313, 459901, 1358657, 2504881}; + while (cin >> k && k != 0) printf("%d\n", sol[k]); + return 0; +} diff --git a/uva_cpp_clean/305/305-21.cpp b/uva_cpp_clean/305/305-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bfe79e61d8a768ec0163772a2fa80ef8f43c171 --- /dev/null +++ b/uva_cpp_clean/305/305-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 305 + Name: Joseph + Problem: https://onlinejudge.org/external/3/305.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main(){ + int n,answer[]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881,13482720}; + while(cin>>n && n) + cout<>n && n) + { + for(i=n+1;e;i++) + { + //if(!(n%i))i+=n;3,5 + t=2*n;//6 + t2=(i-1)%t-1;t--; + e=0; + for(j=0;j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 306 + Name: Cipher + Problem: https://onlinejudge.org/external/3/306.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +#define MAXN 217 + +bool seen[MAXN]; +int nxt[MAXN], + cowner[MAXN], coffset[MAXN], + cycles[MAXN][MAXN], cdeg[MAXN]; + +int main(){ + int n, k, x; + + while (scanf("%d", &n)==1 && n) { + for (int i=0; i + +using namespace std; + +const int SIZE = 210, LEN = 210; + +int n, k, m, v[SIZE]; +char line[LEN], tmp[LEN]; + +int gcd(int a, int b){ + return b == 0 ? a : gcd(b, a % b); +} + +int lcm(int a, int b){ + return a * ( b / gcd(a, b) ); +} + +void GetCycle(){ + m = 1; + for(int it = 0; it < n; it++){ + int ct = 1, a = v[it]; + while(a != it) a = v[a], ct++; + m = lcm(m, ct); + } +} + +int main(){ + while(scanf("%d", &n), n){ + for(int it = 0; it < n; it++) scanf("%d", v + it), v[it]--; + GetCycle(); + while(scanf("%d", &k), k){ + getchar(); + memset(line, ' ', sizeof line); + cin.getline(line, LEN); + line[strlen(line)] = ' '; + line[n] = '\0'; + k %= m; + while(k--){ + memset(tmp, ' ', sizeof tmp); + tmp[n] = '\0'; + for(int it = 0; it < n; it++) + tmp[v[it]] = line[it]; + strcpy(line, tmp); + } + printf("%s\n", line); + } + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/307/307-21.cpp b/uva_cpp_clean/307/307-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..057c0a0d84adafae947aac6abf52044d44fa627e --- /dev/null +++ b/uva_cpp_clean/307/307-21.cpp @@ -0,0 +1,77 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 307 + Name: Sticks + Problem: https://onlinejudge.org/external/3/307.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int X[143], C[143], R[143], len, sz; +bool bt(int si, int sum, int cnt, bool st) { + if (sum == len) return bt(0, 0, cnt-1, 1); + if (!cnt) return 1; + if (sum + C[si]*X[si] + R[si+1] < len) + return 0; + + if (st) { + int i=0; + while (!C[i]) ++i; + --C[i]; + int result = bt(i, sum+X[i], cnt, 0); + ++C[i]; + return result; + } + + for (int i=si; i0 && sum+X[i]<=len) { + --C[i]; + if (bt(i, sum+X[i], cnt, 0)) return 1; + ++C[i]; + + if (sum + X[i] == len) return 0; + } + return 0; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + while (cin >> n && n) { + int sum = 0, mx=0; + for (int i=0; i> X[i]; + sum += X[i]; + mx = max(mx, X[i]); + } + + sort(X, X+n, greater()); + sz = C[0] = 1; + for (int i=1; i=0; --i) + R[i] = R[i+1] + C[i]*X[i]; + + int found = -1; + int hsum = sum>>1; + for (len=mx; found<0 && len<=hsum; ++len) + if (sum%len==0 && bt(0, 0, sum/len, 1)) + found = len; + + cout << (found<0 ? sum : found) << '\n'; + } +} diff --git a/uva_cpp_clean/311/311-21.cpp b/uva_cpp_clean/311/311-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d3413464badf7235dc386632519a1bcae2f5df0 --- /dev/null +++ b/uva_cpp_clean/311/311-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 311 + Name: Packets + Problem: https://onlinejudge.org/external/3/311.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int C2[] = { 0, 5, 3, 1 }, + C1[] = { 0, 7, 6, 5 }, + x1, x2, x3, x4, x5, x6; + while (cin>>x1>>x2>>x3>>x4>>x5>>x6 && (x1||x2||x3||x4||x5||x6)) { + int cnt = x6 + x5 + x4 + ceil(x3/4.0); + x1 -= 11*x5 + C1[x3%4]; + x2 -= 5*x4 + C2[x3%4]; + if (x2 > 0) { + cnt += ceil(x2 / 9.0); + x2 -= 9 * ceil(x2 / 9.0); + } + if (x2 < 0) { + x1 -= -x2*4; + x2 = 0; + } + + if (x1 > 0) + cnt += ceil(x1/36.0); + + cout << cnt << '\n'; + } +} diff --git a/uva_cpp_clean/312/312-4.cpp b/uva_cpp_clean/312/312-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..708c188bbfcb58918ab484c288c42ab179a8524c --- /dev/null +++ b/uva_cpp_clean/312/312-4.cpp @@ -0,0 +1,207 @@ +#include +#include +#include + +#define EMPTY 0 +#define UNKNOWN -1 +#define FILL -2 +#define NOTFILL -3 +#define PROCESSING -4 + +struct node +{ + int row; + int column; + + node() + { + row = 0; + column = 0; + }; + + node(int _row, int _column) + { + row = _row; + column = _column; + }; + + ~node(){}; +}; + +void floodFill(int i, int j, int m, int n, int *tab) +{ + std::queue toProcess, processed; + toProcess.push(node(i, j)); + + bool open = false; + + while (!toProcess.empty()) + { + node item = toProcess.front(); + toProcess.pop(); + + tab[item.row * n + item.column] = PROCESSING; + + if (item.row == 0 || item.row + 1 == m || item.column == 0 || item.column + 1 == n) + { + open = true; + } + + if (item.column != 0 && tab[item.row * n + item.column - 1] == UNKNOWN) + { + toProcess.push(node(item.row, item.column - 1)); + } + + if (item.column + 1 < n && tab[item.row * n + item.column + 1] == UNKNOWN) + { + toProcess.push(node(item.row, item.column + 1)); + } + + if (item.row != 0 && tab[(item.row - 1) * n + item.column] == UNKNOWN) + { + toProcess.push(node(item.row - 1, item.column)); + } + + if (item.row + 1 < m && tab[(item.row + 1) * n + item.column] == UNKNOWN) + { + toProcess.push(node(item.row + 1, item.column)); + } + + processed.push(item); + } + + while (!processed.empty()) + { + node item = processed.front(); + processed.pop(); + + if (open) + { + tab[item.row * n + item.column] = NOTFILL; + } + else + { + tab[item.row * n + item.column] = FILL; + } + } +} + +int main() +{ + std::ios::sync_with_stdio(false); + + int n, m; + + while (std::cin >> m >> n && n != 0 && m != 0) + { + int *tab = new int[m * n]; + int height = 3 * m + 1, width = 5 * n + 1; + char *output = new char[width * height]; + + memset(output, (int)' ', width * height * sizeof(char)); + + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + int temp; + std::cin >> temp; + + if (temp == 1) + { + tab[i * n + j] = UNKNOWN; + } + else + { + for (int k = 0; k < 6; k++) + { + output[i * width * 3 + k + 5 * j] = '+'; + output[i * width * 3 + 3 * width + k + 5 * j] = '+'; + } + + for (int k = 0; k < 4; k++) + { + output[i * width * 3 + k * width + 5 * j] = '+'; + output[i * width * 3 + k * width + 5 * j + 5] = '+'; + } + + tab[i * n + j] = EMPTY; + } + } + } + + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (tab[i * n + j] == UNKNOWN) + { + floodFill(i, j, m, n, tab); + } + } + } + + int index = 0; + + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (tab[i * n + j] == EMPTY) + { + if ((i + 1 < m && tab[(i + 1) * n + j] == EMPTY && (i == 0 || tab[(i - 1) * n + j] != EMPTY)) || (j + 1 < n && tab[i * n + j + 1] == EMPTY && (j == 0 || tab[i * n + j - 1] != EMPTY))) + { + ++index; + + output[i * width * 3 + 1 * width + 1 + 5 * j] = '0' + index / 100; + output[i * width * 3 + 1 * width + 2 + 5 * j] = '0' + (index / 10) % 10; + output[i * width * 3 + 1 * width + 3 + 5 * j] = '0' + index % 10; + } + } + else if (tab[i * n + j] == FILL) + { + for (int k = 0; k < 6; k++) + { + output[i * width * 3 + 0 * width + k + 5 * j] = '+'; + output[i * width * 3 + 1 * width + k + 5 * j] = '+'; + output[i * width * 3 + 2 * width + k + 5 * j] = '+'; + output[i * width * 3 + 3 * width + k + 5 * j] = '+'; + } + } + } + } + + for (int i = 0; i < height; i++) + { + for (int j = width - 1; j >= 0; j--) + { + if (output[i * width + j] != ' ') + { + break; + } + + output[i * width + j] = 0; + } + } + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + if (output[i * width + j] == 0) + { + break; + } + + std::cout << output[i * width + j]; + } + + std::cout << std::endl; + } + + std::cout << std::endl; + + delete[] tab; + delete[] output; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/314/314-13.cpp b/uva_cpp_clean/314/314-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..264f4b956a99306c3f5e757e7120ed7ee99a4f01 --- /dev/null +++ b/uva_cpp_clean/314/314-13.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 110 +#define INF 100000000 + +int n, m, sx, sy, ex, ey; +int dirX[] = {-1, 0, 1, 0}; +int dirY[] = {0, 1, 0, -1}; + +int direction; +bool graph[MAX][MAX]; +bool input[MAX][MAX]; +bool visited[MAX][MAX][4]; + +void initialize() +{ + memset(input, false, sizeof input); + memset(graph, true, sizeof graph); + memset(visited, false, sizeof visited); + + for (int i = 0; i <= n ; ++i) + { + for (int j = 0; j <= m; ++j) + { + if (i == 0 || j == 0 || i == n || j == m) + { + graph[i][j] = false; + } + } + } +} + +void makeGraph() +{ + for (int i = 1; i < n; ++i) + { + for (int j = 1; j < m ; ++j) + { + graph[i][j] = input[i - 1][j] && input[i][j - 1] && input[i - 1][j - 1] && input[i][j]; + } + } +} + +bool isOk(int i, int j) +{ + return i > 0 && j > 0 && i < n && j < m && graph[i][j]; +} + +int bfs() +{ + visited[sx][sy][direction] = true; + + queue, pair > > queue1; + + queue1.push(make_pair(make_pair(direction, 0), make_pair(sx, sy))); + + while (!queue1.empty()) + { + int x = queue1.front().second.first; + int y = queue1.front().second.second; + int d = queue1.front().first.first; + int t = queue1.front().first.second; + + if (x == ex && y == ey) + return t; + + queue1.pop(); + + if (!visited[x][y][(d + 1) % 4]) + { + visited[x][y][(d + 1) % 4] = true; + queue1.push(make_pair(make_pair(((d + 1) % 4), t + 1), make_pair(x, y))); + } + + if (!visited[x][y][(d + 3) % 4]) + { + visited[x][y][(d + 3) % 4] = true; + queue1.push(make_pair(make_pair(((d + 3) % 4), t + 1), make_pair(x, y))); + } + + bool go = true; + + for (int i = 1; i <= 3; ++i) + { + if (isOk(x + (dirX[d] * i), y + (dirY[d] * i)) && go) + { + go = true; + + if (!visited[x + (dirX[d] * i)][y + (dirY[d] * i)][d]) + { + visited[x + (dirX[d] * i)][y + (dirY[d] * i)][d] = true; + queue1.push(make_pair(make_pair(d, t + 1), make_pair(x + (dirX[d] * i), y + (dirY[d] * i)))); + } + } + else go = false; + } + } + + return -1; +} + +int main() +{ + while (cin >> n >> m && (n != 0 || m!= 0)) + { + initialize(); + + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + bool cell; + cin >> cell; + input[i][j] = !cell; + } + } + + makeGraph(); + + string s; + cin >> sx >> sy >> ex >> ey >> s; + + if (s == "north") direction = 0; + else if (s == "east") direction = 1; + else if (s == "south") direction = 2; + else if (s == "west") direction = 3; + + if (!graph[sx][sy]) + { + cout << -1 << endl; + } + + else + { + cout << bfs() << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/314/314-21.cpp b/uva_cpp_clean/314/314-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37a0874c0343a06d1ab4a0d485236a3f39e1ddba --- /dev/null +++ b/uva_cpp_clean/314/314-21.cpp @@ -0,0 +1,88 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 314 + Name: Robot + Problem: https://onlinejudge.org/external/3/314.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct State { + int t; + char i, j, d; + State(char i, char j, char d, int t):i(i),j(j),d(d),t(t){} +}; + + +bool B[99][99], C[99][99], + seen[99][99][4]; + +int dirid[200], + di[] = {-1, 0, 1, 0}, + dj[] = { 0, 1, 0, -1}; + +void pushS(queue &q, int i, int j, int d, int t) { + if (!seen[i][j][d]) { + seen[i][j][d] = true; + q.push(State(i, j, d, t)); + } +} + +bool readBool() { + char ch; + while ((ch=getchar())!='0' && ch!='1'); + return ch-'0'; +} + +int main(){ + int n, m, si, sj, ei, ej; + char dir[20]; + dirid['n'] = 0; dirid['e'] = 1; + dirid['s'] = 2; dirid['w'] = 3; + while (scanf("%d%d", &n, &m)==2 && (n||m)) { + for (int i=0; i q; + + scanf("%d%d%d%d%s", &si, &sj, &ei, &ej, dir); + pushS(q, si, sj, dirid[dir[0]], 0); + while (!q.empty()) { + State s = q.front(); q.pop(); + if (s.i == ei && s.j == ej) { + result = s.t; + break; + } + + pushS(q, s.i, s.j, (s.d+1)%4, s.t+1); + pushS(q, s.i, s.j, (s.d+3)%4, s.t+1); + for (int k=1; k<=3; ++k) { + int ni = s.i + k*di[s.d], + nj = s.j + k*dj[s.d]; + if (!C[ni][nj]) break; + pushS(q, ni, nj, s.d, s.t+1); + } + } + + printf("%d\n", result); + } +} diff --git a/uva_cpp_clean/315/315-13.cpp b/uva_cpp_clean/315/315-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee3eb980b681784a04c1b3b5d274804061a4ed0c --- /dev/null +++ b/uva_cpp_clean/315/315-13.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include + +using namespace std; + +string s; +int u, v, n, root, ans, rootChildern = 0, dfsCount = 0; + +vector visited; +vector parent; +vector cutVertex; +vector < vector > graph; +vector entryTime, bestTime; + +void find(int start) +{ + visited[start] = true; + + entryTime[start] = bestTime[start] = dfsCount++; + + for (int i = 0; i < (int)graph[start].size(); i++) + { + int child = graph[start][i]; + + if (!visited[child]) + { + parent[child] = start; + + if (start == root) rootChildern++; + + find(child); + + // if we want cut-edge -> use ">" and (start, child) is a edge. + if (bestTime[child] >= entryTime[start]) + { + cutVertex[start] = true; + } + + bestTime[start] = min(bestTime[start], bestTime[child]); + } + + else if (child != parent[start]) + { + bestTime[start] = min(bestTime[start], entryTime[child]); + } + } +} + +int main() +{ + while (true) + { + cin >> n; + + if (n == 0) break; + + getline(cin, s); + + dfsCount = ans = 0; + + graph.clear(); + graph.resize(n); + + parent.clear(); + parent.resize(n, -1); + + cutVertex.clear(); + cutVertex.resize(n, 0); + + entryTime.clear(); + entryTime.resize(n, 0); + + visited.clear(); + visited.resize(n, 0); + + bestTime.clear(); + bestTime.resize(n, 0); + + while (true) + { + getline(cin, s); + + istringstream is(s); + + is >> u; + + if (u == 0) break; + + u--; + + while (is >> v) + { + v--; + + graph[v].push_back(u); + graph[u].push_back(v); + } + } + + root = 0; + + for (int i = 0; i < n; i++) + { + if (!visited[i]) + { + root = i; + + rootChildern = 0; + + find(i); + } + + cutVertex[root] = (rootChildern > 1) ? true : false; + } + + for (int i = 0; i < n; i++) + { + ans += cutVertex[i]; + } + + cout << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/315/315-14.cpp b/uva_cpp_clean/315/315-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..308fff2048dd4ba35b841e0b5ab6593f21dcecb2 --- /dev/null +++ b/uva_cpp_clean/315/315-14.cpp @@ -0,0 +1,140 @@ +/*************************************************** + * Problem Name : 315 - Network.cpp + * Problem Link : https://uva.onlinejudge.org/external/3/315.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-08-10 + * Problem Type : Graph ( Articulation Point) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 105 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +int n, t, cnt; +bool vis[MAX], chk[MAX]; +int dis[MAX], low[MAX]; +void dfs (int u, int p) { + vis[u] = true; + dis[u] = low[u] = t++; + int child = 0; + + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + if (vis[v] == true) { + low[u] = min (low[u], dis[v]); + + } else { + dfs (v, u); + low[u] = min (low[u], low[v]); + + if ( dis[u] <= low[v] && p != -1 && chk[u] == 0) { + cnt++; + chk[u] = 1; + } + + child++; + } + } + + if (p == -1 && child > 1 && chk[u] == 0) { + cnt++; + chk[u] = 1; + } +} +void find_articulation_point() { + t = cnt = 0; + SET (vis, false); + SET (chk, false); + SET (dis, -1); + SET (low, -1); + + for (int i = 1; i <= n; i++) { + if (!vis[i]) dfs (i, -1); + } + + for (int i = 0; i < MAX; i++) adj[i].clear(); +} +int main () { + __FastIO; + while (cin >> n) { + if ( n == 0) break; + + string str; + + while (getline (cin, str) ) { + if (str == "0") break; + + vectorv; + stringstream ss; + int x; + ss << str; + + while (ss >> x) { + v.pb (x); + } + + for (int i = 1; i < (int) v.size(); i++) { + adj[v[0]].pb (v[i]); + adj[v[i]].pb (v[0]); + } + } + + find_articulation_point(); + cout << cnt << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/315/315-15.cpp b/uva_cpp_clean/315/315-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db8afd37ed3cabf3bca55d886961ee414d1be532 --- /dev/null +++ b/uva_cpp_clean/315/315-15.cpp @@ -0,0 +1,84 @@ +/* + 關節點數量、DFS + 時間複雜度: O(V+E) = O(V) = O(N) +*/ +#include +#include +#include +#include +#include + +using namespace std; + +vector lowest_visit_time(100), visit_time(100); +vector> adjacency_list(100, vector()); + +int N, step, ans; + +void DFS(int parent, int current) { + visit_time[current] = lowest_visit_time[current] = ++step; + + int child = 0, i; + bool is_articulation_vertex = false; + for (const int& j : adjacency_list[current]) + if (j != parent) { + if (visit_time[j]) { + lowest_visit_time[current] = min(lowest_visit_time[current], visit_time[j]); // 這點之前看過了: back edge + } // 判斷是之前還是現在遍歷時刻較低 + else { // 這點沒看過: tree edge + child++; // current 有新的子樹 + DFS(current, j); // current 是 j 的祖先 + lowest_visit_time[current] = min(lowest_visit_time[current], lowest_visit_time[j]); // 判斷是之前還是現在遍歷時刻較低 + if (lowest_visit_time[j] >= visit_time[current]) // 如果子孫和祖先沒有back edge + is_articulation_vertex = true; // current 就是關節點 + } + } + // 1. current 是樹根且有兩棵以上子樹 + // 2. current 的祖先和 current 的子孫沒有 back edge + // 滿足兩者之一的話 current 就是關節點 + // cout << "Vertex " << current << " is an Articulation Vertex.\n"; + if (current == parent && child > 1) + ans++; + else if (current != parent && is_articulation_vertex) + ans++; +} + +inline void initial() { + int from, to; + string str; + N++; + + for (auto&& row : adjacency_list) + row.clear(); + fill(lowest_visit_time.begin(), lowest_visit_time.end(), 0); + fill(visit_time.begin(), visit_time.end(), 0); + + step = ans = 0; + // 建立鄰接表,這題 N = V > E + // 使用鄰接矩陣會是 O(V^2) = O(N^2) + // 使用鄰接表則會是 O(V+E) = O(N) + // 輸入有點麻煩 + cin.ignore(1024, '\n'); + while (getline(cin, str)) { + istringstream input(str); + input >> from; + if (!from) + break; + while (input >> to) { + adjacency_list[from].push_back(to); + adjacency_list[to].push_back(from); + } + } +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + while (cin >> N && N) { + initial(); + DFS(1, 1); // 題目保證全部連通,隨便取一點都可以 + cout << ans << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/315/315-19.cpp b/uva_cpp_clean/315/315-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d09822585381c26434c8f92cf26b9acfe22caa2 --- /dev/null +++ b/uva_cpp_clean/315/315-19.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +using namespace std; + +typedef vector vi; +vector AdjList; +vi dfs_num, dfs_low; +vector apoints; +int counter, n0; + +void artpoint(int n, int p) { + dfs_num[n] = dfs_low[n] = counter++; + for (int i = 0; i < AdjList[n].size(); i++) { + if (dfs_num[AdjList[n][i]] == -1) { + if (n == 0) n0++; + artpoint(AdjList[n][i], n); + if (dfs_low[AdjList[n][i]] >= dfs_num[n]) apoints[n] = true; + else dfs_low[n] = min(dfs_low[n], dfs_low[AdjList[n][i]]); + } else if (AdjList[n][i] != p) { + dfs_low[n] = min(dfs_low[n], dfs_num[AdjList[n][i]]); + } + } +} + +int main() { + int N; + while (cin >> N && N != 0) { + string s; + AdjList.assign(N, vi()); + while (getline(cin, s) && s != "0") { + istringstream iss(s); + int n1, n2; + iss >> n1; + while (iss >> n2) { + AdjList[n1-1].push_back(n2-1); + AdjList[n2-1].push_back(n1-1); + } + } + n0 = counter = 0; + apoints.assign(N, false); + dfs_num.assign(N, -1); + dfs_low.assign(N, -1); + artpoint(0, 0); + int ap = 0; + apoints[0] = (n0 > 1); + for (int i = 0; i < N; i++) if (apoints[i]) ap++; + printf("%d\n", ap); + } + return 0; +} diff --git a/uva_cpp_clean/315/315-21.cpp b/uva_cpp_clean/315/315-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..835130b9af47428d1a2fb1322f3fc69fe55da6cf --- /dev/null +++ b/uva_cpp_clean/315/315-21.cpp @@ -0,0 +1,77 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 315 + Name: Network + Problem: https://onlinejudge.org/external/3/315.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +vector adj[101]; +int n, mp[101][101], par[101], + seen[101], low[101], depth[101], + arts; + +void dfs(int v, int d) { + seen[v] = true; + depth[v] = low[v] = d; + bool isArt = false; + int childs = 0; + + for (int i=adj[v].size()-1; i>=0; i--) { + int w = adj[v][i]; + if (!seen[w]) { + childs++; + par[w] = v; + dfs(w, d+1); + if (low[w] >= depth[v]) + isArt = true; + if (low[w] < low[v]) + low[v] = low[w]; + } + else if (w != par[v] && depth[w]1)) arts++; +} + + +int main(){ + char line[1000]; + while (cin>>n && n) { + for (int i=0; i +using namespace std; +using ii=tuple; +using vi=vector; +using vii=vector; +using vvi=vector; +using vvii=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + int n,m,u,v; + string s; + cin>>n; + if(!n)break; + getline(cin,s); + vvi g(n); + for(;;){ + getline(cin,s); + stringstream in(s); + in>>u; + if(!u)break; + u--; + while(in>>v){ + v--; + g[u].push_back(v); + g[v].push_back(u); + } + } + vi a(n),b(n),d(n); + int k=1; + functiondfs=[&](int u,int p){ + a[u]=k++; + b[u]=a[u]; + int c=0; + for(int v:g[u]) + if(!a[v]){ + c++; + dfs(v,u); + b[u]=min(b[u],b[v]); + if(b[v]>=a[u])d[u]=1; + }else if(v!=p)b[u]=min(b[u],a[v]); + if(p==-1)d[u]=c>1; + }; + for(int i=0;i + +using namespace std; + +const int MAX_N = 110, LEN = 300; + +int V, dfs_low[MAX_N], dfs_num[MAX_N], dfs_parent[MAX_N], dfsCounter, dfsRoot, rootChildren; +char line[MAX_N], *p; +bool Articulation_Point[MAX_N]; +vector AdjList[MAX_N]; + +void dfs(int u){ + dfs_low[u] = dfs_num[u] = ++dfsCounter; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not dfs_num[v] ){ + dfs_parent[v] = u; + dfs(v); + if( u == dfsRoot ) rootChildren++; + if( dfs_low[v] >= dfs_num[u] ) Articulation_Point[u] = true; + dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + else if( v != dfs_parent[u] ) + dfs_low[u] = min(dfs_low[u], dfs_num[v]); + } +} + +int ArticulationPoints(){ + fill(dfs_low, dfs_low + V + 1, 0); + fill(dfs_num, dfs_num + V + 1, 0); + fill(dfs_parent, dfs_parent + V + 1, -1); + fill(Articulation_Point, Articulation_Point + V + 1, false); + dfsRoot = 1, dfsCounter = rootChildren = 0; + dfs(dfsRoot); + Articulation_Point[dfsRoot] = ( rootChildren > 1 ); + int ret = 0; + for(int u = 1; u <= V; u++) ret += Articulation_Point[u]; + return ret; +} + +void solve(){ + printf("%d\n", ArticulationPoints()); +} + +int read(){ + while(cin.getline(line, LEN)){ + p = strtok(line, " "); + int u = atoi(p); + if( u == 0 ) break; + p = strtok(NULL, " "); + while(p){ + AdjList[u].push_back(atoi(p)); + AdjList[atoi(p)].push_back(u); + p = strtok(NULL, " "); + } + } +} + +void clear(){ + for(int u = 1; u <= V; u++) AdjList[u].clear(); +} + +int main(){ + while(scanf("%d\n", &V), V){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/318/318-21.cpp b/uva_cpp_clean/318/318-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d26c276b657bbaa69cad13bd43269ce2b98e24f5 --- /dev/null +++ b/uva_cpp_clean/318/318-21.cpp @@ -0,0 +1,77 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 318 + Name: Domino Effect + Problem: https://onlinejudge.org/external/3/318.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 1073741824 +#define MAXN 501 + +list adj[MAXN]; +int mat[MAXN][MAXN], d[MAXN]; + +int main(){ + int cse=1, n, m, u, v, w; + while (cin >> n >> m && (n||m)) { + for (int i=0; i>u>>v>>w; u--; v--; + if (mat[u][v] == INF) { + adj[u].push_back(v); + adj[v].push_back(u); + mat[u][v] = mat[v][u] = w; + } + } + + for (int v=1; v d[bi]) + bi = i; + + int bd = d[bi] << 1; + for (int u=0; u bd) { + bd = dd; + bi = u; + bj = v; + } + } + + printf("System #%d\nThe last domino falls after %.1f seconds, ", cse++, bd/2.0); + if (bj < 0) + printf("at key domino %d.\n\n", bi+1); + else printf("between key dominoes %d and %d.\n\n", bi+1, bj+1); + } +} diff --git a/uva_cpp_clean/321/321-21.cpp b/uva_cpp_clean/321/321-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d364ed4753ff89020c1ef205eb988ff865cabb6f --- /dev/null +++ b/uva_cpp_clean/321/321-21.cpp @@ -0,0 +1,97 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 321 + Name: The New Villa + Problem: https://onlinejudge.org/external/3/321.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct State { + int r, l, d; + State(int r, int l, int d):r(r),l(l),d(d){} +}; + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + + int S[17][2048]={}, Pr[17][2048], Pt[17][2048], pr[10240], pt[10240], n, m, s, u, v; + vector adj[17], ads[17]; + for (int cse=1; cin >> n >> m >> s && (n||m||s); ++cse) { + for (int i=0; i> u >> v; --u; --v; + adj[u].push_back(v); + adj[v].push_back(u); + } + + for (int i=0; i> u >> v; --u; --v; + if (u != v) + ads[u].push_back(v); + } + + int ll = 1<<(n-1); + cout << "Villa #" << cse << '\n'; + queue q; + q.push(State(0, 1, 0)); + S[0][1] = cse; + while (!q.empty()) { + State s = q.front(); q.pop(); + if (s.r==n-1 && s.l==ll) { + cout << "The problem can be solved in " << s.d << " steps:\n"; + for (int i=s.d-1, r=s.r, l=s.l; i>=0; --i) { + if (Pt[r][l] == 1) + l^=1<<(pr[i]=Pr[r][l]), + pt[i] = l&(1< + +int factorial[367][785], counter[12]; + +int main() +{ + factorial[0][0] = factorial[1][0] = 1; + for(int i=2 ; i<367 ; i++) + for(int j=0 ; j<785 ; j++) + { + factorial[i][j] += factorial[i-1][j]*i; + if( factorial[i][j]>9 ) + { + factorial[i][j+1] += factorial[i][j]/10; + factorial[i][j] %= 10; + } + } + + int n; + + while( scanf("%d", &n)!=EOF && n ) + { + int pos; + for(pos=785-1 ; pos>0 ; pos--) + if( factorial[n][pos]!=0 ) break; + + for(int i=0 ; i<10 ; i++) counter[i] = 0; + + for( ; pos>=0 ; pos--) counter[factorial[n][pos]]++; + + printf("%d! --\n", n); + printf(" (0) %d (1) %d (2) %d (3) %d (4) %d\n", counter[0], counter[1], counter[2], counter[3], counter[4]); + printf(" (5) %d (6) %d (7) %d (8) %d (9) %d\n", counter[5], counter[6], counter[7], counter[8], counter[9]); + } + return 0; +} diff --git a/uva_cpp_clean/324/324-21.cpp b/uva_cpp_clean/324/324-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d758581239b9479465a12e8c9e22764f127cb67 --- /dev/null +++ b/uva_cpp_clean/324/324-21.cpp @@ -0,0 +1,947 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 324 + Name: Factorial Frequencies + Problem: https://onlinejudge.org/external/3/324.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +#ifndef min +#define min(x,y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef max +#define max(x,y) ((x) > (y) ? (x) : (y)) +#endif + +class BigInt +{ +private: + char *digits; + int size; // number of used bytes (digits) + int capacity; // size of digits + int sign; // -1, 0 or +1 + +public: + BigInt(int n, int cap); + BigInt(int n); + BigInt(long double d); + BigInt(); + BigInt(string s); + BigInt(const char s[]); + BigInt(const BigInt &n); + const BigInt &operator=(const BigInt &n); + const BigInt &operator=(int n); + ~BigInt(); + void normalize(); + static int sig(int n); + static int sig(long double n); + inline int length() { return size; } + + BigInt operator++(); + BigInt operator++(int); + BigInt operator--(); + BigInt operator--(int); + BigInt operator-(); + BigInt operator+ (int n); + BigInt operator+ (BigInt n); + BigInt&operator+=(int n); + BigInt&operator+=(BigInt n); + BigInt operator- (int n); + BigInt operator- (BigInt n); + BigInt&operator-=(int n); + BigInt&operator-=(BigInt n); + BigInt operator* (int n); + BigInt operator* (BigInt n); + void operator*=(int n); + void operator*=(BigInt n); + BigInt operator/ (int n); + BigInt operator/ (BigInt n); + void operator/=(int n); + void operator/=(BigInt n); + int operator% (int n); + BigInt operator% (BigInt n); + void operator%=(int n); + void operator%=(BigInt n); + int divide(int n); + BigInt divide(BigInt n); + BigInt operator* (long double n); + void operator*=(long double n); + + + BigInt operator<< (int n); + void operator<<=(int n); + BigInt operator >> (int n); + void operator>>=(int n); + BigInt operator,(int n); + BigInt operator,(BigInt n); + + /** Casting **/ + bool operator!(); + operator bool(); + operator string(); + + bool operator<(BigInt n); + bool operator>(BigInt n); + bool operator==(BigInt n); + bool operator<=(BigInt n); + bool operator>=(BigInt n); + bool operator<(int n); + bool operator>(int n); + bool operator==(int n); + bool operator<=(int n); + bool operator>=(int n); + int compare(BigInt n); + + int toInt(); + string toString(); + void print(); + void printWithCommas(ostream &out); + +private: + void grow(); + friend istream &operator >> (istream &in, BigInt &n); + friend ostream &operator<<(ostream &out, BigInt n); +}; + +BigInt operator+(int m, BigInt &n) +{ + return n + m; +} + +BigInt operator-(int m, BigInt &n) +{ + return -n + m; +} + +BigInt operator*(int m, BigInt &n) +{ + return n * m; +} + +BigInt operator/(int m, BigInt &n) +{ + return BigInt(m) / n; +} + +BigInt operator%(int m, BigInt &n) +{ + return BigInt(m) % n; +} + +inline bool isDigit(int c) +{ + return(c >= (int)'0' && c <= (int)'9'); +} + +istream &operator >> (istream &in, BigInt &n) // FIXME: see inside +{ + n.size = 0; + n.sign = 1; + int sign = 1; + int c; + while ((c = in.peek()) >= 0 && + (c == ' ' || c == '\t' || c == '\r' || c == '\n')) + in.get(); + if (c < 0 || (c != (int)'-' && !isDigit(c))) + { + in >> c; // XXX: force in.fail() + return in; + } + if (c == (int)'-') { sign = -1; in.get(); } + + // FIXME: Extremely inefficient! Use a string. + while ((c = in.peek()) >= 0 && isDigit(c)) + { + in.get(); + n *= 10; + n += (c - (int)'0'); + } + n.sign = sign; + n.normalize(); + return in; +} + +ostream &operator<<(ostream &out, BigInt n) //FIXME: make more efficient +{ + return out << n.toString(); +} + +BigInt::BigInt(int n, int cap) +{ + cap = max(cap, (int)sizeof(n) * 8); + capacity = cap; + sign = sig(n); + n *= sign; + digits = new char[cap]; + memset(digits, 0, cap); + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(int n) +{ + capacity = 1024; + sign = sig(n); + n *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + while (n) + { + digits[size++] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(long double d) +{ + capacity = 1024; + sign = (d < 0 ? -1 : d > 0 ? 1 : 0); + d *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + d = floor(d); + while (d > 0) + { + digits[size++] = min(max(0, (int)((d - floor(d / 10) * 10) + 0.5)), 9); + d = floor(d / 10); + } +} + +BigInt::BigInt() +{ + capacity = 128; + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; +} + +BigInt::BigInt(string s) +{ + capacity = max((int)s.size(), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const char s[]) +{ + capacity = max((int)strlen(s), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const BigInt &n) +{ + capacity = n.capacity; + sign = n.sign; + size = n.size; + digits = new char[capacity]; + memcpy(digits, n.digits, capacity); +} + +const BigInt &BigInt::operator=(const BigInt &n) +{ + if (&n != this) + { + if (capacity < n.size) + { + capacity = n.capacity; + delete[] digits; + digits = new char[capacity]; + } + sign = n.sign; + size = n.size; + memcpy(digits, n.digits, size); + memset(digits + size, 0, capacity - size); + } + return *this; +} + +const BigInt &BigInt::operator=(int n) +{ + sign = sig(n); + n *= sign; + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } + return *this; +} + +BigInt::~BigInt() +{ + delete[] digits; +} + +void BigInt::normalize() +{ + while (size && !digits[size - 1]) size--; + if (!size) sign = 0; +} + +int BigInt::sig(int n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::sig(long double n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::toInt() +{ + int result = 0; + for (int i = size - 1; i >= 0; i--) + { + result *= 10; + result += digits[i]; + if (result < 0) return sign * 0x7FFFFFFF; + } + return sign * result; +} + +string BigInt::toString() +{ + string s = (sign >= 0 ? "" : "-"); + for (int i = size - 1; i >= 0; i--) + s += (digits[i] + '0'); + if (size == 0) s += '0'; + return s; +} + +void BigInt::print() //FIXME: make more efficient +{ + cout << toString(); +} + +void BigInt::printWithCommas(ostream &out) +{ + if (sign < 0) out.put('-'); + for (int i = size - 1; i >= 0; i--) + { + out.put(digits[i] + '0'); + if (!(i % 3) && i) out.put(','); + } + if (size == 0) out.put('0'); +} + +void BigInt::grow() +{ + char *olddigits = digits; + int oldCap = capacity; + capacity *= 2; + digits = new char[capacity]; + memcpy(digits, olddigits, oldCap); + memset(digits + oldCap, 0, oldCap); + delete[] olddigits; +} + +BigInt BigInt::operator++() +{ + operator+=(1); + return *this; +} + +BigInt BigInt::operator++(int) +{ + return operator++(); +} + +BigInt BigInt::operator--() +{ + operator-=(1); + return *this; +} + +BigInt BigInt::operator--(int) +{ + return operator--(); +} + +BigInt BigInt::operator-() +{ + BigInt result(*this); + result.sign *= -1; + return result; +} + +BigInt BigInt::operator+(int n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt BigInt::operator+(BigInt n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt &BigInt::operator+=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = nsign; + if (sign == nsign) + { + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] + dig + carry; + digits[i] = newdig % 10; + carry = newdig / 10; + n /= 10; + } + size = max(i, size); + } + else operator-=(-n); + return *this; +} + +BigInt &BigInt::operator+=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = n.sign; + if (sign == n.sign) + { + int carry = 0; + int i; + for (i = 0; i < maxS - 1 || carry; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig += n.digits[i]; + digits[i] = newdig % 10; + carry = newdig / 10; + } + size = max(i, size); + } + else + { + n.sign *= -1; + operator-=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator-(int n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt BigInt::operator-(BigInt n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt &BigInt::operator-=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = 1; + if (sign == nsign) + { + BigInt bin = n; + if (sign >= 0 && *this < bin || sign < 0 && *this > bin) + { + // Subtracting a bigger number + operator=(toInt() - n); + return *this; + } + + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] - dig + carry; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + n /= 10; + } + normalize(); + } + else operator+=(-n); + return *this; +} + +BigInt &BigInt::operator-=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = 1; + if (sign == n.sign) + { + if (sign >= 0 && *this < n || sign < 0 && *this > n) + { + // Subtracting a bigger number + BigInt tmp = n; + tmp -= *this; + *this = tmp; + sign = -sign; + return *this; + } + + int carry = 0; + int i; + for (i = 0; i < maxS - 1; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig -= n.digits[i]; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + } + if (carry) // Subtracted a bigger number, need to flip sign + { + if (i) digits[0] = 10 - digits[0]; + size = (i ? 1 : 0); + for (int j = 1; j < i; j++) + { + digits[j] = 9 - digits[j]; + if (digits[i]) size = j + 1; + } + sign *= -1; + } + normalize(); + } + else + { + n.sign *= -1; + operator+=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator*(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + int nsign = sig(n); + n *= nsign; + result.sign = sign * nsign; + if (!result.sign) return result; + + int i, j; + for (i = 0; n; i++) + { + int dig = n % 10; + if (dig) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = result.digits[i + j] + (j < size ? dig * digits[j] : 0) + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + n /= 10; + } + result.size = i + j - 1; + return result; +} + +BigInt BigInt::operator*(BigInt n) +{ + BigInt result(0, size + n.size); + + result.sign = sign * n.sign; + if (!result.sign) return result; + + int i, j; + for (i = 0; i < n.size; i++) + { + if (n.digits[i]) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = + result.digits[i + j] + + (j < size ? n.digits[i] * digits[j] : 0) + + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + } + result.size = i + j - 1; + + return result; +} + +void BigInt::operator*=(int n) +{ + operator=(operator*(n)); +} + +void BigInt::operator*=(BigInt n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator/(int n) +{ + if (!n) n /= n; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +BigInt BigInt::operator/(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +void BigInt::operator/=(int n) +{ + divide(n); +} + +void BigInt::operator/=(BigInt n) +{ + divide(n); +} + +int BigInt::operator%(int n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(int n) +{ + operator=(divide(n)); +} + +BigInt BigInt::operator%(BigInt n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(BigInt n) +{ + operator=(divide(n)); +} + +int BigInt::divide(int n) +{ + if (!n) n /= n; //XXX: force a crash + + int nsign = sig(n); + n *= nsign; + if (!sign) return 0; + sign *= nsign; + + int tmp = 0; + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = tmp / n; + tmp -= digits[i] * n; + } + normalize(); + return tmp; +} + +BigInt BigInt::divide(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + if (!sign) return 0; + sign *= n.sign; + + int oldSign = n.sign; + n.sign = 1; + + BigInt tmp(0, size); + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = 0; + while (tmp >= n) { tmp -= n; digits[i]++; } + } + normalize(); + + n.sign = oldSign; + + return tmp; +} + +// This is only exact to the first 15 or so digits, but it is +// never an over-estimate +BigInt BigInt::operator*(long double n) +{ + // the number of digits after the decimal point to use + #define DIGS_AFTER_DOT 15 + + int nsign = sig(n); + n *= nsign; + int ndigs = n >= 1 ? (int)log10(n) + 1 : 0; + BigInt result(0, size + ndigs); + result.sign = sign * nsign; + if (!result.sign) return result; + + if (n >= 1) for (int i = 0; i < ndigs; i++) n /= 10; + result.size = 0; + + char afterDot[DIGS_AFTER_DOT + 1]; + memset(afterDot, 0, sizeof(afterDot)); + + // Keep going until the DIGS_AFTER_DOT'th digit after the decimal point + for (int i = ndigs - 1; i >= -DIGS_AFTER_DOT; i--) + { + n *= 10; + int dig = (int)floor(n); + n -= dig; + if (!dig) continue; + + int carry = 0; + for (int j = 0; j < size || carry; j++) + { + int newdig = + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) + + dig * digits[j] + + carry; + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) = newdig % 10; + if (i + j >= 0 && result.digits[i + j]) result.size = max(result.size, i + j + 1); + carry = newdig / 10; + } + } + if (!result.size) result.sign = 0; + return result; +} + +void BigInt::operator*=(long double n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator<<(int n) +{ + BigInt result(*this); + result <<= n; + return result; +} + +void BigInt::operator<<=(int n) +{ + if (n < 0) operator>>=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator*=(mult); + } +} + +BigInt BigInt::operator >> (int n) +{ + BigInt result(*this); + result >>= n; + return result; +} + +void BigInt::operator>>=(int n) +{ + if (n < 0) operator<<=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator/=(mult); + } +} + +BigInt BigInt::operator,(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + for (result.size = 0; n; result.size++) + { + result.digits[result.size] = n % 10; + n /= 10; + } + memcpy(result.digits + result.size, digits, size * sizeof(digits[0])); + result.size += size; + result.sign = 1; + result.normalize(); + return result; +} + +BigInt BigInt::operator,(BigInt n) +{ + BigInt result(0, size + n.size); + memcpy(result.digits, n.digits, n.size * sizeof(n.digits[0])); + memcpy(result.digits + n.size, digits, size * sizeof(digits[0])); + result.size = size + n.size; + result.sign = 1; + result.normalize(); + return result; +} + +bool BigInt::operator!() +{ + return !size; +} + +BigInt::operator bool() +{ + return size; +} + +BigInt::operator string() +{ + return toString(); +} + +bool BigInt::operator<(BigInt n) +{ + return(compare(n) < 0); +} + +bool BigInt::operator>(BigInt n) +{ + return(compare(n) > 0); +} + +bool BigInt::operator==(BigInt n) +{ + return(compare(n) == 0); +} + +bool BigInt::operator<=(BigInt n) +{ + return(compare(n) <= 0); +} + +bool BigInt::operator>=(BigInt n) +{ + return(compare(n) >= 0); +} + +bool BigInt::operator<(int n) +{ + return(compare(BigInt(n)) < 0); +} + +bool BigInt::operator>(int n) +{ + return(compare(BigInt(n)) > 0); +} + +bool BigInt::operator==(int n) +{ + return(compare(BigInt(n)) == 0); +} + +bool BigInt::operator<=(int n) +{ + return(compare(BigInt(n)) <= 0); +} + +bool BigInt::operator>=(int n) +{ + return(compare(BigInt(n)) >= 0); +} + +int BigInt::compare(BigInt n) +{ + if (sign < n.sign) return -1; + if (sign > n.sign) return 1; + if (size < n.size) return -sign; + if (size > n.size) return sign; + for (int i = size - 1; i >= 0; i--) + { + if (digits[i] < n.digits[i]) return -sign; + else if (digits[i] > n.digits[i]) return sign; + } + return 0; +} + +int main() +{ + int n, f[10]; + while (cin>>n && n) { + BigInt x = 1; + for (int i=2; i<=n; i++) + x *= i; + + string s = x.toString(); + memset(f, 0, sizeof(f)); + for(int i=0; s[i]; i++) + f[s[i]-'0']++; + + printf("%d! --\n", n); + printf(" (0)%5d (1)%5d (2)%5d (3)%5d (4)%5d\n", f[0],f[1],f[2],f[3],f[4]); + printf(" (5)%5d (6)%5d (7)%5d (8)%5d (9)%5d\n", f[5],f[6],f[7],f[8],f[9]); + } +} + diff --git a/uva_cpp_clean/324/324-9.cpp b/uva_cpp_clean/324/324-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6901bcb9c79576b29f64f448128e8a6cf40e997e --- /dev/null +++ b/uva_cpp_clean/324/324-9.cpp @@ -0,0 +1,19 @@ +import math +import collections + +def solution(): + while True: + n = int(input()) + if n==0: + break + v = collections.Counter(str(math.factorial(n))) + print('%d! --' %(n)) + for it in range(5): + print(' (%d)%5d' %(it, v[str(it)]), end = '') + print('') + for it in range(5,10): + print(' (%d)%5d' %(it, v[str(it)]), end = '') + print('') + +if __name__=="__main__": + solution() diff --git a/uva_cpp_clean/325/325-21.cpp b/uva_cpp_clean/325/325-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8766f93857e26ebfbba7c80304bc7c1ee8ad07ec --- /dev/null +++ b/uva_cpp_clean/325/325-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 325 + Name: Identifying Legal Pascal Real Constants + Problem: https://onlinejudge.org/external/3/325.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + regex re_flo("\\.|e", regex_constants::ECMAScript | regex_constants::icase); + regex re_num("^\\s*(\\+|\\-)?\\d+(\\.\\d+)?(e(\\+|\\-)?\\d+)?\\s*$", + regex_constants::ECMAScript | regex_constants::icase); + regex re_str("^\\s*(.*?)\\s*$", regex_constants::ECMAScript); + + string line; + while (getline(cin, line)) { + smatch sm; + regex_match(line, sm, re_str); + if (sm[1] == "*") break; + + bool legal = line.find_first_of(".eE")!=string::npos && regex_match(line, re_num); + cout << sm[1] << (legal ? " is legal.\n" : " is illegal.\n"); + } +} diff --git a/uva_cpp_clean/326/326-19.cpp b/uva_cpp_clean/326/326-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18c729f80f5450c22fa5bf1b47760723a119f91a --- /dev/null +++ b/uva_cpp_clean/326/326-19.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main() { + long long vals[13]; + int N; + while (cin >> N && N != 0) { + int K; + for (int i = 0; i < N; i++) cin >> vals[i]; + for (int j = 1; j < N; j++) + for (int i = 0; i < N - j; i++) vals[i] = vals[i+1]-vals[i]; + cin >> K; + for (int j = 0; j < K; j++) + for (int i = 1; i < N; i++) vals[i] = vals[i-1]+vals[i]; + printf("Term %d of the sequence is %lld\n", N + K, vals[N-1]); + } + return 0; +} diff --git a/uva_cpp_clean/326/326-9.cpp b/uva_cpp_clean/326/326-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f57d8556dc30ce6216341e4c5cdaabd74f7861e3 --- /dev/null +++ b/uva_cpp_clean/326/326-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +const int SIZE = 12; + +int n, k; +long long num, den, C, Tk, dif[SIZE][SIZE]; + +int main(){ + while(scanf("%d", &n), n){ + for(int j = 0; j < n; j++) scanf("%lld", &dif[0][j]); + for(int i = 1; i < n; i++) + for(int j = 0; j < n - i; j++) + dif[i][j] = dif[i - 1][j + 1] - dif[i - 1][j]; + scanf("%d", &k); + k += n; + C = 1, num = k - 1, den = 1; + Tk = 0; + for(int i = 0; i < n; i++) + Tk += C * dif[i][0], C = C * num-- / den++; + printf("Term %d of the sequence is %lld\n", k, Tk); + } + return(0); +} diff --git a/uva_cpp_clean/331/331-21.cpp b/uva_cpp_clean/331/331-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0640b8d05e8a47d10f5d2b1cf5552ffc1808b7e4 --- /dev/null +++ b/uva_cpp_clean/331/331-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 331 + Name: Mapping the Swaps + Problem: https://onlinejudge.org/external/3/331.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int X[7], n, r, best; +void bt(int cnt) { + if (cnt > best) return; + + bool sorted = 1; + for (int i=1; i> n && n; ++cse) { + for (int i=0; i> X[i]; + + best = n*(n-1)/2; + r = 0; + bt(0); + cout << "There are " << (!best ? 0 : r) << " swap maps for input data set " << cse << ".\n"; + } +} diff --git a/uva_cpp_clean/332/332-19.cpp b/uva_cpp_clean/332/332-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28c0f0272d6829f7ab60d715d13cf8714ace1d68 --- /dev/null +++ b/uva_cpp_clean/332/332-19.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +typedef long long ll; +typedef pair ii; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +int main() { + int j, c = 1; + while (cin >> j && j != -1) { + string s; + cin >> s; + int k = s.length() - j - 2; + ll a = 0, b = 0, p1 = 1, p2 = 1; + for (int i = 2; i < s.length(); i++) { + a *= 10; + a += s[i] - '0'; + p1 *= 10; + if (i - 1 == k) { + b = a; + p2 = p1; + } + } + if (j != 0) a -= b, p1 -= p2; + ll g = gcd(a, p1); + a /= g, p1 /= g; + printf("Case %d: %lld/%lld\n", c++, a, p1); + } + return 0; +} diff --git a/uva_cpp_clean/332/332-9.cpp b/uva_cpp_clean/332/332-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c153f8fc7ed291c57769d01f3217b78dd3c049a --- /dev/null +++ b/uva_cpp_clean/332/332-9.cpp @@ -0,0 +1,24 @@ +def solution(): + + def gcd(a, b): + return a if b==0 else gcd(b, a % b) + + tc = 0 + while True: + tc += 1 + line = input().strip() + if line == '-1': + break + j, n = line.split() + j = int(j) + k = len(n) - 2 - j + n = n[2:] + ( 20 * n [-j:] if j != 0 else '' ) + if j != 0: + num = int(n[:k+j]) - ( int(n[:k]) if k != 0 else 0 ) + else: + num = int(n) + den = ( 10**(k + j) - 10**k if j != 0 else 10**k) + print('Case %d: %d/%d' %(tc, num // gcd(num, den), den // gcd(num, den))) + +if __name__=="__main__": + solution() diff --git a/uva_cpp_clean/333/333-21.cpp b/uva_cpp_clean/333/333-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8246b3320fd4ba149ed69b687c552f47fb9e4d2 --- /dev/null +++ b/uva_cpp_clean/333/333-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 333 + Name: Recognizing Good ISBNs + Problem: https://onlinejudge.org/external/3/333.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool iswhite(char ch) { + switch(ch) { + case ' ': + case '\t': + case '\r': + case '\n': + return 1; + } + return 0; +} + +bool check(char isbn[]) { + int digits[20], l = 0; + for (int i=0; isbn[i]; ++i) + if (isbn[i] == '-' || iswhite(isbn[i])) continue; + else if (isbn[i]=='X' && l == 9) + digits[l++] = 10; + else if (isdigit(isbn[i])) + digits[l++] = isbn[i]-'0'; + else return 0; + + if (l != 10) return 0; + + int r = digits[0]; + for (int i=1; i<10; ++i) + r += (digits[i] += digits[i-1]); + return r % 11 == 0; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string lin; + while (getline(cin, lin)) { + char isbn[90] = {}; + int s = 0, e = lin.length()-1; + while (iswhite(lin[s])) ++s; + while (iswhite(lin[e])) --e; + for (int i=s; i<=e; ++i) + isbn[i-s] = lin[i]; + + cout << isbn << (check(isbn) ? " is correct.\n" : " is incorrect.\n"); + } +} diff --git a/uva_cpp_clean/334/334-21.cpp b/uva_cpp_clean/334/334-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1fd5218726fd9bcb7e026897a9ac099ce8185f5 --- /dev/null +++ b/uva_cpp_clean/334/334-21.cpp @@ -0,0 +1,83 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 334 + Name: Identifying Concurrent Events + Problem: https://onlinejudge.org/external/3/334.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int T[1000][1000], idcnt; +unordered_map ids; +string names[1000]; +int readId() { + cin >> names[idcnt]; + auto r = ids.emplace(names[idcnt], idcnt); + if (r.second) { + ++idcnt; + for (int i=0; i<=idcnt; ++i) + T[i][idcnt] = T[idcnt][i] = 0; + } + return r.first->second; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, m; + for (int cse=1; cin >> n && n; ++cse) { + ids.clear(); + T[0][0] = idcnt = 0; + for (int i=0; i> m; + if (!m) continue; + int u = readId(); + for (int j=1; j> m; + for (int i=0; i 1) + cout << '(' << names[j2] << ',' << names[i2] << ") "; + cout << endl; + } + } +} diff --git a/uva_cpp_clean/334/334-9.cpp b/uva_cpp_clean/334/334-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e793ae5e7e57731abaa4dae69db75e38324f25a --- /dev/null +++ b/uva_cpp_clean/334/334-9.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const int SIZE = 105, LEN = 30; + +int n; + +int ac[SIZE][LEN]; +char ch[] = "IVXLC"; + +void DecimalToRoman(int num){ + map < int, string > cvt; + cvt[1] = "I", cvt[10] = "X", cvt[100] = "C", cvt[1000] = "M"; + cvt[9] = "IX", cvt[90] = "XC", cvt[900] = "CM"; + cvt[5] = "V", cvt[50] = "L", cvt[500] = "D"; + cvt[4] = "IV", cvt[40] = "XL", cvt[400] = "CD"; + for(map :: reverse_iterator it = cvt.rbegin(); it != cvt.rend(); ++it) + while( num >= it -> first ){ + for(int j = 0; j < ( it -> second ).size(); j++) + ac[n][ ( it -> second )[j] - 'A' ]++; + num -= it -> first; + } +} + +void preprocess(){ + for( n = 1; n < SIZE; n++){ + DecimalToRoman(n); + for(int j = 0; j < 5; j++){ + ac[n][ch[j] - 'A'] += ac[n - 1][ch[j] - 'A']; + } + } +} + +int main(){ + preprocess(); + while(scanf("%d", &n), n){ + printf("%d: %d i, %d v, %d x, %d l, %d c\n", n, ac[n]['I' - 'A'], ac[n]['V' - 'A'], ac[n]['X' - 'A'], ac[n]['L' - 'A'], ac[n]['C' - 'A']); + } + return(0); +} diff --git a/uva_cpp_clean/336/336-13.cpp b/uva_cpp_clean/336/336-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1c16393959bf2d8517828b8d44d249812755bc8 --- /dev/null +++ b/uva_cpp_clean/336/336-13.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define INF (int)(1e9) + +void initialize(map dist) +{ + map ::iterator it; + + for (it = dist.begin(); it != dist.end(); it++) + { + dist[it->first] = INF; + } +} + +int bfs(int start, int ttl, map < int, vector > graph, map dist) +{ + initialize(dist); + + dist[start] = 0; + + queue q; + + q.push(start); + + while (!q.empty()) + { + int u = q.front(); + + q.pop(); + + for (int i = 0; i < graph[u].size(); i++) + { + int v = graph[u][i]; + + if (dist[v] == INF) + { + q.push(v); + + dist[v] = dist[u] + 1; + } + } + } + + int ans = 0; + map ::iterator it; + + for (it = dist.begin(); it != dist.end(); it++) + { + if (dist[it->first] > ttl) ans++; + } + + return ans; +} + +int main() +{ + int nc, source, ttl, c = 1; + + while (cin >> nc && nc != 0) + { + map dist; + map < int, vector > graph; + + for (int i = 0; i < nc; i++) + { + int x, y; + + cin >> x >> y; + + dist[x] = INF; + dist[y] = INF; + graph[x].push_back(y); + graph[y].push_back(x); + } + + while (cin >> source >> ttl , (source != 0 || ttl != 0)) + { + int ans = bfs(source, ttl, graph, dist); + + printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", c++, ans, source, ttl); + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/336/336-14.cpp b/uva_cpp_clean/336/336-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4f7edb8c5e7db3e92dc90b751121bd81ffe465c9 --- /dev/null +++ b/uva_cpp_clean/336/336-14.cpp @@ -0,0 +1,77 @@ +/*************************************************** + * Problem name : 336 A Node to Far .cpp + * OJ : Uva + * Result : Accepted + * Date : 17-04-17 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 10000 +#define pl printf("\n"); +using namespace std; +typedef long long ll; +map >adj; +mapcost,visit; +int BFS(int s, int d) { + int count = 0; + queueQ; + Q.push(s); + cost[s] = 0; + visit[s] = 1; + while (!Q.empty()) { + int u = Q.front(); + Q.pop(); + int sz = adj[u].size(); + for (int i = 0; i < sz; i++) { + int v = adj[u][i]; + if(!visit[v]) { + cost[v] = cost[u] + 1; + if (cost[v] > d) { + count++; + } + visit[v] = 1 ; + Q.push(v); + } + } + } + return count; +} +void mem() { + visit.clear(); + cost.clear(); +} +int main () { + int N, t = 1; + while (scanf("%d", &N) == 1 && N != 0) { + for (int i = 1; i <= N; i++) { + int u, v; + scanf("%d %d", &u, &v); + adj[u].push_back(v); + adj[v].push_back(u); + } + int s, d; + while (scanf("%d %d", &s, &d) == 2 && (s != 0 || d != 0)) { + int ans = BFS(s,d); + ans += adj.size()-cost.size(); + printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", t++, ans, s, d); + mem(); + } + adj.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/336/336-19.cpp b/uva_cpp_clean/336/336-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee590bb14e9aec7aaea4aaeaa2eda308395d6737 --- /dev/null +++ b/uva_cpp_clean/336/336-19.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef pair ii; + +vector AdjList; +int ind; +unordered_map m; +int visited[40]; + +void bfs(int i, int ttl, int & acum) { + queue q; + q.push(make_pair(i, ttl)); + while (!q.empty()) { + ii n = q.front(); + q.pop(); + if (visited[n.first]) continue; + visited[n.first] = true; + acum++; + for (int j = 0; j < AdjList[n.first].size() && (n.second); j++) + q.push(make_pair(AdjList[n.first][j], n.second - 1)); + } +} + +int main() { + int edges, cases = 1; + while (scanf("%d", &edges) && !(edges == 0)) { + int i, j; + ind = 0; + AdjList.assign(40, vi()); + m.clear(); + for (int k = 0; k < edges; k++) { + scanf("%d %d", &i, &j); + if (!m.count(i)) m[i] = ind, ind++; + if (!m.count(j)) m[j] = ind, ind++; + AdjList[m[i]].push_back(m[j]); + AdjList[m[j]].push_back(m[i]); + } + while (scanf("%d %d", &i, &j) && !(i == 0 && j == 0)) { + int n = 0; + for (int k = 0; k < ind; k++) visited[k] = false; + bfs(m[i], j, n); + printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", cases++, ind-n, i, j); + } + } + return 0; +} diff --git a/uva_cpp_clean/336/336-21.cpp b/uva_cpp_clean/336/336-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfb2be4cf17b8fb8ae0e24ca1a60bb53e733cc23 --- /dev/null +++ b/uva_cpp_clean/336/336-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 336 + Name: A Node Too Far + Problem: https://onlinejudge.org/external/3/336.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int findSet(map &ids, int ind, int &cnt) { + auto it = ids.find(ind); + if (it == ids.end()) + return ids[ind] = cnt++; + + return it->second; +} + +int main(){ + int en, x, y, s, ttl, cse=1; + unsigned int ar[30][30]; + while (cin>>en && en) { + map ids; + int cnt = 0; + + for (int i=0; i<30; i++) { + ar[i][i]=0; + for (int j=i+1; j<30; j++) + ar[i][j]=ar[j][i]=1000; + } + + for (int i=0; i>x>>y; + int xi = findSet(ids, x, cnt), + yi = findSet(ids, y, cnt); + + ar[xi][yi]=ar[yi][xi]=1; + } + + for (int k=0; k>s>>ttl && (s||ttl)) { + int result = 0; + auto it = ids.find(s); + + if (it == ids.end()) result = cnt; + else { + int sid = it->second; + for (int i=0; i ttl) + result++; + } + + printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", cse++, result, s, ttl); + } + } +} diff --git a/uva_cpp_clean/336/336-4.cpp b/uva_cpp_clean/336/336-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b2318879d4f7b65e9fa012b0e16f87747cbba15 --- /dev/null +++ b/uva_cpp_clean/336/336-4.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +int main() +{ + int caseNr = 0, nc; + + while (std::cin >> nc && nc != 0) + { + std::unordered_map> nodes; + + for (int i = 0; i < nc; i++) + { + int left, right; + std::cin >> left >> right; + + nodes[left].push_back(right); + nodes[right].push_back(left); + } + + int n, ttl; + while (std::cin >> n >> ttl && (n != 0 || ttl != 0)) + { + std::queue pending; + std::unordered_map visited; + + pending.push(n); + + for (int i = 0; i < ttl; i++) + { + std::queue temp; + + while (!pending.empty()) + { + int current = pending.front(); + + pending.pop(); + visited[current] = true; + + for (auto &x : nodes[current]) + { + if (!visited[x]) + { + temp.push(x); + } + } + } + + pending = temp; + } + + if (ttl == 0 && nodes.find(n) != nodes.end()) + { + visited[n] = true; + } + + std::cout << "Case " << ++caseNr << ": " << nodes.size() - visited.size() << " nodes not reachable from node " << n << " with TTL = " << ttl << "." << std::endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/336/336-5.cpp b/uva_cpp_clean/336/336-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f36a408a600d12371e8fae82311e2d0b79f81ae2 --- /dev/null +++ b/uva_cpp_clean/336/336-5.cpp @@ -0,0 +1,203 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + + +int main() +{ + int n,no=0; + while(si(n)) + { + if(n==0) break; + + int a,b; + map >g; + for0(i,n) + { + sii(a,b); + g[a].pb(b); + g[b].pb(a); + } + + while(sii(a,b)) + { + if( a==0 && b==0) + break; + + mii mp; + int cnt; + queueq; + + q.push(a); + mp[a]=0; + + while(!q.empty()) + { + int pr=q.front(); + q.pop(); + + int len=sz(g[pr]); + for0(i,len) + { + int child= g[pr][i]; + if(mp[child]==0 && child != a) + { + mp[child]= mp[pr]+1; + q.push(child); + } + } + } + + cnt=0; + forstl(it,mp) + { + + int p=it->se; + if(p<=b) + cnt++; + } + + int res=g.size()-cnt; + printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n",++no,res,a,b); + + } + + g.clear(); + + } + + + return 0; +} + + diff --git a/uva_cpp_clean/336/336-6.cc b/uva_cpp_clean/336/336-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..9c7879e763f3467792d57f820748f76a07507d96 --- /dev/null +++ b/uva_cpp_clean/336/336-6.cc @@ -0,0 +1,54 @@ +// https://uva.onlinejudge.org/external/3/336.pdf +#include +using namespace std; +using ii=tuple; +using vii=vector; +using vi=vector; +using vvi=vector; +using mii=unordered_map; +using qi=queue; +int main(){ + for(int T=1;;){ + int m,u,v,w,n=0,k; + cin>>m; + if(!m)break; + mii x; + vii e(m); + for(int i=0;i>u>>v; + if(!x[u])x[u]=++n; + if(!x[v])x[v]=++n; + e[i]=make_tuple(x[u]-1,x[v]-1); + } + vvi g(n); + for(ii a:e){ + tie(u,v)=a; + g[u].push_back(v); + g[v].push_back(u); + } + for(;;){ + cin>>w>>k; + if(!w)break; + u=x[w]-1; + vi s(n); + if(u>=0){ + s[u]=1; + qi q; + q.push(u); + while(!q.empty()){ + u=q.front(); + q.pop(); + if(s[u]<=k) + for(int v:g[u]) + if(!s[v]){ + s[v]=s[u]+1; + q.push(v); + } + } + } + int c=0; + for(int x:s)c+=(!x); + cout<<"Case "< + +using namespace std; + +int n, tc; +long long _source, TTL, u, v; +map > G; +map d; +set vert; + +int bfs(int source){ + for(auto it : vert) d[it] = INT_MAX; + queue Q; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + u = Q.front(), Q.pop(); + for(int i = 0; i < G[u].size(); i++){ + v = G[u][i]; + if(d[v] == INT_MAX && d[u] + 1 <= TTL) + d[v] = d[u] + 1, Q.push(v); + } + } + int ct = 0; + for(auto it : vert) + if(d[it] > TTL) ct++; + return ct; +} + +int main(){ + while(scanf("%d", &n), n){ + G.clear(), vert.clear(); + for(int i = 0; i < n; i++) scanf("%lld %lld", &u, &v), G[u].push_back(v), G[v].push_back(u), vert.insert(u), vert.insert(v); + while(scanf("%lld %lld", &_source,&TTL), _source|TTL){ + printf("Case %d: %d nodes not reachable from node %lld with TTL = %lld.\n", ++tc, bfs(_source), _source, TTL); + } + } + return(0); +} diff --git a/uva_cpp_clean/337/337-15.cpp b/uva_cpp_clean/337/337-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6afe7a0de6d93a31774aff9024ba82129f9a636 --- /dev/null +++ b/uva_cpp_clean/337/337-15.cpp @@ -0,0 +1,113 @@ +/* + 模擬、字元替換 + 時間複雜度: O(N) +*/ +#include +#include +#include + +using namespace std; + +char terminal[10][10]; + +void clear_terminal() { + int i, j; + for (i = 0; i < 10; i++) + for (j = 0; j < 10; j++) + terminal[i][j] = ' '; +} + +void write_character(char character, int row, int column, bool mode) { + if (mode) + for (int i = 8; i >= column; i--) + terminal[row][i + 1] = terminal[row][i]; + + terminal[row][column] = character; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, length, n, row, column, case_number = 0; + bool mode; // 0:OVERWRIE, 1:INSERT + string input; + + while (cin >> n && n) { + case_number++; + row = column = mode = 0; + clear_terminal(); + + cin.ignore(1024, '\n'); // 清空多餘的字 + + while (n--) { + getline(cin, input); + length = input.size(); + + for (i = 0; i < length; i++) { + if (input[i] == '^') { + i++; + switch (input[i]) { + case 'b': + column = 0; + break; + case 'c': + clear_terminal(); + break; + case 'd': + row = min(row + 1, 9); + break; + case 'e': + for (j = column; j < 10; j++) + terminal[row][j] = ' '; + break; + case 'h': + row = column = 0; + break; + case 'i': + mode = 1; + break; + case 'l': + column = max(0, column - 1); + break; + case 'o': + mode = 0; + break; + case 'r': + column = min(column + 1, 9); + break; + case 'u': + row = max(0, row - 1); + break; + case '^': + write_character('^', row, column, mode); + column = min(column + 1, 9); + break; + default: + row = input[i] - '0'; + i++; + column = input[i] - '0'; + break; + } + } else { + write_character(input[i], row, column, mode); + column = min(column + 1, 9); + } + } + } + + cout << "Case " << case_number << "\n+----------+\n"; + + for (i = 0; i < 10; i++) { + cout << "|"; + + for (j = 0; j < 10; j++) + cout << terminal[i][j]; + + cout << "|\n"; + } + cout << "+----------+\n"; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/337/337-16.cpp b/uva_cpp_clean/337/337-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf1aa0af152b41f6a3c46eae7bb96db7c217e15c --- /dev/null +++ b/uva_cpp_clean/337/337-16.cpp @@ -0,0 +1,74 @@ +//============================================================================ +// Name : 337 - Interpreting Control Sequences.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 337 Interpreting Control Sequences in C++, Ansi-style +// Run Time : 0.008 seconds +//============================================================================ + +#include +#include +#include +using namespace std; +#define FRI(i, a, b) for(i = a; i < (int)b; ++i) + +int main() { + int i, j, k, n; + int count, row, col; + bool omode; + string word; + count = 1; + while (getline(cin, word) && word != "0") { + stringstream ss(word); + ss >> n; + string terminal[] = { + "+----------+", + "| |", + "| |", + "| |", + "| |", + "| |", + "| |", + "| |", + "| |", + "| |", + "| |", + "+----------+" + }; + row = col = omode = 1; + FRI (i, 0, n) { + getline(cin, word); + FRI (j, 0, word.size()) { + if (word[j] == '^') { + j++; + if (word[j] == 'b') { col = 1; } + else if (word[j] == 'c') { terminal[row] = "| |"; } + else if (word[j] == 'd') { if (row < 10) row++; } + else if (word[j] == 'e') { FRI(k, row, 11) terminal[k].replace(col, 1, 1, ' '); } + else if (word[j] == 'h') { row = col = 1; } + else if (word[j] == 'i') { omode = false; } + else if (word[j] == 'l') { if (col > 1) col--; } + else if (word[j] == 'o') { omode = true; } + else if (word[j] == 'r') { if (col < 10) col++; } + else if (word[j] == 'u') { if (row > 1) row--; } + else if (isdigit(word[j]) && isdigit(word[j + 1])) { + row = word[j] - '0' + 1; + col = word[++j] - '0' + 1; + } + if (word[j] != '^') continue; + } + + if (omode) terminal[row].erase(col, 1); + else terminal[row].erase(10, 1); + + terminal[row].insert(col, 1, word[j]); + if (col < 10) col++; + + } + } + cout << "Case " << count << endl; + FRI (i, 0, 12) cout << terminal[i] << endl; + count++; + } + return 0; +} diff --git a/uva_cpp_clean/340/340-16.cpp b/uva_cpp_clean/340/340-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad28adcf89fb4d71ef7032b0e608909f6188d750 --- /dev/null +++ b/uva_cpp_clean/340/340-16.cpp @@ -0,0 +1,74 @@ +//============================================================================ +// Name : 340 - Master-Mind Hints.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 340 Master-Mind Hints in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include +using namespace std; + +#define FRI(i, a, b) for(i = a; i < (int)b; ++i) + +int main() { + int i; + int n, game = 0; + int sum, strong, weak; + + while (cin >> n, n != 0) { + bool mark[n]; + int secret[n]; + int guess[n]; + int basket[10]; + int cloner[10]; + + memset(basket, false, sizeof(basket)); + + FRI(i, 0, n) { + cin >> secret[i]; + basket[secret[i]]++; + } + + cout << "Game " << ++game << ":" << endl; + while (true) { + sum = strong = weak = 0; + memset(mark, false, sizeof(mark)); + + FRI(i, 0, n) { + cin >> guess[i]; + sum |= guess[i]; + } + + if (sum == 0) { + break; + } + + FRI(i, 1, 10) { + cloner[i] = basket[i]; + } + + // strong + FRI(i, 0, n) { + if (secret[i] == guess[i]) { + mark[i] = true; + cloner[secret[i]]--; + strong++; + } + } + + // weak + FRI(i, 0, n) { + if (!mark[i] && cloner[guess[i]]) { + cloner[guess[i]]--; + weak++; + } + } + + cout << " (" << strong << "," << weak << ")" << endl; + } + + } + return 0; +} diff --git a/uva_cpp_clean/340/340-18.cpp b/uva_cpp_clean/340/340-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d280666e9ae8620aa2cc678ffbfa9e7142c04aa7 --- /dev/null +++ b/uva_cpp_clean/340/340-18.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int n = 1, strongH, weakH, game = 0; + string code, guess = "", codeCopy; + vector guessV; + while(true){ + cin >> n; + if(n == 0) break; + getline(cin, code); //Consume new line + getline(cin, code); + while(true){ + getline(cin, guess); + if(guess[0] == '0') break; + guessV.push_back(guess); + } + game++; + printf("Game %d:\n", game); + for(int i = 0; i < guessV.size(); i++){ + strongH = 0; + weakH = 0; + codeCopy = code; + for(int j = 0; j < code.size(); j++){ + if(codeCopy[j] == ' ') continue; + if(codeCopy[j] == guessV.at(i).at(j)){ + strongH++; + guessV.at(i).at(j) = '.'; + codeCopy[j] = '.'; + } + } + for(int j = 0; j < code.size(); j++){ + if(codeCopy[j] == ' ' || codeCopy[j] == '.') continue; + for(int k = 0; k < code.size(); k++){ + if(codeCopy[j] == guessV.at(i).at(k)){ + weakH++; + guessV.at(i).at(k) = '.'; + break; + } + } + } + printf(" (%d,%d)\n", strongH, weakH); + } + guessV.clear(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/340/340-19.cpp b/uva_cpp_clean/340/340-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e01ff790ae0de3e165bb2962111c5e3ac9e9241 --- /dev/null +++ b/uva_cpp_clean/340/340-19.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +int main() { + int N, code[1002], hint[1002], countc[10], counth[10], cases = 1; + while (cin >> N && N != 0) { + printf("Game %d:\n", cases++); + for (int i = 0; i < N; i++) cin >> code[i]; + do { + for (int i = 0; i < N; i++) cin >> hint[i]; + for (int i = 0; i < 10; i++) countc[i] = counth[i] = 0; + if (hint[0] != 0) { + int sm = 0, wm = 0; + for (int i = 0; i < N; i++) { + if (code[i] == hint[i]) sm++; + else { + countc[code[i]-1]++; + counth[hint[i]-1]++; + } + } + for (int i = 0; i < 10; i++) { + wm += countc[i] > counth[i]? counth[i]: countc[i]; + } + printf(" (%d,%d)\n", sm, wm); + } + } while (hint[0] != 0); + } + return 0; +} diff --git a/uva_cpp_clean/340/340-21.cpp b/uva_cpp_clean/340/340-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c6ad6e4d0c0ed219e322fb7eeac2c13fa78195d --- /dev/null +++ b/uva_cpp_clean/340/340-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 340 + Name: Master-Mind Hints + Problem: https://onlinejudge.org/external/3/340.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + int n, cse = 1; + int secret[1002], gs, cs[10], cg[10]; + while(cin>>n && n) { + memset(cs, 0, sizeof(cs)); + for (int i=0; i>secret[i]; + cs[secret[i]]++; + } + + printf("Game %d:\n", cse++); + for(;;) { + int exact=0, mm=0; + memset(cg, 0, sizeof(cg)); + for (int i=0; i>gs; + cg[gs]++; + if (secret[i] == gs) + exact++; + } + + if (!gs) break; + for (int i=1; i<10; i++) + mm += min(cg[i], cs[i]); + + printf(" (%d,%d)\n", exact, mm-exact); + } + } +} diff --git a/uva_cpp_clean/340/340-4.cpp b/uva_cpp_clean/340/340-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9d5e5e0ca7fd7ff96d644289856e0a4fe121ccf --- /dev/null +++ b/uva_cpp_clean/340/340-4.cpp @@ -0,0 +1,62 @@ +#include +#include + +bool input(int *tab, int n) +{ + for (int i = 0; i < n; i++) + { + std::cin >> tab[i]; + } + + return tab[n - 1] != 0; +} + +int main() +{ + int n, game = 0; + while (std::cin >> n && n != 0) + { + int secret[n], guess[n]; + input(secret, n); + + std::cout << "Game " << ++game << ":" << std::endl; + while (input(guess, n)) + { + int strong = 0, weak = 0; + int secret_copy[n]; + memcpy(secret_copy, secret, n * sizeof(int)); + + for (int i = 0; i < n; i++) + { + if (guess[i] == secret[i]) + { + strong++; + guess[i] = 0; + secret_copy[i] = 0; + } + } + + for (int i = 0; i < n; i++) + { + if (guess[i] == 0) + { + continue; + } + + for (int j = 0; j < n; j++) + { + if (guess[i] == secret_copy[j]) + { + weak++; + secret_copy[j] = 0; + break; + } + } + } + + std::cout << " (" << strong << "," << weak << ")" << std::endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/340/340-9.cpp b/uva_cpp_clean/340/340-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b36fd59b3e4f52ab3f9b715edd9f3c99c2b8663 --- /dev/null +++ b/uva_cpp_clean/340/340-9.cpp @@ -0,0 +1,31 @@ +#include + +#define SIZE 1010 + +using namespace std; + +int tc, N, a, b, code[SIZE], guess[SIZE]; +bool vis[SIZE]; + +int main(){ + while(scanf("%d", &N), N){ + for(int it = 0; it < N; it++) scanf("%d", &code[it]); + printf("Game %d:\n", ++tc); + while(true){ + for(int it = 0; it < N; it++) scanf("%d", &guess[it]); + if(guess[0] == 0) break; + a = b = 0; + memset(vis, false, sizeof vis); + for(int it = 0; it < N; it++) + if( guess[it] == code[it] ) a++, vis[it] = true; + for(int it = 0; it < N; it++) + if( guess[it] != code[it] ){ + for(int i = 0; i < N; i++) + if( not vis[i] and code[i] == guess[it] ) + vis[i] = true, b++, i = N; + } + printf(" (%d,%d)\n", a, b); + } + } + return(0); +} diff --git a/uva_cpp_clean/341/341-16.cpp b/uva_cpp_clean/341/341-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c3c59be944ee9420209e6607164ee24ff43fb0a --- /dev/null +++ b/uva_cpp_clean/341/341-16.cpp @@ -0,0 +1,71 @@ +//============================================================================ +// Name : 341 - Non-Stop Travel.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 341 Non-Stop Travel in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include +#include +#include + +using namespace std; + +#define MAX 1000000 +#define FRI(i, a, b) for(i = a; i < (int)b; ++i) + +int main() { + int i, j, k, l; + int n, m, cases = 0; + int start, end; + stack store; + char numstr[21]; + + while (cin >> n, n != 0) { + ++n; + int delay[n][n]; + int minimal[n]; + string answer[n]; + + store = stack(); + fill(minimal, minimal + n, MAX); + memset(delay, -1, sizeof(delay)); + + FRI(i, 1, n) { + cin >> m; + FRI(j, 0, m) { + cin >> k >> l; + delay[i][k] = l; + } + } + + cin >> start >> end; + sprintf(numstr, "%d", start); + answer[start] = numstr; + minimal[start] = 0; + store.push(start); + + while (!store.empty()) { + start = store.top(); + store.pop(); + + FRI (i, 1, n) { + if (delay[start][i] != -1 && start != i) { + if (minimal[i] > minimal[start] + delay[start][i]) { + sprintf(numstr, "%d", i); + minimal[i] = minimal[start] + delay[start][i]; + answer[i] = answer[start] + " " + numstr; + if (i != end) { + store.push(i); + } + } + } + } + } + cout << "Case " << ++cases << ": Path = " << answer[end] << "; " << minimal[end] <<" second delay" << endl; + } + + return 0; +} diff --git a/uva_cpp_clean/341/341-20.cpp b/uva_cpp_clean/341/341-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcbd2c9551154266caac09f100c8a74b2e20f695 --- /dev/null +++ b/uva_cpp_clean/341/341-20.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +const int INF = ((1<<31) - (1)); +const int MAXN = 20; +vector < int > edge[MAXN+2]; +vector < int > cost[MAXN+2]; +int path[MAXN+2]; + +struct data { + int city, dist; + bool operator < ( const data & p ) const { + return dist > p.dist; + } +} ; + +int dijkstra( int source, int destination ) { + data u, v; + priority_queue < data > q; + int d[MAXN+2]; + for( int i = 0 ; i < MAXN ; ++i ) { + d[i] = INF; + } + + u.city = source, u.dist = 0; + q.push( u ), d[source] = 0; + while( !q.empty() ) { + u = q.top(); + q.pop(); + for( int i = 0 ; i < edge[u.city].size() ; ++i ) { + v.city = edge[u.city][i]; + v.dist = cost[u.city][i] + d[u.city]; + if( d[v.city] > v.dist ) { + d[v.city] = v.dist; + q.push( v ); + path[v.city] = u.city; + } + } + } + return d[destination]; +} + +void printPath( int source, int destination ) { + if( source != destination ) { + printPath( source, path[destination] ); + } + printf( " %d", destination ); +} + +int main( int argc, char ** argv ) { + int NI, nCase = 0; + while( scanf( "%d", &NI ) == 1 && NI ) { + for( int x = 1 ; x <= NI ; ++x ) { + int n; + scanf( "%d", &n ); + for( int i = 1 ; i <= n ; ++i ) { + int y, c; + scanf( "%d %d", &y, &c ); + + edge[x].push_back( y ); + + cost[x].push_back( c ); + } + } + int S, T; + scanf( "%d %d", &S, &T ); + + memset( path, -1, sizeof( path ) ); + int dis = dijkstra( S, T ); + + printf( "Case %d: Path =", ++nCase ); + printPath( S, T ); + printf( "; %d second delay\n", dis ); + + for( int i = 1 ; i <= NI ; ++i ) { + edge[i].clear(); + cost[i].clear(); + } + } + return 0; +} diff --git a/uva_cpp_clean/341/341-21.cpp b/uva_cpp_clean/341/341-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..238e7d59e3433105e2b7025be14c3945a546c1c7 --- /dev/null +++ b/uva_cpp_clean/341/341-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 341 + Name: Non-Stop Travel + Problem: https://onlinejudge.org/external/3/341.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +struct State { + int u, d; + State(){} + State(int u, int d):u(u),d(d){} + bool operator < (const State& e) const { return d > e.d; } +}; + +vector adj[43]; +int D[43], pre[43]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, src, dst; + for (int cse=1; cin >> n && n; ++cse) { + cout << "Case " << cse << ": Path ="; + + for (int i=1, m; i<=n; ++i) { + cin >> m; + adj[i].resize(m); + for (int j=0; j> adj[i][j].u >> adj[i][j].d; + D[i] = INF; + } + + cin >> src >> dst; + priority_queue q; + q.push(State(src, D[src]=0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.d != D[s.u]) continue; + if (s.u == dst) { + stack st; + for (int u=s.u; u!=src; u=pre[u]) + st.push(u); + st.push(src); + while (!st.empty()) { + cout << ' ' << st.top(); + st.pop(); + } + cout << "; " << s.d << " second delay\n"; + break; + } + + for (State vd: adj[s.u]) + if (s.d + vd.d < D[vd.u]) { + q.push(State(vd.u, D[vd.u]=s.d + vd.d)); + pre[vd.u] = s.u; + } + } + } +} diff --git a/uva_cpp_clean/343/343-19.cpp b/uva_cpp_clean/343/343-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc68b9a08c99dfcc0cb44bd08f5c333f9e211948 --- /dev/null +++ b/uva_cpp_clean/343/343-19.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +int value(char c) { + int val = c - 'A'; + if (val < 0) return c - '0'; + return val + 10; +} + +long long eval(string & s, int base) { + long long ret = 0, pot = 1; + for (int i = s.length()-1; i >= 0; i--) { + ret += value(s[i]) * pot; + pot *= base; + } + return ret; +} + +int main() { + string s1, s2; + while (cin >> s1 >> s2) { + int b1 = 2, b2 = 2; + for (int i = 0; i < s1.length(); i++) b1 = max(b1, value(s1[i]) + 1); + for (int i = 0; i < s2.length(); i++) b2 = max(b2, value(s2[i]) + 1); + bool ok = false; + for (int i = b1; i <= 36 && !ok; i++) { + for (int j = b2; j <= 36 && !ok; j++) { + if (eval(s1, i) == eval(s2, j)) { + printf("%s (base %d) = %s (base %d)\n", s1.c_str(), i, s2.c_str(), j); + ok = true; + } + } + } + if (!ok) printf("%s is not equal to %s in any base 2..36\n", s1.c_str(), s2.c_str()); + } + return 0; +} diff --git a/uva_cpp_clean/343/343-21.cpp b/uva_cpp_clean/343/343-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e959ccc657804b8166c7b0cc7cf0631f5bcaad4 --- /dev/null +++ b/uva_cpp_clean/343/343-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 343 + Name: What Base Is This? + Problem: https://onlinejudge.org/external/3/343.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int val[127]; +unsigned long long MODULUS; +void newModulus() { + MODULUS = (unsigned long long)rand() * rand(); +} + +long long conv(const char num[], const int bf) { + long long result = 0; + for (int i=0; num[i]; i++) + result = (result*bf + val[num[i]]) % MODULUS; + return result; +} + +int minbase(const char num[]) { + int mn = 2; + for (int i=0; num[i]; i++) + if (val[num[i]] >= mn) + mn = val[num[i]]+1; + return mn; +} + + +int main(){ + char ds[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + for (int i=0; ds[i]; i++) + val[ds[i]] = i; + + newModulus(); + char w1[20000], w2[20000]; + while (cin>>w1>>w2) { + int m1 = minbase(w1), + m2 = minbase(w2); + + for (int i=m1; i<=36; i++) { + long long x = conv(w1, i); + + for (int j=m2; j<=36; ++j) { + long long r = conv(w2, j); + if (x == r) { + // double check with another modulus + newModulus(); + if ((x=conv(w1, i)) == conv(w2, j)) { + printf("%s (base %d) = %s (base %d)\n", w1, i, w2, j); + goto fin; + } + } + } + } + + printf("%s is not equal to %s in any base 2..36\n", w1, w2); + fin:; + } +} diff --git a/uva_cpp_clean/344/344-16.cpp b/uva_cpp_clean/344/344-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbea27b61eaae7bd8bfa21eda1edeb41f02d0835 --- /dev/null +++ b/uva_cpp_clean/344/344-16.cpp @@ -0,0 +1,51 @@ +//============================================================================ +// Name : 344 - Roman Digititis.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 344 Roman Digititis in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include + +using namespace std; + +int main() { + int i, v, x, l, c; + int n; + + while (cin >> n, n != 0) { + i = v = x = l = c = 0; + for (int m = 1; m <= n; ++m) { + switch (m % 10) { + case 1: i++; break; + case 2: i+=2; break; + case 3: i+=3; break; + case 4: i++,v++; break; + case 5: v++; break; + case 6: v++,i++; break; + case 7: v++,i+=2; break; + case 8: v++,i+=3; break; + case 9: i++,x++; break; + } + + switch (m / 10) { + case 1: x++; break; + case 2: x+=2; break; + case 3: x+=3; break; + case 4: x++,l++; break; + case 5: l++; break; + case 6: l++,x++; break; + case 7: l++,x+=2; break; + case 8: l++,x+=3; break; + case 9: x++,c++; break; + case 10: c++; break; + } + } + + cout << n << ": " << i << " i, " << v << " v, " << x << " x, " << l + << " l, " << c << " c" << endl; + } + + return 0; +} diff --git a/uva_cpp_clean/344/344-19.cpp b/uva_cpp_clean/344/344-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b137708693c3a7cb769002ff3e0ca23c0b37bb76 --- /dev/null +++ b/uva_cpp_clean/344/344-19.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; + +void value(int n, int & i, int & v, int & x, int & l, int & c) { + if (n == 100) { + c += 1; + return; + } + switch(n%10) { + case 9: i += 1, x += 1; break; + case 8: i += 3, v += 1; break; + case 7: i += 2, v += 1; break; + case 6: i += 1, v += 1; break; + case 5: v += 1; break; + case 4: i += 1, v += 1; break; + case 3: i += 3; break; + case 2: i += 2; break; + case 1: i += 1; break; + } + switch(n/10) { + case 9: x += 1, c += 1; break; + case 8: x += 3, l += 1; break; + case 7: x += 2, l += 1; break; + case 6: x += 1, l += 1; break; + case 5: l += 1; break; + case 4: x += 1, l += 1; break; + case 3: x += 3; break; + case 2: x += 2; break; + case 1: x += 1; break; + } +} + +int main() { + int number; + while (scanf("%d", &number) && number != 0) { + int i, v, x, l, c; + i = v = x = l = c = 0; + for (int j = 1; j <= number; j++) value(j, i, v, x, l, c); + printf("%d: %d i, %d v, %d x, %d l, %d c\n", number, i, v, x, l, c); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/344/344-20.cpp b/uva_cpp_clean/344/344-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1a113c6a398b57b93ba5c55e41d0aaf275016c8 --- /dev/null +++ b/uva_cpp_clean/344/344-20.cpp @@ -0,0 +1,35 @@ +#include + +int ans[105][7]; +int n; + +int main() +{ + for(int i=1 ; i<102 ; i++) + { + if( i%10==1 || i%10==4 || i%10==6 || i%10==9 ) ans[i][0]++; + if( i%10==2 || i%10==7 ) ans[i][0] += 2; + if( i%10==3 || i%10==8 ) ans[i][0] += 3; + ans[i+1][0] = ans[i][0]; + + if( i%10==4 || i%10==5 || i%10==6 || i%10==7 || i%10==8 ) ans[i][1]++; + ans[i+1][1] = ans[i][1]; + + if( (i>=9 && i<19) || (i>=40 && i<49) || (i>=59 && i<69) || (i>=90 && i<99) ) ans[i][2]++; + if( (i>=19 && i<29) || (i>=69 && i<79) || i==49 || i==99 ) ans[i][2] += 2; + if( (i>=29 && i<39) || (i>=79 && i<89) ) ans[i][2] += 3; + if( i==39 || i==89 ) ans[i][2] += 4; + ans[i+1][2] = ans[i][2]; + + if( i>=40 && i<90 ) ans[i][3]++; + ans[i+1][3] = ans[i][3]; + + if( i>=90 ) ans[i][4]++; + ans[i+1][4] = ans[i][4]; + } + + while( scanf("%d", &n)==1 && n ) + printf("%d: %d i, %d v, %d x, %d l, %d c\n", n, ans[n][0], ans[n][1], ans[n][2], ans[n][3], ans[n][4]); + + return 0; +} diff --git a/uva_cpp_clean/344/344-21.cpp b/uva_cpp_clean/344/344-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e96237a08f10ce6b3e3dd4e36134d7bb0a70bd25 --- /dev/null +++ b/uva_cpp_clean/344/344-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 344 + Name: Roman Digititis + Problem: https://onlinejudge.org/external/3/344.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() +{ + int c[5][101],i,j,t,n; + memset(c,0,sizeof(c)); + + for(i=0;i<101;i++) + for(j=0;j<5;j++) + c[j][i]=0; + for(i=1;i<101;i++) + { + t=i % 10; + for(j=0;j<3;j+=2) + { + if(t>0 && t<4) c[0+j][i]+=t; + if(t>5 && t<9) c[0+j][i]+=t-5; + if(t==4 || t==9) c[0+j][i]+=1; + if(t>3 && t<9) c[1+j][i]+=1; + if(t==9) c[2+j][i]+=1; + + t=i/10; + } + if(i==100)c[4][i]=+1; + for(j=0;j<5;j++) + c[j][i]+=c[j][i-1]; + } + while (cin>>n && n) + { + printf("%d: %d i, %d v, %d x, %d l, %d c\n",n,c[0][n] + ,c[1][n] + ,c[2][n] + ,c[3][n] + ,c[4][n]); + } + return 0; +} diff --git a/uva_cpp_clean/344/344-5.cpp b/uva_cpp_clean/344/344-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..59b4edf3b4771de4e2370f0ad9b1aedefa9d41cd --- /dev/null +++ b/uva_cpp_clean/344/344-5.cpp @@ -0,0 +1,145 @@ +/****************************************************************** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_HIDDEN *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d",a) +#define PLN(a) printf("%lld",a) + + +#define ff first +#define se second +#define pb push_back +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +////============ CONSTANT ===============//// +#define mx (1000000) +#define inf (1<<30) //infinity value +#define eps 1e-9 +#define mod 1000000007 +////====================================//// +int main() +{ + string rom[] = {"i","v","x","l","c"}; + char s; + string mat; + int n; + while(sii(n)) + { + if(n==0) break; + int p=0; + int f=0; + mat=""; + mapmp; + int a,he; + for(int i=1,j=0;i<=n;i++,j++) + { + a=i; + if(a>=90){ + mp[rom[4]]++; + if(a>=90 && a<100) + mp[rom[2]]++; + if(a==99) + mp[rom[2]]++; + } + if(a>=40 && a<90){ + + mp[rom[3]]++; + if(a>=40 && a<=49){ + mp[rom[2]]++; + } + + if(a==49) + mp[rom[2]]++; + if(a>=60) + { + he=a-50; + mp[rom[2]]+=he/10; + } + + } + if(a>=59 && a<99) + { + if(a%10==9) mp[rom[2]]++; + } + if(a>=9 && a<40){ + if(a%10==9) mp[rom[2]]++; + mp[rom[2]]+=a/10; + } + + if(a%10==1 || a%10==6 || a%10==9 || a%10==4) + mp[rom[0]]++; + if(a%10==2 || a%10==7) + mp[rom[0]]+=2; + if(a%10==3 || a%10==8) + mp[rom[0]]+=3; + if(a%10==4||a%10==5 || a%10==6 || a%10==7 || a%10==8) + mp[rom[1]]++; + + } + pf("%d: %d i, %d v, %d x, %d l, %d c\n",n,mp[rom[0]],mp[rom[1]],mp[rom[2]],mp[rom[3]],mp[rom[4]]); + + } + + return 0; +} + + diff --git a/uva_cpp_clean/347/347-2.cpp b/uva_cpp_clean/347/347-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b1bc1e8e69058d80f192f81da6c85b124da74ec --- /dev/null +++ b/uva_cpp_clean/347/347-2.cpp @@ -0,0 +1,91 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int used[10] = {}; +char s[11] = {}; +int ans[100000] = {}, at = 0; +void check(int idx) { + if(idx == 0) + return; + s[idx] = '\0'; + int i, ti, len, cnt = 0; + char ts[11]; + memcpy(ts, s, sizeof(ts)); + len = strlen(ts); + for(i = 0; ;) { + if(ts[i] == -1) { + if(cnt == len && i == 0) { + int n; + sscanf(s, "%d", &n); + ans[at++] = n; + return; + } + return; + } + cnt++; + ti = i + ts[i]-'0'; + ts[i] = -1; + i = ti; + if(i >= len) + i %= len; + } +} +void dfs(int idx) { + check(idx); + int i; + for(i = 1; i <= 9; i++) { + if(used[i] == 0) { + s[idx] = i+'0'; + used[i] = 1; + dfs(idx+1); + used[i] = 0; + } + } +} + +int main(){ + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + + int n, test = 0, tmp; + dfs(0); + sort(ans, ans+at); + while(scanf("%d", &n) == 1 && n) { + for(int i = 0; i < at; i++) + if(ans[i] >= n) { + tmp = ans[i]; + break; + } + printf("Case %d: %d\n", ++test, tmp); + } + + return 0; +} diff --git a/uva_cpp_clean/347/347-21.cpp b/uva_cpp_clean/347/347-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..510b4abfb80e4a01aa7f6593cc08bea571aa8391 --- /dev/null +++ b/uva_cpp_clean/347/347-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 347 + Name: Run + Problem: https://onlinejudge.org/external/3/347.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool seen[100]; +int isrun(char s[], int len) { + seen[0] = 1; + memset(seen+1, 0, 10); + for (int i=0; i=0; --i) + if (seen[i]) + return i; + return 0; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int cse=1, l; + char num[21]; + while (cin>>num && (num[1] || num[0]!='0')) { + for (l=0; num[l]; ++l) + num[l] -= '0'; + + for (;;) { + int er = isrun(num, l); + if (er < 0) break; + + while (er && num[er]>=9) --er; + if (!er && num[0]==9) { + num[0] = 0; + ++l; + } + + ++num[er]; + for (int i=er+1, k=1; i + +using namespace std; + +int n, tc, d, used[10], p; +vector v, ans((int)1e7); + +inline bool isRunNumber(int num){ + v.clear(), d = 0; + memset(used, 0, sizeof used); + while(num) + v.push_back(num % 10), num /= 10, d++; + reverse(v.begin(), v.end()); + p = 0; + for(int i = 0; i < d; i++){ + p = (p + v[p]) % d; + used[v[p]]++; + if(used[v[p]] > 1) return false; + } + if(p != 0) return false; + return true; +} + +int main(){ + for(int i= (int)1e7; i >= 10; i--) + if(isRunNumber(i)) ans[i] = i; + else ans[i] = ans[i + 1]; + while(scanf("%d", &n), n) + printf("Case %d: %d\n", ++tc, ans[n]); + return(0); +} diff --git a/uva_cpp_clean/348/348-21.cpp b/uva_cpp_clean/348/348-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f551059efa09eb57627ee65ffc866213a2706df0 --- /dev/null +++ b/uva_cpp_clean/348/348-21.cpp @@ -0,0 +1,58 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 348 + Name: Optimal Array Multiplication Sequence + Problem: https://onlinejudge.org/external/3/348.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define INF 1073741824 + +int S[20], DP[20][20], P[20][20]; + +void print(int s, int e) { + if (e == s+1) + cout << "A" << e; + else { + putchar('('); + print(s, P[s][e]); + cout << " x "; + print(P[s][e], e); + putchar(')'); + } +} + +int main(){ + int cse=1, n; + while (cin>>n && n) { + for (int i=0; i>S[i]>>S[i+1]; + DP[i][i+1]=0; + } + + for (int l=2; l<=n; l++) + for (int i=0; i<=n-l; i++) { + int j = i + l; + DP[i][j] = INF; + for (int k=1; k + +long nCase, Z, I, M, L; +long randomNumber[10005], len, temp, flag; + +int main() +{ + nCase = 1; + while( scanf("%ld %ld %ld %ld", &Z, &I, &M, &L)==4 && (Z || I || M || L) ) + { + randomNumber[5005] = {0}, len = 0, flag = 0; + while( true ) + { + temp = ((Z*L)+I)%M; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 350 + Name: Pseudo-Random Numbers + Problem: https://onlinejudge.org/external/3/350.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int calc(int z,int i,int m,int l){ + return (z*l+i)%m;} +int main() +{ + int z,i,l,m,count,casen=1,first; + bool repeated[10000]={0}; + while (cin>>z>>i>>m>>l && (z+i+m+l)) + { + memset(repeated,0,sizeof(repeated)); + count=0; + first=l; + do repeated[l]=1,l=calc(z,i,m,l),count++; while(!repeated[l]); + cout<<"Case "< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +ll Z,I,M; + +int main() +{ + ll L,no=0; + while(cin>>Z>>I>>M>>L) + { + if(Z==0&&I==0&&M==0&&L==0) break; + ll same,c=0; + mapmp; + while(1) + { + same=((Z*L)+I)%M; + mp[same]++; + if(mp[same]==2) break; + L=same; + c++; + } + pf("Case %lld: %lld\n",++no,c); + } + return 0; +} diff --git a/uva_cpp_clean/350/350-9.cpp b/uva_cpp_clean/350/350-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5df51f29e815849b033a39a8dab80521a48d213d --- /dev/null +++ b/uva_cpp_clean/350/350-9.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +typedef pair ii; + +int tc, Z, I, M, L; + +int f(int xo){ + return ( Z * xo + I ) % M; +} + +ii FloydCycleFinding(int xo){ + int tortoise = f(xo), hare = f(f(xo)); + while( tortoise != hare ) tortoise = f(tortoise), hare = f(f(hare)); + int mu = 0; + hare = xo; + while( tortoise != hare ) tortoise = f(tortoise), hare = f(hare), mu++; + int lambda = 1; + hare = f(tortoise); + while( tortoise != hare ) hare = f(hare), lambda++; + return ii(mu, lambda); +} + +int main(){ + while(scanf("%d %d %d %d", &Z, &I, &M, &L), Z | I | M | L) + printf("Case %d: %d\n", ++tc, FloydCycleFinding(L).second); + return(0); +} diff --git a/uva_cpp_clean/352/352-11.cpp b/uva_cpp_clean/352/352-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3777c5e346cccffb783e21c8d18a87b22c6d397d --- /dev/null +++ b/uva_cpp_clean/352/352-11.cpp @@ -0,0 +1,103 @@ +#include +/* +Problem: 352 - The Seasonal War +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=288 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int N, img_number = 0, no_wars = 0; +bool pixels[25][25] = {}, visited[25][25] = {}; +char cell; + +void dfs(int i, int j){ + if (i >= N || j >= N || j < 0 || i < 0) return; + if (visited[i][j] || !pixels[i][j]) return; + + visited[i][j] = 1; + // (i-1, j-1) (i-1, j) (i-1, j+1) + // (i, j-1) (i,j) (i, j+1) + // (i+1, j-1) (i+1, j) (i+1, j+1) + + dfs(i, j-1); // LEFT + dfs(i, j+1); // RIGHT + + dfs(i-1, j); // UP + dfs(i-1, j+1); // TOP RIGHT + dfs(i-1, j-1); // TOP LEFT + + dfs(i+1, j); // DOWN + dfs(i+1, j-1); // BOTTOM LEFT + dfs(i+1, j+1); // BOTTOM RIGHT +} + +int main () { + + while ( scanf("%d\n", &N) != EOF ){ + img_number++; + no_wars = 0; + + for (int i = 0; i < N; i++){ + for (int j = 0; j < N; j++){ + scanf("%c", &cell); + pixels[i][j] = (bool)(cell-'0'); + + visited[i][j] = 0; // initialization + } + scanf("%c", &cell); // for '\n' at the end; + } + + for (int i = 0; i < N; i++){ + for (int j = 0; j < N; j++){ + if (visited[i][j] || !pixels[i][j]) continue; + + dfs(i, j); + no_wars++; + } + } + + printf("Image number %d contains %d war eagles.\n", img_number, no_wars); + } + + return 0; +} + +/* +Sample input:- +----------------- +6 +100100 +001010 +000000 +110000 +111000 +010100 +8 +01100101 +01000001 +00011000 +00000010 +11000011 +10100010 +10000001 +01100000 + +Sample output:- +----------------- +Image number 1 contains 3 war eagles. +Image number 2 contains 6 war eagles. + +Resources:- +------------- +Video Solution (Arabic) [C++] [Eng Mohamed Nasser]: https://www.youtube.com/watch?v=-nRiMjHEIUg + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/352/352-14.cpp b/uva_cpp_clean/352/352-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27e8cb0c56d52d1faeae6a296aed1af90f654d39 --- /dev/null +++ b/uva_cpp_clean/352/352-14.cpp @@ -0,0 +1,100 @@ +/*************************************************** + * Problem name : 352 - The Seasonal War.cpp + * Problem Link : https://uva.onlinejudge.org/external/3/352.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-11-08 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int fx[] = {1, -1, 0, 0, 1, -1, -1, 1}; +int fy[] = {0, 0, 1, -1, 1, -1, 1, -1}; +int color[MAX][MAX]; +int n; +string str[MAX]; +void dfs(int sx, int sy) { + color[sx][sy] = 1; + for (int i = 0; i < 8; i++) { + int xx = sx + fx[i]; + int yy = sy + fy[i]; + if (xx >= 0 && yy >= 0 && xx < n && yy < n && str[xx][yy] == '1' && color[xx][yy] == 0) { + dfs(xx, yy); + } + } + color[sx][sy] = 2; +} +void mem() { + for (int i = 0; i < MAX; i++) { + for (int j = 0; j < MAX; j++) { + color[i][j] = 0; + } + } +} +int main () { + int t = 1; + while (scanf("%d", &n) == 1) { + for (int i = 0; i < n; i++) { + cin >> str[i]; + } + mem(); + int count = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (color[i][j] == 0 && str[i][j] == '1') { + count++; + dfs(i, j); + } + } + } + printf("Image number %d contains %d war eagles.\n", t++, count); + } + return 0; +} + + diff --git a/uva_cpp_clean/352/352-16.cpp b/uva_cpp_clean/352/352-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c029edd78aec545db0ed7418054981d07f6c7c1e --- /dev/null +++ b/uva_cpp_clean/352/352-16.cpp @@ -0,0 +1,66 @@ +//============================================================================ +// Name : 352 - The Seasonal War.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 352 The Seasonal War in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include +#include + +using namespace std; + +#define FRI(i, a, b) for(i = a; i < (int)b; ++i) + +int main() { + int i, j, k, x, y; + int n, answer, number = 0; + stack< pair > group; + pair current; + int di[] = {-1, -1, -1, 0, 0, 1, 1, 1}; + int dj[] = {-1, 0, 1, -1, 1, -1, 0, 1}; + + while (cin >> n) { + bool image[n][n]; + string row[n]; + answer = 0; + + memset(image, false, sizeof image); + + FRI(i, 0, n) { + cin >> row[i]; + } + + FRI(i, 0, n) { + FRI(j, 0, n) { + if (row[i][j] != '0' && !image[i][j]) { + image[i][j] = true; + + group = stack< pair >(); + group.push(make_pair(i, j)); + + while (!group.empty()) { + current = group.top(); + group.pop(); + + FRI (k, 0, 8) { + x = current.second + dj[k]; + y = current.first + di[k]; + + if (x >= 0 && x < n && y >= 0 && y < n && row[y][x] != '0' && !image[y][x]) { + image[y][x] = true; + group.push(make_pair(y, x)); + } + } + } + answer++; + } + } + } + printf("Image number %d contains %d war eagles.\n",++number, answer); + } + + return 0; +} diff --git a/uva_cpp_clean/352/352-18.cpp b/uva_cpp_clean/352/352-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..81b9db7ce5b8d0f13d80ef03a273f7020b37f716 --- /dev/null +++ b/uva_cpp_clean/352/352-18.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +bool visited[26][26]; +int posI[]={1, 1, 1, 0, 0, -1, -1, -1}; +int posJ[]={1, 0, -1, 1, -1, 1, 0, -1}; +int n, tc = 0; +vector warzone; +string input; + +void DFS(int i, int j){ + visited[i][j] = true; + for(int k = 0; k < 8; k++){ + int newI = i + posI[k]; + int newJ = j + posJ[k]; + if(newI >= 0 && newI < n && newJ >= 0 && newJ < n && !visited[newI][newJ] && warzone[newI][newJ] == '1') DFS(newI, newJ); + } +} + +int main(){ + int cont = 0; + while(scanf("%d", &n) != EOF){ + tc++; + warzone.clear(); + memset(visited, false, sizeof(visited)); + for(int i = 0; i < n; i++){ + cin >> input; + warzone.push_back(input); + } + cont = 0; + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++) if(warzone[i][j] == '1' && !visited[i][j]) DFS(i, j), cont++; + } + printf("Image number %d contains %d war eagles.\n", tc, cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/352/352-19.cpp b/uva_cpp_clean/352/352-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfc83903a6c5194abdc6d257670c27ab0a5ab838 --- /dev/null +++ b/uva_cpp_clean/352/352-19.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +char images[25][25], mask[25][25]; +int N; + +int proc(int i, int j) { + if (i < N && j < N && i >= 0 && j >= 0 && !mask[i][j]) { + mask[i][j] = 1; + if (images[i][j] == '1') { + for (int m = -1; m <= 1; m++) + for (int n = -1; n <= 1; n++) + proc(i+m, j+n); + } + return images[i][j] - '0'; + } + return 0; +} + +int main() { + int cases = 1, count; + while (cin >> N) { + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + cin >> images[i][j], mask[i][j] = 0; + count = 0; + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + count += proc(i, j); + printf("Image number %d contains %d war eagles.\n", cases++, count); + } + return 0; +} diff --git a/uva_cpp_clean/352/352-21.cpp b/uva_cpp_clean/352/352-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..155f9aed899525a0202c3d9ece18211d6aed262b --- /dev/null +++ b/uva_cpp_clean/352/352-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 352 + Name: The Seasonal War + Problem: https://onlinejudge.org/external/3/352.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int n; +bool m[30][30]; +void fill(int i, int j) { + if (i<0 || i>=n || j<0 || j>=n || !m[i][j]) return; + m[i][j] = 0; + for (int a=-1; a<2; a++) + for (int b=-1; b<2; b++) + if (a || b) + fill(i+a, j+b); +} + + +int main(){ + int cse=0; + while(cin>>n) { + for (int i=0; i +using namespace std; +using vs=vector; +using vb=vector; +using vvb=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int n; + for(int t=1;cin>>n;t++){ + vs a(n); + for(int i=0;i>a[i]; + vvb s(n,vb(n)); + functiondfs=[&](int y,int x){ + s[y][x]=1; + if(x>0&&!s[y][x-1]&&a[y][x-1]=='1')dfs(y,x-1); + if(y>0&&x>0&&!s[y-1][x-1]&&a[y-1][x-1]=='1')dfs(y-1,x-1); + if(y>0&&!s[y-1][x]&&a[y-1][x]=='1')dfs(y-1,x); + if(y>0&&x0&&!s[y+1][x-1]&&a[y+1][x-1]=='1')dfs(y+1,x-1); + }; + int k=0; + for(int i=0;i + +#define SIZE 30 + +using namespace std; + +int tc, N, CC; +char grid[SIZE][SIZE]; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < N ); +} + +void floodfill(int r, int c){ + if( not is_possible(r, c) ) return; + if( grid[r][c] == '0' ) return; + grid[r][c] = '0'; + for(int d = 0; d < 8; d++) + floodfill(r + dr[d], c + dc[d]); +} + +int main(){ + while(~scanf("%d\n", &N)){ + CC = 0; + for(int row = 0; row < N; row++) scanf("%s", grid[row]); + for(int row = 0; row < N; row++) + for(int col = 0; col < N; col++) + if( grid[row][col] == '1' ) + floodfill(row, col), CC++; + printf("Image number %d contains %d war eagles.\n", ++tc, CC); + } + return(0); +} diff --git a/uva_cpp_clean/353/353-18.cpp b/uva_cpp_clean/353/353-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..db428c98d85e2c229bf51cff441724defb4f3c37 --- /dev/null +++ b/uva_cpp_clean/353/353-18.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +bool isPalindrome(string palindrome){ + for(int i = 0; i < palindrome.size(); i++){ + if(palindrome[i] != palindrome[palindrome.size() - 1 - i]) return false; + } + return true; +} + +int main(){ + string input; + int cont; + while(getline(cin, input)){ + cont = 0; + map usedPal; + for(int i = 0; i < input.size(); i++){ + string palindrome = ""; + for(int j = i; j < input.size(); j++){ + palindrome+=input[j]; + if(isPalindrome(palindrome) && usedPal.find(palindrome) == usedPal.end()){ + cont++; + usedPal[palindrome]+=1; + } + } + } + cout << "The string '" << input << "' contains " << cont << " palindromes.\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/353/353-19.cpp b/uva_cpp_clean/353/353-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96d1730580fbbe33378097db2ae9c5f7755f4052 --- /dev/null +++ b/uva_cpp_clean/353/353-19.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +int mat[90][90]; // -1 not processed, 1 palindrome, 0 otherwise +unordered_set pal; +string s; + +int check(int i, int j) { + if (j < i) return 0; + if (mat[i][j] == -1) { + if (i == j) { + mat[i][j] = 1; + pal.insert(s.substr(i, 1)); + } else { + mat[i][j] = 0; + check(i, j-1); + check(i+1, j); + if (s[i] == s[j] && (j - i == 1 || check(i+1, j-1))) { + mat[i][j] = 1; + pal.insert(s.substr(i, j - i + 1)); + } + } + } return mat[i][j]; +} + +int main() { + while (cin >> s) { + pal.clear(); + for (int i = 0; i < s.length(); i++) + for (int j = i; j < s.length(); j++) + mat[i][j] = -1; + check(0, s.length()-1); + printf("The string '%s' contains %lu palindromes.\n", s.c_str(), pal.size()); + } + return 0; +} diff --git a/uva_cpp_clean/353/353-21.cpp b/uva_cpp_clean/353/353-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b24088d38b5c2ade02b5260370e7e109f08abc06 --- /dev/null +++ b/uva_cpp_clean/353/353-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 353 + Name: Pesky Palindromes + Problem: https://onlinejudge.org/external/3/353.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +char word[100]; +bool isp(int s, int e) { + for (int i=s, j=e; i>word) { + set s; + int l = strlen(word); + for (int i=0; i +#include +#include +#include +#include + +bool is_palindrom(std::string &s, int start, unsigned int length) +{ + if (start < 0) + { + return false; + } + + if (length > s.length()) + { + return false; + } + + for (int i = start, j = start + length - 1; i < j; i++, j--) + { + if (s[i] != s[j]) + { + return false; + } + } + + return true; +} + +int main() +{ + std::string input; + while (std::cin >> input) + { + std::unordered_set palindromes; + std::queue> queue; + + for (unsigned int i = 0; i < input.length(); i++) + { + queue.push(std::make_pair(i, 1)); + } + + while (!queue.empty()) + { + auto pair = queue.front(); + queue.pop(); + + std::string palindrom(pair.second, '\0'); + memcpy(&palindrom[0], &input[pair.first], pair.second); + palindromes.insert(palindrom); + + if (is_palindrom(input, pair.first - 1, pair.second + 1)) + { + queue.push(std::make_pair(pair.first - 1, pair.second + 1)); + } + + if (is_palindrom(input, pair.first, pair.second + 1)) + { + queue.push(std::make_pair(pair.first, pair.second + 1)); + } + + if (is_palindrom(input, pair.first - 1, pair.second + 2)) + { + queue.push(std::make_pair(pair.first - 1, pair.second + 2)); + } + } + + std::cout << "The string '" << input << "' contains " << palindromes.size() << " palindromes." << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/353/353-5.cpp b/uva_cpp_clean/353/353-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..de3843db9c2a7b12cd67b54f8d7fb9ff8e8ff93a --- /dev/null +++ b/uva_cpp_clean/353/353-5.cpp @@ -0,0 +1,89 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define S(s) s.size() +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +string pal,a; +int c=0; +int pl(string s,int len) +{ + for(int i=0,j=len-1;i>s) + { + int len=s.size(); + int l; + int r=0,c=0; + mapmp; + for(int i=0;i=1) + continue; + mp[pal]++; + + l=pal.size(); + if(pl(pal,l)) + c++; + } + } + cout<<"The string '"< + +using namespace std; + +string s; +int ans; +set vis; + +void check(string x){ + if( vis.find(x) == vis.end() ){ + for(int i = 0, j = x.size() - 1; i < j; i++, j--) + if(x[i] != x[j]) return; + ans++; + vis.insert(x); + } +} + +int main(){ + while(cin >> s){ + vis.clear(); + ans = 0; + for(int i = 0; i < s.size(); i++) + for(int j = 0; j < s.size() - i; j++) + check(s.substr(i, j + 1)); + printf("The string '%s' contains %d palindromes.\n", s.c_str(), ans); + } + return(0); +} diff --git a/uva_cpp_clean/355/355-21.cpp b/uva_cpp_clean/355/355-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b33122189d2cb07ede15884d7539e718165f38bc --- /dev/null +++ b/uva_cpp_clean/355/355-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 355 + Name: The Bases Are Loaded + Problem: https://onlinejudge.org/external/3/355.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int val(char ch) { + return ch>='A' ? ch-'A'+10 : ch-'0'; +} + +int main(){ + long long s, t, res[200]; + char num[200]; + + while (cin>>s>>t>>num) { + bool e = false; + long long x = 0; + for (int i=0; !e && num[i]; i++) { + int d = val(num[i]); + if (d>=s) e = true; + else x = x*s + d; + } + if (e) printf("%s is an illegal base %lld number\n", num, s); + else { + printf("%s base %lld = ", num, s); + if (t==10 || !x) cout << x; + else { + int i; + for (i=0; x; i++) { + res[i] = x%t; + x /= t; + } + + while (i--) + putchar(res[i]>=10 ? 'A'+res[i]-10 : '0'+res[i]); + } + printf(" base %lld\n", t); + } + } +} diff --git a/uva_cpp_clean/356/356-21.cpp b/uva_cpp_clean/356/356-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba2d9e7995bc23cfa89e65d42cc6fc35d0e4baf4 --- /dev/null +++ b/uva_cpp_clean/356/356-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 356 + Name: Square Pegs And Round Holes + Problem: https://onlinejudge.org/external/3/356.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + bool frst = 1; + while (cin >> n) { + if (frst) frst = 0; + else printf("\n"); + + int ic = 0, + oc = 0; + double n2 = (2*n-1)/2.0; n2 *= n2; + for (int i=0; i +#include +#include + +using namespace std; + +#define MAX 30002 + +long long dp[MAX]; +long long coins[5] = { 1, 5, 10, 25, 50 }; + +int main() +{ + ios::sync_with_stdio(false); + + long long n; + + while (cin >> n) + { + memset(dp, 0, sizeof dp); + + dp[0] = 1; + + for (int i = 0; i <= 4; i++) + { + for (int j = 0; j < MAX; j++) + { + if (j - coins[i] >= 0) + { + dp[j] += dp[j - coins[i]]; + } + } + } + + long long answer = dp[n]; + + if (answer == 1 || answer == 0) + { + cout << "There is only 1 way to produce " << n << " cents change." << endl; + } + + else + { + cout << "There are " << answer << " ways to produce " << n << " cents change." << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/357/357-14.cpp b/uva_cpp_clean/357/357-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a76a3681ef9eadda8ef05fad4e314ff894fb7a7d --- /dev/null +++ b/uva_cpp_clean/357/357-14.cpp @@ -0,0 +1,81 @@ +/*************************************************** + * Problem name : 357 Let Me Count The Ways.cpp + * Problem Link : https://uva.onlinejudge.org/external/3/357.pdf + * OJ : Uva + * Verdict : AC + * Date : 09.06.2017 + * Problem Type : DP + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 30005 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +//////////////////////////////////////////////////////////////////////////////////////////////////// +int coin[] = {1, 5,10, 25, 50}; +ll dp[6][MAX],make; +ll coinChange(ll i, ll amount){ + if(i>=5){ + if(amount == 0) return 1; + else return 0; + } + if(dp[i][amount] != -1) return dp[i][amount]; + ll rel1 = 0, rel2 = 0; + if(amount - coin[i]>=0) rel1 = coinChange(i, amount - coin[i]); + rel2 = coinChange(i + 1, amount); + + return dp[i][amount] = rel1 + rel2; +} +int main () { + SET(dp); + while(scanf("%lld", &make) == 1){ + ll ans = coinChange(0,make); + if(ans == 1){ + printf("There is only 1 way to produce %lld cents change.\n",make); + } + else{ + printf("There are %lld ways to produce %lld cents change.\n", ans, make); + } + } + return 0; +} + diff --git a/uva_cpp_clean/357/357-19.cpp b/uva_cpp_clean/357/357-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a515cf8a0fd4f8ecec3eeffdd83271eedfa74b1 --- /dev/null +++ b/uva_cpp_clean/357/357-19.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main() { + long long ways[30003]; + for (int i = 0; i < 30003; i++) ways[i] = 1; + for (int i = 5; i < 30003; i++) ways[i] += ways[i-5]; + for (int i = 10; i < 30003; i++) ways[i] += ways[i-10]; + for (int i = 25; i < 30003; i++) ways[i] += ways[i-25]; + for (int i = 50; i < 30003; i++) ways[i] += ways[i-50]; + int n; + while (scanf("%d", &n) != EOF) { + if (ways[n] > 1) printf("There are %lld ways to produce %d cents change.\n", ways[n], n); + else printf("There is only 1 way to produce %d cents change.\n", n); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/357/357-20.cpp b/uva_cpp_clean/357/357-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93aa0a5df94e3a220e403d05f8a11a3c0fb615f7 --- /dev/null +++ b/uva_cpp_clean/357/357-20.cpp @@ -0,0 +1,24 @@ +#include + +const int SZ = 30005; +long nway[SZ]; +const int coin[] = {50, 25, 10, 5, 1}; + +int main() +{ + nway[0] = 1; + for(int i=0 ; i<5 ; i++) + { + int c = coin[i]; + for(int j=c ; j<30005 ; j++) nway[j] += nway[j-c]; + } + + int n; + + while( scanf("%d", &n)==1 ) + { + if( nway[n]==1 ) printf("There is only 1 way to produce %d cents change.\n", n); + else printf("There are %ld ways to produce %d cents change.\n", nway[n], n); + } + return 0; +} diff --git a/uva_cpp_clean/357/357-21.cpp b/uva_cpp_clean/357/357-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57dbbfdc742cc3fe5ce4f250a268ee3ea38d5639 --- /dev/null +++ b/uva_cpp_clean/357/357-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 357 + Name: Let Me Count The Ways + Problem: https://onlinejudge.org/external/3/357.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +// 1 5 10 25 50 +// a + 5b +10c +25d +50e = n +int coins[] = {1, 5, 10, 25, 50}; +long long int DP[30001][5]; +int main() { + for (int i=0; i<2; i++) + for(int j=0; j<5; j++) + DP[i][j]=1; + + for (int i=2; i<30001; i++) { + DP[i][0] = 1; + for (int j=1; j<5; j++) + for (int k=0; k*coins[j]<=i; k++) + DP[i][j] += DP[i-k*coins[j]][j-1]; + } + + int n; + while(cin>>n) + if (n<5) + printf("There is only 1 way to produce %d cents change.\n", n); + else + printf("There are %lld ways to produce %d cents change.\n", DP[n][4], n); +} diff --git a/uva_cpp_clean/357/357-5.cpp b/uva_cpp_clean/357/357-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..efbcdbfe4011dffd39f78a33ec11fc65b517151a --- /dev/null +++ b/uva_cpp_clean/357/357-5.cpp @@ -0,0 +1,113 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define CP(a) cout< subset sum > with repetition + difficulty: easy + date: 19/Mar/2020 + by: @brpapa +*/ +#include +#include +#include +#define ll long long +using namespace std; + +const int N = 5; +int A[N] = {1, 5, 10, 25, 50}; + +ll memo[5][30030]; +ll dp(int n, int s) { + // n-ésima moeda, soma S-s do subconjunto atual + + if (s == 0) return 1ll; + if (n == N || s < 0) return 0ll; + + ll &ans = memo[n][s]; + if (ans != -1) return ans; + + return ans = dp(n+1, s) + dp(n, s-A[n]); +} + +int main() { + memset(memo, -1, sizeof memo); + + int S; + while (cin >> S) { + ll ways = dp(0, S); + printf("There %s %lld %s to produce %d cents change.\n", + ways == 1? "is only":"are", ways, ways == 1? "way":"ways", S); + } + return 0; +} diff --git a/uva_cpp_clean/357/357-9.cpp b/uva_cpp_clean/357/357-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..636e771aa2aec315fe378c066388544e41fe0a3d --- /dev/null +++ b/uva_cpp_clean/357/357-9.cpp @@ -0,0 +1,26 @@ +#include + +#define MAX_N 10 +#define MAX_W 30010 + +using namespace std; + +int n,v[]={1,5,10,25,50}; +long long ans,memo[MAX_N][MAX_W]; + +long long ways(int id,int sum){ + if(sum==0) return 1; + if(sum<0 || id==5) return 0; + if(~memo[id][sum]) return memo[id][sum]; + return memo[id][sum] = ways(id+1,sum)+ways(id,sum-v[id]); +} + +int main(){ + memset(memo,-1,sizeof memo); + while(scanf("%d",&n)==1){ + ans = ways(0,n); + if(ans==1) printf("There is only 1 way to produce %d cents change.\n",n); + else printf("There are %lld ways to produce %d cents change.\n",ans,n); + } + return(0); +} diff --git a/uva_cpp_clean/361/361-19.cpp b/uva_cpp_clean/361/361-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05f4a9d6fb3f0ce2f1e3aa1cc406e1300d65bf00 --- /dev/null +++ b/uva_cpp_clean/361/361-19.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long double ld; +typedef complex point, vect; +typedef vector polygon; +typedef polygon::iterator polIter; +const ld inf = 1e9; const ld eps = 1e-9; const ld pi = acos(-1.0); + +ld area(polygon &p) { + ld result = 0.0; + for (int i = 0; i < p.size(); i++) + result += imag(conj(p[i])*p[(i+1)%p.size()]); + return abs(result) / 2.0; +} + +double dist(point p1, point p2) { + return hypot(p1.real() - p2.real(), p1.imag() - p2.imag()); +} + +double cross(vect a, vect b) { + return a.real() * b.imag() - a.imag() * b.real(); +} + +vect toVec(point a, point b) { + return vect(b.real() - a.real(), b.imag() - a.imag()); +} + +bool ccw(point p, point q, point r) { + return cross(toVec(p, q), toVec(p, r)) > 0; +} + +bool collinear(point p, point q, point r) { + return fabs(cross(toVec(p, q), toVec(p, r))) < eps; +} + +point pivot(0, 0); +bool angleCmp(point a, point b) { + if (collinear(pivot, a, b)) return dist(pivot, a) < dist(pivot, b); + double d1x = a.real() - pivot.real(), d1y = a.imag() - pivot.imag(); + double d2x = b.real() - pivot.real(), d2y = b.imag() - pivot.imag(); + return (atan2(d1y, d1x) - atan2(d2y, d2x)) < 0; +} + +polygon CH(polygon P) { + int i, j, n = (int)P.size(); + if (n <= 3) { + if (!(P[0] == P[n-1])) P.push_back(P[0]); + return P; + } + int P0 = 0; + for (i = 1; i < n; i++) + if (P[i].imag() < P[P0].imag() || (P[i].imag() == P[P0].imag() && P[i].real() > P[P0].real())) + P0 = i; + point temp = P[0]; P[0] = P[P0]; P[P0] = temp; + pivot = P[0]; + sort(++P.begin(), P.end(), angleCmp); + polygon S; + S.push_back(P[n-1]); S.push_back(P[0]); S.push_back(P[1]); + i = 2; + while (i < n) { + j = (int)S.size()-1; + if (ccw(S[j-1], S[j], P[i])) S.push_back(P[i++]); + else S.pop_back(); + } + return S; +} + +ld angle(vect v, vect u) { + return arg(v/u); +} + +int inPolygon(point q, polygon &p) { + int n=p.size(); if(n==0) return -1; + ld sum=0.0, alpha; + for (int i=0; i> c >> r >> o && !(c == 0 && r == 0 && o == 0)) { + int x, y; + polygon cops, robbers; + for (int i = 0; i < c; i++) { + cin >> x >> y; + cops.push_back(point(x, y)); + } + for (int i = 0; i < r; i++) { + cin >> x >> y; + robbers.push_back(point(x, y)); + } + polygon chcops = c > 2? CH(cops): empty; + polygon chrobbers = r > 2? CH(robbers): empty; + printf("Data set %d:\n", t++); + for (int i = 0; i < o; i++) { + cin >> x >> y; + point p(x, y); + if (inPolygon(p, chcops) != -1) { + printf(" Citizen at (%d,%d) is safe.\n", x, y); + } else if (inPolygon(p, chrobbers) != -1) { + printf(" Citizen at (%d,%d) is robbed.\n", x, y); + } else printf(" Citizen at (%d,%d) is neither.\n", x, y); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/362/362-4.cpp b/uva_cpp_clean/362/362-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc57eb1bac4deaf931b5fd60173f79df9249e1d6 --- /dev/null +++ b/uva_cpp_clean/362/362-4.cpp @@ -0,0 +1,42 @@ +#include + +int main() +{ + int size, set = 0; + + while (std::cin >> size && size) + { + std::cout << "Output for data set " << ++set << ", " << size << " bytes:" << std::endl; + + int downloaded = 0, speed = 0, time = 0; + + while (downloaded != size) + { + int chunk; + std::cin >> chunk; + + downloaded += chunk; + speed += chunk; + time++; + + if (time % 5 == 0) + { + if (speed != 0) + { + std::cout << " Time remaining: " << ((5 * (size - downloaded)) + speed - 1) / speed << " seconds" << std::endl; + } + else + { + std::cout << " Time remaining: stalled" << std::endl; + } + + speed = 0; + } + } + + std::cout << "Total time: " << time << " seconds" << std::endl + << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/369/369-10.cpp b/uva_cpp_clean/369/369-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..246422e834261c5f7f88a68a7e23238dffc079ae --- /dev/null +++ b/uva_cpp_clean/369/369-10.cpp @@ -0,0 +1,25 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) +#define ll long long int +using namespace std; + +int main() +{//Sakr_; + + double n,m,ans=1,y; + int x=n-m; + + while(cin>>n>>m) { + int x=n-m; + y=n; + for(;x>=1;x--) { + ans*=(y/x); + y--; + } + + if(n!=0 && m!=0) + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 105 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +ll C[MAX + 1][MAX + 1]; +ll ncr(ll n, ll r) { + if (C[n][r] == -1) + C[n][r] = ncr(n - 1, r) + ncr(n - 1, r - 1); + return C[n][r]; +} +ll get_ncr(ll n, ll r) { + int i, j; + for (i = 0; i <= n; i++) { + for (j = 0; j <= n; j++) { + C[i][j] = -1; + } + } + + for (i = 1; i <= n; i++) { + C[i][0] = 1; + C[i][i] = 1; + } + return ncr(n, r); +} +int main () { + //~ __FastIO; + ll n, m; + while (scanf("%lld %lld", &n, &m) == 2 && n != 0 && m != 0) { + printf("%lld things taken %lld at a time is %lld exactly.\n", n, m, get_ncr(n, m)); + } + return 0; +} diff --git a/uva_cpp_clean/369/369-16.cpp b/uva_cpp_clean/369/369-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c43eb03989eb6bf658bddc7b2208019040f56f2c --- /dev/null +++ b/uva_cpp_clean/369/369-16.cpp @@ -0,0 +1,28 @@ +//============================================================================ +// Name : 369 - Combinations.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 369 Combinations in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include + +using namespace std; + +int main() { + int i, j; + int n, m; + long long int c; + + while (cin >> n >> m, n != 0 && m != 0) { + c = 1; + for (i = m + 1, j = 1; i <= n; ++i, ++j) { + c = c * i / j; + } + + printf("%d things taken %d at a time is %lld exactly.\n", n, m, c); + } + + return 0; +} diff --git a/uva_cpp_clean/369/369-19.cpp b/uva_cpp_clean/369/369-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..968bcfd6cf91ef77a0e220f007b809f30f88cc15 --- /dev/null +++ b/uva_cpp_clean/369/369-19.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +unsigned int comb[101][101]; + +unsigned int proc(int n, int m) { + if(comb[n][m] == 0) { + comb[n][m] = n == m || m == 0? 1: proc(n-1, m-1) + proc(n-1, m); + } + return comb[n][m]; +} + +int main() { + int n, m; + for (int i = 0; i < 101; i++) + for (int j = 0; j < 101; j++) comb[i][j] = 0; + while (scanf("%d %d", &n, &m) && !(n == 0 && m == 0)) { + unsigned int c = proc(n, m); + printf("%d things taken %d at a time is %d exactly.\n", n, m, c); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/369/369-5.cpp b/uva_cpp_clean/369/369-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..559ac54b52eaa8b321e78d36c1d2edef6c2e55d9 --- /dev/null +++ b/uva_cpp_clean/369/369-5.cpp @@ -0,0 +1,24 @@ +#include + +long long nCr(long long n,long long m) +{ +long long res=1,i; +if(n-m + +using namespace std; + +const int SIZE = 105; + +int n, m; +long long C[SIZE][SIZE]; + +void BinomialCoef(){ + for(int i = 0; i < SIZE; i++) C[i][0] = C[i][i] = 1; + for(int i = 2; i < SIZE; i++) + for(int k = 1; k < i; k++) + C[i][k] = C[i - 1][k] + C[i - 1][k - 1]; +} + +int main(){ + BinomialCoef(); + while(scanf("%d %d", &n, &m), n | m) + printf("%d things taken %d at a time is %lld exactly.\n", n, m, C[n][m]); + return(0); +} diff --git a/uva_cpp_clean/371/371-14.cpp b/uva_cpp_clean/371/371-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2fa17998b302ba54356c122a415ac9c429bae59 --- /dev/null +++ b/uva_cpp_clean/371/371-14.cpp @@ -0,0 +1,87 @@ +/*************************************************** + * Problem name : 371 Ackermann Functions.cpp + * Problem Link : https://uva.onlinejudge.org/external/3/371.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-04 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ll L, R; + while (scanf("%lld %lld", &L, &R) == 2 && L != 0 && R != 0) { + ll ans = 0, mx = 0; + for (ll i = min(L, R); i <= max(L, R); i++) { + //~ debug; + ll X = i; + ll cnt = 1; + if (X % 2 == 0) { + X /= 2; + } else { + X = 3 * X + 1; + } + while (X != 1) { + if (X % 2 == 0) { + X /= 2; + cnt++; + } else { + X = 3 * X + 1; + cnt++; + } + } + if (cnt > mx) { + mx = cnt; + ans = i; + } + } + printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n", min(L, R), max(L, R), ans, mx); + } + + return 0; +} diff --git a/uva_cpp_clean/371/371-20.cpp b/uva_cpp_clean/371/371-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97ce5de0508c7a025fe85568dfc7e6484015c5b3 --- /dev/null +++ b/uva_cpp_clean/371/371-20.cpp @@ -0,0 +1,33 @@ +#include + +#define Swap(a, b) ((a)^=(b),(b)^=(a),(a)^=(b)) + +long long ackermannFunction(long long Xn) +{ + if( (Xn&1) ) return ((3*Xn)+1); + else return (Xn>>1); +} + +int main() +{ + long long L, H; + while( scanf("%lld %lld", &L, &H)==2 && (L || H) ) + { + if( L>H ) Swap(L, H); + + long long V, S = 0; + for(long long i=L ; i<=H ; i++) + { + long long perS = 0, temp = i; + do + { + temp = ackermannFunction(temp); + perS++; + } while( temp!=1 ); + if( S>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 371 + Name: Ackermann Functions + Problem: https://onlinejudge.org/external/3/371.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MAX 10000000 +long long int memo[MAX]; +long long int alen(long long int n, int d) { + if (n == 1) return 4; + if (!d || n<0) return -1e7; + if (n < MAX && memo[n]) return memo[n]; + + long long int x = n; + int b = 0; + for (; !(x&1); x>>=1) + b++; + + + long long int r = 1; + if (x < MAX && memo[x]) + r = memo[x]; + + else if (x>1 && 3*x+1>0) { + r = alen(3*x + 1, d-1) + 1; + if (x < MAX && r>0) memo[x] = r; + } + + if (n < MAX && r>0) memo[n] = b + r; + return b + r; +} + +int main(){ + long long int l, r; + while (cin>>l>>r && (l||r)) { + long long int best = 0, besti = l; + if (r < l) { + r ^= l; + l ^= r; + r ^= l; + } + + for (long long int i=r; i>=l; i--) { + long long int al = alen(i, 800); + if (al >= best) { + best = al; + besti = i; + } + } + + printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n", l, r, besti, best - 1); + } +} + diff --git a/uva_cpp_clean/371/371-5.cpp b/uva_cpp_clean/371/371-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e9c5c54f7f5e92148f0f7684ad00ffbf9f1b2907 --- /dev/null +++ b/uva_cpp_clean/371/371-5.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() + { + long long a,b,c,Max,Gn,p,t,i; + while(scanf("%lld%lld",&a,&b)==2) + { + Max=0; + if(a==0||b==0) + break; + if(a>b) + swap(a,b); + for(i=b;i>=a;i--) + { + p=i; + c=1; + if(p%2==0) + { + p/=2; + } + else + { + p=3*p+1; + } + while(p!=1) + { + if(p%2==0) + { + p/=2; + c++; + } + else + { + p=3*p+1; + c++; + } + + } + if(c>=Max) + { + Max=c; + Gn=i; + } + } + printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n",a,b,Gn,Max); + } + return 0; + } diff --git a/uva_cpp_clean/371/371-9.cpp b/uva_cpp_clean/371/371-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10ae0fae0caca263d857c00bcd4bd8f637d91dcd --- /dev/null +++ b/uva_cpp_clean/371/371-9.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main(){ + int ct, ans, tmp; + long x, y, num; + long long n; + while(scanf("%ld %ld", &x, &y), x | y){ + ans = -1; + if(x > y) + swap(x, y); + for(long i = x; i <= y; i++){ + ct = 0; + n = i; + if(n == 1) + ct = 3; + while(n != 1){ + ct++; + if(n & 1) + n = 3 * n + 1; + else + n /= 2; + } + if(ct > ans) + ans = ct, num = i; + } + printf("Between %ld and %ld, %ld generates the longest sequence of %d values.\n", x, y, num, ans); + } + return(0); +} diff --git a/uva_cpp_clean/374/374-10.cpp b/uva_cpp_clean/374/374-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f733ba105600e036708b8736c97928fdccf27d71 --- /dev/null +++ b/uva_cpp_clean/374/374-10.cpp @@ -0,0 +1,45 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) +#define ll long long int + +using namespace std; + +/* +by:Ahmed Sakr (sakr_) with AZA ;l + +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif +*/ + +ll power(ll b,ll a,ll m) +{ + if(a==0) return 1%m; + if(a==1) return b%m; + + if(a%2==0) + { + ll x=(power(b,a/2,m))%m; + + return (x%m *x%m)%m; + } + else + { + ll x=(power(b,a/2,m))%m; + + return ((x%m * x%m)*b%m)%m; + } + + +} + + +int main() +{ + ll b,a,m; + + while(cin>>b>>a>>m) + { + cout< +using namespace std; + +typedef long long ll; + +ll b, p, m; + +inline ll multiply(ll a, ll b) +{ + return (((a % m) * (b % m)) % m); +} + +ll perfect_pow(ll a, ll b) +{ + if (b == 0) + { + return 1; + } + + else if (b == 1) + { + return a; + } + + else + { + ll temp = perfect_pow(a, b / 2); + + if (b % 2 == 0) + { + return multiply(temp, temp); + } + + else + { + return (multiply(multiply(temp, temp), a)); + } + } +} + +int main() +{ + ios::sync_with_stdio(false); + + while (cin >> b >> p >> m) + { + cout << perfect_pow(b, p) << endl; + } +} diff --git a/uva_cpp_clean/374/374-16.cpp b/uva_cpp_clean/374/374-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29861dc9d1d238ea48e99f8db400f98097ba0937 --- /dev/null +++ b/uva_cpp_clean/374/374-16.cpp @@ -0,0 +1,31 @@ +//============================================================================ +// Name : 374 - Big Mod.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 374 - Big Mod in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include + +using namespace std; + +int main() { + int b, p, m, r; + while (cin >> b >> p >> m) { + b %= m; + r = 1; + + while (p) { + if (p & 1) { + r = r * b % m; + } + b = b * b % m; + p >>= 1; + } + + cout << r << endl; + } + + return 0; +} diff --git a/uva_cpp_clean/374/374-19.cpp b/uva_cpp_clean/374/374-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2421d560539881882da76913d66526fff4a96256 --- /dev/null +++ b/uva_cpp_clean/374/374-19.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +long long power(long long b, long long e, long long mod) { + if (e == 0) return 1%mod; + if (e == 1) return b%mod; + long long aux = power(b, e/2, mod); + aux = (aux*aux)%mod; + if (e%2) aux = (aux*(b%mod))%mod; + return aux; +} + +int main() { + int b, p, m; + while (scanf("%d %d %d", &b, &p, &m) == 3) { + printf("%lld\n", power(b, p, m)); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/374/374-21.cpp b/uva_cpp_clean/374/374-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59693aff8dbe9378393897e994b49d0347d3d788 --- /dev/null +++ b/uva_cpp_clean/374/374-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 374 + Name: Big Mod + Problem: https://onlinejudge.org/external/3/374.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +int powmod(int b, int p, int m) { + if (!p) return 1; + if (p == 1) return b % m; + + int half = powmod(b, p / 2, m); + int result = (half * half) % m; + + return p % 2 ? (result * b) % m : result; +} + +int main() { + int b, p, m; + while (cin >> b >> p >> m) + cout << powmod(b % m, p, m) << endl; +} \ No newline at end of file diff --git a/uva_cpp_clean/374/374-5.cpp b/uva_cpp_clean/374/374-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..44127fbe04f8d2e75c7ef6cf4d18984d78577f17 --- /dev/null +++ b/uva_cpp_clean/374/374-5.cpp @@ -0,0 +1,136 @@ +//#include +// +//int main() +//{ +// long long t,b,i,p,m,r; +// while(scanf("%lld %lld %lld",&b,&p,&m)==3) +// { +// r=1; +// while(p) +// { +// if(p%2!=0) +// r=(r*b)%m; +// b=(b*b)%m; +// p/=2; +// } +// printf("%lld\n",r); +// } +// return 0; +//} +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +using namespace std; + +#define for0(i,n) for(int i=0;i=0;i--) +#define rfor1(i,n) for(int i=n;i>=1;i--) +#define forse(i,a,b) for(int i=a;i<=b;i++) +#define ll long long +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sdb(t) scanf("%lf",&t) +#define schar(c) scanf("%c",&c) +#define sstring(s) scanf("%s",s) +#define ssi(a,b) scanf("%d%d",&a,&b) +#define ssl(a,b) scanf("%lld%lld",&a,&b) + +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) + +#define CP(a) cout< " << (x) << endl; +#define db_sarr(i,a) cout<<#a<<"["< "< "< vi; +typedef vector vl; +typedef vector vs; +typedef pair pii; +typedef vector vpii; +typedef set si; +typedef map mii; +typedef map msi; + +////============ CONSTANT ===============//// +#define MAXLL 9223372036854775807 +#define MINLL 9223372036854775808 +#define MAXL 2147483647 +#define MINL 2147483648 +#define mx7 10000007 +#define mx6 1000006 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +template string NumberToString( T Number ){stringstream st;st << Number;return st.str();} + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} +int SOD(int n){int sum=0;for(int i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} +ll pow(ll a, ll b,ll Mod) +{ + if(b==0) return 1; + if(b==1) return a; + ll x=pow(a,b/2,Mod); + x=(x*x)%Mod; + if(b%2==1) x=(x*a)%Mod; + return x; +} +int main() +{ + ll b,p,m; + while(sl(b)) + { + ssl(p,m); + PL(pow(b,p,m)); + } + return 0; +} diff --git a/uva_cpp_clean/374/374-6.cpp b/uva_cpp_clean/374/374-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3ac4b47da4cf3abd8c4cefa48ce6f93f131cb73 --- /dev/null +++ b/uva_cpp_clean/374/374-6.cpp @@ -0,0 +1,24 @@ +/* + math > number theory > module arithmetic + difficulty: easy + date: 15/Jun/2020 + problem: compute (a^n) % m, where a <= 2^31 and n <= 2^31 + hint: use fast power with mod + by: @brpapa +*/ +#include +using namespace std; +typedef long long ll; + +ll fastPower(ll a, ll n, int m) { + if (n == 0) return 1; + if (n % 2 == 0) return fastPower((a*a)%m, n/2, m) % m; + return ((a%m) * fastPower((a*a)%m, (n-1)/2, m)) % m; +} + +int main() { + ll a, n; int m; + while (cin >> a >> n >> m) + cout << fastPower(a, n, m) << endl; + return 0; +} diff --git a/uva_cpp_clean/374/main.cpp b/uva_cpp_clean/374/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8720bd140384fde10297f5eee9ef99ba4d9c175 --- /dev/null +++ b/uva_cpp_clean/374/main.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +int powmod(long base, long exp, long modulus){ + base %= modulus; + long result = 1; + while(exp > 0){ + if (exp & 1) result = (result * base) % modulus; + base = (base * base) % modulus; + exp >>= 1; + } + return result; +} + +int main(){ +long b, p, m, res; +while(cin >> b >> p >> m){ + res = powmod(b, p, m); + cout << res << endl; +} + +return 0; +} diff --git a/uva_cpp_clean/378/378-14.cpp b/uva_cpp_clean/378/378-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17259f14fe2eb498171efef9c9b97db55c8ccf9f --- /dev/null +++ b/uva_cpp_clean/378/378-14.cpp @@ -0,0 +1,90 @@ +/*************************************************** + * Problem Name : 378 - Intersecting Lines.cpp + * Problem Link : https://uva.onlinejudge.org/external/3/378.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-07-17 + * Problem Type : Geometry + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + int tc; + cin >> tc; + cout << "INTERSECTING LINES OUTPUT\n"; + + for (int t = 1; t <= tc; t++) { + int x1, y1, x2, y2, x3, y3, x4, y4; + cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; + int a = - (y1 - y2); + int b = x1 - x2; + int c = (x1 * y2) - (x2 * y1); + int aa = - (y3 - y4); + int bb = x3 - x4; + int cc = (x3 * y4) - (x4 * y3); + double ans = (a * bb) - (b * aa); + + if (ans != 0) { + double x = (double) (c * bb - b * cc) / ans; + double y = (double) (a * cc - c * aa) / ans; + cout << "POINT " << fixed << setprecision (2) << x << " " << y << "\n"; + + } else if (a * x3 + b * y3 - c == 0) { + cout << "LINE\n"; + + } else { + cout << "NONE\n"; + } + } + + cout << "END OF OUTPUT\n"; + return 0; +} diff --git a/uva_cpp_clean/378/378-15.cpp b/uva_cpp_clean/378/378-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9838091af9e64a658e08f22506f506f419044694 --- /dev/null +++ b/uva_cpp_clean/378/378-15.cpp @@ -0,0 +1,56 @@ +/* + 求兩直線交點 + 時間複雜度: O(1) +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + cout << "INTERSECTING LINES OUTPUT\n"; + + int n_testcase; + int x1, x2, x3, x4, y1, y2, y3, y4; + int a1, a2, b1, b2, c1, c2; + int delta, delta_x, delta_y; + + cin >> n_testcase; + while (n_testcase--) { + cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; + // 換成向量(a1, b1) (a2, b2) + a1 = x2 - x1; + b1 = y2 - y1; + a2 = x4 - x3; + b2 = y4 - y3; + + // 法向量求直線 + // b1 * x - a1 * y = c1 + // b2 * x - a2 * y = c2 + c1 = b1 * x1 - a1 * y1; + c2 = b2 * x3 - a2 * y3; + + // 克拉瑪公式解 + // __ __ + // | b1 -a1 | c1 | + // | b2 -a2 | c2 | + // -- -- + delta = -b1 * a2 + a1 * b2; + delta_x = -c1 * a2 + c2 * a1; + delta_y = b1 * c2 - b2 * c1; + + if (delta) + cout << "POINT " << setprecision(2) << fixed << double(delta_x) / delta << " " << setprecision(2) << fixed << double(delta_y) / delta << "\n"; + else { + if (delta_x || delta_y) + cout << "NONE\n"; // 無實數解 + else + cout << "LINE\n"; // 無限多解 + } + } + cout << "END OF OUTPUT\n"; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/378/378-19.cpp b/uva_cpp_clean/378/378-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59f029997aa8219975f32061473814941df914b1 --- /dev/null +++ b/uva_cpp_clean/378/378-19.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef long double ld; +typedef vector vi; + +const ld inf=1e9; +const ld eps=1e-9; +const ld pi=acos(-1.0); + +typedef complex point, vect; +struct line { + ld a; ld b; + ld c; +}; + +line pointsToLine(point a, point b) { + line result; + result.a=b.imag()-a.imag(); + result.b=a.real()-b.real(); + result.c=a.imag()*b.real()-a.real()*b.imag(); + return result; +} + +point intersection(line r, line s) { + ld den=r.b*s.a-r.a*s.b; + point result( (r.c*s.b-r.b*s.c)/den , (r.a*s.c-r.c*s.a)/den ); + return result; +} + +double cross(vect a, vect b) { + return a.real() * b.imag() - a.imag() * b.real(); +} + +vect toVec(point a, point b) { + return vect(b.real() - a.real(), b.imag() - a.imag()); +} + +int main() { + printf("INTERSECTING LINES OUTPUT\n"); + int N; + cin >> N; + line lx = pointsToLine(point(0, 0), point(0, 1)); + while (N--) { + ld x1, y1, x2, y2, x3, y3, x4, y4; + cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; + point p1(x1, y1), p2(x2, y2), p3(x3, y3), p4(x4, y4); + line l1 = pointsToLine(p1, p2); + line l2 = pointsToLine(p3, p4); + if (abs(l1.a*l2.b - l1.b*l2.a) < eps) { + if (abs(cross(toVec(p1, p2), toVec(p1, p3))) < eps) printf("LINE\n"); + else printf("NONE\n"); + } else { + point p = intersection(l1, l2); + ld x = p.real(); + ld y = p.imag(); + if (abs(x) < eps) x = 0; + if (abs(y) < eps) y = 0; + printf("POINT %.2Lf %.2Lf\n", x, y); + } + } + printf("END OF OUTPUT\n"); + return 0; +} diff --git a/uva_cpp_clean/378/378-21.cpp b/uva_cpp_clean/378/378-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90e33fa1a9709296d207c1c2912123d84e4153b3 --- /dev/null +++ b/uva_cpp_clean/378/378-21.cpp @@ -0,0 +1,94 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 378 + Name: Intersecting Lines + Problem: https://onlinejudge.org/external/3/378.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 1e-8 +struct Point { + double x, y; + Point():x(0),y(0){} + Point(double x, double y):x(x),y(y){} +}; +std::istream& operator>> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} +std::ostream& operator<< (std::ostream& stream, const Point p) { + stream << p.x << ' ' << p.y; + return stream; +} + + +struct Line { + Point u, v; + Line(){} + Line(const Point u, const Point v):u(u),v(v){} + bool isVertical() const { return fabs(u.x-v.x) < EPS; } + double slope() const { return (u.y-v.y) / (u.x-v.x); } + double f(double x) const { return slope() * (x - u.x) + u.y; } +}; +std::istream& operator>> (std::istream& stream, Line& l) { + stream >> l.u >> l.v; + return stream; +} + +double dist2(const Point p1, const Point p2) { + double xx = p1.x - p2.x, yy = p1.y - p2.y; + return xx*xx + yy*yy; +} +double dist(const Point p1, const Point p2) { return sqrt(dist(p1, p2)); } +// double dist(const Point p, const Line l) {} + +bool intersect(const Line l, const Point p) { + return l.isVertical() ? p.x == l.u.x + : fabs(l.f(p.x) - p.y) < EPS; +} + +int intersect(const Line l1, const Line l2, Point &r) { + double + x12 = l1.u.x - l1.v.x, + y12 = l1.u.y - l1.v.y, + x34 = l2.u.x - l2.v.x, + y34 = l2.u.y - l2.v.y, + den = x12*y34 - y12*x34; + + if (fabs(den) < EPS) + return intersect(l1, l2.u) ? 2 : 0; + + double + de1 = l1.u.x*l1.v.y - l1.u.y*l1.v.x, + de2 = l2.u.x*l2.v.y - l2.u.y*l2.v.x; + + r.x = (de1*x34 - x12*de2) / den; + r.y = (de1*y34 - y12*de2) / den; + return 1; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(2); + + cout << "INTERSECTING LINES OUTPUT\n"; + int T; cin >> T; + Line l1, l2; + Point p; + while (T--) { + cin >> l1 >> l2; + int r = intersect(l1, l2, p); + if (!r) cout << "NONE\n"; + else if (r>1) cout << "LINE\n"; + else cout << "POINT " << p << '\n'; + } + cout << "END OF OUTPUT\n"; +} diff --git a/uva_cpp_clean/380/380-21.cpp b/uva_cpp_clean/380/380-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16e5a35d3404ae67a0ff9ebf154add664c1579fb --- /dev/null +++ b/uva_cpp_clean/380/380-21.cpp @@ -0,0 +1,78 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 380 + Name: Call Forwarding + Problem: https://onlinejudge.org/external/3/380.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +struct TimeDuration { + int id, tm, du; + TimeDuration(int id, int tm, int du): id(id), tm(tm), du(du) {} +}; + +list adj[10000]; + +bool used[10000]; +list usedNums; +void setUsed(int u) { + if (!used[u]) { + used[u] = 1; + usedNums.push_back(u); + } +} + + +bool inside[10000]; +int forwardsTo(int u, int t) { + if (inside[u]) return 9999; + inside[u] = 1; + for (TimeDuration tu: adj[u]) + if (t>=tu.tm && t<=tu.tm+tu.du) { + int result = forwardsTo(tu.id, t); + inside[u] = 0; + return result; + } + + inside[u] = 0; + return u; +} + + + + +int main(){ + int T, u, v, tm, du; + scanf("%d", &T); + puts("CALL FORWARDING OUTPUT"); + for (int cse=1; cse<=T; cse++) { + printf("SYSTEM %d\n", cse); + for (int x: usedNums) { + adj[x].clear(); + used[x] = 0; + } + + while (scanf("%d", &u)==1 && u) { + scanf("%d %d %d", &tm, &du, &v); + adj[u].push_back(TimeDuration(v, tm, du)); + setUsed(u); setUsed(v); + } + + while (scanf("%d", &tm)==1 && tm!=9000) { + scanf("%d", &u); + printf("AT %04d CALL TO %04d RINGS %04d\n", tm, u, forwardsTo(u, tm)); + } + + } + puts("END OF OUTPUT"); +} diff --git a/uva_cpp_clean/380/380-9.cpp b/uva_cpp_clean/380/380-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ec8b9c3539921a1d91e404c3aad8aa002b0524e --- /dev/null +++ b/uva_cpp_clean/380/380-9.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +struct callings{ + int n1, start, end, n2; +} v[500]; + +int tc, t, n, c1, c2; +vector x; + +int rings(int call, int time){ + x.push_back(call); + for(int i = 0; i < x.size(); i++) + for(int j = i + 1; j < x.size(); j++) + if(x[i] == x[j]) + return 9999; + int p = -1; + for(int i = 0; i < n; i++) + if(call == v[i].n1 && v[i].start <= time && time <= v[i].end) + p = i; + if(p == -1) + return call; + rings(v[p].n2, time); +} + +int main(){ + scanf("%d", &tc); + printf("CALL FORWARDING OUTPUT\n"); + while(tc--){ + printf("SYSTEM %d\n", ++t); + n = -1; + while(scanf("%d", &v[++n].n1), v[n].n1){ + scanf("%d %d %d", &v[n].start, &v[n].end, &v[n].n2); + v[n].end += v[n].start; + } + while(scanf("%d", &c1), c1!=9000){ + x.clear(); + scanf("%d", &c2); + printf("AT %.4d CALL TO %.4d RINGS %.4d\n", c1, c2, rings(c2, c1)); + } + } + printf("END OF OUTPUT\n"); + return(0); +} diff --git a/uva_cpp_clean/382/382-11.cpp b/uva_cpp_clean/382/382-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55e992700ee70fb08bf7ca5a80699e1d45fe24df --- /dev/null +++ b/uva_cpp_clean/382/382-11.cpp @@ -0,0 +1,66 @@ +#include +/* +Problem: 382 - Perfection +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=318 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N, sum; + +int main () { + + printf("PERFECTION OUTPUT\n"); + while ( scanf("%d ", &N) && N ){ + + sum = 0; + + for (int i = 1; i*i <= N; ++i) + if (N%i == 0) sum += i + ((i == N/i)? 0 : N/i); + + sum -= N; + + printf("%5d ", N); + if (sum == N) printf("PERFECT\n"); + else if (sum > N) printf("ABUNDANT\n"); + else printf("DEFICIENT\n"); + + } + + printf("END OF OUTPUT\n"); + + return 0; +} +/* +Sample input:- +----------------- +15 28 6 56 60000 22 496 0 + +Sample output:- +----------------- +PERFECTION OUTPUT +15 DEFICIENT +28 PERFECT +6 PERFECT +56 ABUNDANT +60000 ABUNDANT +22 DEFICIENT +496 PERFECT +END OF OUTPUT + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/382/382-15.cpp b/uva_cpp_clean/382/382-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c52e7b0e31356e9eef67d35044f46fc7703b0f28 --- /dev/null +++ b/uva_cpp_clean/382/382-15.cpp @@ -0,0 +1,50 @@ +/* + 盈數、虧數、完全數 + 時間複雜度: O(logN) (在輸入是 N = 2^M 次時最多要除 logN = M 次) +*/ +#include +#include + +using namespace std; +// 建好根號60000以內質數表 +int primes[53] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241}; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, k, t, factor_sum, count, temp; + cout << "PERFECTION OUTPUT\n"; + + while (cin >> n && n) { + factor_sum = 1; + t = 0; + k = n; + // 因數和公式 + while (k != 1 && t < 53) { + count = temp = 1; + while (k % primes[t] == 0) { + k /= primes[t]; + temp *= primes[t]; + count += temp; + } + factor_sum *= count; + t++; + } + // 多出來的要乘回去 + if (k != 1) + factor_sum *= (k + 1); + + factor_sum -= n; // 自己不算 + + if (factor_sum > n) + cout << setw(5) << n << " ABUNDANT\n"; + else if (factor_sum < n) + cout << setw(5) << n << " DEFICIENT\n"; + else + cout << setw(5) << n << " PERFECT\n"; + } + + cout << "END OF OUTPUT\n"; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/382/382-19.cpp b/uva_cpp_clean/382/382-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..915150205ed5012255301643bffbdcea757293dc --- /dev/null +++ b/uva_cpp_clean/382/382-19.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +int main() { + int n; + printf("PERFECTION OUTPUT\n"); + while (scanf("%d", &n) && n != 0) { + printf("%5d ", n); + int s = n == 1? 0: 1; + for (int i = 2; i < sqrt(n); i++) + if(n % i == 0) s += i + n/i; + if(s == n) printf("PERFECT\n"); + else if (s > n) printf("ABUNDANT\n"); + else printf("DEFICIENT\n"); + } + printf("END OF OUTPUT\n"); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/382/382-5.cpp b/uva_cpp_clean/382/382-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..38d6940cc588006a42da8cfaab46ba6a8800d7b7 --- /dev/null +++ b/uva_cpp_clean/382/382-5.cpp @@ -0,0 +1,32 @@ +#include +int main() +{ + int n,i,j,k,l,m,sum; + printf("PERFECTION OUTPUT\n"); + while(scanf("%d",&n)==1) + { + if(n==0) + break; + sum=0; + for(i=1;i<=n/2;i++) + { + if(n%i==0) + sum=sum+i; + printf("%d",sum); + } + if(sum==n) + { + printf("%5d PERFECT\n",n); + } + else if(sum + +using namespace std; + +int main(){ + int n, sum; + printf("PERFECTION OUTPUT\n"); + while(scanf("%d",&n), n){ + printf("%5d ", n); + sum = 0; + for(int i = 1; i <= n / 2; i++) + if(!(n % i)) + sum += i; + if(sum == n) + printf("PERFECT\n"); + else if(sum > n) + printf("ABUNDANT\n"); + else + printf("DEFICIENT\n"); + } + printf("END OF OUTPUT\n"); + return(0); +} diff --git a/uva_cpp_clean/383/383-14.cpp b/uva_cpp_clean/383/383-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c21675474a3cd99077788917d6bbbe767f2b30a8 --- /dev/null +++ b/uva_cpp_clean/383/383-14.cpp @@ -0,0 +1,101 @@ +/*************************************************** + * Problem name : 383 Shipping Routes.cpp + * OJ : Uva + * Result : AC + * Date : 00-03-17 + * Problem Type : Graph(DFS) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 100 +using namespace std; +typedef long long ll; +//~ int cost[MAX]; +//~ bool visit[MAX]; +vectorV[MAX]; +//~ vectorcost; +//~ vectorvisit; +//~ mapmp; +mapmp, visit, cost; +int BFS(string s, string d) { + queueQ; + //~ visit[mp[s]] = true; + //~ cost[mp[s]] = 0; + visit[s] = 1; + cost[s] = 0; + Q.push(s); + while (!Q.empty()) { + string u = Q.front(); + Q.pop(); + int sz = V[mp[u]].size(); + for (int i = 0; i < sz; i++) { + string v = V[mp[u]][i]; + while (!visit[v]) { + cost[v] = cost[u] + 1; + visit[v] = true; + Q.push(v); + } + } + } + return cost[d]; +} +void mem() { + //~ memset(cost,0,sizeof(cost)); + //~ memset(visit,false,sizeof(visit)); + visit.clear(); + cost.clear(); +} +int main () { + int Node, Edge, tc, Case; + scanf("%d", &tc); + cout << "SHIPPING ROUTES OUTPUT" << endl; + for (int t = 1; t <= tc; t++) { + scanf("%d %d %d", &Node, &Edge, &Case); + for (int i = 1; i <= Node; i++) { + string x; + cin >> x; + mp[x] = i; + } + for (int i = 1; i <= Edge; i++) { + string a, b; + cin >> a >> b; + V[mp[a]].push_back(b); + V[mp[b]].push_back(a); + } + cout << "\nDATA SET " << t << endl << endl; + for (int i = 1; i <= Case; i++) { + int num; + string s, d; + cin >> num >> s >> d; + mem(); + //~ cout << BFS(s, d) << endl; + int c = BFS(s, d); + if (c != 0) { + int C = c * 100 * num; + cout << "$" << C << endl; + } else { + cout << "NO SHIPMENT POSSIBLE" << endl; + } + } + mp.clear(); + for (int i = 0; i < MAX; i++) { + V[i].clear(); + } + } + cout<<"\nEND OF OUTPUT"< +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef pair ii; + +vector AdjList; +int ind; +unordered_map map; +int visited[40]; + +void bfs(int i, int j, int & acum) { + acum = -1; + queue q; + q.push(make_pair(i, 0)); + while (!q.empty()) { + ii n = q.front(); + q.pop(); + if (n.first == j) { + acum = n.second; + break; + } + if (visited[n.first]) continue; + visited[n.first] = true; + for (int j = 0; j < AdjList[n.first].size(); j++) + q.push(make_pair(AdjList[n.first][j], n.second + 1)); + } +} + +int main() { + printf("SHIPPING ROUTES OUTPUT\n\n"); + int cases; + cin >> cases; + for (int c = 1; c <= cases; c++) { + printf("DATA SET %d\n\n", c); + int m, n, p, j; + cin >> m >> n >> p; + ind = 0; + map.clear(); + AdjList.assign(m, vi()); + for (; ind < m; ind++) { + string s; + cin >> s; + map[s] = ind; + } + for (int k = 0; k < n; k++) { + string s1, s2; + cin >> s1 >> s2; + AdjList[map[s1]].push_back(map[s2]); + AdjList[map[s2]].push_back(map[s1]); + } + for (int k = 0; k < p; k++) { + int size; + string s1, s2; + cin >> size >> s1 >> s2; + int cost = 0; + for (int k = 0; k < ind; k++) visited[k] = false; + bfs(map[s1], map[s2], cost); + if (cost != -1) printf("$%d\n", cost*size*100); + else printf("NO SHIPMENT POSSIBLE\n"); + } + printf("\n"); + } + printf("END OF OUTPUT\n"); + return 0; +} diff --git a/uva_cpp_clean/383/383-21.cpp b/uva_cpp_clean/383/383-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c7a56503f88d2e8c5734ec6d5dc2fcd2a05b455 --- /dev/null +++ b/uva_cpp_clean/383/383-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 383 + Name: Shipping Routes + Problem: https://onlinejudge.org/external/3/383.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int dist[31]; + int T; cin>>T; + cout << "SHIPPING ROUTES OUTPUT\n"; + for (int cse=1; cse<=T; ++cse) { + int n, m, p, q; + cin >> n >> m >> q; + cout << "\nDATA SET " << cse << endl << endl; + + string s1, s2; + map ids; + for (int i=0; i> s1; + ids[s1] = i; + } + + vector adj[31]; + for (int i=0; i> s1 >> s2; + int u=ids[s1], v=ids[s2]; + adj[u].push_back(v); + adj[v].push_back(u); + } + + for (int i=0; i> p >> s1 >> s2; + int src=ids[s1], dst=ids[s2]; + memset(dist, -1, n*sizeof(int)); + dist[src]=0; + + queue q; + q.push(src); + while (!q.empty()) { + int u = q.front(); q.pop(); + for (int v: adj[u]) + if (dist[v] < 0) { + dist[v] = dist[u]+1; + if (v == dst) goto fin; + q.push(v); + } + } + fin: + if (dist[dst] < 0) + cout << "NO SHIPMENT POSSIBLE\n"; + else + cout << "$" << (100*p*dist[dst]) << endl; + } + } + cout << "\nEND OF OUTPUT\n"; +} diff --git a/uva_cpp_clean/383/383-9.cpp b/uva_cpp_clean/383/383-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d1e7ae1c8baec992a05e7891bcc8f554429b33b --- /dev/null +++ b/uva_cpp_clean/383/383-9.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +int tc, ct, n, m, q, d[110], p, ans; +string s1, s2, _source, _target; +vector G[110]; +map vert; + +int bfs(int source, int target){ + for(int i = 0; i < ct; i++) d[i] = -1; + queue Q; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + int u = Q.front(); Q.pop(); + for(int i = 0; i < G[u].size(); i++){ + int v = G[u][i]; + if(d[v] == -1) + d[v] = d[u] + 1, Q.push(v); + } + if(d[target] != -1) return d[target] * 100 * p; + } + return -1; +} + +int main(){ + scanf("%d\n", &tc); + puts("SHIPPING ROUTES OUTPUT"); + for(int t = 1; t <= tc; t++){ + putchar('\n'); + printf("DATA SET %d\n\n",t); + for(int i = 0; i < ct; i++) G[i].clear(); + vert.clear(), ct = 0; + scanf("%d %d %d", &n, &m, &q); + for(int i = 0; i < n; i++) cin >> s1, vert[s1] = ct++; + for(int i = 0; i < m; i++) cin >> s1 >> s2, G[vert[s1]].push_back(vert[s2]), G[vert[s2]].push_back(vert[s1]); + while(q--){ + cin >> p >> _source >> _target; + ans = bfs(vert[_source], vert[_target]); + if(ans == -1) puts("NO SHIPMENT POSSIBLE"); + else printf("$%d\n", ans); + } + } + putchar('\n'); + puts("END OF OUTPUT"); + return(0); +} diff --git a/uva_cpp_clean/384/384-21.cpp b/uva_cpp_clean/384/384-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04b42464897deaa1dcb039ffeeb7121d53a65fe2 --- /dev/null +++ b/uva_cpp_clean/384/384-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 384 + Name: Slurpys + Problem: https://onlinejudge.org/external/3/384.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int pos; +string str; +bool slump() { + if (str[pos]!='D' && str[pos]!='E') return 0; + if (str[++pos] != 'F') return 0; + while (str[++pos] == 'F'); + if (str[pos] == 'G') return ++pos; + return slump(); +} + +bool slimp() { + if (str[pos]!='A') return 0; + if (str[++pos]=='H') return ++pos; + if (str[pos]=='B') { + ++pos; + if (!slimp()) + return 0; + } + else if (!slump()) + return 0; + return str[pos++]=='C'; +} + +bool slurpy() { + return slimp() && slump() && pos==str.length(); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + cout << "SLURPYS OUTPUT\n"; + while (T--) { + pos = 0; + cin >> str; + cout << (slurpy() ? "YES\n" : "NO\n"); + } + cout << "END OF OUTPUT\n"; +} diff --git a/uva_cpp_clean/386/386-19.cpp b/uva_cpp_clean/386/386-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74432b1e034d49c74369d5714ffbe93bda2b26a4 --- /dev/null +++ b/uva_cpp_clean/386/386-19.cpp @@ -0,0 +1,249 @@ +#include +#include +using namespace std; + +int main2() { + for (int a = 1; a <= 200; a++) { + for (int b = 2; b <= 200; b++) { + if (b >= a) break; + int a3 = a*a*a, b3 = b*b*b; + int val = a3 - b3; + for (int c = b + 1; c <= 200; c++) { + int c3 = c*c*c; + int d = val - c3; + d = floor(cbrt(d)); + if (c > d) break; + int d3 = d*d*d; + if (a3 == b3 + c3 + d3) printf("Cube = %d, Triple = (%d,%d,%d)\n", a, b, c, d); + } + } + } + return 0; +} + +int main() { + printf("Cube = 6, Triple = (3,4,5)\n" + "Cube = 12, Triple = (6,8,10)\n" + "Cube = 18, Triple = (2,12,16)\n" + "Cube = 18, Triple = (9,12,15)\n" + "Cube = 19, Triple = (3,10,18)\n" + "Cube = 20, Triple = (7,14,17)\n" + "Cube = 24, Triple = (12,16,20)\n" + "Cube = 25, Triple = (4,17,22)\n" + "Cube = 27, Triple = (3,18,24)\n" + "Cube = 28, Triple = (18,19,21)\n" + "Cube = 29, Triple = (11,15,27)\n" + "Cube = 30, Triple = (15,20,25)\n" + "Cube = 36, Triple = (4,24,32)\n" + "Cube = 36, Triple = (18,24,30)\n" + "Cube = 38, Triple = (6,20,36)\n" + "Cube = 40, Triple = (14,28,34)\n" + "Cube = 41, Triple = (2,17,40)\n" + "Cube = 41, Triple = (6,32,33)\n" + "Cube = 42, Triple = (21,28,35)\n" + "Cube = 44, Triple = (16,23,41)\n" + "Cube = 45, Triple = (5,30,40)\n" + "Cube = 46, Triple = (3,36,37)\n" + "Cube = 46, Triple = (27,30,37)\n" + "Cube = 48, Triple = (24,32,40)\n" + "Cube = 50, Triple = (8,34,44)\n" + "Cube = 53, Triple = (29,34,44)\n" + "Cube = 54, Triple = (6,36,48)\n" + "Cube = 54, Triple = (12,19,53)\n" + "Cube = 54, Triple = (27,36,45)\n" + "Cube = 56, Triple = (36,38,42)\n" + "Cube = 57, Triple = (9,30,54)\n" + "Cube = 58, Triple = (15,42,49)\n" + "Cube = 58, Triple = (22,30,54)\n" + "Cube = 60, Triple = (21,42,51)\n" + "Cube = 60, Triple = (30,40,50)\n" + "Cube = 63, Triple = (7,42,56)\n" + "Cube = 66, Triple = (33,44,55)\n" + "Cube = 67, Triple = (22,51,54)\n" + "Cube = 69, Triple = (36,38,61)\n" + "Cube = 70, Triple = (7,54,57)\n" + "Cube = 71, Triple = (14,23,70)\n" + "Cube = 72, Triple = (8,48,64)\n" + "Cube = 72, Triple = (34,39,65)\n" + "Cube = 72, Triple = (36,48,60)\n" + "Cube = 75, Triple = (12,51,66)\n" + "Cube = 75, Triple = (38,43,66)\n" + "Cube = 76, Triple = (12,40,72)\n" + "Cube = 76, Triple = (31,33,72)\n" + "Cube = 78, Triple = (39,52,65)\n" + "Cube = 80, Triple = (28,56,68)\n" + "Cube = 81, Triple = (9,54,72)\n" + "Cube = 81, Triple = (25,48,74)\n" + "Cube = 82, Triple = (4,34,80)\n" + "Cube = 82, Triple = (12,64,66)\n" + "Cube = 82, Triple = (19,60,69)\n" + "Cube = 84, Triple = (28,53,75)\n" + "Cube = 84, Triple = (42,56,70)\n" + "Cube = 84, Triple = (54,57,63)\n" + "Cube = 85, Triple = (50,61,64)\n" + "Cube = 87, Triple = (20,54,79)\n" + "Cube = 87, Triple = (26,55,78)\n" + "Cube = 87, Triple = (33,45,81)\n" + "Cube = 87, Triple = (38,48,79)\n" + "Cube = 88, Triple = (21,43,84)\n" + "Cube = 88, Triple = (25,31,86)\n" + "Cube = 88, Triple = (32,46,82)\n" + "Cube = 89, Triple = (17,40,86)\n" + "Cube = 90, Triple = (10,60,80)\n" + "Cube = 90, Triple = (25,38,87)\n" + "Cube = 90, Triple = (45,60,75)\n" + "Cube = 90, Triple = (58,59,69)\n" + "Cube = 92, Triple = (6,72,74)\n" + "Cube = 92, Triple = (54,60,74)\n" + "Cube = 93, Triple = (32,54,85)\n" + "Cube = 95, Triple = (15,50,90)\n" + "Cube = 96, Triple = (19,53,90)\n" + "Cube = 96, Triple = (48,64,80)\n" + "Cube = 97, Triple = (45,69,79)\n" + "Cube = 99, Triple = (11,66,88)\n" + "Cube = 100, Triple = (16,68,88)\n" + "Cube = 100, Triple = (35,70,85)\n" + "Cube = 102, Triple = (51,68,85)\n" + "Cube = 103, Triple = (12,31,102)\n" + "Cube = 105, Triple = (33,70,92)\n" + "Cube = 106, Triple = (58,68,88)\n" + "Cube = 108, Triple = (12,72,96)\n" + "Cube = 108, Triple = (13,51,104)\n" + "Cube = 108, Triple = (15,82,89)\n" + "Cube = 108, Triple = (24,38,106)\n" + "Cube = 108, Triple = (54,72,90)\n" + "Cube = 110, Triple = (29,75,96)\n" + "Cube = 111, Triple = (16,47,108)\n" + "Cube = 112, Triple = (72,76,84)\n" + "Cube = 113, Triple = (50,74,97)\n" + "Cube = 114, Triple = (18,60,108)\n" + "Cube = 114, Triple = (57,76,95)\n" + "Cube = 115, Triple = (3,34,114)\n" + "Cube = 116, Triple = (23,86,97)\n" + "Cube = 116, Triple = (30,84,98)\n" + "Cube = 116, Triple = (44,60,108)\n" + "Cube = 117, Triple = (13,78,104)\n" + "Cube = 120, Triple = (9,55,116)\n" + "Cube = 120, Triple = (42,84,102)\n" + "Cube = 120, Triple = (60,80,100)\n" + "Cube = 121, Triple = (49,84,102)\n" + "Cube = 122, Triple = (19,92,101)\n" + "Cube = 123, Triple = (6,51,120)\n" + "Cube = 123, Triple = (18,96,99)\n" + "Cube = 123, Triple = (44,51,118)\n" + "Cube = 125, Triple = (20,85,110)\n" + "Cube = 126, Triple = (14,84,112)\n" + "Cube = 126, Triple = (23,94,105)\n" + "Cube = 126, Triple = (63,84,105)\n" + "Cube = 127, Triple = (13,65,121)\n" + "Cube = 129, Triple = (38,57,124)\n" + "Cube = 132, Triple = (5,76,123)\n" + "Cube = 132, Triple = (48,69,123)\n" + "Cube = 132, Triple = (66,88,110)\n" + "Cube = 133, Triple = (21,70,126)\n" + "Cube = 134, Triple = (44,102,108)\n" + "Cube = 134, Triple = (86,95,97)\n" + "Cube = 135, Triple = (15,90,120)\n" + "Cube = 137, Triple = (44,73,128)\n" + "Cube = 138, Triple = (9,108,111)\n" + "Cube = 138, Triple = (69,92,115)\n" + "Cube = 138, Triple = (72,76,122)\n" + "Cube = 138, Triple = (81,90,111)\n" + "Cube = 139, Triple = (94,96,99)\n" + "Cube = 140, Triple = (14,108,114)\n" + "Cube = 140, Triple = (49,98,119)\n" + "Cube = 140, Triple = (90,95,105)\n" + "Cube = 141, Triple = (72,85,122)\n" + "Cube = 142, Triple = (28,46,140)\n" + "Cube = 142, Triple = (31,64,137)\n" + "Cube = 144, Triple = (16,96,128)\n" + "Cube = 144, Triple = (68,78,130)\n" + "Cube = 144, Triple = (72,96,120)\n" + "Cube = 145, Triple = (12,81,136)\n" + "Cube = 145, Triple = (55,75,135)\n" + "Cube = 147, Triple = (22,75,140)\n" + "Cube = 150, Triple = (24,102,132)\n" + "Cube = 150, Triple = (71,73,138)\n" + "Cube = 150, Triple = (75,100,125)\n" + "Cube = 150, Triple = (76,86,132)\n" + "Cube = 151, Triple = (46,47,148)\n" + "Cube = 152, Triple = (24,80,144)\n" + "Cube = 152, Triple = (62,66,144)\n" + "Cube = 153, Triple = (17,102,136)\n" + "Cube = 156, Triple = (65,87,142)\n" + "Cube = 156, Triple = (78,104,130)\n" + "Cube = 159, Triple = (3,121,131)\n" + "Cube = 159, Triple = (87,102,132)\n" + "Cube = 160, Triple = (56,112,136)\n" + "Cube = 160, Triple = (69,123,124)\n" + "Cube = 162, Triple = (18,108,144)\n" + "Cube = 162, Triple = (36,57,159)\n" + "Cube = 162, Triple = (50,96,148)\n" + "Cube = 162, Triple = (59,93,148)\n" + "Cube = 162, Triple = (81,108,135)\n" + "Cube = 164, Triple = (8,68,160)\n" + "Cube = 164, Triple = (24,128,132)\n" + "Cube = 164, Triple = (38,120,138)\n" + "Cube = 164, Triple = (69,99,146)\n" + "Cube = 167, Triple = (12,86,159)\n" + "Cube = 168, Triple = (56,106,150)\n" + "Cube = 168, Triple = (84,112,140)\n" + "Cube = 168, Triple = (108,114,126)\n" + "Cube = 170, Triple = (96,107,141)\n" + "Cube = 170, Triple = (100,122,128)\n" + "Cube = 171, Triple = (19,114,152)\n" + "Cube = 171, Triple = (27,90,162)\n" + "Cube = 171, Triple = (54,80,163)\n" + "Cube = 171, Triple = (107,108,136)\n" + "Cube = 174, Triple = (40,108,158)\n" + "Cube = 174, Triple = (45,126,147)\n" + "Cube = 174, Triple = (47,97,162)\n" + "Cube = 174, Triple = (52,110,156)\n" + "Cube = 174, Triple = (66,90,162)\n" + "Cube = 174, Triple = (76,96,158)\n" + "Cube = 174, Triple = (87,116,145)\n" + "Cube = 175, Triple = (28,119,154)\n" + "Cube = 176, Triple = (25,92,167)\n" + "Cube = 176, Triple = (42,86,168)\n" + "Cube = 176, Triple = (50,62,172)\n" + "Cube = 176, Triple = (64,92,164)\n" + "Cube = 177, Triple = (48,137,142)\n" + "Cube = 178, Triple = (34,80,172)\n" + "Cube = 178, Triple = (48,133,147)\n" + "Cube = 179, Triple = (17,57,177)\n" + "Cube = 180, Triple = (20,120,160)\n" + "Cube = 180, Triple = (50,76,174)\n" + "Cube = 180, Triple = (63,126,153)\n" + "Cube = 180, Triple = (90,120,150)\n" + "Cube = 180, Triple = (116,118,138)\n" + "Cube = 181, Triple = (108,109,150)\n" + "Cube = 184, Triple = (12,144,148)\n" + "Cube = 184, Triple = (108,120,148)\n" + "Cube = 185, Triple = (57,68,180)\n" + "Cube = 185, Triple = (68,113,166)\n" + "Cube = 186, Triple = (18,121,167)\n" + "Cube = 186, Triple = (64,108,170)\n" + "Cube = 186, Triple = (93,124,155)\n" + "Cube = 187, Triple = (58,131,160)\n" + "Cube = 188, Triple = (115,122,149)\n" + "Cube = 189, Triple = (21,46,188)\n" + "Cube = 189, Triple = (21,126,168)\n" + "Cube = 190, Triple = (30,100,180)\n" + "Cube = 190, Triple = (56,133,163)\n" + "Cube = 191, Triple = (39,146,156)\n" + "Cube = 192, Triple = (34,123,173)\n" + "Cube = 192, Triple = (38,106,180)\n" + "Cube = 192, Triple = (96,128,160)\n" + "Cube = 193, Triple = (25,68,190)\n" + "Cube = 194, Triple = (90,138,158)\n" + "Cube = 196, Triple = (126,133,147)\n" + "Cube = 197, Triple = (53,58,194)\n" + "Cube = 198, Triple = (22,132,176)\n" + "Cube = 198, Triple = (27,46,197)\n" + "Cube = 198, Triple = (73,135,170)\n" + "Cube = 198, Triple = (99,132,165)\n" + "Cube = 199, Triple = (6,127,180)\n" + "Cube = 200, Triple = (32,136,176)\n" + "Cube = 200, Triple = (70,140,170)\n"); + return 0; +} diff --git a/uva_cpp_clean/386/386-21.cpp b/uva_cpp_clean/386/386-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc8b979c76eea58cd1244d4cfd9f43f06a36edcd --- /dev/null +++ b/uva_cpp_clean/386/386-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 386 + Name: Perfect Cubes + Problem: https://onlinejudge.org/external/3/386.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +struct P { + int a, b, c, d; + bool operator < (const P& o) const { + if (a==o.a) + return b==o.b ? c +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int a,b,c,d,n; + for(n=6;n<=200;n++) + { + a = n*n*n; + for(int i=2;i + +using namespace std; + +int main(){ + for(int d = 6; d <= 200; d++) + for(int a = 2; a < d; a++) + for(int b = a; b < d; b++) + for(int c = b; c < d; c++) + if(d * d * d == a * a * a + b * b * b + c * c * c) + printf("Cube = %d, Triple = (%d,%d,%d)\n", d, a, b, c); + return(0); +} diff --git a/uva_cpp_clean/387/387-21.cpp b/uva_cpp_clean/387/387-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e77d6bf5ef45e14d0cae7148c565c58d5b2d6821 --- /dev/null +++ b/uva_cpp_clean/387/387-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 387 + Name: A Puzzling Problem + Problem: https://onlinejudge.org/external/3/387.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool U[7]; +int S[7], W[7], H[7], X[7][17], Y[7][17], n; + +int M[4][4]; +bool dfs(int k) { + if (k == n) return 1; + for (int y=0; y<=4-H[k]; ++y) + for (int x=0; x<=4-W[k]; ++x) { + for (int i=0; ipubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + bool frst = 1; + while (cin >> n && n>0) { + if (frst) frst = 0; + else cout << '\n'; + + int co = 0; + for (int i=0; i> H[i] >> W[i]; + + int sz = 0; + for (int j=0; j> ln; + for (int k=0; k>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 388 + Name: Galactic Import + Problem: https://onlinejudge.org/external/3/388.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + int n, dist[27]={}; + while (scanf("%d", &n)==1) { + vector adj[27]; + int mat[27][27]={}, vals[27]; + + for (int i=0; i q; + q.push(0); + while (!q.empty()) { + int u = q.front(); q.pop(); + for (int v: adj[u]) + if (dist[v]<0) { + dist[v] = dist[u]+1; + q.push(v); + } + } + + int best=-1, bi; + for (int i=1; i<27; ++i) + if (dist[i] > 0) { + double price=pow(0.95, dist[i]-1) * vals[i]; + if (price > best) { + best = price; + bi = i; + } + } + + printf("Import from %c\n", bi+64); + } +} diff --git a/uva_cpp_clean/388/388-9.cpp b/uva_cpp_clean/388/388-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28bbec09e0e47d7bad6ec2c5467e75a66878c2dc --- /dev/null +++ b/uva_cpp_clean/388/388-9.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int n; +double f; +char s1, s2[50]; +map p; +map > G; +map d; +const double fee = 0.95; + +char bfs(char source){ + queue Q; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + char u = Q.front(); Q.pop(); + for(int i = 0; i < G[u].size(); i++){ + char v = G[u][i]; + if(d[v] == -1) + d[v] = d[u] + 1, Q.push(v); + } + } + double _max = -1; + char ans; + for(char ch = 'A'; ch <= 'Z'; ch++){ + double v = p[ch] * pow(fee, d[ch] - 1); + if(v > _max) _max = v, ans = ch; + } + return ans; +} + +int main(){ + while(scanf("%d\n", &n) == 1){ + p.clear(), G.clear(); + for(int i = 0; i < n; i++){ + scanf("%c %lf %s\n", &s1, &f, s2); + p[s1] = f, d[s1] = -1; + for(int i = 0; i < strlen(s2); i++) + G[s1].push_back(s2[i]), G[s2[i]].push_back(s1), d[s2[i]] = -1; + } + printf("Import from %c\n", bfs('*')); + } + return(0); +} diff --git a/uva_cpp_clean/389/389-19.cpp b/uva_cpp_clean/389/389-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84007b86ab90161ee70babfa8610803df7885697 --- /dev/null +++ b/uva_cpp_clean/389/389-19.cpp @@ -0,0 +1,46 @@ +#include +#include +using namespace std; + +int ctoi(char c) { + if (c - 'A' >= 0) return c - 'A' + 10; + else return c - '0'; +} + +char itoc(int i) { + if (i < 10) return i + '0'; + else return i + 'A' - 10; +} + +int main() { + string n; + int b1, b2; + while (cin >> n >> b1 >> b2) { + int val = 0; + for (int i = 0; i < n.length(); i++) { + val *= b1; + val += ctoi(n[i]); + } + stack s; + while (val) { + s.push(itoc(val%b2)); + val /= b2; + } + if (s.size() > 7) printf(" ERROR\n"); + else { + for (int i = 6; i >= s.size(); i--) { + printf(" "); + if (i == 1 && s.size() == 0) { + printf("0"); + break; + } + } + while (!s.empty()) { + printf("%c", s.top()); + s.pop(); + } + printf("\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/389/389-21.cpp b/uva_cpp_clean/389/389-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2458ea6ad67f5327061acd24720d2d174ef68cb --- /dev/null +++ b/uva_cpp_clean/389/389-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 389 + Name: Basically Speaking + Problem: https://onlinejudge.org/external/3/389.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char num[10000], w[10]; +int main(){ + int s, t; + + while (scanf("%s %d %d", num, &s, &t)==3) { + bool err=0; + int b10 = 0; + int maxx = pow(t, 7) - 1; + + for (int i=0; !err && num[i]; i++) { + int x = num[i]>='A' ? (10+num[i]-'A') : num[i]-'0'; + b10 = b10 * s + x; + if (b10 > maxx) err=1; + } + + if (err) puts(" ERROR"); + else if (!b10) puts(" 0"); + else { + int i=6; + for(; b10; b10/=t) { + int x = b10%t; + w[i--] = x>9 ? ('A'+x-10) : ('0'+x); + } + while(i>=0) w[i--] = ' '; + puts(w); + } + } +} diff --git a/uva_cpp_clean/392/392-21.cpp b/uva_cpp_clean/392/392-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46462a609b055dfab3def9ced2a24737fd1d76dc --- /dev/null +++ b/uva_cpp_clean/392/392-21.cpp @@ -0,0 +1,41 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 392 + Name: Polynomial Showdown + Problem: https://onlinejudge.org/external/3/392.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int c[9]; + while (scanf("%d%d%d%d%d%d%d%d%d", c, c+1, c+2, c+3, c+4, c+5, c+6, c+7, c+8)==9) { + int i=0; + while(!c[i] && i<8) i++; + + if (i==8) cout<1 || i==8) cout< + +using namespace std; + +const int SIZE = 10; + +int coef[SIZE], r; + +int main(){ + while(scanf("%d", coef) == 1){ + for(int it = 1; it < 9; it++) scanf("%d", coef + it); + r = 0; + for(int it = 0; it < 9; it++){ + if( coef[it] == 0 ) continue; + if( coef[it] < 0 ){ + if( r ) putchar(' '); + putchar('-'); + } + if( r and coef[it] > 0 ){ + putchar(' '); + putchar('+'); + } + if( coef[it] != 0 ){ + if( r++ ) putchar(' '); + if( it == 8 ) printf("%d", abs(coef[it])); + else if( it == 7 ){ + if( abs(coef[it]) != 1 ) + printf("%dx", abs(coef[it])); + else + putchar('x'); + } + else{ + if( abs(coef[it]) != 1 ) + printf("%dx^%d", abs(coef[it]), 8 - it); + else + printf("x^%d", 8 - it); + } + } + } + if( r == 0 ) putchar('0'); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/394/394-16.cpp b/uva_cpp_clean/394/394-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e03df2f65dfa6871788f64f6c9d6159f2236ba53 --- /dev/null +++ b/uva_cpp_clean/394/394-16.cpp @@ -0,0 +1,63 @@ +//============================================================================ +// Name : 394 - Mapmaker.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 394 - Mapmaker in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include +#include +#include + +using namespace std; + +#define FRI(i, a, b) for(i = a; i < (int)b; ++i) +#define FRD(i, a, b) for(i = b - 1; i > a; --i) + +int main() { + int i, j; + int n, r, b; + int d[100], ld[100], ud[100]; + int c[100][100]; + vector names; + + char name[10]; + int addr, index, x; + + cin >> n >> r; + FRI(i, 0, n) { + cin >> name >> b >> x >> d[i]; + + FRI(j, 0, d[i]) { + cin >> ld[j + 1] >> ud[j + 1]; + } + + names.push_back(name); + c[i][d[i]] = x; + c[i][0] = b - (c[i][d[i]] * ld[d[i]]); + + FRD(j, 0, d[i]) { + c[i][j] = c[i][j + 1] * (ud[j + 1] - ld[j + 1] + 1); + c[i][0] -= c[i][j] * ld[j]; + } + } + + FRI(i, 0, r) { + cin >> name >> x; + index = distance(names.begin(), find(names.begin(), names.end(), name)); + addr = c[index][0] + c[index][1] * x; + + cout << name << "[" << x; + FRI(j, 1, d[index]) { + cin >> x; + addr += c[index][j + 1] * x; + cout << ", " << x; + } + + cout << "] = " << addr << endl; + } + + return 0; +} diff --git a/uva_cpp_clean/394/394-21.cpp b/uva_cpp_clean/394/394-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c7d573504d2c9dc3e682651e7a3739c1e9868e7 --- /dev/null +++ b/uva_cpp_clean/394/394-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 394 + Name: Mapmaker + Problem: https://onlinejudge.org/external/3/394.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MAXN 10000 +int D[MAXN], C[MAXN][15], L[15], U[15], n, m; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin>>n>>m) { + string str; + unordered_map ids; + for (int i=0; i> str >> b >> s >> d; + ids[str] = i; + D[i] = d; + for (int j=1; j<=d; ++j) + cin >> L[j] >> U[j]; + + C[i][d] = s; + for (int j=d-1; j>0; --j) + C[i][j] = C[i][j+1] * (U[j+1] - L[j+1] + 1); + C[i][0] = b; + for (int j=1; j<=d; ++j) + C[i][0] -= C[i][j]*L[j]; + } + + for (int i=0; i> str; + int id = ids[str], + ad = C[id][0], ind; + + cout << str << '['; + for (int i=1; i<= D[id]; ++i) { + cin >> ind; + ad += C[id][i] * ind; + if (i > 1) cout << ", "; + cout << ind; + } + cout << "] = " << ad << endl; + } + } +} diff --git a/uva_cpp_clean/394/394-6.cc b/uva_cpp_clean/394/394-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..162945b9467a88e94221afce56c5972b0e73a659 --- /dev/null +++ b/uva_cpp_clean/394/394-6.cc @@ -0,0 +1,46 @@ +// https://uva.onlinejudge.org/external/3/394.pdf +#include +#include +#include + +using namespace std; + +typedef vector vi; +typedef vector vvi; +typedef unordered_map msi; + +int main() { + int n, r; + cin >> n >> r; + msi m; + for (int i = 0; i < n; i++) { + string name; + int b, s, d; + cin >> name >> b >> s >> d; + vi l(d); + vi u(d); + for (int j = 0; j < d; j++) cin >> l[j] >> u[j]; + vi c(d + 1); + c[d] = s; + for (int j = d - 1; j >= 1; j--) c[j] = c[j+1] * (u[j]-l[j]+1); + c[0] = b; + for (int j = 1; j <= d; j++) c[0] -= c[j]*l[j - 1]; + m[name] = c; + } + for (int i = 0; i < r; i++) { + string name; + cin >> name; + vi c = m[name]; + int sum = c[0]; + cout << name << "["; + for (int j = 1; j < c.size(); j++) { + int x; + cin >> x; + cout << x; + if (j < c.size() - 1) cout << ", "; + else cout << "] = "; + sum += c[j] * x; + } + cout << sum << endl; + } +} diff --git a/uva_cpp_clean/397/397-15.cpp b/uva_cpp_clean/397/397-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b239d51b9136c8e8819add6069b900582016498 --- /dev/null +++ b/uva_cpp_clean/397/397-15.cpp @@ -0,0 +1,132 @@ +/* + 模擬、連結串列 + 時間複雜度: O(N^2) +*/ +#include +#include + +using namespace std; + +string variable; + +struct Node { + int data; + Node* next; + Node* prev; +}; + +// 輸出算式 +void print(Node* new_num, Node* op) { + while (new_num->next != NULL) { + cout << new_num->data << " " << char(op->data) << " "; + new_num = new_num->next; + op = op->next; + } + cout << variable << "\n"; +} + +// 找乘除號 +Node* get_mul_div(Node* head, int* index) { + int i; + for (i = 0; head->next != NULL; i++, head = head->next) + if (head->data == 42 /* * */ || head->data == 47 /* / */) + break; + *index = i; + return head; +} + +Node* index_node(Node* head, int index) { + for (int i = 0; i < index; i++) + head = head->next; + return head; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int num, index; + char op; + + Node *op_now, *num_now, *num_next, *new_num; + + while (cin >> num >> op) { + Node *numbers = new Node{0, NULL, NULL}, *operators = new Node{0, NULL, NULL}; + Node *prev_num = numbers, *prev_op = operators; + + do { + prev_num->data = num; + prev_op->data = op; + + num_now = new Node{-1, NULL, prev_num}; + op_now = new Node{-1, NULL, prev_op}; + + prev_num->next = num_now; + prev_op->next = op_now; + + prev_num = num_now; + prev_op = op_now; + } while (op != '=' && cin >> num >> op); + + cin >> variable; + + op_now = get_mul_div(operators, &index); + print(numbers, operators); + // 由左至右計算全部乘法和除法 + while (op_now->next != NULL) { + num_now = index_node(numbers, index); + num_next = num_now->next; + new_num = new Node{0, num_next->next, num_now->prev}; + if (op_now->data == 42) // 乘法 + new_num->data = num_now->data * num_next->data; + else + new_num->data = num_now->data / num_next->data; + + if (num_now->prev == NULL) // 是第一個 + numbers = new_num; + else + num_now->prev->next = new_num; + if (num_next->next != NULL) // 不是最後一個 + num_next->next->prev = new_num; + if (op_now->prev == NULL) // 是第一個 + operators = op_now->next; + else + op_now->prev->next = op_now->next; + if (op_now->next != NULL) // 不是最後一個 + op_now->next->prev = op_now->prev; + + // 釋放記憶體 + delete op_now; + delete num_now; + delete num_next; + + op_now = get_mul_div(operators, &index); + print(numbers, operators); + } + + op_now = operators; + // 由左而右算加減法 + while (op_now->data != 61) { // 等號 + num_now = numbers; + num_next = num_now->next; + + new_num = new Node{0, num_next->next, num_now->prev}; + if (op_now->data == 43) // 加號 + new_num->data = num_now->data + num_next->data; + else + new_num->data = num_now->data - num_next->data; + + // 新的變成開頭 + numbers = new_num; + operators = op_now->next; + + delete op_now; + delete num_now; + delete num_next; + + op_now = operators; + print(numbers, operators); + } + cout << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/400/400-17.cpp b/uva_cpp_clean/400/400-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6801d56c1966df71161af057950cbc56f1b1dad --- /dev/null +++ b/uva_cpp_clean/400/400-17.cpp @@ -0,0 +1,34 @@ +/** + * AOAPC II Example 5-8 Unix ls + */ +#include +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n; + while (cin >> n) { + vector names(n); + int m = 0; + for (string &s : names) { + cin >> s; + m = max(m, (int) s.length()); + } + sort(names.begin(), names.end()); + int col = 62 / (m + 2), row = (n - 1) / col + 1; + cout << string(60, '-') << "\n"; + for (int r = 0; r < row; ++r) { + for (int c = 0; c < col; ++c) { + int ind = r + c * row; + if (ind < n) { + if (c) + cout << " "; + cout << setw(m) << left << names[ind]; + } + else + break; + } + cout << "\n"; + } + } +} diff --git a/uva_cpp_clean/400/400-18.cpp b/uva_cpp_clean/400/400-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..424c3748c9d74be3f9a2980b369c1618ada65e63 --- /dev/null +++ b/uva_cpp_clean/400/400-18.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main(){ + int numF, maxC, spaces, index, C, cAux; + double minF; + string file, maxS, line = "------------------------------------------------------------"; + vector files; + while(true){ + cin >> numF; + if(cin.fail())break; + getline(cin, file); //Get new line + maxS = ""; + maxC = 1; + minF = 0; + C = 0; + index = 0; + files.clear(); + for(int i = 0; i < numF; i++) { + cin >> file; + files.push_back(file); + if(file.size() > maxS.size()) maxS = file; + } + sort(files.begin(), files.end()); + C = maxS.size(); + spaces = C + 2; + cAux = C + 2; + while(C + cAux <= 60){ + C += cAux; + maxC++; + } + minF = ceil((double)numF/maxC); + cout << line << endl; + for(int i = 0; i < minF; i++){ + for(int j = 0; j < maxC; j++){ + if(index + j*minF >= files.size()) break; + printf("%s",files.at(index + j*minF).c_str()); + string word = files.at(index + j*minF).c_str(); + int deltaSpaces = word.size() - spaces; + deltaSpaces = abs(deltaSpaces); + printf("%*s", deltaSpaces, ""); + } + index++; + printf("\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/400/400-19.cpp b/uva_cpp_clean/400/400-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72a9b8931cd773618932f2209d608f891adb35bc --- /dev/null +++ b/uva_cpp_clean/400/400-19.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +using namespace std; + +int main() { + vector v; + int n; + while (cin >> n) { + v.clear(); + int m = 0; + while (n--) { + string in; + cin >> in; + v.push_back(in); + m = max(m, (int)in.length()); + } + sort(v.begin(), v.end()); + int col = (60-m)/(m+2) + 1; + cout << "------------------------------------------------------------\n"; + int div = v.size()/col + (v.size()%col? 1: 0); + for (int i = 0; i < div; i++) { + for (int j = 0; j < col; j++) { + if (j == col - 1) cout.width(m); + else cout.width(m+2); + if (i+j*(div) < v.size()) cout << left << v[i+j*(div)]; + } + cout << endl; + } + } + return 0; +} diff --git a/uva_cpp_clean/400/400-21.cpp b/uva_cpp_clean/400/400-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f369dcde21ee6b0ec1cc43b6e2cd87ea5186592 --- /dev/null +++ b/uva_cpp_clean/400/400-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 400 + Name: Unix ls + Problem: https://onlinejudge.org/external/4/400.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +string list[200]; +int main(){ + int n; + while (cin>>n) { + puts("------------------------------------------------------------"); + int maxlen=0; + for (int i=0; i>list[i]; + maxlen=max(maxlen, (int)list[i].length()); + } + sort(list, list+n); + + int cols = (60-maxlen)/(maxlen+2) + 1, + rows = ceil((double)n/cols); + + for (int i=0; i=n) { + cout<0; k--) + putchar(' '); + + if (rows*(j+1) + i + +using namespace std; + +int n, ml, nr, nc, it; +vector s; + +void sep(){ + for(int i = 0; i < 60; i++) putchar('-'); + putchar('\n'); +} + +int main(){ + while(scanf("%d\n", &n) == 1){ + s.resize(n), ml = -1; + for(int i = 0; i < n; i++) cin>>s[i], ml = max(ml, (int)s[i].size()); + sort(s.begin(), s.end()); + sep(); + nc = 62 / (ml + 2), nr = (n + nc - 1) / nc, it = 0; + ml += 2; + vector > v(nr, vector (nc)); + for(int j = 0; j < nc; j++) + for(int i = 0; i < nr; i++){ + v[i][j] = s[it++]; + if(it == n) i = j = 110; + } + for(int i = 0; i < nr; i++){ + for(int j = 0; j < nc; j++) + if(j != nc-1 && !v[i][j + 1].empty()) printf("%-*s", ml, v[i][j].c_str()); + else printf("%s\n", v[i][j].c_str()), j = nc; + } + } + return(0); +} diff --git a/uva_cpp_clean/401/401-14.cpp b/uva_cpp_clean/401/401-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd57ce22e3fc067e1361e6f4d1634d43f0a66681 --- /dev/null +++ b/uva_cpp_clean/401/401-14.cpp @@ -0,0 +1,113 @@ +/*************************************************** + * Problem name : 401 Palindromes.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/401.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-05-04 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +char isMirror(char ch) { + if (ch == 'A') return 'A'; + else if (ch == 'E') return '3'; + else if (ch == 'H') return 'H'; + else if (ch == 'I') return 'I'; + else if (ch == 'J') return 'L'; + else if (ch == 'L') return 'J'; + else if (ch == 'M') return 'M'; + else if (ch == 'O') return 'O'; + else if (ch == 'S') return '2'; + else if (ch == 'T') return 'T'; + else if (ch == 'U') return 'U'; + else if (ch == 'V') return 'V'; + else if (ch == 'W') return 'W'; + else if (ch == 'X') return 'X'; + else if (ch == 'Y') return 'Y'; + else if (ch == 'Z') return '5'; + else if (ch == '1') return '1'; + else if (ch == '2') return 'S'; + else if (ch == '3') return 'E'; + else if (ch == '5') return 'Z'; + else if (ch == '8') return '8'; + + return 'a'; +} +bool isM(string str) { + //~ int flag = 0; + string rev = ""; + for (int i = 0; i < (int)str.size() ; i++) { + char ch = isMirror(str[i]); + if (ch == 'a') return 0; + rev += ch; + } + reverse(rev.begin(), rev.end()); + if (str == rev)return 1; + + return 0; +} +int main () { + __FastIO; + string str; + while (cin >> str) { + string rev = str; + reverse(rev.begin(), rev.end()); + if (rev == str) { + if (isM(str)) { + cout << str << " -- is a mirrored palindrome.\n"; + } else cout << str << " -- is a regular palindrome.\n"; + } else { + if (isM(str)) { + cout << str << " -- is a mirrored string.\n"; + } else cout << str << " -- is not a palindrome.\n"; + } + cout << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/401/401-15.cpp b/uva_cpp_clean/401/401-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a7134aabf311e08cc33596d939f534948d3b09c --- /dev/null +++ b/uva_cpp_clean/401/401-15.cpp @@ -0,0 +1,65 @@ +/* + 鏡像迴文 + 時間複雜度: O(N) +*/ +#include +#include + +using namespace std; + +char mirror[128]; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + mirror['A'] = 'A'; + mirror['E'] = '3'; + mirror['H'] = 'H'; + mirror['I'] = 'I'; + mirror['J'] = 'L'; + mirror['L'] = 'J'; + mirror['M'] = 'M'; + mirror['O'] = 'O'; + mirror['S'] = '2'; + mirror['T'] = 'T'; + mirror['U'] = 'U'; + mirror['V'] = 'V'; + mirror['W'] = 'W'; + mirror['X'] = 'X'; + mirror['Y'] = 'Y'; + mirror['Z'] = '5'; + mirror['2'] = 'S'; + mirror['3'] = 'E'; + mirror['5'] = 'Z'; + mirror['8'] = '8'; + + string input; + int length, i, j; + + while (cin >> input) { + bool is_mirrored = true, is_palindrome = true; + // 頭尾比對 + length = input.size(); + for (i = 0, j = length - 1; i <= j; i++, j--) { + if (input[i] != input[j]) + is_palindrome = false; + if (input[i] != mirror[input[j]]) + is_mirrored = false; + } + + cout << input; + + if (is_mirrored) { + if (is_palindrome) + cout << " -- is a mirrored palindrome.\n\n"; + else + cout << " -- is a mirrored string.\n\n"; + } else { + if (is_palindrome) + cout << " -- is a regular palindrome.\n\n"; + else + cout << " -- is not a palindrome.\n\n"; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/401/401-18.cpp b/uva_cpp_clean/401/401-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..57f7ea52617fc60a4812e2393c79cf487f90c795 --- /dev/null +++ b/uva_cpp_clean/401/401-18.cpp @@ -0,0 +1,50 @@ +#include +#include + +using namespace std; + +int main(){ + char database[256]; + database['A']='A'; + database['E']='3'; + database['H']='H'; + database['I']='I'; + database['J']='L'; + database['L']='J'; + database['M']='M'; + database['O']='O'; + database['S']='2'; + database['T']='T'; + database['U']='U'; + database['V']='V'; + database['W']='W'; + database['X']='X'; + database['Y']='Y'; + database['Z']='5'; + database['1']='1'; + database['2']='S'; + database['3']='E'; + database['5']='Z'; + database['8']='8'; + string input, auxP, auxM; + bool palindrome, mirrored; + while(getline(cin, input)){ + if(input.length() == 0) break; + auxP = ""; + auxM = ""; + palindrome = false; + mirrored = false; + for(int i = input.length() - 1; i >= 0; i--){ + auxP += input[i]; + if(database[input[i]] != ' ') auxM += database[input[i]]; + } + if(auxP == input) palindrome = true; + if(auxM == input) mirrored = true; + if(!palindrome && !mirrored) cout << input << " -- is not a palindrome." << endl; + else if(palindrome && !mirrored) cout << input << " -- is a regular palindrome." << endl; + else if(mirrored && !palindrome) cout << input << " -- is a mirrored string." << endl; + else cout << input << " -- is a mirrored palindrome." << endl; + if(input.length() != 0) cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/401/401-19.cpp b/uva_cpp_clean/401/401-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fd8032fdc53d9e2ba110a83b54fe87dd9f3f60c --- /dev/null +++ b/uva_cpp_clean/401/401-19.cpp @@ -0,0 +1,52 @@ +#include +#include +using namespace std; + +bool reverse(char a, char b) { + switch (a) { + case 'A': return b == 'A'; + case 'E': return b == '3'; + case 'H': return b == 'H'; + case 'I': return b == 'I'; + case 'J': return b == 'L'; + case 'L': return b == 'J'; + case 'M': return b == 'M'; + case 'O': return b == 'O'; + case 'S': return b == '2'; + case 'T': return b == 'T'; + case 'U': return b == 'U'; + case 'V': return b == 'V'; + case 'W': return b == 'W'; + case 'X': return b == 'X'; + case 'Y': return b == 'Y'; + case 'Z': return b == '5'; + case '1': return b == '1'; + case '2': return b == 'S'; + case '3': return b == 'E'; + case '5': return b == 'Z'; + case '8': return b == '8'; + default: return false; + } +} + +void proc(string & word, bool & pal, bool & mir) { + for (int i = 0, j = word.length() - 1; i <= j; i++, j--) { + if (word[i] != word[j]) pal = false; + if (!reverse(word[i], word[j])) mir = false; + } +} + +int main() { + string word; + while (cin >> word) { + bool pal = true, mir = true; + proc(word, pal, mir); + cout << word << " -- "; + if(pal && mir) cout << "is a mirrored palindrome."; + else if(mir) cout << "is a mirrored string."; + else if(pal) cout << "is a regular palindrome."; + else cout << "is not a palindrome."; + cout << endl << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/401/401-20.cpp b/uva_cpp_clean/401/401-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e63dd30124584443d42410cee2bb329b7626b66 --- /dev/null +++ b/uva_cpp_clean/401/401-20.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + char line[1005]; + + while( cin >> line ) + { + int len = strlen(line); + + bool palindrome_flag; + for(int i=0 ; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) +using namespace std; +int main() +{ + string s,a,b; + char m[3000]; + + memset(m,NULL,sizeof(m)); + m['A']='A'; + m['E']='3'; + m['H']='H'; + m['I']='I'; + m['J']='L'; + m['L']='J'; + m['M']='M'; + m['O']='O'; + m['S']='2'; + m['T']='T'; + m['U']='U'; + m['V']='V'; + m['W']='W'; + m['X']='X'; + m['Y']='Y'; + m['Z']='5'; + m['1']='1'; + m['2']='S'; + m['3']='E'; + m['5']='Z'; + m['8']='8'; + while(cin>>s){ + a=b=""; + int len=s.size(); + for(int i=len-1;i>=0;i--) + { + a+=s[i]; + b+=m[s[i]]; + } + // cout< + +#define SIZE 30 + +using namespace std; + +char s[SIZE]; +bool is_pal, is_mir; +int len; + +bool check_pal(){ + for(int i = 0, j = len - 1; i < j; i++, j--) + if( s[i] != s[j] ) return false; + return true; +} + +bool good(char ch1, char ch2){ + if( ch1 == 'B' or ch1 == 'C' or ch1 == 'D' ) return false; + if( ch1 == 'F' or ch1 == 'G' or ch1 == 'K' ) return false; + if( ch1 == 'N' or ch1 == 'p' or ch1 == 'Q' ) return false; + if( ch1 == 'R' or ch1 == '4' or ch1 == '6' ) return false; + if( ch1 == '7' or ch1 == '9') return false; + if( ch1 == '3' ) return ( ch2 == 'E' ); + if( ch1 == 'E' ) return ( ch2 == '3' ); + if( ch1 == 'J' ) return ( ch2 == 'L' ); + if( ch1 == 'L' ) return ( ch2 == 'J' ); + if( ch1 == '2' ) return ( ch2 == 'S' ); + if( ch1 == 'S' ) return ( ch2 == '2' ); + if( ch1 == '5' ) return ( ch2 == 'Z' ); + if( ch1 == 'Z' ) return ( ch2 == '5' ); + return ( ch1 == ch2 ); +} + +bool check_mir(){ + for(int i = 0, j = len - 1; i <= j; i++, j--) + if( not ( good(s[i], s[j]) ) ) return false; + return true; +} + +int main(){ + while(scanf("%s", s) != EOF){ + len = strlen(s); + is_pal = check_pal(); + is_mir = check_mir(); + if(is_pal){ + if(is_mir) printf("%s -- is a mirrored palindrome.\n\n", s); + else printf("%s -- is a regular palindrome.\n\n", s); + } + else{ + if(is_mir) printf("%s -- is a mirrored string.\n\n", s); + else printf("%s -- is not a palindrome.\n\n", s); + } + } + return(0); +} diff --git a/uva_cpp_clean/406/406-15.cpp b/uva_cpp_clean/406/406-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..006c9248c2a508f284aa3f68209f320c17cee2b0 --- /dev/null +++ b/uva_cpp_clean/406/406-15.cpp @@ -0,0 +1,57 @@ +/* + 區間找質數 + 時間複雜度: O(N) +*/ +#include +#include + +using namespace std; + +vector primes(1, 1); + +void build_prime_array() { + vector isprime(1010, true); + int i, j; + for (i = 2; i < 32; i++) + if (isprime[i]) { + primes.push_back(i); + for (j = i * i; j < 1010; j += i) + isprime[j] = false; + } + for (i = 37; i < 1010; i++) + if (isprime[i]) + primes.push_back(i); +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, n, c, left, right, mid; + build_prime_array(); + + while (cin >> n >> c) { + cout << n << " " << c << ":"; + n++; + right = 0; + + while (primes[right + 1] < n) + right++; + + mid = right / 2; + left = mid; + right = mid + (right & 1); + + // 從中心往外找邊界,注意不能超出[0, n] + while (c--) { + if (left > -1) + left--; + if (primes[right] < n) + right++; + } + + for (i = left + 1; i < right; i++) + cout << " " << primes[i]; + cout << "\n\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/406/406-19.cpp b/uva_cpp_clean/406/406-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef4df3452828ba7fbc7684581881e8f87d2ea19e --- /dev/null +++ b/uva_cpp_clean/406/406-19.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; + +ll sieve_size; +bitset<100000001> bs; +vi primes; + +inline void sieve(ll upperbound = 1010) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +int main() { + primes.push_back(1); + sieve(); + int N, C; + while (cin >> N >> C) { + int ub = upper_bound(primes.begin(), primes.end(), N) - primes.begin(); + int pos = ub/2; + int i = pos - C + (ub%2? 1: 0); + int l = pos + C; + i = max(i, 0); + l = min(l, ub); + printf("%d %d:", N, C); + for (; i < l; i++) printf(" %d", primes[i]); + printf("\n\n"); + } + return 0; +} diff --git a/uva_cpp_clean/406/406-20.cpp b/uva_cpp_clean/406/406-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be9aefed162c5750e232a63989bc7a0110536f06 --- /dev/null +++ b/uva_cpp_clean/406/406-20.cpp @@ -0,0 +1,44 @@ +#include +#include + +const int S = 1005; +bool prime[S]; +int listPrime[S], N, C, length; + +void show_output() +{ + int print = ( length%2==0 ) ? C*2 : (C*2)-1; + + printf("%d %d:", N, C); + if( print>length ) + for(int i=1 ; i<=length ; i++) printf(" %d", listPrime[i]); + else + { + int mid = ( length%2==0 ) ? length/2 : (length/2)+1; + mid -= ( length%2==0 ) ? (print/2)-1 : print/2; + length = print+mid-1; + for(int i=mid ; i<=length ; i++) printf(" %d", listPrime[i]); + } + printf("\n\n"); +} + +int main() +{ + prime[0] = false; + for(int i=1 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 406 + Name: Prime Cuts + Problem: https://onlinejudge.org/external/4/406.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +#define N 10002 +unsigned int prime[N / 64]; +#define gP(n) (prime[n>>6]&(1<<((n>>1)&31))) +void sieve() { + memset(prime, -1, sizeof(prime)); + unsigned int i, i2, sqrtN = (unsigned int)sqrt((double)N) + 1; + for(i = 3; i>6] &= ~(1<<((j>>1)&31)); + } +} + +bool isPrime(int n) { + return n==2 || ((n&1) && gP(n)); +} + + + +int main(){ + sieve(); + vector primes; + primes.push_back(1); + for(int i=2; i<1001; i++) + if (isPrime(i)) + primes.push_back(i); + + int n, c; + while (cin>>n>>c) { + int p = lower_bound(primes.begin(), primes.end(), n) - primes.begin(); + if (primes[p] != n) p--; + int off = ceil((p-2*c+1)/2.0), + cnt = 2*c - (p%2?0:1); + + printf("%d %d:", n, c); + + if (off<=0) { + off=0; + cnt=p+1; + } + + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define st(s) s.size(); +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +using namespace std; + +bool prime[10010]; +void sieve() +{ + // prime[1]=false; + int n=sqrt(10010); + for(int i=2;i<=n;i++) + { + if(prime[i]==true) + { + for(int j=2*i;j<=10010;j=j+i) + { + prime[j]=false; + } + } + } +} +int main() +{ + + cover(prime,true); + sieve(); + int n,c; + while(sc("%d%d",&n,&c)==2) + { + int j=0,save_prime[1010]; + for(int i=1;i<=n;i++) + { + if(prime[i]) + save_prime[++j]=i; + } + //pf("%d\n",save_prime[1]); + int a,b; + if(j%2==1) + { + if(j>2*c-1) + { + a=(j-2*c+1)/2+1; + b=a+2*c-1; + } + else + { + a=1; + b=j+1; + } + } + else + { + if(j>2*c) + { + a=(j-2*c)/2+1; + b=a+2*c; + } + else + { + a=1; + b=j+1; + } + } + cout< + +using namespace std; + +vector get_primes(int n){ + vector small_prime(n + 1), primes; + for(int i = 2 ; i <= n ; ++i){ + if(small_prime[ i ] == 0) + small_prime[ i ] = i, primes.push_back(i); + for(int j = 0 ; j < primes.size() && primes[ j ] <= small_prime[ i ] && i * primes[ j ] <= n ; ++j) + small_prime[ i * primes[ j ] ] = primes[ j ]; + } + primes.insert(primes.begin(), 1); + return primes; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + int n, c; + while(cin >> n >> c){ + vector primes = get_primes(n); + cout << n << ' ' << c << ':'; + for(int low = max(0, (int)(primes.size() + 1) / 2 - c), high = min(primes.size(), primes.size() / 2 + c) ; low < high ; ++low) + cout << ' ' << primes[ low ]; + cout << endl << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/406/406-9.cpp b/uva_cpp_clean/406/406-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b853f6c9ca92539ebb3eb1f610794a8a522210a1 --- /dev/null +++ b/uva_cpp_clean/406/406-9.cpp @@ -0,0 +1,42 @@ +#include + +const long long LIM = 1000; + +using namespace std; + +bitsetprime; +vectorp; +int n,c,l,ll,rr,pib; + +void sieve(){ + p.push_back(1); + p.push_back(2); + for(long long i=3;i>= 1; + ll = l-c+1; + if(ll<0) + ll = 0; + rr = ll+2*c+pib<(int)p.size() && p[ll+2*c+pib]<=n ? ll+2*c+pib : 2*l+1; + while(p[rr]>n || !p[rr]) + rr--; + printf("%d %d:",n,c); + for(int i=ll;i<=rr;i++) + printf(i!=rr ? " %d" : " %d\n",p[i]); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/408/408-11.cpp b/uva_cpp_clean/408/408-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d7fb6c25aff2500709c0db57985857bb0a80fbe --- /dev/null +++ b/uva_cpp_clean/408/408-11.cpp @@ -0,0 +1,70 @@ +#include +/* +Problem: 408 - Uniform Generator +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=349 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int STEP, MOD; + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + + while ( cin >> STEP >> MOD && !cin.eof() ){ + bool good = true; + int last = 0, next = 0; + bool seed[MOD] {0}; // universal zero-initializer.. see resources below + + for (int i = 0; i < MOD; i++){ + next = (last+STEP)%MOD; + + if (seed[next]) {good = false; break;} // if we have seen the number before.. then It's a bad choice + + seed[next] = 1; // we have seen it now.. so index it true :) + last = next; + } + + if (MOD == 0) good = false; // if MOD == 0; the loop won't run.. + // and thus it will be a good chocie.. but that's wrong.. so I hard coded it. + + cout << right << setw(10) << STEP; + cout << right << setw(10) << MOD; + + if (good) cout << setw(17) << "Good Choice\n\n"; + else cout << setw(16) << "Bad Choice\n\n"; + + } + + return 0; +} + +/* +input:- +----------- +3 5 +15 20 +63923 99999 + +output:- +----------- + 3 5 Good Choice + + 15 20 Bad Choice + + 63923 99999 Good Choice + +Resources:- +------------- +std::setw : http://www.cplusplus.com/reference/iomanip/setw/ +std::rigth : http://www.cplusplus.com/reference/ios/right/ +universal zero-initializer :https://stackoverflow.com/questions/1920430/c-array-initialization/1920481 +Video Solution [Eng Yahia Ashraf] (Arabic): https://www.youtube.com/watch?v=VmL4PQIZ-6c +*/ diff --git a/uva_cpp_clean/408/408-18.cpp b/uva_cpp_clean/408/408-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e7bf73fb9c0845ac5adc0c9a864e416e479ce005 --- /dev/null +++ b/uva_cpp_clean/408/408-18.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int main(){ + int step, mod, gen, seed=0; + bool noChoice = false; + while(scanf("%d %d", &step, &mod) == 2){ + vector visited(mod); + seed = 0; + noChoice = false; + for(int i = 0; i < mod; i++){ + gen = (seed + step) % mod; + if(visited[gen]){ + noChoice = true; + break; + } + visited[gen] = true; + seed = gen; + } + if(noChoice) printf("%10d%10d Bad Choice\n\n", step, mod); + else printf("%10d%10d Good Choice\n\n", step, mod); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/408/408-19.cpp b/uva_cpp_clean/408/408-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4a74c57aab50b5529ce1d42d9ff63bfdefab6a3 --- /dev/null +++ b/uva_cpp_clean/408/408-19.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef pair ii; + +ll gcd(ll n, ll m) { + ll auxi; + if (m > n) { + auxi = n; + n = m, m = auxi; + } + while (m != 0) { + auxi = n; + n = m, m = auxi%m; + } + return n; +} + +int main() { + ll step, mod; + while (cin >> step >> mod) { + printf("%10lld%10lld ", step, mod); + step %= mod; + if (gcd(step, mod) == 1) { + printf("Good Choice\n\n"); + } else { + printf("Bad Choice\n\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/408/408-9.cpp b/uva_cpp_clean/408/408-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0100f18e10d9d8e37daca3a5271f227370d9289f --- /dev/null +++ b/uva_cpp_clean/408/408-9.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int STEP, MOD; + +int gcd(int a, int b){ + return b == 0 ? a : gcd(b, a % b); +} + +int main(){ + while(scanf("%d %d", &STEP, &MOD)==2){ + if(gcd(STEP, MOD)==1) + printf("%10d%10d Good Choice\n\n", STEP, MOD); + else + printf("%10d%10d Bad Choice\n\n", STEP, MOD); + } + return(0); +} diff --git a/uva_cpp_clean/409/409-15.cpp b/uva_cpp_clean/409/409-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69c69226c740afe6c37c4942a254f2a4354eb9bc --- /dev/null +++ b/uva_cpp_clean/409/409-15.cpp @@ -0,0 +1,72 @@ +/* + 字串比對 + 時間複雜度: O(N^2) +*/ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +bool compare(const pair &a, + const pair &b) { + return a.second > b.second; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, k, e, count, largest, set = 1; + string input; + regex pattern("[^A-Za-z]"); + unordered_map seen; + vector> excuses; + vector keyword; + + while (cin >> k >> e) { + for (i = 0; i < k; i++) { + cin >> input; + keyword.push_back(input); + } + + cin.ignore(1024, '\n'); // 多的換行 + + for (i = 0; i < e; i++) { + count = 0; + getline(cin, input); + excuses.push_back(make_pair(input, 0)); + + transform(input.begin(), input.end(), input.begin(), ::tolower); // 換成全部小寫 + istringstream word(regex_replace(input, pattern, " ")); // 把全部不是英文換成空白 + + while (word >> input) + seen[input] += 1; + + for (const string &j : keyword) + count += seen[j]; + + excuses[i].second = count; + seen.clear(); + } + + sort(excuses.begin(), excuses.end(), compare); // 按照藉口次數大到小排序 + largest = excuses[0].second; + + cout << "Excuse Set #" << set++ << "\n"; + + for (const auto &excuse : excuses) { + if (excuse.second != largest) + break; + cout << excuse.first << "\n"; + } + cout << "\n"; + excuses.clear(); + keyword.clear(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/409/409-18.cpp b/uva_cpp_clean/409/409-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b1e6e0c25579de341dabd9425ef794e82ef9c36c --- /dev/null +++ b/uva_cpp_clean/409/409-18.cpp @@ -0,0 +1,61 @@ +#include +#include +#include + +using namespace std; + +string toLower(string word){ + for(int i = 0; word[i] > 0; i++){ + if (word[i] >= 'A' && word[i] <= 'Z') word[i] += 32; + } + return word; +} + +int incidence(string sentence, string *keywords, int K){ + int inci = 0; + for(int i = 0; i < K; i++){ + int index = sentence.find(keywords[i]); + int length = keywords[i].length(); + while(index < sentence.length()){ + if(index + length < sentence.length()){ + if(!isalpha(sentence[index + length])) inci++; + } else if(index + length == sentence.length()){ + inci++; + break; + } + index = sentence.find(keywords[i], index + 1); + } + } + return inci; +} + +int main(){ + int K, E, max, set = 0; + while(cin >> K >> E){ + string *keywords = new string[K]; + max = 0; + vector excuses; + string sentence, lsentence; + getline(cin,sentence); + for(int i = 0; i < K; i++){ + getline(cin, keywords[i]); + keywords[i] = toLower(keywords[i]); + } + for(int i = 0; i < E; i++){ + getline(cin, sentence); + lsentence = toLower(sentence); + int inc = incidence(lsentence, keywords, K); + if(inc > max){ + excuses.clear(); + max = inc; + excuses.push_back(sentence); + }else if(inc == max) excuses.push_back(sentence); + } + set++; + cout << "Excuse Set #" << set << endl; + for(int i = 0; i < excuses.size(); i++) cout << excuses[i] << endl; + cout << endl; + delete[] keywords; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/409/409-19.cpp b/uva_cpp_clean/409/409-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88c04a5821dc7a31ba94e066aa00fabe4be97900 --- /dev/null +++ b/uva_cpp_clean/409/409-19.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +bool match(char c) { + return !isalnum(c) && !isspace(c); +} + +char func(char c) { + if (match(c)) return ' '; + return c; +} + +int main() { + int K, E, set = 1; + string s; + unordered_set l; + while (cin >> K >> E) { + l.clear(); + for (int i = 0; i < K; i++) { + cin >> s; + l.insert(s); + } + int m = 0; + string output = ""; + getline(cin, s); + for (int i = 0; i < E; i++) { + int cont = 0; + getline(cin, s); + string aux = s; + transform(aux.begin(), aux.end(), aux.begin(), ::tolower); + transform(aux.begin(), aux.end(), aux.begin(), &func); + //aux.erase(remove_if(aux.begin(), aux.end(), &match), s.end()); + stringstream ss; + ss << aux; + while (ss >> aux) { + if (l.count(aux)) cont++; + } + if (cont == m) output += "\n" + s; + else if (cont > m) output = s, m = cont; + } + cout << "Excuse Set #" << set << "\n" << output << endl << endl; + set++; + } + return 0; +} diff --git a/uva_cpp_clean/409/409-21.cpp b/uva_cpp_clean/409/409-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e488d3abc02eeac6a2ed3444afdde9aa6ad678c2 --- /dev/null +++ b/uva_cpp_clean/409/409-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 409 + Name: Excuses, Excuses! + Problem: https://onlinejudge.org/external/4/409.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +const char tokens[] = " \t\"'.,:;~`!?@#$%^&*()<>/\\0123456789"; +char tolower(char c) { + if (c>='A' && c<='Z') + return c-'A'+'a'; + return c; +} + +int main(){ + int cse=1, n, m, scores[30]; + char lines[30][100], word[100]; + + while (cin>>n>>m) { + map key; + + for (int i=0; i>word; + key[word] = 1; + } + cin.getline(word, 100); + + int j, maxscore = -1; + for (int i=0; imaxscore) + maxscore = scores[i]; + } + + printf("Excuse Set #%d\n", cse++); + for (int i=0; i + +using namespace std; + +const int SIZE = 30, LEN = 80; + +int tc, n, m, ct[SIZE], mx; +set keys; +string s; +char key[LEN], line[SIZE][LEN]; + +int main(){ + while(scanf("%d %d\n", &n, &m) != EOF){ + keys.clear(), mx = 0; + for(int it = 0; it < n; it++){ + scanf("%s\n", key); + keys.insert(key); + } + for(int it = 0; it < m; it++){ + ct[it] = 0; + cin.getline(line[it], LEN); + for(int i = 0; line[it][i]; i++){ + if( isalpha(line[it][i]) ){ + s = tolower(line[it][i]); + for(i++; line[it][i] and isalpha(line[it][i]); i++) + s += tolower(line[it][i]); + if( keys.find(s) != keys.end() ) + ct[it]++; + } + } + mx = max(mx, ct[it]); + } + printf("Excuse Set #%d\n", ++tc); + for(int it = 0; it < m; it++) + if( ct[it] == mx ) printf("%s\n", line[it]); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/410/410-13.cpp b/uva_cpp_clean/410/410-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a0e4eaaff6d55fecf98e73eea98cca6aec4e14a --- /dev/null +++ b/uva_cpp_clean/410/410-13.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + bool first = false; + + int c, s; + int counter = 1; + + while (cin >> c >> s) + { + double avg = 0, imp = 0; + + double b[25][2]; + double* a = new double[2 * c]; + + for (int i = 0; i < 25; i++) + { + b[i][0] = b[i][1] = -1; + } + + for (int i = 0; i < c * 2; i++) + { + a[i] = 0; + } + + for (int i = 0; i < s; i++) + { + cin >> a[i]; + + avg += a[i]; + + b[i][0] = a[i]; + } + + avg /= c; + + sort(a, a + (2 * c)); + + vector< pair < double, double > > box; + + for (int i = 0; i < c; i++) + { + box.push_back(make_pair(a[i], a[(2 * c) - i - 1])); + + imp += (fabs(a[i] + a[(2 * c) - i - 1] - avg)); + } + + int wh = 0; + + for (int i = 0; i < 2 * c; i++) + { + double x = b[i][0]; + + for (int j = 0; j < box.size(); j++) + { + double first = 0, second = 0; + + first = box[j].first; + + second = box[j].second; + + if (x == first || x == second) + { + box.erase(box.begin() + j); + + for (int k = 0; k < 2 * c; k++) + { + if (b[k][0] == first && b[k][1] == -1) + { + b[k][1] = wh; + + break; + } + } + + for (int k = 0; k < 2 * c; k++) + { + if (b[k][0] == second && b[k][1] == -1) + { + b[k][1] = wh; + + break; + } + } + + wh++; + + break; + } + } + } + + cout << "Set #" << counter << endl; + + for (int i = 0; i < c; i++) + { + printf("%2d:", i); + + for (int j = 0; j < 2 * c; j++) + { + if (b[j][1] == i) + { + cout << " " << b[j][0]; + } + } + + cout << endl; + } + + printf("IMBALANCE = %.5lf\n\n", imp); + + counter++; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/410/410-21.cpp b/uva_cpp_clean/410/410-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b44ac11291cf8510bd47590d581dfc0dae69b51 --- /dev/null +++ b/uva_cpp_clean/410/410-21.cpp @@ -0,0 +1,90 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 410 + Name: Station Balance + Problem: https://onlinejudge.org/external/4/410.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define setbit(n,i) ((n)|(1<<(i))) +#define resetbit(n,i) ((n)&(~(1<<(i)))) +#define isset(n,i) ((n)&(1<<(i))) + +double am; +int sm[20],s; +double saved[2048][6]; +int P[2048][6]; + +double rec(int set, int cnt){ + if(cnt==0) + if(set==0) + return 0; + else + return 10000000; + + if(saved[set][cnt]>=0)return saved[set][cnt]; + + int i,j; + double t,m=rec(set,cnt-1)+am; + P[set][cnt]=0; + + for(i=0;i>sm[i]; + am+=sm[i]; + } + am/=c; + + im=rec((1<0; cnt--) + { + printf("%2d:",c-cnt); + for(i=0;i + +using namespace std; + +int c, s, t, tc, pib, foo; +vector < pair > v; +int ans[10][2]; +double mean, imbalance; + +int main(){ + while(scanf("%d %d", &c, &s) == 2){ + v.resize(s); + mean = imbalance = t = 0; + for(int i = 0; i < 10; i++) + ans[i][0] = ans[i][1] = 0; + for(int i = 0; i < s; i++){ + scanf("%d", &v[i].first), v[i].second = i; + mean += v[i].first; + } + mean /= c; + printf("Set #%d\n", ++tc); + sort(v.begin(), v.end()); + pib = v.size() - 1; + pib -= 2 * c - s; + if(c <= s){ + for(int i = v.size() - 1; i > pib; i--) + ans[v[i].second][0] = v[i].first; + } + else{ + for(int i = 0; i < s; i++) + ans[v[i].second][0] = v[i].first; + } + for(int i = 0; i < pib; i++){ + foo = min(v[i].second, v[pib].second); + ans[foo][0] = v[i].first; + ans[foo][1] = v[pib].first; + if(v[i].second > v[pib].second) + swap(ans[foo][0], ans[foo][1]); + pib--; + } + for(int i = 0; i < 10; i++){ + if(ans[i][0] && ans[i][1]){ + printf(" %d: %d %d\n", t++, ans[i][0], ans[i][1]); + imbalance += abs(ans[i][0] + ans[i][1] - mean); + } + else if(ans[i][0] || ans[i][1]){ + printf(" %d: %d\n", t++, ans[i][0]); + imbalance += abs(ans[i][0] - mean); + } + } + for(int i = t; i < c; i++){ + printf(" %d:\n", i); + imbalance += mean; + } + printf("IMBALANCE = %.5lf\n", imbalance); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/412/412-14.cpp b/uva_cpp_clean/412/412-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6d34fb84e35aaccd1bead7e400c4a1522f440ca --- /dev/null +++ b/uva_cpp_clean/412/412-14.cpp @@ -0,0 +1,81 @@ +/*************************************************** + * Problem name : 412 Pi.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/412.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-26 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 51 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int n; + int ar[MAX]; + while (scanf("%d", &n ) == 1 && n != 0) { + int total = 0, pair = 0; + for (int i = 0; i < n; i++) { + scanf("%d", &ar[i]); + } + for (int i = 0; i < n - 1; i++) { + for (int j = i + 1; j < n; j++) { + total++; + if (__gcd(ar[i], ar[j]) == 1) { + pair++; + } + } + } + //~ printf("%d %d\n", total, pair); + if (!pair) printf("No estimate for this data set.\n"); + else { + double pi = sqrt((6.0 * double(total)) / double(pair)); + printf("%.6lf\n", pi); + } + } + return 0; +} + diff --git a/uva_cpp_clean/412/412-21.cpp b/uva_cpp_clean/412/412-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ceac2010b1748c0fa1f7a2a208e00ccd873d7f9f --- /dev/null +++ b/uva_cpp_clean/412/412-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 412 + Name: Pi + Problem: https://onlinejudge.org/external/4/412.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int gcd(int x,int y){ + int c,a=x,b=y; + if(a>n && n) + { + for(i=0;i>a[i]; + sum=0; + t=n*(n-1)/2; + for(i=0;i + +#define SIZE 60 + +using namespace std; + +int N, v[SIZE], num, den; + +int gcd(int a, int b){ + return b == 0 ? a : gcd(b, a % b); +} + +int main(){ + while(scanf("%d", &N), N){ + for(int it = 0; it < N; it++) scanf("%d", &v[it]); + den = num = 0; + for(int it1 = 0; it1 < N; it1++) + for(int it2 = it1 + 1; it2 < N; it2++){ + den++; + if(gcd(v[it1], v[it2]) == 1) num++; + } + if(num==0) puts("No estimate for this data set."); + else printf("%.6f\n", sqrt( 6 * den * 1.0 / num)); + } + return(0); +} diff --git a/uva_cpp_clean/413/413-21.cpp b/uva_cpp_clean/413/413-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75729dd655dc242798e9ede18b17d3a2c839ee01 --- /dev/null +++ b/uva_cpp_clean/413/413-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 413 + Name: Up and Down Sequences + Problem: https://onlinejudge.org/external/4/413.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + + +double savg(const vector &v, function cmp) { + int sum = 0, + cnt = 0, + l = 0, + g = 0; + + for (int i=1; i v; + while (cin >> x && x) + v.push_back(x); + if (v.empty()) break; + + cout << "Nr values = " << v.size() + << ": " << savg(v, greater()) + << ' ' << savg(v, less()) + << endl; + } +} diff --git a/uva_cpp_clean/414/414-11.cpp b/uva_cpp_clean/414/414-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e28373795f74813e525808a0f0f31c4fa5a00b2b --- /dev/null +++ b/uva_cpp_clean/414/414-11.cpp @@ -0,0 +1,78 @@ +#include +/* +Problem: 414 - Machined Surfaces +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=355 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +string line; +int xcount[14]; +int spaces = 0, max_x_count = 0, N = 0; + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + + while ( cin >> N && !cin.eof() && N != 0 ){ + cin.ignore(); + + spaces = 0; + max_x_count = 0; + for (int i = 0; i < N; i++){ + xcount[i] = 0; + getline(cin, line); + + for (int j = 0; j < 25; j++){ + if (line[j] == 'X') xcount[i]++; + } + + max_x_count = max(max_x_count, xcount[i]); + } + + for (int i = 0; i < N; i++){ + spaces += (max_x_count-xcount[i]); + } + + cout << spaces << '\n'; + + } + + return 0; +} +/* +input:- +----------- +4 +XXXX XXXXX +XXX XXXXXXX +XXXXX XXXX +XX XXXXXX +2 +XXXXXXXXXXXXXXXXXXXXXXXXX +XXXXXXXXXXXXXXXXXXXXXXXXX +1 +XXXXXXXXX XX +0 + +output:- +----------- +4 +0 +0 + + +Resources:- +------------- +https://stackoverflow.com/questions/18786575/using-getline-in-c +https://stackoverflow.com/questions/20970780/getline-cin-ignore-in-c +https://www.geeksforgeeks.org/getline-string-c/ + +Code on ideone.com: https://ideone.com/VV06BD +*/ diff --git a/uva_cpp_clean/414/414-14.cpp b/uva_cpp_clean/414/414-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76e593331a86f5c01053a1042911c40361c64323 --- /dev/null +++ b/uva_cpp_clean/414/414-14.cpp @@ -0,0 +1,158 @@ +/*************************************************** + * Problem Name : 414Machined Surfaces.cpp + * Problem Link : https://onlinejudge.org/external/4/414.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-10-12 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ __FastIO; + //~ cout << setprecision (10) << fixed; + int r; + + while (cin >> r) { + if (r == 0) break; + + string str[r]; + int c = 25; + cin.ignore(); + for (int i = 0; i < r; i++) { + getline(cin, str[i]); + } + + int b_cnt[r + 3]; + int b_mn = 27; + + for (int i = 0; i < r; i++) { + int cnt = 0; + + for (int j = 0; j < c; j++) { + if (str[i][j] != 'X') cnt++; + } + + b_cnt[i] = cnt; + b_mn = min (b_mn, cnt); + } + + int res = 0; + + for (int i = 0; i < r; i++) { + res += (b_cnt[i] - b_mn); + } + + cout << res << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/414/414-16.cpp b/uva_cpp_clean/414/414-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d85a9f35d9fd0bc01ee92fa9194e67abda5a7c82 --- /dev/null +++ b/uva_cpp_clean/414/414-16.cpp @@ -0,0 +1,41 @@ +//============================================================================ +// Name : 414 - Machined Surfaces.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 414 - Machined Surfaces in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include +#include + +using namespace std; + +#define FRI(i, a, b) for(i = a; i < (int)b; ++i) + +int main() { + int i; + int n, min, answer; + int blank[15]; + string line; + + while (cin >> n, n != 0) { + getline(cin, line); + min = 25; + FRI(i, 0, n) { + getline(cin, line); + blank[i] = count(line.begin(), line.end(), ' '); + if (min > blank[i]) min = blank[i]; + } + + answer = 0; + FRI(i, 0, n) { + blank[i] -= min; + answer += blank[i]; + } + + cout << answer << endl; + } + return 0; +} diff --git a/uva_cpp_clean/414/414-18.cpp b/uva_cpp_clean/414/414-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0d431fd3e0d3bb3f1bc2ed4bc89f6bc304cf06a1 --- /dev/null +++ b/uva_cpp_clean/414/414-18.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + +int main(){ + int N, maxX, auxRow, sum; + while(true){ + cin >> N; + int ansArray[N]; + string digitalImg; + getline(cin, digitalImg); + if(N == 0) break; + maxX = 0; + sum = 0; + for(int i = 0; i < N; i++){ + auxRow = 0; + getline(cin, digitalImg); + for(int j = 0; j < digitalImg.length(); j++){ + if(digitalImg[j] == 'X') auxRow++; + } + ansArray[i] = auxRow; + maxX = max(maxX, auxRow); + } + for(int i = 0; i < N; i++) sum = sum + (maxX - ansArray[i]); + cout << sum << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/414/414-21.cpp b/uva_cpp_clean/414/414-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09a49c0c3ba093719477e3aad9c59b50f776d10f --- /dev/null +++ b/uva_cpp_clean/414/414-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 414 + Name: Machined Surfaces + Problem: https://onlinejudge.org/external/4/414.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int n; + char line[50]; + while(cin>>n && n) { + cin.getline(line, 50); + + int ms = 100, total = 0; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 416 + Name: LED Test + Problem: https://onlinejudge.org/external/4/416.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define sub(a, b) ((a) & ~(b)) + +int numbers[] = { 126, 48, 109, 121, 51, 91, 95, 112, 127, 123 }; + +bool areMatch(int inps[], int n, int s) { + int burned = 0; + for (int i = n - 1; i >= 0; i--) { + int g = sub(numbers[s + i], burned); + + if ((inps[i] & g) != inps[i]) + return false; + + burned |= sub(g, inps[i]); + } + + return true; +} + + +bool isMatch(int inps[], int n) { + for (int i = 0; i < 10 - n + 1; i++) + if (areMatch(inps, n, i)) + return true; + + return false; +} + + +int main() { + int n; + char num[8]; + + while (cin >> n && n) { + int inps[10]; + + for (int k = 0; k < n; k++) { + while (getchar() != '\n'); + + int x = 0; + for (int i = 0; i < 7; i++) + x = (x << 1) | (getchar() == 'Y'); + + inps[n - k - 1] = x; + } + + + if (!isMatch(inps, n)) + cout << "MIS"; + cout << "MATCH\n"; + } +} diff --git a/uva_cpp_clean/417/417-11.cpp b/uva_cpp_clean/417/417-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99290a72013a62bb24632cb62e1ff1079c69ea04 --- /dev/null +++ b/uva_cpp_clean/417/417-11.cpp @@ -0,0 +1,90 @@ +#include +/* +Problem: 417 - Word Index +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=358 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +#define endl '\n' + +using namespace std; + +string word, ordered_word; +string lowercase = "abcdefghijklmnopqrstuvwxyz"; + +map ordered_dict, M; +map ::iterator it; + +void init_map(); + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + + init_map();// generate all the possible words and store each order + while ( cin >> word && !cin.eof() ){ + ordered_word = word; + + it = ordered_dict.find(word); + + if ( it == ordered_dict.end() ) cout << 0 << endl;// if we can't find the word in the dictionary.. then it is wrong formatted + else cout << it->second << endl; + + } + + return 0; +} + +void init_map(){ + // initialize the main dictionary which contains all possible words and their order + // The function can also be written as five for loops.. think about it.. we are Brute-forcing anyway :"D + queue left; + string word = ""; + + int count = 1, idx = 1; + // we push 'ab..z' first to the queue + for (int i = 0; i < 26; i++){ + left.push(string(1, lowercase[i])); + } + + // we pushed 'a..z'..Now once we get to b.. we need to push 'bc..z', and so on.. + // The idea of the queue is to organize the words as we go + while (!left.empty()){ + if (word.length() > 5) break; + word = left.front(); left.pop(); + + ordered_dict[word] = count; count++; + + idx = word[word.length()-1]-'a'; + for (int i = idx+1; i < 26; i++) + left.push(word+lowercase[i]); + } +} + +/* +Sample input:- +----------------- +z +a +cat +vwxyz + +Sample output:- +----------------- +26 +1 +0 +83681 + +Resources:- +------------- +http://www.cplusplus.com/reference/map/map/ +http://www.cplusplus.com/reference/queue/queue/ +https://stackoverflow.com/questions/17201590/c-convert-from-1-char-to-string + +*/ diff --git a/uva_cpp_clean/417/417-14.cpp b/uva_cpp_clean/417/417-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0de4c4951e4e881abc50ae13e491079d0cbd698 --- /dev/null +++ b/uva_cpp_clean/417/417-14.cpp @@ -0,0 +1,163 @@ +/*************************************************** + * Problem Name : 417 Word Index.cpp + * Problem Link : https://onlinejudge.org/external/4/417.pdf + * OJ : Uva + * Verdict : AC + * Date : 2020-10-16 + * Problem Type : STL + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.rbegin(), v.rend()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define un_map unordered_map +#define un_set unordered_set +#define pb push_back +#define mk make_pair +#define F first +#define S second +#define MAX 100005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +struct custom_hash { + static uint64_t splitmix64 (uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30) ) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27) ) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator() (uint64_t x) const { + static const uint64_t FIXED_RANDOM = + chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64 (x + FIXED_RANDOM); + } +}; +ll lcm ( ll a, ll b ) { + return ( a / __gcd ( a, b ) ) * b; +} +/************************************ Code Start Here ******************************************************/ +mapmp; + +void pre_cal() { + queueq; + + for (char ch = 'a'; ch <= 'z'; ch++) { + q.push (string (1, ch) ); + } + + string str; + int cnt = 1; + + while (!q.empty() ) { + str = q.front(); + q.pop(); + mp[str] = cnt++; + + if ( (int) str.size() >= 5) continue; + + char st = str[ (int) str.size() - 1] + 1; + + for (char ch = st; ch <= 'z'; ch++) { + q.push (str + ch); + } + } +} + + +int main () { + __FastIO; + //~ cout << setprecision (10) << fixed; + pre_cal(); + string str; + + while (cin >> str) { + if (mp.find (str) == mp.end() ) { + cout << 0 << "\n"; + + } else { + cout << mp[str] << "\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/417/417-16.cpp b/uva_cpp_clean/417/417-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a846d0e07c17b1e90096503ae9d16ebb90a41235 --- /dev/null +++ b/uva_cpp_clean/417/417-16.cpp @@ -0,0 +1,115 @@ +//============================================================================ +// Name : 417 - Word Index.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 417 - Word Index in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include + +using namespace std; + +#define FRI(i, a, b) for(i = a; i < b; ++i) + +int main() { + int i, j; + int index, length, countX, countY; + int mapper[30][30] = {0}; + char c, startC; + string word; + + FRI (i, 0, 27) { + mapper[i][0] = 1; + FRI(j, 0, i) { + mapper[i][j+1] = mapper[i-1][j] + mapper[i-1][j+1]; + } + } + + while(getline(cin, word)) { + index = 1; + length = word.length(); + + FRI(i, 1, length) { + if(word[i] <= word[i - 1]) { + break; + } + } + + if (i != length) { + cout << 0 << endl; + continue; + } + + FRI(i, 0, length) { + if (i == 0) { + startC = 'a'; + } else { + startC = word[i-1] + 1; + index += mapper[26][i]; + } + + countX = length - i - 1; + FRI(c, startC, word[i]){ + countY = 'z' - c; + index += mapper[countY][countX]; + } + } + + cout << index << endl; + } + + return 0; +} + +//============================================================================ +// Name : 417 - Word Index.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 417 - Word Index in C++, Ansi-style +// Run Time : 0.010 seconds +//============================================================================ + +// #include +// #include +// #include +// #include +// #include + +// using namespace std; + +// #define FRI(i, a, b) for(i = a; i <= b; ++i) + +// int main() { +// int index = 1; +// char c, startC; +// string word, newWord; +// queue wordQueue; +// map wordIndex; +// map::iterator it; + +// wordQueue.push(""); + +// while(!wordQueue.empty()) { +// word = wordQueue.front(); +// wordQueue.pop(); +// startC = word.back() + 1; +// if (startC == 1) startC = 'a'; +// FRI(c, startC, 'z') { +// newWord = word + c; +// wordIndex[newWord] = index++; + +// if (newWord.length() == 5) continue; +// wordQueue.push(newWord); +// } +// } + +// while(getline(cin, word)) { +// it = wordIndex.find(word); +// if (it != wordIndex.end()) cout << it->second << endl; +// else cout << 0 << endl; +// } + +// return 0; +// } diff --git a/uva_cpp_clean/417/417-19.cpp b/uva_cpp_clean/417/417-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d14d703d29ab211bea2a83b4b769142af4e4c33d --- /dev/null +++ b/uva_cpp_clean/417/417-19.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; + +unordered_map map; +char aux[5]; + +void proc(int level, char start, int limit) { + for (int i = start + 1; i <= 'z'; i++) { + aux[level] = i; + if (level == limit) map.insert(make_pair(string(aux), map.size()+1)); + else proc(level + 1, i, limit); + } +} + +int main() { + for (int i = 0; i <= 4; i++) { + aux[i] = 0; + proc(0, 'a'-1, i); + } + string w; + while (cin >> w) { + if (map.count(w)) printf("%d\n", map[w]); + else printf("0\n"); + } + return 0; +} diff --git a/uva_cpp_clean/417/417-21.cpp b/uva_cpp_clean/417/417-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6255bc576b66f0fda48a1071b5d84f9c86963e80 --- /dev/null +++ b/uva_cpp_clean/417/417-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 417 + Name: Word Index + Problem: https://onlinejudge.org/external/4/417.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +map encodes; + +int cur; +char curw[10]; +void rec(int l, int ind) { + if (ind == l) encodes[curw] = ++cur; + else { + char last = ind==0 ? 'a'-1 : curw[ind-1]; + for (char ch=last+1; ch<='z'; ch++) { + curw[ind] = ch; + rec(l, ind+1); + } + } +} + +int main(){ + for (int i=1; i<=5; i++) { + curw[i] = 0; + rec(i, 0); + } + + + char word[20]; + while(cin>>word) + cout << encodes[word] << endl; +} diff --git a/uva_cpp_clean/417/417-5.cpp b/uva_cpp_clean/417/417-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..939e16254ce55d8a8e86deae7d642056692a0ee5 --- /dev/null +++ b/uva_cpp_clean/417/417-5.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +using namespace std; + +string a="abcdefghijklmnopqrstuvwxyz"; +mapmp; +void alim() +{ + int i,j,k,m,n; + int p=1; + string s,s1,s2,s3,s4,s5; + for(i=0;i<26;i++) + { + s=a[i]; + mp[s]=p; + p++; + } + for(i=0;i<26;i++) + { + for(j=i+1;j<26;j++) + { + s1=a[i]; + s2=a[j]; + s=s1+s2; + mp[s]=p; + p++; + } + } + for(i=0;i<26;i++) + { + for(j=i+1;j<26;j++) + { + for(k=j+1;k<26;k++) + { + s1=a[i]; + s2=a[j]; + s3=a[k]; + s=s1+s2+s3; + mp[s]=p; + p++; + } + } + } + for(i=0;i<26;i++) + { + for(j=i+1;j<26;j++) + { + for(k=j+1;k<26;k++) + { + for(m=k+1;m<26;m++) + { + s1=a[i]; + s2=a[j]; + s3=a[k]; + s4=a[m]; + s=s1+s2+s3+s4; + mp[s]=p; + p++; + } + } + } + } + for(i=0;i<26;i++) + { + for(j=i+1;j<26;j++) + { + for(k=j+1;k<26;k++) + { + for(m=k+1;m<26;m++) + { + for(n=m+1;n<26;n++) + { + s1=a[i]; + s2=a[j]; + s3=a[k]; + s4=a[m]; + s5=a[n]; + s=s1+s2+s3+s4+s5; + mp[s]=p; + p++; + } + } + } + } + } + +} +int main() +{ + char s[20]; + alim(); + while(scanf("%s",s)==1) + { + printf("%d\n",mp[s]); + } + return 0; +} diff --git a/uva_cpp_clean/422/422-19.cpp b/uva_cpp_clean/422/422-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5a217bb919071f95d34b8a98e45320ad3149aa3 --- /dev/null +++ b/uva_cpp_clean/422/422-19.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; + +string grid[101]; + +bool inside(int x, int y, int m, int n) { + return x < m && x >= 0 && y < n && y >= 0; +} + +bool search(const string & word, int x, int y, int pos, const int & incrx, const int & incry, const int & m, const int & n, pair & last) { + if(word[pos] == grid[x][y]) { + if (pos < word.length()-1) { + int found = false; + if (inside(x+incrx, y+incry, m, n)) found = search(word, x+incrx, y+incry, pos+1, incrx, incry, m, n, last); + return found; + } else { + last.first = x; + last.second = y; + return true; + } + } else return false; +} + +int main() { + int m; + cin >> m; + for (int i = 0; i < m; i++) cin >> grid[i]; + string word; + pair last; + while (cin >> word && word != "0") { + bool found = false; + for (int x = 0; x < m && !found; x++) { + for (int y = 0; y < m && !found; y++) { + for (int incrx = -1; incrx <= 1 && !found; incrx++) { + for (int incry = -1; incry <= 1 && !found; incry++) { + if (incrx == 0 && incry == 0) continue; + found = search(word, x, y, 0, incrx, incry, m, m, last); + if (found) printf("%d,%d %d,%d\n", x+1, y+1, last.first+1, last.second+1); + } + } + } + } + if (!found) printf("Not found\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/422/422-21.cpp b/uva_cpp_clean/422/422-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ae9532c7274043bf46f02012da8c8099bb449c9 --- /dev/null +++ b/uva_cpp_clean/422/422-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 422 + Name: Word-Search Wonder + Problem: https://onlinejudge.org/external/4/422.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + cin >> n; + + char X[143][143]; + for (int i=0; i> X[i]; + + string s; + while (cin >> s && s != "0") { + int l = s.length(); + for (int i=0; i0 || dj) { + int ei = i + (l-1) * di, + ej = j + (l-1) * dj; + + if (ei>=0 && ei=0 && ej + +using namespace std; + +int n, len; +vector grid; +string s; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool isValid (const int r, const int c) { + return 0 <= r and r < n and 0 <= c and c < n; +} + +bool check (int r, int c, int pos, const int dir) { + if (pos == len or grid[r][c] != s[pos]) return false; + if (pos == len - 1) return true; + int nr = r + dr[dir]; + int nc = c + dc[dir]; + if (not isValid(nr, nc)) return false; + return check(nr, nc, pos + 1, dir); +} + +void searchInGrid () { + len = s.size(); + for (int r = 0; r < n; r++) + for (int c = 0; c < n; c++) + for (int d = 0; d < 8; d++) + if (check(r, c, 0, d)) { + len--; + cout << r + 1 << ',' << c + 1 << ' '; + cout << r + 1 + dr[d] * len << ',' << c + 1 + dc[d] * len << endl; + return; + } + puts("Not found"); +} + +void processQueries () { + while (cin >> s, s[0] != '0') searchInGrid(); +} + +void readGrid () { + cin >> n; + grid.resize(n); + for (int r = 0; r < n; r++) { + cin >> grid[r]; + } +} + +int main () { + readGrid(); + processQueries(); + return (0); +} diff --git a/uva_cpp_clean/423/423-16.cpp b/uva_cpp_clean/423/423-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4dd0b3db74c4ebd6e9e3c70b46f9b81790151e1 --- /dev/null +++ b/uva_cpp_clean/423/423-16.cpp @@ -0,0 +1,80 @@ +//============================================================================ +// Name : 423 - MPI Maelstrom.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 423 - MPI Maelstrom in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include + +using namespace std; + +#define FRI(i, a, b) for(i = a; i < b; ++i) +#define INF 100000 + +int main() { + int i, j, k; + int n, m; + int min, to, answer; + int a[105][105] = {0}; + int pos[105] = {0}; + + cin >> n; + FRI(i, 0, n) { + FRI(j, 0, i) { + cin >> m; + if (cin.good()) { + a[i][j] = m; + a[j][i] = m; + } else { + a[i][j] = INF; + a[j][i] = INF; + cin.clear(); + cin.ignore(); + } + } + a[i][i] = INF; + pos[i] = INF; + } + + pos[0] = 0; + answer = 0; + FRI(i, 1, n) { + min = INF; + FRI(j, 0, n) { + if (pos[j] == INF) continue; + + FRI(k, 0, n) { + if (pos[k] < INF) continue; + + if (a[j][k] < min) { + min = a[j][k]; + to = k; + } + } + } + + FRI(j, 0, n) { + if (pos[j] == INF) continue; + + FRI(k, 0, n) { + if (a[j][k] == INF || a[j][k] == 0) continue; + a[j][k] -= min; + a[k][j] -= min; + if (a[j][k] < 0) { + a[j][k] = 0; + a[k][j] = 0; + } + } + } + + pos[to] = min; + answer += min; + } + + cout << answer << endl; + + return 0; +} diff --git a/uva_cpp_clean/423/423-21.cpp b/uva_cpp_clean/423/423-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78f963379fd39ff40a681be5b7a0419c216b42d8 --- /dev/null +++ b/uva_cpp_clean/423/423-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 423 + Name: MPI Maelstrom + Problem: https://onlinejudge.org/external/4/423.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct Edge { + int u, v, d; + Edge(int u, int v, int d): u(u),v(v),d(d) {} +}; + +int main(){ + int n, x, d[101]; + + while (cin>>n) { + list eds; + + for (int i=0; i> y; + if (y[0] != 'x') { + sscanf(y, "%d", &x); + eds.push_back(Edge(i, j, x)); + eds.push_back(Edge(j, i, x)); + } + } + } + + d[0]=0; + for (int i=1; i mx) + mx = d[i]; + + cout << mx << endl; + } +} diff --git a/uva_cpp_clean/424/424-14.cpp b/uva_cpp_clean/424/424-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe71423ffde4ba450ddff5d80fcfe29180116ed3 --- /dev/null +++ b/uva_cpp_clean/424/424-14.cpp @@ -0,0 +1,131 @@ +/*************************************************** + * Problem name : 424 Integer Inquiry.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/424.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-23 + * Problem Type : string sum + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 103 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +string add(string a, string b) { + int al = a.size() - 1; + int bl = b.size() - 1; + + int carry = 0; + string result = ""; + + while (al >= 0 && bl >= 0) { + int temp = (int)(a[al] - '0') + (int)(b[bl] - '0') + carry ; + carry = 0; + if (temp > 9 ) { + carry = 1; + temp = temp - 10; + } + result += char(temp + '0'); + al--; + bl--; + } + while (al >= 0) { + int temp = (int)(a[al] - '0') + carry ; + carry = 0; + if (temp > 9) { + carry = 1; + temp = temp % 10; + } + + result += char(temp + '0'); + al--; + } + while (bl >= 0) { + int temp = (int)(b[bl] - '0') + carry ; + carry = 0; + if (temp > 9) { + carry = 1; + temp = temp % 10; + } + result += char(temp + '0'); + bl--; + } + if (carry) { + result += "1"; + } + + string addition = ""; + + //~ for (int i = result.size() - 1; i >= 0; i--) { + //~ addition += result[i]; // reversing the answer + //~ } + //~ return addition; + reverse(result.begin(), result.end()); + return result; +} +string trim(string a) { // for removing leading 0s + string res = ""; + int i = 0; + + while (a[i] == '0'){ + i++; + } + int sz = a.size(); + for (; i < sz; i++){ + res += a[i]; + } + return res; +} +int main () { + string s, ans=""; + while(cin>>s){ + if(s[0] == '0' && s.size() == 1) break; + ans = trim(add(ans, s)); + } + cout << ans; + nl; + return 0; +} + diff --git a/uva_cpp_clean/424/424-20.cpp b/uva_cpp_clean/424/424-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..042b6cf6cc0e6eccc42c6cbbf9014dc36a8b8fd5 --- /dev/null +++ b/uva_cpp_clean/424/424-20.cpp @@ -0,0 +1,36 @@ +#include +#include + +const int S = 105; +char VeryLongInteger[S]; +int temp[S], sum[S]; + +int main() +{ + while( scanf("%s", VeryLongInteger)==1 && strcmp(VeryLongInteger, "0") ) + { + int len = strlen(VeryLongInteger); + for(int i=0 ; i9 ) + { + sum[i+1] += sum[i]/10; + sum[i] %= 10; + } + } + } + + int pos; + for(pos=S-1 ; pos>=0 ; pos--) + if( sum[pos] ) break; + + if( pos==-1 ) printf("0"); + else + for( ; pos>=0 ; pos--) printf("%d", sum[pos]); + printf("\n"); + + return 0; +} diff --git a/uva_cpp_clean/424/424-6.cc b/uva_cpp_clean/424/424-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..e1ad9f7a9d60aca912756343cdd457ac958d8d10 --- /dev/null +++ b/uva_cpp_clean/424/424-6.cc @@ -0,0 +1,33 @@ +// https://uva.onlinejudge.org/external/4/424.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + vvi a; + while(true){ + string s; + cin>>s; + if(s=="0")break; + vi b(s.size()); + for(int i=0;ii)c+=b[i]; + r.push_back(c%10); + c/=10; + } + while(r.size()>1&&r.back()==0){ + r.pop_back(); + } + reverse(r.begin(),r.end()); + for(int k:r)cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 427 + Name: FlatLand Piano Movers + Problem: https://onlinejudge.org/external/4/427.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 1e-6 +long double x, y, w1, w2; +long double f(double t) { + return w1*cos(t) + w2*sin(t) - y*sin(t)*cos(t); +} + +const double pi = acos(-1); +long double maxx() { + double l = 0, r = pi/2, + m1, m2; + + while (r-l > EPS) { + m1 = (l+r)/2; + m2 = (m1 + r)/2; + + if (f(m1) < f(m2)) + r = m1; + else l = m1; + } + return f(m1); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string line; + while (getline(cin, line)) if (line.length()>2) { + stringstream sin(line); + sin >> x; + sin.ignore(100,','); + sin >> y; + + if (y < x) swap(x, y); + while (sin >> w1) { + sin.ignore(100,','); + sin >> w2; + if (w2 < w1) swap(w1, w2); + cout << (x<=maxx() ? 'Y' : 'N'); + } + cout << endl; + } +} + diff --git a/uva_cpp_clean/429/429-13.cpp b/uva_cpp_clean/429/429-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4f2978614d046b7cf9923ec97bd4a402b027d23 --- /dev/null +++ b/uva_cpp_clean/429/429-13.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int test; +string s, t; +vector dis; +vector dic; +map nums; +vector > graph; + +int bfs(int start, int dist) +{ + dis[start] = 0; + + queue p; + + p.push(start); + + while (!p.empty()) + { + int head = p.front(); + + p.pop(); + + for (int i = 0; i < graph[head].size(); i++) + { + int child = graph[head][i]; + + if (dis[head] + 1 < dis[child]) + { + dis[child] = dis[head] + 1; + + p.push(child); + } + } + } + + return dis[dist]; +} + +bool canTrans(string a, string b) +{ + int how = 0; + + for (int i = 0; i < (int)min(a.size(), b.size()); i++) if (a[i] != b[i]) how++; + + if (how == 1 && a.size() == b.size()) return true; + + else return false; +} + +void ini(int n) +{ + graph.clear(); + + graph.resize(n); +} + +int main() +{ + cin >> test; + + while (test--) + { + string word = ""; + + dic.clear(); + + nums.clear(); + + while (cin >> word, word[0] != '*') + { + nums[word] = dic.size(); + + dic.push_back(word); + } + + ini(dic.size()); + + for (int i = 0; i < dic.size(); i++) + { + for (int j = i + 1; j < dic.size(); j++) + { + if (canTrans(dic[i], dic[j])) + { + graph[i].push_back(j); + + graph[j].push_back(i); + } + } + } + + string line; + + size_t pos; + + getline(cin, line); + + getline(cin, line); + + while (line != "") + { + pos = line.find(" "); + + s = line.substr(0, pos); + + t = line.substr(pos + 1, line.size()); + + dis.clear(); + + dis.resize(dic.size(), INT_MAX); + + cout << s << " " << t << " " << bfs(nums[s], nums[t]) << endl; + + if (!getline(cin, line)) + break; + } + + if (test != 0) cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/429/429-19.cpp b/uva_cpp_clean/429/429-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..356dab11afba8c7ec1a372bd529aea5acda0d384 --- /dev/null +++ b/uva_cpp_clean/429/429-19.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair si; +typedef vector vs; +typedef unordered_map svs; + +int proc(string s1, string s2, svs & graph) { + unordered_set match; + queue q; + q.push(make_pair(s1, 0)); + match.insert(s1); + while (!q.empty()) { + if (q.front().first != s2) { + for (int i = 0; i < graph[q.front().first].size(); i++) { + if (!match.count(graph[q.front().first][i])) q.push(make_pair(graph[q.front().first][i], q.front().second + 1)), match.insert(graph[q.front().first][i]); + } + } else return q.front().second; + q.pop(); + } + return 0; +} + +int main() { + int N; + cin >> N; + while (N--) { + unordered_set dic; + svs graph; + string s; + while (cin >> s && s != "*") { + for (auto it = dic.begin(); it != dic.end(); ++it) { + string auxi = *it; + if (auxi.length() == s.length()) { + int dist = 0; + for (int i = 0; i < auxi.length() && dist < 2; i++) { + if (auxi[i] != s[i]) dist++; + } + if (dist == 1) graph[s].push_back(auxi), graph[auxi].push_back(s); + } + } + dic.insert(s); + } + string line; + getline(cin, line); + while (getline(cin, line) && line != "") { + int i = 0; + for (; i < line.length() && line[i] != ' '; i++); + string s1 = line.substr(0, i), s2 = line.substr(i+1, line.length()-i-1); + printf("%s %d\n", line.c_str(), proc(s1, s2, graph)); + } + if (N) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/429/429-21.cpp b/uva_cpp_clean/429/429-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6373a2e95ee24655ebf64ec97f8f03c1f2aa09d2 --- /dev/null +++ b/uva_cpp_clean/429/429-21.cpp @@ -0,0 +1,82 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 429 + Name: Word Transformation + Problem: https://onlinejudge.org/external/4/429.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +#define INF 1073741824 + +bool issim(const char u[], const char v[]) { + int i, cnt=0; + for (i=0; cnt<2 && u[i] && v[i]; i++) + if (u[i] != v[i]) + cnt++; + + if (u[i] != v[i]) + cnt++; + + return cnt<=1; +} + + +list adj[202]; +int d[202], n; +int bfs(int s, int t) { + if (s==t) return 0; + for (int i=0; i q; + q.push(s); + while (!q.empty()) { + int u=q.front(); q.pop(); + for (int v: adj[u]) + if (v == t) return d[u]+1; + else if (d[v] > d[u]+1) { + d[v] = d[u]+1; + q.push(v); + } + } + return -1; +} + + +int main(){ + char words[202][20], line[100], sw[20], tw[20]; + int T, s, t; + cin>>T; + while (T--) { + map ids; + for (n=0; cin>>words[n] && words[n][0]!='*'; n++) { + adj[n].clear(); + ids[words[n]] = n; + for (int i=0; i + +using namespace std; + +int tc, n; +char s[300][300], s1[300], s2[300], ss[300]; +vector G[300]; + +int bfs(int source, int dest){ + int d[n]; + for(int i = 0; i < n; i++) d[i] = -1; + d[source] = 0; + queue Q; + Q.push(source); + while(!Q.empty()){ + int u = Q.front(); + Q.pop(); + for(int i = 0; i < G[u].size(); i++){ + int v = G[u][i]; + if(d[v] == -1){ + d[v] = d[u] + 1; + Q.push(v); + } + } + if(d[dest] != -1) return d[dest]; + } +} + +int ind(char* sss){ + for(int i = 0; i < n; i++) if(strcmp(sss, s[i]) == 0) return i; +} + +bool relation(int x,int y){ + if(strlen(s[x]) != strlen(s[y])) return false; + int dif = 0; + for(int i = 0; i < strlen(s[x]); i++) + if(s[x][i] != s[y][i]) dif++; + return (dif==1); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + n = 0; + while(true){ + cin.getline(s[n], 300); + if(strcmp(s[n], "*") == 0) break; + n++; + } + for(int i = 0; i < n; i++) + for(int j = i + 1; j < n; j++) + if(relation(i, j)){ + G[i].push_back(j); + G[j].push_back(i); + } + while(true){ + cin.getline(ss, 300); + if(sscanf(ss, "%s %s", s1, s2) != 2) break; + printf("%s %s %d\n", s1, s2, bfs(ind(s1), ind(s2))); + } + if(tc) putchar('\n'); + for(int i = 0; i < n; i++) G[i].clear(); + } + return(0); +} diff --git a/uva_cpp_clean/433/433-21.cpp b/uva_cpp_clean/433/433-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..337e65d8bff5e3e11543ced4ab1c7d7763cbc8c1 --- /dev/null +++ b/uva_cpp_clean/433/433-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 433 + Name: Bank (Not Quite O.C.R.) + Problem: https://onlinejudge.org/external/4/433.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int D[] = { 490, 288, 242, 434, 312, 410, 474, 290, 506, 442 }, + B[9], R[9]; +int dfs(int idx, int sum, int gr) { + if (gr > 1) return 0; + if (idx == 9) { + if (sum%11 == 0) { + memcpy(R, B, sizeof(B)); + return 1; + } + return 0; + } + + int r = 0; + int o = B[idx]; + for (int d=0; d<10; ++d) + if ((o&D[d]) == o) { + B[idx] = d; + r += dfs(idx+1, sum + (9-idx)*d, gr + (o!=D[d])); + if (r > 1) return 2; + } + B[idx] = o; + return r; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + + char M[3][43]; + cin.ignore(100, '\n'); + while (T--) { + for (int i=0; i<3; ++i) + cin.getline(M[i], 43); + + for (int i=0; i<9; ++i) { + B[i] = 0; + for (int y=0; y<3; ++y) + for (int x=0; x<3; ++x) { + char ch = M[y][3*i+x]; + if (ch=='_' || ch=='|') + B[i] |= 1<<(3*y + x); + } + } + + int r = dfs(0, 0, 0); + if (!r) cout << "failure\n"; + else if (r>1) cout << "ambiguous\n"; + else { + for (int i=0; i<9; ++i) + cout << R[i]; + cout << '\n'; + } + } +} diff --git a/uva_cpp_clean/434/434-21.cpp b/uva_cpp_clean/434/434-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e36b6521b8e421c3684089f0e99294cfb87b2bc0 --- /dev/null +++ b/uva_cpp_clean/434/434-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 434 + Name: Matty's Blocks + Problem: https://onlinejudge.org/external/4/434.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +int main() { + int T, n, c[8], r[8]; + scanf("%d", &T); + while (T--) { + scanf("%d", &n); + int seen[9]={}, mn = 0; + for (int j=0; j + +#define SIZE 10 + +using namespace std; + +int tc, k, u[SIZE], v[SIZE], _min, _max, j; +bool taken[SIZE]; + +int main(){ + scanf("%d",&tc); + while(tc--){ + memset(taken, false, sizeof taken); + _min = _max = 0; + scanf("%d", &k); + for(int i = 0; i < k; i++) scanf("%d", &u[i]); + for(int i = 0; i < k; i++) scanf("%d", &v[i]), _min += v[i]; + for(int i = 0; i < k; i++){ + for(j = 0; j < k; j++) + if(taken[j] == false && u[i] == v[j]) + taken[j] = true, j = k + 1; + if(j == k) _min += u[i]; + } + for(int i = 0; i < k; i++) + for(int j = 0; j < k; j++) + _max += min(u[i], v[j]); + printf("Matty needs at least %d blocks, and can add at most %d extra blocks.\n", _min, _max - _min); + } + return(0); +} diff --git a/uva_cpp_clean/435/435-21.cpp b/uva_cpp_clean/435/435-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fa5fadc4e91918823555dc668e80c8aa32e5c7e --- /dev/null +++ b/uva_cpp_clean/435/435-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 435 + Name: Block Voting + Problem: https://onlinejudge.org/external/4/435.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + int DP[1024], X[23], n, T; + cin >> T; + while (T-- && cin>>n) { + int sum = 0; + for (int i=0; i> X[i]; + sum += X[i]; + } + + int hs = sum>>1; + for (int i=0; i=0; --k) + DP[k + X[j]] += DP[k]; + + int r = 0; + for (int k=hs-X[i]+1; k<=hs; ++k) + r += DP[k]; + + printf("party %d has power index %d\n", i+1, r); + } + cout << endl; + } +} diff --git a/uva_cpp_clean/436/436-21.cpp b/uva_cpp_clean/436/436-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb1838d99fa47e5ef62e8d41452219fcaaa8b576 --- /dev/null +++ b/uva_cpp_clean/436/436-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 436 + Name: Arbitrage (II) + Problem: https://onlinejudge.org/external/4/436.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string s1, s2; + double M[34][34], d; + for (int cse=1, n,m; cin >> n && n; ++cse) { + unordered_map ids; + for (int i=0; i> s1; + ids[s1] = i; + M[i][i] = 1; + for (int j=0; j> m; + while (m--) { + cin >> s1 >> d >> s2; + int u = ids[s1], + v = ids[s2]; + + M[u][v] = d; + } + + for (int k=0; k0) + for (int j=0; j0) + M[i][j] = max(M[i][j], M[i][k] * M[k][j]); + + bool f = false; + for (int k=0; !f && k 1 + 1e-5) + f = true; + + cout << "Case " << cse << ": " << (f ? "Yes\n" : "No\n"); + } +} diff --git a/uva_cpp_clean/437/437-15.cpp b/uva_cpp_clean/437/437-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73062b0f759a1be3571c83d8b7ca5e359f9a51c2 --- /dev/null +++ b/uva_cpp_clean/437/437-15.cpp @@ -0,0 +1,59 @@ +/* + 最長遞增子序列 (LIS) + 時間複雜度: O(N^2) +*/ +#include +#include +#include + +using namespace std; + +struct brick { + int x; + int y; + int z; + int max_height; // 以這塊磚塊當底時的最大高度 + bool operator>(const brick& other) const { + return x > other.x && y > other.y; // 嚴格遞增 + } +}; + +bool compare(const brick& a, const brick& b) { + // 先照長再照寬排 + if (a.x == b.x) + return a.y < b.y; + return a.x < b.x; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, n, x, y, z, answer, n_case = 0; + + while (cin >> n && n) { + vector bricks; + answer = 0x80000000; // -2147483648 + + for (i = 0; i < n; i++) { + cin >> x >> y >> z; + // 每個磚塊都可以轉 + bricks.push_back(brick{max(x, y), min(x, y), z, z}); + bricks.push_back(brick{max(x, z), min(x, z), y, y}); + bricks.push_back(brick{max(y, z), min(y, z), x, x}); + } + + sort(bricks.begin(), bricks.end(), compare); + n *= 3; // 放了三倍的磚塊進去 + + for (i = 0; i < n; i++) { + for (j = 0; j < i; j++) + if (bricks[i] > bricks[j]) + // 如果 j 可以放在 i 上,那以 i 為底的最大高度就是以 j 為底的最大高度加上 i 的高度 + bricks[i].max_height = max(bricks[i].max_height, bricks[j].max_height + bricks[i].z); + answer = max(answer, bricks[i].max_height); + } + + cout << "Case " << ++n_case << ": maximum height = " << answer << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/437/437-16.cpp b/uva_cpp_clean/437/437-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2aaf5139071c13629bd61fb361701d0d29e1f992 --- /dev/null +++ b/uva_cpp_clean/437/437-16.cpp @@ -0,0 +1,78 @@ +//============================================================================ +// Name : 437 - The Tower of Babylon.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 437 - The Tower of Babylon in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include +#include +#include + +using namespace std; + +#define FRI(i, a, b) for(i = a; i < b; ++i) + +struct block_t { + int x; + int y; + int z; + + bool operator <(const block_t& b) const { + return x < b.x || (!(b.x < x) && y < b.y) || (!(b.x < x) && !(b.y < y) && z < b.z); + } +}; + +int main() { + int i, j, k, l; + int length, high, highest; + int cases = 0; + int n; + int dimension[3]; + set blocks; + set::iterator itBase; + set::iterator itPush; + + while (cin >> n, n != 0) { + highest = 0; + FRI (i, 0, n) { + FRI(j, 0, 3) { + cin >> dimension[j]; + } + sort(dimension, dimension+3); + do { + block_t block; + block.x = dimension[0]; + block.y = dimension[1]; + block.z = dimension[2]; + blocks.insert(block); + } while (next_permutation(dimension, dimension+3)); + } + + int high[blocks.size()] = {0}; + i = 0; + + for (itBase = blocks.begin(); itBase != blocks.end(); ++itBase) { + block_t base = *itBase; + high[i] = base.z; + j = 0; + + for (itPush = blocks.begin(); itPush != itBase; ++itPush) { + block_t push = *itPush; + if (push.x < base.x && push.y < base.y) { + high[i] = max(high[i], high[j] + base.z); + } + j++; + } + highest = max(highest, high[i]); + i++; + } + + cout << "Case " << ++cases << ": maximum height = " << highest << endl; + blocks.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/437/437-21.cpp b/uva_cpp_clean/437/437-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a2eba1bcd4a5c5cfd6a8f95069765e79cc7ece5 --- /dev/null +++ b/uva_cpp_clean/437/437-21.cpp @@ -0,0 +1,88 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 437 + Name: The Tower of Babylon + Problem: https://onlinejudge.org/external/4/437.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 1073741824 + +struct Block { + int x, y, z; + Block () {} + Block (int x, int y, int z): x(x), y(y), z(z) {} + bool operator < (const Block& b) const { + return x>bn && bn) { + n = 3*bn; + for (int i=0; i>a[0]>>a[1]>>a[2]; + sort(a, a+3); + blocks[3*i+0] = Block(a[0], a[1], a[2]); + blocks[3*i+1] = Block(a[0], a[2], a[1]); + blocks[3*i+2] = Block(a[1], a[2], a[0]); + } + blocks[n] = Block(INF, INF, 0); + o[n]=n; + + for (int i=0; i=0; i++) + dfs(i); + + int j; + Block &b0 = blocks[o[0]]; + for (j=1; j<=n; j++) + DP[0][j] = b0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define rep(i, a, n) for (int i = a; i < n; i++) +#define erep(i, a, n) for (int i = a; i <= n; i++) +#define per(i, a, n) for (int i = n - 1; i >= a; i--) +#define pb push_back +#define mp make_pair +#define fi first +#define se second +#define all(x) (x).begin(), (x).end() +#define sz(x) (x).size() + +const int INF = 0x3f3f3f3f; +const double EPS = numeric_limits::epsilon(); +typedef long long ll; +#define MAX 1000 +// end of header + +struct Block { + int x; + int y; + int z; +}; + +Block b[MAX]; // blocks +bool g[MAX][MAX]; // 邻接矩阵 +int dp[MAX]; // block的下标对应的状态(累积最高高度) +int n; + +bool isStackable(int i, int j) { // whether i can be placed under j + return (b[i].x > b[j].x && b[i].y > b[j].y) || (b[i].x > b[j].y && b[i].y > b[j].x); +} + +int maxHeight(int u) { + if (dp[u] != -1) return dp[u]; + dp[u] = b[u].z; + rep(i, 0, n) { + if (g[u][i]) { + dp[u] = max(dp[u], maxHeight(i) + b[u].z); + } + } + return dp[u]; +} + +int main() { + + int kase = 0; + while (~scanf("%d", &n) && n) { + + memset(g, false, sizeof(g)); + memset(dp, -1, sizeof(dp)); + + rep(i, 0, n) { + scanf("%d%d%d", &b[i].x, &b[i].y, &b[i].z); + } + rep(i, 0, n) { + b[n + i].x = b[i].y; + b[n + i].y = b[i].z; + b[n + i].z = b[i].x; + b[2 * n + i].x = b[i].z; + b[2 * n + i].y = b[i].x; + b[2 * n + i].z = b[i].y; + } + n *= 3; + + rep(i, 0, n) { + rep(j, i + 1, n) { + g[i][j] = isStackable(i, j); + g[j][i] = isStackable(j, i); + } + } + + int height = 0; + rep(i, 0, n) { + height = max(height, maxHeight(i)); + } + + printf("Case %d: maximum height = %d\n", ++kase, height); + } + return 0; +} diff --git a/uva_cpp_clean/437/437-9.cpp b/uva_cpp_clean/437/437-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4bdd5e6c156903b5f343866161d4d761b47c3ec6 --- /dev/null +++ b/uva_cpp_clean/437/437-9.cpp @@ -0,0 +1,49 @@ +#include + +#define pb push_back + +using namespace std; + +int tc, n, max_height, foo1, foo2, foo3; +int memo[90]; + +struct tri{ + int x; + int y; + int z; +} tmp; + +vector v; + +int dp(int p){ + if(memo[p] != -1) return memo[p]; + int ans = 0; + for(int i = 0; i < n; i++) + if( (v[i].x < v[p].x && v[i].y < v[p].y) || + (v[i].y < v[p].x && v[i].x < v[p].y) ) + ans = max(ans, dp(i)); + memo[p] = ans + v[p].z; + return memo[p]; +} + +int main(){ + while(scanf("%d", &n), n){ + max_height = 0; + memset(memo, -1, sizeof memo); + v.clear(); + for(int i = 0; i < n; i++){ + scanf("%d %d %d", &foo1, &foo2, &foo3); + tmp.x = foo1, tmp.y = foo2, tmp.z = foo3; + v.pb(tmp); + tmp.x = foo2, tmp.y = foo3, tmp.z = foo1; + v.pb(tmp); + tmp.x = foo3, tmp.y = foo1, tmp.z = foo2; + v.pb(tmp); + } + n *= 3; + for(int i = 0; i < n; i++) + max_height = max(max_height, dp(i)); + printf("Case %d: maximum height = %d\n", ++tc, max_height); + } + return(0); +} diff --git a/uva_cpp_clean/438/438-16.cpp b/uva_cpp_clean/438/438-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fea3abb63bdb58d37163fef05bb6ca67d10751f6 --- /dev/null +++ b/uva_cpp_clean/438/438-16.cpp @@ -0,0 +1,37 @@ +//============================================================================ +// Name : 438 - The Circumference of the Circle.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 438 - The Circumference of the Circle in C++, Ansi-style +// Run Time : 0.000 seconds +// Reference : https://www.quora.com/Given-3-points-in-the-Cartesian-plane-how-can-you-find-the-coordinates-of-the-center-of-the-circle-that-intersects-all-three-points-if-there-exists-such-a-circle +//============================================================================ + +#include +#include +#include + +using namespace std; + +#define FRI(i, a, b) for(i = a; i < b; ++i) +#define PI 3.141592653589793 + +int main() { + int i, j; + double r, u, v, w, p, q; + double a, b, c, d, e, f; + + while (cin >> a >> b >> c >> d >> e >> f) { + r = 0; + u = (a * a + b * b - c * c - d * d) / 2; + v = (a * a + b * b - e * e - f * f) / 2; + w = (a - c) * (b - f) - (a - e) * (b - d); + p = (u * (b - f) - v * (b - d)) / w; + q = (v * (a - c) - u * (a - e)) / w; + r = (a - p) * (a - p) + (b - q) * (b - q); + + cout << setprecision(2) << fixed << 2 * PI * sqrt((float)r) << endl; + } + + return 0; +} diff --git a/uva_cpp_clean/438/438-19.cpp b/uva_cpp_clean/438/438-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1626da5f943bfe31cfaacee24132d8a8e66c584b --- /dev/null +++ b/uva_cpp_clean/438/438-19.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +double pi = 2*acos(0); + +int main() { + double x1, y1, x2, y2, x3, y3; + while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3) { + double px1 = (x1 + x2)/2, py1 = (y1 + y2)/2, + px2 = (x1 + x3)/2, py2 = (y1 + y3)/2, sx = 0, sy = 0, + a = (x2-x1)/(y1-y2), c = (x3-x1)/(y1-y3), + b = py1 - a*px1, d = py2 - c*px2; + if (y1 == y2) { + sx = px1, sy = c*sx + d; + } else if (y1 == y3) { + sx = px2, sy = a*sx + b;; + } else { + sx = (b-d)/(c-a), sy = a*sx + b; + } + printf("%.2lf\n", 2*pi*sqrt((sx - x1)*(sx - x1) + (sy - y1)*(sy - y1))); + } + return 0; +} diff --git a/uva_cpp_clean/438/438-5.cpp b/uva_cpp_clean/438/438-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..35375462b60bb2fc680890fe071bb1f1399849ae --- /dev/null +++ b/uva_cpp_clean/438/438-5.cpp @@ -0,0 +1,65 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main() +{ + double x1,y1,x2,y2,x3,y3,s,a,b,c,r,L; + while(sc("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3)==6) + { + + a=sqrt(pow((x1-x2),2)+pow((y1-y2),2)); + b=sqrt(pow((x2-x3),2)+pow((y2-y3),2)); + c=sqrt(pow((x1-x3),2)+pow((y1-y3),2)); + s=(a+b+c)/2; + L=(sqrt(s*(s-a)*(s-b)*(s-c))); + r=(a*b*c)/(4*L); + pf("%.2lf\n",2*pi*r); + + } + return 0; +} diff --git a/uva_cpp_clean/439/439-10.cpp b/uva_cpp_clean/439/439-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bc815cd80a435276e1120d5c56f03efe8296db3 --- /dev/null +++ b/uva_cpp_clean/439/439-10.cpp @@ -0,0 +1,73 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +const int N= (int)12; + +//vector adj[N]; +int dis[N][N]; + +const int knightx[]={-2,-2,-1,1,2,2,-1,1}; +const int knighty[]={-1,1,-2,-2,-1,1,2,2}; + +bool valid(int x,int y) { + return (x >= 1 && x <= 8 && y >= 1 && y <= 8 && dis[x][y]==-1); +} + +int n,m; +int k,f; +bool can; +int bfs(int x,int y) { + + if (x == k && y == f) return 0; + + queue> q; + dis[x][y] = 0; + q.push({x, y}); + + while (!q.empty()) { + int a = q.front().first; + int b = q.front().second; + q.pop(); + + for (int i = 0; i < 8; i++) { + int t = a + knightx[i]; + int g = b + knighty[i]; + if (valid(t, g)) { + dis[t][g] = dis[a][b] + 1; + if (t == k && g == f) return dis[t][g]; + q.push({t, g}); + } + } + } + +} + +int main() { + FAST + + string a,b; + while (cin>>a>>b) { + + ///if(a=="0"&&b=="0")break; + + memset(dis,-1,sizeof dis); + + k=(b[1]-'1')+1; + f=(b[0]-'a')+1; + + int moves= bfs((a[1]-'1')+1,(a[0]-'a')+1); + + cout<<"To get from "< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 65 +using namespace std; +typedef long long ll; +int fx[] = {1, -1, 2, -2, -2, -1, 1, 2}; +int fy[] = {-2, -2, -1, -1, 1, 2, 2, 1}; +int visit[MAX][MAX]; +int cost[MAX][MAX]; +int BFS_2D(int sx, int sy,int dx,int dy) { + cost[sx][sy] = 0; + visit[sx][sy] = 1; + queueQ; + Q.push(pii(sx, sy)); + while (!Q.empty()) { + pii top = Q.front(); + Q.pop(); + for (int i = 0; i < 8; i++) { + int xx = top.first + fx[i]; + int yy = top.second + fy[i]; + if (xx > 0 && yy > 0 && xx <= 8 && yy <= 8 && !visit[xx][yy]) { + cost[xx][yy] = cost[top.first][top.second] + 1; + visit[xx][yy] = 1; + Q.push(pii(xx, yy)); + } + } + } + return cost[dx][dy]; +} +void mem() { + for (int i = 0; i < MAX; i++) { + for (int j = 0; j < MAX; j++) { + visit[i][j] = 0; + cost[i][j] = 0; + } + } +} +int num(char ch) { + if (ch == 'a') return 1; + else if (ch == 'b') return 2; + else if (ch == 'c') return 3; + else if (ch == 'd') return 4; + else if (ch == 'e') return 5; + else if (ch == 'f') return 6; + else if (ch == 'g') return 7; + else if (ch == 'h') return 8; +} +int main () { + string a, b; + while (cin >> a >> b) { + int sx = num(a[0]); + int sy = a[1] - '0'; + //~ BFS_2D(sx,sy); + int dx = num(b[0]); + int dy = b[1] - '0'; + printf("To get from %s to %s takes %d knight moves.\n", a.c_str(), b.c_str(), BFS_2D(sx,sy,dx,dy)); + mem(); + } + return 0; +} + diff --git a/uva_cpp_clean/439/439-15.cpp b/uva_cpp_clean/439/439-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aba93e3f1c87f0f0b362cfdfb3aff9114b63137a --- /dev/null +++ b/uva_cpp_clean/439/439-15.cpp @@ -0,0 +1,66 @@ +/* + 廣度優先搜索 BFS + 時間複雜度: V = 64,E < 8 * V,O(V+E) = O(1) +*/ +#include +#include +#include + +using namespace std; +const int pos[8][2] = {-1, 2, + 1, 2, + 2, 1, + 2, -1, + 1, -2, + -1, -2, + -2, -1, + -2, 1}; + +int BFS(int x1, int x2, int y1, int y2) { + int i, step, x, y; + + vector> visited(8, vector(8, false)); + queue> bfs_queue; + + bfs_queue.push(make_tuple(x1, y1, 0)); + + while (!bfs_queue.empty()) { + tie(x, y, step) = bfs_queue.front(); + bfs_queue.pop(); + + if (x == x2 && y == y2) + return step; + + step++; + // 每個位置騎士有八個新位置可以移動 + for (i = 0; i < 8; i++) { + x1 = x + pos[i][0]; + y1 = y + pos[i][1]; + + if (x1 < 0 || y1 < 0 || x1 > 7 || y1 > 7 || visited[x1][y1]) + continue; // 超出邊界或是之前走過了 + + if (x1 == x2 && y1 == y2) + return step; + + visited[x1][y1] = true; + bfs_queue.push(make_tuple(x1, y1, step)); + } + } + return -1; +} +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + char start[3], end[3]; + int x1, x2, y1, y2; + + while (cin >> start >> end) { + x1 = start[0] - 'a'; + x2 = end[0] - 'a'; + y1 = start[1] - '1'; + y2 = end[1] - '1'; + cout << "To get from " << start << " to " << end << " takes " << BFS(x1, x2, y1, y2) << " knight moves.\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/439/439-16.cpp b/uva_cpp_clean/439/439-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..725b9a6fff2134035b66a39573e86e0a333bbc50 --- /dev/null +++ b/uva_cpp_clean/439/439-16.cpp @@ -0,0 +1,75 @@ +//============================================================================ +// Name : 439 - Knight Moves.cpp +// Author : ztrixack +// Copyright : MIT License +// Description : 439 - Knight Moves in C++, Ansi-style +// Run Time : 0.000 seconds +//============================================================================ + +#include +#include +#include +#include + +using namespace std; + +#define FRI(i, a, b) for(i = a; i <= b; ++i) + +struct Position { + int x; + int y; + int move; + + Position(int a_x, int a_y, int a_move) : x(a_x), y(a_y), move(a_move) {} +}; + +int main() { + int i, j, x, y, nx, ny; + int count; + string from, to; + queue q; + + while (cin >> from >> to) { + bool board[8][8] = {{false}}; + q = queue(); + + x = from[0] - 'a'; + y = from[1] - '1'; + board[x][y] = true; + q.push(Position(x, y, 0)); + x = to[0] - 'a'; + y = to[1] - '1'; + + while (!q.empty()) { + Position pos = q.front(); + q.pop(); + + if (pos.x == x && pos.y == y) { + count = pos.move; + break; + } + + FRI (i, -2, 2) { + FRI (j, -2, 2) { + if (abs(i) == abs(j) || i == 0 || j == 0) continue; + nx = pos.x + i; + ny = pos.y + j; + + if (nx >= 0 && ny >= 0 && nx < 8 && ny < 8) { + if (nx == x && ny == y) { + count = pos.move + 1; + goto answer; + } else if (!board[nx][ny]) { + board[nx][ny] = true; + q.push(Position(nx, ny, pos.move + 1)); + } + } + } + } + } + +answer: + cout << "To get from " + from + " to " + to + " takes " << count << " knight moves." << endl; + } + return 0; +} diff --git a/uva_cpp_clean/439/439-17.cpp b/uva_cpp_clean/439/439-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba3296bbde70c369920a81060a06d381b82e59f0 --- /dev/null +++ b/uva_cpp_clean/439/439-17.cpp @@ -0,0 +1,62 @@ +/** + * AOAPC II Exercise 6-4 Knight Moves + * 40 AC; + */ +#include +using namespace std; +struct Pt +{ + int x, y; + Pt(int x, int y) : x(x), y(y) { } + bool operator==(const Pt &rhs) + { + return x == rhs.x && y == rhs.y; + } + Pt operator+(const Pt &rhs) + { + return Pt(x + rhs.x, y + rhs.y); + } + bool valid() + { + return 0 <= x && x <= 7 && 1 <= y && y <= 8; + } +}; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + const vector DIRS {{ 1, 2}, { 2, -1}, { 2, 1}, { 1, -2}, + {-1, 2}, {-2, -1}, {-2, 1}, {-1, -2}}; + string xx, yy; + while (cin >> xx >> yy) + { + Pt from(xx[0] - 'a', xx[1] - '0'), to(yy[0] - 'a', yy[1] - '0'); + vector q {from}; + int len = 0; + bool found = from == to; + while (!found) + { + ++len; + vector next; + for (Pt &pt : q) + { + for (const Pt &dir : DIRS) + { + Pt pos = pt + dir; + if (pos.valid()) + if (pos == to) + { + found = true; + break; + } + else + next.push_back(pos); + } + if (found) + break; + } + q = next; + } + cout << "To get from " << xx << " to " << yy << " takes " << len << " knight moves.\n"; + } +} diff --git a/uva_cpp_clean/439/439-21.cpp b/uva_cpp_clean/439/439-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1efe48208a95bd9d5336eaaeb7d2066b0960074f --- /dev/null +++ b/uva_cpp_clean/439/439-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 439 + Name: Knight Moves + Problem: https://onlinejudge.org/external/4/439.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int abs(int n) { + return n<0 ? -n : n; +} + +int d[64][64]; +int main(){ + for (int i=0; i<64; i++) { + int x1=i%8, y1=i/8; + for (int j=0; j<64; j++) + if (i!=j) { + int x2=j%8, y2=j/8; + + if ((abs(x1-x2)==2 && abs(y1-y2)==1) || (abs(x1-x2)==1 && abs(y1-y2)==2)) + d[i][j] = 1; + else + d[i][j]=1000; + } + } + + for (int k=0; k<64; k++) + for (int i=0; i<64; i++) + for (int j=0; j<64; j++) + if (d[i][j] > d[i][k] + d[k][j]) + d[i][j] = d[i][k] + d[k][j]; + + char c1,c2; + int d1,d2; + while(scanf(" %c%d %c%d",&c1,&d1,&c2,&d2)==4) { + int moves = d[(c1-'a')*8 + d1 - 1][(c2-'a')*8 + d2 - 1]; + printf("To get from %c%d to %c%d takes %d knight moves.\n", c1,d1, c2,d2, moves); + } +} diff --git a/uva_cpp_clean/439/439-6.cc b/uva_cpp_clean/439/439-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..e08ce027069c5b829fb493ef4c6f42a6be612515 --- /dev/null +++ b/uva_cpp_clean/439/439-6.cc @@ -0,0 +1,39 @@ +// https://uva.onlinejudge.org/external/4/439.pdf +#include +using namespace std; +using ii=tuple; +using qii=queue; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + string s; + getline(cin,s); + if(s.empty())break; + qii q; + vvi d(8,vi(8)),t(8,vi(8)); + int y=s[0]-'a',x=s[1]-'1'; + q.emplace(y,x); + t[y][x]=1; + y=s[3]-'a';x=s[4]-'1'; + while(!t[y][x]){ + int i,j; + tie(i,j)=q.front(); + q.pop(); + int r[]={-2,-2,-1,-1,1,1,2,2}; + int c[]={-1,1,-2,2,-2,2,-1,1}; + for(int k=0;k<8;k++){ + int u=i+r[k],v=j+c[k]; + if(u>=0&&u<8&&v>=0&&v<8&&!t[u][v]){ + t[u][v]=1; + d[u][v]=d[i][j]+1; + q.emplace(u,v); + } + } + } + cout<<"To get from "< + +using namespace std; + +#define FRI(i, a, b) for(i = a; i < b; ++i) + +int main() { + int i, j, k; + int n, count, head, prev, last; + int link[200], m[200]; + + FRI (i, 3, 151) { + last = 0; + count = 2; + + while (last != 2) { + prev = i; + head = 2; + link[i] = 2; + FRI (j, 2, i) link[j] = j + 1; + FRI (j, 1, i) { + FRI (k, 1, count) { + prev = head; + head = link[head]; + } + last = head; + link[prev] = link[head]; + head = link[head]; + } + count++; + }; + m[i] = count - 1; + } + + while (cin >> n, n != 0) { + cout << m[n] << endl; + } + + return 0; +} diff --git a/uva_cpp_clean/441/441-11.cpp b/uva_cpp_clean/441/441-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1da7d57f9e052b467b4c56c6acfc14b6163cb9e4 --- /dev/null +++ b/uva_cpp_clean/441/441-11.cpp @@ -0,0 +1,91 @@ +#include +/* +Problem: 441 - Lotto +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=382 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int K, S[14]; + +int main () { + scanf("%d\n", &K); + + while ( K ){ + + for (int i = 0; i < K; i++) + scanf("%d", &S[i]); + + for (int i1 = 0; i1 < K - 5; i1++) + for (int i2 = i1+1; i2 < K - 4; i2++) + for (int i3 = i2+1; i3 < K - 3; i3++) + for (int i4 = i3+1; i4 < K - 2; i4++) + for (int i5 = i4+1; i5 < K - 1; i5++) + for (int i6 = i5+1; i6 < K - 0; i6++) + printf("%d %d %d %d %d %d\n", S[i1], S[i2], S[i3], S[i4], S[i5], S[i6]); + + scanf("%d\n", &K); + if (K) printf("\n"); + } + + return 0; +} +/* +Sample input:- +----------------- +7 1 2 3 4 5 6 7 +8 1 2 3 5 8 13 21 34 +0 + +Sample output:- +----------------- +1 2 3 4 5 6 +1 2 3 4 5 7 +1 2 3 4 6 7 +1 2 3 5 6 7 +1 2 4 5 6 7 +1 3 4 5 6 7 +2 3 4 5 6 7 + +1 2 3 5 8 13 +1 2 3 5 8 21 +1 2 3 5 8 34 +1 2 3 5 13 21 +1 2 3 5 13 34 +1 2 3 5 21 34 +1 2 3 8 13 21 +1 2 3 8 13 34 +1 2 3 8 21 34 +1 2 3 13 21 34 +1 2 5 8 13 21 +1 2 5 8 13 34 +1 2 5 8 21 34 +1 2 5 13 21 34 +1 2 8 13 21 34 +1 3 5 8 13 21 +1 3 5 8 13 34 +1 3 5 8 21 34 +1 3 5 13 21 34 +1 3 8 13 21 34 +1 5 8 13 21 34 +2 3 5 8 13 21 +2 3 5 8 13 34 +2 3 5 8 21 34 +2 3 5 13 21 34 +2 3 8 13 21 34 +2 5 8 13 21 34 +3 5 8 13 21 34 + +Resources:- +------------- + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/441/441-13.cpp b/uva_cpp_clean/441/441-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f240b0d0be25c06176b56013d6e002ff1894b5d8 --- /dev/null +++ b/uva_cpp_clean/441/441-13.cpp @@ -0,0 +1,55 @@ +#include +#include + +using namespace std; + +int main() +{ + int s; + + bool first = false; + + cin >> s; + + while (s != 0) + { + int* a = new int[s]; + + for (int i = 0; i < s; i++) + { + cin >> a[i]; + } + + if (first) + { + cout << endl; + } + + else + { + first = true; + } + + for (int i = 0; i < s; i++) + { + for (int j = i + 1; j < s; j++) + { + for (int k = j + 1; k < s; k++) + { + for (int p = k + 1; p < s; p++) + { + for (int q = p + 1; q < s; q++) + { + for (int r = q + 1; r < s; r++) + { + printf("%d %d %d %d %d %d\n", a[i], a[j], a[k], a[p], a[q], a[r]); + } + } + } + } + } + } + + cin >> s; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/441/441-19.cpp b/uva_cpp_clean/441/441-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62b327a740a746d9e65ebef0fb55a324c004327c --- /dev/null +++ b/uva_cpp_clean/441/441-19.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +int n; +int val[13], sel[6]; + +void proc(int pval, int psel) { + if (pval >= n) return; + sel[psel] = val[pval]; + if (psel == 5) { + bool first = false; + for (int i = 0; i < 6; i++) { + if (first) printf(" "); + else first = true; + printf("%d", sel[i]); + } + printf("\n"); + } else proc(pval + 1, psel + 1); + if (n - pval > 6 - psel) proc(pval + 1, psel); +} + +int main() { + bool first = false; + while (scanf("%d", &n) && n != 0) { + if (first) printf("\n"); + else first = true; + for (int i = 0; i < n; i++) scanf("%d", &val[i]); + proc(0, 0); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/441/441-3.cpp b/uva_cpp_clean/441/441-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48350281ec9833797f1deb68e3ee17c2e2033bcc --- /dev/null +++ b/uva_cpp_clean/441/441-3.cpp @@ -0,0 +1,29 @@ +#include //printf scanf + +int main () +{ + // Integer k: size of the array (input) + // Integer nL: integer used to print a new line + int k, nL = 0, lotto[12]; + + while ( scanf("%d",&k), k) + { + if ( nL++ ) + printf ("\n" ); + + for ( int l = 0; l < k; l++ ) + scanf ("%d", &lotto[l]); + + for ( int a = 0; a < k-5; a++ ) + for ( int b = a+1; b < k-4; b++ ) + for ( int c = b+1; c < k-3; c++ ) + for ( int d = c+1; d < k-2; d++ ) + for ( int e = d+1; e < k-1; e++ ) + for ( int f = e+1; f < k; f++ ) + printf ("%d %d %d %d %d %d\n", + lotto[a], lotto[b], lotto[c], + lotto[d], lotto[e], lotto[f] ); + + } + return 0; +} diff --git a/uva_cpp_clean/441/441-9.cpp b/uva_cpp_clean/441/441-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cd51838e74e1f99123418eb348dfc83f58b9abb --- /dev/null +++ b/uva_cpp_clean/441/441-9.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int k, v[13], tc; + +int main(){ + while(scanf("%d", &k), k){ + if(tc) + putchar('\n'); + for(int i = 0; i < k; i++) + scanf("%d", &v[i]); + for(int a = 0; a < k - 5; a++) + for(int b = a + 1; b < k - 4; b++) + for(int c= b + 1; c < k - 3;c++) + for(int d = c + 1; d < k - 2; d++) + for(int e = d + 1; e < k - 1; e++) + for(int f = e + 1; f < k; f++) + printf("%d %d %d %d %d %d\n", v[a], v[b], v[c], v[d], v[e], v[f]); + tc++; + } + return(0); +} diff --git a/uva_cpp_clean/442/442-17.cpp b/uva_cpp_clean/442/442-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff467f6df6db0d86a074b96dd6daa284f894113b --- /dev/null +++ b/uva_cpp_clean/442/442-17.cpp @@ -0,0 +1,53 @@ +/** + * AOAPC II Example 5-3 Matrix Chain Manipulation + */ +#include +using namespace std; +struct Matrix +{ + int a, b; + Matrix(int a = 0, int b = 0) : a(a), b(b) { } +}; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n; + cin >> n; + vector mats(26); + while (n--) + { + char name; + cin >> name; + int ind = name - 'A'; + cin >> mats[ind].a >> mats[ind].b; + } + string expr; + while (cin >> expr) + { + stack s; + bool error = false; + long long ans = 0; + for (char c : expr) + if (isalpha(c)) + s.push(mats[c - 'A']); + else if (c == ')') + { + Matrix mat2 = s.top(); + s.pop(); + Matrix mat1 = s.top(); + s.pop(); + if (mat1.b != mat2.a) + { + error = true; + break; + } + ans += mat1.a * mat1.b * mat2.b; + s.push(Matrix(mat1.a, mat2.b)); + } + if (error) + cout << "error\n"; + else + cout << ans << "\n"; + } +} diff --git a/uva_cpp_clean/442/442-18.cpp b/uva_cpp_clean/442/442-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..122c07c49a97043a25f886e911233bb2a54e0239 --- /dev/null +++ b/uva_cpp_clean/442/442-18.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +using namespace std; + +map> matrixM; + +struct matrix{ + char name; + int row, col; + + void setValues(char name, int row, int col){ + this->name = name; + this->row = row; + this->col = col; + } +}; + +void solve(char input[]){ + stack pila; + bool error = false; + int ans = 0; + for(int i = 0; input[i]; i++){ + if(input[i] == '(') continue; + else if(input[i] == ')'){ + char m1, m2; + if(pila.size() > 1){ + matrix m2 = pila.top(); + pila.pop(); + matrix m1 = pila.top(); + pila.pop(); + if(m1.col == m2.row){ + ans+=m1.col*m1.row*m2.col; + matrix newM; + newM.setValues(m1.name, m1.row, m2.col); + pila.push(newM); + }else{ + error = true; + break; + } + } + }else{ + matrix newM; + newM.setValues(input[i], matrixM[input[i]].first, matrixM[input[i]].second); + pila.push(newM); + } + } + if(pila.size() > 1) error = true; + if(error) printf("error\n"); + else printf("%d\n", ans); +} + +int main(){ + char input[200]; + int n, row, col; + scanf("%d", &n); + for(int i = 0; i < n; i++){ + scanf("%s %d %d", input, &row, &col); + matrixM[input[0]] = make_pair(row, col); + } + while(scanf("%s", input)!=EOF){ + solve(input); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/443/443-14.cpp b/uva_cpp_clean/443/443-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c3acf908853c5d64e6e2816f6d42e8972bbc8a2 --- /dev/null +++ b/uva_cpp_clean/443/443-14.cpp @@ -0,0 +1,80 @@ +/*************************************************** + * Problem name : 443 - Humble Numbers.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/443.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-29 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorhum; +int ar[4] = {2, 3, 5, 7}; +mapmp; +void humNum(ll n) { + if (n > 2000000000 || mp[n] == 1) return; + hum.push_back(n); + mp[n] = 1; + for (int i = 0; i <= 3; i++) { + humNum(n * ar[i]); + } +} +int main () { + ll n; + humNum(1); + sort(hum.begin(), hum.end()); + while (scanf("%lld", &n) == 1 && n != 0) { + printf("The %lld", n); + if (n % 10 == 1 && n % 100 != 11) printf("st"); + else if (n % 10 == 2 && n % 100 != 12) printf("nd"); + else if (n % 10 == 3 && n % 100 != 13) printf("rd"); + else printf("th"); + printf(" humble number is %lld.\n", hum[n - 1]); + } + return 0; +} diff --git a/uva_cpp_clean/443/443-20.cpp b/uva_cpp_clean/443/443-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..667719c843e677cdae7c6b04a6c32b2d72d1a57f --- /dev/null +++ b/uva_cpp_clean/443/443-20.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +const char suffix[6][4] = {"st", "nd", "rd", "th"}; +long humbleNum[5845], suffixPos; +long n, pos2, pos3, pos5, pos7; + +int main() +{ + humbleNum[1] = pos2 = pos3 = pos5 = pos7 = 1; + for(long i=2 ; i<=5842 ; i++) + { + humbleNum[i] = min(min(2*humbleNum[pos2], 3*humbleNum[pos3]), min(5*humbleNum[pos5], 7*humbleNum[pos7])); + if( humbleNum[i]==2*humbleNum[pos2] ) pos2++; + if( humbleNum[i]==3*humbleNum[pos3] ) pos3++; + if( humbleNum[i]==5*humbleNum[pos5] ) pos5++; + if( humbleNum[i]==7*humbleNum[pos7] ) pos7++; + } + + while( scanf("%ld", &n)==1 && n ) + { + if( (n/10)%10!=1 && n%10==1 ) suffixPos = 0; + else if( (n/10)%10!=1 && n%10==2 ) suffixPos = 1; + else if( (n/10)%10!=1 && n%10==3 ) suffixPos = 2; + else suffixPos = 3; + + printf("The %ld%s humble number is %ld.\n", n, suffix[suffixPos], humbleNum[n]); + } + return 0; +} diff --git a/uva_cpp_clean/443/443-21.cpp b/uva_cpp_clean/443/443-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..166f0b3d1ae13a00b1864c3f1f5514271ce4b607 --- /dev/null +++ b/uva_cpp_clean/443/443-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 443 + Name: Humble Numbers + Problem: https://onlinejudge.org/external/4/443.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +void pnum(int cdnl){ + int test_c = abs(cdnl) % 10; + cout< 4) ? "th" + : ((test_c < 4) ? (test_c < 3) ? (test_c < 2) ? (test_c < 1) + ? "th" : "st" : "nd" : "rd" : "th")); +} + +int h[]={2,3,5,7}; +long long humble[5843]; + +int main() +{ + int i,j,k,n; + long long t; + humble[1]=1; + for(i=2;i<5843;i++){ + humble[i]=2100000000; + for(j=1;jhumble[i-1] && t>n && n){ + printf("The ");pnum(n);printf(" humble number is %d.\n",humble[n]); + } + return 0; +} diff --git a/uva_cpp_clean/443/443-4.cpp b/uva_cpp_clean/443/443-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..113e4a418f802d6f5005821f6a3e9c8a2bb09850 --- /dev/null +++ b/uva_cpp_clean/443/443-4.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#define MAX 2000000000 +#define PRECISION 31 + +void generate(std::vector &humble) +{ + for (int i = 0; i < PRECISION; i++) + { + for (int j = 0; j < PRECISION; j++) + { + for (int k = 0; k < PRECISION; k++) + { + for (int m = 0; m < PRECISION; m++) + { + int number = (int)(std::pow(2, i) * std::pow(3, j) * std::pow(5, k) * std::pow(7, m)); + + if (number > MAX || number < 0) + { + break; + } + + humble.push_back(number); + } + } + } + } + + std::sort(humble.begin(), humble.end()); +} + +int main() +{ + std::vector humble; + generate(humble); + + int n; + while (std::cin >> n && n != 0) + { + int reminder = n % 10; + bool exception = n % 100 >= 10 && n % 100 < 20; + + std::cout << "The " << n; + + if (reminder == 1 && !exception) + { + std::cout << "st"; + } + else if (reminder == 2 && !exception) + { + std::cout << "nd"; + } + else if (reminder == 3 && !exception) + { + std::cout << "rd"; + } + else + { + std::cout << "th"; + } + + std::cout << " humble number is " << humble[n - 1] << "." << std::endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/443/443-6.cpp b/uva_cpp_clean/443/443-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41bb51f07abf14ae9f204e20c9c34bfac0208b9e --- /dev/null +++ b/uva_cpp_clean/443/443-6.cpp @@ -0,0 +1,36 @@ +/* + math > ad-hoc > sequences + difficulty: medium + date: 18/Feb/2020 + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int main() { + vector humble(6000); + humble[0] = 1; + int h2 = 0, h3 = 0, h5 = 0, h7 = 0; // humble[h2] é o próximo a ser multiplicado por 2, etc + + for (int h = 1; h < 6000; h++) { + humble[h] = min(min(humble[h2]*2, humble[h3]*3), min(humble[h5]*5, humble[h7]*7)); + if (humble[h] == humble[h2]*2) h2++; + if (humble[h] == humble[h3]*3) h3++; + if (humble[h] == humble[h5]*5) h5++; + if (humble[h] == humble[h7]*7) h7++; + } + int n; + while (cin >> n && n) { + string suff = "th"; + if ((n/10)%10 != 1) { // 2o digito menos significante != 1 + if (n%10 == 1) suff = "st"; + if (n%10 == 2) suff = "nd"; + if (n%10 == 3) suff = "rd"; + } + printf("The %d%s humble number is %d.\n", n, suff.c_str(), humble[n-1]); + } + + return 0; +} diff --git a/uva_cpp_clean/444/444-19.cpp b/uva_cpp_clean/444/444-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d94e31e6af6a89e5bbd5fbd4976f4cce1439bcc --- /dev/null +++ b/uva_cpp_clean/444/444-19.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +using namespace std; + +int main() { + string s; + while (getline(cin, s)) { + if (isdigit(s[0])) { + for (int i = s.length() - 2; i >= 0; i -= 2) { + char c = char(10*(s[i+1] - '0') + (s[i] - '0')); + if (s[i+1] == '1') c = 10*c + (s[i-1] - '0'), i--; + printf("%c", c); + } + } else { + for (int i = s.length() - 1; i >= 0; i--) { + int val = s[i] - 'A' + 65; + if (val >= 100) printf("%d%d1", val%10, (val/10)%10); + else printf("%d%d", val%10, val/10); + } + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/444/444-20.cpp b/uva_cpp_clean/444/444-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9635492661bf9f1b0a27e9719fd57e4950eadb4 --- /dev/null +++ b/uva_cpp_clean/444/444-20.cpp @@ -0,0 +1,35 @@ +#include +#include + +char message[1005], temp[1005]; +int value, n; + +int main() +{ + while( gets(message) ) + { + int len = strlen(message); + if( message[0]-48>=0 && message[0]-48<=9 ) + { + int len2 = 0; + for(int i=len-1 ; i+1!=0 ; i--) temp[len2] = message[i], len2++; + temp[len2] = '\0'; + + value = 0; + for(int i=0 ; i=65 && value<=90) || (value>=97 && value<=122) || value==32 || + value==33 || value==44 || value==46 || value==58 || value==59 || value==63 ) printf("%c", value), value = 0; + } + } + else + for(int i=len-1 ; i>=0 ; i--) + { + n = message[i]; + while( n!=0 ) printf("%d", n%10), n /= 10; + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/444/444-21.cpp b/uva_cpp_clean/444/444-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6544f4000ace777732be1ba53bc9ff8153ac6a43 --- /dev/null +++ b/uva_cpp_clean/444/444-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 444 + Name: Encoder and Decoder + Problem: https://onlinejudge.org/external/4/444.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +bool isc[130]; +char rev[130][4]; + +void encode(char* s) { + for (int i=strlen(s)-1; i>=0; i--) + cout << rev[s[i]]; + cout<=0; i--) { + num = num*10 + s[i] - '0'; + if (isc[num]) { + cout << (char)num; + num = 0; + } + } + cout << endl; +} + +int main(){ + for (int i=0; i<26; i++) + isc['A'+i] = isc['a'+i] = 1; + isc[32]=isc[33]=isc[44]=isc[46]=isc[58]=isc[59]=isc[63]=1; + + for (int i=32; i<130; i++) + if (isc[i]) { + int r = 0; + for (int n=i; n; n/=10) + rev[i][r++] = '0' + n%10; + rev[i][r]=0; + } + + char line[1000]; + while(cin.getline(line, 1000)) { + if (line[0]>='0' && line[0]<='9') + decode(line); + else + encode(line); + } +} diff --git a/uva_cpp_clean/444/444-9.cpp b/uva_cpp_clean/444/444-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e90ba7997fdb3fab583cd0054cf695bac90c0e7d --- /dev/null +++ b/uva_cpp_clean/444/444-9.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +const int LEN = 1000; + +int len; +char line[LEN]; + +void Decode(){ + for(int it = len - 1; it >= 0;){ + int ascii = line[it--] - '0'; + if( ascii == 1 ){ + ascii = 10 * ascii + ( line[it--] - '0' ); + ascii = 10 * ascii + ( line[it--] - '0' ); + } + else + ascii = 10 * ascii + ( line[it--] - '0' ); + putchar(ascii); + } + putchar('\n'); +} + +void Encode(){ + for(int it = len - 1; it >= 0; it--){ + int ascii = line[it]; + while(ascii) putchar('0' + ascii % 10), ascii /= 10; + } + putchar('\n'); +} + +int main(){ + while(cin.getline(line, LEN)){ + len = strlen(line); + if( isdigit(line[0]) ) Decode(); + else Encode(); + } + return(0); +} diff --git a/uva_cpp_clean/445/445-18.cpp b/uva_cpp_clean/445/445-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..54a085ffef86e432432051c8284c9d931084ac87 --- /dev/null +++ b/uva_cpp_clean/445/445-18.cpp @@ -0,0 +1,27 @@ +#include +#include + +using namespace std; + +int main(){ + int cont; + char mazeString[200]; + while(cin.getline(mazeString, 200)){ + for(int i = 0; mazeString[i] > 0; i++){ //Con mazeString[i] > 0 miro si ese char no es nulo (ASCII) + if(mazeString[i]>='0' && mazeString[i]<='9') cont+= mazeString[i]-'0'; //Adapto el num ASCII + else{ + for(int j = 0; j < cont; j++) { + if(mazeString[i] == 'b') cout << " "; + else cout << mazeString[i]; + } + cont=0; + } + if(mazeString[i]=='!'){ //Indica que hasta ahi llega la fila + cout << endl; + cont = 0; + } + } + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/445/445-19.cpp b/uva_cpp_clean/445/445-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e2532d138336aa21abc141f1582933b18c883dd --- /dev/null +++ b/uva_cpp_clean/445/445-19.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + char c; + int r = 0; + while (scanf("%c", &c) != EOF) { + if (isdigit(c)) { + r += c - '0'; + continue; + } else if (c == '!' || c == '\n') printf("\n"); + else if (c == 'b') c = ' '; + for (;r > 0; r--) printf("%c", c); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/445/445-20.cpp b/uva_cpp_clean/445/445-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bce8498ccd0baee959d1d1dad212a908174964a --- /dev/null +++ b/uva_cpp_clean/445/445-20.cpp @@ -0,0 +1,35 @@ +#include +#include + +char line[135]; +int len, temp; + +int main() +{ + while( gets(line) ) + { + len = strlen(line); + for(int i=0 ; i='0' && line[i]<='9' ) + { + temp = 0; + while( line[i]>='0' && line[i]<='9' ) + { + temp += line[i]-'0'; + i++; + } + } + + for(int j=1 ; j<=temp ; j++) + { + if( line[i]=='b' ) printf(" "); + else printf("%c", line[i]); + } + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/445/445-5.cpp b/uva_cpp_clean/445/445-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0da2d2010c6b0a6f8767872362a4703766927bd6 --- /dev/null +++ b/uva_cpp_clean/445/445-5.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +int main() +{ + char s[100010]; + while(gets(s)) + { + int c=0; + for(int i=0;s[i]!='\0';i++) + { + switch(s[i]) + { + case 'b': + { + for(int j=0;j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 446 + Name: Kibbles "n" Bits "n" Bits "n" Bits + Problem: https://onlinejudge.org/external/4/446.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +void printB(int x){ + int i,t=4096; + for(i=12;i>=0;i--)cout<<((x&t)>>i),t=t>>1; +} +int main() +{ + int x,y,n; + char c; + cin>>n; + while(n--) + { + scanf("%x %c %x",&x,&c,&y); + printB(x);printf(" %c ",c);printB(y);printf(" = %d\n",c=='+'?x+y:x-y); + } + return 0; +} diff --git a/uva_cpp_clean/450/450-21.cpp b/uva_cpp_clean/450/450-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e9a20fb7aed96a66899e47b74d0b866535a6c00 --- /dev/null +++ b/uva_cpp_clean/450/450-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 450 + Name: Little Black Book + Problem: https://onlinejudge.org/external/4/450.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int dn, pdep[10000], ord[10000]; +char deps[20][200], + title[10000][200], + fname[10000][200], + lname[10000][200], + saddr[10000][500], + hphon[10000][100], + wphon[10000][100], + cmail[10000][100], + line[10000]; + +bool comp(int i, int j) { return strcmp(lname[i], lname[j]) < 0; } + +int main() { + while (scanf("%d", &dn) == 1) { + int n = 0; + gets(line); + for (int i=0; i + +#define SIZE 200 + +using namespace std; + +struct Data{ + char t[SIZE], f[SIZE], l[SIZE], ha[SIZE], d[SIZE], hp[SIZE], wp[SIZE], cb[SIZE]; +} aux; + +int n; +char line[SIZE], Department[SIZE], *p; +vector v; + +void printData(Data x){ + puts("----------------------------------------"); + printf("%s %s %s\n", x.t, x.f, x.l); + printf("%s\n", x.ha); + printf("Department: %s\n", x.d); + printf("Home Phone: %s\n", x.hp); + printf("Work Phone: %s\n", x.wp); + printf("Campus Box: %s\n", x.cb); +} + +void cp(char* x, char * y){ + int it = 0; + for(it = 0; y[it]; it++) x[it] = y[it]; + x[it] = '\0'; +} + +bool cmp(Data x,Data y){ + return (strcmp(x.l, y.l)<0); +} + +int main(){ + scanf("%d\n", &n); + while(n--){ + cin.getline(Department, SIZE); + strcpy(aux.d, Department); + while(cin.getline(line, SIZE)){ + if(strlen(line) == 0) break; + p = strtok(line, ","); + cp(aux.t, p), p = strtok(NULL, ","); + cp(aux.f, p), p = strtok(NULL, ","); + cp(aux.l, p), p = strtok(NULL, ","); + cp(aux.ha, p), p = strtok(NULL, ","); + cp(aux.hp, p), p = strtok(NULL, ","); + cp(aux.wp, p), p = strtok(NULL, ","); + cp(aux.cb, p), p = strtok(NULL, ","); + v.push_back(aux); + } + } + sort(v.begin(), v.end(), cmp); + for(int i = 0; i < v.size(); i++) printData(v[i]); + return(0); +} diff --git a/uva_cpp_clean/452/452-21.cpp b/uva_cpp_clean/452/452-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d177e041826f87f2aebe2c14dfa059190fda72bb --- /dev/null +++ b/uva_cpp_clean/452/452-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 452 + Name: Project Scheduling + Problem: https://onlinejudge.org/external/4/452.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int X[43], M[43]; +vector adj[43]; +int dfs(int u) { + if (M[u] != -1) + return M[u]; + + int mx = 0; + for (int v: adj[u]) + mx = max(mx, dfs(v)); + + return M[u] = mx + X[u]; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char line[1000], p[43], ch; + int T, t; + cin >> T; + cin.ignore(10, '\n'); + cin.ignore(10, '\n'); + while (T--) { + for (int i=0; i<26; ++i) { + adj[i].clear(); + X[i] = 0; + M[i] = -1; + } + + while (cin.getline(line, 1000) && (ch=line[0])>='A') { + int i = 1; + while (line[i]==' ') ++i; + + t = 0; + while (line[i]>='0') t = t*10 + line[i++] - '0'; + while (line[i]==' ') ++i; + X[ch-'A'] = t; + + while (line[i]) + adj[ch-'A'].push_back(line[i++]-'A'); + } + + int result = 0; + for (int i=0; i<26; ++i) + result = max(result, dfs(i)); + + cout << result << '\n'; + if (T) cout << '\n'; + } +} diff --git a/uva_cpp_clean/452/452-4.cpp b/uva_cpp_clean/452/452-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d578211820b9da00b3b561b124dad763b926e24 --- /dev/null +++ b/uva_cpp_clean/452/452-4.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include + +std::unordered_map dependenciesCounter; + +class task +{ +public: + char name; + int cost; + std::vector dependencies; + + task() + { + name = 0; + cost = 0; + } + + task(char _name, int _cost, std::string _dependencies) + { + name = _name; + cost = _cost; + + for (unsigned int i = 0; i < _dependencies.length(); i++) + { + dependencies.push_back(_dependencies[i]); + dependenciesCounter[_dependencies[i]]++; + } + } +}; + +int dfs(int cost, char current, std::unordered_map &tasks) +{ + if (tasks[current].dependencies.size() == 0) + { + return cost + tasks[current].cost; + } + + int max = 0; + + for (unsigned int i = 0; i < tasks[current].dependencies.size(); i++) + { + int temp = dfs(cost + tasks[current].cost, tasks[current].dependencies[i], tasks); + max = std::max(max, temp); + } + + return max; +} + +int main() +{ + int tests; + std::cin >> tests; + tests++; + + std::string line; + std::cin.ignore(); + + while (tests--) + { + bool read = false; + std::unordered_map tasks; + dependenciesCounter.clear(); + + while (std::getline(std::cin, line)) + { + if (line.length() == 0) + { + break; + } + + read = true; + + char name = 0; + int cost = 0; + std::string dependencies = ""; + std::stringstream ss(line); + + ss >> name >> cost >> dependencies; + tasks[name] = (task(name, cost, dependencies)); + } + + if (read) + { + int max = 0; + + for (char i = 'A'; i <= 'A' + (char)tasks.size(); i++) + { + if (dependenciesCounter[i] != 0) + { + continue; + } + + int temp = dfs(0, i, tasks); + max = std::max(max, temp); + } + + std::cout << max << std::endl; + + if (tests != 0) + { + std::cout << std::endl; + } + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/454/454-21.cpp b/uva_cpp_clean/454/454-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f9c47186325e0580ad69491744ed2f68f61e6aa --- /dev/null +++ b/uva_cpp_clean/454/454-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 454 + Name: Anagrams + Problem: https://onlinejudge.org/external/4/454.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool isempty(const string &str) { + for (int i=0; str[i]; ++i) + if (str[i] != ' ') + return 0; + return 1; +} + +int wc[114][256]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + while (T--) { + string str; + vector v; + while (getline(cin, str) && !isempty(str)) + v.push_back(str); + sort(v.begin(), v.end()); + + + memset(wc, 0, sizeof(wc)); + for (int i=0; i +using namespace std; + +#define fastio ios_base::sync_with_stdio(0);cin.tie(0) +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long int +#define S second +#define F first +#define debug1 cout << "debug1" << '\n' +#define debug2 cout << "debug2" << '\n' +#define debug3 cout << "debug3" << '\n' + + +// int dr[] = {0,-1,0,1,-1,-1,1,1}; +// int dc[] = {-1,0,1,0,-1,1,1,-1}; + + +// Maths Utils +int binExp(int a, int b, int m){int r = 1;while (b){if (b % 2 == 1)r = (r * a) % m;a = (a * a) % m;b = b / 2;}return r;} + +int modinv(int x,int m){ + return binExp(x,m-2,m); +} + +int lsp(string pat){ + int n = pat.size(); + vector pie(n,0); + + int j = 0; + pie[0] = 0; + for(int i = 1;i=0 and pat[i]!=pat[j]){ + if(j>=1)j = pie[j-1]; + else j = -1; + } + + // increment prefix count by 1 + j++; + pie[i] = j; + } + return j; +} + + +void solve(){ + string s; + cin>>s; + int n = s.size(); + int pie = lsp(s); + + if(n%(n-pie)) + cout<> t; + while (t--) + { + solve(); + if(t)cout<<"\n"; + } +} diff --git a/uva_cpp_clean/455/455-14.cpp b/uva_cpp_clean/455/455-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..310b9c3ce15257532cc1685d4442d8f710831741 --- /dev/null +++ b/uva_cpp_clean/455/455-14.cpp @@ -0,0 +1,89 @@ +/*************************************************** + * Problem name : 455 Periodic Strings.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/455.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-11-23 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,0,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int fun(string str, string ss) { + int cnt = 0; + for (int i = 0; i < (int)str.size();) { + if (str.substr(i, ss.size()) == ss) { + cnt++; + i += ss.size(); + } else { + cnt = -1; + break; + } + } + return cnt; +} +int main () { + string str; + int tc; + scanf("%d", &tc); + getchar(); + for (int t = 1; t <= tc; t++) { + cin >> str; + int sz = str.size(); + for (int i = 1; i <= sz; i++) { + string ss = str.substr(0, i); + int szz = ss.size(); + int cnt = fun(str, ss); + if(cnt*szz == sz){ + printf("%d\n", szz); + break; + } + } + if ( t != tc) nl; + } + return 0; +} diff --git a/uva_cpp_clean/455/455-15.cpp b/uva_cpp_clean/455/455-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58c6862a4d9f793603ec44b1795395ac26dd5916 --- /dev/null +++ b/uva_cpp_clean/455/455-15.cpp @@ -0,0 +1,50 @@ +/* + 字串最小週期 + 時間複雜度(naive): O(N^2) + 時間複雜度(KMP) : O(N) +*/ +#include +#include + +using namespace std; + +int KMP_solution(string word) { + int i = 0, j = -1, next[100] = {-1}, length = word.size(); + // next[i] 表示 i 位置前綴和後綴有幾個字相同 + while (i < length) + if (j == -1 || word[i] == word[j]) + next[++i] = ++j; + else + j = next[j]; + return length % (length - next[length]) ? length : length - next[length]; // 無法整除: 沒有小於 length 的循環節 +} + +int naive_solution(string word) { + int i, j, length = word.size(); + for (i = 1; i < length; i++) + if (length % i == 0) { + for (j = length - 1; j >= i; j--) // 反過來比較好算 + if (word[j] != word[j - i]) + break; + if (j < i) // 找到最小解 + break; + } + return i; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n; + string word; + + cin >> n; + while (n--) { + cin >> word; + cout << KMP_solution(word) << "\n"; + if (n) + cout << "\n"; // 小心 Presentation Error + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/455/455-17.cpp b/uva_cpp_clean/455/455-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c232fe01258d6898985a4a4da3da814e38b72a6 --- /dev/null +++ b/uva_cpp_clean/455/455-17.cpp @@ -0,0 +1,32 @@ +/** + * AOAPC II 3-4 Periodic Strings + * String + * 15 AC + */ +#include +using namespace std; +int main() { + int N; + cin >> N; + bool first = true; + while (N--) { + string s; + cin >> s; + int ans = 1; + for (; ans <= s.size() / 2; ++ans) { + if (s.size() % ans) // ! + continue; + auto end = s.begin() + ans, iter = end; + for (; iter < s.end(); iter += ans) + if (!equal(s.begin(), end, iter)) + break; + if (iter >= s.end()) + break; + } + if (first) + first = false; + else + cout << '\n'; + cout << (ans <= s.size() / 2 ? ans : s.size()) << '\n'; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/455/455-18.cpp b/uva_cpp_clean/455/455-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..67aeb03f4b344bdcc2963c321808f860814abf7d --- /dev/null +++ b/uva_cpp_clean/455/455-18.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +using namespace std; + +bool KMP(string input, int jump){ + for(int i = 0; i < jump; i++){ + char pattern = input[i]; + for(int j = i + jump; j < input.size(); j+=jump){ + if(input[j] != pattern) return false; + } + } + return true; +} + +int main(){ + int n; + string input; + cin >> n; + while(n--){ + cin >> input; + int i; + for(i = 1; i < input.size(); i++){ + if(input.size() % i != 0) continue; //No puede ser un patron. + if(KMP(input, i)){ //La substr de 0->i es un patron. + printf("%d\n", i); + break; + } + } + if(i == input.size()) printf("%d\n", i); + if(n) printf("\n"); + } + return 0; +} +/* + La estrategia a usar es KMP para buscar el patron, pero hay que modificarlo ya que no nos dan el patron, + entonces lo que hago para simular ese patron es usar indices, pero los indices que sean multiplos del tamaño + de la string ya que asi simulo que de lo que esta de ese indice hasta cero es mi patron a buscar. + Aprendizaje: Yo habia leido algo sobre KMP mucho antes de encontrarme con este problema pero no lo suficiente para + entenderlo. Para realizar este ejercicio tuve que leer sobre KMP y sus principios. + A tener en cuenta: El patron que se busca debe tener indice multiplo del tamaño de la string debido a que ese + patron debe repetirse de manera uniforme en toda la string. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/455/455-19.cpp b/uva_cpp_clean/455/455-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6201580492f357eb45a9360ec184df9935f3f922 --- /dev/null +++ b/uva_cpp_clean/455/455-19.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +int main() { + int T; + bool first = false; + cin >> T; + while (T--) { + if (first) printf("\n"); + else first = true; + string s; + cin >> s; + int i = 1; + for (; i < s.length(); i++) { + if (s.length() % i == 0) { + bool ok = true; + for (int j = 0; j < i && ok; j++) { + for (int k = 1; k * i + j < s.length() && ok; k++) { + ok = (s[j] == s[j + k * i]); + } + } + if (ok) break; + } + } + printf("%d\n", i); + } + return 0; +} diff --git a/uva_cpp_clean/455/455-21.cpp b/uva_cpp_clean/455/455-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31281b85de83d0f1f5189fa7473d63d9f2ef680e --- /dev/null +++ b/uva_cpp_clean/455/455-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 455 + Name: Periodic Strings + Problem: https://onlinejudge.org/external/4/455.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char str[100]; +int l; + +bool check(int p) { + int c = l/p;//3 + for (int i=0; i>1; + for (int i=1; i<=half; i++) + if (l%i==0 && check(i)) + return i; + return l; +} + +int main(){ + int T; + cin>>T; + while (T--) { + cin>>str; + cout << minPeriod() << endl; + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/455/455-5.cpp b/uva_cpp_clean/455/455-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5679bd334c9ff8e791253f623b236f45c5b7ad39 --- /dev/null +++ b/uva_cpp_clean/455/455-5.cpp @@ -0,0 +1,156 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) cout<<"Case "<<++no<<": " +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +string p; + +int pre[mx6],m; + +void prefix() +{ + int q=0; + pre[1]=0; + forab(i,2,m) + { + while(q>0 && p[q+1]!=p[i]) + { + q=pre[q]; + } + if(p[q+1]==p[i]) + q++; + pre[i]=q; + } +} + +int main() +{ + ios_base::sync_with_stdio(0);cin.tie(); + + int t,no=0; + cin>>t; + while(t--) + { + cin>>p; + m=sz(p); + + p=" "+p; + prefix(); + + int res=m-pre[m]; + if(m%res==0) + { + CP(res); + } + else + { + CP(m); + } + if(t) cout<<"\n"; + } + return 0; +} diff --git a/uva_cpp_clean/455/455-9.cpp b/uva_cpp_clean/455/455-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2328a8122e865d33b1feea2cbb21cb2e3600f9bb --- /dev/null +++ b/uva_cpp_clean/455/455-9.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +vector Zfunction (string s) { + int n = (int) s.size(); + vector z(n); + for (int i = 1, l = 0, r = 0; i < n; i++) { + if (i <= r) z[i] = min(r - i + 1, z[i - l]); + while (i + z[i] < n and s[z[i]] == s[i + z[i]]) z[i]++; + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + } + return z; +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + string s; + cin >> s; + vector z = Zfunction(s); + int n = s.size(), p = -1; + for (int i = 1; i < n and p == -1; i++) + if (n % i == 0 and i + z[i] == n) p = i; + if (p == -1) p = n; + cout << p << endl; + if (tc) puts(""); + } + return (0); +} diff --git a/uva_cpp_clean/457/457-18.cpp b/uva_cpp_clean/457/457-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b015abbfc87547d6e5c72b563156953c2eb5303e --- /dev/null +++ b/uva_cpp_clean/457/457-18.cpp @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; //No usar matrices para optimizar tiempos + +int main(){ + int DNA[10]; + int n, cont = 0; + cin >> n; + while(cont < n){ + int today[43] = {0}; + int aux[43] = {0}; + today[20] = 1; + for(int i = 0; i < 10; i++) cin >> DNA[i]; + for(int i = 0; i < 50; i++){ + for(int j = 1; j <= 40; j++){ //Por comodidad + if(today[j] == 1) cout << "."; + else if(today[j] == 0) cout << " "; + else if(today[j] == 2) cout << "x"; + else cout << "W"; + } + cout << endl; + memcpy(aux, today, sizeof(aux)); + int jLeft, jRight, dish; + for(int j = 1; j <= 40; j++){ + jLeft = j - 1; + if(j == 40) jRight = 0; + else jRight = j + 1; + dish = aux[jLeft] + aux[j] + aux[jRight]; + today[j] = DNA[dish]; + } + } + if(cont != n - 1) cout << endl; + cont++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/457/457-21.cpp b/uva_cpp_clean/457/457-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5059debd7ef27c55474d2387759163c1265fff63 --- /dev/null +++ b/uva_cpp_clean/457/457-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 457 + Name: Linear Cellular Automata + Problem: https://onlinejudge.org/external/4/457.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + char CH[] = " .xW"; + while (T--) { + int DNA[10]; + for (int i=0; i<10; ++i) + cin >> DNA[i]; + + int X[42] = {}; + X[20] = 1; + for (int i=0; i<50; ++i) { + for (int j=1; j<=40; ++j) + cout << CH[X[j]]; + cout << '\n'; + + for (int j=1, b=0, k; j<=40; ++j) + k = b+X[j]+X[j+1], + b = X[j], + X[j] = DNA[k]; + } + + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/458/458-18.cpp b/uva_cpp_clean/458/458-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0236403875a6da6421ecf2f6ad25d229f5cd2723 --- /dev/null +++ b/uva_cpp_clean/458/458-18.cpp @@ -0,0 +1,16 @@ +#include +#include + +using namespace std; + +int main(){ + string input, output; + while(true){ + cin >> input; + output = ""; + if(cin.fail()) break; + for(int i = 0; i < input.length(); i++) output += input[i] - 7; + cout << output << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/458/458-19.cpp b/uva_cpp_clean/458/458-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..111ea9b9793bd26e4d888b0e3414b8c9bf18706b --- /dev/null +++ b/uva_cpp_clean/458/458-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main(){ + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + char c; + while(scanf("%c", &c) != EOF){ + if(c != '\n') c = char(c-7); + printf("%c", c); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/458/458-20.cpp b/uva_cpp_clean/458/458-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fcd593c1b466bf16f0892795e6003ce2b6293e6 --- /dev/null +++ b/uva_cpp_clean/458/458-20.cpp @@ -0,0 +1,19 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + char line[1000]; + + while( cin >> line ) + { + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 458 + Name: The Decoder + Problem: https://onlinejudge.org/external/4/458.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char ch; + while((ch=getchar())!=EOF) + if (ch=='\n') cout< +#include + +using namespace std; + +int main() +{ + string ins; + char a; + while ( getline(cin, ins) ) + { + for ( int i = 0; i < ins.length(); i++ ) + { + a = ins[i] - 7; + cout << a; + } + cout << endl; + } +} diff --git a/uva_cpp_clean/458/458-5.cpp b/uva_cpp_clean/458/458-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e0915bc72664e95a18fcdcf4b84b98ea3f0b9bd3 --- /dev/null +++ b/uva_cpp_clean/458/458-5.cpp @@ -0,0 +1,15 @@ +#include +#include +int main() +{ +int l,i; +char s[1000]; +while(gets(s)) + { + l=strlen(s); + for(i=0;i +using namespace std; +int main(){ + for(string s;getline(cin, s);){ + for(int c:s)cout< + +using namespace std; + +const int LEN = 1000; + +char line[LEN]; + +int main(){ + while(cin.getline(line, LEN)){ + for(int it = 0; line[it]; it++) putchar(line[it] - 7); + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/459/459-10.cpp b/uva_cpp_clean/459/459-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..954ac6c83905be9d75016f95d743bf2ec41cec13 --- /dev/null +++ b/uva_cpp_clean/459/459-10.cpp @@ -0,0 +1,118 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e2+5; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; + +int minimumSpanningTree(vector> &edges) { + + + sort(edges.rbegin(), edges.rend()); + DSU dsu; + dsu.init(N); + int ans = 1e9,a,b,weight; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if (dsu.find(a) != dsu.find(b)) { + + dsu.merge(a, b); + + ans = min(ans,weight); + } + } + return ans; +} + +void test() { + int n; + + DSU dsu; + + string s; cin>>s; + + n= (s[0]-'A')+1; + dsu.init(n); + + cin.ignore(); + + string a; + while (getline(cin,a) && a.size()) { + ///cout<< (a[0]-'A')<<" "<<(a[1]-'A')<<"\n"; + dsu.merge((a[0]-'A'),(a[1]-'A')); + } + + set ans; + + for(int i=0;i>t; + while (t--) { + test(); + + if(t!=0) cout<<"\n"; + } + +} + diff --git a/uva_cpp_clean/459/459-14.cpp b/uva_cpp_clean/459/459-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1a4643f988982683eb73ba3bd0e9f06e3e4b642d --- /dev/null +++ b/uva_cpp_clean/459/459-14.cpp @@ -0,0 +1,120 @@ +/*************************************************** + * Problem name : 459 Graph Connectivity.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/459.pdf + * OJ : Uva + * Verdict : AC + * Date : 02.06.2017 + * Problem Type : Union Find + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +////////////////////////////// Code Start Here //////////////////////////////////////////// +//~ vectorpar; +mappar; +void makeSet(char u) { + par[u] = u; +} +void initUnion(char N) { + for (char i = 'A'; i <= N; i++) { + makeSet(i); + } +} +char findRepresentative(char r) { + if (par[r] == r) { + return r; + } else { + par[r] = findRepresentative(par[r]); + } + + return par[r]; +} +void setUnion(char a, char b) { + char u = findRepresentative(a); + char v = findRepresentative(b); + + if (u != v) { + par[u] = v; + } +} +int main () { + int tc; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + char ch; + cin >> ch; + initUnion(ch); + string Edge; + int flag = 1; + getchar(); + while (flag) { + getline(cin, Edge); + int sz = Edge.size(); + if (sz == 0) { + flag = 0; + break; + } + char a = Edge[0]; + char b = Edge[1]; + //~ cout << a << " "<< b <mp; + int cnt = 0; + for (char i = 'A'; i <= ch; i++) { + char r = findRepresentative(i); + if (mp[r] == 0) { + mp[r] = 1; + cnt++; + } + } + printf("%d\n", cnt); + if (t != tc) printf("\n"); + mp.clear(); + par.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/459/459-19.cpp b/uva_cpp_clean/459/459-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd5923ef751dc70b2c795424dfefda092c86e96f --- /dev/null +++ b/uva_cpp_clean/459/459-19.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +vector adjList; +vi color; + +int proc() { + bool ok = true; + int count = 0; + queue q; + for (int j = 0; j < adjList.size() && ok; j++) { + if (color[j] == -1) { + q.push(j); + color[j] = 0; + count++; + } + while (!q.empty() && ok) { + for (int i = 0; i < adjList[q.front()].size() && ok; i++) { + if (color[adjList[q.front()][i]] == -1) { + color[adjList[q.front()][i]] = 0; + q.push(adjList[q.front()][i]); + } + } + q.pop(); + } + } + return count; +} + +int main() { + int T; + cin >> T; + while (T--) { + char V; + cin >> V; + adjList.assign(V-'A'+1, vi()); + color.assign(V-'A'+1, -1); + string s; + getline(cin, s); + while (getline(cin, s) && s != "") { + int a1 = s[0]-'A', a2 = s[1]-'A'; + adjList[a1].push_back(a2); + adjList[a2].push_back(a1); + } + printf("%d\n", proc()); + if (T) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/459/459-21.cpp b/uva_cpp_clean/459/459-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e09f2371421b7268360b3af89644aa1624d6487f --- /dev/null +++ b/uva_cpp_clean/459/459-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 459 + Name: Graph Connectivity + Problem: https://onlinejudge.org/external/4/459.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +bool seen[30]; +bool ar[30][30]; +int n; + +int dfs (int v) { + if (seen[v]) return 0; + seen[v] = true; + + int res = 1; + for (int i=0; i>T; + while(T--) { + cin >> last; + n = last - 'A' + 1; + for (int i=0; i0; + + cout << result << endl; + if (T) cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int col[50]; + +vi g[50]; + +void dfs(int u) +{ + col[u]=1; + int n=sz(g[u]); + for0(i,n) + { + if(!col[g[u][i]]) + { + dfs(g[u][i]); + } + } +} + +int main() +{ + int t; + si(t); + getchar(); + while(t--) + { + string s,last; + cin>>last; + getchar(); + int n=last[0]-'A'; + n++; + while(getline(cin,s) && s!="") + { + g[s[0]-'A'].pb(s[1]-'A'); + g[s[1]-'A'].pb(s[0]-'A'); + } + + int cnt=0; + for0(i,n) + { + if(!col[i]) + { + cnt++; + dfs(i); + } + } + P(cnt); + if(t) + puts(""); + + cover(col,0); + for0(i,30) + { + g[i].clear(); + } + } + + + return 0; +} + diff --git a/uva_cpp_clean/459/459-6.cc b/uva_cpp_clean/459/459-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..b9e2cd7d3957b362c6d270244de2e578f0c75440 --- /dev/null +++ b/uva_cpp_clean/459/459-6.cc @@ -0,0 +1,40 @@ +// https://uva.onlinejudge.org/external/4/459.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + int t; + string s; + cin>>t; + getline(cin, s); + getline(cin, s); + while(t--){ + int n,u,v,c=0; + getline(cin, s); + n=s[0]-'A'+1; + vvi g(n); + while(1){ + getline(cin,s); + if(s.empty())break; + u=s[0]-'A'; + v=s[1]-'A'; + g[u].push_back(v); + g[v].push_back(u); + } + vi s(n); + functiondfs=[&](int i){ + s[i]=1; + for(int j:g[i]) + if(!s[j]) + dfs(j); + }; + for(int i=0;i + +#define SIZE 30 + +using namespace std; + +int tc, CC; +char ch, ch1, ch2, line[SIZE]; +bool Adj[SIZE][SIZE], vis[SIZE]; + +void dfs(int u){ + vis[u] = true; + for(int v = 0; v <= (ch - 'A'); v++) + if( Adj[u][v] and not vis[v]) + dfs(v); +} + +int main(){ + scanf("%d\n", &tc); + int t = 0; + while(tc--){ + t++; + memset(Adj, false, sizeof Adj); + memset(vis, false, sizeof vis); + CC = 0; + cin.getline(line, SIZE); + sscanf(line, "%c", &ch); + while(cin.getline(line, SIZE)){ + if( sscanf(line, "%c%c", &ch1, &ch2) != 2 ) break; + Adj[ch1 - 'A'][ch2 - 'A'] = Adj[ch2 - 'A'][ch1 - 'A'] = true; + } + for(int u = 0; u <= (ch - 'A'); u++) + if( not vis[u] ) + dfs(u), CC++; + printf("%d\n", CC); + if( tc ) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/460/460-14.cpp b/uva_cpp_clean/460/460-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..405fa3a11391a41d75125d8b93e47976d4a8b386 --- /dev/null +++ b/uva_cpp_clean/460/460-14.cpp @@ -0,0 +1,72 @@ +/*************************************************** + * Problem name : 460 Overlapping Rectangles.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/460.pdf + * OJ : Uva + * Verdict : AC + * Date : 03.06.2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +int main () { + int tc; + scanf("%d", &tc); + for(int t = 1; t<=tc; t++){ + int x2,y2,a2,b2; + int x1,y1,a1,b1; + int x,y,a,b; + scanf("%d %d %d %d", &x1, &y1, &a1, &b1); + scanf("%d %d %d %d", &x2, &y2, &a2, &b2); + x = max(x1,x2); + y = max(y1,y2); + a = min(a1,a2); + b = min(b1,b2); + if(x>=a || y >=b) printf("No Overlap\n"); + else printf("%d %d %d %d\n",x, y, a, b); + if(t!=tc) nl; + } + return 0; +} + diff --git a/uva_cpp_clean/460/460-18.cpp b/uva_cpp_clean/460/460-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f4376af3d30cb963d1e902e00f8698515413135b --- /dev/null +++ b/uva_cpp_clean/460/460-18.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main(){ + int tc, xo1, yo1, x1, y1, xo2, yo2, x2, y2, i, j, k, l; + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d %d %d %d %d %d", &xo1, &yo1, &x1, &y1, &xo2, &yo2, &x2, &y2); + i = max(xo1, xo2); + j = max(yo1, yo2); + k = min(x1, x2); + l = min(y1, y2); + if(i >= k || j >= l) printf("No Overlap\n"); + else printf("%d %d %d %d\n", i, j, k, l); + if(tc) printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/460/460-19.cpp b/uva_cpp_clean/460/460-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25e85ad7d36fb5ca48a80dd0a2716743b3f72c48 --- /dev/null +++ b/uva_cpp_clean/460/460-19.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +int main() { + int N; + cin >> N; + while (N--) { + int x1ll, y1ll, x1ur, y1ur, x2ll, y2ll, x2ur, y2ur; + cin >> x1ll >> y1ll >> x1ur >> y1ur >> x2ll >> y2ll >> x2ur >> y2ur; + int X1, X2, Y1, Y2; + X1 = max(x1ll, x2ll); + Y1 = max(y1ll, y2ll); + X2 = min(x1ur, x2ur); + Y2 = min(y1ur, y2ur); + if (X1 < X2 && Y1 < Y2) printf("%d %d %d %d\n", X1, Y1, X2, Y2); + else printf("No Overlap\n"); + if (N) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/460/460-20.cpp b/uva_cpp_clean/460/460-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3450e19a9d6c19bb048a3337fca5100d1979df0f --- /dev/null +++ b/uva_cpp_clean/460/460-20.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +int main() +{ + int tc, new_line; + + scanf("%d", &tc); + new_line = 0; + while( tc-- ) + { + int XLL_1, YLL_1, XUR_1, YUR_1, XLL_2, YLL_2, XUR_2, YUR_2; + scanf("%d %d %d %d", &XLL_1, &YLL_1, &XUR_1, &YUR_1); + scanf("%d %d %d %d", &XLL_2, &YLL_2, &XUR_2, &YUR_2); + + int XLL = max(XLL_1, XLL_2); + int YLL = max(YLL_1, YLL_2); + int XUR = min(XUR_1, XUR_2); + int YUR = min(YUR_1, YUR_2); + + if( new_line ) printf("\n"); + new_line = 1; + + if( XUR<=XLL || YUR<=YLL ) printf("No Overlap\n"); + else printf("%d %d %d %d\n", XLL, YLL, XUR, YUR); + } + return 0; +} diff --git a/uva_cpp_clean/460/460-21.cpp b/uva_cpp_clean/460/460-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd1bb113b328f2dac55fa9cdbb0eef1d2e9dad4c --- /dev/null +++ b/uva_cpp_clean/460/460-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 460 + Name: Overlapping Rectangles + Problem: https://onlinejudge.org/external/4/460.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool intersect(int x1, int x2, int x3, int x4, int &x5, int &x6) { + if (x1 >= x3) { + swap(x1, x3); + swap(x2, x4); + } + + x5 = max(x1, x3); + x6 = min(x2, x4); + return x5 < x6; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, x1, x2, x3, x4, x5, x6, + y1, y2, y3, y4, y5, y6; + + cin >> n; + while (n--) { + cin >> x1 >> y1 >> x2 >> y2 + >> x3 >> y3 >> x4 >> y4; + + if (intersect(x1, x2, x3, x4, x5, x6) && intersect(y1, y2, y3, y4, y5, y6)) + printf("%d %d %d %d\n", x5, y5, x6, y6); + else + puts("No Overlap"); + if (n) puts(""); + } +} diff --git a/uva_cpp_clean/460/460-5.cpp b/uva_cpp_clean/460/460-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fe82e5f0419d82afaf54d2128553642f355e64d8 --- /dev/null +++ b/uva_cpp_clean/460/460-5.cpp @@ -0,0 +1,171 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int main() +{ + int t; + si(t); + while(t--) + { + int x1,y1,x2,y2; + int xx1,yy1,xx2,yy2; + + sii(x1,y1),sii(x2,y2); + sii(xx1,yy1),sii(xx2,yy2); + + int maxx=max(x1,xx1); + int minx=min(x2,xx2); + + int maxy=max(y1,yy1); + int miny=min(y2,yy2); + + if(minx>maxx && miny>maxy) + PPN(maxx,maxy),PP(minx,miny); + else + puts("No Overlap"); + + if(t) + nl; + } + + + return 0; +} diff --git a/uva_cpp_clean/462/462-21.cpp b/uva_cpp_clean/462/462-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a31e73b648e6c2f26bad8875062a7446d69eed4 --- /dev/null +++ b/uva_cpp_clean/462/462-21.cpp @@ -0,0 +1,100 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 462 + Name: Bridge Hand Evaluator + Problem: https://onlinejudge.org/external/4/462.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +string c[13]; +int so[127], cnt[4]; +int rule1234() { + int points = 0; + for (int i=0; i<13; ++i) { + int cc = cnt[so[c[i][1]]]; + switch(c[i][0]) { + case 'A': points += 4; break; + case 'K': points += 3 - (cc==1); break; + case 'Q': points += 2 - (cc<=2); break; + case 'J': points += 1 - (cc<=3); break; + } + } + return points; +} + +int rule567() { + int points = 0; + for (int i=0; i<4; ++i) + switch (cnt[i]) { + case 0: + case 1: points += 2; break; + case 2: points++; + } + return points; +} + +int stops() { + bool ss[4] = {}; + for (int i=0; i<13; ++i) { + int suit = so[c[i][1]], + cc = cnt[suit]; + switch (c[i][0]) { + case 'A': + ss[suit] = true; + break; + case 'K': + if (cc >= 2) + ss[suit] = true; + break; + case 'Q': + if (cc >= 3) + ss[suit] = true; + } + } + return ss[0] + ss[1] + ss[2] + ss[3]; +} + +char bidSuit() { + if (cnt[0] >= cnt[1] && cnt[0] >= cnt[2] && cnt[0] >= cnt[3]) + return 'S'; + + if (cnt[1] >= cnt[2] && cnt[1] >= cnt[3]) + return 'H'; + + if (cnt[2] >= cnt[3]) + return 'D'; + + return 'C'; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + so['S'] = 0; so['H'] = 1; + so['D'] = 2; so['C'] = 3; + + while (cin >> c[0]) { + for (int i=1; i<13; ++i) + cin >> c[i]; + + memset(cnt, 0, sizeof(cnt)); + for (int i=0; i<13; ++i) + cnt[so[c[i][1]]]++; + + int points = rule1234(); + if (points >= 16 && stops() == 4) + cout << "BID NO-TRUMP\n"; + + else if (points + rule567() >= 14) + cout << "BID " << bidSuit() << '\n'; + + else cout << "PASS\n"; + } +} diff --git a/uva_cpp_clean/462/462-9.cpp b/uva_cpp_clean/462/462-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40f5a3f5f8ae8d24d86ea04d46b4f4d40c62ce98 --- /dev/null +++ b/uva_cpp_clean/462/462-9.cpp @@ -0,0 +1,94 @@ +/** + * > Author : TISparta + * > Date : 11-08-18 + * > Tags : Implementation + * > Difficulty : 3 / 10 + */ + +#include + +using namespace std; + +int total_score, mx_cards; +map > suit; + +int getDec (const vector & arr) { + int ret = 0; + if (arr.size() == 1 and arr[0] == 'K') ret += 1; + if (arr.size() <= 2 and find(begin(arr), end(arr), 'Q') != end(arr)) ret += 1; + if (arr.size() <= 3 and find(begin(arr), end(arr), 'J') != end(arr)) ret += 1; + return ret; +} + +int getInc (const vector & arr) { + if (arr.size() == 2) return 1; + if (arr.size() <= 1) return 2; + return 0; +} + +bool isStopped (const vector & arr) { + if (find(begin(arr), end(arr), 'A') != end(arr)) return true; + if (arr.size() >= 2 and find(begin(arr), end(arr), 'K') != end(arr)) return true; + if (arr.size() >= 3 and find(begin(arr), end(arr), 'Q') != end(arr)) return true; + return false; +} + +void solve () { + int inc = 0, dec = 0, stopped = 0; + for (char ch: {'S', 'H', 'D', 'C'}) { + dec += getDec(suit[ch]); + inc += getInc(suit[ch]); + stopped += isStopped(suit[ch]); + } + total_score -= dec; + if (total_score >= 16 and stopped == 4) { + puts("BID NO-TRUMP"); + return; + } + total_score += inc; + if (total_score < 14) { + puts("PASS"); + return; + } + for (char ch: {'S', 'H', 'D', 'C'}) { + if (suit[ch].size() == mx_cards) { + printf("BID %c\n", ch); + return; + } + } +} + +int valueOf (char ch) { + if (ch == 'J') return 1; + if (ch == 'Q') return 2; + if (ch == 'K') return 3; + if (ch == 'A') return 4; + return 0; +} + +bool read () { + string line; + if (not getline(cin, line)) return false; + istringstream ss(line); + string s; + while (ss >> s) { + total_score += valueOf(s[0]); + suit[s[1]].push_back(s[0]); + mx_cards = max(mx_cards, (int) suit[s[1]].size()); + } + return true; +} + +void clear () { + mx_cards = 0; + total_score = 0; + suit.clear(); +} + +int main () { + while (read()) { + solve(); + clear(); + } + return (0); +} diff --git a/uva_cpp_clean/465/465-20.cpp b/uva_cpp_clean/465/465-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b8809d48456e675b79d8bcfaaa29aa40a504792 --- /dev/null +++ b/uva_cpp_clean/465/465-20.cpp @@ -0,0 +1,22 @@ +#include + +const long MAX = 2147483647; +char expression[505], sign; +double num1, num2, res; + +int main() +{ + while( gets(expression) ) + { + sscanf(expression, "%lf %c %lf", &num1, &sign, &num2); + + if( sign=='+' ) res = num1+num2; + else res = num1*num2; + + printf("%s\n", expression); + if( num1>MAX ) printf("first number too big\n"); + if( num2>MAX ) printf("second number too big\n"); + if( res>MAX ) printf("result too big\n"); + } + return 0; +} diff --git a/uva_cpp_clean/465/465-21.cpp b/uva_cpp_clean/465/465-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2898c09b8eae1316d73a29da7dfe66a9e6514a3 --- /dev/null +++ b/uva_cpp_clean/465/465-21.cpp @@ -0,0 +1,56 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 465 + Name: Overflow + Problem: https://onlinejudge.org/external/4/465.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MAXINT 2147483647 + +int len(char *s) { + int i=0, l=0; + for (; s[i] && s[i]=='0'; i++); + for (; s[i]; i++) l++; + return l; +} + +int main(){ + long long int x1, x2; + char line[100000], w1[10000], w2[10000], op; + + while (cin.getline(line, 100000) && line[0]) { + if (sscanf(line, "%s %c %s", w1, &op, w2) == 3) { + cout<10 || x1>MAXINT) + cout<<"first number too big\n", f=1; + + if (l2>10 || x2>MAXINT) + cout<<"second number too big\n", f=1; + + if (!f) + if ( + (op=='*' && l1+l2>11) || + (op=='+' ? x1+x2 : x1*x2)>MAXINT + ) f=1; + + if (f && op=='*' && (!l1 || !l2)) f=0; + if (f) cout << "result too big\n"; + } + } +} diff --git a/uva_cpp_clean/465/465-5.cpp b/uva_cpp_clean/465/465-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d30b5f6488c30610ddca5fbe254e09d0f7257239 --- /dev/null +++ b/uva_cpp_clean/465/465-5.cpp @@ -0,0 +1,71 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) + +using namespace std; +int main() +{ + double a,b; + char c[10],s[1100]; + while(gets(s)) + { + sscanf(s,"%lf%s%lf",&a,c,&b); + puts(s); + if(a>2147483647) + pf("first number too big\n"); + if(b>2147483647) + pf("second number too big\n"); + if(c[0]=='+') + a=a+b; + else + a=a*b; + + if(a>2147483647) + pf("result too big\n"); + // pf("%c",c[0]); + + } + + return 0; +} + diff --git a/uva_cpp_clean/466/466-21.cpp b/uva_cpp_clean/466/466-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..baff7a65dca19750fc5ccc94119726d6a8a313b3 --- /dev/null +++ b/uva_cpp_clean/466/466-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 466 + Name: Mirror, Mirror + Problem: https://onlinejudge.org/external/4/466.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +char msgs[9][100] = { + "preserved", + "rotated 90 degrees", + "rotated 180 degrees", + "rotated 270 degrees", + "reflected vertically", + "reflected vertically and rotated 90 degrees", + "reflected vertically and rotated 180 degrees", + "reflected vertically and rotated 270 degrees", + "improperly transformed" +}; + +int n; +char X[20][20], Y[20][20], tmp[20][20]; + +void rotate() { + for (int i=0; i + +#define SIZE 20 + +using namespace std; + +int n,tc; +char source[SIZE][SIZE], target[SIZE][SIZE], aux[SIZE][SIZE]; +bool ok; + +bool check(){ + for(int i = 0;i < n; i++) + for(int j = 0;j < n; j++) if(source[i][j] != target[i][j]) return false; + return true; +} + +void rotate(){ + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) aux[i][j] = source[n - j - 1][i]; + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) source[i][j] = aux[i][j]; +} + +void reflect(){ + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) aux[i][j] = source[n- i - 1][j]; + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) source[i][j] = aux[i][j]; +} + +int main(){ + while(scanf("%d\n",&n)==1){ + ok = false; + for(int i = 0; i < n; i++) scanf("%s %s", source[i], target[i]); + for(int i = 1; i <= 2 && ok == false;i++){ + if( check() ){ + if(i == 1) printf("Pattern %d was preserved.\n", ++tc); + else printf("Pattern %d was reflected vertically.\n", ++tc); + ok = true; + break; + } + for(int j = 1; j <= 3; j++){ + rotate(); + if(check()){ + if(i == 1) printf("Pattern %d was rotated %d degrees.\n",++tc,90*j); + else printf("Pattern %d was reflected vertically and rotated %d degrees.\n",++tc,90*j); + ok = true; + break; + } + } + rotate(); + reflect(); + } + if(ok == false) printf("Pattern %d was improperly transformed.\n",++tc); + } + return(0); +} diff --git a/uva_cpp_clean/467/467-21.cpp b/uva_cpp_clean/467/467-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b919b18cf671592eea78e9dade97a388abd029ec --- /dev/null +++ b/uva_cpp_clean/467/467-21.cpp @@ -0,0 +1,101 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 467 + Name: Synching Signals + Problem: https://onlinejudge.org/external/4/467.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +template +class IndexQueue { + int mind, maxd; + vector V[4000]; + void _update() { + while (mind<=maxd && V[mind].empty()) ++mind; + if (mind>maxd) clear(); + } + + public: + IndexQueue() { mind=INF; maxd=-1; } + bool empty() { _update(); return mind>maxd; } + void clear() { + for (int i=mind; i<=maxd; ++i) + V[i].clear(); + mind=INF; + maxd=-1; + } + + void push(int key, T data) { + V[key].push_back(data); + if (key < mind) mind = key; + if (key > maxd) maxd = key; + } + + int top() { return mind; } + T pop() { + _update(); + T r = V[mind].back(); + V[mind].pop_back(); + return r; + } +}; + +// ---------------------------------------------------------------- + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } + +IndexQueue q; +int X[10043], n; +bool isg[17]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string line; + for (int cse=1; getline(cin, line); ++cse) { + int x = n = 0; + for (int i=0; i +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + string s; + int test, n, m; + + cin >> test >> s; + + while (test--) + { + vector land; + + while (cin >> s , s[0] != 'W' || s[0] != 'L') + { + land.push_back(s); + } + + istringstream is(s); + + while (cin >> s , s[0] != '\n') + { + + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/469/469-19.cpp b/uva_cpp_clean/469/469-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d81bb616ffb0e6fe167964c2f1fb4fc1444a917 --- /dev/null +++ b/uva_cpp_clean/469/469-19.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; + +int flood[110][110]; +int N, M; + +void proc(int i, int j, int & val) { + if (i >= 0 && j >= 0 && i < N && j < M && flood[i][j] == -1) { + flood[i][j] = -2; + val++; + proc(i-1, j-1, val); + proc(i, j-1, val); + proc(i+1, j-1, val); + proc(i-1, j, val); + proc(i+1, j, val); + proc(i-1, j+1, val); + proc(i, j+1, val); + proc(i+1, j+1, val); + } +} + +void proc2(int i, int j, int val) { + if (i >= 0 && j >= 0 && i < N && j < M && flood[i][j] == -2) { + flood[i][j] = val; + proc2(i-1, j-1, val); + proc2(i, j-1, val); + proc2(i+1, j-1, val); + proc2(i-1, j, val); + proc2(i+1, j, val); + proc2(i-1, j+1, val); + proc2(i, j+1, val); + proc2(i+1, j+1, val); + } +} + +int main() { + int T; + cin >> T; + string line; + getline(cin, line); + while (T--) { + getline(cin, line); + N = M = 0; + do { + getline(cin, line); + M = line.length(); + for (int i = 0; i < M; i++) { + if (line[i] == 'L') flood[N][i] = 0; + else flood[N][i] = -1; + } + N++; + } while(cin.peek() == 'L' || cin.peek() == 'W'); + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + int val = 0; + proc(i, j, val); + proc2(i, j, val); + } + } + do { + getline(cin, line); + stringstream ss(line); + int x, y; + ss >> x >> y; + printf("%d\n", flood[x-1][y-1]); + } while(isdigit(cin.peek())); + if (T) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/469/469-20.cpp b/uva_cpp_clean/469/469-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74bb9c3015f0adf86b09c0e4ef5ce37ae6441fb6 --- /dev/null +++ b/uva_cpp_clean/469/469-20.cpp @@ -0,0 +1,55 @@ +#include + +#define isValid( nx, ny ) ( (nx >= 0 && nx < n) && (ny >= 0 && ny < m) ) + +using namespace std; + +const int fx[] = { +0, +0, +1, -1, -1, +1, -1, +1 }; +const int fy[] = { -1, +1, +0, +0, +1, +1, -1, -1 }; + +const int MAXN = 99; +vector grid; +bool visited[MAXN+2][MAXN+2]; +string input; +int n, m, counter; + +void dfs( int x, int y ) { + visited[x][y] = true; + ++counter; + + for( int i = 0 ; i < 8 ; ++i ) { + int X = x + fx[i]; + int Y = y + fy[i]; + if( isValid( X, Y ) && grid[X][Y] == 'W' && !visited[X][Y] ) dfs( X, Y ); + } +} + +int main( int argc, char ** argv ) { + int tc; + scanf( "%d ", &tc ); + for( int nCase = 1 ; nCase <= tc ; ++nCase ) { + grid.clear(); + memset( visited, false, sizeof( visited ) ); + while( getline( cin, input ) && ( input[0] == 'W' || input[0] == 'L' ) ) grid.push_back( input ); + + n = grid.size(), m = grid[0].size(); + int r, c; + istringstream in( input ); + in >> r >> c; + counter = 0; + dfs( (r - 1), (c - 1) ); + + if( nCase > 1 ) puts( "" ); + printf( "%d\n", counter ); + while( getline( cin, input ) && !input.empty() ) { + istringstream in( input ); + in >> r >> c; + memset( visited, false, sizeof( visited ) ); + counter = 0; + dfs( (r - 1), (c - 1) ); + + printf( "%d\n", counter ); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/469/469-21.cpp b/uva_cpp_clean/469/469-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89669a984751dc31ea0ea02d0013bd9bcb978a0a --- /dev/null +++ b/uva_cpp_clean/469/469-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 469 + Name: Wetlands of Florida + Problem: https://onlinejudge.org/external/4/469.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXL 300 +#define MAXN 105 +char line[MAXN][MAXL]; +bool seen[MAXN][MAXN]; +int n, m; + +int dfs(int x, int y) { + if (x<0 || y<0 || x>=m || y>=n || seen[y][x] || line[y][x]!='W') + return 0; + + seen[y][x] = 1; + int sum=1; + for (int i=-1; i<=1; i++) + for (int j=-1; j<=1; j++) + if (i || j) + sum += dfs(x+i, y+j); + + return sum; +} + + +int main(){ + int T, x, y; + cin>>T; + cin.getline(line[0], MAXL); + cin.getline(line[0], MAXL); + + while (T--) { + n=0; + while (cin.getline(line[n], MAXL) && (line[n][0]=='L' || line[n][0]=='W')) + n++; + m = strlen(line[0]); + + while (line[n][0]) { + for (int i=0; i +using namespace std; +using vs=vector; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t; + string s; + cin>>t; + getline(cin,s); + getline(cin,s); + for(int T=0;Tdfs=[&](int y,int x,int k){ + c[y][x]=k; + if(x>0&&!c[y][x-1]&&a[y][x-1]=='W')dfs(y,x-1,k); + if(y>0&&x>0&&!c[y-1][x-1]&&a[y-1][x-1]=='W')dfs(y-1,x-1,k); + if(y>0&&!c[y-1][x]&&a[y-1][x]=='W')dfs(y-1,x,k); + if(y>0&&x0&&!c[y+1][x-1]&&a[y+1][x-1]=='W')dfs(y+1,x-1,k); + }; + int k=1; + for(int i=0;i>y>>x; + y--;x--; + cout< + +#define SIZE 110 + +using namespace std; + +int tc, N, M, row, r, c; +char grid[SIZE][SIZE], line[SIZE]; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M ); +} + +int floodfill(int r, int c, char ch1, char ch2){ + if( not is_possible(r, c) ) return 0; + if( grid[r][c] != ch1 ) return 0; + grid[r][c] = ch2; + int ans = 1; + for(int d = 0; d < 8; d++) + ans += floodfill(r + dr[d], c + dc[d], ch1, ch2); + return ans; +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + N = 0; + while(cin.getline(line, SIZE)){ + if( isdigit(line[0]) ) break; + strcpy(grid[N++], line); + } + M = strlen(grid[0]); + do{ + if( sscanf(line, "%d %d", &r, &c) != 2 ) break; + printf("%d\n", floodfill(r - 1, c - 1, 'W', '.')); + floodfill(r - 1, c - 1, '.', 'W'); + }while(cin.getline(line, SIZE)); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/471/471-2.cpp b/uva_cpp_clean/471/471-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8698254b12838eb440ac4a60c5088bdcf36979f2 --- /dev/null +++ b/uva_cpp_clean/471/471-2.cpp @@ -0,0 +1,60 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +bool f[100]; +bool check(ll i) { + memset(f, 0, sizeof f); + while (i) { + int d = i%10; + if (f[d]) return 0; + f[d] = 1; + i/=10; + } + return 1; +} + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; scanf("%d", &tc); + while (tc--) { + ll n; scanf("%lld", &n); + for (ll i = 1;;++i) { + if (!check(i)) continue; + ll res = n*i; + if (res > INF) break; + if (check(res)) printf("%lld / %lld = %lld\n", res, i, n); + } + if (tc) printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/471/471-21.cpp b/uva_cpp_clean/471/471-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3afff744edb8c40933fa1850f753ef47c2a805b --- /dev/null +++ b/uva_cpp_clean/471/471-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 471 + Name: Magic Numbers + Problem: https://onlinejudge.org/external/4/471.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool seen[10]; +int magics[10000000], cnt; +void generate(int x) { + if (x < 100000) + for (int i=!x; i<10; ++i) + if (!seen[i]) { + seen[i] = 1; + generate(magics[cnt++] = x*10 + i); + seen[i] = 0; + } +} + +bool ismagic(long long x) { + bool seen[10]={}; + for (; x; x/=10) { + int d = x%10; + if (seen[d]) return 0; + seen[d] = 1; + } + return 1; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + generate(0); + sort(magics, magics+cnt); + + int T; + cin >> T; + while (T--) { + long long lst, n; cin >> n; + for (int i=0; ; ++i) { + if (i < cnt) lst = magics[i]; + else while (!ismagic(++lst)); + + long long m = n * lst; + if (m > 9876543210LL) break; + if (ismagic(m)) + cout << m << " / " << lst << " = " << n << endl; + } + + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/471/471-9.cpp b/uva_cpp_clean/471/471-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c0e8b17b148de065693f5bb8c6e200a6613ccc --- /dev/null +++ b/uva_cpp_clean/471/471-9.cpp @@ -0,0 +1,31 @@ +#include + +typedef long long ll; + +using namespace std; + +ll N,tmp,a,lim; +int tc, used[10]; +bool ok; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%lld", &N); + for(ll b = 1; b <= 9876543210; b++){ + a = b * N, ok = true; + if(a > (ll)(1e10)) break; + memset(used, 0, sizeof used), tmp = a; + while(tmp) used[tmp % 10]++, tmp /= 10; + for(int j = 0; j < 10; j++) + if(used[j] > 1) ok = false; + memset(used, 0, sizeof used), tmp = b; + while(tmp) used[tmp % 10]++, tmp /= 10; + for(int j = 0; j < 10; j++) + if(used[j] > 1) ok = false; + if(ok) printf("%lld / %lld = %lld\n", a, b, N); + } + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/473/473-21.cpp b/uva_cpp_clean/473/473-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8cfb3dfe4a9502206af884b26f365fe23b36cdf --- /dev/null +++ b/uva_cpp_clean/473/473-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 473 + Name: Raucous Rockers + Problem: https://onlinejudge.org/external/4/473.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int X[10240], t, cse, + memo[10240][128][128], + memoid[10240][128][128]; +int rec(int s, int d, int rem) { + if (s<0 || d<0) return 0; + if (rem<=0) return rec(s, d-1, t); + if (memoid[s][d][rem] == cse) + return memo[s][d][rem]; + + int res = rec(s-1, d, rem); + if (X[s] <= rem) + res = max(res, rec(s-1, d, rem-X[s]) + 1); + else res = max(res, rec(s, d-1, t)); + + memoid[s][d][rem] = cse; + return memo[s][d][rem] = res; +} + + +int main() { + int T, n, m; + scanf("%d", &T); + for(cse=1; cse<=T; cse++) { + scanf("%d%d%d", &n, &t, &m); // 10240 128 128 + for (int s=0; s +#include + +using namespace std; + +vector, pair>> rectangle(10); + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + char c; + int figure_num, count = 0, point_num = 1; + double x, y; + bool flag; + + while (cin >> c && c != '*') { + cin >> rectangle[count].first.first >> rectangle[count].first.second >> rectangle[count].second.first >> rectangle[count].second.second; + count++; + } + + while (cin >> x >> y) { + if (x == 9999.9 && y == 9999.9) + break; + + flag = true; + + for (figure_num = 0; figure_num < count; figure_num++) + // 注意邊界不算 + if (rectangle[figure_num].first.first < x && rectangle[figure_num].second.first > x && rectangle[figure_num].second.second < y && rectangle[figure_num].first.second > y) { + flag = false; + cout << "Point " << point_num << " is contained in figure " << figure_num + 1 << "\n"; + } + + if (flag) + cout << "Point " << point_num << " is not contained in any figure\n"; + + point_num++; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/476/476-18.cpp b/uva_cpp_clean/476/476-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1f6005d811888a55e95bb1593abd7a0bb9a52c41 --- /dev/null +++ b/uva_cpp_clean/476/476-18.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main(){ + double x, y, xo[11], xf[11], yo[11], yf[11]; + int n = 0, cont = 0, flag = 0; + char type; + while(scanf("%c", &type) && type != '*'){ + if(type != 'r') continue; + scanf("%lf %lf %lf %lf", &xo[n], &yo[n], &xf[n], &yf[n]); + n++; + } + while(scanf("%lf %lf", &x, &y) && !(x == 9999.9 && y == 9999.9)){ + flag = 0; + cont++; + for(int i = 0; i < n; i++){ + if(x > xo[i] && x < xf[i] && yo[i] > y && y > yf[i]) printf("Point %d is contained in figure %d\n", cont, i + 1), flag++; + } + if(!flag) printf("Point %d is not contained in any figure\n", cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/477/477-21.cpp b/uva_cpp_clean/477/477-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc7562f76845ce48549974b65cdbbc6f67fc7f9f --- /dev/null +++ b/uva_cpp_clean/477/477-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 477 + Name: Points in Figures: Rectangles and Circles + Problem: https://onlinejudge.org/external/4/477.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +struct CR { + bool R; + double x1, y1, r; + double x2, y2; + + bool includes (double x, double y) { + if (R) return x>x1 && xy1 && y>t && t!='*'; n++) { + cin >> figures[n].x1 >> figures[n].y1; + if (t=='r') { + figures[n].R = true; + cin >> figures[n].x2 >> figures[n].y2; + + if (figures[n].x2 < figures[n].x1) + swap(figures[n].x2, figures[n].x1); + + if (figures[n].y2 < figures[n].y1) + swap(figures[n].y2, figures[n].y1); + } + else cin >> figures[n].r; + } + + double x, y; + for (int p=1; cin>>x>>y && (x!=9999.9 || y!=9999.9); p++) { + bool inSome=false; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 478 + Name: Points in Figures: Rectangles, Circles, Triangles + Problem: https://onlinejudge.org/external/4/478.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define EPS 1e-7 + +double sqr(double x) { return x*x; } + +bool leftTurn(double x1, double y1, double x2, double y2, double x3, double y3) { + return (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1) > EPS; +} + +struct Shape { + char type; + double x[3], y[3]; + + bool has(double xx, double yy) { + switch (type) { + case 'c': + return sqr(xx-x[0]) + sqr(yy-y[0]) < sqr(x[1]); + + case 'r': + return xx>x[0] && xxy[0] && yy>t && t!='*'; n++) { + Shape &s = sh[n]; + s.type = t; + + cin >> s.x[0] >> s.y[0] >> s.x[1]; + if (t == 'r') { + cin >> s.y[1]; + + if (s.x[0]>s.x[1]) + swap(s.x[0], s.x[1]); + + if (s.y[0]>s.y[1]) + swap(s.y[0], s.y[1]); + } + + else if (t == 't') { + cin >> s.y[1] >> s.x[2] >> s.y[2]; + if (!leftTurn(s.x[0], s.y[0], s.x[1], s.y[1], s.x[2], s.y[2])) { + swap(s.x[0], s.x[1]); + swap(s.y[0], s.y[1]); + } + } + } + + int cse = 0; + double x, y; + while (cin>>x>>y && (x!=9999.9 || y!=9999.9)) { + cse++; + bool outer = 1; + for (int i=0; i +#include +#include +#include +#include + +using namespace std; + +struct Point +{ + double x; + double y; + + Point() + { + x = 0; + y = 0; + } + + Point(double _x, double _y) + { + x = _x; + y = _y; + } + + bool operator<(const Point &p) const + { + if (x != p.x) + { + return x < p.x; + } + + return y < p.y; + } +}; + +class Figure +{ +public: + virtual void Check(Point &p, bool &result) = 0; +}; + +class Rectangle : public Figure +{ +public: + Rectangle(string line); + void Check(Point &p, bool &result); + +private: + Point p1; + Point p2; +}; + +class Triangle : public Figure +{ +public: + Triangle(string line); + void Check(Point &p, bool &result); + +private: + Point p1; + Point p2; + Point p3; + vector points; +}; + +class Circle : public Figure +{ +public: + Circle(string line); + void Check(Point &p, bool &result); + + Point p; + double r; +}; + +Rectangle::Rectangle(string line) +{ + char t; + double x1, x2, y1, y2; + stringstream s; + + s << line; + s >> t >> x1 >> y1 >> x2 >> y2; + + p1 = Point(x1, y1); + p2 = Point(x2, y2); +} + +void Rectangle::Check(Point &p, bool &result) +{ + if (p2 < p1) + { + swap(p1, p2); + } + + if ((((p.x > p1.x) && (p.x < p2.x)) && ((p.y > p1.y) && (p.y < p2.y))) || (((p.x > p1.x) && (p.x < p2.x)) && ((p.y < p1.y) && (p.y > p2.y)))) + { + result = true; + return; + } + + result = false; +} + +Triangle::Triangle(string line) +{ + char t; + double x1, x2, x3, y1, y2, y3; + stringstream s; + + s << line; + s >> t >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; + + p1 = Point(x1, y1); + p2 = Point(x2, y2); + p3 = Point(x3, y3); + + points.push_back(p1); + points.push_back(p2); + points.push_back(p3); + + sort(points.begin(), points.end()); +} + +void Triangle::Check(Point &p, bool &result) +{ + bool in = 0; + int n = 3; + + for (int i = 0, j = n - 1; i < n; j = i++) + { + if ((points[i].y <= p.y && p.y < points[j].y) || (points[j].y <= p.y && p.y < points[i].y)) + { + if (p.x < (points[j].x - points[i].x) * (p.y - points[i].y) * 1.0 / (points[j].y - points[i].y) + points[i].x) + { + in ^= 1; + } + } + } + + result = in; +} + +Circle::Circle(string line) +{ + char t; + double x1, y1, _r; + stringstream s; + + s << line; + s >> t >> x1 >> y1 >> _r; + + p = Point(x1, y1); + r = _r; +} + +void Circle::Check(Point &p, bool &result) +{ + if ((pow(p.x - this->p.x, 2) + pow(p.y - this->p.y, 2)) < r * r) + { + result = true; + return; + } + + result = false; +} + +int main() +{ + vector points; + vector
figures; + + int mode = 1; + string line; + + while (getline(cin, line)) + { + if (line.length() > 0 && line[0] == '*') + { + mode = 2; + continue; + } + + if (mode == 1) + { + switch (line[0]) + { + case 'r': + { + Figure *r = new Rectangle(line); + figures.push_back(r); + } + break; + case 't': + { + Figure *t = new Triangle(line); + figures.push_back(t); + } + break; + case 'c': + { + Figure *c = new Circle(line); + figures.push_back(c); + } + break; + } + } + else if (mode == 2) + { + double x, y; + stringstream s; + + s << line; + s >> x >> y; + + if (x == 9999.9 && y == 9999.9) + { + break; + } + + Point point(x, y); + points.push_back(point); + } + } + + bool result; + + for (unsigned int i = 0; i < points.size(); i++) + { + bool ok = false; + for (unsigned int j = 0; j < figures.size(); j++) + { + figures[j]->Check(points[i], result); + + if (result) + { + cout << "Point " << i + 1 << " is contained in figure " << j + 1 << endl; + ok = true; + } + } + + if (!ok) + { + cout << "Point " << i + 1 << " is not contained in any figure" << endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/481/481-13.cpp b/uva_cpp_clean/481/481-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5837c3651de413287760ce22567f6b4c992ddcd4 --- /dev/null +++ b/uva_cpp_clean/481/481-13.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, answer, value; + +vector myArray; + +stack sub; +vector andis; +vector parent; + +int LIS() +{ + int length = 0; + + vector t(n, 0); + + for (int i = 0; i < n; i++) + { + int wh = lower_bound(t.begin(), t.begin() + length, myArray[i]) - t.begin(); + + t[wh] = myArray[i]; + + andis[wh] = i; + + if (wh == 0) + { + parent[i] = -1; + } + + else + { + parent[i] = andis[wh - 1]; + } + + if (length == wh) + { + length++; + } + } + + value = andis[length - 1]; + + return length; +} + +int main() +{ + int x = 1; + + while (cin >> x) + { + myArray.push_back(x); + } + + n = myArray.size(); + + parent.resize(n); + andis.resize(n); + + answer = LIS(); + + cout << answer << endl << "-" << endl; + + while (value != -1) + { + sub.push(myArray[value]); + + value = parent[value]; + } + + while (!sub.empty()) + { + cout << sub.top() << endl; + + sub.pop(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/481/481-14.cpp b/uva_cpp_clean/481/481-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..baeaf4ead90325a46a098162bd96c839e84521d2 --- /dev/null +++ b/uva_cpp_clean/481/481-14.cpp @@ -0,0 +1,115 @@ +/*************************************************** + * Problem name : 481 What Goes Up.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/481.pdf + * OJ : Uva + * Verdict : AC + * Date : 14.06.2017 + * Problem Type : DP(LIS) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +ll L[MAX], I[MAX]; +ll LIS_NlogK(ll n , vectorar) { + I[0] = -100000000; + //~ for (ll i = 1; i <= n; i++) { + //~ I[i] = 100000000; + //~ } + ll Maxlen = 0; + for (ll i = 0; i < n; i++) { + ll low = 0, high = Maxlen, mid; + while (high >= low) { + mid = (low + high) / 2; + if (I[mid] < ar[i]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + I[low] = ar[i]; + L[i] = low; + if(Maxlen < low){ + Maxlen = low; + } + } + return Maxlen; +} +void LIS_Solution(ll n, vectorar) { + vectorSol; + ll i = 0; + for (ll j = 1 ; j < n; j++) { + if (L[j] >= L[i]) { + i = j; + } + } + Sol.push_back(ar[i]); + ll top = i; + for (ll i = top - 1; i >= 0; i--) { + if ( ar[i] < ar[top] && L[i] == L[top] - 1) { + Sol.push_back(ar[i]); + top = i; + } + } + ll sz = Sol.size(); + for (ll i = sz - 1 ; i >= 0; i--) { + printf("%lld\n", Sol[i]); + } + Sol.clear(); +} +int main () { + vectorV; + ll x; + while (scanf("%lld", &x) != EOF) { + V.push_back(x); + } + ll n = V.size(); + ll result = LIS_NlogK(n,V); + printf("%lld\n", result); + printf("-\n"); + LIS_Solution(n,V); + V.clear(); + return 0; +} + diff --git a/uva_cpp_clean/481/481-15.cpp b/uva_cpp_clean/481/481-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4299dc867f931ba93a301b219163886b63c5aa69 --- /dev/null +++ b/uva_cpp_clean/481/481-15.cpp @@ -0,0 +1,52 @@ +/* + 最長遞增子序列 + 時間複雜度: O(NlogN) +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + vector sequence, LIS, pos(1, 0); + sequence.reserve(500000); + + int i, n, LIS_length; + + while (cin >> i) + sequence.push_back(i); + + n = sequence.size(); + pos.reserve(n); + LIS.reserve(n); + + LIS.push_back(sequence[0]); + + // 這裡只能算出 LIS 長度和 pos 資料 + for (i = 1; i < n; i++) { + if (sequence[i] > LIS.back()) { + pos.push_back(LIS.size()); + LIS.push_back(sequence[i]); // sequence[i] 比全部都大 + } else { + vector::iterator it = lower_bound(LIS.begin(), LIS.end(), sequence[i]); // 找到大於 sequence[i] 的最小元素 + *it = sequence[i]; + pos.push_back(distance(LIS.begin(), it)); + } + } + + LIS_length = LIS.size(); + cout << LIS_length-- << "\n-\n"; + + for (i = n - 1; i > -1; i--) + if (pos[i] == LIS_length) + LIS[LIS_length--] = sequence[i]; // 真正的 LIS 要用長度和 pos 算 + + for (const int& i : LIS) + cout << i << "\n"; + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/481/481-21.cpp b/uva_cpp_clean/481/481-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a85b303845b047124d62e4524724ff78b08d3b1 --- /dev/null +++ b/uva_cpp_clean/481/481-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 481 + Name: What Goes Up + Problem: https://onlinejudge.org/external/4/481.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define MAXN 100000 +int n, X[MAXN], M[MAXN], P[MAXN], S[MAXN]; +int main(){ + for (n=0; cin>>X[n]; n++); + int l = 0; + for (int i=0; i>1; + if (X[M[mid]] < X[i]) + lo = mid+1; + else + hi = mid-1; + } + + P[i] = M[lo-1]; + M[lo] = i; + if (lo > l) l = lo; + } + + printf("%d\n-\n", l); + + int k = M[l]; + for (int i=l-1; i>=0; i--) { + S[i] = X[k]; + k = P[k]; + } + + for (int i=0; i longest increasing subsequence (LIS) + difficulty: hard + date: 22/Jan/2020 + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + vector A; + int tmp; while (cin >> tmp) A.push_back(tmp); + int N = A.size(); + + vector L, iL, p(N); + + int jAns = 0; + for (int j = 0; j < N; j++) { + int posL = lower_bound(L.begin(), L.end(), A[j]) - L.begin(); + + if (posL < L.size()) { + L[posL] = A[j]; iL[posL] = j; + } else { + L.push_back(A[j]); iL.push_back(j); + } + + p[j] = posL ? iL[posL - 1] : -1; + if (posL == L.size()-1) jAns = j; + } + + stack recoveredAns; + for (int j = jAns; j != -1; j = p[j]) + recoveredAns.push(A[j]); + + cout << L.size() << endl << "-" << endl; + for (; !recoveredAns.empty(); recoveredAns.pop()) + cout << recoveredAns.top() << endl; + return 0; +} diff --git a/uva_cpp_clean/481/481-9.cpp b/uva_cpp_clean/481/481-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06dc4cd4ba584287df223390d8d79813b3d5d543 --- /dev/null +++ b/uva_cpp_clean/481/481-9.cpp @@ -0,0 +1,34 @@ +#include + +#define pb push_back + +using namespace std; + +int n, num, pos, lis, idxMax; +vector v, u, path, idx; + +void printPath(int idx){ + if(path[idx] == -1){ + printf("%d\n", v[idx]); + return; + } + printPath(path[idx]); + printf("%d\n", v[idx]); +} + +int main(){ + while(scanf("%d",&num) != EOF) v.pb(num); + n = v.size(); + u.resize(n), idx.resize(n), path.resize(n, -1); + for(int i = 0; i < n; i++){ + pos = lower_bound(u.begin(), u.begin() + lis, v[i]) - u.begin(); + u[pos] = v[i]; + idx[pos] = i; + path[i] = pos ? idx[pos - 1] : -1; + if(pos + 1 > lis) lis = pos + 1, idxMax = i; + } + printf("%d\n", lis); + puts("-"); + printPath(idxMax); + return(0); +} diff --git a/uva_cpp_clean/482/482-15.cpp b/uva_cpp_clean/482/482-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60964e5850b67ddfa91cba2e64582c7ff8f9902b --- /dev/null +++ b/uva_cpp_clean/482/482-15.cpp @@ -0,0 +1,49 @@ +/* + 排序 + 時間複雜度: O(NlogN) +*/ +#include +#include +#include +#include +#include + +using namespace std; + +bool compare(const pair& a, + const pair& b) { + return a.second < b.second; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, number; + string order; + vector> data_array; + + while (cin >> n) + while (n--) { + cin.ignore(1024, '\n'); + cin.ignore(1024, '\n'); + getline(cin, order); + istringstream order_input(order); + + while (order_input >> number) + data_array.push_back(make_pair(string(), number)); + + for (auto&& i : data_array) + cin >> i.first; + + sort(data_array.begin(), data_array.end(), compare); // 照 number 先排序 + + for (const auto& i : data_array) + cout << i.first << "\n"; + + data_array.clear(); + + if (n) + cout << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/482/482-19.cpp b/uva_cpp_clean/482/482-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc34dba3af68f5c4b66c52710f27c7913d27d8fa --- /dev/null +++ b/uva_cpp_clean/482/482-19.cpp @@ -0,0 +1,76 @@ +// las12uvas contest PROBLEM A 2015 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // max... +#include // pair +#include +#include // int, ll... +#include // double... +#include // abs, atan... +#include // memset +#include +#include // greater, less... +#include +#include +#include +#include + +using namespace std; + +typedef long long ll; +typedef pair ii; +typedef pair ll_ll; +typedef pair dd; +typedef vector vi; +typedef map mii; +typedef vector vii; +typedef vector
vdd; +typedef vector vll; +typedef vector vvi; + +int main() { + + unordered_map map; + + vector vec; + vector per; + int cases; + scanf("%d", &cases); + string c; + getline(cin, c); + for (int i = 0; i < cases; i++) { + per.clear(); + vec.clear(); + map.clear(); + int max = 0; + int k = 0; + getline(cin, c); + if (i != 0) getline(cin, c); + while(cin.peek() != '\n'){ + int l; + cin >> l; + if(l>max) max = l; + per.push_back(l); + map.insert(pair(l, k)); + k++; + } + for(int j = 0; j < max; j++){ + string f; + cin >> f; + vec.push_back(f); + } + for(int j = 0; j < max; j++){ + cout << vec[map[j+1]] << endl; + } + if(i != cases -1)cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/482/482-21.cpp b/uva_cpp_clean/482/482-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec6b194f17b08db5593c1794fb53dd749eb4c98d --- /dev/null +++ b/uva_cpp_clean/482/482-21.cpp @@ -0,0 +1,51 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 482 + Name: Permutation Arrays + Problem: https://onlinejudge.org/external/4/482.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXL 100000 + +struct Num { + int ind; + char x[50]; + bool operator < (const Num& o) const { + return ind < o.ind; + } +}; + +Num X[10000]; + +int main(){ + char line[MAXL]; + int T; + cin>>T; + while (T--) { + cin.getline(line, MAXL); + cin.getline(line, MAXL); + cin.getline(line, MAXL); + + int cnt=0; + for (char *p=strtok(line, " "); p; p=strtok(0, " ")) + sscanf(p, "%d", &X[cnt++].ind); + for (int i=0; i>X[i].x; + + sort(X, X+cnt); + for (int i=0; i +#include + +using namespace std; + +int main(){ + string input; + while(getline(cin, input)){ + int j = 0; + for(int i = 0; i < input.size(); i++){ + j = i; + while(input[i] != ' ' && i < input.size()) i++; + for(int k = i - 1; k >= j; k--) printf("%c", input[k]); + if(i < input.size() - 1) printf(" "); + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/483/483-19.cpp b/uva_cpp_clean/483/483-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3207dbe188e20a92af6a85b29d9075714a1f33af --- /dev/null +++ b/uva_cpp_clean/483/483-19.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +using namespace std; + +int main() { + string s; + while (getline(cin, s)) { + for (int i = 0; i < s.length(); i++) { + if (isspace(s[i])) { + for (int j = i-1; j >= 0 && !isspace(s[j]); j--) printf("%c", s[j]); + printf("%c", s[i]); + } + } + if (!isspace(s[s.length()-1])) { + for (int j = s.length()-1; j >= 0 && !isspace(s[j]); j--) printf("%c", s[j]); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/483/483-20.cpp b/uva_cpp_clean/483/483-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2139159d02a53fe62607ab0fa6048e75bfdbec2b --- /dev/null +++ b/uva_cpp_clean/483/483-20.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + char line[1005]; + + while( gets(line) ) + { + int index = 0; + for(int i=0 ; i=index ; j--) + cout << line[j]; + cout << " "; + index = i+1; + } + } + + for(int j=strlen(line)-1 ; j>=index ; j--) + cout << line[j]; + cout << endl; + } + return 0; +} diff --git a/uva_cpp_clean/483/483-21.cpp b/uva_cpp_clean/483/483-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3874222bec206350f89533c7d679b3fc6b35158 --- /dev/null +++ b/uva_cpp_clean/483/483-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 483 + Name: Word Scramble + Problem: https://onlinejudge.org/external/4/483.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char s[1000],t[1000]; + int i,k=0; + while(gets(s)){ + for(i=0;s[i];i++) + if(s[i]!=' ') + t[k++]=s[i]; + else{ + while(k--)cout< +#include +int main() +{ + char s[1000]; + int i,j,l,p,c; + while(gets(s)) + { + l=strlen(s); + p=0; + c=0; + for(i=0;i=p;j--) + printf("%c",s[j]); + if(c==0) + { + printf(" "); + c=1; + } + p=i; + } + + } + for(i=l-1;i>p;i--) + printf("%c",s[i]); + printf("\n"); + } +return 0; +} diff --git a/uva_cpp_clean/483/483-9.cpp b/uva_cpp_clean/483/483-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e18a38a7a076cac5708535bf03a4a766ab5a9a9c --- /dev/null +++ b/uva_cpp_clean/483/483-9.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +const int LEN = 1000; + +int i, j; +char line[LEN]; + +int main(){ + while(cin.getline(line, LEN)){ + for(i = 0; line[i]; i++){ + j = i; + while(line[j] and line[j] != ' ') j++; + reverse(line + i, line + j); + i = j; + } + printf("%s\n", line); + } + return(0); +} diff --git a/uva_cpp_clean/484/484-19.cpp b/uva_cpp_clean/484/484-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b200b45cb927462ea9b52f2b67d5d5232dbdef9 --- /dev/null +++ b/uva_cpp_clean/484/484-19.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + queue s; + unordered_map m; + int n; + while (cin >> n) { + if (!m[n]) s.push(n); + m[n]++; + } + while (!s.empty()) { + printf("%d %d\n", s.front(), m[s.front()]); + s.pop(); + } + return 0; +} diff --git a/uva_cpp_clean/484/484-21.cpp b/uva_cpp_clean/484/484-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..257301f2225e05af961fb5fee17a937e3a1f5326 --- /dev/null +++ b/uva_cpp_clean/484/484-21.cpp @@ -0,0 +1,30 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 484 + Name: The Department of Redundancy Department + Problem: https://onlinejudge.org/external/4/484.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +map m; +int a[100000000]; + +int main(){ + int t,i,n=0; + while(scanf("%d",&t)==1){ + if(m.count(t)==0) + a[n++]=t; + m[t]++; + } + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pi 2*acos(0.0) +using namespace std; + +int main() +{ + vectorv; + mapmp; + mapfind; + int i,n; + while(scanf("%d",&n)==1) + { + v.push_back(n); + mp[n]++; + } + for(i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 485 + Name: Pascal's Triangle of Death + Problem: https://onlinejudge.org/external/4/485.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + + if (cells) delete [] cells; + cells = ncells; + } + if (!cp) + memset(cells, 0, capacity*CellSize); + else memset(cells+sz, 0, (capacity-sz)*CellSize); + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + // --------------------------------------------------------------------- + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + + BigInt(const char s[]) { + while (s[0]==' ' || s[0]=='\n' || s[0]=='\t') s++; + sign = 1; + if (s[0] == '+') s++; + if (s[0] == '-') { sign=-1; s++; } + while (s[0]=='0') s++; + if (!s[0]) { init(); return; } + + int len = strlen(s); + size = (len+3)/MAX_D; + cells = new CellType[capacity = size+16]; + for (int i=len-1; i>=0; i-=MAX_D) { + int rc = 0; + for (int k=i-MAX_D+1; k<=i; k++) { + if (k<0 || s[k]=='-' || s[k]=='+') continue; + rc = rc*10 + s[k] - '0'; + } + cells[(len-i-1) / MAX_D] = rc; + } + for (int i=size; i= 0) + delete [] cells; + } + + int getSize() { return size; } + int getCell(int i) { return cells[i]; } + bool isZero() { return !sign; } + BigInt& setOne() { sign=size=cells[0]=1; return *this; } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { + return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign; + } + bool operator == (const BigInt &o) const { return sign==o.sign && numEq(o); } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + + int osz = size; + setSize(max(size, o.size)+1); + for (int i=osz; i= MAX_C) { + cells[i] -= MAX_C; + ++cells[i+1]; + } + } + return autoSize(); + } + // --------------------------------------------------------------------- + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + + BigInt DP[300]; + for (int i=0; i<300; ++i) { + cout << 1; + DP[i].setOne(); + for (int j=i-1; j>0; --j) { + DP[j] += DP[j-1]; + cout << ' ' << DP[j]; + + if (DP[j].getSize() >= 16) i=300; + } + if (i) cout << " 1\n"; + else cout << '\n'; + } +} diff --git a/uva_cpp_clean/485/485-9.cpp b/uva_cpp_clean/485/485-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22f1e3828274611f90212e7ad00ff9a6add7fb6d --- /dev/null +++ b/uva_cpp_clean/485/485-9.cpp @@ -0,0 +1,19 @@ +def main(): + SIZE = 210 + C = [[0 for i in range(SIZE)] for j in range(SIZE) ] + for i in range(SIZE): + C[i][0], C[i][i] = 1, 1 + for i in range(2, SIZE): + for k in range(1, i): + C[i][k] = C[i - 1][k] + C[i - 1][k - 1] + ok = False + for i in range(SIZE): + if ok: + break + print(' '.join( str(C[i][j]) for j in range(i + 1))) + mx = max(C[i][j] for j in range(i + 1)) + if mx >= 1e60: + ok = True + +if __name__ == '__main__': + main() diff --git a/uva_cpp_clean/486/486-21.cpp b/uva_cpp_clean/486/486-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..865c144239ce47e7cfc88292d42d1f2c73aaa520 --- /dev/null +++ b/uva_cpp_clean/486/486-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 486 + Name: English-Number Translator + Problem: https://onlinejudge.org/external/4/486.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char words[][10] = { + "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", + "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", + "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" + }; + int nums[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20, 30, 40, 50, 60, 70, 80, 90 + }; + + map values; + for (int i=0; i<28; ++i) + values[words[i]] = nums[i]; + + + string line, word; + while (getline(cin, line)) { + stringstream sin(line); + int sgn = 1, a=0, b=0, c=0, d=0; + while (sin >> word) { + if (word == "negative") sgn = -1; + else { + auto it = values.find(word); + if (it == values.end()) { + if (word == "hundred") { + b = a; + a = 0; + } + else if (word == "thousand") { + c = b*100 + a; + b = a = 0; + } + else if (word == "million") { + d = c*1000 + b*100 + a; + c = b = a = 0; + } + } + else a += it->second; + } + } + + cout << sgn * (d*1000000 + c*1000 + b*100 + a) << endl; + } +} diff --git a/uva_cpp_clean/487/487-21.cpp b/uva_cpp_clean/487/487-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c24b97d4a8541dea6262db4381060344b6150b6 --- /dev/null +++ b/uva_cpp_clean/487/487-21.cpp @@ -0,0 +1,103 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 487 + Name: Boggle Blitz + Problem: https://onlinejudge.org/external/4/487.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +#define MAXS 100143 +char Pch[MAXS]; +int S[MAXS][128], P[MAXS], scnt; +int nextState(int st, char ch) { + if (!S[st][ch]) { + int nst = S[st][ch] = scnt++; + memset(S[nst], 0, 128*sizeof(int)); + Pch[nst] = ch; + P[nst] = st; + } + + if (scnt > MAXS) throw 1; + return S[st][ch]; +} + +char M[21][21]; +bool inside[21][21]; +int seen[MAXS][21][21], cse, n; +void dfs(int st, int i, int j) { + if (seen[st][i][j] == cse) return; + seen[st][i][j] = cse; + + inside[i][j] = 1; + + char cch = M[i][j]; + for (int di=-1; di<2; ++di) { + int ni = i+di; + if (ni>=0 && ni=0 && njcch) + dfs(nextState(st, M[ni][nj]), ni, nj); + } + } + + inside[i][j] = 0; +} + + +int Q[MAXS]; +char str[21*21]; +void printAll() { + int qz = 1, qi = 0; + Q[0] = 0; + for (int d=0; qi 2) { + for (int i=d-1, kst=st; i>=0; --i) + str[i] = Pch[kst], + kst = P[kst]; + + cout << str << endl; + } + + for (int ch=1; ch<128; ++ch) + if (S[st][ch]) + Q[qz++] = S[st][ch]; + } + } +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + for (cse=1; T-- && cin>>n; ++cse) { + cin.ignore(100, '\n'); + for (int i=0; i + +using namespace std; + +int main(){ + int n, amplitude, frecuency, auxIndex, cont = 0; + cin >> n; + while(cont < n){ + cin >> amplitude >> frecuency; + for(int k = 0; k < frecuency; k++){ + auxIndex = 0; + if(k > 0 || cont > 0) cout << endl; + for(int i = 1; i < 2*amplitude; i++){ + if(i > amplitude) auxIndex +=2; + for(int j = 1; j <= i - auxIndex; j++) cout << i - auxIndex; + cout << endl; + } + } + cont++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/488/488-19.cpp b/uva_cpp_clean/488/488-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bbcc75c067ed684abf459bafc7aa3f8f7c353a9 --- /dev/null +++ b/uva_cpp_clean/488/488-19.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main() { + int N; + scanf("%d", &N); + while (N--) { + int a, f; + scanf("%d %d", &a, &f); + while (f--) { + for (int i = 1; i <= a; i++) { + for (int j = 0; j < i; j++) printf("%d", i); + printf("\n"); + } + for (int i = a-1; i > 0; i--) { + for (int j = 0; j < i; j++) printf("%d", i); + printf("\n"); + } + if (N != 0 || f != 0) printf("\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/488/488-5.cpp b/uva_cpp_clean/488/488-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..593da2fb999f4d5c4706ac5e5077c04e6ffc0378 --- /dev/null +++ b/uva_cpp_clean/488/488-5.cpp @@ -0,0 +1,31 @@ +#include +int main() +{ + int n,b,c,i,l,j,k; + scanf("%d",&n); + for(i=0;i0;k--) + { + for(l=k;l>0;l--) + printf("%d",k); + printf("\n"); + } + if(j==c-1 && i==n-1) + continue; + printf("\n"); + } + + } + + return 0; +} diff --git a/uva_cpp_clean/489/489-17.cpp b/uva_cpp_clean/489/489-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc9194928627f118b7936b62ad7acc38dc712809 --- /dev/null +++ b/uva_cpp_clean/489/489-17.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int rnd; + while ((cin >> rnd) && rnd != -1) { + string key, gus; + cin >> key >> gus; + set keys, wrongs; + for (char c : key) + keys.insert(c); + int chance = 7; + for (char gu : gus) + if (keys.find(gu) != keys.end()) { + keys.erase(gu); + if (keys.empty()) + break; + } + else if (wrongs.find(gu) == wrongs.end()){ + wrongs.insert(gu); + --chance; + if (!chance) + break; + } + cout << "Round " << rnd << '\n'; + if (keys.empty()) + cout << "You win.\n"; + else if (!chance) + cout << "You lose.\n"; + else + cout << "You chickened out.\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/489/489-18.cpp b/uva_cpp_clean/489/489-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c97f1562a1b54a23968537c7209c605d7446c798 --- /dev/null +++ b/uva_cpp_clean/489/489-18.cpp @@ -0,0 +1,39 @@ +#include +#include + +using namespace std; //Usar array del alfabeto para indicar que palabras se usan y optimizar la busqueda + +int main(){ + int round = 1, bl, gl, glA; + string word, input; + while(true){ + cin >> round; + if(round == -1) break; + cin >> word >> input; + bl = 0; + gl = 0; + glA = 0; + int alph[27] = {0}; + for(int i = 0; i < word.length(); i++){ //Marcar que letras del alfabeto usa la solucion + if(alph[word[i]-'a'] == 0){ + alph[word[i]-'a'] = 1; + glA +=1; //Ver cuantas letras usa (No tener en cuenta las repetidas) + } + } + cout << "Round " << round << endl; + for(int i = 0; i < input.length() && bl < 7; i++){ + if(alph[input[i]-'a'] == 0){ + bl++; + alph[input[i]-'a'] = 2; + } + else if(alph[input[i]-'a'] == 1){ + gl++; + alph[input[i]-'a'] = 2; + } + } + if(gl == glA) cout << "You win." << endl; + else if(bl >= 7) cout << "You lose." < +#include +using namespace std; + +int counts[30]; + +int main() { + int r; + while (scanf("%d", &r) && r != -1) { + printf("Round %d\n", r); + for (int i = 0; i < 30; i++) counts[i] = 0; + char c; + int chars = 0, strikes = 0; + scanf("%c", &c); + while(scanf("%c", &c) && isalpha(c)) { + chars++; + counts[c-'a']++; + } + bool win = false; + while(scanf("%c", &c) && isalpha(c)) { + if (counts[c-'a'] == 0) strikes++; + else if (counts[c-'a'] != -1) chars -= counts[c-'a']; + counts[c-'a'] = -1; + if (strikes < 7 && chars == 0) win = true; + } + if (win) printf("You win.\n"); + else if (strikes >= 7) printf("You lose.\n"); + else printf("You chickened out.\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/489/489-20.cpp b/uva_cpp_clean/489/489-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a044c75bba35835954c6903b7e6a3de855c092e --- /dev/null +++ b/uva_cpp_clean/489/489-20.cpp @@ -0,0 +1,62 @@ +#include +#include +#include + +using namespace std; + +char solution[105], guess[105]; +bool check[105]; + +int main() +{ + int round_num; + + while( cin >> round_num && round_num!=-1 ) + { + cin >> solution; + cin >> guess; + + cout << "Round " << round_num << endl; + + for(int i=0 ; i<105 ; i++) check[i] = false; + + bool flag = false; + int counter = 0; + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 489 + Name: Hangman Judge + Problem: https://onlinejudge.org/external/4/489.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int r; + bool word[128]; + bool gues[128]; + char ch; + + memset(word, -1, sizeof(word)); + while(cin>>r && r!=-1) { + cout << "Round " << r << endl; + while (getchar()!='\n'); + + int d = 0; + for (int i='a'; i<='z'; i++) word[i]=gues[i]=0; + while ((ch=getchar()) != '\n') + if (!word[ch]) { + word[ch] = true; + d++; + } + + int wa=0, res=-1; + while ((ch=getchar()) != '\n') + if (res<0 && !gues[ch]) { + gues[ch] = true; + if (!word[ch]) wa++; + else d--; + + res = 0; + if (d==0) cout<<"You win.\n"; + else if (wa==7) cout<<"You lose.\n"; + else res=-1; + } + + if (res==-1) cout<<"You chickened out.\n"; + } +} diff --git a/uva_cpp_clean/489/489-9.cpp b/uva_cpp_clean/489/489-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06a33ef79b3f7cfde0af8397209bd429cb2516ca --- /dev/null +++ b/uva_cpp_clean/489/489-9.cpp @@ -0,0 +1,34 @@ +#include + +#define SIZE 30 + +using namespace std; + +int Round, wrong; +bool vis1[SIZE], vis2[SIZE], ok; +char word[SIZE], guess[SIZE]; + +int main(){ + while(scanf("%d\n", &Round), ~Round){ + ok = wrong = 0; + scanf("%s", word); + scanf("%s", guess); + memset(vis1, false, sizeof vis1); + memset(vis2, false, sizeof vis2); + for(int it = 0; word[it]; it++) + vis1[word[it] - 'a'] = true; + for(int it = 0; guess[it] and not ok and wrong < 7; it++) + if( not vis2[guess[it] - 'a']){ + vis2[guess[it] - 'a'] = true; + if( not vis1[guess[it] -'a'] ) wrong++; + ok = true; + for(int it = 0; it < SIZE and ok; it++) + if( vis1[it] and not vis2[it] ) ok = false; + } + printf("Round %d\n", Round); + if(ok) puts("You win."); + else if( wrong == 7 ) puts("You lose."); + else puts("You chickened out."); + } + return(0); +} diff --git a/uva_cpp_clean/490/490-14.cpp b/uva_cpp_clean/490/490-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a91e951ad006c34d751372c2a8a0a524c70dbb34 --- /dev/null +++ b/uva_cpp_clean/490/490-14.cpp @@ -0,0 +1,89 @@ +/*************************************************** + * Problem Name : 490 Rotating Sentences.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/490.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-07-09 + * Problem Type : Easy + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + string str[105]; + int pos = 0, sz = 0; + + while (getline (cin, str[pos]) ) { + sz = max (sz, (int) str[pos].size() ); + pos++; + } + + for (int i = 0; i < sz; i++) { + for (int j = pos - 1; j >= 0; j--) { + if ( (int) str[j].size() > i) { + cout << str[j][i]; + + } else { + cout << " "; + } + } + + nl; + } + + return 0; +} diff --git a/uva_cpp_clean/490/490-15.cpp b/uva_cpp_clean/490/490-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e135c6e286f736568edb38dfa9e6d9d690f388e --- /dev/null +++ b/uva_cpp_clean/490/490-15.cpp @@ -0,0 +1,34 @@ +/* + 陣列轉向 + 時間複雜度: O(N) +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + string sentence[101]; + int i, j, n = 0, length[101]; + int max_length = 0; + + while (getline(cin, sentence[n])) { + length[n] = sentence[n].size(); + max_length = max(max_length, length[n]); + n++; + } + + for (i = 0; i < max_length; i++) { + for (j = n - 1; j > -1; j--) + if (i < length[j]) + cout << sentence[j][i]; + else + cout << " "; + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/490/490-20.cpp b/uva_cpp_clean/490/490-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4a0ea76f9fa9014facfca4cb9c89d96e4328ffc --- /dev/null +++ b/uva_cpp_clean/490/490-20.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +using namespace std; + +char sentence[105][105]; + +int main() +{ + int line = 0, len = 0; + while( gets(sentence[line]) ) + { + if( len=0 ; j--) + { + if( i>=strlen(sentence[j]) ) cout << " "; + else cout << sentence[j][i]; + } + cout << endl; + } + + return 0; +} diff --git a/uva_cpp_clean/490/490-21.cpp b/uva_cpp_clean/490/490-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdb22af51481f0c57a6c241a3708ec30a2d7efcb --- /dev/null +++ b/uva_cpp_clean/490/490-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 490 + Name: Rotating Sentences + Problem: https://onlinejudge.org/external/4/490.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() +{ + char lines[105][105]; + int i=0,j,maxlen=0,t; + while (gets(lines[i])){ + if((t=strlen(lines[i]))>maxlen)maxlen=t; + for(j=t;j<105;j++)lines[i][j]=' '; + i++; + } + t=i-1; + for(j=0;j=0;i--) + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; + +int main() +{ + char s[2000000],c; + while(gets(s)) + { + for(int i=0, len = strlen(s);i + +using namespace std; + +string line, word; + +bool isvowel(char ch){ + ch = tolower(ch); + return ( ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' ); +} + +int main(){ + while(getline(cin, line)){ + for(int it = 0; it < line.size();){ + if( not isalpha(line[it]) ) putchar(line[it++]); + else{ + char ch = line[it]; + word = ""; + if( isvowel(ch) ) word += ch; + for(it++; it < line.size() and isalpha(line[it]); it++) + word += line[it]; + if( not isvowel(ch) ) word += ch; + word += "ay"; + printf("%s", word.c_str()); + } + } + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/494/494-15.cpp b/uva_cpp_clean/494/494-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6dee63eae0a5891e4fa281d74fc7114ed55a8148 --- /dev/null +++ b/uva_cpp_clean/494/494-15.cpp @@ -0,0 +1,27 @@ +/* + 輸入輸出 + 時間複雜度: O(N) +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + string input; + int word; + regex pattern("[^A-Za-z]"); + + while (getline(cin, input)) { + word = 0; + istringstream sentence(regex_replace(input, pattern, " ")); + while (sentence >> input) + word++; + cout << word << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/494/494-5.cpp b/uva_cpp_clean/494/494-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8fc8a6506c043a627c095ed869449de5f1926ac0 --- /dev/null +++ b/uva_cpp_clean/494/494-5.cpp @@ -0,0 +1,24 @@ +#include +int main() +{ + int i,c,f; + char a[1000]; + while(gets(a)) + { + c=0; + f=1; + for(i=0;a[i];i++) + { + if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z')) + { + if(f) + c++; + f=0; + } + else + f=1; + } + printf("%d\n",c); + } + return 0; +} diff --git a/uva_cpp_clean/495/495-14.cpp b/uva_cpp_clean/495/495-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9ca898c4af943f91137c05eac142db46896232f1 --- /dev/null +++ b/uva_cpp_clean/495/495-14.cpp @@ -0,0 +1,135 @@ +/*************************************************** + * Problem name : 495 - Fibonacci Freeze .cpp + * Problem Link : https://uva.onlinejudge.org/external/4/495.pdf + * OJ : Uva + * Verdict : AC + * Date : 11.06.2017 + * Problem Type : String + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 5001 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +string fib[MAX]; +string add(string a, string b) { + int al = a.size() - 1; + int bl = b.size() - 1; + + int carry = 0; + string result = ""; + + while (al >= 0 && bl >= 0) { + int temp = (int)(a[al] - '0') + (int)(b[bl] - '0') + carry ; + carry = 0; + if (temp > 9 ) { + carry = 1; + temp = temp - 10; + } + result += char(temp + '0'); + al--; + bl--; + } + while (al >= 0) { + int temp = (int)(a[al] - '0') + carry ; + carry = 0; + if (temp > 9) { + carry = 1; + temp = temp % 10; + } + + result += char(temp + '0'); + al--; + } + while (bl >= 0) { + int temp = (int)(b[bl] - '0') + carry ; + carry = 0; + if (temp > 9) { + carry = 1; + temp = temp % 10; + } + result += char(temp + '0'); + bl--; + } + if (carry) { + result += "1"; + } + + string addition = ""; + reverse(result.begin(), result.end()); + return result; +} + +string trim(string a) { // for removing leading 0s + string res = ""; + int i = 0; + + while (a[i] == '0') { + i++; + } + int sz = a.size(); + for (; i < sz; i++) { + res += a[i]; + } + return res; +} +void preCal() { + string s1 = "0"; + string s2 = "1"; + fib[0] = s1; + fib[1] = s2; + for (int i = 2; i <= MAX; i++) { + string s = trim(add(s1, s2)); + fib[i] = s; + s1 = s2; + s2 = s; + } +} +int main () { + int n; + preCal(); + while (scanf("%d", &n) == 1) { + cout << "The Fibonacci number for " << n << " is " << fib[n] << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/495/495-20.cpp b/uva_cpp_clean/495/495-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dffad9a25358c2800a721b6f43ac794311a07dc8 --- /dev/null +++ b/uva_cpp_clean/495/495-20.cpp @@ -0,0 +1,35 @@ +#include + +int fibonacci[5005][2005]; + +int main() +{ + fibonacci[0][0] = 0, fibonacci[1][0] = 1; + for(int i=2 ; i<5005 ; i++) + { + for(int j=0 ; j<2005 ; j++) + { + fibonacci[i][j] += fibonacci[i-1][j]+fibonacci[i-2][j]; + + if( fibonacci[i][j]>9 ) + { + fibonacci[i][j+1] += fibonacci[i][j]/10; + fibonacci[i][j] %= 10; + } + } + } + + int n; + + while( scanf("%d", &n)==1 ) + { + int pos; + for(pos=2005-1 ; pos>0 ; pos--) + if( fibonacci[n][pos]!=0 ) break; + + printf("The Fibonacci number for %d is ", n); + for( ; pos>=0 ; pos--) printf("%d", fibonacci[n][pos]); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/495/495-21.cpp b/uva_cpp_clean/495/495-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..934d727bb84d382f81f2897391f8a764e19628df --- /dev/null +++ b/uva_cpp_clean/495/495-21.cpp @@ -0,0 +1,255 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 495 + Name: Fibonacci Freeze + Problem: https://onlinejudge.org/external/4/495.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + memset(ncells+size, 0, (capacity-size)*CellSize); + + if (cells) delete [] cells; + cells = ncells; + } + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + BigInt(const BigInt& o): cells(0), capacity(0) { + sign = o.sign; + setSize(o.size, 0); + for (int i=0; i= 0) + delete [] cells; + } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { return sign==o.sign ? numLess(o) : sign < o.sign; } + bool operator == (const BigInt &o) const { return sign==o.sign ? numEq(o) : 0; } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + setSize(max(size, o.size)+1); + for (int i=0; i= MAX_C ) { + cells[i] -= MAX_C; + cells[i+1]++; + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + + +int main(){ + BigInt outs[5001]; + outs[0] = BigInt(0); + outs[1] = BigInt(1); + for (int i=2; i<5001; i++) + outs[i] = outs[i-1] + outs[i-2]; + + int n; + while(cin>>n) + cout<<"The Fibonacci number for "< + +#define LIMIT 5001 +#define MAX 1050 + +using namespace std; + +int n, l1, l2, minl, maxl, aux, carry, i, j, k, t; +char fibo[LIMIT][MAX], tmp[MAX], final_tmp[MAX]; + +char* sum(char *s1, char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + minl = l1 > l2 ? l2 : l1; + maxl = l1 > l2 ? l1 : l2; + carry = 0; + for(k = 0, i = l1 - 1,j = l2 - 1; k < minl; k++, i--, j--){ + aux = s1[i] - '0' + s2[j] - '0' + carry; + tmp[k] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l1){ + aux = s1[i] - '0' + carry; + tmp[k] = aux % 10 + '0'; + carry = aux / 10; + k++, i--; + } + if(carry) + tmp[maxl++] = carry + '0', tmp[maxl] = '\0'; + else + tmp[maxl] = '\0'; + i = 0; + while(final_tmp[i++] = tmp[--maxl]) ; + final_tmp[i] = '\0'; + return final_tmp; +} + +void generateFibo(){ + fibo[0][0] = '0', fibo[0][1] = '\0'; + fibo[1][0] = '1', fibo[0][1] = '\0'; + for(t = 2; t <= LIMIT; t++) + strcpy(fibo[t], sum(fibo[t - 1], fibo[t - 2])); +} + +int main(){ + generateFibo(); + while(scanf("%d",&n) == 1) + printf("The Fibonacci number for %d is %s\n", n, fibo[n]); + return(0); +} diff --git a/uva_cpp_clean/496/496-15.cpp b/uva_cpp_clean/496/496-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f046548468021c816d4d0336397763fc0277c00c --- /dev/null +++ b/uva_cpp_clean/496/496-15.cpp @@ -0,0 +1,67 @@ +/* + 集合 + 時間複雜度: O(NlogN) +*/ +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + vector set_a, set_b; + string line; + int temp, pos_a, pos_b, size_a, size_b; + bool is_only_in_a, is_only_in_b, is_a_b_intersect; + + while (getline(cin, line)) { + istringstream input_a(line); + while (input_a >> temp) + set_a.push_back(temp); + getline(cin, line); + istringstream input_b(line); + while (input_b >> temp) + set_b.push_back(temp); + + size_a = set_a.size(); + size_b = set_b.size(); + is_only_in_a = is_only_in_b = is_a_b_intersect = false; + pos_a = pos_b = 0; + + sort(set_a.begin(), set_a.end()); + sort(set_b.begin(), set_b.end()); + set_a.push_back(0x7fffffff); + set_b.push_back(0x7fffffff); + + while (pos_a < size_a || pos_b < size_b) + if (set_a[pos_a] < set_b[pos_b]) { + pos_a++; + is_only_in_a = true; + } else if (set_a[pos_a] > set_b[pos_b]) { + pos_b++; + is_only_in_b = true; + } else { + pos_a++; + pos_b++; + is_a_b_intersect = true; + } + + if (is_a_b_intersect) + if (is_only_in_b) + if (is_only_in_a) + cout << "I'm confused!\n"; + else + cout << "A is a proper subset of B\n"; + else if (is_only_in_a) + cout << "B is a proper subset of A\n"; + else + cout << "A equals B\n"; + else + cout << "A and B are disjoint\n"; + + set_a.clear(); + set_b.clear(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/496/496-21.cpp b/uva_cpp_clean/496/496-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e582b7ea1f312b5fa831a5c9abf70f1db9d1340b --- /dev/null +++ b/uva_cpp_clean/496/496-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 496 + Name: Simply Subsets + Problem: https://onlinejudge.org/external/4/496.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +#define MAXL 10000 + +int cnt; +map ids; +int getId(int x) { + int res = ids[x]; + if (!res) res = ids[x] = ++cnt; + return res; +} + +int main(){ + int x; + char l1[MAXL], l2[MAXL]; + while (cin.getline(l1, MAXL) && cin.getline(l2, MAXL)) { + bitset<128> b1, b2, c; + ids.clear(); + cnt=0; + + for (char *p=strtok(l1," ");p;p=strtok(0," ")) { + sscanf(p, "%d", &x); + b1.set(getId(x)); + } + + for (char *p=strtok(l2," ");p;p=strtok(0," ")) { + sscanf(p, "%d", &x); + b2.set(getId(x)); + } + + if (b1 == b2) + puts("A equals B"); + + else { + c = b1&b2; + if (c == b1) + puts("A is a proper subset of B"); + else if (c == b2) + puts("B is a proper subset of A"); + else if (!c.count()) + puts("A and B are disjoint"); + else + puts("I'm confused!"); + } + } +} diff --git a/uva_cpp_clean/497/497-14.cpp b/uva_cpp_clean/497/497-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..09f66e8f4ba7459db1123e573b845f9cd718104f --- /dev/null +++ b/uva_cpp_clean/497/497-14.cpp @@ -0,0 +1,125 @@ +/*************************************************** + * Problem name : 497 Strategic Defense Initiative - 1.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/497.pdf + * OJ : Uva + * Verdict : AC + * Date : 13.07.2017 + * Problem Type : LIS + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 10000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +/************************************ Code Start Here ******************************************************/ +int L[MAX], I[MAX]; +int LIS_NlogK(int n , vectorar) { + I[0] = -100000000; + for (int i = 1; i <= n; i++) { + I[i] = 100000000; + } + int Maxlen = 0; + for (int i = 0; i < n; i++) { + int low = 0, high = Maxlen, mid; + while (high >= low) { + mid = (low + high) / 2; + if (I[mid] < ar[i]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + I[low] = ar[i]; + L[i] = low; + if (Maxlen < low) { + Maxlen = low; + } + } + return Maxlen; +} +void LIS_Solution(int n, vectorar) { + vectorSol; + int i = 0; + for (int j = 1 ; j < n; j++) { + if (L[j] >= L[i]) { + i = j; + } + } + Sol.push_back(ar[i]); + int top = i; + for (int i = top - 1; i >= 0; i--) { + if ( ar[i] < ar[top] && L[i] == L[top] - 1) { + Sol.push_back(ar[i]); + top = i; + } + } + int sz = Sol.size(); + for (int i = sz - 1 ; i >= 0; i--) { + printf("%d\n", Sol[i]); + } + Sol.clear(); +} +int main () { + int tc; + vectorV; + scanf("%d", &tc); + getchar(); + getchar(); + for (int t = 1; t <= tc; t++) { + string s; + while (getline(cin, s)) { + if (s.size() == 0) break; + int num = 0, sz = s.size(); + for (int i = 0; i < sz; i++) { + num = num * 10 + (s[i] - '0'); + } + V.push_back(num); + } + int n = V.size(); + printf("Max hits: %d\n", LIS_NlogK(n, V)); + LIS_Solution(n, V); + if (t != tc) nl; + V.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/497/497-19.cpp b/uva_cpp_clean/497/497-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a5edc5934d2e6ccdb415dd3242487bfe3b682af --- /dev/null +++ b/uva_cpp_clean/497/497-19.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +using namespace std; + +int main() { + int N; + cin >> N; + string s; + getline(cin, s); + getline(cin, s); + vector v; + while (N--) { + v.clear(); + while (getline(cin, s) && s != "") v.push_back(atoi(s.c_str())); + vector tam(v.size(), 1), prev(v.size(), -1); + int best = 0; + for (int i = 1; i < v.size(); i++) { + for (int j = 0; j < i; j++) { + if (v[j] < v[i] && tam[j] + 1 > tam[i]) + prev[i] = j, tam[i] = tam[j] + 1; + } + if (tam[best] < tam[i]) best = i; + } + printf("Max hits: %d\n", tam[best]); + stack st; + do { + st.push(v[best]); + best = prev[best]; + } while (best != -1); + while (!st.empty()) { + printf("%d\n", st.top()); + st.pop(); + } + if (N) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/497/497-21.cpp b/uva_cpp_clean/497/497-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4f27327f9f2a637be8b54396f6f3fb41ec8a3ea --- /dev/null +++ b/uva_cpp_clean/497/497-21.cpp @@ -0,0 +1,77 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 497 + Name: Strategic Defense Initiative + Problem: https://onlinejudge.org/external/4/497.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +/* Finds longest strictly increasing subsequence. O(n log k) algorithm. */ +vector find_lis(vector &a) +{ + vector b, p(a.size()); + int u, v; + + if (a.size() < 1) return b; + + b.push_back(0); + + for (size_t i = 1; i < a.size(); i++) { + if (a[b.back()] < a[i]) { + p[i] = b.back(); + b.push_back(i); + continue; + } + + for (u = 0, v = b.size()-1; u < v;) { + int c = (u + v) / 2; + if (a[b[c]] < a[i]) u=c+1; else v=c; + } + + if (a[i] < a[b[u]]) { + if (u > 0) p[i] = b[u-1]; + b[u] = i; + } + } + + for (u = b.size(), v = b.back(); u--; v = p[v]) b[u] = v; + return b; +} + + +#include +int main() +{ + int T,n,i,t; + char s[20]; + vector seq,lis; + + scanf("%d\n\n",&T); + while(T--){ + n=0; + seq.clear(); + lis.clear(); + + while(gets(s) && s[0]){ + sscanf(s,"%d",&t); + seq.push_back(t); + n++; + } + lis=find_lis(seq); + printf("Max hits: %d\n",lis.size()); + for(i=0;i + +using namespace std; + +int tc, n, v[10010], lis[10010], path[10010], idxMax, ans; +char s[10]; + +void printPath(int idx){ + if(path[idx] == -1){ + printf("%d\n", v[idx]); + return; + } + printPath(path[idx]); + printf("%d\n", v[idx]); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + n = idxMax = 0, ans = 1; + while(cin.getline(s, 10)){ + if(sscanf(s,"%d", &v[n]) == -1) break; + lis[n] = 1, path[n++] = -1; + } + for(int i = 1; i < n; i++){ + for(int j = 0; j < i; j++) + if(v[i] > v[j] && lis[i] < lis[j] + 1) + lis[i] = lis[j] + 1, path[i] = j; + if(lis[i] > ans) ans = lis[i], idxMax = i; + } + printf("Max hits: %d\n", ans); + printPath(idxMax); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/498/498-19.cpp b/uva_cpp_clean/498/498-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7de27ab17c52be98508271da76596f7ed2c0d910 --- /dev/null +++ b/uva_cpp_clean/498/498-19.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; + +int main() { + string l; + while (getline(cin, l)) { + stringstream ss(l); + long long val; + vector pol; + while (ss >> val) pol.push_back(val); + getline(cin, l); + stringstream ss2(l); + bool first = false; + while (ss2 >> val) { + long long pot = 1, acum = 0; + for (int i = pol.size() - 1; i >= 0; i--) { + acum += pot*pol[i]; + pot *= val; + } + if (first) printf(" "); + else first = true; + printf("%lld", acum); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/498/498-21.cpp b/uva_cpp_clean/498/498-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9c46c9bebf4f7bd41cb7c2b40af44b53e6b20e9 --- /dev/null +++ b/uva_cpp_clean/498/498-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 498 + Name: Polly the Polynomial + Problem: https://onlinejudge.org/external/4/498.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXL 10000 + +int n, c[10000]; +long long f(int x) { + long long r = 0; + for (int i=0; i>c[n]; n++); + + bool fr=1; + while (sin2>>x) { + if (fr) fr=0; + else putchar(' '); + cout << f(x); + } + putchar('\n'); + } +} diff --git a/uva_cpp_clean/498/498-9.cpp b/uva_cpp_clean/498/498-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2074ae358e6c7742b845313f38de7a043fa21526 --- /dev/null +++ b/uva_cpp_clean/498/498-9.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +const int LEN = 200, SIZE = 100; + +int n, m, c[SIZE], x[SIZE]; +char line[LEN], *p; + +int Horner(int X){ + int fX = c[0]; + for(int i = 1; i < n; i++) + fX = fX * X + c[i]; + return fX; +} + +int main(){ + while(cin.getline(line, LEN)){ + p = strtok(line, " "); + n = 0; + while(p) + c[n++] = atoi(p), p = strtok(NULL, " "); + cin.getline(line, LEN); + p = strtok(line, " "); + m = 0; + while(p) + x[m++] = atoi(p), p = strtok(NULL, " "); + for(int it = 0; it < m; it++) + printf( it != m - 1 ? "%d " : "%d\n", Horner(x[it])); + } + return(0); +} diff --git a/uva_cpp_clean/499/499-14.cpp b/uva_cpp_clean/499/499-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..977b03c9aaa69be9b72b166dc1cbb22636db5cb6 --- /dev/null +++ b/uva_cpp_clean/499/499-14.cpp @@ -0,0 +1,83 @@ +/*************************************************** + * Problem name : 499 - What's The Frequency, Kenneth?.cpp + * Problem Link : https://uva.onlinejudge.org/external/4/499.pdf + * OJ : Uva + * Verdict : Ac + * Date : 2017-08-04 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 300 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +mapmp, chk; +//~ int mp[MAX], chk[MAX]; +int main () { + string str; + while (getline(cin, str)) { + int sz = str.size(); + for (int i = 0; i < sz; i++) { + if (isalpha(str[i])) { + mp[str[i]]++; + } + } + int mx = 0; + map :: iterator it; + for ( it = mp.begin(); it != mp.end(); it++) { + mx = max(mx, it->second); + } + for (it = mp.begin(); it != mp.end(); it++) { + if (mx == it->second && chk[it->first] == 0) { + cout << it->first; + chk[it->first] = 1; + } + } + printf(" %d\n", mx); + mp.clear(), chk.clear(); + } + return 0; +} + diff --git a/uva_cpp_clean/499/499-15.cpp b/uva_cpp_clean/499/499-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c16c1c215712d1af3779d2739c10ea5cba587514 --- /dev/null +++ b/uva_cpp_clean/499/499-15.cpp @@ -0,0 +1,35 @@ +/* + 計數器 + 時間複雜度: O(N) +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + char alphabet; + map counter; // 查找和插入都是 O(log(size)),但這裡 size <= 52 是常數,所以是 O(1) + int max_occurrence; + + while (cin.get(alphabet) && alphabet != EOF) { + max_occurrence = 0; + counter[alphabet] = 1; + + while (cin.get(alphabet) && alphabet != '\n') + if (isalpha(alphabet)) + counter[alphabet]++; + + for (const auto& i : counter) + max_occurrence = max(max_occurrence, i.second); + + for (const auto& i : counter) + if (max_occurrence == i.second) + cout << i.first; + + cout << " " << max_occurrence << "\n"; + counter.clear(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/499/499-19.cpp b/uva_cpp_clean/499/499-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03edc89e76b8ec3cd94807277c408b9b6cd9b935 --- /dev/null +++ b/uva_cpp_clean/499/499-19.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +using namespace std; + +int main() { + string line; + int countU[30], countL[30]; + while (getline(cin, line)) { + int max = 0; + for (int i = 0; i < 30 ; i++) countL[i] = countU[i] = 0; + for (int i = 0; i < line.length(); i++) { + if(isupper(line[i])) if(++countU[line[i]-'A'] > max) max = countU[line[i]-'A']; + if(islower(line[i])) if(++countL[line[i]-'a'] > max) max = countL[line[i]-'a']; + } + for (int i = 0; i < 30; i++) if(countU[i] == max) printf("%c", i + 'A'); + for (int i = 0; i < 30; i++) if(countL[i] == max) printf("%c", i + 'a'); + printf(" %d\n", max); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/499/499-20.cpp b/uva_cpp_clean/499/499-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae1cb427fe46eb7a4bce1453f0034f81786b5fd5 --- /dev/null +++ b/uva_cpp_clean/499/499-20.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +using namespace std; + +int letters[55]; + +int main() +{ + char line[1005]; + + while( gets(line) ) + { + for(int i=0 ; i<55 ; i++) letters[i] = 0; + + for(int i=0 ; i='A' && line[i]<='Z' ) letters[line[i]-'A']++; + if( line[i]>='a' && line[i]<='z' ) letters[line[i]-'a'+26]++; + } + + int max_frequency = -1; + + for(int i=0 ; i<55 ; i++) max_frequency = max(max_frequency, letters[i]); + + for(int i=0 ; i<52 ; i++) + { + if( letters[i]==max_frequency ) + { + char temp; + if( i<26 ) + { + temp = 'A'+i; + cout << temp; + } + else + { + temp = 'a'+i-26; + cout << temp; + } + } + } + cout << " " << max_frequency << endl; + } + return 0; +} diff --git a/uva_cpp_clean/499/499-5.cpp b/uva_cpp_clean/499/499-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2b0f9e47f921ea08131e8852e5069dbc66fe82f7 --- /dev/null +++ b/uva_cpp_clean/499/499-5.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + char s[100000]; + while(gets(s)) + { + mapmp1; + mapmp2; + priority_queuepq; + for(int i=0;i::iterator it=mp1.begin(); it!=mp1.end(); ++it) + { + if(pq.top()== it->second) + cout<<""<first; + } + cout<<" "< +#include + +using namespace std; + +int main() { + int n_case, n, m, i, index_now, elements; + + cin >> n_case; + while (n_case--) { + priority_queue, greater> min_heap; + priority_queue, less> max_heap; + queue add, get; + + index_now = elements = 0; + cin >> m >> n; + + while (m-- && cin >> i) + add.push(i); + + while (n-- && cin >> i) + get.push(i); + + while (!add.empty()) { + elements++; + + if (!min_heap.empty() && add.front() > max_heap.top()) + min_heap.push(add.front()); // 維持 min_heap 中最小元素 > max_heap 中最大元素: max->[-4, -1000], min->[1, 2, 3, 8], i=2 + else + max_heap.push(add.front()); + add.pop(); + + while (!get.empty() && elements == get.front()) { + while (max_heap.size() != index_now) { + if (max_heap.size() < index_now) { // min_heap 太多了 + max_heap.push(min_heap.top()); + min_heap.pop(); + } else if (max_heap.size() > index_now) { // min_heap 不夠 + min_heap.push(max_heap.top()); + max_heap.pop(); + } + } + + cout << min_heap.top() << "\n"; + index_now++; + get.pop(); + } + } + if (n_case) + cout << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/501/501-21.cpp b/uva_cpp_clean/501/501-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37792354fd2494eba3dacd7a1864221911be15d4 --- /dev/null +++ b/uva_cpp_clean/501/501-21.cpp @@ -0,0 +1,49 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 501 + Name: Black Box + Problem: https://onlinejudge.org/external/5/501.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main() { + int T, m, n, u, X[40000]; + + scanf("%d", &T); + while (T--) { + scanf("%d%d", &m, &n); + for (int i=0; i ss; + ss.insert(X[0]); + auto it = ss.begin(); + int j = 1; + + for (int i=0; i + +#define SIZE 30010 + +using namespace std; + +int tc, ct, t, m, n, a[SIZE], u; +multiset ::iterator it; + +int main(){ + scanf("%d",&tc); + while(tc--){ + multiset v; + scanf("%d %d", &m, &n); + for(int i = 0; i < m; i++) scanf("%d", &a[i]); + ct = t = 0; + while(n--){ + scanf("%d",&u); + if(t==0){ + while(ct != u){ + v.insert(a[ct]); + ct++; + } + it = v.begin(); + } + else{ + while(ct != u){ + if(a[ct] < *it) v.insert(a[ct]), --it; + else v.insert(a[ct]); + ct++; + } + } + if(t) ++it; + printf("%d\n", *it); + t++; + } + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/506/506-17.cpp b/uva_cpp_clean/506/506-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..064b0618fcf75ca0468229cd6cc68a85bd0f7874 --- /dev/null +++ b/uva_cpp_clean/506/506-17.cpp @@ -0,0 +1,91 @@ +/** + * AOAPC II Example 6-21 System Dependencies + * ICPC WF 1997 + */ +#include +using namespace std; +struct Info +{ + string name; + vector depOns, usedIns; + int status = 0; + Info(string &name) : name(name) { } +}; +map name2id; +vector comps; +list installed; +int id(string &item) +{ + if (name2id.count(item)) + return name2id[item]; + comps.push_back(item); + return name2id[item] = comps.size() - 1; +} +void install(int id, bool exp) +{ + for (int dep : comps[id].depOns) + if (!comps[dep].status) + install(dep, false); + cout << " Installing " << comps[id].name << "\n"; + installed.push_back(id); + comps[id].status = exp ? 1 : 2; +} +void uninstall(int id) +{ + cout << " Removing " << comps[id].name << "\n"; + installed.remove(id); + comps[id].status = 0; + for (int dep : comps[id].depOns) + if (comps[dep].status == 2 && none_of(comps[dep].usedIns.begin(), comps[dep].usedIns.end(), [](int id){ return comps[id].status; })) + uninstall(dep); +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + string line; + while (getline(cin, line)) + { + name2id.clear(); + comps.clear(); + installed.clear(); + while (line != "END") + { + cout << line << "\n"; + if (line[0] == 'L') + for (int i : installed) + cout << " " << comps[i].name << "\n"; + else + { + stringstream ss(line); + string cmd, item; + ss >> cmd >> item; + int item_id = id(item); + if (cmd[0] == 'D') + { + string cur; + while (ss >> cur) + { + int cur_id = id(cur); + comps[item_id].depOns.push_back(cur_id); + comps[cur_id].usedIns.push_back(item_id); + } + } + else if (cmd[0] == 'I') + if (comps[item_id].status) + cout << " " << item << " is already installed.\n"; + else + install(item_id, true); + else + if (!comps[item_id].status) + cout << " " << item << " is not installed.\n"; + else if (any_of(comps[item_id].usedIns.begin(), comps[item_id].usedIns.end(), [](int id){ return comps[id].status; })) + cout << " " << item << " is still needed.\n"; + else + uninstall(item_id); + } + getline(cin, line); + } + cout << "END\n"; + } +} diff --git a/uva_cpp_clean/507/507-10.cpp b/uva_cpp_clean/507/507-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04d946dbc21e0c61e4be606cb4c760ecc9d4a2cf --- /dev/null +++ b/uva_cpp_clean/507/507-10.cpp @@ -0,0 +1,60 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define pi 3.14159265 +using namespace std; +/* +by:Ahmed Sakr (sakr_) with AZA ; +#ifndef ONLINE_JUDGE + freopen("c.in", "r", stdin); +#endif + __builtin_popcountll(); +*/ +bool isPrime(int x) { + if (x <= 1) return false; + if (x <= 3) return true; + if (x % 2 == 0 || x % 3 == 0) return false; + for (int i = 5; i * i <= x; i = i + 6) + if (x % i == 0 || x % (i + 2) == 0) return false; + return true; +} + +int main() { + Sakr_ + + int b,a,n,sum,msum,begin,end,l,x,i; + int c=1; + scanf("%d", &b); + + while (b--) { + scanf("%d", &a); + + begin=end = 0; + sum=msum=0; + x=1; + l=0; + for(i=1;il)) { + msum = sum; + begin = x; + end = i; + l = i-x; + } + + } + + if(msum) { + printf("The nicest part of route %d is between stops %d and %d\n", c, begin, end+1); + } else { + printf("Route %d has no nice parts\n", c); + } + c++; + + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/507/507-19.cpp b/uva_cpp_clean/507/507-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..710663bf574cd0151967532637f5873057f23213 --- /dev/null +++ b/uva_cpp_clean/507/507-19.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main() { + int b; + cin >> b; + for (int c = 1; c <= b; c++) { + int s, ini, count, bini, bend, bcount; + count = bini = bend = bcount = 0; + ini = 1; + cin >> s; + for (int i = 1; i < s; i++) { + int val; + cin >> val; + if (val > count + val) ini = i, count = val; + else count += val; + if (count > bcount || (count == bcount && i+1-ini > bend - bini)) bcount = count, bini = ini, bend = i + 1; + } + if (bini != 0) printf("The nicest part of route %d is between stops %d and %d\n", c, bini, bend); + else printf("Route %d has no nice parts\n", c); + } + return 0; +} diff --git a/uva_cpp_clean/507/507-20.cpp b/uva_cpp_clean/507/507-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee8cb4650cff4043c90ac65131a644a3de7429f9 --- /dev/null +++ b/uva_cpp_clean/507/507-20.cpp @@ -0,0 +1,38 @@ +#include + +int n[20005]; + +int main() +{ + int tc, nCase = 0; + + scanf("%d", &tc); + while( tc-- ) + { + int s; + scanf("%d", &s); + + for(int i=0 ; iMAX) || (sum==MAX && (i-a)>(b-c)) ) + { + MAX = sum; + b = i+1; + c = a; + } + else if( sum<0 ) + { + a = i+1; + sum = 0; + } + } + + if( MAX>0 ) printf("The nicest part of route %d is between stops %d and %d\n", ++nCase, c + 1, b + 1); + else printf("Route %d has no nice parts\n", ++nCase); + } + return 0; +} diff --git a/uva_cpp_clean/507/507-21.cpp b/uva_cpp_clean/507/507-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b15246cf5c670589e64ba988698a942b6cb11d43 --- /dev/null +++ b/uva_cpp_clean/507/507-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 507 + Name: Jill Rides Again + Problem: https://onlinejudge.org/external/5/507.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int T; + cin>>T; + for (int cse=1; cse<=T; cse++) { + int n, bi=1, bj=0, bs=0, + ci=1, cj=0, cs=0; + + cin>>n; + for (int i=1; i>x; + + cs+=x; cj=i+1; + if (cs<0) { + ci=i+1; + cs=0; + } + + if (cs>bs || (cs==bs && cj-ci>bj-bi)) { + bi=ci; + bj=cj; + bs=cs; + } + } + + if (bs<=0) printf("Route %d has no nice parts\n", cse); + else printf("The nicest part of route %d is between stops %d and %d\n", cse, bi, bj); + } +} diff --git a/uva_cpp_clean/507/507-5.cpp b/uva_cpp_clean/507/507-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e22d42bdc7dbf2c3720001f2e27caa604a29ab16 --- /dev/null +++ b/uva_cpp_clean/507/507-5.cpp @@ -0,0 +1,186 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< pii; +typedef pair piii; +typedef pair pll; +typedef pair psi; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforba(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (20010) +#define mod 1000000007 +////=====================================//// +int main() +{ + int t,no=0; + si(t); + while(t--) + { + int n,a[mx]; + si(n); + n--; + for0(i,n) + { + si(a[i]); + } + int sum=0,point=0,st=0,en=0,out=0; + for0(i,n) + { + sum += a[i]; + if(sum<0) + { + sum=0; + point=i+1; + } + else if(out < sum || (sum == out && point== st)) + { + out = sum; + st = point; + en = i; + } + + } + if(out<=0) + printf("Route %d has no nice parts\n",++no); + else + printf("The nicest part of route %d is between stops %d and %d\n",++no,st+1,en+2); + } + return 0; +} + diff --git a/uva_cpp_clean/507/507-9.cpp b/uva_cpp_clean/507/507-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9933fc2fba76a766017b1ee1917f7001a488488 --- /dev/null +++ b/uva_cpp_clean/507/507-9.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int tc, s, v, ans, pre, sum; +pair r; + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + ans = sum = 0; + pre = 1; + scanf("%d", &s); + for(int i = 1; i < s; i++){ + scanf("%d", &v); + sum += v; + if(ans < sum) ans = sum, r = make_pair(pre, i); + if(ans == sum && i - pre > r.second - r.first) r = make_pair(pre, i); + if(sum < 0) sum = 0, pre = i + 1; + } + if(ans == 0) printf("Route %d has no nice parts\n", t); + else printf("The nicest part of route %d is between stops %d and %d\n", t, r.first, r.second + 1); + } + return(0); +} diff --git a/uva_cpp_clean/512/512-17.cpp b/uva_cpp_clean/512/512-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11a59c4637c66c381be588433c25e44d5b756d73 --- /dev/null +++ b/uva_cpp_clean/512/512-17.cpp @@ -0,0 +1,79 @@ +/** + * AOAPC II Example 4-5 Spreadsheet tracking + */ +#include +using namespace std; +struct Cmd { + string op; + vector pos; + + void read() { + cin >> op; + int cur; + if (op[0] == 'E') + for (int i = 0; i < 4; ++i) { + cin >> cur; + pos.push_back(cur); + } + else { + int cnt; + cin >> cnt; + while (cnt--) { + cin >> cur; + pos.push_back(cur); + } + } + } +}; +bool simulate(vector &cmds, int &r, int &c) { + for (Cmd &cmd : cmds) { + if (cmd.op[0] == 'E') { + if (r == cmd.pos[0] && c == cmd.pos[1]) { + r = cmd.pos[2]; c = cmd.pos[3]; + } + else if (r == cmd.pos[2] && c == cmd.pos[3]) { + r = cmd.pos[0]; c = cmd.pos[1]; + } + } + else if (cmd.op[0] == 'D') { + int &oi = (cmd.op[1] == 'R' ? r : c), fi = oi; + for (int po : cmd.pos) + if (po == oi) + return false; + else if (po < oi) + --fi; + oi = fi; + } + else { + int &oi = (cmd.op[1] == 'R' ? r : c), fi = oi; + for (int po : cmd.pos) + if (po <= oi) + ++fi; + oi = fi; + } + } + return true; +} +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int r, c, n, ds = 0; + while ((cin >> r >> c >> n) && r) { + vector cmds(n); + for (Cmd &c : cmds) + c.read(); + if (ds) + cout << '\n'; + cout << "Spreadsheet #" << ++ds << '\n'; + int q, r, c; + cin >> q; + while (q--) { + cin >> r >> c; + cout << "Cell data in (" << r << "," << c << ") "; + if (simulate(cmds, r, c)) + cout << "moved to (" << r << "," << c << ")\n"; + else + cout << "GONE\n"; + } + } +} diff --git a/uva_cpp_clean/514/514-11.cpp b/uva_cpp_clean/514/514-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbcb54fd561f32f76f310ae7214e8724df574a33 --- /dev/null +++ b/uva_cpp_clean/514/514-11.cpp @@ -0,0 +1,81 @@ +#include +/* +Problem: 514 - Rails +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=455 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int N = 0, M = 0; +int marshal_order[1002]; + +int main () { + ios_base::sync_with_stdio(0); + cin.tie(0); + + while ( cin >> N && N != 0 ){ + while (cin >> M && M != 0){ + + stack< int> station; + int next_coach = 1, idx = 0; + + station.push(-1); + + for (int i = 1; i < N; i++){ + marshal_order[idx] = M; + idx++; + cin >> M; + } + marshal_order[idx] = M; + + idx = 0; + while (next_coach < N+1){ + station.push(next_coach); + + while (station.top() == marshal_order[idx]){ + idx++; + station.pop(); + } + next_coach++; + } + + cout << ((station.size() == 1)? "Yes" : "No") << '\n'; + } + + cout << '\n'; + } + + return 0; +} +/* +input:- +----------- +5 +1 2 3 4 5 +5 4 1 2 3 +0 +6 +6 5 4 3 2 1 +0 + +0 + +marshal_order:- +----------- +Yes +No + +Yes + +Resources:- +------------- +stack: http://www.cplusplus.com/reference/stack/stack/ +https://www.geeksforgeeks.org/stack-data-structure-introduction-program/ +https://www.tutorialspoint.com/cpp_standard_library/stack.htm +*/ diff --git a/uva_cpp_clean/514/514-14.cpp b/uva_cpp_clean/514/514-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ef84275e857e39f58069bd800e58f1e2b0325a0 --- /dev/null +++ b/uva_cpp_clean/514/514-14.cpp @@ -0,0 +1,95 @@ +/*************************************************** + * Problem Name : 514 - Rails.cpp + * Problem Link : https://uva.onlinejudge.org/external/5/514.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-07-17 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + int n; + + while (cin >> n) { + if (n == 0) break; + + int ar[n + 3]; + + while (cin >> ar[0]) { + if (ar[0] == 0) { + nl; break; + } + + for (int i = 1; i < n; i++) { + cin >> ar[i]; + } + + stackst; + + for (int i = 1, j = 0; i <= n; i++) { + //~ debug; + st.push (i); + + while (!st.empty() && st.top() == ar[j]) { + st.pop(); + j++; + } + } + + if (st.empty() ) { + cout << "Yes\n"; + + } else cout << "No\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/514/514-15.cpp b/uva_cpp_clean/514/514-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b4b335ad3fe3917bee4e60510e624ebd11839b5 --- /dev/null +++ b/uva_cpp_clean/514/514-15.cpp @@ -0,0 +1,38 @@ +/* + 模擬、堆疊 + 時間複雜度: O(N) +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, n; + while (cin >> n && n) { + while (cin >> i && i) { + vector station, order(n, 0); + int car_now = 0, k = n; + + order[--k] = i; + for (i = k - 1; i > -1; i--) + cin >> order[i]; + + while (!order.empty()) + if (!station.empty() && order.back() == station.back()) { + station.pop_back(); + order.pop_back(); + } else if (car_now < n) + station.push_back(++car_now); + else + break; + + cout << (station.empty() ? "Yes\n" : "No\n"); + } + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/514/514-17.cpp b/uva_cpp_clean/514/514-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e3be2b96a89be69e7c5618c893c8f189509ca24 --- /dev/null +++ b/uva_cpp_clean/514/514-17.cpp @@ -0,0 +1,44 @@ +/** + * AOAPC II Example 5-2 Rails + * ICPC CERC 1997 + */ +#include +using namespace std; +int main() +{ + int n; + while ((cin >> n) && n) + { + int cur; + while ((cin >> cur) && cur) + { + vector tar(n); + tar[0] = cur; + for (int i = 1; i < n; ++i) + cin >> tar[i]; + stack s; + int a = 1, b = 0; + bool ok = true; + while (b < n) + if (a == tar[b]) + { + ++a; + ++b; + } + else if (!s.empty() && s.top() == tar[b]) + { + s.pop(); + ++b; + } + else if (a <= n) + s.push(a++); + else + { + ok = false; + break; + } + cout << (ok ? "Yes" : "No") << "\n"; + } + cout << "\n"; + } +} diff --git a/uva_cpp_clean/514/514-19.cpp b/uva_cpp_clean/514/514-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff5f866a9d77f7e7fc1dc9d51ff7577fddf727b7 --- /dev/null +++ b/uva_cpp_clean/514/514-19.cpp @@ -0,0 +1,36 @@ +#include +#include +using namespace std; + +int main() { + stack s; + int n; + while (scanf("%d", &n) && n != 0) { + int m, cont = 0, pos; + bool ok; + while (scanf("%d", &m) && m != 0) { + if (cont == 0) { + ok = true; + pos = 1; + while (!s.empty()) s.pop(); + } + if (ok){ + if (s.empty()) s.push(pos), pos++; + while (s.top() != m && pos <= n) { + s.push(pos); + pos++; + } + if (s.top() == m) s.pop(); + else ok = false; + } + cont++; + if (cont == n) { + if (ok) printf("Yes\n"); + else printf("No\n"); + cont = 0; + } + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/514/514-20.cpp b/uva_cpp_clean/514/514-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..627d55400036028296cccd93121f5a80c0222241 --- /dev/null +++ b/uva_cpp_clean/514/514-20.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +const int MAXN = 1000; +int coach[MAXN+5]; +stack< int > s; + +int main() { + #ifndef ONLINE_JUDGE + freopen("in.txt", "r", stdin); + freopen("out.txt", "w", stdout); + #endif // ONLINE_JUDGE + + int N; + while(scanf("%d", &N) == 1 && N) { + while(scanf("%d", &coach[0]) == 1 && coach[0]) { + for(int i = 1; i < N; ++i) scanf("%d", &coach[i]); + + int coach_no = 1, idx = 0; + while(coach_no <= N) { + s.push(coach_no); + while(!s.empty() && (s.top() == coach[idx]) && (idx < N)) { + s.pop(); + ++idx; + } + ++coach_no; + } + + if(s.empty()) puts("Yes"); + else puts("No"); + + memset(coach, 0, sizeof(coach)); + while(!s.empty()) s.pop(); + } + puts(""); + } + return 0; +} diff --git a/uva_cpp_clean/514/514-21.cpp b/uva_cpp_clean/514/514-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9aaeeae14db6d276bad8ef275fb5bf54e6b8eed --- /dev/null +++ b/uva_cpp_clean/514/514-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 514 + Name: Rails + Problem: https://onlinejudge.org/external/5/514.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if(p == end) { + if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF; + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return r; +} + +// -------------------------------------------------------------------------- + +class FastStack { + int data[1024], sz; + public: + FastStack():sz(0){} + void clear() { sz=0; } + bool empty() { return !sz; } + void push(int x) { data[sz++] = x; } + int pop() { return data[--sz]; } + int top() { return data[sz-1]; } + int size() { return sz; } +}; + + + +int blocks[1001]; +FastStack src, station; +bool test(int n) { + src.clear(); + station.clear(); + for (int i=n; i>0; i--) + src.push(i); + + for (int i=0; i +#include + +#define repl(i,b) for ( int l = i; i < b; l++ ) + +using std :: stack; + +stack s; +int train[1000]; + +int main () +{ + int n, k; + bool possible; + + while ( scanf("%d", &n) && n ) + { + while ( scanf ("%d", &train[0] ) && train[0] ) + { + k = 1; + possible = true; + + for ( int l = 1; l < n; l++ ) + scanf("%d",&train[l]); + + while ( !s.empty() ) + s.pop(); + + for ( int l = 0; l < n && possible; l++ ) + { + while ( train[l] >= k ) + s.push(k++); + + if ( s.top() == train[l] ) + s.pop(); + else + possible = false; + } + + if ( possible ) + puts("Yes"); + else + puts("No"); + } + puts(""); + } +} diff --git a/uva_cpp_clean/514/514-5.cpp b/uva_cpp_clean/514/514-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bb4afc0afca5041473a0c0e4466b1e8f8d00a421 --- /dev/null +++ b/uva_cpp_clean/514/514-5.cpp @@ -0,0 +1,123 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define st(s) s.size(); +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +using namespace std; +int main() +{ + input; + output; + + int t,s,b[100000]; + while(sc("%d",&t)==1) + { + if(t==0) + break; + vectorv; + int f=0; + while(1) + { + for(int i=0;ist; + int c=0; + s=v.size(); + for(int i=0;it) + break; + } + v.clear(); + if(!st.empty()){ + pf("No\n"); + } + else + pf("Yes\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/514/main.cpp b/uva_cpp_clean/514/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef91a1f2481bdd838c4dc63beb0a89d781bfc4c1 --- /dev/null +++ b/uva_cpp_clean/514/main.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + +using namespace std; + +bool c_tiene(queue cola, int b){ + bool res = false; + int l = cola.size(); + for(int i = 0; i < l; i++){ + int a = cola.front(); + cola.pop(); + cola.push(a); + if(a == b) res = true; + } + return res; +} +bool algoritmo(queue cola, queueorden, stackpila, int aux){ + int vagon; + bool puede = true; + bool solved = false; + while(!solved){ + if(orden.empty()) solved = true; + else{ + vagon = orden.front(); + orden.pop(); + if(c_tiene(cola, vagon)){ + while(aux != vagon){ + aux = cola.front(); + cola.pop(); + pila.push(aux); + } + pila.pop(); + }else{ + if(pila.top() == vagon) pila.pop(); + else{ + puede = false; + solved = true; + } + } + } + } + return puede; +} + +int main(){ + + int m, n; + queue orden; + queue cola; + stack pila; + bool flag = true; + int vagon; + while(true){ + if(flag){ + cin >> m; + if(m == 0) break; + } + n = m; + int aux = 0; + flag = false; + while(!cola.empty()) cola.pop(); + while(!orden.empty()) orden.pop(); + while(!pila.empty()) pila.pop(); + for(int i = 1; i <= n; i++){ + cola.push(i); + cin >> vagon; + if(vagon == 0){ + flag = true; + cout<< endl; + break; + } + orden.push(vagon); + } + if(!flag){ + bool puede = algoritmo(cola, orden, pila, aux); + if(puede) cout<< "Yes" << endl; + else cout<< "No" << endl; + } + } +return 0; +} diff --git a/uva_cpp_clean/516/516-13.cpp b/uva_cpp_clean/516/516-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3d07e967c90bac5947d4a6b8411c0a25aeb5a2c --- /dev/null +++ b/uva_cpp_clean/516/516-13.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 10000001 + +typedef long long ll; +typedef vector vll; +typedef vector vb; +typedef pair pll; +typedef vector< pll > vpll; + +ll x, p, e; +vll primes; +vpll factors; +vb isPrime(MAX, true); + +void findPrime() +{ + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += i) + { + isPrime[j] = false; + } + + primes.push_back(i); + } + } +} + +void findFactors(ll n) +{ + ll primes_index = 0, pf = primes[primes_index]; + + while (pf * pf <= n) + { + ll count = 0; + + if (n % pf == 0) + { + while (n % pf == 0) + { + n /= pf; + + count++; + } + + factors.push_back(make_pair(pf, count)); + } + + pf = primes[++primes_index]; + } + + if (n != 1) + { + factors.push_back(make_pair(n, 1)); + } + + reverse(factors.begin(), factors.end()); +} + +int main() +{ + vpll a; + + findPrime(); + + while (true) + { + string s; + stringstream is; + + getline(cin, s); + + is << s; + + x = 1; + a.clear(); + factors.clear(); + + while (is >> p && p != 0) + { + is >> e; + a.push_back(make_pair(p, e)); + } + + if (p == 0) + { + break; + } + + else + { + for (int i = 0; i < a.size(); ++i) + { + for (int j = 0; j < a[i].second; ++j) + { + x *= a[i].first; + } + } + + x -= 1; + + findFactors(x); + + for (int k = 0; k < factors.size(); ++k) + { + if (k != 0) + cout << " "; + + cout << factors[k].first << " " << factors[k].second; + } + + cout << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/516/516-15.cpp b/uva_cpp_clean/516/516-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c77fb2cb603685f75e911d5194d7f2e18c4627f2 --- /dev/null +++ b/uva_cpp_clean/516/516-15.cpp @@ -0,0 +1,84 @@ +/* + 質因數分解 + 時間複雜度: O(NlogN) +*/ +#include +#include +#include +#include +#include + +using namespace std; + +int power(int& base, int expo) { + if (expo == 0) + return 1; + if (expo == 1) + return base; + int ans = power(base, expo / 2); + return expo & 1 ? ans * ans * base : ans * ans; +} + +vector generate_prime(int N) { + int i, j; + vector prime_table; + vector is_prime(N, true); + for (i = 2; i < N; i++) { + if (is_prime[i]) + prime_table.push_back(i); + for (j = 0; i * prime_table[j] < N; j++) { + is_prime[i * prime_table[j]] = false; + if (i % prime_table[j] == 0) + break; + } + } + return prime_table; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int sum, base, expo, cnt; + string x; + stack> ans; + vector primes = generate_prime(182); + + while (getline(cin, x)) { + if (x.length() == 1) + break; + + istringstream input(x); + sum = 1; + + while (input >> base >> expo) + sum *= power(base, expo); + + sum--; + + for (const int& prime : primes) { + cnt = 0; + while (sum % prime == 0) { + sum /= prime; + cnt++; + } + if (cnt) + ans.push(make_pair(prime, cnt)); + } + + if (sum != 1) + ans.push(make_pair(sum, 1)); + + cout << ans.top().first << " " << ans.top().second; + ans.pop(); + + while (!ans.empty()) { + const pair k = ans.top(); + cout << " " << k.first << " " << k.second; + ans.pop(); + } + + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/516/516-19.cpp b/uva_cpp_clean/516/516-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fd3d232f983262ac2404e747326e753ee2671e1 --- /dev/null +++ b/uva_cpp_clean/516/516-19.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<100000001> bs; +vi primes; + +inline void sieve(ll upperbound = 200) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} + +inline void primeFactors(ll n, vii & factors) { + int c; + ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } + if (n!=1) factors.push_back(ii(n,1)); +} + +int main() { + sieve(); + string s; + while (getline(cin, s) && s != "0") { + stringstream ss(s); + int p, e, n = 1; + while (ss >> p >> e) { + int pot = 1; + for (int i = 0; i < e; i++) pot *= p; + n *= pot; + } + n--; + vii factors; + primeFactors(n, factors); + for (int i = factors.size() - 1; i >= 0; i--) { + if (i != factors.size() - 1) printf(" "); + printf("%d %d", factors[i].first, factors[i].second); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/516/516-20.cpp b/uva_cpp_clean/516/516-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f49389d01da292cd85b77f6261c0a4d53d7d35a2 --- /dev/null +++ b/uva_cpp_clean/516/516-20.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define N 32768 +#define Check(N, pos) ((bool) ((N)&(1<<(pos)))) +#define Set(N, pos) ((N)=((N)|(1<<(pos)))) + +unsigned status[(N>>5)+2], factor[N], power[N]; +vector < unsigned > primes; + +void bitSieve() +{ + unsigned sqrtN = unsigned (sqrt(N)); + for(unsigned i=3 ; i<=sqrtN ; i+=2) + if( !Check(status[i>>5], i&31) ) + for(unsigned j=i*i ; j<=N ; j+=(i<<1)) Set(status[j>>5], j&31); + + primes.push_back(2); + for(unsigned i=3 ; i<=N ; i+=2) + if( !Check(status[i>>5], i&31) ) primes.push_back(i); +} + +int main() +{ + bitSieve(); + + string input; + while( getline(cin, input) && input!="0" ) + { + stringstream converter(input); + + unsigned base, exponent, x = 1; + while( converter >> base >> exponent ) + for(unsigned i=1 ; i<=exponent ; i++) x *= base; + x--; + + for(unsigned i=0 ; i0 ; i--) + printf("%u %d ", factor[i], power[i]); + printf("%u %d\n", factor[0], power[0]); + } + return 0; +} diff --git a/uva_cpp_clean/516/516-21.cpp b/uva_cpp_clean/516/516-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28fdeecd76a16e12bff5cff736c71f4cb2e82662 --- /dev/null +++ b/uva_cpp_clean/516/516-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 516 + Name: Prime Land + Problem: https://onlinejudge.org/external/5/516.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string line; + while (getline(cin, line) && line!="0") { + stringstream sin(line); + int x = 1, p, e; + while (sin >> p >> e) + x *= pow(p, e); + + --x; + vector vec; + for (int i=2, sq=sqrt(x); i<=sq; ++i) + if (x % i == 0) { + int e = 0; + while (x % i == 0) { + x /= i; + ++e; + } + + vec.push_back(e); + vec.push_back(i); + sq = sqrt(x); + } + if (x > 1) { + vec.push_back(1); + vec.push_back(x); + } + + cout << vec[vec.size()-1]; + for (int i=vec.size()-2; i>=0; --i) + cout << ' ' << vec[i]; + cout << endl; + } +} diff --git a/uva_cpp_clean/516/516-9.cpp b/uva_cpp_clean/516/516-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..977816b6d221cca26375aec0d928a3c651c40818 --- /dev/null +++ b/uva_cpp_clean/516/516-9.cpp @@ -0,0 +1,51 @@ +#include + +#define SIZE 110 +#define LIM 110 +#define LIM_SIEVE 1010 + +using namespace std; + +char line[LIM], *p; +int v[SIZE], ct, num; +bool is_prime[LIM_SIEVE]; +vectorprime; +vector< pair >Factors; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < LIM_SIEVE; it1++) + if( is_prime[it1] ){ + for(long long it2 = it1 * it1; it2 < LIM_SIEVE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +void primeFactors(int N){ + Factors.clear(); + for(int it = 0; it < prime.size() and prime[it] * prime[it] <= N and N != 1; it++){ + int e = 0; + while( N % prime[it] == 0 ) N /= prime[it], e++; + if(e) Factors.push_back( make_pair(prime[it], e) ); + } + if(N != 1) Factors.push_back( make_pair(N, 1) ); +} + +int main(){ + sieve(); + while(cin.getline(line, LIM), line[0] != '0'){ + p = strtok(line, " "); + ct = 0; + while(p) v[ct++] = atoi(p), p = strtok(NULL, " "); + num = 1; + for(int it = 1; it < ct; it += 2) + for(int e = 0; e < v[it]; e++) + num *= v[it - 1]; + primeFactors(num - 1); + for(int it = Factors.size() - 1; it >= 0; it--) + printf(it == 0 ? "%d %d\n" : "%d %d ", Factors[it].first, Factors[it].second); + } + return(0); +} diff --git a/uva_cpp_clean/519/519-21.cpp b/uva_cpp_clean/519/519-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..56d20497648d5c215de12b97e6fe4af79f7895a7 --- /dev/null +++ b/uva_cpp_clean/519/519-21.cpp @@ -0,0 +1,88 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 519 + Name: Puzzle (II) + Problem: https://onlinejudge.org/external/5/519.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define TOP 0 +#define RIGHT 1 +#define BOTTOM 2 +#define LEFT 3 + + +bool match[128][128]; +int C[43], n, m, sz; +string P[43]; +char M[7][7]; +bool backtrack(char l, int x, int y) { + if (y == n && M[x-1][n]!='F') + return 0; + + if (x > m) { + if (l != 'F') return 0; + x=1; ++y; + } + + if (y > n) return 1; + + + for (int i=0; i 0 && match[ l ][ P[i][LEFT] ] && match[ M[x][y-1] ][ P[i][TOP] ]) { + --C[i]; + M[x][y] = P[i][BOTTOM]; + if (backtrack(P[i][RIGHT], x+1, y)) + return 1; + ++C[i]; + } + + return 0; +} + +bool solve() { + sort(P, P+n*m); + sz = C[0] = 1; + int cnt[4] = {}; + for (int j=0; j<4; ++j) + cnt[j] += P[0][j]=='F'; + for (int i=1; i>n>>m && (n||m)) { + for (int i=0; i> P[i]; + + cout << (solve() ? "YES\n" : "NO\n"); + } +} diff --git a/uva_cpp_clean/523/523-21.cpp b/uva_cpp_clean/523/523-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e82da497ae15911e4b807675a8ab6323d4aee1e2 --- /dev/null +++ b/uva_cpp_clean/523/523-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 523 + Name: Minimum Transport Cost + Problem: https://onlinejudge.org/external/5/523.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include + +using namespace std; +int w[1000][1000]; +int v[100]; +int mid[1000][1000]; + +int main(){ + int M; + int i,j,t,n; + int src,dest; + char *p,line[1000]; + + scanf("%d\n",&M); + while(M--){ + gets(line); + for(n=1,p=strtok(line," ");p;p=strtok(0," "),n++){ + sscanf(p,"%d",&w[1][n]); + if(w[1][n]==-1)w[1][n]=100000; + mid[1][n]=n; + }n--; + + for(i=2;i<=n;i++) + for(j=1;j<=n;j++){ + scanf("%d",&w[i][j]); + if(w[i][j]==-1)w[i][j]=100000; + mid[i][j]=j; + } + + for(i=1;i<=n;i++) + scanf("%d",&v[i]); + + for(t=1;t<=n;t++) + for(i=1;i<=n;i++) + for(j=1;j<=n;j++) + if(w[i][t]+w[t][j]+v[t]1) + { + sscanf(line,"%d%d",&src,&dest); + printf("From %d to %d :\nPath: ",src,dest); + for(i=src;i!=dest;i=mid[i][dest]) + cout<"; + cout< +#include +#include +#include + +using namespace std; + +int n, ans[20]; + +bool isPrime(int number) +{ + for (int i = 2; i * i <= number; i++) + { + if (number % i == 0) + { + return false; + } + } + + return true; +} + +void backtrack(bool visit[20], int ind, int cur) +{ + ans[ind] = cur; + + if (ind == n && isPrime(1 + ans[ind])) + { + cout << ans[1]; + + for (int i = 2; i <= n; i++) + { + cout << " " << ans[i]; + } + + cout << endl; + } + + if (cur % 2 == 1) + { + for (int i = 2; i <= n; i += 2) + { + if (!visit[i] && isPrime(i + cur)) + { + visit[i] = true; + + backtrack(visit, ind + 1, i); + + visit[i] = false; + } + } + } + + else + { + for (int i = 3; i <= n; i += 2) + { + if (!visit[i] && isPrime(i + cur)) + { + visit[i] = true; + + backtrack(visit, ind + 1, i); + + visit[i] = false; + } + } + } +} + +int main() +{ + int ccc = 1; + + ios::sync_with_stdio(0); + + while (cin >> n) + { + if (ccc != 1) + { + cout << endl; + } + + cout << "Case " << ccc << ":" << endl; + + bool visit[20]; + memset(visit, 0, sizeof visit); + memset(ans, 0, sizeof ans); + + backtrack(visit, 1, 1); + + ccc++; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/524/524-17.cpp b/uva_cpp_clean/524/524-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4016a81e441be13ad171cf1c17a83b81fdab79aa --- /dev/null +++ b/uva_cpp_clean/524/524-17.cpp @@ -0,0 +1,41 @@ +/** + * Prime Ring Problem + */ +#include +using namespace std; +int n; +vector arr; +set primes {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; +vector vis; +void dfs(int cur) +{ + if (cur == n && primes.count(1 + arr.back())) + { + for (int i = 0; i < n - 1; ++i) + cout << arr[i] << " "; + cout << arr.back() << "\n"; + } + else + for (int i = 2; i <= n; ++i) + if (!vis[i] && primes.count(i + arr[cur - 1])) + { + arr[cur] = i; + vis[i] = true; + dfs(cur + 1); + vis[i] = false; + } +} +int main() +{ + int T = 0; + while (cin >> n) + { + if (T) + cout << "\n"; + cout << "Case " << ++T << ":\n"; + arr = vector(n); + arr[0] = 1; + vis = vector(n + 1, false); + dfs(1); + } +} diff --git a/uva_cpp_clean/524/524-18.cpp b/uva_cpp_clean/524/524-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..fe3433a60df1af903e4e5a8dc60afa937de47e08 --- /dev/null +++ b/uva_cpp_clean/524/524-18.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int primeN[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41}, ans[18] = {1}, n; + +bool isPrime(int number){ + for(int i = 0; i < 13; i++) if(number == primeN[i]) return true; + return false; +} + +void DFS(int actual, bool visited[18]){ + if(actual == n){ + if(isPrime(ans[n - 1] + 1)){ + printf("1"); + for(int i = 1; i < n; i++) printf(" %d", ans[i]); + printf("\n"); + } + return; + }else{ + for(int i = 2; i <= n; i++){ + if(visited[i]) continue; + if(isPrime(i + ans[actual - 1])){ + visited[i] = true; + ans[actual] = i; + DFS(actual + 1, visited); + visited[i] = false; + } + } + } +} + +int main(){ + int cases = 0; + bool nl = false; + while(scanf("%d", &n) == 1){ + cases++; + if(nl) printf("\n"); + printf("Case %d:\n", cases); + nl = true; + bool visited[18] = {0}; + DFS(1, visited); + } + return 0; +} +/* + La estrategia es tratar el circulo como un grafo y cada numero como un nodo, de esta manera se usaria el DFS para + poder dar con la solucion, cuando la recursividad llegue al caso base se imprime el vector de respuesta, la + recursividad hace todo el trabajo, pero para que pueda imprimir en orden anti horario, se tiene que cambiar el + estado de visitado a falso luego del llamado recursivo, esto es para que se pueda volver a reutilizar el nodo. En + otras palabras, no hay que dejarse confundir con que hay que imprimir ya sea en orden horario o no, realmente se + imprime todas las posibilidades. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/524/524-19.cpp b/uva_cpp_clean/524/524-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..71ce6b4916cf00a05e0c0dbcd8a42f75a79447bf --- /dev/null +++ b/uva_cpp_clean/524/524-19.cpp @@ -0,0 +1,46 @@ +#include +#include +using namespace std; + +unordered_set primes({2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}); +int sol[16], n; +bool used[17]; + +void proc(int pos) { + if(pos == n) { + if (primes.count(sol[n-1] + sol[0])) { + bool first = false; + for (int i = 0; i < n; i++) { + if (first) printf(" "); + else first = true; + printf("%d", sol[i]); + } + printf("\n"); + } + } else { + for (int i = 2; i <= n; i++) { + if (!used[i] && primes.count(sol[pos-1] + i)) { + used[i] = true; + sol[pos] = i; + proc(pos+1); + used[i] = false; + } + } + } +} + +int main() { + int cases = 1; + sol[0] = 1; + for (int i = 2; i <= n; i++) { + used[i] = false; + } + bool first = false; + while (scanf("%d", &n) != EOF) { + if (first) printf("\n"); + else first = true; + printf("Case %d:\n", cases++); + proc(1); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/524/524-21.cpp b/uva_cpp_clean/524/524-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a4cb460b7874ce18c94187cf1ca1a3131cd3671 --- /dev/null +++ b/uva_cpp_clean/524/524-21.cpp @@ -0,0 +1,57 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 524 + Name: Prime Ring Problem + Problem: https://onlinejudge.org/external/5/524.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int isprime(int x){ + int i; + for(i=2;i<=sqrt((float)x);i++) + if(x%i==0) + return 0; + return 1; +} +int isp[35],ans[17],n; + +void rec(int j,int used){ + int i; + if(j==n){ + if(!isp[ans[j-1]+1])return; + for(i=0;i recursive backtracking > pruned permutations + difficulty: medium + date: 13/Jan/2020 + by: @brpapa +*/ +#include +#include +using namespace std; + +bool isPrime[32] = {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1}; + +int N, p[16]; +bitset<16+1> used; // used[v] é true, se p[i] == v, para algum 0 <= i < N + +// p[i] pode ter valor v? +bool ok(int i, int v) { + return isPrime[p[i-1]+v]; +} + +void bt(int i) { + // gera p[i] + + if (i == N && isPrime[p[N-1]+p[0]]) { + for (int i = 0; i < N-1; i++) cout << p[i] << " "; cout << p[N-1] << "\n"; + return; + } + for (int v = 2; v <= N; v++) + if (!used[v] && ok(i, v)) { + p[i] = v; + used[v] = true; + bt(i+1); + used[v] = false; + } +} + +int main() { + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int T = 0; + while (cin >> N) { + cout << (T > 0? "\n":"") << "Case " << ++T << ":\n"; + for (int i = 0; i < N; i++) p[i] = i+1; + used.reset(); + used[1] = true; // p[0] é 1 + bt(1); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/526/526-19.cpp b/uva_cpp_clean/526/526-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bce9658f1b77c5204295be96170fdc592fb2b00 --- /dev/null +++ b/uva_cpp_clean/526/526-19.cpp @@ -0,0 +1,72 @@ +#include +#include +using namespace std; + +int N, M; +string x, y; +unordered_map > dp; +unordered_map > decdp; + +int proc(int i, int j) { + if (i >= N) return M - j; + if (j >= M) return N - i; + if (!dp[i].count(j)) { + if (x[i] == y[j]) { + dp[i][j] = proc(i+1, j+1); + decdp[i][j] = 0; + } + else { + int v1 = proc(i+1, j+1); + int v2 = proc(i+1, j); + int v3 = proc(i, j+1); + dp[i][j] = 1 + min(v1, min(v2, v3)); + if (v1 + 1 == dp[i][j]) decdp[i][j] = 1; + else if (v2 + 1 == dp[i][j]) decdp[i][j] = 2; + else if (v3 + 1 == dp[i][j]) decdp[i][j] = 3; + } + } + return dp[i][j]; +} + +int c; + +void proc2(int i, int j, int idx) { + c++; + if (i >= N) { + for (; j < M; j++, c++, idx++) printf("%d Insert %d,%c\n", c, idx, y[j]); + return; + } + if (j >= M) { + for (; i < N; i++, c++) printf("%d Delete %d\n", c, idx); + return; + } + if (decdp[i][j] == 0) { + c--; + proc2(i+1, j+1, idx+1); + } else if (decdp[i][j] == 1) { + printf("%d Replace %d,%c\n", c, idx, y[j]); + proc2(i+1, j+1, idx+1); + } else if (decdp[i][j] == 2) { + printf("%d Delete %d\n", c, idx); + proc2(i+1, j, idx); + } else if (decdp[i][j] == 3) { + printf("%d Insert %d,%c\n", c, idx, y[j]); + proc2(i, j+1, idx+1); + } +} + +int main() { + bool first = false; + while (getline(cin, x) && getline(cin, y)) { + N = x.length(); + M = y.length(); + if (first) printf("\n"); + else first = true; + dp.clear(); + decdp.clear(); + printf("%d\n", proc(0, 0)); + c = 0; + proc2(0, 0, 1); + } + return 0; +} diff --git a/uva_cpp_clean/526/526-21.cpp b/uva_cpp_clean/526/526-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18e48cebb9a2f4c6693bdbee59d4977a2e6e8089 --- /dev/null +++ b/uva_cpp_clean/526/526-21.cpp @@ -0,0 +1,65 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 526 + Name: String Distance and Transform Process + Problem: https://onlinejudge.org/external/5/526.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + char s1[100], s2[100]; + int DP[100][100], P[100][100], i, j, c; + for (int i=0; i<100; i++) { + DP[0][i] = DP[i][0] = i; + P[0][i] = 1; P[i][0] = 2; + } + + bool frst = 1; + while (cin.getline(s1+1, 99) && cin.getline(s2+1, 99)) { + if (frst) frst = 0; + else putchar('\n'); + + for (i=1; s1[i]; i++) + for (j=1; s2[j]; j++) + if (s1[i] == s2[j]) { + DP[i][j] = DP[i-1][j-1]; + P[i][j] = 3; + } + else if (DP[i][j-1] < DP[i-1][j] && DP[i][j-1] < DP[i-1][j-1]) { + DP[i][j] = DP[i][j-1] + 1; + P[i][j] = 1; + } + else if (DP[i-1][j] < DP[i-1][j-1]) { + DP[i][j] = DP[i-1][j] + 1; + P[i][j] = 2; + } + else { + DP[i][j] = DP[i-1][j-1] + 1; + P[i][j] = 3; + } + + int id = 0; + printf("%d\n", c = DP[--i][--j]); + while (i || j) { + if (P[i][j] == 1) + printf("%d Insert %d,%c\n", ++id, i+1, s2[j--]); + + else if (P[i][j] == 2) + printf("%d Delete %d\n", ++id, i--); + + else { + if (s1[i] != s2[j]) + printf("%d Replace %d,%c\n", ++id, i, s2[j]); + --i; --j; + } + } + } +} diff --git a/uva_cpp_clean/526/526-9.cpp b/uva_cpp_clean/526/526-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a58b9362172c9ae460e858dfbdd2224d1d5395a --- /dev/null +++ b/uva_cpp_clean/526/526-9.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +const int W_DEL = 1, W_INS = 1, W_SUB = 1; + +const int MAX_LEN = 80 + 10; + +string s1, s2; +int dp[MAX_LEN][MAX_LEN]; + +struct Move { + char move, ch; + int pos; + Move () {} + Move (char move_, char ch_, int pos_): move(move_), ch(ch_), pos(pos_) {} +}; + +void stringAlignment () { + int m = s1.size(), n = s2.size(); + for (int i = 0; i <= n; i++) { + for (int j = 0; j <= m; j++) { + if (i == 0) dp[0][j] = j * W_INS; + else if (j == 0) dp[i][j] = i * W_DEL; + else if (s1[j - 1] == s2[i - 1]) dp[i][j] = dp[i - 1][j - 1]; + else dp[i][j] = min(dp[i - 1][j - 1] + W_SUB, min(dp[i - 1][j] + W_DEL, dp[i][j - 1] + W_INS)); + } + } + int i = n, j = m; + vector ans; + while (i or j) { + if (i and j and s1[j - 1] == s2[i - 1]) i--, j--; + else if (i and j and dp[i - 1][j - 1] + W_SUB == dp[i][j]) { + ans.push_back(Move('C', s2[i - 1], i)); + i--, j--; + } + else if (i and dp[i - 1][j] + W_DEL == dp[i][j]) { + ans.push_back(Move('I', s2[i - 1], i)); + i--; + } + else if (j and dp[i][j - 1] + W_INS == dp[i][j]) { + ans.push_back(Move('D', s1[j - 1], j)); + j--; + } + } + reverse(begin(ans), end(ans)); + int cnt_i = 0, cnt_d = 0; + cout << dp[n][m] << endl; + for (int i = 0; i < ans.size(); i++) { + if (ans[i].move == 'D') ans[i].pos = ans[i].pos + cnt_i - cnt_d; + cout << i + 1 << ' '; + if (ans[i].move == 'D') cout << "Delete " << ans[i].pos << endl; + if (ans[i].move == 'I') cout << "Insert " << ans[i].pos << ',' << ans[i].ch << endl; + if (ans[i].move == 'C') cout << "Replace " << ans[i].pos << ',' << ans[i].ch << endl; + if (ans[i].move == 'I') cnt_i++; + if (ans[i].move == 'D') cnt_d++; + } +} + +int main () { + bool first = true; + while (getline(cin, s1) and getline(cin, s2)) { + if (not first) puts(""); + stringAlignment(); + first = false; + } + return (0); +} diff --git a/uva_cpp_clean/529/529-21.cpp b/uva_cpp_clean/529/529-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f46c0fbd8c7b083f97d65eb92305e778049d4501 --- /dev/null +++ b/uva_cpp_clean/529/529-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 529 + Name: Addition Chains + Problem: https://onlinejudge.org/external/5/529.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +inline int bound(int n,int t){ + return ceil(log((float)n/t)/0.693147); // [log2(n/t)] +} + + +int n,ans[10001],best; +int A[10001]={1,0}; +void AdditionChain(int i) +{ + int k,j; + + for(j=i-1;j>=0;j--) + for(k=j;k>=0;k--) + if(A[k]+A[j]<=n && A[k]+A[j]>A[i-1]) + if(i+bound(n,A[i-1])0) + { + best=1000; + if(n==1) + best=1; + else + AdditionChain(1); + + cout<<1; + for(i=1;i +//#include +using namespace std; + +/*unordered_map > comb; + +int proc(int n, int m) { + if (m == 0 || m == n) return 1; + if (m < 0 || m > n) return 0; + if (comb[n][m] == 0) comb[n][m] = proc(n-1, m-1) + proc(n-1, m); + return comb[n][m]; +}*/ + +int main() { + int n, m; + while (scanf("%d %d", &n, &m) && !(n == 0 && m == 0)) { + //printf("%d\n", proc(n, m)); // TLE + if (m > n/2) m = n - m; + unsigned long long comb = 1; + for (int i = 0; i < m; i++) { + comb *= n-i; // comb = (n! / (n-m!)) * (1 / m!) + comb /= i + 1; + } + printf("%llu\n", comb); + } + return 0; +} diff --git a/uva_cpp_clean/530/530-5.cpp b/uva_cpp_clean/530/530-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..da4212dd813f2b290b6d0da20fe018320d0745f9 --- /dev/null +++ b/uva_cpp_clean/530/530-5.cpp @@ -0,0 +1,27 @@ +#include + +long long nCr(long long n,long long m) +{ +long long res=1,i; +if(n-m + +using namespace std; + +const int SIZE = 1005; + +int n, k; + +long long BinomialCoef(int N, int K){ + long long C = 1; + if( K > N - K ) K = N - K; + for(int ct = 0; ct < K; ct++) + C = C * ( N - ct ) / ( 1 + ct ); + return C; +} + +int main(){ + while(scanf("%d %d", &n, &k), n | k) + printf("%lld\n", BinomialCoef(n, k)); + return(0); +} diff --git a/uva_cpp_clean/531/531-10.cpp b/uva_cpp_clean/531/531-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c66d036f41cb03eb461a6baae8c5139e8212db35 --- /dev/null +++ b/uva_cpp_clean/531/531-10.cpp @@ -0,0 +1,110 @@ +#include +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define oo 1e15 +using namespace std; +/** + * by:Ahmed Sakr (sakr_) with AZA ; + * freopen("input.txt","r",stdin); + * freopen("output.txt","w",stdout); + * __builtin_popcountll(); +**/ +class utility { + +public : + static int gcd(int a, int b) { return (!b) ? a : gcd(b, a % b); } + static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } +}; + +vector a,b; +string res; + +int n,m; +int sizee; +int dp[3005][3005]; + +int solve(int i,int j) { + + if (i == n || j == m) return 0; + + int &ans = dp[i][j]; + if (ans != -1) return ans; + + + ans =0; + + if (a[i] == b[j]) { + ans = max(ans,solve(i + 1, j + 1) + 1); + } else { + ans = max(ans,max(solve(i + 1, j), solve(i, j + 1))); + } + + return ans; +} + +void buildAns(int i,int j) { + + if(i==n || j==m) return; + + if(a[i]==b[j]) { + sizee--; + if(sizee==0) + res+=a[i]; + else + res+= a[i]+" "; + + buildAns(i+1,j+1); + } else if(dp[i+1][j]>dp[i][j+1]) { + buildAns(i+1,j); + } else { + buildAns(i,j+1); + } + +} + + +void solveit() { + + + string s; + + while (cin>>s) { + + a.clear(),b.clear(),res.clear(); + memset(dp,-1,sizeof dp); + + a.push_back(s); + + while (cin>>s) { + if(s=="#") break; + a.push_back(s); + } + + while (cin>>s) { + if(s=="#") break; + b.push_back(s); + } + + n=a.size(); + m=b.size(); + + sizee = solve(0,0); + buildAns(0,0); + cout<>timeOfRepeteLoop; + while (timeOfRepeteLoop--) { + solveit(); + } + +} \ No newline at end of file diff --git a/uva_cpp_clean/531/531-14.cpp b/uva_cpp_clean/531/531-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eeb654fece8be64e977daafb7252e28ab5e99dcf --- /dev/null +++ b/uva_cpp_clean/531/531-14.cpp @@ -0,0 +1,133 @@ +/*************************************************** + * Problem Name : 531 - Compromise.cpp + * Problem Link : https://uva.onlinejudge.org/external/5/531.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-02-06 + * Problem Type : dp(lcs) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 1005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorA, B, ans; +int dp[MAX][MAX]; +int n, m; +int LCS (int i, int j) { + if (i == n || j == m) return 0; + + if (dp[i][j] != -1) return dp[i][j]; + + int ans = 0; + + if (A[i] == B[j]) ans = 1 + LCS (i + 1, j + 1); + else { + int val1 = LCS (i + 1, j); + int val2 = LCS (i, j + 1); + ans = max (val1, val2); + } + + return dp[i][j] = ans; +} +void printALCS (int i, int j) { + if (i == n || j == m) { + for (int k = 0; k < (int) ans.size(); k++) { + if (k != 0) cout << " "; + + cout << ans[k]; + } + + nl; + return; + } + + if (A[i] == B[j]) { + ans.pb (A[i]); + printALCS (i + 1, j + 1); + ans.erase (ans.end() - 1); + + } else { + if (dp[i + 1][j] >= dp[i][j + 1]) printALCS (i + 1, j); + else { + printALCS (i, j + 1); + } + } +} +int main () { + __FastIO; + string str; + + while (cin >> str) { + A.pb (str); + + while (cin >> str) { + if (str == "#") break; + + A.pb (str); + } + + while (cin >> str) { + if (str == "#") break; + + B.pb (str); + } + + n = (int) A.size(); + m = (int) B.size(); + SET (dp, -1); + LCS (0, 0); + printALCS (0, 0); + A.clear(), B.clear(), ans.clear(); + } + + return 0; +} diff --git a/uva_cpp_clean/531/531-21.cpp b/uva_cpp_clean/531/531-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4ee4ffababc2b049890ff007e8c7a91cfad112f --- /dev/null +++ b/uva_cpp_clean/531/531-21.cpp @@ -0,0 +1,74 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 531 + Name: Compromise + Problem: https://onlinejudge.org/external/5/531.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int n, m; +int DP[104][104], P[104][104]; +char words1[104][40], + words2[104][40]; + +bool eq(int u, int v) { + int i; + for (i=0; words1[u][i]; i++) + if (words1[u][i] != words2[v][i]) + return 0; + return words1[u][i] == words2[v][i]; +} + + +bool print(int i, int j) { + if (!i || !j) return 1; + + if (!P[i][j]) { + if (!print(i-1, j-1)) + cout << " "; + cout << words1[i-1]; + + return 0; + } + + return P[i][j]==1 + ? print(i-1, j) + : print(i, j-1); +} + +int main(){ + while (cin>>words1[n=0]) { + while (words1[n][0] != '#') + cin>>words1[++n]; + + cin>>words2[m=0]; + while (words2[m][0] != '#') + cin>>words2[++m]; + + for (int i=1; i<=n; i++) + for (int j=1; j<=m; j++) + if (eq(i-1, j-1)) { + DP[i][j] = DP[i-1][j-1] + 1; + P[i][j] = 0; + } + else if (DP[i-1][j] >= DP[i][j-1]) { + DP[i][j] = DP[i-1][j]; + P[i][j] = 1; + } + else { + DP[i][j] = DP[i][j-1]; + P[i][j] = 2; + } + + print(n, m); + putchar('\n'); + } +} diff --git a/uva_cpp_clean/531/531-9.cpp b/uva_cpp_clean/531/531-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0e8a17484c332b8065ed8d20503d19b180feee5 --- /dev/null +++ b/uva_cpp_clean/531/531-9.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +const int MAX_LEN = 3e3 + 10; + +int dp[MAX_LEN][MAX_LEN]; +vector s1, s2, ans; + +void lcs () { + int n = s1.size(), m = s2.size(); + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (s1[i - 1] == s2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; + else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + int i = n, j = m; + while (i and j) { + if (s1[i - 1] == s2[j - 1]) { + ans.push_back(s1[i - 1]); + i--; + j--; + } + else if (dp[i - 1][j] == dp[i][j]) i--; + else j--; + } + reverse(begin(ans), end(ans)); + if (not ans.empty()) cout << ans[0]; + for (int i = 1; i < ans.size(); i++) cout << ' ' << ans[i]; + cout << endl; + s1.clear(), s2.clear(), ans.clear(); +} + +bool read (vector & s) { + string line; + bool endOfFile = true; + while (getline(cin, line) and line[0] != '#') { + endOfFile = false; + istringstream token(line); + string ss; + while (token >> ss) s.push_back(ss); + } + return not endOfFile; +} + +int main () { + while (read(s1) and read(s2)) lcs(); + return (0); +} diff --git a/uva_cpp_clean/532/532-15.cpp b/uva_cpp_clean/532/532-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1145bc083869fb787e8142e6ca0ca1e4dffb053d --- /dev/null +++ b/uva_cpp_clean/532/532-15.cpp @@ -0,0 +1,59 @@ +/* + 三維迷宮、BFS +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int l, r, c, level, column, row, step; + tuple exit_position; + + while (cin >> l >> r >> c && l) { + queue, int>> bfs_queue; + vector>> maze(l, vector>(r, vector(c))); + + for (level = 0; level < l; level++) + for (row = 0; row < r; row++) + for (column = 0; column < c; column++) { + cin >> maze[level][row][column]; + if (maze[level][row][column] == 'S') + bfs_queue.push(make_pair(make_tuple(level, row, column), 0)); + else if (maze[level][row][column] == 'E') + exit_position = make_tuple(level, row, column); + } + + while (!bfs_queue.empty()) { + step = bfs_queue.front().second; + + if (exit_position == bfs_queue.front().first) + break; + tie(level, row, column) = bfs_queue.front().first; + bfs_queue.pop(); + + if (level < 0 || level >= l || row < 0 || row >= r || column < 0 || column >= c || maze[level][row][column] == '#') + continue; + + maze[level][row][column] = '#'; + + bfs_queue.push(make_pair(make_tuple(level + 1, row, column), step + 1)); + bfs_queue.push(make_pair(make_tuple(level - 1, row, column), step + 1)); + bfs_queue.push(make_pair(make_tuple(level, row + 1, column), step + 1)); + bfs_queue.push(make_pair(make_tuple(level, row - 1, column), step + 1)); + bfs_queue.push(make_pair(make_tuple(level, row, column + 1), step + 1)); + bfs_queue.push(make_pair(make_tuple(level, row, column - 1), step + 1)); + } + + if (bfs_queue.empty()) + cout << "Trapped!\n"; + else + cout << "Escaped in " << step << " minute(s).\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/532/532-21.cpp b/uva_cpp_clean/532/532-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cbba2f0287a12b611d0958bf466552c0cedeca3 --- /dev/null +++ b/uva_cpp_clean/532/532-21.cpp @@ -0,0 +1,84 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 532 + Name: Dungeon Master + Problem: https://onlinejudge.org/external/5/532.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +struct cell{ + int x,y,z; + int step; +} sp,ep; +deque st; +int map[50][50][50]; +int m,n,p; + +bool good(struct cell u){ + if(u.z<0 || u.y<0 || u.x<0)return 0; + if(u.z>=m || u.y>=n || u.x>=p)return 0; + if(map[u.z][u.y][u.x]!=0)return 0; + map[u.z][u.y][u.x]=1; + return 1; +} +void pushh(int x,int y,int z,int s){ + struct cell u; + u.x=x;u.y=y;u.z=z;u.step=s+1; + if(good(u))st.push_back(u); +} +void bfs(){ + struct cell t,u; + + st.push_back(sp); + map[sp.z][sp.y][sp.x]=1; + while(!st.empty()){ + t=st.front();st.pop_front(); + if(t.x==ep.x && t.y==ep.y && t.z==ep.z){ + printf("Escaped in %d minute(s).\n",t.step); + return; + } + pushh(t.x+1,t.y,t.z,t.step); + pushh(t.x,t.y+1,t.z,t.step); + pushh(t.x,t.y,t.z+1,t.step); + pushh(t.x-1,t.y,t.z,t.step); + pushh(t.x,t.y-1,t.z,t.step); + pushh(t.x,t.y,t.z-1,t.step); + } + printf("Trapped!\n"); +} + +int main(){ + int i,j,k; + char ch; + + while(scanf("%d%d%d",&m,&n,&p) && m+n+p){ + memset(map,0,sizeof(map)); + st.clear(); + for(i=0;i +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e4+9; + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + + int count(int u) { + return sz[find(u)]; + } +}; + +vector> adj[N]; +int vis[N]; + +void minimumSpanningTree(vector> &edges) { + + + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + double ans = 1e9,weight; + int a,b; + for (auto e: edges) { + + tie(weight,a,b) = e; + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + + adj[a].push_back({weight,b}); + adj[b].push_back({weight,a}); + + ///cout<>n) { + if(n==0) break; + + vector> arr; + for(int i=0;i>x>>y; + arr.push_back({x,y}); + } + + vector> resarr; + memset(adj,0,sizeof adj); + memset(vis,0,sizeof vis); + + for(int i=0;i +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, k, n, Scenario = 1; + vector> points(200); + vector> dist(200, vector(200)); + + while (cin >> n && n) { + for (i = 0; i < n; i++) + cin >> points[i].first >> points[i].second; + + // 距離矩陣 + for (i = 0; i < n; i++) + for (j = 0; j < i; j++) + dist[j][i] = dist[i][j] = hypot(points[i].first - points[j].first, points[i].second - points[j].second); + + // Floyd-Warshall Algorithm + for (k = 0; k < n; k++) + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + dist[i][j] = min(dist[i][j], max(dist[i][k], dist[k][j])); // 修改成找 兩者最長路 和 原路 長度較短者 + + cout << "Scenario #" << Scenario++ << "\nFrog Distance = " << setprecision(3) << fixed << dist[0][1] << "\n\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/534/534-19.cpp b/uva_cpp_clean/534/534-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d6bf85516ede8bcf0ae963ab8c74b00b3b01e44 --- /dev/null +++ b/uva_cpp_clean/534/534-19.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair stone; +typedef pair ii; +typedef pair jump; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +priority_queue pq; +vector v; + +int main() { + int n, cases = 1; + while (cin >> n && n != 0) { + printf("Scenario #%d\n", cases++); + v.clear(); + for (int i = 0; i < n; i++) { + double x, y; + cin >> x >> y; + v.push_back(stone(x,y)); + for (int j = 0; j < v.size() - 1; j++) { + jump l; l.second.first = j; l.second.second = i; + double dx = x - v[j].first, dy = y - v[j].second; + l.first = -sqrt(dx*dx + dy*dy); + pq.push(l); + } + } + ufds u(n); + double d = 0; + while (!pq.empty()) { + if (!u.isSameSet(pq.top().second.first, pq.top().second.second) && !u.isSameSet(0, 1)) { + d = max(d, -pq.top().first); + u.unionSet(pq.top().second.first, pq.top().second.second); + } + pq.pop(); + } + printf("Frog Distance = %.3f\n", d); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/534/534-20.cpp b/uva_cpp_clean/534/534-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6735ecf10c4f3516176f3a0403c5226f6e73a433 --- /dev/null +++ b/uva_cpp_clean/534/534-20.cpp @@ -0,0 +1,37 @@ +#include +#include + +#define MAX 205 +#define Min(a, b) ((ab)?(a):(b)) +#define sqr(n) ((n)*(n)) + +double x[MAX], y[MAX], distance[MAX][MAX]; + +int main() +{ + int n, nCase = 1; + while( scanf("%d", &n)==1 && n ) + { + for(int i=1 ; i<=n ; i++) scanf("%lf %lf", &x[i], &y[i]); + + for(int i=0 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 534 + Name: Frogger + Problem: https://onlinejudge.org/external/5/534.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXN 202 +int X[MAXN], Y[MAXN]; + +struct Edge { + int u, v; + double dist; + Edge(int u, int v): u(u), v(v) { + double x = X[u] - X[v]; + double y = Y[u] - Y[v]; + dist = sqrt(x*x + y*y); + } + + bool operator < (const Edge &e) const { + return dist > e.dist; + } +}; + +// --------------------------------------------------------- + +int ids[MAXN]; +int find(int u) { + if (ids[u] == u) return u; + return ids[u] = find(ids[u]); +} + +void join(int u, int v) { + ids[find(u)] = find(v); +} + +// --------------------------------------------------------- + + +vector adj[MAXN]; +double mp[MAXN][MAXN]; +int seen[MAXN]; + +double dfs(int v, int w) { + if (v==w) return 0; + if (seen[v]) return -1; + seen[v] = 1; + + double mx = -1; + for (int i: adj[v]) { + double r = dfs(i, w); + if (r >= 0) + mx = max(mx, max(r, mp[v][i])); + } + + seen[v] = 0; + return mx; +} + +// --------------------------------------------------------- + +int main(){ + int cse=1, n; + while (cin>>n && n) { + for (int i=0; i>X[i]>>Y[i]; + adj[i].clear(); + ids[i] = i; + } + + priority_queue eds; + + for (int i=0; i + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 40000, LIM = 210; + +struct Edge_{ + int u, v, w; + Edge_() {}; + Edge_(int u_, int v_, int w_): + u(u_), v(v_), w(w_) {}; + bool operator < (const Edge_& other) const{ + return ( w < other.w ); + } +}E[SIZE]; + +struct Point_{ + int x, y; +}P[LIM]; + +int tc, n, m; +vector < int > AdjList[LIM]; +bool vis[LIM]; + +inline int pow2(int P_){ + return P_ * P_; +} + +inline int dis(int i, int j){ + return pow2(P[i].x - P[j].x) + pow2(P[i].y - P[j].y); +} + +void Kruskal(){ + UnionFind UF(n); + sort(E, E + m); + int setSize = 1; + for(int it = 0; it < m and setSize != n; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ){ + AdjList[E[it].u].push_back(E[it].v); + AdjList[E[it].v].push_back(E[it].u); + setSize++; + UF.unionSet(E[it].u, E[it].v); + } +} + +double dfs(int u, double w = 0){ + vis[u] = true; + if( u == 1 ) return w; + double ans = 0; + for(int it = 0; it < AdjList[u].size(); it++){ + int v = AdjList[u][it]; + if( not vis[v] ) + ans = max(ans, dfs(v, max(w, sqrt(dis(u, v)) ) ) ); + } + return ans; +} + +void clear_(){ + fill(vis, vis + n, false); + for(int it = 0; it < n; it++) + AdjList[it].clear(); + m = 0; +} + +int main(){ + while(scanf("%d", &n), n){ + for(int it = 0; it < n; it++) + scanf("%d %d", &P[it].x, &P[it].y); + for(int i = 0; i < n; i++) + for(int j = i + 1; j < n; j++) + E[m++] = Edge_(i, j, dis(i, j)); + Kruskal(); + printf("Scenario #%d\n", ++tc); + printf("Frog Distance = %.3lf\n\n", dfs(0)); + clear_(); + } + return(0); +} diff --git a/uva_cpp_clean/536/536-15.cpp b/uva_cpp_clean/536/536-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62e0eb998a41496a07143fcc2c84ff4bd00510f8 --- /dev/null +++ b/uva_cpp_clean/536/536-15.cpp @@ -0,0 +1,36 @@ +/* + 樹的遍歷 + 時間複雜度: O(N^2) +*/ +#include +#include + +using namespace std; + +void post_order(int inorder_start, int inorder_end, int&& preorder_head, const string& inorder, const string& preorder) { + if (inorder_start < inorder_end) { + int root = inorder_start, left_chlid_size; + + while (inorder[root] != preorder[preorder_head]) + root++; // 找當前的根節點 + + left_chlid_size = root - inorder_start; //左子樹內點的個數 + + post_order(inorder_start, inorder_start + left_chlid_size, preorder_head + 1, inorder, preorder); // 左子樹 + post_order(root + 1, inorder_end, preorder_head + left_chlid_size + 1, inorder, preorder); // 右子樹 + + cout << inorder[root]; // 後序 + } +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + string preorder, inorder; + while (cin >> preorder >> inorder) { + post_order(0, preorder.length(), 0, inorder, preorder); + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/536/536-17.cpp b/uva_cpp_clean/536/536-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf705e2533daa79961612feea0ab5bf42f748bbc --- /dev/null +++ b/uva_cpp_clean/536/536-17.cpp @@ -0,0 +1,29 @@ +/** + * AOAPC II Exercise 6-3 Tree Recovery + * ULM Local 1997 + * 20 AC; + */ +#include +using namespace std; +string preord, inord, postord; +void recover(int pre_beg, int pre_end, int in_beg, int in_end) +{ + char root = preord[pre_beg]; + int in_root_pos = inord.find(root), left_sz = in_root_pos - in_beg; + if (left_sz) + recover(pre_beg + 1, pre_beg + left_sz, in_beg, in_root_pos); + if (in_root_pos < in_end - 1) + recover(pre_beg + 1 + left_sz, pre_end, in_root_pos + 1, in_end); + postord += root; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + while (cin >> preord >> inord) + { + postord = ""; + recover(0, preord.size(), 0, inord.size()); + cout << postord << "\n"; + } +} diff --git a/uva_cpp_clean/536/536-19.cpp b/uva_cpp_clean/536/536-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49a970f2d59e7192f841c60263e642cd44aad430 --- /dev/null +++ b/uva_cpp_clean/536/536-19.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; + +struct node { + node * left = nullptr; + node * right = nullptr; + char c; + + void build(string & preord, string & inord, int & idx, int & idy, bool * mask) { + c = preord[idx]; + mask[c-'A'] = true; + idx++; + if (c != inord[idy]) { + left = new node(); + left->build(preord, inord, idx, idy, mask); + } + idy++; + if (idy >= inord.length() || mask[inord[idy]-'A']) return; + right = new node(); + right->build(preord, inord, idx, idy, mask); + } + void clear() { + if (left != nullptr) { + left->clear(); + delete left; + } + if (right != nullptr) { + right->clear(); + delete right; + } + } + void print() { + if (left != nullptr) { + left->print(); + } + if (right != nullptr) { + right->print(); + } + printf("%c", c); + } +}; + +struct BT { + bool mask[30]; + node * root = nullptr; + + BT(string & preord, string & inord) { + for (int i = 0; i < 30; i++) mask[i] = false; + root = new node(); + int idx = 0, idy = 0; + root->build(preord, inord, idx, idy, mask); + } + ~BT() { + if (root != nullptr) { + root->clear(); + delete root; + } + } + void print() { + if (root != nullptr) { + root->print(); + } + printf("\n"); + } +}; + +int main() { + string preord, inord; + while (cin >> preord >> inord) { + BT bt(preord, inord); + bt.print(); + } + return 0; +} diff --git a/uva_cpp_clean/536/536-21.cpp b/uva_cpp_clean/536/536-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8831310596b7a2d884c296490c6d431c930a6cf3 --- /dev/null +++ b/uva_cpp_clean/536/536-21.cpp @@ -0,0 +1,45 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 536 + Name: Tree Recovery + Problem: https://onlinejudge.org/external/5/536.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include + +using namespace std; + +char pre[30],in[30],l; +void post(int x,int y){ + int i,j,k=0,p=0,t=1; + if(x>=y)return; + + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main() +{ + int t,no=0; + sc("%d",&t); + getchar(); + while(t--) + { + char s[10000],k[10000]; + gets(s); + int l=strlen(s),j,uu=0,aa=0,pp=0; + double a,p,u; + + for(int i=0;i +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, m; + char c; + double hour_angle, minute_angle, angle; + + while (cin >> n >> c >> m && (n || m)) { + hour_angle = n * 30 + m * 0.5; + minute_angle = m * 6; + angle = fabs(hour_angle - minute_angle); + if (angle > 180) + angle = 360 - angle; + cout << setprecision(3) << fixed << angle << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/539/539-21.cpp b/uva_cpp_clean/539/539-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e44fa9ff4c028a392e3618329d3b9b404c4fd88 --- /dev/null +++ b/uva_cpp_clean/539/539-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 539 + Name: The Settlers of Catan + Problem: https://onlinejudge.org/external/5/539.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int map[30][30]; +int cover[30][30]; +int maxdeph; +int n; + +void dfs(int node,int deph){ + int i; + + for(i=0;imaxdeph) + maxdeph=deph; +} + + +int main(){ + int m,i,a,b; + while(scanf("%d%d",&n,&m)==2 && m+n){ + memset(map,0,sizeof(map)); + memset(cover,0,sizeof(cover)); + maxdeph=0; + for(i=0;i + +using namespace std; + +#define N 1000 + +int nod, edg; +int a[N][N]; +int maxi = 0; + +void f(int k, int ans){ + maxi = max(ans,maxi); + for(int i = 0; i < nod; i++) + if(a[k][i] && i != k){ + a[k][i] = 0; + a[i][k] = 0; + f(i, ans + 1); + a[k][i] = 1; + a[i][k] = 1; + } +} + +int main(){ + int temp1, temp2; + while(scanf("%d%d", &nod, &edg), nod && edg){ + memset(a, 0, sizeof a); + for(int i = 0; i < edg; i++){ + cin >> temp1 >> temp2, + a[temp1][temp2] = 1; + } + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 1005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + int t; + int tc = 1; + while (scanf("%d", &t) == 1 && t != 0) { + vectorar[MAX]; + mapmp; + for (int i = 1; i <= t; i++) { + ll x; + scanf("%lld", &x); + for (int j = 1; j <= x; j++) { + ll value; + scanf("%lld", &value); + mp[value] = i; + } + } + string str; + ll chk[MAX]; + SET(chk, 0); + vectorteam, ans; + while (cin >> str) { + if (str == "STOP") break; + if (str == "ENQUEUE") { + ll teamMember; + scanf("%lld", &teamMember); + if (chk[mp[teamMember]] == 0) { + team.pb(mp[teamMember]); + chk[mp[teamMember]] = 1; + } + ar[mp[teamMember]].pb(teamMember); + } else if (str == "DEQUEUE") { + for (int i = 0; i < (int)team.size(); i++) { + ll x = team[i]; + if ((int)ar[x].size() == 0) { + continue; + } + ans.pb(ar[x][0]); + ar[x].erase (ar[x].begin() + 0); + if ((int)ar[x].size() == 0) { + team.erase (team.begin() + i); + chk[x] = 0; + } + break; + } + } + } + printf("Scenario #%d\n", tc++); + for (int i = 0; i < (int)ans.size(); i++) { + printf("%lld\n", ans[i]); + } + nl; + } + return 0; +} diff --git a/uva_cpp_clean/540/540-15.cpp b/uva_cpp_clean/540/540-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..837204c5b95e029ec8624872fdd3b923d29499a6 --- /dev/null +++ b/uva_cpp_clean/540/540-15.cpp @@ -0,0 +1,55 @@ +/* + 佇列 + 時間複雜度(ENQUEUE): O(1) + 時間複雜度(DEQUEUE): O(1) +*/ +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, n, scenario = 1, n_teammate, id, team_number; + string str; + + while (cin >> n && n) { + unordered_map team; + queue> team_queue; + vector*> team_position(n, NULL); + + // 建立 team 表(編號對應組別) + for (i = 0; i < n; i++) { + cin >> n_teammate; + while (n_teammate-- && cin >> id) + team[id] = i; + } + + cout << "Scenario #" << scenario++ << "\n"; + + while (cin >> str && str != "STOP") { + if (str == "DEQUEUE") { + cout << team_queue.front().front() << "\n"; // 最前面的元素是第一組的第一人 + if (team_queue.front().size() == 1) { // 那一組要沒人了 + team_position[team[team_queue.front().front()]] = NULL; // 新的組員沒有預設位置 + team_queue.pop(); // 整組刪掉 + } else // 如果人還很多 + team_queue.front().pop(); // 第一個離開就好 + } else { + cin >> id; + team_number = team[id]; + if (team_position[team_number] == NULL) { // 新的一組進來 + team_queue.push(queue({id})); // 按照規則排最後 + team_position[team_number] = &team_queue.back(); // 紀錄這組的位置 + } else // 已經有組員了 + team_position[team_number]->push(id); // 插隊 + } + } + cout << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/540/540-17.cpp b/uva_cpp_clean/540/540-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5ad8e940a54ab0188524fed5e8a2985268baad0 --- /dev/null +++ b/uva_cpp_clean/540/540-17.cpp @@ -0,0 +1,44 @@ +/** + * AOAPC II Example 5-6 Team Queue + */ +#include +using namespace std; +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int t, ts = 0; + while ((cin >> t) && t) { + cout << "Scenario #" << ++ts << "\n"; + map team; + int n; + for (int tn = 0; tn < t; ++tn) { + cin >> n; + while (n--) { + int mem; + cin >> mem; + team[mem] = tn; + } + } + queue team_q; + vector> mem_q(t); + string cmd; + while ((cin >> cmd) && cmd[0] != 'S') { + if (cmd[0] == 'E') { + int cur; + cin >> cur; + int tm = team[cur]; + if (mem_q[tm].empty()) + team_q.push(tm); + mem_q[tm].push(cur); + } + else { + auto &q = mem_q[team_q.front()]; + cout << q.front() << "\n"; + q.pop(); + if (q.empty()) + team_q.pop(); + } + } + cout << "\n"; + } +} diff --git a/uva_cpp_clean/540/540-18.cpp b/uva_cpp_clean/540/540-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6a8c82af142bc99c61cc03be0fd226717518e266 --- /dev/null +++ b/uva_cpp_clean/540/540-18.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main(){ + int t, n, teammate, team, cases = 0; + char option[13]; + while(scanf("%d", &t) && t != 0){ + printf("Scenario #%d\n", cases + 1); + map teams; + queue teamQueue[1010]; + queue teamLeader; + for(int i = 0; i < t; i++){ + scanf("%d", &n); + for(int j = 0; j < n; j++){ + scanf("%d", &teammate); + teams[teammate] = i; + } + } + while(scanf("%s", option) && strcmp(option, "STOP") != 0){ + if(strcmp(option, "ENQUEUE") == 0){ + scanf("%d", &teammate); + team = teams[teammate]; + if(teamQueue[team].empty()) teamLeader.push(team); + teamQueue[team].push(teammate); + }else if(strcmp(option, "DEQUEUE") == 0){ + team = teamLeader.front(); + printf("%d\n", teamQueue[team].front()); + teamQueue[team].pop(); + if(teamQueue[team].empty()) teamLeader.pop(); + } + } + cases++; + printf("\n"); + } + return 0; +} +/* + La estrategia es usar 2 queue y un map, una queue que represente las primeras personas de teams diferentes que + llegaron, ellos serian los lideres y la otra para representar la queue de ese equipo (esta seria un vector de queue) + y el map para saber a que team pertenece cada persona. Tarde mucho para llegar a esta solucion ya que la primera + solucion trabajaba con un vector y una queue que practicamente realizaba lo mismo, ya que con el vector simulaba + una queue sin darme cuenta, demore tratando de optimizarlo pero tuve que cambiarlo debido a que UVA mandaba RTE. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/540/540-19.cpp b/uva_cpp_clean/540/540-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..235fb9a8f35e0260dc1e12b65bef353beb7653f2 --- /dev/null +++ b/uva_cpp_clean/540/540-19.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + queue teams[1001]; + unordered_map members; + queue q; + int t, c = 1; + while (cin >> t && t) { + queue teams[1001]; + queue q; + members.clear(); + for (int i = 0; i < t; i++) { + int n; + cin >> n; + for (int j = 0; j < n; j++) { + int var; + cin >> var; + members[var] = i; + } + } + printf("Scenario #%d\n", c++); + string cmd; + while (cin >> cmd) { + if (cmd == "STOP") break; + if (cmd == "DEQUEUE") { + int team = q.front(); + printf("%d\n", teams[team].front()); + teams[team].pop(); + if (teams[team].size() == 0) q.pop(); + } else if (cmd == "ENQUEUE") { + int var; + cin >> var; + int team = members[var]; + teams[team].push(var); + if (teams[team].size() == 1) q.push(team); + } + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/540/540-21.cpp b/uva_cpp_clean/540/540-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe5849324ee4abc70deb0572b2ca4cd05b51327f --- /dev/null +++ b/uva_cpp_clean/540/540-21.cpp @@ -0,0 +1,96 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 540 + Name: Team Queue + Problem: https://onlinejudge.org/external/5/540.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if(p == end) { + if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF; + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readInt() { + char ch; + int sgn=1, r=0; + while (!isdigit(ch=readchar()) && ch!='-'); + if (ch == '-') sgn = -1; + else r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + return sgn*r; +} + +inline char readCh() { + char ch, ch2; + while ((ch=readchar())==' ' || ch=='\n'); + while ((ch2=readchar())!=' ' && ch2!='\n'); + return ch; +} + +// --------------------------------------------------------------- + +class FastQueue { + int data[1024], l, r; + public: + FastQueue():l(0),r(0) {} + void clear() { l=r=0; } + bool empty() { return l==r; } + void push(int x) { data[r++] = x; r&=1023; } + int pop(){ int result = data[l++]; l&=1023; return result; } + int front() { return data[l]; } +}; + + +int eteam[1000000]; +FastQueue tqu[1000], mqu; + +int main() { + ios_base::sync_with_stdio(0); + + char cm; + for (int cse=1, n; (n=readInt()); ++cse) { + cout << "Scenario #" << cse << endl; + + for (int i=0; i +/* +Problem: 541 - Error Correction +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=482 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +int N = 0; +bool matrix[100][100]; +int bit_i = 0, bit_j = 0; +int odd_col_count, odd_row_count, counter; + +int main () { + + ios_base::sync_with_stdio(0); + cin.tie(0); + + while ( cin >> N && N != 0 ){ + odd_row_count = 0; + odd_col_count = 0; + bit_i = 0; + bit_j = 0; + + // taking input.. + for (int i = 0; i < N; i++){ + counter = 0; + for (int j = 0; j < N; j++){ + cin >> matrix[i][j]; + counter += matrix[i][j]; + } + if (counter%2 != 0) {odd_row_count++; bit_i = i+1;} + } + + // processing.. + for (int i = 0; i < N; i++){ + counter = 0; + for (int j = 0; j < N; j++){ + counter += matrix[j][i]; + } + if (counter%2 != 0) {odd_col_count++; bit_j = i+1;} + } + + if (odd_col_count>1 || odd_row_count>1) printf("Corrupt\n"); + else if (odd_col_count == 0 && odd_row_count == 0) printf("OK\n"); + else printf("Change bit (%d,%d)\n", bit_i, bit_j); + } + + return 0; +} +/* +input:- +----------- +4 +1 0 1 0 +0 0 0 0 +1 1 1 1 +0 1 0 1 +4 +1 0 1 0 +0 0 1 0 +1 1 1 1 +0 1 0 1 +4 +1 0 1 0 +0 1 1 0 +1 1 1 1 +0 1 0 1 +0 + +output:- +----------- +OK +Change bit (2,3) +Corrupt + +Resources:- +------------- + +*/ diff --git a/uva_cpp_clean/541/541-14.cpp b/uva_cpp_clean/541/541-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e21216225d64a895f0ae4231cddc5e78fb833b12 --- /dev/null +++ b/uva_cpp_clean/541/541-14.cpp @@ -0,0 +1,69 @@ +/*************************************************** + * Problem name : 541 Error Correction.cpp + * OJ : Uva + * Result : AC + * Date : 03-05-2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define MAX 102 +using namespace std; +typedef long long ll; +int main () { + int n; + int ar[MAX][MAX]; + while(scanf("%d", &n) == 1 && n!=0){ + for(int i = 0; i +using namespace std; + +int main() { + int n; + int sums[100]; + while (scanf("%d", &n) && n != 0) { + int row = -1; + bool corrupt = false; + for (int i = 0; i < n; i++) sums[i] = 0; + for (int i = 0; i < n; i++) { + int sum = 0; + for (int j = 0; j < n; j++) { + int bit; + scanf("%d", &bit); + if (bit) sums[j]++, sum++; + } + if (sum % 2 == 1){ + if (row == -1) row = i; + else corrupt = true; + } + } + int col = -1; + for (int i = 0; i < n && !corrupt; i++) { + if (sums[i] % 2 == 1) { + if (col == -1) col = i; + else corrupt = true; + } + } + if (corrupt) printf("Corrupt\n"); + else if (row == -1 && col == -1) printf("OK\n"); + else { + if (row == -1 || col == -1) printf("Corrupt\n"); + else printf("Change bit (%d,%d)\n", row + 1 , col + 1); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/541/541-20.cpp b/uva_cpp_clean/541/541-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21e84b1870fe511b0611489466bcf0eb63bc72bf --- /dev/null +++ b/uva_cpp_clean/541/541-20.cpp @@ -0,0 +1,38 @@ +#include +#include + +const int S = 102; +int n, matrix[S][S], rowSum[S], columnSum[S]; +int rowFlag, columnFlag, rowFix, columnFix; + +int main() +{ + while( scanf("%d", &n)==1 && n ) + { + memset(rowSum, 0, sizeof(rowSum)); + memset(columnSum, 0, sizeof(columnSum)); + + for(int i=0 ; i1 || columnFlag>1 ) printf("Corrupt\n"); + else if( rowFlag==1 && columnFlag==1 ) printf("Change bit (%d,%d)\n", rowFix, columnFix); + else printf("OK\n"); + } + return 0; +} diff --git a/uva_cpp_clean/541/541-5.cpp b/uva_cpp_clean/541/541-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a4ad4d7356936ee0e0266a46528dc896aa1e4981 --- /dev/null +++ b/uva_cpp_clean/541/541-5.cpp @@ -0,0 +1,104 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main() +{ + int n; + while(sc("%d",&n)) + { + if(n==0) break; + char a[110][110],row[110]={0},cal[110]={0}; + int p=-1,q=-1,flag=0,i,j,f=0,cl,ro; + for(i=0;i1||cl>1||ro+cl==1) + pf("Corrupt\n"); + else if(ro && cl) + pf("Change bit (%d,%d)\n",p+1,q+1); + else + pf("OK\n"); + + } + + return 0; +} diff --git a/uva_cpp_clean/541/541-9.cpp b/uva_cpp_clean/541/541-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7afcbfa29674040dacedc46256f54fef59ccc5e6 --- /dev/null +++ b/uva_cpp_clean/541/541-9.cpp @@ -0,0 +1,31 @@ +#include + +#define SIZE 110 + +using namespace std; + +int n, v[SIZE][SIZE], row[SIZE], column[SIZE], ct; +pair ans; +bool corrupt; + +int main(){ + while(scanf("%d", &n), n){ + memset(row, 0, sizeof row); + memset(column, 0, sizeof column); + ct = 0, corrupt = false; + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) scanf("%d", &v[i][j]); + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) row[i] ^= v[i][j]; + for(int j = 1; j <= n; j++) + for(int i = 1; i <= n; i++) column[j] ^= v[i][j]; + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) + if(row[i] != column[j]) corrupt = true; + else if(row[i] * column[j]) ans = make_pair(i, j), ct++; + if(ct == 1) printf("Change bit (%d,%d)\n", ans.first, ans.second); + else if(ct >= 2 || corrupt) puts("Corrupt"); + else puts("OK"); + } + return(0); +} diff --git a/uva_cpp_clean/542/542-19.cpp b/uva_cpp_clean/542/542-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..482bc82f633ae598c39a6dcae9738ae5b88db267 --- /dev/null +++ b/uva_cpp_clean/542/542-19.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + string teams[16]; + double grid[16][16], prob[16][4]; + for (int i = 0; i < 16; i++) cin >> teams[i]; + for (int i = 0; i < 16; i++) + for (int j = 0; j < 16; j++) + cin >> grid[i][j], grid[i][j]/=100; + for (int i = 0; i < 16; i++) { + prob[i][0] = grid[i][i%2?i-1:i+1]; + } + for (int k = 2, l = 1; k <= 8; k *= 2, l++) { + for (int i = 0; i < 16; i++) { + double sum = 0; + for (int j = i - i%k + (i%(k*2)>k-1? -k: k), cont = 0; cont < k; cont++, j++) sum += grid[i][j]*prob[j][l-1]; + prob[i][l] = prob[i][l-1]*(sum); + } + } + for (int i = 0; i < 16; i++) { + printf("%-10s p=%.2f%%\n", teams[i].c_str(), prob[i][3]*100); + } + return 0; +} diff --git a/uva_cpp_clean/543/543-13.cpp b/uva_cpp_clean/543/543-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9966d0d06cd6f6d1b4e28d398d4afa7aeebb4d50 --- /dev/null +++ b/uva_cpp_clean/543/543-13.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int n; + +bool isPrime(int x) +{ + for (int i = 2; i * i <= x; ++i) + { + if (x % i == 0) return false; + } + + return true; +} + +int main() +{ + while (cin >> n, n != 0) + { + bool ok = false; + + for (int i = 2; i <= n; ++i) + { + if (isPrime(i) && isPrime(n - i)) + { + cout << n << " = " << i << " + " << n - i << endl; + + ok = true; + + break; + } + } + + if (!ok) + { + cout << "Goldbach's conjecture is wrong." << endl; + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/543/543-19.cpp b/uva_cpp_clean/543/543-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70fdc40bfa2614bd9f30dba08b8bfeee210ce253 --- /dev/null +++ b/uva_cpp_clean/543/543-19.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; + +ll sieve_size; +bitset<100000001> bs; +set primes; + + +inline void sieve(ll upperbound = 1000002) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.insert((int)i); + } +} + +int main() { + sieve(); + int n; + while (cin >> n && n != 0) { + int b; + for (auto it = primes.begin(); it != primes.end(); ++it) { + b = n - (*it); + if (primes.count(b)) break; + } + printf("%d = %d + %d\n", n, n - b, b); + } + return 0; +} diff --git a/uva_cpp_clean/543/543-20.cpp b/uva_cpp_clean/543/543-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f729580b9ea4a6730475b7ebe8eb9cf8c32a430 --- /dev/null +++ b/uva_cpp_clean/543/543-20.cpp @@ -0,0 +1,33 @@ +#include +#include + +using namespace std; + +bool prime[1000005]; + +int main() +{ + prime[0] = prime[1] = false; + for(int i=2 ; i<1000005 ; i++) prime[i] = true; + + for(int i=2 ; i*i<1000005 ; i++) + if( prime[i] ) + for(int j=i+i ; j<1000005 ; j+=i) prime[j] = false; + + int n; + + while( cin >> n && n ) + { + bool flag = true; + for(int i=2 ; i<=n ; i++) + if( prime[i] && prime[n-i] ) + { + cout << n << " = " << i << " + " << n-i << endl; + flag = false; + break; + } + + if( flag ) cout << "Goldbach's conjecture is wrong." << endl; + } + return 0; +} diff --git a/uva_cpp_clean/543/543-5.cpp b/uva_cpp_clean/543/543-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..55c94ac828b3c7adc2460f58a9aa736118c69b34 --- /dev/null +++ b/uva_cpp_clean/543/543-5.cpp @@ -0,0 +1,85 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define st(s) s.size(); +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +using namespace std; + +bool prime[10000010]; +void sieve() +{ + cover(prime,true); + prime[1]=false; + int n=sqrt(10000000); + for(int i=2;i<=n;i++) + { + if(prime[i]!=false) + { + for(int j=2*i;j<=10000000;j=j+i) + { + prime[j]=false; + } + } + } +} +int main() +{ + sieve(); + int n; + while(sc("%d",&n)==1) + { + if(n==0) break; + int a,i; + for(i=2;i<1000000;i++) + { + if(prime[i]) + { + a=n-i; + if(prime[a]) + break; + } + } + pf("%d = %d + %d\n",n,i,a); + + } + return 0; +} diff --git a/uva_cpp_clean/543/543-8.cpp b/uva_cpp_clean/543/543-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..812dc63562f622aa4670a221f16c736fc5580229 --- /dev/null +++ b/uva_cpp_clean/543/543-8.cpp @@ -0,0 +1,34 @@ +// Problem name: Goldbach's Conjecture +// Problem link: https://vjudge.net/problem/UVA-543 +// Submission link: https://vjudge.net/solution/32914399 + +#include + +using namespace std; + +vector get_primes(int n){ + vector small_prime(n + 1), primes; + for(int i = 2 ; i <= n ; ++i){ + if(small_prime[ i ] == 0) + small_prime[ i ] = i, primes.push_back(i); + for(int j = 0 ; j < primes.size() && primes[ j ] <= small_prime[ i ] && i * primes[ j ] <= n ; ++j) + small_prime[ i * primes[ j ] ] = primes[ j ]; + } + return small_prime; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int N = 1000000; + const vector small_prime = get_primes(N); + for(int n ; cin >> n && n ;){ + for(int i = 2, j = n - 2 ; ; ++i, --j){ + if(small_prime[ i ] == i && small_prime[ j ] == j){ + cout << n << " = " << i << " + " << j << endl; + break; + } + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/543/543-9.cpp b/uva_cpp_clean/543/543-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdf3b05f6ebd567e718e0d58f87bee325a3e56db --- /dev/null +++ b/uva_cpp_clean/543/543-9.cpp @@ -0,0 +1,40 @@ +#include + +const long long LIM = (int)1e6; + +using namespace std; + +bool prime[LIM+1]; +vectorp; +vectorgoldbach(LIM+1); +int n; + +void goldbach_Conjeture(){ + for(int i=6;i<=LIM;i+=2) + for(int j=0;j<90;j++) + if(!prime[i-p[j]]){ + goldbach[i] = p[j]; + break; + } +} + +void sieve(){ + prime[0] = prime[1] = 1; + for(int i=4;i<=LIM;i+=2) + prime[i] = 1; + for(int i=3;i*i<=LIM;i+=2){ + if(!prime[i]){ + for(int j=i*i;j<=LIM;j+=2*i) + prime[j] = 1; + p.push_back(i); + } + } +} + +int main(){ + sieve(); + goldbach_Conjeture(); + while(scanf("%d",&n),n) + printf("%d = %d + %d\n",n,goldbach[n],n-goldbach[n]); + return(0); +} diff --git a/uva_cpp_clean/544/544-14.cpp b/uva_cpp_clean/544/544-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3055b0bee94db079832492ab9f8e5f7b0275082d --- /dev/null +++ b/uva_cpp_clean/544/544-14.cpp @@ -0,0 +1,143 @@ +/*************************************************** + * Problem name : 544 Heavy Cargo.cpp + * Problem Link : https://uva.onlinejudge.org/external/5/544.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-10-19 + * Problem Type : Graph(MST) + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,false,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 205 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +struct Edge { + int u, v, w; + bool operator<(const Edge &p) { + return w > p.w; + } +}; +vectorE; +vectoradj[MAX], cost[MAX]; +mapmp; +int par[MAX],M[MAX]; +bool visit[MAX]; +int find(int r) { + if (par[r] == r) return r; + else return par[r] = find(par[r]); +} +void MST(int n) { + sort(E.begin(), E.end()); + for (int i = 1; i <= n; i++) { + par[i] = i; + } + int sz = E.size(), s = 0; + for (int i = 0; i < sz; i++) { + int u = find(E[i].u); + int v = find(E[i].v); + if (u != v) { + par[u] = v; + adj[E[i].u].push_back(E[i].v); + adj[E[i].v].push_back(E[i].u); + cost[E[i].u].push_back(E[i].w); + cost[E[i].v].push_back(E[i].w); + //~ printf("%d %d %d\n", E[i].u , E[i].v, E[i].w); + s++; + if (s == n) return; + } + } +} +int bfs(int s, int d) { + queueQ; + Q.push(s); + visit[s] = true; + M[s] = 1000000; + while (!Q.empty()) { + int u = Q.front(); + Q.pop(); + for (int i = 0; i < (int)adj[u].size(); i++) { + int v = adj[u][i]; + int w = cost[u][i]; + if (visit[v] == false) { + visit[v] = true; + Q.push(v); + M[v] = min(w, M[u]); + } + } + } + return M[d]; +} +int main () { + int n, r, t = 1; + while (scanf("%d %d", &n, &r) == 2 && n != 0 && r != 0) { + int k = 1; + for (int i = 0; i < r; i++) { + getchar(); + string u, v; + int w; + cin >> u >> v >> w; + if (mp[u] == 0) mp[u] = k++; + if (mp[v] == 0) mp[v] = k++; + Edge get; + get.u = mp[u], get.v = mp[v] , get.w = w; + //~ printf("%d %d %d\n", get.u, get.v, get.w); + E.push_back(get); + } + MST(n); + getchar(); + string s, d; + cin >> s >> d; + SET(visit); + for(int i = 0; i +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair road; +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +priority_queue pq; +unordered_map map; + +int main() { + int m, n, cases = 1; + while (cin >> m >> n && !(m == 0 && n == 0)) { + map.clear(); + printf("Scenario #%d\n", cases++); + for (int i = 0; i < n; i++) { + string k1, k2; + int weight; + cin >> k1 >> k2 >> weight; + if (!map.count(k1)) map.insert(make_pair(k1, map.size())); + if (!map.count(k2)) map.insert(make_pair(k2, map.size())); + road l; l.second.first = map[k1]; l.second.second = map[k2]; + l.first = weight; + pq.push(l); + } + string c1, c2; + cin >> c1 >> c2; + int i1 = map[c1], i2 = map[c2]; + ufds u(m); + int w = 100000; + while (!pq.empty()) { + if (!u.isSameSet(pq.top().second.first, pq.top().second.second) && !(u.isSameSet(i1, i2))) { + w = min(w, pq.top().first); + u.unionSet(pq.top().second.first, pq.top().second.second); + } + pq.pop(); + } + printf("%d tons\n", w); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/544/544-20.cpp b/uva_cpp_clean/544/544-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13bff4cca08add7aaf0e7fda781cb3d1cb5e399c --- /dev/null +++ b/uva_cpp_clean/544/544-20.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX 9951 +#define Min(a, b) ((ab)?(a):(b)) + +int wght[MAX][MAX]; + +int main() +{ + int n, r, nCase = 1; + while( scanf("%d %d", &n, &r)==2 && (n || r) ) + { + map city; + + for(int i=0 ; i> str1 >> str2 >> weightLimit; + + if( !city[str1] ) city[str1] = index, index++; + if( !city[str2] ) city[str2] = index, index++; + + wght[city[str1]][city[str2]] = wght[city[str2]][city[str1]] = weightLimit; + } + + for(int k=1 ; k<=n ; k++) + for(int i=1 ; i<=n ; i++) + for(int j=1 ; j<=n ; j++) + wght[i][j] = Max(wght[i][j], Min(wght[i][k], wght[k][j])); + + string start, destination; + cin >> start >> destination; + + printf("Scenario #%d\n%d tons\n\n", nCase, wght[city[start]][city[destination]]); + nCase++; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/544/544-21.cpp b/uva_cpp_clean/544/544-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f69e53de09d8dd4a5f802ff495a4b7110bb232b --- /dev/null +++ b/uva_cpp_clean/544/544-21.cpp @@ -0,0 +1,119 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 544 + Name: Heavy Cargo + Problem: https://onlinejudge.org/external/5/544.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define MAXX 1073741824 + +int did[202]; +int find(int x) { + if (did[x] == x) return x; + return did[x] = find(did[x]); +} +void join(int u, int v) { + did[find(u)] = find(v); +} + +// ----------------------------------------------- + + +int cnt; +map ids; +int getId(char *w) { + int res = ids[w]; + if (!res) res = ids[w] = ++cnt; + return res; +} + +int readId() { + char w[100]; + cin>>w; + return getId(w); +} + +// ---------------------------------------------------- + +struct Edge { + int u, v, d; + Edge(int u, int v, int d): u(u), v(v), d(d) {} + bool operator < (const Edge& e) const { + return d < e.d; + } +}; + + +list adj[202]; +int dist[202][202]; +int seen[202]; +int dfs(int u, int v) { + if (u == v) return 20000; + if (seen[u]) return -1; + seen[u] = 1; + + int mn = MAXX; + for (int i: adj[u]) { + int r = dfs(i, v); + if (r>=0) + mn = min(mn, min(r, dist[u][i])); + } + + seen[u] = 0; + return mn == MAXX ? -1 : mn; +} + + +int main(){ + int cse=1, u, v, d, n, m; + char w[100]; + while (cin>>n>>m && (n||m)) { + for (int i=0; i<=n; i++) { + adj[i].clear(); + did[i]=i; + } + + priority_queue q; + ids.clear(); + cnt=0; + + for (int i=0; i> d; + q.push(Edge(u, v, d)); + } + u = readId(); + v = readId(); + + + + int nn = 0; + while (nn < n-1 && !q.empty()) { + Edge e = q.top(); q.pop(); + if (find(e.u) != find(e.v)) { + nn++; + join(e.u, e.v); + adj[e.u].push_back(e.v); + adj[e.v].push_back(e.u); + dist[e.u][e.v] = dist[e.v][e.u] = e.d; + } + } + + printf("Scenario #%d\n%d tons\n\n", cse++, dfs(u, v)); + } +} diff --git a/uva_cpp_clean/544/544-5.cpp b/uva_cpp_clean/544/544-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8f5f34350ce9de60bb99290a12f00b015fc8f6eb --- /dev/null +++ b/uva_cpp_clean/544/544-5.cpp @@ -0,0 +1,212 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +struct data +{ + int node,cost; + bool operator < ( const data& p ) const { //overloading operator + return cost < p.cost; + } +}; + +int main() +{ + int n,e,no=0; + while(sii(n,e)==2) + { + if(!n && !e) break; + + vectorg[n+10]; + + msi mp; + int k=1; + string s1,s2; + for0(i,e) + { + int cost; + cin>>s1>>s2>>cost; + if(!mp[s1]) + mp[s1]=k++; + if(!mp[s2]) + mp[s2]=k++; + + data p; + p.node=mp[s2]; + p.cost=cost; + g[mp[s1]].pb(p); + p.node=mp[s1]; + g[mp[s2]].pb(p); + } + cin>>s1>>s2; + int src=mp[s1],dis=mp[s2],vis[205]={0},res=inf; + + priority_queuepq; + + for0(i,sz(g[src])) + { + pq.push(g[src][i]); + } + vis[src]=1; + while(!pq.empty()) + { + data p=pq.top(); + pq.pop(); + + vis[p.node]=1; + res=min(res,p.cost); + +// P(p.cost); + + if(p.node==dis) + break; + + for0(i,sz(g[p.node])) + { + if(!vis[g[p.node][i].node]) + pq.push(g[p.node][i]); + } + } + printf("Scenario #%d\n%d tons\n\n",++no,res); + } + + return 0; +} + diff --git a/uva_cpp_clean/544/544-9.cpp b/uva_cpp_clean/544/544-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80d3e7d1207d129384bd19739ad62171aa7fa5db --- /dev/null +++ b/uva_cpp_clean/544/544-9.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +struct UnionFind{ + vector path, rank; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +const int SIZE = 20000, LIM = 210, LEN = 35; + +typedef pair ii; + +struct Edge_{ + int u, v, w; + Edge_() {}; + Edge_(int u_, int v_, int w_): + u(u_), v(v_), w(w_) {}; + bool operator < (const Edge_& other) const{ + return ( w > other.w ); + } +}E[SIZE]; + +int tc, n, m, ct; +vector < ii > AdjList[LIM]; +bool vis[LIM]; +char word1[LEN], word2[LEN]; +map < string, int > mp; + +void Kruskal(){ + UnionFind UF(n); + sort(E, E + m); + int setSize = 1; + for(int it = 0; it < m and setSize != n; it++) + if( not UF.isSameSet(E[it].u, E[it].v) ){ + AdjList[E[it].u].push_back( ii(E[it].v, E[it].w) ); + AdjList[E[it].v].push_back( ii(E[it].u, E[it].w) ); + setSize++; + UF.unionSet(E[it].u, E[it].v); + } +} + +int dfs(int u, int w = INT_MAX){ + vis[u] = true; + if( u == mp[word2] ) return w; + int ans = INT_MAX; + for(int it = 0; it < AdjList[u].size(); it++){ + ii v = AdjList[u][it]; + if( not vis[v.first] ) + ans = min(ans, dfs(v.first, min(w, v.second ) ) ); + } + return ans; +} + +void clear_(){ + fill(vis, vis + n, false); + for(int it = 0; it < n; it++) + AdjList[it].clear(); + mp.clear(); + ct = 0; +} + +int main(){ + while(scanf("%d %d\n", &n, &m), n | m){ + for(int it = 0; it < m; it++){ + scanf("%s %s %d\n", word1, word2, &E[it].w); + if( mp.find(word1) == mp.end() ) mp[word1] = ct++; + if( mp.find(word2) == mp.end() ) mp[word2] = ct++; + E[it].u = mp[word1], E[it].v = mp[word2]; + } + scanf("%s %s", word1, word2); + Kruskal(); + printf("Scenario #%d\n", ++tc); + printf("%d tons\n\n", dfs(mp[word1])); + clear_(); + } + return(0); +} diff --git a/uva_cpp_clean/545/545-21.cpp b/uva_cpp_clean/545/545-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23fa115f593c0a6d5dff8a78965359e6b44f1de2 --- /dev/null +++ b/uva_cpp_clean/545/545-21.cpp @@ -0,0 +1,30 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 545 + Name: Heads + Problem: https://onlinejudge.org/external/5/545.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include + +using namespace std; + +int main(){ + register int n,nn,t2; + register double t; + scanf("%d",&nn); + while(nn--){ + scanf("%d",&n); + t=n*0.30102999566398; + t2=t; + printf("2^-%d = %.3fE-%d\n",n,pow(10.0,t2-t+1),t2+1); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/547/547-6.cpp b/uva_cpp_clean/547/547-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4abae70376985f82e3563ef826f92757c31c0541 --- /dev/null +++ b/uva_cpp_clean/547/547-6.cpp @@ -0,0 +1,62 @@ +/* + math > number theory + difficulty: medium + date: 15/Jun/2020 + problem: find the longest DDF sequence + hint: pre-process a array f, where f[i] is the sum of digits of all positive factors of i; think like sieve + by: @brpapa +*/ +#include +using namespace std; +#define MAX 3030 + +int f[MAX]; + +int sumDigits(int n) { + int sum = 0; + while (n > 0) sum += n%10, n /= 10; + return sum; +} + +int memo[MAX]; // memo[n] = tamanho da sequencia que inicia com n +int dp(int n) { + int &ans = memo[n]; + if (ans != -1) return ans; + + if (n == f[n]) return 1; + return ans = 1 + dp(f[n]); +} + +int main() { + for (int i = 0; i <= MAX; i++) f[i] = 1; + for (int i = 2; i <= MAX; i++) { + int sum = sumDigits(i); + for (int j = i; j <= MAX; j += i) f[j] += sum; + } + + memset(memo, -1, sizeof memo); + + int m, n; int t = 1; + while (cin >> m >> n) { + int lo = min(m, n); + int hi = max(m, n); + + int maxLen = 0; + int start = hi; + + for (int i = hi; i >= lo; i--) + if (dp(i) >= maxLen) { + maxLen = dp(i); + start = i; + } + + printf("Input%d: %d %d\n", t, m, n); + printf("Output%d: ", t++); + for (int i = start; true; i = f[i]) { + if (i == f[i]) { cout << i << endl; break; } + cout << i << " "; + } + } + + return 0; +} diff --git a/uva_cpp_clean/548/548-17.cpp b/uva_cpp_clean/548/548-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2c7c588e2011af8614a52b857bfb37118af3083 --- /dev/null +++ b/uva_cpp_clean/548/548-17.cpp @@ -0,0 +1,60 @@ +/** + * AOAPC II Example 6-8 Tree + * Array based. + */ +#include +using namespace std; +vector in, post, L, R; +int sum, sum_pos; +int build(int in_beg, int in_end, int post_beg, int post_end) +{ + if (in_beg >= in_end) + return 0; + int root = post[post_end - 1]; + int pos = in_beg; + while (in[pos] != root) + ++pos; + int left_size = pos - in_beg; + L[root] = build(in_beg, pos, post_beg, post_beg + left_size); + R[root] = build(pos + 1, in_end, post_beg + left_size, post_end - 1); + return root; +} +bool read(vector &tar) +{ + tar.clear(); + string line; + if (!getline(cin, line)) + return false; + stringstream ss(line); + int i; + while (ss >> i) + tar.push_back(i); + return tar.size(); +} +void dfs(int u, int cur_sum) +{ + cur_sum += u; + if (!L[u] && !R[u] && (cur_sum < sum || cur_sum == sum && u < sum_pos)) + { + sum = cur_sum; + sum_pos = u; + return; + } + if (L[u]) + dfs(L[u], cur_sum); + if (R[u]) + dfs(R[u], cur_sum); +} +int main() +{ + while (read(in)) + { + read(post); + L = vector(10000 + 10, 0); + R = vector(10000 + 10, 0); + int n = in.size(), root = build(0, n, 0, n); + sum = numeric_limits::max(); + dfs(root, 0); + cout << sum_pos << "\n"; + } +} diff --git a/uva_cpp_clean/548/548-7.cpp b/uva_cpp_clean/548/548-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..682f9497fe35afae104f666bb9c4e9529f690b52 --- /dev/null +++ b/uva_cpp_clean/548/548-7.cpp @@ -0,0 +1,155 @@ +/** + * Created by LonelyEnvoy on 2017-3-11. + * Tree + * Keywords: 二叉树 + * + */ + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct Node { + int data; + Node* leftChild; + Node* rightChild; + + Node(int _data = 0, Node* _leftChild = NULL, Node* _rightChild = NULL) + : data(_data), leftChild(_leftChild), rightChild(_rightChild) {} +}; + +void createBinTree(int* inOrderList, int* postOrderList, int n, Node*& root) { + // create finished + if (n == 0) { + return; + } + + // find the root node in inOrderList + int rootPos; + for (int i = 0; i < n; i++) { + if (inOrderList[i] == postOrderList[n - 1]) { + rootPos = i; + } + } + + // create root node + root = new Node(inOrderList[rootPos]); + + // create left subTree + createBinTree(inOrderList, postOrderList, rootPos, root->leftChild); + // create right subTree + createBinTree(inOrderList + rootPos + 1, postOrderList + rootPos, n - rootPos - 1, root->rightChild); +} + +void printBinTree(Node* root) { + if (root == NULL) { + return; + } + cout << root->data << " "; + printBinTree(root->leftChild); + printBinTree(root->rightChild); +} + +int findMinWeightLeaf(Node* root) { + stack visitStack; + visitStack.push(root); + + Node* node; + deque traceQueue; + deque minTraceQueue; + int minWeight = numeric_limits::max(); + // traverse preorder + while (!visitStack.empty()) { + node = visitStack.top(); + visitStack.pop(); + traceQueue.push_back(node); + // visit + if (node->leftChild == NULL && node->rightChild == NULL) { + // calculate sum weight + int sumWeight = 0; + for (deque::iterator itr = traceQueue.begin(); itr != traceQueue.end(); ++itr) { + sumWeight += (*itr)->data; + } + // if found a less weighted leaf node, update the min queue + if (sumWeight < minWeight + || (sumWeight == minWeight && traceQueue.back()->data < minTraceQueue.back()->data)) { + minWeight = sumWeight; + minTraceQueue = traceQueue; + } + traceQueue.pop_back(); // backtrack + } + if (node->rightChild) { + visitStack.push(node->rightChild); + } + if (node->leftChild) { + visitStack.push(node->leftChild); + } + } + + return minTraceQueue.back()->data; +} + +void splitString(const string& s, char delim, vector& strings) { + size_t searchPos = 0; + size_t length = s.length(); + while (searchPos < length) { + size_t index = s.find_first_of(delim, searchPos); + if (index == string::npos) { + strings.push_back(s.substr(searchPos, length - searchPos)); + break; + } + strings.push_back(s.substr(searchPos, index - searchPos)); + searchPos = index + 1; + } +} + +int main() { + Node* root; + + string line; + vector strings; + int* values[2]; + istringstream stream; + int numberOfInputs; + while (true) { + // read the inOrderList and postOrderList + for (int i = 0; i < 2; i++) { + // read a line + getline(cin, line); + if (cin.eof()) { + goto exit; + } + + strings.clear(); + + // parse the line + splitString(line, ' ', strings); + numberOfInputs = strings.size(); + values[i] = new int[numberOfInputs]; + for (int j = 0; j < numberOfInputs; j++) { + stream.clear(); + stream.str(strings[j]); + stream >> values[i][j]; + } + } + + // create tree + createBinTree(values[0], values[1], numberOfInputs, root); + + // find the min weight leaf + cout << findMinWeightLeaf(root) << endl; + + // free memory + for (int i = 0; i < 2; i++) { + delete[] values[i]; + } + } + exit: + return 0; +} diff --git a/uva_cpp_clean/554/554-19.cpp b/uva_cpp_clean/554/554-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c64c6562a4b0802542b76e4ec96ccc9c043e3477 --- /dev/null +++ b/uva_cpp_clean/554/554-19.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + string s; + unordered_set dict; + while (cin >> s && s != "#") dict.insert(s); + getline(cin, s); + getline(cin, s); + stringstream ss; + int max = -1; + string msg; + for (int i = 0; i < 27; i++) { + for (int j = 0; j < s.length(); j++) s[j] = s[j] == ' '? 'A': s[j] == 'Z'? ' ': s[j] + 1; + int match = 0; + for (int j = 0; j < s.length(); j++) { + if(s[j] == ' ') { + if (dict.count(ss.str())) match++; + ss.str(""); + } else ss << s[j]; + } + if (dict.count(ss.str())) match++; + ss.str(""); + if (match > max) msg = s, max = match; + } + int count = 0; + string esp = ""; + for (int j = 0; j < msg.length(); j++) { + if(msg[j] == ' ') { + if (count + ss.str().length() + esp.length() > 60) cout << endl, esp = "", count = 0; + cout << esp << ss.str(); + count += ss.str().length() + esp.length(); + esp = " "; + ss.str(""); + } else ss << msg[j]; + } + cout << endl; + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/554/main.cpp b/uva_cpp_clean/554/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbdc48a7ef0b9078188671becd2ba6f87a3f55ac --- /dev/null +++ b/uva_cpp_clean/554/main.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +using namespace std; + +string translate(string s, int k){ + string res = ""; + int asci, diff; + for(int i = 0; i < s.size(); i++){ + if(s[i] == 32) asci = 91-k; + else{ + asci = (int)s[i] - k; + if(asci < 65){ + diff = 65-asci; + if(diff == 1) asci = 32; + else asci = 92-diff; + } + } + res+=asci; + } + return res; +} + +int main(){ + + string word, text, t_text, best_result, res; + int lenght, counter, best_counter = 0; + vector dictionary; + + while(cin >> word && word != "#"){ + dictionary.push_back(word); + } + cin.ignore(); + getline(cin,text); + + for(int k=1; k<27; k++){ + t_text = translate(text, k); + istringstream iss(t_text); + counter = 0; + while(iss >> word){ + for(int i = 0; i < dictionary.size(); i++){ + if(word == dictionary[i]){ + counter++; + break; + } + } + } + + if(best_counter < counter){ + best_result = t_text; + best_counter = counter; + } + } + istringstream iss(best_result); + counter = 0; + int i = 0; + while(iss >> word){ + + if (counter!=0)counter += word.size()+1; + else counter += word.size(); + + if(counter <= 60){ + if (i!=0)res+= " "; + res+=word; + + }else{ + cout << res << endl; + res = word; + counter = word.size(); + } + + i++; + } + cout << res << endl; + + +return 0; +} diff --git a/uva_cpp_clean/555/555-21.cpp b/uva_cpp_clean/555/555-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8908c8081cf4099c94da9e130563129cd7be85d8 --- /dev/null +++ b/uva_cpp_clean/555/555-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 555 + Name: Bridge Hands + Problem: https://onlinejudge.org/external/5/555.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +char tt[]="CDSH23456789TJQKA"; +char swne[]="SWNE"; +class card{ +public: + char suit; + char num; + bool operator<(const card &t) const{ + char *p1,*p2,*p3,*p4; + p1=strchr(tt,suit); p2=strchr(tt,t.suit); + p3=strchr(tt,num); p4=strchr(tt,t.num); + if(p1 players[4]; + set::iterator j; + class card pt; + while((start=getchar())!='#'){ + getchar(); + for(pi=0;swne[pi]!=start;pi++); + for(i=1;i<=52;i++){ + if(i==27)getchar(); + pt.suit=getchar(); + pt.num=getchar(); + players[(i+pi)%4].insert(pt); + } + for(i=0;i<4;i++){ + printf("%c:",swne[i]); + for(j=players[i].begin();j!=players[i].end();j++) + printf(" %c%c",j->suit,j->num); + printf("\n"); + players[i].clear(); + } + getchar(); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/558/558-13.cpp b/uva_cpp_clean/558/558-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..033bd9caf056ebfe56cdf8767621571765491fc1 --- /dev/null +++ b/uva_cpp_clean/558/558-13.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, m, test; +bool hasNegetiveCycle; + +ofstream fout("f.out"); + +vector dist; +vector > > graph; + +bool bellman_ford() +{ + dist[0] = 0; + + for (int k = 0; k < n - 1; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < graph[i].size(); j++) + { + int head = i; + + int child = graph[i][j].first; + + int weight = graph[i][j].second; + + dist[child] = min(dist[child], dist[head] + weight); + } + } + } + + hasNegetiveCycle = false; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < graph[i].size(); j++) + { + int head = i; + + int child = graph[i][j].first; + + int weight = graph[i][j].second; + + if (dist[head] + weight < dist[child]) + { + hasNegetiveCycle = true; + } + } + } + + return hasNegetiveCycle; +} + +void initialize() +{ + hasNegetiveCycle = false; + + graph.clear(); + graph.resize(n); + + dist.clear(); + dist.resize(n, INT_MAX); +} + +int main() +{ + cin >> test; + + while (test--) + { + cin >> n >> m; + + initialize(); + + for (int i = 0; i < m; i++) + { + int x, y, t; + + cin >> x >> y >> t; + + graph[x].push_back(make_pair(y, t)); + } + + if (bellman_ford()) cout << "possible" << endl; + + else cout << "not possible" << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/558/558-19.cpp b/uva_cpp_clean/558/558-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a7fbb1a47a61f34ec7f3d22824497a1e7695191 --- /dev/null +++ b/uva_cpp_clean/558/558-19.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vi; +typedef vector vii; + +bool bellmanford(vector & AdjList, int V) { + int s = 0; + vi dist(V, 100000000); dist[s] = 0; + for (int i = 0; i < V - 1; i++) { + for (int u = 0; u < V; u++) { + for (int j = 0; j < (int)AdjList[u].size(); j++) { + ii v = AdjList[u][j]; + dist[v.first] = min(dist[v.first], dist[u] + v.second); + } + } + } + bool neg = false; + for (int u = 0; u < V && !neg; u++) { + for (int j = 0; j < (int)AdjList[u].size() && !neg; j++) { + ii v = AdjList[u][j]; + if (dist[u] + v.second < dist[v.first]) neg = true; + } + } + return neg; +} + +int main() { + int c; + cin >> c; + while (c--) { + int n, m; + cin >> n >> m; + vector AdjList(n, vii()); + for (int i = 0; i < m; i++) { + int x, y, t; + cin >> x >> y >> t; + AdjList[x].push_back(make_pair(y, t)); + } + bool neg = bellmanford(AdjList, n); + if (neg) printf("possible\n"); + else printf("not possible\n"); + } + return 0; +} diff --git a/uva_cpp_clean/558/558-20.cpp b/uva_cpp_clean/558/558-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fa6eb8f0838e738b23a0e286724cd6d9f9fc6c8 --- /dev/null +++ b/uva_cpp_clean/558/558-20.cpp @@ -0,0 +1,44 @@ +#include + +#define MAXN 1002 +#define MAXE 2002 +#define INF (((2)<<(30))-(1)) + +int d[MAXN], edge_u[MAXE], edge_v[MAXE], edge_cost[MAXE]; + +int main() +{ + int tc; + scanf("%d", &tc); + while( tc-- ) + { + int node, edge; + scanf("%d %d", &node, &edge); + + for(int i=1 ; i<=node ; i++) d[i] = INF; + + d[1] = 0; + for(int i=1 ; i<=edge ; i++) scanf("%d %d %d", &edge_u[i], &edge_v[i], &edge_cost[i]); + + bool neg_cycle = false; + for(int step=1 ; step<=node ; step++) + { + bool updated = false; + for(int i=1 ; i<=edge ; i++) + { + int u = edge_u[i], v = edge_v[i]; + if( d[u]+edge_cost[i] < d[v] ) + { + updated = true; + if( step==node ) neg_cycle = true; + d[v] = d[u]+edge_cost[i]; + } + } + if( !updated ) break; + } + + if( neg_cycle ) puts("possible"); + else puts("not possible"); + } + return 0; +} diff --git a/uva_cpp_clean/558/558-21.cpp b/uva_cpp_clean/558/558-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99e1b0a550b5b17fe1bdfbab9b225ad54c9cccbf --- /dev/null +++ b/uva_cpp_clean/558/558-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 558 + Name: Wormholes + Problem: https://onlinejudge.org/external/5/558.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define INF 200000000 + +struct Edge { + int x, y, w; +}; +Edge eds[2001]; +int d[1005]; + +bool hasCycle(int n, int m) { + d[0]=0; + for (int i=1; i>T; + while(T--) { + cin>>n>>m; + for (int i=0; i> e.x >> e.y >> e.w; + } + + cout << (hasCycle(n, m) ? "possible\n" : "not possible\n"); + } +} diff --git a/uva_cpp_clean/558/558-9.cpp b/uva_cpp_clean/558/558-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2876be82ac565fbcda62de4159624b06e45e79b --- /dev/null +++ b/uva_cpp_clean/558/558-9.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +const int MAX_V = 1010, MAX_E = 2010, INF = 1000000000; + +int tc, V, E, dis[MAX_V], pred[MAX_V], u[MAX_E], v[MAX_E], w[MAX_E]; + +bool NegativeCycle(){ + for(int i = 0; i < E; i++) + if( dis[v[i]] > dis[u[i]] + w[i] ) return true; + return false; +} + +void BellmanFord(int source){ + fill(dis, dis + MAX_V, INF); + dis[source] = 0; + for(int ct = 1; ct < V; ct++) + for(int i = 0; i < E; i++) + if( dis[v[i]] > dis[u[i]] + w[i] ) + dis[v[i]] = dis[u[i]] + w[i]; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &V, &E); + for(int i = 0; i < E; i++) + scanf("%d %d %d", u + i, v + i, w + i); + BellmanFord(0); + puts( NegativeCycle() ? "possible" : "not possible" ); + } + return(0); +} diff --git a/uva_cpp_clean/562/562-10.cpp b/uva_cpp_clean/562/562-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/uva_cpp_clean/562/562-10.cpp differ diff --git a/uva_cpp_clean/562/562-12.cpp b/uva_cpp_clean/562/562-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07b4e63890b959f9da601528e4665e7e45f8ee8d --- /dev/null +++ b/uva_cpp_clean/562/562-12.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) +#define PI(a,b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long +#define S second +#define F first +#define endl '\n' +#define FOR(var,len) for(var = 0;var>n; + int ans[n+1]; + for(i = 1;i<=n;i++){ + cin>>ans[i]; + val+=ans[i]; + } + val1 = val/2; + memset(dp,0,sizeof(dp)); + for(i = 1;i<=n;i++) + for(j = 1;j<=val1;j++){ + if(ans[i]<=j) + dp[i][j] = max(dp[i-1][j],dp[i-1][j-ans[i]]+ans[i]); + else + dp[i][j] = dp[i-1][j]; + } + cout<>tt; + for( t = 0 ;t +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 505 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[MAX]; +int n, need; +int dp[103][25003]; + +int coin_change (int i, int make) { + if (i >= n) { + return make; + } + + if (dp[i][make] != -1) return dp[i][make]; + + int ret1 = 0, ret2 = 0; + + if ( (make + ar[i] <= need) ) { + ret1 = coin_change (i + 1, make + ar[i]); + } + + ret2 = coin_change (i + 1, make); + return dp[i][make] = max (ret1, ret2); +} +int main () { + __FastIO; + int tc; + cin >> tc; + + while (tc--) { + int sum = 0; + cin >> n; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + sum += ar[i]; + } + + need = sum / 2; + SET (dp, -1); + int ans = coin_change (0, 0); + cout << (sum - (2 * ans) ) << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/562/562-19.cpp b/uva_cpp_clean/562/562-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a6526ac03156c49fdaaf43fbafaa7961dc33058 --- /dev/null +++ b/uva_cpp_clean/562/562-19.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef unordered_map uiii; +typedef unordered_map uiv; + +int N, total, mini; +int val[101]; +uiv reg; + +int func(int sum1, int sum2, int p) { + if (2*sum2 - total >= mini) return mini; + if (reg[sum1][p].second != 1) { + if (p < N) reg[sum1][p].first = min(func(min(sum1 + val[p], sum2), max(sum1 + val[p], sum2), p+1), func(sum1, sum2 + val[p], p+1)); + else reg[sum1][p].first = sum2 - sum1; + reg[sum1][p].second = 1; + } + mini = min(reg[sum1][p].first, mini); + return reg[sum1][p].first; +} + +int main() { + int n; + cin >> n; + while (n--) { + reg.clear(); + cin >> N; + total = 0; + for (int i = 0; i < N; i++) cin >> val[i], total += val[i]; + mini = total + 1; + cout << func(0, 0, 0) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/562/562-21.cpp b/uva_cpp_clean/562/562-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..beeb22f42e49c8b4ba7a87ee88f17a5884c91729 --- /dev/null +++ b/uva_cpp_clean/562/562-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 562 + Name: Dividing coins + Problem: https://onlinejudge.org/external/5/562.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int coins[102]; + +int memo[100002][102]; +int rec(int diff, int i) { + if (i<0) + return diff < 0 ? -diff : diff; + + if (memo[50000+diff][i] != -1) + return memo[50000+diff][i]; + + return memo[50000+diff][i] = min( rec(diff+coins[i], i-1), rec(diff-coins[i], i-1) ); +} + +int main(){ + int N; + cin>>N; + while(N--) { + int m; + cin>>m; + for(int i=0; i>coins[i]; + + memset(memo, -1, sizeof(memo)); + cout << rec(0, m-1) << endl; + } +} + diff --git a/uva_cpp_clean/562/562-5.cpp b/uva_cpp_clean/562/562-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..486361bbb4951763f6ca06e19e240514425f92b3 --- /dev/null +++ b/uva_cpp_clean/562/562-5.cpp @@ -0,0 +1,138 @@ +/****************************************************************** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_HIDDEN *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) +#define ull unsigned long long +#define all(v) v.begin(),v.end() + +#define sii(t) scanf("%d",&t) +#define sll(t) scanf("%lld",&t) +#define ssii(a,b) scanf("%d%d",&a,&b) +#define ssll(a,b) scanf("%lld%lld",&a,&b) +#define Case(no) printf("Case %d: ",++no) +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PN(a) printf("%d",a) +#define PLN(a) printf("%lld",a) + +#define ff first +#define se second +#define pb push_back +#define ST(v) sort(all(v)) +#define gcd(a,b) __gcd(a,b) +#define lcm(a,b) (a*(b/gcd(a,b))) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define maxall(v) *max_element(all(v)) +#define minall(v) *min_element(all(v)) +#define cover(a,d) memset(a,d,sizeof(a)) +#define popcount(i) __builtin_popcount(i) //count one +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define un(v) ST(v), (v).earse(unique(all(v)),v.end()) +#define common(a,b) ST(a), ST(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end()) +#define uncommon(a,b) ST(a), ST(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end()) + +////============ CONSTANT ===============//// +#define mx (2000010) +#define inf (1<<30) //infinity value +#define eps 1e-9 +#define mod 1000000007 +////====================================//// + +int main() +{ + int t; + sii(t); + while(t--) + { + int n,p,c; + bool make[10000]; + cover(make,false); + int coin[10000]; + sii(n); + int sum=0; + for(int i=0;i=coin[i];j--) + { + if(make[j-coin[i]]){ + make[j]=true; + } + } + } +// int Min = mod; +// for(int i=0;iabs(v[i]-sum)) +// Min=abs(v[i]-sum); +// } +// P(Min); + for(int i=1;i<=sum;i++) + if(make[i]){ + v[len++]=i; + P(i); + } + sort(v,v+len); + for(int i=0;i + +#define MAX_N 110 +#define MAX_W 50010 + +using namespace std; + +int tc,n,sum,ans,v[MAX_N],dp[MAX_N][MAX_W]; + +int main(){ + scanf("%d",&tc); + while(tc--){ + sum = 0; + scanf("%d",&n); + for(int i=1;i<=n;i++) scanf("%d",&v[i]), sum += v[i]; + ans = INT_MAX; + for(int i=0;i<=n;i++) dp[i][0] = 0; + for(int w=0;w<=sum;w++) dp[0][w] = 0; + for(int i=1;i<=n;i++) + for(int w=1;w<=sum;w++){ + if(v[i]>w) dp[i][w] = dp[i-1][w]; + else dp[i][w] = max(dp[i-1][w],v[i]+dp[i-1][w-v[i]]); + ans = min(ans,abs(sum-2*dp[i][w])); + } + ans = (ans == INT_MAX) ? 0 : ans; + printf("%d\n",ans); + } + return(0); +} diff --git a/uva_cpp_clean/563/563-21.cpp b/uva_cpp_clean/563/563-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d39f302f5429207b9a0e95a5e01dea0e834e646c --- /dev/null +++ b/uva_cpp_clean/563/563-21.cpp @@ -0,0 +1,112 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 563 + Name: Crimewave + Problem: https://onlinejudge.org/external/5/563.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +unsigned int seen[6000], scnt; +bool cap[6000][6000]; +list adj[6000]; +void connect(int u, int v) { + adj[u].push_back(v); + adj[v].push_back(u); + cap[u][v] = 1; + cap[v][u] = 0; +} + +int pre[100000]; +int maxflow(int src, int dest) { + int augments = 0; + bool hasaug = 1; + while (hasaug) { + hasaug = 0; + + queue q; + q.push(src); + seen[src] = ++scnt; + while (!q.empty()) { + int u = q.front(); q.pop(); + if (u == dest) { + for (; u!=src; u=pre[u]) { + cap[pre[u]][u] = 0; + cap[u][pre[u]] = 1; + } + + augments++; + hasaug = 1; + break; + } + + for (int v: adj[u]) + if (seen[v]!=scnt && cap[u][v]) { + seen[v] = scnt; + pre[v] = u; + q.push(v); + } + } + } + return augments; +} + + +bool rob[100][100]; +int nx[]={0,0,1,-1}, + ny[]={1,-1,0,0}; + +int main(){ + int T, a, b, c, u, v; + scanf("%d", &T); + while (T--) { + scanf("%d%d%d", &a, &b, &c); + for (int i=0; i<=a+1; i++) + for (int j=0; j<=b+1; j++) + rob[i][j]=0; + + bool impo = 0; + for (int i=0; i=0; k--) + adj[k].clear(); + + int src=2*a*b+1, dest=2*a*b+2; + for (int i=0; i=0 && i2=0 && j2>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 565 + Name: Pizza Anyone? + Problem: https://onlinejudge.org/external/5/565.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool chosed[16]; +int seen[16][1<<16], cse, + H[17], adj1[16], adj2[16], maxtc, n; +bool dfs(int idx, int sat) { + if (sat == (1< maxtc || seen[idx][sat]==cse) return 0; + seen[idx][sat] = cse; + + chosed[idx] = 0; + if (dfs(idx+1, sat | adj2[idx])) return 1; + + chosed[idx] = 1; + return dfs(idx+1, sat | adj1[idx]); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string str; + for (cse=1; cin >> str; ++cse) { + for (int i=0; i<16; ++i) + adj1[i] = adj2[i] = 0; + + maxtc = 0; + for (n=0; str != "."; ++n) { + for (int j=1; j> str; + } + + H[maxtc+1] = 0; + for (int i=maxtc; i>=0; --i) + H[i] = H[i+1] | adj1[i] | adj2[i]; + + if (!dfs(0, 0)) + cout << "No pizza can satisfy these requests.\n"; + else { + cout << "Toppings: "; + int bb=0; + for (int i=0; i<=maxtc; ++i) + if (chosed[i]) + cout << char('A' + i); + + cout << '\n'; + } + } +} diff --git a/uva_cpp_clean/567/567-14.cpp b/uva_cpp_clean/567/567-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e31933efd2cd07de058e0f016a7cdd6d4b2bba02 --- /dev/null +++ b/uva_cpp_clean/567/567-14.cpp @@ -0,0 +1,82 @@ +/*************************************************** + * Problem name : 567 - Risk(DFS).cpp + * OJ : Uva + * Result : Accepted + * Date : 17-4-17 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 22 +using namespace std; +vectoradj[MAX]; +int cost[MAX],flag; +void dfs(int u) { + if(flag == 0){ + cost[u] = 0; + flag = 1; + } + int sz = adj[u].size(); + for(int i = 0; i +#include +#include +#include +using namespace std; + +typedef vector vi; + +vector AdjList; +bitset<21> visited; + +int proc(int A, int B) { + queue q; + visited.reset(); + q.push(A); + visited.set(A); + while (!q.empty()) { + int d = q.front()/100, node = q.front()%100; + q.pop(); + if (node == B) return d; + for (int i = 0; i < AdjList[node].size(); i++) { + if (!visited[AdjList[node][i]]) q.push((d+1)*100 + AdjList[node][i]), visited.set(AdjList[node][i]); + } + } + return -1; +} + +int main() { + int n, c = 1; + while (cin >> n) { + AdjList.assign(20, vi()); + int v = 0; + do { + for (int i = 0; i < n; i++) { + int auxi; + cin >> auxi; + AdjList[v].push_back(auxi-1); + AdjList[auxi-1].push_back(v); + } + v++; + cin >> n; + } while (v < 19); + printf("Test Set #%d\n", c); + for (int i = 0; i < n; i++) { + int A, B; + cin >> A >> B; + printf("%2d to %2d: %d\n", A, B, proc(A-1, B-1)); + } + printf("\n"); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/567/567-20.cpp b/uva_cpp_clean/567/567-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11eeffbac4a0d93e9706fc641802e4d43d5178ba --- /dev/null +++ b/uva_cpp_clean/567/567-20.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +const int infinity = (2 << 30) - 1; +const int MAX = 22; +vector graph[MAX]; +int d[MAX]; + +int bfs( int source, int destination ) { + for( int i = 0 ; i < MAX ; i++ ) { + d[i] = infinity; + } + queue q; + q.push(source), d[source] = 0; + while( !q.empty() ) { + int u = q.front(); + q.pop(); + for( int i = 0 ; i < graph[u].size() ; i++ ) { + int v = graph[u][i]; + if( d[v] > d[u] + 1 ) { + d[v] = d[u] + 1; + q.push( v ); + } + } + } + return d[destination]; +} + +int main( int argc, char ** argv) { + int nCase = 1, x = 1, edges; + while( scanf( "%d", &edges )!=EOF ) { + for( int i = 1 ; i <= edges ; i++ ) { + int y; + scanf( "%d", &y ); + graph[x].push_back( y ); + graph[y].push_back( x ); + } + + if( x==19 ) { + int N, A, B; + scanf( "%d", &N ); + printf( "Test Set #%d\n", nCase++ ); + for( int i = 1 ; i <= N ; i++ ) { + scanf( "%d %d", &A, &B ); + printf( "%2d to %2d: %d\n", A, B, bfs( A, B ) ); + } + printf( "\n" ); + x = 0; + for( int i = 0 ; i < MAX ; i++ ) { + graph[i].clear(); + } + } + x++; + } + return 0; +} diff --git a/uva_cpp_clean/567/567-21.cpp b/uva_cpp_clean/567/567-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9bbbce2f02bab893159767a47e7258ee76c9491 --- /dev/null +++ b/uva_cpp_clean/567/567-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 567 + Name: Risk + Problem: https://onlinejudge.org/external/5/567.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define INF 10000000 + +int main(){ + int cse = 1, d[22][22], x, y, q, n; + for(;;) { + for (int i=1; i<=20; i++) { + d[i][i] = 0; + for (int j=i+1; j<=20; j++) + d[i][j]=d[j][i]=INF; + } + + for (int i=1; i<20; i++) + if (!(cin>>n)) return 0; + else for (int j=0; j>x; + d[i][x] = d[x][i] = 1; + } + + for (int k=1; k<=20; k++) + for (int i=1; i<=20; i++) + for (int j=1; j<=20; j++) + if (d[i][k] + d[k][j] < d[i][j]) + d[i][j] = d[i][k] + d[k][j]; + + cout << "Test Set #" << (cse++) << endl; + cin >> q; + for (int i=0; i>x>>y; + printf("%2d to %2d: %d\n", x, y, d[x][y]); + } + cout << endl; + } +} diff --git a/uva_cpp_clean/568/568-19.cpp b/uva_cpp_clean/568/568-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..366f172739fc54fb6c531aeb4a80c6f51067702d --- /dev/null +++ b/uva_cpp_clean/568/568-19.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main() { + int digits[10001]; + digits[0] = 1; + for (int i = 1; i < 10001; i++) { + digits[i] = digits[i-1] * i; + while (digits[i] % 10 == 0) digits[i] /= 10; + digits[i] %= 100000; + } + int n; + while (cin >> n) { + printf("%5d -> %d\n", n, digits[n]%10); + } + return 0; +} diff --git a/uva_cpp_clean/568/568-21.cpp b/uva_cpp_clean/568/568-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c61bea94a8c0a1e9b0ab2b26526a6f9f61e81db --- /dev/null +++ b/uva_cpp_clean/568/568-21.cpp @@ -0,0 +1,43 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 568 + Name: Just the Facts + Problem: https://onlinejudge.org/external/5/568.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int count5(int x){ + int xp=5,c=0,t; + while((t=x/xp)>=1){ + c+=t; + xp*=5; + } + return c; +} + +int main() +{ + int n,c5,c2,t,mult; + while (cin>>n) + { + mult=1; + c5=count5(n); + c2=c5; + for(int i=2;i<=n;i++){ + t=i; + while(c5 && !(t%5))t/=5,c5--; + while(c2 && !(t%2))t/=2,c2--; + mult*=(t%10);mult%=10; + } + printf("%5d -> %d\n",n,mult); + } + return 0; +} diff --git a/uva_cpp_clean/568/568-9.cpp b/uva_cpp_clean/568/568-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28a8c0102899e3e51a9e240f5b84cb5845d50245 --- /dev/null +++ b/uva_cpp_clean/568/568-9.cpp @@ -0,0 +1,17 @@ +def solution(): + SIZE = 10010 + factorial, ac = [1], 1 + for it in range(1, SIZE): + ac *= it + while( ac % 10 == 0 ): + ac //= 10 + factorial.append(ac) + while True: + try: + n = int(input()) + print('%5d -> %d' %(n, factorial[n] % 10)) + except EOFError: + break + +if __name__=="__main__": + solution() diff --git a/uva_cpp_clean/571/571-19.cpp b/uva_cpp_clean/571/571-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7459633475522f5c52e9fcdb8dcf1c686ed5fb69 --- /dev/null +++ b/uva_cpp_clean/571/571-19.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iii; + +int Ca, Cb, N, it = 1; + +iii DP[1010][1010]; +int DPV[1010][1010]; + +int proc() { + queue q; + q.push(ii(0, 0)); + DP[0][0] = iii(ii(-1, -1), -1); + DPV[0][0] = it; + while (!q.empty()) { + ii p = q.front(); + q.pop(); + DPV[p.first][p.second] = it; + if (p.second == N) return p.first; + if (DPV[Ca][p.second] != it) { + q.push(ii(Ca, p.second)); + DP[Ca][p.second] = iii(ii(p.first, p.second), 0); + DPV[Ca][p.second] = it; + } + if (DPV[p.first][Cb] != it) { + q.push(ii(p.first, Cb)); + DP[p.first][Cb] = iii(ii(p.first, p.second), 1); + DPV[p.first][Cb] = it; + } + if (DPV[0][p.second] != it) { + q.push(ii(0, p.second)); + DP[0][p.second] = iii(ii(p.first, p.second), 2); + DPV[0][p.second] = it; + } + if (DPV[p.first][0] != it) { + q.push(ii(p.first, 0)); + DP[p.first][0] = iii(ii(p.first, p.second), 3); + DPV[p.first][0] = it; + } + int pa = 0, pb = p.first + p.second; + if (pb > Cb) pa = pb - Cb, pb = Cb; + if (DPV[pa][pb] != it) { + q.push(ii(pa,pb)); + DP[pa][pb] = iii(ii(p.first, p.second), 4); + DPV[pa][pb] = it; + } + pa = p.first + p.second, pb = 0; + if (pa > Ca) pb = pa - Ca, pa = Ca; + if (DPV[pa][pb] != it) { + q.push(ii(pa,pb)); + DP[pa][pb] = iii(ii(p.first, p.second), 5); + DPV[pa][pb] = it; + } + } + return -1; +} + +int main() { + stack s; + while (cin >> Ca >> Cb >> N) { + int sol = proc(); + iii var = DP[sol][N]; + while (var.second != -1) { + s.push(var.second); + var = DP[var.first.first][var.first.second]; + } + while (!s.empty()) { + switch (s.top()) { + case 0: + printf("fill A\n"); break; + case 1: + printf("fill B\n"); break; + case 2: + printf("empty A\n"); break; + case 3: + printf("empty B\n"); break; + case 4: + printf("pour A B\n"); break; + case 5: + printf("pour B A\n"); break; + } + s.pop(); + } + printf("success\n"); + it++; + } + return 0; +} diff --git a/uva_cpp_clean/571/571-20.cpp b/uva_cpp_clean/571/571-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e29506421749f099c695134ba825eab5a28c6d3e --- /dev/null +++ b/uva_cpp_clean/571/571-20.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int Ca, Cb, N; + +void dfs( int A, int B ) { + if( B == N ) { + puts( "success" ); + return; + } + if( !A ) { + puts( "fill A" ); + A = Ca; + } + if( B == Cb ) { + puts( "empty B" ); + B = 0; + } + puts( "pour A B" ); + + B += A; + if( B > Cb ) { + A = B - Cb; + B = Cb; + } + else A = 0; + + dfs( A, B ); +} + +int main( int argc, char ** argv ) { + while( scanf( "%d %d %d", &Ca, &Cb, &N) != EOF ) dfs( 0, 0 ); + + return 0; +} diff --git a/uva_cpp_clean/571/571-21.cpp b/uva_cpp_clean/571/571-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca0acc220324cb4d4fd9e1660b15f96739b7692c --- /dev/null +++ b/uva_cpp_clean/571/571-21.cpp @@ -0,0 +1,77 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 571 + Name: Jugs + Problem: https://onlinejudge.org/external/5/571.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +char actions[][30] = { + "fill A", + "fill B", + "empty A", + "empty B", + "pour A B", + "pour B A" +}; + +int cse=0, A, B, G, seen[1001][1001], path[1000000], plen; +bool dfs(int a, int b, int d) { + if (seen[a][b] == cse) return false; + if (a==G || b==G) { + plen = d; + return true; + } + seen[a][b] = cse; + + if (dfs(0, b, d+1)) { + path[d] = 2; + return 1; + } + + if (dfs(a, 0, d+1)) { + path[d] = 3; + return 1; + } + + if (a<=B-b ? dfs(0, b+a, d+1) : dfs(a+b-B, B, d+1)) { + path[d] = 4; + return 1; + } + + if (b<=A-a ? dfs(b+a, 0, d+1) : dfs(A, a+b-A, d+1)) { + path[d] = 5; + return 1; + } + + if (dfs(A, b, d+1)) { + path[d] = 0; + return 1; + } + + if (dfs(a, B, d+1)) { + path[d] = 1; + return 1; + } + + return false; +} + + +int main(){ + while (scanf("%d%d%d", &A, &B, &G)==3) { + cse++; + dfs(0, 0, 0); + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 105 +using namespace std; +typedef long long ll; +int fx[] = {1, -1, 0, 0, 1, -1, -1, 1}; +int fy[] = {0, 0, 1, -1, 1, -1, 1, -1}; +int color[MAX][MAX]; +int row, col; +string str[MAX]; +void dfs(int sx, int sy) { + color[sx][sy] = 1; + for (int i = 0; i < 8; i++) { + int xx = sx + fx[i]; + int yy = sy + fy[i]; + if (xx >= 0 && yy >= 0 && xx < row && yy < col && str[xx][yy] == '1' && color[xx][yy] == 0) { + dfs(xx, yy); + } + } + color[sx][sy] = 2; +} +void mem() { + for (int i = 0; i < MAX; i++) { + for (int j = 0; j < MAX; j++) { + color[i][j] = 0; + } + } +} +int main () { + while (scanf("%d %d", &row, &col) == 2 && row != 0) { + for (int i = 0; i < row; i++) { + cin >> str[i]; + } + int count = 0; + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + if (color[i][j] == 0 && str[i][j] == '1') { + count++; + dfs(i, j); + } + } + } + printf("%d\n", count); + mem(); + } + return 0; +} + + diff --git a/uva_cpp_clean/572/572-15.cpp b/uva_cpp_clean/572/572-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6978a6af85d9377c854d41f85ea9f8b7aaeaf081 --- /dev/null +++ b/uva_cpp_clean/572/572-15.cpp @@ -0,0 +1,48 @@ +/* + 連通塊數量 + 時間複雜度: O(N^2) +*/ +#include +#include + +using namespace std; + +void DFS(int row, int column, vector> &field, const int &m, const int &n) { + if (row < 0 || row >= m || column < 0 || column >= n || field[row][column] != '@') + return; + + field[row][column] = '*'; + // 八個方向 + DFS(row + 1, column + 1, field, m, n); + DFS(row + 1, column, field, m, n); + DFS(row + 1, column - 1, field, m, n); + DFS(row, column + 1, field, m, n); + DFS(row, column - 1, field, m, n); + DFS(row - 1, column + 1, field, m, n); + DFS(row - 1, column, field, m, n); + DFS(row - 1, column - 1, field, m, n); +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, answer, m, n; + while (cin >> m >> n && (m || n)) { + vector> field(m, vector(n)); + answer = 0; + + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + cin >> field[i][j]; + + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + if (field[i][j] == '@') { + answer++; + DFS(i, j, field, m, n); // 找到一個 @ 的時候 answer + 1 然後消去所有相連的 @ + } + cout << answer << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/572/572-17.cpp b/uva_cpp_clean/572/572-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75620b5fd4ab7ff6c9b2dc0dc45c2afa02ee5180 --- /dev/null +++ b/uva_cpp_clean/572/572-17.cpp @@ -0,0 +1,37 @@ +/** + * AOAPC II Example 6-12 Oil Deposits + */ +#include +using namespace std; +vector grid; +int m, n, cnt; +void dfs(int r, int c) +{ + if (r < 0 || r >= m || c < 0 || c >= n) + return; + if (grid[r][c] != '@') + return; + grid[r][c] = '-'; + for (int dr = -1; dr <= 1; ++dr) + for (int dc = -1; dc <= 1; ++dc) + if (dr || dc) + dfs(r + dr, c + dc); +} +int main() +{ + while ((cin >> m >> n) && m) + { + grid = vector(m); + for (string &line : grid) + cin >> line; + cnt = 0; + for (int i = 0; i < m; ++i) + for (int j = 0; j < n; ++j) + if (grid[i][j] == '@') + { + ++cnt; + dfs(i, j); + } + cout << cnt << "\n"; + } +} diff --git a/uva_cpp_clean/572/572-19.cpp b/uva_cpp_clean/572/572-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23afbfe0c611266ed2578cf334b86779a3a87859 --- /dev/null +++ b/uva_cpp_clean/572/572-19.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +bool grid[100][100]; +int m, n; + +void empty(int x, int y) { + if (x >= 0 && y >= 0 && x < m && y < n && grid[x][y]) { + grid[x][y] = false; + empty(x-1, y-1); + empty(x-1, y); + empty(x-1, y+1); + empty(x, y-1); + empty(x, y+1); + empty(x+1, y-1); + empty(x+1, y); + empty(x+1, y+1); + } +} + +int main() { + while (scanf("%d %d", &m, &n) && m != 0) { + for (int i = 0; i < m; i++) { + char c; + scanf("%c", &c); + for (int j = 0; j < n; j++) { + scanf("%c", &c); + grid[i][j] = c == '@'; + } + } + int cont = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j]) { + empty(i, j); + cont ++; + } + } + } + printf("%d\n", cont); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/572/572-20.cpp b/uva_cpp_clean/572/572-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80cc29678bf3143fe691e825420ba5f863eb8c46 --- /dev/null +++ b/uva_cpp_clean/572/572-20.cpp @@ -0,0 +1,61 @@ +#include + +#define isValid( nx, ny ) ( (nx >= 0 && nx < m) && (ny >= 0 && ny < n) ) + +using namespace std; + +const int fx[] = { +0, +0, +1, -1, -1, +1, -1, +1 }; +const int fy[] = { -1, +1, +0, +0, +1, +1, -1, -1 }; + +const int MAXN = 100; +char G[MAXN+2][MAXN+2]; +bool visited[MAXN+2][MAXN+2]; +int m, n; + +struct node { + int x, y; +}; + +void bfs( int x, int y ) { + node now, next; + queue < node > q; + + now.x = x, now.y = y; + q.push( now ); + visited[x][y] = true; + while( !q.empty() ) { + now = q.front(); + q.pop(); + for( int i = 0 ; i < 8 ; ++i ) { + next.x = now.x + fx[i]; + next.y = now.y + fy[i]; + if( isValid( next.x, next.y ) && G[next.x][next.y] == '@' && !visited[next.x][next.y] ) { + q.push( next ); + visited[next.x][next.y] = true; + } + } + } +} + +int main( int argc, char ** argv ) { + while( scanf( "%d %d", &m , &n ) == 2 && m ) { + for( int i = 0 ; i < m ; ++i ) { + scanf( "%s", G[i] ); + } + + memset( visited, false, sizeof( visited ) ); + + int counter = 0; + for( int i = 0 ; i < m ; ++i ) { + for( int j = 0 ; j < n ; ++j ) { + if( G[i][j] == '@' && !visited[i][j] ) { + bfs( i, j ); + ++counter; + } + } + } + + printf( "%d\n", counter ); + } + return 0; +} diff --git a/uva_cpp_clean/572/572-21.cpp b/uva_cpp_clean/572/572-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e150c3d0d886330cdf09ccf4011a6adede2c3d05 --- /dev/null +++ b/uva_cpp_clean/572/572-21.cpp @@ -0,0 +1,48 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 572 + Name: Oil Deposits + Problem: https://onlinejudge.org/external/5/572.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +char dep[105][105]; +void rec(int x,int y); + +int main() +{ + int m,n,i,j,count; + + while ((scanf("%d%d",&m,&n)!=-1) && m && n) + { + for(i=1;i<=m;i++) + scanf("%s\n",dep[i]+1); + + count=0; + for(i=1;i<=m;i++) + for(j=1;j<=n;j++) + if(dep[i][j]=='@'){ + count++; + rec(i,j); + } + + cout< +#include + +using namespace std; + +int oil = 0; + +void Check(vector> &fields, int m, int n, int add) +{ + if (m < 0 || m >= (int)fields.size() || n < 0 || n >= (int)fields[0].size() || fields[m][n] != '@') + { + return; + } + + fields[m][n] = '*'; + + if (add) + { + oil++; + } + + Check(fields, m - 1, n + 1, 0); + Check(fields, m - 1, n, 0); + Check(fields, m - 1, n - 1, 0); + + Check(fields, m, n - 1, 0); + Check(fields, m, n + 1, 0); + + Check(fields, m + 1, n - 1, 0); + Check(fields, m + 1, n, 0); + Check(fields, m + 1, n + 1, 0); +} + +int main() +{ + while (true) + { + int n, m; + cin >> m >> n; + + if (m == 0 || n == 0) + { + break; + } + + vector> fields(m); + + for (int i = 0; i < m; i++) + { + string temp; + cin >> temp; + + for (int j = 0; j < n; j++) + { + fields[i].push_back(temp[j]); + } + } + + oil = 0; + + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + Check(fields, i, j, 1); + } + } + + cout << oil << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/572/572-5.cpp b/uva_cpp_clean/572/572-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a25e715dc61ebb72d991374f7d4a99f5bc13f3e6 --- /dev/null +++ b/uva_cpp_clean/572/572-5.cpp @@ -0,0 +1,152 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +using namespace std; + +#define ll long long +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sdb(t) scanf("%lf",&t) +#define schar(c) scanf("%c",&c) +#define sstring(s) scanf("%s",s) +#define ssi(a,b) scanf("%d%d",&a,&b) +#define ssl(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PDB(a) printf("%lf\n",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDBN(a) printf("%lf ",a) + +#define CP(a) cout< vi; +typedef vector vl; +typedef vector vs; +typedef pair pii; +typedef vector vpii; +typedef set si; +typedef set ss; +typedef map mii; +typedef map msi; + +//For Define +#define for0(i,n) for(__typeof(n) i=0;i<(n);i++) +#define for1(i,n) for(__typeof(n) i=1;i<=(n);i++) +#define rfor0(i,n) for(__typeof(n) i=(n);i>=0;i--) +#define rfor1(i,n) for(__typeof(n) i=(n);i>=1;i--) +#define For(i,a,b) for(__typeof(b) i=a;i<=b;i++) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define db(x) cout << #x << " -> " << (x) << endl; +#define db_sarr(i,a) cout<<#a<<"["< "< "< "<r-1 || j<-1 || j>c-1 || s[i][j]!='@') + return ; + + s[i][j] = '*'; + + floodfill(i-1,j); + floodfill(i-1,j+1); + floodfill(i-1,j-1); + floodfill(i,j+1); + floodfill(i,j-1); + floodfill(i+1,j); + floodfill(i+1,j+1); + floodfill(i+1,j-1); + +} + + +int main() +{ + int i,cnt; + + while(ssi(r,c)==2) + { + getchar(); + if(r==0 && c==0) + break; + cnt=0; + for0(i,r) + cin>>s[i]; + i=0; + while(i +using namespace std; +using ii=tuple; +using vi=vector; +using vs=vector; +using vii=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int n,m,u,v; + while(1){ + cin>>n>>m; + if(!n)break; + vii e; + vs s(n); + vvi b(n,vi(m)); + for(int i=0;i>s[i]; + int k=0; + for(int i=0;i0&&b[i-1][j])e.push_back(make_tuple(b[i-1][j],b[i][j])); + if(i0&&b[i][j-1])e.push_back(make_tuple(b[i][j-1],b[i][j])); + if(j0&&j>0&&b[i-1][j-1])e.push_back(make_tuple(b[i-1][j-1],b[i][j])); + if(i0&&b[i+1][j-1])e.push_back(make_tuple(b[i+1][j-1],b[i][j])); + if(i>0&&jdfs=[&](int u){ + c[u]=1; + for(int v:g[u]) + if(!c[v]) + dfs(v); + }; + int l=0; + for(int i=0;i + +#define SIZE 110 + +using namespace std; + +int n, m, CC; +char grid[SIZE][SIZE]; +int dr[] = {0, 1, 1, 1, 0, -1, -1, -1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < n and C < m ); +} + +void floodfill(int r, int c){ + if( not is_possible(r, c) ) return; + if( grid[r][c] == '*' ) return; + grid[r][c] = '*'; + for(int d = 0; d < 8; d++) + floodfill(r + dr[d], c + dc[d]); +} + +int main(){ + while(scanf("%d %d\n", &n, &m), n | m){ + CC = 0; + for(int row = 0; row < n; row++) scanf("%s", grid[row]); + for(int row = 0; row < n; row++) + for(int col = 0; col < m; col++) + if( grid[row][col] == '@' ) + floodfill(row, col), CC++; + printf("%d\n", CC); + } + return(0); +} diff --git a/uva_cpp_clean/573/573-14.cpp b/uva_cpp_clean/573/573-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb5b44dda15cfa1313e63815614e035d7b80be1b --- /dev/null +++ b/uva_cpp_clean/573/573-14.cpp @@ -0,0 +1,101 @@ +/*************************************************** + * Problem name : 573 The Snail.cpp + * Problem Link : https://uva.onlinejudge.org/external/5/573.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-07-25 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + double H, U, D, F; + //~ freopen("input.txt", "r", stdin); + //~ freopen("output.txt", "w", stdout); + while (scanf("%lf %lf %lf %lf", &H, &U, &D, &F) == 4) { + if (H == 0) break; + double iH = 0, dC = U, hAc; + int ans, flag; + double f = dC * (F / 100.0); + //~ printf("%lf\n", f); + for (int i = 1; ; i++) { + hAc = iH + dC; + //~ printf("%lf , %lf , %lf , %lf\n", iH,dC,hAc, D); + if (hAc > H) { + flag = 0; + ans = i; + break; + } + iH = hAc - D; + if (iH < 0) { + ans = i; + flag = 1; + break; + } + dC = dC - f; + if (dC <= 0) { + for(int j = i; ;j++){ + iH-=D; + if(iH<0){ + flag = 1; + ans = j+1; + break; + } + } + break; + } + } + //~ printf("%lf , %lf , %lf , %lf\n", iH,dC,hAc, D); + if (flag) { + printf("failure on day %d\n", ans); + } else { + printf("success on day %d\n", ans); + } + } + return 0; +} diff --git a/uva_cpp_clean/573/573-18.cpp b/uva_cpp_clean/573/573-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e8014c26f033c63c13604134d07e5d7dbc9a5ee8 --- /dev/null +++ b/uva_cpp_clean/573/573-18.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main(){ + int h, d, f, day; + float u, factor, aH; + while(scanf("%d %f %d %d", &h, &u, &d, &f) && h != 0){ + day = 1; + factor = u*f/100; + aH = 0; + while(true){ + aH += u; + if(u > 0) u -= factor; + if(aH > h) break; + aH -= d; + if(aH < 0) break; + day++; + } + if(aH > 0) printf("success on day %d\n", day); + else printf("failure on day %d\n", day); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/573/573-21.cpp b/uva_cpp_clean/573/573-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9adf350474e2bb2ddd0e5b40de901483a583b03a --- /dev/null +++ b/uva_cpp_clean/573/573-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 573 + Name: The Snail + Problem: https://onlinejudge.org/external/5/573.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int fun(int h, long double u, int d, long double f) { + long double c = 0; + + for (int i = 1; 1; i++) { + c += u; + if (c > h) return i; + + c -= d; + if (c < 0) return -i; + + u -= u < f ? u : f; + } +} + + + +int main() { + int h, u, d, f, i; + while ((cin >> h >> u >> d >> f) && h) { + + int res = fun(h, u, d, (u * f) / 100.0); + + if (res > 0) + cout << "success on day " << res << endl; + else + cout << "failure on day " << -res << endl; + } +} diff --git a/uva_cpp_clean/573/573-3.cpp b/uva_cpp_clean/573/573-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cee44b40c529a70eadd8a04972a6da4240a5f1c6 --- /dev/null +++ b/uva_cpp_clean/573/573-3.cpp @@ -0,0 +1,42 @@ +#include + +int main(){ + // The input will contain multiple test cases. Ends when H == 0. Var description: + // 1. int height ( the height the snail is supposed to climb ) + // 2. double vUp ( snail's initial vUp ) + // 3. int vDown ( snail's negative vUp ) --> is a constant for each case + // 4. double fatigue ( given in percentage is the decline in snails vUp per day) + int height, vDown; + double vUp, fatigue; + // double snHeight (stands for snail height) is the real time height of the snail + double snHeight; + + // Looping until there is no input: + while ( scanf ("%d %lf %d %lf", &height, &vUp, &vDown, &fatigue) == 4 ){ + // The program should end if height == 0: + if ( height == 0 ) return 0; + // snHeight is always 0 at the beggining + snHeight = 0; + // fatigue is entered as a percentage : + fatigue = fatigue/100.0 * vUp; + // The first day is considered day 1 and not day 0 + for ( int day = 1; ; day++ ) { + snHeight += vUp; + if ( snHeight > height ){ + printf ("%s %d\n", "success on day",day); + break; + } + snHeight -= vDown; + if ( snHeight < 0 ){ + printf ("%s %d\n", "failure on day",day); + break; + } + if ( vUp != 0){ + if ( vUp - fatigue > 0 ) + vUp -= fatigue; + else + vUp = 0; + } + } + } +} diff --git a/uva_cpp_clean/573/main.cpp b/uva_cpp_clean/573/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f07148619111de3124738aad90433372374bf19d --- /dev/null +++ b/uva_cpp_clean/573/main.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +int main(){ + int height, ini_distance_up, dist_down, percentage; + float dist_up, position; + while(cin >> height >> ini_distance_up >> dist_down >> percentage && height != 0){ + position = 0; + int day = 0; + dist_up = ini_distance_up; + bool res = false; + while(true){ + day++; + position += dist_up; + if(position > height){ + res = true; + break; + }else{ + position -= dist_down; + if(position < 0) break; + dist_up -= ini_distance_up * percentage/100.0; + if(dist_up < 0) dist_up = 0; + } + } + if(res) printf("success on day %d\n", day); + else printf("failure on day %d\n", day); + } + + + +return 0; +} diff --git a/uva_cpp_clean/574/574-13.cpp b/uva_cpp_clean/574/574-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77a944b2598063e2fd5b1d34291b5d9d785c61aa --- /dev/null +++ b/uva_cpp_clean/574/574-13.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +bool found = false; +int how, n, nums[20]; +vector ans; +bool visit[20]; + +ofstream fout("f.out"); + +string toString(int num) +{ + string s = ""; + stack myStack; + + while (num != 0) + { + myStack.push((num % 10) + '0'); + + num /= 10; + } + + while (!myStack.empty()) + { + s += myStack.top(); + + myStack.pop(); + } + + return s; +} + +int main() +{ + ios::sync_with_stdio(0); + + cin >> how >> n; + + while (how != 0 || n != 0) + { + found = false; + + ans.clear(); + + memset(visit, 0, sizeof visit); + memset(nums, 0, sizeof nums); + + for (int i = 0; i < n; i++) + { + cin >> nums[i]; + } + + cout << "Sums of " << how << ":" << endl; + + for (int bitmask = (1 << n); bitmask > 0 ; bitmask--) + { + string s = ""; + + int sum = 0, temp = bitmask; + + for (int i = 0; i < n; i++) + { + visit[n - i - 1] = temp % 2; + + temp /= 2; + } + + for (int i = 0; i < n; i++) + { + if (visit[i]) + { + sum += nums[i]; + + s += toString(nums[i]); + + s += '+'; + } + } + + if (sum == how) + { + bool ok = true; + + for (int i = 0; i < (int)ans.size(); i++) + { + if (ans[i] == s) + { + ok = false; + } + } + + if (ok) + { + found = true; + + ans.push_back(s); + + s = s.substr(0, s.size() - 1); + + cout << s << endl; + } + } + } + + if (!found) + { + cout << "NONE" << endl; + } + + cin >> how >> n; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/574/574-19.cpp b/uva_cpp_clean/574/574-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4497d137686bf11801ad4b070a09a8f6e8a29a5 --- /dev/null +++ b/uva_cpp_clean/574/574-19.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; + +int n, t, n2; +int nums[12][2]; +int sol[12], once; + +void proc(int pos, int sum, int elems) { + if (sum == t) { + once = true; + bool first = false; + for (int i = 0; i < pos; i++) { + for (int j = 0; j < sol[i]; j++) { + if (first) printf("+"); + else first = true; + printf("%d", nums[i][0]); + } + } + printf("\n"); + } + if (sum < t && pos < n && sum + (n2-elems)*nums[pos][0] >= t) { + sol[pos] = nums[pos][1]; + for (int i = 0; sol[pos] >= 0; sol[pos]--, i++) { + proc(pos+1, sum + (sol[pos]*nums[pos][0]), elems+i); + } + sol[pos] = 0; + } + +} + +int main() { + for (int i = 0; i < 12; i++) sol[i] = 0; + while (scanf("%d %d", &t, &n) && !(t == 0 && n == 0)) { + n2 = n; + once = false; + printf("Sums of %d:\n", t); + int prev = -1, cont = 0; + for (int i = 0; i < n; i++) { + scanf("%d", &nums[i-cont][0]); + if (nums[i-cont][0] == prev) { + cont ++; + nums[i-cont][1]++; + } else { + prev = nums[i-cont][0]; + nums[i-cont][1] = 1; + } + } + n -= cont; + proc(0, 0, 0); + if (!once) printf("NONE\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/574/574-21.cpp b/uva_cpp_clean/574/574-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0e4558bf7ba7042d112efbf36ca054dd2d819e8 --- /dev/null +++ b/uva_cpp_clean/574/574-21.cpp @@ -0,0 +1,59 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 574 + Name: Sum It Up + Problem: https://onlinejudge.org/external/5/574.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int t, n, cnt, nu[20], nuc[20], chosen[20], chcnt, ppp; +void rec(int sum, int ind) { + if (sum<=0 || ind>=cnt) { + if (!sum) { + for (int i=0; i=0; i--) { + chcnt = oldch + i; + rec(sum - i*nu[ind], ind+1); + } +} + + +int main(){ + while (cin>>t>>n && n) { + cin >> nu[0]; + nuc[0] = 1; + cnt = 1; + for (int i=1; i>nu[cnt]; + if (nu[cnt] != nu[cnt-1]) + nuc[cnt++] = 1; + else nuc[cnt-1]++; + } + + printf("Sums of %d:\n", t); + chcnt = ppp = 0; + rec(t, 0); + if (!ppp) puts("NONE"); + } +} diff --git a/uva_cpp_clean/575/575-14.cpp b/uva_cpp_clean/575/575-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fc141744f31e0564a2be81c483e9dbb6b21c2f2 --- /dev/null +++ b/uva_cpp_clean/575/575-14.cpp @@ -0,0 +1,71 @@ +/*************************************************** + * Problem name : 575 Skew Binary.cpp + * Problem Link : https://uva.onlinejudge.org/external/5/575.pdf + * OJ : Uva + * Verdict : AC + * Date : 2017-10-24 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + string str; + while (cin >> str) { + if (str == "0") break; + int sz = str.size(); + ll ans = 0, j = 1; + for (int i = sz - 1; i >= 0; i--) { + ans += ((str[i] - '0') * ((1 << j) - 1)); + j++; + } + printf("%lld\n", ans); + } + + return 0; +} + diff --git a/uva_cpp_clean/575/575-19.cpp b/uva_cpp_clean/575/575-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..538029a8befa8512d9dfdf974c360132153981a8 --- /dev/null +++ b/uva_cpp_clean/575/575-19.cpp @@ -0,0 +1,14 @@ +#include +#include +using namespace std; + +int main() { + string n; + while (cin >> n && n != "0") { + int val = 0, pot = 2; + for (int i = n.size()-1; i>=0; i--, pot = pot<<1) { + val += (n[i]-'0')*(pot-1); + } + cout << val << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/575/575-21.cpp b/uva_cpp_clean/575/575-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b4296ee853aba61d00835e22a33ba5729ac8639 --- /dev/null +++ b/uva_cpp_clean/575/575-21.cpp @@ -0,0 +1,27 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 575 + Name: Skew Binary + Problem: https://onlinejudge.org/external/5/575.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int i,l,n; + char num[100]; + while(gets(num) && (num[1] || num[0]=='1' || num[0]=='2')){ + l=strlen(num); + for(n=i=0;num[i];i++) + n+=((1<<(l-i))-1)*(num[i]-'0'); + cout< +#include +#include + +int main() +{ +long int l,i,s,c; +char a[100]; +while(scanf("%s",&a)) +{ + s=0; + l=strlen(a); + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,0,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + string str; + int ar[4]; + mapmp; + mp['a'] = 1, mp['e'] = 1, mp['i'] = 1, mp['o'] = 1, mp['u'] = 1, mp['y'] = 1; + while (getline(cin, str)) { + if(str == "e/o/i") break; + SET(ar); + int sz = str.size(), flag = 0, j = 0; + for (int i = 0; i < sz; i++) { + if (str[i] == '/') { + j++; + flag = 0; + } else if (flag == 0 && mp[str[i]] == 1) { + ar[j]++; + flag = 1; + } + else if(flag == 1 && mp[str[i]] == 0){ + flag = 0; + } + } + if(ar[0] == 5 && ar[1] == 7 && ar[2] == 5) printf("Y\n"); + else if(ar[0] != 5) printf("1\n"); + else if(ar[1] != 7) printf("2\n"); + else printf("3\n"); + } + return 0; +} + diff --git a/uva_cpp_clean/576/576-21.cpp b/uva_cpp_clean/576/576-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7ab01e2dea3b7b8d552bbe9b1fa07b4d72435f4 --- /dev/null +++ b/uva_cpp_clean/576/576-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 576 + Name: Haiku Review + Problem: https://onlinejudge.org/external/5/576.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +bool isvowel(char c) { + return c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='y'; +} + + +int haiku[] = {5,7,5}; +int check(char *line) { + int j=-1; + for (int i=0; i<3; i++) { + int c=0; + bool last=0; + for (j++; line[j] && line[j]!='/'; j++) + if (!isvowel(line[j])) last=0; + else if (!last) { c++; last=1; } + + if (c != haiku[i]) + return i+1; + } + return 0; +} + +char results[] = "Y123"; +int main(){ + char line[500]; + while (cin.getline(line, 500) && strcmp(line, "e/o/i")) { + putchar(results[check(line)]); + putchar('\n'); + } +} diff --git a/uva_cpp_clean/579/579-19.cpp b/uva_cpp_clean/579/579-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bdaa58f4989a70932bde5b81543ae553de26077 --- /dev/null +++ b/uva_cpp_clean/579/579-19.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +double proc(double d){ + d = fabs(d); + if(d > 180) return 360-d; + return d; +} + +int main() { + double h, min; + double angle; + scanf("%lf:%lf", &h, &min); + while (h != 0 || min != 0) { + printf("%.3f\n", proc((((h*60+min)/720)-(min/60))*360)); + scanf("%lf:%lf", &h, &min); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/579/579-21.cpp b/uva_cpp_clean/579/579-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15d2f5ba9f489b5a5c1727053d7e946318c7de69 --- /dev/null +++ b/uva_cpp_clean/579/579-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 579 + Name: Clock Hands + Problem: https://onlinejudge.org/external/5/579.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; +int main() +{ + int h,m; + double t; + char y; + while (cin>>h>>y>>m && (h || m)) + printf("%.3f\n",(t=fabs(30*h-5.5*m))<180?t:360-t); + return 0; +} diff --git a/uva_cpp_clean/579/579-5.cpp b/uva_cpp_clean/579/579-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..812a9dc3ea894adc84cde5a2b72f59a4248c48fd --- /dev/null +++ b/uva_cpp_clean/579/579-5.cpp @@ -0,0 +1,70 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*** http://shipuahamed.blogspot.com ** +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; +int main() +{ + + double m,h,angle,p,q; + while(scanf("%lf:%lf",&h,&m)==2) + { + if((h==0)&&(m==0)) + break; + p=h*30+(m/60)*30; + q=m*6; + angle=(p-q); + if(angle<0) + angle*=-1; + if (angle>180) + angle=360-angle; + + printf("%.3lf\n",angle); + } + + return 0; +} diff --git a/uva_cpp_clean/579/579-6.cc b/uva_cpp_clean/579/579-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..51e33af04d3ba3719e4e4b160f9131c3b7e2b8d8 --- /dev/null +++ b/uva_cpp_clean/579/579-6.cc @@ -0,0 +1,19 @@ +// https://uva.onlinejudge.org/external/5/579.pdf +#include +#include +#include + +using namespace std; + +int main() { + while (true) { + int h, m; + scanf("%d:%d\n", &h, &m); + if (!h && !m) break; + double ha = h * 30 + m * 0.5; + if (ha >= 360) ha -= 360; + int ma = m * 6; + double da = abs(ha - ma); + printf("%.3f\n", min(da, 360 - da)); + } +} diff --git a/uva_cpp_clean/580/580-19.cpp b/uva_cpp_clean/580/580-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e39508414eba5d1ce61b6115b9289d8b6db34613 --- /dev/null +++ b/uva_cpp_clean/580/580-19.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; + +typedef long long ll; + +unordered_map dp; + +ll proc(int N) { + if (!dp.count(N)) { + dp[N] = (1<<(N-3)) + proc(N - 1) + proc(N - 2) + proc(N - 3); + } + return dp[N]; +} + +int main() { + int N; + dp[0] = dp[1] = dp[2] = 0, dp[3] = 1; + while (cin >> N && N) { + printf("%lld\n", proc(N)); + } + return 0; +} diff --git a/uva_cpp_clean/580/580-21.cpp b/uva_cpp_clean/580/580-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdb604d5f07832bef47b50b10cd446719bbf7ad2 --- /dev/null +++ b/uva_cpp_clean/580/580-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 580 + Name: Critical Mass + Problem: https://onlinejudge.org/external/5/580.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int DP[31]={1,2,4}; +int main(){ + for (int i=3; i<31; i++) + DP[i] = DP[i-1] + DP[i-2] + DP[i-3]; + + int n; + while (scanf("%d", &n)==1 && n) + printf("%d\n", (1< + +using namespace std; + +int n; +vector trib; + +int main(){ + trib.push_back(0), trib.push_back(0), trib.push_back(1); + for(int i = 3; i <= 33; i++) + trib.push_back(trib[i - 1] + trib[i - 2] + trib[i - 3]); + while(scanf("%d", &n), n) + printf("%ld\n",(1l << n) - trib[n + 3]); + return(0); +} diff --git a/uva_cpp_clean/583/583-13.cpp b/uva_cpp_clean/583/583-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2125ba42874a97bff8ee9c3cc07159ff8c88095 --- /dev/null +++ b/uva_cpp_clean/583/583-13.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 10000001 + +typedef long long ll; +typedef vector vll; +typedef vector vb; +typedef pair pll; +typedef vector< pll > vpll; + +ll x; +vll primes; +vpll factors; +vb isPrime(MAX, true); + +void findPrime() +{ + isPrime[0] = isPrime[1] = false; + + for (ll i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (ll j = i * i; j < MAX; j += i) + { + isPrime[j] = false; + } + + primes.push_back(i); + } + } +} + +void findFactors(ll n) +{ + ll primes_index = 0, pf = primes[primes_index]; + + while (pf * pf <= n) + { + ll count = 0; + + if (n % pf == 0) + { + while (n % pf == 0) + { + n /= pf; + + count++; + } + + factors.push_back(make_pair(pf, count)); + } + + pf = primes[++primes_index]; + } + + if (n != 1) + { + factors.push_back(make_pair(n, 1)); + } +} + +int main() +{ + findPrime(); + + while (cin >> x && x != 0) + { + factors.clear(); + + if (x < 0) + { + factors.push_back(make_pair(-1, 1)); + } + + findFactors(abs(x)); + + cout << x << " = "; + + for (int i = 0; i < factors.size(); ++i) + { + for (int j = 0; j < factors[i].second; ++j) + { + if (i != 0 || j != 0) + cout << " x "; + + cout << factors[i].first; + } + } + + cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/583/583-19.cpp b/uva_cpp_clean/583/583-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..418b7717d82a7a94696d1cffa4634bc0904f3248 --- /dev/null +++ b/uva_cpp_clean/583/583-19.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair ii; +typedef vector vii; + +ll sieve_size; +bitset<10000000> bs; +vi primes; + +void sieve(ll upperbound) { + sieve_size=upperbound+1; bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back((int)i); + } +} vii primeFactors(ll n) { + vii factors; int c; ll idx=0, p=primes[0]; + while (n!=1 && (p*p<=n)) { + c=0; while(n%p==0) c++, n/=p; + if(c!=0) factors.push_back(ii(p,c)); + p=primes[++idx]; + } if (n!=1) factors.push_back(ii(n,1)); + return factors; +} + +int main() { + int n; + sieve(46350); + while (scanf("%d", &n) && n != 0) { + vii factors = primeFactors(fabs(n)); + printf("%d = ", n); + if (n < 0) printf("-1 x "); + bool first = false; + for (int i = 0; i < factors.size(); i++) { + for (int j = 0; j < factors[i].second; j++){ + if (first) printf(" x "); + else first = true; + printf("%d", factors[i].first); + } + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/583/583-20.cpp b/uva_cpp_clean/583/583-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04f5f9bbf424a9d86f99216704b196a140eb02a4 --- /dev/null +++ b/uva_cpp_clean/583/583-20.cpp @@ -0,0 +1,29 @@ +#include +#include + +int main() +{ + int g; + + while( scanf("%d", &g)==1 && g ) + { + printf("%d = ", g); + if( g<0 ) + { + printf("-1 x "); + g = -g; + } + + for(int i=2 ; i<=sqrt(g) ; i++) + while( g%i==0 ) + { + printf("%d", i); + g /= i; + if( g>1 ) printf(" x "); + } + + if( g>1 ) printf("%d", g); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/583/583-5.cpp b/uva_cpp_clean/583/583-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..dd7abf066d4f0f24f295db19c4283def3f085f96 --- /dev/null +++ b/uva_cpp_clean/583/583-5.cpp @@ -0,0 +1,81 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*** http://shipuahamed.blogspot.com ** +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define sc scanf +#define pf printf +#define ll long long +#define pi 2*acos(0.0) + +#define ff first +#define se second +#define inf (1<<30) //infinity value +#define pb push_back +#define mod 1000000007 +#define ST(v) sort(v.begin(),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define un(v) ST(v), v.erase(unique(v.begin(),v.end()),v.end()) + +using namespace std; + +int main() +{ + ll n; + while(cin>>n) + { + if(n==0) break; + pf("%lld = ",n); + if(n<0) + { + pf("-1 x "); + n=-1*n; + } + ll l=sqrt(n); + for(ll i=2;i<=l;i=i++) + { + while(n%i==0) + { + pf("%lld",i); + n=n/i; + if(n>1) + pf(" x "); + } + } + if(n>1) + pf("%lld",n); + pf("\n"); + + } + return 0; +} + diff --git a/uva_cpp_clean/583/583-9.cpp b/uva_cpp_clean/583/583-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4af04f06584314f048d66f2160d87324348ab38 --- /dev/null +++ b/uva_cpp_clean/583/583-9.cpp @@ -0,0 +1,39 @@ +#include + +#define SIZE 100010 + +using namespace std; + +int n; +bool is_prime[SIZE]; +vector prime, Factors; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++) + if(is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +void primeFactors(int N){ + Factors.clear(); + if( N < 0 ) Factors.push_back( -1 ), N *= -1; + for(int it = 0; it < prime.size() and prime[it] * prime[it] <= N and N != 1; it++) + while( N % prime[it] == 0) N /= prime[it], Factors.push_back( prime[it] ); + if( N != 1 ) Factors.push_back( N ); +} + +int main(){ + sieve(); + while(scanf("%d", &n), n){ + primeFactors( n ); + printf("%d = ", n ); + for(int it = 0; it < Factors.size(); it++) + printf( it != Factors.size() - 1 ? "%d x " : "%d\n", Factors[it]); + } + return(0); +} diff --git a/uva_cpp_clean/587/587-21.cpp b/uva_cpp_clean/587/587-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..695100423d9e46b734bbfd72c2171e812572fff5 --- /dev/null +++ b/uva_cpp_clean/587/587-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 587 + Name: There's treasure everywhere! + Problem: https://onlinejudge.org/external/5/587.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +const double sq2 = sqrt(2); +int di[91], + dx[] = {0, 1, 0, -1}, + dy[] = {1, 0, -1, 0}; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(3); + di['N']=0; di['E']=1; di['S']=2; di['W']=3; + + string str; + for (int cse=1; cin >> str && str!="END"; ++cse) { + double dd = 0, x = 0, y = 0; + for (int i=0; str[i]; ++i) + if (str[i]>='0' && str[i]<='9') + dd = dd*10 + str[i]-'0'; + else { + bool isd = str[i+1]!='.' && str[i+1]!=','; + if (isd) dd /= sq2; + + x += dx[di[str[i]]] * dd; + y += dy[di[str[i]]] * dd; + + if (!isd) dd = 0; + } + + cout << "Map #" << cse + << "\nThe treasure is located at (" << x << ',' << y + << ").\nThe distance to the treasure is " << sqrt(x*x+y*y) + << ".\n\n"; + } +} diff --git a/uva_cpp_clean/590/590-21.cpp b/uva_cpp_clean/590/590-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b6e7b406427e56b22c67cd7c855c09da849b963 --- /dev/null +++ b/uva_cpp_clean/590/590-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 590 + Name: Always on the run + Problem: https://onlinejudge.org/external/5/590.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + + +struct State { + int u, d, c; + State(int u, int d, int c):u(u),d(d),c(c) {} + bool operator < (const State& o) const { return c > o.c; } +}; + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int FC[17][17][43], FD[17][17], C[17][1043], n, m; + for (int cse=1; cin >> n >> m && (n||m); ++cse) { + cout << "Scenario #" << cse << "\n"; + + for (int i=0; i> FD[i][j]; + for (int k=0; k> FC[i][j][k]; + } + + for (int i=0; i q; + q.push(State(0, 0, C[0][0] = 0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.c != C[s.u][s.d]) continue; + if (s.d==m && s.u==n-1) { + cout << "The best flight costs " << s.c << ".\n"; + goto fin; + } + + if (s.d < m) + for (int j=0; j +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int answer, n, average, n_case = 1; + while (cin >> n && n) { + vector brick(n, 0); + answer = average = 0; + + while (n) + cin >> brick[--n]; + + for (const int& i : brick) + average += i; + + average /= brick.size(); + + for (const int& i : brick) + if (i > average) + answer += (i - average); + + cout << "Set #" << n_case++ << "\nThe minimum number of moves is " << answer << ".\n\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/591/591-18.cpp b/uva_cpp_clean/591/591-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..73c173c98bff06372eb7ae7a45ec32e5ee26cfee --- /dev/null +++ b/uva_cpp_clean/591/591-18.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int main(){ + int N, K, height, bricks, min, max, minI, maxI, stacks[50], testCase = 0; + while(true){ + cin >> N; + if(testCase != 0) printf("\n"); + if(N == 0) break; + K = 0; + bricks = 0; + for(int i = 0; i < N; i++){ + cin >> stacks[i]; + bricks += stacks[i]; + } + height = bricks/N; + bool change = true; + for(int i = 0; i < N && change; i++){ + min = 101; + max = -101; + change = false; + for(int j = 0; j < N; j++){ + if(stacks[j] < min && stacks[j] != height){ + min = stacks[j]; + minI = j; + change = true; + } + } + for(int j = 0; j < N; j++){ + if(stacks[j] > max && stacks[j] != height){ + max = stacks[j]; + maxI = j; + change = true; + } + } + if(change){ + int delta; + if(max - height <= height - min){ + delta = max - height; + }else delta = height - min; + stacks[maxI] -= delta; + stacks[minI] += delta; + K += delta; + } + } + testCase++; + printf("Set #%d\n", testCase); + printf("The minimum number of moves is %d.\n", K); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/591/591-3.cpp b/uva_cpp_clean/591/591-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a75259d43948148df022cc57f1874df1ce6c2cd --- /dev/null +++ b/uva_cpp_clean/591/591-3.cpp @@ -0,0 +1,28 @@ +#include + +int main() +{ + int n, h[50], sum, avg, res, sets=0; + while ( scanf("%d",&n), n ) + { + sum = 0; + res = 0; + + for ( int i = 0; i < n; i++ ) + { + scanf("%d", &h[i] ); + sum += h[i]; + } + + avg = sum/n; + + for ( int i = 0; i < n; i++ ) + if ( h[i] < avg ) + res += avg - h[i]; + + printf("Set #%d\n", ++sets); + printf("The minimum number of moves is %d.\n\n", res); + } +} + + diff --git a/uva_cpp_clean/591/591-5.cpp b/uva_cpp_clean/591/591-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ed7c57da3a8e620b2e02a3bb88e3ad6f6ab5e8a6 --- /dev/null +++ b/uva_cpp_clean/591/591-5.cpp @@ -0,0 +1,21 @@ +#include +int main() +{ +int n,i,a[100],t=1,sum,c; +while(scanf("%d",&n)==1 && n!=0) +{ + sum=0; + c=0; + for(i=0;isum) + c=c+(a[i]-sum); + printf("Set #%d\nThe minimum number of moves is %d.\n\n",t++,c); +} +return 0; +} diff --git a/uva_cpp_clean/592/592-21.cpp b/uva_cpp_clean/592/592-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69e6e86aaf30e581ac76e0117118764a4d6c11ea --- /dev/null +++ b/uva_cpp_clean/592/592-21.cpp @@ -0,0 +1,164 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 592 + Name: Island of Logic + Problem: https://onlinejudge.org/external/5/592.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define DIVINE 0 +#define HUMAN 1 +#define EVIL 2 +#define LYING 3 +#define DAY 1 +#define NIGHT 0 + +string types[] = { + "divine", + "human", + "evil", +}; + +inline bool ends_with(string const &value, string const &postfix) { + if (postfix.size() > value.size()) return false; + return equal(postfix.rbegin(), postfix.rend(), value.rbegin()); +} + +struct fact { + int src; + int dest; + bool nott; + bool isDay; + bool isLying; + bool isSome; + int isWhat; +}; + +vector trans(int x) { + x >>= 1; + vector guys; + for (int i=0; i<5; ++i) { + guys.push_back(x % 3); + x /= 3; + } + return guys; +} + +bool isFeasible(vector facts, bool isDay, vector guys) { + for (fact f: facts) { + bool saysTruth; + if (f.isDay) { + saysTruth = (isDay && !f.nott) || (!isDay && f.nott); + } + + if (f.isSome) { + saysTruth = (!f.nott && guys[f.dest] == f.isWhat) || (f.nott && guys[f.dest] != f.isWhat); + } + + if (f.isLying) { + bool destIsLyar = (!isDay && guys[f.dest] == HUMAN) || guys[f.dest] == EVIL; + saysTruth = (!f.nott && destIsLyar) || (f.nott && !destIsLyar); + } + + bool srcIsLyar = (!isDay && guys[f.src] == HUMAN) || guys[f.src] == EVIL; + if ((srcIsLyar && saysTruth) || (!srcIsLyar && !saysTruth)) { + return false; + } + } + + return true; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n, cse=1; + while (cin >> n && n) { + string ln; + getline(cin, ln); + vector facts; + for (int i=0; i guys = trans(i); + + if (isFeasible(facts, isDay, guys)) { + isPossible = true; + + if (day == -1) day = isDay; + else if (day != isDay) day = -2; + + for (int i=0; i<5; ++i) { + if (fguys[i] == -1) fguys[i] = guys[i]; + else if (fguys[i] != guys[i]) fguys[i] = -2; + } + } + } + + cout << "Conversation #" << (cse++) << endl; + if (!isPossible) { + cout << "This is impossible." << endl; + } else { + bool someDeduced = day >= 0; + for (int i=0; i<5; ++i) { + if (fguys[i]>=0) { + cout << char('A' + i) << " is " << types[fguys[i]] << "." << endl; + someDeduced = true; + } + } + if (day == 0) cout << "It is night." << endl; + else if (day == 1) cout << "It is day." << endl; + if (!someDeduced) { + cout << "No facts are deducible." << endl; + } + } + cout << endl; + } +} diff --git a/uva_cpp_clean/594/594-19.cpp b/uva_cpp_clean/594/594-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c447aeb1dba43bc3581175e1dfb80bfccd3a93c2 --- /dev/null +++ b/uva_cpp_clean/594/594-19.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() { + int n; + while (cin >> n) { + int aux = 0; + for (int i = 0; i < 4; i++) + aux |= ((n>>(8*i))&255) << (8*(3-i)); + printf("%d converts to %d\n", n, aux); + } + return 0; +} diff --git a/uva_cpp_clean/594/594-21.cpp b/uva_cpp_clean/594/594-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..649123deac931986dbc6427e0d868972089b82da --- /dev/null +++ b/uva_cpp_clean/594/594-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 594 + Name: One Little, Two Little, Three Little Endians + Problem: https://onlinejudge.org/external/5/594.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n; + while (cin>>n) { + int m = ((n&0x000000FF)<<24) + + ((n&0x0000FF00)<<8) + + ((n&0x00FF0000)>>8) + + ((n&0xFF000000)>>24); + + printf("%d converts to %d\n", n, m); + } +} + diff --git a/uva_cpp_clean/594/594-3.cpp b/uva_cpp_clean/594/594-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b25422a17fa201e0bbcc320436090882fcebaa1a --- /dev/null +++ b/uva_cpp_clean/594/594-3.cpp @@ -0,0 +1,17 @@ +#include + +int main () +{ + int N, result; + + while ( scanf("%d", &N) != EOF ) + { + result = 0; + result |= (( N & (127) ) << 24 ); + result |= (( N & (127 << 8)) << 8 ); + result |= (( N & (127 << 16)) >> 8 ); + result |= (( N & (127 << 24)) >> 24); + printf ("%d converts to %d\n", N, result); + } +} + diff --git a/uva_cpp_clean/594/594-9.cpp b/uva_cpp_clean/594/594-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..059e4d3a952074bf5f4a60d988e03bf1813b4db7 --- /dev/null +++ b/uva_cpp_clean/594/594-9.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +long long N, tmp; + +long long convert(long long n){ + bitset <32> bs1(n), bs2; + for(int i = 0; i < 8; i++) bs2[24 + i] = bs1[i]; + for(int i = 8; i < 16; i++) bs2[8 + i] = bs1[i]; + for(int i = 16; i < 24; i++) bs2[ -8 + i] = bs1[i]; + for(int i = 24; i < 32; i++) bs2[ -24 + i] = bs1[i]; + tmp = 0; + for(int i = 0; i < 32; i++) tmp += bs2[i] * (1<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 598 + Name: Bundling Newspapers + Problem: https://onlinejudge.org/external/5/598.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int n; +string names[13], line; +bool chosed[13]; +void dfs(int idx, int cnt) { + if (n-idx < cnt) return; + if (!cnt) { + bool frst = 1; + for (int j=0; jpubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, l, r; + cin >> T; + cin.ignore(100, '\n'); + while (T--) { + cin.ignore(100, '\n'); + getline(cin, line); + for (n=0; cin.peek()!='\n' && getline(cin, names[n]); ++n); + + if (line[0]=='*') l=1, r=n; + else { + stringstream sin(line); + sin >> l; + if (!(sin>>r)) r = l; + } + + for (int sz=l; sz<=r; ++sz) { + cout << "Size " << sz << '\n'; + dfs(0, sz); + cout << '\n'; + } + if (T) cout << '\n'; + } +} diff --git a/uva_cpp_clean/599/599-18.cpp b/uva_cpp_clean/599/599-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..84178232c39677400770fd7a3063a89000d4df31 --- /dev/null +++ b/uva_cpp_clean/599/599-18.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int parent[26], sets = 0, size[26]; +vector > edges; + +void makeSet(){ + for(int i = 0; i < 26; i++) parent[i] = i; +} + +int find(int i){ + if(i == parent[i]) return i; + return find(parent[i]); +} + +bool sameSet(int i, int j){ + return find(i) == find(j); +} + +void unionSet(int i, int j){ + int iRoot = find(i), jRoot = find(j); + parent[iRoot] = jRoot; + size[jRoot]+=size[iRoot]; + sets--; +} + +int main(){ + int tc, acorns = 0; + char input[100]; + scanf("%d", &tc); + while(tc--){ + edges.clear(); + sets = acorns = 0; + memset(size, 0, sizeof(size)); + memset(input, 0, sizeof(input)); + makeSet(); + while(scanf("%s", input) && input[0] != '*') edges.push_back(make_pair(input[1]-'A', input[3]-'A')); + scanf("%s", input); + for(int i = 0; input[i]; i+=2) if(input[i]-'A' >= 0 && input[i]-'A' < 26) size[input[i]-'A'] = 1, sets++; + for(pair p : edges) if(!sameSet(p.first, p.second)) unionSet(p.first, p.second); + for(int i = 0; i < 26; i++) if(parent[i] == i && size[i] == 1) acorns++; + printf("There are %d tree(s) and %d acorn(s).\n", sets - acorns, acorns); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/599/599-21.cpp b/uva_cpp_clean/599/599-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f14b88f187a16cc66f9fbce8dacb40be0a0f333 --- /dev/null +++ b/uva_cpp_clean/599/599-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 599 + Name: The Forrest for the Trees + Problem: https://onlinejudge.org/external/5/599.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int seen[200]; +int main(){ + int T; + char line[1000]; + scanf("%d", &T); + while (getchar()!='\n'); + for (int cse=1; cse<=T; ++cse) { + int ecn = 0; + while (gets(line) && line[0]!='*') { + seen[line[1]] = seen[line[3]] = cse; + ++ecn; + } + + gets(line); + int acn=0, tcn=0; + for (int i=0; ;i+=2) { + ++tcn; + if (seen[line[i]]!=cse) ++acn; + if (!line[i+1]) break; + } + printf("There are %d tree(s) and %d acorn(s).\n", tcn-ecn-acn, acn); + } +} diff --git a/uva_cpp_clean/599/599-6.cc b/uva_cpp_clean/599/599-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..fc643ef79a7e63412bde6828ec7915b528cf3202 --- /dev/null +++ b/uva_cpp_clean/599/599-6.cc @@ -0,0 +1,41 @@ +// https://uva.onlinejudge.org/external/5/599.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + int t; + cin>>t; + string s; + getline(cin,s); + while(t--){ + vvi g(26); + while(1){ + getline(cin,s); + if(s[0]=='*')break; + int u=s[1]-'A',v=s[3]-'A'; + g[u].push_back(v); + g[v].push_back(u); + } + getline(cin,s); + vi n(26); + for(int i=0;idfs=[&](int u){ + c[u]=1; + + for(int v:g[u]) + if(!c[v]) + dfs(v); + }; + int a=0,b=0; + for(int i=0;i<26;i++) + if(n[i]&&!c[i]) + if(g[i].empty())a++; + else{ + dfs(i); + b++; + } + cout<<"There are "< + +using namespace std; + +int a[26][26]; +bool b[26][26]; +int ans1, ans2; +string s; + +void f(int i, int j){ + if(!b[i][j] && !b[j][i]) ans1++; + for(int x = 0; x < 26; x++) + if( j != x && a[j][x] == 1 && !b[j][x]){ + b[j][x]=true; + f(j, x); + } +} + +int main(){ + int c, aux1, aux2; + scanf("%d", &c); + while(c--){ + memset(a, 0, sizeof a); + memset(b, false, sizeof b); + while(cin>>s){ + if(s[0]!='*'){ + a[s[1]-'A'][s[3]-'A'] = 1; + a[s[3]-'A'][s[1]-'A'] = 1; + a[s[1]-'A'][s[1]-'A'] = 1; + a[s[3]-'A'][s[3]-'A'] = 1; + } + else break; + } + cin >> s; + aux2 = 0; + aux1 = s.size() - s.size() / 2; + for(int i = 0; i < 26; i++) if(a[i][i]) aux2++; + ans2 = aux1 - aux2; + ans1 = 0; + for(int i = 0; i < 26; i++) + for(int j = 0; j < 26; j++) + if(i != j && a[i][j] == 1) f(i, j); + printf("There are %d tree(s) and %d acorn(s).\n", ans1, ans2); + } + return(0); +} diff --git a/uva_cpp_clean/604/604-9.cpp b/uva_cpp_clean/604/604-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2700e827ad3787e56362a712a543e676cc9c5364 --- /dev/null +++ b/uva_cpp_clean/604/604-9.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +const int N = 4, N_VOWELS = 2; +const char FILLED = '#'; +const string VOWELS = "AEIOUY"; + +string s; +vector grid1, grid2; +set word1, word2; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +void findCommonWords () { + bool ok = false; + for (const string w: word1) { + if (word2.find(w) != end(word2)) { + ok = true; + cout << w << endl; + } + } + if (not ok) puts("There are no common words for this pair of boggle boards."); +} + +bool isWord () { + int nVowels = 0; + for (char ch: s) if (VOWELS.find(ch) != string::npos) nVowels++; + return nVowels == N_VOWELS; +} + +inline bool isValid (const int r, const int c) { + return 0 <= r and 0 <= c and r < N and c < N; +} + +void backtrack (int r, int c, int len, vector & grid, set & word) { + if (len == N) { + if (isWord()) word.insert(s); + return; + } + for (int d = 0; d < 8; d++) { + int nr = r + dr[d]; + int nc = c + dc[d]; + if (isValid(nr, nc) and grid[nr][nc] != FILLED) { + char ch = grid[nr][nc]; + grid[nr][nc] = FILLED; + s += ch; + backtrack(nr, nc, len + 1, grid, word); + grid[nr][nc] = ch; + s.pop_back(); + } + } +} + +void findWords (vector & grid, set & word ) { + for (int r = 0; r < N; r++) { + for (int c = 0; c < N; c++) { + char ch = grid[r][c]; + grid[r][c] = FILLED; + s += ch; + backtrack(r, c, 1, grid, word); + grid[r][c] = ch; + s.pop_back(); + } + } +} + +bool readGrid () { + string line; + getline(cin, line); + if (line[0] == '#') return false; + for (int r = 0; r < 4; r++) { + grid1.push_back(""); + grid2.push_back(""); + istringstream token(line); + char ch; + for (int c = 0; c < 4; c++) { + token >> ch; + grid1[r] += ch; + } + for (int c = 0; c < 4; c++) { + token >> ch; + grid2[r] += ch; + } + getline(cin, line); + } + return true; +} + +void clearCase () { + grid1.clear(), grid2.clear(); + word1.clear(), word2.clear(); +} + +int main () { + bool first = true; + while (true) { + clearCase(); + if (not readGrid()) break; + if (not first) puts(""); + findWords(grid1, word1); + findWords(grid2, word2); + findCommonWords(); + first = false; + } + return (0); +} diff --git a/uva_cpp_clean/607/607-19.cpp b/uva_cpp_clean/607/607-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7748414be6c2c5bfe338cf6c8aaa20124426cf99 --- /dev/null +++ b/uva_cpp_clean/607/607-19.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +using namespace std; + +int L, C, n, counter; + +inline int proc(int t) { + if (t == 0) return t; + if (t <= 10) return -C; + return (t-10)*(t-10); +} + +int v[1002]; +unordered_map m; + +int func(int tid, int day) { + if (day > counter) return 1000000000; + if (tid >= n) return 0; + if (!m.count(tid*10000+day)) { + int acum = 0, mini = 1000000000; + for (int i = tid; i < n; i++) { + acum += v[i]; + if (acum > L) break; + mini = min(mini, proc(L-acum) + func(i+1, day+1)); + } + m[tid*10000+day] = mini; + } + return m[tid*10000+day]; +} + +int main() { + int c = 1; + while (cin >> n && n != 0) { + if (c > 1) printf("\n"); + int acum = 0; + counter = 1; + cin >> L >> C; + m.clear(); + for (int i = 0; i < n; i++) { + int t; + cin >> t; + v[i] = t; + acum += t; + if (acum > L) counter++, acum = t; + } + int minDI = func(0, 0); + printf("Case %d:\n", c); + printf("Minimum number of lectures: %d\n", counter); + printf("Total dissatisfaction index: %d\n", minDI); + c++; + } + return 0; +} diff --git a/uva_cpp_clean/608/608-21.cpp b/uva_cpp_clean/608/608-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..775c4b73bdaf26c04861fd2a15fac8dc4afa2694 --- /dev/null +++ b/uva_cpp_clean/608/608-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 608 + Name: Counterfeit Dollar + Problem: https://onlinejudge.org/external/6/608.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int E[4]; +string L[4], R[4]; +bool Lb[4][26], Rb[4][26]; +bool check(char ch, bool l) { + int up = l ? 1 : -1, down = -up; + for (int i=0; i<3; ++i) { + bool i1 = Lb[i][ch-'A'], i2 = Rb[i][ch-'A']; + if (E[i]==0 && (i1 || i2)) return 0; + if (E[i]==up && (i1 || !i2)) return 0; + if (E[i]==down && (!i1 || i2)) return 0; + } + return 1; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + while (T--) { + memset(Lb, 0, sizeof(Lb)); + memset(Rb, 0, sizeof(Rb)); + for (int i=0; i<3; ++i) { + string ee; + cin >> L[i] >> R[i] >> ee; + E[i] = ee=="even" ? 0 : ee=="up" ? 1 : -1; + for (int j=0; L[i][j]; ++j) + Lb[i][L[i][j]-'A'] = 1, + Rb[i][R[i][j]-'A'] = 1; + } + + for (char ch='A'; ch<='L'; ++ch) + if (check(ch, true)) + cout << ch << " is the counterfeit coin and it is light.\n"; + else if (check(ch, false)) + cout << ch << " is the counterfeit coin and it is heavy.\n"; + } +} diff --git a/uva_cpp_clean/610/610-21.cpp b/uva_cpp_clean/610/610-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..808a1648f76b0468c1c4bafe069a327a2a0a9f9c --- /dev/null +++ b/uva_cpp_clean/610/610-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 610 + Name: Street Directions + Problem: https://onlinejudge.org/external/6/610.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 1001 +list adj[MAXN]; +int low[MAXN], depth[MAXN], parent[MAXN]; + +void print(int u, int v) { + printf("%d %d\n", u+1, v+1); +} + +void dfs(int u, int d) { + low[u] = depth[u] = d; + for (int v: adj[u]) + if (depth[v] == -1) { + print(u, v); + parent[v] = u; + dfs(v, d+1); + if (low[v] >= d+1) + print(v, u); + else if (low[v] < low[u]) + low[u] = low[v]; + } + else if (v != parent[u]) { + low[u] = min(low[u], depth[v]); + if (depth[v] < depth[u]) + print(u, v); + } +} + + +int main(){ + int cse=1, n, m, u, v; + while (cin>>n>>m && (n||m)) { + for (int i=0; i>u>>v; + adj[u-1].push_back(v-1); + adj[v-1].push_back(u-1); + } + + printf("%d\n\n", cse++); + dfs(0, 0); + puts("#"); + } +} diff --git a/uva_cpp_clean/610/610-6.cc b/uva_cpp_clean/610/610-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..5120c76d1e9bcc3a69468fec9ad1a79ff55d284b --- /dev/null +++ b/uva_cpp_clean/610/610-6.cc @@ -0,0 +1,46 @@ +// https://uva.onlinejudge.org/external/6/610.pdf +#include +using namespace std; +using ii=tuple; +using vi=vector; +using vii=vector; +using vvi=vector; +int main(){ + for(int T=1;;T++){ + int n,m,u,v; + cin>>n>>m; + if(!n)break; + vvi g(n); + for(int i=0;i>u>>v; + u--;v--; + g[u].push_back(v); + g[v].push_back(u); + } + vi a(n),b(n); + vii e; + int k=1; + functiondfs=[&](int u,int p){ + a[u]=k++; + b[u]=a[u]; + for(int v:g[u]){ + if(!a[v]||(v!=p&&a[v]a[u])e.emplace_back(v,u); + }else if(v!=p)b[u]=min(b[u],a[v]); + } + }; + for(int i=0;i + +using namespace std; + +const int MAX_N = 1010; + +int tc, V, E, a, b, dfsCounter, dfs_low[MAX_N], dfs_num[MAX_N], dfs_parent[MAX_N]; +bool path[MAX_N][MAX_N]; +vector < int > AdjList[MAX_N]; + +void dfs(int u){ + dfs_low[u] = dfs_num[u] = ++dfsCounter; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not dfs_num[v] ){ + dfs_parent[v] = u; + dfs(v); + path[u][v] = true; + if( dfs_low[v] > dfs_num[u] ) path[v][u] = true; + dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + else if( v != dfs_parent[u] ) + path[v][u] = true, path[u][v] = false, dfs_low[u] = min(dfs_low[u], dfs_num[v]); + } +} + +void Bridges(){ + fill(dfs_low, dfs_low + V + 1, 0); + fill(dfs_num, dfs_num + V + 1, 0); + fill(dfs_parent, dfs_parent + V + 1, -1); + dfsCounter = 0; + dfs(1); +} + +void solve(){ + Bridges(); + printf("%d\n\n", ++tc); + for(int u = 1; u <= V; u++) + for(int v = 1; v <= V; v++) + if( path[u][v] ) printf("%d %d\n", u, v); + puts("#"); +} + +void read(){ + for(int i = 0; i < E; i++){ + scanf("%d %d", &a, &b); + AdjList[a].push_back(b); + AdjList[b].push_back(a); + } +} + +void clear(){ + fill(path[0], path[V + 1], false); + for(int u = 1; u <= V; u++) AdjList[u].clear(); +} + +int main(){ + while(scanf("%d %d", &V, &E), V | E){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/612/612-14.cpp b/uva_cpp_clean/612/612-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..86bdc0f387a8e9dfe7bdbfbd8884d3c41f57e359 --- /dev/null +++ b/uva_cpp_clean/612/612-14.cpp @@ -0,0 +1,86 @@ +/*************************************************** + * Problem name : 612 - DNA Sorting.cpp + * OJ : Uva + * Verdict : AC + * Date : 09.06.2017 + * Problem Type : AdHOc(Sorting) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 103 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) + return b; + return gcd(b, a % b); +} +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +int main () { + int tc, n, m, sum[MAX]; + string str[MAX]; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d %d", &n, &m); + for (int i = 0; i < m; i++) { + cin >> str[i]; + } + for (int k = 0; k < m; k++) { + int cnt = 0; + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (str[k][i] > str[k][j]) cnt++; + } + } + sum[k] = cnt; + } + //~ debug; + vectorar; + for(int i = 0; i +#include +#include +#include + +using namespace std; + +bool compare(const pair &a, const pair &b) { + return a.second < b.second; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_case, n_dna, i, j, dna_length, unsortedness; + + string str; + cin >> n_case; + while (n_case--) { + cin >> dna_length >> n_dna; + vector> dna; + dna.reserve(n_dna); + + while (n_dna--) { + unsortedness = 0; + cin >> str; + + for (i = 0; i < dna_length; i++) + for (j = i + 1; j < dna_length; j++) + if (str[i] > str[j]) + unsortedness++; + + dna.push_back(make_pair(str, unsortedness)); + } + + stable_sort(dna.begin(), dna.end(), compare); + + for (const pair &dna_data : dna) + cout << dna_data.first << "\n"; + + if (n_case) + cout << "\n"; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/612/612-19.cpp b/uva_cpp_clean/612/612-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c1c843ed2909badd2a1d0433ee30ddcac5534be --- /dev/null +++ b/uva_cpp_clean/612/612-19.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef pair iis; + +set st; + +inline int proc(string s) { + int ret = 0; + for (int i = 0; i < s.length(); i++) { + for (int j = i + 1; j < s.length(); j++) { + if (s[i] > s[j]) ret++; + } + } + return ret; +} + +int main() { + int M; + cin >> M; + while (M--) { + int n, m; + cin >> n >> m; + st.clear(); + for (int i = 0; i < m; i++) { + string s; + cin >> s; + st.insert(iis(ii(proc(s), i), s)); + } + for (auto it = st.begin(); it != st.end(); ++it) { + printf("%s\n", it->second.c_str()); + } + if (M) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/612/612-21.cpp b/uva_cpp_clean/612/612-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cd640a0b8432333de70908f9a87a0ddfb1a7228 --- /dev/null +++ b/uva_cpp_clean/612/612-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 612 + Name: DNA Sorting + Problem: https://onlinejudge.org/external/6/612.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +struct DNA { + char seq[55]; + int score; + bool operator <(const DNA& d) const { + return score < d.score; + } +}; + +DNA ar[110]; + + +int main(){ + int cse, n, m; + int A,C,G,T, score; + + cin>>cse; + while(cse--) { + cin>>n>>m; + for (int i=0; i>s; + A=C=G=T=0; + score = 0; + for (int i=0; i + +using namespace std; + +const int LEN = 55, SIZE = 105; + +int tc, n, m; + +struct DNA{ + char word[LEN]; + int measure; + void unsortedness(){ + measure = 0; + for(int i = 0; i < m; i++) + for(int j = 0; j < i; j++) + if( word[j] > word[i] ) measure++; + } + bool operator < (const DNA& other) const{ + return measure < other.measure; + } +}v[SIZE]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d\n", &m, &n); + for(int it = 0; it < n; it++){ + scanf("%s", v[it].word); + v[it].unsortedness(); + } + stable_sort(v, v + n); + for(int it = 0; it < n; it++) + printf("%s\n", v[it].word); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/614/614-21.cpp b/uva_cpp_clean/614/614-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d221fc56e089a129f2c208a4bd74cac9cc1fe786 --- /dev/null +++ b/uva_cpp_clean/614/614-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 614 + Name: Mapping the Route + Problem: https://onlinejudge.org/external/6/614.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int h, w, gy, gx, M[20][20], R[20][20]; +bool dfs(int x, int y, int d) { + if (R[y][x] >= 0) + return false; + + R[y][x] = d; + if (x == gx && y == gy) + return true; + + // Move Left + if (x>0 && !(M[y][x-1]&1)) + if (dfs(x-1, y, d+1)) + return true; + + // Move Up + if (y>0 && !(M[y-1][x]&2)) + if (dfs(x, y-1, d+1)) + return true; + + // Move Right + if (!(M[y][x]&1)) + if (dfs(x+1, y, d+1)) + return true; + + // Move Down + if (!(M[y][x]&2)) + if (dfs(x, y+1, d+1)) + return true; + + return R[y][x] = 0; +} + +int main(){ + int cse=1, sx, sy; + while (cin>>h>>w>>sy>>sx>>gy>>gx && (h||w)) { + for (int i=0; i>M[i][j]; + + R[i][j] = -1; + if (i==h-1) + M[i][j] |= 2; + } + M[i][w-1] |= 1; + } + + gx--; gy--; + dfs(sx-1, sy-1, 1); + + printf("Maze %d\n\n", cse++); + for (int i=0; i +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + for(int t=0;;t++){ + int n,m,a,b,c,d; + cin>>n>>m>>a>>b>>c>>d; + if(!n)break; + a--;b--;c--;d--; + vvi e(n,vi(m)),f(n,vi(m)); + for(int i=0;i>e[i][j]; + functiondfs=[&](int i,int j,int k){ + f[i][j]=k; + if(i==c&&j==d)return 1; + int y[]={0,-1,0,1}; + int x[]={-1,0,1,0}; + for(int l=0;l<4;l++){ + if(l==0&&(!j||e[i][j-1]&1))continue; + else if(l==1&&(!i||e[i-1][j]&2))continue; + else if(l==2&&(e[i][j]&1))continue; + else if(l==3&&(e[i][j]&2))continue; + int u=i+y[l],v=j+x[l]; + if(u>=0&&u=0&&v0)printf("% 3d",f[i][j]); + else if(f[i][j]<0)printf("???"); + else printf(" "); + } + cout<<"|\n"; + } + for(int j=0;j +#include +#include +#include +#include + +using namespace std; + +vector graph[1000]; +map inDegree; +map newLabel; +int visited[1000], V = 0, root, specialCase; + +void DFS(int node){ + visited[node] = true; + for(int i = 0; i < graph[node].size(); i++) if(!visited[graph[node][i]]) DFS(graph[node][i]); +} + +void cleanAll(){ + for(int i = 0; i < 1000; i++) graph[i].clear(); + inDegree.clear(); + newLabel.clear(); + V = 0; + specialCase = 1; + memset(visited, false, sizeof(visited)); +} + +bool isNotTree(){ + int noRoot = 0; + bool oneInDegree = true; + for(map::iterator it = inDegree.begin (); it != inDegree.end (); it++){ + if((*it).second == 0) root = (*it).first, noRoot++; + else if((*it).second > 1) oneInDegree = false; + } + return noRoot > 1 || noRoot == 0 || !oneInDegree; +} + +bool isConnected(){ + DFS(root); + for(int i = 1; i < V; i++) if(!visited[i]) return false; + return true; +} + +int main(){ + int src, fnal, tstCase; + while(scanf("%d %d", &src, &fnal)) + { + if(src < 0 && fnal < 0) break; + if(src == 0 && fnal == 0){ + tstCase++; + if(specialCase){ //src == 0 && fnal == 0 as unique case + printf("Case %d is a tree.\n", tstCase); + continue; + } + if(isNotTree()){ + printf("Case %d is not a tree.\n", tstCase); + cleanAll(); + continue; + } + if(!isConnected()) printf("Case %d is not a tree.\n", tstCase); + else printf("Case %d is a tree.\n", tstCase); + cleanAll(); + } else{ + if(!newLabel[src]) V++, newLabel[src] = V, inDegree[newLabel[src]] = 0; + if(!newLabel[fnal]) V++, newLabel[fnal] = V, inDegree[newLabel[fnal]] = 0; + graph[newLabel[src]].push_back(newLabel[fnal]); + inDegree[newLabel[fnal]]++; + specialCase = 0; + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/615/615-21.cpp b/uva_cpp_clean/615/615-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3dd50a4edf8325247dcc8b9bd4958521125ffd0c --- /dev/null +++ b/uva_cpp_clean/615/615-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 615 + Name: Is It A Tree? + Problem: https://onlinejudge.org/external/6/615.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +list adj[100]; +bool inside[100]; +int dfs(int u) { + inside[u] = 1; + int sum = 1; + for (int v: adj[u]) + if (!inside[v]) + sum += dfs(v); + inside[u] = 0; + return sum; +} + +int vn; +unordered_map ids; +int getId(int x) { + auto it = ids.find(x); + if (it != ids.end()) + return it->second; + + adj[vn].clear(); + return ids[x] = vn++; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; ; ++cse) { + ids.clear(); + int en=vn=0; + bool ins[100]={}, ist=1; + for (int u, v; cin>>u>>v && (u||v); ++en) { + if (u<0 || v<0) return 0; + if (u == v) ist=0; + if (ist) { + v = getId(v); + if (ins[v]) ist = 0; + else { + ins[v] = 1; + adj[getId(u)].push_back(v); + } + } + } + + if (vn && en!=vn-1) ist = 0; + for (int i=0; ist && i +/* +Problem: 616 - Coconuts, Revisited +Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=557 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N, ans; + +int main () { + + while ( scanf("%d\n", &N) && N >= 0 ){ + + ans = 0; + + for (int i = ceil(sqrt((double)N)); i > 1; --i){ + + int K = N, j = 0; + for (j = 0; j < i; ++j){ + K -= 1; + if (K%i != 0) break; + K -= K/i; + } + + if (j == i && K%i == 0) { + ans = i; + break; + } + } + + if (ans) printf("%d coconuts, %d people and 1 monkey\n", N, ans); + else printf("%d coconuts, no solution\n", N); + + } + + return 0; +} +/* +Sample input:- +----------------- +25 +30 +3121 +-1 + +Sample output:- +----------------- +25 coconuts, 3 people and 1 monkey +30 coconuts, no solution +3121 coconuts, 5 people and 1 monkey + +Resources:- +------------- + + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/616/616-19.cpp b/uva_cpp_clean/616/616-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cac6d3bf687a84b8e3f1eb4f1ffc4fc5b2489d3 --- /dev/null +++ b/uva_cpp_clean/616/616-19.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int main() { + int n; + while (scanf("%d", &n) && n >= 0) { + int s = ceil(sqrt(n)); + int sol = -1; + for (int i = 2; i <= s; i++) { + bool ok = true; + int val = n, cont = 0; + while (ok) { + if (val % i == 0) break; + else if ((val - 1) % i == 0) val = val - 1 - ((val - 1)/i); + else ok = false; + cont++; + } + if (ok && cont == i) sol = i; + } + if (sol != -1) printf("%d coconuts, %d people and 1 monkey\n", n, sol); + else printf("%d coconuts, no solution\n", n); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/616/616-21.cpp b/uva_cpp_clean/616/616-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf3955e56525ffb9f12ac576332e3eeef21766c5 --- /dev/null +++ b/uva_cpp_clean/616/616-21.cpp @@ -0,0 +1,40 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 616 + Name: Coconuts, Revisited + Problem: https://onlinejudge.org/external/6/616.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool check(int n, int p) { + for (int i=0; i> n && n>=0) { + for (int i=21; i>0; --i) + if (check(n, i)) { + printf("%d coconuts, %d people and 1 monkey\n", n, i); + goto fin; + } + printf("%d coconuts, no solution\n", n); + fin:; + } +} diff --git a/uva_cpp_clean/616/616-4.cpp b/uva_cpp_clean/616/616-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9137f3986154c3b8620fe291398f2fa2eea1fd94 --- /dev/null +++ b/uva_cpp_clean/616/616-4.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int main() +{ + int coconuts; + + while (true) + { + cin >> coconuts; + if (coconuts < 0) + break; + + if (coconuts < 2) + { + cout << coconuts << " coconuts, no solution" << endl; + continue; + } + else + { + bool done = false; + int start = coconuts - 1; + if (start > 20) + start = 20; + + for (int people = start; people > 1; people--) + { + int temp = coconuts; + if (temp % people > 1) + continue; + + bool ok = true; + for (int i = 0; i < people; i++) + { + --temp; + if (temp % people != 0) + { + ok = false; + break; + } + + temp = temp - (temp / people); + } + if (temp % people != 0) + ok = false; + + if (ok) + { + cout << coconuts << " coconuts, " << people << " people and 1 monkey" << endl; + done = true; + break; + } + } + + if (!done) + cout << coconuts << " coconuts, no solution" << endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/617/617-21.cpp b/uva_cpp_clean/617/617-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26c59532681763bc4ac511ca6f7d793cc8e99020 --- /dev/null +++ b/uva_cpp_clean/617/617-21.cpp @@ -0,0 +1,61 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 617 + Name: Nonstop Travel + Problem: https://onlinejudge.org/external/6/617.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool F[61]; +double D[7]; +int G[7], Y[7], R[7], S[7], n; +bool check(int s) { + if (s > 60) return 0; + for (int i=0; i G[i] + Y[i]) + return 0; + } + return 1; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; cin >> n && n>-1; ++cse) { + for (int i=0; i> D[i] >> G[i] >> Y[i] >> R[i]; + S[i] = G[i] + Y[i] + R[i]; + } + + cout << "Case " << cse << ": "; + int l = 0; + bool some = 0; + for (int s=30; s<=61; ++s) { + bool fs = check(s); + if (fs) { + if (!l) l = s; + } + else if (l) { + if (!some) some = 1; + else cout << ", "; + + cout << l; + if (l < s-1) + cout << '-' << s-1; + l = 0; + } + } + if (!some) + cout << "No acceptable speeds."; + cout << "\n"; + } +} diff --git a/uva_cpp_clean/617/617-9.cpp b/uva_cpp_clean/617/617-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60ace0755fa6509795750b4bd056668b03741c1f --- /dev/null +++ b/uva_cpp_clean/617/617-9.cpp @@ -0,0 +1,41 @@ +#include + +#define MAX 6 + +using namespace std; + +double L[MAX], tmp; +int G[MAX], Y[MAX], R[MAX], tc, pib, N; +vector v; + +bool isGreen(double time, int p){ + tmp = fmod(time, R[p] + Y[p] + G[p]); + if(tmp <= G[p] + Y[p]) return true; + return false; +} + +bool isValid(int vel){ + for(int i = 0; i < N; i++) + if(!isGreen(3600 * L[i] / vel, i)) + return false; + return true; +} + +int main(){ + while(scanf("%d", &N), N != -1){ + v.clear(); + for(int i = 0; i < N; i++) scanf("%lf %d %d %d", &L[i], &G[i], &Y[i], &R[i]); + for(int i = 30; i <= 60; i++) + if(isValid(i)) v.push_back(i); + printf("Case %d:", ++tc); + for(int i = 0; i < v.size(); i++){ + printf(" %d", v[i]), pib = i; + while(v[i + 1] - v[i] == 1) i++; + if(i != pib) printf("-%d", v[i]); + if(i != v.size() - 1) putchar(','); + } + if(!v.size()) printf(" No acceptable speeds.\n"); + else putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/619/619-21.cpp b/uva_cpp_clean/619/619-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a688914914f84a6e7b61d79e8a653b4210765bfd --- /dev/null +++ b/uva_cpp_clean/619/619-21.cpp @@ -0,0 +1,385 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 619 + Name: Numerically Speaking + Problem: https://onlinejudge.org/external/6/619.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + + if (cells) delete [] cells; + cells = ncells; + } + if (!cp) + memset(cells, 0, capacity*CellSize); + else memset(cells+size, 0, (capacity-size)*CellSize); + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + // --------------------------------------------------------------------- + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + + BigInt(const char s[]) { + while (s[0]==' ' || s[0]=='\n' || s[0]=='\t') s++; + sign = 1; + if (s[0] == '+') s++; + if (s[0] == '-') { sign=-1; s++; } + while (s[0]=='0') s++; + if (!s[0]) { init(); return; } + + int len = strlen(s); + size = (len+3)/MAX_D; + cells = new CellType[capacity = size+16]; + for (int i=len-1; i>=0; i-=MAX_D) { + int rc = 0; + for (int k=i-MAX_D+1; k<=i; k++) { + if (k<0 || s[k]=='-' || s[k]=='+') continue; + rc = rc*10 + s[k] - '0'; + } + cells[(len-i-1) / MAX_D] = rc; + } + for (int i=size; i= 0) + delete [] cells; + } + + int getSize() { return size; } + int getCell(int i) { return cells[i]; } + bool isZero() { return !sign; } + // --------------------------------------------------------------------- + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + bool numEq(const BigInt &o) const { + if (size!=o.size) + return false; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return false; + return true; + } + + public: + bool operator < (const BigInt &o) const { + return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign; + } + bool operator == (const BigInt &o) const { return sign==o.sign && numEq(o); } + bool operator != (const BigInt &o) const { return !(*this == o); } + bool operator <= (const BigInt &o) const { return !(o < *this); } + bool operator > (const BigInt &o) const { return (o < *this); } + bool operator >= (const BigInt &o) const { return !(*this < o); } + // --------------------------------------------------------------------- + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator+= (BigInt o) { + if (!o.sign) return *this; + if (!sign) sign = o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator-=(o); + o.sign *= -1; + return *this; + } + + int osz = size; + setSize(max(size, o.size)+1); + for (int i=osz; i= MAX_C) { + cells[i] -= MAX_C; + ++cells[i+1]; + } + } + return autoSize(); + } + BigInt& operator+= (int x) { return *this += *new BigInt(x); } //TODO: optimize + BigInt operator+ (int x) const { return *new BigInt(*this) += x; } + BigInt operator+ (BigInt& o) const { return *new BigInt(*this) += o; } + + // --------------------------------------------------------------------- + + BigInt& operator-= (BigInt& o) { + if (!o.sign) return *this; + if (!sign) sign = -o.sign; + if (sign != o.sign) { + o.sign *= -1; + operator+=(o); + o.sign *= -1; + return *this; + } + + if (*this == o) { + sign = size = 0; + return *this; + } + if (!o.numLess(*this)) + return *this = -(o - *this); + + for (int i=0; i=0; --i) { + t = t*MAX_C + cells[i]; + cells[i] = t / den; + t -= cells[i] * den; + } + return autoSize(); + } + BigInt operator/ (int den) const { return *new BigInt(*this) *= den; } + + int operator% (int den) const { + int r = 0; + for (int i = size-1; i>=0; --i) + r = (r * MAX_C + cells[i]) % den; + return r; + } + + // --------------------------------------------------------------------- + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; +BigInt operator*(int m, BigInt &n) { return n * m; } +BigInt operator+(int m, BigInt &n) { return n + m; } +BigInt operator-(int m, BigInt &n) { return -n + m; } + +// tostring, input from char + +bool powseen[10][10000]; +BigInt pmemo[10][10000]; +BigInt pow(int a, int b) { + if (!b) return BigInt(1); + if (b==1 || a<=1) + return BigInt(a); + + if (a<10 && b<10000 && powseen[a][b]) + return pmemo[a][b]; + + BigInt c = pow(a, b>>1); + c *= c; + if (b&1) c *= a; + + if (a<10 && b<10000) { + powseen[a][b] = 1; + pmemo[a][b] = c; + } + return c; +} + +BigInt pow(int a, BigInt b) { + if (b.getSize() <= 1) + return pow(a, b.getCell(0)); + + bool has2 = b % 2; + b /= 2; + BigInt c = pow(a, b); + c *= c; + if (b%2) c*= a; + + return c; +} + + +char str[10000], s2[10000]; +void printSep(const char s[]) { + int l = strlen(s); + for (int i=0; s[i]; i++) { + if (i && (l-i)%3==0) + putchar(','); + putchar(s[i]); + } + putchar('\n'); +} + +int main() { + while (scanf("%s", str)==1 && str[0]!='*') { + if (str[0] < 60) { + char result[100000]; + BigInt b(str); + int i=5000; + while (!b.isZero()) { + b -= 1; + s2[--i] = 'a' + (b%26); + b /= 26; + } + printf("%-22s", s2+i); + printSep(str); + } + else { + BigInt b(0); + for (int i=0; str[i]; i++) { + b *= 26; + b += str[i]-'a' + 1; + } + ostringstream os; + os << b; + printf("%-22s", str); + printSep(os.str().c_str()); + } + } +} diff --git a/uva_cpp_clean/621/621-14.cpp b/uva_cpp_clean/621/621-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c0885ffec8b902c74457d7a43111800ff07099a0 --- /dev/null +++ b/uva_cpp_clean/621/621-14.cpp @@ -0,0 +1,49 @@ +/*************************************************** + * Problem name : 621 Secret Research.cpp + * OJ : Uva + * Result : AC + * Date : 01-05-2017 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define MAX 10000 +using namespace std; +typedef long long ll; +int main () { + int tc; + string s; + scanf("%d", &tc); + for(int t = 1; t<=tc; t++){ + cin >> s; + int sz = s.size(); + if((s[0]-'0' == 1 && sz ==1) || (s[0]-'0' == 4 && sz ==1) || (sz == 2 && s[0]-'0' == 7 && s[1]-'0' == 8)){ + printf("+\n"); + } + else if(s[sz-1] == '5' && s[sz-2] == '3'){ + printf("-\n"); + } + else if(s[0] == '9' && s[sz-1] == '4'){ + printf("*\n"); + } + else{ + printf("?\n"); + } + } + return 0; +} + diff --git a/uva_cpp_clean/621/621-18.cpp b/uva_cpp_clean/621/621-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b6c78ec691601477baf9b05a93d495b5440d7c8f --- /dev/null +++ b/uva_cpp_clean/621/621-18.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +int main(){ + string input; + int N; + cin >> N; + getline(cin, input); //New line + while(N-- > 0){ + getline(cin, input); + if(input == "1" || input == "4" || input == "78") printf("+\n"); + else if(input.substr(input.size()-2, 2) == "35") printf("-\n"); + else if(input[0] == '9' && input[input.size() - 1] == '4') printf("*\n"); + else printf("?\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/621/621-19.cpp b/uva_cpp_clean/621/621-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45b702cc057849879cf1e6ff7e3ed5c33ae1a7db --- /dev/null +++ b/uva_cpp_clean/621/621-19.cpp @@ -0,0 +1,17 @@ +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + while (n--) { + string s; + cin >> s; + if (s == "1" || s == "4" || s == "78") printf("+\n"); + else if (s[s.length()-2] == '3' && s[s.length()-1] == '5') printf("-\n"); + else if (s[0] == '9' && s[s.length()-1] == '4') printf("*\n"); + else /*if (s[0] == '1' && s[1] == '9' && s[2] == '0')*/ printf("?\n"); + } + return 0; +} diff --git a/uva_cpp_clean/621/621-2.cpp b/uva_cpp_clean/621/621-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a9397f6bfb372c6852faeda6acafd5edf5c370a --- /dev/null +++ b/uva_cpp_clean/621/621-2.cpp @@ -0,0 +1,59 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; cin >> tc; + while (tc--) { + string s; cin >> s; + if (s == "4" || s == "78" || s == "1") { + cout << "+" << el; + continue; + } + int si = (int) s.size(); + if (s[si-1] == '5' && s[si-2] == '3') { + cout << "-" << el; + continue; + } + if (si >= 3) + if (s[0] == '1' && s[1] == '9' && s[2] == '0') { + cout << "?" << el; + continue; + } + if (s[0] == '9' && s[si-1] == '4') { + cout << "*" << el; + continue; + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/621/621-21.cpp b/uva_cpp_clean/621/621-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df21f8f10e4a18dcdbdb48f5bcd37c164da4be3f --- /dev/null +++ b/uva_cpp_clean/621/621-21.cpp @@ -0,0 +1,37 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 621 + Name: Secret Research + Problem: https://onlinejudge.org/external/6/621.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +char line[10000]; +int main() { + int n; + cin>>n; + cin.getline(line,10); + while(n--) { + cin.getline(line, 10000); + int l = strlen(line); + if (l==1 || (l==2 && line[0]=='7' && line[1]=='8')) + cout<<"+\n"; + else if(l>1 && line[l-2]=='3' && line[l-1]=='5') + cout<<"-\n"; + else if(l>1 && line[0]=='9' && line[l-1]=='4') + cout<<"*\n"; + else if(l>2 && line[0]=='1' && line[1]=='9' && line[2]=='0') + cout<<"?\n"; + else cout<<"+\n"; + } +} diff --git a/uva_cpp_clean/621/621-5.cpp b/uva_cpp_clean/621/621-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b77cc6fbed930c02d648caf682ddc184d4c434d5 --- /dev/null +++ b/uva_cpp_clean/621/621-5.cpp @@ -0,0 +1,26 @@ +#include +#include +int main() +{ + int n,l; + char s[10000]; + while(scanf("%d",&n)==1) + { + while(n--) + { + scanf("%s",&s); + l=strlen(s); + if (strcmp(s,"1")==0||strcmp(s,"4")==0||strcmp(s,"78")==0) + printf("+\n"); + else if (s[l-2]=='3'&&s[l-1]=='5') + printf("-\n"); + else if (s[0]=='9'&&s[l-1]=='4') + printf("*\n"); + else if (s[0]=='1'&&s[1]=='9'&&s[2]=='0') + printf("?\n"); + + + } + } + return 0; +} diff --git a/uva_cpp_clean/621/621-9.cpp b/uva_cpp_clean/621/621-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42e5ae42bfafff899ca38adf3548359e273e9bd4 --- /dev/null +++ b/uva_cpp_clean/621/621-9.cpp @@ -0,0 +1,21 @@ +#include +#include + +int main(){ + int n, a, tam, a1, b, c; + scanf("%d", &n); + while(n--){ + scanf("%d", &a); + a1 = a; + tam = 0; + while( a1 != 0 ) a1 /= 10, tam++; + b = pow( 10, tam - 1); + c = pow( 10, tam - 3); + if( a == 1 || a == 4 || a == 78 ) printf("+\n"); + else if( a % 100 == 35 ) printf("-\n"); + else if (a % 10 == 4 && a / b == 9 ) printf("*\n"); + else if( tam > 3 && a / c == 190 ) printf("?\n"); + else printf("+\n"); + } + return(0); +} diff --git a/uva_cpp_clean/621/main.cpp b/uva_cpp_clean/621/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..148d558026d6a41942e5ee231257ae303af21479 --- /dev/null +++ b/uva_cpp_clean/621/main.cpp @@ -0,0 +1,31 @@ +#include +#include +using namespace std; + +int main(){ + int n; + string experiment; + for(cin>>n; n>0; n--){ + cin >> experiment; + if (experiment.size() < 3) cout << '+' << endl; + else{ + string s = experiment.substr(experiment.size()-2, 2); + string saux; + if(s == "35") cout << '-' << endl; + else{ + s = s[1]; + saux = experiment[0]; + if(saux == "9" && s == "4") cout << '*' << endl; + else{ + s = experiment.substr(0,3); + if(s == "190") cout << '?' << endl; + else cout << '?' << endl; + } + } + } + } + + + +return 0; +} diff --git a/uva_cpp_clean/622/622-19.cpp b/uva_cpp_clean/622/622-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..469c5a915e5d1ba1b0ee9c0740cdf7e74bad7bfc --- /dev/null +++ b/uva_cpp_clean/622/622-19.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +using namespace std; + +int proc(string expression, bool & ok) { + if (expression.length() == 0) { + ok = false; + return 0; + } + int val = 0, op1, fact, j; + bool empty = true, op = false; + for (int i = 0; i < expression.length(); i++) { + if(isdigit(expression[i])) { + j = 1; + for (; j+i < expression.length() && isdigit(expression[j+i]); j++); + fact = atoi((expression.substr(i, j)).c_str()); + j--; + } else if(expression[i] == '(') { + int open = 0; + j = 1; + for (; j+i < expression.length() && !(expression[j+i] == ')' && open == 0); j++) { + if (expression[j+i] == '(') open++; + else if (expression[j+i] == ')') open--; + } + fact = proc(expression.substr(i+1, j-1), ok); + } else { + ok = false; + break; + } + if (!op) op1 = fact; + else op1 *= fact; + i += j + 1; + if (i < expression.length()) { + if (expression[i] == '+') val += op1, op = false; + else if (expression[i] == '*') op = true; + else { + ok = false; + break; + } + if (i + 1 >= expression.length()) ok = false; + } else val += op1; + } + return val; +} + +int main() { + int N; + string expression; + cin >> N; + getline(cin, expression); + while (N--) { + getline(cin, expression); + bool ok = true; + int res = proc(expression, ok); + if (ok) cout << res << endl; + else cout << "ERROR" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/623/623-20.cpp b/uva_cpp_clean/623/623-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3548b48893ebcd32d9ed0bce5075b50d85f67dde --- /dev/null +++ b/uva_cpp_clean/623/623-20.cpp @@ -0,0 +1,32 @@ +#include + +int factorial[1005][3000]; + +int main() +{ + factorial[0][0] = factorial[1][0] = 1; + for(int i=2 ; i<1005 ; i++) + for(int j=0 ; j<3000 ; j++) + { + factorial[i][j] += factorial[i-1][j]*i; + if( factorial[i][j]>9 ) + { + factorial[i][j+1] += factorial[i][j]/10; + factorial[i][j] %= 10; + } + } + + int n; + + while( scanf("%d", &n)==1 ) + { + int pos; + for(pos=3000-1 ; pos>0 ; pos--) + if( factorial[n][pos]!=0 ) break; + + printf("%d!\n", n); + for( ; pos>=0 ; pos--) printf("%d", factorial[n][pos]); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/623/623-21.cpp b/uva_cpp_clean/623/623-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e5946df0ed4415e2f34b367dcd8b32ad981a0b2 --- /dev/null +++ b/uva_cpp_clean/623/623-21.cpp @@ -0,0 +1,94 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 623 + Name: 500! + Problem: https://onlinejudge.org/external/6/623.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +class largeint{ +public: + short int *nums; + int n_size; + #define nlen nums[0] + largeint(int n=2580){ + int i; + nums=(short int *)malloc((n+1)*sizeof(short int)); + memset(nums,0,(n+1)*sizeof(short int)); + n_size=n; + nums[0]=0; + } + ~largeint(){ + free(nums); + } + + void print(){ + if(nlen<0)cout<<'-'; + for(int i=abs(nlen);i>0;i--)cout<0;i--); + nlen=i*s; + } + + largeint& operator=(const largeint &b){ + int i; + for(i=0;i<=abs(b.nlen);i++)nums[i]=b.nums[i]; + return *this; + } + largeint& operator=(const char* b){ + nlen=strlen(b); + for(int i=0;b[i];i++)nums[nlen-i]=b[i]-48; + return *this; + } + + largeint& operator*=(const largeint &b){ + int i,j; + largeint c(n_size); + for(i=1;i<=nlen;i++) + for(j=1;j<=b.nlen;j++) + c.nums[i+j-1]+=nums[i]*b.nums[j]; + + for(i=1;i<=nlen+b.nlen;i++){ + c.nums[i+1]+=c.nums[i]/10; + c.nums[i]%=10; + } + c.fixlen((nlen*b.nlen)/abs(nlen*b.nlen)); + *this=c; + return *this; + } +}; + +int main(){ + register int n,i; + char kk[5]; + largeint sa[1001],p,p2(5); + sa[0]=p="1"; + for(i=1;i<=1000;i++) + { + sprintf(kk,"%d",i); + p2=kk; + p*=p2; + sa[i]=p; + } + while(scanf("%d",&n)==1){ + printf("%d!\n",n); + sa[n].print(); + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/623/623-9.cpp b/uva_cpp_clean/623/623-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82293ce6219c2c4d920f7549cf6df9b79f3eba6f --- /dev/null +++ b/uva_cpp_clean/623/623-9.cpp @@ -0,0 +1,17 @@ +def solution(): + + SIZE = 1010 + factorial, ac = [1], 1 + for it in range(1,SIZE): + ac *= it + factorial.append(ac) + + while True: + try: + n = int(input()) + print("%d!\n%d" %(n, factorial[n])) + except EOFError: + break + +if __name__=="__main__": + solution() diff --git a/uva_cpp_clean/624/624-13.cpp b/uva_cpp_clean/624/624-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa7bd7611f306dc00e96ae95ba3ee0968180eae0 --- /dev/null +++ b/uva_cpp_clean/624/624-13.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int N, t, bitmask = 0, ans = 0, ones; + + while (cin >> N >> t) + { + bitmask = ans = ones = 0; + + int* value = new int[t]; + + for (int i = 0; i < t; i++) + { + cin >> value[i]; + } + + for (int i = 0; i < (1 << t); i++) + { + int index = 0, temp = i, sum = 0, how = 0; + + while (temp) + { + if (temp % 2) + { + how++; + sum += value[t - index - 1]; + } + + index++; + + temp /= 2; + } + + if ((sum > ans && sum <= N) || (sum == ans && how >= ones && sum <= N)) + { + bitmask = i; + + ones = how; + + ans = sum; + } + } + + int index = 0; + stack s; + + while (bitmask) + { + if (bitmask % 2) + { + s.push(value[t - index - 1]); + } + + index++; + + bitmask /= 2; + } + + while (!s.empty()) + { + cout << s.top() << " "; + s.pop(); + } + + cout << "sum:" << ans << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/624/624-14.cpp b/uva_cpp_clean/624/624-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f5327be6ff6f47bbe1b687b346f5453bd4baf08 --- /dev/null +++ b/uva_cpp_clean/624/624-14.cpp @@ -0,0 +1,142 @@ +/*************************************************** + * Problem Name : 624 CD.cpp + * Problem Link : https://uva.onlinejudge.org/external/6/624.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-07-08 + * Problem Type : adHoc(but I did DP) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int ar[23]; +int Sum, n; +int dp[25][1005]; +int dp2[25][1005]; +int total; +int fun (int i, int sum) { + if (i >= n) { + return sum; + } + + if (dp[i][sum] != -1) { + return dp[i][sum]; + } + + int res1 = 0, res2 = 0; + + if (sum + ar[i] <= Sum) { + res1 = fun (i + 1, sum + ar[i]); + } + + res2 = fun (i + 1, sum); + return dp[i][sum] = max (res1, res2); +} +int path (int i, int sum) { + if (i >= n) { + if (sum == total) { + return true; + + } else { + return false; + } + } + + if (dp2[i][sum] != -1) { + return dp2[i][sum]; + } + + int res1 = 0, res2 = 0; + + if (sum + ar[i] <= total) { + res1 = path (i + 1, sum + ar[i]); + } + + res2 = path (i + 1, sum); + return dp2[i][sum] = res1 | res2; +} + +int main () { + __FastIO; + + while (cin >> Sum) { + cin >> n; + + for (int i = 0; i < n; i++) { + cin >> ar[i]; + } + + SET (dp, -1); + int sum = fun (0, 0); + total = sum; + + for (int i = 0; i < n; i++) { + SET (dp2, -1); + bool x = path (i + 1, 0); + + if (!x) { + cout << ar[i] << " "; + total -= ar[i]; + } + + if (total <= 0) break; + } + + cout << "sum:" << sum << "\n"; + } + + return 0; +} diff --git a/uva_cpp_clean/624/624-19.cpp b/uva_cpp_clean/624/624-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0da85deb4bbeffc2749d0d88aa81a35a3910dff2 --- /dev/null +++ b/uva_cpp_clean/624/624-19.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +bool solopt[20], sol[20]; +int tracks[20]; +int N, T, costopt, maxi; + +void proc(int pos, int cost) { + if (cost <= N && cost + maxi*(T-pos) > costopt) { + if (pos == T) { + if (cost > costopt) { + for (int i = 0; i < T; i++) solopt[i] = sol[i]; + costopt = cost; + } + } else { + sol[pos] = true; + proc(pos + 1, cost + tracks[pos]); + sol[pos] = false; + proc(pos + 1, cost); + } + } +} + +int main() { + while (scanf("%d", &N) != EOF) { + scanf("%d", &T); + maxi = 0; + for (int i = 0; i < T; i++) scanf("%d", &tracks[i]), sol[i] = solopt[i] = false, maxi = tracks[i] > maxi? tracks[i]: maxi; + costopt = 0; + proc(0, 0); + for (int i = 0; i < T; i++) { + if (solopt[i]) { + printf("%d ", tracks[i]); + } + } + printf("sum:%d\n", costopt); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/624/624-21.cpp b/uva_cpp_clean/624/624-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52126c9fbe14cc4ed4b0372ae4ed2e5a669afd45 --- /dev/null +++ b/uva_cpp_clean/624/624-21.cpp @@ -0,0 +1,54 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 624 + Name: CD + Problem: https://onlinejudge.org/external/6/624.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int du[21],c,best; +int tmp[21],ans[21]; +void BT(int i, int n){ + if(i==c) + { + if(n=0) + { + tmp[i]=1; + BT(i+1,n-du[i]); + } + tmp[i]=0; + BT(i+1,n); +} + +int main(){ + int N; + while(cin>>N){ + cin>>c; + for(int i=0; i>du[i]; + + best=2147483647; + BT(0,N); + for(int i=0; i + +using namespace std; + +int score, tempScore, tracks, target, gap, + answerArr[20], tempArr[20], input[20]; + +void backtracking(int no) +{ + if ( target - tempScore < gap ) + { + gap = target - tempScore; + score = tempScore; + for ( int l = 0; l < tracks ; l++ ) + answerArr[l] = tempArr[l]; + } + for ( int l = no; l < tracks; l++ ) + if ( tempScore + input[l] <= target) + { + tempArr[no] = input[l]; + tempScore += input[l]; + backtracking(l+1); + tempScore -= input[l]; + tempArr[no] = 0; + } +} + +int main() +{ + while ( scanf("%d %d",&target, &tracks) != EOF ) + { + for ( int l = 0; l < tracks; l++ ) + scanf("%d",&input[l]); + tempScore = score = 0; + gap = target; + backtracking(0); + for ( int l = 0; l < tracks ; l++ ) + if ( answerArr[l] != 0 ) + printf("%d ",answerArr[l]); + printf("sum:%d\n",score); + } +} diff --git a/uva_cpp_clean/624/624-6.cpp b/uva_cpp_clean/624/624-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff7953f77a287067fd8feb4d9cb1331fbb93d2e9 --- /dev/null +++ b/uva_cpp_clean/624/624-6.cpp @@ -0,0 +1,49 @@ +/* + dynamic programming > subset sum + difficulty: easy + date: 24/Mar/2020 + problem: print the subset of A with the max sum k, k <= K + by: @brpapa +*/ +#include +#include +#define INF (1 << 30) +using namespace std; + +int A[22], N, K; + +int memo[22][22*22*22]; +int dp(int n, int k) { + // n-ésimo elemento de A, soma acumulada k + + if (k > K) return -INF; + + int &ans = memo[n][k]; + if (ans != -1) return ans; + + if (n == N) return ans = k; + + return ans = max(dp(n+1, A[n]+k), dp(n+1, k)); +} + +void recover() { + int k = 0; + for (int n = 0; n < N; n++) { + if (memo[n+1][k+A[n]] >= memo[n+1][k]) { + k += A[n]; + cout << A[n] << " "; + } + } + cout << "sum:" << k << endl; +} + +int main() { + while (cin >> K) { + cin >> N; for (int n = 0; n < N; n++) cin >> A[n]; + + memset(memo, -1, sizeof memo); + dp(0, 0); + recover(); + } + return 0; +} diff --git a/uva_cpp_clean/624/624-9.cpp b/uva_cpp_clean/624/624-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86df0f5ca7ddfb1a2868ec403f66dcc6541528a8 --- /dev/null +++ b/uva_cpp_clean/624/624-9.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +vector ans; +vector a; +vector x; +int n, m, maxi = 0; + +void f(int s, int k){ + if(k == n + 1 ) + return; + if( s <= m && s && s >= maxi){ + maxi = max(maxi, s); + ans = x; + } + x.push_back(a[k]); + f(s + a[k], k + 1); + x.pop_back(); + f(s, k + 1); + +} + +int main(){ + int temp; + while(scanf("%d%d", &m, &n) == 2){ + maxi = 0; + for(int i=0;i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 626 + Name: Ecosystem + Problem: https://onlinejudge.org/external/6/626.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector adj[143]; + int coi[143][143], n; + while (cin >> n) { + for (int i=0; i> coi[i][j]; + if (i == j) coi[i][j] = 0; + if (coi[i][j]) + adj[i].push_back(j); + } + } + + int cnt = 0; + for (int i=0; i i ? k > j : k < j) && coi[k][i]) { + cout << i+1 << ' ' << j+1 << ' ' << k+1 << '\n'; + ++cnt; + } + cout << "total:" << cnt << "\n\n"; + } +} diff --git a/uva_cpp_clean/626/626-9.cpp b/uva_cpp_clean/626/626-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ccc1e00b70a5aa6f8ef1e6fd317520436eabf2c4 --- /dev/null +++ b/uva_cpp_clean/626/626-9.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int n, m[100][100], ct; + +int main(){ + while(scanf("%d", &n) == 1){ + ct = 0; + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + scanf("%d", &m[i][j]); + for(int a = 0; a < n; a++) + for(int b = 0; b < n; b++) + for(int c = 0; c < n; c++) + if( ( ( a < b && b < c ) || + ( a > b && b > c ) ) && + ( m[a][b] && m[b][c] && m[c][a] ) ) + printf("%d %d %d\n", a + 1, b + 1, c + 1), ct++; + printf("total:%d\n\n", ct); + } + return(0); +} diff --git a/uva_cpp_clean/627/627-21.cpp b/uva_cpp_clean/627/627-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d8459efaa1007fb9ffb66a219b59047ce402177 --- /dev/null +++ b/uva_cpp_clean/627/627-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 627 + Name: The Net + Problem: https://onlinejudge.org/external/6/627.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool seen[1001]; +int pre[1001]; +vector adj[1001]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int n; + string str; + while (cin >> n) { + for (int i=0, u; i> str; + stringstream sin(str); + sin >> u; + adj[u].clear(); + for (int v; sin.ignore(1); ) + if (sin>>v) + adj[u].push_back(v); + } + + cout << "-----\n"; + int q; cin >> q; + while (q--) { + int src, dst; + cin >> src >> dst; + memset(seen, 0, n+1); + queue q; + q.push(src); + seen[src] = 1; + while (!q.empty()) { + int u = q.front(); q.pop(); + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + pre[v] = u; + if (v == dst) { + stack st; + for (int p=v; p!=src; p=pre[p]) + st.push(p); + cout << src; + for (; !st.empty(); st.pop()) + cout << ' ' << st.top(); + cout << endl; + goto fin; + } + q.push(v); + } + } + cout << "connection impossible\n"; + fin:; + } + } +} diff --git a/uva_cpp_clean/627/627-9.cpp b/uva_cpp_clean/627/627-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44203bc89438d0b8220bbfd53781714536b557e2 --- /dev/null +++ b/uva_cpp_clean/627/627-9.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int n, u, v, q, _source, _target, d[310], path[310], ans; +char s[500], *p; +vector G[310]; + +void printPath(int source, int target, int ct){ + if(source == target){ + printf("%d ", source); + return; + } + printPath(source, path[target], ct + 1); + printf( ct==0 ? "%d\n" : "%d ", target); +} + +int bfs(int source, int target){ + for(int i = 1; i <= n; i++) d[i] = -1; + queue Q; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + u = Q.front(), Q.pop(); + for(int i = 0; i < G[u].size(); i++){ + v = G[u][i]; + if(d[v] == -1) + d[v] = d[u] + 1, path[v] = u, Q.push(v); + } + if(d[target] != -1) return d[target]; + } + return -1; +} + +int main(){ + while(scanf("%d", &n) == 1){ + puts("-----"); + for(int i = 0; i < n; i++){ + scanf("%d-", &v); + cin.getline(s, 500); + p = strtok(s, ","); + while(p) G[v].push_back(atoi(p)), p = strtok(NULL, ","); + } + scanf("%d", &q); + while(q--){ + scanf("%d %d", &_source, &_target); + ans = bfs(_source, _target); + if(ans == -1) puts("connection impossible"); + else printPath(_source, _target, 0); + } + for(int i = 1; i <= n; i++) G[i].clear(); + } + return(0); +} diff --git a/uva_cpp_clean/628/628-19.cpp b/uva_cpp_clean/628/628-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f12877b6f27499ec20a4083a3a66b838a4dfbe4c --- /dev/null +++ b/uva_cpp_clean/628/628-19.cpp @@ -0,0 +1,49 @@ +#include +#include +using namespace std; + +vector dict; +string pattern; +vector sol; +string num[10] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; + +void proc(int pos) { + if (pos == pattern.length()) { + for (int i = 0; i < pos; i++) { + cout << sol[i]; + } + cout << endl; + } else { + if (pattern[pos] == '0') { + for (int i = 0; i < 10; i++) { + sol[pos] = num[i]; + proc(pos+1); + } + } if (pattern[pos] == '#') { + for (int i = 0; i < dict.size(); i++) { + sol[pos] = dict[i]; + proc(pos+1); + } + } + } +} + +int main() { + int n, m; + while (cin >> n) { + dict.clear(); + while (n--) { + string word; + cin >> word; + dict.push_back(word); + } + cin >> m; + cout << "--" << endl; + while (m--) { + cin >> pattern; + sol.assign(pattern.length(), ""); + proc(0); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/628/628-21.cpp b/uva_cpp_clean/628/628-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4231bd2f355e04c2e876beeab9a856ef9e79e813 --- /dev/null +++ b/uva_cpp_clean/628/628-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 628 + Name: Passwords + Problem: https://onlinejudge.org/external/6/628.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + + +char dic[101][270]; +int dicnt; +char rule[270]; +int tmp[270]; + +void BT(int i) +{ + int j; + if(rule[i]==0) + { + for(j=0; rule[j]; j++) + if(tmp[j]<10) + cout<>dicnt) + { + gets(dic[0]); + for(i=0; i>rcnt; + for(i=0; i>rule; + BT(0); + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/628/628-6.cpp b/uva_cpp_clean/628/628-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8d4dccc81d22db853677831161cfecee46c4d50 --- /dev/null +++ b/uva_cpp_clean/628/628-6.cpp @@ -0,0 +1,42 @@ +/* + brute force > iterative + difficulty: easy + date: 14/Apr/2020 + by: @brpapa +*/ +#include +using namespace std; + +// retorna o d-ésimo dígito de num, sendo o 0-ésimo dígito aquele mais à direta +int dig(int num, int d) { + while (d--) num /= 10; + return num % 10; +} + +int main() { + int N; + while (cin >> N) { + vector words(N); + for (string &word : words) cin >> word; + + cout << "--" << endl; + int M; cin >> M; + while (M--) { + string rule; cin >> rule; + int count0 = 0; for (char c : rule) if (c == '0') count0++; + + for (string word : words) { + for (int num = 0; num < pow(10, count0); num++) { + + int d = count0-1; + for (char c : rule) { + if (c == '0') cout << dig(num, d--); + else cout << word; + } + cout << endl; + } + } + } + } + return 0; +} diff --git a/uva_cpp_clean/628/628-9.cpp b/uva_cpp_clean/628/628-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19f2b4d04cee708e8241f85de51eff1616574c87 --- /dev/null +++ b/uva_cpp_clean/628/628-9.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +string a[105], x, b; +int c, k, t; + +void f(string b, int p, string x){ + if(p == b.size()){ + cout << x << endl; + return; + } + if(b[p] == '0'){ + for(int i = 48; i < 58; i++){ + x.push_back(i); + f(b,p + 1,x); + x.pop_back(); + } + } + else{ + for(int i = 0; i < c; i++){ + for(int j = 0; j < a[i].size(); j++) + x.push_back(a[i][j]); + f(b, p + 1, x); + for(int j = 0; j < a[i].size(); j++) + x.pop_back(); + } + } +} + +int main(){ + while(scanf("%d", &c) == 1){ + for(int i = 0; i < c; i++) + cin >> a[i]; + scanf("%d", &t); + while(t--){ + cin >> b; + k = 0; + string x; + printf("--\n"); + f(b, 0, x); + } + } + return(0); +} diff --git a/uva_cpp_clean/630/630-15.cpp b/uva_cpp_clean/630/630-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27762a0bfb917383c88a9707c6eb234379fb83f4 --- /dev/null +++ b/uva_cpp_clean/630/630-15.cpp @@ -0,0 +1,52 @@ +/* + 字串處理 +*/ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_case, n_word, num; + string word, sorted_word, test_word; + unordered_map> table; + + cin >> n_case; + while (n_case--) { + cin >> n_word; + + while (n_word--) { + cin >> word; + sorted_word = word; + sort(sorted_word.begin(), sorted_word.end()); + table[sorted_word].push_back(word); + } + + while (cin >> test_word && test_word != "END") { + cout << "Anagrams for: " << test_word << "\n"; + + num = 1; + sorted_word = test_word; + sort(sorted_word.begin(), sorted_word.end()); + + if (table[sorted_word].empty()) + cout << "No anagrams for: " << test_word << "\n"; + else + for (const string& anagram : table[sorted_word]) + cout << setw(3) << num++ << ") " << anagram << "\n"; + } + + if (n_case) { + cout << "\n"; + table.clear(); + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/630/630-9.cpp b/uva_cpp_clean/630/630-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbee2cc9d6c3e3c3aec64d140126b893c42abb16 --- /dev/null +++ b/uva_cpp_clean/630/630-9.cpp @@ -0,0 +1,36 @@ +#include + +#define SIZE 110 +#define LEN 30 + +using namespace std; + +int tc, N, ct; +char v[SIZE][LEN], dic[SIZE][LEN], s[LEN], ss[LEN], ans[SIZE][LEN]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &N); + for(int it = 0; it < N; it++){ + scanf("%s", v[it]); + strcpy(ss, v[it]); + sort(ss, ss + strlen(ss)); + strcpy(dic[it], ss); + } + while(scanf("%s", s), strcmp(s, "END") != 0){ + ct = 0; + strcpy(ss, s); + sort(ss, ss + strlen(ss)); + for(int it = 0; it < N; it++) + if( strcmp(dic[it], ss) == 0 ) + strcpy(ans[ct++], v[it]); + printf("Anagrams for: %s\n", s); + if( ct == 0 ) printf("No anagrams for: %s\n", s); + for(int it = 0; it < ct; it++) + printf("%3d) %s\n", it + 1, ans[it]); + } + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/634/634-5.cpp b/uva_cpp_clean/634/634-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7ac43594b9c6dc1ecff0b820591218834e317e4f --- /dev/null +++ b/uva_cpp_clean/634/634-5.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + +struct info +{ + int x,y; + info(): x(0), y(0){} +}; + +int main() +{ + int n; + while(scanf("%d",&n)==1) + { + if(n==0) + break; + info a[1010]; + for(int i=0;i p.x) || (a[i].x > p.x && a[(i+1)%n].x < p.x)) && a[i].y>p.y) + { + cnt++; + } + } + + if(cnt&1) + { + puts("T"); + } + else + { + puts("F"); + } + + } + + return 0; +} diff --git a/uva_cpp_clean/636/636-19.cpp b/uva_cpp_clean/636/636-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ed8dc972b2b50928dbde73a9513b3ce00ac2ce0 --- /dev/null +++ b/uva_cpp_clean/636/636-19.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +int main() { + int n; + int digits[11]; + while (cin >> n && n != 0) { + int l = 0; + int b = 0; + while (n) { + digits[l] = n%10; + if (digits[l] + 1 > b) b = digits[l] + 1; + n /= 10; + l++; + } + for (; b <= 100; b++) { + unsigned long long val = 0, pot = 1; + for (int i = 0; i < l; i++) { + val += digits[i]*pot; + pot *= b; + } + long long sq = floor(sqrt(val)); + if (val == sq*sq) break; + } + printf("%d\n", b); + } + return 0; +} diff --git a/uva_cpp_clean/636/636-21.cpp b/uva_cpp_clean/636/636-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d8dff0528f6e408fd514354de35777fb59bd260 --- /dev/null +++ b/uva_cpp_clean/636/636-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 636 + Name: Squares (III) + Problem: https://onlinejudge.org/external/6/636.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + char s[17]; + while (cin >> s && (s[1] || s[0]!='0')) { + int b = 0; + for (int i=0; s[i]; ++i) + b = max(b, s[i]-'0'+1); + + for (; b<101; ++b) { + int n = 0; + for (int i=0; s[i]; ++i) + n = n*b + s[i]-'0'; + + int sq = sqrt(n); + if (sq*sq == n) break; + } + + cout << b << endl; + } +} diff --git a/uva_cpp_clean/637/637-21.cpp b/uva_cpp_clean/637/637-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07756cd0bae80eb7a9b33e8ca87eae1ffedad2b7 --- /dev/null +++ b/uva_cpp_clean/637/637-21.cpp @@ -0,0 +1,61 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 637 + Name: Booklet Printing + Problem: https://onlinejudge.org/external/6/637.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +void printPage(int p) { + if (p) cout << p; + else cout << "Blank"; +} + +int main() { + int n; + while (cin >> n && n) { + printf("Printing order for %d pages:\n", n); + + int P[143][4] = {}, + sz = (n+3)/4, + p=0, i; + + for (i=0; i n) + P[i][j] = 0; + + if (P[i][3] || P[i][0]) { + printf("Sheet %d, front: ", i+1); + printPage(P[i][3]); + cout << ", "; + printPage(P[i][0]); + cout << endl; + } + + if (P[i][1] || P[i][2]) { + printf("Sheet %d, back : ", i+1); + printPage(P[i][1]); + cout << ", "; + printPage(P[i][2]); + cout << endl; + } + } + } +} diff --git a/uva_cpp_clean/639/639-21.cpp b/uva_cpp_clean/639/639-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4042da3e6d86bb69829a662d51d232687a85dc65 --- /dev/null +++ b/uva_cpp_clean/639/639-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 639 + Name: Don't Get Rooked + Problem: https://onlinejudge.org/external/6/639.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int board[5][5],mat[5][5],n; + +void place(int y,int x,int t){ + int i; + for(i=x+1;i=0 && !mat[y][i];i--)board[y][i]+=t; + for(i=y+1;i=0 && !mat[i][x];i--)board[i][x]+=t; +} +int cnt,maxx; +void rec(int pos){ + int x,y; + if(pos==n*n){ + if(cnt>maxx)maxx=cnt; + return; + } + x=pos%n; y=pos/n; + if(mat[y][x] || board[y][x]){ + rec(pos+1); + return; + } + place(y,x,1); + cnt++; + rec(pos+1); + place(y,x,-1); + cnt--; + + rec(pos+1); +} + +int main(){ + int i,j; + while(scanf("%d",&n)==1 && n){ + maxx=cnt=0; + for(i=0;i +#include + +using namespace std; + +bool check[1000005]; + +int main() +{ + for(int i=0 ; i<1000005 ; i++) check[i] = true; + + for(int i=1 ; i<=1000000 ; i++) + { + int generator = i, sum = i; + while( generator!=0 ) + { + sum += generator%10; + generator /= 10; + } + + if( sum<=1000000 ) check[sum] = false; + } + + for(int i=1 ; i<=1000000 ; i++) + if( check[i] ) cout << i << endl; + + return 0; +} diff --git a/uva_cpp_clean/640/640-21.cpp b/uva_cpp_clean/640/640-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..957859c9bd3c7a1bc5fdafde9c2c20b2eace255f --- /dev/null +++ b/uva_cpp_clean/640/640-21.cpp @@ -0,0 +1,32 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 640 + Name: Self Numbers + Problem: https://onlinejudge.org/external/6/640.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int d(int n) { + int sum=0; + for(; n; n/=10) sum+=n%10; + return sum; +} + +#define MAX 1000001 +bool dp[MAX]; +int main(){ + int cnt=0; + for (int i=1; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 641 + Name: Do the Untwist + Problem: https://onlinejudge.org/external/6/641.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +int lc[127]; +char letters[] = "_abcdefghijklmnopqrstuvwxyz."; +int main(){ + for (int i=0; letters[i]; i++) + lc[letters[i]] = i; + + int n, k; + char s[100], o[100]; + while (scanf("%d %s", &k, s)==2 && k) { + int n = strlen(s); + for (int i=0; i + +using namespace std; + +const int MAX_N = 80; + +char plaintext[MAX_N], ciphertext[MAX_N]; +int n, k, plaincode[MAX_N], ciphercode[MAX_N]; + +inline int map_ch(char ch) { + if (ch == '_') return 0; + if (ch == '.') return 27; + return ch - 'a' + 1; +} + +inline char map_int(int num) { + if (num == 0) return '_'; + if (num == 27) return '.'; + return 'a' + num - 1; +} + +void decrypt() { + n = strlen(ciphertext); + for (int i = 0; i < n; i++) ciphercode[i] = map_ch(ciphertext[i]); + for (int i = 0; i < n; i++) plaincode[k * i % n] = ( ciphercode[i] + i ) % 28; + for (int i = 0; i < n; i++) plaintext[i] = map_int(plaincode[i]); + for (int i = 0; i < n; i++) putchar(plaintext[i]); putchar('\n'); +} + +int main() { + while (scanf("%d %s", &k, ciphertext), k) decrypt(); + return(0); +} \ No newline at end of file diff --git a/uva_cpp_clean/642/642-21.cpp b/uva_cpp_clean/642/642-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d37c05c8c376d43aa89d456aaf4d4d80b0a7a6f --- /dev/null +++ b/uva_cpp_clean/642/642-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 642 + Name: Word Amalgamation + Problem: https://onlinejudge.org/external/6/642.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + + +int primes[] = { + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, + 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, + 79, 83, 89, 97, 101, 103, 107, 109, 113 +}; + + + + +map > words; +long long conv(const char word[]) { + long long r=1; + for (int i=0; word[i]; i++) + r *= primes[word[i]-'a']; + return r; +} + +int main(){ + char word[10]; + while (cin>>word && word[0]!='X') + words[conv(word)].insert(word); + + while (cin>>word && word[0]!='X') { + auto it = words.find(conv(word)); + if (it == words.end()) + puts("NOT A VALID WORD"); + + else for (string s: it->second) + cout << s << endl; + + puts("******"); + } +} diff --git a/uva_cpp_clean/642/642-9.cpp b/uva_cpp_clean/642/642-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..481b839ce4cb365de97fc209a74f680dbc41afd2 --- /dev/null +++ b/uva_cpp_clean/642/642-9.cpp @@ -0,0 +1,31 @@ +#include + +#define SIZE 100 + +using namespace std; + +char s[SIZE]; +vector v, dic; +string ss; +int ct; + +int main(){ + while(scanf("%s", s), strcmp(s, "XXXXXX") != 0) + v.push_back(string(s)); + sort(v.begin(), v.end()); + for(int it1 = 0; it1 < v.size(); it1++){ + ss = v[it1]; + sort(ss.begin(), ss.end()); + dic.push_back(ss); + } + while(scanf("%s", s), strcmp(s, "XXXXXX") != 0){ + ct = 0, ss = s; + sort(ss.begin(), ss.end()); + for(int it = 0; it < dic.size(); it++) + if( ss == dic[it] ) + printf("%s\n", v[it].c_str()), ct++; + if( ct == 0 ) puts("NOT A VALID WORD"); + puts("******"); + } + return(0); +} diff --git a/uva_cpp_clean/644/644-18.cpp b/uva_cpp_clean/644/644-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a297219cbe163c44a8b0530da2bb77340dfa16f2 --- /dev/null +++ b/uva_cpp_clean/644/644-18.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main(){ + vector binary; + char line[20]; + bool prefix; + int set = 0; + while(scanf("%s", &line) != EOF){ + if(strcmp(line, "9") != 0) binary.push_back(line); + else{ + set++; + prefix = false; + sort(binary.begin(), binary.end()); + for(int i = 0; i < binary.size() && !prefix; i++){ + for(int j = i + 1; j < binary.size(); j++){ + if(binary[j].find(binary[i]) == 0){ + prefix = true; + break; + } + } + } + if(prefix) printf("Set %d is not immediately decodable\n", set); + else printf("Set %d is immediately decodable\n", set); + binary.clear(); + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/644/644-19.cpp b/uva_cpp_clean/644/644-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e795eacc5c4de848fabe30f1eb5f210d95dc3e90 --- /dev/null +++ b/uva_cpp_clean/644/644-19.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +using namespace std; + +int main() { + string s; + int cases = 1; + bool ok = true; + unordered_map m; + while (cin >> s) { + if (s == "9") { + if (ok) printf("Set %d is immediately decodable\n", cases++); + else printf("Set %d is not immediately decodable\n", cases++); + m.clear(); + ok = true; + } else if (ok) { + for (int i = 1; i < s.length(); i++) { + if (m[s.substr(0,i)] == 1) ok = false; + } + if (m.count(s)) ok = false; + m[s] = 1; + } + } + return 0; +} diff --git a/uva_cpp_clean/644/644-21.cpp b/uva_cpp_clean/644/644-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96b646ccc5bc11a9aa9857306af12c31d645c234 --- /dev/null +++ b/uva_cpp_clean/644/644-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 644 + Name: Immediate Decodability + Problem: https://onlinejudge.org/external/6/644.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +struct Node { + bool val; + Node *l, *r; + + Node():val(0),l(0),r(0){} + ~Node() { + if (l) delete l; + if (r) delete r; + } +}; + +int main(){ + char w[1000]; + for (int cse=1; ; cse++) { + bool f = true, some=0; + Node *root = new Node(); + while (cin>>w && w[0]!='9') { + some = true; + if (f) { + bool np = false; + Node *c=root; + for (int i=0; f && w[i]; i++) { + if (w[i]=='0') { + if (!c->l) { + c->l = new Node(); + np = true; + } + c = c->l; + } + else { + if (!c->r) { + c->r = new Node(); + np = true; + } + c = c->r; + } + if (c->val) f = false; + } + c->val = true; + if (!np) f = false; + } + } + if (!some) break; + printf("Set %d is %simmediately decodable\n", cse, f?"":"not "); + delete root; + } +} diff --git a/uva_cpp_clean/644/644-9.cpp b/uva_cpp_clean/644/644-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2fede585a35dd5032cdfbda64e5588eb0f9bf4d --- /dev/null +++ b/uva_cpp_clean/644/644-9.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +const int SIZE = 100, LEN = 10; + +int tc, n; +char code[SIZE][LEN]; +bool ok; + +bool prefix(int i, int j){ + int l1 = strlen(code[i]), l2 = strlen(code[j]); + for(int it = 0; it < min(l1, l2); it++) + if( code[i][it] != code[j][it] ) return false; + return true; +} + +int main(){ + while(~scanf("%s", code[0])){ + ok = true; + n = 1; + while( scanf("%s", code[n]), code[n][0] != '9' ) n++; + for(int i = 0; i < n and ok ; i++) + for(int j = i + 1; j < n and ok; j++) + if( prefix(i, j) ) ok = false; + printf("Set %d ", ++tc); + puts( ok ? "is immediately decodable" : "is not immediately decodable" ); + } + return(0); +} diff --git a/uva_cpp_clean/652/652-21.cpp b/uva_cpp_clean/652/652-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..041817b893bee4bbe9983bad59a2c7976ff98d98 --- /dev/null +++ b/uva_cpp_clean/652/652-21.cpp @@ -0,0 +1,84 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 652 + Name: Eight + Problem: https://onlinejudge.org/external/6/652.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +unordered_map R; +const char finstr[] = "012345678"; +struct State { + string p; + char s[10]; + int zid, sx; + State() {} + + void recalc() { + sx = 0; + for (int i=0; i<9; ++i) + sx = sx*10 + s[i] - '0'; + } +} +Q[400414]; + +char dn[] = "rldu"; +int dd[] = {-1, 1, -3, 3}, + dy[] = {0, 0, -1, 1}, + dx[] = {-1, 1, 0, 0}, + isg[] = { 0, 1, 1, 1, 0 }; + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int sz = 1; + Q[0].zid = 8; + Q[0].recalc(); + memcpy(Q[0].s, finstr, sizeof(finstr)); + + for (int i=0; i> T; + while (T--) { + int sx = 0; + for (int i=0; i<9; ++i) { + cin >> ch; + sx = sx*10 + (ch == 'x' ? 8 : ch-'1'); + } + + auto it = R.find(sx); + if (it == R.end()) + cout << "unsolvable\n"; + else cout << (it->second) << '\n'; + + if (T) cout << '\n'; + } +} diff --git a/uva_cpp_clean/657/657-15.cpp b/uva_cpp_clean/657/657-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f85881f4750d64f3ccf370633291f85220a2fc83 --- /dev/null +++ b/uva_cpp_clean/657/657-15.cpp @@ -0,0 +1,73 @@ +/* + 區域連通塊 +*/ +#include +#include +#include + +using namespace std; + +void DFS(const int i, const int j, const int& row, const int& column, vector>& field) { + // 清掉所有相鄰的 X + if (i < 0 || i >= row || j < 0 || j >= column || field[i][j] != 'X') + return; + + field[i][j] = '*'; + + DFS(i + 1, j, row, column, field); + DFS(i - 1, j, row, column, field); + DFS(i, j + 1, row, column, field); + DFS(i, j - 1, row, column, field); +} + +int DFS(const int i, const int j, const int& row, const int& column, vector>& field, int& ans) { + // 在地圖上找 X + if (i < 0 || i >= row || j < 0 || j >= column || field[i][j] == '.') + return 0; + + if (field[i][j] == 'X') { + ans++; + DFS(i, j, row, column, field); + } + + field[i][j] = '.'; + + DFS(i + 1, j, row, column, field, ans); + DFS(i - 1, j, row, column, field, ans); + DFS(i, j + 1, row, column, field, ans); + DFS(i, j - 1, row, column, field, ans); + return ans; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int case_num = 1, row, column, i, j, k; + + while (cin >> column >> row && row) { + vector> field(row, vector(column)); + vector answers; + k = 0; + + for (i = 0; i < row; i++) + for (j = 0; j < column; j++) + cin >> field[i][j]; + + for (i = 0; i < row; i++) + for (j = 0; j < column; j++) + if (field[i][j] == '*' || field[i][j] == 'X') { + answers.push_back(0); + DFS(i, j, row, column, field, answers[k++]); + } + + sort(answers.begin(), answers.end()); + + cout << "Throw " << case_num++ << "\n" + << answers[0]; + for (i = 1; i < k; i++) + cout << " " << answers[i]; + cout << "\n\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/657/657-6.cc b/uva_cpp_clean/657/657-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..ce24901ddd5b0ee4d09638e871a0c4738aabfa81 --- /dev/null +++ b/uva_cpp_clean/657/657-6.cc @@ -0,0 +1,60 @@ +// https://uva.onlinejudge.org/external/6/657.pdf +#include +using namespace std; +using vs=vector; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(int t=1;;t++){ + int n,m; + cin>>m>>n; + if(!n)break; + vs a(n); + for(int i=0;i>a[i]; + vvi s(n,vi(m)); + functiondfs=[&](int i,int j,int k){ + s[i][j]=k; + int y[]={-1,0,0,1}; + int x[]={0,-1,1,0}; + for(int l=0;l<4;l++){ + int u=i+y[l],v=j+x[l]; + if(u>=0&&u=0&&vdfs2=[&](int i,int j,int k){ + r[i][j]=k; + int y[]={-1,0,0,1}; + int x[]={0,-1,1,0}; + for(int l=0;l<4;l++){ + int u=i+y[l],v=j+x[l]; + if(u>=0&&u=0&&v + +#define SIZE 55 + +using namespace std; + +int tc, N, M, CC; +char grid[SIZE][SIZE]; +bool vis[SIZE][SIZE]; +vector ans; +vector < pair > pos; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M and grid[R][C] != '.' ); +} + +void floodfill(int r, int c, char ch1, char ch2){ + vis[r][c] = true; + grid[r][c] = ch2; + if( grid[r][c] == 'w' ) pos.push_back( make_pair(r, c) ); + for(int d = 0; d < 4; d++){ + int nr = r + dr[d], nc = c + dc[d]; + if( is_possible(nr, nc) and not vis[nr][nc]){ + if( grid[nr][nc] == 'X' ) floodfill(nr, nc, 'X', 'w'); + else floodfill(nr, nc, grid[nr][nc], '.'); + } + } +} + +int main(){ + while(scanf("%d %d\n", &M, &N), N | M){ + memset(vis, false, sizeof vis); + ans.clear(); + for(int row = 0; row < N; row++) scanf("%s", grid[row]); + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col++) + if( grid[row][col] == 'X' ){ + CC = 0; + pos.clear(); + floodfill(row, col, 'X', 'w'); + for(int it = 0; it < pos.size(); it++) vis[pos[it].first][pos[it].second] = false; + for(int it = 0; it < pos.size(); it++) + if( grid[pos[it].first][pos[it].second] == 'w' ) + floodfill(pos[it].first, pos[it].second, 'w', '.'), CC++; + ans.push_back(CC); + } + sort(ans.begin(), ans.end()); + printf("Throw %d\n", ++tc); + for(int it = 0; it < ans.size(); it++) + printf( it != ans.size() - 1 ? "%d " : "%d\n\n", ans[it]); + } + return(0); +} diff --git a/uva_cpp_clean/658/658-7.cpp b/uva_cpp_clean/658/658-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab7f948293ffe99fb0f8ef76daf184f96fa18568 --- /dev/null +++ b/uva_cpp_clean/658/658-7.cpp @@ -0,0 +1,107 @@ +/** + * Created by LonelyEnvoy on 2017-8-9. + * It's not a Bug, it's a Feature! + * Keywords: 隐式图搜索, dijkstra, BFS + */ + +#include +#include +#include +#include +using namespace std; + +#define rep(i,a,n) for(int i=a;i=a;i++) + +#define INF 0x3f3f3f3f + +#define MAX_BUGS 20 +#define MAX_PATCHES 100 + +char patchesSrc[MAX_PATCHES][MAX_BUGS]; +char patchesDest[MAX_PATCHES][MAX_BUGS]; +int patchesCost[MAX_PATCHES]; + +int n; // number of bugs +int m; // number of patches + +bool vis[1< rhs.totalCost; + } +}; + +inline void init() { + memset(vis, false, sizeof(vis)); + memset(dis, INF, sizeof(dis)); +} + +int dijkstra() { + priority_queue q; + Node curr, next; + curr.status = (1 << n) - 1; + curr.totalCost = 0; + + q.push(curr); + dis[curr.status] = 0; + while (!q.empty()) { + curr = q.top(); + q.pop(); + if (vis[curr.status]) continue; + vis[curr.status] = true; + if (curr.status == 0) return curr.totalCost; + rep(k,0,m) { + bool canPatch = true; + rep(i,0,n) { + if ((patchesSrc[k][i] == '+' && !((1 << (n-i-1)) & curr.status)) + || (patchesSrc[k][i] == '-' && (1 << (n-i-1)) & curr.status)) { + canPatch = false; + break; + } + } + if (!canPatch) continue; + next.totalCost = curr.totalCost + patchesCost[k]; + next.status = curr.status; + rep(i,0,n) { + next.status |= (((patchesDest[k][i] == '+') << (n-i-1))); + next.status &= ~(((patchesDest[k][i] == '-') << (n-i-1))); + } + if (next.status == curr.status) continue; + if (next.totalCost < dis[next.status]) { + dis[next.status] = next.totalCost; + q.push(next); + } + } + } + return -1; +} + + +int main() { + //freopen("acm.out", "w", stdout); + int kase = 0; + int ans; + while (~scanf("%d%d", &n, &m) && n && m) { + rep(i,0,m) { + scanf("%d%s%s", &patchesCost[i], patchesSrc[i], patchesDest[i]); + } + init(); + ans = dijkstra(); + printf("Product %d\n", ++kase); + if (ans != -1) { + printf("Fastest sequence takes %d seconds.\n\n", ans); + } else { + printf("Bugs cannot be fixed.\n\n"); + } + } +} + + + diff --git a/uva_cpp_clean/661/661-19.cpp b/uva_cpp_clean/661/661-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e23dbfbc2fc3141cc9a646389553777d2c7e41bf --- /dev/null +++ b/uva_cpp_clean/661/661-19.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main() { + int n, m, c, cs = 1; + int amperes[21]; + int on[21]; + while (cin >> n >> m >> c && !(n == 0 && m == 0 && c == 0)) { + int current = 0, maxi = 0; + bool ok = true; + for (int i = 0; i < n; i++) cin >> amperes[i], on[i] = 0; + for (int i = 0; i < m; i++) { + int sw; + cin >> sw; + sw--; + if (!on[sw]) current += amperes[sw], maxi = max(maxi, current); + else current -= amperes[sw]; + on[sw] = !on[sw]; + } + if (maxi > c) printf("Sequence %d\nFuse was blown.\n\n", cs++); + else printf("Sequence %d\nFuse was not blown.\nMaximal power consumption was %d amperes.\n\n", cs++, maxi); + } + return 0; +} diff --git a/uva_cpp_clean/661/661-2.cpp b/uva_cpp_clean/661/661-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..56bb64f4d92211e66d7c52cf1a9b500a1dec371f --- /dev/null +++ b/uva_cpp_clean/661/661-2.cpp @@ -0,0 +1,76 @@ + +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define INF 9876543210LL +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction + +int values[N]; +bool status[N]; + +int main() { + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + int n, m, c, T = 1, t = 1; + while (cin >> n >> m >> c) { + + if (!n && !m && !c) break; + + int maxpower = 0, total = 0; + bool blown = false; + + REP(i, n) { + cin >> values[i]; + status[i] = false; + } + + REP(i, m) { + int cur; cin >> cur; + cur--; + if (!status[cur]) { + total += values[cur]; + status[cur] = !status[cur]; + } else { + total -= values[cur]; + status[cur] = !status[cur]; + } + if (total > c) blown = 1; + else if (total > maxpower) maxpower = total; + } + + cout << "Sequence " << t++ << el; + if (blown) cout << "Fuse was blown." << el; + else cout << "Fuse was not blown." << el << "Maximal power consumption was " << maxpower << " amperes." << el; + if (T++ >= 1) cout << el; + } + return 0; +} + + diff --git a/uva_cpp_clean/661/661-20.cpp b/uva_cpp_clean/661/661-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62cee45a5f8dbcd01c4b30610a6b747f0751370f --- /dev/null +++ b/uva_cpp_clean/661/661-20.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; + +int consumption[25], state[25]; + +int main() +{ + int n, m, c; + + int ncase = 0; + while( cin >> n >> m >> c && n+m+c!=0 ) + { + for(int i=0 ; i<25 ; i++) + state[i] = 0; + + for(int i=1 ; i<=n ; i++) + cin >> consumption[i]; + + bool flag = false; + int result = 0, current = 0; + for(int i=0 ; i> opration; + + if( flag ) continue; + + state[opration] = 1 - state[opration]; + + if( state[opration] ) current += consumption[opration]; + else current -= consumption[opration]; + + if( c>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 661 + Name: Blowing Fuses + Problem: https://onlinejudge.org/external/6/661.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int n, m, c, cse=1, f[100]; + bool state[100]; + while(cin>>n>>m>>c && (n||m||c)) { + for (int i=0; i>f[i]; + } + + int sum=0, maxs=0, oi; + for (int i=0; i> oi;oi--; + if (maxs<=c) { + if (state[oi]) sum-=f[oi]; + else { + sum+=f[oi]; + if (sum>maxs) maxs=sum; + } + + state[oi] = !state[oi]; + } + } + + cout << "Sequence " << (cse++) << endl; + if (maxs>c) cout << "Fuse was blown.\n\n"; + else printf("Fuse was not blown.\nMaximal power consumption was %d amperes.\n\n", maxs); + } +} diff --git a/uva_cpp_clean/661/main.cpp b/uva_cpp_clean/661/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..590b57bc116c34fd44f7c7d5b071b74005202b6f --- /dev/null +++ b/uva_cpp_clean/661/main.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +using namespace std; + +int main(){ + int devices, operations, capacity, maxim, device, tension; + int sequence = 1; + vector all_devices; + map on_devices; + while(cin >> devices >> operations >> capacity){ + if(devices == 0 && operations == 0 && capacity == 0) break; + + bool res = false; + on_devices.clear(); + all_devices.clear(); + maxim = 0; + tension=0; + all_devices.push_back(0); + for(int i=1; i <= devices; i++){ + cin >> device; + all_devices.push_back(device); + on_devices.insert(pair (i,0)); + } + for(int i=0; i < operations; i++){ + cin >> device; + if(on_devices[device] == 1){ + tension -= all_devices[device]; + on_devices[device] = 0; + } + else{ + tension += all_devices[device]; + on_devices[device]= 1; + } + if(tension > capacity){ + res = true; + } + maxim = max(maxim, tension); + } + if(res){ + printf("Sequence %d\nFuse was blown.\n\n", sequence); + }else{ + printf("Sequence %d\nFuse was not blown.\nMaximal power consumption was %d amperes.\n\n", sequence, maxim); + } + sequence++; + } + + +return 0; +} diff --git a/uva_cpp_clean/663/663-21.cpp b/uva_cpp_clean/663/663-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80c7e0f5683760bfa51bc652c81db2a3bff7fdad --- /dev/null +++ b/uva_cpp_clean/663/663-21.cpp @@ -0,0 +1,92 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 663 + Name: Sorting Slides + Problem: https://onlinejudge.org/external/6/663.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MAXN 143 + +int X1[MAXN], X2[MAXN], Y1[MAXN], Y2[MAXN], M[MAXN], n; +bool isInside(int i, int x, int y) { + return x>=X1[i] && x<=X2[i] && y>=Y1[i] && y<=Y2[i]; +} + +vector adj[MAXN]; +int matchL[MAXN], matchR[MAXN]; +bool del[MAXN][MAXN], seen[MAXN], good[MAXN]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v] && !del[u][v]) { + seen[v] = true; + if (matchR[v]<0 || bpm(matchR[v])) { + matchR[v] = u; + matchL[u] = v; + return true; + } + } + return false; +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + for (int cse=1; cin >> n && n; ++cse) { + cout << "Heap " << cse << "\n"; + + for (int i=0; i> X1[i] >> X2[i] >> Y1[i] >> Y2[i]; + adj[i].clear(); + } + + + for (int i=0, x,y; i> x >> y; + for (int j=0; j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 664 + Name: Single-Player Games + Problem: https://onlinejudge.org/external/6/664.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define EPS 1e-13 +using namespace std; + + +int X[100143], n; +double Mat[33][33], R[33]; +vector adj[100143]; +void dfs(int id, int j, double m) { + if (adj[j].empty()) Mat[id][n] += m*X[j]; + else if (j < n) Mat[id][j] -= m; + else for (int k: adj[j]) + dfs(id, k, m/adj[j].size()); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(3); + + string line; + for (int cse=1; cin >> n && n; ++cse) { + cin.ignore(100, '\n'); + for (int i=0; i at; + at.push(i); + + bool eqseen=0; + bool frst = 1; + for (int i=0; i<=line.length(); ++i) + if (line[i] == '(') { + if (frst) frst = 0; + else adj[id].clear(), + at.push(id++); + } + else if (line[i] == ')') { + int j = at.top(); + at.pop(); + if (!at.empty()) + adj[at.top()].push_back(j); + } + else if (line[i]=='-' || isdigit(line[i])) { + int r = line[i]=='-' ? 0 : line[i]-'0', j; + for (j=i+1; isdigit(line[j]); ++j) + r = r*10 + line[j] - '0'; + if (line[i]=='-') + r *= -1; + + adj[at.top()].push_back(id); + adj[id].clear(); + X[id++] = r; + + i = j-1; + } + else if (line[i] == '=') eqseen = 1; + else if (eqseen && islower(line[i])) + adj[at.top()].push_back(line[i]-'a'); + } + + // Fill System of Linear Equations Matrix + for (int i=0; i fabs(Mat[im][i])) + im = k; + + if (im != i) + for (int j=0; j<=n; ++j) + swap(Mat[im][j], Mat[i][j]); + + if (fabs(Mat[i][i]) < EPS) continue; + + for (int k=i+1; k=0; --i) { + R[i] = Mat[i][n]; + for (int j=i+1; j EPS) + R[i] -= Mat[i][j] * R[j]; + + R[i] = fabs(Mat[i][i]) < EPS ? NAN : R[i] / Mat[i][i]; + } + + // Print Result + cout << "Game " << cse << '\n'; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 665 + Name: False coin + Problem: https://onlinejudge.org/external/6/665.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char S[143], op; + int T, n, m, X[143]; + cin >> T; + while (T--) { + cin >> n >> m; + memset(S, 0, n+1); + for (int i=0, l; i> l; l<<=1; + for (int j=0; j> X[j]; + + cin >> op; + if (op == '=') + for (int j=0; j>= 1; + for (int j=0; j=l; --j) + S[X[j]] |= op=='>' ? 1 : 2; + } + } + + int res0=0, res1=0; + for (int i=1; i<=n; ++i) + if (S[i] != 3) { + if (!S[i]) { + if (!res0) + res0 = i; + else res0 = -1; + } + else { + if (!res1) + res1 = i; + else res1 = -1; + } + } + + if (res0<=0 && res1<0) cout << "0\n"; + else if (res0<=0 && res1>=0) cout << res1 << "\n"; + else cout << res0 << "\n"; + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/668/668-21.cpp b/uva_cpp_clean/668/668-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5aa76eb01d06922a5930876280c3895d4adfc068 --- /dev/null +++ b/uva_cpp_clean/668/668-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 668 + Name: Parliament + Problem: https://onlinejudge.org/external/6/668.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + while (T-- && cin>>n) { + int res[500]={}, sz=0, i; + for (i=2; i<=n; ++i) + res[sz++] = i, + n -= i; + + if (n > 0) { + int k = i-n-2; + if (k < 0) k = 0; + res[sz++] = n + res[k]; + res[k] = 0; + } + + i=0; + while (!res[i])++i; + cout << res[i]; + for (++i; i +#include +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef pair ii; +typedef vector vii; + +int mf, f, s, t, INF = 2000000; +int res[204][204]; +vi p; + +inline double dis(int x0, int y0, int x1, int y1) { + return sqrt((x0-x1)*(x0-x1) + (y0-y1)*(y0-y1)); +} + +void augment(int v, int minEdge) { + if (v == s) { + f = minEdge; + return; + } else if (p[v] != -1) { + augment(p[v], min(minEdge, res[p[v]][v])); + res[p[v]][v] -= f; res[v][p[v]] += f; + } +} + +int main() { + int L; + cin >> L; + while (L--) { + int N, M; + cin >> N >> M; + vector v(N, ii(0, 0)); + vector P(M, ii(0, 0)); + for (int i = 0; i < N; i++) { + cin >> v[i].first >> v[i].second; + } + for (int i = 0; i < M; i++) { + cin >> P[i].first >> P[i].second; + } + for (int i = 0; i < N+M+1; i++) + for (int j = 0; j < N+M+1; j++) + res[i][j] = 0; + for (int i = 0; i < N - 1; i++) { + for (int j = 0; j < M; j++) { + if (dis(v[i].first, v[i].second, P[j].first, P[j].second) + dis(v[i+1].first, v[i+1].second, P[j].first, P[j].second) <= 2*dis(v[i].first, v[i].second, v[i+1].first, v[i+1].second)) { + res[i+1][j+N] = 1; + } + } + } + for (int i = 1; i < N; i++) res[0][i] = 1; + for (int i = 0; i < M; i++) res[i+N][N+M] = 1; + + s = 0, t = N+M; + + mf = 0; + while (true) { + f = 0; + vi dist(N+M+1, INF); dist[s] = 0; + queue q; + q.push(s); + p.assign(N+M+1, -1); + while (!q.empty()) { + int u = q.front(); q.pop(); + if (u == t) break; + for (int v = 0; v < N+M+1; v++) { + if (res[u][v] > 0 && dist[v] == INF) { + dist[v] = dist[u] + 1, q.push(v), p[v] = u; + } + } + } + augment(t, INF); + if (f == 0) break; + mf += f; + } + printf("%d\n", mf + N); + for (int i = 0; i < N - 1; i++) { + printf("%d %d ", v[i].first, v[i].second); + for (int j = 0; j < M; j++) { + if (res[j+N][i+1]) { + printf("%d %d ", P[j].first, P[j].second); + break; + } + } + } + printf("%d %d\n", v[N-1].first, v[N-1].second); + if (L) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/670/670-21.cpp b/uva_cpp_clean/670/670-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c6223161991eaafef3110f4a531e589553ff381 --- /dev/null +++ b/uva_cpp_clean/670/670-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 670 + Name: The dog task + Problem: https://onlinejudge.org/external/6/670.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int dist2(int x1, int y1, int x2, int y2) { + x1 -= x2; + y1 -= y2; + return x1*x1 + y1*y1; +} + +vector adj[143]; +int XB[143], YB[143], XI[143], YI[143], matchL[143], matchR[143]; +bool seen[143]; +bool bpm(int u) { + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = true; + if (matchR[v]<0 || bpm(matchR[v])) { + matchL[u] = v; + matchR[v] = u; + return true; + } + } + return false; +} + + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, m; + cin >> T; + while (T--) { + cin >> n >> m; + for (int i=0; i> XB[i] >> YB[i]; + adj[i].clear(); + } + + for (int j=0; j> XI[j] >> YI[j]; + for (int i=1; i + +using namespace std; + +const int SIZE = 10010, LEN = 20; + +int tc, n, l, ct, len[SIZE]; +char w[SIZE][LEN], word[LEN], s[LEN], correct[LEN]; +set st; +vector ans; +bool ok; + +bool check1(int id){ + if( abs(l - len[id]) != 1 ) return false; + for(int k = 0; k < max(l, len[id]); k++){ + ok = true; + for(int i = 0, j = 0; i < l and j < len[id] and ok; i++, j++) + if( l < len[id] and j == k ) i--; + else if( l > len[id] and i == k ) j--; + else if( word[i] != w[id][j] ) ok = false; + if(ok) return true; + } + return false; +} + +bool check2(int id){ + if( l != len[id] ) return false; + ct = 0; + for(int i = 0; i < l; i++) + if( word[i] != w[id][i] ) ct++; + return ( ct == 1 ); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + st.clear(); + n = 0; + while(scanf("%s\n", w[n]), w[n][0] != '#'){ + len[n] = strlen(w[n]); + st.insert(w[n++]); + } + while(scanf("%s\n", word), word[0] != '#'){ + l = strlen(word); + if( st.find(word) != st.end() ) printf("%s is correct\n", word); + else{ + ans.clear(); + for(int it = 0; it < n; it++) + if( check1(it) or check2(it) ) ans.push_back(w[it]); + printf("%s:", word); + for(int it = 0, sz = ans.size(); it < sz; it++) + printf( it != sz - 1 ? " %s" : " %s\n", ans[it].c_str()); + if( ans.empty() ) putchar('\n'); + } + } + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/673/673-17.cpp b/uva_cpp_clean/673/673-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8dc554a55d87bea1d9e0a8dfa3334dff8926fad --- /dev/null +++ b/uva_cpp_clean/673/673-17.cpp @@ -0,0 +1,43 @@ +/** + * AOAPC II Exercise 6-1 Parenthesis Balance + * 12 WA; 13 AC; + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int kase; + cin >> kase; + string junk; + getline(cin, junk); + while (kase--) + { + string line; + getline(cin, line); + stack stk; + bool ok = true; + for (char c : line) + if (c == '(' || c == '[') + stk.push(c); + else + { + if (stk.empty()) + { + ok = false; + break; + } + char prev = stk.top(); + stk.pop(); + if (prev == '(' && c != ')' || prev == '[' && c != ']') + { + ok = false; + break; + } + } + if (!stk.empty()) + ok = false; + cout << (ok ? "Yes" : "No") << "\n"; + } +} diff --git a/uva_cpp_clean/673/673-18.cpp b/uva_cpp_clean/673/673-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4f8e355c4d4df9eedb7417e5ba6808a4a0f57835 --- /dev/null +++ b/uva_cpp_clean/673/673-18.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +using namespace std; + +int main(){ + int n; + scanf("%d", &n); + cin.get(); + while(n--){ + string input; + getline(cin, input); + stack pila; + for(int i = 0; input[i]; i++){ + if(!pila.empty() && input[i] == ')' && pila.top() == '(') pila.pop(); + else if(!pila.empty() && input[i] == ']' && pila.top() == '[') pila.pop(); + else if(input[i] != ' ') pila.push(input[i]); + } + if(pila.empty()) printf("Yes\n"); + else printf("No\n"); + } + return 0; +} +/* + La condicion para poder quitar el par de parentesis es que uno este inmediatamente uno al lado del otro, es por eso + que uso una pila, mis primeras soluciones era simplemente un contador que sumaba si encontraba "( o [" y restaba + cuando encontraba ") o ]", si ese contado al final daba cero es decir que si esta balanceado pero como no tenia + en cuenta la regla de que deben de estar inmediatamente al lado, entonces me mandaba WA. Me llevo tiempo pensar + en otra solucion porque el enunciado no es claro para mi. + Ejemplo: + ([)] no es posible debido a que "[" no tiene su contraparte de inmediado, para que sea posible tiene que ser + asi: ([]). +*/ \ No newline at end of file diff --git a/uva_cpp_clean/673/673-19.cpp b/uva_cpp_clean/673/673-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cd6b8306640472ec7ed17e73689ae22aabe8231 --- /dev/null +++ b/uva_cpp_clean/673/673-19.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + string s; + getline(cin, s); + while (n--) { + getline(cin, s); + stack st; + bool ok = true; + for (int i = 0; i < s.length() && ok; i++) { + if (s[i] == '(' || s[i] == '[') st.push(s[i]); + else { + if (!st.empty() && ((s[i] == ')' && st.top() == '(') || (s[i] == ']' && st.top() == '['))) + st.pop(); + else ok = false; + } + } + ok = ok && st.empty(); + if (ok) printf("Yes\n"); + else printf("No\n"); + } + return 0; +} diff --git a/uva_cpp_clean/673/673-20.cpp b/uva_cpp_clean/673/673-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..040bafa54ecd74e9569fe24f73c5e17c79dcb7c5 --- /dev/null +++ b/uva_cpp_clean/673/673-20.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +using namespace std; + +int p_calc[503]; + +int main() +{ + int tc; + + cin >> tc; + cin.get(); + while( tc-- ) + { + for(int i=0 ; i<503 ; i++) p_calc[i] = 0; + + char line[1005]; + gets(line); + + bool flag = true; + int pos = 0; + for(int i=0 ; i=0 ) + { + if( p_calc[pos-1]==1 ) + { + p_calc[pos-1] = 0; + pos--; + } + } + else + { + flag = false; + break; + } + } + + if( line[i]=='[' ) + { + p_calc[pos] = 3; + pos++; + } + + if( line[i]==']' ) + { + if( pos-1>=0 ) + { + if( p_calc[pos-1]==3 ) + { + p_calc[pos-1] = 0; + pos--; + } + } + else + { + flag = false; + break; + } + } + } + + if( flag && p_calc[0]==0 ) cout << "Yes" << endl; + else cout << "No" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/673/673-5.cpp b/uva_cpp_clean/673/673-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b08aee02fe179e32633e5a2e01c2a4af4d33ef4e --- /dev/null +++ b/uva_cpp_clean/673/673-5.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + int t; + string s; + cin>>t; + getchar(); + while(t--) + { + stackst; + getline(cin,s); + if(s.compare("\n")==0) + { + printf("Yes\n"); + continue; + } + if(s.size()==1||s.size()%2!=0) + { + pf("No\n"); + continue; + } + int f=0,i; + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 100 +using namespace std; +typedef long long ll; +int coin[] = {50, 25, 10, 5, 1}; +int make; +int dp[5][7490]; +int call(int i, int amount) { + if (i >= 5) { + if (amount == 0) return 1; + else return 0; + } + if (dp[i][amount] != -1) return dp[i][amount]; + int ret1 = 0, ret2 = 0; + if (amount - coin[i] >= 0) ret1 = call(i, amount - coin[i]); + ret2 = call(i + 1, amount); + //~ return dp[i][amount] = ret1 | ret2; ///check make or not... + return dp[i][amount] = ret1 + ret2; ///check how many away... +} +int main () { + memset(dp, -1, sizeof(dp)); + while (scanf("%d", &make) == 1) { + printf("%d\n", call(0,make)); + } + return 0; +} + diff --git a/uva_cpp_clean/674/674-19.cpp b/uva_cpp_clean/674/674-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7f2184897fda8c4d58a5dabf2de0dc2615aec29 --- /dev/null +++ b/uva_cpp_clean/674/674-19.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main() { + int n; + int count[7490]; + for (int i = 0; i < 7490; i++) count[i] = 1; + for (int i = 5; i < 7490; i++) count[i] += count[i-5]; + for (int i = 10; i < 7490; i++) count[i] += count[i-10]; + for (int i = 25; i < 7490; i++) count[i] += count[i-25]; + for (int i = 50; i < 7490; i++) count[i] += count[i-50]; + while (scanf("%d", &n) != EOF) { + printf("%d\n", count[n]); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/674/674-20.cpp b/uva_cpp_clean/674/674-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c354393632f6df7efcf9b682d36c5a6895c33c67 --- /dev/null +++ b/uva_cpp_clean/674/674-20.cpp @@ -0,0 +1,21 @@ +#include +#include + +using namespace std; + +const int coin[] = {1, 5, 10, 25, 50}; +int ways[7495]; + +int main() +{ + ways[0] = 1; + for(int i=0 ; i<5 ; i++) + for(int j=coin[i] ; j<7490 ; j++) ways[j] += ways[j-coin[i]]; + + int money; + + while( cin >> money ) + cout << ways[money] << endl; + + return 0; +} diff --git a/uva_cpp_clean/674/674-21.cpp b/uva_cpp_clean/674/674-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6325517898beb6cfb2216b41f0009ee52036d060 --- /dev/null +++ b/uva_cpp_clean/674/674-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 674 + Name: Coin Change + Problem: https://onlinejudge.org/external/6/674.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int C[] = {1,5,10,25,50}, DP[10143]; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + DP[0] = 1; + for (int i=0; i<5; ++i) + for (int j=0; j<10043; ++j) + DP[j+C[i]] += DP[j]; + + int n; + while (cin >> n) + cout << DP[n] << "\n"; +} diff --git a/uva_cpp_clean/674/674-5.cpp b/uva_cpp_clean/674/674-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e698329b5c7ce0768637612a9085d34aba364623 --- /dev/null +++ b/uva_cpp_clean/674/674-5.cpp @@ -0,0 +1,23 @@ +#include +#include +long long Mod = 100000007; +int main() +{ + long long coin[6]={1,5,10,25,50},way[7510]={0},i,j,a; + way[0]=1; + for(i=0;i<5;i++) + { + for(j=coin[i];j<=7500;j++) + { + way[j]+=way[j-coin[i]]; + if(way[j]>=Mod) + way[j]%=Mod; + } + } + while(scanf("%lld",&a)==1) + { + printf("%lld\n",way[a]); + } + return 0; +} + diff --git a/uva_cpp_clean/674/674-6.cpp b/uva_cpp_clean/674/674-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5ba48f6b1044ca74b335fb16ce3768178618b1a --- /dev/null +++ b/uva_cpp_clean/674/674-6.cpp @@ -0,0 +1,34 @@ +/* + dynamic programming > subset sum > with repetition + difficulty: easy + date: 23/Jan/2020 + by: @brpapa +*/ +#include +#include +using namespace std; + +const int N = 5; +int coins[N] = {1, 5, 10, 25, 50}; + +int memo[5][7500]; +int cc(int i, int v) { + // estado: moeda i, valor restante v para ser trocado + + if (v == 0) return 1; + if (v < 0 || i == N) return 0; + + int &ans = memo[i][v]; + if (ans != -1) return ans; + + return ans = cc(i+1, v) + cc(i, v-coins[i]); +} + +int main() { + memset(memo, -1, sizeof(memo)); + + int V; + while (cin >> V) + cout << cc(0, V) << endl; + return 0; +} diff --git a/uva_cpp_clean/674/674-9.cpp b/uva_cpp_clean/674/674-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6a5c09db3bb51dd9550e40c84221e93eb3de0f1 --- /dev/null +++ b/uva_cpp_clean/674/674-9.cpp @@ -0,0 +1,23 @@ +#include + +#define MAX_N 10 +#define MAX_W 7500 + +using namespace std; + +int n,v[]={1,5,10,25,50},memo[MAX_N][MAX_W]; + +int ways(int id,int sum){ + if(id==5 || sum<0) return 0; + if(sum==0) return 1; + if(~memo[id][sum]) return memo[id][sum]; + return memo[id][sum] = ways(id+1,sum)+ways(id,sum-v[id]); + +} + +int main(){ + memset(memo,-1,sizeof memo); + while(scanf("%d",&n)==1) + printf("%d\n",ways(0,n)); + return(0); +} diff --git a/uva_cpp_clean/677/677-21.cpp b/uva_cpp_clean/677/677-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e134e8c7af8f5ba0f58dc128505d4eba6114f52b --- /dev/null +++ b/uva_cpp_clean/677/677-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 677 + Name: All Walks of length "n" from the first node + Problem: https://onlinejudge.org/external/6/677.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +list adj[20]; +int path[20], l, n, x; +bool inside[20], somePrinted; +void dfs(int u, int d) { + if (d == l) { + printf("(1"); + for (int i=0; i + +using namespace std; + +int n, m, foo, tmp; +vector a[11], x; +bool ok, visited[11]; + +void backtracking(int p){ + if(x.size() == m + 1){ + putchar('('); + for(int i = 0; i < x.size(); i++) + i != x.size() - 1 ? printf("%d,", x[i]) : printf("%d)\n", x[i]); + ok = false; + return; + } + for(int i = 0; i < a[p].size(); i++){ + if(!visited[a[p][i]]){ + visited[a[p][i]] = true; + x.push_back(a[p][i]); + backtracking(a[p][i]); + visited[a[p][i]] = false; + x.pop_back(); + } + } +} + +int main(){ + visited[1] = true; + x.push_back(1); + while(scanf("%d%d", &n, &m) == 2){ + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++){ + scanf("%d", &tmp); + if(tmp) + a[i + 1].push_back(j + 1); + } + ok = true; + backtracking(1); + if(ok) + printf("no walk of length %d\n", m); + for(int i = 1; i <= n; i++) + a[i].clear(); + if(scanf("%d", &foo) == 1) + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/679/679-17.cpp b/uva_cpp_clean/679/679-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb55207de0c7846207a81c5f0abcdacb583dc7f9 --- /dev/null +++ b/uva_cpp_clean/679/679-17.cpp @@ -0,0 +1,28 @@ +/** + * AOAPC II Example 6-6 Dropping Balls + */ +#include +using namespace std; +int main() +{ + int T; + cin >> T; + while (T--) + { + int D, I; + cin >> D >> I; + int b = 1; + for (int i = 0; i < D - 1; ++i) + if (I % 2) + { + b *= 2; + I = (I + 1) / 2; + } + else + { + b = b * 2 + 1; + I /= 2; + } + cout << b << "\n"; + } +} diff --git a/uva_cpp_clean/679/679-18.cpp b/uva_cpp_clean/679/679-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..709a00ceac11b73a8b1e5bd099318f3dc0ef8256 --- /dev/null +++ b/uva_cpp_clean/679/679-18.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int d, l, maxN; + +int drop(int node, int balls){ + int left = 2 * node, right = 2 * node + 1; + if(left < maxN && right < maxN){ + if(balls % 2 == 0) return drop(right, balls / 2); + else return drop(left, balls/2 + 1); + } + else return node; +} + +int main(){ + int tc, stop; + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &d, &l); + maxN = pow(2, d); + printf("%d\n", drop(1, l)); + } + return 0; +} +/* + Problema muy estresante, spoilers: simulacion te da TLE. La estrategia es usar Binary Search para poder sacar + la respuesta, si nos ponemos a pensar, el numero de pelotas nos dice si el nodo permanece verdadero (par) o + falso (impar), ademas, que cada vez que se mueve una pelota, una mitad de las demas sigue el camino y la otra + se va por el lado contrario. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/679/679-5.cpp b/uva_cpp_clean/679/679-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ae8d815b509f3e16a3468992fdad9fb982ecdd1f --- /dev/null +++ b/uva_cpp_clean/679/679-5.cpp @@ -0,0 +1,172 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int main() +{ + while(1) + { + int t; + si(t); + + if(t==-1) break; + + while(t--) + { + int d,n; + sii(d,n); + + + int k=1,node=1,f=0; + while(k + +using namespace std; + +int tc, x, y, lim; +vector a[20], b[20]; + +int main(){ + a[0].push_back(1); + for(int i = 1; i < 20; i++){ + lim = 1 << i - 1; + for(int j = 0; j < lim; j++) + a[i].push_back(a[i - 1][j] << 1); + for(int j = 0; j < lim; j++) + a[i].push_back((a[i - 1][j] << 1) + 1); + } + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &x, &y); + printf("%d\n",a[x - 1][y - 1]); + } + return(0); +} diff --git a/uva_cpp_clean/681/681-21.cpp b/uva_cpp_clean/681/681-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64a140b0a0ac03d508555794b19df8623fff2814 --- /dev/null +++ b/uva_cpp_clean/681/681-21.cpp @@ -0,0 +1,95 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 681 + Name: Convex Hull Finding + Problem: https://onlinejudge.org/external/6/681.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 1e-8 +struct Point { + long long x, y; + Point():x(0),y(0){} + Point(long long x, long long y):x(x),y(y){} + bool operator < (const Point &o) const { return y == o.y ? x < o.x : y < o.y; } + Point operator - (const Point o) const { return Point(x-o.x, y-o.y); } + Point operator + (const Point o) const { return Point(x+o.x, y+o.y); } + long long operator * (const Point o) const { return x*o.x + y*o.y; } +}; + +std::istream& operator>> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} + +std::ostream& operator<< (std::ostream& stream, const Point p) { + stream << p.x << ' ' << p.y; + return stream; +} + +long long cross(const Point p1, const Point p2) { return p1.x*p2.y - p1.y*p2.x; } + +// leftTurn is positive +long long angle(const Point p1, const Point p2, const Point p3) { return cross(p2-p1, p3-p2); } + +// ----------------------------------------------------------------------------------------------- + +int hull(Point P[], int n, int S[]) { + sort(P, P+n); + + int sz = 0; + for (int i=0; i2 && angle(P[S[sz-3]], P[S[sz-2]], P[S[sz-1]]) <= 0) { + S[sz-2] = S[sz-1]; + --sz; + } + } + + bool sel[616] = {}; + for (int i=1; i=0; --i) if (!sel[i]) { + S[sz++] = i; + while (sz>t && angle(P[S[sz-3]], P[S[sz-2]], P[S[sz-1]]) <= 0) { + S[sz-2] = S[sz-1]; + --sz; + } + } + + return sz; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, tmp; + cin >> T; + cout << T << '\n'; + + Point P[616]; + while (T--) { + cin >> n; + for (int i=0; i> P[i]; + + int S[616]; + int sz = hull(P, n, S); + + cout << sz << '\n'; + for (int i=0; i> tmp) + cout << "-1\n"; + } +} diff --git a/uva_cpp_clean/686/686-15.cpp b/uva_cpp_clean/686/686-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8fd7c91aae7b16bd43ad7252e0239b9ee4706ea --- /dev/null +++ b/uva_cpp_clean/686/686-15.cpp @@ -0,0 +1,44 @@ +/* + 質數、建表 + 時間複雜度: O(N) +*/ +#include +#include + +using namespace std; +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, j, n, ans; + // 建質數表 + vector prime_table; + vector is_prime(32769, true); + is_prime[1] = false; + for (i = 2; i < 32769; i++) { + if (is_prime[i]) + prime_table.push_back(i); + for (j = 0; i * prime_table[j] < 32769; j++) { + is_prime[i * prime_table[j]] = false; + if (i % prime_table[j] == 0) + break; + } + } + + while (cin >> n && n) { + ans = j = 0; + i = n / 2; + + // 找到大於 n 的最小質數位置 + while (prime_table[j] <= i) + j++; + + // 判斷 n - (1 ~ n 之間的質數) 是不是質數 + for (i = 0; i < j; i++) + if (is_prime[n - prime_table[i]]) + ans++; + + cout << ans << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/686/686-19.cpp b/uva_cpp_clean/686/686-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d2d0da8e62838d82e9cdd25c6c1ca6a6d359de5 --- /dev/null +++ b/uva_cpp_clean/686/686-19.cpp @@ -0,0 +1,35 @@ +// I hope you enjoy this video: https://www.youtube.com/watch?v=MxiTG96QOxw +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef vector vi; + +ll sieve_size; +bitset<33010> bs; +vi primes; + +inline void sieve(ll upperbound = 33000) { + sieve_size=upperbound+1; + bs.set(); bs[0]=bs[1]=0; + for (ll i=2; i<=sieve_size; i++) if(bs[i]) { + for (ll j=i*i; j<=sieve_size; j+=i) bs[j] = 0; + primes.push_back(i); + } +} + +int main() { + sieve(); + int n; + while (cin >> n && n != 0) { + int res = 0; + for (int i = 0; i < primes.size(); i++) { + if (n - primes[i] < primes[i]) break; + if (bs[n - primes[i]]) res++; + } + printf("%d\n", res); + } + return 0; +} diff --git a/uva_cpp_clean/686/686-20.cpp b/uva_cpp_clean/686/686-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9674be205e7d535cf4585ef3b77232f967f090fe --- /dev/null +++ b/uva_cpp_clean/686/686-20.cpp @@ -0,0 +1,25 @@ +#include + +bool prime[1000005]; + +int main() +{ + prime[0] = prime[1] = false; + for(int i=2 ; i<1000005 ; i++) prime[i] = true; + + for(int i=2 ; i*i<1000005 ; i++) + if( prime[i] ) + for(int j=i+i ; j<1000005 ; j+=i) prime[j] = false; + + int n; + + while( scanf("%d", &n)==1 && n ) + { + int counter = 0; + for(int i=2 ; i<=n-i ; i++) + if( prime[i] && prime[n-i] ) counter++; + + printf("%d\n", counter); + } + return 0; +} diff --git a/uva_cpp_clean/686/686-5.cpp b/uva_cpp_clean/686/686-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e090d80cad11eb96656f39ddffac3bf9b816d72b --- /dev/null +++ b/uva_cpp_clean/686/686-5.cpp @@ -0,0 +1,92 @@ +/************************************* +******** Team : BUBT_HIDDEN ********** +************************************** +*********** Shipu Ahamed ************* +*************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2*acos(0.0) + +#define ft first +#define se second +#define st(s) s.size(); +#define r(input) freopen("input.txt","r",stdin) +#define w(output) freopen("output.txt","w",stdout) +#define maxall(v) *max_element(v.begin(),v.end()) +#define minall(v) *min_element(v.begin(),v.end()) +#define Sort(v) sort(v.begin(),v.end()) +#define un(v) Sort(v), v.erase(unique(v.begin(),v.end()),v.end()) +#define cover(a,d) memset(a,d,sizeof(a)) +using namespace std; + +bool prime[10000010]; +void sieve() +{ + prime[1]=false; + int n=sqrt(10000000); + for(int i=2;i<=n;i++) + { + if(prime[i]!=false) + { + for(int j=2*i;j<=10000000;j=j+i) + { + prime[j]=false; + } + } + } +} +int main() +{ + cover(prime,true); + sieve(); + int n; + while(sc("%d",&n)==1) + { + if(n==0) break; + int a,i,c=0; + mapmp; + for(i=2;i + +using namespace std; + +vector get_primes(int n){ + vector small_prime(n + 1), primes; + for(int i = 2 ; i <= n ; ++i){ + if(small_prime[ i ] == 0) + small_prime[ i ] = i, primes.push_back(i); + for(int j = 0 ; j < primes.size() && primes[ j ] <= small_prime[ i ] && i * primes[ j ] <= n ; ++j) + small_prime[ i * primes[ j ] ] = primes[ j ]; + } + return primes; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int N = (1 << 15); + const vector primes = get_primes(N); + vector ways(N << 1); + for(int i = 0 ; i < primes.size() ; ++i) + for(int j = i ; j < primes.size() ; ++j) + ++ways[ primes[ i ] + primes[ j ] ]; + for(int n ; cin >> n && n ; cout << ways[ n ] << endl); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/686/686-9.cpp b/uva_cpp_clean/686/686-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45f3c52e448459b76220d323573cec0e670cf7c8 --- /dev/null +++ b/uva_cpp_clean/686/686-9.cpp @@ -0,0 +1,35 @@ +#include + +const long long LIM = (int)1e6; + +using namespace std; + +bitsetprime; +vectorp; +int ct,n; + +void sieve(){ + prime[0] = 1; + for(int i=6;i<=LIM;i+=4) + prime[i] = prime[i-2] = 1; + p.push_back(2); + for(long long i=3;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + ll A, L,t = 1; + while (scanf("%lld %lld", &A, &L) == 2 && (A > 0 || L > 0)) { + ll n = A, cnt = 0; + while (n<=L && n != 1) { + if (n % 2 == 0) n = n / 2; + else n = 3 * n + 1; + cnt++; + } + if (n == 1) cnt++; + printf("Case %lld: A = %lld, limit = %lld, number of terms = %lld\n",t++, A, L, cnt); + } + + return 0; +} + diff --git a/uva_cpp_clean/694/694-18.cpp b/uva_cpp_clean/694/694-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ba19e4ba39e44c0a15eb6f2675ab386630d840b9 --- /dev/null +++ b/uva_cpp_clean/694/694-18.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main(){ + long A, L, cont, cCase = 1, aux; + while(true){ + cin >> A >> L; + aux = A; + if(A < 0 && L < 0) break; + cont = 0; + while(aux != 1 && aux <= L){ + cont++; + if(aux % 2 == 0) aux /= 2; + else aux = 3 * aux + 1; + if(aux == 1) cont++; + } + cout<<"Case "<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 694 + Name: The Collatz Sequence + Problem: https://onlinejudge.org/external/6/694.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() +{ + unsigned long int a,b,l,n=0,count,i=0; + while (cin>>a>>l && (a!=-1 && l!=-1)) + { + i++; + count=1;b=a; + while(a!=1){ + if(a%16==0)a/=16,count+=4; + else if(a%8==0)a/=8,count+=3; + else if(a%4==0)a/=4,count+=2; + while(a%2==0)a/=2,count++; + if(a>1)a=3*a+1; + if(a>l)break; + if(a>1)count++; + } + printf("Case %lu: A = %lu, limit = %lu, number of terms = %lu\n",i,b,l,count); + } + return 0; +} diff --git a/uva_cpp_clean/694/694-5.cpp b/uva_cpp_clean/694/694-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ccf5334bbbabc5b5dbeec37fd3a8fe49538b429c --- /dev/null +++ b/uva_cpp_clean/694/694-5.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2*acos(0.0) +using namespace std; +int main() +{ + ll n,e,c,a; + int no=0; + while(sc("%lld %lld",&n,&e)==2) + { + if(n<0&&e<0) + break; + c=0; + a=n; + while(n<=e) + { + if(n==1) + { + c++; + break; + } + else if(n%2==0) + { + c++; + n/=2; + } + else if(n%2==1) + { + c++; + n=n*3+1; + } + + } + pf("Case %d: A = %lld, limit = %lld, number of terms = %lld\n", + ++no,a,e,c); + } + return 0; +} + diff --git a/uva_cpp_clean/696/696-13.cpp b/uva_cpp_clean/696/696-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0278c0f26478dae112aa98ccb13b1a791352d696 --- /dev/null +++ b/uva_cpp_clean/696/696-13.cpp @@ -0,0 +1,66 @@ +#include +#include + +using namespace std; + +int n, m; + +int sovle(int n, int m) +{ + if (n > m) + { + swap(n, m); + } + + if (n == 1) + { + return m; + } + + if (n == 2) + { + return m / 4 * 4 + (m % 4 == 1 ? 2 : (m % 4 >= 2 ? 4 : 0)); + } + + return (n * m + 1) / 2; +} + +int main() +{ + cin >> m >> n; + + queue result; + + queue rows; + + queue columns; + + while (m != 0 || n != 0) + { + int r = 0; + + rows.push(m); + + columns.push(n); + + r = sovle(n, m); + + result.push(r); + + cin >> m >> n; + } + + while (!result.empty()) + { + cout << result.front() << " knights may be placed on a " << rows.front() << " row " << columns.front() << " column board." << endl; + + result.pop(); + + rows.pop(); + + columns.pop(); + } +} + + + diff --git a/uva_cpp_clean/696/696-15.cpp b/uva_cpp_clean/696/696-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e1b84b6b86721b1010bba6724ac8d3dcbd721d5 --- /dev/null +++ b/uva_cpp_clean/696/696-15.cpp @@ -0,0 +1,35 @@ +/* + N騎士問題 + 時間複雜度: O(1) +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, m, large, small, div, mod; + while (cin >> n >> m && n) { + if (n > m) { + large = n; + small = m; + } else { + large = m; + small = n; + } + + if (small == 1) + cout << large; // 一條線放滿 + else if (small == 2) { + div = large / 4; // 把 2 * large 拆成很多 2 * 4 的狀態 + mod = large % 4; + cout << div * 4 + (mod > 1 ? 4 : 2 * mod); + } else + cout << (large * small + 1) / 2; // 交錯放 + + cout << " knights may be placed on a " << n << " row " << m << " column board.\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/696/696-21.cpp b/uva_cpp_clean/696/696-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fb321f9f45af78adf29a65023244996ab27097e --- /dev/null +++ b/uva_cpp_clean/696/696-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 696 + Name: How Many Knights + Problem: https://onlinejudge.org/external/6/696.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int n, m, res; + while (cin>>n>>m && (n||m)) { + int a=min(n, m), b=max(n, m); + if (a==1) res = b; + else if (a==2) + res = b%4==1 ? 4*(b>>2)+2 : 4*((b-1)>>2)+4; + else res = (n*m+1)>>1; + printf("%d knights may be placed on a %d row %d column board.\n", min(n*m, max(4, res)), n, m); + } +} diff --git a/uva_cpp_clean/696/696-9.cpp b/uva_cpp_clean/696/696-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6b0755110bd3e4ed812d058dd7341cb0e1ab87c --- /dev/null +++ b/uva_cpp_clean/696/696-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int M, N, ans; + +int main(){ + while(scanf("%d %d", &M, &N), M | N){ + if( M == 1 or N == 1 ) ans = M * N; + else if( N == 2 or M == 2 ){ + ans = 4 * ( ( N * M ) / 8 ); + if( ( N * M ) % 8 == 2 ) ans += 2; + else if( ( N * M ) % 8 != 0 ) ans += 4; + } + else + ans = ( M * N + 1 ) / 2 + ( M == 2 or N == 2 ); + printf("%d knights may be placed on a %d row %d column board.\n", ans, M, N); + } + return(0); +} diff --git a/uva_cpp_clean/699/699-17.cpp b/uva_cpp_clean/699/699-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d57f03351685a4ec457c8728bc454d6c3135975a --- /dev/null +++ b/uva_cpp_clean/699/699-17.cpp @@ -0,0 +1,37 @@ +/** + * AOAPC II Example 6-10 The Falling Leaves + */ +#include +using namespace std; +vector sums; +void build(int r) +{ + int v; + cin >> v; + if (v <= 0) + return; + sums[r] += v; + build(r - 1); + build(r + 1); +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int T = 1, v; + while ((cin >> v) && v > 0) + { + sums = vector(100, 0); + sums[50] = v; + build(49); + build(51); + int pos = 0; + while (sums[pos] == 0) + ++pos; + cout << "Case " << T++ << ":\n" + << sums[pos++]; + while (sums[pos] > 0) + cout << " " << sums[pos++]; + cout << "\n\n"; + } +} diff --git a/uva_cpp_clean/700/700-21.cpp b/uva_cpp_clean/700/700-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1babb1cbbea3ace172486240b74fe8c06515487 --- /dev/null +++ b/uva_cpp_clean/700/700-21.cpp @@ -0,0 +1,47 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 700 + Name: Date Bugs + Problem: https://onlinejudge.org/external/7/700.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +int R[30], A[30], S[30], n; +bool check(int x) { + for (int i=0; i= b) + result = 100001; + else { + S[i] = b - A[i]; + R[i] = y - A[i]; + if (y > result) + result = y; + } + } + + for (; result<10000; ++result) + if (check(result)) + break; + + printf("Case #%d:\n", cse); + if (result < 10000) + printf("The actual year is %d.\n\n", result); + else puts("Unknown bugs detected.\n"); + } +} diff --git a/uva_cpp_clean/700/700-9.cpp b/uva_cpp_clean/700/700-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2d976f8f5bafc3947acd4f3bb4354755f65cc01 --- /dev/null +++ b/uva_cpp_clean/700/700-9.cpp @@ -0,0 +1,26 @@ +#include + +#define SIZE 30 + +using namespace std; + +int tc, n, y[SIZE], a[SIZE], b[SIZE], year; +bool ok; + +int main(){ + while(scanf("%d", &n), n){ + year = 0; + for(int i = 0;i < n; i++) scanf("%d %d %d", &y[i], &a[i], &b[i]), year = max(year, y[i]); + while(true){ + ok = true; + for(int i = 0; i < n; i++) + if((year - y[i]) % (b[i] - a[i])) ok = false; + if(ok || year == 10000) break; + year++; + } + printf("Case #%d:\n", ++tc); + if(year == 10000) puts("Unknown bugs detected.\n"); + else printf("The actual year is %d.\n\n", year); + } + return(0); +} diff --git a/uva_cpp_clean/701/701-21.cpp b/uva_cpp_clean/701/701-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16a7966a83ebd9cd0d69e418226fdf8b9d2b3087 --- /dev/null +++ b/uva_cpp_clean/701/701-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 701 + Name: The Archeologists' Dilemma + Problem: https://onlinejudge.org/external/7/701.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + double c = log2(10), lg2n, lg2n1, left, right; + + int n; + while (cin >> n) { + lg2n = log2(n); + lg2n1= log2(n+1); + + for (int len = log10(n)+2; ; ++len) { + left = lg2n + len * c; + right= lg2n1+ len * c; + if (int(left) < int(right)) { + cout << (int)ceil(left) << endl; + break; + } + } + } +} diff --git a/uva_cpp_clean/702/702-20.cpp b/uva_cpp_clean/702/702-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9519a9734a7c9ef9915cf9696246028edb4525e --- /dev/null +++ b/uva_cpp_clean/702/702-20.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +long long dp[40][40][2]; + +long long rec( int l, int r, int f ) { + if( (l + r) == 0 ) return 1; + long long &ret = dp[l][r][f]; + if( ret != -1 ) return ret; + ret = 0; + if( !f ) + for( int i = 1 ; i <= l ; ++i ) ret += rec( (i - 1), (l + r - i), 1 ); + else + for( int i = 1 ; i <= r ; ++i ) ret += rec( (i + l - 1), (r - i), 0 ); + return ret; +} + +int main( int argc, char ** argv ) { + int N, m; + while( cin >> N >> m ) { + memset( dp, -1, sizeof( dp ) ); + + if( m == 1 ) cout << ( (N >= 4) ? rec( 0, (N - 3), 1 ) : 1 ) << endl; + else cout << rec( (m - 1), (N - m), 0 ) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/703/703-21.cpp b/uva_cpp_clean/703/703-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59dec340661b2468a07559ac288520a1b1318d15 --- /dev/null +++ b/uva_cpp_clean/703/703-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 703 + Name: Triple Ties: The Organizer's Nightmare + Problem: https://onlinejudge.org/external/7/703.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector adj[143]; + int coi[143][143], eqi[143][143], res[161800][3], n; + while (cin >> n) { + for (int i=0; i> coi[i][j]; + } + + for (int i=0; ii ? k>j : k + +using namespace std; + +int n, m[110][110], ct; +vector ans[10000], v; +bool ok; + +int main(){ + while(scanf("%d", &n) == 1){ + ct = 0; + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + scanf("%d", &m[i][j]); + for(int a = 0; a < n; a++) + for(int b = 0; b < n; b++) + for(int c = 0; c < n; c++) + if( ( a < b && b < c ) || ( a > b && b > c ) ){ + if( m[a][b] && m[b][c] && m[c][a]){ + ans[ct].push_back(a + 1); + ans[ct].push_back(b + 1); + ans[ct++].push_back(c + 1); + } + else if( !m[a][b] && !m[b][c] && !m[c][a] && + !m[b][a] && !m[c][b] && !m[a][c]){ + v.clear(); + v.push_back(a + 1); + v.push_back(b + 1); + v.push_back(c + 1); + sort(v.begin(),v.end()); + ok = true; + for(int i = 0; i < ct; i++) + if(ans[i] == v){ + ok = false; + break; + } + if(ok) ans[ct++] = v; + } + } + printf("%d\n", ct); + for(int i = 0; i < ct; i++) + printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]), ans[i].clear(); + } + return(0); +} diff --git a/uva_cpp_clean/705/705-21.cpp b/uva_cpp_clean/705/705-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de6761d6cf1f0bcd89ff0e6f76ff518bb6ae9c2f --- /dev/null +++ b/uva_cpp_clean/705/705-21.cpp @@ -0,0 +1,84 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 705 + Name: Slash Maze + Problem: https://onlinejudge.org/external/7/705.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int ni[2][4] = { + { 1, 0, -1, 0 }, + { -1, 0, 1, 0 } +}; + +int nj[2][4] = { + { 0, -1, 0, 1 }, + { 0, 1, 0, -1 } +}; + +int nd[2][4] = { + { 3, 2, 1, 0 }, + { 1, 0, 3, 2 } +}; + +int pos[2][4] = { + { 0, 0, 1, 1 }, + { 1, 0, 0, 1 } +}; + +int w, h; +bool M[77][77], S[77][77][2]; +int dfs(int si, int sj) { + int dir=0, cnt=0, i=si, j=sj; + while (i>=0 && i=0 && j> w >> h && (w || h); ++cse) { + memset(M, 0, sizeof(M)); + memset(S, 0, sizeof(S)); + for (int i=0; i +#include +using namespace std; + +string output_type[5] = { + " ", + " |", + "| ", + "| |", + " -- ", +}; + +int digits[10][5] = { + {4, 3, 0, 3, 4}, // 0 + {0, 1, 0, 1, 0}, // 1 + {4, 1, 4, 2, 4}, // 2 + {4, 1, 4, 1, 4}, // 3 + {0, 3, 4, 1, 0}, // 4 + {4, 2, 4, 1, 4}, // 5 + {4, 2, 4, 3, 4}, // 6 + {4, 1, 0, 1, 0}, // 7 + {4, 3, 4, 3, 4}, // 8 + {4, 3, 4, 1, 4} // 9 +}; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, i, j, k, temp_n; + string s; + while (cin >> n >> s && n) { + for (auto&& c : s) + c -= '0'; + + for (i = 0; i < 5; i++) { + if (i == 0 || i == 2 || i == 4) + temp_n = 1; + else + temp_n = n; + + while (temp_n--) { + for (j = 0; j < s.length(); j++) { + cout << output_type[digits[s[j]][i]][0]; + + for (k = 0; k < n; k++) + cout << output_type[digits[s[j]][i]][1]; + + cout << output_type[digits[s[j]][i]][3]; + + if (j != s.length() - 1) + cout << ' '; + } + cout << "\n"; + } + } + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/706/706-19.cpp b/uva_cpp_clean/706/706-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2c598df3747eeb9b11de6a3512c3f3948232f02 --- /dev/null +++ b/uva_cpp_clean/706/706-19.cpp @@ -0,0 +1,58 @@ +#include +#include +using namespace std; + +int main() { + int s; + string n; + while (cin >> s >> n && !(s == 0 && n == "0")) { + for (int i = 0; i < n.length(); i++) { + char c = n[i] != '1' && n[i] != '4'? '-': ' '; + cout << ' '; + for (int j = 0; j < s; j++) { + cout << c; + } + cout << ' '; + if (i != n.length() - 1) cout << ' '; + } cout << endl; + for (int j = 0; j < s; j++) { + for (int i = 0; i < n.length(); i++) { + cout << (n[i] != '1' && n[i] != '2' && n[i] != '3' && n[i] != '7'? '|': ' '); + for (int j = 0; j < s; j++) { + cout << ' '; + } + cout << (n[i] != '5' && n[i] != '6'? '|': ' '); + if (i != n.length() - 1) cout << ' '; + } cout << endl; + } + for (int i = 0; i < n.length(); i++) { + char c = n[i] != '1' && n[i] != '7' && n[i] != '0'? '-': ' '; + cout << ' '; + for (int j = 0; j < s; j++) { + cout << c; + } + cout << ' '; + if (i != n.length() - 1) cout << ' '; + } cout << endl; + for (int j = 0; j < s; j++) { + for (int i = 0; i < n.length(); i++) { + cout << (n[i] != '1' && n[i] != '3' && n[i] != '4' && n[i] != '5' && n[i] != '7' && n[i] != '9'? '|': ' '); + for (int j = 0; j < s; j++) { + cout << ' '; + } + cout << (n[i] != '2'? '|': ' '); + if (i != n.length() - 1) cout << ' '; + } cout << endl; + } + for (int i = 0; i < n.length(); i++) { + char c = n[i] != '1' && n[i] != '7' && n[i] != '4'? '-': ' '; + cout << ' '; + for (int j = 0; j < s; j++) { + cout << c; + } + cout << ' '; + if (i != n.length() - 1) cout << ' '; + } cout << endl << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/706/706-20.cpp b/uva_cpp_clean/706/706-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba8c4a6fd5b8255d5ecbf90f1185a4cd295e9cd5 --- /dev/null +++ b/uva_cpp_clean/706/706-20.cpp @@ -0,0 +1,55 @@ +#include +#include + +const char table[10][7] = +{ +'-','|','|',' ','|','|','-', +' ','|',' ',' ','|',' ',' ', +'-','|',' ','-',' ','|','-', +'-','|',' ','-','|',' ','-', +' ','|','|','-','|',' ',' ', +'-',' ','|','-','|',' ','-', +'-',' ','|','-','|','|','-', +'-','|',' ',' ','|',' ',' ', +'-','|','|','-','|','|','-', +'-','|','|','-','|',' ','-', +}; +int s, len, digit; +char n[11]; + +int main() +{ + while( scanf("%d %s", &s, n)==2 && s && n ) + { + len = strlen(n); + for(int i=0 ; i<2*s+3 ; i++) + { + for(int j=0 ; j0) && i<(s + 1) ) + { + printf("%c", table[digit][2]); + for(int k=0 ; k(s+1) && i<(2*s+2) ) + { + printf("%c", table[digit][5]); + for(int k=0 ; k>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 706 + Name: LC-Display + Problem: https://onlinejudge.org/external/7/706.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +void coutX(char ch,int x){ + while(x--)cout<9 || nt<0)continue; + cout<<(lcd[nt][i]?"|":" "); + coutX(' ',s); + cout<<((lcd[nt][i+1])?"|":" "); + if(k9 || nt<0)continue; + cout<<" "; + coutX(lcd[nt][i]?'-':' ',s); + cout<<" "; + if(k>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 710 + Name: The Game + Problem: https://onlinejudge.org/external/7/710.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +bool inRange(int x, int l, int r) { return x>=l && x>w>>h && (w||h); ++brd) { + cin.ignore(100, '\n'); + for (int i=1; i<=h; ++i) { + cin.getline(M[i]+1, 143); + for (int j=strlen(M[i]+1)+1; j<=w; ++j) + M[i][j] = ' '; + M[i][0] = M[i][w+1] = ' '; + M[i][w+2] = 0; + } + w+=2; h+=2; + memset(M[0], ' ', w); M[0][w]=0; + memset(M[h-1], ' ', w); M[h-1][w]=0; + + cout << "Board #" << brd << ":\n"; + for (int gme=1; cin>>x1>>y1>>x2>>y2 && (x1||y1||x2||y2); ++gme) { + cout << "Pair " << gme << ": "; ++cse; + int result = bfs(x1, y1, x2, y2); + if (result < 0) cout << "impossible.\n"; + else cout << result << " segments.\n"; + } + cout << '\n'; + } +} diff --git a/uva_cpp_clean/710/710-9.cpp b/uva_cpp_clean/710/710-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d640d4057b2ff24ffe6a24ec73c5e7d4e603ddb --- /dev/null +++ b/uva_cpp_clean/710/710-9.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +const int SIZE = 100, INF = INT_MAX, N_DIR = 5; + +int tc, q, ans, n, m, sRow, sCol, tRow, tCol, memo[SIZE][SIZE][N_DIR]; +char grid[SIZE][SIZE], line[SIZE]; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +void printBoard() { + printf("Board #%d:\n", ++tc); +} + +void printPair() { + if (ans == INF) printf("Pair %d: impossible.\n", ++q); + else printf("Pair %d: %d segments.\n", ++q, ans); +} + +bool isValid(int r, int c) { + return (0 <= r and 0 <= c and r < n and c < m and grid[r][c] == ' '); +} + +void backtrack(int r, int c, int d) { + for (int dir = 0; dir < 4; dir++) { + int nr = r + dr[dir], nc = c + dc[dir]; + if (isValid(nr, nc) and memo[r][c][d] + (d != dir) <= memo[nr][nc][dir]) { + memo[nr][nc][dir] = memo[r][c][d] + (d != dir); + backtrack(nr, nc, dir); + } + } +} + +void clear() { + ans = INF; + for (int row = 0; row < n; row++) + for (int col = 0; col < m; col++) + for (int dir = 0; dir < N_DIR; dir++) + memo[row][col][dir] = INF; +} + +void solve() { + clear(); + grid[tRow][tCol] = ' '; + for (int dir = 0; dir < N_DIR; dir++) memo[sRow][sCol][dir] = 0; + backtrack(sRow, sCol, 4); + ans = memo[tRow][tCol][0]; + for (int dir = 1; dir < N_DIR; dir++) ans = min(ans, memo[tRow][tCol][dir]); + grid[tRow][tCol] = 'X'; +} + + +bool query() { + cin.getline(line, SIZE); + sscanf(line, "%d %d %d %d\n", &sCol, &sRow, &tCol, &tRow); + return (sRow or sRow or tRow or tCol); +} + +inline void init() { + q = 0; + memset(grid, ' ', sizeof grid); +} + +bool readGrid() { + init(); + cin.getline(line, SIZE); + sscanf(line, "%d %d\n", &m, &n); + if (m == 0 and n == 0) return false; + for (int row = 1; row <= n; row++) { + cin.getline(grid[row] + 1, SIZE); + grid[row][m + 1] = ' '; + } + m += 2, n += 2; + return true; +} + +int main() { + while (readGrid()) { + printBoard(); + while (query()) { + solve(); + printPair(); + } + putchar('\n'); + } + return (0); +} diff --git a/uva_cpp_clean/711/711-9.cpp b/uva_cpp_clean/711/711-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6bd195d2d3537ea058e5edca68f42695e609568 --- /dev/null +++ b/uva_cpp_clean/711/711-9.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +const int MAX_N = 6, OFFSET = 5, MAX_EVEN = 10, MAX_ODD = 9; + +int T, sum, ct[MAX_N + OFFSET]; +bool zero, valid; + +void backtracking(int num = 1, int s = 0) { + if (num > MAX_N) { + valid |= s == sum; + return; + } + if (valid) return; + if (ct[num]) { + ct[num]--; + backtracking(num, s + num); + ct[num]++; + } + backtracking(num + 1, s); +} + +void reduce() { + for (int i = 1; i <= MAX_N; i++) + ct[i] = (ct[i] & 1) ? min(ct[i], MAX_ODD) : min(ct[i], MAX_EVEN); + sum = 0; + for (int i = 1; i <= MAX_N; i++) sum += i * ct[i]; + sum >>= 1; +} + +int getSum() { + int S = 0; + for (int i = 1; i <= MAX_N; i++) S += i * ct[i]; + return S; +} + +int main() { + while (true) { + valid = false; + zero = true; + for (int i = 1; i <= MAX_N; i++) { + scanf("%d", ct + i); + zero &= ct[i] == 0; + } + if (zero) break; + printf("Collection #%d:\n", ++T); + if (getSum() & 1) { + puts("Can't be divided.\n"); + continue; + } + reduce(); + backtracking(); + puts(valid ? "Can be divided.\n" : "Can't be divided.\n"); + } + return (0); +} diff --git a/uva_cpp_clean/712/712-17.cpp b/uva_cpp_clean/712/712-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbcb10e4caf6178d6f168f8c24a1388346e17a7f --- /dev/null +++ b/uva_cpp_clean/712/712-17.cpp @@ -0,0 +1,33 @@ +/** + * AOAPC II Exercise 6-2 S-Trees + * 45 AC; + */ +#include +using namespace std; +int main() +{ + int n, kase = 0; + while (cin >> n && n) + { + cout << "S-Tree #" << ++kase << ":\n"; + string ordering, terminals; + getline(cin, ordering); + getline(cin, ordering); + cin >> terminals; + vector ord; + for (int i = 1; i < 3 * n; i += 3) + ord.push_back(ordering[i] - '1'); + int m; + cin >> m; + while (m--) + { + string vva; + cin >> vva; + int pos = 0; + for (int i = 0; i < n; ++i) + pos += (1 << n - 1 - i) * (vva[ord[i]] - '0'); + cout << terminals[pos]; + } + cout << "\n\n"; + } +} diff --git a/uva_cpp_clean/712/712-19.cpp b/uva_cpp_clean/712/712-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee272a8488a8aa9654de7b1b25c38d817bb6145e --- /dev/null +++ b/uva_cpp_clean/712/712-19.cpp @@ -0,0 +1,34 @@ +#include +#include +using namespace std; + +int main() { + int n; + int c = 1; + while (cin >> n && n != 0) { + vector var; + for (int i = 0; i < n; i++) { + char c; + int val; + cin >> c >> val; + var.push_back(val); + } + string dist; + cin >> dist; + int m; + cin >> m; + printf("S-Tree #%d:\n", c++); + for (int i = 0; i < m; i++) { + string path; + cin >> path; + int pos = 0; + for (int i = 0; i < n; i++) { + pos <<= 1; + pos += path[i] == '1'? 1: 0; + } + printf("%c", dist[pos]); + } + printf("\n\n"); + } + return 0; +} diff --git a/uva_cpp_clean/713/713-19.cpp b/uva_cpp_clean/713/713-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01736adb49431c73e0469ed0cff81e02d918d81d --- /dev/null +++ b/uva_cpp_clean/713/713-19.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +int main() { + int N; + cin >> N; + while (N--) { + string a, b; + cin >> a >> b; + bool carry = false, zeros = true; + int i = 0; + for (; i < a.length() && i < b.length(); i++) { + int val = a[i] + b[i] - '0' - '0'; + if (carry) val++; + if (!(zeros && val%10 == 0)) { + zeros = false; + printf("%c", val%10 + '0'); + } + carry = val >= 10; + } + for (; i < a.length(); i++) { + int val = a[i] - '0'; + if (carry) val++; + if (!(zeros && val%10 == 0)) { + zeros = false; + printf("%c", val%10 + '0'); + } + carry = val >= 10; + } + for (; i < b.length(); i++) { + int val = b[i] - '0'; + if (carry) val++; + if (!(zeros && val%10 == 0)) { + zeros = false; + printf("%c", val%10 + '0'); + } + carry = val >= 10; + } + if (carry) printf("1"); + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/713/713-5.cpp b/uva_cpp_clean/713/713-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8a4e9c982eb4193d67cbd5c3ca226e78b87b4707 --- /dev/null +++ b/uva_cpp_clean/713/713-5.cpp @@ -0,0 +1,67 @@ +#include +#include +int main() +{ +char a[260],b[260],p[205]; +int t,l,len,e,f,i,r,s,d,y,q,j; +scanf("%d",&t); +while(t--) +{ +scanf("%s%s",&a,&b); +l=strlen(a); +len=strlen(b); +if(l>len) +{ +y=l; +for(i=len;il) +{ +y=len; +for(i=l;i9) +{ +r=s%10; +p[j]=r+'0'; +f=1; +} + else{ + p[j]=s+'0'; + f=0; + } +} +if(f==1){ + p[j]=f+'0'; + p[j+1]='\0'; + } +else{ + p[j]='\0'; + } + i=0; +y=strlen(p); +while (p[i]=='0') +i++; +for (;i +#include +#include +#include + +using namespace std; + +bool is_possible(const long long &page_per_group, const int &n_group, const vector &books) { + long long sum = 0; + int i = books.size() - 1, j = 0; + + while (i > -1) { + if (sum + books[i] > page_per_group) { + sum = 0; + j++; // 下一堆 + } else + sum += books[i--]; // 還放得下就塞進去 + if (j == n_group) + return false; // 分太多組,代表 page_per_group 太小 + } + return true; // 分太少組,代表 page_per_group 太大 +} + +void slash_pos(const long long &page_per_group, const int &n_group, vector &slash_position, const vector &books) { + long long sum = 0; + int i = books.size() - 1, j = 1; + + while (i > -1) { + if (sum + books[i] <= page_per_group && i >= n_group - j) { //每組至少一個 + sum += books[i--]; + } else { + j++; + sum = 0; + slash_position.push_back(i + 1); // 紀錄第幾個後要印出 / + } + if (j > n_group) + break; + } + return; +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_case, n_book, n_group, i, j; + long long lower, upper, mid; + vector slash_position; + slash_position.reserve(n_group); + + cin >> n_case; + while (n_case--) { + cin >> n_book >> n_group; + + vector books(n_book, 0); + + for (i = 0; i < n_book; i++) + cin >> books[i]; + + lower = *max_element(books.begin(), books.end()); // 下界是分成 n_book 組的情形 + upper = accumulate(books.begin(), books.end(), 0LL); // 上界是只分一組的情形 + + // 二分搜尋找最多頁的一組的最小頁數 + do { + mid = (lower + upper) / 2; + if (is_possible(mid, n_group, books)) + upper = mid; + else + lower = mid + 1; + } while (lower < upper); + + slash_pos(lower, n_group, slash_position, books); + + cout << books[0]; + for (i = 1; i < n_book; i++) { + if (!slash_position.empty() && i == slash_position.back()) { + cout << " /"; + slash_position.pop_back(); + } + cout << " " << books[i]; + } + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/714/714-19.cpp b/uva_cpp_clean/714/714-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27a0914edb680a254abaef667939977fb2b868a7 --- /dev/null +++ b/uva_cpp_clean/714/714-19.cpp @@ -0,0 +1,45 @@ +#include +#include +using namespace std; + +int main() { + int books[500], N; + cin >> N; + while (N--) { + int m, k; + cin >> m >> k; + long long end = 0, ini = 0; + for (int i = 0; i < m; i++) { + cin >> books[i]; + end += books[i]; + if (books[i] > ini) ini = books[i]; + } + while (ini < end) { + long long mid = (ini+end)/2; + long long acum = 0, count = 1; + for (int i = 0; i < m; i++) { + acum += books[i]; + if (acum > mid) count++, acum = books[i]; + } + if (count == k) end = mid; + else if (count > k) ini = mid + 1; + else end = mid - 1; + } + stack s; + long long acum = 0, scr = 1; + for (int i = m-1; i >= 0; i--) { + if (acum + books[i] > ini || scr + i < k) acum = 0, s.push(-1), scr++; + acum += books[i]; s.push(books[i]); + } + bool first = false; + while (!s.empty()) { + if (first) printf(" "); + else first = true; + if (s.top() == -1) printf("/"); + else printf("%d", s.top()); + s.pop(); + } + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/714/714-21.cpp b/uva_cpp_clean/714/714-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..542c566b146bf2420ff1d87785c00fa5103e57fc --- /dev/null +++ b/uva_cpp_clean/714/714-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 714 + Name: Copying Books + Problem: https://onlinejudge.org/external/7/714.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +#define INF 4611686018427387904 +typedef unsigned long long Long; + +Long DP[600][600], sum; +int main(){ + int T, n, k, X[600], Pa[600]; + scanf("%d", &T); + while (T--) { + + sum = 0; + scanf("%d%d", &n, &k); + + for (int i=0; i=0; i--) { + sum += X[i]; + if (sum>DP[k][n] || i+1 +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_case, i, j, k, difference, n; + char str[10001]; + + cin >> n_case; + while (n_case--) { + cin >> str; + + i = 0; + j = 1; + n = strlen(str); + + while (j < n) { + // 同樣的字,找到第一個不一樣的字,下面就能知道 i 開頭還是 j 開頭字典序比較小 + k = 0; + do { + difference = str[i + k] - str[(j + k) % n]; + k++; + } while (difference == 0 && i + k < n); + + if (difference > 0) { + i += k; // 找到字典序更小的字,把 i 移到字上(相當於以此為開頭搜尋) + j = max(i + 1, j); // j 至少要是 i + 1,越大越好 + } else + j += k; // 找到字典序更大的字,把 j 移到字後(跳過他) + } + cout << i + 1 << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/721/721-19.cpp b/uva_cpp_clean/721/721-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..611c738a2fc8191d4e32453ace7f0ed56408a8c6 --- /dev/null +++ b/uva_cpp_clean/721/721-19.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair dd; +typedef pair ii; +typedef pair di; +typedef pair id; +typedef pair ddii; +typedef vector vii; +typedef vector vid; +typedef vector vd; +typedef vector vi; + +vector AdjList1, AdjList2; + +int P, Q; + +int dijkstra(int s, vector * AdjList){ + vi dist(P, 1000000000); dist[s]=0; + priority_queue, greater > pq; + pq.push(ii(0,s)); + while(!pq.empty()){ + ii front=pq.top(); pq.pop(); + int d=front.first, u=front.second; + if (d > dist[u]) continue; + for(int j=0; j< (int)(*AdjList)[u].size(); j++){ + ii v = (*AdjList)[u][j]; + if (dist[u] + v.second < dist[v.first]){ + dist[v.first] = dist[u] + v.second; + pq.push(ii(dist[v.first],v.first)); + }}} + int ret = 0; + for (int i = 0; i < dist.size();i++) ret += dist[i]; + return ret; +} + +int main() { + int N; + cin >> N; + while (N--) { + cin >> P >> Q; + AdjList1.assign(P, vii()); + AdjList2.assign(P, vii()); + for (int i = 0; i < Q; i++) { + int x, y, d; + cin >> x >> y >> d; + AdjList1[x-1].push_back(make_pair(y-1, d)); + AdjList2[y-1].push_back(make_pair(x-1, d)); + } + int cost = dijkstra(0, &AdjList1) + dijkstra(0, &AdjList2); + printf("%d\n", cost); + } + return 0; +} diff --git a/uva_cpp_clean/722/722-15.cpp b/uva_cpp_clean/722/722-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98f3e81e2110f504210bc494b76b1b8daf588756 --- /dev/null +++ b/uva_cpp_clean/722/722-15.cpp @@ -0,0 +1,53 @@ +/* + 連通塊大小 +*/ +#include +#include +#include + +using namespace std; + +void DFS(const int x, const int y, const int& row, const int& column, vector& field, int& ans) { + if (x < 0 || x >= row || y < 0 || y >= column || field[x][y] == '1') + return; + + field[x][y] = '1'; + ans++; + + DFS(x + 1, y, row, column, field, ans); + DFS(x - 1, y, row, column, field, ans); + DFS(x, y + 1, row, column, field, ans); + DFS(x, y - 1, row, column, field, ans); +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_case, i, j, ans, row, column; + string k; + vector field; + + cin >> n_case; + while (n_case--) { + cin >> i >> j; + cin.ignore(1024, '\n'); + + ans = 0; + + while (getline(cin, k) && k[0]) + field.push_back(k); + + row = field.size(); + column = field[0].length(); + + DFS(i - 1, j - 1, row, column, field, ans); + + cout << ans << "\n"; + + if (n_case) + cout << "\n"; + + field.clear(); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/722/722-19.cpp b/uva_cpp_clean/722/722-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..866691df6dab732fb32b0e59bbd9fd0ddbce3ef1 --- /dev/null +++ b/uva_cpp_clean/722/722-19.cpp @@ -0,0 +1,40 @@ +#include +#include + +using namespace std; + +char grid[100][100]; + +int limx, limy; +int inix, iniy; +char water; + +int area(int x, int y){ + if(x<0 || x>=limx || y<0 || y>=limy || grid[x][y] != water) return 0; + else{ + grid[x][y]='q'; + return 1 + area(x+1, y) + area(x-1, y) + area(x, y+1) + area(x, y-1); + } +} + +int main(){ + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + int cases; + cin >> cases; + for(int i = 0; i> iniy >> inix; + string line; + limx = limy = 0; + getline(cin, line); + while(getline(cin, line) && line.size() != 0){ + limx = line.size(); + for(int j = 0; j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 722 + Name: Lakes + Problem: https://onlinejudge.org/external/7/722.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 1000 +char M[MAXN][MAXN]; +int n, m; + +int dfs(int r, int c){ + if (r<0 || r>=n || c<0 || c>=m || M[r][c]=='1') return 0; + M[r][c] = '1'; + + return dfs(r+1,c) + dfs(r-1, c) + dfs(r, c+1) + dfs(r, c-1) + 1; +} + +int main(){ + int T, r, c; + cin>>T; + while (T--) { + cin>>r>>c; + cin.getline(M[0], MAXN); + for (n=0; cin.getline(M[n], MAXN) && (M[n][0]=='1' || M[n][0]=='0'); n++); + m = strlen(M[0]); + + cout << dfs(r-1, c-1) << endl; + if (T) putchar('\n'); + } +} diff --git a/uva_cpp_clean/722/722-9.cpp b/uva_cpp_clean/722/722-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6bcb2403d7c9a5140220eda76fe6ad3216b22ce --- /dev/null +++ b/uva_cpp_clean/722/722-9.cpp @@ -0,0 +1,41 @@ +#include + +#define SIZE 110 + +using namespace std; + +int tc, N, M, row, col; +char line[SIZE], grid[SIZE][SIZE]; +int dr[] = {0, 1, 0, -1}; +int dc[] = {1, 0, -1, 0}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M ); +} + +int floodfill(int r, int c){ + if( not is_possible(r, c) ) return 0; + if( grid[r][c] == '1' ) return 0; + grid[r][c] = '1'; + int ans = 1; + for(int d = 0; d < 4; d++) + ans += floodfill(r + dr[d], c + dc[d]); + return ans; +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + cin.getline(line, SIZE); + sscanf(line, "%d %d", &row, &col); + N = 0; + while(cin.getline(line, SIZE)){ + if( sscanf(line, "%s", grid[N]) == -1 ) break; + N++; + } + M = strlen(grid[0]); + printf("%d\n", floodfill(row - 1, col - 1)); + if( tc ) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/725/725-11.cpp b/uva_cpp_clean/725/725-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2dfdf6e21a67c6f4d050e5be39c3c6234cd8910 --- /dev/null +++ b/uva_cpp_clean/725/725-11.cpp @@ -0,0 +1,69 @@ +#include +/* +Problem: 725 - Division +Link : https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=666 + +Solution by: Mohamed Hisham El-Banna + +Gmail : Mohamed00Hisham@Gmail.com +Github : www.github.com/Mhmd-Hisham +LinkedIn: www.linkedin.com/in/Mhmd-Hisham +*/ + +using namespace std; + +typedef signed long long ll; +typedef unsigned long long ull; + +int N; + +int main () { + + scanf("%d\n", &N); + while ( N ){ + + bool found = false; + for (int b = 1234; b <= 98765/N; b++){ + int a = b*N; // since a/b = N; + int tmp, used = (b < 10000); + + tmp = a; while(tmp) { used |= 1<<(tmp%10); tmp /= 10; } + tmp = b; while(tmp) { used |= 1<<(tmp%10); tmp /= 10; } + + if (used == ((1<<10) - 1)) { + printf("%0.5d / %0.5d = %d\n", a, b, N); + found = true; + } + } + + if (!found) + printf("There are no solutions for %d.\n", N); + + scanf("%d\n", &N); + if (N) printf("\n"); + } + + return 0; +} +/* +Sample input:- +----------------- +61 +62 +0 + +Sample output:- +----------------- +There are no solutions for 61. + +79546 / 01283 = 62 +94736 / 01528 = 62 + +Resources:- +------------- +Video Solution (Arabic) []: + +Explanation: +--------------- + +*/ diff --git a/uva_cpp_clean/725/725-14.cpp b/uva_cpp_clean/725/725-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02f1e3373f57c27f0880e77218e9cd9430633555 --- /dev/null +++ b/uva_cpp_clean/725/725-14.cpp @@ -0,0 +1,150 @@ +/*************************************************** + * Problem Name : 725 - Division.cpp + * Problem Link : https://uva.onlinejudge.org/external/7/725.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-07-16 + * Problem Type : AdHoc + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + __FastIO; + ll n; + int t = 1; + + while (cin >> n) { + if (n == 0) break; + + vectorv; + vectorans; + + for (int i = 1234; i <= 98675; i++) { + ll mul = n * i; + ll chk ; + + if (mul >= 10000 && mul < 1000000) { + bool flag = 1; + chk = i; + mapmp; + + if (i < 10000) { + mp[0]++; + } + + string str; + + while (chk != 0) { + int r = chk % 10; + chk /= 10; + + if (mp[r] == 0) { + mp[r]++; + str.pb (r + '0'); + + } else { + flag = 0; + break; + } + } + + if (flag) { + string s = ""; + chk = mul; + + while (chk != 0) { + int r = chk % 10; + chk /= 10; + + if (mp[r] == 0) { + mp[r]++; + s.pb (r + '0'); + + } else { + flag = 0; + break; + } + } + + if (flag) { + reverse (all (str) ); + reverse (all (s) ); + v.pb (s); + ans.pb (str); + } + } + } + } + + if (t != 1) { + nl; + } + + if ( (int) v.size() == 0) { + cout << "There are no solutions for " << n << ".\n"; + + } else { + for (int i = 0; i < (int) v.size(); i++) { + cout << v[i] << " / "; + string str = ""; + str = ans[i]; + + if ( (int) str.size() == 4) { + cout << 0; + } + + cout << ans[i]; + cout << " = " << n << "\n"; + } + } + t++; + } + + return 0; +} diff --git a/uva_cpp_clean/725/725-17.cpp b/uva_cpp_clean/725/725-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..800f4c34e364698b71dfc011d89b926a9a79d121 --- /dev/null +++ b/uva_cpp_clean/725/725-17.cpp @@ -0,0 +1,44 @@ +/** + * Division + * AOAPC II Example 7-1 + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + cout << setfill('0'); + int n; + bool first = true; + while (cin >> n && n) + { + if (first) + first = false; + else + cout << "\n"; + bool found = false; + for (long long abcde = 10234; abcde <= 98765; ++abcde) + { + if (abcde % n) + continue; + int fghij = abcde / n; + string str = to_string(abcde * 100000 + fghij); + sort(str.begin(), str.end()); + bool ok = true; + for (int i = 0; i < 10; ++i) + if (str[i] != '0' + i) + { + ok = false; + break; + } + if (ok) + { + cout << abcde << " / " << setw(5) << fghij << " = " << n << "\n"; + found = true; + } + } + if (!found) + cout << "There are no solutions for " << n << ".\n"; + } +} diff --git a/uva_cpp_clean/725/725-18.cpp b/uva_cpp_clean/725/725-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a7a89c5b03540d4fb0bfa06d2e45d6b1b4c7f0b0 --- /dev/null +++ b/uva_cpp_clean/725/725-18.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int main(){ + int n; + bool nl = false; + while(scanf("%d", &n) == 1 && n != 0){ + if(nl) printf("\n"); + nl = true; + bool exists = false; + for(int i = 1234; i <= 99999; i++){ + if(i * n > 99999) break; + bool used[10], possible = true; + for(int j = 0; j < 10; j++) used[j] = false; + char number[10]; + sprintf(number, "%05d", i); //Genera una string con un formato a partir de un entero + for(int j = 0; number[j]; j++) used[number[j] - '0'] = true; + sprintf(number, "%05d", i * n); + for(int j = 0; number[j]; j++) used[number[j] - '0'] = true; + for(int j = 0; j < 10; j++){ + if(!used[j]){ + possible = false; + break; + } + } + if(possible){ + printf("%05d / %05d = %d\n", i * n, i, n); + exists = true; + } + } + if(!exists) printf("There are no solutions for %d.\n", n); + } + return 0; +} +/* + La estrategia es ir hallando a fuerza bruta la solucion, una manera facil es hallar un numero y en un vector de + booleanos marcar como verdadero los numeros usados, luego a ese numero se le multiplica por el numero de la input y + se marcan en el mismo vector los numeros usados, si todo el vector esta en verdadero entonces esa es respuesta + (division entre 1), ya que el ejercicio pide que sean numeros diferentes, ademas, como el numero puede empezar + con cero por delante, se empieza por el 1234. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/725/725-19.cpp b/uva_cpp_clean/725/725-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93f2b9c6ee39175f2f2951c1d1e758bf734dbfcb --- /dev/null +++ b/uva_cpp_clean/725/725-19.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; + +vector > sol; +bool used[10]; +int num = 0, den = 0; + +void proc(int p) { + if (p == 10) { + if (num%den == 0) { + sol[num/den].insert(make_pair(num, den)); + } + } else { + for (int i = 9; i >= 0; i--) { + if (!used[i]) { + used[i] = true; + if (p < 5) num = num*10 + i; + else den = den*10 + i; + proc(p+1); + used[i] = false; + if (p < 5) num /= 10; + else den /= 10; + } + } + } +} + +int main() { + sol.assign(81, set()); + for (int i = 0; i < 10; i++) used[i] = false; + proc(0); + int N; + bool first = false; + while (cin >> N && N != 0) { + if (first) printf("\n"); + else first = true; + if (sol[N].size() == 0) printf("There are no solutions for %d.\n", N); + else { + for (auto it = sol[N].begin(); it != sol[N].end(); ++it) { + ii f = *it; + printf("%.5d / %.5d = %d\n", f.first, f.second, N); + } + } + } + return 0; +} diff --git a/uva_cpp_clean/725/725-2.cpp b/uva_cpp_clean/725/725-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38e38c3e3d5a2e97556f68a0f9d36b0f32b0c8b6 --- /dev/null +++ b/uva_cpp_clean/725/725-2.cpp @@ -0,0 +1,63 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +int main(){ + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int n, count = 0; + while (scanf("%d", &n)) { + if (!n) break; + bool f = 0; + count++; + if (count > 1) + printf("\n"); + for (int fghij = 1234; fghij <= 98765 / n; ++fghij) { + int abcde = fghij * n; + int tmp, used = (fghij < 10000); + tmp = abcde; + while (tmp) { + used |= 1 << (tmp % 10); + tmp /= 10; + } + tmp = fghij; + while (tmp) { + used |= 1 << (tmp % 10); + tmp /= 10; + } + if (used == (1<<10) - 1) { + printf("%0.5d / %0.5d = %d\n", abcde, fghij, n); + f = 1; + } + } + if (!f) printf("There are no solutions for %d.\n", n); + // puts(""); + } + return 0; +} diff --git a/uva_cpp_clean/725/725-21.cpp b/uva_cpp_clean/725/725-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5cfc8bfece527ad57932356dbde3d4c8bbc8f89 --- /dev/null +++ b/uva_cpp_clean/725/725-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 725 + Name: Division + Problem: https://onlinejudge.org/external/7/725.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int N; +int used[11],cnt; +int x[3628801]; +int y[3628801]; +void BT(int i ,long long int p) +{ + if(i==10) + { + int a=p/100000; + int b=p%100000; + x[cnt]=a; + y[cnt++]=b; + return; + } + + for(int j=0; j<10; j++) + if(!used[j]) + { + used[j]=1; + BT(i+1,p*10+j); + used[j]=0; + } +} + + +int main() +{ + int cse=0,t; + cnt=0; + BT(0,0); + while(cin>>N && N) + { + t=0; + if(cse++) + cout< + +int main () +{ + int divident, divisor, n, okFlag, loopLimit, temp, newLine = 0; + bool notSolvable; + while ( scanf("%d",&n), n ) + { + // divident / divisor = sum <=> (max)divident / (max)divisor = sum <=> + // (max)divisor = (max)divident / sum, where (max)divident = 98765 + loopLimit = 98765 / n; + + if ( newLine++ ) + puts(""); + + notSolvable = true; + + for ( int divisor = 1234; divisor <= loopLimit; divisor++ ) + { + okFlag = 0; + divident = n * divisor; + // To avoid furhter examination due to integer division + if ( divident / divisor == n ) + { + okFlag = ( divisor < 10000 ); // *is first digit 0?* + temp = divident; + while ( temp ){ okFlag |= 1 << ( temp%10 ); temp /= 10; } + temp = divisor; + while ( temp ){ okFlag |= 1 << ( temp%10 ); temp /= 10; } + if ( okFlag == 1023 ) + { + notSolvable = false; + printf("%.5d / %.5d = %d\n", divident, divisor, n ); + } + } + } + if ( notSolvable ) + printf("There are no solutions for %d.\n", n); + } +} + + diff --git a/uva_cpp_clean/725/725-6.cpp b/uva_cpp_clean/725/725-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..650fa2ee5e023f3085dab94c69721fd21879267c --- /dev/null +++ b/uva_cpp_clean/725/725-6.cpp @@ -0,0 +1,36 @@ +/* + brute force > iterative + difficulty: easy + date: 11/Jan/2020 + by: @brpapa +*/ +#include +#define setBit(S, i) (S |= (1 << i)) +#define setAll(n) ((1 << n) - 1) +using namespace std; + +int main() { + int N, T = 0; + while (1) { + scanf("%d", &N); if (N == 0) break; + if (T++ != 0) printf("\n"); + + bool found = false; + for (int den = 1234; den*N <= 98765; den++) { + int num = den * N; + + int mask; // conjunto de bits {9, ..., 0} + mask = (den < 10000); // se tem 0 à esquerda + + for (int tmp = num; tmp > 0; tmp /= 10) setBit(mask, tmp % 10); + for (int tmp = den; tmp > 0; tmp /= 10) setBit(mask, tmp % 10); + + if (mask == setAll(10)) { // todos os 9 bits foram ligados + printf("%.5d / %.5d = %d\n", num, den, N); + found = true; + } + } + if (!found) printf("There are no solutions for %d.\n", N); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/725/725-9.cpp b/uva_cpp_clean/725/725-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96f62a6bc34101c80e567e7d1276e875dafb41be --- /dev/null +++ b/uva_cpp_clean/725/725-9.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int b, tmp, used, N, ct, tc; + +int main(){ + while(scanf("%d", &N), N){ + if(tc++) + putchar('\n'); + ct = 0; + for(int a = 1234; a <= 98765 / N; a++){ + b = a * N; tmp = b; + used = (a < 10000); + while(tmp) + used |= 1 << (tmp % 10), tmp /= 10; + tmp = a; + while(tmp) + used |= 1<<(tmp % 10), tmp /= 10; + if(used == (1<<10) - 1) + printf("%.5d / %.5d = %d\n", b, a, N), ct++; + } + if(!ct) + printf("There are no solutions for %d.\n", N); + } + return(0); +} diff --git a/uva_cpp_clean/727/727-21.cpp b/uva_cpp_clean/727/727-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c812e168566f7f7ebb5881ff2fbc3a37aefebd77 --- /dev/null +++ b/uva_cpp_clean/727/727-21.cpp @@ -0,0 +1,62 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 727 + Name: Equation + Problem: https://onlinejudge.org/external/7/727.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int pred[120]; +char line[100]; + +int main(){ + pred['(']=1; + pred['+']=pred['-']=2; + pred['*']=pred['/']=3; + + int T; + cin>>T; + + cin.getline(line, 100); + cin.getline(line, 100); + while (T--) { + stack st; + + while (cin.getline(line, 100) && line[0]) { + char ch = line[0]; + if (ch>='0' && ch<='9') + putchar(ch); + else if (ch == '(') + st.push(ch); + else if (ch==')') { + while ((ch=st.top()) != '(') { + putchar(ch); + st.pop(); + } + st.pop(); + } + else { + while (!st.empty() && pred[st.top()] >= pred[ch]) { + putchar(st.top()); + st.pop(); + } + st.push(ch); + } + } + while (!st.empty()) { + putchar(st.top()); + st.pop(); + } + + if (T) putchar('\n');putchar('\n'); + } +} diff --git a/uva_cpp_clean/727/727-9.cpp b/uva_cpp_clean/727/727-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d569f69b068fa038f26ba58632ff9e2a083760a9 --- /dev/null +++ b/uva_cpp_clean/727/727-9.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int tc; +char ch, ch1; +map < char, int > priority; + +int main(){ + priority['+'] = priority['-'] = 1; + priority['*'] = priority['/'] = 2; + scanf("%d\n", &tc); + while(tc--){ + stack st; + while(scanf("%c", &ch) == 1){ + if( ch == '\n' ) break; + getchar(); + if( isdigit(ch) ) putchar(ch); + else if( ch == ')' ){ + while( not st.empty() ){ + ch = st.top(); + st.pop(); + if( ch == '(' ) break; + putchar(ch); + } + } + else if( ch == '(' ) + st.push(ch); + else{ + if( not st.empty() and priority[st.top()] >= priority[ch] ) + while( not st.empty() ){ + ch1 = st.top(); + if( ch1 == '(' or priority[ch1] < priority[ch] ) break; + st.pop(); + putchar(ch1); + } + st.push(ch); + } + } + while( not st.empty() ) putchar(st.top()), st.pop(); + putchar('\n'); + if( tc ) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/729/729-19.cpp b/uva_cpp_clean/729/729-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98124714d3167193afeb5811d919fc5e57181b3b --- /dev/null +++ b/uva_cpp_clean/729/729-19.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +void func(int pos, int bits, int N, int H, string & s) { + if (pos == N) cout << s << endl; + else { + if (N - pos > H - bits) s[pos] = '0', func(pos+1, bits, N, H, s); + if (H > bits) s[pos] = '1', func(pos+1, bits+1, N, H, s); + } +} + +int main() { + int T; + cin >> T; + while (T--) { + int N, H; + cin >> N >> H; + string s = string(N, ' '); + func(0, 0, N, H, s); + if (T) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/729/729-21.cpp b/uva_cpp_clean/729/729-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec449c3d5e33214884f421d59a57d0f53d088714 --- /dev/null +++ b/uva_cpp_clean/729/729-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 729 + Name: The Hamming Distance Problem + Problem: https://onlinejudge.org/external/7/729.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int n; +char out[20] = "0000000000000000000"; +void print(int i, int d) { + if (!d) { + cout<=n) return; + + print(i+1, d); + out[i]='1'; + print(i+1, d-1); + out[i]='0'; +} + + +int main(){ + int T, h; + cin>>T; + while (T--) { + cin>>n>>h; + memset(out, '0', sizeof(out)); + out[n]=0; + print(0, h); + if (T) cout< + +using namespace std; + +int main(){ + int c, n, m; + scanf("%d", &c); + while(c--){ + scanf("%d %d", &n, &m); + for(int i = 0; i < (1<= 0; j--) + printf("%d", (i>>j) & 1); + putchar('\n'); + } + if(c) + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/732/732-19.cpp b/uva_cpp_clean/732/732-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86ea2ad7b316b6577cc15cd33aac0dec62a9d699 --- /dev/null +++ b/uva_cpp_clean/732/732-19.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +using namespace std; + +string s1, s2; +vector v; +stack s; + +void func (int pos1, int pos2, int posv){ + if (pos2 == s2.length()) { + bool first = false; + for (int i = 0; i < v.size(); i++) { + if (first) cout << " "; + else first = true; + cout << (v[i]? "i": "o"); + } + cout << endl; + } + else { + if (pos1 < s1.size()) { + s.push(s1[pos1]); + v[posv] = true; + func(pos1+1, pos2, posv+1); + s.pop(); + } + if (!s.empty() && s.top() == s2[pos2]) { + s.pop(); + v[posv] = false; + func(pos1, pos2+1, posv+1); + s.push(s2[pos2]); + } + } +} + +int main() { + while (getline(cin, s1) && getline(cin, s2)) { + cout << "[" << endl; + if (s1.length() == s2.length()) { + v.assign(2*s1.length(), false); + func(0, 0, 0); + } + cout << "]" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/732/732-21.cpp b/uva_cpp_clean/732/732-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8fb14a6c2583f859cca9564f07871a6967bab9d --- /dev/null +++ b/uva_cpp_clean/732/732-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 732 + Name: Anagrams by Stack + Problem: https://onlinejudge.org/external/7/732.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include + +inline int readchar() { + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if(p == end) { + if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF; + p = buf; + } + return *p++; +} + +bool readStr(char *str) { + char ch; + while ((ch=readchar())==' ' || ch=='\n'); + if (ch == EOF) return 0; + + *str++ = ch; + while ((ch=readchar())!=' ' && ch!='\n' && ch!=EOF) + *str++ = ch; + *str = 0; + return 1; +} + +// --------------------------------------------------------------------------- + +char w1[1000], w2[1000], + stk[1000], res[1000]; +void recurse(int si, int i, int j) { + if (!w2[j]) { + if (!si && !w1[i]) { + putchar(res[0]); + for (int k=1; k>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 734 + Name: The Programmer's Hex + Problem: https://onlinejudge.org/external/7/734.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +string H[7], H2[7], tsq; +string normalize(const string &s) { + string res = s; + for (int i=1; i<6; ++i) + res = min(res, s.substr(i, 6-i) + s.substr(0, i)); + + return res; +} + +int ID[7], P[7], R[7]; +bool used[7]; +bool bt(int peg, int le=0) { + if (peg == 7) return le == H[P[1]][ ( R[1] + 4 )%6 ]; + + int midE = H[P[0]][peg-1]; + bool seenid[7] = {}; + for (int i=0; i<7; ++i) + if (!used[i] && !seenid[ID[i]]) { + P[peg] = i; + used[i] = 1; + seenid[ID[i]] = 1; + + bool seen[10] = {}; + for (int j=0; j<6; ++j) { + if (H[i][ (j + peg + 2)%6 ] == midE && (!le || H[i][ (j + peg + 3)%6 ] == le)) { + int nle = H[i][ (j + peg + 1)%6 ]; + if (!le || !seen[nle-'0']) { + seen[nle-'0'] = 1; + R[peg] = j; + if (bt(peg+1, nle)) + return 1; + } + } + } + used[i] = 0; + } + return 0; +} + + +bool solve() { + bool seenid[7] = {}; + memset(used, 0, sizeof(used)); + for (int i=0; i<7; ++i) + if (!seenid[ID[i]]) { + P[0] = i; + used[i] = 1; + seenid[ID[i]] = 1; + if (bt(1)) return 1; + used[i] = 0; + } + return 0; +} + +char BUF[1000000]; +int main() { + cout.rdbuf()->pubsetbuf(BUF, 1000000); + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> H[0]) { + for (int i=1; i<7; ++i) + cin >> H[i]; + cin >> tsq; + + int sz = 0; + for (int i=0; i<7; ++i) { + H2[i] = normalize(H[i]); + ID[i] = -1; + for (int j=0; j +#include +#include +#include +#include +#include +#include + +using namespace std; + +int num; +set save; +set scores; +set ::iterator it1; +set ::iterator it2; +set ::iterator it3; + +void initializing() +{ + save.clear(); + scores.clear(); + + for (int i = 0; i <= 20; i++) + { + scores.insert(i * 1); + scores.insert(i * 2); + scores.insert(i * 3); + } + scores.insert(50); +} + +void addPer(int a, int b, int c) +{ + save.insert((a * 10000) + (b * 100) + c); + save.insert((a * 10000) + (c * 100) + b); + save.insert((b * 10000) + (a * 100) + c); + save.insert((b * 10000) + (c * 100) + a); + save.insert((c * 10000) + (a * 100) + b); + save.insert((c * 10000) + (b * 100) + a); +} + +void showResult(int num, int c, int p) +{ + if (p == 0) + { + printf("THE SCORE OF %d CANNOT BE MADE WITH THREE DARTS.\n", num); + } + + else + { + printf("NUMBER OF COMBINATIONS THAT SCORES %d IS %d.\n", num, c); + printf("NUMBER OF PERMUTATIONS THAT SCORES %d IS %d.\n", num, p); + } + + printf("**********************************************************************\n"); +} + +int main() +{ + ios::sync_with_stdio(0); + + initializing(); + + cin >> num; + + while (num > 0) + { + save.clear(); + + int p = 0, c = 0; + + if (num > 180) + { + showResult(num, c, p); + } + + else + { + for (it1 = scores.begin(); it1 != scores.end(); it1++) + { + for (it2 = scores.begin(); it2 != scores.end(); it2++) + { + for (it3 = scores.begin(); it3 != scores.end(); it3++) + { + if (*it1 + *it2 + *it3 == num) + { + p++; + + int temp = (((*it1 + 25) * 10000) + ((*it2 + 25) * 100) + (*it3 + 25)); + + if (save.find(temp) == save.end()) + { + c++; + + addPer(*it1 + 25, *it2 + 25, *it3 + 25); + } + } + } + } + } + + showResult(num, c, p); + } + + cin >> num; + } + + printf("END OF OUTPUT\n"); +} \ No newline at end of file diff --git a/uva_cpp_clean/735/735-21.cpp b/uva_cpp_clean/735/735-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4b1c8bedb22d1d17b94135476f1b1c2fb47ef41 --- /dev/null +++ b/uva_cpp_clean/735/735-21.cpp @@ -0,0 +1,53 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 735 + Name: Dart-a-Mania + Problem: https://onlinejudge.org/external/7/735.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int S[] = {6, 3, 0, 1}; + int nums[55], nn=0; + bool ff[1000]={}; ff[50] = 1; + for (int i=0; i<=20; ++i) + for (int j=1; j<4; ++j) + ff[i*j] = 1; + for (int i=0; i<61; ++i) + if (ff[i]) + nums[nn++] = i; + + int n; + while (cin >> n && n>0) { + int c=0, p=0; + for (int i=0; i n) break; + int z = n-x-y; + if (z >= y && ff[z]) { + ++c; + p += S[(x==y)+(y==z)+(z==x)]; + } + } + } + if (!c) + cout << "THE SCORE OF " << n << " CANNOT BE MADE WITH THREE DARTS.\n"; + else { + cout << "NUMBER OF COMBINATIONS THAT SCORES " << n << " IS " << c << ".\n"; + cout << "NUMBER OF PERMUTATIONS THAT SCORES " << n << " IS " << p << ".\n"; + } + cout << "**********************************************************************\n"; + } + cout << "END OF OUTPUT\n"; +} diff --git a/uva_cpp_clean/735/735-3.cpp b/uva_cpp_clean/735/735-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dedcdd3794a84d0049f441a0c210e7ff9157760 --- /dev/null +++ b/uva_cpp_clean/735/735-3.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include + +using namespace std; + +set scores; +set:: iterator l,k,d; +bool valid[65][65][65]; +int tempInt,combinations, permutations; + +int main() +{ + // Setting up the scores array + scores.insert(0); + scores.insert(50); + for ( int l = 1; l < 21; l++ ) + scores.insert(l), scores.insert(l*2), scores.insert(l*3); + + int N; + while ( scanf("%d",&N), N > 0 ) + { + memset(valid,true,274625); + permutations = 0,combinations = 0; + + for ( l = scores.begin(); l != scores.end(); l++) + for ( k = scores.begin(); k != scores.end(); k++ ) + { + d = scores.find(N-*l-*k); + if ( d != scores.end() ) + { + permutations++; + if ( valid[*l][*k][*d] ) + { + valid[*l][*k][*d] = false; + valid[*l][*d][*k] = false; + valid[*k][*d][*l] = false; + valid[*k][*l][*d] = false; + valid[*d][*k][*l] = false; + valid[*d][*l][*k] = false; + combinations++; + } + } + } + + + //OUTPUT SEQUENCE: + if ( permutations && combinations ) + { + printf("NUMBER OF COMBINATIONS THAT SCORES %d IS %d.\n", N, combinations); + printf("NUMBER OF PERMUTATIONS THAT SCORES %d IS %d.\n", N, permutations); + } + else + printf("THE SCORE OF %d CANNOT BE MADE WITH THREE DARTS.\n",N); + + printf("%s\n","**********************************************************************"); + } + printf("%s\n","END OF OUTPUT"); +} diff --git a/uva_cpp_clean/735/735-9.cpp b/uva_cpp_clean/735/735-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e87a6a12cd17e743030db7ab9f71dd0fe72ae69 --- /dev/null +++ b/uva_cpp_clean/735/735-9.cpp @@ -0,0 +1,65 @@ +#include + +#define MAX 100000 + +using namespace std; + +int n,numberOfCombinations, numberOfPermutations, t; +vector ans[MAX], v, x; + +inline void asterisks(){ + for(int i = 0; i < 70; i++) putchar('*'); + putchar('\n'); +} + +void addSolution(int num1, int num2, int num3){ + v.clear(); + v.push_back(num1), v.push_back(num2), v.push_back(num3); + sort(v.begin(), v.end()); + for(int i = 0; i < numberOfCombinations; i++) + if(ans[i] == v) return; + ans[numberOfCombinations++] = v; + if(num1 == num2 && num1 != num3) numberOfPermutations += 3; + if(num1 != num2 && num1 == num3) numberOfPermutations += 3; + if(num2 == num3 && num1 != num3) numberOfPermutations += 3; + if(num1 != num2 && num2 != num3) numberOfPermutations += 6; + if(num1 == num2 && num2 == num3) numberOfPermutations += 1; +} + +void solve(){ + for(int a = 0; a < x.size(); a++) + for(int b = 0;b < x.size(); b++) + for(int c = 0;c < x.size(); c++) + if(x[a] + x[b] + x[c] == n) + addSolution(x[a], x[b], x[c]); +} + +void reset(){ + for(int i = 0; i < numberOfCombinations; i++) + ans[i].clear(); + numberOfCombinations = numberOfPermutations = 0; +} + +void generate(){ + x.push_back(0); + for(int i = 1; i <= 20; i++) + x.push_back(i), x.push_back(i * 2), x.push_back(i * 3); + x.push_back(50); +} + +int main(){ + generate(); + while(scanf("%d", &n),n > 0){ + reset(); + solve(); + if(!numberOfCombinations) + printf("THE SCORE OF %d CANNOT BE MADE WITH THREE DARTS.\n", n); + else{ + printf("NUMBER OF COMBINATIONS THAT SCORES %d IS %d.\n", n, numberOfCombinations); + printf("NUMBER OF PERMUTATIONS THAT SCORES %d IS %d.\n", n, numberOfPermutations); + } + asterisks(); + } + printf("END OF OUTPUT\n"); + return(0); +} diff --git a/uva_cpp_clean/736/736-9.cpp b/uva_cpp_clean/736/736-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97c1dc1ff6e39ed2df00da00f8f483e56c1e5381 --- /dev/null +++ b/uva_cpp_clean/736/736-9.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +const char EMPTY = ' '; +const vector dir = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"}; + +int n; +string s; +vector grid; +int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; +int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; + +inline bool isValid (const int r, const int c) { + return 0 <= r and 0 <= c and r < n and c < n; +} + +bool check (int r, int c, int id, const int d) { + if (grid[r][c] != s[id - 1]) return false; + if (id == s.size()) return true; + while (true) { + int nr = r + dr[d]; + int nc = c + dc[d]; + if (not isValid(nr, nc)) return false; + if (grid[nr][nc] != EMPTY) return check(nr, nc, id + 1, d); + r = nr, c = nc; + } +} + +void searchInGrid () { + bool found = false; + puts(""); + cout << s << endl; + for (int r = 0; r < n; r++) + for (int c = 0; c < n; c++) + for (int d = 0; d < 8; d++) + if (check(r, c, 1, d)) { + found = true; + cout << '(' << r + 1 << ',' << c + 1 << ") - " << dir[d] << endl; + } + if (not found) puts("not found"); +} + +void processQueries () { + while (getline(cin, s), not s.empty()) { + searchInGrid(); + } +} + +void readGrid () { + cin >> n; + cin.ignore(); + grid.resize(n); + for (int r = 0; r < n; r++) getline(cin, grid[r]); +} + +int main () { + int tc; + cin >> tc; + while (tc--) { + readGrid(); + processQueries(); + if (tc) puts(""); + } + return (0); +} diff --git a/uva_cpp_clean/737/737-15.cpp b/uva_cpp_clean/737/737-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6e331e72ea94474339d3383215e288e2cb58af2 --- /dev/null +++ b/uva_cpp_clean/737/737-15.cpp @@ -0,0 +1,40 @@ +/* + 計算幾何 +*/ +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n, x, y, z, d, x_left, y_left, z_left, x_right, y_right, z_right; + while (cin >> n && n--) { + cin >> x >> y >> z >> d; + + x_left = x; + y_left = y; + z_left = z; + + x_right = x + d; + y_right = y + d; + z_right = z + d; + + while (n--) { + cin >> x >> y >> z >> d; + + x_left = max(x_left, x); + y_left = max(y_left, y); + z_left = max(z_left, z); + + x_right = min(x_right, x + d); + y_right = min(y_right, y + d); + z_right = min(z_right, z + d); + } + + cout << max(0, (x_right - x_left) * (y_right - y_left) * (z_right - z_left)) << "\n"; // 體積不能為負數 + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/739/739-21.cpp b/uva_cpp_clean/739/739-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4b75f666a2fe10f67dc454b250fb7abf27e593e --- /dev/null +++ b/uva_cpp_clean/739/739-21.cpp @@ -0,0 +1,44 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 739 + Name: Soundex Indexing + Problem: https://onlinejudge.org/external/7/739.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int codes[127]; +const char cgs[] = "AEIOUYWH|BPFV|CSKGJQXZ|DT|L|MN|R"; +void print(const char s1[], const char s2[]) { + printf("%9s%-25s%s\n", "", s1, s2); +} + +int main(){ + int id=0; + for (int i=0; cgs[i]; i++) + if (cgs[i]=='|') id++; + else codes[cgs[i]] = id; + + print("NAME", "SOUNDEX CODE"); + + char w[30], c[5]; c[4]=0; + while (cin>>w) { + c[0]=w[0]; + + int j=1; + for (int i=1; j<4 && w[i]; i++) + if (codes[w[i]] && codes[w[i]]!=codes[w[i-1]]) + c[j++] = '0'+codes[w[i]]; + + for (; j<4; j++) c[j]='0'; + print(w, c); + } + printf("%19sEND OF OUTPUT\n", ""); +} diff --git a/uva_cpp_clean/739/739-9.cpp b/uva_cpp_clean/739/739-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..786d842f072450a634e833f26959a200b2d003a1 --- /dev/null +++ b/uva_cpp_clean/739/739-9.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +const int LEN = 25, MAX_ASCII = 100; + +char name[LEN], unavailable[] = "AEIOUYWH"; +int code, mp[MAX_ASCII]; + +bool canBeEncoded(int id) { + for (int i = 0; i < 8; i++) + if (name[id] == unavailable[i]) return false; + return mp[name[id]] != mp[name[id - 1]]; +} + +int soundexCode() { + code = 0; + for (int i = 1; name[i] and code < 100; i++) + if ( canBeEncoded(i) ) code = code * 10 + mp[name[i]]; + while (code and code < 100) code *= 10; + return code; +} + +int main() { + mp['B'] = mp['P'] = mp['F'] = mp['V'] = 1; + mp['C'] = mp['S'] = mp['K'] = mp['G'] = mp['J'] = mp['Q'] = mp['X'] = mp['Z'] = 2; + mp['D'] = mp['T'] = 3; + mp['L'] = 4; + mp['M'] = mp['N'] = 5; + mp['R'] = 6; + printf("%9s%-25s%s\n", " ", "NAME", "SOUNDEX CODE"); + while (scanf("%s", name) == 1) + printf("%9s%-25s%c%.3d\n", " ", name, name[0], soundexCode()); + printf("%19s%s\n", " ", "END OF OUTPUT"); + return(0); +} \ No newline at end of file diff --git a/uva_cpp_clean/740/740-21.cpp b/uva_cpp_clean/740/740-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c32a3182db657971dd814acc30980c817dc377a8 --- /dev/null +++ b/uva_cpp_clean/740/740-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 740 + Name: Baudot Data Communication Code + Problem: https://onlinejudge.org/external/7/740.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char sd[100], su[100]; + cin.getline(sd, 100); + cin.getline(su, 100); + + string str; + while (cin >> str) { + char *ss = sd; + + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 748 + Name: Exponentiation + Problem: https://onlinejudge.org/external/7/748.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +#ifndef min +#define min(x,y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef max +#define max(x,y) ((x) > (y) ? (x) : (y)) +#endif + +class BigInt +{ +private: + char *digits; + int size; // number of used bytes (digits) + int capacity; // size of digits + int sign; // -1, 0 or +1 + +public: + BigInt(int n, int cap); + BigInt(int n); + BigInt(long double d); + BigInt(); + BigInt(string s); + BigInt(const char s[]); + BigInt(const BigInt &n); + const BigInt &operator=(const BigInt &n); + const BigInt &operator=(int n); + ~BigInt(); + void normalize(); + static int sig(int n); + static int sig(long double n); + inline int length() { return size; } + + BigInt operator++(); + BigInt operator++(int); + BigInt operator--(); + BigInt operator--(int); + BigInt operator-(); + BigInt operator+ (int n); + BigInt operator+ (BigInt n); + BigInt&operator+=(int n); + BigInt&operator+=(BigInt n); + BigInt operator- (int n); + BigInt operator- (BigInt n); + BigInt&operator-=(int n); + BigInt&operator-=(BigInt n); + BigInt operator* (int n); + BigInt operator* (BigInt n); + void operator*=(int n); + void operator*=(BigInt n); + BigInt operator/ (int n); + BigInt operator/ (BigInt n); + void operator/=(int n); + void operator/=(BigInt n); + int operator% (int n); + BigInt operator% (BigInt n); + void operator%=(int n); + void operator%=(BigInt n); + int divide(int n); + BigInt divide(BigInt n); + BigInt operator* (long double n); + void operator*=(long double n); + + + BigInt operator<< (int n); + void operator<<=(int n); + BigInt operator >> (int n); + void operator>>=(int n); + BigInt operator,(int n); + BigInt operator,(BigInt n); + + /** Casting **/ + bool operator!(); + operator bool(); + operator string(); + + bool operator<(BigInt n); + bool operator>(BigInt n); + bool operator==(BigInt n); + bool operator<=(BigInt n); + bool operator>=(BigInt n); + bool operator<(int n); + bool operator>(int n); + bool operator==(int n); + bool operator<=(int n); + bool operator>=(int n); + int compare(BigInt n); + + int toInt(); + string toString(); + void print(); + void printWithCommas(ostream &out); + +private: + void grow(); + friend istream &operator >> (istream &in, BigInt &n); + friend ostream &operator<<(ostream &out, BigInt n); + friend long double log2(BigInt x, long double epsilon); + inline friend long double log(BigInt x, long double epsilon); + inline friend long double log10(BigInt x, long double epsilon); + inline friend long double lg(BigInt x, long double epsilon); + inline friend long double ln(BigInt x, long double epsilon); +}; + +BigInt operator+(int m, BigInt &n) +{ + return n + m; +} + +BigInt operator-(int m, BigInt &n) +{ + return -n + m; +} + +BigInt operator*(int m, BigInt &n) +{ + return n * m; +} + +BigInt operator/(int m, BigInt &n) +{ + return BigInt(m) / n; +} + +BigInt operator%(int m, BigInt &n) +{ + return BigInt(m) % n; +} + +inline bool isDigit(int c) +{ + return(c >= (int)'0' && c <= (int)'9'); +} + +istream &operator >> (istream &in, BigInt &n) // FIXME: see inside +{ + n.size = 0; + n.sign = 1; + int sign = 1; + int c; + while ((c = in.peek()) >= 0 && + (c == ' ' || c == '\t' || c == '\r' || c == '\n')) + in.get(); + if (c < 0 || (c != (int)'-' && !isDigit(c))) + { + in >> c; // XXX: force in.fail() + return in; + } + if (c == (int)'-') { sign = -1; in.get(); } + + // FIXME: Extremely inefficient! Use a string. + while ((c = in.peek()) >= 0 && isDigit(c)) + { + in.get(); + n *= 10; + n += (c - (int)'0'); + } + n.sign = sign; + n.normalize(); + return in; +} + +ostream &operator<<(ostream &out, BigInt n) //FIXME: make more efficient +{ + return out << n.toString(); +} + +BigInt::BigInt(int n, int cap) +{ + cap = max(cap, (int)sizeof(n) * 8); + capacity = cap; + sign = sig(n); + n *= sign; + digits = new char[cap]; + memset(digits, 0, cap); + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(int n) +{ + capacity = 1024; + sign = sig(n); + n *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + while (n) + { + digits[size++] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(long double d) +{ + capacity = 1024; + sign = (d < 0 ? -1 : d > 0 ? 1 : 0); + d *= sign; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + d = floor(d); + while (d > 0) + { + digits[size++] = min(max(0, (int)((d - floor(d / 10) * 10) + 0.5)), 9); + d = floor(d / 10); + } +} + +BigInt::BigInt() +{ + capacity = 128; + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; +} + +BigInt::BigInt(string s) +{ + capacity = max((int)s.size(), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const char s[]) +{ + capacity = max((int)strlen(s), 16); + sign = 0; + digits = new char[capacity]; + memset(digits, 0, capacity); + + istringstream in(s); + in >> (*this); +} + +BigInt::BigInt(const BigInt &n) +{ + capacity = n.capacity; + sign = n.sign; + size = n.size; + digits = new char[capacity]; + memcpy(digits, n.digits, capacity); +} + +const BigInt &BigInt::operator=(const BigInt &n) +{ + if (&n != this) + { + if (capacity < n.size) + { + capacity = n.capacity; + delete[] digits; + digits = new char[capacity]; + } + sign = n.sign; + size = n.size; + memcpy(digits, n.digits, size); + memset(digits + size, 0, capacity - size); + } + return *this; +} + +const BigInt &BigInt::operator=(int n) +{ + sign = sig(n); + n *= sign; + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } + return *this; +} + +BigInt::~BigInt() +{ + delete[] digits; +} + +void BigInt::normalize() +{ + while (size && !digits[size - 1]) size--; + if (!size) sign = 0; +} + +int BigInt::sig(int n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::sig(long double n) +{ + return(n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::toInt() +{ + int result = 0; + for (int i = size - 1; i >= 0; i--) + { + result *= 10; + result += digits[i]; + if (result < 0) return sign * 0x7FFFFFFF; + } + return sign * result; +} + +string BigInt::toString() +{ + string s = (sign >= 0 ? "" : "-"); + for (int i = size - 1; i >= 0; i--) + s += (digits[i] + '0'); + if (size == 0) s += '0'; + return s; +} + +void BigInt::print() //FIXME: make more efficient +{ + cout << toString(); +} + +void BigInt::printWithCommas(ostream &out) +{ + if (sign < 0) out.put('-'); + for (int i = size - 1; i >= 0; i--) + { + out.put(digits[i] + '0'); + if (!(i % 3) && i) out.put(','); + } + if (size == 0) out.put('0'); +} + +void BigInt::grow() +{ + char *olddigits = digits; + int oldCap = capacity; + capacity *= 2; + digits = new char[capacity]; + memcpy(digits, olddigits, oldCap); + memset(digits + oldCap, 0, oldCap); + delete[] olddigits; +} + +BigInt BigInt::operator++() +{ + operator+=(1); + return *this; +} + +BigInt BigInt::operator++(int) +{ + return operator++(); +} + +BigInt BigInt::operator--() +{ + operator-=(1); + return *this; +} + +BigInt BigInt::operator--(int) +{ + return operator--(); +} + +BigInt BigInt::operator-() +{ + BigInt result(*this); + result.sign *= -1; + return result; +} + +BigInt BigInt::operator+(int n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt BigInt::operator+(BigInt n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt &BigInt::operator+=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = nsign; + if (sign == nsign) + { + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] + dig + carry; + digits[i] = newdig % 10; + carry = newdig / 10; + n /= 10; + } + size = max(i, size); + } + else operator-=(-n); + return *this; +} + +BigInt &BigInt::operator+=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = n.sign; + if (sign == n.sign) + { + int carry = 0; + int i; + for (i = 0; i < maxS - 1 || carry; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig += n.digits[i]; + digits[i] = newdig % 10; + carry = newdig / 10; + } + size = max(i, size); + } + else + { + n.sign *= -1; + operator-=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator-(int n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt BigInt::operator-(BigInt n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt &BigInt::operator-=(int n) +{ + if (size == capacity) grow(); + + int nsign = sig(n); + if (!nsign) return *this; + if (!sign) sign = 1; + if (sign == nsign) + { + BigInt bin = n; + if ((sign >= 0 && *this < bin) || (sign < 0 && *this > bin)) + { + // Subtracting a bigger number + operator=(toInt() - n); + return *this; + } + + n *= nsign; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] - dig + carry; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + n /= 10; + } + normalize(); + } + else operator+=(-n); + return *this; +} + +BigInt &BigInt::operator-=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) grow(); //FIXME: this is stupid + + if (!n.sign) return *this; + if (!sign) sign = 1; + if (sign == n.sign) + { + if ((sign >= 0 && *this < n) || (sign < 0 && *this > n)) + { + // Subtracting a bigger number + BigInt tmp = n; + tmp -= *this; + *this = tmp; + sign = -sign; + return *this; + } + + int carry = 0; + int i; + for (i = 0; i < maxS - 1; i++) + { + int newdig = carry; + if (i < size) newdig += digits[i]; + if (i < n.size) newdig -= n.digits[i]; + if (newdig < 0) newdig += 10, carry = -1; + else carry = 0; + digits[i] = newdig; + } + if (carry) // Subtracted a bigger number, need to flip sign + { + if (i) digits[0] = 10 - digits[0]; + size = (i ? 1 : 0); + for (int j = 1; j < i; j++) + { + digits[j] = 9 - digits[j]; + if (digits[i]) size = j + 1; + } + sign *= -1; + } + normalize(); + } + else + { + n.sign *= -1; + operator+=(n); + n.sign *= -1; + } + return *this; +} + +BigInt BigInt::operator*(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + int nsign = sig(n); + n *= nsign; + result.sign = sign * nsign; + if (!result.sign) return result; + + int i, j; + for (i = 0; n; i++) + { + int dig = n % 10; + if (dig) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = result.digits[i + j] + (j < size ? dig * digits[j] : 0) + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + n /= 10; + } + result.size = i + j - 1; + return result; +} + +BigInt BigInt::operator*(BigInt n) +{ + BigInt result(0, size + n.size); + + result.sign = sign * n.sign; + if (!result.sign) return result; + + int i, j; + for (i = 0; i < n.size; i++) + { + if (n.digits[i]) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = + result.digits[i + j] + + (j < size ? n.digits[i] * digits[j] : 0) + + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + } + result.size = i + j - 1; + + return result; +} + +void BigInt::operator*=(int n) +{ + operator=(operator*(n)); +} + +void BigInt::operator*=(BigInt n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator/(int n) +{ + if (!n) n /= n; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +BigInt BigInt::operator/(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +void BigInt::operator/=(int n) +{ + divide(n); +} + +void BigInt::operator/=(BigInt n) +{ + divide(n); +} + +int BigInt::operator%(int n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(int n) +{ + operator=(divide(n)); +} + +BigInt BigInt::operator%(BigInt n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(BigInt n) +{ + operator=(divide(n)); +} + +int BigInt::divide(int n) +{ + if (!n) n /= n; //XXX: force a crash + + int nsign = sig(n); + n *= nsign; + if (!sign) return 0; + sign *= nsign; + + int tmp = 0; + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = tmp / n; + tmp -= digits[i] * n; + } + normalize(); + return tmp; +} + +BigInt BigInt::divide(BigInt n) +{ + if (!n) n.size /= n.size; //XXX: force a crash + + if (!sign) return 0; + sign *= n.sign; + + int oldSign = n.sign; + n.sign = 1; + + BigInt tmp(0, size); + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = 0; + while (tmp >= n) { tmp -= n; digits[i]++; } + } + normalize(); + + n.sign = oldSign; + + return tmp; +} + +// This is only exact to the first 15 or so digits, but it is +// never an over-estimate +BigInt BigInt::operator*(long double n) +{ + // the number of digits after the decimal point to use + #define DIGS_AFTER_DOT 15 + + int nsign = sig(n); + n *= nsign; + int ndigs = n >= 1 ? (int)log10(n) + 1 : 0; + BigInt result(0, size + ndigs); + result.sign = sign * nsign; + if (!result.sign) return result; + + if (n >= 1) for (int i = 0; i < ndigs; i++) n /= 10; + result.size = 0; + + char afterDot[DIGS_AFTER_DOT + 1]; + memset(afterDot, 0, sizeof(afterDot)); + + // Keep going until the DIGS_AFTER_DOT'th digit after the decimal point + for (int i = ndigs - 1; i >= -DIGS_AFTER_DOT; i--) + { + n *= 10; + int dig = (int)floor(n); + n -= dig; + if (!dig) continue; + + int carry = 0; + for (int j = 0; j < size || carry; j++) + { + int newdig = + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) + + dig * digits[j] + + carry; + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) = newdig % 10; + if (i + j >= 0 && result.digits[i + j]) result.size = max(result.size, i + j + 1); + carry = newdig / 10; + } + } + if (!result.size) result.sign = 0; + return result; +} + +void BigInt::operator*=(long double n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator<<(int n) +{ + BigInt result(*this); + result <<= n; + return result; +} + +void BigInt::operator<<=(int n) +{ + if (n < 0) operator>>=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator*=(mult); + } +} + +BigInt BigInt::operator >> (int n) +{ + BigInt result(*this); + result >>= n; + return result; +} + +void BigInt::operator>>=(int n) +{ + if (n < 0) operator<<=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) mult *= 2; + } + operator/=(mult); + } +} + +BigInt BigInt::operator,(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + for (result.size = 0; n; result.size++) + { + result.digits[result.size] = n % 10; + n /= 10; + } + memcpy(result.digits + result.size, digits, size * sizeof(digits[0])); + result.size += size; + result.sign = 1; + result.normalize(); + return result; +} + +BigInt BigInt::operator,(BigInt n) +{ + BigInt result(0, size + n.size); + memcpy(result.digits, n.digits, n.size * sizeof(n.digits[0])); + memcpy(result.digits + n.size, digits, size * sizeof(digits[0])); + result.size = size + n.size; + result.sign = 1; + result.normalize(); + return result; +} + +bool BigInt::operator!() +{ + return !size; +} + +BigInt::operator bool() +{ + return size; +} + +BigInt::operator string() +{ + return toString(); +} + +bool BigInt::operator<(BigInt n) +{ + return(compare(n) < 0); +} + +bool BigInt::operator>(BigInt n) +{ + return(compare(n) > 0); +} + +bool BigInt::operator>=(BigInt n) +{ + return(compare(n) >= 0); +} + +int BigInt::compare(BigInt n) +{ + if (sign < n.sign) return -1; + if (sign > n.sign) return 1; + if (size < n.size) return -sign; + if (size > n.size) return sign; + for (int i = size - 1; i >= 0; i--) + { + if (digits[i] < n.digits[i]) return -sign; + else if (digits[i] > n.digits[i]) return sign; + } + return 0; +} + +int main(){ + char r[10]; + int n; + while(cin>>r>>n) { + int rr=0, l=0, pn; + for (l=0; r[l]; l++) + if (r[l] == '.') + pn = l; + else rr = rr*10 + r[l] - '0'; + pn = (l-pn-1) * n; + + BigInt R(rr); + BigInt res(1); + for (int i=0; i +#include +#include + +using namespace std; + +int TC, a, b, row[8], lineCounter; + +bool place(int r, int c) +{ + for (int pre = 0; pre < c; pre++) + { + if (row[pre] == r || (abs(row[pre] - r) == abs(c - pre))) return false; + } + + return true; +} + +void backtrack(int c) +{ + if (c == 8 && row[b] == a) + { + printf("%2d %d %d %d %d %d %d %d %d\n", lineCounter++, row[0] + 1, row[1] + 1, row[2] + 1, row[3] + 1, row[4] + 1, row[5] + 1, row[6] + 1, row[7] + 1); + } + + for (int r = 0; r < 8; r++) + { + if (place(r, c)) + { + row[c] = r; + + backtrack(c + 1); + } + } +} + +int main() +{ + cin >> TC; + + bool first = false; + + while (TC--) + { + cin >> a >> b; + + if (first) cout << endl; + else first = true; + + cout << "SOLN COLUMN" << endl; + cout << " # 1 2 3 4 5 6 7 8" << endl << endl; + + lineCounter = 1; + memset(row, 0, sizeof row); + + a--; + b--; + + backtrack(0); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/750/750-18.cpp b/uva_cpp_clean/750/750-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b83c248522c28195c729d38ab073634d0f00de70 --- /dev/null +++ b/uva_cpp_clean/750/750-18.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +int sol; + +void backtracking(int currentCol, int colI, int col[15], bool row[15], bool dig1[20], bool dig2[20]){ + if(currentCol == colI){ + backtracking(currentCol + 1, colI, col, row, dig1, dig2); + }else if(currentCol == 9){ + sol++; + printf("%2d ", sol); + for(int i = 1 ; i <= 8 ; i++ ) printf(" %d", col[i]); + printf("\n"); + }else{ + for(int i = 1; i <= 8; i++){ + if(!row[i] && !dig1[currentCol + i] && !dig2[i - currentCol + 8]){ + row[i] = dig1[currentCol + i] = dig2[i - currentCol + 8] = true; + col[currentCol] = i; + backtracking(currentCol + 1, colI, col, row, dig1, dig2); + row[i] = dig1[currentCol + i] = dig2[i - currentCol + 8] = false; + col[currentCol] = 0; + } + } + } +} + +int main(){ + int dataset, colI, rowI; + scanf("%d", &dataset); + for(int i = 0; i < dataset; i++){ + if(i > 0) printf("\n"); + scanf("%d %d", &rowI, &colI); + int col[15] = {0}; + bool row[15] = {0}, dig1[20] = {0}, dig2[20] = {0}; + col[colI] = rowI; + row[rowI] = true; + dig1[colI + rowI] = true; + dig2[rowI - colI + 8] = true; + printf("SOLN COLUMN\n"); + printf(" # 1 2 3 4 5 6 7 8\n\n"); + sol = 0; + backtracking(1, colI, col, row, dig1, dig2); + } + return 0; +} +/* + La estrategia es algo parecida a la usada en el UVA524, usar backtracking/DFS para poder buscar todas las soluciones + posibles, aqui cambia algunas cosas, para facilitar la busqueda en vez de usar una matriz use dos vector, el de + las columnas guardo todas las filas que pertenecen a la solucion (para mostrar la respuesta) y el + vector de las filas es booleano para indicar que hay una reina ubicada, tambien use dos vectores mas para las + diagonales, en este uso dos formulas para saber la diagonal que le pertenece a una fila, estas son: + dig1 = col + row, y dig2 = row - col + 8. +*/ \ No newline at end of file diff --git a/uva_cpp_clean/750/750-19.cpp b/uva_cpp_clean/750/750-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..427ff73227394b2e6b7f097b180f09a1f4b29dbd --- /dev/null +++ b/uva_cpp_clean/750/750-19.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +int sol[8]; +int mask[8][8]; + +int cont; + +void func(int col) { + if (col == 8) { + printf("%2d ", cont++); + for (int i = 0; i < 8; i++) + printf(" %d", sol[i]); + printf("\n"); + return; + } + if (sol[col] != 0) { + func(col + 1); + return; + } + for (int i = 0; i < 8; i++) { + if (!mask[i][col]) { + for (int j = 0; j < 8; j++) { + mask[j][col]++; + mask[i][j]++; + if (i-col+j < 8 && i-col+j >= 0) mask[i-col+j][j]++; + if (col+i-j < 8 && col+i-j >= 0) mask[col+i-j][j]++; + } + sol[col] = i + 1; + func(col + 1); + for (int j = 0; j < 8; j++) { + mask[j][col]--; + mask[i][j]--; + if (i-col+j < 8 && i-col+j >= 0) mask[i-col+j][j]--; + if (col+i-j < 8 && col+i-j >= 0) mask[col+i-j][j]--; + } + } + } + sol[col] = 0; +} + +int main() { + int T; + cin >> T; + while (T--) { + int row, col; + cin >> row >> col; + row--, col--; + for (int i = 0; i < 8; i++) + for (int j = 0; j < 8; j++) + mask[i][j] = 0; + for (int j = 0; j < 8; j++) { + sol[j] = 0; + mask[row][j]++; + mask[j][col]++; + if (row-col+j < 8 && row-col+j >= 0) mask[row-col+j][j]++; + if (col+row-j < 8 && col+row-j >= 0) mask[col+row-j][j]++; + } + printf("SOLN COLUMN\n # 1 2 3 4 5 6 7 8\n\n"); + sol[col] = row + 1; + cont = 1; + func(0); + if (T) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/750/750-21.cpp b/uva_cpp_clean/750/750-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c008dad1780b31a02942ffb05bcf69974bd5994 --- /dev/null +++ b/uva_cpp_clean/750/750-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 750 + Name: 8 Queens Chess Problem + Problem: https://onlinejudge.org/external/7/750.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int urow[20],udi1[20],udi2[20]; +int row[8],ans[100][8]; +int cnt=0; + +bool check(int i, int j) +{ + return !urow[j] && !udi1[i+j] && !udi2[i+8-j]; +} + +void BT(int i) +{ + if(i==8) + memcpy(ans[cnt++],row,sizeof(row)); + else + for(int j=0; j<8; j++) + if(check(i,j)){ + urow[j]=udi1[i+j]=udi2[i+8-j]=1; + row[i]=j; + BT(i+1); + urow[j]=udi1[i+j]=udi2[i+8-j]=0; + } +} + +int main() +{ + int cse,col,row,i,j,sln; + BT(0); + + + cin>>cse; + while(cse--) + { + puts("SOLN COLUMN"); + puts(" # 1 2 3 4 5 6 7 8\n"); + cin>>row>>col; + sln=1; + for(i=0; i +#include +#include + +int rowsArray[8], cases, reqRow, reqColumn, lineCounter; + +// bool function place: +// given the row and column of a queen position check whether is is feasible or not +bool place ( int row, int column ) +{ + for ( int prev = 0; prev < column; prev++ ) + if ( rowsArray[prev] == row || abs(rowsArray[prev] - row ) == abs(prev - column) ) + return false; + return true; +} + +void backtrack ( int column ) +{ + // If we are past the last column, and the input requirement is met: + // continue with the output sequence + if ( column == 8 && rowsArray[reqColumn] == reqRow ) + { + // Print solution number, and queense location at row 0 (0-indexed) + printf ("%2d %d", ++lineCounter, rowsArray[0]+1); + // Now from rows 1 to 7 (0-indexed) print the row of the queen + for ( int j = 1; j < 8; j++ ) + printf(" %d", rowsArray[j] + 1); + printf("\n"); + } + + // Check the rows that the queen can possible be placed at + for ( int row = 0; row < 8; row++ ) + if ( place(row,column) ) + { + rowsArray[column] = row; + backtrack(column+1); + } +} + +int main() +{ + scanf("%d",&cases); + while ( cases-- ) + { + scanf("%d %d",&reqRow,&reqColumn); + // 0-indexed + reqRow--; reqColumn--; + + memset( rowsArray, 0, sizeof(rowsArray) ); + lineCounter = 0; + printf("SOLN COLUMN\n"); + printf(" # 1 2 3 4 5 6 7 8\n\n"); + backtrack(0); + if ( cases ) + printf("\n"); + } +} diff --git a/uva_cpp_clean/750/750-6.cpp b/uva_cpp_clean/750/750-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e6acd06be680ff4fcfd8f4dbbd629342381a46c --- /dev/null +++ b/uva_cpp_clean/750/750-6.cpp @@ -0,0 +1,50 @@ +/* + brute force > iterative > all permutations + difficulty: easy + date: 12/Jan/2020 + by: @brpapa +*/ +#include +#include +#include +#define abs(a) ((a) > 0 ? (a) : -(a)) +using namespace std; + +struct Taux { + int row[8]; // row[c] = linha da coluna c +}; +vector validArrangements; + +int main() { + // pré-processa todas as disposições válidas (q nao compartilham diagonais) + int row[8] = {0, 1, 2, 3, 4, 5, 6, 7}; + do { + bool valid = true; + for (int i = 0; i < 8 && valid; i++) + for (int j = i+1; j < 8 && valid; j++) + if (abs(i-j) == abs(row[i]-row[j])) + valid = false; + + if (valid) { + Taux tmp; for (int i = 0; i < 8; i++) tmp.row[i] = row[i]; + validArrangements.push_back(tmp); + } + } while (next_permutation(row, row+8)); + + int T; scanf("%d", &T); + while (T--) { + int r0, c0; scanf("%d %d", &r0, &c0); r0--; c0--; + + int s = 1; + printf("SOLN COLUMN\n # 1 2 3 4 5 6 7 8\n\n"); + for (Taux va : validArrangements) + if (va.row[c0] == r0) { + printf("%2d ", s++); + for (int c = 0; c < 8; c++) + printf(" %d", va.row[c]+1); + printf("\n"); + } + printf("%s", T > 0? "\n" : ""); + } + return 0; +} diff --git a/uva_cpp_clean/750/750-9.cpp b/uva_cpp_clean/750/750-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0825f275ba9cc3e3a905e5b25e79389df1434fb5 --- /dev/null +++ b/uva_cpp_clean/750/750-9.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +vector x, ans[100]; +bool ok; +int k, t; + +void f(int col){ + if(col == 8){ + ans[k++] = x; + return; + } + for(int i = 1; i <= 8; i++){ + ok = true; + for(int j = 0; j < x.size(); j++) + if(x[j] == i || abs(col - j) == abs(x[j] - i)){ + ok = false; + break; + } + if(ok){ + x.push_back(i); + f(col + 1); + x.pop_back(); + } + } +} + +int main(){ + f(0); + int n, m, c; + scanf("%d", &c); + while(c--){ + scanf("%d%d", &n, &m); + printf("SOLN COLUMN\n"); + printf(" # 1 2 3 4 5 6 7 8\n\n"); + t = 0; + for(int i = 0; i < k; i++) + if(ans[i][m - 1] == n){ + printf("%2d ", ++t); + for(int j = 0; j < 8; j++){ + printf("%d", ans[i][j]); + if(j != 7) + putchar(' '); + } + putchar('\n'); + } + if(c) + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/751/751-21.cpp b/uva_cpp_clean/751/751-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a25098eadd86cfa4fd900600d8ba3971aca9e93 --- /dev/null +++ b/uva_cpp_clean/751/751-21.cpp @@ -0,0 +1,93 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 751 + Name: Triangle War + Problem: https://onlinejudge.org/external/7/751.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +const int fstate = (1<<18) - 1; +int tcn[18], + lid[10][10], + ltr[18][2] = { + { 0, 1 }, { 1, 2 }, + { 1, 5 }, { 3, 4 }, { 4, 5 }, + { 5, 6 }, { 6, 7 }, + { 4, 10 }, { 8, 9 }, { 9, 10 }, + { 6, 12 }, { 10, 11 }, { 11, 12 }, + { 12, 13 }, { 13, 14 }, + { 9, 15 }, { 11, 16 }, { 13, 17 }, + }; + +bool seen[262143]; +int memo[262143]; +int alphabeta(int state) { + if (state == fstate) return 0; + if (seen[state]) return memo[state]; + + int bd = -10000; + for (int i=0, msk=1; i<18; ++i, msk<<=1) + if (!(state&msk)) { + int t1 = ltr[i][0], + t2 = ltr[i][1]; + ++tcn[t1]; ++tcn[t2]; + + int t = alphabeta(state | msk); + if (tcn[t1]!=3 && tcn[t2]!=3) + bd = max(bd, -t); + else bd = max(bd, t + (tcn[t1]==3) + (tcn[t2]==3)); + + --tcn[t1]; --tcn[t2]; + } + + seen[state] = 1; + return memo[state] = bd; +} + + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + for (int i=1,l=1,k=0; i<10; i+=l++) + for (int j=i; j> T; + for (int cse=1; cse<=T; ++cse) { + memset(tcn, 0, sizeof(tcn)); + + int state=0, score[2]={}; + bool player=0; + int m; cin >> m; + for (int i=0, u, v; i> u >> v; + int id = lid[u][v]; + state |= 1<score[1] ? 'A' : 'B') << " wins.\n"; + } +} diff --git a/uva_cpp_clean/753/753-21.cpp b/uva_cpp_clean/753/753-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4d6ae020d8177205208d40121e75f4b0d10c472 --- /dev/null +++ b/uva_cpp_clean/753/753-21.cpp @@ -0,0 +1,108 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 753 + Name: A Plug for UNIX + Problem: https://onlinejudge.org/external/7/753.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + + +//Edmonds-Karp +//return the largest flow,flow[] will return every edge's flow +//n, the number of nodes in the graph;mat, the capacity matrix +//O(VE^2) +#define MAXN 1000 +#define inf 1000000000 + +int max_flow(int n,int mat[][MAXN],int source,int sink,int flow[][MAXN]){ + int pre[MAXN],que[MAXN],d[MAXN],p,q,t,i,j; + if (source==sink) return inf; + for (i=0;i0) + flow[pre[i]-1][i]+=d[sink],i=pre[i]-1; + else + flow[i][-pre[i]-1]-=d[sink],i=-pre[i]-1; + } + for (j=i=0;i +#include +#include + +using namespace std; + +int main(){ + char dic[] = "22233344455566670778889990"; + char number[100], concat[100]; + string newLine; + int N, no; + cin >> N; + getline(cin, newLine); + while(N-- > 0){ + cin >> no; + getline(cin, newLine); + map ans; + for(int i = 0; i < no; i++){ + cin.getline(number,100); + int index = 0; + for(int j = 0; number[j] > 0; j++){ + if(number[j] >= '0' && number[j] <= '9'){ + concat[index] = number[j]; + index++; + }else if(isalpha(number[j])){ + concat[index] = dic[number[j] - 'A']; + index++; + } + if(index == 3){ + concat[index] = '-'; + index++; + } + } + concat[index] = '\0'; + ans[concat] += 1; + } + bool duplicate = false; + for(auto iterator = ans.begin(); iterator != ans.end(); iterator++){ + if(iterator->second != 1){ + duplicate = true; + cout << iterator->first << " " << iterator->second << endl; + } + } + if(!duplicate) printf("No duplicates.\n"); + if(N != 0) printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/755/755-19.cpp b/uva_cpp_clean/755/755-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22cea4cec7b050880801ff5a625eaaaa07a315a6 --- /dev/null +++ b/uva_cpp_clean/755/755-19.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +using namespace std; + +unordered_map dir; +set repeated; + +int proc(char c) { + switch (c) { + case 'A': + case 'B': + case 'C': return 2; + case 'D': + case 'E': + case 'F': return 3; + case 'G': + case 'H': + case 'I': return 4; + case 'J': + case 'K': + case 'L': return 5; + case 'M': + case 'N': + case 'O': return 6; + case 'P': + case 'R': + case 'S': return 7; + case 'T': + case 'U': + case 'V': return 8; + case 'W': + case 'X': + case 'Y': return 9; + } + return 0; +} + +int main() { + int N; + cin >> N; + while (N--) { + dir.clear(); + repeated.clear(); + int M; + cin >> M; + while (M--) { + string s; + cin >> s; + int val = 0; + for (int i = 0; i < s.length(); i++) { + if (s[i] == '-') continue; + val *= 10; + if (isalpha(s[i])) val += proc(s[i]); + else val += s[i] - '0'; + } + dir[val]++; + if (dir[val] > 1) repeated.insert(val); + } + if (repeated.empty()) printf("No duplicates.\n"); + else { + for (auto it = repeated.begin(); it != repeated.end(); ++it) { + printf("%03d-%04d %d\n", (*it)/10000, (*it)%10000, dir[*it]); + } + } + if (N) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/755/755-21.cpp b/uva_cpp_clean/755/755-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a46cccc0e29f6e514de5f48ec727332925bcfacc --- /dev/null +++ b/uva_cpp_clean/755/755-21.cpp @@ -0,0 +1,57 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 755 + Name: 487--3279 + Problem: https://onlinejudge.org/external/7/755.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int d[127]; + +int main() { + char wd[]="ABCDEFGHIJKLMNOPRSTUVWXY"; + for (int i=0; i<24; i++) + d[wd[i]] = '0' + (i/3) + 2; + + for (int i=0; i<10; i++) + d['0'+i] = '0' + i; + + char num[20], snum[9]="000-0000"; + int sid[]={0,1,2,4,5,6,7}; + + int T, n; + cin>>T; + while (T--) { + map uses; + cin >> n; + for (int i=0; i>num; + int c=0; + for (int i=0; c<7 && num[i]; i++) + if (d[num[i]]) + snum[sid[c++]]=d[num[i]]; + + uses[snum]++; + } + + bool nodups=1; + for (auto &s: uses) + if (s.second > 1) { + cout << s.first << " " << s.second << endl; + nodups=0; + } + + if (nodups) puts("No duplicates."); + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/758/758-21.cpp b/uva_cpp_clean/758/758-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d775c0bc53c2528c3ccdd59bd8ca1d649737976 --- /dev/null +++ b/uva_cpp_clean/758/758-21.cpp @@ -0,0 +1,168 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 758 + Name: The Same Game + Problem: https://onlinejudge.org/external/7/758.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int H[17], LL[17], RR[17], C[11][17], CI[17]; +char M[11][17]; + + +bool D[11][17], K[11][17]; +int dcn, Y[200], X[200]; +void dfs(int r, int c) { + if (D[r][c] || r > H[c]) return; + D[r][c] = 1; + + Y[dcn] = r; X[dcn] = c; ++dcn; + char ch = M[r][c]; + + if (r > 1 && ch==M[r-1][c]) + dfs(r-1, c); + + if (r < H[c] && ch==M[r+1][c]) + dfs(r+1, c); + + if (LL[c] > 0 && ch==M[r][LL[c]]) + dfs(r, LL[c]); + + if (RR[c] < 16 && ch==M[r][RR[c]]) + dfs(r, RR[c]); +} + +// ------------------------------------------------- + +void updateCounts(int s, int e) { + if (s < 1) s = 1; + if (e >15) e = 15; + for (int c=s; c<=e; c=RR[c]) + for (int r=H[c]; r>0; --r) + if (!C[r][c]) { + dfs(r, c); + for (int i=0; i mxc) mxc = c; + } + + if (rs) + C[r][c] = + C[r][LL[c]] = + C[r][RR[c]] = 0; + } + + // remove column if necessary + if (!(H[c] = h)) { + for (int r=min(H[LL[c]], H[RR[c]]); r>0; --r) + C[r][LL[c]] = C[r][RR[c]] = 0; + + LL[RR[c]] = LL[c]; + RR[LL[c]] = RR[c]; + for (int k=c+1; k<16; ++k) + --CI[k]; + } + } + + // update counts of moved balls + if (mxc>0) updateCounts(LL[mnc], RR[mxc]); +} + +void printState() { + for (int r=10; r>0; --r) { + for (int c=RR[0]; c<16; c=RR[c]) + putchar(H[c] < r ? ' ' : M[r][c]); + putchar('\n'); + } + putchar('\n'); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin>>T; + for (int cse=1; cse<=T; ++cse) { + if (cse>1) cout << endl; + cout << "Game " << cse << ":\n\n"; + + memset(C, 0, sizeof(C)); + for (int c=0; c<17; ++c) { + CI[c] = c; + LL[c] = c-1; + RR[c] = c+1; + H[c] = 10; + } + + for (int r=10; r>0; --r) + cin >> (M[r]+1); + + updateCounts(0, 16); + + int sum = 0; + bool hasMove = 1; + for (int i=1; hasMove; ++i) { + int bn = 1, br=0, bc=0; + + for (int c=RR[0]; c<16; c=RR[c]) + for (int r=1; r<=H[c]; ++r) + if (C[r][c] > bn){ + bn = C[r][c]; + br = r; + bc = c; + } + + if ((hasMove = br)) { + int sco = (bn-2)*(bn-2); + //printState(); + cout << "Move " << i << " at (" << br << ',' << CI[bc] + << "): removed " << bn << " balls of color " << M[br][bc] + << ", got " << sco << " points.\n"; + markComp(br, bc); + removeMarks(); + sum += sco; + } + } + + int rem = 0; + for (int c=RR[0]; c<16; c=RR[c]) + rem += H[c]; + + if (!rem) sum += 1000; + cout << "Final score: " << sum << ", with " << rem << " balls remaining.\n"; + } +} diff --git a/uva_cpp_clean/758/758-9.cpp b/uva_cpp_clean/758/758-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..236bcd38b6b91559fdb82b23b5f66e5445007b6c --- /dev/null +++ b/uva_cpp_clean/758/758-9.cpp @@ -0,0 +1,126 @@ +#include + +#define SIZE 20 +#define LIM 80 + +using namespace std; + +int tc, moves, posr[LIM], posc[LIM], balls[LIM], points[LIM], score, remainder_; +char grid[SIZE][SIZE], color[LIM]; +bool vis[SIZE][SIZE]; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; +const int N = 10, M = 15; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M ); +} + +int floodfill(int r, int c, char ch1, char ch2){ + grid[r][c] = ch2; + vis[r][c] = true; + int ct = 1; + for(int d = 0; d < 4; d++){ + int nr = r + dr[d], nc = c + dc[d]; + if( is_possible(nr, nc) and grid[nr][nc] == ch1 and not vis[nr][nc]) + ct += floodfill(nr, nc, ch1, ch2); + } + return ct; +} + +int Count(int r, int c){ + int ct = 1; + for(int d = 0; d < 4; d++) + if( is_possible(r + dr[d], c + dc[d]) and grid[r + dr[d]][c + dc[d]] == grid[r][c] ) + ct++; + return ct; +} + +bool solved(){ + remainder_ = 0; + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col++) + if( grid[row][col] == '.' ) continue; + else if( Count(row, col) != 1 ) return false; + else remainder_++; + if( remainder_ == 0 ) score += 1000; + return true; +} + +bool Is_empty(int c){ + for(int row = 0; row < N; row++) + if( grid[row][c] != '.' ) return false; + return true; +} + +void swap_columns(int c1, int c2){ + for(int row = 0; row < N; row++) + swap( grid[row][c1], grid[row][c2] ); +} + +void shiftColumns(){ + for(int row = N - 1; row >= 0; row--) + for(int col = 0; col < M; col++) + if( grid[row][col] == '.' ){ + for(int r = row - 1; r >= 0; r--) + if( grid[r][col] != '.' ) swap(grid[row][col], grid[r][col]), r = 0; + } +} + +void shiftRows(){ + for(int col = 0; col < M; col++) + if( Is_empty(col) ){ + for(int c = col + 1; c < M; c++) + if( not Is_empty(c) ) + swap_columns(col, c), c = M; + } +} + +void modifyGrid(){ + shiftColumns(); + shiftRows(); +} + +void solve(){ + while( not solved() ){ + memset(vis, false, sizeof vis); + int _balls = 0, _posr, _posc; + char _color; + for(int col = 0; col < M; col++) + for(int row = N - 1; row >= 0; row--) + if( not vis[row][col] and grid[row][col] != '.' and Count(row, col) >= 2 ){ + int CC = floodfill(row, col, grid[row][col], grid[row][col]); + if( CC > _balls ) _balls = CC, _posr = row, _posc = col, _color = grid[row][col]; + } + posr[moves] = _posr, posc[moves] = _posc, balls[moves] = _balls, color[moves] = _color; + points[moves] = ( _balls - 2 ) * ( _balls - 2 ), score += points[moves++]; + memset(vis, false, sizeof vis); + floodfill(_posr, _posc, _color, '.'); + modifyGrid(); + } +} + +void printSolution(int t){ + if( t != 1 ) putchar('\n'); + printf("Game %d:\n\n", t); + for(int it = 0; it < moves; it++) + printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n", it + 1, 10 - posr[it], 1 + posc[it], balls[it], color[it], points[it]); + printf("Final score: %d, with %d balls remaining.\n", score, remainder_); +} + +inline void clear_(){ + moves = score = remainder_ = 0; +} + +int main(){ + scanf("%d", &tc); + for(int t = 1; t <= tc; t++){ + clear_(); + getchar(); + for(int row = 0; row < N; row++) scanf("%s", grid[row]); + solve(); + printSolution(t); + clear_(); + } + return(0); +} diff --git a/uva_cpp_clean/759/759-9.cpp b/uva_cpp_clean/759/759-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04d200c96dfc653e54ba9332aef421de7c38cbf0 --- /dev/null +++ b/uva_cpp_clean/759/759-9.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +const int LEN = 100, LIM = 4000; + +int n; +char line[LEN]; +map < string, int > mp; + +void DecimalToRoman(int num){ + map < int, string > cvt; + cvt[1] = "I", cvt[10] = "X", cvt[100] = "C", cvt[1000] = "M"; + cvt[9] = "IX", cvt[90] = "XC", cvt[900] = "CM"; + cvt[5] = "V", cvt[50] = "L", cvt[500] = "D"; + cvt[4] = "IV", cvt[40] = "XL", cvt[400] = "CD"; + string s = ""; + for(map :: reverse_iterator it = cvt.rbegin(); it != cvt.rend(); ++it) + while( num >= it -> first ){ + s += ( it -> second ); + num -= it -> first; + } + mp[s] = n; +} + +int main(){ + for( n = 0; n < LIM; n++) DecimalToRoman(n); + while(cin.getline(line, LEN)){ + if( mp.find(line) != mp.end() ) printf("%d\n", mp[line]); + else puts("This is not a valid number"); + } +} diff --git a/uva_cpp_clean/760/760-19.cpp b/uva_cpp_clean/760/760-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b703e1c31b462d75a469f1d468ea9f66324bad51 --- /dev/null +++ b/uva_cpp_clean/760/760-19.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +int size = 4; +char characters[4] = {'a', 'c', 'g', 't'}; + +struct node { + string pattern; + bool isword; + bool issuffix; + int nodes; + unordered_map map; + int words; + int lastword; + + node() { isword = issuffix = false; pattern = ""; nodes = 0; words = 0; lastword = -1;} + + node(string pattern, bool isword, bool issuffix, unordered_map & map, int nodes, int words, int lastword) { + this->pattern = pattern; + this->isword = isword; + this->issuffix = issuffix; + this->map = map; + this->nodes = nodes; + this->words = words; + this->lastword = lastword; + } + + void insert(string word, int i, bool isword, int wordId) { + if (i < word.length()) { + if (nodes == 0) pattern = word.substr(i, word.length()-i); + else { + int j; + for (j = 0; i < word.length() && j < pattern.length() && word[i] == pattern[j]; i++, j++); + if (j == pattern.length()) { + if (i < word.length()) { + if (!map.count(word[i])) map.insert(pair(word[i], new node())); + map[word[i]]->insert(word, i, isword, wordId); + } + } else { + unordered_map aux = map; + map = unordered_map (); + map.insert(pair(pattern[j], new node(pattern.substr(j, pattern.length()-j), this->isword, issuffix, aux, nodes, words, lastword))); + pattern = pattern.substr(0, j); + issuffix = false; + this->isword = false; + if (i < word.length()) { + map.insert(pair(word[i], new node())); + map[word[i]]->insert(word, i, isword, wordId); + } + } + //if (!map.count(word[i])) map.insert(pair(word[i], new node(, isword, issuffix, map))); + //else map[word[i]]->insert(word, i+1, isword); + } + if (i >= word.length() || nodes == 0) { + issuffix = true; + this->isword |= isword; + } + nodes++; + if (wordId > lastword) lastword = wordId, words++; + } + } + + int search(string word, int i) { + int j; + for (j = 0; i < word.length() && j < pattern.length() && word[i] == pattern[j]; i++, j++); + if (j == pattern.length()) { + if (i == word.length()) return isword? 2: issuffix? 1: 0; + else { + if (i >= word.length() || !map.count(word[i])) return 0; + return map[word[i]]->search(word, i); + } + } else return 0; + } + + void print(int i){ + //cout << i << "p:" << pattern << endl; + printf("%d p: %s w: %d n: %d\n", i, pattern.c_str(), words, nodes); + for (auto it = map.begin(); it != map.end(); ++it) it->second->print(i+1); + } + + set* lrs(int & l, int insertedWords) { + if (words != insertedWords) { l = 0; return NULL; } + set* res = new set(); + l = pattern.length(); + res->insert(pattern); + if (!map.empty()) { + for (int i = 0; i < size; i++) { + if (map.count(characters[i])) { + int rl; + set* auxi = map[characters[i]]->lrs(rl, insertedWords); + if (auxi != NULL) { + if (pattern.length() + rl > l) { l = pattern.length() + rl; res->clear(); } + if (pattern.length() + rl == l) { + for (auto it = auxi->begin(); it != auxi->end(); ++it) { + res->insert(pattern + (*it)); + } + } + delete auxi; + } + } + } + } + return res; + } +}; + +struct suffixtree { + node* root; + int words; + + suffixtree() { root = new node(); words = 0; } + + ~suffixtree() { delete root; } + + void insert(string word) { + for (int j = word.length() - 1; j >= 0; j--) + root->insert(word, j, j == 0, words); + words++; + } + + int search(string word) { return root->search(word, 0); } + + void clear() { delete root; root = new node(); words = 0; } + + void print() { root->print(0); } + + set* lrs(int & l) { + return root->lrs(l, words); + } +}; + +int main() { + suffixtree tree; + string s1, s2; + bool first = false; + while (cin >> s1 >> s2) { + if (first) printf("\n"); + else first = true; + tree.insert(s1); + tree.insert(s2); + int l = 0; + set* sol = tree.lrs(l); + if (sol == NULL || l == 0) printf("No common sequence.\n"); + else for (auto it = sol->begin(); it != sol->end(); ++it) printf("%s\n", (*it).c_str()); + delete sol; + tree.clear(); + } + return 0; +} diff --git a/uva_cpp_clean/762/762-13.cpp b/uva_cpp_clean/762/762-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb96339416ae90cb273ff4e419f527783d6ce96c --- /dev/null +++ b/uva_cpp_clean/762/762-13.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 500000 + +bool f = false; +int t, n; +string src, dest; +int src_num, dest_num; + +vector parent; +vector visited; +map name; +map reve; +vector > graph; + +void printSol(int start, int end) +{ + if (f) + cout << endl; + + else + f = true; + + if(!visited[end]) + { + cout << "No route" << endl; + } + + else + { + stack p; + + for (int i = end; i != -1; i = parent[i]) + { + p.push(i); + } + + int temp = p.top(); + + p.pop(); + + while (!p.empty()) + { + int sec = p.top(); + + p.pop(); + + cout << reve[temp] << " " << reve[sec] << endl; + + temp = sec; + } + } +} + +void initialize() +{ + n = 0; + + name.clear(); + reve.clear(); + + graph.clear(); + graph.resize(MAX); + + visited.clear(); + visited.resize(MAX, false); + parent.clear(); + parent.resize(MAX, 0); + +} + +void bfs(int start, int end) +{ + queue q; + + q.push(start); + + visited[start] = true; + parent[start] = -1; + + while(!q.empty()) + { + int top = q.front(); + + q.pop(); + + for (int i = 0; i < graph[top].size(); i++) + { + int child = graph[top][i]; + + if(!visited[child]) + { + parent[child] = top; + + visited[child] = true; + + q.push(child); + } + } + } + + printSol(start, end); +} + +int main() +{ + while (cin >> t) + { + initialize(); + + for (int i = 0; i < t; i++) + { + string v, u; + int num_v, num_u; + + cin >> v >> u; + + num_v = name[v]; + num_u = name[u]; + + if(name[v] == 0) + num_v = ++n; + + if(name[u] == 0) + num_u = ++n; + + reve[num_v] = v; + reve[num_u] = u; + + name[v] = num_v; + name[u] = num_u; + + graph[num_v].push_back(num_u); + graph[num_u].push_back(num_v); + } + + cin >> src >> dest; + + src_num = name[src]; + dest_num = name[dest]; + + if (src_num == 0) + src_num = ++n; + + if (dest_num == 0) + dest_num = ++n; + + reve[src_num] = src; + reve[dest_num] = dest; + + name[src] = src_num; + name[dest] = dest_num; + + bfs(src_num, dest_num); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/762/762-14.cpp b/uva_cpp_clean/762/762-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..793914d1ce91d922fff9af3a6ec3709781513836 --- /dev/null +++ b/uva_cpp_clean/762/762-14.cpp @@ -0,0 +1,136 @@ +/*************************************************** + * Problem name : 762 We Ship Cheap.cpp + * Problem Link : https://uva.onlinejudge.org/external/7/762.pdf + * OJ : UVA + * Verdict : AC + * Date : 2017-12-01 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE,MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +mapmp; +mapmpp; +vectoradj[MAX]; +bool visit[MAX]; +ll par[MAX], cost[MAX]; +ll BFS(ll s, ll d) { + queueQ; + Q.push(s); + cost[s] = 0; + visit[s] = true; + while (!Q.empty()) { + ll u = Q.front(); + Q.pop(); + for (int i = 0; i < (int)adj[u].size(); i++) { + ll v = adj[u][i]; + if (!visit[v]) { + visit[v] = true; + par[v] = u; + cost[v] = cost[u] + 1; + Q.push(v); + } + } + } + return cost[d]; +} +int main () { + ll n, t = 1; + while (scanf("%lld", &n) == 1) { + ll k = 1; + if (t != 1) nl; + t++; + for (int i = 1; i <= n; i++) { + string u, v; + cin >> u >> v; + if (mp[u] == 0) { + mp[u] = k++; + mpp[mp[u]] = u; + } + if (mp[v] == 0) { + mp[v] = k++; + mpp[mp[v]] = v; + } + adj[mp[u]].push_back(mp[v]); + adj[mp[v]].push_back(mp[u]); + } + string s, d; + cin >> s >> d; + if (mp[s] == 0 || mp[d] == 0) { + printf("No route\n"); + } else { + SET(par, 0); + SET(visit, false); + SET(cost, -1); + ll chk = BFS(mp[s], mp[d]); + if (chk == -1) { + printf("No route\n"); + } else { + vectorpath; + path.push_back(mp[d]); + for (int i = mp[d]; ;) { + path.push_back(par[i]); + if (par[i] == mp[s]) break; + i = par[i]; + } + ll sz = path.size(); + for (int i = sz - 1; i >= 1; i--) { + cout << mpp[path[i]] << " " << mpp[path[i - 1]] << "\n"; + } + } + } + mp.clear(), mpp.clear(); + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + } + } + return 0; +} diff --git a/uva_cpp_clean/762/762-21.cpp b/uva_cpp_clean/762/762-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d94625f160ae391c487f0a179394cd6dd2251d8f --- /dev/null +++ b/uva_cpp_clean/762/762-21.cpp @@ -0,0 +1,83 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 762 + Name: We Ship Cheap + Problem: https://onlinejudge.org/external/7/762.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +vector adj[1000]; +bool seen[1000]; +int fid[1000], pre[1000], cse; +int readid() { + char s[5]; + cin >> s; + int u = (s[0]-'A')*26 + s[1]-'A'; + if (fid[u] != cse) { + seen[u] = 0; + fid[u] = cse; + adj[u].clear(); + } + return u; +} + +void printid(int id) { + cout << char((id/26)+'A') + << char((id%26)+'A'); +} + +void printpath(int src, int dst) { + if (src == dst) return; + printpath(src, pre[dst]); + printid(pre[dst]); + cout << ' '; + printid(dst); + cout << endl; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int m; + for (;cin >> m; ++cse) { + if (cse) cout << endl; + for (int i=0; i q; + q.push(src); + seen[src] = 1; + while (!q.empty()) { + int u = q.front(); q.pop(); + for (int v: adj[u]) + if (!seen[v]) { + seen[v] = 1; + pre[v] = u; + + if (v == dst) { + printpath(src, dst); + goto fin; + } + q.push(v); + } + } + + cout << "No route\n"; + fin:; + } +} diff --git a/uva_cpp_clean/762/762-9.cpp b/uva_cpp_clean/762/762-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5bd8ee54421407dc4aa4ba55819898d6df17dcb --- /dev/null +++ b/uva_cpp_clean/762/762-9.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +int tc, n; +string s1, s2, _source, _target; +map d; +map path; +map >G; + +void printPath(string source, string target){ + if(source == target) return; + printPath(source, path[target]); + cout << path[target] << ' ' << target << endl; +} + +int bfs(string source, string target){ + queue Q; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + string u = Q.front(); Q.pop(); + if(u == target) return d[u]; + for(int i = 0; i < (int)G[u].size(); i++){ + string v = G[u][i]; + if(d[v] == -1) + d[v] = d[u]+1, path[v] = u, Q.push(v); + } + } + return -1; +} + +int main(){ + while(scanf("%d", &n) == 1){ + G.clear(); + if(tc++) putchar('\n'); + for(int i = 0; i < n; i++){ + cin >> s1 >> s2; + G[s1].push_back(s2); + G[s2].push_back(s1); + d[s1] = d[s2] = -1; + } + cin >> _source >> _target; + if(bfs(_source, _target) == -1) printf("No route\n"); + else printPath(_source, _target); + } + return(0); +} diff --git a/uva_cpp_clean/763/763-19.cpp b/uva_cpp_clean/763/763-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1c542a7fa78fe6b455cdf4112663935d6d00aa0 --- /dev/null +++ b/uva_cpp_clean/763/763-19.cpp @@ -0,0 +1,71 @@ +#include +#include +using namespace std; + +int fib[200]; + +void fix(int i) { + while (fib[i] > 1) { + fib[i] -= 2; + if (i == 0) { + fib[1] += 1; + if (fib[1] > 1) fix(1); + } else if (i == 1) { + fib[2] += 1; + fib[0] += 1; + if (fib[2] > 1) fix(2); + if (fib[0] > 1) fix(0); + } else { + fib[i+1] += 1; + fib[i-2] += 1; + if (fib[i+1] > 1) fix(i+1); + if (fib[i-2] > 1) fix(i-2); + } + } +} + +int main() { + string n1, n2; + bool bln = false; + while (cin >> n1 >> n2) { + if (bln) printf("\n"); + else bln = true; + int l = max(n1.length(), n2.length()) + 40; + for (int i = l; i >= 0; i--) { + fib[i] = 0; + if (i < n1.length()) fib[i] += n1[n1.length() - 1 - i] - '0'; + else fib[i] += 0; + if (i < n2.length()) fib[i] += n2[n2.length() - 1 - i] - '0'; + else fib[i] += 0; + } + for (int i = l; i >= 0; i--) { + if (fib[i] > 1) fix(i); + } + bool stop = false; + while (!stop) { + stop = true; + if (fib[0] > 1) stop = false, fix(0); + for (int i = 1; i <= l; i++) { + if (fib[i] > 1) { + stop = false; + fix(i); + } + if (fib[i] == 1 && fib[i-1] == 1) { + stop = false; + fib[i] = fib[i-1] = 0; + fib[i+1]++; + } + } + } + bool first = false; + for (int i = l; i >= 0; i--) { + if (first || fib[i] != 0) { + first = true; + printf("%d", fib[i]); + } + } + if (!first) printf("0"); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/763/763-21.cpp b/uva_cpp_clean/763/763-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86f39ad84653d7ba1baee047a574a06e5a5f8bf8 --- /dev/null +++ b/uva_cpp_clean/763/763-21.cpp @@ -0,0 +1,75 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 763 + Name: Fibinary Numbers + Problem: https://onlinejudge.org/external/7/763.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int n1[2000], n1l; +void inc(int ind) { + if (ind >= n1l) { + for (int i=n1l; i<=ind+1; i++) + n1[i] = 0; + n1l = ind+1; + } + + if (!n1[ind]) { + if (ind>0 && n1[ind-1]) { + n1[ind-1] = 0; + inc(ind + 1); + } + else if (n1[ind+1]) { + n1[ind+1] = 0; + inc(ind + 2); + } + else n1[ind] = 1; + } + else { + if (!ind) { + n1[0] = 0; + inc(1); + } + else if (ind == 1) { + n1[0] = 1; + n1[1] = 0; + inc(2); + } + else { + inc(ind-1); + inc(ind-2); + } + } +} + +int main() { + bool frst=1; + char b1[1000], b2[1000]; + while (scanf("%s %s", b1, b2) == 2) { + n1l = 1; n1[0]=n1[1]=0; + int k=0; + for (int i=strlen(b1)-1; i>=0; i--, k++) + if (b1[i] == '1') + inc(k); + + k=0; + for (int i=strlen(b2)-1; i>=0; i--, k++) + if (b2[i] == '1') + inc(k); + + if (frst) frst = 0; + else putchar('\n'); + for (int i=n1l-1; i>=0; i--) + putchar(n1[i]+'0'); + putchar('\n'); + } +} diff --git a/uva_cpp_clean/763/763-9.cpp b/uva_cpp_clean/763/763-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da72b593712d0ff69b596777337d2b292a934a7b --- /dev/null +++ b/uva_cpp_clean/763/763-9.cpp @@ -0,0 +1,117 @@ +#include + +#define LIMIT 105 + +using namespace std; + +int tc, ct, carry, l1, l2, minl, maxl, k, i, j, aux; +char fibo[LIMIT][LIMIT], n1[LIMIT], n2[LIMIT], ans[LIMIT], tmp[LIMIT], final_tmp[LIMIT], fibinary[LIMIT]; +bool ok; + +char* subt(char *s1, char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + carry = 0; + for(k = 0, i = l1 - 1, j = l2 - 1; k < l2; k++, i--, j--){ + aux = (s1[i] - '0') - (s2[j] - '0') - carry; + if(aux >= 0) + tmp[k] = aux + '0', carry = 0; + else + tmp[k] = (aux + 10) + '0', carry = 1; + } + while(k < l1){ + aux = (s1[i--] - '0') - carry; + if(aux >= 0) + tmp[k++] = aux + '0', carry = 0; + else + tmp[k++] = aux + '0', carry = 1; + } + while(tmp[k - 1] == '0') + k--; + tmp[k] = '\0'; + i = 0; + while(k > 0) + final_tmp[i++] = tmp[--k]; + final_tmp[i] = '\0'; + return final_tmp; +} + +bool less_or_equal(char* s1, char* s2){ + l1 = strlen(s1), l2 = strlen(s2); + if(l1 < l2) + return true; + if(l1 > l2) + return false; + for(i = 0; i < l1; i++) + if(s1[i] < s2[i]) + return true; + else if(s1[i] > s2[i]) + return false; + return true; +} + +char* sum(char *s1, char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + minl = l1 > l2 ? l2 : l1; + maxl = l1 > l2 ? l1 : l2; + carry = 0; + for(k = 0,i = l1 - 1,j = l2 - 1; k < minl; k++, i--, j--){ + aux = s1[i] - '0' + s2[j] - '0' + carry; + tmp[k] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l1){ + aux = s1[i--] - '0' + carry; + tmp[k++] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l2){ + aux = s2[j--] - '0' + carry; + tmp[k++] = aux % 10 + '0'; + carry = aux / 10; + } + if(carry) + tmp[maxl++] = carry + '0', tmp[maxl] = '0'; + else + tmp[maxl] = '\0'; + i = 0; + while(maxl > 0) + final_tmp[i++] = tmp[--maxl]; + final_tmp[i] = '\0'; + return final_tmp; +} + +void generateFibo(){ + fibo[0][0] = '0', fibo[0][1] = '\0'; + fibo[1][0] = '1', fibo[1][1] = '\0'; + for(int t = 2; t < LIMIT; t++) + strcpy(fibo[t], sum(fibo[t - 1], fibo[t - 2])); +} + +int main(){ + generateFibo(); + while(scanf("%s\n%s", n1, n2) == 2){ + getchar(); + if(tc) + putchar('\n'); + tc++; + strcpy(fibinary, fibo[0]); + for(int x = strlen(n1) - 1, y = strlen(n1) + 1; x >= 0; x--) + if(n1[x] == '1') + strcpy(fibinary, sum(fibinary, fibo[y - x])); + for(int x = strlen(n2) - 1, y = strlen(n2) + 1; x >= 0; x--) + if(n2[x] == '1') + strcpy(fibinary, sum(fibinary, fibo[y - x])); + ok = false; + ct = 0; + for(int x = LIMIT - 1; x >= 2; x--) + if(less_or_equal(fibo[x], fibinary)) + strcpy(fibinary, subt(fibinary, fibo[x])), ans[ct++] = '1', ok = true; + else if(ok) + ans[ct++] = '0'; + if(!ct) + ans[ct++] = '0'; + ans[ct] = '\0'; + printf("%s\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/775/775-21.cpp b/uva_cpp_clean/775/775-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfc4e85a78fc90d3b24822e25901f94e308a928f --- /dev/null +++ b/uva_cpp_clean/775/775-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 775 + Name: Hamiltonian Cycle + Problem: https://onlinejudge.org/external/7/775.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int P[286], n; +vector adj[286]; +bool mat[286][286], chosed[286]; +bool bt(int idx, int u, int ce1) { + if (!ce1) return 0; + P[idx] = u; + if (idx == n-1) { + for (int i=0; i> n) { + cin.ignore(100, '\n'); + for (int i=1; i<=n; ++i) + adj[i].clear(); + + memset(mat, 0, sizeof(mat)); + while (cin.peek()!='%' && cin >> u >> v) { + if (!mat[u][v]) { + adj[u].push_back(v); + adj[v].push_back(u); + mat[u][v] = mat[v][u] = 1; + } + cin.ignore(100, '\n'); + } + cin.ignore(100, '\n'); + + int ce1=0; + for (int i=1; i<=n; ++i) + ce1 += mat[i][1]; + + memset(chosed, 0, sizeof(chosed)); + if (!bt(0, 1, ce1)) cout << "N\n"; + } +} diff --git a/uva_cpp_clean/776/776-21.cpp b/uva_cpp_clean/776/776-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb8870f809bbba79d82141c466ef64590930e165 --- /dev/null +++ b/uva_cpp_clean/776/776-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 776 + Name: Monkeys in a Regular Forest + Problem: https://onlinejudge.org/external/7/776.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MAXN 105 +char M[MAXN][MAXN]; +int S[MAXN][MAXN], mxc[MAXN], w, h; +void dfs(int y, int x, int id) { + if (S[y][x]) return; + S[y][x] = id; + + char cc = M[y][x]; + for (int i=-1; i<2; ++i) + for (int j=-1; j<2; ++j) if (i||j) { + int ny=y+i, nx=x+j; + if (ny>=0 && ny=0 && nx0); + } + + for (int i=0; i + +#define SIZE 1010 + +using namespace std; + +int N, M, ct, ans[SIZE][SIZE], sep[SIZE]; +char grid[SIZE][SIZE]; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M ); +} + +void floodfill(int r, int c, char ch){ + if( not is_possible(r, c) ) return; + if( grid[r][c] != ch ) return; + grid[r][c] = '.'; + ans[r][c] = ct; + for(int d = 0; d < 8; d++) + floodfill(r + dr[d], c + 2 * dc[d], ch); +} + +void solve(){ + M = strlen(grid[0]); + ct = 1; + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col += 2) + if( grid[row][col] != '.' ) + floodfill(row, col, grid[row][col]), ct++; + memset(sep, 0, sizeof sep); + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col += 2) + if( ans[row][col] < 10 ) sep[col] = max(sep[col], 1); + else if( ans[row][col] < 100 ) sep[col] = max(sep[col], 2); + else if( ans[row][col] < 1000 ) sep[col] = max(sep[col], 3); + else sep[col] = 4; + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col += 2) + printf( col != M - 1 ? "%*d " : "%*d\n", sep[col], ans[row][col]); + puts("%"); + N = 0; +} + +int main(){ + while(cin.getline(grid[N], SIZE)){ + if( grid[N][0] == '%' ) solve(); + else N++; + } + solve(); + return(0); +} diff --git a/uva_cpp_clean/782/782-21.cpp b/uva_cpp_clean/782/782-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52d0dc3d9cfa53d738fadf8658a362c9a2cc95c7 --- /dev/null +++ b/uva_cpp_clean/782/782-21.cpp @@ -0,0 +1,108 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 782 + Name: Contour Painting + Problem: https://onlinejudge.org/external/7/782.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline unsigned int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())); + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + while (ch!='\n') ch=readchar(); + return r; +} + +int readLine(char *str) { + int len=0; + char ch; + while ((ch=readchar())!='\n' && ch!=EOF) + str[len++] = ch; + + if (ch == EOF && !len) return EOF; + str[len] = 0; + return len; +} + +// ------------------------------------------------------------ + +char M[320][880]; +bool S[320][880]; +int ll[320], h, w, + dy[] = {0,1,0,-1}, + dx[] = {1,0,-1,0}; +void dfs(int y, int x) { + if (S[y][x]) return; + S[y][x] = 1; + if (x < ll[y]) + M[y][x] = ' '; + + for (int k=0; k<4; ++k) { + int ny=y+dy[k], nx=x+dx[k]; + if (ny>=0 && ny=0 && nx +using namespace std; +using vb=vector; +using vvb=vector; +using vs=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t; + string s; + cin>>t; + getline(cin,s); + while(t--){ + vs a; + vvb b; + for(;;){ + getline(cin,s); + s.append(string(81-s.size(),' ')); + a.push_back(s); + if(s[0]=='_')break; + b.push_back(vb(81)); + } + functiondfs=[&](int y,int x, int k){ + b[y][x]=1; + int r[]={-1,0,1,0}; + int c[]={0,1,0,-1}; + for(int i=0;i<4;i++){ + int u=y+r[i],v=x+c[i]; + if(u>=0&&u=0&&v + +#define SIZE 100 + +using namespace std; + +int tc, N, M, len; +char grid[SIZE][SIZE]; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; +bool vis[SIZE][SIZE]; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M ); +} + +void floodfill(int r, int c){ + grid[r][c] = ' '; + vis[r][c] = true; + for(int d = 0; d < 4; d++){ + int row = r + dr[d], col = c + dc[d]; + if( is_possible(row, col) and not vis[row][col]){ + if( grid[row][col] == 'X' ) + grid[r][c] = '#'; + else + floodfill(row, col); + } + } +} + +void solve(){ + memset(vis, false, sizeof vis); + M++; + for(int row = 0; row < N; row++){ + len = strlen(grid[row]); + while( len != M ) grid[row][len++] = ' '; + grid[row][len] = '\0'; + } + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col++) + if( grid[row][col] == '*' ) + floodfill(row, col), row = N, col = M; + for(int row = 0; row <= N; row++){ + len = strlen(grid[row]) - 1; + while( grid[row][len] == ' ' ) grid[row][len--] = '\0'; + } + for(int row = 0; row <= N; row++) + printf("%s\n", grid[row]); +} + +int main(){ + scanf("%d", &tc); getchar(); + while(tc--){ + N = M = 0; + while(cin.getline(grid[N], SIZE)){ + if( grid[N][0] == '_' ) break; + M = max(M, (int) strlen(grid[N])); + N++; + } + solve(); + } + return(0); +} diff --git a/uva_cpp_clean/784/784-6.cc b/uva_cpp_clean/784/784-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..3a4b1553ce5bb47901da3e4afadf8622ad1933ed --- /dev/null +++ b/uva_cpp_clean/784/784-6.cc @@ -0,0 +1,34 @@ +// https://uva.onlinejudge.org/external/7/784.pdf +#include +using namespace std; +using vs=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t; + cin>>t; + string s; + getline(cin,s); + while(t--){ + vs a; + for(;;){ + getline(cin,s); + a.push_back(s); + if(s[0]=='_')break; + } + functiondfs=[&](int y,int x){ + a[y][x]='#'; + int r[]={-1,0,1,0}; + int c[]={0,1,0,-1}; + for(int i=0;i<4;i++){ + int u=y+r[i],v=x+c[i]; + if(a[u][v]==' ')dfs(u,v); + } + }; + for(int i=0;i + +#define SIZE 100 + +using namespace std; + +int tc, N, M; +char grid[SIZE][SIZE]; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M ); +} + +void floodfill(int r, int c){ + if( not is_possible(r, c) ) return; + if( grid[r][c] != ' ' ) return; + grid[r][c] = '#'; + for(int d = 0; d < 4; d++) + floodfill(r + dr[d], c + dc[d]); +} + +void solve(){ + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col++) + if( grid[row][col] == '*' ) + grid[row][col] = ' ', floodfill(row, col), row = N, col = M; + for(int row = 0; row <= N; row++) + printf("%s\n", grid[row]); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + N = M = 0; + while(cin.getline(grid[N], SIZE)){ + if( grid[N][0] == '_' ) break; + else N++; + } + M = strlen(grid[0]); + solve(); + } + return(0); +} diff --git a/uva_cpp_clean/785/785-21.cpp b/uva_cpp_clean/785/785-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8594cdb04a025270e5b76eed6cdb3da771022710 --- /dev/null +++ b/uva_cpp_clean/785/785-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 785 + Name: Grid Colouring + Problem: https://onlinejudge.org/external/7/785.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} + +int readLine(char *str) { + int l=0; + char ch; + while ((ch=readchar())!='\n' && ch!=EOF) { + *str++ = ch; + ++l; + } + *str = 0; + return ch == EOF && !l ? EOF : l; +} + +// -------------------------------------------------------------- + +char M[32][81], w[31], sep, h; +void dfs(int y, int x, char c) { + if (x >= w[y]) return; + M[y][x] = c; + + if (x>0 && M[y][x-1] == ' ') + dfs(y, x-1, c); + + if (M[y][x+1] == ' ') + dfs(y, x+1, c); + + if (y>0 && M[y-1][x] == ' ') + dfs(y-1, x, c); + + if (y +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +string s[100]; +char c; +bool f; +void dfs(int i,int j) +{ + if(f & s[i][j]!=' ') return; + s[i][j]=c; + f=true; + dfs(i,j-1); + dfs(i,j+1); + dfs(i-1,j); + dfs(i+1,j); +} +int main() +{ + int n=-1; + while(getline(cin,s[++n])!=NULL) + { + if(s[n][0]!='_') + continue; + for0(i,n) + { + for(int j=0;s[i][j];j++) + { + if(s[i][j]!='X' && s[i][j]!=' ') + { + f=false; + c=s[i][j]; + dfs(i,j); + } + } + } + for0(i,n+1) + CP(s[i]); + n=-1; + } + return 0; +} + diff --git a/uva_cpp_clean/785/785-6.cc b/uva_cpp_clean/785/785-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..276498b03fef1dbb317364f225bb0d6fe0fc2491 --- /dev/null +++ b/uva_cpp_clean/785/785-6.cc @@ -0,0 +1,36 @@ +// https://uva.onlinejudge.org/external/7/785.pdf +#include +using namespace std; +using vs=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + string s; + vs a; + for(;;){ + getline(cin,s); + if(cin.eof())return 0; + a.push_back(s); + if(s[0]=='_')break; + } + functiondfs=[&](int y,int x, int k){ + a[y][x]=k; + int r[]={-1,0,1,0}; + int c[]={0,1,0,-1}; + for(int i=0;i<4;i++){ + int u=y+r[i],v=x+c[i]; + if(u>=0&&u=0&&v + +#define SIZE 100 + +using namespace std; + +int N; +char grid[SIZE][SIZE], contour, _max; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; +bool vis[SIZE][SIZE]; +map < char, int > v; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and grid[R][C] and not vis[R][C] ); +} + +void floodfill(int r, int c, char ch){ + if( not is_possible(r, c) ) return; + if( grid[r][c] == contour ) return; + grid[r][c] = ch; + vis[r][c] = true; + for(int d = 0; d < 4; d++) + floodfill(r + dr[d], c + dc[d], ch); +} + +int count(char ch){ + if( v[ch] != 0 ) return v[ch]; + int ct = 0; + for(int row = 0; row < N; row++) + for(int col = 0; grid[row][col]; col++) + if( grid[row][col] == ch ) + ct++; + return v[ch] = ct; +} + +void findContourCharacter(){ + _max = 0; + for(int row = 0; row < N; row++) + for(int col = 0; grid[row][col]; col++) + if( grid[row][col] != ' ' and grid[row][col] != '_' and count(grid[row][col]) > _max ) + _max = count(grid[row][col]), contour = grid[row][col]; +} + +void solve(){ + v.clear(); + memset(vis, false, sizeof vis); + findContourCharacter(); + for(int row = 0; row < N; row++) + for(int col = 0; grid[row][col]; col++) + if( not vis[row][col] and grid[row][col] != ' ' and grid[row][col] != '_' and grid[row][col] != contour ) + floodfill(row, col, grid[row][col]); + for(int row = 0; row <= N; row++) + printf("%s\n", grid[row]); + N = 0; +} + +int main(){ + while(cin.getline(grid[N], SIZE)){ + if( grid[N][0] == '_' ) solve(); + else N++; + } + return(0); +} diff --git a/uva_cpp_clean/787/787-21.cpp b/uva_cpp_clean/787/787-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f59602f09c800428151d99c1571109057fdfc9b8 --- /dev/null +++ b/uva_cpp_clean/787/787-21.cpp @@ -0,0 +1,203 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 787 + Name: Maximum Sub-sequence Product + Problem: https://onlinejudge.org/external/7/787.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + +class BigInt { + typedef int CellType; + const static int CellSize = sizeof(CellType); + const static int MAX_C = 10000; + const static int MAX_D = 4; + CellType *cells; + int capacity, size, sign; + + int sgn(int x) { + return x<0 ? -1 : x>0 ? 1 : 0; + } + + int countDigits(int x) { + if (!x) return 0; + if (x < 0) x = -x; + return floor(log10(x)) + 1; + } + + void init() { + sign = size = 0; + cells = new CellType[capacity = 4]; + memset(cells, 0, 4*CellSize); + } + + int setSize(int sz, bool cp = true) { + if (sz > capacity) { + CellType* ncells = new CellType[capacity = sz + 16]; + if (cp && size) + memcpy(ncells, cells, size*CellSize); + else size = 0; + memset(ncells+size, 0, (capacity-size)*CellSize); + + if (cells) delete [] cells; + cells = ncells; + } + return size = sz; + } + + BigInt& autoSize() { + while (size>0 && !cells[size-1]) + size--; + if (!size) sign=0; + return *this; + } + + public: + BigInt() { init(); } + BigInt(int x) { + init(); + sign = sgn(x); x*=sign; + for (size=0; x; size++) { + cells[size] = x % MAX_C; + x /= MAX_C; + } + } + BigInt(const BigInt& o): cells(0), capacity(0) { + sign = o.sign; + setSize(o.size, 0); + for (int i=0; i= 0) + delete [] cells; + } + + + private: + bool numLess(const BigInt &o) const { + if (size != o.size) + return size < o.size; + for (int i=size-1; i>=0; i--) + if (cells[i] != o.cells[i]) + return cells[i] < o.cells[i]; + return false; + } + + public: + bool operator < (const BigInt &o) const { + return sign==o.sign ? (sign>0 ? numLess(o) : o.numLess(*this)) : sign < o.sign; + } + bool operator > (const BigInt &o) const { return (o < *this); } + + + + BigInt& operator=(const BigInt& o) { + if ((sign = o.sign)) { + setSize(o.size, 0); + memcpy(cells, o.cells, o.size*CellSize); + } + else size=0; + return *this; + } + + BigInt& operator*= (int x) { + sign *= sgn(x); + if (!sign) { + size=sign=0; + return *this; + } + if (x<0) x = -x; + + int cr = 0; + for (int i=0; i < size; i++) { + cells[i] = cells[i] * x + cr; + cr = cells[i] / MAX_C; + cells[i] %= MAX_C; + } + + if (cr) { + int ex = (countDigits(cr)+MAX_D-1)/MAX_D, sz=size; + setSize(size + ex); + size = sz; + for (; cr; cr /= MAX_C) + cells[size++] = cr % MAX_C; + } + + return autoSize(); + } + + + friend std::ostream& operator<< (std::ostream& stream, const BigInt& bs) { + if (!bs.sign) + stream << 0; + else { + if (bs.sign < 0) stream << '-'; + stream << bs.cells[bs.size-1]; + for (int i=bs.size-2; i>=0; i--) + stream << setfill('0') << setw(MAX_D) << bs.cells[i]; + } + return stream; + } +}; + + + +int main(){ + int n, X[200]; + while (cin>>X[0]) { + int imxm=X[0]; + for (n=1; cin>>X[n] && X[n]!=-999999; n++) + imxm = max(imxm, X[n]); + + BigInt mxm(imxm); + for (int i=0; i mxm) + mxm = m1; + + BigInt m2(X[fn+1]); + for (int j=fn+2; j mxm) + mxm = m2; + } + else { + BigInt m(X[s]); + for (int j=s+1; j mxm) + mxm = m; + } + } + + cout << mxm << endl; + } +} diff --git a/uva_cpp_clean/787/787-9.cpp b/uva_cpp_clean/787/787-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e186cf20c795f451c45f9e803b01978bde6fd117 --- /dev/null +++ b/uva_cpp_clean/787/787-9.cpp @@ -0,0 +1,18 @@ +from sys import stdin,stdout + +def main(): + while True: + v = list(map(int, stdin.readline().rstrip().split())) + if len(v) == 0: + break + ans = v[0] + for i in range(len(v) - 1): + ans = max(ans, v[i]) + p = v[i] + for j in range(i + 1, len(v) - 1): + p *= v[j] + ans = max(ans, p) + stdout.write(str(int(ans)) + "\n") + +if __name__ == "__main__": + main() diff --git a/uva_cpp_clean/790/790-21.cpp b/uva_cpp_clean/790/790-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40af3d3cfec067b2bd11c442bc1a36c200ecd82c --- /dev/null +++ b/uva_cpp_clean/790/790-21.cpp @@ -0,0 +1,95 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 790 + Name: Head Judge Headache + Problem: https://onlinejudge.org/external/7/790.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define MAXN 500000 +char prob[MAXN], stat[MAXN]; +int tid[MAXN], hr[MAXN], mn[MAXN], ord[MAXN]; +bool complog(int i, int j) { + int t1 = hr[i]*60+mn[i], + t2 = hr[j]*60+mn[j]; + return t1 != t2 ? t1 < t2 + : stat[i] < stat[j]; +} + +int ttime[30], solvn[30], tnum[30]; +bool comp(int i, int j) { + if (solvn[i] != solvn[j]) + return solvn[i] > solvn[j]; + + if (ttime[i] != ttime[j]) + return ttime[i] < ttime[j]; + + return i < j; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char line[1000]; + int T, n, tn; + cin >> T; + cin.getline(line, 1000); + cin.getline(line, 1000); + while (T--) { + for (n=tn=0; cin.getline(line, 1000) && line[0]; ++n) { + sscanf(line, "%d %c %d:%d %c", tid+n, prob+n, hr+n, mn+n, stat+n); + if (tid[n] > tn) tn = tid[n]; + ord[n] = n; + } + sort(ord, ord+n, complog); + + for (int i=0; i ttime[lti]) + lr = i+1; + + cout << setw(4) << lr + << setw(5) << (ti+1); + + if (solvn[ti]) + cout << setw(5) << solvn[ti] << " " + << setw(5) << ttime[ti]; + cout << endl; + + lti = ti; + } + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/790/790-3.cpp b/uva_cpp_clean/790/790-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e839584a3b7405d6f0de2c290869443e53fe2beb --- /dev/null +++ b/uva_cpp_clean/790/790-3.cpp @@ -0,0 +1,50 @@ +// Problem: UVa ID: 790 - Head Judge Headache + +#include //sort +#include //formatted input/output +#include //to store the teams + +using namespace std; + +struct team { + int number; + int problemsSolved; + int time; //in seconds; + int rank; +}; + +// problemsSolved -> time -> teamNumber; +bool sortFunc (team a, team b){ + // First checking whether the two teams have solved the same number of problems + if ( a.problemsSolved != b.problemsSolved ){ + if ( a.problemsSolved > b.problemsSolved ) return false; + else return true; + } + else { + // Now checking the second parameter, time + if ( a.time != b.time ){ + if ( a.time < b.time ) return false; + else return true; + } + else { + if ( a.number < b.number ) return false; + else return true; + } + + } + +} + + + +int main () { + vector teamVector; //LOL + int teamSoFar = 0; + int tempInt; // variable that will be used in many purposes (-_-) + while ( scanf("%d", &teamVector[l].number) != EOF ){ + + + + } +} + diff --git a/uva_cpp_clean/790/790-9.cpp b/uva_cpp_clean/790/790-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32eb5f7c1489188db2edc0fc87be0e3d9f93f1ba --- /dev/null +++ b/uva_cpp_clean/790/790-9.cpp @@ -0,0 +1,92 @@ +#include + +#define MAX_N 26 +#define MAX_P 26 +#define SIZE 20 + +using namespace std; + +struct Team{ + int problems, total_time, number; +}aux; + +struct In{ + int _n, _mm, _s, _l; +}tmp; + +int tc, n, h, mm, score[MAX_N][MAX_P], it, _max; +char l, s, line[SIZE]; +vector v; +vector u; +bool solved[MAX_N][MAX_P]; + +void fillData(){ + for(int i = 0;i < u.size(); i++){ + if(solved[u[i]._n][u[i]._l] == false){ + if(u[i]._s == 1) + solved[u[i]._n][u[i]._l] = true, score[u[i]._n][u[i]._l] += u[i]._mm; + else + score[u[i]._n][u[i]._l] += 20; + } + } +} + +void fillScores(){ + for(int i = 1;i <= _max; i++){ + aux.number = i; + int sum = 0,np = 0; + for(int j = 0;j < MAX_P; j++) + if(solved[i][j]) sum += score[i][j], np++; + aux.total_time = sum; + aux.problems = np; + v.push_back(aux); + } +} + +bool cmp1(In x, In y){ + if(x._mm != y._mm) return(x._mm < y._mm); + return (x._s < y._s); +} + +bool cmp2(Team x, Team y){ + if(x.problems != y.problems) return (x.problems > y.problems); + if(x.total_time != y.total_time) return (x.total_time < y.total_time); + return (x.number < y.number); +} + +inline bool equals(Team x, Team y){ + return (x.problems == y.problems && x.total_time == y.total_time); +} + +void printResults(){ + puts("RANK TEAM PRO/SOLVED TIME"); + for(int i = 0; i < v.size(); i++){ + if(i == 0 || !equals(v[i], v[i - 1])) it = i + 1; + if(v[i].problems) printf("%4d %4d %4d %4d\n", it, v[i].number, v[i].problems, v[i].total_time); + else printf("%4d %4d\n", it, v[i].number); + } + v.clear(), u.clear(); + memset(score, 0, sizeof score); + memset(solved, false, sizeof solved); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + _max = -1; + while(cin.getline(line, SIZE)){ + if(sscanf(line,"%d %c %d:%d %c", &n, &l, &h, &mm, &s) == -1) break; + _max = max(_max, n); + tmp._n = n, tmp._l = l - 'A', tmp._mm = h * 60 + mm; + tmp._s = (s == 'Y') ? 1 : 0; + u.push_back(tmp); + } + sort(u.begin(), u.end(), cmp1); + fillData(); + fillScores(); + sort(v.begin(), v.end(), cmp2); + printResults(); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/793/793-14.cpp b/uva_cpp_clean/793/793-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6a643d2e2f82803ebaa1bf037dd770c4264d9ee --- /dev/null +++ b/uva_cpp_clean/793/793-14.cpp @@ -0,0 +1,159 @@ +/*************************************************** + * Problem Name : 793 Network Connections.cpp + * Problem Link : https://onlinejudge.org/external/7/793.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-10-29 + * Problem Type : Data Structrue(DSU) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; +typedef long long ll; +typedef unsigned long long ull; + +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin) +#define __FileWrite freopen ("output.txt", "w", stdout) +#define SET(a,v) memset(a,v,sizeof(a)) +#define SZ(v) (int)v.size() +#define pii pair +#define pil pair +#define pli pair +#define pll pair +#define debug cout <<"######\n" +#define debug1(x) cout <<"### " << x << " ###\n" +#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n" +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define rall(v) v.begin(), v.end() +#define srt(v) sort(v.begin(), v.end()) +#define r_srt(v) sort(v.rbegin(), v.rend()) +#define rev(v) reverse(v.begin(), v.end()) +#define Sqr(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define max3(a, b, c) max(a, max(b, c)) +#define min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 1005 +#define INF 1000000009 +#define MOD 1000000007 + +template +using min_heap = priority_queue, std::greater>; +template +using ordered_set = tree, rb_tree_tag, + tree_order_statistics_node_update>; + +template string toString ( T Number ) { + stringstream ss; + ss << Number; + return ss.str(); +} +template int toInt (T str) { + stringstream ss; + ss << str; + int num; + ss >> num; + return num; +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +int par[MAX]; +int sz[MAX]; +int find(int r){ + if(par[r] == r)return r; + else return par[r] = find(par[r]); +} + +void make_set(int x, int y){ + int u = find(x); + int v = find(y); + if(u!=v){ + if(sz[u]> tc; + while(tc--){ + cin >> n; + for(int i = 1; i<=n; i++){ + par[i] = i; + sz[i] = 1; + } + cin.ignore(); + int sf = 0, u_sf = 0; + string line; + while(getline(cin, line)){ + if(line[0] == '\0') break; + char ch; + int x, y; + string a,b,c; + stringstream ss; + ss << line; + + ss >> a; + ss >> b; + ss >> c; + + ch = a[0]; + + x = toInt(b); + y = toInt(c); + + if(ch == 'c'){ + make_set(x,y); + }else{ + if(find(x) != find(y))u_sf++; + else sf++; + } + } + cout << sf << "," << u_sf << "\n"; + if(tc!=0) cout << "\n"; + } + return 0; +} diff --git a/uva_cpp_clean/793/793-15.cpp b/uva_cpp_clean/793/793-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47f5120675460d357e3e3d08cbdb54bc3a0c1214 --- /dev/null +++ b/uva_cpp_clean/793/793-15.cpp @@ -0,0 +1,72 @@ +/* + 並查集 + 時間複雜度(每個操作最糟): O(logN) + 時間複雜度(平均): O(1) +*/ +#include +#include +#include +#include + +using namespace std; + +int get_parent(const int& vertex, vector& parents) { + if (parents[vertex] == vertex) + return vertex; + parents[vertex] = get_parent(parents[vertex], parents); // 路徑壓縮 + return parents[vertex]; +} + +bool same(const int& x, const int& y, vector& parents) { + return get_parent(x, parents) == get_parent(y, parents); // 有共同祖先就是同一個集合 +} + +void rank_union(const int& x, const int& y, vector& rank, vector& parents) { + int x_parent = get_parent(x, parents), y_parent = get_parent(y, parents); + if (rank[x_parent] > rank[y_parent]) + parents[y_parent] = x_parent; // 將小集合合併到大集合 + else { + parents[x_parent] = y_parent; // 將小集合合併到大集合 + if (rank[x_parent] == rank[y_parent]) // 集合大小相同就合併之後 rank + 1 + rank[y_parent]++; + } +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, n_case, n_computer, computer_1, computer_2, correct, wrong; + char operation; + string line; + + cin >> n_case; + while (n_case--) { + cin >> n_computer; + cin.ignore(1024, '\n'); + + n_computer++; + correct = wrong = 0; + vector rank(n_computer, 0), parents; + // 一開始每台電腦都自己一個集合 + parents.reserve(n_computer); + for (i = 0; i < n_computer; i++) + parents.push_back(i); + + while (getline(cin, line) && line.length()) { + istringstream input(line); + input >> operation >> computer_1 >> computer_2; + + if (operation == 'c') // 合併 + rank_union(computer_1, computer_2, rank, parents); + else if (same(computer_1, computer_2, parents)) + correct++; + else + wrong++; + } + + cout << correct << "," << wrong << "\n"; + if (n_case) + cout << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/793/793-18.cpp b/uva_cpp_clean/793/793-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..61e434e92726017caad5d81895ad712605257800 --- /dev/null +++ b/uva_cpp_clean/793/793-18.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +int parent[900]; + +void makeSet(int n){ + for(int i = 0; i < n; i++) parent[i] = i; +} + +int find(int i){ + if(parent[i] == i) return i; + return find(parent[i]); +} + +bool sameSet(int i, int j){ + return find(i) == find(j); +} + +void unionSet(int i, int j){ + int iRoot = find(i), jRoot = find(j); + parent[iRoot] = jRoot; +} + +int main(){ + int tc, n, u, v, yes, no; + char request; + scanf("%d", &tc); + while(tc--){ + scanf("%d", &n); + getchar(); + yes = no = 0; + makeSet(n); + while((request = getchar()) && isalpha(request)){ + scanf("%d %d", &u, &v); + getchar(); + if(request == 'c') unionSet(u-1, v-1); + else { + if(sameSet(u-1, v-1)) yes++; + else no++; + } + } + printf("%d,%d\n", yes, no); + if(tc) printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/793/793-19.cpp b/uva_cpp_clean/793/793-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5d343b75e1760217b4e903bd90637f2a995cb98 --- /dev/null +++ b/uva_cpp_clean/793/793-19.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +typedef vector vi; + +struct ufds { + vi p, r; int numSets; + ufds(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +int main() { + int N; + cin >> N; + int s; + bool first = true; + while (N--) { + ufds* net; + char t; + int n1, n2, suc = 0, unsuc = 0; + if (first) cin >> s, first = false; + net = new ufds(s); + string aux; + while (cin >> aux) { + if (aux.length() != 1 || (aux[0]-'0' >= 0 && aux[0]-'0' <= 9)) { + s = atoi(aux.c_str()); + break; + } + cin >> n1 >> n2; + if (aux[0] == 'c') { + net->unionSet(n1-1, n2-1); + } else if (aux[0] == 'q'){ + if (net->isSameSet(n1-1, n2-1)) suc++; + else unsuc++; + } + } + printf("%d,%d\n", suc, unsuc); + if (N) printf("\n"); + delete net; + } + return 0; +} diff --git a/uva_cpp_clean/793/793-21.cpp b/uva_cpp_clean/793/793-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88e7860e991e5a08af67118d860c5a76f109c562 --- /dev/null +++ b/uva_cpp_clean/793/793-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 793 + Name: Network Connections + Problem: https://onlinejudge.org/external/7/793.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define MAXN 10000000 + +int ids[MAXN]; +int find(int i) { + if (ids[i] == i) return i; + return ids[i] = find(ids[i]); +} + +void connect(int x, int y) { + ids[find(x)] = find(y); +} + +int main(){ + char line[100], tp; + int T, n, x, y; + cin>>T; + while (T--) { + cin>>n; cin.getline(line, 100); + for (int i=0; i<=n+1; i++) + ids[i] = i; + + int succ=0, fail=0; + while (cin.getline(line, 100) && sscanf(line, "%c %d %d", &tp, &x, &y) == 3) + if (tp=='q') + if (find(x) == find(y)) + succ++; + else + fail++; + else connect(x, y); + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforab(i,(n)-1,0) +#define rfor1(i,n) rforab(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//File input/output +#define input freopen("in.txt","r",stdin) +#define output freopen("out.txt","w",stdout) + +//upper bound and lower bound +#define LB(a,value) (lower_bound(all(a),value)-a.begin()) +#define UB(a,value) (upper_bound(all(a),value)-a.begin()) + +//Test Case & New line +#define Case(no) printf("Case %d: ",++no) +#define nl puts("") + +int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int par[1000005]; + +int Find(int n) +{ + if(par[n]==n) + return n; + return par[n]=Find(par[n]); +} + +void Union(int a,int b) +{ + int u= Find(a); + int v= Find(b); + if(u!=v) + { + par[u]=v; + } +} + +int main() +{ + int t; + si(t); + while(t--) + { + int n,a,b; + si(n); + + getchar(); + + for1(i,n) + par[i]=i; + + string s; + int cnt1=0,cnt2=0; + while(getline(cin,s)) + { + if(s.length()==0) break; + + stringstream ss; + ss<>s>>a>>b; + + if(s=="c") + { + Union(a,b); + } + else + { + int u= Find(a); + int v= Find(b); + if(u==v) + cnt1++; + else + cnt2++; + } + + } + + printf("%d,%d\n",cnt1,cnt2); + + if(t) + nl; + + + } + + return 0; +} diff --git a/uva_cpp_clean/793/793-6.cc b/uva_cpp_clean/793/793-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..ea603d7ef910d06139e2b9f17466eae8b134e244 --- /dev/null +++ b/uva_cpp_clean/793/793-6.cc @@ -0,0 +1,41 @@ +// https://uva.onlinejudge.org/external/7/793.pdf +#include +using namespace std; +using vi=vector; +int main(){ + int t,n,u,v; + cin>>t; + while(t--){ + cin>>n; + string l; + getline(cin,l); + vi s=vi(n, 1),p=vi(n); + for(int i=0;ifind=[&](int i){ + if(i==p[i])return i; + return p[i]=find(p[i]); + }; + functionunite=[&](int i, int j){ + i=find(i); + j=find(j); + if(i==j)return; + if(s[i]>q>>u>>v; + u--;v--; + if(q=='c')unite(u,v); + else if(find(u)==find(v))a++; + else b++; + } + cout< + +using namespace std; + +struct UnionFind{ + int numSets; + vector path, rank, setSize; + + UnionFind(int N_): numSets(N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + setSize.assign(N_, 1); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + numSets--; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x, setSize[x] += setSize[y]; + else path[x] = y, setSize[y] += setSize[x]; + if( rank[x] == rank[y] ) rank[y]++; + } + + int sizeOfSet(int idx){ + return setSize[ findSet(idx) ]; + } +}; + +const int LIM = 20; +int tc, N, a, b, ans1, ans2; +char line[LIM], ch; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d\n", &N); + ans1 = ans2 = 0; + UnionFind UF(N); + while(true){ + cin.getline(line, LIM); + if( sscanf(line, "%c %d %d", &ch, &a, &b) == -1 ) break; + if( ch == 'c' ) UF.unionSet(a - 1, b - 1); + else{ + if( UF.isSameSet(a - 1, b - 1) ) ans1++; + else ans2++; + } + } + printf("%d,%d\n", ans1, ans2); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/795/795-9.cpp b/uva_cpp_clean/795/795-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..425053df9131e35c7ceb402ab8d0c123669e560f --- /dev/null +++ b/uva_cpp_clean/795/795-9.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +const int SIZE = 6; + +int x, y; +char grid[SIZE][SIZE]; +string line, _line, ans; +vector < pair > pos = { {0, 1}, {0, 3}, {0, 5}, + {1, 4}, + {2, 2}, + {3, 1}, {3, 4}, + {4, 5}, + {5, 3} }; + +void rotate() { + for (int i = 0; i < 9; i++) { + x = pos[i].first, y = pos[i].second; + pos[i].first = y, pos[i].second = 5 - x; + } +} + +void process() { + int pib = 0; + ans = ""; + while (pib < line.size()) { + _line = ""; + for (int i = 0; i < 36; i++) + grid[i / 6][i % 6] = line[pib + i]; + for (int r = 0; r < 4; r++) { + for(int j = 0; j < 9; j++) + _line += grid[pos[j].first][pos[j].second]; + rotate(); + sort(pos.begin(), pos.end()); + } + reverse(_line.begin(), _line.end()); + ans.insert(ans.begin(), _line.begin(), _line.end()); + pib += 36; + } + while (ans.back() == '#') ans.pop_back(); + printf("%s\n", ans.c_str()); +} + +int main() { + while (getline(cin, line)) process(); + return(0); +} \ No newline at end of file diff --git a/uva_cpp_clean/796/796-13.cpp b/uva_cpp_clean/796/796-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43df5118d471747b214e4b165b3004dfcbd44144 --- /dev/null +++ b/uva_cpp_clean/796/796-13.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int n, dfsCount = 0; + +vector visited; +set < pair > ans; +vector > graph; +vector num, low, parent; + +void find(int source) +{ + visited[source] = true; + + num[source] = low[source] = dfsCount++; + + for (int i = 0; i < (int)graph[source].size(); i++) + { + int child = graph[source][i]; + + if (!visited[child]) + { + parent[child] = source; + + find(child); + + low[source] = min(low[source], low[child]); + + if (low[child] > num[source]) + { + ans.insert(make_pair(min(source, child), max(source, child))); + } + } + + else if (child != parent[source]) + { + low[source] = min(low[source], num[child]); + } + } +} + +int main() +{ + while (cin >> n) + { + dfsCount = 0; + + ans.clear(); + + graph.clear(); + graph.resize(n); + + visited.clear(); + visited.resize(n, 0); + + num.clear(); + num.resize(n, 0); + + low.clear(); + low.resize(n, 0); + + parent.clear(); + parent.resize(n, -1); + + for (int i = 0; i < n; i++) + { + int x, s, y; + char c; + + cin >> x >> c >> s >> c; + + for (int i = 0; i < s; i++) + { + cin >> y; + + graph[x].push_back(y); + } + } + + for (int i = 0; i < n; i++) + { + if (!visited[i]) + { + find(i); + } + } + + cout << ans.size() << " critical links" << endl; + + set < pair > :: iterator i; + + for (i = ans.begin(); i != ans.end() ; i++) + { + pair answer = *i; + + cout << answer.first << " - " << answer.second << endl; + } + + cout << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/796/796-14.cpp b/uva_cpp_clean/796/796-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd8c2fc04e6cb57b2ff012391bba004ec6932212 --- /dev/null +++ b/uva_cpp_clean/796/796-14.cpp @@ -0,0 +1,155 @@ +/*************************************************** + * Problem Name : 796 - Critical Links.cpp + * Problem Link : https://uva.onlinejudge.org/external/7/796.pdf + * OJ : Uva + * Verdict : AC + * Date : 2018-08-09 + * Problem Type : Graph (Find Bridge) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectoradj[MAX]; +vectorans; +int n, t; +bool vis[MAX]; +mapmp; +int dis[MAX], low[MAX]; +void dfs (int u, int p) { + vis[u] = true; + low[u] = dis[u] = t++; + + for (int i = 0; i < (int) adj[u].size(); i++) { + int v = adj[u][i]; + + if (v == p) continue; + + if (vis[v] == true) { + low[u] = min (low[u], dis[v]); + + } else { + dfs (v, u); + low[u] = min (low[u], low[v]); + + if (dis[u] < low[v]) { + if (u > v) { + if (mp[pii (v, u)] == 0) { + ans.pb (pii (v, u) ); + mp[pii (v, u)] = 1; + } + + } else { + if (mp[pii (u, v)] == 0) { + ans.pb (pii (u, v) ); + mp[pii (u, v)] = 1; + } + } + } + } + } +} +void find_bridge() { + t = 0; + SET (vis, false); + SET (dis, -1); + SET (low, -1); + mp.clear(); + + for (int i = 0; i < n; i++) { + if (!vis[i]) dfs (i, -1); + } +} +int main () { + //~ __FastIO; + while (cin >> n) { + if (n == 0) { + cout << "0 critical links\n"; + nl; + continue; + } + + for (int i = 0; i < n; i++) { + int u; + string str; + cin >> u >> str; + int num = 0; + + for (int j = 1; j < (int) str.size() - 1; j++) { + num = (num * 10) + (str[j] - '0'); + } + + for (int j = 0; j < num; j++) { + int v; + cin >> v; + adj[u].pb (v); + adj[v].pb (u); + } + } + + find_bridge(); + cout << (int) ans.size() << " critical links\n"; + sort (all (ans) ); + + for (int i = 0; i < (int) ans.size(); i++) { + cout << ans[i].first << " - " << ans[i].second << "\n"; + } + + nl; + ans.clear(); + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + } + } + + return 0; +} diff --git a/uva_cpp_clean/796/796-21.cpp b/uva_cpp_clean/796/796-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5987e501d3d2c20bc44652f85c1c3195eb809611 --- /dev/null +++ b/uva_cpp_clean/796/796-21.cpp @@ -0,0 +1,72 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 796 + Name: Critical Links + Problem: https://onlinejudge.org/external/7/796.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + + +#define INF 1073741824 +#define MAXN 1000 +list adj[MAXN]; +int low[MAXN], depth[MAXN], parent[MAXN], visited[MAXN]; + +#define Pair pair +vector arts; + +void findAPs(int u, int d) { + visited[u] = 1; + low[u] = depth[u] = d; + + for (int v: adj[u]) + if (!visited[v]) { + parent[v] = u; + findAPs(v, d+1); + if (low[v] >= d+1) + arts.push_back(Pair(min(u, v), max(u, v))); + else + low[u] = min(low[u], low[v]); + } + else if (v != parent[u]) + low[u] = min(low[u], depth[v]); +} + +int main(){ + int n; + while (cin>>n) { + for (int i=0; i>v; + adj[u].push_back(v); + } + } + + arts.clear(); + for (int i=0; i +using namespace std; +using ii=tuple; +using vi=vector; +using vii=vector; +using vvi=vector; +using vvii=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int n,m,u,v; + while(cin>>n){ + string s; + getline(cin,s); + vvi g(n); + for(;;){ + getline(cin,s); + if(s.empty())break; + stringstream in(s); + string r; + in>>u>>r; + while(in>>v){ + g[u].push_back(v); + g[v].push_back(u); + } + } + vi a(n),b(n); + vii e; + int k=1; + functiondfs=[&](int u,int p){ + a[u]=k++; + b[u]=a[u]; + for(int v:g[u]) + if(!a[v]){ + dfs(v,u); + b[u]=min(b[u],b[v]); + if(b[v]>a[u])e.emplace_back(min(u,v),max(u,v)); + }else if(v!=p)b[u]=min(b[u],a[v]); + }; + for(int i=0;i traversal > bridges or articulation points + difficulty: easy + date: 04/Feb/2020 + by: @brpapa +*/ +#include +#include +#include +using namespace std; + +int V; +vector> adjList; + +vector num, low, parent; + +int countNum; +vector> bridgeEdges; + +void dfs(int v) { + low[v] = num[v] = countNum++; + + for (int u : adjList[v]) { + if (num[u] == -1) { + parent[u] = v; + dfs(u); + + low[v] = min(low[v], low[u]); + if (num[v] < low[u]) + bridgeEdges.push_back(pair(min(v, u), max(v, u))); + } + else if (u != parent[v]) // back edge not direct + low[v] = min(low[v], low[u]); + } +} + +int main() { + while (cin >> V) { + countNum = 0; adjList.assign(V, vector()); bridgeEdges.clear(); + num.assign(V, -1); low.assign(V, -1); parent.assign(V, -1); + + for (int i = 0; i < V; i++) { + int v, e; scanf("%d (%d) ", &v, &e); + while (e--) { + int u; cin >> u; + adjList[v].push_back(u); + } + } + + for (int v = 0; v < V; v++) + if (num[v] == -1) + dfs(v); + + sort(bridgeEdges.begin(), bridgeEdges.end()); + printf("%d critical links\n", bridgeEdges.size()); + for (auto edge : bridgeEdges) + printf("%d - %d\n", edge.first, edge.second); + cout << endl; + } + + return 0; +} diff --git a/uva_cpp_clean/796/796-9.cpp b/uva_cpp_clean/796/796-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc4e063b1f6c9d837ff16b989eda030a47234f29 --- /dev/null +++ b/uva_cpp_clean/796/796-9.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +const int MAX_N = 1010; + +int V, Ei, a, b, dfsCounter, dfs_low[MAX_N], dfs_num[MAX_N], dfs_parent[MAX_N]; +vector AdjList[MAX_N]; +vector < pair < int, int > > bridge; + +void dfs(int u){ + dfs_low[u] = dfs_num[u] = ++dfsCounter; + for(int j = 0; j < AdjList[u].size(); j++){ + int v = AdjList[u][j]; + if( not dfs_num[v] ){ + dfs_parent[v] = u; + dfs(v); + if( dfs_low[v] > dfs_num[u] ) bridge.push_back( make_pair(min(u, v), max(u, v)) ); + + dfs_low[u] = min(dfs_low[u], dfs_low[v]); + } + else if( v != dfs_parent[u] ) + dfs_low[u] = min(dfs_low[u], dfs_num[v]); + } +} + +void Bridges(){ + fill(dfs_low, dfs_low + V, 0); + fill(dfs_num, dfs_num + V, 0); + fill(dfs_parent, dfs_parent + V, -1); + dfsCounter = 0; + for(int u = 0; u < V; u++) + if( not dfs_num[u] ) + dfs(u); +} + +void solve(){ + Bridges(); + sort(bridge.begin(), bridge.end()); + printf("%d critical links\n", (int) bridge.size()); + for(int i = 0; i < bridge.size(); i++) + printf("%d - %d\n", bridge[i].first, bridge[i].second); + putchar('\n'); +} + +void read(){ + for(int i = 0; i < V; i++){ + scanf("%d (%d)", &a, &Ei); + for(int j = 0; j < Ei; j++){ + scanf("%d", &b); + AdjList[a].push_back(b); + } + } +} + +void clear(){ + bridge.clear(); + for(int u = 0; u < V; u++) AdjList[u].clear(); +} + +int main(){ + while(scanf("%d", &V) == 1){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/804/804-17.cpp b/uva_cpp_clean/804/804-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..014f1ba9ad9eae68f100e2cad2930abb80168b41 --- /dev/null +++ b/uva_cpp_clean/804/804-17.cpp @@ -0,0 +1,56 @@ +/** + * AOAPC II Exercise 6-7 Petri Net Simulation + * ICPC WF 1998 + * 53 CE; 55 AC; + */ +#include +using namespace std; +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int np, T = 1; + while (cin >> np && np) + { + vector nps(np); + for (int &i : nps) + cin >> i; + int nt; + cin >> nt; + vector> outs(nt); + vector> ins(nt); + for (int i = 0; i < nt; ++i) + { + int cur; + while (cin >> cur && cur) + if (cur < 0) + ++ins[i][-1 * cur - 1]; + else + outs[i].push_back(cur - 1); + } + int nf; + cin >> nf; + bool live = true; + int cnt = 0; + while (live && ++cnt <= nf) + { + live = false; + for (int t = 0; t < nt; ++t) + if (all_of(ins[t].begin(), ins[t].end(), [&](map::value_type i){ return nps[i.first] >= i.second; })) + { + for (auto &i : ins[t]) + nps[i.first] -= i.second; + for (int i : outs[t]) + nps[i] += 1; + live = true; + break; + } + } + cout << "Case " << T++ << ": " << (live ? "still live" : "dead") << " after " << cnt - 1 << " transitions\n" + << "Places with tokens:"; + for (int i = 0; i < np; ++i) + if (nps[i]) + cout << " " << i + 1 << " (" << nps[i] << ")"; + cout << "\n\n"; + } +} diff --git a/uva_cpp_clean/806/806-17.cpp b/uva_cpp_clean/806/806-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85bfc3d98a1f9cdc23324cfb558815ba31ef82d5 --- /dev/null +++ b/uva_cpp_clean/806/806-17.cpp @@ -0,0 +1,87 @@ +/** + * Spatial Structures + * ICPC WF 1998 + * AOAPC II Exercise 6-8 + */ +#include +using namespace std; +vector g; +set ans; +void trans(int r, int c, int len, int path, int dep) +{ + bool black = true; + for (int rr = r; black && rr < r + len; ++rr) + for (int cc = c; cc < c + len; ++cc) + if (g[rr][cc] != '1') + { + black = false; + break; + } + if (black) + ans.insert(path); + else if (len > 1) + { + int new_len = len / 2, new_dep = dep + 1; + trans(r, c, new_len, path + 1 * pow(5, dep), new_dep); + trans(r, c + new_len, new_len, path + 2 * pow(5, dep), new_dep); + trans(r + new_len, c, new_len, path + 3 * pow(5, dep), new_dep); + trans(r + new_len, c + new_len, new_len, path + 4 * pow(5, dep), new_dep); + } +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n, kase = 0; + while (cin >> n && n) + { + if (kase) + cout << "\n"; + cout << "Image " << ++kase << "\n"; + if (n > 0) + { + g = vector(n); + for (string &row : g) + cin >> row; + ans.clear(); + trans(0, 0, n, 0, 0); + int cnt = 0; + for (int i : ans) + { + if (cnt && cnt % 12 == 0) + cout << "\n"; + else if (cnt) + cout << " "; + cout << i; + ++cnt; + } + if (cnt) + cout << "\n"; + cout << "Total number of black nodes = " << cnt << "\n"; + } + else + { + n = -1 * n; + int node; + g = vector(n, string(n, '.')); + while (cin >> node && node != -1) + { + int len = n, r = 0, c = 0, dir; + while (dir = node % 5) + { + len /= 2; + if (dir == 2 || dir == 4) + c += len; + if (dir == 3 || dir == 4) + r += len; + node = (node - dir) / 5; + } + for (int rr = r; rr < r + len; ++rr) + for (int cc = c; cc < c + len; ++cc) + g[rr][cc] = '*'; + } + for (string &row : g) + cout << row << "\n"; + } + } +} diff --git a/uva_cpp_clean/814/814-17.cpp b/uva_cpp_clean/814/814-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8236524ebdc905318b45a9ea290ea55c89765ef --- /dev/null +++ b/uva_cpp_clean/814/814-17.cpp @@ -0,0 +1,73 @@ +/** + * AOAPC II Example 5-11 The Letter Carrier's Rounds + * ICPC WF 1999 + */ +#include +using namespace std; +string get_mta(string &ori) { + return ori.substr(ori.find('@') + 1); +} +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + set adds; + string s; + while ((cin >> s) && s[0] != '*') { + string mta; + int num; + cin >> mta >> num; + while (num--) { + string name; + cin >> name; + adds.insert(name + "@" + mta); + } + } + string s_add; + while ((cin >> s_add) && s_add[0] != '*') { + string s_mta = get_mta(s_add), r_add; + vector r_mtas; + set r_mtas_in; + map> r_adds; + set r_users; + while ((cin >> r_add) && r_add[0] != '*') + if (r_users.find(r_add) == r_users.end()) { + r_users.insert(r_add); + string cur_mta = get_mta(r_add); + if (r_mtas_in.find(cur_mta) == r_mtas_in.end()) { + r_mtas.push_back(cur_mta); + r_mtas_in.insert(cur_mta); + } + r_adds[cur_mta].push_back(r_add); + } + string data = "", temp; + getline(cin, temp); + while (getline(cin, temp) && temp[0] != '*') + data += " " + temp + "\n"; + for (string &r_mta : r_mtas) { + cout << "Connection between " << s_mta << " and " << r_mta << "\n" + << " HELO " << s_mta << "\n" + << " 250\n" + << " MAIL FROM:<" << s_add << ">\n" + << " 250\n"; + bool ok = false; + for (string &r_add : r_adds[r_mta]) { + cout << " RCPT TO:<" << r_add << ">\n" + << " "; + if (adds.find(r_add) != adds.end()) { + cout << "250\n"; + ok = true; + } + else + cout << "550\n"; + } + if (ok) + cout << " DATA\n" + << " 354\n" + << data + << " .\n" + << " 250\n"; + cout << " QUIT\n" + << " 221\n"; + } + } +} diff --git a/uva_cpp_clean/815/815-19.cpp b/uva_cpp_clean/815/815-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88f46b81bb994d04da3e129f8b7c8ec6e7cfc3b2 --- /dev/null +++ b/uva_cpp_clean/815/815-19.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +int v[910]; + +int main() { + int n, m, c = 1; + while (cin >> n >> m && !(n == 0 && m == 0)) { + for (int i = 0; i < n*m; i++) cin >> v[i]; + int maxVol; + cin >> maxVol; + sort(v, v + (n*m)); + int a = 0, vol = 0, i = 0; + double lev = v[0]; + for (; i < n*m - 1 && vol < maxVol; i++) { + a++; + if (a * 100 * (v[i+1] - v[i]) + vol > maxVol) { + lev = v[i] + ((maxVol - vol) / (a*100.0)); + vol = maxVol; + } + else { + vol += a * 100 * (v[i+1] - v[i]); + } + } + if (vol < maxVol) a++, lev = v[i] + ((maxVol - vol) / (a*100.0)); + printf("Region %d\n", c++); + printf("Water level is %.2lf meters.\n", lev); + printf("%.2lf percent of the region is under water.\n\n", 100*a/double(n*m)); + } + return 0; +} diff --git a/uva_cpp_clean/816/816-17.cpp b/uva_cpp_clean/816/816-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..250158b679986453f888443903772590a5ea007e --- /dev/null +++ b/uva_cpp_clean/816/816-17.cpp @@ -0,0 +1,110 @@ +/** + * AOAPC II Example 6-14 Abbott's Revenge + * ICPC WF 2000 + * Array based + */ +#include +using namespace std; +struct Dir +{ + int r, c, dir; + Dir(int r = 0, int c = 0, int dir = 0) : r(r), c(c), dir(dir) { } +}; +Dir walk(Dir &ori, int turn) +{ + static const vector dr { -1, 0, 1, 0 }, + dc { 0, 1, 0, -1 }; + int dir = ori.dir; + if (turn == 1) + dir = (dir + 3) % 4; + else if (turn == 2) + dir = (dir + 1) % 4; + return Dir(ori.r + dr[dir], ori.c + dc[dir], dir); +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + const string dirs = "NESW", turns = "FLR"; + string name; + while ((cin >> name) && name != "END") + { + auto oks = vector>>>(10, vector>>(10, vector>(4, vector(3, false)))); + int entr_r, entr_c, goal_r, goal_c; + char entr_dir_char; + cin >> entr_r >> entr_c >> entr_dir_char >> goal_r >> goal_c; + int pos_r, pos_c, entr_dir = dirs.find(entr_dir_char); + while ((cin >> pos_r) && pos_r) + { + cin >> pos_c; + string sign; + while ((cin >> sign) && sign != "*") + { + auto &pos = oks[pos_r][pos_c][dirs.find(sign.front())]; + for (char c : sign.substr(1)) + pos[turns.find(c)] = true; + } + } + Dir beg_pos = Dir(entr_r, entr_c, entr_dir); + Dir pos1 = walk(beg_pos, 0); + auto p = vector>>(10, vector>(10, vector(4))); + // p[pos1.r][pos1.c][pos1.dir] = beg_pos; + auto vis = vector>>(10, vector>(10, vector(4, false))); + vis[pos1.r][pos1.c][pos1.dir] = true; + queue q; + q.push(pos1); + bool found = false; + Dir fin; + if (pos1.r == goal_r && pos1.c == goal_c) + { + found = true; + fin = pos1; + } + while (!q.empty() && !found) + { + Dir cur = q.front(); + q.pop(); + auto &cur_pos = oks[cur.r][cur.c][cur.dir]; + for (int turn = 0; turn < 3; ++turn) + if (cur_pos[turn]) + { + Dir res = walk(cur, turn); + if (1 <= res.r && res.r <= 9 && 1 <= res.c && res.c <= 9 && !vis[res.r][res.c][res.dir]) + { + p[res.r][res.c][res.dir] = cur; + if (res.r == goal_r && res.c == goal_c) + { + fin = res; + found = true; + break; + } + vis[res.r][res.c][res.dir] = true; + q.push(res); + } + } + } + cout << name; + if (!found) + { + cout << "\n No Solution Possible\n"; + continue; + } + vector stk; + while (true) + { + stk.push_back(fin); + if (fin.r == pos1.r && fin.c == pos1.c && fin.dir == pos1.dir) + break; + fin = p[fin.r][fin.c][fin.dir]; + } + stk.push_back(beg_pos); + int cnt = 0; + for (int i = stk.size() - 1; i >= 0; --i) + { + if (cnt++ % 10 == 0) + cout << "\n "; + cout << " (" << stk[i].r << "," << stk[i].c << ")"; + } + cout << "\n"; + } +} diff --git a/uva_cpp_clean/820/820-19.cpp b/uva_cpp_clean/820/820-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fed6ea044a0e70d196f4a5fca16963022e0d13f4 --- /dev/null +++ b/uva_cpp_clean/820/820-19.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +using namespace std; + +typedef vector vi; + +int N, M, mf, f, s, t, INF = 2000000; +int res[100][100]; +vi p; + +void augment(int v, int minEdge) { + if (v == s) { + f = minEdge; + return; + } else if (p[v] != -1) { + augment(p[v], min(minEdge, res[p[v]][v])); + res[p[v]][v] -= f; res[v][p[v]] += f; + } +} + +int main() { + int cases = 1; + while (cin >> N && N) { + int c; + cin >> s >> t >> c; + s--, t--; + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + res[i][j] = 0; + for (int i = 0; i < c; i++) { + int n1, n2, b; + cin >> n1 >> n2 >> b; + res[n1-1][n2-1] += b, res[n2-1][n1-1] += b; + } + + mf = 0; + while (true) { + f = 0; + vi dist(N, INF); dist[s] = 0; + queue q; + q.push(s); + p.assign(N, -1); + while (!q.empty()) { + int u = q.front(); q.pop(); + if (u == t) break; + for (int v = 0; v < N; v++) { + if (res[u][v] > 0 && dist[v] == INF) { + dist[v] = dist[u] + 1, q.push(v), p[v] = u; + } + } + } + augment(t, INF); + if (f == 0) break; + mf += f; + } + printf("Network %d\nThe bandwidth is %d.\n\n", cases, mf); + cases++; + } + return 0; +} diff --git a/uva_cpp_clean/820/820-6.cpp b/uva_cpp_clean/820/820-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbd513ff59946d088177052b2ee627cf1f31cd58 --- /dev/null +++ b/uva_cpp_clean/820/820-6.cpp @@ -0,0 +1,113 @@ +/* + graphs > maximum flow + difficulty: easy + date: 06/Aug/2020 + by: @brpapa +*/ +#include +using namespace std; + +vector>> adj_list; // adj_list[u] = {{v, w}, ...} +int V; + +vector>> remaining; // remaining[u] = {{v, current remaining capacity}, ...} +vector>> flow; // flow[u] = {{v, current flow}, ...} +vector> bfs_parent; // bfs_parent[u] = {pai de u, índice em remaining[u]} +vector> rev; + +// O(V+E) - find an augmenting path and returns your bottleneck flow, or 0, if there is no more an augmenting path +int bfs(int s, int t) { + vector seen(V, false); + bfs_parent.assign(V, {-1, -1}); + + queue q; q.push(s); + + while (!q.empty()) { + int u = q.front(); q.pop(); + seen[u] = true; + + if (u == t) break; + + // para cada aresta u -w-> v + for (int i = 0; i < remaining[u].size(); i++) { + int v = remaining[u][i].first; + int w = remaining[u][i].second; + + if (!seen[v] && w > 0) { + bfs_parent[v] = {u,i}; + q.push(v); + } + } + } + + // t não foi alcançado + if (bfs_parent[t].first == -1) return 0; + + // procura pelo o gargalo, iterando cada aresta u -> v do caminho encontrado + int bottleneck = INT_MAX; + for (int v = t, u; bfs_parent[v].first != -1; v = u) { + u = bfs_parent[v].first; + int i = bfs_parent[v].second; + bottleneck = min(bottleneck, remaining[u][i].second); + } + return bottleneck; +} + +/* O(V*E*E) - returns the max flow */ +int edmonds_karp(int s, int t) { + remaining.assign(V, vector>()); + flow.assign(V, vector>()); + rev.assign(V, vector()); + + // para cada aresta u -w-> v do grafo original + for (int u = 0; u < V; u++) + for (auto adj : adj_list[u]) { + int v = adj.first, w = adj.second; + + rev[u].push_back(remaining[v].size()); + rev[v].push_back(remaining[u].size()); + + remaining[u].push_back({v, w}); // forward edge + remaining[v].push_back({u, 0}); // backward edge + flow[u].push_back({v, 0}); // forward edge + flow[v].push_back({u, 0}); // backward edge + } + + int acc_flow = 0, new_flow; + + // enquanto existir augmenting path + while ((new_flow = bfs(s, t))) { + acc_flow += new_flow; + + // atualiza os grafos com o bottleneck, para cada aresta u -> v do caminho encontrado pela bfs + for (int v = t, u; bfs_parent[v].first != -1; v = u) { + u = bfs_parent[v].first; + int i = bfs_parent[v].second; + int j = rev[u][i]; + + remaining[u][i].second -= new_flow; // weight of u -> v + remaining[v][j].second += new_flow; // weight of v -> u + + flow[u][i].second += new_flow; // weight of u -> v + flow[v][j].second -= new_flow; // weight of v -> u + } + } + return acc_flow; +} + +int main() { + int count = 1; + while (cin >> V && V) { + adj_list.assign(V, vector>()); + int s, t; cin >> s >> t; s--; t--; + int E; cin >> E; + while (E--) { + int u, v, w; cin >> u >> v >> w; u--; v--; + adj_list[u].push_back({v, w}); + adj_list[v].push_back({u, w}); + } + printf("Network %d\n", count++); + printf("The bandwidth is %d.\n\n", edmonds_karp(s, t)); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/821/821-13.cpp b/uva_cpp_clean/821/821-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a632119019538872c32b72a13dd065aabbdeeb34 --- /dev/null +++ b/uva_cpp_clean/821/821-13.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 105 +#define INF 1e7 + 8 + +int web[MAX][MAX]; + +void initialize() +{ + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + web[i][j] = (i == j) ? 0 : INF; + } + } +} + +int main() +{ + int x, y, t = 1; + + while (cin >> x >> y, x != 0 || y != 0) + { + initialize(); + + web[x][y] = 1; + + while (cin >> x >> y, x != 0 || y != 0) web[x][y] = 1; + + for (int k = 0; k < MAX; k++) + { + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + web[i][j] = min(web[i][j], web[i][k] + web[k][j]); + } + } + } + + int sum = 0; + double how = 0.0; + + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + if (web[i][j] != 0 && web[i][j] != INF) + { + how = how + 1.0; + + sum += web[i][j]; + } + } + } + + printf("Case %d: average length between pages = %.3f clicks\n", t++, sum / how); + } +} \ No newline at end of file diff --git a/uva_cpp_clean/821/821-15.cpp b/uva_cpp_clean/821/821-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d2f8ef64d195a1f15078c304402e39b70427cda --- /dev/null +++ b/uva_cpp_clean/821/821-15.cpp @@ -0,0 +1,44 @@ +/* + 所有最短路徑平均長度(Floyd-Warshall) + 時間複雜度: O(N^3) +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int from, to, n, i, j, k, sum, n_case = 1; + while (cin >> from >> to && from) { + sum = 0; + vector> distance(101, vector(101, 0x3fffffff)); + n = max(from, to); + distance[from][to] = 1; + + while (cin >> from >> to && from) { + distance[from][to] = 1; + n = max(n, max(from, to)); + } + + n++; + + for (k = 1; k < n; k++) + for (i = 1; i < n; i++) + for (j = 1; j < n; j++) + if (i != j) + distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]); + n = 0; + for (const vector& row : distance) + for (const int& dist : row) + if (dist != 0x3fffffff) { + sum += dist; + n++; + } + cout << "Case " << n_case++ << ": average length between pages = " << setprecision(3) << fixed << double(sum) / n << " clicks\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/821/821-19.cpp b/uva_cpp_clean/821/821-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32c5bf04499a16a9f02a0fd16948b26aa249f06e --- /dev/null +++ b/uva_cpp_clean/821/821-19.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +using namespace std; + +int grid[100][100], ind; +unordered_map m; + +void reset(int i) { + for (int j = 0; j <= i; j++) { + grid[i][j] = grid[j][i] = 1000000; + } +} + +void floyd() { + for (int k = 0; k < ind; k++) + for (int i = 0; i < ind; i++) + for (int j = 0; j < ind; j++) + if (grid[i][k] + grid[k][j] < grid[i][j]) { + grid[i][j] = grid[i][k] + grid[k][j]; + } +} + +int main() { + int i, j, cases = 1; + while (scanf("%d %d", &i, &j) && !(i == 0 && j == 0)) { + m.clear(); + ind = 0; + do { + if (!m.count(i)) m[i] = ind, reset(ind), ind++; + if (!m.count(j)) m[j] = ind, reset(ind), ind++; + grid[m[i]][m[j]] = 1; + } while (scanf("%d %d", &i, &j) && !(i == 0 && j == 0)); + floyd(); + double sum = 0; + for (int i = 0; i < ind; i++) { + for (int j = 0; j < ind; j++) { + if (i != j) sum += grid[i][j]; + } + } + sum /= (ind - 1)*ind; + printf("Case %d: average length between pages = %.3lf clicks\n", cases++, sum); + } + return 0; +} diff --git a/uva_cpp_clean/821/821-21.cpp b/uva_cpp_clean/821/821-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1093ec9f0d01625964e766ff32db206c7c1b969 --- /dev/null +++ b/uva_cpp_clean/821/821-21.cpp @@ -0,0 +1,61 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 821 + Name: Page Hopping + Problem: https://onlinejudge.org/external/8/821.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +list adj[110]; +bool h[110]; +int d[110]; + +int main(){ + for (int cse=1; ;cse++) { + int u, v, n=0; + memset(h, 0, sizeof(h)); + while (cin>>u>>v && (u || v)) { + n = max(n, max(u, v)); + if (!h[u-1]) { + adj[u-1].clear(); + h[u-1]=1; + } + if (!h[v-1]) { + adj[v-1].clear(); + h[v-1]=1; + } + adj[u-1].push_back(v-1); + } + if (!n) break; + + int sum=0, cnt=0; + for (int i=0; i q; + q.push_back(i); + d[i] = 0; + while (!q.empty()) { + int u = q.front(); q.pop_front(); + for (int v: adj[u]) + if (d[v] < 0) { + d[v] = d[u] + 1; + sum += d[v]; + q.push_back(v); + } + } + } + printf("Case %d: average length between pages = %.3f clicks\n", cse, sum/(cnt*(cnt-1.0))); + } +} diff --git a/uva_cpp_clean/821/821-4.cpp b/uva_cpp_clean/821/821-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0eae08dd6eab5e21c9a5c9ea09758835d8633d4 --- /dev/null +++ b/uva_cpp_clean/821/821-4.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include + +int main() +{ + int from, to, caseNr = 0; + + while (std::cin >> from >> to && from != 0 && to != 0) + { + int index = 0; + std::unordered_map rules; + std::unordered_map> edges; + + do + { + if (rules.find(from) == rules.end()) + { + rules[from] = index++; + } + + if (rules.find(to) == rules.end()) + { + rules[to] = index++; + } + + edges[rules[from]].insert(rules[to]); + + } while (std::cin >> from >> to && from != 0 && to != 0); + + int n = rules.size(); + int *tab = new int[n * n]; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + { + tab[i * n + j] = 0; + } + else if (edges[i].find(j) != edges[i].end()) + { + tab[i * n + j] = 1; + } + else + { + tab[i * n + j] = 1000; + } + } + } + + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + tab[i * n + j] = std::min(tab[i * n + j], tab[i * n + k] + tab[k * n + j]); + } + } + } + + int sum = 0; + + for (int i = 0; i < n * n; i++) + { + sum += tab[i]; + } + + std::cout << "Case " << ++caseNr << ": average length between pages = " << std::fixed << std::setprecision(3) << sum / (n * ((double)n - 1)) << " clicks" << std::endl; + + delete[] tab; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/822/822-21.cpp b/uva_cpp_clean/822/822-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cb695bc23780afee19d32f673ce35a419eeec43 --- /dev/null +++ b/uva_cpp_clean/822/822-21.cpp @@ -0,0 +1,83 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 822 + Name: Queue and A + Problem: https://onlinejudge.org/external/8/822.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int pid[7], lastSchedule[7], finJob[7]; +bool comp(int i, int j) { + return lastSchedule[i] != lastSchedule[j] + ? lastSchedule[i] < lastSchedule[j] + : pid[i] < pid[j]; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int serv[23], n, m, id, cnt, offs, per; + for (int cse=1; cin >> n && n; ++cse) { + priority_queue, greater > tms; + queue jobs[23]; + + map ids; + for (int i=0; i> id >> cnt >> offs >> serv[i] >> per; + ids[id] = i; + + for (int j=0; j> m; + vector pr[7]; + for (int i=0; i> pid[i] >> cnt; + + pr[i].resize(cnt); + for (int j=0; j> id; + pr[i][j] = ids[id]; + } + } + + + int lastTm = -1; + while (!tms.empty()) { + int tm = tms.top(); tms.pop(); + if (tm == lastTm) continue; + lastTm = tm; + + int P[7], psz=0; + for (int i=0; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 824 + Name: Coast Tracker + Problem: https://onlinejudge.org/external/8/824.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int xx[] = {0, -1, -1, -1, 0, 1, 1, 1}, + yy[] = {1, 1, 0, -1, -1, -1, 0, 1}, + M[3][3]; + +int nextDir(int d) { + for (int i=0; i<8; i++) { + int nd = (d+6+i)%8; + if (M[xx[nd]+1][yy[nd]+1]) + return nd; + } + return d; +} + +int main() { + int x, y, d, xi, yi, si; + while (cin>>x>>y>>d && (x>=0 && y>=0 && d>=0)) { + for (int i=0; i<8; i++) { + cin>>xi>>yi>>si; + M[xi-x+1][yi-y+1] = si; + } + cout << nextDir(d) << endl; + } +} diff --git a/uva_cpp_clean/824/824-6.cc b/uva_cpp_clean/824/824-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..bab173d785523f00406e64847b4f0a0cc824afab --- /dev/null +++ b/uva_cpp_clean/824/824-6.cc @@ -0,0 +1,25 @@ +// https://uva.onlinejudge.org/external/8/824.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + for(;;){ + int y,x,d,j,k,l; + cin>>x>>y>>d; + if(x==-1)break; + vvi a(3,vi(3)); + for(int i=0;i<8;i++){ + cin>>j>>k>>l; + a[y-k+1][j-x+1]=l; + } + int u[]={-1,-1,0,1,1,1,0,-1}; + int v[]={0,-1,-1,-1,0,1,1,1}; + int e=(d-2)%8; + if(e<0)e+=8; + while(!a[1+u[e]][1+v[e]])e=(e+1)%8; + cout< +#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0); +#define ll long long +#define oo 1e15 +using namespace std; + +/** + * by:Ahmed Sakr (sakr_) with AZA ; + * freopen("input.txt","r",stdin); + * freopen("output.txt","w",stdout); + * __builtin_popcountll(); +**/ + +class utility { +public : + static int gcd(int a, int b) { return (!b) ? a : gcd(b, a % b); } + static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } + static bool check_prime(int n) { bool is_prime = true; + if (n == 0 || n == 1) {is_prime = false;} + for (int i = 2; i <= n / 2; ++i) { + if (n % i == 0) {is_prime = false;break;}} + return is_prime;} +}; + +int r,c; +vector> arr; +vector> dp; + +int solve (int a,int b) { + + if(a>=r || b>=c) { + return 0; + } + + if(arr[a][b] == 1) return 0; + + if(a == r-1 && b==c-1) { + return 1; + } + + int &ans= dp[a][b]; + if(ans!=-1) return ans; + + ans= solve(a,b+1); + ans+= solve(a+1,b); + + return ans; + +} + + +void solveit(int t) { + + cin>>r>>c; + + cin.ignore(); + arr = vector>(r+2,vector(c+2,0)); + dp = vector>(r+2,vector(c+2,-1)); + + int row,col; + string s; + for(int i=0;i> row; + row--; + + while (temp>>col) { + arr[row][col-1]=1; + } + + } + + cout<>timeOfRepeteLoop; + while (timeOfRepeteLoop--) { + solveit(timeOfRepeteLoop); + } + + + +} + + diff --git a/uva_cpp_clean/825/825-21.cpp b/uva_cpp_clean/825/825-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c5158ebc60ecef3b6679534837cee99825f8af9 --- /dev/null +++ b/uva_cpp_clean/825/825-21.cpp @@ -0,0 +1,73 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 825 + Name: Walking on the Safe Side + Problem: https://onlinejudge.org/external/8/825.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct State { + int i, j, d; + State(int i, int j, int d):i(i),j(j),d(d){} +}; + +long long DP[143][143]; +int D[143][143], n, m; +bool B[143][143]; + +int di[] = {1,0,-1,0}, + dj[] = {0,1,0,-1}; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + while (T--) { + cin >> n >> m; + for (int i=0; i> u; + string line; + getline(cin, line); + stringstream sin(line); + while (sin >> v) + B[u-1][v-1] = 1; + } + + queue q; + if (!B[0][0]) { + q.push(State(0,0,D[0][0]=0)); + DP[0][0] = 1; + } + while (!q.empty()) { + State s = q.front(); q.pop(); + + for (int k=0; k<2; ++k) { + int ni = s.i + di[k], + nj = s.j + dj[k], + nd = s.d + 1; + + if (ni>=0 && ni=0 && nj nd) + q.push(State(ni, nj, D[ni][nj] = nd)); + + DP[ni][nj] += DP[s.i][s.j]; + } + } + } + + cout << DP[n-1][m-1] << "\n"; + if (T) cout << "\n"; + } +} diff --git a/uva_cpp_clean/833/833-21.cpp b/uva_cpp_clean/833/833-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c96cef46058b8ef2fbbdb5d4793f6848f6171aea --- /dev/null +++ b/uva_cpp_clean/833/833-21.cpp @@ -0,0 +1,93 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 833 + Name: Water Falls + Problem: https://onlinejudge.org/external/8/833.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +#define EPS 1e-8 + +struct Point { + double x, y; + Point():x(0),y(0){} + Point(double x, double y):x(x),y(y){} + bool operator < (const Point &o) const { return x < o.x; } + bool operator == (const Point &o) const { return fabs(x-o.x)> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} + +// ------------------------------------------------------------------------------------------ + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(0); + + int T, n, m; + cin >> T; + Line L[1143]; + Point Pu[1143], Pv[1143], p; + while (T-- && cin >> n) { + for (int i=0; i> Pu[i] >> Pv[i]; + if (Pu[i].x > Pv[i].x) + swap(Pu[i], Pv[i]); + L[i] = Line(Pu[i], Pv[i]); + } + cin >> m; + while (m--) { + cin >> p; + int bi = 0; + while (bi >= 0) { + bi = -1; + double by = 0; + for (int i=0; i Pu[i].x && p.x < Pv[i].x) { + double yy = L[i].y(p.x); + if (yy > by && yy < p.y) + bi = i, + by = yy; + } + if (bi != -1) + p = Pu[bi].y < Pv[bi].y ? Pu[bi] : Pv[bi]; + } + cout << p.x << '\n'; + } + if (T) cout << '\n'; + } +} diff --git a/uva_cpp_clean/834/834-19.cpp b/uva_cpp_clean/834/834-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40299374de2fef4b7f5abc7a2d7f05058c4a0613 --- /dev/null +++ b/uva_cpp_clean/834/834-19.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + + +int main() { + int n, d; + while (cin >> n >> d) { + vector v; + while (d != 0) { + int bi = n / d; + v.push_back(bi); + n -= d*bi; + int auxi = d; + d = n, n = auxi; + } + printf("["); + for (int i = 0; i < v.size(); i++) { + printf("%d", v[i]); + if (i != v.size() - 1) { + if (i == 0) printf(";"); + else printf(","); + } + } + printf("]\n"); + } + return 0; +} diff --git a/uva_cpp_clean/834/834-20.cpp b/uva_cpp_clean/834/834-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69a1609cd3e092d39c7044fa5ed0b1efa50d8b6e --- /dev/null +++ b/uva_cpp_clean/834/834-20.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + int numerator, denominator; + + while( cin >> numerator >> denominator ) + { + cout << "[" << numerator/denominator << ";"; + + numerator %= denominator; + swap(numerator, denominator); + + for(int i=0 ; i0 ) cout << ","; + cout << numerator/denominator; + + numerator %= denominator; + swap(numerator, denominator); + } + cout << "]" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/834/834-21.cpp b/uva_cpp_clean/834/834-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33b84334b0b95fcaaeac335901a02567e5e2be28 --- /dev/null +++ b/uva_cpp_clean/834/834-21.cpp @@ -0,0 +1,33 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 834 + Name: Continued Fractions + Problem: https://onlinejudge.org/external/8/834.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + int a, b; + while(cin>>a>>b) { + cout<<"["; + for (int i=0; a && b; i++) { + int x = a/b; + int t = b; + b = a - b*x; + a = t; + + if (i==1) cout << ";"; + else if (i>1) cout << ","; + cout << x; + } + cout << "]\n"; + } +} diff --git a/uva_cpp_clean/834/834-4.cpp b/uva_cpp_clean/834/834-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96908ebae867ef530f1dac2fe3d5e47305569fb9 --- /dev/null +++ b/uva_cpp_clean/834/834-4.cpp @@ -0,0 +1,222 @@ +#include +#include +using namespace std; + +class Rational +{ +public: + Rational(); + Rational(int numerator, int denominator = 1); + Rational(const Rational &r); + + Rational operator+(Rational &r); + Rational operator-(Rational &r); + Rational operator*(Rational &r); + Rational operator/(Rational &r); + + friend bool operator==(Rational &r1, Rational &r2); + friend bool operator!=(Rational &r1, Rational &r2); + friend bool operator<(Rational &r1, Rational &r2); + friend bool operator>(Rational &r1, Rational &r2); + friend bool operator<=(Rational &r1, Rational &r2); + friend bool operator>=(Rational &r1, Rational &r2); + + friend ostream &operator<<(ostream &output, Rational &r); + friend istream &operator>>(istream &input, Rational &r); + + void shorten(); + + int getnumerator(); + int getdenominator(); + +private: + int numerator; + int denominator; + + static int gcd(int x, int y); +}; + +Rational::Rational() +{ + numerator = 0; + denominator = 1; +} + +Rational::Rational(const Rational &r) +{ + this->numerator = r.numerator; + this->denominator = r.denominator; +} + +Rational::Rational(int numerator, int denominator) +{ + this->numerator = numerator; + this->denominator = denominator; + + shorten(); +} + +Rational Rational::operator+(Rational &r) +{ + int nww = (this->denominator * r.denominator) / gcd(this->denominator, r.denominator); + int w1 = nww / this->denominator, w2 = nww / r.denominator; + + return Rational(w1 * this->numerator + w2 * r.numerator, nww); +} + +Rational Rational::operator-(Rational &r) +{ + int nww = (this->denominator * r.denominator) / gcd(this->denominator, r.denominator); + int w1 = nww / this->denominator, w2 = nww / r.denominator; + + return Rational(w1 * this->numerator - w2 * r.numerator, nww); +} + +Rational Rational::operator*(Rational &r) +{ + return Rational(this->numerator * r.numerator, this->denominator * r.denominator); +} + +Rational Rational::operator/(Rational &r) +{ + return Rational(this->numerator * r.denominator, this->denominator * r.numerator); +} + +inline int Rational::getnumerator() +{ + return this->numerator; +} + +inline int Rational::getdenominator() +{ + return this->denominator; +} + +int Rational::gcd(int x, int y) +{ + while (y != 0) + { + int r = x % y; + x = y; + y = r; + } + + return x; +} + +void Rational::shorten() +{ + int _gcd = gcd(this->numerator, this->denominator); + + this->numerator /= _gcd; + this->denominator /= _gcd; + + if (this->denominator < 0) + { + this->denominator = 0 - this->denominator; + this->numerator = 0 - this->numerator; + } +} + +bool operator==(Rational &r1, Rational &r2) +{ + return (r1.denominator == r2.denominator) && (r1.numerator == r2.numerator); +} + +bool operator!=(Rational &r1, Rational &r2) +{ + return (r1.denominator != r2.denominator) || (r1.numerator != r2.numerator); +} + +bool operator>(Rational &r1, Rational &r2) +{ + Rational x = r1 - r2; + if (((double)x.getnumerator() / (double)x.getdenominator()) > 0) + return true; + else + return false; +} + +bool operator<(Rational &r1, Rational &r2) +{ + Rational x = r1 - r2; + if (((double)x.getnumerator() / (double)x.getdenominator()) < 0) + return true; + else + return false; +} + +bool operator>=(Rational &r1, Rational &r2) +{ + Rational x = r1 - r2; + if (((double)x.getnumerator() / (double)x.getdenominator()) >= 0) + return true; + else + return false; +} + +bool operator<=(Rational &r1, Rational &r2) +{ + Rational x = r1 - r2; + if (((double)x.getnumerator() / (double)x.getdenominator()) <= 0) + return true; + else + return false; +} + +ostream &operator<<(ostream &output, Rational &r) +{ + output << r.numerator; + + if (r.denominator != 1) + output << "|" << r.denominator; + + return output; +} + +istream &operator>>(istream &input, Rational &r) +{ + char separator; + input >> r.numerator >> separator >> r.denominator; + r.shorten(); + + return input; +} + +int main() +{ + int numerator, denomirator; + + while (cin >> numerator >> denomirator) + { + vector numbers; + + while (true) + { + if (numerator % denomirator == 0) + break; + + numbers.push_back(numerator / denomirator); + + int t = numerator; + numerator = denomirator; + denomirator = t % denomirator; + } + + numbers.push_back(numerator); + + cout << "[" << numbers[0] << ";"; + + for (unsigned int i = 1; i < numbers.size(); i++) + { + cout << numbers[i]; + + if (i + 1 != numbers.size()) + cout << ","; + } + + cout << "]" << endl; + } + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/834/834-9.cpp b/uva_cpp_clean/834/834-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7068897382cd2f79d79c766003ed4654f1e2e3b --- /dev/null +++ b/uva_cpp_clean/834/834-9.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main(){ + int n, m; + vector a; + while(scanf("%d %d", &n, &m) == 2){ + while(m > 0){ + a.push_back(n / m); + n %= m; + swap(n, m); + } + putchar('['); + for(int i = 0; i < a.size(); i++){ + printf("%d", a[i]); + if(i == 0 && a.size() != 1) + putchar(';'); + else if(i != a.size() - 1) + putchar(','); + } + printf("]\n"); + a.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/836/836-19.cpp b/uva_cpp_clean/836/836-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eff3a06e6491c77d51d04be1e92a9bc6bf551a2 --- /dev/null +++ b/uva_cpp_clean/836/836-19.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ii; + +int height[30][30]; + +int main() { + int n; + cin >> n; + string s; + getline(cin, s); + getline(cin, s); + while (n--) { + vector v; + while (getline(cin, s) && s != "") { + v.push_back(s); + } + for (int i = 1; i <= v.size(); i++) { + for (int j = 1; j <= v[i - 1].length(); j++) { + int val = v[i - 1][j - 1] == '1'? 1: 0; + height[i][j] = val? height[i - 1][j] + val: 0; + } + } + int maxi = 0; + for (int i = 1; i <= v.size(); i++) { + for (int j = 1; j <= v[i - 1].length(); j++) { + int val = v[i - 1][j - 1] == '1'? 1: 0; + int smaxi = 0, h = 100; + for (int k = j; k >= 1; k--) { + if (height[i][k] == 0) break; + h = min(h, height[i][k]); + smaxi = max(smaxi, (j - k + 1) * h); + } + maxi = max(maxi, smaxi); + } + } + printf("%d\n", maxi); + if (n) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/836/836-21.cpp b/uva_cpp_clean/836/836-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d17adc8d86f6655ac5b395250677445400e9146f --- /dev/null +++ b/uva_cpp_clean/836/836-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 836 + Name: Largest Submatrix + Problem: https://onlinejudge.org/external/8/836.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +int main(){ + char line[100]; + int T, n; + cin>>T; + cin.getline(line, 100); + cin.getline(line, 100); + + int mat[30][30], cum[30][30]; + + + while (T--) { + for (n=0; cin.getline(line, 100) && line[0]; n++) + for (int i=0; line[i]; i++) { + mat[n+1][i+1] = line[i] - '0'; + cum[n+1][i+1] = cum[n][i+1] + mat[n+1][i+1]; + } + + int mx=0; + for (int i1=0; i1 mx) mx = sum; + } + } + cout << mx << endl; + + + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/836/836-5.cpp b/uva_cpp_clean/836/836-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..e2d1e2660b8aa3159c901835f33ce45c50ae8f97 --- /dev/null +++ b/uva_cpp_clean/836/836-5.cpp @@ -0,0 +1,245 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +struct zeone +{ + int one=0,zero=0; + +}; + +int main() +{ + int t; + si(t); + while(t--) + { + string s[30]; + cin>>s[0]; + + int n=sz(s[0]); + + zeone cs[50][50]; + + for0(i,n) + { + if(s[0][i]=='0') + { + cs[1][i+1].zero = cs[1][i].zero+1; + cs[1][i+1].one = cs[1][i].one; + } + else + { + cs[1][i+1].one = cs[1][i].one+1; + cs[1][i+1].zero = cs[1][i].zero; + } + +// PN(cs[1][i+1].one); + } +// nl; + + for1(i,n-1) + { + cin>>s[i]; + for0(j,n) + { + if(s[i][j]=='0') + { + cs[i+1][j+1].zero = cs[i][j+1].zero + cs[i+1][j].zero - cs[i][j].zero +1; + cs[i+1][j+1].one = cs[i][j+1].one + cs[i+1][j].one - cs[i][j].one; + } + else + { + cs[i+1][j+1].zero = cs[i][j+1].zero + cs[i+1][j].zero - cs[i][j].zero; + cs[i+1][j+1].one = cs[i][j+1].one + cs[i+1][j].one - cs[i][j].one+1; + } +// PN(cs[i+1][j+1].one); + + } +// nl; + } + +// nl; +// for0(i,n) +// { +// cout<=p) + { + res.zero = p; + p = ( cs[i][j].one - (cs[i][l-1].one + cs[k-1][j].one) ) + cs[k-1][l-1].one; + if(res.one + +using namespace std; + +int tc, n, cum_sum[25][25], max_sum; +string s[25]; + +bool check(int pi, int pj, int x, int y){ + for(int i = pi; i < pi + y; i++) + if(pj == 0){ + if(cum_sum[i][pj + x - 1] != x) + return false; + } + else{ + if(cum_sum[i][pj + x - 1] - cum_sum[i][pj - 1] != x) + return false; + } + return true; +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + getchar(); + cin >> s[0]; + n = s[0].size(); + for(int i = 1; i < n; i++) + cin >> s[i]; + memset(cum_sum, 0, sizeof cum_sum); + for(int i = 0; i < n; i++){ + cum_sum[i][0] = s[i][0] - '0'; + for(int j = 1; j < n; j++) + cum_sum[i][j] = cum_sum[i][j - 1] + s[i][j] - '0'; + } + max_sum = 0; + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + for(int x = 1; x <= n - j; x++) + for(int y = 1; y <= n - i; y++) + if(x * y > max_sum && check(i, j, x, y)) + max_sum = x * y; + printf("%d\n", max_sum); + if(tc) + putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/837/837-21.cpp b/uva_cpp_clean/837/837-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fa6edac15a5e8df17db0e3380e82b4826a610d9 --- /dev/null +++ b/uva_cpp_clean/837/837-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 837 + Name: Light and Transparencies + Problem: https://onlinejudge.org/external/8/837.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct Event { + int u, s; + double t; + Event(double t, int u, int s):t(t),u(u),s(s){} + bool operator < (const Event &o) const { return t > o.t; } +}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + cout << fixed << setprecision(3); + + int T, n; + cin >> T; + double Xl[10000], Xr[10000], C[10000]; + while (T-- && cin >> n) { + priority_queue q; + for (int i=0; i> x1 >> t >> x2 >> t >> C[i]; + Xl[i] = min(x1, x2); + Xr[i] = max(x1, x2); + q.push(Event(Xl[i], i, 1)); + } + + cout << (n<<1|1) << '\n'; + double cc=1, lx=-1e10000; + while (!q.empty()) { + Event e = q.top(); q.pop(); + cout << lx << ' ' << e.t << ' ' << cc << '\n'; + if (e.s) { + cc *= C[e.u]; + q.push(Event(Xr[e.u], e.u, 0)); + } + else cc /= C[e.u]; + lx = e.t; + } + cout << lx << " +inf 1.000\n"; + if (T) cout << '\n'; + } +} diff --git a/uva_cpp_clean/839/839-17.cpp b/uva_cpp_clean/839/839-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4c284b09f4814f524e43f1b77f410663b416442 --- /dev/null +++ b/uva_cpp_clean/839/839-17.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +bool balance(int &w) +{ + int wl, dl, wr, dr; + cin >> wl >> dl >> wr >> dr; + bool bl = true, br = true; + if (!wl) + bl = balance(wl); + if (!wr) + br = balance(wr); + w = wl + wr; + return bl && br && wl * dl == wr * dr; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int T, w; + cin >> T; + while (T--) + { + cout << (balance(w) ? "YES" : "NO") << "\n"; + if (T) + cout << "\n"; + } +} diff --git a/uva_cpp_clean/846/846-18.cpp b/uva_cpp_clean/846/846-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f849c310594a6e6ced76933b51602efefb8a3b44 --- /dev/null +++ b/uva_cpp_clean/846/846-18.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int main(){ + int N, x, y, delta, minStep, len; + bool changeLen; + cin >> N; + while(N-- > 0){ + cin >> x >> y; + delta = y - x; + minStep = 0; + len = 1; + changeLen = false; + while(delta > 0){ + delta -= len; + minStep++; + if(changeLen) len++; + changeLen = !changeLen; + } + printf("%d\n", minStep); + } + return 0; +} +// Ir aumentando el tam de los pasos en 1, pero cada dos pasos, asi como en la vida real \ No newline at end of file diff --git a/uva_cpp_clean/846/846-19.cpp b/uva_cpp_clean/846/846-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9675fa8e09d012253cbdfaba39c4e89f0cfda5ec --- /dev/null +++ b/uva_cpp_clean/846/846-19.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; + +int main() { + int T; + cin >> T; + while (T--) { + int n1, n2; + cin >> n1 >> n2; + n2 -= n1; + if (n2 == 0) { + printf("0\n"); + continue; + } + n2--; + n1 = ((-1 + floor(sqrt(1+4*n2)))/2) + 1; + int auxi = n1*(n1-1); + if (n2-auxi < n1) printf("%d\n", n1*2 - 1); + else printf("%d\n", n1*2); + } + return 0; +} diff --git a/uva_cpp_clean/846/846-20.cpp b/uva_cpp_clean/846/846-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4813325434e502cd182857a394044cb96e29705 --- /dev/null +++ b/uva_cpp_clean/846/846-20.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +int main() +{ + int tc; + + cin >> tc; + while( tc-- ) + { + int x, y; + cin >> x >> y; + + int len = y-x, min_steps = 0; + if( len!=0 ) + { + int sum = 0, temp = 2; + while( len>sum ) + { + sum += temp/2; + temp++; + min_steps++; + } + } + + cout << min_steps << endl; + } + return 0; +} diff --git a/uva_cpp_clean/846/846-21.cpp b/uva_cpp_clean/846/846-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c09334227f688658fb25c1d8766862184711cc4 --- /dev/null +++ b/uva_cpp_clean/846/846-21.cpp @@ -0,0 +1,30 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 846 + Name: Steps + Problem: https://onlinejudge.org/external/8/846.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + int T, a, b; + cin>>T; + while(T--) { + cin>>a>>b; + int n = b-a, + k = (sqrt(4.0*n+1)-1)/2, + d = n - k*k - k; + + if (d == 0) cout << 2*k < +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sp printf(" ") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define pb push_back +#define MAX 100005 +#define INF 1000000000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm(ll a, ll b) { + return a * b / __gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +int main () { + //~ __FastIO; + ll n; + while (scanf("%lld", &n) == 1) { + ll sum = 1, ans; + for (int i = 1; ; i++) { + + if (i % 2 == 1) { + if (sum * 9 >= n) { + ans = i; break; + } else { + sum *= 9; + } + } else { + if (sum * 2 >= n) { + ans = i; break; + } else { + sum *= 2; + } + } + + } + if (ans % 2 == 1) { + printf("Stan wins.\n"); + } else { + printf("Ollie wins.\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/847/847-20.cpp b/uva_cpp_clean/847/847-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..900ddb657a611403df4945744ef345d4f7b2d3da --- /dev/null +++ b/uva_cpp_clean/847/847-20.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +int main() +{ + unsigned long long n; + + while( cin >> n ) + { + unsigned long long p = 1, counter = 0; + while( p>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 847 + Name: A Multiplication Game + Problem: https://onlinejudge.org/external/8/847.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +#define EPS 1e-8 +int main(){ + double lg18 = log2(18), + lg9 = log2(9)/lg18; + long long int n; + while (cin>>n) { + double t = log2(n)/lg18; + t = t - (int)t; + cout << (t>EPS && t<=lg9+EPS ? "Stan wins." : "Ollie wins.") << endl; + } +} diff --git a/uva_cpp_clean/847/847-3.cpp b/uva_cpp_clean/847/847-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5275f950b12701d52555754189ccebc2329f840b --- /dev/null +++ b/uva_cpp_clean/847/847-3.cpp @@ -0,0 +1,34 @@ +#include + +int main () +{ + int N, tempScore, turn; + + while ( scanf("%d",&N) != EOF ) + { + tempScore = 1; + turn = 0; + + while ( tempScore < N ) + { + turn += 1; + if ( 9 * tempScore >= N ) + break; + if ( 18 * tempScore >= N ) + { + turn++; + break; + } + for ( int l = 9; l > 1; l-- ) + if ( (l * 9 * tempScore) < N ) + { + tempScore *= l; + break; + } + } + if ( turn % 2 == 0 ) + puts("Ollie wins."); + else + puts("Stan wins."); + } +} diff --git a/uva_cpp_clean/847/847-5.cpp b/uva_cpp_clean/847/847-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3a44bfd4a4bd56794443ec65aa1d82c39595e42c --- /dev/null +++ b/uva_cpp_clean/847/847-5.cpp @@ -0,0 +1,25 @@ +#include +int main() +{ + long long int n,p; + while(scanf("%lld",&n)!=EOF) + { + p=1; + while(p=n) + { + puts("Stan wins."); + break; + } + p*=2; + if(p>=n) + { + puts("Ollie wins."); + break; + } + } + } +} + diff --git a/uva_cpp_clean/847/847-6.cpp b/uva_cpp_clean/847/847-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21abb15f5a15a33cba82e254aa63d685545ec963 --- /dev/null +++ b/uva_cpp_clean/847/847-6.cpp @@ -0,0 +1,28 @@ +/* + math > game theory > minimax > optimized + difficulty: hard + date: 28/Mar/2020 + problem: multiplication game + hint: note that if Stan always multiply by 9 and Ollie by 2, it's still an optimal solution + by: @brpapa +*/ +#include +using namespace std; +const int STAN = 0, OLLIE = 1; + +int N; +int f(int p, int n) { + // jogador atual p, inteiro n + + if (n >= N) return !p; // p é perdedor + + if (p == STAN) return f(!p, n*9); // max + else return f(!p, n*2); // min +} + +int main() { + while (cin >> N) { + cout << (f(STAN, 1) == STAN? "Stan" : "Ollie") << " wins.\n"; + } + return 0; +} diff --git a/uva_cpp_clean/847/847-9.cpp b/uva_cpp_clean/847/847-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d289fc719f596328ed2f1aa7ed93e5a79125ecaf --- /dev/null +++ b/uva_cpp_clean/847/847-9.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +long long n, num; +bool stan_turn; + +int main() { + while (~scanf("%lld", &n)) { + stan_turn = true, num = 1; + while (num < n) { + if (stan_turn) num *= 9; + else num *= 2; + stan_turn ^= 1; + } + puts(stan_turn ? "Ollie wins." : "Stan wins."); + } + return (0); +} \ No newline at end of file diff --git a/uva_cpp_clean/850/850-21.cpp b/uva_cpp_clean/850/850-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebb7d0e4d42dff8075515136b18da7bc576afaf2 --- /dev/null +++ b/uva_cpp_clean/850/850-21.cpp @@ -0,0 +1,66 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 850 + Name: Crypt Kicker II + Problem: https://onlinejudge.org/external/8/850.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char fox[] = "the quick brown fox jumps over the lazy dog"; +char m[128]; +bool test(char line[]) { + memset(m, 0, sizeof(m)); + + for (int i=0; fox[i]; i++) + if (fox[i]==' ' && line[i]!=' ') + return false; + else if (!m[line[i]]) + m[line[i]] = fox[i]; + else if (m[line[i]] != fox[i]) + return false; + + return true; +} + + +char lines[102][102]; +int main(){ + int fen = strlen(fox); + + int T; + cin>>T; + cin.getline(lines[0], 102); + cin.getline(lines[0], 102); + + while (T--) { + int n = 0, i; + while (cin.getline(lines[n], 102) && lines[n][0]) + n++; + + for (i=0; i +using namespace std; + +char grid[9][9]; +bool proc[9][9]; + +int area(int x, int y, bool & black, bool & white){ + if(x<0 || x>=9 || y<0 || y>=9) return 0; + else if (grid[x][y] != '.'){ + if(grid[x][y] == 'X') black = true; + if(grid[x][y] == 'O') white = true; + return 0; + } + else if(!proc[x][y]){ + proc[x][y]=true; + return 1 + area(x+1, y, black, white) + area(x-1, y, black, white) + area(x, y+1, black, white) + area(x, y-1, black, white); + } + return 0; +} + +int main(){ + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + int cases; + cin >> cases; + for(int i = 0; i < cases; i++){ + int white = 0, black = 0; + for(int y = 0; y < 9; y++){ + for (int x = 0; x < 9; x++){ + cin >> grid[x][y]; + if(grid[x][y] == 'X') black++; + else if (grid[x][y] == 'O') white++; + proc[x][y] = false; + } + } + for(int y = 0; y < 9; y++){ + for (int x = 0; x < 9; x++){ + bool bblack = false, bwhite = false; + int a = area(x, y, bblack, bwhite); + if(bblack && !bwhite) black += a; + if(!bblack && bwhite) white += a; + } + } + cout << "Black " << black << " White " << white << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/852/852-21.cpp b/uva_cpp_clean/852/852-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..471c5436dba42603ee4fa5643f309783a08446f3 --- /dev/null +++ b/uva_cpp_clean/852/852-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 852 + Name: Deciding victory in Go + Problem: https://onlinejudge.org/external/8/852.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool S[10][10]; +char M[10][10]; +int dcolor(int i, int j) { + if (M[i][j] != '.') + return M[i][j]=='O' ? 1 : 2; + + if (S[i][j]) return 0; + S[i][j] = 1; + + int res = 0; + if (res<3 && i>0) res |= dcolor(i-1, j); + if (res<3 && i<8) res |= dcolor(i+1, j); + if (res<3 && j>0) res |= dcolor(i, j-1); + if (res<3 && j<8) res |= dcolor(i, j+1); + return res; +} + +void dfill(int i, int j, char c) { + if (M[i][j] != '.') return; + M[i][j] = c; + if (i>0) dfill(i-1, j, c); + if (i<8) dfill(i+1, j, c); + if (j>0) dfill(i, j-1, c); + if (j<8) dfill(i, j+1, c); +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; cin >> T; + while (T--) { + for (int i=0; i<9; ++i) + cin >> M[i]; + + memset(S, 0, sizeof(S)); + for (int i=0; i<9; ++i) + for (int j=0; j<9; ++j) + if (M[i][j]=='.') { + int c = dcolor(i, j); + dfill(i, j, c==1?'O' : c==2?'X' : -1); + } + + int bn=0, wn=0; + for (int i=0; i<9; ++i) + for (int j=0; j<9; ++j) + if (M[i][j] == 'X') ++bn; + else if (M[i][j] == 'O') ++wn; + + cout << "Black " << bn << " White " << wn << endl; + } +} diff --git a/uva_cpp_clean/852/852-6.cc b/uva_cpp_clean/852/852-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..595c9e3ddf1f5b471e2442887ab75c53c19ed81c --- /dev/null +++ b/uva_cpp_clean/852/852-6.cc @@ -0,0 +1,44 @@ +// https://uva.onlinejudge.org/external/8/852.pdf +#include +using namespace std; +using vb=vector; +using vvb=vector; +using vs=vector; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int t,n=9; + cin>>t; + while(t--){ + vs a(n); + for(int i=0;i>a[i]; + vvb s(n, vb(n)); + bool b,w; + int c,x=0,y=0; + functiondfs=[&](int y,int x){ + s[y][x]=1; + c++; + int r[]={-1,0,1,0}; + int c[]={0,1,0,-1}; + for(int i=0;i<4;i++){ + int u=y+r[i],v=x+c[i]; + if(u>=0&&u=0&&v + +#define SIZE 15 + +using namespace std; + +int tc, black, white; +char grid[SIZE][SIZE]; +bool vis1[SIZE][SIZE], vis2[SIZE][SIZE]; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; +const int N = 9; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < N ); +} + +int floodfill1(int r, int c){ + if( not is_possible(r, c) or vis1[r][c] ) return 0; + if( grid[r][c] == 'O' ) return 0; + vis1[r][c] = true; + int ct = 1; + for(int d = 0; d < 4; d++) + ct += floodfill1(r + dr[d], c + dc[d]); + return ct; +} + +int floodfill2(int r, int c){ + if( not is_possible(r, c) or vis2[r][c] ) return 0; + if( grid[r][c] == 'X' ) return 0; + vis2[r][c] = true; + int ct = 1; + for(int d = 0; d < 4; d++) + ct += floodfill2(r + dr[d], c + dc[d]); + return ct; +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + memset(vis1, false, sizeof vis1); + memset(vis2, false, sizeof vis2); + black = white = 0; + for(int row = 0; row < N; row++) scanf("%s", grid[row]); + for(int row = 0; row < N; row++) + for(int col = 0; col < N; col++) + if( grid[row][col] == 'X' ) + black += floodfill1(row, col); + else if( grid[row][col] == 'O' ) + white += floodfill2(row, col); + for(int row = 0; row < N; row++) + for(int col = 0; col < N; col++) + if( vis1[row][col] and vis2[row][col] ) + black--, white--; + printf("Black %d White %d\n", black, white); + } + return(0); +} diff --git a/uva_cpp_clean/853/853-21.cpp b/uva_cpp_clean/853/853-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01b14290e192a1bc29ae41d14b3e51523dc480ba --- /dev/null +++ b/uva_cpp_clean/853/853-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 853 + Name: DVD Subtitles + Problem: https://onlinejudge.org/external/8/853.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +void toup(char* word){ + int i; + for(i=0;word[i];i++) + if(word[i]>='A' && word[i]<='Z')word[i]+=32; +} +typedef bitset<1001> biti; +int main(){ + int nn,n,i,j,s; + char line[500],word[30],*p; + map::iterator i1,i2; + set::iterator si; + string ww; + bool hw=0; + scanf("%d",&nn); + while(nn--){ + map l[2]; + scanf("%d",&n);getchar(); + for(j=0;j<2;j++) + for(i=0;i'z') && line[s])s++; + while(line[s] && sscanf(line+s,"%[abcdefghijklmnopqrstuvwxyz-]",word)==1){ + s+=strlen(word); + while((line[s]<'a' || line[s]>'z') && line[s])s++; + if(strlen(word)<3)continue; + ww=word; + l[j][ww].set(i); + } + } + for(i1=l[0].begin();i1!=l[0].end();i1++) + if(i1->second.count()>1){ + set s1,s2; + s1.insert(i1->first); + for(i2=i1,i2++;i2!=l[0].end();i2++) + if(i1->second==i2->second){ + s1.insert(i2->first); + i2->second.reset(); + } + for(i2=l[1].begin();i2!=l[1].end();i2++) + if(i1->second==i2->second) + s2.insert(i2->first); + if(!s1.empty() && !s2.empty()) + { + for(si=s1.begin();si!=s1.end();si++){ + if(si!=s1.begin())cout<<" ";cout<<*si;} + cout<<"/"; + for(si=s2.begin();si!=s2.end();si++){ + if(si!=s2.begin())cout<<" ";cout<<*si;} + cout<<"\n"; + hw=1; + } + } + if(nn && hw)cout<<"\n"; + hw=0; + } + return 0; +} diff --git a/uva_cpp_clean/855/855-21.cpp b/uva_cpp_clean/855/855-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27589b65f161491a09029f6086a47dac56c50e3c --- /dev/null +++ b/uva_cpp_clean/855/855-21.cpp @@ -0,0 +1,31 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 855 + Name: Lunch in Grid City + Problem: https://onlinejudge.org/external/8/855.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + int T, n, X[50001], Y[50001]; + scanf("%d", &T); + while (T--) { + scanf("%*d%*d%d", &n); + for (int i=0; i>1; + nth_element(X, X+mid, X+n); + nth_element(Y, Y+mid, Y+n); + + printf("(Street: %d, Avenue: %d)\n", X[mid], Y[mid]); + } +} diff --git a/uva_cpp_clean/855/855-9.cpp b/uva_cpp_clean/855/855-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5c98ed178975a9b446e05a16bbcf7da1ce3e2fd --- /dev/null +++ b/uva_cpp_clean/855/855-9.cpp @@ -0,0 +1,26 @@ +#include + +#define SIZE 50010 + +using namespace std; + +int tc, row, col, f, n, x[SIZE], y[SIZE]; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d %d", &row, &col, &f); + for(int i = 0; i < f; i++) scanf("%d %d", &x[i], &y[i]); + n = f / 2; + if(f&1){ + nth_element(x, x + n, x + f), nth_element(y, y + n, y + f); + row = x[n], col = y[n]; + } + else{ + nth_element(x, x + n - 1, x + f), nth_element(y, y + n - 1, y + f); + row = x[n - 1], col = y[n - 1]; + } + printf("(Street: %d, Avenue: %d)\n", row, col); + } + return(0); +} diff --git a/uva_cpp_clean/859/859-21.cpp b/uva_cpp_clean/859/859-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad8d16cb0df33bbc8bfbf8fd5942135883800c22 --- /dev/null +++ b/uva_cpp_clean/859/859-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 859 + Name: Chinese Checkers + Problem: https://onlinejudge.org/external/8/859.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct State { + int y, x, c; + State (int y, int x, int c):y(y),x(x),c(c){} + bool operator < (const State &s) const { return y!=s.y ? y > s.y : x < s.x; } +}; + + +bool seen[1000][1000], M[1000][1000]; +int dx[] = {-1,0,1,-1,1}, + dy[] = { 0,1,0, 1,1}, + n, m; + +bool check(State &s) { return s.x>0 && s.x<=m && s.y<=n; } + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + bool frst = 1; + int x, y, sx, sy; + while (cin >> n >> m) { + if (frst) frst = 0; + else cout << endl; + + for (int i=1; i<=n; ++i) { + memset(seen[i]+1, 0, m); + memset(M[i]+1, 0, m); + } + + for (int i=0; i<4*m; ++i) { + cin >> y >> x; + M[y][x] = 1; + } + cin >> sy >> sx; + + vector v; + for (int k=0; k<3; ++k) { + State t(sy+dy[k], sx+dx[k], 1); + if (check(t) && !M[t.y][t.x]) + v.push_back(t); + } + + queue q; + q.push(State(sy, sx, 0)); + while (!q.empty()) { + State s = q.front(); q.pop(); + for (int k=0; k<5; ++k) { + State t(s.y+2*dy[k], s.x+2*dx[k], s.c+1); + if (check(t) && !M[t.y][t.x] && !seen[t.y][t.x] && M[t.y-dy[k]][t.x-dx[k]]) { + seen[t.y][t.x] = 1; + v.push_back(t); + q.push(t); + } + } + } + + + sort(v.begin(), v.end()); + for (State &s: v) + cout << s.y << ' ' << s.x << ' ' << s.c << endl; + } +} diff --git a/uva_cpp_clean/860/860-21.cpp b/uva_cpp_clean/860/860-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe0d306e9197faca5835e35ca49bd5097beef578 --- /dev/null +++ b/uva_cpp_clean/860/860-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 860 + Name: Entropy Text Analyzer + Problem: https://onlinejudge.org/external/8/860.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +using namespace std; + + +int main(){ + scanf("%*[,.:;!?\"() \t\n]"); + map cnts; + char word[1000]; + int l=0; + while (scanf("%[^,.:;!?\"() \t\n] %*[,.:;!?\"() \t\n]", word)==1) { + if (strcmp(word, "****END_OF_INPUT****")==0) break; + else if (strcmp(word, "****END_OF_TEXT****")==0) { + double et=0, ll=log10(l); + for (auto &it: cnts) + et += it.second * (ll - log10(it.second)); + et /= l; + + printf("%d %.1lf %.0lf\n", l, et, 100*et/ll); + cnts.clear(); + l=0; + continue; + } + + for (int i=0; word[i]; i++) + word[i] = tolower(word[i]); + cnts[word]++; + l++; + } +} diff --git a/uva_cpp_clean/861/861-21.cpp b/uva_cpp_clean/861/861-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91176b6f87ce1d7480a301e53d25642b20f61903 --- /dev/null +++ b/uva_cpp_clean/861/861-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 861 + Name: Little Bishops + Problem: https://onlinejudge.org/external/8/861.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include + +using namespace std; + +int a[20],n,k; + +int too(int i){ + if(i0;j-=2) + if(cross(j,i)) + return 0; + return 1; +} + +int rec(int u,int m){ + int i; + int t; + if(m==0)return 1; + if(u>=2*n)return 0; + + a[u]=0; + t=rec(u+2,m); + for(i=1;i<=too(u);i++) + { + a[u]=i; + if(check(u)) + t+=rec(u+2,m-1); + } + return t; +} + +int s[9][3][65]={-1}; +int saved(int i,int j){ + if(s[n][i][j]==-1) + s[n][i][j]=rec(i,j); + return s[n][i][j]; +} + + +int ans(){ + int t=0; + int i; + for(i=0;i<=k;i++) + t+=saved(1,i)*saved(2,k-i); + return t; +} + +int main(){ + memset(s,-1,sizeof(s)); + while(scanf("%d%d",&n,&k)==2 && n+k){ + cout< +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + vector map(128); + int n_case, length, i; + string plaintext, substitution; + + cin >> n_case; + cin.ignore(1024, '\n'); + cin.ignore(1024, '\n'); // 第一次要濾掉兩個換行 + while (n_case--) { + iota(map.begin(), map.end(), 0); // 自己對應自己 + + getline(cin, plaintext); // 有可能有空白 + getline(cin, substitution); + + cout << substitution << "\n" + << plaintext << "\n"; + + length = plaintext.length(); + + for (i = 0; i < length; i++) + map[plaintext[i]] = substitution[i]; // 建對應表 + + while (getline(cin, plaintext) && plaintext.length()) { + for (const char& c : plaintext) + cout << map[c]; + cout << "\n"; + } + if (n_case) + cout << "\n"; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/865/865-18.cpp b/uva_cpp_clean/865/865-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1eb44e75fcf6eb073fc6e8f790c166b36cb2154c --- /dev/null +++ b/uva_cpp_clean/865/865-18.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int main(){ + char alpha[70], sust[70], line[70], ascii[128]; + int tc; + scanf("%d", &tc); + getchar(); + getchar(); + while(tc-- > 0){ + gets(alpha); + gets(sust); + printf("%s\n%s\n",sust, alpha); + for(int i = 0; i < 128; i++) ascii[i] = i; + for(int i = 0; alpha[i] != 0; i++) ascii[alpha[i]] = sust[i]; + while(gets(line)){ + if(line[0] == '\0') break; + for(int i = 0; line[i] != 0; i++) printf("%c", ascii[line[i]]); + printf("\n"); + } + if(tc > 0) printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/865/865-9.cpp b/uva_cpp_clean/865/865-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97e6de89016c8e76fdbf57945846dcfebe1a0e3e --- /dev/null +++ b/uva_cpp_clean/865/865-9.cpp @@ -0,0 +1,15 @@ +first = True +for tc in range( int( input() ) ): + if first: input() + else: print('') + in_tab, out_tab = input(), input() + print(out_tab) + print(in_tab) + while True: + try: + line = input() + if len(line) == 0: break + print(line.translate( str.maketrans(in_tab, out_tab) )) + except EOFError: + break + first = False \ No newline at end of file diff --git a/uva_cpp_clean/866/866-5.cpp b/uva_cpp_clean/866/866-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d614fa23f2e06ecff1843b95f22cdc4357c68d43 --- /dev/null +++ b/uva_cpp_clean/866/866-5.cpp @@ -0,0 +1,223 @@ +/****************************************************************** +*** Problem : *** +*** Author : Shipu Ahamed (Psycho Timekiller) *** +*** E-mail : shipuahamed01@gmail.com *** +*** University : BUBT,Dept. of CSE *** +*** Team : BUBT_Psycho *** +*** My Blog : http://shipuahamed.blogspot.com *** +*** Facebook : http://www.facebook.com/DeesheharaShipu *** +******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int tarea(int x1, int y1, int x2, int y2, int x3, int y3){ //i don't use struct bcz it's tough for me. + return x1*y2-y1*x2+x2*y3-y2*x3+x3*y1-y3*x1; +} + +bool online(int x1,int y1,int x2,int y2,int x3,int y3) +{ + int minx,miny,maxx,maxy; + minx=min(x1,x2);miny=min(y1,y2); + maxx=max(x1,x2);maxy=max(y1,y2); + + if(minx<=x3 && maxx>=x3 && miny<=y3 && maxy>=y3 ) + return true; + else + return false; +} + +bool intersection(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) +{ + int a1=tarea(x3,y3,x4,y4,x1,y1); + int a2=tarea(x3,y3,x4,y4,x2,y2); + int a3=tarea(x1,y1,x2,y2,x3,y3); + int a4=tarea(x1,y1,x2,y2,x4,y4); + +// PP(a1,a2),PP(a3,a4); +// P(online(x3,y3,x4,y4,x1,y1)); +// nl; + if(a1*a2<0 && a3*a4<0) return true; + else if(a1==0 && online(x3,y3,x4,y4,x1,y1)) return true; + else if(a2==0 && online(x3,y3,x4,y4,x2,y2)) return true; + else if(a3==0 && online(x1,y1,x2,y2,x3,y3)) return true; + else if(a4==0 && online(x1,y1,x2,y2,x4,y4)) return true; + else + return false; +} + +struct line +{ + int x1,y1,x2,y2; +}; + +int main() +{ + int t; + si(t); + while(t--) + { + vectorv; + line p; + + int n; + si(n); + while(n--) + { + sii(p.x1,p.y1),sii(p.x2,p.y2); + v.pb(p); + } + + n=sz(v); + + int res=0; + for0(i,n) + { +// PP(v[i].x1,v[i].y1),PP(v[i].x2,v[i].y2); + int cnt=0; + for0(j,n) + { + if(i==j) continue; + + if(intersection(v[i].x1,v[i].y1,v[i].x2,v[i].y2,v[j].x1,v[j].y1,v[j].x2,v[j].y2 )) + cnt++; + } + res += cnt+1; + } + + P(res); + if(t) + nl; + + } + + return 0; +} diff --git a/uva_cpp_clean/868/868-21.cpp b/uva_cpp_clean/868/868-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db57ae03218cdce218f9c45556d852dd701fdab3 --- /dev/null +++ b/uva_cpp_clean/868/868-21.cpp @@ -0,0 +1,55 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 868 + Name: Numerical Maze + Problem: https://onlinejudge.org/external/8/868.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 2147483647 +using namespace std; + + +int X[143][143], n, m, rj, + di[] = { 0, 0, 1, -1 }, + dj[] = { 1, -1, 0, 0 }; + +void dfs(int i, int j, int x=1, int c=1) { + if (x > c) x=1, ++c; + if (i<0 || i>=n || j<0 || j>m || X[i][j]!=x) return; + if (i == n-1) { rj = min(rj, j); return; } + + int o = X[i][j]; X[i][j]=0; + for (int k=0; k<4; ++k) + dfs(i+di[k], j+dj[k], x+1, c); + X[i][j] = o; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T; + cin >> T; + while (T--) { + cin >> n >> m; + for (int i=0; i> X[i][j]; + + rj = INF; + for (int j=0; j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 869 + Name: Airline Comparison + Problem: https://onlinejudge.org/external/8/869.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int f1[300][300],f2[300][300]; + +void check(){ + int i,j; + for(i=0;i<256;i++) + for(j=0;j<256;j++) + if(f1[i][j]!=f2[i][j]){ + printf("NO\n"); + return; + } + printf("YES\n"); +} + +int main(){ + int nn,i,j,k,n; + char src,dest; + + + scanf("%d",&nn); + while(nn--){ + memset(f1,0,sizeof(0)); + memset(f1,0,sizeof(0)); + scanf("%d\n",&n); + for(i=0;i +using namespace std; + +int grid[25][25]; +int N, M; + +void proc(int i, int j, int & val) { + if (i < N && j < M && i >= 0 && j >= 0 && grid[i][j]) { + grid[i][j] = 0; + val++; + proc(i+1, j, val); + proc(i+1, j+1, val); + proc(i, j+1, val); + proc(i-1, j+1, val); + proc(i-1, j, val); + proc(i-1, j-1, val); + proc(i, j-1, val); + proc(i+1, j-1, val); + } +} + +int main() { + int T; + cin >> T; + string line; + getline(cin, line); + getline(cin, line); + while (T--) { + N = 0, M = 0; + while (getline(cin, line) && line != "") { + M = line.size(); + N++; + for (int i = 0; i < M; i++) { + grid[N-1][i] = line[i] - '0'; + } + } + int maxi = 0; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + int val = 0; + proc(i, j, val); + if (val > maxi) maxi = val; + } + } + printf("%d\n", maxi); + if (T) printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/871/871-21.cpp b/uva_cpp_clean/871/871-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f90dcbb1c4238415ed07f1bf89e51c90dbb10d87 --- /dev/null +++ b/uva_cpp_clean/871/871-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 871 + Name: Counting Cells in a Blob + Problem: https://onlinejudge.org/external/8/871.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +char M[30][30], h, w; +int dfs(int y, int x) { + int result = M[y][x] - '0'; + if (!result) return 0; + + M[y][x] = '0'; + for (int i=-1; i<2; ++i) + for (int j=-1; j<2; ++j) + if (i || j) + if (y+i>=0 && y+i=0 && x+j> T; + cin.getline(M[0], 30); + cin.getline(M[0], 30); + + while (T--) { + for (h=w=0; cin.getline(M[h], 30) && M[h][0]; ++h); + + int mx = 0; + w = strlen(M[0]); + for (int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define pi 2*acos(0.0) +#define all(v) v.begin(),v.end() + +//input +#define si(t) scanf("%d",&t) +#define sl(t) scanf("%lld",&t) +#define sf(t) scanf("%f",&t) +#define sd(t) scanf("%lf",&t) +#define sc(c) scanf("%c",&c) +#define sii(a,b) scanf("%d%d",&a,&b) +#define sll(a,b) scanf("%lld%lld",&a,&b) + +//Output +#define P(a) printf("%d\n",a) +#define PL(a) printf("%lld\n",a) +#define PF(a) printf("%f\n",a) +#define PD(a) printf("%lf\n",a) +#define PS(a) printf("%s\n",a) +#define PSN(a) printf("%s ",a) +#define PN(a) printf("%d ",a) +#define PLN(a) printf("%lld ",a) +#define PFN(a) printf("%f ",a) +#define PDN(a) printf("%lf ",a) +#define PP(a,b) printf("%d %d\n",a,b) +#define PPN(a,b) printf("%d %d ",a,b) +#define PPL(a,b) printf("%lld %lld\n",a,b) +#define PPLN(a,b) printf("%lld %lld ",a,b) + +#define CP(a) cout< vi; +typedef vector vll; +typedef vector vs; +typedef set si; +typedef set ss; +typedef map mii; +typedef map mll; +typedef map msi; +typedef map mci; + +template string toString( T Number ){stringstream st;st << Number;return st.str();} +template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} + +//For Define +#define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) +#define for0(i,n) forab(i,0,(n)-1) +#define for1(i,n) forab(i,1,n) +#define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) +#define rfor0(i,n) rforba(i,(n)-1,0) +#define rfor1(i,n) rforba(i,n,1) +#define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) + +//Debug +#define dbg(x) cout << #x << " -> " << (x) << endl; +#define dbgsarr(i,a) cout<<#a<<"["< "< "< "<>p ; return p;} + +ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } +ll modInverse(ll a, ll m){return pow(a,m-2,m);} + +////============ CONSTANT ===============//// +#define mx7 10000007 +#define mx6 1000006 +#define mx5 100005 +#define inf 1<<30 //infinity value +#define eps 1e-9 +#define mx (100010) +#define mod 1000000007 +////=====================================//// + +int cnt,a[30][30],r,c; + +void dfs(int i,int j) +{ + if(i>=r || i<0 || j<0 || j>=c || a[i][j]==0) return; + cnt++; + a[i][j]=0; + + dfs(i,j+1); + dfs(i,j-1); + dfs(i+1,j); + dfs(i-1,j); + dfs(i+1,j+1); + dfs(i+1,j-1); + dfs(i-1,j+1); + dfs(i-1,j-1); +} + +int main() +{ + int t; + si(t); + getchar(); + getchar(); + while(t--) + { + int i=0,n; + string s; + + while(getline(cin,s) && sz(s)) + { + n=sz(s); + if(i==0) + { + c=n; + } + for0(j,n) + { + a[i][j]=s[j]-'0'; + } + i++; + } + r=i; + + int res=0; + for0(i,r) + { + for0(j,c) + { + cnt=0; + if(a[i][j]) + dfs(i,j); + res=max(res,cnt); + } + } + P(res); + if(t) + puts(""); + cover(a,0); + } + + return 0; +} + diff --git a/uva_cpp_clean/871/871-9.cpp b/uva_cpp_clean/871/871-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5b24c12197da0b205bebde150a44a0dd8e3373a --- /dev/null +++ b/uva_cpp_clean/871/871-9.cpp @@ -0,0 +1,48 @@ +#include + +#define SIZE 30 + +using namespace std; + +int tc, N, M, ans; +char line[SIZE], grid[SIZE][SIZE]; +int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; +int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; + +inline bool is_possible(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M ); +} + +int floodfill(int r, int c){ + if( not is_possible(r, c) ) return 0; + if( grid[r][c] != '1' ) return 0; + grid[r][c] = '.'; + int ct = 1; + for(int d = 0; d < 8; d++) + ct += floodfill(r + dr[d], c + dc[d]); + return ct; +} + +void solve(){ + ans = 0; + for(int row = 0; row < N; row++) + for(int col = 0; col < M; col++) + if( grid[row][col] == '1' ) + ans = max(ans, floodfill(row, col)); + printf("%d\n", ans); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + N = 0; + while(cin.getline(line, SIZE)){ + if( sscanf(line, "%s", grid[N]) == -1 ) break; + N++; + } + M = strlen(grid[0]); + solve(); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/872/872-21.cpp b/uva_cpp_clean/872/872-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0538323b8cbac516e2e89ee439a719c92e5ab7a --- /dev/null +++ b/uva_cpp_clean/872/872-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 872 + Name: Ordering + Problem: https://onlinejudge.org/external/8/872.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +char chars[30], sels[30], seen[30]; +int n, adj[27][27], ids[127]; +vector result; + +void dfs(int u, int ind) { + if (ind == n-1) { + char w[] = " "; + for (int i=0; i>T; + cin.getline(l1, 100); + while (T--) { + cin.getline(l1, 100); + cin.getline(l1, 100); stringstream sin1(l1); + cin.getline(l2, 100); stringstream sin2(l2); + + for (n=1; sin1>>chars[n]; n++) + ids[chars[n]] = n; + + memset(adj, 0, sizeof(adj)); + while (sin2>>rel) + adj[ids[rel[0]]][ids[rel[2]]]=1; + + result.clear(); + dfs(0, 0); + if (result.size()) { + sort(result.begin(), result.end()); + for (string s: result) + cout << s << endl; + } + else puts("NO"); + if (T) cout << endl; + } +} diff --git a/uva_cpp_clean/872/872-6.cc b/uva_cpp_clean/872/872-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..943cf69ea1453e0133feb628a4969a8e0108db6e --- /dev/null +++ b/uva_cpp_clean/872/872-6.cc @@ -0,0 +1,62 @@ +// https://uva.onlinejudge.org/external/8/872.pdf +#include +using namespace std; +using vi=vector; +using vvi=vector; +using qi=queue; +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + int K; + cin>>K; + string s; + getline(cin,s); + for(int T=0;T>x){ + m[x]=n++; + c.push_back(x); + } + sort(c.begin(),c.end()); + for(int i=0;i>y){ + int u=m[y[0]],v=m[y[2]]; + g[u].push_back(v); + a[v]++; + } + string t; + vi r(n); + int o=0; + functionf=[&](int u){ + r[u]=1; + t.push_back(c[u]); + if(t.size()==n){ + o=1; + for(int i=0;i + +using namespace std; + +const int MAX_V = 50; + +int tc; +string line, blank, x, y, z; +stringstream ss; +vector v; +vector ans; +map mp; +bool pos[MAX_V][MAX_V], vis[MAX_V], found; + +bool check(int id){ + for(int i = 0; i < ans.size(); i++) + if( not pos[ans[i]][id] ) return false; + return true; +} + +void backtrack(){ + if( ans.size() == v.size() ){ + for(int i = 0; i < ans.size(); i++){ + i == 0 ? cout << "" : cout << " "; + cout << v[ans[i]]; + } + cout << endl; + found = true; + return; + } + for(int i = 0; i < v.size(); i++) + if( not vis[i] and check(i) ){ + vis[i] = true; + ans.push_back(i); + backtrack(); + vis[i] = false; + ans.pop_back(); + } +} + +void solve(){ + backtrack(); + if( not found ) puts("NO"); + if( tc ) puts(""); +} + +void read(){ + memset(pos, true, sizeof pos); + getline(cin, blank); + getline(cin, line); + ss.clear(); + ss << line; + while( ss >> x ) v.push_back(x); + sort(v.begin(), v.end()); + for(int i = 0; i < v.size(); i++) mp[v[i]] = i; + getline(cin, line); + ss.clear(); + ss << line; + while( ss >> z){ + x = z.substr(0, z.find("<")); + y = z.substr(z.find("<") + 1); + int u = mp[x], v = mp[y]; + pos[v][u] = false; + } +} + +void clear(){ + v.clear(); + mp.clear(); + found = false; +} + +int main(){ + cin >> tc; + cin.ignore(); + while(tc--){ + read(); + solve(); + clear(); + } + return(0); +} diff --git a/uva_cpp_clean/880/880-19.cpp b/uva_cpp_clean/880/880-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abae57c5c08a31fb6d996b32b9496e3e2d274b30 --- /dev/null +++ b/uva_cpp_clean/880/880-19.cpp @@ -0,0 +1,14 @@ +#include +#include +using namespace std; + +int main() { + unsigned long long n; + while (cin >> n) { + unsigned long long den = (-1 + floor(sqrt(1+8*n)))/2; + unsigned long long num = n - (den*(den+1)/2); + if (num == 0) printf("1/%llu\n", den); + else printf("%llu/%llu\n", den + 2 - num , num); + } + return 0; +} diff --git a/uva_cpp_clean/880/880-21.cpp b/uva_cpp_clean/880/880-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3bea597ab7a4934e94669175112c7fe2de3e2d3 --- /dev/null +++ b/uva_cpp_clean/880/880-21.cpp @@ -0,0 +1,26 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 880 + Name: Cantor Fractions + Problem: https://onlinejudge.org/external/8/880.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long n; + while (cin >> n) { n--; + long long y = (sqrt(8*n+1)-1)/2, + d = n - y*(y+1)/2; + cout << (y + 1 - d) << '/' << (1 + d) << endl; + } +} diff --git a/uva_cpp_clean/884/884-18.cpp b/uva_cpp_clean/884/884-18.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1cecc3c555ba2a87ddd0bbd4486baf855434f541 --- /dev/null +++ b/uva_cpp_clean/884/884-18.cpp @@ -0,0 +1,55 @@ +#include +#define pb push_back +#define pf push_front +#define pob pop_back +#define fp first +#define sp second +#define mp make_pair +#define ins insert +#define uEdge(u, v) g[u].pb(v), g[v].pb(u) +#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w}) +#define dEdge(u, v) g[u].pb(v) +#define dwEdge(u, v, w) g[u].pb({v, w}) +#define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define minHeap int, vector, greater + +using namespace std; +typedef long long int lli; +typedef pair pii; +vector g[0]; +int MAXN = 1000001, spf[1000001]; + +lli gcd(lli a, lli b){ + if(b == 0) return a; + a%=b; + return gcd(b, a); +} + +void smallPF(){ //O(nlog(n)*log(n)) + spf[1] = 1; + for(int i = 2; i < MAXN; i++) spf[i] = i; + for(int i = 4; i < MAXN; i+=2) spf[i] = 2; + for(int i = 3; i * i < MAXN; i++){ + if(spf[i] == i){ + for(int j = i * i; j < MAXN; j+=i) if(spf[j] == j) spf[j] = i; + } + } +} + +int fact(int num){ //O(log(n)) + int ans = 0; + while(num != 1){ + ans++; + num/=spf[num]; + } + return ans; +} + +int main(){ + smallPF(); + int n, ans[MAXN]; + ans[1] = 0; + for(int i = 2; i < MAXN; i++) ans[i] = ans[i - 1] + fact(i); + while(scanf("%d", &n) != EOF) printf("%d\n", ans[n]); + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/884/884-21.cpp b/uva_cpp_clean/884/884-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a08774abc25ce28233771f4d572275abbeb83334 --- /dev/null +++ b/uva_cpp_clean/884/884-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 884 + Name: Factorial Factors + Problem: https://onlinejudge.org/external/8/884.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +const int MAXP = 1000003; +int C[MAXP]; +int divcount(int n, int p) { + int cnt = 0; + while (n%p == 0) { + n /= p; + ++cnt; + } + return cnt; +} + +void sieve() { + for (int i=2; i> n) + cout << C[n] << endl; +} diff --git a/uva_cpp_clean/886/886-9.cpp b/uva_cpp_clean/886/886-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97bdfc0cabee8c6aaaff1381a8adae9dad81c278 --- /dev/null +++ b/uva_cpp_clean/886/886-9.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +string s; +vector name; +vector number; + +void solve () { + do { + bool match = false; + for (int i = 0; i < number.size() and not match; i++) + if (number[i] == s) cout << s << endl, match = true; + if (match) continue; + vector ans; + for (int i = 0; i < name.size(); i++) + if (name[i].size() >= s.size() and name[i].substr(0, s.size()) == s) + ans.push_back(number[i]); + if (ans.empty()) cout << '0' << endl; + else { + cout << ans[0]; + for (int i = 1; i < ans.size(); i++) cout << ' ' << ans[i]; + cout << endl; + } + } while (cin >> s); +} + +char value (char ch) { + ch = toupper(ch); + if (ch <= 'C') return '2'; + if (ch <= 'F') return '3'; + if (ch <= 'I') return '4'; + if (ch <= 'L') return '5'; + if (ch <= 'O') return '6'; + if (ch <= 'S') return '7'; + if (ch <= 'V') return '8'; + return '9'; +} + +string convert (const char first_ch, const string x) { + string ret = ""; + ret += value(first_ch); + for (const char ch: x) ret += value(ch); + return ret; +} + +void readData () { + string line; + while (getline(cin, line)) { + vector word; + istringstream token(line); + while (token >> s) word.push_back(s); + if (word.size() == 1) return; + name.push_back(convert(word[0][0], word[1])); + number.push_back(word[2]); + } +} + +int main () { + readData(); + solve(); + return (0); +} diff --git a/uva_cpp_clean/891/891-21.cpp b/uva_cpp_clean/891/891-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0046417b08a3f589c62d6422e45700da98161cf1 --- /dev/null +++ b/uva_cpp_clean/891/891-21.cpp @@ -0,0 +1,123 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 891 + Name: Syntrax + Problem: https://onlinejudge.org/external/8/891.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +inline int readchar(bool skip=true) { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + int result = *p; + if (skip) ++p; + return result; +} +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch != '\n') + ch = readchar(); + return r; +} +inline int readLine(char *str) { + int len=0; + char ch; + while ((ch=readchar())!='\n' && ch!=EOF) + str[len++] = ch; + + str[len] = 0; + if (ch == EOF && !len) return EOF; + return len; +} +// ------------------------------------------------------------------------------------------ + +struct Triplet { + int w, h1, h2; + Triplet(){} + Triplet(int w, int h1, int h2):w(w),h1(h1),h2(h2){} +}; + +struct Token { + char ch; + int len; + Token(char ch, int len=0):ch(ch),len(len){} +}; + + +vector tokens; +int pos; +Triplet evaluate() { + Token &s = tokens[pos++]; + Triplet r1, r2; + switch (s.ch) { + case '"': + return Triplet(s.len + 6, 2, 1); + + case '(': + r1 = evaluate(); + r2 = evaluate(); + ++pos; + return Triplet(r1.w + r2.w, max(r1.h1, r2.h1), max(r1.h2, r2.h2)); + + case '{': + r1 = evaluate(); + r2 = evaluate(); + ++pos; + return Triplet(max(r1.w, r2.w) + 4, r1.h1, r1.h2 + 1 + r2.h1 + r2.h2); + + case '[': + r1 = evaluate(); + ++pos; + return Triplet(r1.w + 4, r1.h1 + 1, r1.h2); + } + throw "Syntax Error"; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char line[1000000]; + int T = readUInt(); + while (T--) { + int len = readLine(line); + tokens.clear(); + for (int i=0; i<=len; ++i) + if (line[i] == '"') { + int j = i+1; + while (line[j] != '"') ++j; + tokens.push_back(Token('"', j-i+1)); + i = j; + } + else if (line[i] != ' ' && line[i] != '\t') + tokens.push_back(Token(line[i])); + + pos = 0; + Triplet result = evaluate(); + cout << result.w+4 << 'x' << result.h1 + result.h2 << '\n'; + } +} diff --git a/uva_cpp_clean/893/893-19.cpp b/uva_cpp_clean/893/893-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7130b41ca05fab40263c6a9a474e1baf349175a8 --- /dev/null +++ b/uva_cpp_clean/893/893-19.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +long long dateToInt (long long m, long long d, long long y){ + return + 1461*(y+4800+(m-14)/12)/4+367*(m -2-(m-14)/12*12)/12- + 3*((y+4900+(m-14)/12)/100)/4+d-32075; +} + +void intToDate (long jd, long long &m, long long &d, long long &y){ + long long x, n, i, j; + x=jd+68569; n=4*x/146097;x-=(146097*n+3)/4; + i=(4000*(x+1))/1461001;x-=1461*i/4-31; j=80*x/2447; + d=x-2447*j/80;x=j/11; m=j+2-12*x; + y=100*(n-49)+i+x; +} + +int main() { + long long incr, d, m, y; + while (scanf("%lld %lld %lld %lld", &incr, &d, &m, &y) && !(incr == 0 && d == 0 && m == 0 && y == 0)) { + long long jd = dateToInt(m, d, y) + incr; + intToDate(jd, m, d, y); + printf("%lld %lld %lld\n", d, m, y); + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/893/893-6.cc b/uva_cpp_clean/893/893-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..ec4dc12b63991be8ba83c179b8a802bfb6a60f7c --- /dev/null +++ b/uva_cpp_clean/893/893-6.cc @@ -0,0 +1,63 @@ +// http://uva.onlinejudge.org/external/8/893.pdf +#include + +using namespace std; + +int isleap(int year) { + return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); +} + +int year2offset(int year) { + int sum = 0; + for (int i = 1998; i < year; i++) + sum += isleap(i) ? 366 : 365; + return sum; +} + +int month2offset(int year, int month) { + int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int sum = month > 2 && isleap(year) ? 1 : 0; + for (int i = 1; i < month; i++) sum += days[i-1]; + return sum; +} + +int day2offset(int day) { return day - 1; } + +int date2offset(int year, int month, int day) { + return year2offset(year) + month2offset(year, month) + day2offset(day); +} + +int offset2year(int offset) { + int year = 1998; + while (true) { + int days = isleap(year) ? 366 : 365; + if (offset < days) break; + offset -= days; + year++; + } + return year; +} + +int offset2month(int offset, int year) { + int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if (isleap(year)) days[1]++; + for (int i = 0; i < 12; i++) { + if (offset < days[i]) return i + 1; + offset -= days[i]; + } + return 12; +} + +int main() { + while (true) { + int delta, day, month, year; + cin >> delta >> day >> month >> year; + if (!day) break; + int offset = date2offset(year, month, day) + delta; + year = offset2year(offset); + offset -= year2offset(year); + month = offset2month(offset, year); + offset -= month2offset(year, month); + cout << offset + 1 << " " << month << " " << year << endl; + } +} diff --git a/uva_cpp_clean/893/main.cpp b/uva_cpp_clean/893/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f2360605f46dbd5da3e65509434c45771da7ab6 --- /dev/null +++ b/uva_cpp_clean/893/main.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +int main(){ + int remaining, day, month, year, passed; + while(cin >> remaining >> day >> month >> year && month != 0 && day != 0){ + while(remaining > 0){ + switch (month) { + case 1: + case 3: + case 5: + case 7: + case 8: + case 10: + case 12: + passed = 32 - day; + break; + case 2: + if(year%400 == 0 || (year % 4 == 0 && year % 100 != 0)) passed = 30 - day; + else passed = 29 - day; + break; + default: passed = 31 - day; + } + if(passed <= remaining){ + day = 1; + month += 1; + remaining -= passed; + }else{ + day += remaining; + remaining = 0; + } + if(month == 13){ + month = 1; + year+=1; + } + } + printf("%d %d %d\n", day, month, year); + } + + +return 0; +} diff --git a/uva_cpp_clean/895/895-21.cpp b/uva_cpp_clean/895/895-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25d0861aff27593cafe1a98362543c0de5bbd38f --- /dev/null +++ b/uva_cpp_clean/895/895-21.cpp @@ -0,0 +1,46 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 895 + Name: Word Problem + Problem: https://onlinejudge.org/external/8/895.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string str; + vector words; + while (cin >> str && str!="#") + if (str.length() <= 7) { + sort(str.begin(), str.end()); + words.push_back(str); + } + + cin.ignore(100, '\n'); + while (getline(cin, str) && str[0]!='#') { + sort(str.begin(), str.end()); + str = str.substr(str.find_first_not_of(" \t"), str.length()); + + int cnt = 0; + for (string &w: words) { + int i = 0; + for (char ch: w) { + while (ch!=str[i] && i + +using namespace std; + +typedef long long ll; + +vector get_primes(int n){ + vector prime(n + 1, 1); + vector primes; + prime[ 0 ] = prime[ 1 ] = 0; + if(n >= 2){ + primes.push_back(2); + for(int i = 4 ; i <= n ; i += 2) + prime[ i ] = 0; + } + for(int i = 3 ; i <= n ; i += 2) + if(prime[ i ]){ + primes.push_back(i); + for(ll j = (ll)i * i ; j <= n ; j += 2 * i) + prime[ j ] = 0; + } + return prime; +} + +set get_anagrammatic_primes(const vector &prime){ + set anagrammatic_primes; + vector visited(prime.size()); + for(int i = 2 ; i < prime.size() ; ++i){ + if(prime[ i ] && !visited[ i ]){ + int p = i; + bool ok = true; + string s = to_string(p); + sort(s.begin(), s.end()); + vector seq; + do{ + p = atoi(s.c_str()); + if(p < prime.size()){ + seq.push_back(p); + visited[ p ] = 1; + if(!prime[ p ]) + ok = false; + } + }while(next_permutation(s.begin(), s.end())); + if(ok){ + for(auto p : seq) + anagrammatic_primes.insert(p); + } + } + } + return anagrammatic_primes; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int N = 10000000; + const vector prime = get_primes(N); + const set anagrammatic_primes = get_anagrammatic_primes(prime); + for(int n ; cin >> n && n ; ){ + auto it = anagrammatic_primes.lower_bound(n + 1); + cout << (it == anagrammatic_primes.end() || *it >= (int)pow(10, (int)log10(n) + 1) ? 0 : *it) << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/897/897-9.cpp b/uva_cpp_clean/897/897-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59bd503d43b1908b21765b32100987108fde0163 --- /dev/null +++ b/uva_cpp_clean/897/897-9.cpp @@ -0,0 +1,46 @@ +#include + +#define SIZE 10000000 + +using namespace std; + +bool is_prime[SIZE], ok; +vectorprime; +setanagrammatic_prime; +string s, ans; +int n; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1++) + if(is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } +} + +int main(){ + sieve(); + for(int it = 0; it < prime.size(); it++){ + s = to_string(prime[it]); + vectorv; + for(int i=0;i + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, n, dp[51] = {0, 1, 2}; + + for (i = 3; i < 51; i++) + dp[i] = dp[i - 1] + dp[i - 2]; // i - 1 加一塊直的或 i - 2 加一塊橫的 + + while (cin >> n && n) + cout << dp[n] << "\n"; + + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/900/900-19.cpp b/uva_cpp_clean/900/900-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fd0ba566b88ca9376ff8f48fc4edbf6de34d5b6 --- /dev/null +++ b/uva_cpp_clean/900/900-19.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +long long fib[51]; + +int main() { + fib[1] = 1, fib[2] = 2; + for (int i = 3; i < 51; i++) fib[i] = fib[i-1] + fib[i-2]; + int l; + while (cin >> l && l != 0) { + printf("%lld\n", fib[l]); + } + return 0; +} diff --git a/uva_cpp_clean/900/900-21.cpp b/uva_cpp_clean/900/900-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62ef1e386111de9316069eafcd49962f54a34291 --- /dev/null +++ b/uva_cpp_clean/900/900-21.cpp @@ -0,0 +1,25 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 900 + Name: Brick Wall Patterns + Problem: https://onlinejudge.org/external/9/900.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main() { + long long int p[51] = { 1, 1}; + for (int i = 2; i < 51; i++) + p[i] = p[i - 1] + p[i - 2]; + + int n; + while (cin >> n && n) + cout << p[n] << endl; +} \ No newline at end of file diff --git a/uva_cpp_clean/900/900-5.cpp b/uva_cpp_clean/900/900-5.cpp new file mode 100755 index 0000000000000000000000000000000000000000..babd04063da7c929de6dd397361fc9d768acab81 --- /dev/null +++ b/uva_cpp_clean/900/900-5.cpp @@ -0,0 +1,16 @@ +#include +int main() +{ + long long a[60],i,n; + while(scanf("%lld",&n)==1) + { + if(n==0) + break; + a[0]=0; + a[1]=1; + for(i=2;i<=n+1;i++) + a[i]=a[i-1]+a[i-2]; + printf("%lld\n",a[n+1]); + } + return 0; +} diff --git a/uva_cpp_clean/900/900-9.cpp b/uva_cpp_clean/900/900-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a2411524f156567642af01f07bfaacce25c4e22 --- /dev/null +++ b/uva_cpp_clean/900/900-9.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int n; +vector fib(51); + +int main(){ + fib[0] = fib[1] = 1; + for(int i = 2; i <= 50; i++) + fib[i] = fib[i - 1] + fib[i - 2]; + while(scanf("%d", &n), n){ + printf("%ld\n", fib[n]); + } + return(0); +} diff --git a/uva_cpp_clean/902/902-13.cpp b/uva_cpp_clean/902/902-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77e6fcdaa2868fce84cea98f2c02ed20a0a62289 --- /dev/null +++ b/uva_cpp_clean/902/902-13.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +using namespace std; + +map list; + +int main() +{ + int n, size, m = 0; + + string s = "", sub = "", result = ""; + + while (cin >> n >> s) + { + if (n >= s.length()) + { + cout << s << endl; + } + + else + { + size = s.length() - n; + + for (int i = 0; i <= size; i++) + { + sub = s.substr(i, n); + + list[sub]++; + + if (m < list[sub]) + { + m = list[sub]; + + result = sub; + } + } + + cout << result << endl; + } + + sub = ""; + + result = ""; + + m = size = 0; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/902/902-19.cpp b/uva_cpp_clean/902/902-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3c8f0c82abaa17fba890c355c93d6e949a1b602 --- /dev/null +++ b/uva_cpp_clean/902/902-19.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + int n; + string msg; + unordered_map freq; + while (cin >> n >> msg) { + freq.clear(); + int max = 0; + string pass = ""; + for (int ini = 0; ini + n <= msg.length(); ini++) { + string sub = msg.substr(ini, n); + if (!freq.count(sub)) freq.insert(pair(sub, 0)); + freq[sub]++; + if (freq[sub] > max) max = freq[sub], pass = sub; + } + cout << pass << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/902/902-21.cpp b/uva_cpp_clean/902/902-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41b8927e69702e4f7d956cb7eed57a4d006a0d05 --- /dev/null +++ b/uva_cpp_clean/902/902-21.cpp @@ -0,0 +1,64 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 902 + Name: Password Search + Problem: https://onlinejudge.org/external/9/902.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int diffs = 0; +struct Node { + int cnt, cse; + Node* adj[26]; + Node(): cse(0),cnt(0) { + memset(adj, 0, sizeof(adj)); + } + ~Node() { + for (int i=0; i<26; i++) + if (adj[i]) + delete adj[i]; + } +}; + +char line[1000000]; +int main(){ + int n; + Node *root = new Node(); + for (int cse=1; scanf("%d%s", &n, line)==2; cse++) { + int mxc=0, mxi=0; + bool sht = 0; + for (int s=0; line[s+n-1]; s++) { + Node *p = root; + for (int i=s; iadj[ch]) + p->adj[ch] = new Node(); + p = p->adj[ch]; + } + if (sht) break; + + if (p->cse != cse) { + p->cse = cse; + p->cnt = 1; + } + else if (++p->cnt > mxc) { + mxc = p->cnt; + mxi = s; + } + } + + line[mxi+n] = 0; + puts(line + mxi); + } +} diff --git a/uva_cpp_clean/904/904-21.cpp b/uva_cpp_clean/904/904-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42f5407143c2d7743a990657572b9bfe50166441 --- /dev/null +++ b/uva_cpp_clean/904/904-21.cpp @@ -0,0 +1,85 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 904 + Name: Overlapping Air Traffic Control Zones + Problem: https://onlinejudge.org/external/9/904.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct Vol { + int p1[3], p2[3]; + int volume() const { + int r = 1; + for (int i=0; i<3; ++i) + r *= p2[i]-p1[i]; + return r; + } + + bool operator == (const Vol &o) const { + for (int i=0; i<3; ++i) + if (p1[i]!=o.p1[i] || p2[i]!=o.p2[i]) + return 0; + return 1; + } +} +vt[17], V[114]; + +bool intersect(const Vol &v1, const Vol &v2, Vol &vr) { + for (int i=0; i<3; ++i) { + vr.p1[i] = max(v1.p1[i], v2.p1[i]); + vr.p2[i] = min(v1.p2[i], v2.p2[i]); + if (vr.p1[i] >= vr.p2[i]) + return 0; + } + return 1; +} + + +int sz; +int dfs(int si, int cnt, const Vol& v) { + Vol vi; + int result = (cnt&1 ? 1 : -1) * v.volume(); + for (int i=si; i> n) { + sz = 0; + for (int i=0; i> vt[i].p1[0] >> vt[i].p1[1] >> vt[i].p1[2] + >> vt[i].p2[0] >> vt[i].p2[1] >> vt[i].p2[2]; + + for (int j=0; j>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 906 + Name: Rational Neighbor + Problem: https://onlinejudge.org/external/9/906.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int gcd(int a, int b) { + return !b ? a : gcd(b, a%b); +} + +int main(){ + int a, b; + double n; + + while (scanf("%d%d%lf", &a, &b, &n)==3) { + int gg = gcd(a, b); a/=gg; b/=gg; + + double bna = b*n+a; + for (int d=1; ; d++) { + int c = bna*d / b; + if (c*b > a*d) { + printf("%d %d\n", c, d); + break; + } + } + } +} diff --git a/uva_cpp_clean/906/906-9.cpp b/uva_cpp_clean/906/906-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdb05a1c0a779064cfb7938aa44c899910d85ffe --- /dev/null +++ b/uva_cpp_clean/906/906-9.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +long long a, b, x, y, num, den, foo; +double n, pib, prec; +bool ok; + +int main(){ + while(scanf("%lld %lld %lf", &a, &b, &n) == 3){ + pib = (double)a / b; + ok = true; + for(long long y = 1; ok; y++){ + x = pib * y - 2; + while(x * b <= a * y) + x++; + prec = (double)x / y - pib; + if(prec - n < 1e-15){ + printf("%lld %lld\n", x, y); + ok = false; + break; + } + } + } + return(0); +} diff --git a/uva_cpp_clean/907/907-21.cpp b/uva_cpp_clean/907/907-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7d83126b7674d8c626c424ecd844fb37103c53b --- /dev/null +++ b/uva_cpp_clean/907/907-21.cpp @@ -0,0 +1,52 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 907 + Name: Winterim Backpacking Trip + Problem: https://onlinejudge.org/external/9/907.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int X[616], n, k; +bool check(int mx) { + int sum=0, cnt=1; + for (int i=0; i mx) { + sum = X[i]; + if (++cnt > k) + return false; + } + } + return true; +} + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + while (cin >> n >> k) { + ++n; ++k; + int lo=0, hi=0; + for (int i=0; i> X[i]; + if (X[i] > lo) + lo = X[i]; + hi += X[i]; + } + + while (lo <= hi) { + int mid = (lo + hi) >> 1; + if (check(mid)) + hi = mid-1; + else + lo = mid+1; + } + cout << hi+1 << endl; + } +} diff --git a/uva_cpp_clean/908/908-10.cpp b/uva_cpp_clean/908/908-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..387f1c144d9e2c5687406df0f02739eb17f7b216 --- /dev/null +++ b/uva_cpp_clean/908/908-10.cpp @@ -0,0 +1,121 @@ +/// by:Ahmed Sakr (sakr_) with AZA /// +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +const int N= (int)1e6+5; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + + +struct DSU { + int par[N], sz[N]; + void init(int n) { + for (int i = 0; i < n; i++) par[i] = i; + for (int i = 0; i < n; i++) sz[i] = 1; + } + int find (int u) { + if (par[u] == u) return u; + return par[u] = find(par[u]); + } + void merge(int a, int b) { + a = find(a); + b = find(b); + if (a == b) return; + if (sz[a] < sz[b]) swap(a, b); + par[b] = a; + sz[a] += sz[b]; + } + int count(int u) { + return sz[find(u)]; + } +}; + +// {weight, a, b} +int minimumSpanningTree(vector> &edges) { + sort(edges.begin(), edges.end()); + DSU dsu; + dsu.init(N); + int ans = 0,a,b,weight; + for (auto e: edges) { + + tie(weight,a,b) = e; + + + if (dsu.find(a) != dsu.find(b)) { + dsu.merge(a, b); + ans+=weight; + } + } + return ans; +} + + +int main() { + FAST + + int n,m,w,a,b; + int ans=0; + + bool end = 0; + + while (cin>>n) { + + if(end) cout<<"\n"; + + ans=0; + vector> arr; + for(int i=1;i>a>>b>>w; + ans+=w; + } + + cout<>k; + + for(int i=0;i>a>>b>>w; + arr.push_back({w,a,b}); + } + + int m; cin>>m; + + for(int i=0;i>a>>b>>w; + arr.push_back({w,a,b}); + } + + auto res = minimumSpanningTree(arr); + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0) +#define SET(a) memset(a,-1,sizeof(a)) +#define pii pair +#define pll pair +#define debug printf("#########\n") +#define nl printf("\n") +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pb push_back +#define MAX 100000 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll gcd(ll a, ll b) { + if (a % b == 0) return b; + return gcd(b, a % b); +} +ll lcm(ll a, ll b) { + return a * b / gcd(a, b); +} +/************************************ Code Start Here ******************************************************/ +ll par[MAX]; +struct edge { + ll u, v, w; + bool operator<(const edge &p)const { + return w < p.w; + } +}; +vectore; +//~ vectorEd; +ll find(ll r) { + if (par[r] == r) return r ; + else return par[r] = find(par[r]); +} +ll MST(ll n) { + sort(e.begin(), e.end()); + for (int i = 0; i <= n; i++) { + par[i] = i; + } + ll cnt = 0, s = 0, sz = e.size(); + for (int i = 0; i < sz; i++) { + ll u = find(e[i].u); + ll v = find(e[i].v); + if (u != v) { + par[u] = v; + cnt++; + s += e[i].w; + if (cnt == n) break; + } + } + return s; +} +void Clear() { + for (int i = 0; i < MAX; i++) { + par[i] = 0; + } + e.clear(); +} +int main () { + ll N, t = 1; + while (scanf("%lld", &N) == 1) { + for (int i = 1 ; i < N; i++) { + ll u, v, w; + scanf("%lld %lld %lld", &u, &v, &w); + edge get; + get.u = u; + get.v = v; + get.w = w; + e.push_back(get); + } + ll mx1 = MST(N); + ll n, nn; + scanf("%lld", &n); + for (int i = 1; i <= n; i++) { + ll u, v, w; + scanf("%lld %lld %lld", &u, &v, &w); + edge get; + get.u = u; + get.v = v; + get.w = w; + e.push_back(get); + } + scanf("%lld", &nn); + for (int i = 1; i <= nn; i++) { + ll u, v, w; + scanf("%lld %lld %lld", &u, &v, &w); + edge get; + get.u = u; + get.v = v; + get.w = w; + e.push_back(get); + } + ll mx2 = MST(N); + if(t!=1) nl; + printf("%lld\n%lld\n", mx1, mx2); + t++; + Clear(); + } + return 0; +} diff --git a/uva_cpp_clean/908/908-19.cpp b/uva_cpp_clean/908/908-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47b5b4a7c8ade7182de3ea84fb7258dca8305b45 --- /dev/null +++ b/uva_cpp_clean/908/908-19.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +using namespace std; + +typedef vector vi; +typedef pair ii; +typedef pair iii; + +struct ufds { + vi p, r; int numSets; + void clear(int N) { + p.assign(N, 0); r.assign(N, 0); numSets = N; + for (int i = 0; i < N; i++) p[i] = i; + } + int findSet(int i) { return (p[i]==i)? i: (p[i]=findSet(p[i])); } + bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } + void unionSet(int i, int j) { + int x = findSet(i), y = findSet(j); + if (x != y) { + if (r[x] > r[y]) p[y] = x; + else { + p[x] = y; + if (r[x] == r[y]) r[y]++; + } numSets--; + } + } +}; + +ufds ds; + +int main() { + int N; + bool first = false; + while (cin >> N) { + if (first) printf("\n"); + else first = true; + int ccost = 0; + for (int i = 0; i < N - 1; i++) { + int s, d, c; + cin >> s >> d >> c; + ccost += c; + } + priority_queue pq; + ds.clear(N); + int K; + cin >> K; + for (int i = 0; i < K; i++) { + int s, d, c; + cin >> s >> d >> c; + pq.push(iii(-c, ii(s-1, d-1))); + } + int M; + cin >> M; + for (int i = 0; i < M; i++) { + int s, d, c; + cin >> s >> d >> c; + pq.push(iii(-c, ii(s-1, d-1))); + } + int ncost = 0; + while (!pq.empty() && ds.numSets > 1) { + iii line = pq.top(); pq.pop(); + if (!ds.isSameSet(line.second.first, line.second.second)) { + ds.unionSet(line.second.first, line.second.second); + ncost -= line.first; + } + } + printf("%d\n%d\n", ccost, ncost); + } + return 0; +} diff --git a/uva_cpp_clean/908/908-21.cpp b/uva_cpp_clean/908/908-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..557049155f3eff3014c662cf9d5de7a2465810ae --- /dev/null +++ b/uva_cpp_clean/908/908-21.cpp @@ -0,0 +1,114 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 908 + Name: Re-connecting Computer Sites + Problem: https://onlinejudge.org/external/9/908.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXN 1000017 +unordered_map M[MAXN]; +list adj[MAXN]; +int parent[MAXN], depth[MAXN]; +void reparent(int u, int d) { + depth[u] = d; + for (int v: adj[u]) + if (v != parent[u]) { + parent[v] = u; + reparent(v, d+1); + } +} + +int reconnect(int u, int v, int w) { + int a=u, b=v, tw, + bw=w, bd, bi, bj; + + while (a != b) { + if (depth[a] >= depth[b]) { + tw = M[a][parent[a]]; + if (tw > bw) { + bd = 1; + bw = tw; + bi = a; + bj = parent[a]; + } + a = parent[a]; + } + + if (depth[b] > depth[a]) { + tw = M[b][parent[b]]; + if (tw > bw) { + bd = 2; + bw = tw; + bi = b; + bj = parent[b]; + } + b = parent[b]; + } + } + + if (bw > w) { + adj[bi].remove(bj); + adj[bj].remove(bi); + adj[u].push_back(v); + adj[v].push_back(u); + M[u][v] = M[v][u] = w; + if (bd == 1) { + parent[u] = v; + reparent(u, depth[v]+1); + } + else { + parent[v] = u; + reparent(v, depth[u]+1); + } + + return bw-w; + } + return 0; +} + +int main(){ + bool frst = 1; + int n, m, k, u, v, w; + while (scanf("%d", &n)==1) { + if (frst) frst = 0; + else putchar('\n'); + + for (int i=1; i<=n; i++) { + adj[i].clear(); + M[i].clear(); + } + + long long sum = 0; + for (int i=1; i + +using namespace std; + +struct UnionFind{ + vector path, rank, setSize; + + UnionFind(int N_){ + rank.assign(N_, 0); + path.assign(N_, 0); + for(int it = 0; it < N_; it++) path[it] = it; + }; + + int findSet(int idx){ + return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) ); + } + + bool isSameSet(int idx1, int idx2){ + return ( findSet(idx1) == findSet(idx2) ); + } + + void unionSet(int idx1, int idx2){ + if( isSameSet(idx1, idx2) ) return; + int x = findSet(idx1), y = findSet(idx2); + if( rank[x] > rank[y] ) path[y] = x; + else path[x] = y; + if( rank[x] == rank[y] ) rank[y]++; + } +}; + +typedef pair ii; + +int tc, N, K, M, s, a, b, w, original_cost; +vector < pair < int, ii > > EdgeList; + +int Kruskal(){ + UnionFind UF(N); + int new_cost = 0, setSize = 1; + sort(EdgeList.begin(), EdgeList.end()); + for(int it = 0; it < M + K; it++){ + pair front = EdgeList[it]; + if( not UF.isSameSet(front.second.first, front.second.second) ) + new_cost += front.first, setSize++; + UF.unionSet(front.second.first, front.second.second); + if( setSize == N ) break; + } + return new_cost; +} + +int main(){ + while(~scanf("%d", &N)){ + if(tc++) putchar('\n'); + original_cost = 0; + EdgeList.clear(); + for(int it = 0; it + 1 < N; it++){ + scanf("%*d %*d %d", &s); + original_cost += s; + } + scanf("%d", &K); + for(int it = 0; it < K; it++){ + scanf("%d %d %d", &a, &b, &w); + EdgeList.push_back( make_pair(w, ii(a, b) ) ); + } + scanf("%d", &M); + for(int it = 0; it < M; it++){ + scanf("%d %d %d", &a, &b, &w); + EdgeList.push_back( make_pair(w, ii(a, b) ) ); + } + printf("%d\n", original_cost); + printf("%d\n", Kruskal()); + } + return(0); +} diff --git a/uva_cpp_clean/910/910-21.cpp b/uva_cpp_clean/910/910-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1be4b14e08fe9983c3ed42fd26fef5138c4281d9 --- /dev/null +++ b/uva_cpp_clean/910/910-21.cpp @@ -0,0 +1,60 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 910 + Name: TV game + Problem: https://onlinejudge.org/external/9/910.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct State { + int u, d; + State(int u, int d):u(u),d(d){} +}; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int adj[43][2], S[43], C[43][43], n, m; + while (cin >> n) { + for (int i=0; i> c1 >> c2 >> c3 >> c4; + int u = c1-'A'; + adj[u][0] = c2 - 'A'; + adj[u][1] = c3 - 'A'; + S[u] = c4 == 'x'; + } + + cin >> m; + for (int i=0; i q; + q.push(State(0, 0)); + while (!q.empty()) { + State s = q.front(); q.pop(); + int c = C[s.u][s.d]; + if (s.d < m) + for (int i=0; i<2; ++i) { + int v = adj[s.u][i]; + if (!C[v][s.d+1]) + q.push(State(v, s.d+1)); + C[v][s.d+1] += c; + } + } + + int sum = 0; + for (int i=0; i +#include +#include +using namespace std; + +unordered_map > comb; +long long proc(int n, int m) { + if (m == 0 || m == n) return 1; + if (m < 0 || m > n) return 0; + if (comb[n][m] == 0) comb[n][m] = proc(n-1, m-1) + proc(n-1, m); + return comb[n][m]; +} + +int main() { + int N, K; + while (cin >> N) { + cin >> K; + long long coef = 1; + for (int i = 0; i < K; i++) { + int var; + cin >> var; + coef *= proc(N, var); + N -= var; + } + cout << coef << endl; + } + return 0; +} diff --git a/uva_cpp_clean/911/911-9.cpp b/uva_cpp_clean/911/911-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7c272c130a0ed1429111f749be961522d5988c1 --- /dev/null +++ b/uva_cpp_clean/911/911-9.cpp @@ -0,0 +1,14 @@ +import math +import functools + +def main(): + while True: + try: + n, k = int(input()), input() + z = list(map(int, input().split())) + except EOFError: + break + print( math.factorial(n) // functools.reduce(lambda a, b: a * math.factorial(b), z, 1) ) + +if __name__ == '__main__': + main() diff --git a/uva_cpp_clean/912/912-9.cpp b/uva_cpp_clean/912/912-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5967a2c366a2e11c438ab1e942faa380800c0d17 --- /dev/null +++ b/uva_cpp_clean/912/912-9.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int n; +bool first = true; +vector > a, b; +set V; +map color, vis; +map > G; + +void print () { + puts("YES"); + for (auto x: color) cout << x.first << ' ' << (char)x.second << endl; +} + +bool dfs (int u, int c) { + vis[u] = true; + color[u] = c; + bool ret = true; + for (auto v: G[u]) { + if (color.count(v) and color[v] != c) return false; + if (not vis.count(v)) ret |= dfs(v, c); + } + return ret; +} + +bool solve () { + for (int i = 0; i < n; i++) { + int u = a[i].first, v = b[i].first; + if (a[i].second + b[i].second == 0) { + if (a[i].first != b[i].first) return false; + } + else if (a[i].second and b[i].second) { + if (u == v) continue; + G[u].insert(v); + G[v].insert(u); + } + else if (a[i].second) { + if (color.count(u) and color[u] != v) return false; + color[u] = v; + } + else { + if (color.count(v) and color[v] != u) return false; + color[v] = u; + } + } + for (int u: V) + if (not vis.count(u) and color.count(u) and not dfs(u, color[u])) return false; + return true; +} + +void read (vector >& x) { + string line; + for (int i = 0; i < n; i++) { + getline(cin, line); + if (isdigit(line[0])) { + V.insert(stoi(line)); + x.push_back({stoi(line), true}); + } + else x.push_back({line[0], false}); + } +} + +void init () { + first = false; + a.clear(); + b.clear(); + V.clear(); + G.clear(); + vis.clear(); + color.clear(); +} + +int main () { + while (scanf("%d\n", &n) == 1) { + if (not first) puts(""); + init(); + read(a); + read(b); + if (not solve()) puts("NO"); + else print(); + } + return (0); +} diff --git a/uva_cpp_clean/913/913-15.cpp b/uva_cpp_clean/913/913-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b75fce5731014d96939f6f4885bf769f65c12b9 --- /dev/null +++ b/uva_cpp_clean/913/913-15.cpp @@ -0,0 +1,16 @@ +/* + 數學 + 時間複雜度: O(1) +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + unsigned long long n; + while (cin >> n) + cout << (3 * n * n + 3) / 2 + 3 * n - 9 << "\n"; // ((n + 1) / 2) * ((n + 1) / 2) * 6 - 9 +} \ No newline at end of file diff --git a/uva_cpp_clean/913/913-19.cpp b/uva_cpp_clean/913/913-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a033764d67ed232060156923f10f177e47089f73 --- /dev/null +++ b/uva_cpp_clean/913/913-19.cpp @@ -0,0 +1,12 @@ +#include +using namespace std; + +int main() { + unsigned long long N; + while (cin >> N) { + unsigned long long s = N/2; + s = (N*(N+1)/2) - (s*(s+1)); + printf("%llu\n", 3*(2*s - 3)); + } + return 0; +} diff --git a/uva_cpp_clean/913/913-21.cpp b/uva_cpp_clean/913/913-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3199dfdb847c5f511039ab7ed1aa5eb716a2208 --- /dev/null +++ b/uva_cpp_clean/913/913-21.cpp @@ -0,0 +1,24 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 913 + Name: Joana and the Odd Numbers + Problem: https://onlinejudge.org/external/9/913.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int main(){ + long long int n; + while(cin>>n) { + long long t = (n+1)*((n+1)>>1)-1; + cout << 3*t - 6 < +int main() +{ + long long n; + while (scanf("%lld",&n)==1) + printf("%lld\n",((n*(n+2))/2)*3-6); +return 0; +} diff --git a/uva_cpp_clean/914/914-8.cpp b/uva_cpp_clean/914/914-8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e1d1582d0bb5001b6b65927489eddb2f88079be --- /dev/null +++ b/uva_cpp_clean/914/914-8.cpp @@ -0,0 +1,70 @@ +// Problem name: Jumping Champion +// Problem link: https://vjudge.net/problem/UVA-914 +// Submission link: https://vjudge.net/solution/32926004 + +#include + +using namespace std; + +typedef long long ll; + +vector get_primes(int n){ + vector is_prime(n + 1, 1); + vector primes; + is_prime[ 0 ] = is_prime[ 1 ] = 0; + if(n >= 2){ + primes.push_back(2); + for(int i = 4 ; i <= n ; i += 2) + is_prime[ i ] = 0; + } + for(int i = 3 ; i <= n ; i += 2) + if(is_prime[ i ]){ + primes.push_back(i); + for(ll j = (ll)i * i ; j <= n ; j += 2 * i) + is_prime[ j ] = 0; + } + return primes; +} + +vector> get_frequencies(vector primes){ + const int MAX_DIFF = 114; // calculated after running get_primes first; + vector> freq(primes.size(), vector(MAX_DIFF, 0)); + for(int i = 1 ; i < primes.size() ; ++i){ + for(int j = 0 ; j < MAX_DIFF ; ++j) + freq[ i ][ j ] = freq[ i - 1 ][ j ]; + ++freq[ i ][ primes[ i ] - primes[ i - 1 ] - 1 ]; + } + return freq; +} + +int main(){ + ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); + + const int N = 1000000; + const vector primes = get_primes(N); + const vector> freq = get_frequencies(primes); + + int t; + for(cin >> t ; t-- ; ){ + int l, r; + cin >> l >> r; + int p1 = lower_bound(primes.begin(), primes.end(), l) - primes.begin(); + int p2 = upper_bound(primes.begin(), primes.end(), r) - primes.begin(); + int ans = 0, f_ans = 0; + bool unique = false; + if(p1 < p2){ + for(int i = 0, f ; i < freq[ p1 ].size() ; ++i){ + f = freq[ p2 - 1 ][ i ] - freq[ p1 ][ i ]; + if(f > f_ans) + ans = i + 1, f_ans = f, unique = true; + else if(f == f_ans) + unique = false; + } + } + if(unique) + cout << "The jumping champion is " << ans << endl; + else + cout << "No jumping champion" << endl; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/914/914-9.cpp b/uva_cpp_clean/914/914-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12712ca602530f22febd8df92b41103ec89d1111 --- /dev/null +++ b/uva_cpp_clean/914/914-9.cpp @@ -0,0 +1,42 @@ +#include + +#define SIZE 1000000 + +using namespace std; + +bool is_prime[SIZE]; +vectorprime; +int tc, L, U; + +void sieve(){ + memset(is_prime, true, sizeof is_prime); + is_prime[0] = is_prime[1] = false; + for(long long it1 = 2; it1 < SIZE; it1 ++){ + if(is_prime[it1]){ + for(long long it2 = it1 * it1; it2 < SIZE; it2 += it1) + is_prime[it2] = false; + prime.push_back(it1); + } + } +} + +int main(){ + sieve(); + scanf("%d",&tc); + while(tc--){ + scanf("%d %d",&L, &U); + int pos = lower_bound(prime.begin(), prime.end(), L) - prime.begin(); + map v; + int idx, _max = -1, ans = -1; + for(int it = pos + 1; it < prime.size() and prime[it] <= U; it++){ + idx = prime[it] - prime[it - 1]; + v[idx] += 1; + if(v[idx] == _max) ans = -1; + if(v[idx] > _max ) _max = v[idx], ans = idx; + } + if( ans != -1) + printf("The jumping champion is %d\n", ans); + else puts("No jumping champion"); + } + return(0); +} diff --git a/uva_cpp_clean/920/920-19.cpp b/uva_cpp_clean/920/920-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e45591dc03ad07f29a32cfc5d9f5131bfd6f5b0 --- /dev/null +++ b/uva_cpp_clean/920/920-19.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +using namespace std; + +typedef pair ff; + +int main() { + priority_queue pq; + int C; + cin >> C; + while (C--) { + int N; + cin >> N; + for (int i = 0; i < N; i++) { + ff p; + cin >> p.first >> p.second; + pq.push(p); + } + float dist = 0, maxi = 0; + ff last = pq.top(); + pq.pop(); + while (!pq.empty()) { + if (pq.top().second > maxi) { + float h = pq.top().second - maxi, H = pq.top().second - last.second, D = last.first - pq.top().first, d; + d = (D*h)/H; + dist += sqrt((h*h) + (d*d)); + maxi = pq.top().second; + } + last = pq.top(); + pq.pop(); + } + printf("%.2f\n", dist); + } + return 0; +} diff --git a/uva_cpp_clean/920/920-21.cpp b/uva_cpp_clean/920/920-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7139758cac925158d4adb11e587e6736f6fa2ac --- /dev/null +++ b/uva_cpp_clean/920/920-21.cpp @@ -0,0 +1,50 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 920 + Name: Sunny Mountains + Problem: https://onlinejudge.org/external/9/920.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct Point { + double x, y; + Point():x(0),y(0){} + Point(double x, double y):x(x),y(y){} + bool operator < (const Point &o) const { return x < o.x; } +}; +std::istream& operator>> (std::istream& stream, Point& p) { + stream >> p.x >> p.y; + return stream; +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n; + cin >> T; + Point P[143]; + while (T-- && cin >> n) { + for (int i=0; i> P[i]; + sort(P, P+n); + + double ly = 0, sum = 0; + for (int i=n-2; i>=0; i-=2) + if (ly<=P[i].y && ly>=P[i+1].y) { + double m = (P[i].y-P[i+1].y) / (P[i].x-P[i+1].x); + double x = (ly - P[i].y)/m + P[i].x; + double xx = x - P[i].x, yy = ly - P[i].y; + sum += sqrt(xx*xx + yy*yy); + ly = P[i].y; + } + + cout << fixed << setprecision(2) << sum << '\n'; + } +} diff --git a/uva_cpp_clean/921/921-21.cpp b/uva_cpp_clean/921/921-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2f439c02624402262fd1a6ca2fedd52b269ac96 --- /dev/null +++ b/uva_cpp_clean/921/921-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 921 + Name: A Word Puzzle in the Sunny Mountains + Problem: https://onlinejudge.org/external/9/921.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; +// +// +vector C[110]; +string D[120]; +int n, m, l; +char R[26]; +// +bool bt(int ind) { + if (ind == n) { + for (int i=0; i &v = C[ind]; + for (int i=0; i>T; + while (T--) { + int x; + cin>>l>>n; + for (int i=0; i>x && x) + C[i].push_back(x-1); + } + cin >> str; +// + for (m=0; cin>>D[m] && D[m][0]!='*'; ++m); + + memset(R, 0, l+1); + for (int i=0; str[i]; ++i) + R[C[0][i]] = str[i]; +// + if (bt(0)) + cout << R << endl; +// + else + cout << "IMPOSSIBLE\n"; + } +} diff --git a/uva_cpp_clean/924/924-14.cpp b/uva_cpp_clean/924/924-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f771976d070d60bf8edd118441f21ffa375ee90d --- /dev/null +++ b/uva_cpp_clean/924/924-14.cpp @@ -0,0 +1,99 @@ +/*************************************************** + * Problem name : 924 - Spreading The News.cpp + * OJ : Uva + * Result : Accepted + * Date : 17-03-17 + * Problem Type : Graph + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define pii pair +#define MAX 2505 +using namespace std; +typedef long long ll; +vectoradj[MAX]; +int cost[MAX], visit[MAX], flag, ans; +int BFS(int s) { + int mx = 0; + queueQ; + visit[s] = 1; + cost[s] = 0; + Q.push(s); + int count = 0; + while (!Q.empty()) { + int u = Q.front(); + Q.pop(); + if (cost[u] == flag) { + flag = cost[u]; + } else { + if (count > mx) { + mx = count; + ans = cost[u]; + } + count = 0; + flag++; + } + int sz = adj[u].size(); + for (int i = 0; i < sz; i++) { + int v = adj[u][i]; + if (!visit[v]) { + count++; + visit[v] = 1; + cost[v] = cost[u] + 1; + Q.push(v); + } + + } + } + return mx; +} +void mem() { + for (int i = 0; i < MAX; i++) { + visit[i] = 0; + } +} +int main () { + int E; + while (scanf("%d", &E) == 1) { + for (int i = 0; i < E; i++) { + int N; + scanf("%d", &N); + for (int j = 1; j <= N; j++) { + int u; + scanf("%d", &u); + adj[i].push_back(u); + } + } + int n; + scanf("%d", &n); + for (int i = 1; i <= n; i++) { + int s; + scanf("%d", &s); + mem(); + flag = 0; + int M = BFS(s); + if (M == 0) { + printf("0\n"); + } else { + printf("%d %d\n", M, ans); + } + } + for (int i = 0; i < MAX; i++) { + adj[i].clear(); + } + } + return 0; +} + diff --git a/uva_cpp_clean/924/924-15.cpp b/uva_cpp_clean/924/924-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c4f0f3759cf32efd735f9c624fdf914fd1a57f5 --- /dev/null +++ b/uva_cpp_clean/924/924-15.cpp @@ -0,0 +1,68 @@ +/* + BFS + 時間複雜度: O(N) +*/ +#include +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_employee, n_source, source, n_friend, friends, n_notice, day_now; + queue> spreading; + + while (cin >> n_employee) { + vector> adjadency_list(n_employee); + + for (int i = 0; i < n_employee; i++) { + cin >> n_friend; + while (n_friend--) { + cin >> friends; + adjadency_list[i].emplace_back(friends); + } + } + + cin >> n_source; + while (n_source--) { + cin >> source; + + if (adjadency_list[source].empty()) + cout << "0\n"; + else { + vector unvisited(n_employee, true); + vector noticed; + noticed.reserve(n_employee); + + day_now = n_notice = 0; + unvisited[source] = false; + spreading.push(make_pair(source, 0)); + + while (!spreading.empty()) { + pair& now = spreading.front(); + spreading.pop(); + + if (day_now == now.second) { + noticed.emplace_back(n_notice); + n_notice = 0; + day_now++; + } + + for (const int& n : adjadency_list[now.first]) + if (unvisited[n]) { + unvisited[n] = false; + spreading.push(make_pair(n, day_now)); + n_notice++; + } + } + + vector::iterator&& it = max_element(noticed.begin(), noticed.end()); + cout << *it << " " << distance(noticed.begin(), it) << "\n"; + } + } + } +} \ No newline at end of file diff --git a/uva_cpp_clean/924/924-19.cpp b/uva_cpp_clean/924/924-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2af34ddde876e6579c47a0f93373582dfed585ac --- /dev/null +++ b/uva_cpp_clean/924/924-19.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vi; + +ii proc(int s, vector & graph) { + unordered_set match; + queue q; + q.push(make_pair(s, 0)); + match.insert(s); + int cday = 0, ccount = 0; + int boomday = 1, boomsize = 1; + while (!q.empty()) { + for (int i = 0; i < graph[q.front().first].size(); i++) { + if (!match.count(graph[q.front().first][i])) q.push(make_pair(graph[q.front().first][i], q.front().second + 1)), match.insert(graph[q.front().first][i]); + } + if (cday != q.front().second) { + if (ccount > boomsize) boomsize = ccount, boomday = cday; + cday = q.front().second, ccount = 0; + } + ccount++; + q.pop(); + } + if (ccount > boomsize) boomsize = ccount, boomday = cday; + return make_pair(cday? boomday: -1, boomsize); +} + +int main() { + int E; + cin >> E; + vector graph(E, vi()); + for (int i = 0; i < E; i++) { + int N; + cin >> N; + while (N--) { + int f; + cin >> f; + graph[i].push_back(f); + } + } + int T; + cin >> T; + while (T--) { + int s; + cin >> s; + ii r = proc(s, graph); + if (r.first >= 0) printf("%d %d\n", r.second, r.first); + else printf("0\n"); + } + return 0; +} diff --git a/uva_cpp_clean/924/924-21.cpp b/uva_cpp_clean/924/924-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b035f9221b4fdf5d16e55da061f9c6f74ece5046 --- /dev/null +++ b/uva_cpp_clean/924/924-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 924 + Name: Spreading The News + Problem: https://onlinejudge.org/external/9/924.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +bool seen[3000]; +int today[3000], tomorrow[3000]; +list adj[3000]; +int main(){ + int T, n, d, u, v; + scanf("%d", &n); + + for (int i=0; i mx) { + mx = c2; + mxi = d; + } + swap(today, tomorrow); + c1 = c2; + } + + printf("%d %d\n", mx, mxi); + } + } +} diff --git a/uva_cpp_clean/924/924-9.cpp b/uva_cpp_clean/924/924-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c88cf55c51d145c96649a2066c8c49fda8af6d6 --- /dev/null +++ b/uva_cpp_clean/924/924-9.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int n, u, v, q, _source, d[3000]; +vector G[30000]; +pair ans; + +pair bfs(int source){ + for(int i = 0; i < n; i++) d[i] = -1; + queue Q; + pair _max = make_pair(0, 0); + int ct = 0, pib = 0; + Q.push(source), d[source] = 0; + while(!Q.empty()){ + u = Q.front(), Q.pop(); + if(pib != d[u]){ + if(ct > _max.first) + _max = make_pair(ct,pib+1); + ct = 0; + } + pib = d[u]; + for(int i = 0; i < G[u].size(); i++){ + v = G[u][i]; + if(d[v] == -1) + d[v] = d[u] + 1, Q.push(v), ct++; + } + } + return _max; +} + +int main(){ + scanf("%d", &n); + for(int i = 0; i < n; i++){ + scanf("%d", &u); + for(int j = 0; j < u; j++) scanf("%d", &v), G[i].push_back(v); + } + scanf("%d", &q); + while(q--){ + scanf("%d", &_source); + if(G[_source].size() == 0) puts("0"); + else ans = bfs(_source), printf("%d %d\n", ans.first, ans.second); + } + return(0); +} diff --git a/uva_cpp_clean/925/925-21.cpp b/uva_cpp_clean/925/925-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39a8470acd82f28a975af70d2b1f6255afbfc5cc --- /dev/null +++ b/uva_cpp_clean/925/925-21.cpp @@ -0,0 +1,89 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 925 + Name: No more prerequisites, please! + Problem: https://onlinejudge.org/external/9/925.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +bool seen[143]; +bool R[143][143]; +string names[143]; +vector adj[143]; +bool dfs(int u, int dst) { + if (u == dst) + return true; + + seen[u] = 1; + for (int v: adj[u]) + if (!seen[v] && !R[u][v] && dfs(v, dst)) + return true; + + return false; +} + +bool comp(int u, int v) { return names[u] < names[v]; } + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + string s; + int T, n, m, c; + cin >> T; + while (T--) { + cin >> n; + for (int i=0; i> names[i]; + sort(names, names+n); + + unordered_map ids; + for (int i=0; i> m; + while (m--) { + cin >> s >> c; + int u = ids[s]; + while (c--) { + cin >> s; + adj[u].push_back(ids[s]); + } + } + + for (int u=0; u>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 926 + Name: Walking Around Wisely + Problem: https://onlinejudge.org/external/9/926.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + long long C[43][43]; + bool BR[43][43], BU[43][43]; + int T, n, m, si, sj, ei, ej; + cin >> T; + while (T--) { + cin >> n + >> sj>>si >> ej>>ei + >> m; + + for (int i=0; i<=n; ++i) + memset(BR[i], 0, n+1), + memset(BU[i], 0, n+1), + memset(C[i], 0, (n+1)*sizeof(long long)); + + for (int i=0, bi,bj; i> bj >> bi >> dir; + switch(dir) { + case 'E': BR[bi][bj] = 1; break; + case 'N': BU[bi][bj] = 1; break; + case 'W': BR[bi][bj-1] = 1; break; + case 'S': BU[bi-1][bj] = 1; break; + } + } + + C[si][sj] = 1; + for (int i=si; i<=ei; ++i) + for (int j=sj; j<=ej; ++j) { + if (!BR[i][j]) + C[i][j+1] += C[i][j]; + + if (!BU[i][j]) + C[i+1][j] += C[i][j]; + } + + cout << C[ei][ej] << endl; + } +} diff --git a/uva_cpp_clean/927/927-13.cpp b/uva_cpp_clean/927/927-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fd36b07e639bd813308df00faa82d9a7d1117c8 --- /dev/null +++ b/uva_cpp_clean/927/927-13.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include + +using namespace std; + +long long power(long long n, long long t) +{ + if (t == 0) + { + return 1; + } + + else if (t == 1) + { + return n; + } + + else + { + long long temp = power(n, t / 2); + + if (t % 2 == 0) + { + return temp * temp; + } + + else + { + return temp * temp * n; + } + } +} + +int main() +{ + int t; + + cin >> t; + + while (t--) + { + int p, d, k; + + cin >> p; + + int* f = new int[p + 1]; + + for (int i = 0; i < p + 1; i++) + { + cin >> f[i]; + } + + cin >> d >> k; + + int ind = 0; + + long long range = 0; + + while (range <= k) + { + ind++; + + range += (d * ind); + } + + range -= (d * ind); + + if (range == k) + { + ind--; + } + + vector a; + + for (int i = 1; i <= ind; i++) + { + long long temp = 0; + + for (int j = 0; j < p + 1; j++) + { + temp += (f[j] * power(i, j)); + } + + a.push_back(temp); + } + + cout << a[a.size() - 1] << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/927/927-19.cpp b/uva_cpp_clean/927/927-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afe141677d02a7ca174801559595f365eeb79781 --- /dev/null +++ b/uva_cpp_clean/927/927-19.cpp @@ -0,0 +1,31 @@ +#include +#include +using namespace std; + +inline int proc(int n) { + return ceil((sqrt(1 + 8*n) - 1) / 2); +} + +int main() { + int pol[25]; + int C; + cin >> C; + while (C--) { + int deg; + cin >> deg; + for (int i = 0; i <= deg; i++) { + cin >> pol[i]; + } + int d, k; + cin >> d >> k; + k--; + int n = proc(k/d + 1); + long long val = 0; + for (int i = deg; i >= 0; i--) { + val *= n; + val += pol[i]; + } + printf("%lld\n", val); + } + return 0; +} diff --git a/uva_cpp_clean/927/927-2.cpp b/uva_cpp_clean/927/927-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b83fe12a953df05739b4acc0fd710edd8e54312 --- /dev/null +++ b/uva_cpp_clean/927/927-2.cpp @@ -0,0 +1,51 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define F first +#define S second +#define PI 3.14159265358979323846 /* pi */ +#define FOR(i,a,b) for(int i = a; i <= b; ++i) +#define FORD(i,a,b) for(int i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define SQR(x) (x)*(x) +#define all(v) ((v).begin()),((v).end()) +#define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. +#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. +typedef long long ll; +typedef pair pii; +typedef pair pll; +typedef long double ld; +typedef vector vi; +typedef vector vll; +typedef vector vpii; +typedef vector vpll; +const double EPS = 1e-9; +const int N = 1e3+2, M = 3e5+5, OO = 0x3f3f3f3f; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +int main(){ + // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); + // freopen("input.txt", "r", stdin); + // freopen("output.txt", "w", stdout); + int tc; scanf("%d", &tc); + while(tc--) { + int n; scanf("%d", &n); + vector cof(n+1); + for(int& i: cof) scanf("%d", &i); + int d, k; scanf("%d", &d); scanf("%d", &k); + + int c = 1, t = 0; + // while (t < k) t += d*c++; + for(int i = 0; i < k;) i+=d*c++; + ll ans = 0ll; + for(int i = 0; i < cof.size(); ++i) { + ans += cof[i] * pow(c-1, i); + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/uva_cpp_clean/927/927-21.cpp b/uva_cpp_clean/927/927-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95c0a8d7ee027047af13daea344c06c6815a77d9 --- /dev/null +++ b/uva_cpp_clean/927/927-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 927 + Name: Integer Sequences from Addition of Terms + Problem: https://onlinejudge.org/external/9/927.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T, n, d, k, C[43]; + cin >> T; + while (T--) { + cin >> n; + for (int i=0; i<=n; ++i) + cin >> C[i]; + + cin >> d >> k; + int p = ceil((sqrt(1 + 8.0*k/d) - 1) / 2.0); + + long long r = C[n]; + for (int i=n-1; i>=0; --i) + r = r*p + C[i]; + + cout << r << endl; + } +} diff --git a/uva_cpp_clean/927/927-9.cpp b/uva_cpp_clean/927/927-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..200ad143ca3c006a71c84b5c9326fff8e7d73e62 --- /dev/null +++ b/uva_cpp_clean/927/927-9.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int n, k, aux, d; +long long ans, tmp; +vector coef; + +void f(int x){ + for(int i = 0; i < coef.size(); i++) + ans += coef[i] * pow(x,i); +} + +int main(){ + int c; + scanf("%d", &c); + while(c--){ + scanf("%d", &n); + for(int i = 0; i <= n; i++) + scanf("%d", &aux), coef.push_back(aux); + scanf("%d", &d); + scanf("%d", &k); + tmp = 0; + ans = 0; + for(int i = 1; ; i++){ + tmp += i * d; + if(tmp >= k){ + ans = 0; + f(i); + break; + } + } + cout << ans << endl; + coef.clear(); + } + return(0); +} diff --git a/uva_cpp_clean/928/928-21.cpp b/uva_cpp_clean/928/928-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49e8bc96d5b1c3e0d908162f2bcfcbf26b8c448e --- /dev/null +++ b/uva_cpp_clean/928/928-21.cpp @@ -0,0 +1,69 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 928 + Name: Eternal Truths + Problem: https://onlinejudge.org/external/9/928.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +struct State { + int i, j, s, d; + State(int i, int j, int s, int d):i(i),j(j),s(s),d(d){} +}; + +int di[] = {1, -1, 0, 0}, + dj[] = {0, 0, 1, -1}; +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + char M[313][313]; + bool S[313][313][3]; + int T, r, c, si, sj; + cin >> T; + while (T-- && cin >> r >> c) { + for (int i=0; i> M[i]; + for (int j=0; j q; + q.push(State(si, sj, 0, 0)); + S[si][sj][0] = 1; + while (!q.empty()) { + State s = q.front(); q.pop(); + if (M[s.i][s.j] == 'E') { + cout << s.d << '\n'; + goto fin; + } + + int len = s.s + 1; + for (int k=0; k<4; ++k) { + State t(s.i + len*di[k], s.j + len*dj[k], len%3, s.d+1); + if (t.i>=0 && t.i=0 && t.j +#include + +using namespace std; +using namespace __gnu_pbds; + +#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define inf INT_MAX + +typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set; +typedef long long ll; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + if (n <= 3) + return true; + + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +const int N= (int)1e4+9; + +int c,r; + +int dx[]={-1,1,0,0}; +int dy[]={0,0,-1,1}; + +bool valid(int a,int b) { + return (a>=0 && a=0 && b> r >> c; + + int grid[r + 2][c + 2]; + int dis[r + 2][c + 2]; + bool vis[r + 2][c + 2]; + + for(int i=0;i>grid[i][j]; + } + } + + for (int i = 0; i <= r; i++) { + for (int j = 0; j <= c; ++j) { + dis[i][j] = inf; + vis[i][j] = 0; + } + } + + + priority_queue> pq; + dis[0][0] = grid[0][0]; + pq.push({0, 0, 0}); + + while (!pq.empty()) { + int ro = get<1>(pq.top()); + int co = get<2>(pq.top()); + pq.pop(); + + if (vis[ro][co]) continue; + vis[ro][co] = 1; + + for (int i = 0; i < 4; i++) { + int n1 = ro + dx[i]; + int n2 = co + dy[i]; + if (valid(n1, n2)) { + + if (dis[ro][co] + grid[n1][n2] < dis[n1][n2]) { + dis[n1][n2] = dis[ro][co] + grid[n1][n2]; + pq.push({-1 * dis[n1][n2], n1, n2}); + } + + } + } + + } + + cout << dis[r-1][c-1] << "\n"; + + +} + +int main() { + FAST + + int t = 1; + cin>>t; + while (t--) { + solve(); + } + +} + diff --git a/uva_cpp_clean/929/929-13.cpp b/uva_cpp_clean/929/929-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0d758ddb28df60a7f336cc44983a79bf2f347ba --- /dev/null +++ b/uva_cpp_clean/929/929-13.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define MAX 1005 + +int n, m, t; +int dis[MAX][MAX]; +int maze[MAX][MAX]; + +char disX[4] = { 0, 1, 0, -1 }; +char disY[4] = { 1, 0, -1, 0 }; + +class comparator +{ +public: + + bool operator()(pair, int> &child1, pair, int> &child2) + { + return child1.second > child2.second; + } +}; + +void initialize() +{ + for (int i = 0; i < MAX; i++) + { + for (int j = 0; j < MAX; j++) + { + dis[i][j] = INT_MAX; + + maze[i][j] = INT_MAX; + } + } +} + +int travel() +{ + dis[1][1] = 0; + + priority_queue < pair, int>, vector, int>>, comparator>pq; + + pq.push(make_pair(make_pair(1, 1), dis[1][1])); + + while (!pq.empty()) + { + pair, int> head = pq.top(); + + pq.pop(); + + int i = head.first.first; + int j = head.first.second; + + int headDis = head.second; + + for (int k = 0; k < 4; k++) + { + int x = i + disX[k]; + int y = j + disY[k]; + + if (maze[x][y] != INT_MAX) + { + if (maze[x][y] + headDis < dis[x][y]) + { + dis[x][y] = headDis + maze[x][y]; + + pq.push(make_pair(make_pair(x, y), dis[x][y])); + } + } + } + } + + return dis[n][m] + maze[1][1]; +} + +int main() +{ + cin >> t; + + while (t--) + { + cin >> n >> m; + + initialize(); + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + cin >> maze[i][j]; + } + } + + cout << travel() << endl; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/929/929-14.cpp b/uva_cpp_clean/929/929-14.cpp new file mode 100755 index 0000000000000000000000000000000000000000..995193db58a0d6afe4ec510f19d9d945afdaf3cc --- /dev/null +++ b/uva_cpp_clean/929/929-14.cpp @@ -0,0 +1,104 @@ +/*************************************************** + * Problem name : 929 Number Maze.cpp + * Problem Link : https://uva.onlinejudge.org/external/9/929.pdf + * OJ : Uva + * Verdict : AC + * Date : 12.05.2017 + * Problem Type : Graph(dijkstra) + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define SET_MN(a) memset(a,0,sizeof(a)) +#define SET_MX(a) memset(a,1000000,sizeof(a)) +#define pii pair +#define MAX 1000 +#define nl printf("\n") +#define debug printf("#########\n") +using namespace std; +typedef long long ll; +int fx[] = {1, -1, 0, 0}; +int fy[] = {0, 0, 1, -1}; +int ar[MAX][MAX], cost[MAX][MAX]; +int M, N; +int isValid(int xx, int yy) { + if (xx >= 0 && yy >= 0 && xx < N && yy < M) return 1; + else return 0; +} +struct node { + int xxx, yyy, w; + node(int a, int b, int c) { + xxx = a; + yyy = b; + w = c; + } + //~ bool operator < ( const node &p ) const { + //~ return w > p.w; + //~ } +}; +bool operator <(node a , node b) { + return a.w > b.w; +} +int dijkstra(int sx, int sy, int dx, int dy) { + priority_queueQ; + cost[sx][sy] = ar[sx][sy]; + Q.push(node(sx, sy, cost[sx][sy])); + while (!Q.empty()) { + node top = Q.top(); + Q.pop(); + int x = top.xxx; + int y = top.yyy; + //~ cout << x << " " << y; + //~ nl; + for (int i = 0; i < 4; i++) { + //~ debug; + int xx = x + fx[i]; + int yy = y + fy[i]; + if (isValid(xx, yy)) { + //~ debug; + if ((cost[x][y] + ar[xx][yy]) < cost[xx][yy]) { + cost[xx][yy] = cost[x][y] + ar[xx][yy]; + //~ if(dx == xx && dy == yy){ + //~ return cost[xx][yy]; + //~ } + Q.push(node(xx, yy, cost[xx][yy])); + } + } + } + } + return cost[dx][dy]; +} +int main () { + int tc; + scanf("%d", &tc); + for (int t = 1; t <= tc; t++) { + scanf("%d %d", &N, &M); + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + scanf("%d", &ar[i][j]); + } + } + if (N == 1 && M == 1) { + printf("%d\n", ar[0][0]); + } else { + int sx = 0, sy = 0; + int dx = N - 1, dy = M - 1; + SET_MX(cost); + //~ dijkstra(sx, sy); + printf("%d\n", dijkstra(sx, sy, dx, dy)); + } + } + return 0; +} + diff --git a/uva_cpp_clean/929/929-19.cpp b/uva_cpp_clean/929/929-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b501b6f34b19d4999bcca2bfd53bbc314fab659a --- /dev/null +++ b/uva_cpp_clean/929/929-19.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef pair ii; +typedef vector vii; +typedef vector vi; + +vector adjList; + +int dijkstra(int S, int T) { + vi dist(adjList.size(), INT_MAX); + dist[S] = 0; + priority_queue > pq; + pq.push(ii(0, S)); + while (!pq.empty()) { + ii front = pq.top(); pq.pop(); + int d = front.first, u = front.second; + if (d > dist[u]) continue; + for (int j = 0; j < adjList[u].size(); j++) { + ii v = adjList[u][j]; + if (dist[u] + v.second < dist[v.first]) { + dist[v.first] = dist[u] + v.second; + pq.push(ii(dist[v.first], v.first)); + } + } + } + return dist[T]; +} + +int main() { + int N; + cin >> N; + for (int i = 1; i <= N; i++) { + int n, m; + cin >> n >> m; + adjList.assign(n*m, vii()); + vi cells(n*m, 0); + for (int j = 0; j < n; j++) { + for (int k = 0; k < m; k++) { + cin >> cells[j*m + k]; + } + } + for (int j = 0; j < n; j++) { + for (int k = 0; k < m; k++) { + if (j-1 >= 0) adjList[j*m + k].push_back(make_pair((j-1)*m + k, cells[(j-1)*m + k])); + if (j+1 < n) adjList[j*m + k].push_back(make_pair((j+1)*m + k, cells[(j+1)*m + k])); + if (k-1 >= 0) adjList[j*m + k].push_back(make_pair(j*m + k-1, cells[j*m + k-1])); + if (k+1 < m) adjList[j*m + k].push_back(make_pair(j*m + k+1, cells[j*m + k+1])); + } + } + int d = dijkstra(0, n*m - 1); + printf("%d\n", d + cells[0]); + } + return 0; +} diff --git a/uva_cpp_clean/929/929-20.cpp b/uva_cpp_clean/929/929-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3c183af389d2a7cbd70fdc14d7eb3bf55c8b223 --- /dev/null +++ b/uva_cpp_clean/929/929-20.cpp @@ -0,0 +1,69 @@ +#include + +#define isValid( nx, ny, row, col ) ( (1 <= nx && nx <= row) && (1 <= ny && ny <= col) ) + +using namespace std; + +const int fx[] = { +1, -1, +0, +0 }; +const int fy[] = { +0, +0, +1, -1 }; +const int MAXN = 1000; +const int INF = (1 << 31) - 1; + +int graph[MAXN+2][MAXN+2]; +int d[MAXN+2][MAXN+2]; + +struct data { + int x, y, cst; + bool operator < (const data& t) const { + return (cst > t.cst); + } +} ; + +int dijkstra( int N, int M ) { + data now, next; + priority_queue < data > q; + + for( int i = 0 ; i <= MAXN ; ++i ) { + for( int j = 0 ; j <= MAXN ; ++j ) { + d[i][j] = INF; + } + } + + d[1][1] = graph[1][1]; + now.x = 1, now.y = 1, now.cst = graph[1][1]; + q.push( now ); + while( !q.empty() ) { + now = q.top(); + q.pop(); + + if( now.x == N && now.y == M ) return d[N][M]; + + for( int i = 0 ; i < 4 ; ++i ) { + next.x = now.x + fx[i]; + next.y = now.y + fy[i]; + + if( isValid( next.x, next.y, N, M ) && ( d[next.x][next.y] > graph[next.x][next.y] + d[now.x][now.y] ) ) { + d[next.x][next.y] = graph[next.x][next.y] + d[now.x][now.y]; + next.cst = d[next.x][next.y]; + q.push( next ); + } + } + } +} + +int main( int argc, char ** argv ) { + int tc; + scanf( "%d", &tc ); + for( int nCase = 1 ; nCase <= tc ; ++nCase ) { + int N, M; + scanf( "%d %d", &N, &M ); + for( int i = 1 ; i <= N ; ++i ) { + for( int j = 1 ; j <= M ; ++j ) { + scanf( "%d", &graph[i][j] ); + } + } + + printf("%d\n", dijkstra( N, M ) ); + } + return 0; +} diff --git a/uva_cpp_clean/929/929-21.cpp b/uva_cpp_clean/929/929-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfd9a98c3594a08e05563052c8ea40f3b3e8849e --- /dev/null +++ b/uva_cpp_clean/929/929-21.cpp @@ -0,0 +1,107 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 929 + Name: Number Maze + Problem: https://onlinejudge.org/external/9/929.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 1073741824 +using namespace std; + +inline int readchar() { + const int N = 4194304; + static char buf[N]; + static char *p = buf, *end = buf; + bool fin = 0; + if (fin) return EOF; + if (p == end) { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) { + fin = 1; + return EOF; + } + p = buf; + } + return *p++; +} + +inline bool isdigit(char ch) { return ch>='0' && ch<='9'; } +inline int readUInt() { + char ch; + unsigned int r=0; + while (!isdigit(ch=readchar())) + if (ch == EOF) return EOF; + r = ch-'0'; + while (isdigit(ch=readchar())) + r = (r<<3) + (r<<1) + ch-'0'; + + while (ch!=' ' && ch!='\n') + ch = readchar(); + return r; +} + +// ---------------------------------------------------------------- + +#define MAXN 1017 + +bool S[MAXN][MAXN]; +int D[MAXN][MAXN], + X[MAXN][MAXN], n, m, + dy[] = {0,0,1,-1}, + dx[] = {1,-1,0,0}; + +struct State { + int i, j, d; + State(int i, int j, int dd):i(i),j(j),d(-1){ + if (i>=0 && i=0 && j s.d; } + bool isvalid() { return d != -1; } +}; + + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int T = readUInt(); + while (T--) { + n = readUInt(); + m = readUInt(); + + for (int i=0; i q; + q.push(State(0,0,0)); + while (!q.empty()) { + State s = q.top(); q.pop(); + if (s.i==n-1 && s.j==m-1) { + cout << s.d << endl; + break; + } + + if (S[s.i][s.j]) continue; + S[s.i][s.j] = true; + + for (int k=0; k<4; ++k) { + State t(s.i+dy[k], s.j+dx[k], s.d); + if (t.isvalid()) + q.push(t); + } + } + } +} diff --git a/uva_cpp_clean/929/929-9.cpp b/uva_cpp_clean/929/929-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f8511a7e210627c77b842aebae8c76c319e76b1 --- /dev/null +++ b/uva_cpp_clean/929/929-9.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +const int SIZE = 1010, INF = 1000000000; + +typedef pair ii; + +int tc, N, M, W[SIZE][SIZE], dis[SIZE * SIZE]; +int dr[] = {0, -1, 0, 1}; +int dc[] = {1, 0, -1, 0}; + +inline bool isValid(int R, int C){ + return ( 0 <= R and 0 <= C and R < N and C < M ); +} + +int Dijkstra(int source, int target){ + fill(dis, dis + N * M + 1, INF); + dis[source] = W[0][0]; + set < ii > st; + st.insert( ii(0, source) ); + while( not st.empty() ){ + int u = st.begin() -> second, ru = u / M, cu = u % M; + st.erase( st.begin() ); + if( u == target ) return dis[u]; + for(int d = 0; d < 4; d++){ + int rv = ru + dr[d], cv = cu + dc[d], v = rv * M + cv, w = W[rv][cv]; + if( isValid(rv, cv) and dis[v] > dis[u] + w ) + dis[v] = dis[u] + w, st.insert( ii(dis[v], v) ); + } + } +} + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d %d", &N, &M); + for(int i = 0; i < N; i++) + for(int j = 0; j < M; j++) + scanf("%d", &W[i][j]); + printf("%d\n", Dijkstra(0, N * M - 1)); + } + return(0); +} diff --git a/uva_cpp_clean/939/939-21.cpp b/uva_cpp_clean/939/939-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24d967b7c48f116e9e03daf067b3d4d25504eacf --- /dev/null +++ b/uva_cpp_clean/939/939-21.cpp @@ -0,0 +1,70 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 939 + Name: Genes + Problem: https://onlinejudge.org/external/9/939.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +using namespace std; + +#define MAXN 3117 +int states[MAXN], par1[MAXN], par2[MAXN]; +char stateNames[3][50] = { + "non-existent", + "recessive", + "dominant" +}; + +int lastId = 0; +string names[MAXN]; +map ids; +int readId() { + cin>>names[lastId]; + auto it = ids.find(names[lastId]); + if (it == ids.end()) { + par1[lastId] = par2[lastId] = states[lastId] = -1; + ids[names[lastId]] = lastId; + return lastId++; + } + return it->second; +} + +int newStates[] = { 0, 0, 1, 2, 2 }; +int getState(int id) { + if (states[id] != -1) + return states[id]; + + return states[id] = newStates[getState(par1[id]) + getState(par2[id])]; +} + +int main(){ + ids["dominant"] = -1; + ids["recessive"] = -2; + ids["non-existent"] = -3; + + int n; + scanf("%d", &n); + while (n--) { + int x = readId(), + y = readId(); + + if (y < 0) + states[x] = y+3; + else if (par1[y]==-1) + par1[y] = x; + else par2[y] = x; + } + + for (auto p: ids) + if (p.second>=0) + cout << p.first << ' ' << stateNames[getState(p.second)] << endl; +} diff --git a/uva_cpp_clean/941/941-19.cpp b/uva_cpp_clean/941/941-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd03ed14bc639a4abe546170b9aa8da8929363ac --- /dev/null +++ b/uva_cpp_clean/941/941-19.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +using namespace std; + +int main() { + unsigned long long fact[21], k; + fact[0] = 1; + for (int i = 1; i < 21; i++) fact[i] = fact[i-1]*i; + char p[21]; + bool mask[21]; + queue q; + int T; + string s; + cin >> T; + while (T--) { + cin >> s >> k; + for (int i = 0; i < s.length(); i++) p[i] = s[i], mask[i] = false; + sort(p, p + s.length()); + for (int i = 0; i < s.length(); i++) { + int j, f, last; + for (j = 0, f = 1, last = 0; j < s.length(); j++) { + if (!mask[j]) { + last = j; + if (fact[s.length()-(i+1)]*f > k) break; + f++; + } + } + mask[last] = true; + q.push(p[last]); + k -= fact[s.length()-(i+1)]*(f-1); + } + for (int i = 0; i < s.length(); i++) printf("%c", q.front()), q.pop(); + printf("\n"); + } + return 0; +} diff --git a/uva_cpp_clean/941/941-21.cpp b/uva_cpp_clean/941/941-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08cab08854a15df035bdee6f9cf5e84d04ab278e --- /dev/null +++ b/uva_cpp_clean/941/941-21.cpp @@ -0,0 +1,80 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 941 + Name: Permutations + Problem: https://onlinejudge.org/external/9/941.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +#include +#include +using namespace std; + + +struct Char { + char ch; int cnt; + Char(char ch, int cnt):ch(ch),cnt(cnt) {} +}; + +list lsc; + +template +void printKth(Int k, Int sz, int len) { + char result[30]; + result[len] = 0; + + for (int i=0; icnt / (len-i); + if (k < sz2) { + result[i] = it->ch; + if (--it->cnt == 0) + lsc.erase(it); + + sz = sz2; + break; + } + else k -= sz2; + } + puts(result); +} + + +long long Facts[21]={1,1}; +int main(){ + for (int i=2; i<21; i++) + Facts[i] = Facts[i-1]*i; + + + int T; + long long k; + char str[30]; + scanf("%d", &T); + while (T--) { + scanf("%s %lld", str, &k); + int len = strlen(str); + sort(str, str+len); + + lsc.clear(); + long long sz = Facts[len]; + for (int i=0, cr=0; i(k, sz, len); + else printKth(k, sz, len); + } +} diff --git a/uva_cpp_clean/944/944-9.cpp b/uva_cpp_clean/944/944-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..090178a8d69d7844338207463eeda39cb08a670d --- /dev/null +++ b/uva_cpp_clean/944/944-9.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const int LIM = 100010; + +int num, ct, L, H, dp[LIM]; +bool first = true; + +int ssd(int n){ + int sum = 0; + while(n) sum += ( n % 10 ) * ( n % 10 ), n /= 10; + return sum; +} + +int main(){ + dp[1] = 1; + for(int it = 2; it < LIM; it++){ + num = it, ct = 0; + set vis; + while(true){ + if( dp[num] ){ + dp[it] = ct + dp[num]; + break; + } + if( vis.find( num ) != vis.end() ){ + break; + } + vis.insert(num); + num = ssd(num); + ct++; + } + } + while(~scanf("%d %d", &L, &H)){ + if( not first ) putchar('\n'); + first = false; + for(int it = L; it <= H; it++) + if( dp[it] ) printf("%d %d\n", it, dp[it]); + } + return(0); +} diff --git a/uva_cpp_clean/947/947-9.cpp b/uva_cpp_clean/947/947-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..379d877f289eb596b76463ff219db65181f6f9ae --- /dev/null +++ b/uva_cpp_clean/947/947-9.cpp @@ -0,0 +1,42 @@ +#include + +#define SIZE 10 + +using namespace std; + +int tc, cp, wp, len, lim, ans; +char guess[SIZE], v[SIZE]; +bool vis[SIZE]; + +bool check(int num){ + memset(vis, false, sizeof vis); + for(int it = len - 1; it >= 0; it--){ + v[it] = num % 10 + '0', num /= 10; + if( v[it] == '0' ) return false; + } + int a = 0, b = 0; + for(int it = 0; it < len; it++) + if( v[it] == guess[it] ) a++, vis[it] = true; + if( a != cp ) return false; + for(int it = 0; it < len; it++) + if( guess[it] != v[it] ){ + for(int i = 0; i < len; i++) + if( not vis[i] and v[i] == guess[it] ) + b++, vis[i] = true, i = len; + } + return ( b == wp ); +} + +int main(){ + scanf("%d\n", &tc); + while(tc--){ + scanf("%s %d %d\n", guess, &cp, &wp); + len = strlen(guess); + lim = exp10(len); + ans = 0; + for(int it = (lim - 1) / 9; it < lim; it++) + ans += check(it); + printf("%d\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/948/948-15.cpp b/uva_cpp_clean/948/948-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c45497203d67a68d89f82e790fdce7953ea65be --- /dev/null +++ b/uva_cpp_clean/948/948-15.cpp @@ -0,0 +1,36 @@ +/* + 費式進位法 + 時間複雜度: O(N) +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int i, n, n_case, Fibonacci[39] = {1, 2}; + + for (i = 2; i < 39; i++) + Fibonacci[i] = Fibonacci[i - 1] + Fibonacci[i - 2]; + + cin >> n_case; + while (n_case--) { + cin >> n; + cout << n << " = "; + + for (i = 38; Fibonacci[i] > n; i--) + ; + + while (i > -1) { + if (n < Fibonacci[i]) + cout << "0"; + else + cout << "1"; + n %= Fibonacci[i--]; + } + cout << " (fib)\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/948/948-20.cpp b/uva_cpp_clean/948/948-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ddf5d8cd7a387f336cdd81e0a6e34c44e2666b4 --- /dev/null +++ b/uva_cpp_clean/948/948-20.cpp @@ -0,0 +1,33 @@ +#include + +const int SZ = 40; +int fib[SZ], tc, sum; +bool flag; + +int main() +{ + fib[0]=0, fib[1]=1; + for(int i=2 ; i>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 948 + Name: Fibonaccimal Base + Problem: https://onlinejudge.org/external/9/948.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int Fib[43] = {1, 2}; +int main() { + for (int i=2; i<43; i++) + Fib[i] = Fib[i-1] + Fib[i-2]; + + int T, n; + scanf("%d", &T); + while (T--) { + scanf("%d", &n); + printf("%d = ", n); + + int lastI = -1; + for (int x=n; x; ) { + int ind = lower_bound(Fib, Fib+43, x) - Fib; + if (Fib[ind] > x) --ind; + for (int i=lastI-1; i>ind; i--) + putchar('0'); + putchar('1'); + x -= Fib[lastI = ind]; + } + for (; lastI>0; lastI--) putchar('0'); + puts(" (fib)"); + } +} diff --git a/uva_cpp_clean/948/948-9.cpp b/uva_cpp_clean/948/948-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81ec5974f172d32d8e3d87c4dcf56e1f8a9c3dbe --- /dev/null +++ b/uva_cpp_clean/948/948-9.cpp @@ -0,0 +1,109 @@ +#include + +#define LIMIT 50 + +using namespace std; + +int tc, ct, carry, l1, l2, minl, maxl, k, i, j, aux; +char fibo[LIMIT][LIMIT], ans[LIMIT], tmp[LIMIT], final_tmp[LIMIT], fibinary[LIMIT]; +bool ok; + +char* subt(char *s1,char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + carry = 0; + for(k=0, i = l1 - 1, j = l2 - 1; k < l2; k++, i--, j--){ + aux = (s1[i] - '0') - (s2[j] - '0') - carry; + if(aux >= 0) + tmp[k] = aux + '0', carry = 0; + else + tmp[k] = (aux + 10) + '0', carry = 1; + } + while(k < l1){ + aux = (s1[i--] - '0') - carry; + if(aux >= 0) + tmp[k++] = aux + '0', carry = 0; + else + tmp[k++] = aux + '0', carry = 1; + } + while(tmp[k - 1] == '0') + k--; + tmp[k] = '\0'; + i = 0; + while(k > 0) + final_tmp[i++] = tmp[--k]; + final_tmp[i] = '\0'; + return final_tmp; +} + +bool less_or_equal(char* s1, char* s2){ + l1 = strlen(s1), l2 = strlen(s2); + if(l1 < l2) + return true; + if(l1 > l2) + return false; + for(i = 0; i < l1; i++) + if(s1[i] < s2[i]) + return true; + else if(s1[i] > s2[i]) + return false; + return true; +} + +char* sum(char *s1, char *s2){ + l1 = strlen(s1), l2 = strlen(s2); + minl = l1 > l2 ? l2 : l1; + maxl = l1 > l2 ? l1 : l2; + carry = 0; + for(k = 0, i = l1 - 1, j = l2 - 1; k < minl; k++, i--, j--){ + aux = s1[i] - '0' + s2[j] - '0' + carry; + tmp[k] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l1){ + aux = s1[i--] - '0' + carry; + tmp[k++] = aux % 10 + '0'; + carry = aux / 10; + } + while(k < l2){ + aux = s2[j--] - '0' + carry; + tmp[k++] = aux % 10 + '0'; + carry = aux / 10; + } + if(carry) + tmp[maxl++] = carry + '0', tmp[maxl] = '0'; + else + tmp[maxl] = '\0'; + i = 0; + while(maxl > 0) + final_tmp[i++] = tmp[--maxl]; + final_tmp[i] = '\0'; + return final_tmp; +} + +void generateFibo(){ + fibo[0][0] = '0', fibo[0][1] = '\0'; + fibo[1][0] = '1', fibo[1][1] = '\0'; + for(int t = 2; t < LIMIT; t++) + strcpy(fibo[t], sum(fibo[t - 1], fibo[t - 2])); +} + +int main(){ + generateFibo(); + scanf("%d", &tc); + getchar(); + while(tc--){ + scanf("%s", fibinary); + getchar(); + ok = false; + ct = 0; + printf("%s = ", fibinary); + for(int x = LIMIT - 1; x >= 2; x--) + if(less_or_equal( fibo[x], fibinary)) + strcpy(fibinary, subt(fibinary, fibo[x])), ans[ct++] = '1', ok = true; + else if(ok) + ans[ct++] = '0'; + ans[ct] = '\0'; + printf("%s (fib)\n", ans); + } + return(0); +} diff --git a/uva_cpp_clean/949/949-21.cpp b/uva_cpp_clean/949/949-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c1adbd764da8a045f2c4aaec95eb155394fc03d --- /dev/null +++ b/uva_cpp_clean/949/949-21.cpp @@ -0,0 +1,105 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 949 + Name: Getaway + Problem: https://onlinejudge.org/external/9/949.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +struct State { + int x, y; + State(int x, int y):x(x),y(y){} +}; + +int dy[] = {0,0,1,-1}, + dx[] = {1,-1,0,0}; + +int fnd(int x, int y) { + for (int i=0; i<4; ++i) + if (dy[i]==y && dx[i]==x) + return i; + return -1; +} + +bool B[117][117][4], + S[117][117]; +int T[117][117][517], + D[117][117], + dn[117*117*500]; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + int nv, nh, c, x1, y1, x2, y2, tm; + for (int cse=1; cin>>nv>>nh; ++cse) { + memset(dn, 0, nv*nh*sizeof(int)); + for (int i=0; i> c; + for (int i=0; i> x1 >> y1 >> x2 >> y2; + int id = fnd(x2-x1, y2-y1); + if (id != -1) + B[x1][y1][id] = cse; + } + + cin >> c; + for (int i=0; i> tm >> x1 >> y1; + T[x1][y1][tm] = cse; + } + + queue q; + q.push(State(0,0)); + S[0][0] = dn[0] = 1; + for (tm=0; !q.empty();) { + if (!dn[tm]) ++tm; + + State s = q.front(); q.pop(); + if (D[s.x][s.y] > tm) q.push(s); + else { + if (s.x==nv-1 && s.y==nh-1) { + cout << D[s.x][s.y] << endl; + goto fin; + } + + --dn[tm]; + for (int i=0; i<4; ++i) + if (!B[s.x][s.y][i]) { + State t(s.x+dx[i], s.y+dy[i]); + if (t.x>=0 && t.y>=0 && t.x>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 957 + Name: Popes + Problem: https://onlinejudge.org/external/9/957.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int X[100001]; +int main(){ + int n, y, si, bsi, bei; + while (cin>>y>>n) { + cin>>X[0]; + si=bsi=bei=0; + for (int ei=1; ei>X[ei]; + + while (X[ei] - X[si] >= y) + si++; + + if (ei-si > bei-bsi) { + bsi = si; + bei = ei; + } + } + printf("%d %d %d\n", bei-bsi+1, X[bsi], X[bei]); + } +} diff --git a/uva_cpp_clean/957/957-9.cpp b/uva_cpp_clean/957/957-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09b08310990df15a2642acec45014cbc5cc1e7c7 --- /dev/null +++ b/uva_cpp_clean/957/957-9.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int p, n, ans1, ans2, largest; +vector v; + +int main(){ + while(scanf("%d\n%d", &p, &n) == 2){ + v.resize(n); + largest = 0; + for(int i = 0; i < n; i++) + scanf("%d", &v[i]); + for(int l = 0, r; l < n; l++){ + if(v[n - 1] - v[l] < p) r = n - 1; + else r = lower_bound(v.begin(), v.end(), v[l] + p - 1) - v.begin(); + while(v[r] - v[l] >= p) r--; + while(r + 1 < n && v[r + 1] == v[r]) r++; + if(v[r] - v[l] < p && r - l + 1 > largest) largest = r - l + 1, ans1 = v[l], ans2 = v[r]; + } + printf("%d %d %d\n", largest, ans1, ans2); + } + return(0); +} diff --git a/uva_cpp_clean/960/960-9.cpp b/uva_cpp_clean/960/960-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7f8746e588a471551a7fbda438e9796f3bb98d6 --- /dev/null +++ b/uva_cpp_clean/960/960-9.cpp @@ -0,0 +1,46 @@ +#include + +#define LIM (long long)1e4 + +using namespace std; + +int tc; +bitset prime; +long long a, b; +vector primes; +bool ok; + +void sieve(){ + prime[0] = prime[1] = 1; + for(long long i = 2; i <= LIM; i++){ + if( !prime[i] ){ + for(long long j = i * i; j <= LIM; j += i) + prime[j] = 1; + primes.push_back(i); + } + } +} + +bool isPrime(long long N){ + if(N <= LIM) return !prime[N]; + for(int i = 0; i < (int)primes.size(); i++) + if(N % primes[i] == 0) + return false; + return true; +} + +int main(){ + sieve(); + scanf("%d", &tc); + while(tc--){ + ok = false; + scanf("%lld %lld", &a, &b); + a = abs(a), b = abs(b); + if(isPrime(a * a + b * b)) ok = true; + else if(a == 0 && isPrime(b) && b % 4 == 3) ok = true; + else if(b == 0 && isPrime(a) && a % 4 == 3) ok = true; + printf( ok ? "P\n" : "C\n"); + } + return(0); +} + diff --git a/uva_cpp_clean/962/962-19.cpp b/uva_cpp_clean/962/962-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f4aa02228a0ecf2bb12d8fdcf9422ff24f14450 --- /dev/null +++ b/uva_cpp_clean/962/962-19.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; + +int main() { + set s; + unordered_map m; + for (long long i = 1; i <= 1001; i++) { + for (long long j = i; j <= 1001; j++) { + long long val = (i*i*i + j*j*j); + m[val]++; + if (m[val] > 1) s.insert(val); + } + } + long long n1, rg; + while (cin >> n1 >> rg) { + auto it = s.lower_bound(n1); + bool ok = false; + while (it != s.end()) { + if (*it > n1 + rg) break; + ok = true; + printf("%lld\n", *it); + ++it; + } + if (!ok) printf("None\n"); + } + return 0; +} diff --git a/uva_cpp_clean/967/967-14.cpp b/uva_cpp_clean/967/967-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50e35eba4be0945a0201adc3311722b08b69cb68 --- /dev/null +++ b/uva_cpp_clean/967/967-14.cpp @@ -0,0 +1,158 @@ +/*************************************************** + * Problem Name : 967 - Circular.cpp + * Problem Link : https://uva.onlinejudge.org/external/9/967.pdf + * OJ : Uva + * Verdict : AC + * Date : 2019-04-02 + * Problem Type : Number Theory + * Author Name : Saikat Sharma + * University : CSE, MBSTU + ***************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) +#define __FileRead freopen ("input.txt", "r", stdin); +#define __FileWrite freopen ("output.txt", "w", stdout); +#define SET(a,v) memset(a,v,sizeof(a)) +#define pii pair +#define pll pair +#define debug cout <<"#########\n"; +#define nl cout << "\n"; +#define sp cout << " "; +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define si(n) scanf("%d", &n) +#define ss(n) scanf("%s", n) +#define pf(n) scanf("%d", n) +#define pfl(n) scanf("%lld", n) +#define all(v) v.begin(), v.end() +#define Pow2(x) ((x)*(x)) +#define Mod(x, m) ((((x) % (m)) + (m)) % (m)) +#define Max3(a, b, c) max(a, max(b, c)) +#define Min3(a, b, c) min(a, min(b, c)) +#define pb push_back +#define mk make_pair +#define MAX 1000005 +#define INF 1000000000 +#define MOD 1000000007 +using namespace std; +typedef long long ll; +typedef unsigned long long ull; + +template +std::string NumberToString ( T Number ) { + std::ostringstream ss; + ss << Number; + return ss.str(); +} +ll lcm (ll a, ll b) { + return a * b / __gcd (a, b); +} +/************************************ Code Start Here ******************************************************/ +vectorprime; +char isPrime[MAX]; +int sum[MAX]; +void seivePrime (int n) { + isPrime[0] = 1, isPrime[1] = 1; + + for (int i = 4; i <= n; i += 2) { + isPrime[i] = 1; + } + + int sq = sqrt (n); + + for (int i = 3; i <= sq; i += 2) { + if (isPrime[i] == 0) { + for (int j = i * i ; j <= n; j += i) { + isPrime[j] = 1; + } + } + } +} +int stringToNum (string str) { + int num = 0; + int sz = str.size(); + + for (int i = 0; i < sz; i++) { + num = (num * 10) + (str[i] - '0'); + } + + return num; +} +string getNum (string n) { + string str; + + for (int i = 1; i < (int) n.size(); i++) { + str.pb (n[i]); + } + + str.pb (n[0]); + return str; +} + +bool isCurcular (int n) { + string s = NumberToString (n); + + if (isPrime[n] == 1) return false; + + for (int i = 1; i < (int) s.size(); i++) { + s = getNum (s); + int num = stringToNum (s); + + if (isPrime[num] == 1) return false; + } + + return true; +} + +void preCal() { + sum[100] = 0; + + for (int i = 101; i < MAX; i++) { + if (isPrime[i] == 0 && isCurcular (i) ) { + sum[i] = sum[i - 1] + 1; + + } else { + sum[i] = sum[i - 1]; + } + } +} + +int main () { + __FastIO; + seivePrime (MAX - 2); + preCal(); + int l, r; + + while (cin >> l) { + if (l == -1) break; + + cin >> r; + int ans = sum[r] - sum[l - 1]; + + if (ans == 0) { + cout << "No Circular Primes.\n"; + + } else if (ans == 1) { + cout << "1 Circular Prime.\n"; + + } else { + cout << ans << " Circular Primes.\n"; + } + } + + return 0; +} diff --git a/uva_cpp_clean/967/967-15.cpp b/uva_cpp_clean/967/967-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e86634a7eaf97fbc8b9d2b17aabd1c3339210803 --- /dev/null +++ b/uva_cpp_clean/967/967-15.cpp @@ -0,0 +1,33 @@ +/* + 建表、二分搜 + 時間複雜度: O(1) +*/ +#include +#include +#include + +using namespace std; + +vector circular_prime{113, 131, 197, 199, 311, 337, 373, 719, 733, 919, 971, 991, 1193, 1931, 3119, 3779, 7793, 7937, 9311, 9377, 11939, 19391, 19937, 37199, 39119, 71993, 91193, 93719, 93911, 99371, 193939, 199933, 319993, 331999, 391939, 393919, 919393, 933199, 939193, 939391, 993319, 999331}; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int left, right; + + while (cin >> left >> right) { + // 二分搜找位置需要最多 6 * 2 = 12次 + vector::iterator&& lower = lower_bound(circular_prime.begin(), circular_prime.end(), left); + vector::iterator&& upper = lower_bound(circular_prime.begin(), circular_prime.end(), right + 1); + int&& ans = distance(lower, upper); + + if (ans == 0) + cout << "No Circular Primes.\n"; + else if (ans == 1) + cout << "1 Circular Prime.\n"; + else + cout << ans << " Circular Primes.\n"; + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/967/967-6.cpp b/uva_cpp_clean/967/967-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a821559fa0b278864df4ff8f614a626ae16aba6b --- /dev/null +++ b/uva_cpp_clean/967/967-6.cpp @@ -0,0 +1,35 @@ +/* + miscellaneous > two pointers + difficulty: medium + date: 30/Mar/2020 + problem: pope + by: @brpapa +*/ +#include +#include +using namespace std; + +int main() { + int Y, N; + while (cin >> Y >> N) { + vector A(N); for (int &a : A) cin >> a; + + int K = Y; + int k = 0; + + int l, r = 0; + + int ans = 0, ansLower, ansUpper; + for (l = 0; l < N; l++) { + if (l > 0) k--; + + // k: qte de papas em A[l:r-1] + while (r < N && A[l]+K-1 >= A[r]) k++, r++; + + if (k > ans) + ans = k, ansLower = A[l], ansUpper = A[r-1]; + } + cout << ans << " " << ansLower << " " << ansUpper << endl; + } + return 0; +} diff --git a/uva_cpp_clean/978/978-21.cpp b/uva_cpp_clean/978/978-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa1794affb689d738e75917ba7546017a30c3e11 --- /dev/null +++ b/uva_cpp_clean/978/978-21.cpp @@ -0,0 +1,63 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 978 + Name: Lemmings Battle! + Problem: https://onlinejudge.org/external/9/978.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +char sold[100001]; +int main(){ + int T, n, m, k, x; + scanf("%d", &T); + while (T--) { + scanf("%d%d%d", &k, &n, &m); + priority_queue qb, qg; + for (int i=0; i 0) + qg.push(+sold[i]); + else qb.push(-sold[i]); + } + + if (!qg.empty()) { + puts("green wins"); + for(; !qg.empty(); qg.pop()) + printf("%d\n", qg.top()); + } + + else if (!qb.empty()) { + puts("blue wins"); + for(; !qb.empty(); qb.pop()) + printf("%d\n", qb.top()); + } + + else puts("green and blue died"); + if (T) putchar('\n'); + } +} diff --git a/uva_cpp_clean/978/978-6.cc b/uva_cpp_clean/978/978-6.cc new file mode 100644 index 0000000000000000000000000000000000000000..66ac9c4e0d8571351191d2ac5fb332563eb5e763 --- /dev/null +++ b/uva_cpp_clean/978/978-6.cc @@ -0,0 +1,58 @@ +// http://uva.onlinejudge.org/external/9/978.pdf +#include +#include + +using namespace std; + +typedef priority_queue pqi; + +int main() { + int t; + cin >> t; + for (int i = 0; i < t; i++) { + int b, sg, sb; + cin >> b >> sg >> sb; + pqi x; + for (int j = 0; j < sg; j++) { + int a; + cin >> a; + x.push(a); + } + pqi y; + for (int j = 0; j < sb; j++) { + int a; + cin >> a; + y.push(a); + } + vector v(b); + while (!x.empty() && !y.empty()) { + int l = min((long unsigned)b, min(x.size(), y.size())); + for (int j = 0; j < l; j++) { + int a = x.top(); + x.pop(); + int b = y.top(); + y.pop(); + v[j] = a - b; + } + for (int j = 0; j < l; j++) { + if (v[j] > 0) x.push(v[j]); + else if (v[j] < 0) y.push(-v[j]); + } + } + if (x.empty() && y.empty()) cout << "green and blue died\n"; + else if (y.empty()) { + cout << "green wins\n"; + while (!x.empty()) { + cout << x.top() << endl; + x.pop(); + } + } else { + cout << "blue wins\n"; + while (!y.empty()) { + cout << y.top() << endl; + y.pop(); + } + } + if (i < t - 1) cout << endl; + } +} diff --git a/uva_cpp_clean/978/978-9.cpp b/uva_cpp_clean/978/978-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4c03fe78c88aebfc33622504cfae0e5fd07f0cd --- /dev/null +++ b/uva_cpp_clean/978/978-9.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int tc, B, SG, SB, num, a, b, ct; +multiset > ::iterator it; + +int main(){ + scanf("%d", &tc); + while(tc--){ + multiset > green, blue; + scanf("%d %d %d", &B, &SG, &SB); + for(int i = 0; i < SG; i++) scanf("%d", &num), green.insert(num); + for(int i = 0; i < SB; i++) scanf("%d", &num), blue.insert(num); + while(!green.empty() && !blue.empty()){ + vector next_green, next_blue; + ct = 0; + for(it = green.begin(); it != green.end() && ct != B && !blue.empty(); ++it, ct++){ + a = *it, b = *blue.begin(); + if(a > b) next_green.push_back(a - b); + else if(a < b) next_blue.push_back(b - a); + blue.erase(blue.begin()); + } + while(ct--) green.erase(green.begin()); + for(int i = 0; i < next_green.size(); i++) green.insert(next_green[i]); + for(int i = 0; i < next_blue.size(); i++) blue.insert(next_blue[i]); + } + if(!green.empty() && blue.empty()){ + puts("green wins"); + for(it = green.begin(); it != green.end(); ++it) printf("%d\n", *it); + } + else if(green.empty() && !blue.empty()){ + puts("blue wins"); + for(it = blue.begin(); it != blue.end(); ++it) printf("%d\n", *it); + } + else puts("green and blue died"); + if(tc) putchar('\n'); + } + return(0); +} diff --git a/uva_cpp_clean/983/983-2.cpp b/uva_cpp_clean/983/983-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..309f3f747a8eb4223cad6832c2431ddb8f6c401f --- /dev/null +++ b/uva_cpp_clean/983/983-2.cpp @@ -0,0 +1,62 @@ +#include "bits/stdc++.h" + +using namespace std; +#define el "\n" +#define FOR(i,a,b) for(long long i = a; i <= b; ++i) +#define FORD(i,a,b) for(long long i = a; i >= b; --i) +#define RI(i,n) FOR(i,1,n) +#define REP(i,n) FOR(i,0,(n)-1) +#define all(v) ((v).begin()),((v).end()) +typedef long long ll; +typedef vector vi; +typedef vector vll; +const double EPS = 1e-9; +const int OO = 0x3f3f3f3f; +const string ALPHA = "abcdefghijklmnopqrstuvwxyz"; +// int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; // 4 Direction +// int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; // 8 Direction +// int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; // Knight Direction +// int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; // Hexagonal Direction +ll A[1003][1003], S[1003][1003]; +int main() { + //freopen("input.txt", "r", stdin); + //freopen("output.txt", "w", stdout); + ios::sync_with_stdio(false); + cin.tie(0); + int n, m, t = 0; + while (cin >> n >> m) { + if (t++) printf("\n"); + ll k = n-m+1, su = 0; + + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + cin >> A[n-i-1][j]; + + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) { + if (i) A[i][j] += A[i-1][j]; + if (j) A[i][j] += A[i][j-1]; + if (i && j) A[i][j] -= A[i-1][j-1]; + } + + for (int i = 0; i < k; ++i) + for (int j = 0; j < k; ++j) { + S[i][j] = A[i+m-1][j+m-1]; + if (i) S[i][j] -= A[i-1][j+m-1]; + if (j) S[i][j] -= A[i+m-1][j-1]; + if (i && j) S[i][j] += A[i-1][j-1]; + } + + for (int i = 0; i < k; ++i) + for (int j = 0; j < k;++j) { + printf("%lld\n", S[k-i-1][j]); + su += S[i][j]; + } + + printf("%lld\n", su); + } + return 0; +} + +/* +*/ diff --git a/uva_cpp_clean/983/983-21.cpp b/uva_cpp_clean/983/983-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4d1e2f6184aa35dbf2d00717b8f06af15004c7e --- /dev/null +++ b/uva_cpp_clean/983/983-21.cpp @@ -0,0 +1,35 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 983 + Name: Localized Summing for Blurring + Problem: https://onlinejudge.org/external/9/983.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +int S[1001][1001], x; +int main(){ + int frst=1, n, m; + while (scanf("%d%d", &n, &m)==2) { + for (int i=1; i<=n; i++) + for (int j=1; j<=n; j++) { + scanf("%d", &x); + S[i][j] = x + S[i-1][j] + S[i][j-1] - S[i-1][j-1]; + } + + if (frst) frst = 0; + else putchar('\n'); + + long long totalSum = 0; + for (int i=0; i<=n-m; i++) + for (int j=0; j<=n-m; j++) { + printf("%d\n", x = S[i+m][j+m] - S[i][j+m] - S[i+m][j] + S[i][j]); + totalSum += x; + } + printf("%lld\n", totalSum); + } +} diff --git a/uva_cpp_clean/983/983-9.cpp b/uva_cpp_clean/983/983-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e31c4ec7e6004b8470728f40438cba9fff6c1da2 --- /dev/null +++ b/uva_cpp_clean/983/983-9.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int tc, n, m, sum, v[1010][1010]; +long long total_sum; + +int main(){ + while(scanf("%d %d", &n, &m) == 2){ + total_sum = 0; + if(tc++) putchar('\n'); + for(int i = 1; i <= n; i++) + for(int j = 1; j <= n; j++) scanf("%d", &v[i][j]), v[i][j] += v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1]; + for(int i = 1; i <= n - m + 1; i++) + for(int j = 1; j <= n - m + 1; j++){ + sum = v[i + m - 1][j + m - 1] - v[i + m - 1][j - 1] - v[i - 1][j + m - 1] + v[i - 1][j - 1]; + total_sum += (long long)sum; + printf("%d\n", sum); + } + printf("%lld\n", total_sum); + } + return(0); +} diff --git a/uva_cpp_clean/986/986-21.cpp b/uva_cpp_clean/986/986-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..194d9c328f2ef6d76a935e42021abbfbbf453e77 --- /dev/null +++ b/uva_cpp_clean/986/986-21.cpp @@ -0,0 +1,67 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 986 + Name: How Many? + Problem: https://onlinejudge.org/external/9/986.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int n,r,k; + +long long memo[44][44][22]; +long long f(int x,int y,int r){ + if(y<0)return 0; + if(x==2*n && y==0 && r==0)return 1; + if(x==2*n)return 0; + + long long &ans=memo[x][y][r]; + if(ans==-1){ + if(y==k-1) + if(r>0) + ans=f(x+2,y,r-1)+f(x+2,y+2,r)+f(x+1,y-1,r); + else + ans=f(x+2,y+2,r)+f(x+1,y-1,r); + else + ans=f(x+1,y+1,r)+f(x+1,y-1,r); + } + return ans; +} + +//------------------------------------------ +long long DP[44][44][22]; +long long dp(int x,int y,int r){ + if(y<0)return 0; + return DP[x][y][r]; +} +//------------------------------------------ + +int main(){ + int x,y,rp; + while(scanf("%d%d%d",&n,&r,&k)==3){ + //memset(memo,-1,sizeof(memo)); + //cout<=0;x--) + for(y=0;y<=n;y++) + for(rp=0;rp<=r;rp++) + if(y==k-1) + DP[x][y][rp]=(rp>0 ? dp(x+2,y,rp-1) : 0)+ + dp(x+2,y+2,rp) + + dp(x+1,y-1,rp); + else + DP[x][y][rp]=dp(x+1,y+1,rp)+ + dp(x+1,y-1,rp); + cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 988 + Name: Many Paths, One Destination + Problem: https://onlinejudge.org/external/9/988.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + vector adj[143]; + int n, C[143]; + bool frst = 1; + while (cin >> n) { + if (frst) frst = 0; + else cout << "\n"; + + for (int i=0, sz; i> sz; + adj[i].resize(sz); + for (int j=0; j> adj[i][j]; + + if (!sz) adj[i].push_back(n); + } + + C[0] = 1; C[n] = 0; + for (int i=0; i specials > directed acyclic graph (DAG) + difficulty: easy + date: 29/May/2020 + problem: counting paths from 0 to V-1 + by: @brpapa +*/ +#include +using namespace std; + +int V; +vector> adjList; +vector state; + +vector memo; +int dp(int u) { + if (u == V-1) return 1; + + int &ans = memo[u]; + if (ans != -1) return ans; + + ans = 0; + for (int v : adjList[u]) ans += dp(v); + return ans; +} + +int main() { + int t = 0; + while (cin >> V) { + if (t++ > 0) cout << endl; + V++; adjList.assign(V, vector()); + for (int u = 0; u < V-1; u++) { + int choices; cin >> choices; + if (choices == 0) adjList[u].push_back(V-1); + while (choices--) { + int v; cin >> v; + adjList[u].push_back(v); + } + } + + memo.assign(V, -1); + cout << dp(0) << endl; + } + return 0; +} diff --git a/uva_cpp_clean/989/989-21.cpp b/uva_cpp_clean/989/989-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb94c7625263c1a1b4a1695722e0e8de4b9c583c --- /dev/null +++ b/uva_cpp_clean/989/989-21.cpp @@ -0,0 +1,76 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 989 + Name: Su Doku + Problem: https://onlinejudge.org/external/9/989.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +using namespace std; + +int n,a[10][10]; +bool check(int i,int j,int x) +{ + int k,l; + for(k=0; k>a[i][j]; + + if(BT(0)) + for(i=0; i recursive backtracking > sudoku + difficulty: easy + date: 19/Mar/2020 + by: @brpapa +*/ +#include +#include +#define MAX 9 // max n^2 +using namespace std; + +int n; +int board[MAX][MAX]; +bool changeable[MAX][MAX]; // posição de board livre para alteração +bool hasSolution; + +bool usedOnRow[MAX][MAX+1]; // usedOnRow[r][num] = num foi usado na linha r? +bool usedOnCol[MAX][MAX+1]; // usedOnCol[c][num] = num foi usado na coluna c? +bool usedOnBox[MAX][MAX+1]; // usedOnBox[b][num] = num foi usado no bloco b? + +void markUsedAs(bool used, int r, int c, int num) { + usedOnRow[r][num] = used; + usedOnCol[c][num] = used; + int b = n*(r/n) + c/n; + usedOnBox[b][num] = used; +} + +void bt(int i) { + int r = i/(n*n), c = i%(n*n); + // define board[r][c] com algum num possível + + if (hasSolution) return; + + if (i == (n*n)*(n*n)) { + hasSolution = true; + for (int r = 0; r < n*n; r++) { + for (int c = 0; c < n*n-1; c++) + cout << board[r][c] << " "; + cout << board[r][n*n-1] << endl; + } + return; + } + + int b = n*(r/n) + c/n; // (r,c) está na box b + + if (!changeable[r][c]) bt(i+1); + else { + for (int num = 1; num <= n*n; num++) + if (!usedOnRow[r][num] && !usedOnCol[c][num] && !usedOnBox[b][num]) { + board[r][c] = num; + + markUsedAs(1, r, c, num); + bt(i+1); + markUsedAs(0, r, c, num); + } + } +} + +int main() { + int t = 0; + while (cin >> n) { + memset(usedOnRow, 0, sizeof usedOnRow); + memset(usedOnCol, 0, sizeof usedOnCol); + memset(usedOnBox, 0, sizeof usedOnBox); + hasSolution = false; + + for (int r = 0; r < n*n; r++) + for (int c = 0; c < n*n; c++) { + cin >> board[r][c]; + markUsedAs(1, r, c, board[r][c]); + changeable[r][c] = board[r][c] == 0; + } + + if (t++ > 0) cout << endl; + bt(0); + if (!hasSolution) cout << "NO SOLUTION" << endl; + } + return 0; +} diff --git a/uva_cpp_clean/989/989-9.cpp b/uva_cpp_clean/989/989-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27c442a4783e542e5000a5fe701e5cff31dd97d9 --- /dev/null +++ b/uva_cpp_clean/989/989-9.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +const int MAX_N_N = 10; + +int n, n_n, filled, ones, mat[MAX_N_N][MAX_N_N], row[MAX_N_N], col[MAX_N_N], sq[MAX_N_N]; +bool found, first = true; + +void print() { + if (not found) { + puts("NO SOLUTION"); + return; + } + for (int i = 0; i < n_n; i++) + for (int j = 0; j < n_n; j++) + printf(j == n_n - 1 ? "%d\n" : "%d ", mat[i][j]); +} + +void backtrack(int r, int c, int res) { + if (res == 0) found = true; + if (r == n_n or found) return; + int p = (r / n) * n + c / n; + int pos = ones bitand (compl (row[r] bitor col[c] bitor sq[p])); + if (mat[r][c] == 0) { + for (int num = 1; num <= n_n and not found; num++) + if((pos >> num) & 1) { + int bin_num = pos & -pos; + mat[r][c] = num; + row[r] |= bin_num, col[c] |= bin_num, sq[p] |= bin_num; + backtrack(c + 1 == n_n ? r + 1 : r, c + 1 == n_n ? 0 : c + 1, res - 1); + if (not found) mat[r][c] = 0; + row[r] ^= bin_num; col[c] ^= bin_num, sq[p] ^= bin_num; + pos -= bin_num; + } + } + else backtrack(c + 1 == n_n ? r + 1 : r, c + 1 == n_n ? 0 : c + 1, res); +} + +void read() { + for (int i = 0; i < n_n; i++) row[i] = col[i] = sq[i] = 1; + for (int i = 0; i < n_n; i++) + for (int j = 0; j < n_n; j++) { + scanf("%d", &mat[i][j]); + if (mat[i][j]) { + filled++; + row[i] |= (1 << mat[i][j]); + col[j] |= (1 << mat[i][j]); + sq[(i / n) * n + j / n] |= (1 << mat[i][j]); + } + } +} + +int main() { + while (scanf("%d", &n) == 1) { + if (not first) putchar('\n'); + first = false; + n_n = n * n; + filled = 0; + found = false; + ones = (1 << (n_n + 1)) - 1; + read(); + backtrack(0, 0, n_n * n_n - filled); + print(); + } + return (0); +} diff --git a/uva_cpp_clean/990/990-12.cpp b/uva_cpp_clean/990/990-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11d63175dc6995c693da06b244b448e0332e279a --- /dev/null +++ b/uva_cpp_clean/990/990-12.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) +#define PI(a,b) pair +#define MP make_pair +#define EB emplace_back +#define MOD 1000000007 +#define int long long +#define S second +#define F first +#define endl '\n' +#define FOR(var,len) for(var = 0;var>t; + + + while(true){ + cin>>w; + cin>>n; + int D[n],V[n]; + memset(dp,0,sizeof(dp)); + + for(i = 0;i>D[i]; + + cin>>V[i]; + } + for(i = 1;i<=n;i++) + for(j = 1;j<=t;++j) + { + if(3*w*D[i-1]>j) + {dp[i][j] = dp[i-1][j]; + } + else{ + dp[i][j] = max(dp[i-1][j],dp[i-1][j-3*w*D[i-1]]+V[i-1]); + + } + } + int t1 = t; + cout< s1; + int res = dp[n][t]; + for(i = n;i>0 and res>0 ;i--){ + if(res == dp[i-1][t1]) + continue; + else{ + s1.push(i-1); + res-= V[i-1]; + t1 -= 3*w*D[i-1]; + } + } + cout<>t)cout<>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 990 + Name: Diving for Gold + Problem: https://onlinejudge.org/external/9/990.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#define INF 67108864 +using namespace std; + +int cse, + n, t, w, d[43], v[43], + DP[43][1043], dpb[43][1043], + P[43][1043]; + +int dfs(int j, int r) { + if (r < 0) return -INF; + if (!r || j == n) return 0; + if (dpb[j][r] == cse) + return DP[j][r]; + + int s1 = dfs(j+1, r), + s2 = dfs(j+1, r-3*w*d[j]) + v[j]; + + dpb[j][r] = cse; + P[j][r] = s2 >= s1; + return DP[j][r] = max(s1, s2); +} + +int main() { + ios_base::sync_with_stdio(0);cin.tie(0); + + bool sel[43]; + for (cse=1; cin >> t >> w >> n; ++cse) { + if (cse > 1) cout << "\n"; + for (int i=0; i> d[i] >> v[i]; + + cout << dfs(0, t) << "\n"; + + int cnt = 0; + memset(sel, 0, n); + for (int j=0, r=t; j0; ++j) + if (P[j][r]) { + ++cnt; + sel[j] = 1; + r -= 3*w*d[j]; + } + + cout << cnt << "\n"; + for (int i=0; i 0-1 knapsack + difficulty: easy + date: 14/May/2020 + problem: with recovering + by: @brpapa +*/ +#include +using namespace std; +typedef pair ii; +const int INF = 1 << 30; +const int MAX_N = 33; +const int MAX_T = 1010; + +int T, W, N; +int d[MAX_N], v[MAX_N]; + +int memo[MAX_N][MAX_T]; +int dp(int n, int t) { + // tesouro atual n, t segundos sob a água já gastos + + if (t >= T) return -INF; + if (n == N) return 0; + + int &ans = memo[n][t]; + if (ans != -1) return ans; + + return ans = max( + v[n] + dp(n+1, t + 3*W*d[n]), + dp(n+1, t) + ); +} + +int main() { + int t = 0; + while (cin >> T >> W) { + if (t++ != 0) cout << endl; + cin >> N; for (int n = 0; n < N; n++) cin >> d[n] >> v[n]; + + memset(memo, -1, sizeof memo); + cout << dp(0, 0) << endl; + + vector recovered; + for (int n = 0, t = 0; n < N; n++) + if (v[n] + dp(n+1, t + 3*W*d[n]) >= dp(n+1, t)) { + recovered.push_back(ii(d[n], v[n])); + t += 3*W*d[n]; + } + cout << recovered.size() << endl; + for (ii rec : recovered) cout << rec.first << " " << rec.second << endl; + } + return 0; +} diff --git a/uva_cpp_clean/990/990-9.cpp b/uva_cpp_clean/990/990-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81d6089dcb4b311895ae366bde4afae5e0b1d350 --- /dev/null +++ b/uva_cpp_clean/990/990-9.cpp @@ -0,0 +1,43 @@ +#include + +#define MAX_N 40 +#define MAX_T 1010 + +using namespace std; + +int tc,T,w,n,depth[MAX_N],gold[MAX_N],W[MAX_N],dp[MAX_N][MAX_T]; +bool path[MAX_N][MAX_T]; + +void print_path(int i,int t,int ct){ + if(i==0){ + printf("%d\n",ct); + return; + } + if(path[i][t]){ + print_path(i-1,t-W[i],ct+1); + printf("%d %d\n",depth[i],gold[i]); + } + else{ + print_path(i-1,t,ct); + } +} + +int main(){ + while(scanf("%d %d %d",&T,&w,&n)==3){ + if(tc++) putchar('\n'); + memset(path,false,sizeof path); + for(int i=1;i<=n;i++) + scanf("%d %d",&depth[i],&gold[i]), W[i] = depth[i]*3*w; + for(int i=1;i<=n;i++) dp[i][0] = 0; + for(int t=1;t<=T;t++) dp[0][t] = 0; + for(int i=1;i<=n;i++) + for(int t=1;t<=T;t++){ + dp[i][t] = dp[i-1][t]; + if(t>W[i] && gold[i]+dp[i-1][t-W[i]]>dp[i-1][t]) + dp[i][t] = gold[i]+dp[i-1][t-W[i]], path[i][t] = true; + } + printf("%d\n",dp[n][T]); + print_path(n,T,0); + } + return(0); +} diff --git a/uva_cpp_clean/991/991-13.cpp b/uva_cpp_clean/991/991-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82f78548b0e071ea805421323583a822b1809fbf --- /dev/null +++ b/uva_cpp_clean/991/991-13.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +using namespace std; + +long long cal(long long n) +{ + long long Ck = 1, Cn; + + for (long long i = 1; i <= n; i++) + { + Cn = ((2 * (2 * i - 1)) / (i + 1)) * Ck; + + Ck = Cn; + } + + return Ck; +} + +int main() +{ + long long n; + + cin >> n; + + cout << cal(n) << " " << n + 1 << endl; +} diff --git a/uva_cpp_clean/991/991-15.cpp b/uva_cpp_clean/991/991-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8248ae66dc06926b0f0581a8ce4a3fdc80c834c --- /dev/null +++ b/uva_cpp_clean/991/991-15.cpp @@ -0,0 +1,21 @@ +/* + 卡塔蘭數、建表 + 時間複雜度: O(1) +*/ +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + bool flag = false; + int n, ans[11] = {1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796}; + while (cin >> n) { + if (flag) + cout << "\n"; + cout << ans[n] << "\n"; + flag = true; + } +} \ No newline at end of file diff --git a/uva_cpp_clean/991/991-19.cpp b/uva_cpp_clean/991/991-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8276222a2f8ed2c077b3e4b112d429cf31385e18 --- /dev/null +++ b/uva_cpp_clean/991/991-19.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main() { + int catalan[11]; + catalan[0] = 1; + for (int i = 1; i < 11; i++) + catalan[i] = (catalan[i-1]*2*i*(2*i - 1))/((i+1)*i); + int n; + bool first = false; + while (cin >> n) { + if (first) printf("\n"); + else first = true; + printf("%d\n", catalan[n]); + } + return 0; +} diff --git a/uva_cpp_clean/991/991-20.cpp b/uva_cpp_clean/991/991-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ca71482e2c13a44d87adb9a0147d5bcb2e790c5 --- /dev/null +++ b/uva_cpp_clean/991/991-20.cpp @@ -0,0 +1,20 @@ +#include +#include + +using namespace std; + +int handshake[] = {1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796}; + +int main() +{ + int n; + + bool new_line = false; + while( cin >> n ) + { + if( new_line ) cout << endl; + new_line = true; + cout << handshake[n] << endl; + } + return 0; +} diff --git a/uva_cpp_clean/991/991-21.cpp b/uva_cpp_clean/991/991-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e10eb54adabd38186d15c5e7b8b10223c2d3431 --- /dev/null +++ b/uva_cpp_clean/991/991-21.cpp @@ -0,0 +1,28 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 991 + Name: Safe Salutations + Problem: https://onlinejudge.org/external/9/991.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +using namespace std; + + +int main() { + int DP[13] = { 1 }; + for (int i=1; i<13; ++i) + DP[i] = (4*i-2)*DP[i-1]/(i+1); + + int frst = 1, n; + while (cin >> n) { + if (frst) frst = 0; + else cout << endl; + cout << DP[n] << endl; + } +} diff --git a/uva_cpp_clean/991/991-9.cpp b/uva_cpp_clean/991/991-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9b48c6e1c3c2dbca9ae223d255870b3695ae9be --- /dev/null +++ b/uva_cpp_clean/991/991-9.cpp @@ -0,0 +1,18 @@ +#include + +#define SIZE 20 + +using namespace std; + +int tc,n,catalan[SIZE]; + +int main(){ + catalan[0] = 1; + for(int it=1;it<12;it++) + catalan[it] = catalan[it-1]*(4*it-2)/(it+1); + while(~scanf("%d",&n)){ + if(tc++) putchar('\n'); + printf("%d\n",catalan[n]); + } + return(0); +} diff --git a/uva_cpp_clean/993/993-15.cpp b/uva_cpp_clean/993/993-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2928dacf050a2276da2fdc4bc873d0495c00c68 --- /dev/null +++ b/uva_cpp_clean/993/993-15.cpp @@ -0,0 +1,45 @@ +/* + 因數分解 +*/ +#include +#include +#include + +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + int n_case; + cin >> n_case; + while (n_case--) { + vector number; + int n, cnt = 0; + cin >> n; + if (n == 0) + cout << "0\n"; + else if (n == 1) + cout << "1\n"; + else { + // 這裡要由大到小做回來 + for (int i = 9; i > 1 && n != 1; i--) { + while (n % i == 0) { + number.push_back(i); + n /= i; + } + } + + sort(number.begin(), number.end()); + + if (n != 1) + cout << "-1\n"; + else { + for (const int& i : number) + cout << i; + cout << "\n"; + } + } + } + return 0; +} \ No newline at end of file diff --git a/uva_cpp_clean/993/993-19.cpp b/uva_cpp_clean/993/993-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..768c10ef1575dadbc0472f80222c3ecd29bf7cde --- /dev/null +++ b/uva_cpp_clean/993/993-19.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; + +int main() { + int T; + cin >> T; + while (T--) { + int N; + cin >> N; + if (N <= 1) printf("%d\n", N); + else { + int div = 9; + stack s; + while (div > 1 && N != 1) { + if (N % div == 0) s.push(div), N /= div; + else div --; + } + if (N == 1) { + while (!s.empty()) printf("%d", s.top()), s.pop(); + printf("\n"); + } else printf("-1\n"); + } + } + return 0; +} diff --git a/uva_cpp_clean/993/993-21.cpp b/uva_cpp_clean/993/993-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9dd023fc49800962c09a3d9b27393ce426bab501 --- /dev/null +++ b/uva_cpp_clean/993/993-21.cpp @@ -0,0 +1,39 @@ +/* + >>~~ UVa Online Judge ACM Problem Solution ~~<< + + ID: 993 + Name: Product of digits + Problem: https://onlinejudge.org/external/9/993.pdf + Language: C++ + + Author: Arash Shakery + Email: arash.shakery@gmail.com +*/ + +#include +#include +#include +using namespace std; + +int main(){ + long long n; + int T; + cin>>T; + while (T--) { + cin >> n; + if (n<10) cout << n << endl; + else { + vector result; + for (int i=9; i>=2; i--) + for (; n%i==0; n/=i) + result.push_back(i); + + if (n>1) puts("-1"); + else { + for (int i=result.size()-1; i>=0; i--) + cout << result[i]; + cout << endl; + } + } + } +} diff --git a/uva_cpp_clean/993/993-9.cpp b/uva_cpp_clean/993/993-9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7402841b3654cc37311fea5d7618a5b2947a91d --- /dev/null +++ b/uva_cpp_clean/993/993-9.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int tc, N; + +int main(){ + scanf("%d", &tc); + while(tc--){ + scanf("%d", &N); + if ( N == 0 ){ + puts("0"); + continue; + } + if ( N == 1 ){ + puts("1"); + continue; + } + vector Factors; + for(int it = 9; it > 1; it--) + while( N % it == 0 ) Factors.push_back( it ), N /= it; + if ( N != 1 ) puts("-1"); + else + for(int it = Factors.size() - 1; it >= 0; it--) + printf(it != 0 ? "%d" : "%d\n", Factors[it]); + } + return(0); +} diff --git a/uva_cpp_clean/997/997-15.cpp b/uva_cpp_clean/997/997-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..804993f60b557d36a88ff900923810b222f3764e --- /dev/null +++ b/uva_cpp_clean/997/997-15.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace std; + +int n; + +void recursion(const string& str, vector& sequence) { + int sign = 1, i, memory = 0, length = str.length(); + char operator_; + + for (i = 1; i < length; i++) + if (str[i] == '-') + sign = -1; + else if (str[i] >= '0' && str[i] <= '9') + memory = memory * 10 + str[i] - '0'; + else { + operator_ = str[i]; + break; + } + + memory *= sign; + + if (operator_ == '+') { + recursion(str.substr(i + 1), sequence); + int64_t prev = sequence[0]; + sequence[0] = memory; + for (int i = 1; i < n; i++) { + int64_t temp = sequence[i]; + sequence[i] = sequence[i - 1] + prev; + prev = temp; + } + } else if (operator_ == '*') { + recursion(str.substr(i + 1), sequence); + sequence[0] *= memory; + for (int i = 1; i < n; i++) + sequence[i] *= sequence[i - 1]; + } else + fill(sequence.begin(), sequence.end(), memory); +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(0); + + string str; + + while (cin >> str >> n) { + vector sequence(n); + recursion(str, sequence); + for (int i = 0; i < n; i++) + cout << sequence[i] << (i == n - 1 ? '\n' : ' '); + } + return 0; +} \ No newline at end of file